From cf9a616ec12e65c238124f97ded1de77f0b22ccc Mon Sep 17 00:00:00 2001 From: langhuihui <178529795@qq.com> Date: Thu, 27 Feb 2020 20:48:04 +0800 Subject: [PATCH] first commit --- LICENSE | 674 +++++++++ README.md | 3 + avformat/avpacket.go | 87 ++ avformat/codec.go | 308 ++++ avformat/flv.go | 115 ++ avformat/h264.go | 144 ++ avformat/mp4.go | 2421 +++++++++++++++++++++++++++++++ avformat/mpegts/mpegts.go | 578 ++++++++ avformat/mpegts/mpegts.md | 520 +++++++ avformat/mpegts/mpegts_crc32.go | 60 + avformat/mpegts/mpegts_pat.go | 230 +++ avformat/mpegts/mpegts_pes.go | 754 ++++++++++ avformat/mpegts/mpegts_pmt.go | 384 +++++ avformat/mpegts/mpegts_psi.go | 232 +++ avformat/sps.go | 215 +++ config.go | 34 + go.mod | 13 + go.sum | 41 + hook.go | 69 + index.go | 53 + logger.go | 32 + pool/slice_pool.go | 21 + publisher.go | 38 + room.go | 250 ++++ subscriber.go | 135 ++ summary.go | 142 ++ util/SSE.go | 73 + util/big_little_endian.go | 269 ++++ util/bits/bits.go | 118 ++ util/bits/bits_test.go | 51 + util/bits/bufio/bufio.go | 22 + util/bits/golomb_reader.go | 65 + util/bits/pio/pio.go | 3 + util/bits/pio/reader.go | 121 ++ util/bits/pio/vec.go | 68 + util/bits/pio/vec_test.go | 21 + util/bits/pio/writer.go | 87 ++ util/convert.go | 384 +++++ util/crc32.go | 126 ++ util/stderr.go | 20 + util/util.go | 40 + util/vecio.go | 205 +++ 42 files changed, 9226 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 avformat/avpacket.go create mode 100644 avformat/codec.go create mode 100644 avformat/flv.go create mode 100644 avformat/h264.go create mode 100644 avformat/mp4.go create mode 100644 avformat/mpegts/mpegts.go create mode 100644 avformat/mpegts/mpegts.md create mode 100644 avformat/mpegts/mpegts_crc32.go create mode 100644 avformat/mpegts/mpegts_pat.go create mode 100644 avformat/mpegts/mpegts_pes.go create mode 100644 avformat/mpegts/mpegts_pmt.go create mode 100644 avformat/mpegts/mpegts_psi.go create mode 100644 avformat/sps.go create mode 100644 config.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 hook.go create mode 100644 index.go create mode 100644 logger.go create mode 100644 pool/slice_pool.go create mode 100644 publisher.go create mode 100644 room.go create mode 100644 subscriber.go create mode 100644 summary.go create mode 100644 util/SSE.go create mode 100644 util/big_little_endian.go create mode 100644 util/bits/bits.go create mode 100644 util/bits/bits_test.go create mode 100644 util/bits/bufio/bufio.go create mode 100644 util/bits/golomb_reader.go create mode 100644 util/bits/pio/pio.go create mode 100644 util/bits/pio/reader.go create mode 100644 util/bits/pio/vec.go create mode 100644 util/bits/pio/vec_test.go create mode 100644 util/bits/pio/writer.go create mode 100644 util/convert.go create mode 100644 util/crc32.go create mode 100644 util/stderr.go create mode 100644 util/util.go create mode 100644 util/vecio.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e72bfdd --- /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 +. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea3b905 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Monibuca核心引擎 + +该项目为Monibuca的引擎部分,该部分逻辑是流媒体服务器的核心转发逻辑。仅包含最基础的功能,不含任何网络协议部分,但包含了一个插件的引入机制,其他功能均由插件实现 \ No newline at end of file diff --git a/avformat/avpacket.go b/avformat/avpacket.go new file mode 100644 index 0000000..33eb950 --- /dev/null +++ b/avformat/avpacket.go @@ -0,0 +1,87 @@ +package avformat + +import ( + "sync" + + "github.com/Monibuca/engine/pool" +) + +var ( + AVPacketPool = &sync.Pool{ + New: func() interface{} { + return new(AVPacket) + }, + } + SendPacketPool = &sync.Pool{ + New: func() interface{} { + return new(SendPacket) + }, + } +) + +// Video or Audio +type AVPacket struct { + Timestamp uint32 + Type byte //8 audio,9 video + IsAACSequence bool + IsADTS bool + // Video + VideoFrameType byte //4bit + IsAVCSequence bool + Payload []byte + RefCount int //Payload的引用次数 +} + +func (av *AVPacket) IsKeyFrame() bool { + return av.VideoFrameType == 1 || av.VideoFrameType == 4 +} +func (av *AVPacket) ADTS2ASC() (tagPacket *AVPacket) { + tagPacket = NewAVPacket(FLV_TAG_TYPE_AUDIO) + tagPacket.Payload = ADTSToAudioSpecificConfig(av.Payload) + tagPacket.IsAACSequence = true + ADTSLength := 7 + ((1 - int(av.Payload[1]&1)) << 1) + if len(av.Payload) > ADTSLength { + av.Payload[0] = 0xAF + av.Payload[1] = 0x01 //raw AAC + copy(av.Payload[2:], av.Payload[ADTSLength:]) + av.Payload = av.Payload[:(len(av.Payload) - ADTSLength + 2)] + } + return +} +func (av *AVPacket) Recycle() { + if av.RefCount == 0 { + return + } else if av.RefCount == 1 { + av.RefCount = 0 + pool.RecycleSlice(av.Payload) + AVPacketPool.Put(av) + } else { + av.RefCount-- + } +} +func NewAVPacket(avType byte) (p *AVPacket) { + p = AVPacketPool.Get().(*AVPacket) + p.Type = avType + p.IsAVCSequence = false + p.VideoFrameType = 0 + p.Timestamp = 0 + p.IsAACSequence = false + p.IsADTS = false + return +} + +type SendPacket struct { + Timestamp uint32 + Packet *AVPacket +} + +func (packet *SendPacket) Recycle() { + packet.Packet.Recycle() + SendPacketPool.Put(packet) +} +func NewSendPacket(p *AVPacket, timestamp uint32) (result *SendPacket) { + result = SendPacketPool.Get().(*SendPacket) + result.Packet = p + result.Timestamp = timestamp + return +} diff --git a/avformat/codec.go b/avformat/codec.go new file mode 100644 index 0000000..e055559 --- /dev/null +++ b/avformat/codec.go @@ -0,0 +1,308 @@ +package avformat + +import ( + "errors" + + "github.com/Monibuca/engine/util" +) + +const ( + ADTS_HEADER_SIZE = 7 +) + +// ISO/IEC 14496-15 11(16)/page +// +// Advanced Video Coding +// + +type AVCDecoderConfigurationRecord struct { + ConfigurationVersion byte // 8 bits Version + AVCProfileIndication byte // 8 bits + ProfileCompatibility byte // 8 bits + AVCLevelIndication byte // 8 bits + Reserved1 byte // 6 bits + LengthSizeMinusOne byte // 2 bits 非常重要,每个NALU包前面都(lengthSizeMinusOne & 3)+1个字节的NAL包长度描述 + Reserved2 byte // 3 bits + NumOfSequenceParameterSets byte // 5 bits SPS 的个数,计算方法是 numOfSequenceParameterSets & 0x1F + NumOfPictureParameterSets byte // 8 bits PPS 的个数 + + SequenceParameterSetLength uint16 // 16 byte SPS Length + SequenceParameterSetNALUnit []byte // n byte SPS + PictureParameterSetLength uint16 // 16 byte PPS Length + PictureParameterSetNALUnit []byte // n byte PPS +} + +//func (p *AVCDecoderConfigurationRecord) Marshal(b []byte) (n int) { +// b[0] = 1 +// b[1] = p.AVCProfileIndication +// b[2] = p.ProfileCompatibility +// b[3] = p.AVCLevelIndication +// b[4] = p.LengthSizeMinusOne | 0xfc +// b[5] = uint8(len(p.SPS)) | 0xe0 +// n += 6 +// +// for _, sps := range p.SPS { +// pio.PutU16BE(b[n:], uint16(len(sps))) +// n += 2 +// copy(b[n:], sps) +// n += len(sps) +// } +// +// b[n] = uint8(len(p.PPS)) +// n++ +// +// for _, pps := range p.PPS { +// pio.PutU16BE(b[n:], uint16(len(pps))) +// n += 2 +// copy(b[n:], pps) +// n += len(pps) +// } +// +// return +//} +var ErrDecconfInvalid = errors.New("decode error") + +func (p *AVCDecoderConfigurationRecord) Unmarshal(b []byte) (n int, err error) { + if len(b) < 7 { + err = errors.New("not enough len") + return + } + + p.AVCProfileIndication = b[1] + p.ProfileCompatibility = b[2] + p.AVCLevelIndication = b[3] + p.LengthSizeMinusOne = b[4] & 0x03 + spscount := int(b[5] & 0x1f) + n += 6 + var sps, pps [][]byte + for i := 0; i < spscount; i++ { + if len(b) < n+2 { + err = ErrDecconfInvalid + return + } + spslen := int(util.BigEndian.Uint16(b[n:])) + n += 2 + + if len(b) < n+spslen { + err = ErrDecconfInvalid + return + } + sps = append(sps, b[n:n+spslen]) + n += spslen + } + p.SequenceParameterSetLength = uint16(len(sps[0])) + p.SequenceParameterSetNALUnit = sps[0] + if len(b) < n+1 { + err = ErrDecconfInvalid + return + } + ppscount := int(b[n]) + n++ + + for i := 0; i < ppscount; i++ { + if len(b) < n+2 { + err = ErrDecconfInvalid + return + } + ppslen := int(util.BigEndian.Uint16(b[n:])) + n += 2 + + if len(b) < n+ppslen { + err = ErrDecconfInvalid + return + } + pps = append(pps, b[n:n+ppslen]) + n += ppslen + } + p.PictureParameterSetLength = uint16(len(pps[0])) + p.PictureParameterSetNALUnit = pps[0] + return +} + +// ISO/IEC 14496-3 38(52)/page +// +// Audio +// + +type AudioSpecificConfig struct { + AudioObjectType byte // 5 bits + SamplingFrequencyIndex byte // 4 bits + ChannelConfiguration byte // 4 bits + GASpecificConfig +} + +type GASpecificConfig struct { + FrameLengthFlag byte // 1 bit + DependsOnCoreCoder byte // 1 bit + ExtensionFlag byte // 1 bit +} + +// +// AudioObjectTypes -> ISO/IEC 14496-3 43(57)/page +// +// 1 AAC MAIN ISO/IEC 14496-3 subpart 4 +// 2 AAC LC ISO/IEC 14496-3 subpart 4 +// 3 AAC SSR ISO/IEC 14496-3 subpart 4 +// 4 AAC LTP ISO/IEC 14496-3 subpart 4 +// +// + +// ISO/IEC 13838-7 20(25)/page +// +// Advanced Audio Coding +// +// AudioDataTransportStream +type ADTS struct { + ADTSFixedHeader + ADTSVariableHeader +} + +// 28 bits +type ADTSFixedHeader struct { + SyncWord uint16 // 12 bits The bit string ‘1111 1111 1111’. See ISO/IEC 11172-3,subclause 2.4.2.3 (Table 8) + ID byte // 1 bit MPEG identifier, set to ‘1’. See ISO/IEC 11172-3,subclause 2.4.2.3 (Table 8) + Layer byte // 2 bits Indicates which layer is used. Set to ‘00’. See ISO/IEC 11172-3,subclause 2.4.2.3 (Table 8) + ProtectionAbsent byte // 1 bit Indicates whether error_check() data is present or not. Same assyntax element ‘protection_bit’ in ISO/IEC 11172-3,subclause 2.4.1 and 2.4.2 (Table 8) + Profile byte // 2 bits profile used. See clause 2 (Table 8) + SamplingFrequencyIndex byte // 4 bits indicates the sampling frequency used according to the followingtable (Table 8) + PrivateBit byte // 1 bit see ISO/IEC 11172-3, subclause 2.4.2.3 (Table 8) + ChannelConfiguration byte // 3 bits indicates the channel configuration used. Ifchannel_configuration is greater than 0, the channelconfiguration is given in Table 42, see subclause 8.5.3.1. Ifchannel_configuration equals 0, the channel configuration is notspecified in the header and must be given by aprogram_config_element() following as first syntactic element inthe first raw_data_block() after the header (seesubclause 8.5.3.2), or by the implicit configuration (seesubclause 8.5.3.3) or must be known in the application (Table 8) + OriginalCopy byte // 1 bit see ISO/IEC 11172-3, definition of data element copyright + Home byte // 1 bit see ISO/IEC 11172-3, definition of data element original/copy +} + +// SyncWord, 同步头 总是0xFFF, all bits must be 1,代表着一个ADTS帧的开始 +// ID, MPEG Version: 0 for MPEG-4, 1 for MPEG-2 +// Layer, always: '00' +// ProtectionAbsent, 表示是否误码校验 +// Profile, 表示使用哪个级别的AAC,有些芯片只支持AAC LC 。在MPEG-2 AAC中定义了3种. +// SamplingFrequencyIndex, 表示使用的采样率下标,通过这个下标在 Sampling Frequencies[ ]数组中查找得知采样率的值 +// PrivateBit, +// ChannelConfiguration, 表示声道数 +// OriginalCopy, +// Home, + +// Profile: +// +// 0: Main profile +// 1: Low Complexity profile(LC) +// 2: Scalable Sampling Rate profile(SSR) +// 3: Reserved +// +var SamplingFrequencies = [...]int{96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350} + +// Sampling Frequencies[]: +// +// 0: 96000 Hz +// 1: 88200 Hz +// 2: 64000 Hz +// 3: 48000 Hz +// 4: 44100 Hz +// 5: 32000 Hz +// 6: 24000 Hz +// 7: 22050 Hz +// 8: 16000 Hz +// 9: 12000 Hz +// 10: 11025 Hz +// 11: 8000 Hz +// 12: 7350 Hz +// 13: Reserved +// 14: Reserved +// 15: frequency is written explictly +// + +// ChannelConfiguration: +// +// 0: Defined in AOT Specifc Config +// 1: 1 channel: front-center +// 2: 2 channels: front-left, front-right +// 3: 3 channels: front-center, front-left, front-right +// 4: 4 channels: front-center, front-left, front-right, back-center +// 5: 5 channels: front-center, front-left, front-right, back-left, back-right +// 6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel +// 7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel +// 8-15: Reserved +// + +// 28 bits +type ADTSVariableHeader struct { + CopyrightIdentificationBit byte // 1 bit One bit of the 72-bit copyright identification field (seecopyright_id above). The bits of this field are transmitted frame by frame; the first bit is indicated by the copyright_identification_start bit set to ‘1’. The field consists of an 8-bit copyright_identifier, followed by a 64-bit copyright_number.The copyright identifier is given by a Registration Authority as designated by SC29. The copyright_number is a value which identifies uniquely the copyrighted material. See ISO/IEC 13818-3, subclause 2.5.2.13 (Table 9) + CopyrightIdentificationStart byte // 1 bit One bit to indicate that the copyright_identification_bit in this audio frame is the first bit of the 72-bit copyright identification. If no copyright identification is transmitted, this bit should be kept '0'.'0' no start of copyright identification in this audio frame '1' start of copyright identification in this audio frame See ISO/IEC 13818-3, subclause 2.5.2.13 (Table 9) + AACFrameLength uint16 // 13 bits Length of the frame including headers and error_check in bytes(Table 9) + ADTSBufferFullness uint16 // 11 bits state of the bit reservoir in the course of encoding the ADTS frame, up to and including the first raw_data_block() and the optionally following adts_raw_data_block_error_check(). It is transmitted as the number of available bits in the bit reservoir divided by NCC divided by 32 and truncated to an integer value (Table 9). A value of hexadecimal 7FF signals that the bitstream is a variable rate bitstream. In this case, buffer fullness is not applicable + NumberOfRawDataBlockInFrame byte // 2 bits Number of raw_data_block()’s that are multiplexed in the adts_frame() is equal to number_of_raw_data_blocks_in_frame + 1. The minimum value is 0 indicating 1 raw_data_block()(Table 9) +} + +// CopyrightIdentificationBit, +// CopyrightIdentificationStart, +// AACFrameLength, 一个ADTS帧的长度包括ADTS头和raw data block. +// ADTSBufferFullness, 0x7FF 说明是码率可变的码流. +// NumberOfRawDataBlockInFrame, 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧 + +// 所以说number_of_raw_data_blocks_in_frame == 0 表示说ADTS帧中有一个AAC数据块并不是说没有。(一个AAC原始帧包含一段时间内1024个采样及相关数据) +func ADTSToAudioSpecificConfig(data []byte) []byte { + profile := ((data[2] & 0xc0) >> 6) + 1 + sampleRate := (data[2] & 0x3c) >> 2 + channel := ((data[2] & 0x1) << 2) | ((data[3] & 0xc0) >> 6) + config1 := (profile << 3) | ((sampleRate & 0xe) >> 1) + config2 := ((sampleRate & 0x1) << 7) | (channel << 3) + return []byte{0xAF, 0x00, config1, config2} +} +func AudioSpecificConfigToADTS(asc AudioSpecificConfig, rawDataLength int) (adts ADTS, adtsByte []byte, err error) { + if asc.ChannelConfiguration > 8 || asc.FrameLengthFlag > 13 { + err = errors.New("Reserved field.") + return + } + + // ADTSFixedHeader + adts.SyncWord = 0xfff + adts.ID = 0 + adts.Layer = 0 + adts.ProtectionAbsent = 1 + + // SyncWord(12) + ID(1) + Layer(2) + ProtectionAbsent(1) + adtsByte = append(adtsByte, 0xff) + adtsByte = append(adtsByte, 0xf1) + + if asc.AudioObjectType >= 3 || asc.AudioObjectType == 0 { + adts.Profile = 1 + } else { + adts.Profile = asc.AudioObjectType - 1 + } + + adts.SamplingFrequencyIndex = asc.SamplingFrequencyIndex + adts.PrivateBit = 0 + adts.ChannelConfiguration = asc.ChannelConfiguration + adts.OriginalCopy = 0 + adts.Home = 0 + + // Profile(2) + SamplingFrequencyIndex(4) + PrivateBit(1) + ChannelConfiguration(3)(取高1位) + byte3 := uint8(adts.Profile<<6) + uint8(adts.SamplingFrequencyIndex<<2) + uint8(adts.PrivateBit<<1) + uint8((adts.ChannelConfiguration&0x7)>>2) + adtsByte = append(adtsByte, byte3) + + // ADTSVariableHeader + adts.CopyrightIdentificationBit = 0 + adts.CopyrightIdentificationStart = 0 + adts.AACFrameLength = 7 + uint16(rawDataLength) + adts.ADTSBufferFullness = 0x7ff + adts.NumberOfRawDataBlockInFrame = 0 + + // ChannelConfiguration(3)(取低2位) + OriginalCopy(1) + Home(1) + CopyrightIdentificationBit(1) + CopyrightIdentificationStart(1) + AACFrameLength(13)(取高2位) + byte4 := uint8((adts.ChannelConfiguration&0x3)<<6) + uint8((adts.AACFrameLength&0x1fff)>>11) + adtsByte = append(adtsByte, byte4) + + // AACFrameLength(13) + // xx xxxxxxxx xxx + // 取中间的部分 + byte5 := uint8(((adts.AACFrameLength & 0x1fff) >> 3) & 0x0ff) + adtsByte = append(adtsByte, byte5) + + // AACFrameLength(13)(取低3位) + ADTSBufferFullness(11)(取高5位) + byte6 := uint8((adts.AACFrameLength&0x0007)<<5) + 0x1f + adtsByte = append(adtsByte, byte6) + + // ADTSBufferFullness(11)(取低6位) + NumberOfRawDataBlockInFrame(2) + adtsByte = append(adtsByte, 0xfc) + + return +} diff --git a/avformat/flv.go b/avformat/flv.go new file mode 100644 index 0000000..3e523c3 --- /dev/null +++ b/avformat/flv.go @@ -0,0 +1,115 @@ +package avformat + +import ( + "io" + + "github.com/Monibuca/engine/pool" + "github.com/Monibuca/engine/util" +) + +const ( + // FLV Tag Type + FLV_TAG_TYPE_AUDIO = 0x08 + FLV_TAG_TYPE_VIDEO = 0x09 + FLV_TAG_TYPE_SCRIPT = 0x12 +) + +var ( + // 音频格式. 4 bit + SoundFormat = map[byte]string{ + 0: "Linear PCM, platform endian", + 1: "ADPCM", + 2: "MP3", + 3: "Linear PCM, little endian", + 4: "Nellymoser 16kHz mono", + 5: "Nellymoser 8kHz mono", + 6: "Nellymoser", + 7: "G.711 A-law logarithmic PCM", + 8: "G.711 mu-law logarithmic PCM", + 9: "reserved", + 10: "AAC", + 11: "Speex", + 14: "MP3 8Khz", + 15: "Device-specific sound"} + + // 采样频率. 2 bit + SoundRate = map[byte]int{ + 0: 5500, + 1: 11000, + 2: 22000, + 3: 44000} + + // 量化精度. 1 bit + SoundSize = map[byte]string{ + 0: "8Bit", + 1: "16Bit"} + + // 音频类型. 1bit + SoundType = map[byte]string{ + 0: "Mono", + 1: "Stereo"} + + // 视频帧类型. 4bit + FrameType = map[byte]string{ + 1: "keyframe (for AVC, a seekable frame)", + 2: "inter frame (for AVC, a non-seekable frame)", + 3: "disposable inter frame (H.263 only)", + 4: "generated keyframe (reserved for server use only)", + 5: "video info/command frame"} + + // 视频编码类型. 4bit + CodecID = map[byte]string{ + 1: "JPEG (currently unused)", + 2: "Sorenson H.263", + 3: "Screen video", + 4: "On2 VP6", + 5: "On2 VP6 with alpha channel", + 6: "Screen video version 2", + 7: "AVC", + 12: "H265"} +) + +var FLVHeader = []byte{0x46, 0x4c, 0x56, 0x01, 0x05, 0, 0, 0, 9, 0, 0, 0, 0} + +func WriteFLVTag(w io.Writer, tag *SendPacket) (err error) { + head := pool.GetSlice(11) + defer pool.RecycleSlice(head) + tail := pool.GetSlice(4) + defer pool.RecycleSlice(tail) + head[0] = tag.Packet.Type + dataSize := uint32(len(tag.Packet.Payload)) + util.BigEndian.PutUint32(tail, dataSize+11) + util.BigEndian.PutUint24(head[1:], dataSize) + util.BigEndian.PutUint24(head[4:], tag.Timestamp) + util.BigEndian.PutUint32(head[7:], 0) + if _, err = w.Write(head); err != nil { + return + } + // Tag Data + if _, err = w.Write(tag.Packet.Payload); err != nil { + return + } + if _, err = w.Write(tail); err != nil { // PreviousTagSizeN(4) + return + } + return +} +func ReadFLVTag(r io.Reader) (tag *AVPacket, err error) { + head := pool.GetSlice(11) + defer pool.RecycleSlice(head) + if _, err = io.ReadFull(r, head); err != nil { + return + } + tag = NewAVPacket(head[0]) + dataSize := util.BigEndian.Uint24(head[1:]) + tag.Timestamp = util.BigEndian.Uint24(head[4:]) + body := pool.GetSlice(int(dataSize)) + defer pool.RecycleSlice(body) + if _, err = io.ReadFull(r, body); err == nil { + tag.Payload = body + t := pool.GetSlice(4) + _, err = io.ReadFull(r, t) + pool.RecycleSlice(t) + } + return +} diff --git a/avformat/h264.go b/avformat/h264.go new file mode 100644 index 0000000..2c36403 --- /dev/null +++ b/avformat/h264.go @@ -0,0 +1,144 @@ +package avformat + +import ( + "io" +) + +// Start Code + NAL Unit -> NALU Header + NALU Body +// RTP Packet -> NALU Header + NALU Body + +// NALU Body -> Slice Header + Slice data +// Slice data -> flags + Macroblock layer1 + Macroblock layer2 + ... +// Macroblock layer1 -> mb_type + PCM Data +// Macroblock layer2 -> mb_type + Sub_mb_pred or mb_pred + Residual Data +// Residual Data -> + +const ( + // NALU Type + NALU_Unspecified = 0 + NALU_Non_IDR_Picture = 1 + NALU_Data_Partition_A = 2 + NALU_Data_Partition_B = 3 + NALU_Data_Partition_C = 4 + NALU_IDR_Picture = 5 + NALU_SEI = 6 + NALU_SPS = 7 + NALU_PPS = 8 + NALU_Access_Unit_Delimiter = 9 + NALU_Sequence_End = 10 + NALU_Stream_End = 11 + NALU_Filler_Data = 12 + NALU_SPS_Extension = 13 + NALU_Prefix = 14 + NALU_SPS_Subset = 15 + NALU_DPS = 16 + NALU_Reserved1 = 17 + NALU_Reserved2 = 18 + NALU_Not_Auxiliary_Coded = 19 + NALU_Coded_Slice_Extension = 20 + NALU_Reserved3 = 21 + NALU_Reserved4 = 22 + NALU_Reserved5 = 23 + NALU_NotReserved = 24 + // 24 - 31 NALU_NotReserved +) + +var ( + NALU_AUD_BYTE = []byte{0x00, 0x00, 0x00, 0x01, 0x09, 0xF0} + NALU_Delimiter1 = []byte{0x00, 0x00, 0x01} + NALU_Delimiter2 = []byte{0x00, 0x00, 0x00, 0x01} + RTMP_AVC_HEAD = []byte{0x17, 0x00, 0x00, 0x00, 0x00, 0x01, 0x42, 0x00, 0x1E, 0xFF} + RTMP_KEYFRAME_HEAD = []byte{0x17, 0x01, 0x00, 0x00, 0x00} + RTMP_NORMALFRAME_HEAD = []byte{0x27, 0x01, 0x00, 0x00, 0x00} +) +var NALU_SEI_BYTE []byte + +// H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(NAL) +// NAL - Network Abstract Layer +// raw byte sequence payload (RBSP) 原始字节序列载荷 + +type H264 struct { +} + +type NALUnit struct { + NALUHeader + RBSP +} + +type NALUHeader struct { + forbidden_zero_bit byte // 1 bit 0 + nal_ref_idc byte // 2 bits nal_unit_type等于6,9,10,11或12的NAL单元其nal_ref_idc都应等于 0 + nal_uint_type byte // 5 bits 包含在 NAL 单元中的 RBSP 数据结构的类型 +} + +type RBSP interface { +} + +/* +0 Unspecified non-VCL +1 Coded slice of a non-IDR picture VCL +2 Coded slice data partition A VCL +3 Coded slice data partition B VCL +4 Coded slice data partition C VCL +5 Coded slice of an IDR picture VCL +6 Supplemental enhancement information (SEI) non-VCL +7 Sequence parameter set non-VCL +8 Picture parameter set non-VCL +9 Access unit delimiter non-VCL +10 End of sequence non-VCL +11 End of stream non-VCL +12 Filler data non-VCL +13 Sequence parameter set extension non-VCL +14 Prefix NAL unit non-VCL +15 Subset sequence parameter set non-VCL +16 Depth parameter set non-VCL +17..18 Reserved non-VCL +19 Coded slice of an auxiliary coded picture without partitioning non-VCL +20 Coded slice extension non-VCL +21 Coded slice extension for depth view components non-VCL +22..23 Reserved non-VCL +24..31 Unspecified non-VCL + +0:未规定 +1:非IDR图像中不采用数据划分的片段 +2:非IDR图像中A类数据划分片段 +3:非IDR图像中B类数据划分片段 +4:非IDR图像中C类数据划分片段 +5:IDR图像的片段 +6:补充增强信息(SEI) +7:序列参数集(SPS) +8:图像参数集(PPS) +9:分割符 +10:序列结束符 +11:流结束符 +12:填充数据 +13:序列参数集扩展 +14:带前缀的NAL单元 +15:子序列参数集 +16 – 18:保留 +19:不采用数据划分的辅助编码图像片段 +20:编码片段扩展 +21 – 23:保留 +24 – 31:未规定 + +nal_unit_type NAL类型 nal_reference_bit +0 未使用 0 +1 非IDR的片 此片属于参考帧,则不等于0,不属于参考帧,则等与0 +2 片数据A分区 同上 +3 片数据B分区 同上 +4 片数据C分区 同上 +5 IDR图像的片 5 +6 补充增强信息单元(SEI) 0 +7 序列参数集 非0 +8 图像参数集 非0 +9 分界符 0 +10 序列结束 0 +11 码流结束 0 +12 填充 0 +13..23 保留 0 +24..31 不保留 0 +*/ + +func ReadPPS(w io.Writer) { + +} diff --git a/avformat/mp4.go b/avformat/mp4.go new file mode 100644 index 0000000..fe19232 --- /dev/null +++ b/avformat/mp4.go @@ -0,0 +1,2421 @@ +package avformat + +import ( + "github.com/Monibuca/engine/util" +) + +type MP4 interface { +} + +type MP4Box interface { + Header() *MP4Header + Body() *MP4Body +} + +// +// ISO_IEC_14496-12_2012.pdf Page/17 +// +// The standard boxes all use compact types (32-bit) and most boxes will use the compact (32-bit) size +// standard header +type MP4BoxHeader struct { + BoxSize uint32 // 32 bits, is an integer that specifies the number of bytes in this box, including all its fields and contained boxes; if size is 1 then the actual size is in the field largesize; if size is 0, then this box is the last one in the file, and its contents extend to the end of the file (normally only used for a Media Data Box) + BoxType uint32 // 32 bits, identifies the box type; standard boxes use a compact type, which is normally four printable characters, to permit ease of identification, and is shown so in the boxes below. User extensions use an extended type; in this case, the type field is set to ‘uuid’. +} + +// +// ISO_IEC_14496-12_2012.pdf Page/17 +// +// Many objects also contain a version number and flags field +// full box header +type MP4FullBoxHeader struct { + Version uint8 // 8 bits, is an integer that specifies the version of this format of the box. + Flags [3]byte // 24 bits, is a map of flags +} + +// +// ISO_IEC_14496-12_2012.pdf Page/17 +// +// Typically only the Media Data Box(es) need the 64-bit size. +// lagesize box header +type MP4BoxLargeHeader struct { + LargeSize uint64 // 64 bits + UUIDs [16]uint8 // 128 bits +} + +// if(size == 1) +// { +// unsigned int(64) largesize; +// } +// else if(size == 0) +// { +// // box extends to end of file +// } +// if(boxtype == ‘uuid’) +// { +// unsigned int(8)[16] usertype = extended_type; +// } + +type MP4Header struct { + MP4BoxHeader +} + +type MP4Body struct{} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/18 +// +// Box Type: ftyp +// Container: File +// Mandatory: Yes +// Quantity: Exactly one (but see below) +// +// Each brand is a printable four-character code, registered with ISO, that identifies a precise specification +type FileTypeBox struct { + MP4BoxHeader // standard header + + MajorBrand uint32 // 32 bits, is a brand identifier + MinorVersion uint32 // 32 bits, is an informative integer for the minor version of the major brand + CompatibleBrands []uint32 // 32 bits array, is a list, to the end of the box, of brands +} + +func NewFileTypeBox() (box *FileTypeBox) { + box = new(FileTypeBox) + box.MP4BoxHeader.BoxType, _ = util.ByteToUint32([]byte("ftyp"), true) + + return +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/30 +// +// Box Types: pdin +// Container: File +// Mandatory: No +// Quantity: Zero or One +type ProgressiveDownloadInformationBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Rate uint32 // 32 bits, is a download rate expressed in bytes/second + InitialDelay uint32 // 32 bits, is the suggested delay to use when playing the file, such that if download continues at the given rate, all data within the file will arrive in time for its use and playback should not need to stall. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/30 +// +// Box Type: moov +// Container: File +// Mandatory: Yes +// Quantity: Exactly one +// +// The metadata for a presentation is stored in the single Movie Box which occurs at the top-level of a file. +// Normally this box is close to the beginning or end of the file, though this is not required +type MovieBox struct { + MP4BoxHeader // standard header + + //Mhb MovieHeaderBox // the first child box(header box) +} + +func NewMovieBox() (box *MovieBox) { + box = new(MovieBox) + box.MP4BoxHeader.BoxType, _ = util.ByteToUint32([]byte("moov"), true) + + return +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/31 +// +// Box Type: mvhd +// Container: Movie Box ('moov') +// Mandatory: Yes +// Quantity: Exactly one +// +// This box defines overall information which is media-independent, and relevant to the entire presentation +// considered as a whole +type MovieHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + CreationTime interface{} // uint64 or uint32, is an integer that declares the creation time of the presentation (in seconds since midnight, Jan. 1, 1904, in UTC time) + ModificationTime interface{} // uint64 or uint32, is an integer that declares the most recent time the presentation was modified (in seconds since midnight, Jan. 1, 1904, in UTC time) + TimeScale uint32 // 32 bits, is an integer that specifies the time-scale for the entire presentation; this is the number of time units that pass in one second. For example, a time coordinate system that measures time in sixtieths of a second has a time scale of 60. + Duration interface{} // uint64 or uint32, is an integer that declares length of the presentation (in the indicated timescale). This property is derived from the presentation's tracks: the value of this field corresponds to the duration of the longest track in the presentation. If the duration cannot be determined then duration is set to all 1s. + Rate int32 // 32 bits, is a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0 (0x00010000) is normal forward playback + Volume int16 // 16 bits, is a fixed point 8.8 number that indicates the preferred playback volume. 1.0 (0x0100) is full volume. + Reserved1 int16 // 16 bits, bit[16] + Reserved2 [2]uint32 // 32 bits array, const unsigned int(32)[2] + Matrix [9]int32 // 32 bits array, provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex values(0,0,0x40000000). + PreDefined [6]int32 // 32 bits array, bit(32)[6] + NextTrackID uint32 // 32 bits, is a non-zero integer that indicates a value to use for the track ID of the next track to be added to this presentation. Zero is not a valid track ID value. The value of next_track_ID shall be larger than the largest track-ID in use. If this value is equal to all 1s (32-bit maxint), and a new media track is to be added, then a search must be made in the file for an unused track identifier. +} + +// CreationTime : 创建时间(相对于UTC时间1904-01-01零点的秒数) +// ModificationTime : 修改时间 +// TimeScale : 文件媒体在1秒时间内的刻度值,可以理解为1秒长度的时间单元数 +// Duration : 该track的时间长度,用duration和time scale值可以计算track时长,比如audio track的time scale = 8000, duration = 560128,时长为70.016,video track的time scale = 600, duration = 42000,时长为70 +// Rate : 推荐播放速率,高16位和低16位分别为小数点整数部分和小数部分,即[16.16] 格式,该值为1.0(0x00010000)表示正常前向播放 +// Volume : 与rate类似,[8.8] 格式,1.0(0x0100)表示最大音量 +// Matrix : 视频变换矩阵 { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } +// NextTrackID : 下一个track使用的id号 + +// PreDefined: +// Preview Time : 开始预览此movie的时间 +// Preview Duration : 以movie的time scale为单位,预览的duration +// Poster Time : The time value of the time of the movie poster. +// Selection Time : The time value for the start time of the current selection. +// Selection Duration : The duration of the current selection in movie time scale units. +// Current Time : 当前时间 + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/32 +// +// Box Type: trak +// Container: Movie Box ('moov') +// Mandatory: Yes +// Quantity: One or more +type TrackBox struct { + MP4BoxHeader // standard header + + Thb TrackHeaderBox // the first child box(header box) +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/32 +// +// Box Type: tkhd +// Container: Track Box ('trak') +// Mandatory: Yes +// Quantity: Exactly one +type TrackHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + CreationTime interface{} // uint64 or uint32, + ModificationTime interface{} // uint64 or uint32, + TrackID uint32 // 32 bits, is an integer that uniquely identifies this track over the entire life-time of this presentation. Track IDs are never re-used and cannot be zero + Reserved1 uint32 // 32 bits, + Duration interface{} // uint64 or uint32, + Reserved2 [2]uint32 // 32 bits array, + Layer int16 // 16 bits, specifies the front-to-back ordering of video tracks; tracks with lower numbers are closer to the viewer. 0 is the normal value, and -1 would be in front of track 0, and so on + AlternateGroup int16 // 16 bits, + Volume int16 // 16 bits, if track_is_audio 0x0100 else 0 + Reserved3 uint16 // 16 bits, + Matrix [9]int32 // 32 bits array, provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex (0,0,0x40000000). { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } + Width uint32 // 32 bits, + Height uint32 // 32 bits, +} + +// CreationTime : 创建时间 +// ModificationTime : 修改时间 +// TrackID : id号,不能重复且不能为0 +// Reserved1 : 保留位 +// Duration : track的时间长度 +// Reserved2 : 保留位 +// Layer : 视频层,默认为0,值小的在上层 +// AlternateGroup : track分组信息,默认为0表示该track未与其他track有群组关系 +// Volume : [8.8] 格式,如果为音频track,1.0(0x0100)表示最大音量;否则为0 +// Reserved3 : 保留位 +// Matrix : 视频变换矩阵 { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } +// Width : 宽 +// Height : 高,均为 [16.16] 格式值,与sample描述中的实际画面大小比值,用于播放时的展示宽高 + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/34 +// +// Box Type: tref +// Container: Track Box (‘trak’) +// Mandatory: No +// Quantity: Zero or one +type TrackReferenceBox struct { + MP4BoxHeader // standard header +} + +type TrackReferenceTypeBox struct { + MP4BoxHeader // standard header + + TrackIDs []uint32 // 32 bits, is an integer that provides a reference from the containing track to another track in the presentation. track_IDs are never re-used and cannot be equal to zero +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/35 +// +// Box Type: trgr +// Container: Track Box (‘trak’) +// Mandatory: No +// Quantity: Zero or one +type TrackGroupBox struct { + MP4BoxHeader // standard header +} + +type TrackGroupTypeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + TrackGroupID uint32 // 32 bits, indicates the grouping type and shall be set to one of the following values, or a value registered, or a value from a derived specification or registration +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/54 +// +// Box Type: edts +// Container: Track Box (‘trak’) +// Mandatory: No +// Quantity: Zero or one +type EditBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/54 +// +// Box Type: elst +// Container: Edit Box (‘edts’) +// Mandatory: No +// Quantity: Zero or one +type EditListBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + Tables []EditListTable // Edit List Table +} + +type EditListTable struct { + SegmentDuration interface{} // uint64 or uint32, is an integer that specifies the duration of this edit segment in units of the timescale in the Movie Header Box + MediaTime interface{} // uint64 or uint32, is an integer containing the starting time within the media of this edit segment (in media time scale units, in composition time). If this field is set to –1, it is an empty edit. The last edit in a track shall never be an empty edit. Any difference between the duration in the Movie Header Box, and the track’s duration is expressed as an implicit empty edit at the end. + MediaRateInteger int16 // 16 bits, + MediaRateFraction int16 // 16 bits, +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/36 +// +// Box Type: mdia +// Container: Track Box ('trak') +// Mandatory: Yes +// Quantity: Exactly one +// +// The media declaration container contains all the objects that declare information about the media data within a track. +type MediaBox struct { + MP4BoxHeader // standard header + + Mhb MediaHeaderBox // the first child box(header box) +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/36 +// +// Box Type: mdhd +// Container: Media Box ('mdia') +// Mandatory: Yes +// Quantity: Exactly one +// +// The media header declares overall information that is media-independent, and relevant to characteristics of the media in a track. +type MediaHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + CreationTime interface{} // int64 or int32, is an integer that declares the creation time of the presentation (in seconds since midnight, Jan. 1, 1904, in UTC time) + ModificationTime interface{} // int64 or int32, is an integer that declares the most recent time the presentation was modified (in seconds since midnight, Jan. 1, 1904, in UTC time) + TimeScale uint32 // 32 bits, is an integer that specifies the time-scale for the entire presentation; this is the number of time units that pass in one second. For example, a time coordinate system that measures time in sixtieths of a second has a time scale of 60. + Duration interface{} // int64 or int32, is an integer that declares length of the presentation (in the indicated timescale). This property is derived from the presentation's tracks: the value of this field corresponds to the duration of the longest track in the presentation. If the duration cannot be determined then duration is set to all 1s. + Pad byte // 1 bit, + Language [2]byte // 15 bits, unsigned int(5)[3], declares the language code for this media. See ISO 639-2/T for the set of three charactercodes. Each character is packed as the difference between its ASCII value and 0x60. Since the code is confined to being three lower-case letters, these values are strictly positive + PreDefined uint16 // 16 bits, +} + +// Language : 媒体的语言码 +// PreDefined : 媒体的回放质量???怎样生成此质量,什么是参照点 + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/37 +// +// Box Type: hdlr +// Container: Media Box ('mdia') or Meta Box ('meta') +// Mandatory: Yes +// Quantity: Exactly one +type HandlerBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + PreDefined uint32 // 32 bits, + HandlerType uint32 // 32 bits, when present in a meta box, contains an appropriate value to indicate the format of the meta box contents. The value 'null' can be used in the primary meta box to indicate that it is merely being used to hold resources + Reserved [3]uint32 // 32 bits, + Name string // string, is a null-terminated string in UTF-8 characters which gives a human-readable name for the track type (for debugging and inspection purposes). +} + +// handler_type when present in a media box, is an integer containing one of the following values, or a value from a derived specification: +// 'vide' Video track +// 'soun' Audio track +// 'hint' Hint track +// 'meta' Timed Metadata track +// 'auxv' Auxiliary Video track + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/38 +// +// Box Type: minf +// Container: Media Box ('mdia') +// Mandatory: Yes +// Quantity: Exactly one +// +// This box contains all the objects that declare characteristic information of the media in the track. +type MediaInformationBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/38 +// +// Box Types: vmhd, smhd, hmhd, nmhd +// Container: Media Information Box (‘minf’) +// Mandatory: Yes +// Quantity: Exactly one specific media header shall be present +// +// There is a different media information header for each track type (corresponding to the media handler-type); +// the matching header shall be present, which may be one of those defined here, or one defined in a derived specification +type MediaInformationHeaderBoxes struct { + // VideoMediaHeaderBox + // +} + +// Box Types: vmhd +// The video media header contains general presentation information, independent of the coding, for video media. +// Note that the flags field has the value 1. +type VideoMediaHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + GraphicsMode uint16 // 16 bits, specifies a composition mode for this video track, from the following enumerated set, which may be extended by derived specifications: copy = 0 copy over the existing image + Opcolor [3]uint16 // 16 bits array, is a set of 3 colour values (red, green, blue) available for use by graphics modes +} + +// Box Types: smhd +// The sound media header contains general presentation information, independent of the coding, for audio media. +// This header is used for all tracks containing audio. +type SoundMediaHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Balance int16 // 16 bits, is a fixed-point 8.8 number that places mono audio tracks in a stereo space; 0 is centre (the normal value); full left is -1.0 and full right is 1.0 + Reserved uint16 // 16 bits, +} + +// Box Types: hmhd +// The hint media header contains general information, independent of the protocol, for hint tracks. +// (A PDU is a Protocol Data Unit.) +type HintMediaHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + MaxPDUSize uint16 // 16 bits, gives the size in bytes of the largest PDU in this (hint) stream + AvgPDUSize uint16 // 16 bits, gives the average size of a PDU over the entire presentation + MaxBitrate uint32 // 32 bits, gives the maximum rate in bits/second over any window of one second + AvgBitrate uint32 // 32 bits, gives the average rate in bits/second over the entire presentation + Reserved uint32 // 32 bits, +} + +// Box Types: nmhd +// Streams other than visual and audio (e.g., timed metadata streams) may use a null Media Header Box, as defined here. +type NullMediaHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/56 +// +// Box Type: dinf +// Container: Media Information Box ('minf') or Meta Box ('meta') +// Mandatory: Yes (required within 'minf' box) and No (optional within 'meta' box) +// Quantity: Exactly one +// +// The data information box contains objects that declare the location of the media information in a track +type DataInformationBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- +// +// ISO_IEC_14496-12_2012.pdf Page/56 +// +// Box Types: url, urn, dref +// Container: Data Information Box ('dinf') +// Mandatory: Yes +// Quantity: Exactly one +type DataReferenceBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + DataEntry interface{} // DataEntryUrlBox or DataEntryUrnBox. +} + +// aligned(8) class DataReferenceBox +// extends FullBox('dref', version = 0, 0) { +// unsigned int(32) entry_count; +// for (i=1; i <= entry_count; i++) { +// DataEntryBox(entry_version, entry_flags) data_entry; +// } +// } + +type DataEntryUrlBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Location string // string, +} + +type DataEntryUrnBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Name string // string, + Location string // string, +} + +// ------------------------------------------------------------------------------------------------------- +// +// ISO_IEC_14496-12_2012.pdf Page/40 +// +// Box Type: stbl +// Container: Media Information Box ('minf') +// Mandatory: Yes +// Quantity: Exactly one +type SampleTableBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/40 +// +// Box Types: stsd +// Container: Sample Table Box ('stbl') +// Mandatory: Yes +// Quantity: Exactly one +type SampleDescriptionBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table +} + +// for (i = 1 ; i <= entry_count ; i++) { +// switch (handler_type){ +// case ‘soun’: // for audio tracks +// AudioSampleEntry(); +// break; +// case ‘vide’: // for video tracks +// VisualSampleEntry(); +// break; +// case ‘hint’: // Hint track +// HintSampleEntry(); +// break; +// case ‘meta’: // Metadata track +// MetadataSampleEntry(); +// break; +// } +// } + +// box header和version字段后会有一个entry count字段,根据entry的个数,每个entry会有type信息,如“vide”、“sund”等, +// 根据type不同sample description会提供不同的信息,例如对于video track,会有“VisualSampleEntry”类型信息, +// 对于audio track会有“AudioSampleEntry”类型信息. +// 视频的编码类型、宽高、长度,音频的声道、采样等信息都会出现在这个box中 + +// is the appropriate sample entry +type SampleEntry struct { + Reserved [6]uint8 // 48 bits, + DataReferenceIndex uint16 // 16 bits, is an integer that contains the index of the data reference to use to retrieve data associated with samples that use this sample description. Data references are stored in Data Reference Boxes. The index ranges from 1 to the number of data references. +} + +type HintSampleEntry struct { + Data []uint8 // 8 bits array, +} + +// Box Types: btrt +type BitRateBox struct { + MP4BoxHeader // standard header + + BufferSizeDB uint32 // 32 bits, gives the size of the decoding buffer for the elementary stream in bytes. + MaxBitrate uint32 // 32 bits, gives the maximum rate in bits/second over any window of one second. + AvgBitrate uint32 // 32 bits, gives the average rate in bits/second over the entire presentation. +} + +type MetaDataSampleEntry struct{} + +type XMLMetaDataSampleEntry struct { + ContentEncoding string // optional, is a null-terminated string in UTF-8 characters, and provides a MIME type which identifies the content encoding of the timed metadata + NameSpace string // string, gives the namespace of the schema for the timed XML metadata + SchemaLocation string // optional, optionally provides an URL to find the schema corresponding to the namespace. This is needed for decoding of the timed metadata by XML aware encoding mechanisms such as BiM. + Brb BitRateBox // optional +} + +type TextMetaDataSampleEntry struct { + ContentEncoding string // optional, is a null-terminated string in UTF-8 characters, and provides a MIME type which identifies the content encoding of the timed metadata + MimeFormat string // string, provides a MIME type which identifies the content format of the timed metadata. Examples for this field are ‘text/html’ and ‘text/plain’. + Brb BitRateBox // optional +} + +type URIBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + TheURI string // string, is a URI formatted according to the rules in 6.2.4 +} + +type URIInitBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + UriInitializationData []uint8 // 8 bits array, is opaque data whose form is defined in the documentation of the URI form. +} + +type URIMetaSampleEntry struct { + TheLabel URIBox + Init URIInitBox // optional + //Mpeg4 MPEG4BitRateBox // optional +} + +// Box Types: pasp +type PixelAspectRatioBox struct { + MP4BoxHeader // standard header + + HSpacing uint32 // 32 bits, define the relative width and height of a pixel; + VSpacing uint32 // 32 bits, define the relative width and height of a pixel; +} + +// Box Types: clap +// Visual Sequences +type CleanApertureBox struct { + MP4BoxHeader // standard header + + CleanApertureWidthN uint32 // 32 bits, a fractional number which defines the exact clean aperture width, in counted pixels, of the video image + CleanApertureWidthD uint32 // 32 bits, a fractional number which defines the exact clean aperture width, in counted pixels, of the video image + CleanApertureHeightN uint32 // 32 bits, a fractional number which defines the exact clean aperture height, in counted pixels, of the video image + CleanApertureHeightD uint32 // 32 bits, a fractional number which defines the exact clean aperture height, in counted pixels, of the video image + HorizOffN uint32 // 32 bits, a fractional number which defines the horizontal offset of clean aperture centre minus (width-1)/2. Typically 0 + HorizOffD uint32 // 32 bits, a fractional number which defines the horizontal offset of clean aperture centre minus (width-1)/2. Typically 0 + VertOffN uint32 // 32 bits, a fractional number which defines the vertical offset of clean aperture centre minus (height-1)/2. Typically 0 + VertOffD uint32 // 32 bits, a fractional number which defines the vertical offset of clean aperture centre minus (height-1)/2. Typically 0 +} + +// Box Types: colr +type ColourInformationBox struct { + MP4BoxHeader // standard header + + ColourType uint32 // 32 bits, an indication of the type of colour information supplied. For colour_type ‘nclx’: these fields are exactly the four bytes defined for PTM_COLOR_INFO( ) in A.7.2 of ISO/IEC 29199-2 but note that the full range flag is here in a different bit position +} + +// if (colour_type == ‘nclx’) /* on-screen colours */ +// { +// unsigned int(16) colour_primaries; +// unsigned int(16) transfer_characteristics; +// unsigned int(16) matrix_coefficients; +// unsigned int(1) full_range_flag; +// unsigned int(7) reserved = 0; +// } +// else if (colour_type == ‘rICC’) +// { +// ICC_profile; // restricted ICC profile +// } +// else if (colour_type == ‘prof’) +// { +// ICC_profile; // unrestricted ICC profile +// } + +// ICC_profile : an ICC profile as defined in ISO 15076-1 or ICC.1:2010 is supplied. + +type VisualSampleEntry struct { + PreDefined1 uint16 // 16 bits, + Reserved1 uint16 // 16 bits, + PreDefined2 [3]uint32 // 96 bits, + Width uint16 // 16 bits, are the maximum visual width and height of the stream described by this sample description, in pixels + Height uint16 // 16 bits, are the maximum visual width and height of the stream described by this sample description, in pixels + HorizreSolution uint32 // 32 bits, fields give the resolution of the image in pixels-per-inch, as a fixed 16.16 number + VertreSolution uint32 // 32 bits, fields give the resolution of the image in pixels-per-inch, as a fixed 16.16 number + Reserved3 uint32 // 32 bits, + FrameCount uint16 // 16 bits, indicates how many frames of compressed video are stored in each sample. The default is 1, for one frame per sample; it may be more than 1 for multiple frames per sample + CompressorName [32]string // 32 string, is a name, for informative purposes. It is formatted in a fixed 32-byte field, with the first byte set to the number of bytes to be displayed, followed by that number of bytes of displayable data, and then padding to complete 32 bytes total (including the size byte). The field may be set to 0. + Depth uint16 // 16 bits, takes one of the following values 0x0018 – images are in colour with no alpha + PreDefined3 int16 // 16 bits, + Cab CleanApertureBox // optional, other boxes from derived specifications + Parb PixelAspectRatioBox // optional, other boxes from derived specifications +} + +// Audio Sequences +type AudioSampleEntry struct { + Reserved1 [2]uint32 // 32 bits array, + ChannelCount uint16 // 16 bits, is the number of channels such as 1 (mono) or 2 (stereo) + SampleSize uint16 // 16 bits, is in bits, and takes the default value of 16 + PreDefined uint16 // 16 bits, + Reserved2 uint16 // 16 bits, + SampleRate uint32 // 32 bits, is the sampling rate expressed as a 16.16 fixed-point number (hi.lo) +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/48 +// +// Box Type: stts +// Container: Sample Table Box ('stbl') +// Mandatory: Yes +// Quantity: Exactly one +type TimeToSampleBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + Table []TimeToSampleTable // Time To Sample Table , EntryCount elements +} + +type TimeToSampleTable struct { + SampleCount []uint32 // 32 bits, is an integer that counts the number of consecutive samples that have the given duration + SampleDelta []uint32 // 32 bits, is an integer that gives the delta of these samples in the time-scale of the media. +} + +// “stts”存储了sample的duration,描述了sample时序的映射方法,我们通过它可以找到任何时间的sample. +// “stts”可以包含一个压缩的表来映射时间和sample序号,用其他的表来提供每个sample的长度和指针. +// 表中每个条目提供了在同一个时间偏移量里面连续的sample序号,以及samples的偏移量. +// 递增这些偏移量,就可以建立一个完整的time to sample表 + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/49 +// +// Box Type: ctts +// Container: Sample Table Box (‘stbl’) +// Mandatory: No +// Quantity: Zero or one +type CompositionOffsetBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + Table []CompositionOffsetTable // Composition Offset Table, EntryCount elements. +} + +type CompositionOffsetTable struct { + SampleCount uint32 // 32 bits, is an integer that counts the number of consecutive samples that have the given offset. + SampleOffset interface{} // int32 or uint32, is an integer that gives the offset between CT and DT, such that CT(n) = DT(n) + CTTS(n). +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/50 +// +// Box Type: cslg +// Container: Sample Table Box (‘stbl’) +// Mandatory: No +// Quantity: Zero or one +type CompositionToDecodeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + CompositionToDTSShift int32 // 32 bits, signed, if this value is added to the composition times (as calculated by the CTS offsets from the DTS), then for all samples, their CTS is guaranteed to be greater than or equal to their DTS, and the buffer model implied by the indicated profile/level will be honoured; if leastDecodeToDisplayDelta is positive or zero, this field can be 0; otherwise it should be at least (- leastDecodeToDisplayDelta) + LeastDecodeToDisplayDelta int32 // 32 bits, signed, the smallest composition offset in the CompositionTimeToSample box in this track + GreatestDecodeToDisplayDelta int32 // 32 bits, signed, the largest composition offset in the CompositionTimeToSample box in this track + CompositionStartTime int32 // 32 bits, signed, the smallest computed composition time (CTS) for any sample in the media of this track + CompositionEndTime int32 // 32 bits, signed, the composition time plus the composition duration, of the sample with the largest computed composition time (CTS) in the media of this track; if this field takes the value 0, the composition end time is unknown. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/58 +// +// Box Type: stsc +// Container: Sample Table Box ('stbl') +// Mandatory: Yes +// Quantity: Exactly one +type SampleToChunkBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + Table []SampleToChunkTable // Sample To Chunk Table, entry count elements. +} + +type SampleToChunkTable struct { + FirstChunk []uint32 // 32 bits, is an integer that gives the index of the first chunk in this run of chunks that share the same samples-per-chunk and sample-description-index; the index of the first chunk in a track has the value 1 (the first_chunk field in the first record of this box has the value 1, identifying that the first sample maps to the first chunk). + SamplesPerChunk []uint32 // 32 bits, is an integer that gives the number of samples in each of these chunks + SampleDescriptionIndex []uint32 // 32 bits, is an integer that gives the index of the sample entry that describes the samples in this chunk. The index ranges from 1 to the number of sample entries in the Sample Description Box +} + +// 用chunk组织sample可以方便优化数据获取,一个thunk包含一个或多个sample. +// “stsc”中用一个表描述了sample与chunk的映射关系,查看这张表就可以找到包含指定sample的thunk,从而找到这个sample + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/57 +// +// Box Type: stsz, stz2 +// Container: Sample Table Box (‘stbl’) +// Mandatory: Yes +// Quantity: Exactly one variant must be present +type SampleSizeBoxes struct{} + +// Box Type: stsz +type SampleSizeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SampleSize uint32 // 32 bits, is integer specifying the default sample size. If all the samples are the same size, this field contains that size value. If this field is set to 0, then the samples have different sizes, and those sizes are stored in the sample size table. If this field is not 0, it specifies the constant sample size, and no array follows. + SampleCount uint32 // 32 bits, is an integer that gives the number of samples in the track; if sample-size is 0, then it is also the number of entries in the following table. + EntrySize interface{} // 32 bits array, SampleCount elements, is an integer specifying the size of a sample, indexed by its number. +} + +// if (sample_size == 0) { +// for (i = 1; i <= sample_count; i++) { +// unsigned int(32) entry_size; +// } +// } + +// Box Type: stz2 +type CompactSampleSizeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Reserved [3]uint8 // 24 bits, + FieldSize uint8 // 8 bits, is an integer specifying the size in bits of the entries in the following table; it shall take the value 4, 8 or 16. If the value 4 is used, then each byte contains two values: entry[i]<<4 + entry[i+1]; if the sizes do not fill an integral number of bytes, the last byte is padded with zeros. + SampleCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + EntrySize interface{} // +} + +// for (i = 1; i <= sample_count; i++) { +// unsigned int(field_size) entry_size; +// } + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/59 +// +// Box Type: stco, co64 +// Container: Sample Table Box (‘stbl’) +// Mandatory: Yes +// Quantity: Exactly one variant must be present +type ChunkOffsetBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + ChunkOffset []uint32 // 32 bits array, entry count elements. +} + +// “stco”定义了每个thunk在媒体流中的位置。位置有两种可能,32位的和64位的,后者对非常大的电影很有用。 +// 在一个表中只会有一种可能,这个位置是在整个文件中的,而不是在任何box中的,这样做就可以直接在文件中找到媒体数据, +// 而不用解释box。需要注意的是一旦前面的box有了任何改变,这张表都要重新建立,因为位置信息已经改变了 + +// Box Type: co64 +type ChunkLargeOffsetBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table + ChunkOffset []uint64 // 64 bits array, entry count elements. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/51 +// +// Box Type: stss +// Container: Sample Table Box (‘stbl’) +// Mandatory: No +// Quantity: Zero or one +// +// This box provides a compact marking of the sync samples within the stream. The table is arranged in strictly increasing order of sample number. +// If the sync sample box is not present, every sample is a sync sample. +type SyncSampleBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. If entry_count is zero, there are no sync samples within the stream and the following table is empty + SampleNumber []uint32 // 32 bits array, entry count elements. gives the numbers of the samples that are sync samples in the stream. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/52 +// +// Box Type: stsh +// Container: Sample Table Box (‘stbl’) +// Mandatory: No +// Quantity: Zero or one +type ShadowSyncSampleBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. + Table []ShadowSyncSampleTable // Shadow Sync Sample Table, entry count elements. +} + +type ShadowSyncSampleTable struct { + ShadowedSampleNumber uint32 // 32 bits, gives the number of a sample for which there is an alternative sync sample. + SyncSampleNumber uint32 // 32 bits, gives the number of the alternative sync sample. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/60 +// +// Box Type: padb +// Container: Sample Table (‘stbl’) +// Mandatory: No +// Quantity: Zero or one +// +// In some streams the media samples do not occupy all bits of the bytes given by the sample size, and are +// padded at the end to a byte boundary. In some cases, it is necessary to record externally the number of +// padding bits used. This table supplies that information. +type PaddingBitsBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SampleCount uint32 // 32 bits, counts the number of samples in the track; it should match the count in other tables + Table []PaddingBitsTable // Padding Bits Table, (sample count + 1) / 2 elements. +} + +type PaddingBitsTable struct { + Reserved1 byte // 1 bit, + Pad1 byte // 3 bits, a value from 0 to 7, indicating the number of bits at the end of sample (i*2)+1. + Reserved2 byte // 1 bit, + Pad2 byte // 3 bits, a value from 0 to 7, indicating the number of bits at the end of sample (i*2)+2. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/46 +// +// Box Type: stdp +// Container: Sample Table Box (‘stbl’). +// Mandatory: No. +// Quantity: Zero or one. +// +// This box contains the degradation priority of each sample. The values are stored in the table, one for each +// sample. The size of the table, sample_count is taken from the sample_count in the Sample Size Box +// ('stsz'). Specifications derived from this define the exact meaning and acceptable range of the priority field. +type DegradationPriorityBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Priority []uint16 // 16 bits array, sample count elements, is integer specifying the degradation priority for each sample. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/53 +// +// Box Types: sdtp +// Container: Sample Table Box (‘stbl’) +// Mandatory: No +// Quantity: Zero or one +type IndependentAndDisposableSamplesBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Table []IndependentAndDisposableSamplesTable // Independent And Disposable Samples Table, sample count elements +} + +type IndependentAndDisposableSamplesTable struct { + IsLeading byte // 2 bits, + SampleDependsOn byte // 2 bits, + SampleIsDependedOn byte // 2 bits, + SampleHasTedundancy byte // 2 bits, +} + +// is_leading takes one of the following four values: +// 0: the leading nature of this sample is unknown; +// 1: this sample is a leading sample that has a dependency before the referenced I-picture (and is +// therefore not decodable); +// 2: this sample is not a leading sample; +// 3: this sample is a leading sample that has no dependency before the referenced I-picture (and is +// therefore decodable); +// sample_depends_on takes one of the following four values: +// 0: the dependency of this sample is unknown; +// 1: this sample does depend on others (not an I picture); +// 2: this sample does not depend on others (I picture); +// 3: reserved +// sample_is_depended_on takes one of the following four values: +// 0: the dependency of other samples on this sample is unknown; +// 1: other samples may depend on this one (not disposable); +// 2: no other sample depends on this one (disposable); +// 3: reserved +// sample_has_redundancy takes one of the following four values: +// 0: it is unknown whether there is redundant coding in this sample; +// 1: there is redundant coding in this sample; +// 2: there is no redundant coding in this sample; +// 3: reserved + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/75 +// +// Box Type: sbgp +// Container: Sample Table Box (‘stbl’) or Track Fragment Box (‘traf’) +// Mandatory: No +// Quantity: Zero or more. +type SampleToGroupBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + GroupingType uint32 // 32 bits, is an integer that identifies the type (i.e. criterion used to form the sample groups) of the sample grouping and links it to its sample group description table with the same value for grouping type. At most one occurrence of this box with the same value for grouping_type (and, if used, grouping_type_parameter) shall exist for a track. + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. + Table []SampleToGroupTable // Sample To Group Table, entry count elements. +} + +type SampleToGroupTable struct { + SampleCount uint32 // 32 bits, is an integer that gives the number of consecutive samples with the same sample group descriptor. If the sum of the sample count in this box is less than the total sample count, then the reader should effectively extend it with an entry that associates the remaining samples with no group. It is an error for the total in this box to be greater than the sample_count documented elsewhere, and the reader behaviour would then be undefined. + GroupDescriptionIndex uint32 // 32 bits, is an integer that gives the index of the sample group entry which describes the samples in this group. The index ranges from 1 to the number of sample group entries in the SampleGroupDescription Box, or takes the value 0 to indicate that this sample is a member of no group of this type. +} + +// unsigned int(32) grouping_type; +// if (version == 1) { +// unsigned int(32) grouping_type_parameter; +// } +// unsigned int(32) entry_count; + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/76 +// +// Box Type: sgpd +// Container: Sample Table Box (‘stbl’) or Track Fragment Box (‘traf’) +// Mandatory: No +// Quantity: Zero or more, with one for each Sample to Group Box. +type SampleGroupDescriptionBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + GroupingType uint32 // 32 bits, is an integer that identifies the SampleToGroup box that is associated with this sample group description. + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. +} + +// default_length : indicates the length of every group entry (if the length is constant), or zero (0) if it is variable +// description_length : indicates the length of an individual group entry, in the case it varies from entry to entry and default_length is therefore 0 + +// if (version==1) { unsigned int(32) default_length; } + +// for (i = 1 ; i <= entry_count ; i++){ +// if (version==1) { +// if (default_length==0) { +// unsigned int(32) description_length; +// } +// } +// switch (handler_type){ +// case ‘vide’: // for video tracks +// VisualSampleGroupEntry (grouping_type); +// break; +// case ‘soun’: // for audio tracks +// AudioSampleGroupEntry(grouping_type); +// break; +// case ‘hint’: // for hint tracks +// HintSampleGroupEntry(grouping_type); +// break; +// } +// } + +type SampleGroupDescriptionEntry struct{} + +type VisualSampleGroupEntry struct{} + +type AudioSampleGroupEntry struct{} + +type HintSampleGroupEntry struct{} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/61 +// +// Box Type: subs +// Container: Sample Table Box (‘stbl’) or Track Fragment Box (‘traf’) +// Mandatory: No +// Quantity: Zero or one +type SubSampleInformationBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. + Table []SubSampleInformationTable // Sub-Sample Information Table, entry count elements. + +} + +type SubSampleInformationTable struct { + SampleDelta uint32 // 32 bits, is an integer that specifies the sample number of the sample having sub-sample structure. It is coded as the difference between the desired sample number, and the sample number indicated in the previous entry. If the current entry is the first entry, the value indicates the sample number of the first sample having sub-sample information, that is, the value is the difference between the sample number and zero (0). + SubsampleCount uint16 // 16 bits, is an integer that specifies the number of sub-sample for the current sample. If there is no sub-sample structure, then this field takes the value 0. + CountTable []SubSampleCountTable // Sub-Sample Information Table1, subsample count elements. +} + +type SubSampleCountTable struct { + SubsampleSize interface{} // uint16 or uint32, is an integer that specifies the size, in bytes, of the current sub-sample + SubsamplePriority uint8 // 8 bits, is an integer specifying the degradation priority for each sub-sample. Higher values of subsample_priority, indicate sub-samples which are important to, and have a greater impact on, the decoded quality. + DiscardAble uint8 // 8 bits, equal to 0 means that the sub-sample is required to decode the current sample, while equal to 1 means the sub-sample is not required to decode the current sample but may be used for enhancements, e.g., the sub-sample consists of supplemental enhancement information (SEI) messages. + Reserved uint32 // 32 bits, +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/62 +// +// Box Type: saiz +// Container: Sample Table Box (‘stbl’) or Track Fragment Box ('traf') +// Mandatory: No +// Quantity: Zero or More +type SampleAuxiliaryInformationSizesBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Table interface{} // SampleAuxiliaryInformationSizesTable1 or SampleAuxiliaryInformationSizesTable2. +} + +type SampleAuxiliaryInformationSizesTable1 struct { + AuxInfoType uint32 // 32 bits, + AuxInfoTypeParameter uint32 // 32 bits, + DefaultSampleInfoSize uint8 // 8 bits, is an integer specifying the sample auxiliary information size for the case where all the indicated samples have the same sample auxiliary information size. If the size varies then this field shall be zero. + SampleCount uint32 // 32 bits, +} + +type SampleAuxiliaryInformationSizesTable2 struct { + DefaultSampleInfoSize uint8 // 8 bits, is an integer specifying the sample auxiliary information size for the case where all the indicated samples have the same sample auxiliary information size. If the size varies then this field shall be zero. + SampleCount uint32 // 32 bits, +} + +// if (flags & 1) { +// unsigned int(32) aux_info_type; +// unsigned int(32) aux_info_type_parameter; +// } +// unsigned int(8) default_sample_info_size; +// unsigned int(32) sample_count; +// if (default_sample_info_size == 0) { +// unsigned int(8) sample_info_size[ sample_count ]; +// } + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/63 +// +// Box Type: saio +// Container: Sample Table Box (‘stbl’) or Track Fragment Box ('traf') +// Mandatory: No +// Quantity: Zero or More +type SampleAuxiliaryInformationOffsetsBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + //EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. +} + +type AuxInfo struct { + AuxInfoType uint32 // 32 bits, + AuxInfoTypeParameter uint32 // 32 bits, +} + +// if (flags & 1) { +// unsigned int(32) aux_info_type; +// unsigned int(32) aux_info_type_parameter; +// } +// unsigned int(32) entry_count; +// if ( version == 0 ) { +// unsigned int(32) offset[ entry_count ]; +// } +// else { +// unsigned int(64) offset[ entry_count ]; +// } + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/78 +// +// Box Type: udta +// Container: Movie Box (‘moov’) or Track Box (‘trak’) +// Mandatory: No +// Quantity: Zero or one +type UserDataBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/64 +// +// Box Type: mvex +// Container: Movie Box (‘moov’) +// Mandatory: No +// Quantity: Zero or one +type MovieExtendsBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/65 +// +// Box Type: mehd +// Container: Movie Extends Box(‘mvex’) +// Mandatory: No +// Quantity: Zero or one +// +// The Movie Extends Header is optional, and provides the overall duration, including fragments, of a fragmented +// movie. If this box is not present, the overall duration must be computed by examining each fragment. +type MovieExtendsHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header +} + +// if (version==1) { +// unsigned int(64) fragment_duration; +// } else { // version==0 +// unsigned int(32) fragment_duration; +// } + +// fragment_duration : is an integer that declares length of the presentation of the whole movie including +// fragments (in the timescale indicated in the Movie Header Box). The value of this field corresponds to +// the duration of the longest track, including movie fragments. If an MP4 file is created in real-time, such +// as used in live streaming, it is not likely that the fragment_duration is known in advance and this +// box may be omitted. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/65 +// +// Box Type: trex +// Container: Movie Extends Box (‘mvex’) +// Mandatory: Yes +// Quantity: Exactly one for each track in the Movie Box +type TrackExtendsBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + TrackID uint32 // 32 bits, identifies the track; this shall be the track ID of a track in the Movie Box + DefaultSampleDescriptionIndex uint32 // 32 bits, + DefaultSampleDuration uint32 // 32 bits, + DefaultSampleSize uint32 // 32 bits, + DefaultSampleFlags uint32 // 32 bits, +} + +// default_ : these fields set up defaults used in the track fragments. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/72 +// +// Box Type: leva +// Container: Movie Extends Box (`mvex’) +// Mandatory: No +// Quantity: Zero or one +type LevelAssignmentBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + LevelCount uint8 // 8 bits, specifies the number of levels each fraction is grouped into. level_count shall be greater than or equal to 2. + Table []LevelAssignmentTable // Level Assignment Table, level count elements. +} + +type LevelAssignmentTable struct { + TrackId uint32 // 32 bits, for loop entry j specifies the track identifier of the track assigned to level j. + PaddingFlag byte // 1 bit, equal to 1 indicates that a conforming fraction can be formed by concatenating any positive integer number of levels within a fraction and padding the last Media Data box by zero bytes up to the full size that is indicated in the header of the last Media Data box. The semantics of padding_flag equal to 0 are that this is not assured. + AssignmentType byte // 7 bits, +} + +// for (j=1; j <= level_count; j++) { +// unsigned int(32) track_id; +// unsigned int(1) padding_flag; +// unsigned int(7) assignment_type; +// if (assignment_type == 0) { +// unsigned int(32) grouping_type; +// } +// else if (assignment_type == 1) { +// unsigned int(32) grouping_type; +// unsigned int(32) grouping_type_parameter; +// } +// else if (assignment_type == 2) {} // no further syntax elements needed +// else if (assignment_type == 3) {} // no further syntax elements needed +// else if (assignment_type == 4) { +// unsigned int(32) sub_track_id; +// } +// // other assignment_type values are reserved +// } + +// assignment_type : indicates the mechanism used to specify the assignment to a level. +// assignment_type values greater than 4 are reserved, while the semantics for the other values are +// specified as follows. The sequence of assignment_types is restricted to be a set of zero or more of +// type 2 or 3, followed by zero or more of exactly one type. +// • 0: sample groups are used to specify levels, i.e., samples mapped to different sample group +// description indexes of a particular sample grouping lie in different levels within the identified track; +// other tracks are not affected and must have all their data in precisely one level; +// • 1: as for assignment_type 0 except assignment is by a parameterized sample group; +// • 2, 3: level assignment is by track (see the Subsegment Index Box for the difference in processing +// of these levels) +// • 4: the respective level contains the samples for a sub-track. The sub-tracks are specified through +// the Sub Track box; other tracks are not affected and must have all their data in precisely one +// level; + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/66 +// +// Box Type: moof +// Container: File +// Mandatory: No +// Quantity: Zero or more +type MovieFragmentBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/66 +// +// Box Type: mfhd +// Container: Movie Fragment Box ('moof') +// Mandatory: Yes +// Quantity: Exactly one +// +// The movie fragment header contains a sequence number, as a safety check. The sequence number usually +// starts at 1 and must increase for each movie fragment in the file, in the order in which they occur. This allows +// readers to verify integrity of the sequence; it is an error to construct a file where the fragments are out of sequence. +type MovieFragmentHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SequenceNumber uint32 // 32 bits, the ordinal number of this fragment, in increasing order +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/67 +// +// Box Type: traf +// Container: Movie Fragment Box ('moof') +// Mandatory: No +// Quantity: Zero or more +type TrackFragmentBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/67 +// +// Box Type: tfhd +// Container: Track Fragment Box ('traf') +// Mandatory: Yes +// Quantity: Exactly one +type TrackFragmentHeaderBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + TrackID uint32 // 32 bits, + + // all the following are optional fields + BaseDataOffset uint64 // 64 bits, the base offset to use when calculating data offsets + SampleDescriptionIndex uint32 // 32 bits, + DefaultSampleDuration uint32 // 32 bits, + DefaultSampleSize uint32 // 32 bits, + DefaultSampleFlags uint32 // 32 bits, +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/68 +// +// Box Type: trun +// Container: Track Fragment Box ('traf') +// Mandatory: No +// Quantity: Zero or more +type TrackFragmentRunBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SampleCount uint32 // 32 bits, the number of samples being added in this run; also the number of rows in the following table (the rows can be empty) + + // the following are optional fields + DataOffset int32 // 32 bits, signed, is added to the implicit or explicit data_offset established in the track fragment header. + FirstSampleFlags uint32 // 32 bits, provides a set of flags for the first sample only of this run. + + // all fields in the following array are optional + Table []TrackFragmentRunTable // Track Fragment Run Table 1, SampleCount elements. +} + +type TrackFragmentRunTable struct { + SampleDuration uint32 // 32 bits, + SampleSize uint32 // 32 bits, + SampleFlags uint32 // 32 bits, + SampleCompositionTimeOffset interface{} // uint32 or int32, +} + +// if (version == 0){ +// unsigned int(32) sample_composition_time_offset; +// } +// else{ +// signed int(32) sample_composition_time_offset; +// } + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/71 +// +// Box Type: tfdt +// Container: Track Fragment box (‘traf’) +// Mandatory: No +// Quantity: Zero or one +type TrackFragmentBaseMediaDecodeTimeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + BaseMediaDecodeTime interface{} // uint32 or uint64, is an integer equal to the sum of the decode durations of all earlier samples in the media, expressed in the media's timescale. It does not include the samples added in the enclosing track fragment. +} + +// if (version==1) { +// unsigned int(64) baseMediaDecodeTime; +// } else { // version==0 +// unsigned int(32) baseMediaDecodeTime; + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/69 +// +// Box Type: mfra +// Container: File +// Mandatory: No +// Quantity: Zero or one +type MovieFragmentRandomAccessBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/70 +// +// Box Type: tfra +// Container: Movie Fragment Random Access Box (‘mfra’) +// Mandatory: No +// Quantity: Zero or one per track +type TrackFragmentRandomAccessBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + TrackID uint32 // 32 bits, is an integer identifying the track_ID. + Reserved uint32 // 26 bits, + LengthSizeOfTrafNum byte // 2 bits, indicates the length in byte of the traf_number field minus one. + LengthSizeOfTrunNum byte // 2 bits, indicates the length in byte of the trun_number field minus one. + LengthSizeOfSampleNum byte // 2 bits, indicates the length in byte of the sample_number field minus one. + NumberOfEntry uint32 // 32 bits, is an integer that gives the number of the entries for this track. If this value is zero, it indicates that every sample is a sync sample and no table entry follows. + Table []TrackFragmentRandomAccessTable // Track Fragment RandomAccess Table 1, NumberOfEntry elements. +} + +type TrackFragmentRandomAccessTable struct { + Time interface{} // uint32 or uint64, is 32 or 64 bits integer that indicates the presentation time of the sync sample in units defined in the ‘mdhd’ of the associated track. + Moofoffset interface{} // uint32 or uint64, is 32 or 64 bits integer that gives the offset of the ‘moof’ used in this entry. Offset is the byte-offset between the beginning of the file and the beginning of the ‘moof’. + TrafNumber interface{} // unsigned int((length_size_of_traf_num+1) * 8). indicates the ‘traf’ number that contains the sync sample. The number ranges from 1 (the first ‘traf’ is numbered 1) in each ‘moof’. + TrunNumber interface{} // unsigned int((length_size_of_trun_num+1) * 8). indicates the ‘trun’ number that contains the sync sample. The number ranges from 1 in each ‘traf’ + SampleNumber interface{} // unsigned int((length_size_of_sample_num+1) * 8) . indicates the sample number of the sync sample. The number ranges from 1 in each ‘trun’. +} + +// for(i=1; i <= number_of_entry; i++){ +// if(version==1){ +// unsigned int(64) time; +// unsigned int(64) moof_offset; +// }else{ +// unsigned int(32) time; +// unsigned int(32) moof_offset; +// } +// unsigned int((length_size_of_traf_num+1) * 8) traf_number; +// unsigned int((length_size_of_trun_num+1) * 8) trun_number; +// unsigned int((length_size_of_sample_num+1) * 8) sample_number; +// } + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/71 +// +// Box Type: mfro +// Container: Movie Fragment Random Access Box (‘mfra’) +// Mandatory: Yes +// Quantity: Exactly one +// +// The Movie Fragment Random Access Offset Box provides a copy of the length field from the enclosing Movie +// Fragment Random Access Box. It is placed last within that box, so that the size field is also last in the +// enclosing Movie Fragment Random Access Box. When the Movie Fragment Random Access Box is also last +// in the file this permits its easy location. The size field here must be correct. However, neither the presence of +// the Movie Fragment Random Access Box, nor its placement last in the file, are assured. +type MovieFragmentRandomAccessOffsetBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Size uint32 // 32 bits, is an integer gives the number of bytes of the enclosing ‘mfra’ box. This field is placed at the last of the enclosing box to assist readers scanning from the end of the file in finding the ‘mfra’ box. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/29 +// +// Box Type: mdat +// Container: File +// Mandatory: No +// Quantity: Zero or more +type MediaDataBox struct { + MP4BoxHeader // standard header + + Data []byte // 8 bits array, is the contained media data. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/29 +// +// Box Types: free, skip +// Container: File or other box +// Mandatory: No +// Quantity: Zero or more +// +// The contents of a free-space box are irrelevant and may be ignored, or the object deleted, without affecting +// the presentation. (Care should be exercised when deleting the object, as this may invalidate the offsets used +// in the sample table, unless this object is after all the media data). +type FreeSpaceBox struct { + MP4BoxHeader // standard header + + Data []uint8 // 8 bits array, +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/78 +// +// Box Type: cprt +// Container: User data box (‘udta’) +// Mandatory: No +// Quantity: Zero or more +// +// The Copyright box contains a copyright declaration which applies to the entire presentation, when contained +// within the Movie Box, or, when contained in a track, to that entire track. There may be multiple copyright +// boxes using different language codes. +type CopyrightBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Pad byte // 1 bit, + Language [2]byte // 15 bits, declares the language code for the following text. See ISO 639-2/T for the set of three character codes. Each character is packed as the difference between its ASCII value and 0x60. The code is confined to being three lower-case letters, so these values are strictly positive. + Notice string // string, is a null-terminated string in either UTF-8 or UTF-16 characters, giving a copyright notice. If UTF- 16 is used, the string shall start with the BYTE ORDER MARK (0xFEFF), to distinguish it from a UTF- 8 string. This mark does not form part of the final string. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/79 +// +// Box Type: tsel +// Container: User Data Box (‘udta’) +// Mandatory: No +// Quantity: Zero or One +// +// The track selection box is contained in the user data box of the track it modifies. +type TrackSelectionBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SwitchGroup int32 // 32 bits, is an integer that specifies a group or collection of tracks. If this field is 0 (default value) or if the Track Selection box is absent there is no information on whether the track can be used for switching during playing or streaming. If this integer is not 0 it shall be the same for tracks that can be used for switching between each other. Tracks that belong to the same switch group shall belong to the same alternate group. A switch group may have only one member. + AttributeList []uint32 // 32 bits array, to end of the box, is a list, to the end of the box, of attributes. The attributes in this list should be used as descriptions of tracks or differentiation criteria for tracks in the same alternate or switch group. Each differentiating attribute is associated with a pointer to the field or information that distinguishes the track. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/100 +// +// Box Type: strk +// Container: User Data box (‘udta’) of the corresponding Track box (‘trak’) +// Mandatory: No +// Quantity: Zero or more +// +// This box contains objects that define and provide information about a sub track in the present track. +type SubTrack struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/100 +// +// Box Type: stri +// Container: Sub Track box (‘strk’) +// Mandatory: Yes +// Quantity: One +type SubTrackInformation struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SwitchGroup int16 // 16 bits, + AlternateGroup int16 // 16 bits, + SubTrackID uint32 // 32 bits, is an integer. A non-zero value uniquely identifies the sub track locally within the track. A zero value (default) means that sub track ID is not assigned. + AttributeList []uint32 // 32 bits array, is a list, to the end of the box, of attributes. The attributes in this list should be used as descriptions of sub tracks or differentiating criteria for tracks and sub tracks in the same alternate or switch group +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/101 +// +// Box Type: strd +// Container: Sub Track box (‘strk’) +// Mandatory: Yes +// Quantity: One +// +// This box contains objects that provide a definition of the sub track. +type SubTrackDefinition struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/81 +// +// Box Type: meta +// Container: File, Movie Box (‘moov’), Track Box (‘trak’), or Additional Metadata Container Box (‘meco’) +// Mandatory: No +// Quantity: Zero or one (in File, ‘moov’, and ‘trak’), One or more (in ‘meco’) +type MetaBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + PrimaryResource PrimaryItemBox // optional + FileLocations DataInformationBox // optional + ItemLocations ItemLocationBox // optional + Protections ItemProtectionBox // optional + ItemInfos ItemInfoBox // optional + IPMPControl IPMPControlBox // optional + ItemRefs ItemReferenceBox // optional + ItemData ItemDataBox // optional + //OtherBoxes []Box // optional +} + +type IPMPControlBox struct{} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/82 +// +// Box Type: iloc +// Container: Meta box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +type ItemLocationBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + OffsetSize byte // 4 bits, + LengthSize byte // 4 bits, + BaseOffsetSize byte // 4 bits, + IndexSize byte // 4 bits, if version == 1, index_size replace to reserved. + ItemCount uint16 // 16 bits, + Table interface{} // version == 1 -> ItemLocationTable1 , version == 2 -> ItemLocationTable2, ItemCount elements. +} + +type ItemLocationTable1 struct { + ItemID uint16 // 16 bits, + Reserved uint16 // 12 bits, + ConstructionMethod byte // 4 bits, + DataReferenceIndex uint16 // 16 bits, + BaseOffset interface{} // unsigned int(base_offset_size*8), + ExtentCount uint16 // 16 bits, + ExtentTable []ItemLocationExtentTable1 // Item Location Extent Table1, ExtentCount elements. +} + +type ItemLocationTable2 struct { + ItemID uint16 // 16 bits, + DataReferenceIndex uint16 // 16 bits, + BaseOffset interface{} // unsigned int(base_offset_size*8), + ExtentCount uint16 // 16 bits, + ExtentTable []ItemLocationExtentTable2 // Item Location Extent Table2, ExtentCount elements. +} + +type ItemLocationExtentTable1 struct { + ExtentIndex interface{} // unsigned int(index_size*8) + ItemLocationExtentTable2 +} + +type ItemLocationExtentTable2 struct { + ExtentOffset interface{} // unsigned int(offset_size*8) + ExtentLength interface{} // unsigned int(length_size*8) +} + +// for (i=0; i 0)) { +// unsigned int(index_size*8) extent_index; +// } + +// unsigned int(offset_size*8) extent_offset; +// unsigned int(length_size*8) extent_length; +// } + +// offset_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the offset field. +// length_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the length field. +// base_offset_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the base_offset field. +// index_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the extent_index field. +// item_count : counts the number of resources in the following array. +// item_ID : is an arbitrary integer ‘name’ for this resource which can be used to refer to it (e.g. in a URL). +// construction_method : is taken from the set 0 (file), 1 (idat) or 2 (item) +// data-reference-index : is either zero (‘this file’) or a 1-based index into the data references in the data information box. +// base_offset : provides a base value for offset calculations within the referenced data. If +// base_offset_size : is 0, base_offset takes the value 0, i.e. it is unused. +// extent_count : provides the count of the number of extents into which the resource is fragmented; it must have the value 1 or greater +// extent_index : provides an index as defined for the construction method +// extent_offset : provides the absolute offset in bytes from the beginning of the containing file, of this item. If offset_size is 0, offset takes the value 0 +// extent_length : provides the absolute length in bytes of this metadata item. If length_size is 0, length takes the value 0. If the value is 0, then length of the item is the length of the entire referenced file. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/85 +// +// Box Type: ipro +// Container: Meta box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +// +// The item protection box provides an array of item protection information, for use by the Item Information Box. +type ItemProtectionBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + ProtectionCount uint16 // 16 bits, + Table []ItemProtectionTable // Item Protection Table, ProtectionCount elements. +} + +type ItemProtectionTable struct { + ProtectionInformation ProtectionSchemeInfoBox +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/92 +// +// Box Types: sinf +// Container: Protected Sample Entry, or Item Protection Box (‘ipro’) +// Mandatory: Yes +// Quantity: One or More +type ProtectionSchemeInfoBox struct { + MP4BoxHeader // standard header + + OriginalFormat OriginalFormatBox // + Type SchemeTypeBox // optional + Info SchemeInformationBox // optional +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/93 +// +// Box Types: frma +// Container: Protection Scheme Information Box (‘sinf’) or Restricted Scheme Information Box (‘rinf’) +// Mandatory: Yes when used in a protected sample entry or in a restricted sample entry +// Quantity: Exactly one +// +// The Original Format Box ‘frma’ contains the four-character-code of the original un-transformed sample description: + +type OriginalFormatBox struct { + MP4BoxHeader // standard header + + DataFormat uint32 // 32 bits, is the four-character-code of the original un-transformed sample entry (e.g. “mp4v” if the stream contains protected or restricted MPEG-4 visual material). +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/93 +// +// Box Types: schm +// Container: Protection Scheme Information Box (‘sinf’), Restricted Scheme Information Box (‘rinf’), +// or SRTP Process box (‘srpp‘) +// Mandatory: No +// +// Quantity: Zero or one in ‘sinf’, depending on the protection structure; Exactly one in ‘rinf’ and ‘srpp’ +// The Scheme Type Box (‘schm’) identifies the protection or restriction scheme. +type SchemeTypeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SchemeType uint32 // 32 bits, is the code defining the protection or restriction scheme. + SchemeVersion uint32 // 32 bits, is the version of the scheme (used to create the content) +} + +// if (flags & 0x000001) { +// unsigned int(8) scheme_uri[]; // browser uri +// } + +// scheme_URI : allows for the option of directing the user to a web-page if they do not have the scheme installed on their system. It is an absolute URI formed as a null-terminated string in UTF-8 characters. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/94 +// +// Box Types: schi +// Container: Protection Scheme Information Box (‘sinf’), Restricted Scheme Information Box (‘rinf’), +// or SRTP Process box (‘srpp‘) +// Mandatory: No +// Quantity: Zero or one +// The Scheme Information Box is a container Box that is only interpreted by the scheme being used. Any +// information the encryption or restriction system needs is stored here. The content of this box is a series of +// boxes whose type and format are defined by the scheme declared in the Scheme Type Box. +type SchemeInformationBox struct { + MP4BoxHeader // standard header + + SchemeSpecificData []SchemeTypeBox +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/85 +// +// Box Type: iinf +// Container: Meta Box (‘meta’) +// Mandatory: No +// Quantity: Zero or one + +type ItemInfoBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint16 // 16 bits, + ItemInfos []ItemInfoEntry // EntryCount elements. +} + +// Box Type: infe +type ItemInfoEntry struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + ItemID uint16 // 16 bits + ItemProtectionIndex uint16 // 16 bits + ItemType uint32 // 32 bits, + ItemName string // string, + ContentType string // string, + ContentEncoding string // string, optional + ItemUriType string // string, + ExtensionType uint32 // 32 bits, optional + ItemInfoExtension // optional +} + +type ItemInfoExtension struct { +} + +// if ((version == 0) || (version == 1)) { +// unsigned int(16) item_ID; +// unsigned int(16) item_protection_index +// string item_name; +// string content_type; +// string content_encoding; //optional +// } + +// if (version == 1) { +// unsigned int(32) extension_type; //optional +// ItemInfoExtension(extension_type); //optional +// } + +// if (version == 2) { +// unsigned int(16) item_ID; +// unsigned int(16) item_protection_index; +// unsigned int(32) item_type; +// string item_name; + +// if (item_type==’mime’) { +// string content_type; +// string content_encoding; //optional +// } else if (item_type == ‘uri ‘) { +// string item_uri_type; +// } +// } + +// item_id : contains either 0 for the primary resource (e.g., the XML contained in an ‘xml ‘ box) or the ID of the item for which the following information is defined. +// item_protection_index : contains either 0 for an unprotected item, or the one-based index into the item protection box defining the protection applied to this item (the first box in the item protection box has the index 1). +// item_name : is a null-terminated string in UTF-8 characters containing a symbolic name of the item (source file for file delivery transmissions). +// item_type : is a 32-bit value, typically 4 printable characters, that is a defined valid item type indicator, such as ‘mime’ +// content_type : is a null-terminated string in UTF-8 characters with the MIME type of the item. If the item is content encoded (see below), then the content type refers to the item after content decoding. +// item_uri_type : is a string that is an absolute URI, that is used as a type indicator. +// content_encoding : is an optional null-terminated string in UTF-8 characters used to indicate that the binary file is encoded and needs to be decoded before interpreted. The values are as defined for Content-Encoding for HTTP/1.1. Some possible values are “gzip”, “compress” and “deflate”. An empty string indicates no content encoding. Note that the item is stored after the content encoding has been applied. +// extension_type : is a printable four-character code that identifies the extension fields of version 1 with respect to version 0 of the Item information entry. +// content_location : is a null-terminated string in UTF-8 characters containing the URI of the file as defined in HTTP/1.1 (RFC 2616). +// content_MD5 : is a null-terminated string in UTF-8 characters containing an MD5 digest of the file. See HTTP/1.1 (RFC 2616) and RFC 1864. +// content_length : gives the total length (in bytes) of the (un-encoded) file. +// transfer_length : gives the total length (in bytes) of the (encoded) file. Note that transfer length is equal to content length if no content encoding is applied (see above). +// entry_count provides : a count of the number of entries in the following array. +// group_ID : indicates a file group to which the file item (source file) belongs. See 3GPP TS 26.346 for more details on file groups. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/82 +// +// Box Type: ‘xml ‘ or ‘bxml’ +// Container: Meta box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +// +// When the primary data is in XML format and it is desired that the XML be stored directly in the meta-box, one +// of these forms may be used. The Binary XML Box may only be used when there is a single well-defined +// binarization of the XML for that defined format as identified by the handler. +// Within an XML box the data is in UTF-8 format unless the data starts with a byte-order-mark (BOM), which +// indicates that the data is in UTF-16 format. +type XMLBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + XML string // string, +} + +type BinaryXMLBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + Data []uint8 // 8 bits array, +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/93 +// +// Box Type: pitm +// Container: Meta box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +// +// For a given handler, the primary data may be one of the referenced items when it is desired that it be stored +// elsewhere, or divided into extents; or the primary metadata may be contained in the meta-box (e.g. in an XML +// box). Either this box must occur, or there must be a box within the meta-box (e.g. an XML box) containing the +// primary information in the format required by the identified handler. +type PrimaryItemBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + ItemID uint16 // 16 bits, is the identifier of the primary item +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/95 +// +// Box Type: fiin +// Container: Meta Box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +// +// The FD item information box is optional, although it is mandatory for files using FD hint tracks. It provides +// information on the partitioning of source files and how FD hint tracks are combined into FD sessions. Each +// partition entry provides details on a particular file partitioning, FEC encoding and associated File and FEC +// reservoirs. It is possible to provide multiple entries for one source file (identified by its item ID) if alternative +// FEC encoding schemes or partitionings are used in the file. All partition entries are implicitly numbered and +// the first entry has number 1. +type FDItemInformationBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint16 // 16 bits, + PE []PartitionEntry // EntryCount elements. + FDSGB FDSessionGroupBox // optional + GidToNameB GroupIdToNameBox // optional +} + +// Box Type: paen +type PartitionEntry struct { + FPB FilePartitionBox // + FECRB FECReservoirBox //optional + FRB FileReservoirBox //optional +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/99 +// +// Box Type: fire +// Container: Partition Entry (‘paen’) +// Mandatory: No +// Quantity: Zero or One +// +// The File reservoir box associates the source file identified in the file partition box ('fpar') with File reservoirs +// stored as additional items. It contains a list that starts with the first File reservoir associated with the first +// source block of the source file and continues sequentially through the source blocks of the source file. +type FileReservoirBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint16 // 16 bits, gives the number of entries in the following list. An entry count here should match the total number or blocks in the corresponding file partition box. + Table []FileReservoirTable // EntryCount elements. +} + +type FileReservoirTable struct { + ItemID uint16 // 16 bits, indicates the location of the File reservoir associated with a source block. + SymbolCount uint32 // 32 bits, indicates the number of source symbols contained in the File reservoir. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/95 +// +// Box Type: fpar +// Container: Partition Entry (‘paen’) +// Mandatory: Yes +// Quantity: Exactly one +// +// The File Partition box identifies the source file and provides a partitioning of that file into source blocks and +// symbols. Further information about the source file, e.g., filename, content location and group IDs, is contained +// in the Item Information box ('iinf'), where the Item Information entry corresponding to the item ID of the +// source file is of version 1 and includes a File Delivery Item Information Extension ('fdel'). +type FilePartitionBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + ItemID uint16 // 16 bits, + PacketPayloadSize uint16 // 16 bits, + Reserved uint8 // 8 bits, + FECEncodingID uint8 // 8 bits, + FECInstanceID uint16 // 16 bits, + MaxSourceBlockLength uint16 // 16 bits, + EncodingSymbolLength uint16 // 16 bits, + MaxNumberOfEncodingSymbols uint16 // 16 bits, + SchemeSpecificInfo string // string, + EntryCount uint16 // 16 bits, + Tanble []FilePartitionTable //File Partition Table, EntryCount elements. +} + +type FilePartitionTable struct { + BlockCount uint16 // 16 bits, + BlockSize uint32 // 32 bits, +} + +// item_ID : references the item in the item location box ('iloc') that the file partitioning applies to. +// packet_payload_size : gives the target ALC/LCT or FLUTE packet payload size of the partitioning algorithm. Note that UDP packet payloads are larger, as they also contain ALC/LCT or FLUTE headers. +// FEC_encoding_ID : identifies the FEC encoding scheme and is subject to IANA registration (see RFC 5052). Note that i) value zero corresponds to the "Compact No-Code FEC scheme" also known as "Null-FEC" (RFC 3695); ii) value one corresponds to the “MBMS FEC” (3GPP TS 26.346); iii) for values in the range of 0 to 127, inclusive, the FEC scheme is Fully-Specified, whereas for values in the range of 128 to 255, inclusive, the FEC scheme is Under-Specified. +// FEC_instance_ID : provides a more specific identification of the FEC encoder being used for an UnderSpecified FEC scheme. This value should be set to zero for Fully-Specified FEC schemes and shall be ignored when parsing a file with FEC_encoding_ID in the range of 0 to 127, inclusive. FEC_instance_ID is scoped by the FEC_encoding_ID. See RFC 5052 for further details. +// max_source_block_length : gives the maximum number of source symbols per source block. +// encoding_symbol_length : gives the size (in bytes) of one encoding symbol. All encoding symbols of one item have the same length, except the last symbol which may be shorter. +// max_number_of_encoding_symbols : gives the maximum number of encoding symbols that can be generated for a source block for those FEC schemes in which the maximum number of encoding symbols is relevant, such as FEC encoding ID 129 defined in RFC 5052. For those FEC schemes in which the maximum number of encoding symbols is not relevant, the semantics of this field is unspecified. +// scheme_specific_info : is a base64-encoded null-terminated string of the scheme-specific object transfer information (FEC-OTI-Scheme-Specific-Info). The definition of the information depends on the FEC encoding ID. +// entry_count : gives the number of entries in the list of (block_count, block_size) pairs that provides a partitioning of the source file. Starting from the beginning of the file, each entry indicates how the next segment of the file is divided into source blocks and source symbols. +// block_count : indicates the number of consecutive source blocks of size block_size. +// block_size : indicates the size of a block (in bytes). A block_size that is not a multiple of the encoding_symbol_length symbol size indicates with Compact No-Code FEC that the last source symbols includes padding that is not stored in the item. With MBMS FEC (3GPP TS 26.346) the padding may extend across multiple symbols but the size of padding should never be more than encoding_symbol_length. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/97 +// +// Box Type: fecr +// Container: Partition Entry (‘paen’) +// Mandatory: No +// Quantity: Zero or One +// +// The FEC reservoir box associates the source file identified in the file partition box ('fpar') with FEC +// reservoirs stored as additional items. It contains a list that starts with the first FEC reservoir associated with +// the first source block of the source file and continues sequentially through the source blocks of the source file. +type FECReservoirBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint16 // 16 bits, + Table []FECReservoirTable // FEC Reservoir Table, EntryCount elements. +} + +type FECReservoirTable struct { + ItemID uint16 // 16 bits, indicates the location of the FEC reservoir associated with a source block. + SymbolCount uint32 // 32 bits, indicates the number of repair symbols contained in the FEC reservoir. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/97 +// +// Box Type: segr +// Container: FD Information Box (‘fiin’) +// Mandatory: No +// Quantity: Zero or One +type FDSessionGroupBox struct { + MP4BoxHeader // standard header + + NumSessionGroups uint16 // 16 bits, + Table []FDSessionGroupTable // FD Session Group Table, NumSessionGroups elements. +} + +type FDSessionGroupTable struct { + EntryCount uint8 // 8 bits, + GIDTable []FDSessionGroupIDTable // FDSession Group ID Table, EntryCount elements. + NumChannelsInSessionGroup uint16 // 16 bits + HTIDTable []FDSessionHintTrackIDTable // FDSession Hint Track ID Table, NumChannelsInSessionGroup elements. +} + +type FDSessionGroupIDTable struct { + GroupID uint32 // 32 bits +} + +type FDSessionHintTrackIDTable struct { + HintTrackID uint32 // 32 bits +} + +// for(i=0; i < num_session_groups; i++) { +// unsigned int(8) entry_count; + +// for (j=0; j < entry_count; j++) { +// unsigned int(32) group_ID; +// } + +// unsigned int(16) num_channels_in_session_group; + +// for(k=0; k < num_channels_in_session_group; k++) { +// unsigned int(32) hint_track_id; +// } +// } + +// num_session_groups : specifies the number of session groups. +// entry_count : gives the number of entries in the following list comprising all file groups that the session group complies with. The session group contains all files included in the listed file groups as specified by the item information entry of each source file. Note that the FDT for the session group should only contain those groups that are listed in this structure. +// group_ID : indicates a file group that the session group complies with. +// num_channels_in_session_groups : specifies the number of channels in the session group. The value of num_channels_in_session_groups shall be a positive integer. +// hint_track_ID : specifies the track ID of the FD hint track belonging to a particular session group. Note that one FD hint track corresponds to one LCT channel. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/98 +// +// Box Type: gitn +// Container: FD Information Box (‘fiin’) +// Mandatory: No +// Quantity: Zero or One +// +// The Group ID to Name box associates file group names to file group IDs used in the version 1 item +// information entries in the item information box ('iinf'). +type GroupIdToNameBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + EntryCount uint16 // 16 bits, gives the number of entries in the following list. + Table []GroupIdToNameBox // Group Id To Name Table, EntryCount elements. +} + +type GroupIdToNameTable struct { + GroupID uint32 // 32 bits, indicates a file group. + GroupName string // string, is a null-terminated string in UTF-8 characters containing a file group name. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/90 +// +// Box Type: idat +// Container: Metadata box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +// +// This box contains the data of metadata items that use the construction method indicating that an item’s data +// extents are stored within this box. +type ItemDataBox struct { + MP4BoxHeader // standard header + + Data []byte // 8 bits array, is the contained meta data +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/91 +// +// Box Type: iref +// Container: Metadata box (‘meta’) +// Mandatory: No +// Quantity: Zero or one +// +// The item reference box allows the linking of one item to others via typed references. All the references for one +// item of a specific type are collected into a single item type reference box, whose type is the reference type, +// and which has a ‘from item ID’ field indicating which item is linked. The items linked to are then represented by +// an array of ‘to item ID’s. All these single item type reference boxes are then collected into the item reference +// box. The reference types defined for the track reference box defined in 8.3.3 may be used here if appropriate, +// or other registered reference types. +type ItemReferenceBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SITRB []SingleItemTypeReferenceBox +} + +type SingleItemTypeReferenceBox struct { + MP4BoxHeader // standard header + + FromItemID uint16 // 16 bits, contains the ID of the item that refers to other items + ReferenceCount uint16 // 16 bits, is the number of references + Table []SingleItemTypeReferenceTable // Single Item Type Reference Table, ReferenceCount elements. +} + +type SingleItemTypeReferenceTable struct { + ToItemID uint16 // 16 bits, contains the ID of the item referred to +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/87 +// +// Box Type: meco +// Container: File, Movie Box (‘moov’), or Track Box (‘trak’) +// Mandatory: No +// Quantity: Zero or one +type AdditionalMetadataContainerBox struct { + MP4BoxHeader // standard header +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/88 +// +// Box Type: mere +// Container: Additional Metadata Container Box (‘meco’) +// Mandatory: No +// Quantity: Zero or more +// +// The metabox relation box indicates a relation between two meta boxes at the same level, i.e., the top level of +// the file, the Movie Box, or Track Box. The relation between two meta boxes is unspecified if there is no +// metabox relation box for those meta boxes. Meta boxes are referenced by specifying their handler types. +type MetaboxRelationBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + FirstMetaboxHandlerType uint32 // 32 bits, indicates the first meta box to be related. + SecondMetaboxHandlerType uint32 // 32 bits, indicates the second meta box to be related. + MetaboxRelation uint8 // 8 bits, indicates the relation between the two meta boxes. +} + +// metabox_relation indicates the relation between the two meta boxes. The following values are defined: +// 1 The relationship between the boxes is unknown (which is the default when this box is not present); +// 2 the two boxes are semantically un-related (e.g., one is presentation, the other annotation); +// 3 the two boxes are semantically related but complementary (e.g., two disjoint sets of meta-data expressed in two different meta-data systems); +// 4 the two boxes are semantically related but overlap (e.g., two sets of meta-data neither of which is a subset of the other); neither is ‘preferred’ to the other; +// 5 the two boxes are semantically related but the second is a proper subset or weaker version of the first; the first is preferred; +// 6 the two boxes are semantically related and equivalent (e.g., two essentially identical sets of meta-data expressed in two different meta-data systems). + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/105 +// +// Box Type: styp +// Container: File +// Mandatory: No +// Quantity: Zero or more +// +// If segments are stored in separate files (e.g. on a standard HTTP server) it is recommended that these +// ‘segment files’ contain a segment-type box, which must be first if present, to enable identification of those files, +// and declaration of the specifications with which they are compliant. +// A segment type has the same format as an 'ftyp' box [4.3], except that it takes the box type 'styp'. The +// brands within it may include the same brands that were included in the 'ftyp' box that preceded the +// ‘moov’ box, and may also include additional brands to indicate the compatibility of this segment with various +// specification(s). +// Valid segment type boxes shall be the first box in a segment. Segment type boxes may be removed if +// segments are concatenated (e.g. to form a full file), but this is not required. Segment type boxes that are not +// first in their files may be ignored. + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/106 +// +// Box Type: sidx +// Container: File +// Mandatory: No +// Quantity: Zero or more +type SegmentIndexBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + ReferenceID uint32 // 32 bits, + TimeScale uint32 // 32 bits, + EarliestPresentationTime interface{} // uint32 or uint64, + FirstOffset interface{} // uint32 or uint64, + Reserved uint16 // 16 bits, + ReferenceCount uint16 // 16 bits, + Table []SegmentIndexTable // Segment Index Table, ReferenceCount elements +} + +type SegmentIndexTable struct { + ReferenceType byte // 1 bit + ReferencedSize uint32 // 32 bits + SubSegmentDuration uint32 // 32 bits, + StartsWithSAP byte // 1 bit + SAPType byte // 3 bits, + SAPDeltaTime uint32 // 28 bits, +} + +// if (version==0) { +// unsigned int(32) earliest_presentation_time; +// unsigned int(32) first_offset; +// } +// else { +// unsigned int(64) earliest_presentation_time; +// unsigned int(64) first_offset; +// } + +// unsigned int(16) reserved = 0; +// unsigned int(16) reference_count; + +// for(i=1; i <= reference_count; i++) +// { +// bit (1) reference_type; +// unsigned int(31) referenced_size; +// unsigned int(32) subsegment_duration; +// bit(1) starts_with_SAP; +// unsigned int(3) SAP_type; +// unsigned int(28) SAP_delta_time; +// } + +// reference_ID : provides the stream ID for the reference stream; if this Segment Index box is referenced from a “parent” Segment Index box, the value of reference_ID shall be the same as the value of reference_ID of the “parent” Segment Index box; +// timescale : provides the timescale, in ticks per second, for the time and duration fields within this box; it is recommended that this match the timescale of the reference stream or track; for files based on this specification, that is the timescale field of the Media Header Box of the track; +// earliest_presentation_time : is the earliest presentation time of any access unit in the reference stream in the first subsegment, in the timescale indicated in the timescale field; +// first_offset : is the distance in bytes, in the file containing media, from the anchor point, to the first byte of the indexed material; +// reference_count : provides the number of referenced items; +// reference_type : when set to 1 indicates that the reference is to a segment index (‘sidx’) box; otherwise the reference is to media content (e.g., in the case of files based on this specification, to a movie fragment box); if a separate index segment is used, then entries with reference type 1 are in the index segment, and entries with reference type 0 are in the media file; +// referenced_size : the distance in bytes from the first byte of the referenced item to the first byte of the next referenced item, or in the case of the last entry, the end of the referenced material; +// subsegment_duration : when the reference is to Segment Index box, this field carries the sum of the subsegment_duration fields in that box; when the reference is to a subsegment, this field carries the difference between the earliest presentation time of any access unit of the reference stream in the next subsegment (or the first subsegment of the next segment, if this is the last subsegment of the segment, or the end presentation time of the reference stream if this is the last subsegment of the stream) and the earliest presentation time of any access unit of the reference stream in the referenced subsegment; the duration is in the same units as earliest_presentation_time; +// starts_with_SAP : indicates whether the referenced subsegments start with a SAP. For the detailed semantics of this field in combination with other fields, see the table below. +// SAP_type : indicates a SAP type as specified in Annex I, or the value 0. Other type values are reserved. For the detailed semantics of this field in combination with other fields, see the table below. +// SAP_delta_time : indicates TSAP of the first SAP, in decoding order, in the referenced subsegment for the reference stream. If the referenced subsegments do not contain a SAP, SAP_delta_time is reserved with the value 0; otherwise SAP_delta_time is the difference between the earliest presentation time of the subsegment, and the TSAP (note that this difference may be zero, in the case that the subsegment starts with a SAP). + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/109 +// +// Box Type: ssix +// Container: File +// Mandatory: No +// Quantity: Zero or more +type SubsegmentIndexBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + SubSegmentCount uint32 // 32 bits, is a positive integer specifying the number of subsegments for which partial subsegment information is specified in this box. subsegment_count shall be equal to reference_count (i.e., the number of movie fragment references) in the immediately preceding Segment Index box. + Table []SubsegmentIndexTable // Subsegment Index Table, SubSegmentCount elements. +} + +type SubsegmentIndexTable struct { + RangesCount uint32 // 32 bits, specifies the number of partial subsegment levels into which the media data is grouped. This value shall be greater than or equal to 2. + Rtable []SubsegmentRangesTable // Subsegment Ranges Table, RangesCount elements. +} + +type SubsegmentRangesTable struct { + level uint8 // 8 bits, specifies the level to which this partial subsegment is assigned. + range_size [3]byte // 24 bits, indicates the size of the partial subsegment. +} + +// ------------------------------------------------------------------------------------------------------- + +// +// ISO_IEC_14496-12_2012.pdf Page/111 +// +// Box Type: prft +// Container: File +// Mandatory: No +// Quantity: Zero or more +type ProducerReferenceTimeBox struct { + MP4BoxHeader // standard header + MP4FullBoxHeader // full box header + + ReferenceTrackID uint32 // 32 bits, provides the track_ID for the reference track. + NtpTimestamp uint64 // 64 bits, indicates a UTC time in NTP format corresponding to decoding_time. + MediaTime interface{} // uint32 or uint64, corresponds to the same time as ntp_timestamp, but in the time units used for the reference track, and is measured on this media clock as the media is produced. +} + +// if (version==0) { +// unsigned int(32) media_time; +// } else { +// unsigned int(64) media_time; +// } + +// ------------------------------------------------------------------------------------------------------- diff --git a/avformat/mpegts/mpegts.go b/avformat/mpegts/mpegts.go new file mode 100644 index 0000000..18506d0 --- /dev/null +++ b/avformat/mpegts/mpegts.go @@ -0,0 +1,578 @@ +package mpegts + +import ( + "bytes" + "errors" + "io" + "io/ioutil" + + "github.com/Monibuca/engine/util" + //"sync" +) + +// NALU AUD 00 00 00 01 09 F0 + +const ( + TS_PACKET_SIZE = 188 + TS_DVHS_PACKET_SIZE = 192 + TS_FEC_PACKET_SIZE = 204 + + TS_MAX_PACKET_SIZE = 204 + + PID_PAT = 0x0000 + PID_CAT = 0x0001 + PID_TSDT = 0x0002 + PID_RESERVED1 = 0x0003 + PID_RESERVED2 = 0x000F + PID_NIT_ST = 0x0010 + PID_SDT_BAT_ST = 0x0011 + PID_EIT_ST = 0x0012 + PID_RST_ST = 0x0013 + PID_TDT_TOT_ST = 0x0014 + PID_NET_SYNC = 0x0015 + PID_RESERVED3 = 0x0016 + PID_RESERVED4 = 0x001B + PID_SIGNALLING = 0x001C + PID_MEASURE = 0x001D + PID_DIT = 0x001E + PID_SIT = 0x001F + // 0x0003 - 0x000F Reserved + // 0x0010 - 0x1FFE May be assigned as network_PID, Program_map_PID, elementary_PID, or for other purposes + // 0x1FFF Null Packet + + // program_association_section + // conditional_access_section + // TS_program_map_section + // TS_description_section + // ISO_IEC_14496_scene_description_section + // ISO_IEC_14496_object_descriptor_section + // Metadata_section + // IPMP_Control_Information_section (defined in ISO/IEC 13818-11) + TABLE_PAS = 0x00 + TABLE_CAS = 0x01 + TABLE_TSPMS = 0x02 + TABLE_TSDS = 0x03 + TABLE_ISO_IEC_14496_SDC = 0x04 + TABLE_ISO_IEC_14496_ODC = 0x05 + TABLE_MS = 0x06 + TABLE_IPMP_CIS = 0x07 + // 0x06 - 0x37 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 reserved + // 0x38 - 0x3F Defined in ISO/IEC 13818-6 + // 0x40 - 0xFE User private + // 0xFF Forbidden + + STREAM_TYPE_H264 = 0x1B + STREAM_TYPE_AAC = 0x0F + + // 1110 xxxx + // 110x xxxx + STREAM_ID_VIDEO = 0xE0 // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC14496-2 video stream number xxxx + STREAM_ID_AUDIO = 0xC0 // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC14496-3 audio stream number x xxxx + + PAT_PKT_TYPE = 0 + PMT_PKT_TYPE = 1 + PES_PKT_TYPE = 2 +) + +// +// MPEGTS -> PAT + PMT + PES +// ES -> PES -> TS +// + +type MpegTsStream struct { + firstTsPkt *MpegTsPacket // 每一帧的第一个TS包 + patPkt *MpegTsPacket // 装载PAT的TS包 + pmtPkt *MpegTsPacket // 装载PMT的TS包 + pat *MpegTsPAT // PAT表信息 + pmt *MpegTsPMT // PMT表信息 + closed bool //是否已经关闭 + TsPesPktChan chan *MpegTsPesStream // TS + PES Packet Channel,将封装的每一帧ES数据,通过channel来传输 +} + +func NewMpegTsStream(bufferLength int) (ts *MpegTsStream) { + ts = new(MpegTsStream) + ts.firstTsPkt = new(MpegTsPacket) + ts.patPkt = new(MpegTsPacket) + ts.pmtPkt = new(MpegTsPacket) + ts.pat = new(MpegTsPAT) + ts.pmt = new(MpegTsPMT) + ts.TsPesPktChan = make(chan *MpegTsPesStream, bufferLength) + return +} + +// ios13818-1-CN.pdf 33/165 +// +// TS +// + +// Packet == Header + Payload == 188 bytes +type MpegTsPacket struct { + Header MpegTsHeader + Payload []byte +} + +// 前面32bit的数据即TS分组首部,它指出了这个分组的属性 +type MpegTsHeader struct { + SyncByte byte // 8 bits 同步字节,固定为0x47,表示后面是一个TS分组 + TransportErrorIndicator byte // 1 bit 传输错误标志位 + PayloadUnitStartIndicator byte // 1 bit 负载单元开始标志(packet不满188字节时需填充).为1时,表示在4个字节后,有一个调整字节 + TransportPriority byte // 1 bit 传输优先级 + Pid uint16 // 13 bits Packet ID号码,唯一的号码对应不同的包.为0表示携带的是PAT表 + TransportScramblingControl byte // 2 bits 加密标志位(00:未加密;其他表示已加密) + AdaptionFieldControl byte // 2 bits 附加区域控制.表示TS分组首部后面是否跟随有调整字段和有效负载.01仅含有效负载(没有adaptation_field),10仅含调整字段(没有Payload),11含有调整字段和有效负载(有adaptation_field,adaptation_field之后是Payload).为00的话解码器不进行处理.空分组没有调整字段 + ContinuityCounter byte // 4 bits 包递增计数器.范围0-15,具有相同的PID的TS分组传输时每次加1,到15后清0.不过,有些情况下是不计数的. + + MpegTsHeaderAdaptationField +} + +// 调整字段,只可能出现在每一帧的开头(当含有pcr的时候),或者结尾(当不满足188个字节的时候) +// adaptionFieldControl 00 -> 高字节代表调整字段, 低字节代表负载字段 0x20 0x10 +// PCR字段编码在MPEG-2 TS包的自适应字段(Adaptation field)的6个Byte中,其中6 bits为预留位,42 bits为有效位() +// MpegTsHeaderAdaptationField + stuffing bytes +type MpegTsHeaderAdaptationField struct { + AdaptationFieldLength byte // 8bits 本区域除了本字节剩下的长度(不包含本字节!!!切记), if adaptationFieldLength > 0, 那么就有下面8个字段. adaptation_field_length 值必须在 0 到 182 的区间内.当 adaptation_field_control 值为'10'时,adaptation_field_length 值必须为 183 + DiscontinuityIndicator byte // 1bit 置于"1"时,指示当前传输流包的不连续性状态为真.当 discontinuity_indicator 设置为"0"或不存在时,不连续性状态为假.不连续性指示符用于指示两种类型的不连续性,系统时间基不连续性和 continuity_counter 不连续性. + RandomAccessIndicator byte // 1bit 指示当前的传输流包以及可能的具有相同 PID 的后续传输流包,在此点包含有助于随机接入的某些信息.特别的,该比特置于"1"时,在具有当前 PID 的传输流包的有效载荷中起始的下一个 PES 包必须包含一个 discontinuity_indicator 字段中规定的基本流接入点.此外,在视频情况中,显示时间标记必须在跟随基本流接入点的第一图像中存在 + ElementaryStreamPriorityIndicator byte // 1bit 在具有相同 PID 的包之间,它指示此传输流包有效载荷内承载的基本流数据的优先级.1->指示该有效载荷具有比其他传输流包有效载荷更高的优先级 + PCRFlag byte // 1bit 1->指示 adaptation_field 包含以两部分编码的 PCR 字段.0->指示自适应字段不包含任何 PCR 字段 + OPCRFlag byte // 1bit 1->指示 adaptation_field 包含以两部分编码的 OPCR字段.0->指示自适应字段不包含任何 OPCR 字段 + SplicingPointFlag byte // 1bit 1->指示 splice_countdown 字段必须在相关自适应字段中存在,指定拼接点的出现.0->指示自适应字段中 splice_countdown 字段不存在 + TrasportPrivateDataFlag byte // 1bit 1->指示自适应字段包含一个或多个 private_data 字节.0->指示自适应字段不包含任何 private_data 字节 + AdaptationFieldExtensionFlag byte // 1bit 1->指示自适应字段扩展的存在.0->指示自适应字段中自适应字段扩展不存在 + + // Optional Fields + ProgramClockReferenceBase uint64 // 33 bits pcr + Reserved1 byte // 6 bits + ProgramClockReferenceExtension uint16 // 9 bits + OriginalProgramClockReferenceBase uint64 // 33 bits opcr + Reserved2 byte // 6 bits + OriginalProgramClockReferenceExtension uint16 // 9 bits + SpliceCountdown byte // 8 bits + TransportPrivateDataLength byte // 8 bits 指定紧随传输private_data_length 字段的 private_data 字节数. private_data 字节数不能使专用数据扩展超出自适应字段的范围 + PrivateDataByte byte // 8 bits 不通过 ITU-T|ISO/IEC 指定 + AdaptationFieldExtensionLength byte // 8 bits 指定紧随此字段的扩展的自适应字段数据的字节数,包括要保留的字节(如果存在) + LtwFlag byte // 1 bit 1->指示 ltw_offset 字段存在 + PiecewiseRateFlag byte // 1 bit 1->指示 piecewise_rate 字段存在 + SeamlessSpliceFlag byte // 1 bit 1->指示 splice_type 以及 DTS_next_AU 字段存在. 0->指示无论是 splice_type 字段还是 DTS_next_AU 字段均不存在 + + // Optional Fields + LtwValidFlag byte // 1 bit 1->指示 ltw_offset 的值必将生效.0->指示 ltw_offset 字段中该值未定义 + LtwOffset uint16 // 15 bits 其值仅当 ltw_valid 标志字段具有'1'值时才定义.定义时,法定时间窗补偿以(300/fs)秒为度量单位,其中 fs 为此 PID 所归属的节目的系统时钟频率 + Reserved3 byte // 2 bits 保留 + PiecewiseRate uint32 // 22 bits 只要当 ltw_flag 和 ltw_valid_flag 均置于‘1’时,此 22 比特字段的含义才确定 + SpliceType byte // 4 bits + DtsNextAU uint64 // 33 bits (解码时间标记下一个存取单元) + + // stuffing bytes + // 此为固定的 8 比特值等于'1111 1111',能够通过编码器插入.它亦能被解码器丢弃 +} + +// ios13818-1-CN.pdf 77 +// +// Descriptor +// + +type MpegTsDescriptor struct { + Tag byte // 8 bits 标识每一个描述符 + Length byte // 8 bits 指定紧随 descriptor_length 字段的描述符的字节数 + Data []byte +} + +func ReadTsPacket(r io.Reader) (packet MpegTsPacket, err error) { + lr := &io.LimitedReader{R: r, N: TS_PACKET_SIZE} + + // header + packet.Header, err = ReadTsHeader(lr) + if err != nil { + return + } + + // payload + packet.Payload = make([]byte, lr.N) + _, err = lr.Read(packet.Payload) + if err != nil { + return + } + + return +} + +func ReadTsHeader(r io.Reader) (header MpegTsHeader, err error) { + var h uint32 + + // MPEGTS Header 4个字节 + h, err = util.ReadByteToUint32(r, true) + if err != nil { + return + } + + // payloadUnitStartIndicator + // 为1时,表示在4个字节后,有一个调整字节.包头后需要除去一个字节才是有效数据(payload_unit_start_indicator="1") + // header.payloadUnitStartIndicator = uint8(h & 0x400000) + + // | 1111 1111 | 0000 0000 | 0000 0000 | 0000 0000 | + if (h&0xff000000)>>24 != 0x47 { + err = errors.New("mpegts header sync error!") + return + } + + // | 1111 1111 | 0000 0000 | 0000 0000 | 0000 0000 | + header.SyncByte = byte((h & 0xff000000) >> 24) + + // | 0000 0000 | 1000 0000 | 0000 0000 | 0000 0000 | + header.TransportErrorIndicator = byte((h & 0x800000) >> 23) + + // | 0000 0000 | 0100 0000 | 0000 0000 | 0000 0000 | + header.PayloadUnitStartIndicator = byte((h & 0x400000) >> 22) + + // | 0000 0000 | 0010 0000 | 0000 0000 | 0000 0000 | + header.TransportPriority = byte((h & 0x200000) >> 21) + + // | 0000 0000 | 0001 1111 | 1111 1111 | 0000 0000 | + header.Pid = uint16((h & 0x1fff00) >> 8) + + // | 0000 0000 | 0000 0000 | 0000 0000 | 1100 0000 | + header.TransportScramblingControl = byte((h & 0xc0) >> 6) + + // | 0000 0000 | 0000 0000 | 0000 0000 | 0011 0000 | + // 0x30 , 0x20 -> adaptation_field, 0x10 -> Payload + header.AdaptionFieldControl = byte((h & 0x30) >> 4) + + // | 0000 0000 | 0000 0000 | 0000 0000 | 0000 1111 | + header.ContinuityCounter = byte(h & 0xf) + + // | 0010 0000 | + // adaptionFieldControl + // 表示TS分组首部后面是否跟随有调整字段和有效负载. + // 01仅含有效负载(没有adaptation_field) + // 10仅含调整字段(没有Payload) + // 11含有调整字段和有效负载(有adaptation_field,adaptation_field之后是Payload). + // 为00的话解码器不进行处理.空分组没有调整字段 + // 当值为'11时,adaptation_field_length 值必须在0 到182 的区间内. + // 当值为'10'时,adaptation_field_length 值必须为183. + // 对于承载PES 包的传输流包,只要存在欠充足的PES 包数据就需要通过填充来完全填满传输流包的有效载荷字节. + // 填充通过规定自适应字段长度比自适应字段中数据元的长度总和还要长来实现,以致于自适应字段在完全容纳有效的PES 包数据后,有效载荷字节仍有剩余.自适应字段中额外空间采用填充字节填满. + if header.AdaptionFieldControl >= 2 { + // adaptationFieldLength + header.AdaptationFieldLength, err = util.ReadByteToUint8(r) + if err != nil { + return + } + + if header.AdaptationFieldLength > 0 { + lr := &io.LimitedReader{R: r, N: int64(header.AdaptationFieldLength)} + + // discontinuityIndicator(1) + // randomAccessIndicator(1) + // elementaryStreamPriorityIndicator + // PCRFlag + // OPCRFlag + // splicingPointFlag + // trasportPrivateDataFlag + // adaptationFieldExtensionFlag + var flags uint8 + flags, err = util.ReadByteToUint8(lr) + if err != nil { + return + } + + header.DiscontinuityIndicator = flags & 0x80 + header.RandomAccessIndicator = flags & 0x40 + header.ElementaryStreamPriorityIndicator = flags & 0x20 + header.PCRFlag = flags & 0x10 + header.OPCRFlag = flags & 0x08 + header.SplicingPointFlag = flags & 0x04 + header.TrasportPrivateDataFlag = flags & 0x02 + header.AdaptationFieldExtensionFlag = flags & 0x01 + + // randomAccessIndicator + // 在此点包含有助于随机接入的某些信息. + // 特别的,该比特置于"1"时,在具有当前 PID 的传输流包的有效载荷中起始的下一个 PES 包必须包含一个 discontinuity_indicator 字段中规定的基本流接入点. + // 此外,在视频情况中,显示时间标记必须在跟随基本流接入点的第一图像中存在 + if header.RandomAccessIndicator != 0 { + } + + // PCRFlag + // 1->指示 adaptation_field 包含以两部分编码的 PCR 字段. + // 0->指示自适应字段不包含任何 PCR 字段 + if header.PCRFlag != 0 { + var pcr uint64 + pcr, err = util.ReadByteToUint48(lr, true) + if err != nil { + return + } + + // PCR(i) = PCR_base(i)*300 + PCR_ext(i) + // afd.programClockReferenceBase * 300 + afd.programClockReferenceExtension + header.ProgramClockReferenceBase = pcr >> 15 // 9 bits + 6 bits + header.ProgramClockReferenceExtension = uint16(pcr & 0x1ff) // 9 bits -> | 0000 0001 | 1111 1111 | + } + + // OPCRFlag + if header.OPCRFlag != 0 { + var opcr uint64 + opcr, err = util.ReadByteToUint48(lr, true) + if err != nil { + return + } + + // OPCR(i) = OPCR_base(i)*300 + OPCR_ext(i) + // afd.originalProgramClockReferenceBase * 300 + afd.originalProgramClockReferenceExtension + header.OriginalProgramClockReferenceBase = opcr >> 15 // 9 bits + 6 bits + header.OriginalProgramClockReferenceExtension = uint16(opcr & 0x1ff) // 9 bits -> | 0000 0001 | 1111 1111 | + } + + // splicingPointFlag + // 1->指示 splice_countdown 字段必须在相关自适应字段中存在,指定拼接点的出现. + // 0->指示自适应字段中 splice_countdown 字段不存在 + if header.SplicingPointFlag != 0 { + header.SpliceCountdown, err = util.ReadByteToUint8(lr) + if err != nil { + return + } + } + + // trasportPrivateDataFlag + // 1->指示自适应字段包含一个或多个 private_data 字节. + // 0->指示自适应字段不包含任何 private_data 字节 + if header.TrasportPrivateDataFlag != 0 { + header.TransportPrivateDataLength, err = util.ReadByteToUint8(lr) + if err != nil { + return + } + + // privateDataByte + b := make([]byte, header.TransportPrivateDataLength) + if _, err = lr.Read(b); err != nil { + return + } + } + + // adaptationFieldExtensionFlag + if header.AdaptationFieldExtensionFlag != 0 { + } + + // 消耗掉剩下的数据,我们不关心 + if lr.N > 0 { + // Discard 是一个 io.Writer,对它进行的任何 Write 调用都将无条件成功 + // 但是ioutil.Discard不记录copy得到的数值 + // 用于发送需要读取但不想存储的数据,目的是耗尽读取端的数据 + if _, err = io.CopyN(ioutil.Discard, lr, int64(lr.N)); err != nil { + return + } + } + + } + } + + return +} + +func WriteTsHeader(w io.Writer, header MpegTsHeader) (written int, err error) { + if header.SyncByte != 0x47 { + err = errors.New("mpegts header sync error!") + return + } + + h := uint32(header.SyncByte)<<24 + uint32(header.TransportErrorIndicator)<<23 + uint32(header.PayloadUnitStartIndicator)<<22 + uint32(header.TransportPriority)<<21 + uint32(header.Pid)<<8 + uint32(header.TransportScramblingControl)<<6 + uint32(header.AdaptionFieldControl)<<4 + uint32(header.ContinuityCounter) + if err = util.WriteUint32ToByte(w, h, true); err != nil { + return + } + + written += 4 + + if header.AdaptionFieldControl >= 2 { + // adaptationFieldLength(8) + if err = util.WriteUint8ToByte(w, header.AdaptationFieldLength); err != nil { + return + } + + written += 1 + + if header.AdaptationFieldLength > 0 { + + // discontinuityIndicator(1) + // randomAccessIndicator(1) + // elementaryStreamPriorityIndicator(1) + // PCRFlag(1) + // OPCRFlag(1) + // splicingPointFlag(1) + // trasportPrivateDataFlag(1) + // adaptationFieldExtensionFlag(1) + threeIndicatorFiveFlags := uint8(header.DiscontinuityIndicator<<7) + uint8(header.RandomAccessIndicator<<6) + uint8(header.ElementaryStreamPriorityIndicator<<5) + uint8(header.PCRFlag<<4) + uint8(header.OPCRFlag<<3) + uint8(header.SplicingPointFlag<<2) + uint8(header.TrasportPrivateDataFlag<<1) + uint8(header.AdaptationFieldExtensionFlag) + if err = util.WriteUint8ToByte(w, threeIndicatorFiveFlags); err != nil { + return + } + + written += 1 + + // PCR(i) = PCR_base(i)*300 + PCR_ext(i) + if header.PCRFlag != 0 { + pcr := header.ProgramClockReferenceBase<<15 | 0x3f<<9 | uint64(header.ProgramClockReferenceExtension) + if err = util.WriteUint48ToByte(w, pcr, true); err != nil { + return + } + + written += 6 + } + + // OPCRFlag + if header.OPCRFlag != 0 { + opcr := header.OriginalProgramClockReferenceBase<<15 | 0x3f<<9 | uint64(header.OriginalProgramClockReferenceExtension) + if err = util.WriteUint48ToByte(w, opcr, true); err != nil { + return + } + + written += 6 + } + } + + } + + return +} + +// +//func (s *MpegTsStream) TestWrite(fileName string) error { +// +// if fileName != "" { +// file, err := os.Create(fileName) +// if err != nil { +// panic(err) +// } +// defer file.Close() +// +// patTsHeader := []byte{0x47, 0x40, 0x00, 0x10} +// +// if err := WritePATPacket(file, patTsHeader, *s.pat); err != nil { +// panic(err) +// } +// +// // TODO:这里的pid应该是由PAT给的 +// pmtTsHeader := []byte{0x47, 0x41, 0x00, 0x10} +// +// if err := WritePMTPacket(file, pmtTsHeader, *s.pmt); err != nil { +// panic(err) +// } +// } +// +// var videoFrame int +// var audioFrame int +// for { +// tsPesPkt, ok := <-s.TsPesPktChan +// if !ok { +// fmt.Println("frame index, video , audio :", videoFrame, audioFrame) +// break +// } +// +// if tsPesPkt.PesPkt.Header.StreamID == STREAM_ID_AUDIO { +// audioFrame++ +// } +// +// if tsPesPkt.PesPkt.Header.StreamID == STREAM_ID_VIDEO { +// println(tsPesPkt.PesPkt.Header.Pts) +// videoFrame++ +// } +// +// fmt.Sprintf("%s", tsPesPkt) +// +// // if err := WritePESPacket(file, tsPesPkt.TsPkt.Header, tsPesPkt.PesPkt); err != nil { +// // return err +// // } +// +// } +// +// return nil +//} + +func (s *MpegTsStream) readPAT(packet *MpegTsPacket, pr io.Reader) (err error) { + // 首先找到PID==0x00的TS包(PAT) + if PID_PAT == packet.Header.Pid { + if len(packet.Payload) == 188 { + pr = &util.Crc32Reader{R: pr, Crc32: 0xffffffff} + } + // Header + PSI + Paylod + pat, err := ReadPAT(pr) + if err != nil { + return err + } + s.pat = &pat + s.patPkt = packet + } + return +} +func (s *MpegTsStream) readPMT(packet *MpegTsPacket, pr io.Reader) (err error) { + // 在读取PAT中已经将所有频道节目信息(PMT_PID)保存了起来 + // 接着读取所有TS包里面的PID,找出PID==PMT_PID的TS包,就是PMT表 + for _, v := range s.pat.Program { + if v.ProgramMapPID == packet.Header.Pid { + if len(packet.Payload) == 188 { + pr = &util.Crc32Reader{R: pr, Crc32: 0xffffffff} + } + // Header + PSI + Paylod + pmt, err := ReadPMT(pr) + if err != nil { + return err + } + // send pmt + s.pmt = &pmt + s.pmtPkt = packet + } + } + return +} +func (s *MpegTsStream) Feed(ts io.Reader) error { + var frame int64 + var tsPktArr []MpegTsPacket + for { + packet, err := ReadTsPacket(ts) + if err == io.EOF { + // 文件结尾 把最后面的数据发出去 + pesPkt, err := TsToPES(tsPktArr) + if err != nil { + return err + } + s.TsPesPktChan <- &MpegTsPesStream{ + TsPkt: *s.firstTsPkt, + PesPkt: pesPkt, + } + return nil + } + if err != nil { + return err + } + pr := bytes.NewReader(packet.Payload) + err = s.readPAT(&packet, pr) + if err != nil { + return err + } + err = s.readPMT(&packet, pr) + if err != nil { + return err + } + // 在读取PMT中已经将所有的音视频PES的索引信息全部保存了起来 + // 接着读取所有TS包里面的PID,找出PID==elementaryPID的TS包,就是音视频数据 + for _, v := range s.pmt.Stream { + if v.ElementaryPID == packet.Header.Pid { + if packet.Header.PayloadUnitStartIndicator == 1 { + if frame != 0 { + pesPkt, err := TsToPES(tsPktArr) + if err != nil { + return err + } + s.TsPesPktChan <- &MpegTsPesStream{ + TsPkt: *s.firstTsPkt, + PesPkt: pesPkt, + } + + tsPktArr = nil + } + s.firstTsPkt = &packet + frame++ + } + tsPktArr = append(tsPktArr, packet) + } + } + } + return nil +} diff --git a/avformat/mpegts/mpegts.md b/avformat/mpegts/mpegts.md new file mode 100644 index 0000000..6497206 --- /dev/null +++ b/avformat/mpegts/mpegts.md @@ -0,0 +1,520 @@ +#MPEGTS + +---------- + +Name:苏荣 +Data:2016/5/27 09:03:30 + + +---------- + +## PSI(Program Specific Information) 节目特定信息 +PSI 可以认为属于 6 个表: +1) 节目相关表(PAT) +2) TS 节目映射表(PMT) +3) 网络信息表(NIT) +4) 有条件访问表(CAT) +5) 传输流描述表 +6) IPMP 控制信息表 + +##ES流(Elementary Stream):基本码流,不分段的音频、视频或其他信息的连续码流. + +##PES流:把基本流ES分割成段,并加上相应头文件打包成形的打包基本码流 + +##PS流(Program Stream):节目流,将具有共同时间基准的一个或多个PES组合(复合)而成的单一数据流(用于播放或编辑系统,如m2p). + +##TS流(Transport Stream):传输流,将具有共同时间基准或独立时间基准的一个或多个PES组合(复合)而成的单一数据流(用于数据传输). + +##PES ES TS +视频压缩成H264码流,可以称之为ES流,将其每帧打包为PES流,然后分拆为多个188字节,称为TS流. + +H264(ES) = PES1(一帧ES打包) + PES2(一帧ES打包) + PES3(一帧ES打包) + ... + +PES1 = PES1 Header + PES1 Payload = PES1 Packet Start Code Prefix + Stream ID + PES1 Packet Length + Send PES1 Header(不确定大小) + PES1 Payload + +PES1 Payload = TS1 Payload + TS2 Payload + TS3 Payload + ... + +PES1 = TS1 + TS2 + TS3 + .... + +PES1 = TS1(TS1 Header + PES1 Header + TS1 Payload) + TS2(有三种可能) + TS3(有三种可能) + ...... + +TS1(TS流第一个包) = TS1 Header + PES1 Header + TS1 Payload + +TS2(TS流第二个包,第一种情况) = TS2 Header + 自适应字段 + TS2 Payload (出现概率 1%) + +TS2(TS流第二个包,第二种情况) = TS2 Header + 自适应字段 (出现概率 0.1%) + +TS2(TS流第二个包,第三种情况) = TS2 Header + TS2 Payload (出现概率 98.9%) + +一段ES流 = N个PES(N帧) + +同一个PES的TS的PID是相同的 + +##寻找第一个TS包 +Header PID = 0x000 说明数据包是PAT表信息 +第一个TS包 一般叫做 PAT (Program Association Table,节目相关表) + +TS流 : PID=005 + PID=002 + PID=000 + +一般来说第一个TS包一般在第一个位置,本例举出一个特殊情况(第一个TS包在第三) + +在寻找第一个TS包时,不断读取TS包,直到找到pid=000的位置,并将读取过的TS包置入缓冲区 + +##寻找下一个TS包 +第二个TS包 一般叫做PMT(Program Map Table,节目映射表) + +##解析TS包 +payload_unit_start_indicator : 该字段用来表示TS包的有效净荷有PES包或者PSI数据的情况. + +当TS包带有PES包数据时(出现概率99.9%).不带PES包(出现概率0.1%). + +1. 当TS包带有PES包数据时,payload_unit_start_indicator具有以下的特点: +a. 置为1,标识TS包的有效净荷以PES包的第一个字节开始. +b. 置为0,表示TS包的开始不是PES包. + +2. 当TS包带有PSI数据时,payload_unit_start_indicator具有以下特点: +a. 置为1,表示TS包带有PSI部分的第一个字节,即第一个字节带有指针pointer_field. +b. 置为0,表示TS包不带有一个PSI部分的第一个字节,即在有效净荷中没有指针point_field. +c. 对于空包的包,payload_unit_start_indicator应该置为0 + +adaptionFieldControl: +01 -> 仅含有效负载(TS包第三种情况) +10 -> 仅含调整字段(TS包第二种情况) +11 -> 含有调整字段和有效负载(TS包第一种情况) + +TS流,通过一个个的TS包来传送. TS包可以是传送PSI SI等各表的数据包,也可以是传送节目音视频数据(携带的PES包:音视频基本流包)的包;TS携带 PSI SI等表的数据时,各个表以各表对应的Section语法格式做为传输单元存放到TS包中 以便传输; +TS包,有一个TS包的PID,系统就是根据这个PID来找对应的TS包;对于包含音视频数据(PES包)的TS包,系统通过TS的PID找到对应TS数据包,提取其中的数据组合成节目的音视频;对于携带PSI SI等数据的TS包,系统通过TS的PID找到对应TS数据包,提取各个PSI SI数据表格,用来指导系统;因此其中部分PID用来固定传输某些数据内容. + +有了TS的PID后, 如果TS包携带的是PSI SI等表格的Section数据时,有时还不能确定该PID的TS包中携带的数据是什么,SDT BAT ST 等表传送时,都用的是PID为0X0011的TS数据包,对于这种携带PSI SI Section单元的TS包,对应的数据(表的Section语法中)还有一个 TABLE_ID字段,用来可以确定是具体的什么表 + +因此PID+TableID就可以确定负载带了什么,是PES还是PSI. + + +---------- + + +1. 第一个包: + +包头 : 47 60 00 10 +0x47 : syncByte +0x6 : 0110(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 1. +0x000 : 0 0000 0000 0000, pid = 0,说明是第一个TS包(PAT表) +0x10 : 0001 0000, adaptionFieldControl = 01,说明仅含有效负载(TS包第三种情况) + +负载 : 00 00 B0 0D 00 00 C1 00 00 00 01 E0 +81 0C 8C BE 32 FF FF......FF + +指针 : 00 +table id : 00 +固定值 : B (1011) +section_length : 0 0D(值:13) +transport_stream_id : 00 00 +version number & current_next_indicator : C1 +section_number : 00 +last_section_number : 00 +program_number : 00 01 +program_map_PID : E0 81(因为program_number > 0) +CRC_32 : 0C 8C BE 32 + + if (program_number == 0) + { + network_PID + }else + { + program_map_PID + } + +E0 81 = reserved3 + program_map_PID = | 1110 0000 | 1000 0001 | +program_map_PID = 0x81(说明PMT的pid为081) + + +---------- + + +2. 第二个包 + +包头 : 47 60 81 10 +0x47 : syncByte +0x6 : 0110(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 1. +0x081 : 0 0000 1000 0001, pid = 0x081(说明是PMT表,因为前面的PAT表给出了) +0x10 : 0001 0000, adaptionFieldControl = 01,说明仅含有效负载(TS包第三种情况) + +负载 : 00 02 B0 17 00 01 C1 00 00 E8 10 F0 00 1B E8 10 +F0 00 03 E8 14 F0 00 66 74 A4 2D FF FF FF FF FF......FF + +指针 : 00 +table id : 02 +固定值 : B +section_length : 0 17(值:23,表示到后面FF FF FF FF FF FF之前总共有23个字节) +program_number : 00 01 +reserved2 & version_number & current_next_indicator : C1 +section_number : 00 +last_section_number : 00 +PCR_PID : E8 10 +program_info_length : F0 00 前4位为保留位 后12位为描述信息长度 此处为0 + +第一流分析 : 1B E8 10 F0 00 +stream_type : 1B 视频流(H264)(ITU-T H.264建议书| SO/IEC 14496-10 视频中定义的 AVC 视频流) +elementary_PID : E8 10 前3位为保留位取后13位 则PID=810 表示此PID的都是视频流 +ES_info_length : F0 00 前4位为保留位 后12位为描述信息长度 此处为0 + +第二流分析 : 03 E8 14 F0 00 +stream_type : 03 音频流(MP3) +elementary_PID : E8 14 前3位为保留位取后13位 则PID=814 表示此PID的都是音频流 +ES_info_length : F0 00 前4位为保留位 后12位为描述信息长度 此处为0 + + + +CRC : 66 74 A4 2D + + +reserved4 + program_info_length = | 1111 0000 | 0000 0000 | +program_info_length = 0 + +stream_type : 03 表示流是音频流 MP3 格式 814 表示 pid=814 的TS包存储的是MP3格式的音频流. +stream_type : 01 表示流是视频流 h264格式 810 表示 pid=810 的TS包存储的是h264格式的视频流 + + +---------- + + +3. 第三个包 +包头 : 47 48 14 10 +0x47 : syncByte +0x4 : 0100(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 1. +0x814 : 0 1000 0001 0100, pid = 0x814(音频MP3) +0x10 : 0001 0000, adaptionFieldControl = 01 + +这里: +payload_unit_start_indicator = 1, 说明有效载荷起始符为1,含有PES包头 +adaptionFieldControl = 01, 说明仅含有效负载(TS包第三种情况) + +负载 : 00 00 01 C0 01 88 80 80 05 21 00 01 96 07 FF FD 85 00 33 22 22 11 22 11 11 11 11 11 11 24 82 41 00 90 40 00 00 00 00 00 40 00 ....... 70 34 5B CE 64 B7 D2 F5 4E 07 50 8E 11 1E 60 61 21 32 11 59 + +packetStartCodePrefix : 00 00 01 +streamID : C0 +pes_PacketLength : 01 88(值为392,占用392个字节,一帧数据长度,也可以置为0) +Sned PES HEADER : 占用不确定位 本例为:80 80 05 21 00 01 96 07 + + +Sned PES HEADER 包括以下几个字段: 80 80 05 21 00 01 96 07(解析为二进制显示) +| 8 0 | 8 0 | 0 5 | 2 1 | 0 0 | 0 1 | 9 6 | 0 7 | +| 1000 0000| 1000 0000 | 0000 0101 | 0010 0001 | 0000 0000 | 0000 0001 | 1001 0110 | 0000 1110 | + +(注意,下面的数值是用二进制表示,不特别声明,都是用16进制表示) +(0x80) +constTen : 10 固定 +PES_scrambling_control : 00 PES加扰控制 +PES_priority : 0 PES 包中该有效载荷的优先级 +data_alignment_indicator : 0 数据定位指示符 +copyright : 0 PES 包有效载荷的素材依靠版权所保护 +original_or_copy : 0 PES 包有效载荷的内容是原始的 + +(0x80) +PTS_DTS_flags : 10 PES 包头中 PTS 字段存在 +ESCR_flag : 0 +ES_rate_flag : 0 +DSM_trick_mode_flag : 0 +additional_copy_info_flag : 0 +PES_CRC_flag : 0 +PES_extension_flag : 0 + +(0x05) +PES_header_data_length : 0000 0101(值为5)PES头数据长度,表示后面还有5个字节,之后就是一帧的数据 + +(0x4200032C)(十进制:1107297068) +PTS(presentation time stamp): 0010 0001 0000 0000 0000 0001 1001 0110 0 + +下面字段在本例中都没有: +ESCR(42) = ESCR_base(33) + ESCR_extension(9) +ES_rate(22) +DSM特技方式(8) +additional_copy_info(7) +previous_PES_packet_CRC(16) +PES_Extension(不确定) + + +因为 PTS_DTS_flags == 10,所以本例中只有PTS没有DTS. + + +注意 : 本TS包 包含PES头信息 说明开始下一帧 + +---------- + + +4. 第四个包 +包头 : 47 08 14 11 +0x47 : syncByte +0x0 : 0000(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 0. +0x814 : 0 1000 0001 0100, pid = 0x814(音频MP3) +0x11 : 0001 0001, adaptionFieldControl = 01 + +这里: +payload_unit_start_indicator = 0, 说明有效载荷起始符为0,不含有PES包头 +adaptionFieldControl = 01, 说明仅含有效负载(TS包第三种情况) + +---------- + + +5. 第五个包 +包头 : 47 08 14 32 +0x47 : syncByte +0x0 : 0000(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 0. +0x814 : 0 1000 0001 0100, pid = 0x814(音频MP3) +0x32 : 0011 0010, adaptionFieldControl = 11 + +这里: +payload_unit_start_indicator = 0, 说明有效载荷起始符为0,不含有PES包头 +adaptionFieldControl = 11, 说明先有自适应字段,再有有效载荷(TS包第一种情况) + +负载 : 99 00 FF FF FF ... FF 52 DE E6 B5 D0 76 CD CB B2 24 B3 92 AD 4E CD 19 D2 CC 82 D4 78 10 80 6C 0E 99 49 A4 59 C0 + +adaptation_field_length : 99(值为153,表示占用153个字节) + +discontinuity_indicator & random_access_indicator & +elementary_stream_priority_indicator & PCR_flag & +OPCR_flag & splicing_point_flag & +transport_private_data_flag & adaptation_field_extension_flag : 00 剩下的所有字段都为0 + +(00 FF FF FF ... FF)这里都是调整字段,从52 DE E6 B5 D0(从00(FF之前,99之后) 开始算是第1个字节,跳到第153个字节)开始,就是真正的帧数据了 + + +---------- + + +6. 第六个包 +包头 : 47 48 14 13 +0x47 : syncByte +0x4 : 0100(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 1. +0x814 : 0 1000 0001 0100, pid = 0x814(音频MP3) +0x13 : 0001 0011, adaptionFieldControl = 01,说明仅含有效负载(TS包第三种情况) + +这里: +payload_unit_start_indicator = 1, 说明有效载荷起始符为1,含有PES包头 +adaptionFieldControl = 01, 说明仅含有效负载(TS包第三种情况) + +负载 : 00 00 01 C0 01 88 80 80 05 21 00 01 A6 E7 FF FD + +packetStartCodePrefix : 00 00 01 +streamID : C0 +pes_PacketLength : 01 88(值为392,占用392个字节) +Sned PES HEADER : 占用不确定位 + +所以本包数据流ID 和 第二个包的流ID是一样的 + +注意 : 本TS包 又包含PES头信息 说明开始下一帧 + + +---------- + +7. 第七个包 +包头 : 47 48 10 30 +0x47 : syncByte +0x4 : 0100(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 1. +0x810 : 0 1000 0001 0000, pid = 0x810(视频H264) +0x30 : 0011 0000, adaptionFieldControl = 11,说明含有调整字段和有效负载(TS包第一种情况) + +这里: +payload_unit_start_indicator = 1, 说明有效载荷起始符为1,含有PES包头 +adaptionFieldControl = 11, 说明含有调整字段和有效负载(TS包第一种情况) + +负载 : 07 10 00 00 01 0F 7E 88 00 00 01 E0 00 00 80 C0 0A 31 00 01 96 07 11 00 01 7E 91 00 00 00 01 67 4D 40 1E 96 ...... D2 99 71 F3 + +adaptation_field_length : 07(值为7,表示占用153个字节) + +discontinuity_indicator & random_access_indicator & +elementary_stream_priority_indicator & PCR_flag & +OPCR_flag & splicing_point_flag & +transport_private_data_flag & adaptation_field_extension_flag : 10 + +(10 00 00 01 0F 7E 88)调整字段 + +packetStartCodePrefix : 00 00 01 +streamID : EO +pes_PacketLength : 00 00(值为0,占用0个字节,一帧数据长度,也可以置为0,此时需要自己去计算) +Sned PES HEADER : 占用不确定位 + + +---------- + + +8. 第八个包 +包头 : 47 08 10 11 +0x47 : syncByte +0x0 : 0000(这里的最后一个字节,要给到下面),payload_unit_start_indicator = 0. +0x810 : 0 1000 0001 0000, pid = 0x810(视频H264) +0x11 : 0001 0001, adaptionFieldControl = 01, 说明仅含有效负载(TS包第三种情况) + +这里: +payload_unit_start_indicator = 0, 说明有效载荷起始符为0,不含有PES包头 +adaptionFieldControl = 01, 说明仅含有效负载(TS包第三种情况) + + +---------- + +总结这个八个包: + +第一个TS包(PID:0X00) : 包含了PAT. +第二个TS包(PID:0X81) : 包含了PMT. +第三个TS包(PID:0x814) : 音频PES包头所有的TS包. +第四个TS包(PID:0x814) : 音频TS包. +第五个TS包(PID:0x814) : 音频TS包. +第六个TS包(PID:0x814) : 音频PES包头所有的TS包. +第七个TS包(PID:0x810) : 视频PES包头所有的TS包. +第八个TS包(PID:0x810) : 视频TS包. + + +---------- + + +// Packet Header: +// PID是TS流中唯一识别标志,Packet Data是什么内容就是由PID决定的.如果一个TS流中的一个Packet的Packet Header中的PID是0x0000, +// 那么这个Packet的Packet Data就是DVB的PAT表而非其他类型数据(如Video,Audio或其他业务信息). + +// 分析一个Header: +// 二进制: 0100 0111 0000 0111 1110 0101 0001 0010 +// 十六进制: 4 7 0 7 e 5 1 2 + +// syncByte = 0x47 就是0x47,这是DVB TS规定的同步字节,固定是0x47 +// transportErrorIndicator = 0 表示当前包没有发生传输错误 +// payloadUnitStartIndicator = 0 具体含义参考ISO13818-1标准文档 +// transportPriority = 0 表示当前包是低优先级 +// pid = 0x07e5(0 0111 1110 0101) Video PID +// transportScramblingControl = 00 表示节目没有加密 +// adaptionFieldControl = 01 具体含义参考ISO13818-1标准文档 +// continuityCounter = 0010 表示当前传送的相同类型的包是第3个 + + +---------- + + +// 分析一段TS流:(PAT) +// Packet Header : 0x47 0x40 0x00 0x10 +// Packet Data : 00 00 b0 11 00 01 c1 00 00 00 00 e0 1f 00 01 e1 00 24 ac48 84 ff ff ... ff ff + +// Header PID = 0x0000 说明数据包是PAT表信息,包头后需要除去一个字节才是有效数据(payload_unit_start_indicator="1") +// 所以,Packet Data就应该是 : 00 b0 11 00 01 c1 00 00 00 00 e0 1f 00 01 e1 00 24 ac48 84 ff ff ... ff ff + +// +// 00 | b0 11 | 00 01 | c1 | 00 | 00 | 00 00 | e0 1f | 00 01 e1 00 | +// + +// table_id = 0000 0000 + +// section_syntax_indicator = 1 +// zero = 0 +// reserved1 = 11 +// sectionLength = 0000 0001 0001 + +// transportStreamID = 0000 0000 0000 0001 + +// reserved2 = 11 +// versionNumber = 0000 0 +// currentNextIndicator 1 + +// sectionNumber = 0000 0000 + +// lastSectionNumber = 0000 0000 + +// programNumber = 0000 0000 0000 0000 + +// reserved3 = 111 +// networkPID = 0 0000 0001 1111 + +// crc32 + + +---------- + + +// 分析一段TS流:(PMT) +// Packet Header : 0x47 0x43 0xe8 0x12 +// Packet Data : 00 02 b0 12 00 01 c1 00 00 e3 e9 f0 00 1b e3 e9 f0 00 f0 af b4 4f ff ff ... ff ff + +// Header PID = 0x03e8 说明数据包是PMT表信息,包头后需要除去一个字节才是有效数据(payload_unit_start_indicator="1") +// 所以,Packet Data就应该是 : 02 b0 12 00 01 c1 00 00 e3 e9 f0 00 1b e3 e9 f0 00 f0 af b4 4f ff ff ... ff ff + +// 1 2 3 4 5 6 7 8 9 10 11 12 +// 02 | b0 12 | 00 01 | c1 | 00 | 00 | e3 e9 | f0 00 | 1b | e3 e9 | f0 00 | f0 af b4 4f | +// + +// 1: +// table_id = 0000 0010 + +// 2: +// section_syntax_indicator = 1 +// zero = 0 +// reserved1 = 11 +// section_length = 0000 0001 0010 + +// 3: +// program_number = 0000 0000 0000 0001 + +// 4: +// reserved2 = 11 +// version_number = 00 000 +// current_next_indicator = 1 + +// 5: +// section_number = 0000 0000 + +// 6: +// last_section_number = 0000 0000 + +// 7: +// reserved3 = 111 +// PCR_PID = 0 0011 1110 1001 + +// 8: +// reserved4 = 1111 +// program_info_length = 0000 0000 0000 + +// 9: +// stream_type = 0001 1011 + +// 10: +// reserved5 = 111 +// elementary_PID = 0 0011 1110 1001 + +// 11: +// reserved6 = 1111 +// ES_info_length = 0000 0000 0000 + +// 12: +// crc + + +---------- + + +##TS流解码过程 +1. 获取TS中的PAT +2. 获取TS中的PMT +3. 根据PMT可以知道当前网络中传输的视频(音频)类型(H264),相应的PID,PCR的PID等信息. +4. 设置demux 模块的视频Filter 为相应视频的PID和stream type等. +5. 从视频Demux Filter 后得到的TS数据包中的payload 数据就是 one piece of PES,在TS header中有一些关于此 payload属于哪个 PES的 第多少个数据包. 因此软件中应该将此payload中的数据copy到PES的buffer中,用于拼接一个PES包. +6. 拼接好的PES包的包头会有 PTS,DTS信息,去掉PES的header就是 ES. +7. 直接将 被被拔掉 PES包头的ES包送给decoder就可以进行解码.解码出来的数据就是一帧一帧的视频数据,这些数据至少应当与PES中的PTS关联一下,以便进行视音频同步. +8. I,B,B,P 信息是在ES中的. + + +---------- + + +1. 首先找到PID为0x00的TS包,找到里面的节目映射表(PMT)PID,因为可能有几个节目信息.所以可能有几个PMT_PID,以一个为例 +2. 接着查找该PMT_PID的TS包,通常就紧接着.在该PMT包中找音频和视频的PID.以视频为例. +3. 开始提取一帧ES数据 + 3.1 查找视频PID的TS包 + 3.2 找PES包头,方法:TS包头第2个字节的高6位(有效载荷单元起始指示符)为1的TS包,跳过自适应字段,找到PES包头,提取时间戳,再跳至ES数据,这就是一帧ES数据的开始部分. + 3.3 查找有效载荷单元起始指示符为0的TS包.跳过TS包头,跳过自适应字段,提取后面的ES数据 + 3.4 同3.3接着查找 + 3.5 当碰到有效载荷单元起始指示符又变为1的视频TS包,就知道这是下一帧的开始了,将前面的所有ES数据组合成一帧数据.开始下一轮组帧. + + +---------- + + +##参考文档: + +1. [TS流](http://blog.csdn.net/cabbage2008/article/category/5885203) +1. [TS各个表 与 SECTION 的解析 CAS原理 ](http://blog.sina.com.cn/s/blog_6b94d5680101r5l6.html) \ No newline at end of file diff --git a/avformat/mpegts/mpegts_crc32.go b/avformat/mpegts/mpegts_crc32.go new file mode 100644 index 0000000..9204316 --- /dev/null +++ b/avformat/mpegts/mpegts_crc32.go @@ -0,0 +1,60 @@ +package mpegts + +// http://www.stmc.edu.hk/~vincent/ffmpeg_0.4.9-pre1/libavformat/mpegtsenc.c + +var Crc32_Table = []uint32{ + 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, + 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, + 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, + 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, + 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, + 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, + 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef, + 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, + 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, + 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, + 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0, + 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, + 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, + 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, + 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, + 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, + 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, + 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, + 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050, + 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, + 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, + 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, + 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, + 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, + 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, + 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, + 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9, + 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, + 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, + 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, + 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, + 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, + 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, + 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, + 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, + 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, + 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, + 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, + 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676, + 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, + 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, + 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, + 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4, +} + +func GetCRC32(data []byte) (crc uint32) { + crc = 0xffffffff + + for _, v := range data { + crc = (crc << 8) ^ Crc32_Table[((crc>>24)^uint32(v))&0xff] + + } + + return +} diff --git a/avformat/mpegts/mpegts_pat.go b/avformat/mpegts/mpegts_pat.go new file mode 100644 index 0000000..1ab7d37 --- /dev/null +++ b/avformat/mpegts/mpegts_pat.go @@ -0,0 +1,230 @@ +package mpegts + +import ( + "bytes" + "errors" + "fmt" + "io" + + "github.com/Monibuca/engine/util" +) + +// ios13818-1-CN.pdf 43(57)/166 +// +// PAT +// + +var DefaultPATPacket = []byte{ + // TS Header + 0x47, 0x40, 0x00, 0x10, + + // Pointer Field + 0x00, + + // PSI + 0x00, 0xb0, 0x0d, 0x00, 0x01, 0xc1, 0x00, 0x00, + + // PAT + 0x00, 0x01, 0xe1, 0x00, + + // CRC + 0xe8, 0xf9, 0x5e, 0x7d, + + // Stuffing 167 bytes + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +} + +// TS Header : +// SyncByte = 0x47 +// TransportErrorIndicator = 0(B:0), PayloadUnitStartIndicator = 1(B:0), TransportPriority = 0(B:0), +// Pid = 0, +// TransportScramblingControl = 0(B:00), AdaptionFieldControl = 1(B:01), ContinuityCounter = 0(B:0000), + +// PSI : +// TableID = 0x00, +// SectionSyntaxIndicator = 1(B:1), Zero = 0(B:0), Reserved1 = 3(B:11), +// SectionLength = 13(0x00d) +// TransportStreamID = 0x0001 +// Reserved2 = 3(B:11), VersionNumber = (B:00000), CurrentNextIndicator = 1(B:0), +// SectionNumber = 0x00 +// LastSectionNumber = 0x00 + +// PAT : +// ProgramNumber = 0x0001 +// Reserved3 = 15(B:1110), ProgramMapPID = 4097(0x1001) + +// PAT表主要包含频道号码和每一个频道对应的PMT的PID号码,这些信息我们在处理PAT表格的时候会保存起来,以后会使用到这些数据 +type MpegTsPATProgram struct { + ProgramNumber uint16 // 16 bit 节目号 + Reserved3 byte // 3 bits 保留位 + NetworkPID uint16 // 13 bits 网络信息表(NIT)的PID,节目号为0时对应的PID为network_PID + ProgramMapPID uint16 // 13 bit 节目映射表的PID,节目号大于0时对应的PID.每个节目对应一个 +} + +// Program Association Table (节目关联表) +// 节目号为0x0000时,表示这是NIT,PID=0x001f,即3. +// 节目号为0x0001时,表示这是PMT,PID=0x100,即256 +type MpegTsPAT struct { + // PSI + TableID byte // 8 bits 0x00->PAT,0x02->PMT + SectionSyntaxIndicator byte // 1 bit 段语法标志位,固定为1 + Zero byte // 1 bit 0 + Reserved1 byte // 2 bits 保留位 + SectionLength uint16 // 12 bits 该字段的头两比特必为'00',剩余 10 比特指定该分段的字节数,紧随 section_length 字段开始,并包括 CRC.此字段中的值应不超过 1021(0x3FD) + TransportStreamID uint16 // 16 bits 该字段充当标签,标识网络内此传输流有别于任何其他多路复用流.其值由用户规定 + Reserved2 byte // 2 bits 保留位 + VersionNumber byte // 5 bits 范围0-31,表示PAT的版本号 + CurrentNextIndicator byte // 1 bit 发送的PAT是当前有效还是下一个PAT有效,0则要等待下一个表 + SectionNumber byte // 8 bits 分段的号码.PAT可能分为多段传输.第一段为00,以后每个分段加1,最多可能有256个分段 + LastSectionNumber byte // 8 bits 最后一个分段的号码 + + // N Loop + Program []MpegTsPATProgram // PAT表里面的所有频道索引信息 + + Crc32 uint32 // 32 bits 包含处理全部传输流节目映射分段之后,在附件 B 规定的解码器中给出寄存器零输出的 CRC 值 +} + +func ReadPAT(r io.Reader) (pat MpegTsPAT, err error) { + lr, psi, err := ReadPSI(r, PSI_TYPE_PAT) + if err != nil { + return + } + + pat = psi.Pat + + // N Loop + // 一直循环去读4个字节,用lr的原因是确保不会读过头了. + for lr.N > 0 { + + // 获取每一个频道的节目信息,保存起来 + programs := MpegTsPATProgram{} + + programs.ProgramNumber, err = util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + // 如果programNumber为0,则是NetworkPID,否则是ProgramMapPID(13) + if programs.ProgramNumber == 0 { + programs.NetworkPID, err = util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + programs.NetworkPID = programs.NetworkPID & 0x1fff + } else { + programs.ProgramMapPID, err = util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + programs.ProgramMapPID = programs.ProgramMapPID & 0x1fff + } + + pat.Program = append(pat.Program, programs) + } + if cr, ok := r.(*util.Crc32Reader); ok { + err = cr.ReadCrc32UIntAndCheck() + if err != nil { + return + } + } + + return +} + +func WritePAT(w io.Writer, pat MpegTsPAT) (err error) { + bw := &bytes.Buffer{} + + // 将pat(所有的节目索引信息)写入到缓冲区中 + for _, pats := range pat.Program { + if err = util.WriteUint16ToByte(bw, pats.ProgramNumber, true); err != nil { + return + } + + if pats.ProgramNumber == 0 { + if err = util.WriteUint16ToByte(bw, pats.NetworkPID&0x1fff|7<<13, true); err != nil { + return + } + } else { + // | 0001 1111 | 1111 1111 | + // 7 << 13 -> 1110 0000 0000 0000 + if err = util.WriteUint16ToByte(bw, pats.ProgramMapPID&0x1fff|7<<13, true); err != nil { + return + } + } + } + + if pat.SectionLength == 0 { + pat.SectionLength = 2 + 3 + 4 + uint16(len(bw.Bytes())) + } + + psi := MpegTsPSI{} + + psi.Pat = pat + + if err = WritePSI(w, PSI_TYPE_PAT, psi, bw.Bytes()); err != nil { + return + } + + return +} + +func WritePATPacket(w io.Writer, tsHeader []byte, pat MpegTsPAT) (err error) { + if pat.TableID != TABLE_PAS { + err = errors.New("PAT table ID error") + return + } + + // 将所有要写的数据(PAT),全部放入到buffer中去. + // buffer 里面已经写好了整个pat表(PointerField+PSI+PAT+CRC) + bw := &bytes.Buffer{} + if err = WritePAT(bw, pat); err != nil { + return + } + + // TODO:如果Pat.Program里面包含的信息很大,大于188? + stuffingBytes := util.GetFillBytes(0xff, TS_PACKET_SIZE-4-bw.Len()) + + // PATPacket = TsHeader + PAT + Stuffing Bytes + var PATPacket []byte + PATPacket = append(PATPacket, tsHeader...) + PATPacket = append(PATPacket, bw.Bytes()...) + PATPacket = append(PATPacket, stuffingBytes...) + + fmt.Println("-------------------------") + fmt.Println("Write PAT :", PATPacket) + fmt.Println("-------------------------") + + // 写PAT负载 + if _, err = w.Write(PATPacket); err != nil { + return + } + + return +} + +func WriteDefaultPATPacket(w io.Writer) (err error) { + _, err = w.Write(DefaultPATPacket) + if err != nil { + return + } + + return +} diff --git a/avformat/mpegts/mpegts_pes.go b/avformat/mpegts/mpegts_pes.go new file mode 100644 index 0000000..8013e3b --- /dev/null +++ b/avformat/mpegts/mpegts_pes.go @@ -0,0 +1,754 @@ +package mpegts + +import ( + "bytes" + "errors" + "fmt" + "io" + "io/ioutil" + + "github.com/Monibuca/engine/avformat" + "github.com/Monibuca/engine/util" +) + +// ios13818-1-CN.pdf 45/166 +// +// PES +// + +// 每个传输流和节目流在逻辑上都是由 PES 包构造的 +type MpegTsPesStream struct { + TsPkt MpegTsPacket + PesPkt MpegTsPESPacket +} + +// PES--Packetized Elementary Streams (分组的ES),ES形成的分组称为PES分组,是用来传递ES的一种数据结构 +// 1110 xxxx 为视频流(0xE0) +// 110x xxxx 为音频流(0xC0) +type MpegTsPESPacket struct { + Header MpegTsPESHeader + Payload []byte +} + +type MpegTsPESHeader struct { + PacketStartCodePrefix uint32 // 24 bits 同跟随它的 stream_id 一起组成标识包起始端的包起始码.packet_start_code_prefix 为比特串"0000 0000 0000 0000 0000 0001"(0x000001) + StreamID byte // 8 bits stream_id 指示基本流的类型和编号,如 stream_id 表 2-22 所定义的.传输流中,stream_id 可以设置为准确描述基本流类型的任何有效值,如表 2-22 所规定的.传输流中,基本流类型在 2.4.4 中所指示的节目特定信息中指定 + PesPacketLength uint16 // 16 bits 指示 PES 包中跟随该字段最后字节的字节数.0->指示 PES 包长度既未指示也未限定并且仅在这样的 PES 包中才被允许,该 PES 包的有效载荷由来自传输流包中所包含的视频基本流的字节组成 + + MpegTsOptionalPESHeader + + PayloadLength uint64 // 这个不是标准文档里面的字段,是自己添加的,方便计算 +} + +// 可选的PES Header = MpegTsOptionalPESHeader + stuffing bytes(0xFF) m * 8 +type MpegTsOptionalPESHeader struct { + ConstTen byte // 2 bits 常量10 + PesScramblingControl byte // 2 bit 指示 PES 包有效载荷的加扰方式.当加扰在 PES 等级上实施时, PES 包头,其中包括任选字段只要存在,应不加扰(见表 2-23) + PesPriority byte // 1 bit 指示在此 PES 包中该有效载荷的优先级.1->指示该 PES 包有效载荷比具有此字段置于"0"的其他 PES 包有效载荷有更高的有效载荷优先级.多路复用器能够使用该PES_priority 比特最佳化基本流内的数据 + DataAlignmentIndicator byte // 1 bit 1->指示 PES 包头之后紧随 2.6.10 中data_stream_alignment_descriptor 字段中指示的视频句法单元或音频同步字,只要该描述符字段存在.若置于值"1"并且该描述符不存在,则要求表 2-53,表 2-54 或表 2-55 的 alignment_type"01"中所指示的那种校准.0->不能确定任何此类校准是否发生 + Copyright byte // 1 bit 1->指示相关 PES 包有效载荷的素材依靠版权所保护.0->不能确定该素材是否依靠版权所保护 + OriginalOrCopy byte // 1 bit 1->指示相关 PES 包有效载荷的内容是原始的.0->指示相关 PES 包有效载荷的内容是复制的 + PtsDtsFlags byte // 2 bits 10->PES 包头中 PTS 字段存在. 11->PES 包头中 PTS 字段和 DTS 字段均存在. 00->PES 包头中既无任何 PTS 字段也无任何 DTS 字段存在. 01->禁用 + EscrFlag byte // 1 bit 1->指示 PES 包头中 ESCR 基准字段和 ESCR 扩展字段均存在.0->指示无任何 ESCR 字段存在 + EsRateFlag byte // 1 bit 1->指示 PES 包头中 ES_rate 字段存在.0->指示无任何 ES_rate 字段存在 + DsmTrickModeFlag byte // 1 bit 1->指示 8 比特特技方式字段存在.0->指示此字段不存在 + AdditionalCopyInfoFlag byte // 1 bit 1->指示 additional_copy_info 存在.0->时指示此字段不存在 + PesCRCFlag byte // 1 bit 1->指示 PES 包中 CRC 字段存在.0->指示此字段不存在 + PesExtensionFlag byte // 1 bit 1->时指示 PES 包头中扩展字段存在.0->指示此字段不存在 + PesHeaderDataLength byte // 8 bits 指示在此 PES包头中包含的由任选字段和任意填充字节所占据的字节总数.任选字段的存在由前导 PES_header_data_length 字段的字节来指定 + + // Optional Field + Pts uint64 // 33 bits 指示时间与解码时间的关系如下: PTS 为三个独立字段编码的 33 比特数.它指示基本流 n 的显示单元 k 在系统目标解码器中的显示时间 tpn(k).PTS 值以系统时钟频率除以 300(产生 90 kHz)的周期为单位指定.显示时间依照以下公式 2-11 从 PTS 中推出.有关编码显示时间标记频率上的限制参阅 2.7.4 + Dts uint64 // 33 bits 指示基本流 n 的存取单元 j 在系统目标解码器中的解码时间 tdn(j). DTS 的值以系统时钟频率除以 300(生成90 kHz)的周期为单位指定.依照以下公式 2-12 从 DTS 中推出解码时间 + EscrBase uint64 // 33 bits 其值由 ESCR_base(i) 给出,如公式 2-14 中给出的 + EscrExtension uint16 // 9 bits 其值由 ESCR_ext(i) 给出,如公式 2-15 中给出的. ESCR 字段指示包含 ESCR_base 最后比特的字节到达 PES流的 PES-STD 输入端的预期时间(参阅 2.5.2.4) + EsRate uint32 // 22 bits 在PES 流情况中,指定系统目标解码器接收 PES 包字节的速率.ES_rate 在包括它的 PES 包以及相同 PES 流的后续 PES 包中持续有效直至遇到新的 ES_rate 字段时为止.ES 速率值以 50 字节/秒为度量单位.0 值禁用 + TrickModeControl byte // 3 bits 指示适用于相关视频流的特技方式.在其他类型基本流的情况中,此字段以及后随 5 比特所规定的那些含义未确定.对于 trick_mode 状态的定义,参阅 2.4.2.3 的特技方式段落 + TrickModeValue byte // 5 bits + AdditionalCopyInfo byte // 7 bits 包含与版权信息有关的专用数据 + PreviousPESPacketCRC uint16 // 16 bits 包含产生解码器中 16 寄存器零输出的 CRC 值, 类似于附件 A 中定义的解码器. 但在处理先前的 PES 包数据字节之后, PES 包头除外,采用多项式 + + // PES Extension + PesPrivateDataFlag byte // 1 bit 1->指示该 PES 包头包含专用数据. 0->指示 PES 包头中不存在专用数据 + PackHeaderFieldFlag byte // 1 bit 1->指示 ISO/IEC 11172-1 包头或节目流包头在此 PES包头中存储.若此字段处于节目流中包含的 PES 包中,则此字段应设置为"0.传输流中, 0->指示该 PES 头中无任何包头存在 + ProgramPacketSequenceCounterFlag byte // 1 bit 1->指示 program_packet_sequence_counter, MPEG1_MPEG2_identifier 以及 original_stuff_length 字段在 PES 包中存在.0->它指示这些字段在 PES 头中不存在 + PSTDBufferFlag byte // 1 bit 1->指示 P-STD_buffer_scale 和 P-STD_buffer_size 在 PES包头中存在.0->指示这些字段在 PES 头中不存在 + Reserved byte // 3 bits + PesExtensionFlag2 byte // 1 bits 1->指示 PES_extension_field_length 字段及相关的字段存在.0->指示 PES_extension_field_length 字段以及任何相关的字段均不存在. + + // Optional Field + PesPrivateData [16]byte // 128 bits 此数据,同前后字段数据结合,应不能仿真packet_start_code_prefix (0x000001) + PackHeaderField byte // 8 bits 指示 pack_header_field() 的长度,以字节为单位 + ProgramPacketSequenceCounter byte // 7 bits + Mpeg1Mpeg2Identifier byte // 1 bit 1->指示此 PES 包承载来自 ISO/IEC 11172-1 流的信息.0->指示此 PES 包承载来自节目流的信息 + OriginalStuffLength byte // 6 bits 在原始 ITU-T H.222.0 建议书| ISO/IEC 13818-1 PES 包头或在原始 ISO/IEC 11172-1 包头中所使用的填充字节数 + PSTDBufferScale byte // 1bit 它的含义仅当节目流中包含此 PES 包时才规定.它指示所使用的标度因子用于解释后续的 P-STD_buffer_size 字段.若前导 stream_id 指示音频流,则P-STD 缓冲器标度字段必为"0"值.若前导 stream_id 指示视频流,则 P-STD_buffer_scale 字段必为"1"值.对于所有其他流类型,该值可为"1"或为"0" + PSTDBufferSize uint16 // 13 bits 其含义仅当节目流中包含此 PES包时才规定.它规定在 P-STD 中,输入缓冲器 BSn 的尺寸.若 STD_buffer_scale 为 "0"值,则 P-STD_buffer_size以 128 字节为单位度量该缓冲器尺寸.若 P-STD_buffer_scale 为"1",则 P-STD_buffer_size 以 1024 字节为单位度量该缓冲器尺寸 + PesExtensionFieldLength byte // 7 bits 指示 PES 扩展字段中跟随此长度字段的直至并包括任何保留字节为止的数据长度,以字节为度量单位 + StreamIDExtensionFlag byte // 1 bits + //pesExtensionField []byte // PES_extension_field_length bits + //packField []byte // pack_field_length bits +} + +// pts_dts_Flags == "10" -> PTS +// 0010 4 +// PTS[32...30] 3 +// marker_bit 1 +// PTS[29...15] 15 +// marker_bit 1 +// PTS[14...0] 15 +// marker_bit 1 + +// pts_dts_Flags == "11" -> PTS + DTS + +type MpegtsPESFrame struct { + Pid uint16 + IsKeyFrame bool + ContinuityCounter byte + ProgramClockReferenceBase uint64 +} + +func ReadPESHeader(r io.Reader) (header MpegTsPESHeader, err error) { + var flags uint8 + var length uint + + // packetStartCodePrefix(24) (0x000001) + header.PacketStartCodePrefix, err = util.ReadByteToUint24(r, true) + if err != nil { + return + } + + if header.PacketStartCodePrefix != 0x0000001 { + err = errors.New("read PacketStartCodePrefix is not 0x0000001") + return + } + + // streamID(8) + header.StreamID, err = util.ReadByteToUint8(r) + if err != nil { + return + } + + // pes_PacketLength(16) + header.PesPacketLength, err = util.ReadByteToUint16(r, true) + if err != nil { + return + } + + length = uint(header.PesPacketLength) + + // PES包长度可能为0,这个时候,需要自己去算 + // 0 <= len <= 65535 + // 如果当length为0,那么先设置为最大值,然后用LimitedReade去读,如果读到最后面剩下的字节数小于65536,才是正确的包大小. + // 一个包一般情况下不可能会读1<<31个字节. + if length == 0 { + length = 1 << 31 + } + + // lrPacket 和 lrHeader 位置指针是在同一位置的 + lrPacket := &io.LimitedReader{R: r, N: int64(length)} + lrHeader := lrPacket + + // constTen(2) + // pes_ScramblingControl(2) + // pes_Priority(1) + // dataAlignmentIndicator(1) + // copyright(1) + // originalOrCopy(1) + flags, err = util.ReadByteToUint8(lrHeader) + if err != nil { + return + } + + header.ConstTen = flags & 0xc0 + header.PesScramblingControl = flags & 0x30 + header.PesPriority = flags & 0x08 + header.DataAlignmentIndicator = flags & 0x04 + header.Copyright = flags & 0x02 + header.OriginalOrCopy = flags & 0x01 + + // pts_dts_Flags(2) + // escr_Flag(1) + // es_RateFlag(1) + // dsm_TrickModeFlag(1) + // additionalCopyInfoFlag(1) + // pes_CRCFlag(1) + // pes_ExtensionFlag(1) + flags, err = util.ReadByteToUint8(lrHeader) + if err != nil { + return + } + + header.PtsDtsFlags = flags & 0xc0 + header.EscrFlag = flags & 0x20 + header.EsRateFlag = flags & 0x10 + header.DsmTrickModeFlag = flags & 0x08 + header.AdditionalCopyInfoFlag = flags & 0x04 + header.PesCRCFlag = flags & 0x02 + header.PesExtensionFlag = flags & 0x01 + + // pes_HeaderDataLength(8) + header.PesHeaderDataLength, err = util.ReadByteToUint8(lrHeader) + if err != nil { + return + } + + length = uint(header.PesHeaderDataLength) + + lrHeader = &io.LimitedReader{R: lrHeader, N: int64(length)} + + // 00 -> PES 包头中既无任何PTS 字段也无任何DTS 字段存在 + // 10 -> PES 包头中PTS 字段存在 + // 11 -> PES 包头中PTS 字段和DTS 字段均存在 + // 01 -> 禁用 + + // PTS(33) + if flags&0x80 != 0 { + var pts uint64 + pts, err = util.ReadByteToUint40(lrHeader, true) + if err != nil { + return + } + + header.Pts = util.GetPtsDts(pts) + } + + // DTS(33) + if flags&0x80 != 0 && flags&0x40 != 0 { + var dts uint64 + dts, err = util.ReadByteToUint40(lrHeader, true) + if err != nil { + return + } + + header.Dts = util.GetPtsDts(dts) + } + + // reserved(2) + escr_Base1(3) + marker_bit(1) + + // escr_Base2(15) + marker_bit(1) + escr_Base23(15) + + // marker_bit(1) + escr_Extension(9) + marker_bit(1) + if header.EscrFlag != 0 { + _, err = util.ReadByteToUint48(lrHeader, true) + if err != nil { + return + } + + //s.pes.escr_Base = escrBaseEx & 0x3fffffffe00 + //s.pes.escr_Extension = uint16(escrBaseEx & 0x1ff) + } + + // es_Rate(22) + if header.EsRateFlag != 0 { + header.EsRate, err = util.ReadByteToUint24(lrHeader, true) + if err != nil { + return + } + } + + // 不知道为什么这里不用 + /* + // trickModeControl(3) + trickModeValue(5) + if s.pes.dsm_TrickModeFlag != 0 { + trickMcMv, err := util.ReadByteToUint8(lrHeader) + if err != nil { + return err + } + + s.pes.trickModeControl = trickMcMv & 0xe0 + s.pes.trickModeValue = trickMcMv & 0x1f + } + */ + + // marker_bit(1) + additionalCopyInfo(7) + if header.AdditionalCopyInfoFlag != 0 { + header.AdditionalCopyInfo, err = util.ReadByteToUint8(lrHeader) + if err != nil { + return + } + + header.AdditionalCopyInfo = header.AdditionalCopyInfo & 0x7f + } + + // previous_PES_Packet_CRC(16) + if header.PesCRCFlag != 0 { + header.PreviousPESPacketCRC, err = util.ReadByteToUint16(lrHeader, true) + if err != nil { + return + } + } + + // pes_PrivateDataFlag(1) + packHeaderFieldFlag(1) + programPacketSequenceCounterFlag(1) + + // p_STD_BufferFlag(1) + reserved(3) + pes_ExtensionFlag2(1) + if header.PesExtensionFlag != 0 { + var flags uint8 + flags, err = util.ReadByteToUint8(lrHeader) + if err != nil { + return + } + + header.PesPrivateDataFlag = flags & 0x80 + header.PackHeaderFieldFlag = flags & 0x40 + header.ProgramPacketSequenceCounterFlag = flags & 0x20 + header.PSTDBufferFlag = flags & 0x10 + header.PesExtensionFlag2 = flags & 0x01 + + // TODO:下面所有的标志位,可能获取到的数据,都简单的读取后,丢弃,如果日后需要,在这里处理 + + // pes_PrivateData(128) + if header.PesPrivateDataFlag != 0 { + if _, err = io.CopyN(ioutil.Discard, lrHeader, int64(16)); err != nil { + return + } + } + + // packFieldLength(8) + if header.PackHeaderFieldFlag != 0 { + if _, err = io.CopyN(ioutil.Discard, lrHeader, int64(1)); err != nil { + return + } + } + + // marker_bit(1) + programPacketSequenceCounter(7) + marker_bit(1) + + // mpeg1_mpeg2_Identifier(1) + originalStuffLength(6) + if header.ProgramPacketSequenceCounterFlag != 0 { + if _, err = io.CopyN(ioutil.Discard, lrHeader, int64(2)); err != nil { + return + } + } + + // 01 + p_STD_bufferScale(1) + p_STD_bufferSize(13) + if header.PSTDBufferFlag != 0 { + if _, err = io.CopyN(ioutil.Discard, lrHeader, int64(2)); err != nil { + return + } + } + + // marker_bit(1) + pes_Extension_Field_Length(7) + + // streamIDExtensionFlag(1) + if header.PesExtensionFlag != 0 { + if _, err = io.CopyN(ioutil.Discard, lrHeader, int64(2)); err != nil { + return + } + } + } + + // 把剩下的头的数据消耗掉 + if lrHeader.N > 0 { + if _, err = io.CopyN(ioutil.Discard, lrHeader, int64(lrHeader.N)); err != nil { + return + } + } + + // 2的16次方,16个字节 + if lrPacket.N < 65536 { + // 这里得到的其实是负载长度,因为已经偏移过了Header部分. + //header.pes_PacketLength = uint16(lrPacket.N) + header.PayloadLength = uint64(lrPacket.N) + } + + return +} + +func WritePESHeader(w io.Writer, header MpegTsPESHeader) (written int, err error) { + if header.PacketStartCodePrefix != 0x0000001 { + err = errors.New("write PacketStartCodePrefix is not 0x0000001") + return + } + + // packetStartCodePrefix(24) (0x000001) + if err = util.WriteUint24ToByte(w, header.PacketStartCodePrefix, true); err != nil { + return + } + + written += 3 + + // streamID(8) + if err = util.WriteUint8ToByte(w, header.StreamID); err != nil { + return + } + + written += 1 + + // pes_PacketLength(16) + // PES包长度可能为0,这个时候,需要自己去算 + // 0 <= len <= 65535 + if err = util.WriteUint16ToByte(w, header.PesPacketLength, true); err != nil { + return + } + + //fmt.Println("Length :", payloadLength) + //fmt.Println("PES Packet Length :", header.pes_PacketLength) + + written += 2 + + // constTen(2) + // pes_ScramblingControl(2) + // pes_Priority(1) + // dataAlignmentIndicator(1) + // copyright(1) + // originalOrCopy(1) + // 1000 0001 + if header.ConstTen != 0x80 { + err = errors.New("pes header ConstTen != 0x80") + return + } + + flags := header.ConstTen | header.PesScramblingControl | header.PesPriority | header.DataAlignmentIndicator | header.Copyright | header.OriginalOrCopy + if err = util.WriteUint8ToByte(w, flags); err != nil { + return + } + + written += 1 + + // pts_dts_Flags(2) + // escr_Flag(1) + // es_RateFlag(1) + // dsm_TrickModeFlag(1) + // additionalCopyInfoFlag(1) + // pes_CRCFlag(1) + // pes_ExtensionFlag(1) + sevenFlags := header.PtsDtsFlags | header.EscrFlag | header.EsRateFlag | header.DsmTrickModeFlag | header.AdditionalCopyInfoFlag | header.PesCRCFlag | header.PesExtensionFlag + if err = util.WriteUint8ToByte(w, sevenFlags); err != nil { + return + } + + written += 1 + + // pes_HeaderDataLength(8) + if err = util.WriteUint8ToByte(w, header.PesHeaderDataLength); err != nil { + return + } + + written += 1 + + // PtsDtsFlags == 192(11), 128(10), 64(01)禁用, 0(00) + if header.PtsDtsFlags&0x80 != 0 { + // PTS和DTS都存在(11),否则只有PTS(10) + if header.PtsDtsFlags&0x80 != 0 && header.PtsDtsFlags&0x40 != 0 { + // 11:PTS和DTS + // PTS(33) + 4 + 3 + pts := util.PutPtsDts(header.Pts) | 3<<36 + if err = util.WriteUint40ToByte(w, pts, true); err != nil { + return + } + + written += 5 + + // DTS(33) + 4 + 3 + dts := util.PutPtsDts(header.Dts) | 1<<36 + if err = util.WriteUint40ToByte(w, dts, true); err != nil { + return + } + + written += 5 + } else { + // 10:只有PTS + // PTS(33) + 4 + 3 + pts := util.PutPtsDts(header.Pts) | 2<<36 + if err = util.WriteUint40ToByte(w, pts, true); err != nil { + return + } + + written += 5 + } + } + + return +} + +func WritePESPacket(w io.Writer, frame *MpegtsPESFrame, packet MpegTsPESPacket) (err error) { + var tsPkts []byte + if tsPkts, err = PESToTs(frame, packet); err != nil { + return + } + + // bw.Bytes == PES Packet + if _, err = w.Write(tsPkts); err != nil { + return + } + + return +} + +func IowWritePESPacket(w io.Writer, tsHeader MpegTsHeader, packet MpegTsPESPacket) (err error) { + if packet.Header.PacketStartCodePrefix != 0x000001 { + return errors.New("packetStartCodePrefix != 0x000001") + } + + bw := &bytes.Buffer{} + + // TODO:如果头长度大于65536,字段会为0,是否要改? + _, err = WritePESHeader(bw, packet.Header) + if err != nil { + return + } + + PESPacket := &util.IOVec{} + PESPacket.Append(bw.Bytes()) // header + PESPacket.Append(packet.Payload) // packet + + // 用IOVecWriter来写PES包,IOVecWriter实现了Write方法. + // 因为通常在将一帧PES封装成TS包(188字节)的时候,一般情况下一帧PES字节数会大于188,并且分多次封装. + // 例如这一帧PES字节数为189,那么在封装第二个TS包的时候就只会封装1字节,会导致多次写操作,降低性能. + // 因此将所有的字节数,都写进缓冲中去,然后用系统调用syscall来写入. + iow := util.NewIOVecWriter(w) + + var isKeyFrame bool + var headerLength int + + isKeyFrame = CheckPESPacketIsKeyFrame(packet) + + // 写一帧PES + // 如果是I帧,会有pcr,所以会有调整字段AF. + // 如果当前包字节不满188字节,会需要填充0xff,所以会有调整字段AF. + for i := 0; PESPacket.Length > 0; i++ { + + header := MpegTsHeader{ + SyncByte: 0x47, + Pid: tsHeader.Pid, + AdaptionFieldControl: 1, + ContinuityCounter: byte(i % 15), + } + + // 每一帧开头 + if i == 0 { + header.PayloadUnitStartIndicator = 1 + } + + // I帧 + if isKeyFrame { + header.AdaptionFieldControl = 0x03 + header.AdaptationFieldLength = 7 + header.PCRFlag = 1 + header.RandomAccessIndicator = tsHeader.RandomAccessIndicator + header.ProgramClockReferenceBase = tsHeader.ProgramClockReferenceBase + header.ProgramClockReferenceExtension = tsHeader.ProgramClockReferenceExtension + + isKeyFrame = false + } + + // 这个包大小,会在每一次PESPacket.WriteTo中慢慢减少. + packetLength := PESPacket.Length + + // 包不满188字节 + if packetLength < TS_PACKET_SIZE-4 { + + if header.AdaptionFieldControl >= 2 { + header.AdaptationFieldLength = uint8(TS_PACKET_SIZE - 4 - 1 - packetLength - 7) + } else { + header.AdaptionFieldControl = 0x03 + header.AdaptationFieldLength = uint8(TS_PACKET_SIZE - 4 - 1 - packetLength) + } + + headerLength, err = WriteTsHeader(iow, header) + if err != nil { + return + } + + stuffingLength := int(header.AdaptationFieldLength - 1) + if _, err = iow.Write(util.GetFillBytes(0xff, stuffingLength)); err != nil { + return + } + + headerLength += stuffingLength + + } else { + headerLength, err = WriteTsHeader(iow, header) + if err != nil { + return + } + } + + /* + if headerLength, err = writeTsHeader(iow, header, packetLength); err != nil { + return + } + */ + + payloadLength := 188 - headerLength + + // 写PES负载 + if _, err = PESPacket.WriteTo(iow, payloadLength); err != nil { + return + } + } + + iow.Flush() + + return +} + +func CheckPESPacketIsKeyFrame(packet MpegTsPESPacket) bool { + + nalus := bytes.SplitN(packet.Payload, avformat.NALU_Delimiter1, -1) + + for _, v := range nalus { + if v[0]&0x1f == avformat.NALU_IDR_Picture { + return true + } + } + + return false +} + +func TsToPES(tsPkts []MpegTsPacket) (pesPkt MpegTsPESPacket, err error) { + var index int + + for i := 0; i < len(tsPkts); i++ { + if tsPkts[i].Header.SyncByte != 0x47 { + err = errors.New("mpegts header sync error!") + return + } + + if tsPkts[i].Header.PayloadUnitStartIndicator == 1 { + index++ + + // 一个PES包里面只可能包含一个PayloadUnitStartIndicator=1的TS包. + if index >= 2 { + err = errors.New("TsToPES error PayloadUnitStartIndicator >= 2") + return + } + + r := bytes.NewReader(tsPkts[i].Payload) + lr := &io.LimitedReader{R: r, N: int64(len(tsPkts[i].Payload))} + + // TS Packet PES Header Start Index + hBegin := lr.N + + // PES Header + pesPkt.Header, err = ReadPESHeader(lr) + if err != nil { + return + } + + // TS Packet PES Header End Index + hEnd := lr.N + + pesHeaderLength := hBegin - hEnd + + if pesHeaderLength > 0 && pesHeaderLength <= hBegin { + pesPkt.Payload = append(pesPkt.Payload, tsPkts[i].Payload[pesHeaderLength:]...) + } + } + + if tsPkts[i].Header.PayloadUnitStartIndicator == 0 { + // MpegTsPacket Header 已经包含了自适应字段在里面,所以MpegTsPacket Payload直接就是PES Pyaload + pesPkt.Payload = append(pesPkt.Payload, tsPkts[i].Payload...) + } + + } + + return +} + +func PESToTs(frame *MpegtsPESFrame, packet MpegTsPESPacket) (tsPkts []byte, err error) { + if packet.Header.PacketStartCodePrefix != 0x000001 { + err = errors.New("packetStartCodePrefix != 0x000001") + return + } + + bwPESPkt := &bytes.Buffer{} + _, err = WritePESHeader(bwPESPkt, packet.Header) + if err != nil { + return + } + + if _, err = bwPESPkt.Write(packet.Payload); err != nil { + return + } + + var tsHeaderLength int + for i := 0; bwPESPkt.Len() > 0; i++ { + bwTsHeader := &bytes.Buffer{} + + tsHeader := MpegTsHeader{ + SyncByte: 0x47, + TransportErrorIndicator: 0, + PayloadUnitStartIndicator: 0, + TransportPriority: 0, + Pid: frame.Pid, + TransportScramblingControl: 0, + AdaptionFieldControl: 1, + ContinuityCounter: frame.ContinuityCounter, + } + + frame.ContinuityCounter++ + frame.ContinuityCounter = frame.ContinuityCounter % 16 + + // 每一帧的开头,当含有pcr的时候,包含调整字段 + if i == 0 { + tsHeader.PayloadUnitStartIndicator = 1 + + // 当PCRFlag为1的时候,包含调整字段 + if frame.IsKeyFrame { + tsHeader.AdaptionFieldControl = 0x03 + tsHeader.AdaptationFieldLength = 7 + tsHeader.PCRFlag = 1 + tsHeader.RandomAccessIndicator = 1 + tsHeader.ProgramClockReferenceBase = frame.ProgramClockReferenceBase + } + } + + pesPktLength := bwPESPkt.Len() + + // 每一帧的结尾,当不满足188个字节的时候,包含调整字段 + if pesPktLength < TS_PACKET_SIZE-4 { + var tsStuffingLength uint8 + + tsHeader.AdaptionFieldControl = 0x03 + tsHeader.AdaptationFieldLength = uint8(TS_PACKET_SIZE - 4 - 1 - pesPktLength) + + // TODO:如果第一个TS包也是最后一个TS包,是不是需要考虑这个情况? + // MpegTsHeader最少占6个字节.(前4个走字节 + AdaptationFieldLength(1 byte) + 3个指示符5个标志位(1 byte)) + if tsHeader.AdaptationFieldLength >= 1 { + tsStuffingLength = tsHeader.AdaptationFieldLength - 1 + } else { + tsStuffingLength = 0 + } + + // error + tsHeaderLength, err = WriteTsHeader(bwTsHeader, tsHeader) + if err != nil { + return + } + + if tsStuffingLength > 0 { + if _, err = bwTsHeader.Write(util.GetFillBytes(0xff, int(tsStuffingLength))); err != nil { + return + } + } + + tsHeaderLength += int(tsStuffingLength) + } else { + tsHeaderLength, err = WriteTsHeader(bwTsHeader, tsHeader) + if err != nil { + return + } + } + + tsPayloadLength := TS_PACKET_SIZE - tsHeaderLength + + //fmt.Println("tsPayloadLength :", tsPayloadLength) + + // 这里不断的减少PES包 + tsHeaderByte := bwTsHeader.Bytes() + tsPayloadByte := bwPESPkt.Next(tsPayloadLength) + + // tmp := tsHeaderByte[3] << 2 + // tmp = tmp >> 6 + // if tmp == 2 { + // fmt.Println("fuck you mother.") + // } + + tsPktByte := append(tsHeaderByte, tsPayloadByte...) + + if len(tsPktByte) != TS_PACKET_SIZE { + err = errors.New(fmt.Sprintf("%s, packet size=%d", "TS_PACKET_SIZE != 188,", len(tsPktByte))) + return + } + + tsPkts = append(tsPkts, tsPktByte...) + } + + return +} diff --git a/avformat/mpegts/mpegts_pmt.go b/avformat/mpegts/mpegts_pmt.go new file mode 100644 index 0000000..2e4f71f --- /dev/null +++ b/avformat/mpegts/mpegts_pmt.go @@ -0,0 +1,384 @@ +package mpegts + +import ( + "bytes" + "errors" + "fmt" + "io" + + "github.com/Monibuca/engine/util" +) + +// ios13818-1-CN.pdf 46(60)-153(167)/page +// +// PMT +// + +var DefaultPMTPacket = []byte{ + // TS Header + 0x47, 0x41, 0x00, 0x10, + + // Pointer Field + 0x00, + + // PSI + 0x02, 0xb0, 0x17, 0x00, 0x01, 0xc1, 0x00, 0x00, + + // PMT + 0xe1, 0x01, + 0xf0, 0x00, + + // H264 + 0x1b, 0xe1, 0x01, 0xf0, 0x00, + + // AAC + 0x0f, 0xe1, 0x02, 0xf0, 0x00, + + //0x00, 0x00, 0x00, 0x00, 0x00, + + // CRC for not audio + //0x00, 0x00, 0x00, 0x00, + + // CRC for AAC + 0x9e, 0x28, 0xc6, 0xdd, + + // CRC for MP3 + // 0x4e, 0x59, 0x3d, 0x1e, + + // Stuffing 157 bytes + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +} + +// TS Header : +// SyncByte = 0x47 +// TransportErrorIndicator = 0(B:0), PayloadUnitStartIndicator = 1(B:0), TransportPriority = 0(B:0), +// Pid = 4097(0x1001), +// TransportScramblingControl = 0(B:00), AdaptionFieldControl = 1(B:01), ContinuityCounter = 0(B:0000), + +// PSI : +// TableID = 0x02, +// SectionSyntaxIndicator = 1(B:1), Zero = 0(B:0), Reserved1 = 3(B:11), +// SectionLength = 23(0x17) +// ProgramNumber = 0x0001 +// Reserved2 = 3(B:11), VersionNumber = (B:00000), CurrentNextIndicator = 1(B:0), +// SectionNumber = 0x00 +// LastSectionNumber = 0x00 + +// PMT: +// Reserved3 = 15(B:1110), PcrPID = 256(0x100) +// Reserved4 = 16(B:1111), ProgramInfoLength = 0(0x000) +// H264: +// StreamType = 0x1b, +// Reserved5 = 15(B:1110), ElementaryPID = 256(0x100) +// Reserved6 = 16(B:1111), EsInfoLength = 0(0x000) +// AAC: +// StreamType = 0x0f, +// Reserved5 = 15(B:1110), ElementaryPID = 257(0x101) +// Reserved6 = 16(B:1111), EsInfoLength = 0(0x000) + +type MpegTsPmtStream struct { + StreamType byte // 8 bits 指示具有 PID值的包内承载的节目元类型,其 PID值由 elementary_PID所指定 + Reserved5 byte // 3 bits 保留位 + ElementaryPID uint16 // 13 bits 指定承载相关节目元的传输流包的 PID + Reserved6 byte // 4 bits 保留位 + EsInfoLength uint16 // 12 bits 该字段的头两比特必为'00',剩余 10比特指示紧随 ES_info_length字段的相关节目元描述符的字节数 + + // N Loop Descriptors + Descriptor []MpegTsDescriptor // 不确定字节数,可变 +} + +// Program Map Table (节目映射表) +type MpegTsPMT struct { + // PSI + TableID byte // 8 bits 0x00->PAT,0x02->PMT + SectionSyntaxIndicator byte // 1 bit 段语法标志位,固定为1 + Zero byte // 1 bit 0 + Reserved1 byte // 2 bits 保留位 + SectionLength uint16 // 12 bits 该字段的头两比特必为'00',剩余 10 比特指定该分段的字节数,紧随 section_length 字段开始,并包括 CRC.此字段中的值应不超过 1021(0x3FD) + ProgramNumber uint16 // 16 bits 指定 program_map_PID 所适用的节目 + Reserved2 byte // 2 bits 保留位 + VersionNumber byte // 5 bits 范围0-31,表示PAT的版本号 + CurrentNextIndicator byte // 1 bit 发送的PAT是当前有效还是下一个PAT有效 + SectionNumber byte // 8 bits 分段的号码.PAT可能分为多段传输.第一段为00,以后每个分段加1,最多可能有256个分段 + LastSectionNumber byte // 8 bits 最后一个分段的号码 + + Reserved3 byte // 3 bits 保留位 0x07 + PcrPID uint16 // 13 bits 指明TS包的PID值.该TS包含有PCR域,该PCR值对应于由节目号指定的对应节目.如果对于私有数据流的节目定义与PCR无关.这个域的值将为0x1FFF + Reserved4 byte // 4 bits 预留位 0x0F + ProgramInfoLength uint16 // 12 bits 前两位bit为00.该域指出跟随其后对节目信息的描述的byte数 + ProgramInfoDescriptor []MpegTsDescriptor // N Loop Descriptors 可变 节目信息描述 + + // N Loop + Stream []MpegTsPmtStream // PMT表里面的所有音视频索引信息 + + Crc32 uint32 // 32 bits 包含处理全部传输流节目映射分段之后,在附件 B 规定的解码器中给出寄存器零输出的 CRC 值 +} + +func ReadPMT(r io.Reader) (pmt MpegTsPMT, err error) { + lr, psi, err := ReadPSI(r, PSI_TYPE_PMT) + if err != nil { + return + } + + pmt = psi.Pmt + + // reserved3(3) + pcrPID(13) + pcrPID, err := util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + pmt.PcrPID = pcrPID & 0x1fff + + // reserved4(4) + programInfoLength(12) + // programInfoLength(12) == 0x00(固定为0) + programInfoLength(10) + programInfoLength, err := util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + pmt.ProgramInfoLength = programInfoLength & 0x3ff + + // 如果length>0那么,紧跟programInfoLength后面就有length个字节 + if pmt.ProgramInfoLength > 0 { + lr := &io.LimitedReader{R: lr, N: int64(pmt.ProgramInfoLength)} + pmt.ProgramInfoDescriptor, err = ReadPMTDescriptor(lr) + if err != nil { + return + } + } + + // N Loop + // 开始N循环,读取所有的流的信息 + for lr.N > 0 { + var streams MpegTsPmtStream + // streamType(8) + streams.StreamType, err = util.ReadByteToUint8(lr) + if err != nil { + return + } + + // reserved5(3) + elementaryPID(13) + streams.ElementaryPID, err = util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + streams.ElementaryPID = streams.ElementaryPID & 0x1fff + + // reserved6(4) + esInfoLength(12) + // esInfoLength(12) == 0x00(固定为0) + esInfoLength(10) + streams.EsInfoLength, err = util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + streams.EsInfoLength = streams.EsInfoLength & 0x3ff + + // 如果length>0那么,紧跟esInfoLength后面就有length个字节 + if streams.EsInfoLength > 0 { + lr := &io.LimitedReader{R: lr, N: int64(streams.EsInfoLength)} + streams.Descriptor, err = ReadPMTDescriptor(lr) + if err != nil { + return + } + } + + // 每读取一个流的信息(音频流或者视频流或者其他),都保存起来 + pmt.Stream = append(pmt.Stream, streams) + } + if cr, ok := r.(*util.Crc32Reader); ok { + err = cr.ReadCrc32UIntAndCheck() + if err != nil { + return + } + } + return +} + +func ReadPMTDescriptor(lr *io.LimitedReader) (Desc []MpegTsDescriptor, err error) { + var desc MpegTsDescriptor + for lr.N > 0 { + // tag (8) + desc.Tag, err = util.ReadByteToUint8(lr) + if err != nil { + return + } + + // length (8) + desc.Length, err = util.ReadByteToUint8(lr) + if err != nil { + return + } + + desc.Data = make([]byte, desc.Length) + _, err = lr.Read(desc.Data) + if err != nil { + return + } + + Desc = append(Desc, desc) + } + + return +} + +func WritePMTDescriptor(w io.Writer, descs []MpegTsDescriptor) (err error) { + for _, desc := range descs { + // tag(8) + if err = util.WriteUint8ToByte(w, desc.Tag); err != nil { + return + } + + // length (8) + if err = util.WriteUint8ToByte(w, uint8(len(desc.Data))); err != nil { + return + } + + // data + if _, err = w.Write(desc.Data); err != nil { + return + } + } + + return +} + +func WritePMTBody(w io.Writer, pmt MpegTsPMT) (err error) { + // reserved3(3) + pcrPID(13) + if err = util.WriteUint16ToByte(w, pmt.PcrPID|7<<13, true); err != nil { + return + } + + // programInfoDescriptor 节目信息描述,字节数不能确定 + bw := &bytes.Buffer{} + if err = WritePMTDescriptor(bw, pmt.ProgramInfoDescriptor); err != nil { + return + } + + pmt.ProgramInfoLength = uint16(bw.Len()) + + // reserved4(4) + programInfoLength(12) + // programInfoLength(12) == 0x00(固定为0) + programInfoLength(10) + if err = util.WriteUint16ToByte(w, pmt.ProgramInfoLength|0xf000, true); err != nil { + return + } + + // programInfoDescriptor + if _, err = w.Write(bw.Bytes()); err != nil { + return + } + + // 循环读取所有的流的信息(音频或者视频) + for _, esinfo := range pmt.Stream { + // streamType(8) + if err = util.WriteUint8ToByte(w, esinfo.StreamType); err != nil { + return + } + + // reserved5(3) + elementaryPID(13) + if err = util.WriteUint16ToByte(w, esinfo.ElementaryPID|7<<13, true); err != nil { + return + } + + // descriptor ES流信息描述,字节数不能确定 + bw := &bytes.Buffer{} + if err = WritePMTDescriptor(bw, esinfo.Descriptor); err != nil { + return + } + + esinfo.EsInfoLength = uint16(bw.Len()) + + // reserved6(4) + esInfoLength(12) + // esInfoLength(12) == 0x00(固定为0) + esInfoLength(10) + if err = util.WriteUint16ToByte(w, esinfo.EsInfoLength|0xf000, true); err != nil { + return + } + + // descriptor + if _, err = w.Write(bw.Bytes()); err != nil { + return + } + } + + return +} + +func WritePMT(w io.Writer, pmt MpegTsPMT) (err error) { + bw := &bytes.Buffer{} + + if err = WritePMTBody(bw, pmt); err != nil { + return + } + + if pmt.SectionLength == 0 { + pmt.SectionLength = 2 + 3 + 4 + uint16(len(bw.Bytes())) + } + + psi := MpegTsPSI{} + + psi.Pmt = pmt + + if err = WritePSI(w, PSI_TYPE_PMT, psi, bw.Bytes()); err != nil { + return + } + + return +} + +func WritePMTPacket(w io.Writer, tsHeader []byte, pmt MpegTsPMT) (err error) { + if pmt.TableID != TABLE_TSPMS { + err = errors.New("PMT table ID error") + return + } + + // 将所有要写的数据(PMT),全部放入到buffer中去. + // buffer 里面已经写好了整个PMT表(PointerField+PSI+PMT+CRC) + bw := &bytes.Buffer{} + if err = WritePMT(bw, pmt); err != nil { + return + } + + // TODO:如果Pmt.Stream里面包含的信息很大,大于188? + stuffingBytes := util.GetFillBytes(0xff, TS_PACKET_SIZE-4-bw.Len()) + + var PMTPacket []byte + PMTPacket = append(PMTPacket, tsHeader...) + PMTPacket = append(PMTPacket, bw.Bytes()...) + PMTPacket = append(PMTPacket, stuffingBytes...) + + fmt.Println("-------------------------") + fmt.Println("Write PMT :", PMTPacket) + fmt.Println("-------------------------") + + // 写PMT负载 + if _, err = w.Write(PMTPacket); err != nil { + return + } + + return +} + +func WriteDefaultPMTPacket(w io.Writer) (err error) { + _, err = w.Write(DefaultPMTPacket) + if err != nil { + return + } + + return +} diff --git a/avformat/mpegts/mpegts_psi.go b/avformat/mpegts/mpegts_psi.go new file mode 100644 index 0000000..5834344 --- /dev/null +++ b/avformat/mpegts/mpegts_psi.go @@ -0,0 +1,232 @@ +package mpegts + +import ( + "errors" + "fmt" + "io" + "io/ioutil" + + "github.com/Monibuca/engine/util" +) + +// +// PSI +// + +const ( + PSI_TYPE_PAT = 1 + PSI_TYPE_PMT = 2 + PSI_TYPE_NIT = 3 + PSI_TYPE_CAT = 4 + PSI_TYPE_TST = 5 + PSI_TYPE_IPMP_CIT = 6 +) + +type MpegTsPSI struct { + // PAT + // PMT + // CAT + // NIT + Pat MpegTsPAT + Pmt MpegTsPMT +} + +// 当传输流包有效载荷包含 PSI 数据时,payload_unit_start_indicator 具有以下意义: +// 若传输流包承载 PSI分段的首字节,则 payload_unit_start_indicator 值必为 1,指示此传输流包的有效载荷的首字节承载pointer_field. +// 若传输流包不承载 PSI 分段的首字节,则 payload_unit_start_indicator 值必为 0,指示在此有效载荷中不存在 pointer_field +// 只要是PSI就一定会有pointer_field +func ReadPSI(r io.Reader, pt uint32) (lr *io.LimitedReader, psi MpegTsPSI, err error) { + // pointer field(8) + cr, ok := r.(*util.Crc32Reader) + if ok { + r = cr.R + } + pointer_field, err := util.ReadByteToUint8(r) + if err != nil { + return + } + + if pointer_field != 0 { + // 无论如何都应该确保能将pointer_field读取到,并且io.Reader指针向下移动 + // ioutil.Discard常用在,http中,如果Get请求,获取到了很大的Body,要丢弃Body,就用这个方法. + // 因为http默认重链接的时候,必须等body读取完成. + // 用于发送需要读取但不想存储的数据,目的是耗尽读取端的数据 + if _, err = io.CopyN(ioutil.Discard, r, int64(pointer_field)); err != nil { + return + } + } + if ok { + r = cr + } + + // table id(8) + tableId, err := util.ReadByteToUint8(r) + if err != nil { + return + } + + // sectionSyntaxIndicator(1) + zero(1) + reserved1(2) + sectionLength(12) + // sectionLength 前两个字节固定为00 + sectionSyntaxIndicatorAndSectionLength, err := util.ReadByteToUint16(r, true) + if err != nil { + return + } + + // 指定该分段的字节数,紧随 section_length 字段开始,并包括 CRC + // 因此剩下最多只能在读sectionLength长度的字节 + lr = &io.LimitedReader{R: r, N: int64(sectionSyntaxIndicatorAndSectionLength & 0x3FF)} + + // PAT TransportStreamID(16) or PMT ProgramNumber(16) + transportStreamIdOrProgramNumber, err := util.ReadByteToUint16(lr, true) + if err != nil { + return + } + + // reserved2(2) + versionNumber(5) + currentNextIndicator(1) + versionNumberAndCurrentNextIndicator, err := util.ReadByteToUint8(lr) + if err != nil { + return + } + + // sectionNumber(8) + sectionNumber, err := util.ReadByteToUint8(lr) + if err != nil { + return + } + + // lastSectionNumber(8) + lastSectionNumber, err := util.ReadByteToUint8(lr) + if err != nil { + return + } + + // 因为lr.N是从sectionLength开始计算,所以要减去 pointer_field(8) + table id(8) + sectionSyntaxIndicator(1) + zero(1) + reserved1(2) + sectionLength(12) + lr.N -= 4 + + switch pt { + case PSI_TYPE_PAT: + { + if tableId != TABLE_PAS { + err = errors.New(fmt.Sprintf("%s, id=%d", "read pmt table id != 2", tableId)) + return + } + + psi.Pat.TableID = tableId + psi.Pat.SectionSyntaxIndicator = uint8((sectionSyntaxIndicatorAndSectionLength & 0x8000) >> 15) + psi.Pat.SectionLength = sectionSyntaxIndicatorAndSectionLength & 0x3FF + psi.Pat.TransportStreamID = transportStreamIdOrProgramNumber + psi.Pat.VersionNumber = versionNumberAndCurrentNextIndicator & 0x3e + psi.Pat.CurrentNextIndicator = versionNumberAndCurrentNextIndicator & 0x01 + psi.Pat.SectionNumber = sectionNumber + psi.Pat.LastSectionNumber = lastSectionNumber + } + case PSI_TYPE_PMT: + { + if tableId != TABLE_TSPMS { + err = errors.New(fmt.Sprintf("%s, id=%d", "read pmt table id != 2", tableId)) + return + } + + psi.Pmt.TableID = tableId + psi.Pmt.SectionSyntaxIndicator = uint8((sectionSyntaxIndicatorAndSectionLength & 0x8000) >> 15) + psi.Pmt.SectionLength = sectionSyntaxIndicatorAndSectionLength & 0x3FF + psi.Pmt.ProgramNumber = transportStreamIdOrProgramNumber + psi.Pmt.VersionNumber = versionNumberAndCurrentNextIndicator & 0x3e + psi.Pmt.CurrentNextIndicator = versionNumberAndCurrentNextIndicator & 0x01 + psi.Pmt.SectionNumber = sectionNumber + psi.Pmt.LastSectionNumber = lastSectionNumber + } + } + + return +} + +func WritePSI(w io.Writer, pt uint32, psi MpegTsPSI, data []byte) (err error) { + var tableId, versionNumberAndCurrentNextIndicator, sectionNumber, lastSectionNumber uint8 + var sectionSyntaxIndicatorAndSectionLength, transportStreamIdOrProgramNumber uint16 + + switch pt { + case PSI_TYPE_PAT: + { + if psi.Pat.TableID != TABLE_PAS { + err = errors.New(fmt.Sprintf("%s, id=%d", "write pmt table id != 0", tableId)) + return + } + + tableId = psi.Pat.TableID + sectionSyntaxIndicatorAndSectionLength = uint16(psi.Pat.SectionSyntaxIndicator)<<15 | 3<<12 | psi.Pat.SectionLength + transportStreamIdOrProgramNumber = psi.Pat.TransportStreamID + versionNumberAndCurrentNextIndicator = psi.Pat.VersionNumber<<1 | psi.Pat.CurrentNextIndicator + sectionNumber = psi.Pat.SectionNumber + lastSectionNumber = psi.Pat.LastSectionNumber + } + case PSI_TYPE_PMT: + { + if psi.Pmt.TableID != TABLE_TSPMS { + err = errors.New(fmt.Sprintf("%s, id=%d", "write pmt table id != 2", tableId)) + return + } + + tableId = psi.Pmt.TableID + sectionSyntaxIndicatorAndSectionLength = uint16(psi.Pmt.SectionSyntaxIndicator)<<15 | 3<<12 | psi.Pmt.SectionLength + transportStreamIdOrProgramNumber = psi.Pmt.ProgramNumber + versionNumberAndCurrentNextIndicator = psi.Pmt.VersionNumber<<1 | psi.Pmt.CurrentNextIndicator + sectionNumber = psi.Pmt.SectionNumber + lastSectionNumber = psi.Pmt.LastSectionNumber + } + } + + // pointer field(8) + if err = util.WriteUint8ToByte(w, 0); err != nil { + return + } + + cw := &util.Crc32Writer{W: w, Crc32: 0xffffffff} + + // table id(8) + if err = util.WriteUint8ToByte(cw, tableId); err != nil { + return + } + + // sectionSyntaxIndicator(1) + zero(1) + reserved1(2) + sectionLength(12) + // sectionLength 前两个字节固定为00 + // 1 0 11 sectionLength + if err = util.WriteUint16ToByte(cw, sectionSyntaxIndicatorAndSectionLength, true); err != nil { + return + } + + // PAT TransportStreamID(16) or PMT ProgramNumber(16) + if err = util.WriteUint16ToByte(cw, transportStreamIdOrProgramNumber, true); err != nil { + return + } + + // reserved2(2) + versionNumber(5) + currentNextIndicator(1) + // 0x3 << 6 -> 1100 0000 + // 0x3 << 6 | 1 -> 1100 0001 + if err = util.WriteUint8ToByte(cw, versionNumberAndCurrentNextIndicator); err != nil { + return + } + + // sectionNumber(8) + if err = util.WriteUint8ToByte(cw, sectionNumber); err != nil { + return + } + + // lastSectionNumber(8) + if err = util.WriteUint8ToByte(cw, lastSectionNumber); err != nil { + return + } + + // data + if _, err = cw.Write(data); err != nil { + return + } + + // crc32 + crc32 := util.BigLittleSwap(uint(cw.Crc32)) + if err = util.WriteUint32ToByte(cw, uint32(crc32), true); err != nil { + return + } + + return +} diff --git a/avformat/sps.go b/avformat/sps.go new file mode 100644 index 0000000..ad6414d --- /dev/null +++ b/avformat/sps.go @@ -0,0 +1,215 @@ +package avformat + +import ( + "bytes" + + "github.com/Monibuca/engine/util/bits" +) + +type SPSInfo struct { + ProfileIdc uint + LevelIdc uint + + MbWidth uint + MbHeight uint + + CropLeft uint + CropRight uint + CropTop uint + CropBottom uint + + Width uint + Height uint +} + +func ParseSPS(data []byte) (self SPSInfo, err error) { + r := &bits.GolombBitReader{R: bytes.NewReader(data)} + + if _, err = r.ReadBits(8); err != nil { + return + } + + if self.ProfileIdc, err = r.ReadBits(8); err != nil { + return + } + + // constraint_set0_flag-constraint_set6_flag,reserved_zero_2bits + if _, err = r.ReadBits(8); err != nil { + return + } + + // level_idc + if self.LevelIdc, err = r.ReadBits(8); err != nil { + return + } + + // seq_parameter_set_id + if _, err = r.ReadExponentialGolombCode(); err != nil { + return + } + + if self.ProfileIdc == 100 || self.ProfileIdc == 110 || + self.ProfileIdc == 122 || self.ProfileIdc == 244 || + self.ProfileIdc == 44 || self.ProfileIdc == 83 || + self.ProfileIdc == 86 || self.ProfileIdc == 118 { + + var chroma_format_idc uint + if chroma_format_idc, err = r.ReadExponentialGolombCode(); err != nil { + return + } + + if chroma_format_idc == 3 { + // residual_colour_transform_flag + if _, err = r.ReadBit(); err != nil { + return + } + } + + // bit_depth_luma_minus8 + if _, err = r.ReadExponentialGolombCode(); err != nil { + return + } + // bit_depth_chroma_minus8 + if _, err = r.ReadExponentialGolombCode(); err != nil { + return + } + // qpprime_y_zero_transform_bypass_flag + if _, err = r.ReadBit(); err != nil { + return + } + + var seq_scaling_matrix_present_flag uint + if seq_scaling_matrix_present_flag, err = r.ReadBit(); err != nil { + return + } + + if seq_scaling_matrix_present_flag != 0 { + for i := 0; i < 8; i++ { + var seq_scaling_list_present_flag uint + if seq_scaling_list_present_flag, err = r.ReadBit(); err != nil { + return + } + if seq_scaling_list_present_flag != 0 { + var sizeOfScalingList uint + if i < 6 { + sizeOfScalingList = 16 + } else { + sizeOfScalingList = 64 + } + lastScale := uint(8) + nextScale := uint(8) + for j := uint(0); j < sizeOfScalingList; j++ { + if nextScale != 0 { + var delta_scale uint + if delta_scale, err = r.ReadSE(); err != nil { + return + } + nextScale = (lastScale + delta_scale + 256) % 256 + } + if nextScale != 0 { + lastScale = nextScale + } + } + } + } + } + } + + // log2_max_frame_num_minus4 + if _, err = r.ReadExponentialGolombCode(); err != nil { + return + } + + var pic_order_cnt_type uint + if pic_order_cnt_type, err = r.ReadExponentialGolombCode(); err != nil { + return + } + if pic_order_cnt_type == 0 { + // log2_max_pic_order_cnt_lsb_minus4 + if _, err = r.ReadExponentialGolombCode(); err != nil { + return + } + } else if pic_order_cnt_type == 1 { + // delta_pic_order_always_zero_flag + if _, err = r.ReadBit(); err != nil { + return + } + // offset_for_non_ref_pic + if _, err = r.ReadSE(); err != nil { + return + } + // offset_for_top_to_bottom_field + if _, err = r.ReadSE(); err != nil { + return + } + var num_ref_frames_in_pic_order_cnt_cycle uint + if num_ref_frames_in_pic_order_cnt_cycle, err = r.ReadExponentialGolombCode(); err != nil { + return + } + for i := uint(0); i < num_ref_frames_in_pic_order_cnt_cycle; i++ { + if _, err = r.ReadSE(); err != nil { + return + } + } + } + + // max_num_ref_frames + if _, err = r.ReadExponentialGolombCode(); err != nil { + return + } + + // gaps_in_frame_num_value_allowed_flag + if _, err = r.ReadBit(); err != nil { + return + } + + if self.MbWidth, err = r.ReadExponentialGolombCode(); err != nil { + return + } + self.MbWidth++ + + if self.MbHeight, err = r.ReadExponentialGolombCode(); err != nil { + return + } + self.MbHeight++ + + var frame_mbs_only_flag uint + if frame_mbs_only_flag, err = r.ReadBit(); err != nil { + return + } + if frame_mbs_only_flag == 0 { + // mb_adaptive_frame_field_flag + if _, err = r.ReadBit(); err != nil { + return + } + } + + // direct_8x8_inference_flag + if _, err = r.ReadBit(); err != nil { + return + } + + var frame_cropping_flag uint + if frame_cropping_flag, err = r.ReadBit(); err != nil { + return + } + if frame_cropping_flag != 0 { + if self.CropLeft, err = r.ReadExponentialGolombCode(); err != nil { + return + } + if self.CropRight, err = r.ReadExponentialGolombCode(); err != nil { + return + } + if self.CropTop, err = r.ReadExponentialGolombCode(); err != nil { + return + } + if self.CropBottom, err = r.ReadExponentialGolombCode(); err != nil { + return + } + } + + self.Width = (self.MbWidth * 16) - self.CropLeft*2 - self.CropRight*2 + self.Height = ((2 - frame_mbs_only_flag) * self.MbHeight * 16) - self.CropTop*2 - self.CropBottom*2 + + return +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..50f253e --- /dev/null +++ b/config.go @@ -0,0 +1,34 @@ +package engine + +import "log" + +const ( + PLUGIN_SUBSCRIBER = 1 + PLUGIN_PUBLISHER = 1 << 1 + PLUGIN_HOOK = 1 << 2 +) + +var ( + cg = &Config{Plugins: make(map[string]interface{})} + plugins = make(map[string]*PluginConfig) +) + +type PluginConfig struct { + Name string //插件名称 + Type byte //类型 + Config interface{} //插件配置 + Run func() +} + +type Config struct { + Plugins map[string]interface{} +} + +func InstallPlugin(opt *PluginConfig) { + log.Printf("install plugin %s", opt.Name) + plugins[opt.Name] = opt +} + +type ListenerConfig struct { + ListenAddr string +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5e354f2 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module github.com/Monibuca/engine + +go 1.13 + +require ( + github.com/BurntSushi/toml v0.3.1 + github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect + github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 + github.com/go-ole/go-ole v1.2.4 // indirect + github.com/shirou/gopsutil v2.20.1+incompatible + github.com/stretchr/testify v1.5.1 // indirect + golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..61cfe30 --- /dev/null +++ b/go.sum @@ -0,0 +1,41 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk= +github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/elgs/gostrgen v0.0.0-20161222160715-9d61ae07eeae/go.mod h1:wruC5r2gHdr/JIUs5Rr1V45YtsAzKXZxAnn/5rPC97g= +github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0= +github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478/go.mod h1:0j1+svBH8ABEIPdUP0AIg4qedsybnXGJBakCEw8cfoo= +github.com/funny/utest v0.0.0-20161029064919-43870a374500/go.mod h1:mUn39tBov9jKnTWV1RlOYoNzxdBFHiSzXWdY1FoNGGg= +github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI= +github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= +github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +github.com/langhuihui/monibuca v0.4.1 h1:hR5xiVtYJM272ChQUrKdNd+AQyY98SNxVZEx2WAuNmA= +github.com/langhuihui/monibuca v0.4.1/go.mod h1:S4rqYUQ+bCB3WdwuXTJ92FqVRZz5Sh7zAXOJc94JqMI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quangngotan95/go-m3u8 v0.1.0/go.mod h1:smzfWHlYpBATVNu1GapKLYiCtEo5JxridIgvvudZ+Wc= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/shirou/gopsutil v2.19.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil v2.20.1+incompatible h1:oIq9Cq4i84Hk8uQAUOG3eNdI/29hBawGrD5YRl6JRDY= +github.com/shirou/gopsutil v2.20.1+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903/go.mod h1:9o96byDMk+osDZqiIS2a7E7y0cWmg4rRTjQRWVHpFWE= +github.com/yakovlevdmv/WS-Discovery v0.0.0-20180512141937-16170c6c3677/go.mod h1:/VKdrRRbAVE0pvkoPTUlfXw1zxqEpflVsgF25aR5gbk= +github.com/yakovlevdmv/goonvif v0.0.0-20180517145634-8181eb3ef2fb/go.mod h1:Os0AToR0I28wSLpS4rQtZdMEcfGKJcSrTaJughAopv4= +github.com/yakovlevdmv/gosoap v0.0.0-20180512142237-299a954b1c6d/go.mod h1:NhCpqPG+N2wrLSqEHVG3FKl4uAPvtFHUx7IlCVpW1PU= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20200226051749-491c5fce7268/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/hook.go b/hook.go new file mode 100644 index 0000000..61f0dd8 --- /dev/null +++ b/hook.go @@ -0,0 +1,69 @@ +package engine + +var AuthHooks = make(AuthHook, 0) + +type AuthHook []func(string) error + +func (h AuthHook) AddHook(hook func(string) error) { + AuthHooks = append(h, hook) +} +func (h AuthHook) Trigger(sign string) error { + for _, f := range h { + if err := f(sign); err != nil { + return err + } + } + return nil +} + +var OnPublishHooks = make(OnPublishHook, 0) + +type OnPublishHook []func(r *Room) + +func (h OnPublishHook) AddHook(hook func(r *Room)) { + OnPublishHooks = append(h, hook) +} +func (h OnPublishHook) Trigger(r *Room) { + for _, f := range h { + f(r) + } +} + +var OnSubscribeHooks = make(OnSubscribeHook, 0) + +type OnSubscribeHook []func(s *OutputStream) + +func (h OnSubscribeHook) AddHook(hook func(s *OutputStream)) { + OnSubscribeHooks = append(h, hook) +} +func (h OnSubscribeHook) Trigger(s *OutputStream) { + for _, f := range h { + f(s) + } +} + +var OnDropHooks = make(OnDropHook, 0) + +type OnDropHook []func(s *OutputStream) + +func (h OnDropHook) AddHook(hook func(s *OutputStream)) { + OnDropHooks = append(h, hook) +} +func (h OnDropHook) Trigger(s *OutputStream) { + for _, f := range h { + f(s) + } +} + +var OnSummaryHooks = make(OnSummaryHook, 0) + +type OnSummaryHook []func(bool) + +func (h OnSummaryHook) AddHook(hook func(bool)) { + OnSummaryHooks = append(h, hook) +} +func (h OnSummaryHook) Trigger(v bool) { + for _, f := range h { + f(v) + } +} diff --git a/index.go b/index.go new file mode 100644 index 0000000..bba2175 --- /dev/null +++ b/index.go @@ -0,0 +1,53 @@ +package engine + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "os" + "runtime" + "time" + + "github.com/BurntSushi/toml" +) + +var ConfigRaw []byte +var Version = "1.0.0" +var EngineInfo = &struct { + Version string + StartTime time.Time +}{Version, time.Now()} + +func Run(configFile string) (err error) { + if runtime.GOOS == "windows" { + ioutil.WriteFile("shutdown.bat", []byte(fmt.Sprintf("taskkill /pid %d -t -f", os.Getpid())), 0777) + } else { + ioutil.WriteFile("shutdown.sh", []byte(fmt.Sprintf("kill -9 %d", os.Getpid())), 0777) + } + log.Printf("start monibuca version:%s", Version) + if ConfigRaw, err = ioutil.ReadFile(configFile); err != nil { + log.Printf("read config file error: %v", err) + return + } + go Summary.StartSummary() + if _, err = toml.Decode(string(ConfigRaw), cg); err == nil { + for name, config := range plugins { + if cfg, ok := cg.Plugins[name]; ok { + b, _ := json.Marshal(cfg) + if err = json.Unmarshal(b, config.Config); err != nil { + log.Println(err) + continue + } + } else if config.Config != nil { + continue + } + if config.Run != nil { + go config.Run() + } + } + } else { + log.Printf("decode config file error: %v", err) + } + return +} diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..c70126e --- /dev/null +++ b/logger.go @@ -0,0 +1,32 @@ +package engine + +import ( + "io" + "log" +) + +type LogWriter struct { + io.Writer + origin io.Writer +} + +func (w *LogWriter) Write(data []byte) (n int, err error) { + if n, err = w.Writer.Write(data); err != nil { + go log.SetOutput(w.origin) + } + return w.origin.Write(data) +} + +func AddWriter(wn io.Writer) { + log.SetOutput(&LogWriter{ + Writer: wn, + origin: log.Writer(), + }) +} + +func MayBeError(info error) (hasError bool) { + if hasError = info != nil; hasError { + log.Print(info) + } + return +} diff --git a/pool/slice_pool.go b/pool/slice_pool.go new file mode 100644 index 0000000..8f10ec4 --- /dev/null +++ b/pool/slice_pool.go @@ -0,0 +1,21 @@ +package pool + +import ( + "github.com/funny/slab" +) + +var ( + slicePool = slab.NewChanPool( + 16, // The smallest chunk size is 16B. + 64*1024, // The largest chunk size is 64KB. + 2, // Power of 2 growth in chunk size. + 1024*1024, // Each slab will be 1MB in size. + ) +) + +func RecycleSlice(slice []byte) { + slicePool.Free(slice) +} +func GetSlice(s int) []byte { + return slicePool.Alloc(s) +} diff --git a/publisher.go b/publisher.go new file mode 100644 index 0000000..6100788 --- /dev/null +++ b/publisher.go @@ -0,0 +1,38 @@ +package engine + +import ( + "log" + "reflect" + "time" +) + +type Publisher interface { + OnClosed() +} + +type InputStream struct { + *Room +} + +func (p *InputStream) Close() { + if p.Running() { + p.Cancel() + } +} +func (p *InputStream) Running() bool { + return p.Room != nil && p.Err() == nil +} +func (p *InputStream) OnClosed() { +} +func (p *InputStream) Publish(streamPath string, publisher Publisher) bool { + p.Room = AllRoom.Get(streamPath) + if p.Publisher != nil { + return false + } + p.Publisher = publisher + p.Type = reflect.ValueOf(publisher).Elem().Type().Name() + log.Printf("publish set :%s", p.Type) + p.StartTime = time.Now() + OnPublishHooks.Trigger(p.Room) + return true +} diff --git a/room.go b/room.go new file mode 100644 index 0000000..2473f96 --- /dev/null +++ b/room.go @@ -0,0 +1,250 @@ +package engine + +import ( + "context" + "log" + "sync" + "time" + + "github.com/Monibuca/engine/avformat" +) + +var ( + AllRoom = Collection{} + roomCtxBg = context.Background() +) + +type Collection struct { + sync.Map +} + +func (c *Collection) Get(name string) (result *Room) { + item, loaded := AllRoom.LoadOrStore(name, &Room{ + Subscribers: make(map[string]*OutputStream), + Control: make(chan interface{}), + VideoChan: make(chan *avformat.AVPacket, 1), + AudioChan: make(chan *avformat.AVPacket, 1), + }) + result = item.(*Room) + if !loaded { + result.StreamPath = name + result.Context, result.Cancel = context.WithCancel(roomCtxBg) + go result.Run() + } + return +} + +type Room struct { + context.Context + Publisher + RoomInfo + Control chan interface{} + Cancel context.CancelFunc + Subscribers map[string]*OutputStream // 订阅者 + VideoTag *avformat.AVPacket // 每个视频包都是这样的结构,区别在于Payload的大小.FMS在发送AVC sequence header,需要加上 VideoTags,这个tag 1个字节(8bits)的数据 + AudioTag *avformat.AVPacket // 每个音频包都是这样的结构,区别在于Payload的大小.FMS在发送AAC sequence header,需要加上 AudioTags,这个tag 1个字节(8bits)的数据 + FirstScreen []*avformat.AVPacket + AudioChan chan *avformat.AVPacket + VideoChan chan *avformat.AVPacket + UseTimestamp bool //是否采用数据包中的时间戳 +} + +type RoomInfo struct { + StreamPath string + StartTime time.Time + SubscriberInfo []*SubscriberInfo + Type string + VideoInfo struct { + PacketCount int + CodecID byte + SPSInfo avformat.SPSInfo + } + AudioInfo struct { + PacketCount int + SoundFormat byte //4bit + SoundRate int //2bit + SoundSize byte //1bit + SoundType byte //1bit + } +} +type UnSubscribeCmd struct { + *OutputStream +} +type SubscribeCmd struct { + *OutputStream +} +type ChangeRoomCmd struct { + *OutputStream + NewRoom *Room +} + +func (r *Room) onClosed() { + log.Printf("room destoryed :%s", r.StreamPath) + AllRoom.Delete(r.StreamPath) + if r.Publisher != nil { + r.OnClosed() + } +} +func (r *Room) Subscribe(s *OutputStream) { + s.Room = r + if r.Err() == nil { + s.SubscribeTime = time.Now() + log.Printf("subscribe :%s %s,to room %s", s.Type, s.ID, r.StreamPath) + s.packetQueue = make(chan *avformat.SendPacket, 1024) + s.Context, s.Cancel = context.WithCancel(r) + s.Control <- &SubscribeCmd{s} + } +} + +func (r *Room) UnSubscribe(s *OutputStream) { + if r.Err() == nil { + r.Control <- &UnSubscribeCmd{s} + } +} +func (r *Room) Run() { + log.Printf("room create :%s", r.StreamPath) + defer r.onClosed() + update := time.NewTicker(time.Second) + defer update.Stop() + for { + select { + case <-r.Done(): + return + case <-update.C: + if Summary.Running() { + r.SubscriberInfo = make([]*SubscriberInfo, len(r.Subscribers)) + i := 0 + for _, v := range r.Subscribers { + r.SubscriberInfo[i] = &v.SubscriberInfo + i++ + } + } + case s := <-r.Control: + switch v := s.(type) { + case *UnSubscribeCmd: + delete(r.Subscribers, v.ID) + log.Printf("%s subscriber %s removed remains:%d", r.StreamPath, v.ID, len(r.Subscribers)) + if len(r.Subscribers) == 0 && r.Publisher == nil { + r.Cancel() + } + case *SubscribeCmd: + if _, ok := r.Subscribers[v.ID]; !ok { + r.Subscribers[v.ID] = v.OutputStream + log.Printf("%s subscriber %s added remains:%d", r.StreamPath, v.ID, len(r.Subscribers)) + OnSubscribeHooks.Trigger(v.OutputStream) + } + case *ChangeRoomCmd: + if _, ok := v.NewRoom.Subscribers[v.ID]; !ok { + delete(r.Subscribers, v.ID) + v.NewRoom.Subscribe(v.OutputStream) + if len(r.Subscribers) == 0 && r.Publisher == nil { + r.Cancel() + } + } + } + case audio := <-r.AudioChan: + for _, v := range r.Subscribers { + v.sendAudio(audio) + } + case video := <-r.VideoChan: + for _, v := range r.Subscribers { + v.sendVideo(video) + } + } + } +} +func (r *Room) PushAudio(audio *avformat.AVPacket) { + if len(audio.Payload) < 4 { + return + } + if audio.Payload[0] == 0xFF && (audio.Payload[1]&0xF0) == 0xF0 { + //audio.IsADTS = true + r.AudioInfo.SoundFormat = 10 + r.AudioInfo.SoundRate = avformat.SamplingFrequencies[(audio.Payload[2]&0x3c)>>2] + r.AudioInfo.SoundType = ((audio.Payload[2] & 0x1) << 2) | ((audio.Payload[3] & 0xc0) >> 6) + r.AudioTag = audio.ADTS2ASC() + } else if r.AudioTag == nil { + audio.IsAACSequence = true + if len(audio.Payload) < 5 { + return + } + r.AudioTag = audio + tmp := audio.Payload[0] // 第一个字节保存着音频的相关信息 + if r.AudioInfo.SoundFormat = tmp >> 4; r.AudioInfo.SoundFormat == 10 { //真的是AAC的话,后面有一个字节的详细信息 + //0 = AAC sequence header,1 = AAC raw。 + if aacPacketType := audio.Payload[1]; aacPacketType == 0 { + config1 := audio.Payload[2] + config2 := audio.Payload[3] + //audioObjectType = (config1 & 0xF8) >> 3 + // 1 AAC MAIN ISO/IEC 14496-3 subpart 4 + // 2 AAC LC ISO/IEC 14496-3 subpart 4 + // 3 AAC SSR ISO/IEC 14496-3 subpart 4 + // 4 AAC LTP ISO/IEC 14496-3 subpart 4 + r.AudioInfo.SoundRate = avformat.SamplingFrequencies[((config1&0x7)<<1)|(0x90>>7)] + r.AudioInfo.SoundType = (config2 >> 3) & 0x0F //声道 + //frameLengthFlag = (config2 >> 2) & 0x01 + //dependsOnCoreCoder = (config2 >> 1) & 0x01 + //extensionFlag = config2 & 0x01 + } + } else { + r.AudioInfo.SoundRate = avformat.SoundRate[(tmp&0x0c)>>2] // 采样率 0 = 5.5 kHz or 1 = 11 kHz or 2 = 22 kHz or 3 = 44 kHz + r.AudioInfo.SoundSize = (tmp & 0x02) >> 1 // 采样精度 0 = 8-bit samples or 1 = 16-bit samples + r.AudioInfo.SoundType = tmp & 0x01 // 0 单声道,1立体声 + } + return + } + audio.RefCount = len(r.Subscribers) + if !r.UseTimestamp { + audio.Timestamp = uint32(time.Since(r.StartTime) / time.Millisecond) + } + r.AudioInfo.PacketCount++ + r.AudioChan <- audio +} +func (r *Room) setH264Info(video *avformat.AVPacket) { + r.VideoTag = video + if r.VideoInfo.CodecID != 7 { + return + } + info := avformat.AVCDecoderConfigurationRecord{} + //0:codec,1:IsAVCSequence,2~4:compositionTime + if _, err := info.Unmarshal(video.Payload[5:]); err == nil { + r.VideoInfo.SPSInfo, err = avformat.ParseSPS(info.SequenceParameterSetNALUnit) + } +} +func (r *Room) PushVideo(video *avformat.AVPacket) { + if len(video.Payload) < 3 { + return + } + video.VideoFrameType = video.Payload[0] >> 4 // 帧类型 4Bit, H264一般为1或者2 + r.VideoInfo.CodecID = video.Payload[0] & 0x0f // 编码类型ID 4Bit, JPEG, H263, AVC... + video.IsAVCSequence = video.VideoFrameType == 1 && video.Payload[1] == 0 + if r.VideoTag == nil { + if video.IsAVCSequence { + r.setH264Info(video) + } else { + log.Println("no AVCSequence") + } + } else { + //更换AVCSequence + if video.IsAVCSequence { + r.setH264Info(video) + } + if r.FirstScreen != nil { + if video.IsKeyFrame() { + for _, cache := range r.FirstScreen { //清空队列 + cache.Recycle() + } + r.FirstScreen = r.FirstScreen[:0] + } + r.FirstScreen = append(r.FirstScreen, video) + video.RefCount = len(r.Subscribers) + 1 + } else { + video.RefCount = len(r.Subscribers) + } + if !r.UseTimestamp { + video.Timestamp = uint32(time.Since(r.StartTime) / time.Millisecond) + } + r.VideoInfo.PacketCount++ + r.VideoChan <- video + } +} diff --git a/subscriber.go b/subscriber.go new file mode 100644 index 0000000..1a67522 --- /dev/null +++ b/subscriber.go @@ -0,0 +1,135 @@ +package engine + +import ( + "context" + "fmt" + "github.com/Monibuca/engine/avformat" + "time" +) + +type Subscriber interface { + Send(*avformat.SendPacket) error +} + +type SubscriberInfo struct { + ID string + TotalDrop int //总丢帧 + TotalPacket int + Type string + BufferLength int + SubscribeTime time.Time +} +type OutputStream struct { + context.Context + *Room + SubscriberInfo + SendHandler func(*avformat.SendPacket) error + Cancel context.CancelFunc + Sign string + VTSent bool + ATSent bool + VSentTime uint32 + ASentTime uint32 + packetQueue chan *avformat.SendPacket + dropCount int + OffsetTime uint32 + firstScreenIndex int +} + +func (s *OutputStream) IsClosed() bool { + return s.Context != nil && s.Err() != nil +} + +func (s *OutputStream) Close() { + if s.Cancel != nil { + s.Cancel() + } +} +func (s *OutputStream) Play(streamPath string) (err error) { + AllRoom.Get(streamPath).Subscribe(s) + defer s.UnSubscribe(s) + for { + select { + case <-s.Done(): + return s.Err() + case p := <-s.packetQueue: + if err = s.SendHandler(p); err != nil { + s.Cancel() //此处为了使得IsClosed 返回true + return + } + p.Recycle() + } + } +} +func (s *OutputStream) sendPacket(packet *avformat.AVPacket, timestamp uint32) { + if !packet.IsAVCSequence && timestamp == 0 { + timestamp = 1 //防止为0 + } + s.TotalPacket++ + s.BufferLength = len(s.packetQueue) + if s.dropCount > 0 { + if packet.IsKeyFrame() { + fmt.Printf("%s drop packet:%d\n", s.ID, s.dropCount) + s.dropCount = 0 //退出丢包 + } else { + s.dropCount++ + s.TotalDrop++ + return + } + } + if s.BufferLength == cap(s.packetQueue) { + s.dropCount++ + s.TotalDrop++ + packet.Recycle() + } else if !s.IsClosed() { + s.packetQueue <- avformat.NewSendPacket(packet, timestamp) + } +} + +func (s *OutputStream) sendVideo(video *avformat.AVPacket) error { + isKF := video.IsKeyFrame() + if s.VTSent { + if s.FirstScreen == nil || s.firstScreenIndex == -1 { + s.sendPacket(video, video.Timestamp-s.VSentTime+s.OffsetTime) + } else if !isKF && s.firstScreenIndex < len(s.FirstScreen) { + firstScreen := s.FirstScreen[s.firstScreenIndex] + firstScreen.RefCount++ + s.VSentTime = firstScreen.Timestamp - s.FirstScreen[0].Timestamp + s.sendPacket(firstScreen, s.VSentTime) + video.Recycle() //回收当前数据 + s.firstScreenIndex++ + } else { + s.firstScreenIndex = -1 //收到关键帧或者首屏缓冲已播完后退出首屏渲染模式 + s.OffsetTime += s.VSentTime + s.VSentTime = video.Timestamp + s.sendPacket(video, s.OffsetTime) + } + return nil + } + //非首屏渲染模式跳过开头的非关键帧 + if !isKF { + if s.FirstScreen == nil { + return nil + } + } else if s.FirstScreen != nil { + s.firstScreenIndex = -1 //跳过首屏渲染 + } + s.VTSent = true + s.sendPacket(s.VideoTag, 0) + s.VSentTime = video.Timestamp + return s.sendVideo(video) +} +func (s *OutputStream) sendAudio(audio *avformat.AVPacket) error { + if s.ATSent { + if s.FirstScreen != nil && s.firstScreenIndex == -1 { + audio.Recycle() + return nil + } + s.sendPacket(audio, audio.Timestamp-s.ASentTime) + return nil + } + s.ATSent = true + s.sendPacket(s.AudioTag, 0) + s.ASentTime = audio.Timestamp + return s.sendAudio(audio) +} diff --git a/summary.go b/summary.go new file mode 100644 index 0000000..a9e5031 --- /dev/null +++ b/summary.go @@ -0,0 +1,142 @@ +package engine + +import ( + "log" + "time" + + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/disk" + "github.com/shirou/gopsutil/mem" + "github.com/shirou/gopsutil/net" +) + +var ( + Summary = ServerSummary{} +) + +type ServerSummary struct { + Address string + Memory struct { + Total uint64 + Free uint64 + Used uint64 + Usage float64 + } + CPUUsage float64 + HardDisk struct { + Total uint64 + Free uint64 + Used uint64 + Usage float64 + } + NetWork []NetWorkInfo + Rooms []*RoomInfo + lastNetWork []NetWorkInfo + ref int + control chan bool + reportChan chan *ServerSummary + Children map[string]*ServerSummary +} +type NetWorkInfo struct { + Name string + Receive uint64 + Sent uint64 + ReceiveSpeed uint64 + SentSpeed uint64 +} + +func (s *ServerSummary) StartSummary() { + ticker := time.NewTicker(time.Second) + s.control = make(chan bool) + s.reportChan = make(chan *ServerSummary) + for { + select { + case <-ticker.C: + if s.ref > 0 { + Summary.collect() + } + case v := <-s.control: + if v { + if s.ref++; s.ref == 1 { + log.Println("start report summary") + OnSummaryHooks.Trigger(true) + } + } else { + if s.ref--; s.ref == 0 { + s.lastNetWork = nil + log.Println("stop report summary") + OnSummaryHooks.Trigger(false) + } + } + case report := <-s.reportChan: + s.Children[report.Address] = report + } + } +} +func (s *ServerSummary) Running() bool { + return s.ref > 0 +} +func (s *ServerSummary) Add() { + s.control <- true +} +func (s *ServerSummary) Done() { + s.control <- false +} +func (s *ServerSummary) Report(slave *ServerSummary) { + s.reportChan <- slave +} +func (s *ServerSummary) collect() { + v, _ := mem.VirtualMemory() + //c, _ := cpu.Info() + cc, _ := cpu.Percent(time.Second, false) + d, _ := disk.Usage("/") + //n, _ := host.Info() + nv, _ := net.IOCounters(true) + //boottime, _ := host.BootTime() + //btime := time.Unix(int64(boottime), 0).Format("2006-01-02 15:04:05") + s.Memory.Total = v.Total / 1024 / 1024 + s.Memory.Free = v.Available / 1024 / 1024 + s.Memory.Used = v.Used / 1024 / 1024 + s.Memory.Usage = v.UsedPercent + //fmt.Printf(" Mem : %v MB Free: %v MB Used:%v Usage:%f%%\n", v.Total/1024/1024, v.Available/1024/1024, v.Used/1024/1024, v.UsedPercent) + //if len(c) > 1 { + // for _, sub_cpu := range c { + // modelname := sub_cpu.ModelName + // cores := sub_cpu.Cores + // fmt.Printf(" CPU : %v %v cores \n", modelname, cores) + // } + //} else { + // sub_cpu := c[0] + // modelname := sub_cpu.ModelName + // cores := sub_cpu.Cores + // fmt.Printf(" CPU : %v %v cores \n", modelname, cores) + //} + s.CPUUsage = cc[0] + s.HardDisk.Free = d.Free / 1024 / 1024 / 1024 + s.HardDisk.Total = d.Total / 1024 / 1024 / 1024 + s.HardDisk.Used = d.Used / 1024 / 1024 / 1024 + s.HardDisk.Usage = d.UsedPercent + s.NetWork = make([]NetWorkInfo, len(nv)) + for i, n := range nv { + s.NetWork[i].Name = n.Name + s.NetWork[i].Receive = n.BytesRecv + s.NetWork[i].Sent = n.BytesSent + if s.lastNetWork != nil && len(s.lastNetWork) > i { + s.NetWork[i].ReceiveSpeed = n.BytesRecv - s.lastNetWork[i].Receive + s.NetWork[i].SentSpeed = n.BytesSent - s.lastNetWork[i].Sent + } + } + s.lastNetWork = s.NetWork + //fmt.Printf(" Network: %v bytes / %v bytes\n", nv[0].BytesRecv, nv[0].BytesSent) + //fmt.Printf(" SystemBoot:%v\n", btime) + //fmt.Printf(" CPU Used : used %f%% \n", cc[0]) + //fmt.Printf(" HD : %v GB Free: %v GB Usage:%f%%\n", d.Total/1024/1024/1024, d.Free/1024/1024/1024, d.UsedPercent) + //fmt.Printf(" OS : %v(%v) %v \n", n.Platform, n.PlatformFamily, n.PlatformVersion) + //fmt.Printf(" Hostname : %v \n", n.Hostname) + s.Rooms = nil + AllRoom.Range(func(key interface{}, v interface{}) bool { + s.Rooms = append(s.Rooms, &v.(*Room).RoomInfo) + return true + }) + return +} diff --git a/util/SSE.go b/util/SSE.go new file mode 100644 index 0000000..8581fdc --- /dev/null +++ b/util/SSE.go @@ -0,0 +1,73 @@ +package util + +import ( + "context" + "encoding/json" + "net/http" + "os/exec" +) + +var ( + sseEent = []byte("event: ") + sseBegin = []byte("data: ") + sseEnd = []byte("\n\n") +) + +type SSE struct { + http.ResponseWriter + context.Context +} + +func (sse *SSE) Write(data []byte) (n int, err error) { + if err = sse.Err(); err != nil { + return + } + _, err = sse.ResponseWriter.Write(sseBegin) + n, err = sse.ResponseWriter.Write(data) + _, err = sse.ResponseWriter.Write(sseEnd) + if err != nil { + return + } + sse.ResponseWriter.(http.Flusher).Flush() + return +} + +func (sse *SSE) WriteEvent(event string, data []byte) (err error) { + if err = sse.Err(); err != nil { + return + } + _, err = sse.ResponseWriter.Write(sseEent) + _, err = sse.ResponseWriter.Write([]byte(event)) + _, err = sse.ResponseWriter.Write([]byte("\n")) + _, err = sse.Write(data) + return +} + +func NewSSE(w http.ResponseWriter, ctx context.Context) *SSE { + header := w.Header() + header.Set("Content-Type", "text/event-stream") + header.Set("Cache-Control", "no-cache") + header.Set("Connection", "keep-alive") + header.Set("X-Accel-Buffering", "no") + header.Set("Access-Control-Allow-Origin", "*") + return &SSE{ + w, + ctx, + } +} + +func (sse *SSE) WriteJSON(data interface{}) (err error) { + var jsonData []byte + if jsonData, err = json.Marshal(data); err == nil { + if _, err = sse.Write(jsonData); err != nil { + return + } + return + } + return +} +func (sse *SSE) WriteExec(cmd *exec.Cmd) error { + cmd.Stderr = sse + cmd.Stdout = sse + return cmd.Run() +} diff --git a/util/big_little_endian.go b/util/big_little_endian.go new file mode 100644 index 0000000..2bb9f57 --- /dev/null +++ b/util/big_little_endian.go @@ -0,0 +1,269 @@ +package util + +// +// 注意:RTMP模式下都是大端模式 +// + +var LittleEndian littleEndian + +// BigEndian is the big-endian implementation of ByteOrder. +var BigEndian bigEndian + +// 低位字节排放在内存的低地址端,高位字节排放在内存的高地址端. +type littleEndian struct{} + +// b == 0x1234, b[0] == 0x12, b[1] == 0x34 +// b[0]低字节 b[1]高字节 +// 内存地址 低 -> 高 +// 0x34 0x12 + +// byte(v)低字节 b[0]内存低地址 +// byte(v>>8)高字节 b[1]内存高地址 + +// b == 2222 2222 1111 1111 +// b >> 8 -> 0000 0000 2222 2222 +// b << 8 -> 1111 1111 0000 0000 + +func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 } +func (littleEndian) Uint24(b []byte) uint32 { return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 } +func (littleEndian) Uint32(b []byte) uint32 { + return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 +} +func (littleEndian) Uint40(b []byte) uint64 { + return uint64(b[0]) | uint64(b[1])<<8 | + uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 +} +func (littleEndian) Uint48(b []byte) uint64 { + return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | + uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 +} +func (littleEndian) Uint64(b []byte) uint64 { + return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | + uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 +} + +// +// Put +// + +func (littleEndian) PutUint16(b []byte, v uint16) { + b[0] = byte(v) + b[1] = byte(v >> 8) +} +func (littleEndian) PutUint24(b []byte, v uint32) { + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) +} +func (littleEndian) PutUint32(b []byte, v uint32) { + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) +} +func (littleEndian) PutUint64(b []byte, v uint64) { + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + b[4] = byte(v >> 32) + b[5] = byte(v >> 40) + b[6] = byte(v >> 48) + b[7] = byte(v >> 56) +} + +// +// To +// + +func (littleEndian) ToUint16(v uint16) []byte { + b := make([]byte, 2) + b[0] = byte(v) + b[1] = byte(v >> 8) + return b +} +func (littleEndian) ToUint24(v uint32) []byte { + b := make([]byte, 3) + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + return b +} +func (littleEndian) ToUint32(v uint32) []byte { + b := make([]byte, 4) + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + return b +} +func (littleEndian) ToUint40(v uint64) []byte { + b := make([]byte, 5) + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + b[4] = byte(v >> 32) + return b +} +func (littleEndian) ToUint48(v uint64) []byte { + b := make([]byte, 6) + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + b[4] = byte(v >> 32) + b[5] = byte(v >> 40) + return b +} +func (littleEndian) ToUint64(v uint64) []byte { + b := make([]byte, 8) + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + b[4] = byte(v >> 32) + b[5] = byte(v >> 40) + b[6] = byte(v >> 48) + b[7] = byte(v >> 56) + return b +} + +// 高位字节排放在内存的低地址端,低位字节排放在内存的高地址端 +type bigEndian struct{} + +// b == 0x1234, b[0] == 0x12, b[1] == 0x34 +// 内存地址 低 -> 高 +// 0x12 0x34 +func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 } +func (bigEndian) Uint24(b []byte) uint32 { return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16 } +func (bigEndian) Uint32(b []byte) uint32 { + return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 +} +func (bigEndian) Uint40(b []byte) uint64 { + return uint64(b[4]) | uint64(b[3])<<8 | + uint64(b[2])<<16 | uint64(b[1])<<24 | uint64(b[0])<<32 +} +func (bigEndian) Uint48(b []byte) uint64 { + return uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | + uint64(b[2])<<24 | uint64(b[1])<<32 | uint64(b[0])<<40 +} +func (bigEndian) Uint64(b []byte) uint64 { + return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | + uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 +} + +// +// Put +// + +func (bigEndian) PutUint16(b []byte, v uint16) { + b[0] = byte(v >> 8) + b[1] = byte(v) +} +func (bigEndian) PutUint24(b []byte, v uint32) { + b[0] = byte(v >> 16) + b[1] = byte(v >> 8) + b[2] = byte(v) +} +func (bigEndian) PutUint32(b []byte, v uint32) { + b[0] = byte(v >> 24) + b[1] = byte(v >> 16) + b[2] = byte(v >> 8) + b[3] = byte(v) +} +func (bigEndian) PutUint64(b []byte, v uint64) { + b[0] = byte(v >> 56) + b[1] = byte(v >> 48) + b[2] = byte(v >> 40) + b[3] = byte(v >> 32) + b[4] = byte(v >> 24) + b[5] = byte(v >> 16) + b[6] = byte(v >> 8) + b[7] = byte(v) +} + +// +// To +// + +func (bigEndian) ToUint16(v uint16) []byte { + b := make([]byte, 2) + b[0] = byte(v >> 8) + b[1] = byte(v) + return b +} +func (bigEndian) ToUint24(v uint32) []byte { + b := make([]byte, 3) + b[0] = byte(v >> 16) + b[1] = byte(v >> 8) + b[2] = byte(v) + return b +} +func (bigEndian) ToUint32(v uint32) []byte { + b := make([]byte, 4) + b[0] = byte(v >> 24) + b[1] = byte(v >> 16) + b[2] = byte(v >> 8) + b[3] = byte(v) + return b +} +func (bigEndian) ToUint40(v uint64) []byte { + b := make([]byte, 5) + b[0] = byte(v >> 32) + b[1] = byte(v >> 24) + b[2] = byte(v >> 16) + b[3] = byte(v >> 8) + b[4] = byte(v) + return b +} +func (bigEndian) ToUint48(v uint64) []byte { + b := make([]byte, 6) + b[0] = byte(v >> 40) + b[1] = byte(v >> 32) + b[2] = byte(v >> 24) + b[3] = byte(v >> 16) + b[4] = byte(v >> 8) + b[5] = byte(v) + return b +} +func (bigEndian) ToUint64(v uint64) []byte { + b := make([]byte, 8) + b[0] = byte(v >> 56) + b[1] = byte(v >> 48) + b[2] = byte(v >> 40) + b[3] = byte(v >> 32) + b[4] = byte(v >> 24) + b[5] = byte(v >> 16) + b[6] = byte(v >> 8) + b[7] = byte(v) + return b +} + +//哥伦布解码 +func GetUev(buff []byte, start int) (value int, pos int) { + l := len(buff) + var nZeroNum uint = 0 + for start < l*8 { + if (buff[start/8] & (0x80 >> uint(start%8))) > 0 { + break + } + nZeroNum += 1 + start += 1 + } + dwRet := 0 + start += 1 + var i uint + for i = 0; i < nZeroNum; i++ { + dwRet <<= 1 + if (buff[start/8] & (0x80 >> uint(start%8))) > 0 { + dwRet += 1 + } + start += 1 + } + return (1 << nZeroNum) - 1 + dwRet, start +} + +func BigLittleSwap(v uint) uint { + return (v >> 24) | ((v>>16)&0xff)<<8 | ((v>>8)&0xff)<<16 | (v&0xff)<<24 +} diff --git a/util/bits/bits.go b/util/bits/bits.go new file mode 100644 index 0000000..4a09f0a --- /dev/null +++ b/util/bits/bits.go @@ -0,0 +1,118 @@ +package bits + +import ( + "io" +) + +type Reader struct { + R io.Reader + n int + bits uint64 +} + +func (self *Reader) ReadBits64(n int) (bits uint64, err error) { + if self.n < n { + var b [8]byte + var got int + want := (n - self.n + 7) / 8 + if got, err = self.R.Read(b[:want]); err != nil { + return + } + if got < want { + err = io.EOF + return + } + for i := 0; i < got; i++ { + self.bits <<= 8 + self.bits |= uint64(b[i]) + } + self.n += got * 8 + } + bits = self.bits >> uint(self.n-n) + self.bits ^= bits << uint(self.n-n) + self.n -= n + return +} + +func (self *Reader) ReadBits(n int) (bits uint, err error) { + var bits64 uint64 + if bits64, err = self.ReadBits64(n); err != nil { + return + } + bits = uint(bits64) + return +} + +func (self *Reader) Read(p []byte) (n int, err error) { + for n < len(p) { + want := 8 + if len(p)-n < want { + want = len(p) - n + } + var bits uint64 + if bits, err = self.ReadBits64(want * 8); err != nil { + break + } + for i := 0; i < want; i++ { + p[n+i] = byte(bits >> uint((want-i-1)*8)) + } + n += want + } + return +} + +type Writer struct { + W io.Writer + n int + bits uint64 +} + +func (self *Writer) WriteBits64(bits uint64, n int) (err error) { + if self.n+n > 64 { + move := uint(64 - self.n) + mask := bits >> move + self.bits = (self.bits << move) | mask + self.n = 64 + if err = self.FlushBits(); err != nil { + return + } + n -= int(move) + bits ^= (mask << move) + } + self.bits = (self.bits << uint(n)) | bits + self.n += n + return +} + +func (self *Writer) WriteBits(bits uint, n int) (err error) { + return self.WriteBits64(uint64(bits), n) +} + +func (self *Writer) Write(p []byte) (n int, err error) { + for n < len(p) { + if err = self.WriteBits64(uint64(p[n]), 8); err != nil { + return + } + n++ + } + return +} + +func (self *Writer) FlushBits() (err error) { + if self.n > 0 { + var b [8]byte + bits := self.bits + if self.n%8 != 0 { + bits <<= uint(8 - (self.n % 8)) + } + want := (self.n + 7) / 8 + for i := 0; i < want; i++ { + b[i] = byte(bits >> uint((want-i-1)*8)) + } + if _, err = self.W.Write(b[:want]); err != nil { + return + } + self.n = 0 + } + return +} diff --git a/util/bits/bits_test.go b/util/bits/bits_test.go new file mode 100644 index 0000000..c4957c9 --- /dev/null +++ b/util/bits/bits_test.go @@ -0,0 +1,51 @@ +package bits + +import ( + "bytes" + "testing" +) + +func TestBits(t *testing.T) { + rdata := []byte{0xf3, 0xb3, 0x45, 0x60} + rbuf := bytes.NewReader(rdata[:]) + r := &Reader{R: rbuf} + var u32 uint + if u32, _ = r.ReadBits(4); u32 != 0xf { + t.FailNow() + } + if u32, _ = r.ReadBits(4); u32 != 0x3 { + t.FailNow() + } + if u32, _ = r.ReadBits(2); u32 != 0x2 { + t.FailNow() + } + if u32, _ = r.ReadBits(2); u32 != 0x3 { + t.FailNow() + } + b := make([]byte, 2) + if r.Read(b); b[0] != 0x34 || b[1] != 0x56 { + t.FailNow() + } + + wbuf := &bytes.Buffer{} + w := &Writer{W: wbuf} + w.WriteBits(0xf, 4) + w.WriteBits(0x3, 4) + w.WriteBits(0x2, 2) + w.WriteBits(0x3, 2) + n, _ := w.Write([]byte{0x34, 0x56}) + if n != 2 { + t.FailNow() + } + w.FlushBits() + wdata := wbuf.Bytes() + if wdata[0] != 0xf3 || wdata[1] != 0xb3 || wdata[2] != 0x45 || wdata[3] != 0x60 { + t.FailNow() + } + + b = make([]byte, 8) + PutUInt64BE(b, 0x11223344, 32) + if b[0] != 0x11 || b[1] != 0x22 || b[2] != 0x33 || b[3] != 0x44 { + t.FailNow() + } +} diff --git a/util/bits/bufio/bufio.go b/util/bits/bufio/bufio.go new file mode 100644 index 0000000..fdaa88e --- /dev/null +++ b/util/bits/bufio/bufio.go @@ -0,0 +1,22 @@ +package bufio + +import ( + "io" +) + +type Reader struct { + buf [][]byte + R io.ReadSeeker +} + +func NewReaderSize(r io.ReadSeeker, size int) *Reader { + buf := make([]byte, size*2) + return &Reader{ + R: r, + buf: [][]byte{buf[0:size], buf[size:]}, + } +} + +func (self *Reader) ReadAt(b []byte, off int64) (n int, err error) { + return +} diff --git a/util/bits/golomb_reader.go b/util/bits/golomb_reader.go new file mode 100644 index 0000000..da57cb2 --- /dev/null +++ b/util/bits/golomb_reader.go @@ -0,0 +1,65 @@ +package bits + +import ( + "io" +) + +type GolombBitReader struct { + R io.Reader + buf [1]byte + left byte +} + +func (self *GolombBitReader) ReadBit() (res uint, err error) { + if self.left == 0 { + if _, err = self.R.Read(self.buf[:]); err != nil { + return + } + self.left = 8 + } + self.left-- + res = uint(self.buf[0]>>self.left) & 1 + return +} + +func (self *GolombBitReader) ReadBits(n int) (res uint, err error) { + for i := 0; i < n; i++ { + var bit uint + if bit, err = self.ReadBit(); err != nil { + return + } + res |= bit << uint(n-i-1) + } + return +} + +func (self *GolombBitReader) ReadExponentialGolombCode() (res uint, err error) { + i := 0 + for { + var bit uint + if bit, err = self.ReadBit(); err != nil { + return + } + if !(bit == 0 && i < 32) { + break + } + i++ + } + if res, err = self.ReadBits(i); err != nil { + return + } + res += (1 << uint(i)) - 1 + return +} + +func (self *GolombBitReader) ReadSE() (res uint, err error) { + if res, err = self.ReadExponentialGolombCode(); err != nil { + return + } + if res&0x01 != 0 { + res = (res + 1) / 2 + } else { + res = -res / 2 + } + return +} diff --git a/util/bits/pio/pio.go b/util/bits/pio/pio.go new file mode 100644 index 0000000..4a73aa9 --- /dev/null +++ b/util/bits/pio/pio.go @@ -0,0 +1,3 @@ +package pio + +var RecommendBufioSize = 1024 * 64 diff --git a/util/bits/pio/reader.go b/util/bits/pio/reader.go new file mode 100644 index 0000000..c28a119 --- /dev/null +++ b/util/bits/pio/reader.go @@ -0,0 +1,121 @@ +package pio + +func U8(b []byte) (i uint8) { + return b[0] +} + +func U16BE(b []byte) (i uint16) { + i = uint16(b[0]) + i <<= 8 + i |= uint16(b[1]) + return +} + +func I16BE(b []byte) (i int16) { + i = int16(b[0]) + i <<= 8 + i |= int16(b[1]) + return +} + +func I24BE(b []byte) (i int32) { + i = int32(int8(b[0])) + i <<= 8 + i |= int32(b[1]) + i <<= 8 + i |= int32(b[2]) + return +} + +func U24BE(b []byte) (i uint32) { + i = uint32(b[0]) + i <<= 8 + i |= uint32(b[1]) + i <<= 8 + i |= uint32(b[2]) + return +} + +func I32BE(b []byte) (i int32) { + i = int32(int8(b[0])) + i <<= 8 + i |= int32(b[1]) + i <<= 8 + i |= int32(b[2]) + i <<= 8 + i |= int32(b[3]) + return +} + +func U32LE(b []byte) (i uint32) { + i = uint32(b[3]) + i <<= 8 + i |= uint32(b[2]) + i <<= 8 + i |= uint32(b[1]) + i <<= 8 + i |= uint32(b[0]) + return +} + +func U32BE(b []byte) (i uint32) { + i = uint32(b[0]) + i <<= 8 + i |= uint32(b[1]) + i <<= 8 + i |= uint32(b[2]) + i <<= 8 + i |= uint32(b[3]) + return +} + +func U40BE(b []byte) (i uint64) { + i = uint64(b[0]) + i <<= 8 + i |= uint64(b[1]) + i <<= 8 + i |= uint64(b[2]) + i <<= 8 + i |= uint64(b[3]) + i <<= 8 + i |= uint64(b[4]) + return +} + +func U64BE(b []byte) (i uint64) { + i = uint64(b[0]) + i <<= 8 + i |= uint64(b[1]) + i <<= 8 + i |= uint64(b[2]) + i <<= 8 + i |= uint64(b[3]) + i <<= 8 + i |= uint64(b[4]) + i <<= 8 + i |= uint64(b[5]) + i <<= 8 + i |= uint64(b[6]) + i <<= 8 + i |= uint64(b[7]) + return +} + +func I64BE(b []byte) (i int64) { + i = int64(int8(b[0])) + i <<= 8 + i |= int64(b[1]) + i <<= 8 + i |= int64(b[2]) + i <<= 8 + i |= int64(b[3]) + i <<= 8 + i |= int64(b[4]) + i <<= 8 + i |= int64(b[5]) + i <<= 8 + i |= int64(b[6]) + i <<= 8 + i |= int64(b[7]) + return +} diff --git a/util/bits/pio/vec.go b/util/bits/pio/vec.go new file mode 100644 index 0000000..7a1c229 --- /dev/null +++ b/util/bits/pio/vec.go @@ -0,0 +1,68 @@ +package pio + +func VecLen(vec [][]byte) (n int) { + for _, b := range vec { + n += len(b) + } + return +} + +func VecSliceTo(in [][]byte, out [][]byte, s int, e int) (n int) { + if s < 0 { + s = 0 + } + + if e >= 0 && e < s { + panic("pio: VecSlice start > end") + } + + i := 0 + off := 0 + for s > 0 && i < len(in) { + left := len(in[i]) + read := s + if left < read { + read = left + } + left -= read + off += read + s -= read + e -= read + if left == 0 { + i++ + off = 0 + } + } + if s > 0 { + panic("pio: VecSlice start out of range") + } + + for e != 0 && i < len(in) { + left := len(in[i]) - off + read := left + if e > 0 && e < read { + read = e + } + out[n] = in[i][off : off+read] + n++ + left -= read + e -= read + off += read + if left == 0 { + i++ + off = 0 + } + } + if e > 0 { + panic("pio: VecSlice end out of range") + } + + return +} + +func VecSlice(in [][]byte, s int, e int) (out [][]byte) { + out = make([][]byte, len(in)) + n := VecSliceTo(in, out, s, e) + out = out[:n] + return +} diff --git a/util/bits/pio/vec_test.go b/util/bits/pio/vec_test.go new file mode 100644 index 0000000..8c43769 --- /dev/null +++ b/util/bits/pio/vec_test.go @@ -0,0 +1,21 @@ +package pio + +import ( + "fmt" +) + +func ExampleVec() { + vec := [][]byte{[]byte{1, 2, 3}, []byte{4, 5, 6, 7, 8, 9}, []byte{10, 11, 12, 13}} + println(VecLen(vec)) + + vec = VecSlice(vec, 1, -1) + fmt.Println(vec) + + vec = VecSlice(vec, 2, -1) + fmt.Println(vec) + + vec = VecSlice(vec, 8, 8) + fmt.Println(vec) + + // Output: +} diff --git a/util/bits/pio/writer.go b/util/bits/pio/writer.go new file mode 100644 index 0000000..fdbb1b6 --- /dev/null +++ b/util/bits/pio/writer.go @@ -0,0 +1,87 @@ +package pio + +func PutU8(b []byte, v uint8) { + b[0] = v +} + +func PutI16BE(b []byte, v int16) { + b[0] = byte(v >> 8) + b[1] = byte(v) +} + +func PutU16BE(b []byte, v uint16) { + b[0] = byte(v >> 8) + b[1] = byte(v) +} + +func PutI24BE(b []byte, v int32) { + b[0] = byte(v >> 16) + b[1] = byte(v >> 8) + b[2] = byte(v) +} + +func PutU24BE(b []byte, v uint32) { + b[0] = byte(v >> 16) + b[1] = byte(v >> 8) + b[2] = byte(v) +} + +func PutI32BE(b []byte, v int32) { + b[0] = byte(v >> 24) + b[1] = byte(v >> 16) + b[2] = byte(v >> 8) + b[3] = byte(v) +} + +func PutU32BE(b []byte, v uint32) { + b[0] = byte(v >> 24) + b[1] = byte(v >> 16) + b[2] = byte(v >> 8) + b[3] = byte(v) +} + +func PutU32LE(b []byte, v uint32) { + b[3] = byte(v >> 24) + b[2] = byte(v >> 16) + b[1] = byte(v >> 8) + b[0] = byte(v) +} + +func PutU40BE(b []byte, v uint64) { + b[0] = byte(v >> 32) + b[1] = byte(v >> 24) + b[2] = byte(v >> 16) + b[3] = byte(v >> 8) + b[4] = byte(v) +} + +func PutU48BE(b []byte, v uint64) { + b[0] = byte(v >> 40) + b[1] = byte(v >> 32) + b[2] = byte(v >> 24) + b[3] = byte(v >> 16) + b[4] = byte(v >> 8) + b[5] = byte(v) +} + +func PutU64BE(b []byte, v uint64) { + b[0] = byte(v >> 56) + b[1] = byte(v >> 48) + b[2] = byte(v >> 40) + b[3] = byte(v >> 32) + b[4] = byte(v >> 24) + b[5] = byte(v >> 16) + b[6] = byte(v >> 8) + b[7] = byte(v) +} + +func PutI64BE(b []byte, v int64) { + b[0] = byte(v >> 56) + b[1] = byte(v >> 48) + b[2] = byte(v >> 40) + b[3] = byte(v >> 32) + b[4] = byte(v >> 24) + b[5] = byte(v >> 16) + b[6] = byte(v >> 8) + b[7] = byte(v) +} diff --git a/util/convert.go b/util/convert.go new file mode 100644 index 0000000..2bf429b --- /dev/null +++ b/util/convert.go @@ -0,0 +1,384 @@ +package util + +import ( + "errors" + "io" +) + +/* +func ReadByteToUintX(r io.Reader, l int) (data uint64, err error) { + if l%8 != 0 || l > 64 { + return 0, errors.New("disable convert") + } + + bb := make([]byte, l) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + switch l / 8 { + case 1: + { + return uint8(bb[0]), nil + } + case 2: + { + return BigEndian.Uint16(bb), nil + } + case 3: + { + return BigEndian.Uint24(bb), nil + } + case 4: + { + return BigEndian.Uint32(bb), nil + } + case 5: + { + //return BigEndian.Uint40(bb), nil + return 0, errors.New("disable convert") + } + case 6: + { + return BigEndian.Uint48(bb), nil + } + case 7: + { + //return BigEndian.Uint56(bb), nil + return 0, errors.New("disable convert") + } + case 8: + { + return BigEndian.Uint64(bb), nil + } + } + + return 0, errors.New("convert not exist") +} +*/ + +// // 千万注意大小端,RTMP是大端 +func ByteToUint32N(data []byte) (ret uint32, err error) { + if len(data) > 4 { + return 0, errors.New("ByteToUint32N error!") + } + + for i := 0; i < len(data); i++ { + ret <<= 8 + ret |= uint32(data[i]) + } + + return +} + +// // 千万注意大小端,RTMP是大端 +func ByteToUint64N(data []byte) (ret uint64, err error) { + if len(data) > 8 { + return 0, errors.New("ByteToUint64N error!") + } + + for i := 0; i < len(data); i++ { + ret <<= 8 + ret |= uint64(data[i]) + } + + return +} + +// 千万注意大小端,RTMP是大端 +func ByteToUint32(data []byte, bigEndian bool) (ret uint32, err error) { + if bigEndian { + return BigEndian.Uint32(data), nil + } else { + return LittleEndian.Uint32(data), nil + } +} + +func Uint32ToByte(data uint32, bigEndian bool) (ret []byte, err error) { + if bigEndian { + return BigEndian.ToUint32(data), nil + } else { + return LittleEndian.ToUint32(data), nil + } +} + +func ReadByteToUint8(r io.Reader) (data uint8, err error) { + bb := make([]byte, 1) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + return uint8(bb[0]), nil +} + +func ReadByteToUint16(r io.Reader, bigEndian bool) (data uint16, err error) { + bb := make([]byte, 2) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + if bigEndian { + return BigEndian.Uint16(bb), nil + } else { + return LittleEndian.Uint16(bb), nil + } +} + +func ReadByteToUint24(r io.Reader, bigEndian bool) (data uint32, err error) { + bb := make([]byte, 3) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + if bigEndian { + return BigEndian.Uint24(bb), nil + } else { + return LittleEndian.Uint24(bb), nil + } +} + +func ReadByteToUint32(r io.Reader, bigEndian bool) (data uint32, err error) { + bb := make([]byte, 4) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + if bigEndian { + return BigEndian.Uint32(bb), nil + } else { + return LittleEndian.Uint32(bb), nil + } +} + +func ReadByteToUint40(r io.Reader, bigEndian bool) (data uint64, err error) { + bb := make([]byte, 5) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + if bigEndian { + return BigEndian.Uint40(bb), nil + } else { + return LittleEndian.Uint40(bb), nil + } +} + +func ReadByteToUint48(r io.Reader, bigEndian bool) (data uint64, err error) { + bb := make([]byte, 6) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + if bigEndian { + return BigEndian.Uint48(bb), nil + } else { + return LittleEndian.Uint48(bb), nil + } +} + +/* +func ReadByteToUint56(r io.Reader) (data uint64, err error) { + bb := make([]byte, 7) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + return uint8(bb[0]), nil +} +*/ + +func ReadByteToUint64(r io.Reader, bigEndian bool) (data uint64, err error) { + bb := make([]byte, 8) + if _, err := io.ReadFull(r, bb); err != nil { + return 0, err + } + + if bigEndian { + return BigEndian.Uint64(bb), nil + } else { + return LittleEndian.Uint64(bb), nil + } +} + +func WriteUint8ToByte(w io.Writer, data uint8) error { + bb := make([]byte, 8) + bb[0] = byte(data) + _, err := w.Write(bb[:1]) + if err != nil { + return err + } + + return nil +} + +func WriteUint16ToByte(w io.Writer, data uint16, bigEndian bool) error { + var bb []byte + if bigEndian { + bb = BigEndian.ToUint16(data) + } else { + bb = LittleEndian.ToUint16(data) + } + + _, err := w.Write(bb) + if err != nil { + return err + } + + return nil +} + +func WriteUint24ToByte(w io.Writer, data uint32, bigEndian bool) error { + var bb []byte + if bigEndian { + bb = BigEndian.ToUint24(data) + } else { + bb = LittleEndian.ToUint24(data) + } + + _, err := w.Write(bb) + if err != nil { + return err + } + + return nil +} + +func WriteUint32ToByte(w io.Writer, data uint32, bigEndian bool) error { + var bb []byte + if bigEndian { + bb = BigEndian.ToUint32(data) + } else { + bb = LittleEndian.ToUint32(data) + } + + _, err := w.Write(bb) + if err != nil { + return err + } + + return nil +} + +func WriteUint40ToByte(w io.Writer, data uint64, bigEndian bool) error { + var bb []byte + if bigEndian { + bb = BigEndian.ToUint40(data) + } else { + bb = LittleEndian.ToUint40(data) + } + + _, err := w.Write(bb) + if err != nil { + return err + } + + return nil +} + +func WriteUint48ToByte(w io.Writer, data uint64, bigEndian bool) error { + var bb []byte + if bigEndian { + bb = BigEndian.ToUint48(data) + } else { + bb = LittleEndian.ToUint48(data) + } + + _, err := w.Write(bb) + if err != nil { + return err + } + + return nil +} + +func WriteUint64ToByte(w io.Writer, data uint64, bigEndian bool) error { + var bb []byte + if bigEndian { + bb = BigEndian.ToUint64(data) + } else { + bb = LittleEndian.ToUint64(data) + } + + _, err := w.Write(bb) + if err != nil { + return err + } + + return nil +} + +func GetPtsDts(v uint64) uint64 { + // 4 + 3 + 1 + 15 + 1 + 15 + 1 + // 0011 + // 0010 + PTS[30-32] + marker_bit + PTS[29-15] + marker_bit + PTS[14-0] + marker_bit + pts1 := ((v >> 33) & 0x7) << 30 + pts2 := ((v >> 17) & 0x7fff) << 15 + pts3 := ((v >> 1) & 0x7fff) + + return pts1 | pts2 | pts3 +} + +func PutPtsDts(v uint64) uint64 { + // 4 + 3 + 1 + 15 + 1 + 15 + 1 + // 0011 + // 0010 + PTS[30-32] + marker_bit + PTS[29-15] + marker_bit + PTS[14-0] + marker_bit + // 0x100010001 + // 0001 0000 0000 0000 0001 0000 0000 0000 0001 + // 3个 market_it + pts1 := (v >> 30) & 0x7 << 33 + pts2 := (v >> 15) & 0x7fff << 17 + pts3 := (v & 0x7fff) << 1 + + return pts1 | pts2 | pts3 | 0x100010001 +} + +func GetPCR(v uint64) uint64 { + // program_clock_reference_base(33) + Reserved(6) + program_clock_reference_extension(9) + base := v >> 15 + ext := v & 0x1ff + return base*300 + ext +} + +func PutPCR(pcr uint64) uint64 { + base := pcr / 300 + ext := pcr % 300 + return base<<15 | 0x3f<<9 | ext +} + +func GetFillBytes(data byte, n int) []byte { + b := make([]byte, n) + for i := range b { + b[i] = data + } + + return b +} +func ToFloat64(num interface{}) float64 { + switch v := num.(type) { + case uint: + return float64(v) + case int: + return float64(v) + case uint8: + return float64(v) + case uint16: + return float64(v) + case uint32: + return float64(v) + case uint64: + return float64(v) + case int8: + return float64(v) + case int16: + return float64(v) + case int32: + return float64(v) + case int64: + return float64(v) + case float64: + return v + case float32: + return float64(v) + } + return 0 +} diff --git a/util/crc32.go b/util/crc32.go new file mode 100644 index 0000000..0e2a509 --- /dev/null +++ b/util/crc32.go @@ -0,0 +1,126 @@ +package util + +import ( + "fmt" + "io" + "io/ioutil" +) + +var Crc32_Table = []uint32{ + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, + 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, + 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, + 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, + 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, + 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, + 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, + 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, + 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, + 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, + 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, + 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, + 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, + 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, + 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, + 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, + 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, + 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, + 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, + 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, + 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, + 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, + 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, + 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, + 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, + 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, + 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, + 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, + 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, + 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, + 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, + 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, + 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, + 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, + 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, + 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, + 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, + 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, + 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, + 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, + 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, + 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, + 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D, +} + +type Crc32Reader struct { + R io.Reader + Crc32 uint32 +} + +type Crc32Writer struct { + W io.Writer + Crc32 uint32 +} + +func (cr *Crc32Reader) Read(b []byte) (n int, err error) { + if n, err = cr.R.Read(b); err != nil { + return + } + + cr.Crc32 = getCrc32(cr.Crc32, b) + + return +} + +func (cr *Crc32Reader) ReadCrc32UIntAndCheck() (err error) { + _, err = io.CopyN(ioutil.Discard, cr, 4) + if err != nil { + return err + } + + if cr.Crc32 != 0 { + err = fmt.Errorf("crc32(%x) != 0", cr.Crc32) + return err + } + + return nil +} + +func (wr *Crc32Writer) Write(b []byte) (n int, err error) { + if n, err = wr.W.Write(b); err != nil { + return + } + + wr.Crc32 = getCrc32(wr.Crc32, b) + + return +} + +func getCrc32(crc uint32, data []byte) uint32 { + for _, v := range data { + crc = Crc32_Table[v^byte(crc)] ^ (crc >> 8) + } + + return crc +} diff --git a/util/stderr.go b/util/stderr.go new file mode 100644 index 0000000..31e74ad --- /dev/null +++ b/util/stderr.go @@ -0,0 +1,20 @@ +// +build linux + +package util + +import ( + "log" + "os" + "syscall" +) + +func init() { + logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) + if err != nil { + log.Println("服务启动出错", "打开异常日志文件失败", err) + return + } + + // 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息 + syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd())) +} diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..e439e04 --- /dev/null +++ b/util/util.go @@ -0,0 +1,40 @@ +package util + +import ( + "bufio" + "io" + "os" +) + +// 检查文件或目录是否存在 +// 如果由 filename 指定的文件或目录存在则返回 true,否则返回 false +func Exist(filename string) bool { + _, err := os.Stat(filename) + return err == nil || os.IsExist(err) +} + +func ReadFileLines(filename string) (lines []string, err error) { + file, err := os.OpenFile(filename, os.O_RDONLY, 0644) + if err != nil { + return + } + defer file.Close() + + bio := bufio.NewReader(file) + for { + var line []byte + + line, _, err = bio.ReadLine() + if err != nil { + if err == io.EOF { + file.Close() + return lines, nil + } + return + } + + lines = append(lines, string(line)) + } + + return +} diff --git a/util/vecio.go b/util/vecio.go new file mode 100644 index 0000000..920f308 --- /dev/null +++ b/util/vecio.go @@ -0,0 +1,205 @@ +package util + +import ( + + //"fmt" + "io" + "net" + "os" + "unsafe" +) + +/* +#include +// Structure for scatter/gather I/O. +struct iovec{ + void *iov_base; // Pointer to data. + size_t iov_len; // Length of data. +}; +*/ + +type SysIOVec struct { + Base uintptr + Length uint64 +} + +type IOVec struct { + Data [][]byte + Length int + index int +} + +func (iov *IOVec) Append(b []byte) { + iov.Data = append(iov.Data, b) + iov.Length += len(b) +} + +// Data模型: +// index -> | Data[0][0] | Data[0][1] | Data[0][2] | ... | Data[0][n] | +// | Data[1][0] | Data[1][1] | Data[1][2] | ... | Data[1][n] | +// ...... +// | Data[n][0] | Data[n][1] | Data[n][2] | ... | Data[n][n] | +// +// index是下标 + +func (iov *IOVec) WriteTo(w io.Writer, n int) (written int, err error) { + for n > 0 && iov.Length > 0 { + data := iov.Data[iov.index] + + // 用来存放每次需要写入的数据 + var b []byte + + // 只会读n个字节,超出n个字节,不管 + // 如果iov.Data里面有1000个数据,可是每次只读184个字节,那么剩下的数据(856)重新放回Data + if n > len(data) { + b = data + } else { + b = data[:n] + } + + // n个字节后面的数据 + // 如果这时候n个字节后面已经没有数据了,我们就将下标index往后移一位 + // 否则我们将n个字节后面的数据重新放回Data里. + data = data[len(b):] + if len(data) == 0 { + iov.index++ + } else { + iov.Data[iov.index] = data + } + + n -= len(b) + iov.Length -= len(b) + written += len(b) + + if _, err = w.Write(b); err != nil { + return + } + } + return +} + +type IOVecWriter struct { + fd uintptr + smallBuffer []byte + sysIOV []SysIOVec +} + +func NewIOVecWriter(w io.Writer) (iow *IOVecWriter) { + var err error + var file *os.File + + // TODO:是否要增加其他的类型断言 + switch value := w.(type) { + case *net.TCPConn: + { + file, err = value.File() + if err != nil { + return + } + } + case *os.File: + { + file = value + } + default: + return + } + + iow = &IOVecWriter{ + fd: file.Fd(), + } + + return +} + +// 1 2 3 4 5 6 +// --- -------------- --- --- --- ----------- +// | | | | | | | | | | | | ...... +// --- -------------- --- --- --- ----------- +// +// 1 -> 5个字节, 3 -> 15个字节, 4 -> 10个字节, 5 -> 15个字节 + +// 1,3,4,5内存块太小(小于16个字节),因此我们将它组装起来为samllbuffer +// 并且将Base置于每次组装smallBuffer前总长度的尾部. +// +// samllbuffer: +// 1 3 4 5 ........ +// ------------------------------ +// | | +// ------------------------------ +// <--> 第一个小内存块,假设地址为0xF10000 +// 5 +// <------> 第二个小内存块,假设地址为0xF20000 +// 20 +// <----------> 第三个小内存块,假设地址为0xF30000 +// 30 +// <--------------> 第四个小内存块,假设地址为0xF40000 +// 45 +// +// 开始Base == 每次组装smallBuffer尾部 +// 即: +// Base1 = 0, smallBuffer += 5, +// Base3 = 5, smallBuffer += 15, +// Base4 = 20, smallBuffer += 10, +// Base5 = 30, smallBuffer += 15, +// +// 然后我们将每一块内存块都取出来,比samllBuffer小的内存块,我们就将Base指向内存块的地址 +// 之前小于16个字节的内存块,肯定会比smallBuffer小,因为smallBuffer是所有小内存快的总和. +// 即: +// Base1 = &smallBuffer[0], Base1 = 0xF10000, +// Base3 = &smallBuffer[5], Base3 = 0xF20000, +// Base4 = &smallBuffer[20], Base4 = 0xF30000, +// Base5 = &smallBuffer[30], Base5 = 0xF40000, + +func (iow *IOVecWriter) Write(data []byte) (written int, err error) { + siov := SysIOVec{ + Length: uint64(len(data)), + } + + // unsafe.Pointer == void * + // Base 用整数的形式来记录内存中有几个数据 + // 如果数据小于16,这个时候小块内存的Base还不是数据的内存地址 + if siov.Length < 16 { + // Base 置于上一块samllBuffer的末尾 + // 然后拼接smallBuffer + siov.Base = uintptr(len(iow.smallBuffer)) + iow.smallBuffer = append(iow.smallBuffer, data...) + } else { + siov.Base = uintptr(unsafe.Pointer(&data[0])) + } + + iow.sysIOV = append(iow.sysIOV, siov) + + return written, nil +} + +func (iow *IOVecWriter) Flush() error { + // 取出每一块内存 + for i, _ := range iow.sysIOV { + siov := &iow.sysIOV[i] // 一定要拿地址,如果这里不是取地址,那么无法改变下面Base的值 + if siov.Base < uintptr(len(iow.smallBuffer)) { + // 这个时候小块内存的Base就是数据的内存地址 + siov.Base = uintptr(unsafe.Pointer(&iow.smallBuffer[siov.Base])) + } + } + + N := 1024 + count := len(iow.sysIOV) + // 每次最多取1024个内存块(不管是大内存块,还是小内存块) + for i := 0; i < count; i += N { + n := count - i + if n > N { + n = N + } + + // _, _, errno := syscall.Syscall(syscall.SYS_WRITEV, iow.fd, uintptr(unsafe.Pointer(&iow.sysIOV[i])), uintptr(n)) + // if errno != 0 { + // return errors.New(errno.Error()) + // } + } + + iow.sysIOV = iow.sysIOV[:0] + iow.smallBuffer = iow.smallBuffer[:0] + + return nil +}