diff --git a/.classpath b/.classpath deleted file mode 100644 index d6e5a50..0000000 --- a/.classpath +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e49f4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +build/ +.idea/ +.gradle/ +local.properties +import-summary.txt +gradle/ +gradlew +gradlew.bat diff --git a/.project b/.project deleted file mode 100644 index b9a003a..0000000 --- a/.project +++ /dev/null @@ -1,33 +0,0 @@ - - - SipUA - - - - - - com.android.ide.eclipse.adt.ResourceManagerBuilder - - - - - com.android.ide.eclipse.adt.PreCompilerBuilder - - - - - org.eclipse.jdt.core.javabuilder - - - - - com.android.ide.eclipse.adt.ApkBuilder - - - - - - com.android.ide.eclipse.adt.AndroidNature - org.eclipse.jdt.core.javanature - - diff --git a/ADDITIONAL_TERMS.txt b/ADDITIONAL_TERMS.txt index ff0c142..37c2d3c 100644 --- a/ADDITIONAL_TERMS.txt +++ b/ADDITIONAL_TERMS.txt @@ -1,16 +1,16 @@ -Additional Terms according to GPL, Section 7 -============================================ - -When using the code of Sipdroid Open Source Project you must comply with the following terms: - -1) The main activity (Sipdroid.java) allows to acess a menu. From this menu you can choose the About Box displaying legal notices and author attributions. -This functionality must be left fully intact. There must be no other activity put in front of the main activity on startup. You may not direct the user into another screen (e.g. Preferences), either on first or subsequent startups. -The message of the About box must be left unchanged, too. However, you may add further infos (e.g. Features of your modified version) to the end of the text. - -2) Modified version may not keep the package name "org.sipdroid.sipua". It must be modified, too. - -Expressing these additional terms does not imply that you would otherwise - if they were missing - be allowed to remove the legal notices and author attributions. It is just written down to make totally clear and to avoid any further discussion when a violation occurs. - - - i-p-tel GmbH - July 23, 2009 +Additional Terms according to GPL, Section 7 +============================================ + +When using the code of Sipdroid Open Source Project you must comply with the following terms: + +1) The main activity (Sipdroid.java) allows to acess a menu. From this menu you can choose the About Box displaying legal notices and author attributions. +This functionality must be left fully intact. There must be no other activity put in front of the main activity on startup. You may not direct the user into another screen (e.g. Preferences), either on first or subsequent startups. +The message of the About box must be left unchanged, too. However, you may add further infos (e.g. Features of your modified version) to the end of the text. + +2) Modified version may not keep the package name "org.sipdroid.sipua". It must be modified, too. + +Expressing these additional terms does not imply that you would otherwise - if they were missing - be allowed to remove the legal notices and author attributions. It is just written down to make totally clear and to avoid any further discussion when a violation occurs. + + + i-p-tel GmbH + July 23, 2009 diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index 42eaaec..2dd7925 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -1,107 +1,107 @@ - - Further changes will be logged on http://sipdroid.org (Google Code) - - --- This change log ends here (first public beta) --- - - Author: i-p-tel GmbH - Date: March, 2009 - - The following source files have been added: - - ./sipdroid/media/G711.java - ./sipdroid/sipua/phone/ButtonGridLayout.java - ./sipdroid/sipua/phone/Call.java - ./sipdroid/sipua/phone/CallCard.java - ./sipdroid/sipua/phone/CallerInfo.java - ./sipdroid/sipua/phone/CallerInfoAsyncQuery.java - ./sipdroid/sipua/phone/CallStateException.java - ./sipdroid/sipua/phone/Connection.java - ./sipdroid/sipua/phone/ContactsAsyncHelper.java - ./sipdroid/sipua/phone/Phone.java - ./sipdroid/sipua/phone/PhoneUtils.java - ./sipdroid/sipua/ui/Activity2.java - ./sipdroid/sipua/ui/Caller.java - ./sipdroid/sipua/ui/DTMF.java - ./sipdroid/sipua/ui/OneShotAlarm.java - ./sipdroid/sipua/ui/PSTN.java - ./sipdroid/sipua/ui/Receiver.java - ./sipdroid/sipua/ui/RegisterService.java - ./sipdroid/sipua/ui/Settings.java - ./sipdroid/sipua/ui/SIP.java - ./sipdroid/sipua/ui/SipdroidListener.java - - The following source files have been modified: - - ./sipdroid/media/JAudioLauncher.java - ./sipdroid/media/MediaLauncher.java - ./sipdroid/media/RtpStreamReceiver.java - ./sipdroid/media/RtpStreamSender.java - ./sipdroid/net/RtpSocket.java - ./sipdroid/net/KeepAliveSip.java - ./sipdroid/net/KeepAliveUdp.java - ./sipdroid/sipua/RegisterAgent.java - ./sipdroid/sipua/SipdroidEngine.java - ./sipdroid/sipua/UserAgent.java - ./sipdroid/sipua/UserAgentProfile.java - ./sipdroid/sipua/ui/Sipdroid.java - ./zoolu/call/ExtendedCall.java - ./zoolu/net/UdpProvider.java - ./zoolu/sip/dialog/InviteDialog.java - ./zoolu/sip/dialog/ExtendedInviteDialog.java - ./zoolu/sip/message/BaseMessageFactory.java - ./zoolu/sip/provider/SipProvider.java - - - Author: Hughes Systique Corporation - Date: Nov 17-2008 - - re-ported for Version 1.0r1 - - Author: Hughes Systique Corporation - Date: 30 April, 2008 - - Initial release. - - Author: Hughes Systique Corporation - Date: 2 April, 2008 - - The following files were modified as part of the Porting Process: - - ./zoolu/sdp/AttributeField.java - ./zoolu/sdp/MediaDescriptor.java - ./zoolu/sdp/MediaField.java - ./zoolu/sdp/SdpParser.java - ./zoolu/sdp/SessionDescriptor.java - ./zoolu/sdp/SessionNameField.java - ./zoolu/sip/address/SipURL.java - ./zoolu/sip/call/Call.java - ./zoolu/sip/call/CallListenerAdapter.java - ./zoolu/sip/call/ExtendedCall.java - ./zoolu/sip/call/ExtendedCallListener.java - ./zoolu/sip/call/SdpTools.java - ./zoolu/sip/dialog/Dialog.java - ./zoolu/sip/dialog/DialogInfo.java - ./zoolu/sip/dialog/ExtendedInviteDialog.java - ./zoolu/sip/dialog/NotifierDialog.java - ./zoolu/sip/dialog/NotifierDialogListener.java - ./zoolu/sip/dialog/SubscriberDialog.java - ./zoolu/sip/header/AllowEventsHeader.java - ./zoolu/sip/header/AllowHeader.java - ./zoolu/sip/header/AuthenticationHeader.java - ./zoolu/sip/header/AuthenticationInfoHeader.java - ./zoolu/sip/header/AuthorizationHeader.java - ./zoolu/sip/header/ListHeader.java - ./zoolu/sip/header/MultipleHeader.java - ./zoolu/sip/header/ParametricHeader.java - ./zoolu/sip/message/BaseMessage.java - ./zoolu/sip/message/BaseMessageFactory.java - ./zoolu/sip/message/BaseMessageOtp.java - ./zoolu/sip/provider/SipProvider.java - ./zoolu/tools/Timer.java - - The changes can be summarized as - 1) Remove JMF / RAT specific code from User Agent code - 2) All warnings in the MJSip 1.6 release were fixed - 3) Strongly type checked code - 4) Some bug fixes - + + Further changes will be logged on http://sipdroid.org (Google Code) + + --- This change log ends here (first public beta) --- + + Author: i-p-tel GmbH + Date: March, 2009 + + The following source files have been added: + + ./sipdroid/media/G711.java + ./sipdroid/sipua/phone/ButtonGridLayout.java + ./sipdroid/sipua/phone/Call.java + ./sipdroid/sipua/phone/CallCard.java + ./sipdroid/sipua/phone/CallerInfo.java + ./sipdroid/sipua/phone/CallerInfoAsyncQuery.java + ./sipdroid/sipua/phone/CallStateException.java + ./sipdroid/sipua/phone/Connection.java + ./sipdroid/sipua/phone/ContactsAsyncHelper.java + ./sipdroid/sipua/phone/Phone.java + ./sipdroid/sipua/phone/PhoneUtils.java + ./sipdroid/sipua/ui/Activity2.java + ./sipdroid/sipua/ui/Caller.java + ./sipdroid/sipua/ui/DTMF.java + ./sipdroid/sipua/ui/OneShotAlarm.java + ./sipdroid/sipua/ui/PSTN.java + ./sipdroid/sipua/ui/Receiver.java + ./sipdroid/sipua/ui/RegisterService.java + ./sipdroid/sipua/ui/Settings.java + ./sipdroid/sipua/ui/SIP.java + ./sipdroid/sipua/ui/SipdroidListener.java + + The following source files have been modified: + + ./sipdroid/media/JAudioLauncher.java + ./sipdroid/media/MediaLauncher.java + ./sipdroid/media/RtpStreamReceiver.java + ./sipdroid/media/RtpStreamSender.java + ./sipdroid/net/RtpSocket.java + ./sipdroid/net/KeepAliveSip.java + ./sipdroid/net/KeepAliveUdp.java + ./sipdroid/sipua/RegisterAgent.java + ./sipdroid/sipua/SipdroidEngine.java + ./sipdroid/sipua/UserAgent.java + ./sipdroid/sipua/UserAgentProfile.java + ./sipdroid/sipua/ui/Sipdroid.java + ./zoolu/call/ExtendedCall.java + ./zoolu/net/UdpProvider.java + ./zoolu/sip/dialog/InviteDialog.java + ./zoolu/sip/dialog/ExtendedInviteDialog.java + ./zoolu/sip/message/BaseMessageFactory.java + ./zoolu/sip/provider/SipProvider.java + + + Author: Hughes Systique Corporation + Date: Nov 17-2008 + + re-ported for Version 1.0r1 + + Author: Hughes Systique Corporation + Date: 30 April, 2008 + + Initial release. + + Author: Hughes Systique Corporation + Date: 2 April, 2008 + + The following files were modified as part of the Porting Process: + + ./zoolu/sdp/AttributeField.java + ./zoolu/sdp/MediaDescriptor.java + ./zoolu/sdp/MediaField.java + ./zoolu/sdp/SdpParser.java + ./zoolu/sdp/SessionDescriptor.java + ./zoolu/sdp/SessionNameField.java + ./zoolu/sip/address/SipURL.java + ./zoolu/sip/call/Call.java + ./zoolu/sip/call/CallListenerAdapter.java + ./zoolu/sip/call/ExtendedCall.java + ./zoolu/sip/call/ExtendedCallListener.java + ./zoolu/sip/call/SdpTools.java + ./zoolu/sip/dialog/Dialog.java + ./zoolu/sip/dialog/DialogInfo.java + ./zoolu/sip/dialog/ExtendedInviteDialog.java + ./zoolu/sip/dialog/NotifierDialog.java + ./zoolu/sip/dialog/NotifierDialogListener.java + ./zoolu/sip/dialog/SubscriberDialog.java + ./zoolu/sip/header/AllowEventsHeader.java + ./zoolu/sip/header/AllowHeader.java + ./zoolu/sip/header/AuthenticationHeader.java + ./zoolu/sip/header/AuthenticationInfoHeader.java + ./zoolu/sip/header/AuthorizationHeader.java + ./zoolu/sip/header/ListHeader.java + ./zoolu/sip/header/MultipleHeader.java + ./zoolu/sip/header/ParametricHeader.java + ./zoolu/sip/message/BaseMessage.java + ./zoolu/sip/message/BaseMessageFactory.java + ./zoolu/sip/message/BaseMessageOtp.java + ./zoolu/sip/provider/SipProvider.java + ./zoolu/tools/Timer.java + + The changes can be summarized as + 1) Remove JMF / RAT specific code from User Agent code + 2) All warnings in the MJSip 1.6 release were fixed + 3) Strongly type checked code + 4) Some bug fixes + diff --git a/LICENSE.TXT b/LICENSE.TXT index 3d90694..20d40b6 100644 --- a/LICENSE.TXT +++ b/LICENSE.TXT @@ -1,674 +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 + 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.txt b/README.txt index 6b5a5bc..e39352e 100644 --- a/README.txt +++ b/README.txt @@ -1,53 +1,53 @@ - - Sipdroid is an open-source SIP client for Android - - See http://sipdroid.org for more info - - Copyright (C) 2009 The Sipdroid Open Source Project (http://sipdroid.org) - Copyright (C) 2008 Hughes Systique Corporation, USA (http://hsc.com) - Copyright (C) 2006 The Android Open Source Project (http://android.com) - Copyright (C) 2005 Luca Veltri - University of Parma - Italy (http://mjsip.org) - - This file is part of Sipdroid (http://www.sipdroid.org) - - Sipdroid 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 source code 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 source code; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - - The source code was based on: - - - Mjsip - contributing the original stack, - packages org.zoolu.* and where Copyright noted - - - Hughes Systique Corporation - initial port and development of a Test SIP Client to Google's Android Platform, - packages org.sipdroid.* where Copyright noted - - Then the Sipdroid Open Source project began with porting to an actual phone, - the Android Developer Phone 1, and testing basic calls. Therefore also patches to - the Android Frameworks have been submitted. - - As Copyright noted, mostly modules in org.sipdroid.ui.* have been added to - fully integrate the Client into Android's built-in dialer such as the standard UI - can be used to initiate calls or ring. - - Work has begun to add features like putting calls on-hold, muting, sending DTMF, - and handing-off between networks. - - Welcome to the growing worldwide community of Sipdroid users/developers! - - - i-p-tel GmbH - March, 2009 + + Sipdroid is an open-source SIP client for Android + + See http://sipdroid.org for more info + + Copyright (C) 2009 The Sipdroid Open Source Project (http://sipdroid.org) + Copyright (C) 2008 Hughes Systique Corporation, USA (http://hsc.com) + Copyright (C) 2006 The Android Open Source Project (http://android.com) + Copyright (C) 2005 Luca Veltri - University of Parma - Italy (http://mjsip.org) + + This file is part of Sipdroid (http://www.sipdroid.org) + + Sipdroid 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 source code 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 source code; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + The source code was based on: + + - Mjsip + contributing the original stack, + packages org.zoolu.* and where Copyright noted + + - Hughes Systique Corporation + initial port and development of a Test SIP Client to Google's Android Platform, + packages org.sipdroid.* where Copyright noted + + Then the Sipdroid Open Source project began with porting to an actual phone, + the Android Developer Phone 1, and testing basic calls. Therefore also patches to + the Android Frameworks have been submitted. + + As Copyright noted, mostly modules in org.sipdroid.ui.* have been added to + fully integrate the Client into Android's built-in dialer such as the standard UI + can be used to initiate calls or ring. + + Work has begun to add features like putting calls on-hold, muting, sending DTMF, + and handing-off between networks. + + Welcome to the growing worldwide community of Sipdroid users/developers! + + + i-p-tel GmbH + March, 2009 diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..bf45d4d --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,31 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 33 + buildToolsVersion "33.0.0" + + defaultConfig { + applicationId "org.sipdroid.sipua" + minSdkVersion 16 + targetSdkVersion 31 + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' + + ndk.abiFilters 'x86', 'x86_64', 'arm64-v8a' + } + } + + lintOptions { + checkReleaseBuilds false + } + + externalNativeBuild { + ndkBuild { + path 'src/main/jni/Android.mk' + } + } +} diff --git a/AndroidManifest.xml b/app/src/main/AndroidManifest.xml similarity index 92% rename from AndroidManifest.xml rename to app/src/main/AndroidManifest.xml index b8d427f..21aa63a 100644 --- a/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,29 +25,29 @@ --> - + + android:anyDensity="true" /> + android:exported="true" android:configChanges="orientation|keyboardHidden" android:showWhenLocked="true" android:showOnLockScreen="true"> + android:exported="true" android:icon="@drawable/ic_launcher_phone"> @@ -55,7 +55,7 @@ - + @@ -66,7 +66,7 @@ + android:theme="@android:style/Theme.Dialog" android:exported="true" > @@ -110,12 +110,12 @@ - + - + @@ -127,7 +127,7 @@ - + @@ -139,7 +139,6 @@ - @@ -159,6 +158,8 @@ + + SLIDING: do nothing..."); - - // Or, to have slide hints always hidden while sliding: - //setSlideHints(0, 0); - return; - } - - // Update slide hints based on the current Phone state. - - final boolean hasRingingCall = Receiver.ccCall != null && Receiver.ccCall.getState() == Call.State.INCOMING; - - int slideUpHint = 0; - int slideDownHint = 0; - if (hasRingingCall) { - slideUpHint = R.string.slide_hint_up_to_answer; - } else { - slideDownHint = R.string.slide_hint_down_to_end_call; - } - setSlideHints(slideUpHint, slideDownHint); - } - - /** - * Sets the text of the "slide hints" based on the specified resource - * IDs. (A resource ID of zero means "hide the hint and arrow".) - */ - private void setSlideHints(int upHintResId, int downHintResId) { - // TODO: It would probably look really cool to do a "fade in" animation - // when a hint becomes visible after previously being hidden, rather - // than having it just pop on. - - // TODO: Also consider having both slide hints *always* visible, - // so that as you slide you first cover up one and later reveal - // the other. But this is tricky: the text of the hint that - // "starts off hidden" will need to be pre-set to the value it - // should have *after* the slide is complete. - - if (DBG) { - String upHint = - (upHintResId != 0) ? mInCallScreen.getString(upHintResId) : ""; - String downHint = - (downHintResId != 0) ? mInCallScreen.getString(downHintResId) : ""; - log("setSlideHints: UP '" + upHint + "', DOWN '" + downHint + "'"); - } - - mSlideUp.setVisibility((upHintResId != 0) ? View.VISIBLE : View.GONE); - if (upHintResId != 0) mSlideUpHint.setText(upHintResId); - - mSlideDown.setVisibility((downHintResId != 0) ? View.VISIBLE : View.GONE); - if (downHintResId != 0) mSlideDownHint.setText(downHintResId); - } - - // - // Sliding state management - // - - /** - * Handles a touch event on the CallCard. - * @see CallCard.dispatchTouchEvent - */ - /* package */ void handleCallCardTouchEvent(MotionEvent ev) { - // if (DBG) log("handleCallCardTouchEvent(" + ev + ")..."); - - if (mInCallScreen == null || mInCallScreen.isFinishing()) { - Log.i(LOG_TAG, "handleCallCardTouchEvent: InCallScreen gone; ignoring touch..."); - return; - } - - final int action = ev.getAction(); - - // All the sliding code depends on deltas, so it - // doesn't really matter in what coordinate space - // we are, as long as it's independant of our position - final int xAbsolute = (int) ev.getRawX(); - final int yAbsolute = (int) ev.getRawY(); - - if (isSlideInProgress()) { - long now = SystemClock.elapsedRealtime(); - if (now-mTouchDownTime > 1000 || InCallScreen.pactive || - now-InCallScreen.pactivetime < 1000) - abortSlide(); - else - switch (action) { - case MotionEvent.ACTION_DOWN: - // Shouldn't happen in this state. - break; - case MotionEvent.ACTION_MOVE: - // Move the CallCard! - updateWhileSliding(yAbsolute); - break; - case MotionEvent.ACTION_UP: - // See if we've slid far enough to do some action - // (ie. hang up, or answer an incoming call, - // depending on our current state.) - stopSliding(yAbsolute); - break; - case MotionEvent.ACTION_CANCEL: - // Because we set the FLAG_IGNORE_CHEEK_PRESSES - // WindowManager flag (see init()), we'll get an - // ACTION_CANCEL event if a valid ACTION_DOWN is later - // followed by an ACTION_MOVE that's a "fat touch". - // In this case, abort the slide. - if (DBG) log("handleCallCardTouchEvent: ACTION_CANCEL: " + ev); - abortSlide(); - break; - } - } else { - switch (action) { - case MotionEvent.ACTION_DOWN: - // This event is a touch DOWN on the card: start sliding! - startSliding(xAbsolute, yAbsolute); - break; - case MotionEvent.ACTION_MOVE: - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - // If we're not sliding (yet), ignore anything other than - // ACTION_DOWN. - break; - } - } - } - - long mTouchDownTime; - - /** - * Start sliding the card. - * Called when we get a DOWN touch event on the slideable CallCard. - * x and y are the location of the DOWN event in absolute screen coordinates. - */ - /* package */ void startSliding(int x, int y) { - if (DBG) log("startSliding(" + x + ", " + y + ")..."); - - if (mCallEndedState) { - if (DBG) log("startSliding: CALL ENDED state; ignoring..."); - return; - } - - mSlideInProgress = true; - mTouchDownY = y; - mTouchDownTime = SystemClock.elapsedRealtime(); - } - - /** - * Handle a MOVE event while sliding. - * @param y the y-position of the MOVE event in absolute screen coordinates. - */ - /* package */ void updateWhileSliding(int y) { - int totalSlideAmount = y - mTouchDownY; - // if (DBG) log("--------------> MOTION! y = " + y - // + " (total slide = " + totalSlideAmount + ")"); - - // TODO: consider caching popupTopPosY and popupBottomPosY in - // startSliding (to save a couple of method calls each time here.) - // TODO: this block is duplicated above; use a single helper method instead. - mMainFrame.getLocationInWindow(mTempLocation); - final int mainFrameX = mTempLocation[0]; - final int mainFrameY = 0; //mTempLocation[1]; - - // In the "top" position the CallCard is aligned exactly with the - // top edge of the main frame. - final int popupTopPosY = mainFrameY; - - // And in the "bottom" position, the bottom of the CallCard is - // aligned exactly with the bottom of the main frame. - final int popupBottomPosY = mainFrameY + mMainFrame.getHeight() - height; - - // Forcibly reposition the CallCard - int newCardTop = mCardPreferredY + totalSlideAmount; - // if (DBG) log(" ==> New card top: " + newCardTop); - - // But never slide *beyond* the topmost or bottom-most position: - if (newCardTop < popupTopPosY) newCardTop = popupTopPosY; - else if (newCardTop > popupBottomPosY) newCardTop = popupBottomPosY; - - // Forcibly reposition the PopupWindow. - mCallCard.update(mCardPreferredX, newCardTop, -1, -1); - } - - /** - * Stop sliding the card. - * Called when we get an UP touch event while sliding. - */ - /* package */ void stopSliding(int y) { - int totalSlideAmount = y - mTouchDownY; - if (DBG) log("stopSliding: Total slide delta: " + totalSlideAmount); - - // When not sliding, the card is pegged to either the very top - // or very bottom of the in-call main frame. - - // So the precise slide amount that *should* be required to "complete - // the slide" is the amount of vertical space in the in-call frame NOT - // taken up by the CallCard: - - int slideDistanceRequired = mMainFrame.getHeight() - height; - // if (DBG) log(" -> main frame height: " + mMainFrame.getHeight()); - // if (DBG) log(" -> PopupWindow height: " + mPopup.getHeight()); - // if (DBG) log(" -> DIFFERENCE = " + slideDistanceRequired); - - // But fudge slideDistanceRequired a little, in case you slid - // "just far enough" but the card jittered a bit when you let go. - slideDistanceRequired -= 30; - - // Modify totalSlideAmount so that a positive value means "a slide - // in the correct direction". (Thus if the card started at the - // bottom of the screen, meaning that the user needs to slide - // *up*, we need to flip the sign of totalSlideAmount.) - if (!mCardAtTop) totalSlideAmount = -totalSlideAmount; - - if (totalSlideAmount >= slideDistanceRequired) { - if (DBG) log("==> Slide was far enough! slid " - + totalSlideAmount + ", needed >= " + slideDistanceRequired); - finishSuccessfulSlide(); - } else { - if (DBG) log("==> Didn't slide enough to take action: slid " - + totalSlideAmount + ", needed >= " + slideDistanceRequired); - abortSlide(); - } - } - - /** - * The user successfully completed a "slide" operation. - * Activate whatever action the slide was supposed to trigger. - * - * (That could either be (1) hang up the ongoing call(s), or (2) - * answer an incoming call.) - * - * This method is responsible for triggering any screen updates that need - * to happen, based on any internal state changes due to the slide. - */ - private void finishSuccessfulSlide() { - if (DBG) log("finishSuccessfulSlide()..."); - - mSlideInProgress = false; - - // TODO: Need to test lots of possible edge cases here, like if the - // state of the Phone changes while the slide was happening. - // (For example, say the user slid the card UP to answer an incoming - // call, but the phone's no longer ringing by the time we got here...) - - // TODO: The state-checking logic here is very similar to the logic in - // updateCardSlideHints(). Rather than duplicating this logic in both - // places, maybe use a single helper function that generates a - // complete "slidability matrix" (ie. all slide hints / states / - // actions) based on the current state of the Phone. - - boolean phoneStateAboutToChange = false; - - // Perform the "successful slide" action. - - if (mCardAtTop) { - // The downward slide action is to hang up any ongoing - // call(s). - if (DBG) log(" =========> Slide complete: HANGING UP..."); - mInCallScreen.reject(); - - // Any "hangup" action is going to cause - // the Phone state to change imminently. - phoneStateAboutToChange = true; - } else { - // The upward slide action is to answer the incoming call. - // (We put the ongoing call on hold if there's already one line in - // use, or hang up the ongoing call if both lines are in use.) - if (DBG) log(" =========> Slide complete: ANSWERING..."); - mInCallScreen.answer(); - - // Either of the "answer call" functions is going to cause - // the Phone state to change imminently. - phoneStateAboutToChange = true; - } - - // Finally, update the state of the UI depending on what just happened. - // Update the "permanent" position of the sliding card, and the slide - // hints. - // - // But *don't* do this if we know the Phone state's about to change, - // like if the user just did a "slide up to answer". In that case - // we know we're going to get a onPhoneStateChanged() call in a few - // milliseconds, and *that's* going to result in an updateScreen() call. - // (And if we were to do that update now, we'd just get a brief flash - // of the card at the bottom or the screen. So don't do anything - // here.) - - if (!phoneStateAboutToChange) { - updateCardPreferredPosition(); - updateCardSlideHints(); - - // And force an immmediate re-layout. (No need to do any - // animation here, since the card's new "preferred position" is - // exactly where the user just slid it.) - mMainFrame.requestLayout(); - } - } - - /** - * Bail out of an in-progress slide *without* activating whatever - * action the slide was supposed to trigger. - */ - private void abortSlide() { - if (DBG) log("abortSlide()..."); - - mSlideInProgress = false; - - // This slide had no effect. Nothing about the state of the - // UI has changed, so no need to updateCardPreferredPosition() or - // updateCardSlideHints(). But we *do* need to reposition the - // PopupWindow back in its correct position. - - // TODO: smoothly animate back to the preferred position. - mCallCard.update(mCardPreferredX, mCardPreferredY, -1, -1); - } - - /* package */ public boolean isSlideInProgress() { - return mSlideInProgress; - } - - private void log(String msg) { - Log.d(LOG_TAG, "[" + this + "] " + msg); - } - - boolean first = true; - - public void onGlobalLayout() { - if (DBG) log("onGlobalLayout()..."); - if (first) { - first = false; - showPopup(); - } - } - - /* package */ public static class WindowAttachNotifierView extends View { - private SlidingCardManager mSlidingCardManager; - - public WindowAttachNotifierView(Context c) { - super(c); - } - - public void setSlidingCardManager(SlidingCardManager slidingCardManager) { - mSlidingCardManager = slidingCardManager; - } - - @Override - protected void onAttachedToWindow() { - // This is called when the view is attached to a window. - // At this point it has a Surface and will start drawing. - if (DBG) mSlidingCardManager.log("WindowAttachNotifierView: onAttachedToWindow!"); - super.onAttachedToWindow(); - - // The code in showPopup() needs to know the sizes and - // positions of some views in the mMainFrame view hierarchy, - // in order to set the popup window's size and position. That - // means that showPopup() needs to be called *after* the whole - // in-call UI has been measured and laid out. At this point - // that hasn't happened yet, so we can't directly call - // mSlidingCardManager.showPopup() from here. - // - // Also, to reduce flicker onscreen, we'd like the PopupWindow - // to appear *before* any of the main view hierarchy becomes - // visible. So we use the main view hierarchy's - // ViewTreeObserver to get notified *after* the layout - // happens, but before anything gets drawn. - - // Get the ViewTreeObserver for the main InCallScreen view - // hierarchy. (You can only call getViewTreeObserver() after - // the view tree gets attached to a Window, which is why we do - // this here rather than in InCallScreen.onCreate().) - final ViewTreeObserver viewTreeObserver = getViewTreeObserver(); - // Arrange for the SlidingCardManager to get called after - // the main view tree has been laid out. - // (addOnPreDrawListener() would also be basically equivalent here.) - viewTreeObserver.addOnGlobalLayoutListener(mSlidingCardManager); - - // See SlidingCardManager.onGlobalLayout() for the next step. - } - - @Override - protected void onDetachedFromWindow() { - // This is called when the view is detached from a window. - // At this point it no longer has a surface for drawing. - if (DBG) mSlidingCardManager.log("WindowAttachNotifierView: onDetachedFromWindow!"); - super.onDetachedFromWindow(); - - // Nothing necessary here (yet) since we already - // dismiss the popup from onDestroy(). - } - } - -} +package org.sipdroid.sipua.phone; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * Copyright (C) 2006 The Android Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import org.sipdroid.sipua.R; +import org.sipdroid.sipua.ui.InCallScreen; +import org.sipdroid.sipua.ui.Receiver; + +import android.content.Context; +import android.os.SystemClock; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.widget.RelativeLayout; +import android.widget.TextView; +import android.view.ViewTreeObserver; + +/** + * Helper class to manage the sliding "call card" on the InCallScreen. + */ +public class SlidingCardManager implements ViewTreeObserver.OnGlobalLayoutListener { + private static final String LOG_TAG = "PHONE/SlidingCardManager"; + private static final boolean DBG = false; + + // + // In the new "simplified" sliding card UI, the card is always in + // one of the following states: + // + // CARD AT TOP OF SCREEN: + // - "In call" states + // (including "Dialing", "On hold", "Conference call", "Busy") + // + // CARD AT BOTTOM OF SCREEN: + // - Incoming call (either normal *or* "Call waiting") + // - "Call ended" state: + // - This is a non-interactive state. All touch events are ignored, + // and the card never moves.) + // - This state is only ever used while the Phone is totally idle; + // it's visible for a couple of seconds after a call ends + // (see InCallScreen.delayedCleanupAfterDisconnect().) + // + // Sliding the card UP *always* answers the incoming call. + // (We put the ongoing call on hold if there's already one line in + // use, or hang up the ongoing call if both lines are in use.) + // + // Sliding the card DOWN *always* ends all current calls. + // + // There's no longer any concept of "locking" or "unlocking" the + // touchscreen by sliding the card. + // + + /** + * Reference to the InCallScreen activity that owns us. This will be + * null if we haven't been initialized yet *or* after the InCallScreen + * activity has been destroyed. + */ + private InCallScreen mInCallScreen; + + private Phone mPhone; + + // Touch mode states, and state of the sliding card + private boolean mSlideInProgress = false; + private int mTouchDownY; // Y-coordinate of the DOWN event that started the slide + + // mCardAtTop is true if the card should currently be at the top + // of the screen, or false if it should be at the bottom. + private boolean mCardAtTop; + private boolean mCallEndedState; + private int mCardPreferredX, mCardPreferredY; + + // UI elements: + + private CallCard mCallCard; + + // Slide hints + private ViewGroup mSlideUp; + private TextView mSlideUpHint; + private ViewGroup mSlideDown; + private TextView mSlideDownHint; + + // Some UI elements from the main InCallScreen that we use. + private ViewGroup mMainFrame; + + // Temporary int array used with various getLocationInWindow() calls. + private int[] mTempLocation = new int[2]; // Use this only from the main thread! + + // + // Slide hints (portrait mode values come directly from incall_screen.xml): + static final int SLIDE_UP_HINT_TOP_LANDSCAPE = 88; + static final int SLIDE_DOWN_HINT_TOP_LANDSCAPE = 160; + + public SlidingCardManager() { + } + + /** + * Initializes the internal state of the SlidingCardManager. + * + * @param phone the Phone app's Phone instance + * @param inCallScreen the InCallScreen activity + * @param mainFrame the InCallScreen's main frame containing the in-call UI elements + */ + /* package */ public void init(Phone phone, + InCallScreen inCallScreen, + ViewGroup mainFrame) { + if (DBG) log("init()..."); + mPhone = phone; + mInCallScreen = inCallScreen; + mMainFrame = mainFrame; + + // Slide hints + mSlideUp = (ViewGroup) mInCallScreen.findViewById(R.id.slideUp); + mSlideUpHint = (TextView) mInCallScreen.findViewById(R.id.slideUpHint); + mSlideDown = (ViewGroup) mInCallScreen.findViewById(R.id.slideDown); + mSlideDownHint = (TextView) mInCallScreen.findViewById(R.id.slideDownHint); + + mCallCard = (CallCard) mMainFrame.findViewById(R.id.callCard); + mCallCard.setSlidingCardManager(this); + } + + /* package */ void setPhone(Phone phone) { + mPhone = phone; + } + + /** + * Null out our reference to the InCallScreen activity. + * This indicates that the InCallScreen activity has been destroyed. + */ + void clearInCallScreenReference() { + mInCallScreen = null; + } + + /** + * Updates the PopupWindow's size and makes it visible onscreen. + * + * This needs to be done *after* our main UI gets laid out and + * attached to its Window, because (1) we base the popup's size on the + * post-layout size of elements in our main View hierarchy, and (2) we + * need to have a valid window token for our call to + * mPopup.showAtLocation(). + */ + public /* package */ void showPopup() { + if (DBG) log("showPopup()..."); + updateCardPreferredPosition(); // Sets mCardAtTop, mCallEndedState, + // mCardPreferredX, and mCardPreferredY + updateCardSlideHints(); + + } + + int height; + + /** + * Update the "permanent" position (ie. the actual layout params) + * of the sliding card based on the current call state. + * + * This method sets mCardAtTop, mCallEndedState, mCardPreferredX, and mCardPreferredY. + * It also repositions the PopupWindow if it's showing. + * + * Note that *while sliding* we manually reposition the card + * on every motion event that comes in. The x/y position we set here + * determines where the card should be while *not* sliding. + * + * TODO: If the card position changes for some reason *other* + * than user action (i.e. as a result of an onPhoneStateChanged() + * callback), we should smoothly animate the position change! + * (For example, if you're in a call and the other end hangs up, the + * card should switch to "Call ended" mode and smoothly animate to the + * bottom position.) + */ + public /* package */ void updateCardPreferredPosition() { + if (DBG) log("updateCardPreferredPosition()..."); + //if (DBG) log("- card's LayoutParams: " + mCallCard.getLayoutParams()); + + // Bail out if our View hierarchy isn't attached to a Window yet + // (since the mMainFrame.getLocationOnScreen() call below + // will fail.) + if (mMainFrame.getWindowToken() == null) { + if (DBG) log("updateCardPreferredPosition: View hierarchy unattached; bailing..."); + return; + } + + /* + if (mMainFrame.getHeight() == 0) { + // The code here needs to know the sizes and positions of some + // views in the mMainFrame view hierarchy, so you're only + // allowed to call this method *after* the whole in-call UI + // has been measured and laid out. + // (This is why we defer calling showPopup() until an + // onGlobalLayout() call comes in.) + throw new IllegalStateException( + "updateCardPreferredPosition: main frame not measured yet"); + } + */ + + // Given the current state of the Phone and the UI, decide whether + // the card should be at the TOP or BOTTOM of the screen right now. + + // Compute the possible coordinates onscreen for the popup. + // TODO: this block is duplicated below; use a single helper method instead. + mMainFrame.getLocationInWindow(mTempLocation); + final int mainFrameX = mTempLocation[0]; + final int mainFrameY = 0; //mTempLocation[1]; + if (DBG) log("- mMainFrame loc in window: " + mainFrameX + ", " + mainFrameY); + + // In the "top" position the CallCard is aligned exactly with the + // top edge of the main frame. + final int popupTopPosY = mainFrameY; + + // And in the "bottom" position, the bottom of the CallCard is + // aligned exactly with the bottom of the main frame. + if (height == 0) { + height = mCallCard.getHeight(); + if (height == 0) return; + // Reposition the "slide hints". + RelativeLayout.LayoutParams lp = + (RelativeLayout.LayoutParams) mSlideUp.getLayoutParams(); + // Equivalent to setting android:layout_marginTop in XML + lp.bottomMargin = height; + mSlideUp.setLayoutParams(lp); + lp = (RelativeLayout.LayoutParams) mSlideDown.getLayoutParams(); + // Equivalent to setting android:layout_marginTop in XML + lp.topMargin = height; + mSlideDown.setLayoutParams(lp); + height += 10; + } + final int popupBottomPosY = mainFrameY + mMainFrame.getHeight() - height; + + if (Receiver.ccCall != null && Receiver.ccCall.getState() != Call.State.DISCONNECTED) { + // When the phone is in use, the position of the card is + // determined solely by whether an incoming call is ringing or + // not. + final boolean hasRingingCall = Receiver.ccCall.getState() == Call.State.INCOMING; + mCardAtTop = !hasRingingCall; + mCallEndedState = false; + } else { + // Phone is completely idle! Display the CALL ENDED state + // with the card at the bottom of the screen. + mCardAtTop = false; + mCallEndedState = true; + } + mCardPreferredX = mainFrameX; + mCardPreferredY = mCardAtTop ? popupTopPosY : popupBottomPosY; + + if (DBG) log("==> Setting card preferred position (mCardAtTop = " + + mCardAtTop + ") to: " + + mCardPreferredX + ", " + mCardPreferredY); + + // This is a no-op if the PopupWindow isn't showing. + mCallCard.update(mCardPreferredX, mCardPreferredY, -1, -1); + } + + + // + // "Slide hints" management + // + + /** + * Update the "slide hints" (displayed onscreen either above or below + * the slidable CallCard) based on the current state. + */ + public /* package */ void updateCardSlideHints() { + if (DBG) log("updateCardSlideHints()..."); + + if (mSlideInProgress) { + // If currently sliding, do nothing. (Leave the hints in whatever + // state they were before we started the slide.) + if (DBG) log("--> SLIDING: do nothing..."); + + // Or, to have slide hints always hidden while sliding: + //setSlideHints(0, 0); + return; + } + + // Update slide hints based on the current Phone state. + + final boolean hasRingingCall = Receiver.ccCall != null && Receiver.ccCall.getState() == Call.State.INCOMING; + + int slideUpHint = 0; + int slideDownHint = 0; + if (hasRingingCall) { + slideUpHint = R.string.slide_hint_up_to_answer; + } else { + slideDownHint = R.string.slide_hint_down_to_end_call; + } + setSlideHints(slideUpHint, slideDownHint); + } + + /** + * Sets the text of the "slide hints" based on the specified resource + * IDs. (A resource ID of zero means "hide the hint and arrow".) + */ + private void setSlideHints(int upHintResId, int downHintResId) { + // TODO: It would probably look really cool to do a "fade in" animation + // when a hint becomes visible after previously being hidden, rather + // than having it just pop on. + + // TODO: Also consider having both slide hints *always* visible, + // so that as you slide you first cover up one and later reveal + // the other. But this is tricky: the text of the hint that + // "starts off hidden" will need to be pre-set to the value it + // should have *after* the slide is complete. + + if (DBG) { + String upHint = + (upHintResId != 0) ? mInCallScreen.getString(upHintResId) : ""; + String downHint = + (downHintResId != 0) ? mInCallScreen.getString(downHintResId) : ""; + log("setSlideHints: UP '" + upHint + "', DOWN '" + downHint + "'"); + } + + mSlideUp.setVisibility((upHintResId != 0) ? View.VISIBLE : View.GONE); + if (upHintResId != 0) mSlideUpHint.setText(upHintResId); + + mSlideDown.setVisibility((downHintResId != 0) ? View.VISIBLE : View.GONE); + if (downHintResId != 0) mSlideDownHint.setText(downHintResId); + } + + // + // Sliding state management + // + + /** + * Handles a touch event on the CallCard. + * @see CallCard.dispatchTouchEvent + */ + /* package */ void handleCallCardTouchEvent(MotionEvent ev) { + // if (DBG) log("handleCallCardTouchEvent(" + ev + ")..."); + + if (mInCallScreen == null || mInCallScreen.isFinishing()) { + Log.i(LOG_TAG, "handleCallCardTouchEvent: InCallScreen gone; ignoring touch..."); + return; + } + + final int action = ev.getAction(); + + // All the sliding code depends on deltas, so it + // doesn't really matter in what coordinate space + // we are, as long as it's independant of our position + final int xAbsolute = (int) ev.getRawX(); + final int yAbsolute = (int) ev.getRawY(); + + if (isSlideInProgress()) { + long now = SystemClock.elapsedRealtime(); + if (now-mTouchDownTime > 1000 || InCallScreen.pactive || + now-InCallScreen.pactivetime < 1000) + abortSlide(); + else + switch (action) { + case MotionEvent.ACTION_DOWN: + // Shouldn't happen in this state. + break; + case MotionEvent.ACTION_MOVE: + // Move the CallCard! + updateWhileSliding(yAbsolute); + break; + case MotionEvent.ACTION_UP: + // See if we've slid far enough to do some action + // (ie. hang up, or answer an incoming call, + // depending on our current state.) + stopSliding(yAbsolute); + break; + case MotionEvent.ACTION_CANCEL: + // Because we set the FLAG_IGNORE_CHEEK_PRESSES + // WindowManager flag (see init()), we'll get an + // ACTION_CANCEL event if a valid ACTION_DOWN is later + // followed by an ACTION_MOVE that's a "fat touch". + // In this case, abort the slide. + if (DBG) log("handleCallCardTouchEvent: ACTION_CANCEL: " + ev); + abortSlide(); + break; + } + } else { + switch (action) { + case MotionEvent.ACTION_DOWN: + // This event is a touch DOWN on the card: start sliding! + startSliding(xAbsolute, yAbsolute); + break; + case MotionEvent.ACTION_MOVE: + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + // If we're not sliding (yet), ignore anything other than + // ACTION_DOWN. + break; + } + } + } + + long mTouchDownTime; + + /** + * Start sliding the card. + * Called when we get a DOWN touch event on the slideable CallCard. + * x and y are the location of the DOWN event in absolute screen coordinates. + */ + /* package */ void startSliding(int x, int y) { + if (DBG) log("startSliding(" + x + ", " + y + ")..."); + + if (mCallEndedState) { + if (DBG) log("startSliding: CALL ENDED state; ignoring..."); + return; + } + + mSlideInProgress = true; + mTouchDownY = y; + mTouchDownTime = SystemClock.elapsedRealtime(); + } + + /** + * Handle a MOVE event while sliding. + * @param y the y-position of the MOVE event in absolute screen coordinates. + */ + /* package */ void updateWhileSliding(int y) { + int totalSlideAmount = y - mTouchDownY; + // if (DBG) log("--------------> MOTION! y = " + y + // + " (total slide = " + totalSlideAmount + ")"); + + // TODO: consider caching popupTopPosY and popupBottomPosY in + // startSliding (to save a couple of method calls each time here.) + // TODO: this block is duplicated above; use a single helper method instead. + mMainFrame.getLocationInWindow(mTempLocation); + final int mainFrameX = mTempLocation[0]; + final int mainFrameY = 0; //mTempLocation[1]; + + // In the "top" position the CallCard is aligned exactly with the + // top edge of the main frame. + final int popupTopPosY = mainFrameY; + + // And in the "bottom" position, the bottom of the CallCard is + // aligned exactly with the bottom of the main frame. + final int popupBottomPosY = mainFrameY + mMainFrame.getHeight() - height; + + // Forcibly reposition the CallCard + int newCardTop = mCardPreferredY + totalSlideAmount; + // if (DBG) log(" ==> New card top: " + newCardTop); + + // But never slide *beyond* the topmost or bottom-most position: + if (newCardTop < popupTopPosY) newCardTop = popupTopPosY; + else if (newCardTop > popupBottomPosY) newCardTop = popupBottomPosY; + + // Forcibly reposition the PopupWindow. + mCallCard.update(mCardPreferredX, newCardTop, -1, -1); + } + + /** + * Stop sliding the card. + * Called when we get an UP touch event while sliding. + */ + /* package */ void stopSliding(int y) { + int totalSlideAmount = y - mTouchDownY; + if (DBG) log("stopSliding: Total slide delta: " + totalSlideAmount); + + // When not sliding, the card is pegged to either the very top + // or very bottom of the in-call main frame. + + // So the precise slide amount that *should* be required to "complete + // the slide" is the amount of vertical space in the in-call frame NOT + // taken up by the CallCard: + + int slideDistanceRequired = mMainFrame.getHeight() - height; + // if (DBG) log(" -> main frame height: " + mMainFrame.getHeight()); + // if (DBG) log(" -> PopupWindow height: " + mPopup.getHeight()); + // if (DBG) log(" -> DIFFERENCE = " + slideDistanceRequired); + + // But fudge slideDistanceRequired a little, in case you slid + // "just far enough" but the card jittered a bit when you let go. + slideDistanceRequired -= 30; + + // Modify totalSlideAmount so that a positive value means "a slide + // in the correct direction". (Thus if the card started at the + // bottom of the screen, meaning that the user needs to slide + // *up*, we need to flip the sign of totalSlideAmount.) + if (!mCardAtTop) totalSlideAmount = -totalSlideAmount; + + if (totalSlideAmount >= slideDistanceRequired) { + if (DBG) log("==> Slide was far enough! slid " + + totalSlideAmount + ", needed >= " + slideDistanceRequired); + finishSuccessfulSlide(); + } else { + if (DBG) log("==> Didn't slide enough to take action: slid " + + totalSlideAmount + ", needed >= " + slideDistanceRequired); + abortSlide(); + } + } + + /** + * The user successfully completed a "slide" operation. + * Activate whatever action the slide was supposed to trigger. + * + * (That could either be (1) hang up the ongoing call(s), or (2) + * answer an incoming call.) + * + * This method is responsible for triggering any screen updates that need + * to happen, based on any internal state changes due to the slide. + */ + private void finishSuccessfulSlide() { + if (DBG) log("finishSuccessfulSlide()..."); + + mSlideInProgress = false; + + // TODO: Need to test lots of possible edge cases here, like if the + // state of the Phone changes while the slide was happening. + // (For example, say the user slid the card UP to answer an incoming + // call, but the phone's no longer ringing by the time we got here...) + + // TODO: The state-checking logic here is very similar to the logic in + // updateCardSlideHints(). Rather than duplicating this logic in both + // places, maybe use a single helper function that generates a + // complete "slidability matrix" (ie. all slide hints / states / + // actions) based on the current state of the Phone. + + boolean phoneStateAboutToChange = false; + + // Perform the "successful slide" action. + + if (mCardAtTop) { + // The downward slide action is to hang up any ongoing + // call(s). + if (DBG) log(" =========> Slide complete: HANGING UP..."); + mInCallScreen.reject(); + + // Any "hangup" action is going to cause + // the Phone state to change imminently. + phoneStateAboutToChange = true; + } else { + // The upward slide action is to answer the incoming call. + // (We put the ongoing call on hold if there's already one line in + // use, or hang up the ongoing call if both lines are in use.) + if (DBG) log(" =========> Slide complete: ANSWERING..."); + mInCallScreen.answer(); + + // Either of the "answer call" functions is going to cause + // the Phone state to change imminently. + phoneStateAboutToChange = true; + } + + // Finally, update the state of the UI depending on what just happened. + // Update the "permanent" position of the sliding card, and the slide + // hints. + // + // But *don't* do this if we know the Phone state's about to change, + // like if the user just did a "slide up to answer". In that case + // we know we're going to get a onPhoneStateChanged() call in a few + // milliseconds, and *that's* going to result in an updateScreen() call. + // (And if we were to do that update now, we'd just get a brief flash + // of the card at the bottom or the screen. So don't do anything + // here.) + + if (!phoneStateAboutToChange) { + updateCardPreferredPosition(); + updateCardSlideHints(); + + // And force an immmediate re-layout. (No need to do any + // animation here, since the card's new "preferred position" is + // exactly where the user just slid it.) + mMainFrame.requestLayout(); + } + } + + /** + * Bail out of an in-progress slide *without* activating whatever + * action the slide was supposed to trigger. + */ + private void abortSlide() { + if (DBG) log("abortSlide()..."); + + mSlideInProgress = false; + + // This slide had no effect. Nothing about the state of the + // UI has changed, so no need to updateCardPreferredPosition() or + // updateCardSlideHints(). But we *do* need to reposition the + // PopupWindow back in its correct position. + + // TODO: smoothly animate back to the preferred position. + mCallCard.update(mCardPreferredX, mCardPreferredY, -1, -1); + } + + /* package */ public boolean isSlideInProgress() { + return mSlideInProgress; + } + + private void log(String msg) { + Log.d(LOG_TAG, "[" + this + "] " + msg); + } + + boolean first = true; + + public void onGlobalLayout() { + if (DBG) log("onGlobalLayout()..."); + if (first) { + first = false; + showPopup(); + } + } + + /* package */ public static class WindowAttachNotifierView extends View { + private SlidingCardManager mSlidingCardManager; + + public WindowAttachNotifierView(Context c) { + super(c); + } + + public void setSlidingCardManager(SlidingCardManager slidingCardManager) { + mSlidingCardManager = slidingCardManager; + } + + @Override + protected void onAttachedToWindow() { + // This is called when the view is attached to a window. + // At this point it has a Surface and will start drawing. + if (DBG) mSlidingCardManager.log("WindowAttachNotifierView: onAttachedToWindow!"); + super.onAttachedToWindow(); + + // The code in showPopup() needs to know the sizes and + // positions of some views in the mMainFrame view hierarchy, + // in order to set the popup window's size and position. That + // means that showPopup() needs to be called *after* the whole + // in-call UI has been measured and laid out. At this point + // that hasn't happened yet, so we can't directly call + // mSlidingCardManager.showPopup() from here. + // + // Also, to reduce flicker onscreen, we'd like the PopupWindow + // to appear *before* any of the main view hierarchy becomes + // visible. So we use the main view hierarchy's + // ViewTreeObserver to get notified *after* the layout + // happens, but before anything gets drawn. + + // Get the ViewTreeObserver for the main InCallScreen view + // hierarchy. (You can only call getViewTreeObserver() after + // the view tree gets attached to a Window, which is why we do + // this here rather than in InCallScreen.onCreate().) + final ViewTreeObserver viewTreeObserver = getViewTreeObserver(); + // Arrange for the SlidingCardManager to get called after + // the main view tree has been laid out. + // (addOnPreDrawListener() would also be basically equivalent here.) + viewTreeObserver.addOnGlobalLayoutListener(mSlidingCardManager); + + // See SlidingCardManager.onGlobalLayout() for the next step. + } + + @Override + protected void onDetachedFromWindow() { + // This is called when the view is detached from a window. + // At this point it no longer has a surface for drawing. + if (DBG) mSlidingCardManager.log("WindowAttachNotifierView: onDetachedFromWindow!"); + super.onDetachedFromWindow(); + + // Nothing necessary here (yet) since we already + // dismiss the popup from onDestroy(). + } + } + +} diff --git a/src/org/sipdroid/sipua/ui/Activity2.java b/app/src/main/java/org/sipdroid/sipua/ui/Activity2.java similarity index 97% rename from src/org/sipdroid/sipua/ui/Activity2.java rename to app/src/main/java/org/sipdroid/sipua/ui/Activity2.java index d6ba91e..6a49c15 100644 --- a/src/org/sipdroid/sipua/ui/Activity2.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/Activity2.java @@ -1,36 +1,36 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; - -public class Activity2 extends Activity { - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Intent startActivity = new Intent(); - startActivity.setClass(this,InCallScreen.class); - startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(startActivity); - finish(); - } -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; + +public class Activity2 extends Activity { + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Intent startActivity = new Intent(); + startActivity.setClass(this,InCallScreen.class); + startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(startActivity); + finish(); + } +} diff --git a/src/org/sipdroid/sipua/ui/AutoAnswer.java b/app/src/main/java/org/sipdroid/sipua/ui/AutoAnswer.java similarity index 97% rename from src/org/sipdroid/sipua/ui/AutoAnswer.java rename to app/src/main/java/org/sipdroid/sipua/ui/AutoAnswer.java index ab467c1..98735a5 100644 --- a/src/org/sipdroid/sipua/ui/AutoAnswer.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/AutoAnswer.java @@ -1,70 +1,70 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences.Editor; -import android.media.AudioManager; -import android.os.Bundle; -import android.preference.PreferenceManager; - -public class AutoAnswer extends Activity { - AudioManager am; - - boolean getMode() { - return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Settings.PREF_AUTO_DEMAND, Settings.DEFAULT_AUTO_DEMAND); - } - - void restoreVolume() { - am.setStreamVolume(AudioManager.STREAM_RING,1,0); - am.setRingerMode( - PreferenceManager.getDefaultSharedPreferences(this).getInt("ringermode"+getMode(), - AudioManager.RINGER_MODE_NORMAL)); - am.setStreamVolume(AudioManager.STREAM_RING, - PreferenceManager.getDefaultSharedPreferences(this).getInt("volume"+getMode(), - am.getStreamMaxVolume(AudioManager.STREAM_RING)* - (getMode()?4:3)/4 - ),AudioManager.FLAG_VIBRATE | AudioManager.FLAG_SHOW_UI); - } - - void saveVolume() { - Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); - - edit.putInt("volume"+getMode(),am.getStreamVolume(AudioManager.STREAM_RING)); - edit.putInt("ringermode"+getMode(),am.getRingerMode()); - edit.commit(); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); - - am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); - saveVolume(); - edit.putBoolean(Settings.PREF_AUTO_DEMAND, !getMode()); - edit.commit(); - restoreVolume(); - Receiver.updateAutoAnswer(); - finish(); - } -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences.Editor; +import android.media.AudioManager; +import android.os.Bundle; +import android.preference.PreferenceManager; + +public class AutoAnswer extends Activity { + AudioManager am; + + boolean getMode() { + return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Settings.PREF_AUTO_DEMAND, Settings.DEFAULT_AUTO_DEMAND); + } + + void restoreVolume() { + am.setStreamVolume(AudioManager.STREAM_RING,1,0); + am.setRingerMode( + PreferenceManager.getDefaultSharedPreferences(this).getInt("ringermode"+getMode(), + AudioManager.RINGER_MODE_NORMAL)); + am.setStreamVolume(AudioManager.STREAM_RING, + PreferenceManager.getDefaultSharedPreferences(this).getInt("volume"+getMode(), + am.getStreamMaxVolume(AudioManager.STREAM_RING)* + (getMode()?4:3)/4 + ),AudioManager.FLAG_VIBRATE | AudioManager.FLAG_SHOW_UI); + } + + void saveVolume() { + Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); + + edit.putInt("volume"+getMode(),am.getStreamVolume(AudioManager.STREAM_RING)); + edit.putInt("ringermode"+getMode(),am.getRingerMode()); + edit.commit(); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); + + am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + saveVolume(); + edit.putBoolean(Settings.PREF_AUTO_DEMAND, !getMode()); + edit.commit(); + restoreVolume(); + Receiver.updateAutoAnswer(); + finish(); + } +} diff --git a/src/org/sipdroid/sipua/ui/CallScreen.java b/app/src/main/java/org/sipdroid/sipua/ui/CallScreen.java similarity index 97% rename from src/org/sipdroid/sipua/ui/CallScreen.java rename to app/src/main/java/org/sipdroid/sipua/ui/CallScreen.java index 1eb8552..46b26df 100644 --- a/src/org/sipdroid/sipua/ui/CallScreen.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/CallScreen.java @@ -1,282 +1,282 @@ -package org.sipdroid.sipua.ui; - -import java.io.IOException; -import java.net.InetAddress; -import java.util.Random; - -import org.sipdroid.media.RtpStreamReceiver; -import org.sipdroid.net.RtpPacket; -import org.sipdroid.net.RtpSocket; -import org.sipdroid.net.SipdroidSocket; -import org.sipdroid.sipua.R; -import org.sipdroid.sipua.UserAgent; -import org.sipdroid.sipua.ui.InstantAutoCompleteTextView; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.KeyguardManager; -import android.app.KeyguardManager.OnKeyguardExitResult; -import android.content.ActivityNotFoundException; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.media.AudioManager; -import android.net.Uri; -import android.os.Build; -import android.os.Handler; -import android.os.Message; -import android.os.SystemClock; -import android.preference.PreferenceManager; -import android.text.InputType; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.EditText; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -public class CallScreen extends Activity implements DialogInterface.OnClickListener { - public static final int FIRST_MENU_ID = Menu.FIRST; - public static final int HANG_UP_MENU_ITEM = FIRST_MENU_ID + 1; - public static final int HOLD_MENU_ITEM = FIRST_MENU_ID + 2; - public static final int MUTE_MENU_ITEM = FIRST_MENU_ID + 3; - public static final int VIDEO_MENU_ITEM = FIRST_MENU_ID + 5; - public static final int SPEAKER_MENU_ITEM = FIRST_MENU_ID + 6; - public static final int TRANSFER_MENU_ITEM = FIRST_MENU_ID + 7; - public static final int ANSWER_MENU_ITEM = FIRST_MENU_ID + 8; - public static final int BLUETOOTH_MENU_ITEM = FIRST_MENU_ID + 9; - public static final int DTMF_MENU_ITEM = FIRST_MENU_ID + 10; - - private static EditText transferText; - boolean speakerphone; - int speakermode; - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - boolean result = super.onCreateOptionsMenu(menu); - - MenuItem m = menu.add(0, HOLD_MENU_ITEM, 0, R.string.menu_hold); - m.setIcon(android.R.drawable.stat_sys_phone_call_on_hold); - m = menu.add(0, SPEAKER_MENU_ITEM, 0, R.string.menu_speaker); - m.setIcon(android.R.drawable.stat_sys_speakerphone); - m = menu.add(0, MUTE_MENU_ITEM, 0, R.string.menu_mute); - m.setIcon(android.R.drawable.stat_notify_call_mute); - m = menu.add(0, ANSWER_MENU_ITEM, 0, R.string.menu_answer); - m.setIcon(android.R.drawable.ic_menu_call); - m = menu.add(0, BLUETOOTH_MENU_ITEM, 0, R.string.menu_bluetooth); - m.setIcon(R.drawable.stat_sys_phone_call_bluetooth); - m = menu.add(0, TRANSFER_MENU_ITEM, 0, R.string.menu_transfer); - m.setIcon(android.R.drawable.ic_menu_call); - m = menu.add(0, VIDEO_MENU_ITEM, 0, R.string.menu_video); - m.setIcon(android.R.drawable.ic_menu_camera); - m = menu.add(0, HANG_UP_MENU_ITEM, 0, R.string.menu_endCall); - m.setIcon(R.drawable.ic_menu_end_call); - - return result; - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - boolean result = super.onPrepareOptionsMenu(menu); - - if (Receiver.mSipdroidEngine != null && - Receiver.mSipdroidEngine.ua != null && - Receiver.mSipdroidEngine.ua.audio_app != null) { - menu.findItem(HOLD_MENU_ITEM).setVisible(true); - menu.findItem(MUTE_MENU_ITEM).setVisible(true); - menu.findItem(VIDEO_MENU_ITEM).setVisible(Receiver.call_state == UserAgent.UA_STATE_INCALL); - menu.findItem(TRANSFER_MENU_ITEM).setVisible(true); - menu.findItem(BLUETOOTH_MENU_ITEM).setVisible(RtpStreamReceiver.isBluetoothAvailable()); - } else { - menu.findItem(HOLD_MENU_ITEM).setVisible(false); - menu.findItem(MUTE_MENU_ITEM).setVisible(false); - menu.findItem(VIDEO_MENU_ITEM).setVisible(false); - menu.findItem(TRANSFER_MENU_ITEM).setVisible(false); - menu.findItem(BLUETOOTH_MENU_ITEM).setVisible(false); - } - menu.findItem(SPEAKER_MENU_ITEM).setVisible(!(Receiver.headset > 0 || Receiver.docked > 0 || Receiver.bluetooth > 0)); - menu.findItem(ANSWER_MENU_ITEM).setVisible(Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL); - - return result; - } - - public void onClick(DialogInterface dialog, int which) - { - if (which == DialogInterface.BUTTON_POSITIVE) - Receiver.engine(this).transfer(transferText.getText().toString()); - } - - private void transfer() { - transferText = new InstantAutoCompleteTextView(Receiver.mContext,null); - transferText.setInputType(InputType.TYPE_CLASS_TEXT | - InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); - - new AlertDialog.Builder(this) - .setTitle(Receiver.mContext.getString(R.string.transfer_title)) - .setView(transferText) - .setPositiveButton(android.R.string.ok, this) - .setNegativeButton(android.R.string.cancel, this) - .show(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - boolean result = super.onOptionsItemSelected(item); - Intent intent = null; - - switch (item.getItemId()) { - case HANG_UP_MENU_ITEM: - Receiver.stopRingtone(); - Receiver.engine(this).rejectcall(); - break; - - case ANSWER_MENU_ITEM: - Receiver.engine(this).answercall(); - break; - - case HOLD_MENU_ITEM: - Receiver.engine(this).togglehold(); - break; - - case TRANSFER_MENU_ITEM: - transfer(); - break; - - case MUTE_MENU_ITEM: - Receiver.engine(this).togglemute(); - break; - - case SPEAKER_MENU_ITEM: - Receiver.engine(this).speaker(RtpStreamReceiver.speakermode == AudioManager.MODE_NORMAL? - AudioManager.MODE_IN_CALL:AudioManager.MODE_NORMAL); - break; - - case BLUETOOTH_MENU_ITEM: - Receiver.engine(this).togglebluetooth(); - break; - - case VIDEO_MENU_ITEM: - if (Receiver.call_state == UserAgent.UA_STATE_HOLD) Receiver.engine(this).togglehold(); - try { - speakerphone = true; - intent = new Intent(Intent.ACTION_VIEW); - String url = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_POSURL, org.sipdroid.sipua.ui.Settings.DEFAULT_POSURL); - - if (url.length() > 0) - url += "?action=video"; - else - url = "http://"+Settings.DEFAULT_SERVER+"/"+(new Random().nextInt(9000) + 1000); - intent.setData(Uri.parse(url)); - startActivity(intent); - } catch (ActivityNotFoundException e) { - } - break; - } - - return result; - } - - long enabletime; - KeyguardManager mKeyguardManager; - KeyguardManager.KeyguardLock mKeyguardLock; - boolean enabled; - - void disableKeyguard() { - if (mKeyguardManager == null) { - mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); - mKeyguardLock = mKeyguardManager.newKeyguardLock("Sipdroid"); - enabled = true; - } - if (enabled) { - mKeyguardLock.disableKeyguard(); - if (Integer.parseInt(Build.VERSION.SDK) == 16 && Build.MODEL.contains("HTC One")) - mKeyguardManager.exitKeyguardSecurely(new OnKeyguardExitResult() { - public void onKeyguardExitResult(boolean success) { - } - }); - enabled = false; - enabletime = SystemClock.elapsedRealtime(); - } - } - - void reenableKeyguard() { - if (!enabled) { - try { - if (Integer.parseInt(Build.VERSION.SDK) < 5) - Thread.sleep(1000); - } catch (InterruptedException e) { - } - mKeyguardLock.reenableKeyguard(); - enabled = true; - } - } - - SipdroidSocket socket; - RtpSocket rtp_socket; - Context mContext = this; - Intent intent; - - @Override - public void onResume() { - super.onResume(); - if (Integer.parseInt(Build.VERSION.SDK) >= 5 && Integer.parseInt(Build.VERSION.SDK) <= 7) - disableKeyguard(); - } - - Handler mHandler = new Handler() { - public void handleMessage(Message msg) { - onResume(); - } - }; - - @Override - public void onPause() { - if (socket != null) { - socket.close(); - socket = null; - } - super.onPause(); - if (Integer.parseInt(Build.VERSION.SDK) >= 5 && Integer.parseInt(Build.VERSION.SDK) <= 7) - reenableKeyguard(); - } - - @Override - public void onStart() { - super.onStart(); - if (Integer.parseInt(Build.VERSION.SDK) < 5 || Integer.parseInt(Build.VERSION.SDK) > 7) - disableKeyguard(); - if (speakerphone) { - Receiver.engine(this).speaker(speakermode); - speakerphone = false; - } - } - - @Override - public void onStop() { - super.onStop(); - if (Integer.parseInt(Build.VERSION.SDK) < 5 || Integer.parseInt(Build.VERSION.SDK) > 7) - reenableKeyguard(); - if (speakerphone) { - RtpStreamReceiver.notoast = true; - speakermode = Receiver.engine(this).speaker(AudioManager.MODE_NORMAL); - } - } - -} +package org.sipdroid.sipua.ui; + +import java.io.IOException; +import java.net.InetAddress; +import java.util.Random; + +import org.sipdroid.media.RtpStreamReceiver; +import org.sipdroid.net.RtpPacket; +import org.sipdroid.net.RtpSocket; +import org.sipdroid.net.SipdroidSocket; +import org.sipdroid.sipua.R; +import org.sipdroid.sipua.UserAgent; +import org.sipdroid.sipua.ui.InstantAutoCompleteTextView; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.KeyguardManager; +import android.app.KeyguardManager.OnKeyguardExitResult; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.media.AudioManager; +import android.net.Uri; +import android.os.Build; +import android.os.Handler; +import android.os.Message; +import android.os.SystemClock; +import android.preference.PreferenceManager; +import android.text.InputType; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.EditText; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +public class CallScreen extends Activity implements DialogInterface.OnClickListener { + public static final int FIRST_MENU_ID = Menu.FIRST; + public static final int HANG_UP_MENU_ITEM = FIRST_MENU_ID + 1; + public static final int HOLD_MENU_ITEM = FIRST_MENU_ID + 2; + public static final int MUTE_MENU_ITEM = FIRST_MENU_ID + 3; + public static final int VIDEO_MENU_ITEM = FIRST_MENU_ID + 5; + public static final int SPEAKER_MENU_ITEM = FIRST_MENU_ID + 6; + public static final int TRANSFER_MENU_ITEM = FIRST_MENU_ID + 7; + public static final int ANSWER_MENU_ITEM = FIRST_MENU_ID + 8; + public static final int BLUETOOTH_MENU_ITEM = FIRST_MENU_ID + 9; + public static final int DTMF_MENU_ITEM = FIRST_MENU_ID + 10; + + private static EditText transferText; + boolean speakerphone; + int speakermode; + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + boolean result = super.onCreateOptionsMenu(menu); + + MenuItem m = menu.add(0, HOLD_MENU_ITEM, 0, R.string.menu_hold); + m.setIcon(android.R.drawable.stat_sys_phone_call_on_hold); + m = menu.add(0, SPEAKER_MENU_ITEM, 0, R.string.menu_speaker); + m.setIcon(android.R.drawable.stat_sys_speakerphone); + m = menu.add(0, MUTE_MENU_ITEM, 0, R.string.menu_mute); + m.setIcon(android.R.drawable.stat_notify_call_mute); + m = menu.add(0, ANSWER_MENU_ITEM, 0, R.string.menu_answer); + m.setIcon(android.R.drawable.ic_menu_call); + m = menu.add(0, BLUETOOTH_MENU_ITEM, 0, R.string.menu_bluetooth); + m.setIcon(R.drawable.stat_sys_phone_call_bluetooth); + m = menu.add(0, TRANSFER_MENU_ITEM, 0, R.string.menu_transfer); + m.setIcon(android.R.drawable.ic_menu_call); + m = menu.add(0, VIDEO_MENU_ITEM, 0, R.string.menu_video); + m.setIcon(android.R.drawable.ic_menu_camera); + m = menu.add(0, HANG_UP_MENU_ITEM, 0, R.string.menu_endCall); + m.setIcon(R.drawable.ic_menu_end_call); + + return result; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + boolean result = super.onPrepareOptionsMenu(menu); + + if (Receiver.mSipdroidEngine != null && + Receiver.mSipdroidEngine.ua != null && + Receiver.mSipdroidEngine.ua.audio_app != null) { + menu.findItem(HOLD_MENU_ITEM).setVisible(true); + menu.findItem(MUTE_MENU_ITEM).setVisible(true); + menu.findItem(VIDEO_MENU_ITEM).setVisible(Receiver.call_state == UserAgent.UA_STATE_INCALL); + menu.findItem(TRANSFER_MENU_ITEM).setVisible(true); + menu.findItem(BLUETOOTH_MENU_ITEM).setVisible(RtpStreamReceiver.isBluetoothAvailable()); + } else { + menu.findItem(HOLD_MENU_ITEM).setVisible(false); + menu.findItem(MUTE_MENU_ITEM).setVisible(false); + menu.findItem(VIDEO_MENU_ITEM).setVisible(false); + menu.findItem(TRANSFER_MENU_ITEM).setVisible(false); + menu.findItem(BLUETOOTH_MENU_ITEM).setVisible(false); + } + menu.findItem(SPEAKER_MENU_ITEM).setVisible(!(Receiver.headset > 0 || Receiver.docked > 0 || Receiver.bluetooth > 0)); + menu.findItem(ANSWER_MENU_ITEM).setVisible(Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL); + + return result; + } + + public void onClick(DialogInterface dialog, int which) + { + if (which == DialogInterface.BUTTON_POSITIVE) + Receiver.engine(this).transfer(transferText.getText().toString()); + } + + private void transfer() { + transferText = new InstantAutoCompleteTextView(Receiver.mContext,null); + transferText.setInputType(InputType.TYPE_CLASS_TEXT | + InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); + + new AlertDialog.Builder(this) + .setTitle(Receiver.mContext.getString(R.string.transfer_title)) + .setView(transferText) + .setPositiveButton(android.R.string.ok, this) + .setNegativeButton(android.R.string.cancel, this) + .show(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean result = super.onOptionsItemSelected(item); + Intent intent = null; + + switch (item.getItemId()) { + case HANG_UP_MENU_ITEM: + Receiver.stopRingtone(); + Receiver.engine(this).rejectcall(); + break; + + case ANSWER_MENU_ITEM: + Receiver.engine(this).answercall(); + break; + + case HOLD_MENU_ITEM: + Receiver.engine(this).togglehold(); + break; + + case TRANSFER_MENU_ITEM: + transfer(); + break; + + case MUTE_MENU_ITEM: + Receiver.engine(this).togglemute(); + break; + + case SPEAKER_MENU_ITEM: + Receiver.engine(this).speaker(RtpStreamReceiver.speakermode == AudioManager.MODE_NORMAL? + AudioManager.MODE_IN_CALL:AudioManager.MODE_NORMAL); + break; + + case BLUETOOTH_MENU_ITEM: + Receiver.engine(this).togglebluetooth(); + break; + + case VIDEO_MENU_ITEM: + if (Receiver.call_state == UserAgent.UA_STATE_HOLD) Receiver.engine(this).togglehold(); + try { + speakerphone = true; + intent = new Intent(Intent.ACTION_VIEW); + String url = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_POSURL, org.sipdroid.sipua.ui.Settings.DEFAULT_POSURL); + + if (url.length() > 0) + url += "?action=video"; + else + url = "http://"+Settings.DEFAULT_SERVER+"/"+(new Random().nextInt(9000) + 1000); + intent.setData(Uri.parse(url)); + startActivity(intent); + } catch (ActivityNotFoundException e) { + } + break; + } + + return result; + } + + long enabletime; + KeyguardManager mKeyguardManager; + KeyguardManager.KeyguardLock mKeyguardLock; + boolean enabled; + + void disableKeyguard() { + if (mKeyguardManager == null) { + mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); + mKeyguardLock = mKeyguardManager.newKeyguardLock("Sipdroid"); + enabled = true; + } + if (enabled) { + mKeyguardLock.disableKeyguard(); + if (Integer.parseInt(Build.VERSION.SDK) == 16 && Build.MODEL.contains("HTC One")) + mKeyguardManager.exitKeyguardSecurely(new OnKeyguardExitResult() { + public void onKeyguardExitResult(boolean success) { + } + }); + enabled = false; + enabletime = SystemClock.elapsedRealtime(); + } + } + + void reenableKeyguard() { + if (!enabled) { + try { + if (Integer.parseInt(Build.VERSION.SDK) < 5) + Thread.sleep(1000); + } catch (InterruptedException e) { + } + mKeyguardLock.reenableKeyguard(); + enabled = true; + } + } + + SipdroidSocket socket; + RtpSocket rtp_socket; + Context mContext = this; + Intent intent; + + @Override + public void onResume() { + super.onResume(); + if (Integer.parseInt(Build.VERSION.SDK) >= 5 && Integer.parseInt(Build.VERSION.SDK) <= 7) + disableKeyguard(); + } + + Handler mHandler = new Handler() { + public void handleMessage(Message msg) { + onResume(); + } + }; + + @Override + public void onPause() { + if (socket != null) { + socket.close(); + socket = null; + } + super.onPause(); + if (Integer.parseInt(Build.VERSION.SDK) >= 5 && Integer.parseInt(Build.VERSION.SDK) <= 7) + reenableKeyguard(); + } + + @Override + public void onStart() { + super.onStart(); + if (Integer.parseInt(Build.VERSION.SDK) < 5 || Integer.parseInt(Build.VERSION.SDK) > 7) + disableKeyguard(); + if (speakerphone) { + Receiver.engine(this).speaker(speakermode); + speakerphone = false; + } + } + + @Override + public void onStop() { + super.onStop(); + if (Integer.parseInt(Build.VERSION.SDK) < 5 || Integer.parseInt(Build.VERSION.SDK) > 7) + reenableKeyguard(); + if (speakerphone) { + RtpStreamReceiver.notoast = true; + speakermode = Receiver.engine(this).speaker(AudioManager.MODE_NORMAL); + } + } + +} diff --git a/src/org/sipdroid/sipua/ui/Caller.java b/app/src/main/java/org/sipdroid/sipua/ui/Caller.java similarity index 97% rename from src/org/sipdroid/sipua/ui/Caller.java rename to app/src/main/java/org/sipdroid/sipua/ui/Caller.java index 6cc57d9..e9bb133 100644 --- a/src/org/sipdroid/sipua/ui/Caller.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/Caller.java @@ -1,321 +1,321 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import java.util.Vector; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import org.sipdroid.media.RtpStreamReceiver; -import org.sipdroid.sipua.UserAgent; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.database.Cursor; -import android.net.Uri; -import android.os.SystemClock; -import android.preference.PreferenceManager; -import android.provider.ContactsContract; -import android.provider.ContactsContract.CommonDataKinds.Phone; -import android.provider.ContactsContract.PhoneLookup; -import android.telephony.PhoneNumberUtils; -import android.text.TextUtils; -import android.util.Log; - -public class Caller extends BroadcastReceiver { - - static long noexclude; - String last_number; - long last_time; - - static String getNumber(Context context,Uri contactRef,String column) { - String number = ""; - - Cursor phonesCursor = context.getContentResolver().query(contactRef, null, null, null, - ContactsContract.CommonDataKinds.Phone.IS_PRIMARY + " DESC"); - if (phonesCursor != null) - { - if (phonesCursor.moveToNext()) - { - String id = phonesCursor.getString(phonesCursor - .getColumnIndex(column)); - Cursor pCur = context.getContentResolver().query(Phone.CONTENT_URI, - null, Phone.CONTACT_ID + "=?", new String[] { id }, null); - while (pCur.moveToNext()) { - String n = pCur.getString(pCur - .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); - if (TextUtils.isEmpty(n)) continue; - if (!number.equals("")) number = number + "&"; - n = PhoneNumberUtils.stripSeparators(n); - number = number + searchReplaceNumber(context, n); - } - pCur.close(); - } - phonesCursor.close(); - } - return number; - } - - @Override - public void onReceive(final Context context, Intent intent) { - String intentAction = intent.getAction(); - String number = getResultData(); - Boolean force = false; - - if (intentAction.equals(Intent.ACTION_NEW_OUTGOING_CALL) && number != null) - { - if (!Sipdroid.release) Log.i("SipUA:","outgoing call"); - if (!Sipdroid.on(context)) return; - boolean sip_type = !PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_PSTN); - boolean ask = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_ASK); - - if (Receiver.call_state != UserAgent.UA_STATE_IDLE && RtpStreamReceiver.isBluetoothAvailable()) { - setResultData(null); - switch (Receiver.call_state) { - case UserAgent.UA_STATE_INCOMING_CALL: - Receiver.engine(context).answercall(); - if (RtpStreamReceiver.bluetoothmode) - break; - default: - if (RtpStreamReceiver.bluetoothmode) - Receiver.engine(context).rejectcall(); - else - Receiver.engine(context).togglebluetooth(); - break; - } - return; - } - if (last_number != null && last_number.equals(number) && (SystemClock.elapsedRealtime()-last_time) < 3000) { - setResultData(null); - return; - } - last_time = SystemClock.elapsedRealtime(); - last_number = number; - if (number.endsWith("+")) - { - sip_type = !sip_type; - number = number.substring(0,number.length()-1); - force = true; - } - if (SystemClock.elapsedRealtime() < noexclude + 10000) { - noexclude = 0; - force = true; - } - if (sip_type && !force) { - String sExPat = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_EXCLUDEPAT, Settings.DEFAULT_EXCLUDEPAT); - boolean bExNums = false; - boolean bExTypes = false; - if (sExPat.length() > 0) - { - Vector vExPats = getTokens(sExPat, ","); - Vector vPatNums = new Vector(); - Vector vTypesCode = new Vector(); - for(int i = 0; i < vExPats.size(); i++) - { - if (vExPats.get(i).startsWith("h") || vExPats.get(i).startsWith("H")) - vTypesCode.add(Integer.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)); - else if (vExPats.get(i).startsWith("m") || vExPats.get(i).startsWith("M")) - vTypesCode.add(Integer.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)); - else if (vExPats.get(i).startsWith("w") || vExPats.get(i).startsWith("W")) - vTypesCode.add(Integer.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)); - else - vPatNums.add(vExPats.get(i)); - } - if(vTypesCode.size() > 0) - bExTypes = isExcludedType(vTypesCode, number, context); - if(vPatNums.size() > 0) - bExNums = isExcludedNum(vPatNums, number); - } - if (bExTypes || bExNums) - sip_type = false; - } - - if (!sip_type) - { - setResultData(number); - } - else - { - if (number != null && !intent.getBooleanExtra("android.phone.extra.ALREADY_CALLED",false)) { - // Migrate the "prefix" option. TODO Remove this code in a future release. - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - if (sp.contains("prefix")) { - String prefix = sp.getString(Settings.PREF_PREFIX, Settings.DEFAULT_PREFIX); - Editor editor = sp.edit(); - if (!prefix.trim().equals("")) { - editor.putString(Settings.PREF_SEARCH, "(.*)," + prefix + "\\1"); - } - editor.remove(Settings.PREF_PREFIX); - editor.commit(); - } - - // Search & replace. - String callthru_number = searchReplaceNumber(context,number); - String callthru_prefix; - - if (!ask && !force && PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_PAR, Settings.DEFAULT_PAR)) - { - number = getNumber(context,Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number), PhoneLookup._ID); - if (number.equals("")) - number = callthru_number; - } else - number = callthru_number; - - if (PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_SIPONLY)) - force = true; - if (!ask && Receiver.engine(context).call(number,force)) - setResultData(null); - else if (!ask && PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_CALLTHRU, Settings.DEFAULT_CALLTHRU) && - (callthru_prefix = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_CALLTHRU2, Settings.DEFAULT_CALLTHRU2)).length() > 0) { - callthru_number = (callthru_prefix+","+callthru_number+"#"); - setResultData(callthru_number); - } else if (ask || force) { - setResultData(null); - final String n = number; - (new Thread() { - public void run() { - try { - Thread.sleep(200); - } catch (InterruptedException e) { - } - Intent intent = new Intent(Intent.ACTION_CALL, - Uri.fromParts("sipdroid", Uri.decode(n), null)); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(intent); - } - }).start(); - } - } - } - } - } - - static private String searchReplaceNumber(Context context,String number) { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - String pattern = sp.getString(Settings.PREF_SEARCH, Settings.DEFAULT_SEARCH); - // Comma should be safe as separator. - String[] split = pattern.split(","); - // We need exactly 2 parts: search and replace. Otherwise - // we just return the current number. - if (split.length == 1) - split = new String[] {split[0],""}; - if (split.length != 2) - return number; - - String modNumber = split[1]; - - try { - // Compiles the regular expression. This could be done - // when the user modify the pattern... TODO Optimize - // this, only compile once. - Pattern p = Pattern.compile(split[0]); - Matcher m = p.matcher(number); - // Main loop of the function. - if (m.matches()) { - for (int i = 0; i < m.groupCount() + 1; i++) { - String r = m.group(i); - if (r != null) { - modNumber = modNumber.replace("\\" + i, r); - } - } - } - // If the modified number is the same as the replacement - // value, we guess that the user typed a bad replacement - // value and we use the original number. - if (modNumber.equals(split[1])) { - modNumber = number.replaceAll(split[0], split[1]); - } - } catch (PatternSyntaxException e) { - // Wrong pattern syntax. Give back the original number. - modNumber = number; - } - - // Returns the modified number. - return modNumber; - } - - Vector getTokens(String sInput, String sDelimiter) - { - Vector vTokens = new Vector(); - int iStartIndex = 0; - final int iEndIndex = sInput.lastIndexOf(sDelimiter); - for (; iStartIndex < iEndIndex; iStartIndex++) - { - int iNextIndex = sInput.indexOf(sDelimiter, iStartIndex); - String sPattern = sInput.substring(iStartIndex, iNextIndex).trim(); - vTokens.add(sPattern); - iStartIndex = iNextIndex; - } - if(iStartIndex < sInput.length()) - vTokens.add(sInput.substring(iStartIndex, sInput.length()).trim()); - - return vTokens; - } - - boolean isExcludedNum(Vector vExNums, String sNumber) - { - for (int i = 0; i < vExNums.size(); i++) - { - Pattern p = null; - Matcher m = null; - try - { - p = Pattern.compile(vExNums.get(i)); - m = p.matcher(sNumber); - } - catch(PatternSyntaxException pse) - { - return false; - } - if(m != null && m.find()) - return true; - } - return false; - } - - boolean isExcludedType(Vector vExTypesCode, String sNumber, Context oContext) - { - Uri contactRef = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, sNumber); - final String[] PHONES_PROJECTION = new String[] - { - ContactsContract.CommonDataKinds.Phone.NUMBER, // 0 - ContactsContract.CommonDataKinds.Phone.TYPE, // 1 - }; - Cursor phonesCursor = oContext.getContentResolver().query(contactRef, PHONES_PROJECTION, null, null, - null); - if (phonesCursor != null) - { - while (phonesCursor.moveToNext()) - { - final int type = phonesCursor.getInt(1); - if(vExTypesCode.contains(Integer.valueOf(type))) - return true; - } - phonesCursor.close(); - } - return false; - } - -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import org.sipdroid.media.RtpStreamReceiver; +import org.sipdroid.sipua.UserAgent; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.database.Cursor; +import android.net.Uri; +import android.os.SystemClock; +import android.preference.PreferenceManager; +import android.provider.ContactsContract; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.PhoneLookup; +import android.telephony.PhoneNumberUtils; +import android.text.TextUtils; +import android.util.Log; + +public class Caller extends BroadcastReceiver { + + static long noexclude; + String last_number; + long last_time; + + static String getNumber(Context context,Uri contactRef,String column) { + String number = ""; + + Cursor phonesCursor = context.getContentResolver().query(contactRef, null, null, null, + ContactsContract.CommonDataKinds.Phone.IS_PRIMARY + " DESC"); + if (phonesCursor != null) + { + if (phonesCursor.moveToNext()) + { + String id = phonesCursor.getString(phonesCursor + .getColumnIndex(column)); + Cursor pCur = context.getContentResolver().query(Phone.CONTENT_URI, + null, Phone.CONTACT_ID + "=?", new String[] { id }, null); + while (pCur.moveToNext()) { + String n = pCur.getString(pCur + .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); + if (TextUtils.isEmpty(n)) continue; + if (!number.equals("")) number = number + "&"; + n = PhoneNumberUtils.stripSeparators(n); + number = number + searchReplaceNumber(context, n); + } + pCur.close(); + } + phonesCursor.close(); + } + return number; + } + + @Override + public void onReceive(final Context context, Intent intent) { + String intentAction = intent.getAction(); + String number = getResultData(); + Boolean force = false; + + if (intentAction.equals(Intent.ACTION_NEW_OUTGOING_CALL) && number != null) + { + if (!Sipdroid.release) Log.i("SipUA:","outgoing call"); + if (!Sipdroid.on(context)) return; + boolean sip_type = !PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_PSTN); + boolean ask = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_ASK); + + if (Receiver.call_state != UserAgent.UA_STATE_IDLE && RtpStreamReceiver.isBluetoothAvailable()) { + setResultData(null); + switch (Receiver.call_state) { + case UserAgent.UA_STATE_INCOMING_CALL: + Receiver.engine(context).answercall(); + if (RtpStreamReceiver.bluetoothmode) + break; + default: + if (RtpStreamReceiver.bluetoothmode) + Receiver.engine(context).rejectcall(); + else + Receiver.engine(context).togglebluetooth(); + break; + } + return; + } + if (last_number != null && last_number.equals(number) && (SystemClock.elapsedRealtime()-last_time) < 3000) { + setResultData(null); + return; + } + last_time = SystemClock.elapsedRealtime(); + last_number = number; + if (number.endsWith("+")) + { + sip_type = !sip_type; + number = number.substring(0,number.length()-1); + force = true; + } + if (SystemClock.elapsedRealtime() < noexclude + 10000) { + noexclude = 0; + force = true; + } + if (sip_type && !force) { + String sExPat = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_EXCLUDEPAT, Settings.DEFAULT_EXCLUDEPAT); + boolean bExNums = false; + boolean bExTypes = false; + if (sExPat.length() > 0) + { + Vector vExPats = getTokens(sExPat, ","); + Vector vPatNums = new Vector(); + Vector vTypesCode = new Vector(); + for(int i = 0; i < vExPats.size(); i++) + { + if (vExPats.get(i).startsWith("h") || vExPats.get(i).startsWith("H")) + vTypesCode.add(Integer.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)); + else if (vExPats.get(i).startsWith("m") || vExPats.get(i).startsWith("M")) + vTypesCode.add(Integer.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)); + else if (vExPats.get(i).startsWith("w") || vExPats.get(i).startsWith("W")) + vTypesCode.add(Integer.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)); + else + vPatNums.add(vExPats.get(i)); + } + if(vTypesCode.size() > 0) + bExTypes = isExcludedType(vTypesCode, number, context); + if(vPatNums.size() > 0) + bExNums = isExcludedNum(vPatNums, number); + } + if (bExTypes || bExNums) + sip_type = false; + } + + if (!sip_type) + { + setResultData(number); + } + else + { + if (number != null && !intent.getBooleanExtra("android.phone.extra.ALREADY_CALLED",false)) { + // Migrate the "prefix" option. TODO Remove this code in a future release. + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + if (sp.contains("prefix")) { + String prefix = sp.getString(Settings.PREF_PREFIX, Settings.DEFAULT_PREFIX); + Editor editor = sp.edit(); + if (!prefix.trim().equals("")) { + editor.putString(Settings.PREF_SEARCH, "(.*)," + prefix + "\\1"); + } + editor.remove(Settings.PREF_PREFIX); + editor.commit(); + } + + // Search & replace. + String callthru_number = searchReplaceNumber(context,number); + String callthru_prefix; + + if (!ask && !force && PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_PAR, Settings.DEFAULT_PAR)) + { + number = getNumber(context,Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number), PhoneLookup._ID); + if (number.equals("")) + number = callthru_number; + } else + number = callthru_number; + + if (PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_SIPONLY)) + force = true; + if (!ask && Receiver.engine(context).call(number,force)) + setResultData(null); + else if (!ask && PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_CALLTHRU, Settings.DEFAULT_CALLTHRU) && + (callthru_prefix = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_CALLTHRU2, Settings.DEFAULT_CALLTHRU2)).length() > 0) { + callthru_number = (callthru_prefix+","+callthru_number+"#"); + setResultData(callthru_number); + } else if (ask || force) { + setResultData(null); + final String n = number; + (new Thread() { + public void run() { + try { + Thread.sleep(200); + } catch (InterruptedException e) { + } + Intent intent = new Intent(Intent.ACTION_CALL, + Uri.fromParts("sipdroid", Uri.decode(n), null)); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } + }).start(); + } + } + } + } + } + + static private String searchReplaceNumber(Context context,String number) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + String pattern = sp.getString(Settings.PREF_SEARCH, Settings.DEFAULT_SEARCH); + // Comma should be safe as separator. + String[] split = pattern.split(","); + // We need exactly 2 parts: search and replace. Otherwise + // we just return the current number. + if (split.length == 1) + split = new String[] {split[0],""}; + if (split.length != 2) + return number; + + String modNumber = split[1]; + + try { + // Compiles the regular expression. This could be done + // when the user modify the pattern... TODO Optimize + // this, only compile once. + Pattern p = Pattern.compile(split[0]); + Matcher m = p.matcher(number); + // Main loop of the function. + if (m.matches()) { + for (int i = 0; i < m.groupCount() + 1; i++) { + String r = m.group(i); + if (r != null) { + modNumber = modNumber.replace("\\" + i, r); + } + } + } + // If the modified number is the same as the replacement + // value, we guess that the user typed a bad replacement + // value and we use the original number. + if (modNumber.equals(split[1])) { + modNumber = number.replaceAll(split[0], split[1]); + } + } catch (PatternSyntaxException e) { + // Wrong pattern syntax. Give back the original number. + modNumber = number; + } + + // Returns the modified number. + return modNumber; + } + + Vector getTokens(String sInput, String sDelimiter) + { + Vector vTokens = new Vector(); + int iStartIndex = 0; + final int iEndIndex = sInput.lastIndexOf(sDelimiter); + for (; iStartIndex < iEndIndex; iStartIndex++) + { + int iNextIndex = sInput.indexOf(sDelimiter, iStartIndex); + String sPattern = sInput.substring(iStartIndex, iNextIndex).trim(); + vTokens.add(sPattern); + iStartIndex = iNextIndex; + } + if(iStartIndex < sInput.length()) + vTokens.add(sInput.substring(iStartIndex, sInput.length()).trim()); + + return vTokens; + } + + boolean isExcludedNum(Vector vExNums, String sNumber) + { + for (int i = 0; i < vExNums.size(); i++) + { + Pattern p = null; + Matcher m = null; + try + { + p = Pattern.compile(vExNums.get(i)); + m = p.matcher(sNumber); + } + catch(PatternSyntaxException pse) + { + return false; + } + if(m != null && m.find()) + return true; + } + return false; + } + + boolean isExcludedType(Vector vExTypesCode, String sNumber, Context oContext) + { + Uri contactRef = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, sNumber); + final String[] PHONES_PROJECTION = new String[] + { + ContactsContract.CommonDataKinds.Phone.NUMBER, // 0 + ContactsContract.CommonDataKinds.Phone.TYPE, // 1 + }; + Cursor phonesCursor = oContext.getContentResolver().query(contactRef, PHONES_PROJECTION, null, null, + null); + if (phonesCursor != null) + { + while (phonesCursor.moveToNext()) + { + final int type = phonesCursor.getInt(1); + if(vExTypesCode.contains(Integer.valueOf(type))) + return true; + } + phonesCursor.close(); + } + return false; + } + +} diff --git a/src/org/sipdroid/sipua/ui/ChangeAccount.java b/app/src/main/java/org/sipdroid/sipua/ui/ChangeAccount.java similarity index 97% rename from src/org/sipdroid/sipua/ui/ChangeAccount.java rename to app/src/main/java/org/sipdroid/sipua/ui/ChangeAccount.java index c93e2f4..918a043 100644 --- a/src/org/sipdroid/sipua/ui/ChangeAccount.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/ChangeAccount.java @@ -1,45 +1,45 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences.Editor; -import android.os.Bundle; -import android.preference.PreferenceManager; - -public class ChangeAccount extends Activity { - - public static int getPref(Context context) { - return PreferenceManager.getDefaultSharedPreferences(context).getInt(Settings.PREF_ACCOUNT, Settings.DEFAULT_ACCOUNT); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); - - edit.putInt(Settings.PREF_ACCOUNT, Receiver.engine(this).pref = 1-getPref(this)); - edit.commit(); - Receiver.engine(this).register(); - finish(); - } -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences.Editor; +import android.os.Bundle; +import android.preference.PreferenceManager; + +public class ChangeAccount extends Activity { + + public static int getPref(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context).getInt(Settings.PREF_ACCOUNT, Settings.DEFAULT_ACCOUNT); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); + + edit.putInt(Settings.PREF_ACCOUNT, Receiver.engine(this).pref = 1-getPref(this)); + edit.commit(); + Receiver.engine(this).register(); + finish(); + } +} diff --git a/src/org/sipdroid/sipua/ui/Checkin.java b/app/src/main/java/org/sipdroid/sipua/ui/Checkin.java similarity index 96% rename from src/org/sipdroid/sipua/ui/Checkin.java rename to app/src/main/java/org/sipdroid/sipua/ui/Checkin.java index 21a5bba..c87c787 100644 --- a/src/org/sipdroid/sipua/ui/Checkin.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/Checkin.java @@ -1,75 +1,75 @@ -package org.sipdroid.sipua.ui; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; - -import org.sipdroid.sipua.UserAgentProfile; -import org.zoolu.tools.Random; - -import android.content.Intent; -import android.net.Uri; -import android.os.SystemClock; -import android.preference.PreferenceManager; - -public class Checkin { - - static long hold; - static int createButton; - - static void url(final String opt,final boolean in_call) { - (new Thread() { - public void run() { - try { - URL url = new URL(opt); - String line; - String[] lines; - BufferedReader in; - - if (!in_call) - try { - sleep(3000); - } catch (InterruptedException e) { - } - in = new BufferedReader(new InputStreamReader(url.openStream())); - for (;;) { - line = in.readLine(); - if (line == null) break; - lines = line.split(" "); - if (lines.length == 2) { - if (lines[0].equals("createButton")) - createButton = Integer.valueOf(lines[1]); - else { - int i = 0; - for (UserAgentProfile user_profile : Receiver.engine(Receiver.mContext).user_profiles) { - if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_DNS+i, Settings.DEFAULT_DNS).equals(lines[0]) || - (user_profile != null && user_profile.realm != null && - user_profile.realm.contains(lines[0]))) { - if (in_call) { - hold = SystemClock.elapsedRealtime(); - Receiver.engine(Receiver.mContext).rejectcall(); - } - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(lines[1])); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - Receiver.mContext.startActivity(intent); - } - i++; - } - } - } - } - in.close(); - } catch (Exception e) { - if (!Sipdroid.release) e.printStackTrace(); - } - - } - }).start(); - } - - public static void checkin(boolean in_call) { - if (!in_call || SystemClock.elapsedRealtime() < hold + 5*60*1000 || - Random.nextInt(5) == 1) - url("http://sipdroid.googlecode.com/svn/images/checkin",in_call); - } -} +package org.sipdroid.sipua.ui; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; + +import org.sipdroid.sipua.UserAgentProfile; +import org.zoolu.tools.Random; + +import android.content.Intent; +import android.net.Uri; +import android.os.SystemClock; +import android.preference.PreferenceManager; + +public class Checkin { + + static long hold; + static int createButton; + + static void url(final String opt,final boolean in_call) { + (new Thread() { + public void run() { + try { + URL url = new URL(opt); + String line; + String[] lines; + BufferedReader in; + + if (!in_call) + try { + sleep(3000); + } catch (InterruptedException e) { + } + in = new BufferedReader(new InputStreamReader(url.openStream())); + for (;;) { + line = in.readLine(); + if (line == null) break; + lines = line.split(" "); + if (lines.length == 2) { + if (lines[0].equals("createButton")) + createButton = Integer.valueOf(lines[1]); + else { + int i = 0; + for (UserAgentProfile user_profile : Receiver.engine(Receiver.mContext).user_profiles) { + if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_DNS+i, Settings.DEFAULT_DNS).equals(lines[0]) || + (user_profile != null && user_profile.realm != null && + user_profile.realm.contains(lines[0]))) { + if (in_call) { + hold = SystemClock.elapsedRealtime(); + Receiver.engine(Receiver.mContext).rejectcall(); + } + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(lines[1])); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Receiver.mContext.startActivity(intent); + } + i++; + } + } + } + } + in.close(); + } catch (Exception e) { + if (!Sipdroid.release) e.printStackTrace(); + } + + } + }).start(); + } + + public static void checkin(boolean in_call) { + if (!in_call || SystemClock.elapsedRealtime() < hold + 5*60*1000 || + Random.nextInt(5) == 1) + url("http://sipdroid.googlecode.com/svn/images/checkin",in_call); + } +} diff --git a/src/org/sipdroid/sipua/ui/CreateAccount.java b/app/src/main/java/org/sipdroid/sipua/ui/CreateAccount.java similarity index 97% rename from src/org/sipdroid/sipua/ui/CreateAccount.java rename to app/src/main/java/org/sipdroid/sipua/ui/CreateAccount.java index c76bd4c..8788194 100644 --- a/src/org/sipdroid/sipua/ui/CreateAccount.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/CreateAccount.java @@ -1,247 +1,247 @@ -/* - * Copyright (C) 2010 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.sipdroid.sipua.ui; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.util.List; -import java.util.Locale; -import java.util.Random; - -import org.sipdroid.sipua.R; -import org.sipdroid.sipua.RegisterAgent; -import org.sipdroid.sipua.SipdroidEngine; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.app.Dialog; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences.Editor; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.net.Uri; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.preference.PreferenceManager; -import android.text.format.Time; -import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.TextView; -import android.widget.Toast; - -public class CreateAccount extends Dialog { - - Context mContext; - - public CreateAccount(Context context) { - super(context); - mContext = context; - } - - static String email,trunkserver,trunkuser,trunkpassword,trunkport; - - public static String isPossible(Context context) { - Boolean found = false; - email = trunkserver = null; - for (int i = 0; i < SipdroidEngine.LINES; i++) { - String j = (i!=0?""+i:""); - String username = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_USERNAME+j, Settings.DEFAULT_USERNAME), - server = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_SERVER+j, Settings.DEFAULT_SERVER); - if (username.equals("") || server.equals("")) - continue; - if (server.contains("pbxes")) - found = true; - else if (i == 0 && - !PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PROTOCOL+j, Settings.DEFAULT_PROTOCOL).equals("tcp") && - PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_3G+j, Settings.DEFAULT_3G) && - Receiver.engine(context).isRegistered(i) && - Receiver.engine(context).ras[i].CurrentState == RegisterAgent.REGISTERED) { - trunkserver = server; - trunkuser = username; - trunkpassword = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PASSWORD+j, Settings.DEFAULT_PASSWORD); - trunkport = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PORT+j, Settings.DEFAULT_PORT); - } - } - if (found) return null; - Account[] accounts = AccountManager.get(context).getAccountsByType("com.google"); - for (Account account : accounts) { - email = account.name; - break; - } - if (email == null) return null; - Intent intent = new Intent(Intent.ACTION_SENDTO); - intent.setPackage("com.google.android.apps.googlevoice"); - intent.setData(Uri.fromParts("smsto", "", null)); - List a = context.getPackageManager().queryIntentActivities(intent,PackageManager.GET_INTENT_FILTERS); - if (a != null && a.size() != 0) { - trunkserver = null; - return context.getString(R.string.menu_create); - } - if (trunkserver != null) - return "New PBX linked to "+trunkserver; - return null; - } - - String line; - - Handler mHandler = new Handler() { - public void handleMessage(Message msg) { - Toast.makeText(mContext, line, Toast.LENGTH_LONG).show(); - buttonCancel.setEnabled(true); - buttonOK.setEnabled(true); - setCancelable(true); - } - }; - - String generatePassword(int length) - { - String availableCharacters = ""; - String password = ""; - - // Generate the appropriate character set - availableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - availableCharacters = availableCharacters + "0123456789"; - - // Generate the random number generator - Random selector = new Random(); - - // Generate the password - int i; - for(i = 0; i < length; i++) - { - password = password + availableCharacters.charAt(selector.nextInt(availableCharacters.length() - 1)); - } - - return password; - } - - void CreateAccountNow() { - buttonCancel.setEnabled(false); - buttonOK.setEnabled(false); - setCancelable(false); - Toast.makeText(mContext, "Please stand by while your account is being created", Toast.LENGTH_LONG).show(); - (new Thread() { - public void run() { - line = "Can't connect to webserver"; - try { - String password = generatePassword(8); - String language = Locale.getDefault().toString().substring(0,2); - if (!language.equals("de") && !language.equals("es") && !language.equals("fr") && - !language.equals("it") && !language.equals("ru")) - if (language.equals("ja")) - language = "jp"; - else if (language.equals("zh")) - language = "cn"; - else - language = "en"; - String s = "https://www1.pbxes.com/config.php?m=register&a=update&f=action&username="+Uri.encode(etName.getText().toString())+"&password=" - +Uri.encode(etPass.getText().toString())+"&password_confirm="+Uri.encode(etConfirm.getText().toString())+"&language="+language+"&email="+Uri.encode(email)+"&land="+Uri.encode(Time.getCurrentTimezone())+ - "&sipdroid="+Uri.encode(password); - if (trunkserver != null) { - s = s+"&trunkserver="+Uri.encode(trunkserver+":"+trunkport)+ - "&trunkuser="+Uri.encode(trunkuser); - } - URL url = new URL(s); - BufferedReader in; - in = new BufferedReader(new InputStreamReader(url.openStream())); - line = in.readLine(); - if (line == null) { - in = new BufferedReader(new InputStreamReader(url.openStream())); - line = in.readLine(); - } - if (line != null) { - if (line.equals("OK")) { - Editor edit = PreferenceManager.getDefaultSharedPreferences(mContext).edit(); - edit.putString(Settings.PREF_SERVER, Settings.DEFAULT_SERVER); - edit.putString(Settings.PREF_USERNAME, etName.getText()+"-200"); - edit.putString(Settings.PREF_DOMAIN, Settings.DEFAULT_DOMAIN); - edit.putString(Settings.PREF_FROMUSER, Settings.DEFAULT_FROMUSER); - edit.putString(Settings.PREF_PORT, "5061"); - edit.putString(Settings.PREF_PROTOCOL, "tcp"); - edit.putString(Settings.PREF_PASSWORD, password); - edit.commit(); - Receiver.engine(mContext).updateDNS(); - Receiver.engine(mContext).halt(); - Receiver.engine(mContext).StartEngine(); - dismiss(); - } - } - in.close(); - } catch (IOException e) { - if (!Sipdroid.release) e.printStackTrace(); - } - mHandler.sendEmptyMessage(0); - } - }).start(); - } - - EditText etName,etPass,etConfirm; - TextView tAdd; - Button buttonCancel,buttonOK; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.create_dialog); - setTitle("Create Free Account"); - buttonOK = (Button) findViewById(R.id.Button01); - buttonOK.setOnClickListener(new Button.OnClickListener() { - public void onClick(View v) { - CreateAccountNow(); - } - }); - buttonCancel = (Button) findViewById(R.id.Button02); - buttonCancel.setOnClickListener(new Button.OnClickListener() { - public void onClick(View v) { - dismiss(); - } - }); - etName = (EditText) findViewById(R.id.EditText01); - etName.setText(email.substring(0,email.indexOf("@"))); - tAdd = (TextView) findViewById(R.id.Text01); - tAdd.setText(email); - etPass = (EditText) findViewById(R.id.EditText02); - etConfirm = (EditText) findViewById(R.id.EditText03); - - TextView intro = (TextView) findViewById(R.id.intro); - TextView intro2 = (TextView) findViewById(R.id.email); - if (trunkserver != null) { - intro.setText("To save battery life by utilizing SIP over TCP protocol, a new PBXes account is being offered to you. It will be automatically linked to your existing "+trunkserver+" account, and therefore get the same password as your "+trunkserver+" account."); - TextView intro3 = (TextView) findViewById(R.id.password); - TextView intro4 = (TextView) findViewById(R.id.password_confirm); - etPass.setVisibility(View.GONE); - etConfirm.setVisibility(View.GONE); - etPass.setText(trunkpassword); - etConfirm.setText(trunkpassword); - intro3.setVisibility(View.GONE); - intro4.setVisibility(View.GONE); - intro2.setText("Email Address"); - } else { - intro.setText("A new PBXes account will be created. It will be linked to your existing Google Voice account, and therefore get the same password as your Google Voice account."); - intro2.setText("Google Voice Name"); - } - } - -} +/* + * Copyright (C) 2010 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package org.sipdroid.sipua.ui; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.List; +import java.util.Locale; +import java.util.Random; + +import org.sipdroid.sipua.R; +import org.sipdroid.sipua.RegisterAgent; +import org.sipdroid.sipua.SipdroidEngine; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.Dialog; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences.Editor; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.net.Uri; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.preference.PreferenceManager; +import android.text.format.Time; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +public class CreateAccount extends Dialog { + + Context mContext; + + public CreateAccount(Context context) { + super(context); + mContext = context; + } + + static String email,trunkserver,trunkuser,trunkpassword,trunkport; + + public static String isPossible(Context context) { + Boolean found = false; + email = trunkserver = null; + for (int i = 0; i < SipdroidEngine.LINES; i++) { + String j = (i!=0?""+i:""); + String username = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_USERNAME+j, Settings.DEFAULT_USERNAME), + server = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_SERVER+j, Settings.DEFAULT_SERVER); + if (username.equals("") || server.equals("")) + continue; + if (server.contains("pbxes")) + found = true; + else if (i == 0 && + !PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PROTOCOL+j, Settings.DEFAULT_PROTOCOL).equals("tcp") && + PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_3G+j, Settings.DEFAULT_3G) && + Receiver.engine(context).isRegistered(i) && + Receiver.engine(context).ras[i].CurrentState == RegisterAgent.REGISTERED) { + trunkserver = server; + trunkuser = username; + trunkpassword = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PASSWORD+j, Settings.DEFAULT_PASSWORD); + trunkport = PreferenceManager.getDefaultSharedPreferences(context).getString(Settings.PREF_PORT+j, Settings.DEFAULT_PORT); + } + } + if (found) return null; + Account[] accounts = AccountManager.get(context).getAccountsByType("com.google"); + for (Account account : accounts) { + email = account.name; + break; + } + if (email == null) return null; + Intent intent = new Intent(Intent.ACTION_SENDTO); + intent.setPackage("com.google.android.apps.googlevoice"); + intent.setData(Uri.fromParts("smsto", "", null)); + List a = context.getPackageManager().queryIntentActivities(intent,PackageManager.GET_INTENT_FILTERS); + if (a != null && a.size() != 0) { + trunkserver = null; + return context.getString(R.string.menu_create); + } + if (trunkserver != null) + return "New PBX linked to "+trunkserver; + return null; + } + + String line; + + Handler mHandler = new Handler() { + public void handleMessage(Message msg) { + Toast.makeText(mContext, line, Toast.LENGTH_LONG).show(); + buttonCancel.setEnabled(true); + buttonOK.setEnabled(true); + setCancelable(true); + } + }; + + String generatePassword(int length) + { + String availableCharacters = ""; + String password = ""; + + // Generate the appropriate character set + availableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + availableCharacters = availableCharacters + "0123456789"; + + // Generate the random number generator + Random selector = new Random(); + + // Generate the password + int i; + for(i = 0; i < length; i++) + { + password = password + availableCharacters.charAt(selector.nextInt(availableCharacters.length() - 1)); + } + + return password; + } + + void CreateAccountNow() { + buttonCancel.setEnabled(false); + buttonOK.setEnabled(false); + setCancelable(false); + Toast.makeText(mContext, "Please stand by while your account is being created", Toast.LENGTH_LONG).show(); + (new Thread() { + public void run() { + line = "Can't connect to webserver"; + try { + String password = generatePassword(8); + String language = Locale.getDefault().toString().substring(0,2); + if (!language.equals("de") && !language.equals("es") && !language.equals("fr") && + !language.equals("it") && !language.equals("ru")) + if (language.equals("ja")) + language = "jp"; + else if (language.equals("zh")) + language = "cn"; + else + language = "en"; + String s = "https://www1.pbxes.com/config.php?m=register&a=update&f=action&username="+Uri.encode(etName.getText().toString())+"&password=" + +Uri.encode(etPass.getText().toString())+"&password_confirm="+Uri.encode(etConfirm.getText().toString())+"&language="+language+"&email="+Uri.encode(email)+"&land="+Uri.encode(Time.getCurrentTimezone())+ + "&sipdroid="+Uri.encode(password); + if (trunkserver != null) { + s = s+"&trunkserver="+Uri.encode(trunkserver+":"+trunkport)+ + "&trunkuser="+Uri.encode(trunkuser); + } + URL url = new URL(s); + BufferedReader in; + in = new BufferedReader(new InputStreamReader(url.openStream())); + line = in.readLine(); + if (line == null) { + in = new BufferedReader(new InputStreamReader(url.openStream())); + line = in.readLine(); + } + if (line != null) { + if (line.equals("OK")) { + Editor edit = PreferenceManager.getDefaultSharedPreferences(mContext).edit(); + edit.putString(Settings.PREF_SERVER, Settings.DEFAULT_SERVER); + edit.putString(Settings.PREF_USERNAME, etName.getText()+"-200"); + edit.putString(Settings.PREF_DOMAIN, Settings.DEFAULT_DOMAIN); + edit.putString(Settings.PREF_FROMUSER, Settings.DEFAULT_FROMUSER); + edit.putString(Settings.PREF_PORT, "5061"); + edit.putString(Settings.PREF_PROTOCOL, "tcp"); + edit.putString(Settings.PREF_PASSWORD, password); + edit.commit(); + Receiver.engine(mContext).updateDNS(); + Receiver.engine(mContext).halt(); + Receiver.engine(mContext).StartEngine(); + dismiss(); + } + } + in.close(); + } catch (IOException e) { + if (!Sipdroid.release) e.printStackTrace(); + } + mHandler.sendEmptyMessage(0); + } + }).start(); + } + + EditText etName,etPass,etConfirm; + TextView tAdd; + Button buttonCancel,buttonOK; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.create_dialog); + setTitle("Create Free Account"); + buttonOK = (Button) findViewById(R.id.Button01); + buttonOK.setOnClickListener(new Button.OnClickListener() { + public void onClick(View v) { + CreateAccountNow(); + } + }); + buttonCancel = (Button) findViewById(R.id.Button02); + buttonCancel.setOnClickListener(new Button.OnClickListener() { + public void onClick(View v) { + dismiss(); + } + }); + etName = (EditText) findViewById(R.id.EditText01); + etName.setText(email.substring(0,email.indexOf("@"))); + tAdd = (TextView) findViewById(R.id.Text01); + tAdd.setText(email); + etPass = (EditText) findViewById(R.id.EditText02); + etConfirm = (EditText) findViewById(R.id.EditText03); + + TextView intro = (TextView) findViewById(R.id.intro); + TextView intro2 = (TextView) findViewById(R.id.email); + if (trunkserver != null) { + intro.setText("To save battery life by utilizing SIP over TCP protocol, a new PBXes account is being offered to you. It will be automatically linked to your existing "+trunkserver+" account, and therefore get the same password as your "+trunkserver+" account."); + TextView intro3 = (TextView) findViewById(R.id.password); + TextView intro4 = (TextView) findViewById(R.id.password_confirm); + etPass.setVisibility(View.GONE); + etConfirm.setVisibility(View.GONE); + etPass.setText(trunkpassword); + etConfirm.setText(trunkpassword); + intro3.setVisibility(View.GONE); + intro4.setVisibility(View.GONE); + intro2.setText("Email Address"); + } else { + intro.setText("A new PBXes account will be created. It will be linked to your existing Google Voice account, and therefore get the same password as your Google Voice account."); + intro2.setText("Google Voice Name"); + } + } + +} diff --git a/src/org/sipdroid/sipua/ui/InCallScreen.java b/app/src/main/java/org/sipdroid/sipua/ui/InCallScreen.java similarity index 88% rename from src/org/sipdroid/sipua/ui/InCallScreen.java rename to app/src/main/java/org/sipdroid/sipua/ui/InCallScreen.java index 8d5b20a..ef19141 100644 --- a/src/org/sipdroid/sipua/ui/InCallScreen.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/InCallScreen.java @@ -1,615 +1,563 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import java.util.HashMap; -import org.sipdroid.media.RtpStreamReceiver; -import org.sipdroid.media.RtpStreamSender; -import org.sipdroid.sipua.R; -import org.sipdroid.sipua.UserAgent; -import org.sipdroid.sipua.phone.Call; -import org.sipdroid.sipua.phone.CallCard; -import org.sipdroid.sipua.phone.Phone; -import org.sipdroid.sipua.phone.SlidingCardManager; -import android.annotation.SuppressLint; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.content.res.Configuration; -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; -import android.hardware.SensorManager; -import android.media.AudioManager; -import android.media.ToneGenerator; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.os.SystemClock; -import android.preference.PreferenceManager; -import android.provider.Settings; -import android.util.Log; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; -import android.widget.EditText; -import android.widget.RelativeLayout; -import android.widget.SlidingDrawer; -import android.widget.TextView; - -public class InCallScreen extends CallScreen implements View.OnClickListener, SensorEventListener { - - final int MSG_ANSWER = 1; - final int MSG_ANSWER_SPEAKER = 2; - final int MSG_BACK = 3; - final int MSG_TICK = 4; - final int MSG_POPUP = 5; - final int MSG_ACCEPT = 6; - final int MSG_ACCEPT_FORCE = 7; - - final int SCREEN_OFF_TIMEOUT = 12000; - - CallCard mCallCard; - Phone ccPhone; - int oldtimeout; - SensorManager sensorManager; - Sensor proximitySensor; - boolean first; - - void screenOff(boolean off) { - ContentResolver cr = getContentResolver(); - - if (proximitySensor != null) - return; - if (off) { - if (oldtimeout == 0) { - oldtimeout = Settings.System.getInt(cr, Settings.System.SCREEN_OFF_TIMEOUT, 60000); - Settings.System.putInt(cr, Settings.System.SCREEN_OFF_TIMEOUT, SCREEN_OFF_TIMEOUT); - } - } else { - if (oldtimeout == 0 && Settings.System.getInt(cr, Settings.System.SCREEN_OFF_TIMEOUT, 60000) == SCREEN_OFF_TIMEOUT) - oldtimeout = 60000; - if (oldtimeout != 0) { - Settings.System.putInt(cr, Settings.System.SCREEN_OFF_TIMEOUT, oldtimeout); - oldtimeout = 0; - } - } - } - - @Override - public void onStop() { - super.onStop(); - mHandler.removeMessages(MSG_BACK); - mHandler.removeMessages(MSG_ACCEPT); - mHandler.sendEmptyMessageDelayed(MSG_ACCEPT_FORCE, 1000); - if (Receiver.call_state == UserAgent.UA_STATE_IDLE) - finish(); - sensorManager.unregisterListener(this); - started = false; - } - - @Override - public void onStart() { - super.onStart(); - mHandler.removeMessages(MSG_ACCEPT_FORCE); - if (Receiver.call_state == UserAgent.UA_STATE_IDLE) - mHandler.sendEmptyMessageDelayed(MSG_BACK, Receiver.call_end_reason == -1? - 2000:5000); - first = true; - pactive = false; - pactivetime = SystemClock.elapsedRealtime(); - sensorManager.registerListener(this,proximitySensor,SensorManager.SENSOR_DELAY_NORMAL); - started = true; - Receiver.progress(); - } - - @Override - public void onPause() { - super.onPause(); - if (!Sipdroid.release) Log.i("SipUA:","on pause"); - switch (Receiver.call_state) { - case UserAgent.UA_STATE_INCOMING_CALL: -// if (!RtpStreamReceiver.isBluetoothAvailable()) Receiver.moveTop(); - break; - case UserAgent.UA_STATE_IDLE: - if (Receiver.ccCall != null) - mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); - mHandler.sendEmptyMessageDelayed(MSG_BACK, Receiver.call_end_reason == -1? - 2000:5000); - break; - } - if (t != null) { - running = false; - t.interrupt(); - } - screenOff(false); - if (mCallCard.mElapsedTime != null) mCallCard.mElapsedTime.stop(); - } - - void moveBack() { - if (Receiver.ccConn != null && !Receiver.ccConn.isIncoming() && Integer.parseInt(Build.VERSION.SDK) < 25) { - // after an outgoing call don't fall back to the contact - // or call log because it is too easy to dial accidentally from there - startActivity(Receiver.createHomeIntent()); - } - onStop(); - } - - Context mContext = this; - - @Override - public void onResume() { - super.onResume(); - if (!Sipdroid.release) Log.i("SipUA:","on resume"); - switch (Receiver.call_state) { - case UserAgent.UA_STATE_INCOMING_CALL: - if (Receiver.pstn_state == null || Receiver.pstn_state.equals("IDLE")) - if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ON, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ON) && - !mKeyguardManager.inKeyguardRestrictedInputMode()) - mHandler.sendEmptyMessageDelayed(MSG_ANSWER, 1000); - else if ((PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ONDEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ONDEMAND) && - PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_DEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_DEMAND)) || - (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_HEADSET, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_HEADSET) && - Receiver.headset > 0)) - mHandler.sendEmptyMessageDelayed(MSG_ANSWER_SPEAKER, 10000); - break; - case UserAgent.UA_STATE_INCALL: - mDialerDrawer.close(); - mDialerDrawer.setVisibility(View.VISIBLE); - if (Receiver.docked <= 0) - screenOff(true); - break; - case UserAgent.UA_STATE_IDLE: - if (!mHandler.hasMessages(MSG_BACK)) - moveBack(); - break; - } - if (Receiver.call_state != UserAgent.UA_STATE_INCALL) { - mDialerDrawer.close(); - mDialerDrawer.setVisibility(View.GONE); - } - if (Receiver.ccCall != null) mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); - if (mSlidingCardManager != null) mSlidingCardManager.showPopup(); - mHandler.sendEmptyMessage(MSG_TICK); - mHandler.sendEmptyMessage(MSG_POPUP); - if (t == null && Receiver.call_state != UserAgent.UA_STATE_IDLE) { - mDigits.setText(""); - running = true; - (t = new Thread() { - public void run() { - int len = 0; - long time; - ToneGenerator tg = null; - - if (Settings.System.getInt(getContentResolver(), - Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1) - tg = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, (int)(ToneGenerator.MAX_VOLUME*2*org.sipdroid.sipua.ui.Settings.getEarGain())); - for (;;) { - if (!running) { - t = null; - break; - } - if (len != mDigits.getText().length()) { - time = SystemClock.elapsedRealtime(); - if (tg != null) tg.startTone(mToneMap.get(mDigits.getText().charAt(len))); - Receiver.engine(Receiver.mContext).info(mDigits.getText().charAt(len++),250); - time = 250-(SystemClock.elapsedRealtime()-time); - try { - if (time > 0) sleep(time); - } catch (InterruptedException e) { - } - if (tg != null) tg.stopTone(); - try { - if (running) sleep(250); - } catch (InterruptedException e) { - } - continue; - } - mHandler.sendEmptyMessage(MSG_TICK); - try { - sleep(1000); - } catch (InterruptedException e) { - } - } - if (tg != null) tg.release(); - } - }).start(); - } - } - - Handler mHandler = new Handler() { - @SuppressLint("NewApi") - public void handleMessage(Message msg) { - switch (msg.what) { - case MSG_ANSWER: - if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL) - answer(); - break; - case MSG_ANSWER_SPEAKER: - if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL) { - answer(); - Receiver.engine(mContext).speaker(AudioManager.MODE_NORMAL); - } - break; - case MSG_BACK: - moveBack(); - break; - case MSG_TICK: - mCodec.setText(RtpStreamReceiver.getCodec()); - if (RtpStreamReceiver.good != 0) { - if (RtpStreamReceiver.timeout != 0) - mStats.setText("no data"); - else if (RtpStreamSender.m > 1) - mStats.setText(Math.round(RtpStreamReceiver.loss/RtpStreamReceiver.good*100)+"%loss, "+ - Math.round(RtpStreamReceiver.lost/RtpStreamReceiver.good*100)+"%lost, "+ - Math.round(RtpStreamReceiver.late/RtpStreamReceiver.good*100)+"%late (>"+ - (RtpStreamReceiver.jitter-250*RtpStreamReceiver.mu)/8/RtpStreamReceiver.mu+"ms)"); - else - mStats.setText(Math.round(RtpStreamReceiver.lost/RtpStreamReceiver.good*100)+"%lost, "+ - Math.round(RtpStreamReceiver.late/RtpStreamReceiver.good*100)+"%late (>"+ - (RtpStreamReceiver.jitter-250*RtpStreamReceiver.mu)/8/RtpStreamReceiver.mu+"ms)"); - mStats.setVisibility(View.VISIBLE); - } else - mStats.setVisibility(View.GONE); - break; - case MSG_POPUP: - if (mSlidingCardManager != null) mSlidingCardManager.showPopup(); - break; - case MSG_ACCEPT: - case MSG_ACCEPT_FORCE: - setScreenBacklight((float) -1); - getWindow().setFlags(0, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - if (mDialerDrawer != null) { - mDialerDrawer.close(); - mDialerDrawer.setVisibility(View.VISIBLE); - } - ContentResolver cr = getContentResolver(); - if (hapticset && haptic != 0) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) - if (!Settings.System.canWrite(mContext)) - break; - Settings.System.putInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, haptic); - hapticset = false; - } - break; - } - } - }; - - ViewGroup mInCallPanel,mMainFrame; - SlidingDrawer mDialerDrawer; - public static SlidingCardManager mSlidingCardManager; - TextView mStats; - TextView mCodec; - - public void initInCallScreen() { - mInCallPanel = (ViewGroup) findViewById(R.id.inCallPanel); - mMainFrame = (ViewGroup) findViewById(R.id.mainFrame); - View callCardLayout = getLayoutInflater().inflate( - R.layout.call_card_popup, - mInCallPanel); - mCallCard = (CallCard) callCardLayout.findViewById(R.id.callCard); - mCallCard.reset(); - - mSlidingCardManager = new SlidingCardManager(); - mSlidingCardManager.init(ccPhone, this, mMainFrame); - SlidingCardManager.WindowAttachNotifierView wanv = - new SlidingCardManager.WindowAttachNotifierView(this); - wanv.setSlidingCardManager(mSlidingCardManager); - wanv.setVisibility(View.GONE); - RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(0, 0); - mMainFrame.addView(wanv, lp); - - mStats = (TextView) findViewById(R.id.stats); - mCodec = (TextView) findViewById(R.id.codec); - mDialerDrawer = (SlidingDrawer) findViewById(R.id.dialer_container); - mCallCard.displayOnHoldCallStatus(ccPhone,null); - mCallCard.displayOngoingCallStatus(ccPhone,null); - if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) - mCallCard.updateForLandscapeMode(); - - // Have the WindowManager filter out touch events that are "too fat". - getWindow().addFlags(WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES); - - mDigits = (EditText) findViewById(R.id.digits); - mDisplayMap.put(R.id.one, '1'); - mDisplayMap.put(R.id.two, '2'); - mDisplayMap.put(R.id.three, '3'); - mDisplayMap.put(R.id.four, '4'); - mDisplayMap.put(R.id.five, '5'); - mDisplayMap.put(R.id.six, '6'); - mDisplayMap.put(R.id.seven, '7'); - mDisplayMap.put(R.id.eight, '8'); - mDisplayMap.put(R.id.nine, '9'); - mDisplayMap.put(R.id.zero, '0'); - mDisplayMap.put(R.id.pound, '#'); - mDisplayMap.put(R.id.star, '*'); - - mToneMap.put('1', ToneGenerator.TONE_DTMF_1); - mToneMap.put('2', ToneGenerator.TONE_DTMF_2); - mToneMap.put('3', ToneGenerator.TONE_DTMF_3); - mToneMap.put('4', ToneGenerator.TONE_DTMF_4); - mToneMap.put('5', ToneGenerator.TONE_DTMF_5); - mToneMap.put('6', ToneGenerator.TONE_DTMF_6); - mToneMap.put('7', ToneGenerator.TONE_DTMF_7); - mToneMap.put('8', ToneGenerator.TONE_DTMF_8); - mToneMap.put('9', ToneGenerator.TONE_DTMF_9); - mToneMap.put('0', ToneGenerator.TONE_DTMF_0); - mToneMap.put('#', ToneGenerator.TONE_DTMF_P); - mToneMap.put('*', ToneGenerator.TONE_DTMF_S); - - View button; - for (int viewId : mDisplayMap.keySet()) { - button = findViewById(viewId); - button.setOnClickListener(this); - } - } - - Thread t; - EditText mDigits; - boolean running; - public static boolean started; - private static final HashMap mDisplayMap = - new HashMap(); - private static final HashMap mToneMap = - new HashMap(); - - public void onClick(View v) { - int viewId = v.getId(); - - // if the button is recognized - if (mDisplayMap.containsKey(viewId)) { - appendDigit(mDisplayMap.get(viewId)); - } - } - - void appendDigit(final char c) { - mDigits.getText().append(c); - } - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - -// if (Integer.parseInt(Build.VERSION.SDK) >= 26) -// requestWindowFeature(Window.FEATURE_NO_TITLE); - getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); - setContentView(R.layout.incall); - - initInCallScreen(); - - sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); - proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); - - if(!android.os.Build.BRAND.equalsIgnoreCase("archos")) - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); - } - - public void reject() { - if (Receiver.ccCall != null) { - Receiver.stopRingtone(); - Receiver.ccCall.setState(Call.State.DISCONNECTED); - mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); - mDialerDrawer.close(); - mDialerDrawer.setVisibility(View.GONE); - if (mSlidingCardManager != null) - mSlidingCardManager.showPopup(); - } - (new Thread() { - public void run() { - Receiver.engine(mContext).rejectcall(); - } - }).start(); - } - - public void answer() { - (new Thread() { - public void run() { - Receiver.engine(mContext).answercall(); - } - }).start(); - if (Receiver.ccCall != null) { - Receiver.ccCall.setState(Call.State.ACTIVE); - Receiver.ccCall.base = SystemClock.elapsedRealtime(); - mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); - mDialerDrawer.setVisibility(View.VISIBLE); - if (mSlidingCardManager != null) - mSlidingCardManager.showPopup(); - } - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_MENU: - if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL && mSlidingCardManager == null) { - answer(); - return true; - } - break; - - case KeyEvent.KEYCODE_CALL: - switch (Receiver.call_state) { - case UserAgent.UA_STATE_INCOMING_CALL: - answer(); - break; - case UserAgent.UA_STATE_INCALL: - case UserAgent.UA_STATE_HOLD: - Receiver.engine(this).togglehold(); - break; - } - // consume KEYCODE_CALL so PhoneWindow doesn't do anything with it - return true; - - case KeyEvent.KEYCODE_BACK: - if (mDialerDrawer.isOpened()) - mDialerDrawer.animateClose(); - return true; - - case KeyEvent.KEYCODE_CAMERA: - // Disable the CAMERA button while in-call since it's too - // easy to press accidentally. - return true; - - case KeyEvent.KEYCODE_VOLUME_DOWN: - case KeyEvent.KEYCODE_VOLUME_UP: - if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL) { - Receiver.stopRingtone(); - return true; - } - RtpStreamReceiver.adjust(keyCode,true,!(pactive || SystemClock.elapsedRealtime()-pactivetime < 1000)); - return true; - } - if (Receiver.call_state == UserAgent.UA_STATE_INCALL) { - char number = event.getNumber(); - if (Character.isDigit(number) || number == '*' || number == '#') { - appendDigit(number); - return true; - } - } - return super.onKeyDown(keyCode, event); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - boolean result = super.onCreateOptionsMenu(menu); - - MenuItem m = menu.add(0, DTMF_MENU_ITEM, 0, R.string.menu_dtmf); - m.setIcon(R.drawable.ic_menu_dial_pad); - return result; - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - boolean result = super.onPrepareOptionsMenu(menu); - - menu.findItem(DTMF_MENU_ITEM).setVisible(Receiver.call_state == UserAgent.UA_STATE_INCALL); - if (pactive || SystemClock.elapsedRealtime()-pactivetime < 1000) { - menu.findItem(HOLD_MENU_ITEM).setVisible(false); - menu.findItem(MUTE_MENU_ITEM).setVisible(false); - menu.findItem(VIDEO_MENU_ITEM).setVisible(false); - menu.findItem(TRANSFER_MENU_ITEM).setVisible(false); - menu.findItem(BLUETOOTH_MENU_ITEM).setVisible(false); - menu.findItem(SPEAKER_MENU_ITEM).setVisible(false); - menu.findItem(ANSWER_MENU_ITEM).setVisible(false); - menu.findItem(DTMF_MENU_ITEM).setVisible(false); - } - return result; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case DTMF_MENU_ITEM: - mDialerDrawer.animateOpen(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - @Override - public boolean onKeyUp(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_VOLUME_DOWN: - case KeyEvent.KEYCODE_VOLUME_UP: - RtpStreamReceiver.adjust(keyCode,false,!(pactive || SystemClock.elapsedRealtime()-pactivetime < 1000)); - return true; - case KeyEvent.KEYCODE_ENDCALL: - if (Receiver.pstn_state == null || - (Receiver.pstn_state.equals("IDLE") && (SystemClock.elapsedRealtime()-Receiver.pstn_time) > 3000)) { - reject(); - return true; - } - break; - } - Receiver.pstn_time = 0; - return false; - } - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - } - - void setScreenBacklight(float a) { - WindowManager.LayoutParams lp = getWindow().getAttributes(); - lp.screenBrightness = a; - getWindow().setAttributes(lp); - } - - static final float PROXIMITY_THRESHOLD = 5.0f; - public static boolean pactive; - public static long pactivetime; - static int haptic; - static boolean hapticset; - - @SuppressLint("NewApi") - @Override - public void onSensorChanged(SensorEvent event) { - boolean keepon = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_KEEPON, org.sipdroid.sipua.ui.Settings.DEFAULT_KEEPON); - if (first) { - first = false; - return; - } - float distance = event.values[0]; - boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < event.sensor.getMaximumRange()); - if (!keepon || - Receiver.call_state == UserAgent.UA_STATE_HOLD || Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL - || RtpStreamReceiver.speakermode == AudioManager.MODE_NORMAL || Receiver.headset > 0 || Receiver.docked > 0) - active = false; - pactive = active; - pactivetime = SystemClock.elapsedRealtime(); - if (!active) { - mHandler.sendEmptyMessageDelayed(MSG_ACCEPT, 1000); - return; - } - mHandler.removeMessages(MSG_ACCEPT); - setScreenBacklight((float) 0.1); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - closeOptionsMenu(); - mDialerDrawer.close(); - mDialerDrawer.setVisibility(View.GONE); - ContentResolver cr = getContentResolver(); - if (!hapticset) { - haptic = Settings.System.getInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, 1); - hapticset = true; - } - if (haptic == 0) - return; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) - if (!Settings.System.canWrite(mContext)) { - Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); - intent.setData(Uri.parse("package:org.sipdroid.sipua")); - startActivity(intent); - return; - } - Settings.System.putInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, 0); - } -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import java.util.HashMap; +import org.sipdroid.media.RtpStreamReceiver; +import org.sipdroid.media.RtpStreamSender; +import org.sipdroid.sipua.R; +import org.sipdroid.sipua.UserAgent; +import org.sipdroid.sipua.phone.Call; +import org.sipdroid.sipua.phone.CallCard; +import org.sipdroid.sipua.phone.Phone; +import org.sipdroid.sipua.phone.SlidingCardManager; +import android.annotation.SuppressLint; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.res.Configuration; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.media.AudioManager; +import android.media.ToneGenerator; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.SystemClock; +import android.preference.PreferenceManager; +import android.provider.Settings; +import android.util.Log; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.EditText; +import android.widget.RelativeLayout; +import android.widget.SlidingDrawer; +import android.widget.TextView; + +public class InCallScreen extends CallScreen implements View.OnClickListener, SensorEventListener { + + final int MSG_ANSWER = 1; + final int MSG_ANSWER_SPEAKER = 2; + final int MSG_BACK = 3; + final int MSG_TICK = 4; + final int MSG_POPUP = 5; + final int MSG_ACCEPT = 6; + final int MSG_ACCEPT_FORCE = 7; + + CallCard mCallCard; + Phone ccPhone; + SensorManager sensorManager; + Sensor proximitySensor; + boolean first; + + @Override + public void onStop() { + super.onStop(); + mHandler.removeMessages(MSG_BACK); + mHandler.removeMessages(MSG_ACCEPT); + mHandler.sendEmptyMessageDelayed(MSG_ACCEPT_FORCE, 1000); + if (Receiver.call_state == UserAgent.UA_STATE_IDLE) + finish(); + sensorManager.unregisterListener(this); + started = false; + } + + @Override + public void onStart() { + super.onStart(); + mHandler.removeMessages(MSG_ACCEPT_FORCE); + if (Receiver.call_state == UserAgent.UA_STATE_IDLE) + mHandler.sendEmptyMessageDelayed(MSG_BACK, Receiver.call_end_reason == -1? + 2000:5000); + first = true; + pactive = false; + pactivetime = SystemClock.elapsedRealtime(); + sensorManager.registerListener(this,proximitySensor,SensorManager.SENSOR_DELAY_NORMAL); + started = true; + Receiver.progress(); + } + + @Override + public void onPause() { + super.onPause(); + if (!Sipdroid.release) Log.i("SipUA:","on pause"); + switch (Receiver.call_state) { + case UserAgent.UA_STATE_INCOMING_CALL: +// if (!RtpStreamReceiver.isBluetoothAvailable()) Receiver.moveTop(); + break; + case UserAgent.UA_STATE_IDLE: + if (Receiver.ccCall != null) + mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); + mHandler.sendEmptyMessageDelayed(MSG_BACK, Receiver.call_end_reason == -1? + 2000:5000); + break; + } + if (t != null) { + running = false; + t.interrupt(); + } + if (mCallCard.mElapsedTime != null) mCallCard.mElapsedTime.stop(); + } + + void moveBack() { + if (Receiver.ccConn != null && !Receiver.ccConn.isIncoming() && Integer.parseInt(Build.VERSION.SDK) < 25) { + // after an outgoing call don't fall back to the contact + // or call log because it is too easy to dial accidentally from there + startActivity(Receiver.createHomeIntent()); + } + onStop(); + } + + Context mContext = this; + + @Override + public void onResume() { + super.onResume(); + if (!Sipdroid.release) Log.i("SipUA:","on resume"); + switch (Receiver.call_state) { + case UserAgent.UA_STATE_INCOMING_CALL: + if (Receiver.pstn_state == null || Receiver.pstn_state.equals("IDLE")) + if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ON, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ON) && + !mKeyguardManager.inKeyguardRestrictedInputMode()) + mHandler.sendEmptyMessageDelayed(MSG_ANSWER, 1000); + else if ((PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ONDEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ONDEMAND) && + PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_DEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_DEMAND)) || + (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_HEADSET, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_HEADSET) && + Receiver.headset > 0)) + mHandler.sendEmptyMessageDelayed(MSG_ANSWER_SPEAKER, 10000); + break; + case UserAgent.UA_STATE_INCALL: + mDialerDrawer.close(); + mDialerDrawer.setVisibility(View.VISIBLE); + break; + case UserAgent.UA_STATE_IDLE: + if (!mHandler.hasMessages(MSG_BACK)) + moveBack(); + break; + } + if (Receiver.call_state != UserAgent.UA_STATE_INCALL) { + mDialerDrawer.close(); + mDialerDrawer.setVisibility(View.GONE); + } + if (Receiver.ccCall != null) mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); + if (mSlidingCardManager != null) mSlidingCardManager.showPopup(); + mHandler.sendEmptyMessage(MSG_TICK); + mHandler.sendEmptyMessage(MSG_POPUP); + if (t == null && Receiver.call_state != UserAgent.UA_STATE_IDLE) { + mDigits.setText(""); + running = true; + (t = new Thread() { + public void run() { + int len = 0; + long time; + ToneGenerator tg = null; + + if (Settings.System.getInt(getContentResolver(), + Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1) + tg = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, (int)(ToneGenerator.MAX_VOLUME*2*org.sipdroid.sipua.ui.Settings.getEarGain())); + for (;;) { + if (!running) { + t = null; + break; + } + if (len != mDigits.getText().length()) { + time = SystemClock.elapsedRealtime(); + if (tg != null) tg.startTone(mToneMap.get(mDigits.getText().charAt(len))); + Receiver.engine(Receiver.mContext).info(mDigits.getText().charAt(len++),250); + time = 250-(SystemClock.elapsedRealtime()-time); + try { + if (time > 0) sleep(time); + } catch (InterruptedException e) { + } + if (tg != null) tg.stopTone(); + try { + if (running) sleep(250); + } catch (InterruptedException e) { + } + continue; + } + mHandler.sendEmptyMessage(MSG_TICK); + try { + sleep(1000); + } catch (InterruptedException e) { + } + } + if (tg != null) tg.release(); + } + }).start(); + } + } + + Handler mHandler = new Handler() { + @SuppressLint("NewApi") + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_ANSWER: + if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL) + answer(); + break; + case MSG_ANSWER_SPEAKER: + if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL) { + answer(); + Receiver.engine(mContext).speaker(AudioManager.MODE_NORMAL); + } + break; + case MSG_BACK: + moveBack(); + break; + case MSG_TICK: + mCodec.setText(RtpStreamReceiver.getCodec()); + if (RtpStreamReceiver.good != 0) { + if (RtpStreamReceiver.timeout != 0) + mStats.setText("no data"); + else if (RtpStreamSender.m > 1) + mStats.setText(Math.round(RtpStreamReceiver.loss/RtpStreamReceiver.good*100)+"%loss, "+ + Math.round(RtpStreamReceiver.lost/RtpStreamReceiver.good*100)+"%lost, "+ + Math.round(RtpStreamReceiver.late/RtpStreamReceiver.good*100)+"%late (>"+ + (RtpStreamReceiver.jitter-250*RtpStreamReceiver.mu)/8/RtpStreamReceiver.mu+"ms)"); + else + mStats.setText(Math.round(RtpStreamReceiver.lost/RtpStreamReceiver.good*100)+"%lost, "+ + Math.round(RtpStreamReceiver.late/RtpStreamReceiver.good*100)+"%late (>"+ + (RtpStreamReceiver.jitter-250*RtpStreamReceiver.mu)/8/RtpStreamReceiver.mu+"ms)"); + mStats.setVisibility(View.VISIBLE); + } else + mStats.setVisibility(View.GONE); + break; + case MSG_POPUP: + if (mSlidingCardManager != null) mSlidingCardManager.showPopup(); + break; + case MSG_ACCEPT: + case MSG_ACCEPT_FORCE: + setScreenBacklight((float) -1); + getWindow().setFlags(0, + WindowManager.LayoutParams.FLAG_FULLSCREEN); + if (mDialerDrawer != null) { + mDialerDrawer.close(); + mDialerDrawer.setVisibility(View.VISIBLE); + } + } + } + }; + + ViewGroup mInCallPanel,mMainFrame; + SlidingDrawer mDialerDrawer; + public static SlidingCardManager mSlidingCardManager; + TextView mStats; + TextView mCodec; + + public void initInCallScreen() { + mInCallPanel = (ViewGroup) findViewById(R.id.inCallPanel); + mMainFrame = (ViewGroup) findViewById(R.id.mainFrame); + View callCardLayout = getLayoutInflater().inflate( + R.layout.call_card_popup, + mInCallPanel); + mCallCard = (CallCard) callCardLayout.findViewById(R.id.callCard); + mCallCard.reset(); + + mSlidingCardManager = new SlidingCardManager(); + mSlidingCardManager.init(ccPhone, this, mMainFrame); + SlidingCardManager.WindowAttachNotifierView wanv = + new SlidingCardManager.WindowAttachNotifierView(this); + wanv.setSlidingCardManager(mSlidingCardManager); + wanv.setVisibility(View.GONE); + RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(0, 0); + mMainFrame.addView(wanv, lp); + + mStats = (TextView) findViewById(R.id.stats); + mCodec = (TextView) findViewById(R.id.codec); + mDialerDrawer = (SlidingDrawer) findViewById(R.id.dialer_container); + mCallCard.displayOnHoldCallStatus(ccPhone,null); + mCallCard.displayOngoingCallStatus(ccPhone,null); + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) + mCallCard.updateForLandscapeMode(); + + // Have the WindowManager filter out touch events that are "too fat". + getWindow().addFlags(WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES); + + mDigits = (EditText) findViewById(R.id.digits); + mDisplayMap.put(R.id.one, '1'); + mDisplayMap.put(R.id.two, '2'); + mDisplayMap.put(R.id.three, '3'); + mDisplayMap.put(R.id.four, '4'); + mDisplayMap.put(R.id.five, '5'); + mDisplayMap.put(R.id.six, '6'); + mDisplayMap.put(R.id.seven, '7'); + mDisplayMap.put(R.id.eight, '8'); + mDisplayMap.put(R.id.nine, '9'); + mDisplayMap.put(R.id.zero, '0'); + mDisplayMap.put(R.id.pound, '#'); + mDisplayMap.put(R.id.star, '*'); + + mToneMap.put('1', ToneGenerator.TONE_DTMF_1); + mToneMap.put('2', ToneGenerator.TONE_DTMF_2); + mToneMap.put('3', ToneGenerator.TONE_DTMF_3); + mToneMap.put('4', ToneGenerator.TONE_DTMF_4); + mToneMap.put('5', ToneGenerator.TONE_DTMF_5); + mToneMap.put('6', ToneGenerator.TONE_DTMF_6); + mToneMap.put('7', ToneGenerator.TONE_DTMF_7); + mToneMap.put('8', ToneGenerator.TONE_DTMF_8); + mToneMap.put('9', ToneGenerator.TONE_DTMF_9); + mToneMap.put('0', ToneGenerator.TONE_DTMF_0); + mToneMap.put('#', ToneGenerator.TONE_DTMF_P); + mToneMap.put('*', ToneGenerator.TONE_DTMF_S); + + View button; + for (int viewId : mDisplayMap.keySet()) { + button = findViewById(viewId); + button.setOnClickListener(this); + } + } + + Thread t; + EditText mDigits; + boolean running; + public static boolean started; + private static final HashMap mDisplayMap = + new HashMap(); + private static final HashMap mToneMap = + new HashMap(); + + public void onClick(View v) { + int viewId = v.getId(); + + // if the button is recognized + if (mDisplayMap.containsKey(viewId)) { + appendDigit(mDisplayMap.get(viewId)); + } + } + + void appendDigit(final char c) { + mDigits.getText().append(c); + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + +// if (Integer.parseInt(Build.VERSION.SDK) >= 26) +// requestWindowFeature(Window.FEATURE_NO_TITLE); + getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); + setContentView(R.layout.incall); + + initInCallScreen(); + + sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); + proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); + + if(!android.os.Build.BRAND.equalsIgnoreCase("archos")) + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); + } + + public void reject() { + if (Receiver.ccCall != null) { + Receiver.stopRingtone(); + Receiver.ccCall.setState(Call.State.DISCONNECTED); + mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); + mDialerDrawer.close(); + mDialerDrawer.setVisibility(View.GONE); + if (mSlidingCardManager != null) + mSlidingCardManager.showPopup(); + } + (new Thread() { + public void run() { + Receiver.engine(mContext).rejectcall(); + } + }).start(); + } + + public void answer() { + (new Thread() { + public void run() { + Receiver.engine(mContext).answercall(); + } + }).start(); + if (Receiver.ccCall != null) { + Receiver.ccCall.setState(Call.State.ACTIVE); + Receiver.ccCall.base = SystemClock.elapsedRealtime(); + mCallCard.displayMainCallStatus(ccPhone,Receiver.ccCall); + mDialerDrawer.setVisibility(View.VISIBLE); + if (mSlidingCardManager != null) + mSlidingCardManager.showPopup(); + } + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_MENU: + if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL && mSlidingCardManager == null) { + answer(); + return true; + } + break; + + case KeyEvent.KEYCODE_CALL: + switch (Receiver.call_state) { + case UserAgent.UA_STATE_INCOMING_CALL: + answer(); + break; + case UserAgent.UA_STATE_INCALL: + case UserAgent.UA_STATE_HOLD: + Receiver.engine(this).togglehold(); + break; + } + // consume KEYCODE_CALL so PhoneWindow doesn't do anything with it + return true; + + case KeyEvent.KEYCODE_BACK: + if (mDialerDrawer.isOpened()) + mDialerDrawer.animateClose(); + return true; + + case KeyEvent.KEYCODE_CAMERA: + // Disable the CAMERA button while in-call since it's too + // easy to press accidentally. + return true; + + case KeyEvent.KEYCODE_VOLUME_DOWN: + case KeyEvent.KEYCODE_VOLUME_UP: + if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL) { + Receiver.stopRingtone(); + return true; + } + RtpStreamReceiver.adjust(keyCode,true,!(pactive || SystemClock.elapsedRealtime()-pactivetime < 1000)); + return true; + } + if (Receiver.call_state == UserAgent.UA_STATE_INCALL) { + char number = event.getNumber(); + if (Character.isDigit(number) || number == '*' || number == '#') { + appendDigit(number); + return true; + } + } + return super.onKeyDown(keyCode, event); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + boolean result = super.onCreateOptionsMenu(menu); + + MenuItem m = menu.add(0, DTMF_MENU_ITEM, 0, R.string.menu_dtmf); + m.setIcon(R.drawable.ic_menu_dial_pad); + return result; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + boolean result = super.onPrepareOptionsMenu(menu); + + menu.findItem(DTMF_MENU_ITEM).setVisible(Receiver.call_state == UserAgent.UA_STATE_INCALL); + if (pactive || SystemClock.elapsedRealtime()-pactivetime < 1000) { + menu.findItem(HOLD_MENU_ITEM).setVisible(false); + menu.findItem(MUTE_MENU_ITEM).setVisible(false); + menu.findItem(VIDEO_MENU_ITEM).setVisible(false); + menu.findItem(TRANSFER_MENU_ITEM).setVisible(false); + menu.findItem(BLUETOOTH_MENU_ITEM).setVisible(false); + menu.findItem(SPEAKER_MENU_ITEM).setVisible(false); + menu.findItem(ANSWER_MENU_ITEM).setVisible(false); + menu.findItem(DTMF_MENU_ITEM).setVisible(false); + } + return result; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case DTMF_MENU_ITEM: + mDialerDrawer.animateOpen(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_DOWN: + case KeyEvent.KEYCODE_VOLUME_UP: + RtpStreamReceiver.adjust(keyCode,false,!(pactive || SystemClock.elapsedRealtime()-pactivetime < 1000)); + return true; + case KeyEvent.KEYCODE_ENDCALL: + if (Receiver.pstn_state == null || + (Receiver.pstn_state.equals("IDLE") && (SystemClock.elapsedRealtime()-Receiver.pstn_time) > 3000)) { + reject(); + return true; + } + break; + } + Receiver.pstn_time = 0; + return false; + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + } + + void setScreenBacklight(float a) { + WindowManager.LayoutParams lp = getWindow().getAttributes(); + lp.screenBrightness = a; + getWindow().setAttributes(lp); + } + + static final float PROXIMITY_THRESHOLD = 5.0f; + public static boolean pactive; + public static long pactivetime; + + @SuppressLint("NewApi") + @Override + public void onSensorChanged(SensorEvent event) { + boolean keepon = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_KEEPON, org.sipdroid.sipua.ui.Settings.DEFAULT_KEEPON); + if (first) { + first = false; + return; + } + float distance = event.values[0]; + boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < event.sensor.getMaximumRange()); + if (!keepon || + Receiver.call_state == UserAgent.UA_STATE_HOLD || Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL + || RtpStreamReceiver.speakermode == AudioManager.MODE_NORMAL || Receiver.headset > 0 || Receiver.docked > 0) + active = false; + pactive = active; + pactivetime = SystemClock.elapsedRealtime(); + if (!active) { + mHandler.sendEmptyMessageDelayed(MSG_ACCEPT, 1000); + return; + } + mHandler.removeMessages(MSG_ACCEPT); + setScreenBacklight((float) 0.1); + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN); + closeOptionsMenu(); + mDialerDrawer.close(); + mDialerDrawer.setVisibility(View.GONE); + } +} diff --git a/src/org/sipdroid/sipua/ui/InstantAutoCompleteTextView.java b/app/src/main/java/org/sipdroid/sipua/ui/InstantAutoCompleteTextView.java similarity index 97% rename from src/org/sipdroid/sipua/ui/InstantAutoCompleteTextView.java rename to app/src/main/java/org/sipdroid/sipua/ui/InstantAutoCompleteTextView.java index 22684b7..a2bf554 100644 --- a/src/org/sipdroid/sipua/ui/InstantAutoCompleteTextView.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/InstantAutoCompleteTextView.java @@ -1,43 +1,43 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import android.content.Context; -import android.util.AttributeSet; -import android.widget.AutoCompleteTextView; - -public class InstantAutoCompleteTextView extends AutoCompleteTextView { - public InstantAutoCompleteTextView(Context context, AttributeSet attrs) { - super(context, attrs); - } - - @Override - public void onWindowFocusChanged(boolean hasWindowFocus) { - super.onWindowFocusChanged(hasWindowFocus); -// if (hasWindowFocus && getAdapter() != null && getAdapter().getCount() > 0) -// showDropDown(); - } - - @Override - public boolean enoughToFilter() { - return true; - } -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.AutoCompleteTextView; + +public class InstantAutoCompleteTextView extends AutoCompleteTextView { + public InstantAutoCompleteTextView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + super.onWindowFocusChanged(hasWindowFocus); +// if (hasWindowFocus && getAdapter() != null && getAdapter().getCount() > 0) +// showDropDown(); + } + + @Override + public boolean enoughToFilter() { + return true; + } +} diff --git a/src/org/sipdroid/sipua/ui/LoopAlarm.java b/app/src/main/java/org/sipdroid/sipua/ui/LoopAlarm.java similarity index 97% rename from src/org/sipdroid/sipua/ui/LoopAlarm.java rename to app/src/main/java/org/sipdroid/sipua/ui/LoopAlarm.java index cec011d..1024f64 100644 --- a/src/org/sipdroid/sipua/ui/LoopAlarm.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/LoopAlarm.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -public class LoopAlarm extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - if (!Sipdroid.release) Log.i("SipUA:","alarm"); - Receiver.engine(context).keepAlive(); - } -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class LoopAlarm extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + if (!Sipdroid.release) Log.i("SipUA:","alarm"); + Receiver.engine(context).keepAlive(); + } +} diff --git a/src/org/sipdroid/sipua/ui/OneShotAlarm.java b/app/src/main/java/org/sipdroid/sipua/ui/OneShotAlarm.java similarity index 97% rename from src/org/sipdroid/sipua/ui/OneShotAlarm.java rename to app/src/main/java/org/sipdroid/sipua/ui/OneShotAlarm.java index 3b94275..c389d8d 100644 --- a/src/org/sipdroid/sipua/ui/OneShotAlarm.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/OneShotAlarm.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -public class OneShotAlarm extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - if (!Sipdroid.release) Log.i("SipUA:","alarm"); - Receiver.engine(context).expire(); - } -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class OneShotAlarm extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + if (!Sipdroid.release) Log.i("SipUA:","alarm"); + Receiver.engine(context).expire(); + } +} diff --git a/src/org/sipdroid/sipua/ui/OneShotAlarm2.java b/app/src/main/java/org/sipdroid/sipua/ui/OneShotAlarm2.java similarity index 97% rename from src/org/sipdroid/sipua/ui/OneShotAlarm2.java rename to app/src/main/java/org/sipdroid/sipua/ui/OneShotAlarm2.java index 15c577e..b655c14 100644 --- a/src/org/sipdroid/sipua/ui/OneShotAlarm2.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/OneShotAlarm2.java @@ -1,53 +1,53 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import org.sipdroid.sipua.SipdroidEngine; - -import android.annotation.TargetApi; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Build; -import android.preference.PreferenceManager; -import android.util.Log; - -public class OneShotAlarm2 extends BroadcastReceiver { - - @TargetApi(26) - @Override - public void onReceive(Context context, Intent intent) { - if (!Sipdroid.release) Log.i("SipUA:","alarm2"); - for (int i = 0; i < SipdroidEngine.LINES; i++) - if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_WLAN+(i!=0?i:""), Settings.DEFAULT_WLAN) || - PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_3G+(i!=0?i:""), Settings.DEFAULT_3G) || - PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_VPN+(i!=0?i:""), Settings.DEFAULT_VPN) || - PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_EDGE+(i!=0?i:""), Settings.DEFAULT_EDGE)) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(new Intent(context, RegisterService.class)); - } else { - context.startService(new Intent(context,RegisterService.class)); - } - return; - } - context.stopService(new Intent(context,RegisterService.class)); - } -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import org.sipdroid.sipua.SipdroidEngine; + +import android.annotation.TargetApi; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import android.preference.PreferenceManager; +import android.util.Log; + +public class OneShotAlarm2 extends BroadcastReceiver { + + @TargetApi(26) + @Override + public void onReceive(Context context, Intent intent) { + if (!Sipdroid.release) Log.i("SipUA:","alarm2"); + for (int i = 0; i < SipdroidEngine.LINES; i++) + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_WLAN+(i!=0?i:""), Settings.DEFAULT_WLAN) || + PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_3G+(i!=0?i:""), Settings.DEFAULT_3G) || + PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_VPN+(i!=0?i:""), Settings.DEFAULT_VPN) || + PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_EDGE+(i!=0?i:""), Settings.DEFAULT_EDGE)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(new Intent(context, RegisterService.class)); + } else { + context.startService(new Intent(context,RegisterService.class)); + } + return; + } + context.stopService(new Intent(context,RegisterService.class)); + } +} diff --git a/src/org/sipdroid/sipua/ui/PSTN.java b/app/src/main/java/org/sipdroid/sipua/ui/PSTN.java similarity index 97% rename from src/org/sipdroid/sipua/ui/PSTN.java rename to app/src/main/java/org/sipdroid/sipua/ui/PSTN.java index 129ab8b..3100395 100644 --- a/src/org/sipdroid/sipua/ui/PSTN.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/PSTN.java @@ -1,57 +1,57 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import android.app.Activity; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.preference.PreferenceManager; - -public class PSTN extends Activity { - - static void callPSTN(String uri) { - String number; - - if (uri.indexOf(":") >= 0) { - number = uri.substring(uri.indexOf(":")+1); - if (!number.equals("")) { - Intent intent = new Intent(Intent.ACTION_CALL, - Uri.fromParts("tel", Uri.decode(number)+ - (!PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_PSTN) ? "+" : ""), null)); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - Receiver.mContext.startActivity(intent); - } - } - } - - @Override - public void onCreate(Bundle saved) { - super.onCreate(saved); - Intent intent; - Uri uri; - if (Receiver.mContext == null) Receiver.mContext = this; - if ((intent = getIntent()) != null - && (uri = intent.getData()) != null) - callPSTN(uri.toString()); - finish(); - } -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.preference.PreferenceManager; + +public class PSTN extends Activity { + + static void callPSTN(String uri) { + String number; + + if (uri.indexOf(":") >= 0) { + number = uri.substring(uri.indexOf(":")+1); + if (!number.equals("")) { + Intent intent = new Intent(Intent.ACTION_CALL, + Uri.fromParts("tel", Uri.decode(number)+ + (!PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_PSTN) ? "+" : ""), null)); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Receiver.mContext.startActivity(intent); + } + } + } + + @Override + public void onCreate(Bundle saved) { + super.onCreate(saved); + Intent intent; + Uri uri; + if (Receiver.mContext == null) Receiver.mContext = this; + if ((intent = getIntent()) != null + && (uri = intent.getData()) != null) + callPSTN(uri.toString()); + finish(); + } +} diff --git a/src/org/sipdroid/sipua/ui/PhoneStart.java b/app/src/main/java/org/sipdroid/sipua/ui/PhoneStart.java similarity index 100% rename from src/org/sipdroid/sipua/ui/PhoneStart.java rename to app/src/main/java/org/sipdroid/sipua/ui/PhoneStart.java diff --git a/src/org/sipdroid/sipua/ui/Receiver.java b/app/src/main/java/org/sipdroid/sipua/ui/Receiver.java similarity index 96% rename from src/org/sipdroid/sipua/ui/Receiver.java rename to app/src/main/java/org/sipdroid/sipua/ui/Receiver.java index 97371eb..f6ff344 100644 --- a/src/org/sipdroid/sipua/ui/Receiver.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/Receiver.java @@ -1,793 +1,801 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.util.List; - -import android.Manifest; -import android.annotation.SuppressLint; -import android.annotation.TargetApi; -import android.app.AlarmManager; -import android.app.KeyguardManager; -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.ActivityNotFoundException; -import android.content.BroadcastReceiver; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences.Editor; -import android.content.pm.ActivityInfo; -import android.content.pm.PackageManager; -import android.location.Location; -import android.location.LocationManager; -import android.media.AudioManager; -import android.media.Ringtone; -import android.media.RingtoneManager; -import android.net.ConnectivityManager; -import android.net.Uri; -import android.net.NetworkInfo.DetailedState; -import android.net.wifi.ScanResult; -import android.net.wifi.SupplicantState; -import android.net.wifi.WifiConfiguration; -import android.net.wifi.WifiInfo; -import android.net.wifi.WifiManager; -import android.os.Build; -import android.os.Handler; -import android.os.Message; -import android.os.PowerManager; -import android.os.StrictMode; -import android.os.SystemClock; -import android.os.Vibrator; -import android.preference.PreferenceManager; -import android.provider.Settings; -import android.telephony.TelephonyManager; -import android.text.TextUtils; -import android.util.Log; -import android.widget.RemoteViews; - -import org.sipdroid.media.Bluetooth; -import org.sipdroid.media.RtpStreamReceiver; -import org.sipdroid.media.RtpStreamSender; -import org.sipdroid.sipua.*; -import org.sipdroid.sipua.phone.Call; -import org.sipdroid.sipua.phone.Connection; -import org.zoolu.sip.provider.SipProvider; - - public class Receiver extends BroadcastReceiver { - - final static String ACTION_PHONE_STATE_CHANGED = "android.intent.action.PHONE_STATE"; - final static String ACTION_SIGNAL_STRENGTH_CHANGED = "android.intent.action.SIG_STR"; - final static String ACTION_DATA_STATE_CHANGED = "android.intent.action.ANY_DATA_STATE"; - final static String ACTION_DOCK_EVENT = "android.intent.action.DOCK_EVENT"; - final static String EXTRA_DOCK_STATE = "android.intent.extra.DOCK_STATE"; - final static String ACTION_SCO_AUDIO_STATE_CHANGED = "android.media.SCO_AUDIO_STATE_CHANGED"; - final static String EXTRA_SCO_AUDIO_STATE = "android.media.extra.SCO_AUDIO_STATE"; - final static String PAUSE_ACTION = "com.android.music.musicservicecommand.pause"; - final static String TOGGLEPAUSE_ACTION = "com.android.music.musicservicecommand.togglepause"; - final static String ACTION_DEVICE_IDLE = "com.android.server.WifiManager.action.DEVICE_IDLE"; - final static String ACTION_VPN_CONNECTIVITY = "vpn.connectivity"; - final static String ACTION_EXTERNAL_APPLICATIONS_AVAILABLE = "android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE"; - final static String ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE = "android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE"; - final static String METADATA_DOCK_HOME = "android.dock_home"; - final static String CATEGORY_DESK_DOCK = "android.intent.category.DESK_DOCK"; - final static String CATEGORY_CAR_DOCK = "android.intent.category.CAR_DOCK"; - final static int EXTRA_DOCK_STATE_DESK = 1; - final static int EXTRA_DOCK_STATE_CAR = 2; - - public final static int MWI_NOTIFICATION = 1; - public final static int CALL_NOTIFICATION = 2; - public final static int MISSED_CALL_NOTIFICATION = 3; - public final static int AUTO_ANSWER_NOTIFICATION = 4; - public final static int REGISTER_NOTIFICATION = 5; - public final static int REGISTER_NOTIFICATION_0 = 7; - - final static int MSG_HOLD = 1; - final static int MSG_HANGUP = 2; - - final static long[] vibratePattern = {0,1000,1000}; - - public static int docked = -1,headset = -1,bluetooth = -1; - public static SipdroidEngine mSipdroidEngine; - - public static Context mContext; - public static Service sContext; - public static SipdroidListener listener_video; - public static Call ccCall; - public static Connection ccConn; - public static int call_state; - public static int call_end_reason = -1; - - public static String pstn_state; - public static long pstn_time; - public static String MWI_account; - private static String laststate,lastnumber; - - @TargetApi(26) - public static synchronized SipdroidEngine engine(Context context) { - if (mContext == null || !context.getClass().getName().contains("ReceiverRestrictedContext")) - mContext = context; - if (mSipdroidEngine == null) { - if (android.os.Build.VERSION.SDK_INT > 9) { - ReceiverNew.setPolicy(); - } - mSipdroidEngine = new SipdroidEngine(); - mSipdroidEngine.StartEngine(); - if (Integer.parseInt(Build.VERSION.SDK) >= 8) - Bluetooth.init(); - } else - mSipdroidEngine.CheckEngine(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(new Intent(context, RegisterService.class)); - } else { - context.startService(new Intent(context,RegisterService.class)); - } - - return mSipdroidEngine; - } - - public static Ringtone oRingtone; - static PowerManager.WakeLock wl; - - public static void stopRingtone() { - if (v != null) - v.cancel(); - if (Receiver.oRingtone != null) { - Ringtone ringtone = Receiver.oRingtone; - oRingtone = null; - ringtone.stop(); - } - } - - static android.os.Vibrator v; - - public static void onState(int state,String caller) { - if (ccCall == null) { - ccCall = new Call(); - ccConn = new Connection(); - ccCall.setConn(ccConn); - ccConn.setCall(ccCall); - } - if (call_state != state) { - if (state != UserAgent.UA_STATE_IDLE) - call_end_reason = -1; - call_state = state; - switch(call_state) - { - case UserAgent.UA_STATE_INCOMING_CALL: - RtpStreamReceiver.good = RtpStreamReceiver.lost = RtpStreamReceiver.loss = RtpStreamReceiver.late = 0; - RtpStreamReceiver.speakermode = speakermode(); - bluetooth = -1; - String text = caller.toString(); - if (text.indexOf("= 0 && text.indexOf("@") >= 0) - text = text.substring(text.indexOf("= 0) - text2 = text2.substring(text2.indexOf("\"")+1,text2.lastIndexOf("\"")); - broadcastCallStateChanged("RINGING", caller); - mContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); - ccCall.setState(Call.State.INCOMING); - ccConn.setUserData(null); - ccConn.setAddress(text,text2); - ccConn.setIncoming(true); - ccConn.date = System.currentTimeMillis(); - ccCall.base = 0; - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - int rm = am.getRingerMode(); - int vs = am.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); - KeyguardManager mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); - if (v == null) v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); - if ((pstn_state == null || pstn_state.equals("IDLE")) && - PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ON, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ON) && - !mKeyguardManager.inKeyguardRestrictedInputMode()) - v.vibrate(vibratePattern,1); - else { - if ((pstn_state == null || pstn_state.equals("IDLE")) && - (rm == AudioManager.RINGER_MODE_VIBRATE || - (rm == AudioManager.RINGER_MODE_NORMAL && vs == AudioManager.VIBRATE_SETTING_ON))) - v.vibrate(vibratePattern,1); - if (am.getStreamVolume(AudioManager.STREAM_RING) > 0) { - String sUriSipRingtone = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE, - Settings.System.DEFAULT_RINGTONE_URI.toString()); - if(!TextUtils.isEmpty(sUriSipRingtone)) { - oRingtone = RingtoneManager.getRingtone(mContext, Uri.parse(sUriSipRingtone)); - if (oRingtone != null) oRingtone.play(); - } - } - } - moveTop(); - if (wl == null) { - PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); - wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | - PowerManager.ACQUIRE_CAUSES_WAKEUP, "Sipdroid.onState"); - } - wl.acquire(); - Checkin.checkin(true); - break; - case UserAgent.UA_STATE_OUTGOING_CALL: - RtpStreamReceiver.good = RtpStreamReceiver.lost = RtpStreamReceiver.loss = RtpStreamReceiver.late = 0; - RtpStreamReceiver.speakermode = speakermode(); - bluetooth = -1; - onText(MISSED_CALL_NOTIFICATION, null, 0,0); - engine(mContext).register(); - broadcastCallStateChanged("OFFHOOK", caller); - ccCall.setState(Call.State.DIALING); - ccConn.setUserData(null); - ccConn.setAddress(caller,caller); - ccConn.setIncoming(false); - ccConn.date = System.currentTimeMillis(); - ccCall.base = 0; - moveTop(); - Checkin.checkin(true); - break; - case UserAgent.UA_STATE_IDLE: - broadcastCallStateChanged("IDLE", null); - onText(CALL_NOTIFICATION, null, 0,0); - ccCall.setState(Call.State.DISCONNECTED); - if (listener_video != null) - listener_video.onHangup(); - stopRingtone(); - if (wl != null && wl.isHeld()) - wl.release(); - mContext.startActivity(createIntent(InCallScreen.class)); - ccConn.log(ccCall.base); - ccConn.date = 0; - engine(mContext).listen(); - break; - case UserAgent.UA_STATE_INCALL: - broadcastCallStateChanged("OFFHOOK", null); - if (ccCall.base == 0) { - ccCall.base = SystemClock.elapsedRealtime(); - } - progress(); - ccCall.setState(Call.State.ACTIVE); - stopRingtone(); - if (wl != null && wl.isHeld()) - wl.release(); - mContext.startActivity(createIntent(InCallScreen.class)); - break; - case UserAgent.UA_STATE_HOLD: - onText(CALL_NOTIFICATION, mContext.getString(R.string.card_title_on_hold), android.R.drawable.stat_sys_phone_call_on_hold,ccCall.base); - ccCall.setState(Call.State.HOLDING); - if (InCallScreen.started && (pstn_state == null || !pstn_state.equals("RINGING"))) mContext.startActivity(createIntent(InCallScreen.class)); - break; - } - RtpStreamReceiver.ringback(false); - } - } - - static int notifications[] = new int[REGISTER_NOTIFICATION+2]; - - public static int alloc(int i) - { - int x; - - if (notifications[i] != 0) - return notifications[i]; - if (notifications[5] == REGISTER_NOTIFICATION || notifications[6] == REGISTER_NOTIFICATION) - x = REGISTER_NOTIFICATION+1; - else - x = REGISTER_NOTIFICATION; - return notifications[i] = x; - } - - public static int free(int i) - { - int ret = notifications[i]; - - notifications[i] = 0; - return ret; - } - - static String cache_text; - static int cache_res; - - @SuppressLint("NewApi") - @TargetApi(26) - public static void onText(int type,String text,int mInCallResId,long base) { - int otype = type; - - if (mSipdroidEngine != null && type == REGISTER_NOTIFICATION+mSipdroidEngine.pref) { - cache_text = text; - cache_res = mInCallResId; - } - NotificationManager mNotificationMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - if (text != null) { - Notification notification; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - mNotificationMgr.createNotificationChannel(new NotificationChannel("status", "Status", NotificationManager.IMPORTANCE_LOW)); - mNotificationMgr.createNotificationChannel(new NotificationChannel("call", "Call", NotificationManager.IMPORTANCE_HIGH)); - mNotificationMgr.createNotificationChannel(new NotificationChannel("missed", "Missed Call", NotificationManager.IMPORTANCE_LOW)); - mNotificationMgr.createNotificationChannel(new NotificationChannel("message", "Voice Message", NotificationManager.IMPORTANCE_HIGH)); - notification = new Notification.Builder(mContext,"status").build(); - } else - notification = new Notification.Builder(mContext).build(); - notification.icon = mInCallResId; - if (type == MISSED_CALL_NOTIFICATION) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) - notification = new Notification.Builder(mContext,"missed") - .setSmallIcon(mInCallResId) - .setContentIntent(PendingIntent.getActivity(mContext, 0, createCallLogIntent(), 0)) - .setContentTitle(mContext.getString(R.string.app_name)) - .setContentText(text) - .build(); - else - notification = new Notification.Builder(mContext) - .setSmallIcon(mInCallResId) - .setContentIntent(PendingIntent.getActivity(mContext, 0, createCallLogIntent(), 0)) - .setContentTitle(mContext.getString(R.string.app_name)) - .setContentText(text) - .build(); - notification.flags |= Notification.FLAG_AUTO_CANCEL; - if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_NOTIFY, org.sipdroid.sipua.ui.Settings.DEFAULT_NOTIFY)) { - notification.flags |= Notification.FLAG_SHOW_LIGHTS; - notification.ledARGB = 0xff0000ff; /* blue */ - notification.ledOnMS = 125; - notification.ledOffMS = 2875; - } - } else { - switch (type) { - case MWI_NOTIFICATION: - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - notification = new Notification.Builder(mContext,"message").build(); - } else - notification = new Notification.Builder(mContext).build(); - notification.icon = mInCallResId; - notification.flags |= Notification.FLAG_AUTO_CANCEL; - notification.contentIntent = PendingIntent.getActivity(mContext, 0, - createMWIIntent(), 0); - notification.flags |= Notification.FLAG_SHOW_LIGHTS; - notification.ledARGB = 0xff00ff00; /* green */ - notification.ledOnMS = 125; - notification.ledOffMS = 2875; - break; - case AUTO_ANSWER_NOTIFICATION: - notification.contentIntent = PendingIntent.getActivity(mContext, 0, - createIntent(AutoAnswer.class), 0); - break; - case CALL_NOTIFICATION: - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && !InCallScreen.started) - notification = new Notification.Builder(mContext,"call") - .setFullScreenIntent(PendingIntent.getActivity(mContext, 0, - createIntent(Sipdroid.class), 0), true) - .setSmallIcon(mInCallResId).build(); - default: - if (type >= REGISTER_NOTIFICATION && mSipdroidEngine != null && type != REGISTER_NOTIFICATION+mSipdroidEngine.pref && - mInCallResId == R.drawable.sym_presence_available) - notification.contentIntent = PendingIntent.getActivity(mContext, 0, - createIntent(ChangeAccount.class), 0); - else - notification.contentIntent = PendingIntent.getActivity(mContext, 0, - createIntent(Sipdroid.class), 0); - if (mInCallResId == R.drawable.sym_presence_away) { - notification.flags |= Notification.FLAG_SHOW_LIGHTS; - notification.ledARGB = 0xffff0000; /* red */ - notification.ledOnMS = 125; - notification.ledOffMS = 2875; - } - break; - } - notification.flags |= Notification.FLAG_ONGOING_EVENT; - RemoteViews contentView = new RemoteViews(mContext.getPackageName(), - R.layout.ongoing_call_notification); - contentView.setImageViewResource(R.id.icon, notification.icon); - if (base != 0) { - contentView.setChronometer(R.id.text1, base, text+" (%s)", true); - } else if (type >= REGISTER_NOTIFICATION) { - contentView.setTextViewText(R.id.text2, text); - if (mSipdroidEngine != null) - if (type == REGISTER_NOTIFICATION_0) - contentView.setTextViewText(R.id.text1, - mSipdroidEngine.user_profiles[0].username+"@"+ - mSipdroidEngine.user_profiles[0].realm_orig); - else - contentView.setTextViewText(R.id.text1, - mSipdroidEngine.user_profiles[type-REGISTER_NOTIFICATION].username+"@"+ - mSipdroidEngine.user_profiles[type-REGISTER_NOTIFICATION].realm_orig); - } else - contentView.setTextViewText(R.id.text1, text); - notification.contentView = contentView; - } - if (type == REGISTER_NOTIFICATION_0) - type = REGISTER_NOTIFICATION; - else if (type >= REGISTER_NOTIFICATION) - type = alloc(type); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Receiver.sContext != null && type == REGISTER_NOTIFICATION) - Receiver.sContext.startForeground(type,notification); - else - mNotificationMgr.notify(type,notification); - } else { - if (type >= REGISTER_NOTIFICATION) - type = free(type); - if (type == 0) - type = REGISTER_NOTIFICATION; - mNotificationMgr.cancel(type); - } - if (type != AUTO_ANSWER_NOTIFICATION) - updateAutoAnswer(); - if (mSipdroidEngine != null && otype >= REGISTER_NOTIFICATION && otype != REGISTER_NOTIFICATION+mSipdroidEngine.pref) - onText(REGISTER_NOTIFICATION+mSipdroidEngine.pref,cache_text,cache_res,0); - } - - static void updateAutoAnswer() { - if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ONDEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ONDEMAND) && - Sipdroid.on(mContext)) { - if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_DEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_DEMAND)) - updateAutoAnswer(1); - else - updateAutoAnswer(0); - } else - updateAutoAnswer(-1); - } - - private static int autoAnswerState = -1; - - static void updateAutoAnswer(int status) { - if (status != autoAnswerState) { - switch (autoAnswerState = status) { - case 0: - Receiver.onText(Receiver.AUTO_ANSWER_NOTIFICATION,mContext.getString(R.string.auto_disabled),R.drawable.auto_answer_disabled,0); - break; - case 1: - Receiver.onText(Receiver.AUTO_ANSWER_NOTIFICATION,mContext.getString(R.string.auto_enabled),R.drawable.auto_answer,0); - break; - case -1: - Receiver.onText(Receiver.AUTO_ANSWER_NOTIFICATION, null, 0, 0); - break; - } - } - } - - public static void registered() { - } - - public static void url(final String opt) { - (new Thread() { - public void run() { - try { - URL url = new URL(PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_POSURL, org.sipdroid.sipua.ui.Settings.DEFAULT_POSURL)+ - "?"+opt); - BufferedReader in; - URLConnection connection = url.openConnection(); - connection.setRequestProperty("Accept", "text/html"); - in = new BufferedReader(new InputStreamReader(connection.getInputStream())); - in.close(); - } catch (IOException e) { - if (!Sipdroid.release) e.printStackTrace(); - } - - } - }).start(); - } - - static boolean was_playing; - - static void broadcastCallStateChanged(String state,String number) { - if (state == null) { - state = laststate; - number = lastnumber; - } - if (android.os.Build.VERSION.SDK_INT < 19) { - Intent intent = new Intent(ACTION_PHONE_STATE_CHANGED); - intent.putExtra("state",state); - if (number != null) - intent.putExtra("incoming_number", number); - intent.putExtra(mContext.getString(R.string.app_name), true); - mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE); - } - if (state.equals("IDLE")) { - if (was_playing) { - if (pstn_state == null || pstn_state.equals("IDLE")) - mContext.sendBroadcast(new Intent(TOGGLEPAUSE_ACTION)); - was_playing = false; - } - } else { - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - if ((laststate == null || laststate.equals("IDLE")) && (was_playing = am.isMusicActive())) - mContext.sendBroadcast(new Intent(PAUSE_ACTION)); - } - laststate = state; - lastnumber = number; - } - - @TargetApi(23) - public static void alarm(int renew_time,Class cls) { - if (!Sipdroid.release) Log.i("SipUA:","alarm "+renew_time); - Intent intent = new Intent(mContext, cls); - PendingIntent sender = PendingIntent.getBroadcast(mContext, - 0, intent, 0); - AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); - am.cancel(sender); - if (renew_time > 0) { - if (android.os.Build.VERSION.SDK_INT >= 23) - am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+renew_time*1000, sender); - else if (android.os.Build.VERSION.SDK_INT >= 19) - am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+renew_time*1000, sender); - else - am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+renew_time*1000, sender); - } - } - - public static long expire_time; - - public static synchronized void reRegister(int renew_time) { - if (renew_time == 0) - expire_time = 0; - else { - if (expire_time != 0 && renew_time*1000 + SystemClock.elapsedRealtime() > expire_time) return; - expire_time = renew_time*1000 + SystemClock.elapsedRealtime(); - } - alarm(renew_time-15, OneShotAlarm.class); - } - - static Intent createIntent(Classcls) { - Intent startActivity = new Intent(); - startActivity.setClass(mContext,cls); - startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return startActivity; - } - - public static Intent createCallLogIntent() { - Intent intent = new Intent(Intent.ACTION_VIEW, null); - intent.setType("vnd.android.cursor.dir/calls"); - return intent; - } - - static Intent createHomeDockIntent() { - Intent intent = new Intent(Intent.ACTION_MAIN, null); - if (docked == EXTRA_DOCK_STATE_CAR) { - intent.addCategory(CATEGORY_CAR_DOCK); - } else if (docked == EXTRA_DOCK_STATE_DESK) { - intent.addCategory(CATEGORY_DESK_DOCK); - } else { - return null; - } - - ActivityInfo ai = intent.resolveActivityInfo( - mContext.getPackageManager(), PackageManager.GET_META_DATA); - if (ai == null) { - return null; - } - - if (ai.metaData != null && ai.metaData.getBoolean(METADATA_DOCK_HOME)) { - intent.setClassName(ai.packageName, ai.name); - return intent; - } - - return null; - } - - public static Intent createHomeIntent() { - Intent intent = createHomeDockIntent(); - if (intent != null) { - try { - return intent; - } catch (ActivityNotFoundException e) { - } - } - intent = new Intent(Intent.ACTION_MAIN, null); - intent.addCategory(Intent.CATEGORY_HOME); - return intent; - } - - static Intent createMWIIntent() { - Intent intent; - - if (MWI_account != null) - intent = new Intent(Intent.ACTION_CALL, Uri.parse(MWI_account.replaceFirst("sip:","sipdroid:"))); - else - intent = new Intent(Intent.ACTION_DIAL); - return intent; - } - - public static void moveTop() { - progress(); - mContext.startActivity(createIntent(Activity2.class)); - } - - public static void progress() { - if (call_state == UserAgent.UA_STATE_IDLE) return; - int mode = RtpStreamReceiver.speakermode; - if (mode == -1) - mode = speakermode(); - if (mode == AudioManager.MODE_NORMAL) - Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.menu_speaker), android.R.drawable.stat_sys_speakerphone,Receiver.ccCall.base); - else if (bluetooth > 0) - Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.menu_bluetooth), R.drawable.stat_sys_phone_call_bluetooth,Receiver.ccCall.base); - else switch (call_state) { - case UserAgent.UA_STATE_INCALL: - Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.card_title_in_progress), R.drawable.stat_sys_phone_call,Receiver.ccCall.base); - break; - case UserAgent.UA_STATE_OUTGOING_CALL: - Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.card_title_dialing), R.drawable.stat_sys_phone_call,Receiver.ccCall.base); - break; - case UserAgent.UA_STATE_INCOMING_CALL: - Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.card_title_incoming_call), R.drawable.stat_sys_phone_call,Receiver.ccCall.base); - break; - } - RtpStreamSender.changed = true; - } - - public static boolean on_wlan; - - static boolean on_vpn() { - return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_ON_VPN, org.sipdroid.sipua.ui.Settings.DEFAULT_ON_VPN); - } - - static void on_vpn(boolean enable) { - Editor edit = PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).edit(); - - edit.putBoolean(org.sipdroid.sipua.ui.Settings.PREF_ON_VPN,enable); - edit.commit(); - } - - public static boolean isFast(int i) { - WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); - WifiInfo wi = wm.getConnectionInfo(); - - if (PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_USERNAME+(i!=0?i:""),"").equals("") || - PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_SERVER+(i!=0?i:""),"").equals("")) - return false; - if (wi != null) { - if (!Sipdroid.release) Log.i("SipUA:","isFastWifi() "+WifiInfo.getDetailedStateOf(wi.getSupplicantState()) - +" "+wi.getIpAddress()); - if (wi.getIpAddress() != 0 && (WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.OBTAINING_IPADDR - || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTED) - || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTING) { - on_wlan = true; - if (!on_vpn()) - return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_WLAN+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_WLAN); - else - return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_VPN+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_VPN); - } - } - on_wlan = false; - return isFastGSM(i); - } - - @TargetApi(24) - static boolean isFastGSM(int i) { - TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); - int nt = 0; - - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) - if (Receiver.mContext.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) - != PackageManager.PERMISSION_GRANTED) { - nt = TelephonyManager.NETWORK_TYPE_UMTS; - } - if (nt == 0) { - if (android.os.Build.VERSION.SDK_INT >= 24) - nt = tm.getDataNetworkType(); - else - nt = tm.getNetworkType(); - } - if (Sipdroid.market) - return false; - if (on_vpn() && (nt >= TelephonyManager.NETWORK_TYPE_EDGE)) - return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_VPN+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_VPN); - if (nt >= TelephonyManager.NETWORK_TYPE_UMTS) - return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_3G+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_3G); - if (nt == TelephonyManager.NETWORK_TYPE_EDGE) - return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_EDGE+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_EDGE); - return false; - } - - public static int speakermode() { - if (docked > 0 && headset <= 0) - return AudioManager.MODE_NORMAL; - else - return AudioManager.MODE_IN_CALL; - } - - static Handler mHandler = new Handler() { - public void handleMessage(Message msg) { - switch (msg.what) { - case MSG_HOLD: - engine(mContext).togglehold(); - break; - case MSG_HANGUP: - engine(mContext).rejectcall(); - break; - } - } - }; - - @Override - public void onReceive(Context context, Intent intent) { - String intentAction = intent.getAction(); - - if (!Sipdroid.on(context)) return; - if (!Sipdroid.release) Log.i("SipUA:",intentAction); - if (mContext == null) mContext = context; - if (intentAction.equals(Intent.ACTION_BOOT_COMPLETED)){ - on_vpn(false); - engine(context).register(); - } else - if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION) || - intentAction.equals(ACTION_EXTERNAL_APPLICATIONS_AVAILABLE) || - intentAction.equals(ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) || - intentAction.equals(Intent.ACTION_PACKAGE_REPLACED)) { - engine(context).register(); - } else - if (intentAction.equals(ACTION_VPN_CONNECTIVITY) && intent.hasExtra("connection_state")) { - String state = intent.getSerializableExtra("connection_state").toString(); - if (state != null && on_vpn() != state.equals("CONNECTED")) { - on_vpn(state.equals("CONNECTED")); - for (SipProvider sip_provider : engine(context).sip_providers) - if (sip_provider != null) - sip_provider.haltConnections(); - engine(context).register(); - } - } else - if (intentAction.equals(ACTION_DATA_STATE_CHANGED)) { - engine(context).registerMore(); - } else - if (intentAction.equals(ACTION_PHONE_STATE_CHANGED) && - !intent.getBooleanExtra(context.getString(R.string.app_name),false)) { - stopRingtone(); - pstn_state = intent.getStringExtra("state"); - pstn_time = SystemClock.elapsedRealtime(); - if (pstn_state.equals("IDLE") && call_state != UserAgent.UA_STATE_IDLE) - broadcastCallStateChanged(null,null); - if (!pstn_state.equals("IDLE") && call_state == UserAgent.UA_STATE_INCALL) - mHandler.sendEmptyMessageDelayed(MSG_HOLD, 5000); - else if (!pstn_state.equals("IDLE") && (call_state == UserAgent.UA_STATE_INCOMING_CALL || call_state == UserAgent.UA_STATE_OUTGOING_CALL)) - mHandler.sendEmptyMessageDelayed(MSG_HANGUP, 5000); - else if (pstn_state.equals("IDLE")) { - mHandler.removeMessages(MSG_HOLD); - mHandler.removeMessages(MSG_HANGUP); - if (call_state == UserAgent.UA_STATE_HOLD) - mHandler.sendEmptyMessageDelayed(MSG_HOLD, 1000); - } - } else - if (intentAction.equals(ACTION_DOCK_EVENT)) { - docked = intent.getIntExtra(EXTRA_DOCK_STATE, -1) & 7; - if (call_state == UserAgent.UA_STATE_INCALL) - engine(mContext).speaker(speakermode()); - } else - if (intentAction.equals(ACTION_SCO_AUDIO_STATE_CHANGED)) { - bluetooth = intent.getIntExtra(EXTRA_SCO_AUDIO_STATE, -1); - progress(); - RtpStreamSender.changed = true; - } else - if (intentAction.equals(Intent.ACTION_HEADSET_PLUG)) { - headset = intent.getIntExtra("state", -1); - if (call_state == UserAgent.UA_STATE_INCALL) - engine(mContext).speaker(speakermode()); - } else - if (intentAction.equals(Intent.ACTION_SCREEN_OFF)) { - if (SipdroidEngine.pwl != null) - for (PowerManager.WakeLock pwl : SipdroidEngine.pwl) - if (pwl != null && pwl.isHeld()) { - pwl.release(); - pwl.acquire(); - } - } - } -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import static android.app.PendingIntent.FLAG_IMMUTABLE; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; + +import android.Manifest; +import android.annotation.SuppressLint; +import android.annotation.TargetApi; +import android.app.AlarmManager; +import android.app.KeyguardManager; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.ActivityNotFoundException; +import android.content.BroadcastReceiver; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences.Editor; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationManager; +import android.media.AudioManager; +import android.media.Ringtone; +import android.media.RingtoneManager; +import android.net.ConnectivityManager; +import android.net.Uri; +import android.net.NetworkInfo.DetailedState; +import android.net.wifi.ScanResult; +import android.net.wifi.SupplicantState; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.os.Handler; +import android.os.Message; +import android.os.PowerManager; +import android.os.StrictMode; +import android.os.SystemClock; +import android.os.Vibrator; +import android.preference.PreferenceManager; +import android.provider.Settings; +import android.telephony.TelephonyManager; +import android.text.TextUtils; +import android.util.Log; +import android.widget.RemoteViews; + +import org.sipdroid.media.Bluetooth; +import org.sipdroid.media.RtpStreamReceiver; +import org.sipdroid.media.RtpStreamSender; +import org.sipdroid.sipua.*; +import org.sipdroid.sipua.phone.Call; +import org.sipdroid.sipua.phone.Connection; +import org.zoolu.sip.provider.SipProvider; + + public class Receiver extends BroadcastReceiver { + + final static String ACTION_PHONE_STATE_CHANGED = "android.intent.action.PHONE_STATE"; + final static String ACTION_SIGNAL_STRENGTH_CHANGED = "android.intent.action.SIG_STR"; + final static String ACTION_DATA_STATE_CHANGED = "android.intent.action.ANY_DATA_STATE"; + final static String ACTION_DOCK_EVENT = "android.intent.action.DOCK_EVENT"; + final static String EXTRA_DOCK_STATE = "android.intent.extra.DOCK_STATE"; + final static String ACTION_SCO_AUDIO_STATE_CHANGED = "android.media.SCO_AUDIO_STATE_CHANGED"; + final static String EXTRA_SCO_AUDIO_STATE = "android.media.extra.SCO_AUDIO_STATE"; + final static String PAUSE_ACTION = "com.android.music.musicservicecommand.pause"; + final static String TOGGLEPAUSE_ACTION = "com.android.music.musicservicecommand.togglepause"; + final static String ACTION_DEVICE_IDLE = "com.android.server.WifiManager.action.DEVICE_IDLE"; + final static String ACTION_VPN_CONNECTIVITY = "vpn.connectivity"; + final static String ACTION_EXTERNAL_APPLICATIONS_AVAILABLE = "android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE"; + final static String ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE = "android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE"; + final static String METADATA_DOCK_HOME = "android.dock_home"; + final static String CATEGORY_DESK_DOCK = "android.intent.category.DESK_DOCK"; + final static String CATEGORY_CAR_DOCK = "android.intent.category.CAR_DOCK"; + final static int EXTRA_DOCK_STATE_DESK = 1; + final static int EXTRA_DOCK_STATE_CAR = 2; + + public final static int MWI_NOTIFICATION = 1; + public final static int CALL_NOTIFICATION = 2; + public final static int MISSED_CALL_NOTIFICATION = 3; + public final static int AUTO_ANSWER_NOTIFICATION = 4; + public final static int REGISTER_NOTIFICATION = 5; + public final static int REGISTER_NOTIFICATION_0 = 7; + + final static int MSG_HOLD = 1; + final static int MSG_HANGUP = 2; + + final static long[] vibratePattern = {0,1000,1000}; + + public static int docked = -1,headset = -1,bluetooth = -1; + public static SipdroidEngine mSipdroidEngine; + + public static Context mContext; + public static Service sContext; + public static SipdroidListener listener_video; + public static Call ccCall; + public static Connection ccConn; + public static int call_state; + public static int call_end_reason = -1; + + public static String pstn_state; + public static long pstn_time; + public static String MWI_account; + private static String laststate,lastnumber; + + @TargetApi(26) + public static synchronized SipdroidEngine engine(Context context) { + if (mContext == null || !context.getClass().getName().contains("ReceiverRestrictedContext")) + mContext = context; + if (mSipdroidEngine == null) { + if (android.os.Build.VERSION.SDK_INT > 9) { + ReceiverNew.setPolicy(); + } + mSipdroidEngine = new SipdroidEngine(); + mSipdroidEngine.StartEngine(); + if (Integer.parseInt(Build.VERSION.SDK) >= 8) + Bluetooth.init(); + } else + mSipdroidEngine.CheckEngine(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(new Intent(context, RegisterService.class)); + } else { + context.startService(new Intent(context,RegisterService.class)); + } + + return mSipdroidEngine; + } + + public static Ringtone oRingtone; + static PowerManager.WakeLock wl; + + public static void stopRingtone() { + if (v != null) + v.cancel(); + if (Receiver.oRingtone != null) { + Ringtone ringtone = Receiver.oRingtone; + oRingtone = null; + ringtone.stop(); + } + } + + static android.os.Vibrator v; + + public static void onState(int state,String caller) { + if (ccCall == null) { + ccCall = new Call(); + ccConn = new Connection(); + ccCall.setConn(ccConn); + ccConn.setCall(ccCall); + } + if (call_state != state) { + if (state != UserAgent.UA_STATE_IDLE) + call_end_reason = -1; + call_state = state; + switch(call_state) + { + case UserAgent.UA_STATE_INCOMING_CALL: + RtpStreamReceiver.good = RtpStreamReceiver.lost = RtpStreamReceiver.loss = RtpStreamReceiver.late = 0; + RtpStreamReceiver.speakermode = speakermode(); + bluetooth = -1; + String text = caller.toString(); + if (text.indexOf("= 0 && text.indexOf("@") >= 0) + text = text.substring(text.indexOf("= 0) + text2 = text2.substring(text2.indexOf("\"")+1,text2.lastIndexOf("\"")); + broadcastCallStateChanged("RINGING", caller); +// mContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); + ccCall.setState(Call.State.INCOMING); + ccConn.setUserData(null); + ccConn.setAddress(text,text2); + ccConn.setIncoming(true); + ccConn.date = System.currentTimeMillis(); + ccCall.base = 0; + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + int rm = am.getRingerMode(); + int vs = am.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); + KeyguardManager mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); + if (v == null) v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); + if ((pstn_state == null || pstn_state.equals("IDLE")) && + PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ON, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ON) && + !mKeyguardManager.inKeyguardRestrictedInputMode()) + v.vibrate(vibratePattern,1); + else { + if ((pstn_state == null || pstn_state.equals("IDLE")) && + (rm == AudioManager.RINGER_MODE_VIBRATE || + (rm == AudioManager.RINGER_MODE_NORMAL && vs == AudioManager.VIBRATE_SETTING_ON))) + v.vibrate(vibratePattern,1); + if (am.getStreamVolume(AudioManager.STREAM_RING) > 0) { + String sUriSipRingtone = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE, + Settings.System.DEFAULT_RINGTONE_URI.toString()); + if(!TextUtils.isEmpty(sUriSipRingtone)) { + oRingtone = RingtoneManager.getRingtone(mContext, Uri.parse(sUriSipRingtone)); + if (oRingtone != null) oRingtone.play(); + } + } + } + moveTop(); + if (wl == null) { + PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); + wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | + PowerManager.ACQUIRE_CAUSES_WAKEUP, "Sipdroid:onState"); + } + wl.acquire(); + Checkin.checkin(true); + break; + case UserAgent.UA_STATE_OUTGOING_CALL: + RtpStreamReceiver.good = RtpStreamReceiver.lost = RtpStreamReceiver.loss = RtpStreamReceiver.late = 0; + RtpStreamReceiver.speakermode = speakermode(); + bluetooth = -1; + onText(MISSED_CALL_NOTIFICATION, null, 0,0); + engine(mContext).register(); + broadcastCallStateChanged("OFFHOOK", caller); + ccCall.setState(Call.State.DIALING); + ccConn.setUserData(null); + ccConn.setAddress(caller,caller); + ccConn.setIncoming(false); + ccConn.date = System.currentTimeMillis(); + ccCall.base = 0; + moveTop(); + Checkin.checkin(true); + break; + case UserAgent.UA_STATE_IDLE: + broadcastCallStateChanged("IDLE", null); + onText(CALL_NOTIFICATION, null, 0,0); + ccCall.setState(Call.State.DISCONNECTED); + if (listener_video != null) + listener_video.onHangup(); + stopRingtone(); + if (wl != null && wl.isHeld()) + wl.release(); + mContext.startActivity(createIntent(InCallScreen.class)); + ccConn.log(ccCall.base); + ccConn.date = 0; + engine(mContext).listen(); + break; + case UserAgent.UA_STATE_INCALL: + broadcastCallStateChanged("OFFHOOK", null); + if (ccCall.base == 0) { + ccCall.base = SystemClock.elapsedRealtime(); + } + progress(); + ccCall.setState(Call.State.ACTIVE); + stopRingtone(); + if (wl != null && wl.isHeld()) + wl.release(); + mContext.startActivity(createIntent(InCallScreen.class)); + break; + case UserAgent.UA_STATE_HOLD: + onText(CALL_NOTIFICATION, mContext.getString(R.string.card_title_on_hold), android.R.drawable.stat_sys_phone_call_on_hold,ccCall.base); + ccCall.setState(Call.State.HOLDING); + if (InCallScreen.started && (pstn_state == null || !pstn_state.equals("RINGING"))) mContext.startActivity(createIntent(InCallScreen.class)); + break; + } + RtpStreamReceiver.ringback(false); + } + } + + static int notifications[] = new int[REGISTER_NOTIFICATION+2]; + + public static int alloc(int i) + { + int x; + + if (notifications[i] != 0) + return notifications[i]; + if (notifications[5] == REGISTER_NOTIFICATION || notifications[6] == REGISTER_NOTIFICATION) + x = REGISTER_NOTIFICATION+1; + else + x = REGISTER_NOTIFICATION; + return notifications[i] = x; + } + + public static int free(int i) + { + int ret = notifications[i]; + + notifications[i] = 0; + return ret; + } + + static String cache_text; + static int cache_res; + + @SuppressLint("NewApi") + @TargetApi(26) + public static void onText(int type,String text,int mInCallResId,long base) { + int otype = type; + int flags = 0; + if (android.os.Build.VERSION.SDK_INT >= 31) + flags |= FLAG_IMMUTABLE; + + if (mSipdroidEngine != null && type == REGISTER_NOTIFICATION+mSipdroidEngine.pref) { + cache_text = text; + cache_res = mInCallResId; + } + NotificationManager mNotificationMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + if (text != null) { + Notification notification; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + mNotificationMgr.createNotificationChannel(new NotificationChannel("status", "Status", NotificationManager.IMPORTANCE_LOW)); + mNotificationMgr.createNotificationChannel(new NotificationChannel("call", "Call", NotificationManager.IMPORTANCE_HIGH)); + mNotificationMgr.createNotificationChannel(new NotificationChannel("missed", "Missed Call", NotificationManager.IMPORTANCE_LOW)); + mNotificationMgr.createNotificationChannel(new NotificationChannel("message", "Voice Message", NotificationManager.IMPORTANCE_HIGH)); + notification = new Notification.Builder(mContext,"status").build(); + } else + notification = new Notification.Builder(mContext).build(); + notification.icon = mInCallResId; + if (type == MISSED_CALL_NOTIFICATION) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) + notification = new Notification.Builder(mContext,"missed") + .setSmallIcon(mInCallResId) + .setContentIntent(PendingIntent.getActivity(mContext, 0, createCallLogIntent(), flags)) + .setContentTitle(mContext.getString(R.string.app_name)) + .setContentText(text) + .build(); + else + notification = new Notification.Builder(mContext) + .setSmallIcon(mInCallResId) + .setContentIntent(PendingIntent.getActivity(mContext, 0, createCallLogIntent(), flags)) + .setContentTitle(mContext.getString(R.string.app_name)) + .setContentText(text) + .build(); + notification.flags |= Notification.FLAG_AUTO_CANCEL; + if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_NOTIFY, org.sipdroid.sipua.ui.Settings.DEFAULT_NOTIFY)) { + notification.flags |= Notification.FLAG_SHOW_LIGHTS; + notification.ledARGB = 0xff0000ff; /* blue */ + notification.ledOnMS = 125; + notification.ledOffMS = 2875; + } + } else { + switch (type) { + case MWI_NOTIFICATION: + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + notification = new Notification.Builder(mContext,"message").build(); + } else + notification = new Notification.Builder(mContext).build(); + notification.icon = mInCallResId; + notification.flags |= Notification.FLAG_AUTO_CANCEL; + notification.contentIntent = PendingIntent.getActivity(mContext, 0, + createMWIIntent(), flags); + notification.flags |= Notification.FLAG_SHOW_LIGHTS; + notification.ledARGB = 0xff00ff00; /* green */ + notification.ledOnMS = 125; + notification.ledOffMS = 2875; + break; + case AUTO_ANSWER_NOTIFICATION: + notification.contentIntent = PendingIntent.getActivity(mContext, 0, + createIntent(AutoAnswer.class), flags); + break; + case CALL_NOTIFICATION: + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && !InCallScreen.started) + notification = new Notification.Builder(mContext,"call") + .setFullScreenIntent(PendingIntent.getActivity(mContext, 0, + createIntent(Sipdroid.class), flags), true) + .setSmallIcon(mInCallResId).build(); + default: + if (type >= REGISTER_NOTIFICATION && mSipdroidEngine != null && type != REGISTER_NOTIFICATION+mSipdroidEngine.pref && + mInCallResId == R.drawable.sym_presence_available) + notification.contentIntent = PendingIntent.getActivity(mContext, 0, + createIntent(ChangeAccount.class), flags); + else + notification.contentIntent = PendingIntent.getActivity(mContext, 0, + createIntent(Sipdroid.class), flags); + if (mInCallResId == R.drawable.sym_presence_away) { + notification.flags |= Notification.FLAG_SHOW_LIGHTS; + notification.ledARGB = 0xffff0000; /* red */ + notification.ledOnMS = 125; + notification.ledOffMS = 2875; + } + break; + } + notification.flags |= Notification.FLAG_ONGOING_EVENT; + RemoteViews contentView = new RemoteViews(mContext.getPackageName(), + R.layout.ongoing_call_notification); + contentView.setImageViewResource(R.id.icon, notification.icon); + if (base != 0) { + contentView.setChronometer(R.id.text1, base, text+" (%s)", true); + } else if (type >= REGISTER_NOTIFICATION) { + contentView.setTextViewText(R.id.text2, text); + if (mSipdroidEngine != null) + if (type == REGISTER_NOTIFICATION_0) + contentView.setTextViewText(R.id.text1, + mSipdroidEngine.user_profiles[0].username+"@"+ + mSipdroidEngine.user_profiles[0].realm_orig); + else + contentView.setTextViewText(R.id.text1, + mSipdroidEngine.user_profiles[type-REGISTER_NOTIFICATION].username+"@"+ + mSipdroidEngine.user_profiles[type-REGISTER_NOTIFICATION].realm_orig); + } else + contentView.setTextViewText(R.id.text1, text); + notification.contentView = contentView; + } + if (type == REGISTER_NOTIFICATION_0) + type = REGISTER_NOTIFICATION; + else if (type >= REGISTER_NOTIFICATION) + type = alloc(type); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Receiver.sContext != null && type == REGISTER_NOTIFICATION) + Receiver.sContext.startForeground(type,notification); + else + mNotificationMgr.notify(type,notification); + } else { + if (type >= REGISTER_NOTIFICATION) + type = free(type); + if (type == 0) + type = REGISTER_NOTIFICATION; + mNotificationMgr.cancel(type); + } + if (type != AUTO_ANSWER_NOTIFICATION) + updateAutoAnswer(); + if (mSipdroidEngine != null && otype >= REGISTER_NOTIFICATION && otype != REGISTER_NOTIFICATION+mSipdroidEngine.pref) + onText(REGISTER_NOTIFICATION+mSipdroidEngine.pref,cache_text,cache_res,0); + } + + static void updateAutoAnswer() { + if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_ONDEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ONDEMAND) && + Sipdroid.on(mContext)) { + if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_AUTO_DEMAND, org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_DEMAND)) + updateAutoAnswer(1); + else + updateAutoAnswer(0); + } else + updateAutoAnswer(-1); + } + + private static int autoAnswerState = -1; + + static void updateAutoAnswer(int status) { + if (status != autoAnswerState) { + switch (autoAnswerState = status) { + case 0: + Receiver.onText(Receiver.AUTO_ANSWER_NOTIFICATION,mContext.getString(R.string.auto_disabled),R.drawable.auto_answer_disabled,0); + break; + case 1: + Receiver.onText(Receiver.AUTO_ANSWER_NOTIFICATION,mContext.getString(R.string.auto_enabled),R.drawable.auto_answer,0); + break; + case -1: + Receiver.onText(Receiver.AUTO_ANSWER_NOTIFICATION, null, 0, 0); + break; + } + } + } + + public static void registered() { + } + + public static void url(final String opt) { + (new Thread() { + public void run() { + try { + URL url = new URL(PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_POSURL, org.sipdroid.sipua.ui.Settings.DEFAULT_POSURL)+ + "?"+opt); + BufferedReader in; + URLConnection connection = url.openConnection(); + connection.setRequestProperty("Accept", "text/html"); + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + in.close(); + } catch (IOException e) { + if (!Sipdroid.release) e.printStackTrace(); + } + + } + }).start(); + } + + static boolean was_playing; + + static void broadcastCallStateChanged(String state,String number) { + if (state == null) { + state = laststate; + number = lastnumber; + } + if (android.os.Build.VERSION.SDK_INT < 19) { + Intent intent = new Intent(ACTION_PHONE_STATE_CHANGED); + intent.putExtra("state",state); + if (number != null) + intent.putExtra("incoming_number", number); + intent.putExtra(mContext.getString(R.string.app_name), true); + mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE); + } + if (state.equals("IDLE")) { + if (was_playing) { + if (pstn_state == null || pstn_state.equals("IDLE")) + mContext.sendBroadcast(new Intent(TOGGLEPAUSE_ACTION)); + was_playing = false; + } + } else { + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + if ((laststate == null || laststate.equals("IDLE")) && (was_playing = am.isMusicActive())) + mContext.sendBroadcast(new Intent(PAUSE_ACTION)); + } + laststate = state; + lastnumber = number; + } + + @TargetApi(23) + public static void alarm(int renew_time,Class cls) { + if (!Sipdroid.release) Log.i("SipUA:","alarm "+renew_time); + Intent intent = new Intent(mContext, cls); + int flags = 0; + if (android.os.Build.VERSION.SDK_INT >= 31) + flags |= FLAG_IMMUTABLE; + PendingIntent sender = PendingIntent.getBroadcast(mContext, + 0, intent, flags); + AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); + am.cancel(sender); + if (renew_time > 0) { + if (android.os.Build.VERSION.SDK_INT >= 23) + am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+renew_time*1000, sender); + else if (android.os.Build.VERSION.SDK_INT >= 19) + am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+renew_time*1000, sender); + else + am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+renew_time*1000, sender); + } + } + + public static long expire_time; + + public static synchronized void reRegister(int renew_time) { + if (renew_time == 0) + expire_time = 0; + else { + if (expire_time != 0 && renew_time*1000 + SystemClock.elapsedRealtime() > expire_time) return; + expire_time = renew_time*1000 + SystemClock.elapsedRealtime(); + } + alarm(renew_time-15, OneShotAlarm.class); + } + + static Intent createIntent(Classcls) { + Intent startActivity = new Intent(); + startActivity.setClass(mContext,cls); + startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return startActivity; + } + + public static Intent createCallLogIntent() { + Intent intent = new Intent(Intent.ACTION_VIEW, null); + intent.setType("vnd.android.cursor.dir/calls"); + return intent; + } + + static Intent createHomeDockIntent() { + Intent intent = new Intent(Intent.ACTION_MAIN, null); + if (docked == EXTRA_DOCK_STATE_CAR) { + intent.addCategory(CATEGORY_CAR_DOCK); + } else if (docked == EXTRA_DOCK_STATE_DESK) { + intent.addCategory(CATEGORY_DESK_DOCK); + } else { + return null; + } + + ActivityInfo ai = intent.resolveActivityInfo( + mContext.getPackageManager(), PackageManager.GET_META_DATA); + if (ai == null) { + return null; + } + + if (ai.metaData != null && ai.metaData.getBoolean(METADATA_DOCK_HOME)) { + intent.setClassName(ai.packageName, ai.name); + return intent; + } + + return null; + } + + public static Intent createHomeIntent() { + Intent intent = createHomeDockIntent(); + if (intent != null) { + try { + return intent; + } catch (ActivityNotFoundException e) { + } + } + intent = new Intent(Intent.ACTION_MAIN, null); + intent.addCategory(Intent.CATEGORY_HOME); + return intent; + } + + static Intent createMWIIntent() { + Intent intent; + + if (MWI_account != null) + intent = new Intent(Intent.ACTION_CALL, Uri.parse(MWI_account.replaceFirst("sip:","sipdroid:"))); + else + intent = new Intent(Intent.ACTION_DIAL); + return intent; + } + + public static void moveTop() { + progress(); + mContext.startActivity(createIntent(Activity2.class)); + } + + public static void progress() { + if (call_state == UserAgent.UA_STATE_IDLE) return; + int mode = RtpStreamReceiver.speakermode; + if (mode == -1) + mode = speakermode(); + if (mode == AudioManager.MODE_NORMAL) + Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.menu_speaker), android.R.drawable.stat_sys_speakerphone,Receiver.ccCall.base); + else if (bluetooth > 0) + Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.menu_bluetooth), R.drawable.stat_sys_phone_call_bluetooth,Receiver.ccCall.base); + else switch (call_state) { + case UserAgent.UA_STATE_INCALL: + Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.card_title_in_progress), R.drawable.stat_sys_phone_call,Receiver.ccCall.base); + break; + case UserAgent.UA_STATE_OUTGOING_CALL: + Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.card_title_dialing), R.drawable.stat_sys_phone_call,Receiver.ccCall.base); + break; + case UserAgent.UA_STATE_INCOMING_CALL: + Receiver.onText(Receiver.CALL_NOTIFICATION, mContext.getString(R.string.card_title_incoming_call), R.drawable.stat_sys_phone_call,Receiver.ccCall.base); + break; + } + RtpStreamSender.changed = true; + } + + public static boolean on_wlan; + + static boolean on_vpn() { + return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_ON_VPN, org.sipdroid.sipua.ui.Settings.DEFAULT_ON_VPN); + } + + static void on_vpn(boolean enable) { + Editor edit = PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).edit(); + + edit.putBoolean(org.sipdroid.sipua.ui.Settings.PREF_ON_VPN,enable); + edit.commit(); + } + + public static boolean isFast(int i) { + WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); + WifiInfo wi = wm.getConnectionInfo(); + + if (PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_USERNAME+(i!=0?i:""),"").equals("") || + PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_SERVER+(i!=0?i:""),"").equals("")) + return false; + if (wi != null) { + if (!Sipdroid.release) Log.i("SipUA:","isFastWifi() "+WifiInfo.getDetailedStateOf(wi.getSupplicantState()) + +" "+wi.getIpAddress()); + if (wi.getIpAddress() != 0 && (WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.OBTAINING_IPADDR + || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTED) + || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTING) { + on_wlan = true; + if (!on_vpn()) + return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_WLAN+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_WLAN); + else + return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_VPN+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_VPN); + } + } + on_wlan = false; + return isFastGSM(i); + } + + @TargetApi(24) + static boolean isFastGSM(int i) { + TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); + int nt = 0; + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) + if (Receiver.mContext.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) + != PackageManager.PERMISSION_GRANTED) { + nt = TelephonyManager.NETWORK_TYPE_UMTS; + } + if (nt == 0) { + if (android.os.Build.VERSION.SDK_INT >= 24) + nt = tm.getDataNetworkType(); + else + nt = tm.getNetworkType(); + } + if (Sipdroid.market) + return false; + if (on_vpn() && (nt >= TelephonyManager.NETWORK_TYPE_EDGE)) + return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_VPN+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_VPN); + if (nt >= TelephonyManager.NETWORK_TYPE_UMTS) + return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_3G+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_3G); + if (nt == TelephonyManager.NETWORK_TYPE_EDGE) + return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_EDGE+(i!=0?i:""), org.sipdroid.sipua.ui.Settings.DEFAULT_EDGE); + return false; + } + + public static int speakermode() { + if (docked > 0 && headset <= 0) + return AudioManager.MODE_NORMAL; + else + return AudioManager.MODE_IN_CALL; + } + + static Handler mHandler = new Handler() { + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_HOLD: + engine(mContext).togglehold(); + break; + case MSG_HANGUP: + engine(mContext).rejectcall(); + break; + } + } + }; + + @Override + public void onReceive(Context context, Intent intent) { + String intentAction = intent.getAction(); + + if (!Sipdroid.on(context)) return; + if (!Sipdroid.release) Log.i("SipUA:",intentAction); + if (mContext == null) mContext = context; + if (intentAction.equals(Intent.ACTION_BOOT_COMPLETED)){ + on_vpn(false); + engine(context).register(); + } else + if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION) || + intentAction.equals(ACTION_EXTERNAL_APPLICATIONS_AVAILABLE) || + intentAction.equals(ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) || + intentAction.equals(Intent.ACTION_PACKAGE_REPLACED)) { + engine(context).register(); + } else + if (intentAction.equals(ACTION_VPN_CONNECTIVITY) && intent.hasExtra("connection_state")) { + String state = intent.getSerializableExtra("connection_state").toString(); + if (state != null && on_vpn() != state.equals("CONNECTED")) { + on_vpn(state.equals("CONNECTED")); + for (SipProvider sip_provider : engine(context).sip_providers) + if (sip_provider != null) + sip_provider.haltConnections(); + engine(context).register(); + } + } else + if (intentAction.equals(ACTION_DATA_STATE_CHANGED)) { + engine(context).registerMore(); + } else + if (intentAction.equals(ACTION_PHONE_STATE_CHANGED) && + !intent.getBooleanExtra(context.getString(R.string.app_name),false)) { + stopRingtone(); + pstn_state = intent.getStringExtra("state"); + pstn_time = SystemClock.elapsedRealtime(); + if (pstn_state.equals("IDLE") && call_state != UserAgent.UA_STATE_IDLE) + broadcastCallStateChanged(null,null); + if (!pstn_state.equals("IDLE") && call_state == UserAgent.UA_STATE_INCALL) + mHandler.sendEmptyMessageDelayed(MSG_HOLD, 5000); + else if (!pstn_state.equals("IDLE") && (call_state == UserAgent.UA_STATE_INCOMING_CALL || call_state == UserAgent.UA_STATE_OUTGOING_CALL)) + mHandler.sendEmptyMessageDelayed(MSG_HANGUP, 5000); + else if (pstn_state.equals("IDLE")) { + mHandler.removeMessages(MSG_HOLD); + mHandler.removeMessages(MSG_HANGUP); + if (call_state == UserAgent.UA_STATE_HOLD) + mHandler.sendEmptyMessageDelayed(MSG_HOLD, 1000); + } + } else + if (intentAction.equals(ACTION_DOCK_EVENT)) { + docked = intent.getIntExtra(EXTRA_DOCK_STATE, -1) & 7; + if (call_state == UserAgent.UA_STATE_INCALL) + engine(mContext).speaker(speakermode()); + } else + if (intentAction.equals(ACTION_SCO_AUDIO_STATE_CHANGED)) { + bluetooth = intent.getIntExtra(EXTRA_SCO_AUDIO_STATE, -1); + progress(); + RtpStreamSender.changed = true; + } else + if (intentAction.equals(Intent.ACTION_HEADSET_PLUG)) { + headset = intent.getIntExtra("state", -1); + if (call_state == UserAgent.UA_STATE_INCALL) + engine(mContext).speaker(speakermode()); + } else + if (intentAction.equals(Intent.ACTION_SCREEN_OFF)) { + if (SipdroidEngine.pwl != null) + for (PowerManager.WakeLock pwl : SipdroidEngine.pwl) + if (pwl != null && pwl.isHeld()) { + pwl.release(); + pwl.acquire(); + } + } + } +} diff --git a/src/org/sipdroid/sipua/ui/ReceiverNew.java b/app/src/main/java/org/sipdroid/sipua/ui/ReceiverNew.java similarity index 97% rename from src/org/sipdroid/sipua/ui/ReceiverNew.java rename to app/src/main/java/org/sipdroid/sipua/ui/ReceiverNew.java index 885c73a..6df2318 100644 --- a/src/org/sipdroid/sipua/ui/ReceiverNew.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/ReceiverNew.java @@ -1,33 +1,33 @@ -package org.sipdroid.sipua.ui; - -import android.annotation.TargetApi; -import android.os.Build; -import android.os.StrictMode; - -/* - * Copyright (C) 2010 The Sipdroid Open Source Project - * Copyright (C) 2007 The Android Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -@TargetApi(Build.VERSION_CODES.GINGERBREAD) public class ReceiverNew { - static void setPolicy() { - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); - StrictMode.setThreadPolicy(policy); - } -} +package org.sipdroid.sipua.ui; + +import android.annotation.TargetApi; +import android.os.Build; +import android.os.StrictMode; + +/* + * Copyright (C) 2010 The Sipdroid Open Source Project + * Copyright (C) 2007 The Android Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +@TargetApi(Build.VERSION_CODES.GINGERBREAD) public class ReceiverNew { + static void setPolicy() { + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); + StrictMode.setThreadPolicy(policy); + } +} diff --git a/src/org/sipdroid/sipua/ui/RegisterService.java b/app/src/main/java/org/sipdroid/sipua/ui/RegisterService.java similarity index 97% rename from src/org/sipdroid/sipua/ui/RegisterService.java rename to app/src/main/java/org/sipdroid/sipua/ui/RegisterService.java index 69d2559..add63b9 100644 --- a/src/org/sipdroid/sipua/ui/RegisterService.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/RegisterService.java @@ -1,83 +1,83 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import org.sipdroid.media.RtpStreamReceiver; - -import android.app.Notification; -import android.app.Service; -import android.content.Intent; -import android.content.IntentFilter; -import android.net.ConnectivityManager; -import android.net.wifi.WifiManager; -import android.os.Build; -import android.os.IBinder; - -public class RegisterService extends Service { - Receiver m_receiver; - Caller m_caller; - - public void onDestroy() { - super.onDestroy(); - if (m_receiver != null) { - unregisterReceiver(m_receiver); - m_receiver = null; - } - Receiver.alarm(0, OneShotAlarm2.class); - } - - @Override - public void onCreate() { - super.onCreate(); - Receiver.sContext = this; - if (Receiver.mContext == null) Receiver.mContext = this; - if (m_receiver == null) { - IntentFilter intentfilter = new IntentFilter(); - intentfilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - intentfilter.addAction(Receiver.ACTION_DATA_STATE_CHANGED); - intentfilter.addAction(Receiver.ACTION_PHONE_STATE_CHANGED); - intentfilter.addAction(Receiver.ACTION_DOCK_EVENT); - intentfilter.addAction(Intent.ACTION_HEADSET_PLUG); - intentfilter.addAction(Intent.ACTION_USER_PRESENT); - intentfilter.addAction(Intent.ACTION_SCREEN_OFF); - intentfilter.addAction(Intent.ACTION_SCREEN_ON); - intentfilter.addAction(Receiver.ACTION_VPN_CONNECTIVITY); - intentfilter.addAction(Receiver.ACTION_SCO_AUDIO_STATE_CHANGED); - intentfilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); - registerReceiver(m_receiver = new Receiver(), intentfilter); - intentfilter = new IntentFilter(); - } - Receiver.engine(this).isRegistered(); - } - - @Override - public void onStart(Intent intent, int id) { - super.onStart(intent,id); - RtpStreamReceiver.restoreSettings(); - Receiver.alarm(10*60, OneShotAlarm2.class); - } - - @Override - public IBinder onBind(Intent arg0) { - return null; - } - -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import org.sipdroid.media.RtpStreamReceiver; + +import android.app.Notification; +import android.app.Service; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.os.IBinder; + +public class RegisterService extends Service { + Receiver m_receiver; + Caller m_caller; + + public void onDestroy() { + super.onDestroy(); + if (m_receiver != null) { + unregisterReceiver(m_receiver); + m_receiver = null; + } + Receiver.alarm(0, OneShotAlarm2.class); + } + + @Override + public void onCreate() { + super.onCreate(); + Receiver.sContext = this; + if (Receiver.mContext == null) Receiver.mContext = this; + if (m_receiver == null) { + IntentFilter intentfilter = new IntentFilter(); + intentfilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + intentfilter.addAction(Receiver.ACTION_DATA_STATE_CHANGED); + intentfilter.addAction(Receiver.ACTION_PHONE_STATE_CHANGED); + intentfilter.addAction(Receiver.ACTION_DOCK_EVENT); + intentfilter.addAction(Intent.ACTION_HEADSET_PLUG); + intentfilter.addAction(Intent.ACTION_USER_PRESENT); + intentfilter.addAction(Intent.ACTION_SCREEN_OFF); + intentfilter.addAction(Intent.ACTION_SCREEN_ON); + intentfilter.addAction(Receiver.ACTION_VPN_CONNECTIVITY); + intentfilter.addAction(Receiver.ACTION_SCO_AUDIO_STATE_CHANGED); + intentfilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); + registerReceiver(m_receiver = new Receiver(), intentfilter); + intentfilter = new IntentFilter(); + } + Receiver.engine(this).isRegistered(); + } + + @Override + public void onStart(Intent intent, int id) { + super.onStart(intent,id); + RtpStreamReceiver.restoreSettings(); + Receiver.alarm(10*60, OneShotAlarm2.class); + } + + @Override + public IBinder onBind(Intent arg0) { + return null; + } + +} diff --git a/src/org/sipdroid/sipua/ui/SIP.java b/app/src/main/java/org/sipdroid/sipua/ui/SIP.java similarity index 97% rename from src/org/sipdroid/sipua/ui/SIP.java rename to app/src/main/java/org/sipdroid/sipua/ui/SIP.java index 45cb317..d39c70b 100644 --- a/src/org/sipdroid/sipua/ui/SIP.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/SIP.java @@ -1,68 +1,68 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -import android.Manifest; -import android.app.Activity; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -import android.os.SystemClock; -import android.preference.PreferenceManager; - -public class SIP extends Activity { - - void callPSTN(String uri) { - String number; - - if (uri.indexOf(":") >= 0) { - number = uri.substring(uri.indexOf(":")+1); - if (!number.equals("")) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) - if (checkSelfPermission(Manifest.permission.CALL_PHONE) - != PackageManager.PERMISSION_GRANTED) { - String[] perms = {Manifest.permission.CALL_PHONE}; - requestPermissions(perms,0); - return; - } - Intent intent = new Intent(Intent.ACTION_CALL, - Uri.fromParts(Uri.decode(number).contains("@")?"sipdroid":"tel", Uri.decode(number)+ - (PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_PSTN) ? "+" : ""), null)); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - Caller.noexclude = SystemClock.elapsedRealtime(); - startActivity(intent); - } - } - } - - @Override - public void onCreate(Bundle saved) { - super.onCreate(saved); - Intent intent; - Uri uri; - Sipdroid.on(this,true); - if ((intent = getIntent()) != null - && (uri = intent.getData()) != null) - callPSTN(uri.toString()); - finish(); - } -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import android.Manifest; +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.os.Bundle; +import android.os.SystemClock; +import android.preference.PreferenceManager; + +public class SIP extends Activity { + + void callPSTN(String uri) { + String number; + + if (uri.indexOf(":") >= 0) { + number = uri.substring(uri.indexOf(":")+1); + if (!number.equals("")) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) + if (checkSelfPermission(Manifest.permission.CALL_PHONE) + != PackageManager.PERMISSION_GRANTED) { + String[] perms = {Manifest.permission.CALL_PHONE}; + requestPermissions(perms,0); + return; + } + Intent intent = new Intent(Intent.ACTION_CALL, + Uri.fromParts(Uri.decode(number).contains("@")?"sipdroid":"tel", Uri.decode(number)+ + (PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_PREF, Settings.DEFAULT_PREF).equals(Settings.VAL_PREF_PSTN) ? "+" : ""), null)); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Caller.noexclude = SystemClock.elapsedRealtime(); + startActivity(intent); + } + } + } + + @Override + public void onCreate(Bundle saved) { + super.onCreate(saved); + Intent intent; + Uri uri; + Sipdroid.on(this,true); + if ((intent = getIntent()) != null + && (uri = intent.getData()) != null) + callPSTN(uri.toString()); + finish(); + } +} diff --git a/src/org/sipdroid/sipua/ui/SIPUri.java b/app/src/main/java/org/sipdroid/sipua/ui/SIPUri.java similarity index 100% rename from src/org/sipdroid/sipua/ui/SIPUri.java rename to app/src/main/java/org/sipdroid/sipua/ui/SIPUri.java diff --git a/src/org/sipdroid/sipua/ui/Settings.java b/app/src/main/java/org/sipdroid/sipua/ui/Settings.java similarity index 99% rename from src/org/sipdroid/sipua/ui/Settings.java rename to app/src/main/java/org/sipdroid/sipua/ui/Settings.java index b9c0ae8..d31d670 100644 --- a/src/org/sipdroid/sipua/ui/Settings.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/Settings.java @@ -271,7 +271,7 @@ void reload() { } private void setDefaultValues() { - settings = getSharedPreferences(sharedPrefsFile, Integer.parseInt(Build.VERSION.SDK) >= 11?4:MODE_PRIVATE); + settings = getSharedPreferences(sharedPrefsFile, MODE_MULTI_PROCESS); for (int i = 0; i < SipdroidEngine.LINES; i++) { String j = (i!=0?""+i:""); diff --git a/src/org/sipdroid/sipua/ui/SettingsNew.java b/app/src/main/java/org/sipdroid/sipua/ui/SettingsNew.java similarity index 96% rename from src/org/sipdroid/sipua/ui/SettingsNew.java rename to app/src/main/java/org/sipdroid/sipua/ui/SettingsNew.java index faef2b6..492a167 100644 --- a/src/org/sipdroid/sipua/ui/SettingsNew.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/SettingsNew.java @@ -1,27 +1,27 @@ -package org.sipdroid.sipua.ui; - -import java.lang.reflect.Method; - -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.PowerManager; - -public class SettingsNew { - static void ignoreBattery(Context context) { - try { - Intent intent = new Intent(); - String packageName = context.getPackageName(); - PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - Method m = pm.getClass().getMethod("isIgnoringBatteryOptimizations",new Class[] { String.class }); - - if (!(Boolean)m.invoke(pm,packageName)) { - intent.setAction("android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"); - intent.setData(Uri.parse("package:" + packageName)); - } - context.startActivity(intent); - } catch (Exception e) { - e.printStackTrace(); - } - } -} +package org.sipdroid.sipua.ui; + +import java.lang.reflect.Method; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.PowerManager; + +public class SettingsNew { + static void ignoreBattery(Context context) { + try { + Intent intent = new Intent(); + String packageName = context.getPackageName(); + PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + Method m = pm.getClass().getMethod("isIgnoringBatteryOptimizations",new Class[] { String.class }); + + if (!(Boolean)m.invoke(pm,packageName)) { + intent.setAction("android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"); + intent.setData(Uri.parse("package:" + packageName)); + } + context.startActivity(intent); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/org/sipdroid/sipua/ui/SipRingtonePreference.java b/app/src/main/java/org/sipdroid/sipua/ui/SipRingtonePreference.java similarity index 97% rename from src/org/sipdroid/sipua/ui/SipRingtonePreference.java rename to app/src/main/java/org/sipdroid/sipua/ui/SipRingtonePreference.java index 3402a83..904b328 100644 --- a/src/org/sipdroid/sipua/ui/SipRingtonePreference.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/SipRingtonePreference.java @@ -1,68 +1,68 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.sipdroid.sipua.ui; - -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences.Editor; -import android.media.RingtoneManager; -import android.net.Uri; -import android.preference.PreferenceManager; -import android.preference.RingtonePreference; -import android.provider.Settings; -import android.text.TextUtils; -import android.util.AttributeSet; - -public class SipRingtonePreference extends RingtonePreference -{ - private Context mContext; - - public SipRingtonePreference(Context context, AttributeSet attrs) - { - super(context, attrs); - mContext = context; - } - - @Override - protected void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) - { - super.onPrepareRingtonePickerIntent(ringtonePickerIntent); - ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); - ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); - ringtonePickerIntent.putExtras(new Intent( RingtoneManager.ACTION_RINGTONE_PICKER)); - } - - @Override - protected void onSaveRingtone(Uri ringtoneUri) - { - Editor edit = PreferenceManager.getDefaultSharedPreferences(mContext).edit(); - edit.putString(org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE, ringtoneUri != null ? ringtoneUri.toString() : org.sipdroid.sipua.ui.Settings.DEFAULT_SIPRINGTONE); - edit.commit(); - } - - @Override - protected Uri onRestoreRingtone() - { - String uriString = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE, - Settings.System.DEFAULT_RINGTONE_URI.toString()); - return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null; - } -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.sipdroid.sipua.ui; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences.Editor; +import android.media.RingtoneManager; +import android.net.Uri; +import android.preference.PreferenceManager; +import android.preference.RingtonePreference; +import android.provider.Settings; +import android.text.TextUtils; +import android.util.AttributeSet; + +public class SipRingtonePreference extends RingtonePreference +{ + private Context mContext; + + public SipRingtonePreference(Context context, AttributeSet attrs) + { + super(context, attrs); + mContext = context; + } + + @Override + protected void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) + { + super.onPrepareRingtonePickerIntent(ringtonePickerIntent); + ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); + ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); + ringtonePickerIntent.putExtras(new Intent( RingtoneManager.ACTION_RINGTONE_PICKER)); + } + + @Override + protected void onSaveRingtone(Uri ringtoneUri) + { + Editor edit = PreferenceManager.getDefaultSharedPreferences(mContext).edit(); + edit.putString(org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE, ringtoneUri != null ? ringtoneUri.toString() : org.sipdroid.sipua.ui.Settings.DEFAULT_SIPRINGTONE); + edit.commit(); + } + + @Override + protected Uri onRestoreRingtone() + { + String uriString = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE, + Settings.System.DEFAULT_RINGTONE_URI.toString()); + return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null; + } +} diff --git a/src/org/sipdroid/sipua/ui/Sipdroid.java b/app/src/main/java/org/sipdroid/sipua/ui/Sipdroid.java similarity index 99% rename from src/org/sipdroid/sipua/ui/Sipdroid.java rename to app/src/main/java/org/sipdroid/sipua/ui/Sipdroid.java index a934bb9..d66eea5 100644 --- a/src/org/sipdroid/sipua/ui/Sipdroid.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/Sipdroid.java @@ -100,7 +100,8 @@ public void onStart() { Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_CALL_LOG, - Manifest.permission.RECORD_AUDIO + Manifest.permission.RECORD_AUDIO, + Manifest.permission.BLUETOOTH_CONNECT }; for(String perm: perms) if (checkSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) { diff --git a/src/org/sipdroid/sipua/ui/SipdroidListener.java b/app/src/main/java/org/sipdroid/sipua/ui/SipdroidListener.java similarity index 97% rename from src/org/sipdroid/sipua/ui/SipdroidListener.java rename to app/src/main/java/org/sipdroid/sipua/ui/SipdroidListener.java index 99fe7b8..1fd8d99 100644 --- a/src/org/sipdroid/sipua/ui/SipdroidListener.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/SipdroidListener.java @@ -1,26 +1,26 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -public interface SipdroidListener { - - public void onHangup(); -} +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +public interface SipdroidListener { + + public void onHangup(); +} diff --git a/src/org/sipdroid/sipua/ui/VideoCamera.java b/app/src/main/java/org/sipdroid/sipua/ui/VideoCamera.java similarity index 97% rename from src/org/sipdroid/sipua/ui/VideoCamera.java rename to app/src/main/java/org/sipdroid/sipua/ui/VideoCamera.java index 453a97d..99862dd 100644 --- a/src/org/sipdroid/sipua/ui/VideoCamera.java +++ b/app/src/main/java/org/sipdroid/sipua/ui/VideoCamera.java @@ -1,668 +1,668 @@ -package org.sipdroid.sipua.ui; - -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * Copyright (C) 2007 The Android Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -/* -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Method; -import java.net.InetAddress; -import java.util.ArrayList; - -import org.sipdroid.media.RtpStreamReceiver; -import org.sipdroid.media.RtpStreamSender; -import org.sipdroid.net.RtpPacket; -import org.sipdroid.net.RtpSocket; -import org.sipdroid.net.SipdroidSocket; -import org.sipdroid.sipua.R; - -import android.content.Context; -import android.hardware.Camera; -import android.hardware.Camera.CameraInfo; -import android.location.LocationManager; -import android.media.AudioManager; -import android.media.MediaPlayer; -import android.media.MediaRecorder; -import android.net.LocalServerSocket; -import android.net.LocalSocket; -import android.net.LocalSocketAddress; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.os.SystemClock; -import android.preference.PreferenceManager; -import android.provider.MediaStore; -import android.telephony.TelephonyManager; -import android.util.Log; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuItem; -import android.view.SurfaceHolder; -import android.view.View; -import android.view.Window; -import android.view.WindowManager; -import android.view.View.OnClickListener; -import android.view.View.OnLongClickListener; -import android.widget.MediaController; -import android.widget.TextView; -import android.widget.VideoView; - -public class VideoCamera extends CallScreen implements - SipdroidListener, SurfaceHolder.Callback, MediaRecorder.OnErrorListener, MediaPlayer.OnErrorListener, OnClickListener, OnLongClickListener { - - Thread t; - Context mContext = this; - - private static final String TAG = "videocamera"; - - private static int UPDATE_RECORD_TIME = 1; - - private static final float VIDEO_ASPECT_RATIO = 176.0f / 144.0f; - VideoPreview mVideoPreview; - SurfaceHolder mSurfaceHolder = null; - VideoView mVideoFrame; - MediaController mMediaController; - - private MediaRecorder mMediaRecorder; - private boolean mMediaRecorderRecording = false; - - private TextView mRecordingTimeView,mFPS; - - ArrayList mGalleryItems = new ArrayList(); - - View mPostPictureAlert; - LocationManager mLocationManager = null; - - private Handler mHandler = new MainHandler(); - LocalSocket receiver,sender; - LocalServerSocket lss; - int obuffering,opos; - int fps; - - private class MainHandler extends Handler { - @Override - public void handleMessage(Message msg) { - long now = SystemClock.elapsedRealtime(); - long delta = now - Receiver.ccCall.base; - - long seconds = (delta + 500) / 1000; // round to nearest - long minutes = seconds / 60; - long hours = minutes / 60; - long remainderMinutes = minutes - (hours * 60); - long remainderSeconds = seconds - (minutes * 60); - - String secondsString = Long.toString(remainderSeconds); - if (secondsString.length() < 2) { - secondsString = "0" + secondsString; - } - String minutesString = Long.toString(remainderMinutes); - if (minutesString.length() < 2) { - minutesString = "0" + minutesString; - } - String text = minutesString + ":" + secondsString; - if (hours > 0) { - String hoursString = Long.toString(hours); - if (hoursString.length() < 2) { - hoursString = "0" + hoursString; - } - text = hoursString + ":" + text; - } - mRecordingTimeView.setText(text); - if (fps != 0) mFPS.setText(fps+(videoQualityHigh?"h":"l")+"fps"); - if (mVideoFrame != null) { - int buffering = mVideoFrame.getBufferPercentage(),pos = mVideoFrame.getCurrentPosition(); - if (buffering != 100 && buffering != 0) { - mMediaController.show(); - } - if (buffering != 0 && !mMediaRecorderRecording) mVideoPreview.setVisibility(View.INVISIBLE); - if (((obuffering != buffering && buffering == 100) || (opos == 0 && pos > 0)) && rtp_socket != null) { - RtpPacket keepalive = new RtpPacket(new byte[12],0); - keepalive.setPayloadType(125); - try { - rtp_socket.send(keepalive); - } catch (Exception e) { - } - } - obuffering = buffering; - opos = pos; - } - - // Work around a limitation of the T-Mobile G1: The T-Mobile - // hardware blitter can't pixel-accurately scale and clip at the same time, - // and the SurfaceFlinger doesn't attempt to work around this limitation. - // In order to avoid visual corruption we must manually refresh the entire - // surface view when changing any overlapping view's contents. - mVideoPreview.invalidate(); - mHandler.sendEmptyMessageDelayed(UPDATE_RECORD_TIME, 1000); - } - }; - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - - mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - - //setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT); - requestWindowFeature(Window.FEATURE_PROGRESS); - setScreenOnFlag(); - setContentView(R.layout.video_camera); - - mVideoPreview = (VideoPreview) findViewById(R.id.camera_preview); - mVideoPreview.setAspectRatio(VIDEO_ASPECT_RATIO); - - // don't set mSurfaceHolder here. We have it set ONLY within - // surfaceCreated / surfaceDestroyed, other parts of the code - // assume that when it is set, the surface is also set. - SurfaceHolder holder = mVideoPreview.getHolder(); - holder.addCallback(this); - holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); - - mRecordingTimeView = (TextView) findViewById(R.id.recording_time); - mFPS = (TextView) findViewById(R.id.fps); - mVideoFrame = (VideoView) findViewById(R.id.video_frame); - } - - int speakermode; - boolean justplay; - - @Override - public void onStart() { - super.onStart(); - speakermode = Receiver.engine(this).speaker(AudioManager.MODE_NORMAL); - videoQualityHigh = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_VQUALITY, org.sipdroid.sipua.ui.Settings.DEFAULT_VQUALITY).equals("high"); - if ((intent = getIntent()).hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { - int extraVideoQuality = intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); - videoQualityHigh = (extraVideoQuality > 0); - } - } - - @Override - public void onResume() { - if (!Sipdroid.release) Log.i("SipUA:","on resume"); - justplay = intent.hasExtra("justplay"); - if (!justplay) { - receiver = new LocalSocket(); - try { - lss = new LocalServerSocket("Sipdroid"); - receiver.connect(new LocalSocketAddress("Sipdroid")); - receiver.setReceiveBufferSize(500000); - receiver.setSendBufferSize(500000); - sender = lss.accept(); - sender.setReceiveBufferSize(500000); - sender.setSendBufferSize(500000); - } catch (IOException e1) { - if (!Sipdroid.release) e1.printStackTrace(); - super.onResume(); - finish(); - return; - } - checkForCamera(); - mVideoPreview.setVisibility(View.VISIBLE); - if (!mMediaRecorderRecording) initializeVideo(); - startVideoRecording(); - } else if (Receiver.engine(mContext).getRemoteVideo() != 0 && PreferenceManager.getDefaultSharedPreferences(this).getString(org.sipdroid.sipua.ui.Settings.PREF_SERVER, org.sipdroid.sipua.ui.Settings.DEFAULT_SERVER).equals(org.sipdroid.sipua.ui.Settings.DEFAULT_SERVER)) { - mVideoFrame.setVideoURI(Uri.parse("rtsp://"+Receiver.engine(mContext).getRemoteAddr()+"/"+ - Receiver.engine(mContext).getRemoteVideo()+"/sipdroid")); - mVideoFrame.setMediaController(mMediaController = new MediaController(this)); - mVideoFrame.setOnErrorListener(this); - mVideoFrame.requestFocus(); - mVideoFrame.start(); - } - - mRecordingTimeView.setText(""); - mRecordingTimeView.setVisibility(View.VISIBLE); - mHandler.removeMessages(UPDATE_RECORD_TIME); - mHandler.sendEmptyMessage(UPDATE_RECORD_TIME); - super.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - - // This is similar to what mShutterButton.performClick() does, - // but not quite the same. - if (mMediaRecorderRecording) { - stopVideoRecording(); - - try { - lss.close(); - receiver.close(); - sender.close(); - } catch (IOException e) { - if (!Sipdroid.release) e.printStackTrace(); - } - } - - Receiver.engine(this).speaker(speakermode); - finish(); - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - - switch (keyCode) { - // finish for these events - case KeyEvent.KEYCODE_CALL: - Receiver.engine(this).togglehold(); - case KeyEvent.KEYCODE_BACK: - finish(); - return true; - - case KeyEvent.KEYCODE_CAMERA: - // Disable the CAMERA button while in-call since it's too - // easy to press accidentally. - return true; - - case KeyEvent.KEYCODE_VOLUME_DOWN: - case KeyEvent.KEYCODE_VOLUME_UP: - RtpStreamReceiver.adjust(keyCode,true,true); - return true; - } - - return super.onKeyDown(keyCode, event); - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - boolean result = super.onPrepareOptionsMenu(menu); - - if (mMediaRecorderRecording) menu.findItem(VIDEO_MENU_ITEM).setVisible(false); - return result; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case VIDEO_MENU_ITEM: - intent.removeExtra("justplay"); - onResume(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { - if (!justplay && !mMediaRecorderRecording) initializeVideo(); - } - - public void surfaceCreated(SurfaceHolder holder) { - mSurfaceHolder = holder; - } - - public void surfaceDestroyed(SurfaceHolder holder) { - mSurfaceHolder = null; - } - - boolean isAvailableSprintFFC,useFront = true; - - private void checkForCamera() - { - try - { - Class.forName("android.hardware.HtcFrontFacingCamera"); - isAvailableSprintFFC = true; - } - catch (Exception ex) - { - isAvailableSprintFFC = false; - } - } - - boolean videoQualityHigh; - Camera mCamera; - - // initializeVideo() starts preview and prepare media recorder. - // Returns false if initializeVideo fails - private boolean initializeVideo() { - Log.v(TAG, "initializeVideo"); - - if (mSurfaceHolder == null) { - Log.v(TAG, "SurfaceHolder is null"); - return false; - } - - mMediaRecorderRecording = true; - - if (mMediaRecorder == null) - mMediaRecorder = new MediaRecorder(); - else - mMediaRecorder.reset(); - if (mCamera != null) { - if (Integer.parseInt(Build.VERSION.SDK) >= 8) - VideoCameraNew2.reconnect(mCamera); - mCamera.release(); - mCamera = null; - } - - if (useFront && Integer.parseInt(Build.VERSION.SDK) >= 5) { - if (isAvailableSprintFFC) - { - try - { - Method method = Class.forName("android.hardware.HtcFrontFacingCamera").getDeclaredMethod("getCamera", null); - mCamera = (Camera) method.invoke(null, null); - } - catch (Exception ex) - { - Log.d(TAG, ex.toString()); - } - } else { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { - mCamera = VideoCameraNew_SDK9.open(); - } else { - mCamera = Camera.open(); - Camera.Parameters parameters = mCamera.getParameters(); - parameters.set("camera-id", 2); - mCamera.setParameters(parameters); - } - } - VideoCameraNew.unlock(mCamera); - mMediaRecorder.setCamera(mCamera); - mVideoPreview.setOnClickListener(this); - } - mVideoPreview.setOnLongClickListener(this); - mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); - mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); - mMediaRecorder.setOutputFile(sender.getFileDescriptor()); - if (Integer.parseInt(Build.VERSION.SDK) < 14) - mMediaRecorder.setVideoFrameRate(20); - else if (Integer.parseInt(Build.VERSION.SDK) > 16) - mMediaRecorder.setVideoFrameRate(30); - - if (videoQualityHigh) { - mMediaRecorder.setVideoSize(352,288); - } else { - mMediaRecorder.setVideoSize(176,144); - } - mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); - mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); - - try { - mMediaRecorder.prepare(); - mMediaRecorder.setOnErrorListener(this); - mMediaRecorder.start(); - } catch (IOException exception) { - releaseMediaRecorder(); - finish(); - return false; - } - return true; - } - - private void releaseMediaRecorder() { - Log.v(TAG, "Releasing media recorder."); - if (mMediaRecorder != null) { - mMediaRecorder.reset(); - if (mCamera != null) { - if (Integer.parseInt(Build.VERSION.SDK) >= 8) - VideoCameraNew2.reconnect(mCamera); - mCamera.release(); - mCamera = null; - } - mMediaRecorder.release(); - mMediaRecorder = null; - } - } - - public void onError(MediaRecorder mr, int what, int extra) { - if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { - finish(); - } - } - - boolean change; - - private void startVideoRecording() { - Log.v(TAG, "startVideoRecording"); - - if (Receiver.listener_video == null) { - Receiver.listener_video = this; - RtpStreamSender.delay = 1; - - try { - if (rtp_socket == null) - rtp_socket = new RtpSocket(new SipdroidSocket(Receiver.engine(mContext).getLocalVideo()), - InetAddress.getByName(Receiver.engine(mContext).getRemoteAddr()), - Receiver.engine(mContext).getRemoteVideo()); - } catch (Exception e) { - if (!Sipdroid.release) e.printStackTrace(); - return; - } - - (t = new Thread() { - public void run() { - int frame_size = 1400; - byte[] buffer = new byte[frame_size + 14]; - buffer[12] = 4; - RtpPacket rtp_packet = new RtpPacket(buffer, 0); - int seqn = 0; - int num,number = 0,src,dest,len = 0,head = 0,lasthead = 0,lasthead2 = 0,cnt = 0,stable = 0; - long now,lasttime = 0; - double avgrate = videoQualityHigh?45000:24000; - double avglen = avgrate/20; - - InputStream fis = null; - try { - fis = receiver.getInputStream(); - } catch (IOException e1) { - if (!Sipdroid.release) e1.printStackTrace(); - rtp_socket.getDatagramSocket().close(); - return; - } - - rtp_packet.setPayloadType(103); - while (Receiver.listener_video != null && videoValid()) { - num = -1; - try { - num = fis.read(buffer,14+number,frame_size-number); - } catch (IOException e) { - if (!Sipdroid.release) e.printStackTrace(); - break; - } - if (num < 0) { - try { - sleep(20); - } catch (InterruptedException e) { - break; - } - continue; - } - number += num; - head += num; - try { - now = SystemClock.elapsedRealtime(); - if (lasthead != head+fis.available() && ++stable >= 5 && now-lasttime > 700) { - if (cnt != 0 && len != 0) - avglen = len/cnt; - if (lasttime != 0) { - fps = (int)((double)cnt*1000/(now-lasttime)); - avgrate = (double)((head+fis.available())-lasthead2)*1000/(now-lasttime); - } - lasttime = now; - lasthead = head+fis.available(); - lasthead2 = head; - len = cnt = stable = 0; - } - } catch (IOException e1) { - if (!Sipdroid.release) e1.printStackTrace(); - break; - } - - for (num = 14; num <= 14+number-3; num++) - if (buffer[num] == 0 && buffer[num+1] == 0 && (buffer[num+2]&0xfc) == 0x80) { - break; - } - if (num > 14+number-3) { - num = 0; - rtp_packet.setMarker(false); - } else { - num = 14+number - num; - rtp_packet.setMarker(true); - } - - rtp_packet.setSequenceNumber(seqn++); - rtp_packet.setPayloadLength(number-num+2); - if (seqn > 10) try { - rtp_socket.send(rtp_packet); - len += number-num; - } catch (Exception e) { - if (!Sipdroid.release) e.printStackTrace(); - break; - } - - if (num > 0) { - num -= 2; - dest = 14; - src = 14+number - num; - if (num > 0 && buffer[src] == 0) { - src++; - num--; - } - number = num; - while (num-- > 0) - buffer[dest++] = buffer[src++]; - buffer[12] = 4; - - cnt++; - try { - if (avgrate != 0) - Thread.sleep((int)(avglen/avgrate*1000)); - } catch (Exception e) { - break; - } - rtp_packet.setTimestamp(SystemClock.elapsedRealtime()*90); - } else { - number = 0; - buffer[12] = 0; - } - if (change) { - change = false; - long time = SystemClock.elapsedRealtime(); - - try { - while (fis.read(buffer,14,frame_size) > 0 && - SystemClock.elapsedRealtime()-time < 3000); - } catch (Exception e) { - } - number = 0; - buffer[12] = 0; - } - } - rtp_socket.getDatagramSocket().close(); - try { - while (fis.read(buffer,0,frame_size) > 0); - } catch (IOException e) { - } - } - }).start(); - } - } - - private void stopVideoRecording() { - Log.v(TAG, "stopVideoRecording"); - if (mMediaRecorderRecording || mMediaRecorder != null) { - Receiver.listener_video = null; - t.interrupt(); - RtpStreamSender.delay = 0; - - if (mMediaRecorderRecording && mMediaRecorder != null) { - try { - mMediaRecorder.setOnErrorListener(null); - mMediaRecorder.setOnInfoListener(null); - mMediaRecorder.stop(); - } catch (RuntimeException e) { - Log.e(TAG, "stop fail: " + e.getMessage()); - } - - mMediaRecorderRecording = false; - } - releaseMediaRecorder(); - } - } - - private void setScreenOnFlag() { - Window w = getWindow(); - final int keepScreenOnFlag = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; - if ((w.getAttributes().flags & keepScreenOnFlag) == 0) { - w.addFlags(keepScreenOnFlag); - } - } - - public void onHangup() { - finish(); - } - - @Override - public boolean onKeyUp(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_VOLUME_DOWN: - case KeyEvent.KEYCODE_VOLUME_UP: - RtpStreamReceiver.adjust(keyCode,false,true); - return true; - case KeyEvent.KEYCODE_ENDCALL: - if (Receiver.pstn_state == null || - (Receiver.pstn_state.equals("IDLE") && (SystemClock.elapsedRealtime()-Receiver.pstn_time) > 3000)) { - Receiver.engine(mContext).rejectcall(); - return true; - } - break; - } - return false; - } - - static TelephonyManager tm; - - static boolean videoValid() { - if (Receiver.on_wlan) - return true; - if (tm == null) tm = (TelephonyManager) Receiver.mContext.getSystemService(Context.TELEPHONY_SERVICE); - if (tm.getNetworkType() < TelephonyManager.NETWORK_TYPE_UMTS) - return false; - return true; - } - - @Override - public boolean onError(MediaPlayer mp, int what, int extra) { - return true; - } - - @Override - public void onClick(View v) { - useFront = !useFront; - initializeVideo(); - change = true; - } - - @Override - public boolean onLongClick(View v) { - videoQualityHigh = !videoQualityHigh; - initializeVideo(); - change = true; - return true; - } - -} -*/ +package org.sipdroid.sipua.ui; + +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * Copyright (C) 2007 The Android Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.InetAddress; +import java.util.ArrayList; + +import org.sipdroid.media.RtpStreamReceiver; +import org.sipdroid.media.RtpStreamSender; +import org.sipdroid.net.RtpPacket; +import org.sipdroid.net.RtpSocket; +import org.sipdroid.net.SipdroidSocket; +import org.sipdroid.sipua.R; + +import android.content.Context; +import android.hardware.Camera; +import android.hardware.Camera.CameraInfo; +import android.location.LocationManager; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.media.MediaRecorder; +import android.net.LocalServerSocket; +import android.net.LocalSocket; +import android.net.LocalSocketAddress; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.SystemClock; +import android.preference.PreferenceManager; +import android.provider.MediaStore; +import android.telephony.TelephonyManager; +import android.util.Log; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; +import android.view.SurfaceHolder; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; +import android.view.View.OnClickListener; +import android.view.View.OnLongClickListener; +import android.widget.MediaController; +import android.widget.TextView; +import android.widget.VideoView; + +public class VideoCamera extends CallScreen implements + SipdroidListener, SurfaceHolder.Callback, MediaRecorder.OnErrorListener, MediaPlayer.OnErrorListener, OnClickListener, OnLongClickListener { + + Thread t; + Context mContext = this; + + private static final String TAG = "videocamera"; + + private static int UPDATE_RECORD_TIME = 1; + + private static final float VIDEO_ASPECT_RATIO = 176.0f / 144.0f; + VideoPreview mVideoPreview; + SurfaceHolder mSurfaceHolder = null; + VideoView mVideoFrame; + MediaController mMediaController; + + private MediaRecorder mMediaRecorder; + private boolean mMediaRecorderRecording = false; + + private TextView mRecordingTimeView,mFPS; + + ArrayList mGalleryItems = new ArrayList(); + + View mPostPictureAlert; + LocationManager mLocationManager = null; + + private Handler mHandler = new MainHandler(); + LocalSocket receiver,sender; + LocalServerSocket lss; + int obuffering,opos; + int fps; + + private class MainHandler extends Handler { + @Override + public void handleMessage(Message msg) { + long now = SystemClock.elapsedRealtime(); + long delta = now - Receiver.ccCall.base; + + long seconds = (delta + 500) / 1000; // round to nearest + long minutes = seconds / 60; + long hours = minutes / 60; + long remainderMinutes = minutes - (hours * 60); + long remainderSeconds = seconds - (minutes * 60); + + String secondsString = Long.toString(remainderSeconds); + if (secondsString.length() < 2) { + secondsString = "0" + secondsString; + } + String minutesString = Long.toString(remainderMinutes); + if (minutesString.length() < 2) { + minutesString = "0" + minutesString; + } + String text = minutesString + ":" + secondsString; + if (hours > 0) { + String hoursString = Long.toString(hours); + if (hoursString.length() < 2) { + hoursString = "0" + hoursString; + } + text = hoursString + ":" + text; + } + mRecordingTimeView.setText(text); + if (fps != 0) mFPS.setText(fps+(videoQualityHigh?"h":"l")+"fps"); + if (mVideoFrame != null) { + int buffering = mVideoFrame.getBufferPercentage(),pos = mVideoFrame.getCurrentPosition(); + if (buffering != 100 && buffering != 0) { + mMediaController.show(); + } + if (buffering != 0 && !mMediaRecorderRecording) mVideoPreview.setVisibility(View.INVISIBLE); + if (((obuffering != buffering && buffering == 100) || (opos == 0 && pos > 0)) && rtp_socket != null) { + RtpPacket keepalive = new RtpPacket(new byte[12],0); + keepalive.setPayloadType(125); + try { + rtp_socket.send(keepalive); + } catch (Exception e) { + } + } + obuffering = buffering; + opos = pos; + } + + // Work around a limitation of the T-Mobile G1: The T-Mobile + // hardware blitter can't pixel-accurately scale and clip at the same time, + // and the SurfaceFlinger doesn't attempt to work around this limitation. + // In order to avoid visual corruption we must manually refresh the entire + // surface view when changing any overlapping view's contents. + mVideoPreview.invalidate(); + mHandler.sendEmptyMessageDelayed(UPDATE_RECORD_TIME, 1000); + } + }; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + + mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + + //setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT); + requestWindowFeature(Window.FEATURE_PROGRESS); + setScreenOnFlag(); + setContentView(R.layout.video_camera); + + mVideoPreview = (VideoPreview) findViewById(R.id.camera_preview); + mVideoPreview.setAspectRatio(VIDEO_ASPECT_RATIO); + + // don't set mSurfaceHolder here. We have it set ONLY within + // surfaceCreated / surfaceDestroyed, other parts of the code + // assume that when it is set, the surface is also set. + SurfaceHolder holder = mVideoPreview.getHolder(); + holder.addCallback(this); + holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); + + mRecordingTimeView = (TextView) findViewById(R.id.recording_time); + mFPS = (TextView) findViewById(R.id.fps); + mVideoFrame = (VideoView) findViewById(R.id.video_frame); + } + + int speakermode; + boolean justplay; + + @Override + public void onStart() { + super.onStart(); + speakermode = Receiver.engine(this).speaker(AudioManager.MODE_NORMAL); + videoQualityHigh = PreferenceManager.getDefaultSharedPreferences(mContext).getString(org.sipdroid.sipua.ui.Settings.PREF_VQUALITY, org.sipdroid.sipua.ui.Settings.DEFAULT_VQUALITY).equals("high"); + if ((intent = getIntent()).hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { + int extraVideoQuality = intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); + videoQualityHigh = (extraVideoQuality > 0); + } + } + + @Override + public void onResume() { + if (!Sipdroid.release) Log.i("SipUA:","on resume"); + justplay = intent.hasExtra("justplay"); + if (!justplay) { + receiver = new LocalSocket(); + try { + lss = new LocalServerSocket("Sipdroid"); + receiver.connect(new LocalSocketAddress("Sipdroid")); + receiver.setReceiveBufferSize(500000); + receiver.setSendBufferSize(500000); + sender = lss.accept(); + sender.setReceiveBufferSize(500000); + sender.setSendBufferSize(500000); + } catch (IOException e1) { + if (!Sipdroid.release) e1.printStackTrace(); + super.onResume(); + finish(); + return; + } + checkForCamera(); + mVideoPreview.setVisibility(View.VISIBLE); + if (!mMediaRecorderRecording) initializeVideo(); + startVideoRecording(); + } else if (Receiver.engine(mContext).getRemoteVideo() != 0 && PreferenceManager.getDefaultSharedPreferences(this).getString(org.sipdroid.sipua.ui.Settings.PREF_SERVER, org.sipdroid.sipua.ui.Settings.DEFAULT_SERVER).equals(org.sipdroid.sipua.ui.Settings.DEFAULT_SERVER)) { + mVideoFrame.setVideoURI(Uri.parse("rtsp://"+Receiver.engine(mContext).getRemoteAddr()+"/"+ + Receiver.engine(mContext).getRemoteVideo()+"/sipdroid")); + mVideoFrame.setMediaController(mMediaController = new MediaController(this)); + mVideoFrame.setOnErrorListener(this); + mVideoFrame.requestFocus(); + mVideoFrame.start(); + } + + mRecordingTimeView.setText(""); + mRecordingTimeView.setVisibility(View.VISIBLE); + mHandler.removeMessages(UPDATE_RECORD_TIME); + mHandler.sendEmptyMessage(UPDATE_RECORD_TIME); + super.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + + // This is similar to what mShutterButton.performClick() does, + // but not quite the same. + if (mMediaRecorderRecording) { + stopVideoRecording(); + + try { + lss.close(); + receiver.close(); + sender.close(); + } catch (IOException e) { + if (!Sipdroid.release) e.printStackTrace(); + } + } + + Receiver.engine(this).speaker(speakermode); + finish(); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + + switch (keyCode) { + // finish for these events + case KeyEvent.KEYCODE_CALL: + Receiver.engine(this).togglehold(); + case KeyEvent.KEYCODE_BACK: + finish(); + return true; + + case KeyEvent.KEYCODE_CAMERA: + // Disable the CAMERA button while in-call since it's too + // easy to press accidentally. + return true; + + case KeyEvent.KEYCODE_VOLUME_DOWN: + case KeyEvent.KEYCODE_VOLUME_UP: + RtpStreamReceiver.adjust(keyCode,true,true); + return true; + } + + return super.onKeyDown(keyCode, event); + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + boolean result = super.onPrepareOptionsMenu(menu); + + if (mMediaRecorderRecording) menu.findItem(VIDEO_MENU_ITEM).setVisible(false); + return result; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case VIDEO_MENU_ITEM: + intent.removeExtra("justplay"); + onResume(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + if (!justplay && !mMediaRecorderRecording) initializeVideo(); + } + + public void surfaceCreated(SurfaceHolder holder) { + mSurfaceHolder = holder; + } + + public void surfaceDestroyed(SurfaceHolder holder) { + mSurfaceHolder = null; + } + + boolean isAvailableSprintFFC,useFront = true; + + private void checkForCamera() + { + try + { + Class.forName("android.hardware.HtcFrontFacingCamera"); + isAvailableSprintFFC = true; + } + catch (Exception ex) + { + isAvailableSprintFFC = false; + } + } + + boolean videoQualityHigh; + Camera mCamera; + + // initializeVideo() starts preview and prepare media recorder. + // Returns false if initializeVideo fails + private boolean initializeVideo() { + Log.v(TAG, "initializeVideo"); + + if (mSurfaceHolder == null) { + Log.v(TAG, "SurfaceHolder is null"); + return false; + } + + mMediaRecorderRecording = true; + + if (mMediaRecorder == null) + mMediaRecorder = new MediaRecorder(); + else + mMediaRecorder.reset(); + if (mCamera != null) { + if (Integer.parseInt(Build.VERSION.SDK) >= 8) + VideoCameraNew2.reconnect(mCamera); + mCamera.release(); + mCamera = null; + } + + if (useFront && Integer.parseInt(Build.VERSION.SDK) >= 5) { + if (isAvailableSprintFFC) + { + try + { + Method method = Class.forName("android.hardware.HtcFrontFacingCamera").getDeclaredMethod("getCamera", null); + mCamera = (Camera) method.invoke(null, null); + } + catch (Exception ex) + { + Log.d(TAG, ex.toString()); + } + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + mCamera = VideoCameraNew_SDK9.open(); + } else { + mCamera = Camera.open(); + Camera.Parameters parameters = mCamera.getParameters(); + parameters.set("camera-id", 2); + mCamera.setParameters(parameters); + } + } + VideoCameraNew.unlock(mCamera); + mMediaRecorder.setCamera(mCamera); + mVideoPreview.setOnClickListener(this); + } + mVideoPreview.setOnLongClickListener(this); + mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); + mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); + mMediaRecorder.setOutputFile(sender.getFileDescriptor()); + if (Integer.parseInt(Build.VERSION.SDK) < 14) + mMediaRecorder.setVideoFrameRate(20); + else if (Integer.parseInt(Build.VERSION.SDK) > 16) + mMediaRecorder.setVideoFrameRate(30); + + if (videoQualityHigh) { + mMediaRecorder.setVideoSize(352,288); + } else { + mMediaRecorder.setVideoSize(176,144); + } + mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); + mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); + + try { + mMediaRecorder.prepare(); + mMediaRecorder.setOnErrorListener(this); + mMediaRecorder.start(); + } catch (IOException exception) { + releaseMediaRecorder(); + finish(); + return false; + } + return true; + } + + private void releaseMediaRecorder() { + Log.v(TAG, "Releasing media recorder."); + if (mMediaRecorder != null) { + mMediaRecorder.reset(); + if (mCamera != null) { + if (Integer.parseInt(Build.VERSION.SDK) >= 8) + VideoCameraNew2.reconnect(mCamera); + mCamera.release(); + mCamera = null; + } + mMediaRecorder.release(); + mMediaRecorder = null; + } + } + + public void onError(MediaRecorder mr, int what, int extra) { + if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { + finish(); + } + } + + boolean change; + + private void startVideoRecording() { + Log.v(TAG, "startVideoRecording"); + + if (Receiver.listener_video == null) { + Receiver.listener_video = this; + RtpStreamSender.delay = 1; + + try { + if (rtp_socket == null) + rtp_socket = new RtpSocket(new SipdroidSocket(Receiver.engine(mContext).getLocalVideo()), + InetAddress.getByName(Receiver.engine(mContext).getRemoteAddr()), + Receiver.engine(mContext).getRemoteVideo()); + } catch (Exception e) { + if (!Sipdroid.release) e.printStackTrace(); + return; + } + + (t = new Thread() { + public void run() { + int frame_size = 1400; + byte[] buffer = new byte[frame_size + 14]; + buffer[12] = 4; + RtpPacket rtp_packet = new RtpPacket(buffer, 0); + int seqn = 0; + int num,number = 0,src,dest,len = 0,head = 0,lasthead = 0,lasthead2 = 0,cnt = 0,stable = 0; + long now,lasttime = 0; + double avgrate = videoQualityHigh?45000:24000; + double avglen = avgrate/20; + + InputStream fis = null; + try { + fis = receiver.getInputStream(); + } catch (IOException e1) { + if (!Sipdroid.release) e1.printStackTrace(); + rtp_socket.getDatagramSocket().close(); + return; + } + + rtp_packet.setPayloadType(103); + while (Receiver.listener_video != null && videoValid()) { + num = -1; + try { + num = fis.read(buffer,14+number,frame_size-number); + } catch (IOException e) { + if (!Sipdroid.release) e.printStackTrace(); + break; + } + if (num < 0) { + try { + sleep(20); + } catch (InterruptedException e) { + break; + } + continue; + } + number += num; + head += num; + try { + now = SystemClock.elapsedRealtime(); + if (lasthead != head+fis.available() && ++stable >= 5 && now-lasttime > 700) { + if (cnt != 0 && len != 0) + avglen = len/cnt; + if (lasttime != 0) { + fps = (int)((double)cnt*1000/(now-lasttime)); + avgrate = (double)((head+fis.available())-lasthead2)*1000/(now-lasttime); + } + lasttime = now; + lasthead = head+fis.available(); + lasthead2 = head; + len = cnt = stable = 0; + } + } catch (IOException e1) { + if (!Sipdroid.release) e1.printStackTrace(); + break; + } + + for (num = 14; num <= 14+number-3; num++) + if (buffer[num] == 0 && buffer[num+1] == 0 && (buffer[num+2]&0xfc) == 0x80) { + break; + } + if (num > 14+number-3) { + num = 0; + rtp_packet.setMarker(false); + } else { + num = 14+number - num; + rtp_packet.setMarker(true); + } + + rtp_packet.setSequenceNumber(seqn++); + rtp_packet.setPayloadLength(number-num+2); + if (seqn > 10) try { + rtp_socket.send(rtp_packet); + len += number-num; + } catch (Exception e) { + if (!Sipdroid.release) e.printStackTrace(); + break; + } + + if (num > 0) { + num -= 2; + dest = 14; + src = 14+number - num; + if (num > 0 && buffer[src] == 0) { + src++; + num--; + } + number = num; + while (num-- > 0) + buffer[dest++] = buffer[src++]; + buffer[12] = 4; + + cnt++; + try { + if (avgrate != 0) + Thread.sleep((int)(avglen/avgrate*1000)); + } catch (Exception e) { + break; + } + rtp_packet.setTimestamp(SystemClock.elapsedRealtime()*90); + } else { + number = 0; + buffer[12] = 0; + } + if (change) { + change = false; + long time = SystemClock.elapsedRealtime(); + + try { + while (fis.read(buffer,14,frame_size) > 0 && + SystemClock.elapsedRealtime()-time < 3000); + } catch (Exception e) { + } + number = 0; + buffer[12] = 0; + } + } + rtp_socket.getDatagramSocket().close(); + try { + while (fis.read(buffer,0,frame_size) > 0); + } catch (IOException e) { + } + } + }).start(); + } + } + + private void stopVideoRecording() { + Log.v(TAG, "stopVideoRecording"); + if (mMediaRecorderRecording || mMediaRecorder != null) { + Receiver.listener_video = null; + t.interrupt(); + RtpStreamSender.delay = 0; + + if (mMediaRecorderRecording && mMediaRecorder != null) { + try { + mMediaRecorder.setOnErrorListener(null); + mMediaRecorder.setOnInfoListener(null); + mMediaRecorder.stop(); + } catch (RuntimeException e) { + Log.e(TAG, "stop fail: " + e.getMessage()); + } + + mMediaRecorderRecording = false; + } + releaseMediaRecorder(); + } + } + + private void setScreenOnFlag() { + Window w = getWindow(); + final int keepScreenOnFlag = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + if ((w.getAttributes().flags & keepScreenOnFlag) == 0) { + w.addFlags(keepScreenOnFlag); + } + } + + public void onHangup() { + finish(); + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_DOWN: + case KeyEvent.KEYCODE_VOLUME_UP: + RtpStreamReceiver.adjust(keyCode,false,true); + return true; + case KeyEvent.KEYCODE_ENDCALL: + if (Receiver.pstn_state == null || + (Receiver.pstn_state.equals("IDLE") && (SystemClock.elapsedRealtime()-Receiver.pstn_time) > 3000)) { + Receiver.engine(mContext).rejectcall(); + return true; + } + break; + } + return false; + } + + static TelephonyManager tm; + + static boolean videoValid() { + if (Receiver.on_wlan) + return true; + if (tm == null) tm = (TelephonyManager) Receiver.mContext.getSystemService(Context.TELEPHONY_SERVICE); + if (tm.getNetworkType() < TelephonyManager.NETWORK_TYPE_UMTS) + return false; + return true; + } + + @Override + public boolean onError(MediaPlayer mp, int what, int extra) { + return true; + } + + @Override + public void onClick(View v) { + useFront = !useFront; + initializeVideo(); + change = true; + } + + @Override + public boolean onLongClick(View v) { + videoQualityHigh = !videoQualityHigh; + initializeVideo(); + change = true; + return true; + } + +} +*/ diff --git a/src/org/zoolu/net/IpAddress.java b/app/src/main/java/org/zoolu/net/IpAddress.java similarity index 96% rename from src/org/zoolu/net/IpAddress.java rename to app/src/main/java/org/zoolu/net/IpAddress.java index b430c03..e5a12cc 100644 --- a/src/org/zoolu/net/IpAddress.java +++ b/app/src/main/java/org/zoolu/net/IpAddress.java @@ -1,179 +1,179 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -import java.net.BindException; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; - -import org.sipdroid.sipua.ui.Receiver; -import org.sipdroid.sipua.ui.Settings; -import org.sipdroid.sipua.ui.Sipdroid; - -import android.preference.PreferenceManager; -import android.content.Context; - -import com.jstun.demo.DiscoveryTest; - -/** - * IpAddress is an IP address. - */ -public class IpAddress { - - /** The host address/name */ - String address; - - /** The InetAddress */ - InetAddress inet_address; - - /** Local IP address */ - public static String localIpAddress = "127.0.0.1"; - - public static Context getUIContext() { - return Receiver.mContext; - } - - // ********************* Protected ********************* - - /** Creates an IpAddress */ - IpAddress(InetAddress iaddress) { - init(null, iaddress); - } - - /** Inits the IpAddress */ - private void init(String address, InetAddress iaddress) { - this.address = address; - this.inet_address = iaddress; - } - - /** Gets the InetAddress */ - InetAddress getInetAddress() { - if (inet_address == null) - try { - inet_address = InetAddress.getByName(address); - } catch (java.net.UnknownHostException e) { - inet_address = null; - } - return inet_address; - } - - // ********************** Public *********************** - - /** Creates an IpAddress */ - public IpAddress(String address) { - init(address, null); - } - - /** Creates an IpAddress */ - public IpAddress(IpAddress ipaddr) { - init(ipaddr.address, ipaddr.inet_address); - } - - /** Gets the host address */ - /* - * public String getAddress() { if (address==null) - * address=inet_address.getHostAddress(); return address; } - */ - - /** Makes a copy */ - public Object clone() { - return new IpAddress(this); - } - - /** Wthether it is equal to Object obj */ - public boolean equals(Object obj) { - try { - IpAddress ipaddr = (IpAddress) obj; - if (!toString().equals(ipaddr.toString())) - return false; - return true; - } catch (Exception e) { - return false; - } - } - - /** Gets a String representation of the Object */ - public String toString() { - if (address == null && inet_address != null) - address = inet_address.getHostAddress(); - return address; - } - - // *********************** Static *********************** - - /** Gets the IpAddress for a given fully-qualified host name. */ - public static IpAddress getByName(String host_addr) - throws java.net.UnknownHostException { - InetAddress iaddr = InetAddress.getByName(host_addr); - return new IpAddress(iaddr); - } - - /** Sets the local IP address into the variable localIpAddress */ - public static void setLocalIpAddress() { - localIpAddress = "127.0.0.1"; - - try { - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { - NetworkInterface intf = en.nextElement(); - - for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { - InetAddress inetAddress = enumIpAddr.nextElement(); - - if (!inetAddress.isLoopbackAddress()) { - if (inetAddress.getHostAddress().toString().contains(":")) - continue; - if (!PreferenceManager.getDefaultSharedPreferences(getUIContext()).getBoolean(Settings.PREF_STUN, Settings.DEFAULT_STUN)) { - localIpAddress = inetAddress.getHostAddress().toString(); - } else { - try { - String StunServer = PreferenceManager.getDefaultSharedPreferences(getUIContext()).getString(Settings.PREF_STUN_SERVER, Settings.DEFAULT_STUN_SERVER); - int StunServerPort = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getUIContext()).getString(Settings.PREF_STUN_SERVER_PORT, Settings.DEFAULT_STUN_SERVER_PORT)); - - DiscoveryTest StunDiscover = new DiscoveryTest(inetAddress, StunServer, StunServerPort); - - // call out to stun server - StunDiscover.test(); - //System.out.println("Public ip is:" + StunDiscover.di.getPublicIP().getHostAddress()); - localIpAddress = StunDiscover.di.getPublicIP().getHostAddress(); - } catch (BindException be) { - if (!Sipdroid.release) - System.out.println(inetAddress.toString() + ": " + be.getMessage()); - } catch (Exception e) { - if (!Sipdroid.release) { - System.out.println(e.getMessage()); - e.printStackTrace(); - } - } - } - } - } - } - } catch (Exception ex) { - // do nothing - } - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +import java.net.BindException; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; + +import org.sipdroid.sipua.ui.Receiver; +import org.sipdroid.sipua.ui.Settings; +import org.sipdroid.sipua.ui.Sipdroid; + +import android.preference.PreferenceManager; +import android.content.Context; + +import com.jstun.demo.DiscoveryTest; + +/** + * IpAddress is an IP address. + */ +public class IpAddress { + + /** The host address/name */ + String address; + + /** The InetAddress */ + InetAddress inet_address; + + /** Local IP address */ + public static String localIpAddress = "127.0.0.1"; + + public static Context getUIContext() { + return Receiver.mContext; + } + + // ********************* Protected ********************* + + /** Creates an IpAddress */ + IpAddress(InetAddress iaddress) { + init(null, iaddress); + } + + /** Inits the IpAddress */ + private void init(String address, InetAddress iaddress) { + this.address = address; + this.inet_address = iaddress; + } + + /** Gets the InetAddress */ + InetAddress getInetAddress() { + if (inet_address == null) + try { + inet_address = InetAddress.getByName(address); + } catch (java.net.UnknownHostException e) { + inet_address = null; + } + return inet_address; + } + + // ********************** Public *********************** + + /** Creates an IpAddress */ + public IpAddress(String address) { + init(address, null); + } + + /** Creates an IpAddress */ + public IpAddress(IpAddress ipaddr) { + init(ipaddr.address, ipaddr.inet_address); + } + + /** Gets the host address */ + /* + * public String getAddress() { if (address==null) + * address=inet_address.getHostAddress(); return address; } + */ + + /** Makes a copy */ + public Object clone() { + return new IpAddress(this); + } + + /** Wthether it is equal to Object obj */ + public boolean equals(Object obj) { + try { + IpAddress ipaddr = (IpAddress) obj; + if (!toString().equals(ipaddr.toString())) + return false; + return true; + } catch (Exception e) { + return false; + } + } + + /** Gets a String representation of the Object */ + public String toString() { + if (address == null && inet_address != null) + address = inet_address.getHostAddress(); + return address; + } + + // *********************** Static *********************** + + /** Gets the IpAddress for a given fully-qualified host name. */ + public static IpAddress getByName(String host_addr) + throws java.net.UnknownHostException { + InetAddress iaddr = InetAddress.getByName(host_addr); + return new IpAddress(iaddr); + } + + /** Sets the local IP address into the variable localIpAddress */ + public static void setLocalIpAddress() { + localIpAddress = "127.0.0.1"; + + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { + NetworkInterface intf = en.nextElement(); + + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { + InetAddress inetAddress = enumIpAddr.nextElement(); + + if (!inetAddress.isLoopbackAddress()) { + if (inetAddress.getHostAddress().toString().contains(":")) + continue; + if (!PreferenceManager.getDefaultSharedPreferences(getUIContext()).getBoolean(Settings.PREF_STUN, Settings.DEFAULT_STUN)) { + localIpAddress = inetAddress.getHostAddress().toString(); + } else { + try { + String StunServer = PreferenceManager.getDefaultSharedPreferences(getUIContext()).getString(Settings.PREF_STUN_SERVER, Settings.DEFAULT_STUN_SERVER); + int StunServerPort = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getUIContext()).getString(Settings.PREF_STUN_SERVER_PORT, Settings.DEFAULT_STUN_SERVER_PORT)); + + DiscoveryTest StunDiscover = new DiscoveryTest(inetAddress, StunServer, StunServerPort); + + // call out to stun server + StunDiscover.test(); + //System.out.println("Public ip is:" + StunDiscover.di.getPublicIP().getHostAddress()); + localIpAddress = StunDiscover.di.getPublicIP().getHostAddress(); + } catch (BindException be) { + if (!Sipdroid.release) + System.out.println(inetAddress.toString() + ": " + be.getMessage()); + } catch (Exception e) { + if (!Sipdroid.release) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + } + } + } + } + } catch (Exception ex) { + // do nothing + } + } +} diff --git a/src/org/zoolu/net/SocketAddress.java b/app/src/main/java/org/zoolu/net/SocketAddress.java similarity index 95% rename from src/org/zoolu/net/SocketAddress.java rename to app/src/main/java/org/zoolu/net/SocketAddress.java index e1da310..8e639d1 100644 --- a/src/org/zoolu/net/SocketAddress.java +++ b/app/src/main/java/org/zoolu/net/SocketAddress.java @@ -1,108 +1,108 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -/** - * A SocketAddress is a pair { address, port }. - */ -public class SocketAddress { - /** The InetAddress */ - IpAddress ipaddr; - - /** The port */ - int port; - - /** Creates a SocketAddress. */ - public SocketAddress(IpAddress ipaddr, int port) { - init(ipaddr, port); - } - - /** Creates a SocketAddress. */ - public SocketAddress(String addr, int port) { - init(new IpAddress(addr), port); - } - - /** Creates a SocketAddress. */ - public SocketAddress(String soaddr) { - String addr = null; - int port = -1; - int colon = soaddr.indexOf(':'); - if (colon < 0) - addr = soaddr; - else { - addr = soaddr.substring(0, colon); - try { - port = Integer.parseInt(soaddr.substring(colon + 1)); - } catch (Exception e) { - } - } - init(new IpAddress(addr), port); - } - - /** Creates a SocketAddress. */ - public SocketAddress(SocketAddress soaddr) { - init(soaddr.ipaddr, soaddr.port); - } - - /** Inits the SocketAddress. */ - private void init(IpAddress ipaddr, int port) { - this.ipaddr = ipaddr; - this.port = port; - } - - /** Gets the host address. */ - public IpAddress getAddress() { - return ipaddr; - } - - /** Gets the port. */ - public int getPort() { - return port; - } - - /** Makes a copy. */ - public Object clone() { - return new SocketAddress(this); - } - - /** Wthether it is equal to Object obj. */ - public boolean equals(Object obj) { - try { - SocketAddress saddr = (SocketAddress) obj; - if (port != saddr.port) - return false; - if (!ipaddr.equals(saddr.ipaddr)) - return false; - return true; - } catch (Exception e) { - return false; - } - } - - /** Gets a String representation of the Object. */ - public String toString() { - return (ipaddr.toString() + ":" + port); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +/** + * A SocketAddress is a pair { address, port }. + */ +public class SocketAddress { + /** The InetAddress */ + IpAddress ipaddr; + + /** The port */ + int port; + + /** Creates a SocketAddress. */ + public SocketAddress(IpAddress ipaddr, int port) { + init(ipaddr, port); + } + + /** Creates a SocketAddress. */ + public SocketAddress(String addr, int port) { + init(new IpAddress(addr), port); + } + + /** Creates a SocketAddress. */ + public SocketAddress(String soaddr) { + String addr = null; + int port = -1; + int colon = soaddr.indexOf(':'); + if (colon < 0) + addr = soaddr; + else { + addr = soaddr.substring(0, colon); + try { + port = Integer.parseInt(soaddr.substring(colon + 1)); + } catch (Exception e) { + } + } + init(new IpAddress(addr), port); + } + + /** Creates a SocketAddress. */ + public SocketAddress(SocketAddress soaddr) { + init(soaddr.ipaddr, soaddr.port); + } + + /** Inits the SocketAddress. */ + private void init(IpAddress ipaddr, int port) { + this.ipaddr = ipaddr; + this.port = port; + } + + /** Gets the host address. */ + public IpAddress getAddress() { + return ipaddr; + } + + /** Gets the port. */ + public int getPort() { + return port; + } + + /** Makes a copy. */ + public Object clone() { + return new SocketAddress(this); + } + + /** Wthether it is equal to Object obj. */ + public boolean equals(Object obj) { + try { + SocketAddress saddr = (SocketAddress) obj; + if (port != saddr.port) + return false; + if (!ipaddr.equals(saddr.ipaddr)) + return false; + return true; + } catch (Exception e) { + return false; + } + } + + /** Gets a String representation of the Object. */ + public String toString() { + return (ipaddr.toString() + ":" + port); + } + +} diff --git a/src/org/zoolu/net/TcpConnection.java b/app/src/main/java/org/zoolu/net/TcpConnection.java similarity index 96% rename from src/org/zoolu/net/TcpConnection.java rename to app/src/main/java/org/zoolu/net/TcpConnection.java index 56b57d8..59f54b9 100644 --- a/src/org/zoolu/net/TcpConnection.java +++ b/app/src/main/java/org/zoolu/net/TcpConnection.java @@ -1,207 +1,207 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -// import java.net.InetAddress; -import java.io.*; - -/** - * TcpConnection provides a TCP connection oriented transport service. - */ -public class TcpConnection extends Thread { - /** The reading buffer size */ - static final int BUFFER_SIZE = 65535; - - /** - * Default value for the maximum time that the tcp connection can remain - * active after been halted (in milliseconds) - */ - public static final int DEFAULT_SOCKET_TIMEOUT = 2000; // 2sec - - /** The TCP socket */ - TcpSocket socket; - - /** - * Maximum time that the connection can remain active after been halted (in - * milliseconds) - */ - int socket_timeout; - - /** - * Maximum time that the connection remains active without receiving data - * (in milliseconds) - */ - long alive_time; - - /** The InputStream */ - InputStream istream; - - /** The OutputStream */ - OutputStream ostream; - - /** InputStream/OutputStream error */ - Exception error; - - /** Whether it has been halted */ - boolean stop; - - /** Whether it is running */ - boolean is_running; - - /** TcpConnection listener */ - TcpConnectionListener listener; - - /** Costructs a new TcpConnection */ - public TcpConnection(TcpSocket socket, TcpConnectionListener listener) { - init(socket, 0, listener); - start(); - } - - /** Costructs a new TcpConnection */ - public TcpConnection(TcpSocket socket, long alive_time, - TcpConnectionListener listener) { - init(socket, alive_time, listener); - start(); - } - - /** Inits the TcpConnection */ - private void init(TcpSocket socket, long alive_time, - TcpConnectionListener listener) { - this.listener = listener; - this.socket = socket; - this.socket_timeout = DEFAULT_SOCKET_TIMEOUT; - this.alive_time = alive_time; - this.stop = false; - this.is_running = true; - - this.istream = null; - this.ostream = null; - this.error = null; - try { - istream = new BufferedInputStream(socket.getInputStream()); - ostream = new BufferedOutputStream(socket.getOutputStream()); - } catch (Exception e) { - error = e; - } - } - - /** Whether the service is running */ - public boolean isRunning() { - return is_running; - } - - /** Gets the TcpSocket */ - public TcpSocket getSocket() { - return socket; - } - - /** Gets the remote IP address */ - public IpAddress getRemoteAddress() { - return socket.getAddress(); - } - - /** Gets the remote port */ - public int getRemotePort() { - return socket.getPort(); - } - - /** Stops running */ - public void halt() { - stop = true; - } - - /** Sends data */ - public void send(byte[] buff, int offset, int len) throws IOException { - if (!stop && ostream != null) { - ostream.write(buff, offset, len); - ostream.flush(); - } - } - - /** Sends data */ - public void send(byte[] buff) throws IOException { - send(buff, 0, buff.length); - } - - /** Runs the tcp receiver */ - public void run() { - byte[] buff = new byte[BUFFER_SIZE]; - long expire = 0; - if (alive_time > 0) - expire = System.currentTimeMillis() + alive_time; - try { - if (error != null) - throw error; -// socket.setSoTimeout(socket_timeout); modified - // loop - while (!stop) { - int len = 0; - if (istream != null) { - try { - len = istream.read(buff); - } catch (InterruptedIOException ie) { - if (alive_time > 0 - && System.currentTimeMillis() > expire) - halt(); - continue; - } - } - if (len < 0) { // error=new Exception("TCP connection closed"); - stop = true; - } else if (len > 0) { - if (listener != null) - listener.onReceivedData(this, buff, len); - if (alive_time > 0) - expire = System.currentTimeMillis() + alive_time; - } - } - } catch (Exception e) { - error = e; - stop = true; - } - is_running = false; - if (istream != null) - try { - istream.close(); - } catch (Exception e) { - } - if (ostream != null) - try { - ostream.close(); - } catch (Exception e) { - } - if (listener != null) - listener.onConnectionTerminated(this, error); - listener = null; - } - - /** Gets a String representation of the Object */ - public String toString() { - return "tcp:" // modified + socket.getLocalAddress() + ":" + socket.getLocalPort() -// + "<->" + socket.getAddress() + ":" + socket.getPort(); - ; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +// import java.net.InetAddress; +import java.io.*; + +/** + * TcpConnection provides a TCP connection oriented transport service. + */ +public class TcpConnection extends Thread { + /** The reading buffer size */ + static final int BUFFER_SIZE = 65535; + + /** + * Default value for the maximum time that the tcp connection can remain + * active after been halted (in milliseconds) + */ + public static final int DEFAULT_SOCKET_TIMEOUT = 2000; // 2sec + + /** The TCP socket */ + TcpSocket socket; + + /** + * Maximum time that the connection can remain active after been halted (in + * milliseconds) + */ + int socket_timeout; + + /** + * Maximum time that the connection remains active without receiving data + * (in milliseconds) + */ + long alive_time; + + /** The InputStream */ + InputStream istream; + + /** The OutputStream */ + OutputStream ostream; + + /** InputStream/OutputStream error */ + Exception error; + + /** Whether it has been halted */ + boolean stop; + + /** Whether it is running */ + boolean is_running; + + /** TcpConnection listener */ + TcpConnectionListener listener; + + /** Costructs a new TcpConnection */ + public TcpConnection(TcpSocket socket, TcpConnectionListener listener) { + init(socket, 0, listener); + start(); + } + + /** Costructs a new TcpConnection */ + public TcpConnection(TcpSocket socket, long alive_time, + TcpConnectionListener listener) { + init(socket, alive_time, listener); + start(); + } + + /** Inits the TcpConnection */ + private void init(TcpSocket socket, long alive_time, + TcpConnectionListener listener) { + this.listener = listener; + this.socket = socket; + this.socket_timeout = DEFAULT_SOCKET_TIMEOUT; + this.alive_time = alive_time; + this.stop = false; + this.is_running = true; + + this.istream = null; + this.ostream = null; + this.error = null; + try { + istream = new BufferedInputStream(socket.getInputStream()); + ostream = new BufferedOutputStream(socket.getOutputStream()); + } catch (Exception e) { + error = e; + } + } + + /** Whether the service is running */ + public boolean isRunning() { + return is_running; + } + + /** Gets the TcpSocket */ + public TcpSocket getSocket() { + return socket; + } + + /** Gets the remote IP address */ + public IpAddress getRemoteAddress() { + return socket.getAddress(); + } + + /** Gets the remote port */ + public int getRemotePort() { + return socket.getPort(); + } + + /** Stops running */ + public void halt() { + stop = true; + } + + /** Sends data */ + public void send(byte[] buff, int offset, int len) throws IOException { + if (!stop && ostream != null) { + ostream.write(buff, offset, len); + ostream.flush(); + } + } + + /** Sends data */ + public void send(byte[] buff) throws IOException { + send(buff, 0, buff.length); + } + + /** Runs the tcp receiver */ + public void run() { + byte[] buff = new byte[BUFFER_SIZE]; + long expire = 0; + if (alive_time > 0) + expire = System.currentTimeMillis() + alive_time; + try { + if (error != null) + throw error; +// socket.setSoTimeout(socket_timeout); modified + // loop + while (!stop) { + int len = 0; + if (istream != null) { + try { + len = istream.read(buff); + } catch (InterruptedIOException ie) { + if (alive_time > 0 + && System.currentTimeMillis() > expire) + halt(); + continue; + } + } + if (len < 0) { // error=new Exception("TCP connection closed"); + stop = true; + } else if (len > 0) { + if (listener != null) + listener.onReceivedData(this, buff, len); + if (alive_time > 0) + expire = System.currentTimeMillis() + alive_time; + } + } + } catch (Exception e) { + error = e; + stop = true; + } + is_running = false; + if (istream != null) + try { + istream.close(); + } catch (Exception e) { + } + if (ostream != null) + try { + ostream.close(); + } catch (Exception e) { + } + if (listener != null) + listener.onConnectionTerminated(this, error); + listener = null; + } + + /** Gets a String representation of the Object */ + public String toString() { + return "tcp:" // modified + socket.getLocalAddress() + ":" + socket.getLocalPort() +// + "<->" + socket.getAddress() + ":" + socket.getPort(); + ; + } + +} diff --git a/src/org/zoolu/net/TcpConnectionListener.java b/app/src/main/java/org/zoolu/net/TcpConnectionListener.java similarity index 97% rename from src/org/zoolu/net/TcpConnectionListener.java rename to app/src/main/java/org/zoolu/net/TcpConnectionListener.java index 6a75a68..d4c08bb 100644 --- a/src/org/zoolu/net/TcpConnectionListener.java +++ b/app/src/main/java/org/zoolu/net/TcpConnectionListener.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -/** - * Listener for TcpConnection events. - */ -public interface TcpConnectionListener { - /** When new data is received through the TcpConnection. */ - public void onReceivedData(TcpConnection tcp_conn, byte[] data, int len); - - /** When TcpConnection terminates. */ - public void onConnectionTerminated(TcpConnection tcp_conn, Exception error); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +/** + * Listener for TcpConnection events. + */ +public interface TcpConnectionListener { + /** When new data is received through the TcpConnection. */ + public void onReceivedData(TcpConnection tcp_conn, byte[] data, int len); + + /** When TcpConnection terminates. */ + public void onConnectionTerminated(TcpConnection tcp_conn, Exception error); +} diff --git a/src/org/zoolu/net/TcpServer.java b/app/src/main/java/org/zoolu/net/TcpServer.java similarity index 96% rename from src/org/zoolu/net/TcpServer.java rename to app/src/main/java/org/zoolu/net/TcpServer.java index 56033ee..da6fb29 100644 --- a/src/org/zoolu/net/TcpServer.java +++ b/app/src/main/java/org/zoolu/net/TcpServer.java @@ -1,171 +1,171 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -import java.net.ServerSocket; -import java.io.IOException; -import java.io.InterruptedIOException; - -/** - * TcpServer implements a TCP server wainting for incoming connection. - */ -public class TcpServer extends Thread { - /** - * Default value for the maximum time that the tcp server can remain active - * after been halted (in milliseconds) - */ - public static final int DEFAULT_SOCKET_TIMEOUT = 5000; // 5sec - - /** Default ServerSocket backlog value */ - static int socket_backlog = 50; - - /** The protocol type */ - // protected static final String PROTO="tcp"; - /** The TCP server socket */ - ServerSocket server_socket; - - /** - * Maximum time that the connection can remain active after been halted (in - * milliseconds) - */ - int socket_timeout; - - /** - * Maximum time that the server remains active without incoming connections - * (in milliseconds) - */ - long alive_time; - - /** Whether it has been halted */ - boolean stop; - - /** Whether it is running */ - boolean is_running; - - /** TcpServer listener */ - TcpServerListener listener; - - /** Costructs a new TcpServer */ - public TcpServer(int port, TcpServerListener listener) - throws java.io.IOException { - init(port, null, 0, listener); - start(); - } - - /** Costructs a new TcpServer */ - public TcpServer(int port, IpAddress bind_ipaddr, TcpServerListener listener) - throws java.io.IOException { - init(port, bind_ipaddr, 0, listener); - start(); - } - - /** Costructs a new TcpServer */ - public TcpServer(int port, IpAddress bind_ipaddr, long alive_time, - TcpServerListener listener) throws java.io.IOException { - init(port, bind_ipaddr, alive_time, listener); - start(); - } - - /** Inits the TcpServer */ - private void init(int port, IpAddress bind_ipaddr, long alive_time, - TcpServerListener listener) throws java.io.IOException { - this.listener = listener; - if (bind_ipaddr == null) - server_socket = new ServerSocket(port); - else - server_socket = new ServerSocket(port, socket_backlog, bind_ipaddr - .getInetAddress()); - this.socket_timeout = DEFAULT_SOCKET_TIMEOUT; - this.alive_time = alive_time; - this.stop = false; - this.is_running = true; - } - - public int getPort() { - return server_socket.getLocalPort(); - } - - /** Whether the service is running */ - public boolean isRunning() { - return is_running; - } - - /** Stops running */ - public void halt() { - stop = true; - try { - server_socket.close(); // modified - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** Runs the server */ - public void run() { - Exception error = null; - try { -// server_socket.setSoTimeout(socket_timeout); modified - long expire = 0; - if (alive_time > 0) - expire = System.currentTimeMillis() + alive_time; - // loop - while (!stop) { - TcpSocket socket = null; - try { - socket = new TcpSocket(server_socket.accept()); - } catch (InterruptedIOException ie) { - if (alive_time > 0 && System.currentTimeMillis() > expire) - halt(); - continue; - } - if (listener != null) - listener.onIncomingConnection(this, socket); - if (alive_time > 0) - expire = System.currentTimeMillis() + alive_time; - } - } catch (Exception e) { - error = e; - stop = true; - } - is_running = false; - try { - server_socket.close(); - } catch (java.io.IOException e) { - } - server_socket = null; - - if (listener != null) - listener.onServerTerminated(this, error); - listener = null; - } - - /** Gets a String representation of the Object */ - public String toString() { - return "tcp:" // modified + server_socket.getInetAddress() + ":" - //+ server_socket.getLocalPort(); - ; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +import java.net.ServerSocket; +import java.io.IOException; +import java.io.InterruptedIOException; + +/** + * TcpServer implements a TCP server wainting for incoming connection. + */ +public class TcpServer extends Thread { + /** + * Default value for the maximum time that the tcp server can remain active + * after been halted (in milliseconds) + */ + public static final int DEFAULT_SOCKET_TIMEOUT = 5000; // 5sec + + /** Default ServerSocket backlog value */ + static int socket_backlog = 50; + + /** The protocol type */ + // protected static final String PROTO="tcp"; + /** The TCP server socket */ + ServerSocket server_socket; + + /** + * Maximum time that the connection can remain active after been halted (in + * milliseconds) + */ + int socket_timeout; + + /** + * Maximum time that the server remains active without incoming connections + * (in milliseconds) + */ + long alive_time; + + /** Whether it has been halted */ + boolean stop; + + /** Whether it is running */ + boolean is_running; + + /** TcpServer listener */ + TcpServerListener listener; + + /** Costructs a new TcpServer */ + public TcpServer(int port, TcpServerListener listener) + throws java.io.IOException { + init(port, null, 0, listener); + start(); + } + + /** Costructs a new TcpServer */ + public TcpServer(int port, IpAddress bind_ipaddr, TcpServerListener listener) + throws java.io.IOException { + init(port, bind_ipaddr, 0, listener); + start(); + } + + /** Costructs a new TcpServer */ + public TcpServer(int port, IpAddress bind_ipaddr, long alive_time, + TcpServerListener listener) throws java.io.IOException { + init(port, bind_ipaddr, alive_time, listener); + start(); + } + + /** Inits the TcpServer */ + private void init(int port, IpAddress bind_ipaddr, long alive_time, + TcpServerListener listener) throws java.io.IOException { + this.listener = listener; + if (bind_ipaddr == null) + server_socket = new ServerSocket(port); + else + server_socket = new ServerSocket(port, socket_backlog, bind_ipaddr + .getInetAddress()); + this.socket_timeout = DEFAULT_SOCKET_TIMEOUT; + this.alive_time = alive_time; + this.stop = false; + this.is_running = true; + } + + public int getPort() { + return server_socket.getLocalPort(); + } + + /** Whether the service is running */ + public boolean isRunning() { + return is_running; + } + + /** Stops running */ + public void halt() { + stop = true; + try { + server_socket.close(); // modified + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** Runs the server */ + public void run() { + Exception error = null; + try { +// server_socket.setSoTimeout(socket_timeout); modified + long expire = 0; + if (alive_time > 0) + expire = System.currentTimeMillis() + alive_time; + // loop + while (!stop) { + TcpSocket socket = null; + try { + socket = new TcpSocket(server_socket.accept()); + } catch (InterruptedIOException ie) { + if (alive_time > 0 && System.currentTimeMillis() > expire) + halt(); + continue; + } + if (listener != null) + listener.onIncomingConnection(this, socket); + if (alive_time > 0) + expire = System.currentTimeMillis() + alive_time; + } + } catch (Exception e) { + error = e; + stop = true; + } + is_running = false; + try { + server_socket.close(); + } catch (java.io.IOException e) { + } + server_socket = null; + + if (listener != null) + listener.onServerTerminated(this, error); + listener = null; + } + + /** Gets a String representation of the Object */ + public String toString() { + return "tcp:" // modified + server_socket.getInetAddress() + ":" + //+ server_socket.getLocalPort(); + ; + } + +} diff --git a/src/org/zoolu/net/TcpServerListener.java b/app/src/main/java/org/zoolu/net/TcpServerListener.java similarity index 97% rename from src/org/zoolu/net/TcpServerListener.java rename to app/src/main/java/org/zoolu/net/TcpServerListener.java index ba7f536..7652e07 100644 --- a/src/org/zoolu/net/TcpServerListener.java +++ b/app/src/main/java/org/zoolu/net/TcpServerListener.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -/** - * Listener for TcpServer events. - */ -public interface TcpServerListener { - /** When a new incoming connection is established */ - public void onIncomingConnection(TcpServer tcp_server, TcpSocket socket); - - /** When ConnectionServer terminates. */ - public void onServerTerminated(TcpServer tcp_server, Exception error); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +/** + * Listener for TcpServer events. + */ +public interface TcpServerListener { + /** When a new incoming connection is established */ + public void onIncomingConnection(TcpServer tcp_server, TcpSocket socket); + + /** When ConnectionServer terminates. */ + public void onServerTerminated(TcpServer tcp_server, Exception error); +} diff --git a/src/org/zoolu/net/TcpSocket.java b/app/src/main/java/org/zoolu/net/TcpSocket.java similarity index 96% rename from src/org/zoolu/net/TcpSocket.java rename to app/src/main/java/org/zoolu/net/TcpSocket.java index 4055b6f..038ecce 100644 --- a/src/org/zoolu/net/TcpSocket.java +++ b/app/src/main/java/org/zoolu/net/TcpSocket.java @@ -1,138 +1,138 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -import java.net.InetSocketAddress; -import java.net.Socket; // import java.net.InetAddress; -import java.io.InputStream; -import java.io.OutputStream; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSocket; - -import org.apache.http.conn.ssl.SSLSocketFactory; - -/** - * TcpSocket provides a uniform interface to TCP transport protocol, regardless - * J2SE or J2ME is used. - */ -public class TcpSocket { - /** Socket */ - Socket socket; - - /** Creates a new TcpSocket */ - TcpSocket() { - socket = null; - } - - /** Creates a new TcpSocket */ - TcpSocket(Socket sock) { - socket = sock; - } - - static boolean lock; - - /** Creates a new UdpSocket */ - public TcpSocket(IpAddress ipaddr, int port, String host) throws java.io.IOException { -// socket = new Socket(ipaddr.getInetAddress(), port); modified - SSLSocketFactory f = - (SSLSocketFactory) SSLSocketFactory.getSocketFactory(); - if (host == null) - socket = new Socket(); - else - socket = f.createSocket(); - if (lock) throw new java.io.IOException(); - lock = true; - try { - socket.connect(new InetSocketAddress(ipaddr.toString(), port), - Thread.currentThread().getName().equals("main")?1000:10000); - } catch (java.io.IOException e) { - lock = false; - throw e; - } - if (host != null && !host.equals(ipaddr.toString())) { - HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); - SSLSession s = ((SSLSocket)socket).getSession(); - if (!hv.verify(host, s)) { - lock = false; - throw new java.io.IOException(); - } - } - lock = false; - } - - /** Closes this socket. */ - public void close() throws java.io.IOException { - socket.close(); - } - - /** Gets the address to which the socket is connected. */ - public IpAddress getAddress() { - return new IpAddress(socket.getInetAddress()); - } - - /** Gets an input stream for this socket. */ - public InputStream getInputStream() throws java.io.IOException { - return socket.getInputStream(); - } - - /** Gets the local address to which the socket is bound. */ - public IpAddress getLocalAddress() { - return new IpAddress(socket.getLocalAddress()); - } - - /** Gets the local port to which this socket is bound. */ - public int getLocalPort() { - return socket.getLocalPort(); - } - - /** Gets an output stream for this socket. */ - public OutputStream getOutputStream() throws java.io.IOException { - return socket.getOutputStream(); - } - - /** Gets the remote port to which this socket is connected. */ - public int getPort() { - return socket.getPort(); - } - - /** Gets the socket timeout. */ - public int getSoTimeout() throws java.net.SocketException { - return socket.getSoTimeout(); - } - - /** Enables/disables the socket timeou, in milliseconds. */ - public void setSoTimeout(int timeout) throws java.net.SocketException { - socket.setSoTimeout(timeout); - } - - /** Converts this object to a String. */ - public String toString() { - return socket.toString(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +import java.net.InetSocketAddress; +import java.net.Socket; // import java.net.InetAddress; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; + +import org.apache.http.conn.ssl.SSLSocketFactory; + +/** + * TcpSocket provides a uniform interface to TCP transport protocol, regardless + * J2SE or J2ME is used. + */ +public class TcpSocket { + /** Socket */ + Socket socket; + + /** Creates a new TcpSocket */ + TcpSocket() { + socket = null; + } + + /** Creates a new TcpSocket */ + TcpSocket(Socket sock) { + socket = sock; + } + + static boolean lock; + + /** Creates a new UdpSocket */ + public TcpSocket(IpAddress ipaddr, int port, String host) throws java.io.IOException { +// socket = new Socket(ipaddr.getInetAddress(), port); modified + SSLSocketFactory f = + (SSLSocketFactory) SSLSocketFactory.getSocketFactory(); + if (host == null) + socket = new Socket(); + else + socket = f.createSocket(); + if (lock) throw new java.io.IOException(); + lock = true; + try { + socket.connect(new InetSocketAddress(ipaddr.toString(), port), + Thread.currentThread().getName().equals("main")?1000:10000); + } catch (java.io.IOException e) { + lock = false; + throw e; + } + if (host != null && !host.equals(ipaddr.toString())) { + HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); + SSLSession s = ((SSLSocket)socket).getSession(); + if (!hv.verify(host, s)) { + lock = false; + throw new java.io.IOException(); + } + } + lock = false; + } + + /** Closes this socket. */ + public void close() throws java.io.IOException { + socket.close(); + } + + /** Gets the address to which the socket is connected. */ + public IpAddress getAddress() { + return new IpAddress(socket.getInetAddress()); + } + + /** Gets an input stream for this socket. */ + public InputStream getInputStream() throws java.io.IOException { + return socket.getInputStream(); + } + + /** Gets the local address to which the socket is bound. */ + public IpAddress getLocalAddress() { + return new IpAddress(socket.getLocalAddress()); + } + + /** Gets the local port to which this socket is bound. */ + public int getLocalPort() { + return socket.getLocalPort(); + } + + /** Gets an output stream for this socket. */ + public OutputStream getOutputStream() throws java.io.IOException { + return socket.getOutputStream(); + } + + /** Gets the remote port to which this socket is connected. */ + public int getPort() { + return socket.getPort(); + } + + /** Gets the socket timeout. */ + public int getSoTimeout() throws java.net.SocketException { + return socket.getSoTimeout(); + } + + /** Enables/disables the socket timeou, in milliseconds. */ + public void setSoTimeout(int timeout) throws java.net.SocketException { + socket.setSoTimeout(timeout); + } + + /** Converts this object to a String. */ + public String toString() { + return socket.toString(); + } + +} diff --git a/src/org/zoolu/net/UdpPacket.java b/app/src/main/java/org/zoolu/net/UdpPacket.java similarity index 96% rename from src/org/zoolu/net/UdpPacket.java rename to app/src/main/java/org/zoolu/net/UdpPacket.java index 96f7828..9e14eab 100644 --- a/src/org/zoolu/net/UdpPacket.java +++ b/app/src/main/java/org/zoolu/net/UdpPacket.java @@ -1,140 +1,140 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -import java.net.DatagramPacket; - -// import java.net.InetAddress; - -/** - * UdpPacket provides a uniform interface to UDP packets, regardless J2SE or - * J2ME is used. - */ -public class UdpPacket { - /** The DatagramPacket */ - DatagramPacket packet; - - /** Creates a new UdpPacket */ - UdpPacket(DatagramPacket packet) { - this.packet = packet; - } - - /** Gets the DatagramPacket */ - DatagramPacket getDatagramPacket() { - return packet; - } - - /** Sets the DatagramPacket */ - void setDatagramPacket(DatagramPacket packet) { - this.packet = packet; - } - - /** Creates a new UdpPacket */ - public UdpPacket(byte[] buf, int length) { - packet = new DatagramPacket(buf, length); - } - - /** Creates a new UdpPacket */ - public UdpPacket(byte[] buf, int length, IpAddress ipaddr, int port) { - packet = new DatagramPacket(buf, length, ipaddr.getInetAddress(), port); - } - - /** Creates a new UdpPacket */ - public UdpPacket(byte[] buf, int offset, int length) { - packet = new DatagramPacket(buf, offset, length); - } - - /** Creates a new UdpPacket */ - public UdpPacket(byte[] buf, int offset, int length, IpAddress ipaddr, - int port) { - packet = new DatagramPacket(buf, offset, length, ipaddr - .getInetAddress(), port); - } - - /** - * Gets the IP address of the machine to which this datagram is being sent - * or from which the datagram was received. - */ - public IpAddress getIpAddress() { - return new IpAddress(packet.getAddress()); - } - - /** Gets the data received or the data to be sent. */ - public byte[] getData() { - return packet.getData(); - } - - /** - * Gets the length of the data to be sent or the length of the data - * received. - */ - public int getLength() { - return packet.getLength(); - } - - /** - * Gets the offset of the data to be sent or the offset of the data - * received. - */ - public int getOffset() { - return packet.getOffset(); - } - - /** - * Gets the port number on the remote host to which this datagram is being - * sent or from which the datagram was received. - */ - public int getPort() { - return packet.getPort(); - } - - /** Sets the IP address of the machine to which this datagram is being sent. */ - public void setIpAddress(IpAddress ipaddr) { - packet.setAddress(ipaddr.getInetAddress()); - } - - /** Sets the data buffer for this packet. */ - public void setData(byte[] buf) { - packet.setData(buf); - } - - /** Sets the data buffer for this packet. */ - public void setData(byte[] buf, int offset, int length) { - packet.setData(buf, offset, length); - } - - /** Sets the length for this packet. */ - public void setLength(int length) { - packet.setLength(length); - } - - /** - * Sets the port number on the remote host to which this datagram is being - * sent. - */ - public void setPort(int iport) { - packet.setPort(iport); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +import java.net.DatagramPacket; + +// import java.net.InetAddress; + +/** + * UdpPacket provides a uniform interface to UDP packets, regardless J2SE or + * J2ME is used. + */ +public class UdpPacket { + /** The DatagramPacket */ + DatagramPacket packet; + + /** Creates a new UdpPacket */ + UdpPacket(DatagramPacket packet) { + this.packet = packet; + } + + /** Gets the DatagramPacket */ + DatagramPacket getDatagramPacket() { + return packet; + } + + /** Sets the DatagramPacket */ + void setDatagramPacket(DatagramPacket packet) { + this.packet = packet; + } + + /** Creates a new UdpPacket */ + public UdpPacket(byte[] buf, int length) { + packet = new DatagramPacket(buf, length); + } + + /** Creates a new UdpPacket */ + public UdpPacket(byte[] buf, int length, IpAddress ipaddr, int port) { + packet = new DatagramPacket(buf, length, ipaddr.getInetAddress(), port); + } + + /** Creates a new UdpPacket */ + public UdpPacket(byte[] buf, int offset, int length) { + packet = new DatagramPacket(buf, offset, length); + } + + /** Creates a new UdpPacket */ + public UdpPacket(byte[] buf, int offset, int length, IpAddress ipaddr, + int port) { + packet = new DatagramPacket(buf, offset, length, ipaddr + .getInetAddress(), port); + } + + /** + * Gets the IP address of the machine to which this datagram is being sent + * or from which the datagram was received. + */ + public IpAddress getIpAddress() { + return new IpAddress(packet.getAddress()); + } + + /** Gets the data received or the data to be sent. */ + public byte[] getData() { + return packet.getData(); + } + + /** + * Gets the length of the data to be sent or the length of the data + * received. + */ + public int getLength() { + return packet.getLength(); + } + + /** + * Gets the offset of the data to be sent or the offset of the data + * received. + */ + public int getOffset() { + return packet.getOffset(); + } + + /** + * Gets the port number on the remote host to which this datagram is being + * sent or from which the datagram was received. + */ + public int getPort() { + return packet.getPort(); + } + + /** Sets the IP address of the machine to which this datagram is being sent. */ + public void setIpAddress(IpAddress ipaddr) { + packet.setAddress(ipaddr.getInetAddress()); + } + + /** Sets the data buffer for this packet. */ + public void setData(byte[] buf) { + packet.setData(buf); + } + + /** Sets the data buffer for this packet. */ + public void setData(byte[] buf, int offset, int length) { + packet.setData(buf, offset, length); + } + + /** Sets the length for this packet. */ + public void setLength(int length) { + packet.setLength(length); + } + + /** + * Sets the port number on the remote host to which this datagram is being + * sent. + */ + public void setPort(int iport) { + packet.setPort(iport); + } + +} diff --git a/src/org/zoolu/net/UdpProvider.java b/app/src/main/java/org/zoolu/net/UdpProvider.java similarity index 96% rename from src/org/zoolu/net/UdpProvider.java rename to app/src/main/java/org/zoolu/net/UdpProvider.java index ca8de0d..f227d7d 100644 --- a/src/org/zoolu/net/UdpProvider.java +++ b/app/src/main/java/org/zoolu/net/UdpProvider.java @@ -1,210 +1,210 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -import java.io.IOException; -import java.io.InterruptedIOException; - -/** - * UdpProvider provides an UDP send/receive service. On the receiver side it - * waits for UDP datagrams and passes them to the UdpProviderListener. - *

- * If the attribute alive_time has a non-zero value, the UdpProvider - * stops after alive_time milliseconds of inactivity. - *

- * When a new packet is received, the - * onReceivedPacket(UdpProvider,DatagramPacket) method is fired. - *

- * Method onServiceTerminated(UdpProvider) is fired when the the UdpProvider - * stops receiving packets. - */ -public class UdpProvider extends Thread { - /** The reading buffer size */ - public static final int BUFFER_SIZE = 65535; - - /** - * Default value for the maximum time that the UDP receiver can remain - * active after been halted (in milliseconds) - */ - public static final int DEFAULT_SOCKET_TIMEOUT = 2000; // 2sec - - /** UDP socket */ - UdpSocket socket; - - /** - * Maximum time that the UDP receiver can remain active after been halted - * (in milliseconds) - */ - int socket_timeout; - - /** - * Maximum time that the UDP receiver remains active without receiving UDP - * datagrams (in milliseconds) - */ - long alive_time; - - /** - * Minimum size for received packets. Shorter packets are silently - * discarded. - */ - int minimum_length; - - /** Whether it has been halted */ - boolean stop; - - /** Whether it is running */ - boolean is_running; - - /** UdpProvider listener */ - UdpProviderListener listener; - - /** Creates a new UdpProvider */ - public UdpProvider(UdpSocket socket, UdpProviderListener listener) { - init(socket, 0, listener); - start(); - } - - /** Creates a new UdpProvider */ - public UdpProvider(UdpSocket socket, long alive_time, - UdpProviderListener listener) { - init(socket, alive_time, listener); - start(); - } - - /** Inits the UdpProvider */ - private void init(UdpSocket socket, long alive_time, - UdpProviderListener listener) { - this.listener = listener; - this.socket = socket; - this.socket_timeout = DEFAULT_SOCKET_TIMEOUT; - this.alive_time = alive_time; - this.minimum_length = 0; - this.stop = false; - this.is_running = true; - } - - /** Gets the UdpSocket */ - public UdpSocket getUdpSocket() { - return socket; - } - - /** Sets a new UdpSocket */ - /* - * public void setUdpSocket(UdpSocket socket) { this.socket=socket; } - */ - - /** Whether the service is running */ - public boolean isRunning() { - return is_running; - } - - /** - * Sets the maximum time that the UDP service can remain active after been - * halted - */ - public void setSoTimeout(int timeout) { - socket_timeout = timeout; - } - - /** - * Gets the maximum time that the UDP service can remain active after been - * halted - */ - public int getSoTimeout() { - return socket_timeout; - } - - /** - * Sets the minimum size for received packets. Packets shorter than that are - * silently discarded. - */ - public void setMinimumReceivedDataLength(int len) { - minimum_length = len; - } - - /** - * Gets the minimum size for received packets. Packets shorter than that are - * silently discarded. - */ - public int getMinimumReceivedDataLength() { - return minimum_length; - } - - /** Sends a UdpPacket */ - public void send(UdpPacket packet) throws IOException { - if (!stop) - socket.send(packet); - } - - /** Stops running */ - public void halt() { - stop = true; - socket.close(); // modified - } - - /** The main thread */ - public void run() { - byte[] buf = new byte[BUFFER_SIZE]; - UdpPacket packet = new UdpPacket(buf, buf.length); - - Exception error = null; - long expire = 0; - if (alive_time > 0) - expire = System.currentTimeMillis() + alive_time; - try { -// socket.setSoTimeout(socket_timeout); modified - // loop - while (!stop) { - try { - socket.receive(packet); - } catch (InterruptedIOException ie) { - if (alive_time > 0 && System.currentTimeMillis() > expire) - halt(); - continue; - } - if (packet.getLength() >= minimum_length) { - if (listener != null) - listener.onReceivedPacket(this, packet); - if (alive_time > 0) - expire = System.currentTimeMillis() + alive_time; - } - packet = new UdpPacket(buf, buf.length); - } - } catch (Exception e) { - error = e; - stop = true; - } - is_running = false; - if (listener != null) - listener.onServiceTerminated(this, error); - listener = null; - } - - /** Gets a String representation of the Object */ - public String toString() { - return "udp:" + socket.getLocalAddress() + ":" + socket.getLocalPort(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +import java.io.IOException; +import java.io.InterruptedIOException; + +/** + * UdpProvider provides an UDP send/receive service. On the receiver side it + * waits for UDP datagrams and passes them to the UdpProviderListener. + *

+ * If the attribute alive_time has a non-zero value, the UdpProvider + * stops after alive_time milliseconds of inactivity. + *

+ * When a new packet is received, the + * onReceivedPacket(UdpProvider,DatagramPacket) method is fired. + *

+ * Method onServiceTerminated(UdpProvider) is fired when the the UdpProvider + * stops receiving packets. + */ +public class UdpProvider extends Thread { + /** The reading buffer size */ + public static final int BUFFER_SIZE = 65535; + + /** + * Default value for the maximum time that the UDP receiver can remain + * active after been halted (in milliseconds) + */ + public static final int DEFAULT_SOCKET_TIMEOUT = 2000; // 2sec + + /** UDP socket */ + UdpSocket socket; + + /** + * Maximum time that the UDP receiver can remain active after been halted + * (in milliseconds) + */ + int socket_timeout; + + /** + * Maximum time that the UDP receiver remains active without receiving UDP + * datagrams (in milliseconds) + */ + long alive_time; + + /** + * Minimum size for received packets. Shorter packets are silently + * discarded. + */ + int minimum_length; + + /** Whether it has been halted */ + boolean stop; + + /** Whether it is running */ + boolean is_running; + + /** UdpProvider listener */ + UdpProviderListener listener; + + /** Creates a new UdpProvider */ + public UdpProvider(UdpSocket socket, UdpProviderListener listener) { + init(socket, 0, listener); + start(); + } + + /** Creates a new UdpProvider */ + public UdpProvider(UdpSocket socket, long alive_time, + UdpProviderListener listener) { + init(socket, alive_time, listener); + start(); + } + + /** Inits the UdpProvider */ + private void init(UdpSocket socket, long alive_time, + UdpProviderListener listener) { + this.listener = listener; + this.socket = socket; + this.socket_timeout = DEFAULT_SOCKET_TIMEOUT; + this.alive_time = alive_time; + this.minimum_length = 0; + this.stop = false; + this.is_running = true; + } + + /** Gets the UdpSocket */ + public UdpSocket getUdpSocket() { + return socket; + } + + /** Sets a new UdpSocket */ + /* + * public void setUdpSocket(UdpSocket socket) { this.socket=socket; } + */ + + /** Whether the service is running */ + public boolean isRunning() { + return is_running; + } + + /** + * Sets the maximum time that the UDP service can remain active after been + * halted + */ + public void setSoTimeout(int timeout) { + socket_timeout = timeout; + } + + /** + * Gets the maximum time that the UDP service can remain active after been + * halted + */ + public int getSoTimeout() { + return socket_timeout; + } + + /** + * Sets the minimum size for received packets. Packets shorter than that are + * silently discarded. + */ + public void setMinimumReceivedDataLength(int len) { + minimum_length = len; + } + + /** + * Gets the minimum size for received packets. Packets shorter than that are + * silently discarded. + */ + public int getMinimumReceivedDataLength() { + return minimum_length; + } + + /** Sends a UdpPacket */ + public void send(UdpPacket packet) throws IOException { + if (!stop) + socket.send(packet); + } + + /** Stops running */ + public void halt() { + stop = true; + socket.close(); // modified + } + + /** The main thread */ + public void run() { + byte[] buf = new byte[BUFFER_SIZE]; + UdpPacket packet = new UdpPacket(buf, buf.length); + + Exception error = null; + long expire = 0; + if (alive_time > 0) + expire = System.currentTimeMillis() + alive_time; + try { +// socket.setSoTimeout(socket_timeout); modified + // loop + while (!stop) { + try { + socket.receive(packet); + } catch (InterruptedIOException ie) { + if (alive_time > 0 && System.currentTimeMillis() > expire) + halt(); + continue; + } + if (packet.getLength() >= minimum_length) { + if (listener != null) + listener.onReceivedPacket(this, packet); + if (alive_time > 0) + expire = System.currentTimeMillis() + alive_time; + } + packet = new UdpPacket(buf, buf.length); + } + } catch (Exception e) { + error = e; + stop = true; + } + is_running = false; + if (listener != null) + listener.onServiceTerminated(this, error); + listener = null; + } + + /** Gets a String representation of the Object */ + public String toString() { + return "udp:" + socket.getLocalAddress() + ":" + socket.getLocalPort(); + } + +} diff --git a/src/org/zoolu/net/UdpProviderListener.java b/app/src/main/java/org/zoolu/net/UdpProviderListener.java similarity index 97% rename from src/org/zoolu/net/UdpProviderListener.java rename to app/src/main/java/org/zoolu/net/UdpProviderListener.java index 337f750..1e6ae37 100644 --- a/src/org/zoolu/net/UdpProviderListener.java +++ b/app/src/main/java/org/zoolu/net/UdpProviderListener.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -/** - * Listener for UdpProvider events. - */ -public interface UdpProviderListener { - /** When a new UDP datagram is received. */ - public void onReceivedPacket(UdpProvider udp, UdpPacket packet); - - /** When UdpProvider terminates. */ - public void onServiceTerminated(UdpProvider udp, Exception error); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +/** + * Listener for UdpProvider events. + */ +public interface UdpProviderListener { + /** When a new UDP datagram is received. */ + public void onReceivedPacket(UdpProvider udp, UdpPacket packet); + + /** When UdpProvider terminates. */ + public void onServiceTerminated(UdpProvider udp, Exception error); +} diff --git a/src/org/zoolu/net/UdpSocket.java b/app/src/main/java/org/zoolu/net/UdpSocket.java similarity index 96% rename from src/org/zoolu/net/UdpSocket.java rename to app/src/main/java/org/zoolu/net/UdpSocket.java index 31fafb8..3914d4d 100644 --- a/src/org/zoolu/net/UdpSocket.java +++ b/app/src/main/java/org/zoolu/net/UdpSocket.java @@ -1,104 +1,104 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.net; - -import java.net.DatagramSocket; -import java.net.DatagramPacket; - -/** - * UdpSocket provides a uniform interface to UDP transport protocol, regardless - * J2SE or J2ME is used. - */ -public class UdpSocket { - - /** DatagramSocket */ - DatagramSocket socket; - - /** Creates a new UdpSocket */ - public UdpSocket() throws java.net.SocketException { - socket = new DatagramSocket(); - } - - /** Creates a new UdpSocket */ - public UdpSocket(int port) throws java.net.SocketException { - socket = new DatagramSocket(port); - } - - /** Creates a new UdpSocket */ - UdpSocket(DatagramSocket sock) { - socket = sock; - } - - /** Creates a new UdpSocket */ - public UdpSocket(int port, IpAddress ipaddr) - throws java.net.SocketException { - socket = new DatagramSocket(port, ipaddr.getInetAddress()); - } - - /** Closes this datagram socket. */ - public void close() { - socket.close(); - } - - /** Gets the local address to which the socket is bound. */ - public IpAddress getLocalAddress() { - return new IpAddress(socket.getInetAddress()); - } - - /** Gets the port number on the local host to which this socket is bound. */ - public int getLocalPort() { - return socket.getLocalPort(); - } - - /** Gets the socket timeout. */ - public int getSoTimeout() throws java.net.SocketException { - return socket.getSoTimeout(); - } - - /** - * Enables/disables socket timeout with the specified timeout, in - * milliseconds. - */ - public void setSoTimeout(int timeout) throws java.net.SocketException { - socket.setSoTimeout(timeout); - } - - /** Receives a datagram packet from this socket. */ - public void receive(UdpPacket pkt) throws java.io.IOException { - DatagramPacket dgram = pkt.getDatagramPacket(); - socket.receive(dgram); - pkt.setDatagramPacket(dgram); - } - - /** Sends an UDP packet from this socket. */ - public void send(UdpPacket pkt) throws java.io.IOException { - socket.send(pkt.getDatagramPacket()); - } - - /** Converts this object to a String. */ - public String toString() { - return socket.toString(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.net; + +import java.net.DatagramSocket; +import java.net.DatagramPacket; + +/** + * UdpSocket provides a uniform interface to UDP transport protocol, regardless + * J2SE or J2ME is used. + */ +public class UdpSocket { + + /** DatagramSocket */ + DatagramSocket socket; + + /** Creates a new UdpSocket */ + public UdpSocket() throws java.net.SocketException { + socket = new DatagramSocket(); + } + + /** Creates a new UdpSocket */ + public UdpSocket(int port) throws java.net.SocketException { + socket = new DatagramSocket(port); + } + + /** Creates a new UdpSocket */ + UdpSocket(DatagramSocket sock) { + socket = sock; + } + + /** Creates a new UdpSocket */ + public UdpSocket(int port, IpAddress ipaddr) + throws java.net.SocketException { + socket = new DatagramSocket(port, ipaddr.getInetAddress()); + } + + /** Closes this datagram socket. */ + public void close() { + socket.close(); + } + + /** Gets the local address to which the socket is bound. */ + public IpAddress getLocalAddress() { + return new IpAddress(socket.getInetAddress()); + } + + /** Gets the port number on the local host to which this socket is bound. */ + public int getLocalPort() { + return socket.getLocalPort(); + } + + /** Gets the socket timeout. */ + public int getSoTimeout() throws java.net.SocketException { + return socket.getSoTimeout(); + } + + /** + * Enables/disables socket timeout with the specified timeout, in + * milliseconds. + */ + public void setSoTimeout(int timeout) throws java.net.SocketException { + socket.setSoTimeout(timeout); + } + + /** Receives a datagram packet from this socket. */ + public void receive(UdpPacket pkt) throws java.io.IOException { + DatagramPacket dgram = pkt.getDatagramPacket(); + socket.receive(dgram); + pkt.setDatagramPacket(dgram); + } + + /** Sends an UDP packet from this socket. */ + public void send(UdpPacket pkt) throws java.io.IOException { + socket.send(pkt.getDatagramPacket()); + } + + /** Converts this object to a String. */ + public String toString() { + return socket.toString(); + } + +} diff --git a/src/org/zoolu/sdp/AttributeField.java b/app/src/main/java/org/zoolu/sdp/AttributeField.java similarity index 96% rename from src/org/zoolu/sdp/AttributeField.java rename to app/src/main/java/org/zoolu/sdp/AttributeField.java index 173ec4b..e6a643a 100644 --- a/src/org/zoolu/sdp/AttributeField.java +++ b/app/src/main/java/org/zoolu/sdp/AttributeField.java @@ -1,76 +1,76 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sdp; - -/* HSC CHANGE START */ -/* import org.zoolu.tools.Parser; */ -/* HSC CHANGE END */ - -/** - * SDP attribute field. - *

- *

- * - *
- *    attribute-fields = "a=" (att-field ":" att-value) | att-field CRLF
- * 
- * - *
- */ -public class AttributeField extends SdpField { - /** Creates a new AttributeField. */ - public AttributeField(String attribute) { - super('a', attribute); - } - - /** Creates a new AttributeField. */ - public AttributeField(String attribute, String a_value) { - super('a', attribute + ":" + a_value); - } - - /** Creates a new AttributeField. */ - public AttributeField(SdpField sf) { - super(sf); - } - - /** Gets the attribute name. */ - public String getAttributeName() { - int i = value.indexOf(":"); - if (i < 0) - return value; - else - return value.substring(0, i); - } - - /** Gets the attribute value. */ - public String getAttributeValue() { - int i = value.indexOf(":"); - if (i < 0) - return null; - else - return value.substring(i + 1); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sdp; + +/* HSC CHANGE START */ +/* import org.zoolu.tools.Parser; */ +/* HSC CHANGE END */ + +/** + * SDP attribute field. + *

+ *

+ * + *
+ *    attribute-fields = "a=" (att-field ":" att-value) | att-field CRLF
+ * 
+ * + *
+ */ +public class AttributeField extends SdpField { + /** Creates a new AttributeField. */ + public AttributeField(String attribute) { + super('a', attribute); + } + + /** Creates a new AttributeField. */ + public AttributeField(String attribute, String a_value) { + super('a', attribute + ":" + a_value); + } + + /** Creates a new AttributeField. */ + public AttributeField(SdpField sf) { + super(sf); + } + + /** Gets the attribute name. */ + public String getAttributeName() { + int i = value.indexOf(":"); + if (i < 0) + return value; + else + return value.substring(0, i); + } + + /** Gets the attribute value. */ + public String getAttributeValue() { + int i = value.indexOf(":"); + if (i < 0) + return null; + else + return value.substring(i + 1); + } + +} diff --git a/src/org/zoolu/sdp/ConnectionField.java b/app/src/main/java/org/zoolu/sdp/ConnectionField.java similarity index 96% rename from src/org/zoolu/sdp/ConnectionField.java rename to app/src/main/java/org/zoolu/sdp/ConnectionField.java index 18c59a7..0e53da3 100644 --- a/src/org/zoolu/sdp/ConnectionField.java +++ b/app/src/main/java/org/zoolu/sdp/ConnectionField.java @@ -1,112 +1,112 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sdp; - -import org.zoolu.tools.Parser; - -/** - * SDP connection field. - *

- *

- * - *
- *    connection-field = "c=" nettype SP addrtype SP connection-address CRLF
- *                       ;a connection field must be present
- *                       ;in every media description or at the
- *                       ;session-level
- * 
- * - *
- */ -public class ConnectionField extends SdpField { - /** Creates a new ConnectionField. */ - public ConnectionField(String connection_field) { - super('c', connection_field); - } - - /** Creates a new ConnectionField. */ - public ConnectionField(String address_type, String address, int ttl, int num) { - super('c', null); - value = "IN " + address_type + " " + address; - if (ttl > 0) - value += "/" + ttl; - if (num > 0) - value += "/" + num; - } - - /** Creates a new ConnectionField. */ - public ConnectionField(String address_type, String address) { - super('c', "IN " + address_type + " " + address); - } - - /** Creates a new ConnectionField. */ - public ConnectionField(SdpField sf) { - super(sf); - } - - /** Gets the connection address. */ - public String getAddressType() { - String type = (new Parser(value)).skipString().getString(); - return type; - } - - /** Gets the connection address. */ - public String getAddress() { - String address = (new Parser(value)).skipString().skipString() - .getString(); - int i = address.indexOf("/"); - if (i < 0) - return address; - else - return address.substring(0, i); - } - - /** Gets the TTL. */ - public int getTTL() { - String address = (new Parser(value)).skipString().skipString() - .getString(); - int i = address.indexOf("/"); - if (i < 0) - return 0; - int j = address.indexOf("/", i); - if (j < 0) - return Integer.parseInt(address.substring(i)); - else - return Integer.parseInt(address.substring(i, j)); - } - - /** Gets the number of addresses. */ - public int getNum() { - String address = (new Parser(value)).skipString().skipString() - .getString(); - int i = address.indexOf("/"); - if (i < 0) - return 0; - int j = address.indexOf("/", i); - if (j < 0) - return 0; - return Integer.parseInt(address.substring(j)); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sdp; + +import org.zoolu.tools.Parser; + +/** + * SDP connection field. + *

+ *

+ * + *
+ *    connection-field = "c=" nettype SP addrtype SP connection-address CRLF
+ *                       ;a connection field must be present
+ *                       ;in every media description or at the
+ *                       ;session-level
+ * 
+ * + *
+ */ +public class ConnectionField extends SdpField { + /** Creates a new ConnectionField. */ + public ConnectionField(String connection_field) { + super('c', connection_field); + } + + /** Creates a new ConnectionField. */ + public ConnectionField(String address_type, String address, int ttl, int num) { + super('c', null); + value = "IN " + address_type + " " + address; + if (ttl > 0) + value += "/" + ttl; + if (num > 0) + value += "/" + num; + } + + /** Creates a new ConnectionField. */ + public ConnectionField(String address_type, String address) { + super('c', "IN " + address_type + " " + address); + } + + /** Creates a new ConnectionField. */ + public ConnectionField(SdpField sf) { + super(sf); + } + + /** Gets the connection address. */ + public String getAddressType() { + String type = (new Parser(value)).skipString().getString(); + return type; + } + + /** Gets the connection address. */ + public String getAddress() { + String address = (new Parser(value)).skipString().skipString() + .getString(); + int i = address.indexOf("/"); + if (i < 0) + return address; + else + return address.substring(0, i); + } + + /** Gets the TTL. */ + public int getTTL() { + String address = (new Parser(value)).skipString().skipString() + .getString(); + int i = address.indexOf("/"); + if (i < 0) + return 0; + int j = address.indexOf("/", i); + if (j < 0) + return Integer.parseInt(address.substring(i)); + else + return Integer.parseInt(address.substring(i, j)); + } + + /** Gets the number of addresses. */ + public int getNum() { + String address = (new Parser(value)).skipString().skipString() + .getString(); + int i = address.indexOf("/"); + if (i < 0) + return 0; + int j = address.indexOf("/", i); + if (j < 0) + return 0; + return Integer.parseInt(address.substring(j)); + } + +} diff --git a/src/org/zoolu/sdp/MediaDescriptor.java b/app/src/main/java/org/zoolu/sdp/MediaDescriptor.java similarity index 96% rename from src/org/zoolu/sdp/MediaDescriptor.java rename to app/src/main/java/org/zoolu/sdp/MediaDescriptor.java index f28dd8e..f2795db 100644 --- a/src/org/zoolu/sdp/MediaDescriptor.java +++ b/app/src/main/java/org/zoolu/sdp/MediaDescriptor.java @@ -1,308 +1,308 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2010 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sdp; - -import java.util.Vector; - -/** - * Class MediaDescriptor handles SDP media descpriptions. - *

- * A MediaDescriptor can be part of a SessionDescriptor, and contains details - * that apply onto to a single media stream. - *

- * A single SessionDescriptor may convey zero or more MediaDescriptors. - *

- * In the current implementation, the MediaDescriptor consists of the m (media) - * and c (connection information) fields, followed by zero or more a (attribute) - * fields. The m field is mandatory for a MediaDescriptor. - */ -public class MediaDescriptor { - /** Media field ('m'). */ - MediaField m; - /** Connection field ('c') */ - ConnectionField c; - /** Vector of attribute fileds ('a') */ - /* HSC CHANGE STARTS */ - Vector av; - - /* HSC CHANGE ENDS */ - - /** - * Creates a new MediaDescriptor. - * - * @param md - * the cloned MediaDescriptor - */ - public MediaDescriptor(MediaDescriptor md) { - m = new MediaField(md.m); - if (md.c != null) - c = new ConnectionField(md.c); - else - c = null; - /* HSC CHANGE STARTS */ - av = new Vector(); - /* HSC CHANGE ENDS */ - for (int i = 0; i < md.av.size(); i++) { - av.addElement(new AttributeField((AttributeField) md.av - .elementAt(i))); - } - } - - /** - * Creates a new MediaDescriptor with m media and c connection. - * No attribute is set by default. - * - * @param media - * the MediaField - * @param connection - * the ConnectionField, or null if no ConnectionField is present - * in the MediaDescriptor - */ - public MediaDescriptor(MediaField media, ConnectionField connection) { - m = media; - c = connection; - /* HSC CHANGE BEGINS */ - av = new Vector(); - /* HSC CHANGE ENDS */ - } - - /** - * Creates a new MediaDescriptor with m media, c connection, - * and a attribute. - * - * @param media - * the MediaField - * @param connection - * the ConnectionField, or null if no ConnectionField is present - * in the MediaDescriptor - * @param attribute - * the first AttributeField - */ - public MediaDescriptor(MediaField media, ConnectionField connection, - AttributeField attribute) { - m = media; - c = connection; - /* HSC CHANGE BEGINS */ - av = new Vector(); - /* HSC CHANGE ENDS */ - if (attribute != null) - av.addElement(attribute); - } - - /** - * Creates a new MediaDescriptor with m=media and c=connection, - * with attributes 'a' equals to attributes (Vector of - * AttributeField). - * - * @param media - * the MediaField - * @param connection - * the ConnectionField, or null if no ConnectionField is present - * in the MediaDescriptor - * @param attributes - * the Vector of AttributeField - */ - /* HSC CHANGE BEGINS */ - public MediaDescriptor(MediaField media, ConnectionField connection, - Vector attributes) { - m = media; - c = connection; - av = new Vector(attributes.size()); - av.setSize(attributes.size()); - for (int i = 0; i < attributes.size(); i++) - av.setElementAt((AttributeField) attributes.elementAt(i), i); - } - - /* HSC CHANGE ENDS */ - - /** - * Creates a new MediaDescriptor with m media, c connection, - * and a attribute. - * - * @param media - * the media field vaule - * @param connection - * the connection field vaule, or null if no connection field is - * present in the MediaDescriptor - * @param attribute - * the first media attribute alue - */ - public MediaDescriptor(String media, String connection, String attribute) { - m = new MediaField(media); - if (connection != null) - c = new ConnectionField(connection); - /* HSC CHANGE BEGINS */ - av = new Vector(); - /* HSC CHANGE ENDS */ - if (attribute != null) - av.addElement(new AttributeField(attribute)); - } - - /** - * Creates a new MediaDescriptor from String str. - * - * @param str - * the media field line - */ - /* - * public MediaDescriptor(String str) { SdpParser par=new SdpParser(str); - * m=par.parseMediaField(); c=par.parseConnectionField(); av=new Vector(); - * AttributeField a=par.parseAttributeField(); while (a!=null) { - * av.addElement(a); a=par.parseAttributeField(); } } - */ - - /** - * Gets media. - * - * @return the MediaField - */ - public MediaField getMedia() { - return m; - } - - /** - * Gets connection information. - * - * @return the ConnectionField - */ - public ConnectionField getConnection() { - return c; - } - - /** - * Gets a Vector of attribute values. - * - * @return a Vector of AttributeField - */ - /* HSC CHANGE BEGINS */ - public Vector getAttributes() { - Vector v = new Vector(av.size()); - /* HSC CHANGE ENDS */ - for (int i = 0; i < av.size(); i++) - v.addElement((AttributeField) av.elementAt(i)); - return v; - } - - /** - * Adds a new attribute - * - * @param attribute - * the new AttributeField - * @return this MediaDescriptor - */ - public MediaDescriptor addAttribute(AttributeField attribute) { - av.addElement(new AttributeField(attribute)); - return this; - } - - /** - * Whether it has a particular attribute - * - * @param a_name - * the attribute name - * @return true if found, otherwise returns null - */ - public boolean hasAttribute(String a_name) { - for (int i = 0; i < av.size(); i++) { - if (((AttributeField) av.elementAt(i)).getAttributeName().equals( - a_name)) - return true; - } - return false; - } - - /** - * Gets a particular attribute - * - * @param a_name - * the attribute name - * @return the AttributeField, or null if not found - */ - public AttributeField getAttribute(String a_name) { - for (int i = 0; i < av.size(); i++) { - AttributeField a = (AttributeField) av.elementAt(i); - if (a.getAttributeName().equals(a_name)) - return a; - } - return null; - } - - /** - * Gets a Vector of attribute values of a particular attribute name. - * - * @param a_name - * the attribute name - * @return a Vector of AttributeFields - */ - /* HSC CHANGE BEGINS */ - public Vector getAttributes(String a_name) { - Vector v = new Vector(av.size()); - /* HSC CHANGE ENDS */ - for (int i = 0; i < av.size(); i++) { - AttributeField a = (AttributeField) av.elementAt(i); - if (a.getAttributeName().equals(a_name)) - v.addElement(a); - } - return v; - } - - /** - * Returns whether a particular codec is included in the media descriptor. - * - * @param codec - * the codec name (e.g. "GSM", "PCMA/8000") - * @return a String of the matched codec (e.g. "GSM/8000"), - * or null when the codec is not included. - */ - public String hasCodec(String codec) { - for (int i = 0; i < av.size(); i++) { - AttributeField a = (AttributeField) av.elementAt(i); - if (a.getAttributeName().equalsIgnoreCase("rtpmap")) { - String[] ar = a.getAttributeValue().split(" +", 2); - if (ar.length==2 && ar[1].toLowerCase().startsWith(codec.toLowerCase())) { - return ar[1]; - } - } - } - return null; - } - - /** - * Gets a String rapresentation of the MediaDescriptor. - * - * @return the string representation - */ - public String toString() { - String str = ""; - str += m; - if (c != null) - str += c; - for (int i = 0; i < av.size(); i++) - str += (AttributeField) av.elementAt(i); - return str; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2010 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sdp; + +import java.util.Vector; + +/** + * Class MediaDescriptor handles SDP media descpriptions. + *

+ * A MediaDescriptor can be part of a SessionDescriptor, and contains details + * that apply onto to a single media stream. + *

+ * A single SessionDescriptor may convey zero or more MediaDescriptors. + *

+ * In the current implementation, the MediaDescriptor consists of the m (media) + * and c (connection information) fields, followed by zero or more a (attribute) + * fields. The m field is mandatory for a MediaDescriptor. + */ +public class MediaDescriptor { + /** Media field ('m'). */ + MediaField m; + /** Connection field ('c') */ + ConnectionField c; + /** Vector of attribute fileds ('a') */ + /* HSC CHANGE STARTS */ + Vector av; + + /* HSC CHANGE ENDS */ + + /** + * Creates a new MediaDescriptor. + * + * @param md + * the cloned MediaDescriptor + */ + public MediaDescriptor(MediaDescriptor md) { + m = new MediaField(md.m); + if (md.c != null) + c = new ConnectionField(md.c); + else + c = null; + /* HSC CHANGE STARTS */ + av = new Vector(); + /* HSC CHANGE ENDS */ + for (int i = 0; i < md.av.size(); i++) { + av.addElement(new AttributeField((AttributeField) md.av + .elementAt(i))); + } + } + + /** + * Creates a new MediaDescriptor with m media and c connection. + * No attribute is set by default. + * + * @param media + * the MediaField + * @param connection + * the ConnectionField, or null if no ConnectionField is present + * in the MediaDescriptor + */ + public MediaDescriptor(MediaField media, ConnectionField connection) { + m = media; + c = connection; + /* HSC CHANGE BEGINS */ + av = new Vector(); + /* HSC CHANGE ENDS */ + } + + /** + * Creates a new MediaDescriptor with m media, c connection, + * and a attribute. + * + * @param media + * the MediaField + * @param connection + * the ConnectionField, or null if no ConnectionField is present + * in the MediaDescriptor + * @param attribute + * the first AttributeField + */ + public MediaDescriptor(MediaField media, ConnectionField connection, + AttributeField attribute) { + m = media; + c = connection; + /* HSC CHANGE BEGINS */ + av = new Vector(); + /* HSC CHANGE ENDS */ + if (attribute != null) + av.addElement(attribute); + } + + /** + * Creates a new MediaDescriptor with m=media and c=connection, + * with attributes 'a' equals to attributes (Vector of + * AttributeField). + * + * @param media + * the MediaField + * @param connection + * the ConnectionField, or null if no ConnectionField is present + * in the MediaDescriptor + * @param attributes + * the Vector of AttributeField + */ + /* HSC CHANGE BEGINS */ + public MediaDescriptor(MediaField media, ConnectionField connection, + Vector attributes) { + m = media; + c = connection; + av = new Vector(attributes.size()); + av.setSize(attributes.size()); + for (int i = 0; i < attributes.size(); i++) + av.setElementAt((AttributeField) attributes.elementAt(i), i); + } + + /* HSC CHANGE ENDS */ + + /** + * Creates a new MediaDescriptor with m media, c connection, + * and a attribute. + * + * @param media + * the media field vaule + * @param connection + * the connection field vaule, or null if no connection field is + * present in the MediaDescriptor + * @param attribute + * the first media attribute alue + */ + public MediaDescriptor(String media, String connection, String attribute) { + m = new MediaField(media); + if (connection != null) + c = new ConnectionField(connection); + /* HSC CHANGE BEGINS */ + av = new Vector(); + /* HSC CHANGE ENDS */ + if (attribute != null) + av.addElement(new AttributeField(attribute)); + } + + /** + * Creates a new MediaDescriptor from String str. + * + * @param str + * the media field line + */ + /* + * public MediaDescriptor(String str) { SdpParser par=new SdpParser(str); + * m=par.parseMediaField(); c=par.parseConnectionField(); av=new Vector(); + * AttributeField a=par.parseAttributeField(); while (a!=null) { + * av.addElement(a); a=par.parseAttributeField(); } } + */ + + /** + * Gets media. + * + * @return the MediaField + */ + public MediaField getMedia() { + return m; + } + + /** + * Gets connection information. + * + * @return the ConnectionField + */ + public ConnectionField getConnection() { + return c; + } + + /** + * Gets a Vector of attribute values. + * + * @return a Vector of AttributeField + */ + /* HSC CHANGE BEGINS */ + public Vector getAttributes() { + Vector v = new Vector(av.size()); + /* HSC CHANGE ENDS */ + for (int i = 0; i < av.size(); i++) + v.addElement((AttributeField) av.elementAt(i)); + return v; + } + + /** + * Adds a new attribute + * + * @param attribute + * the new AttributeField + * @return this MediaDescriptor + */ + public MediaDescriptor addAttribute(AttributeField attribute) { + av.addElement(new AttributeField(attribute)); + return this; + } + + /** + * Whether it has a particular attribute + * + * @param a_name + * the attribute name + * @return true if found, otherwise returns null + */ + public boolean hasAttribute(String a_name) { + for (int i = 0; i < av.size(); i++) { + if (((AttributeField) av.elementAt(i)).getAttributeName().equals( + a_name)) + return true; + } + return false; + } + + /** + * Gets a particular attribute + * + * @param a_name + * the attribute name + * @return the AttributeField, or null if not found + */ + public AttributeField getAttribute(String a_name) { + for (int i = 0; i < av.size(); i++) { + AttributeField a = (AttributeField) av.elementAt(i); + if (a.getAttributeName().equals(a_name)) + return a; + } + return null; + } + + /** + * Gets a Vector of attribute values of a particular attribute name. + * + * @param a_name + * the attribute name + * @return a Vector of AttributeFields + */ + /* HSC CHANGE BEGINS */ + public Vector getAttributes(String a_name) { + Vector v = new Vector(av.size()); + /* HSC CHANGE ENDS */ + for (int i = 0; i < av.size(); i++) { + AttributeField a = (AttributeField) av.elementAt(i); + if (a.getAttributeName().equals(a_name)) + v.addElement(a); + } + return v; + } + + /** + * Returns whether a particular codec is included in the media descriptor. + * + * @param codec + * the codec name (e.g. "GSM", "PCMA/8000") + * @return a String of the matched codec (e.g. "GSM/8000"), + * or null when the codec is not included. + */ + public String hasCodec(String codec) { + for (int i = 0; i < av.size(); i++) { + AttributeField a = (AttributeField) av.elementAt(i); + if (a.getAttributeName().equalsIgnoreCase("rtpmap")) { + String[] ar = a.getAttributeValue().split(" +", 2); + if (ar.length==2 && ar[1].toLowerCase().startsWith(codec.toLowerCase())) { + return ar[1]; + } + } + } + return null; + } + + /** + * Gets a String rapresentation of the MediaDescriptor. + * + * @return the string representation + */ + public String toString() { + String str = ""; + str += m; + if (c != null) + str += c; + for (int i = 0; i < av.size(); i++) + str += (AttributeField) av.elementAt(i); + return str; + } + +} diff --git a/src/org/zoolu/sdp/MediaField.java b/app/src/main/java/org/zoolu/sdp/MediaField.java similarity index 96% rename from src/org/zoolu/sdp/MediaField.java rename to app/src/main/java/org/zoolu/sdp/MediaField.java index b0fe17d..f613be2 100644 --- a/src/org/zoolu/sdp/MediaField.java +++ b/app/src/main/java/org/zoolu/sdp/MediaField.java @@ -1,122 +1,122 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sdp; - -import org.zoolu.tools.Parser; -import java.util.Vector; - -/** - * SDP media field. - *

- *

- * - *
- *      media-field = "m=" media SP port ["/" integer] SP proto 1*(SP fmt) CRLF
- * 
- * - *
- */ -public class MediaField extends SdpField { - /** Creates a new MediaField. */ - public MediaField(String media_field) { - super('m', media_field); - } - - /** Creates a new MediaField. */ - public MediaField(String media, int port, int num, String transport, - String formats) { - super('m', null); - value = media + " " + port; - if (num > 0) - value += "/" + num; - value += " " + transport + " " + formats; - } - - /** - * Creates a new MediaField. - * - * @param formatlist - * a Vector of media formats (properly a Vector of Strings) - */ - public MediaField(String media, int port, int num, String transport, - /* HSC CHANGE BEGINS */ - Vector formatlist) { - /* HSC CHANGE ENDS */ - super('m', null); - value = media + " " + port; - if (num > 0) - value += "/" + num; - value += " " + transport; - for (int i = 0; i < formatlist.size(); i++) - value += " " + formatlist.elementAt(i); - } - - /** Creates a new SdpField. */ - public MediaField(SdpField sf) { - super(sf); - } - - /** Gets the media type. */ - public String getMedia() { - return new Parser(value).getString(); - } - - /** Gets the media port. */ - public int getPort() { - String port = (new Parser(value)).skipString().getString(); - int i = port.indexOf('/'); - if (i < 0) - return Integer.parseInt(port); - else - return Integer.parseInt(port.substring(0, i)); - } - - /** Gets the transport protocol. */ - public String getTransport() { - return (new Parser(value)).skipString().skipString().getString(); - } - - /** Gets the media formats. */ - public String getFormats() { - return (new Parser(value)).skipString().skipString().skipString() - .skipWSP().getRemainingString(); - } - - /** Gets the media formats as a Vector of String. */ - /* HSC CHANGE BEGINS */ - public Vector getFormatList() { - Vector formatlist = new Vector(); - /* HSC CHANGE ENDS */ - Parser par = new Parser(value); - par.skipString().skipString().skipString(); - while (par.hasMore()) { - String fmt = par.getString(); - if (fmt != null && fmt.length() > 0) - formatlist.addElement(fmt); - } - return formatlist; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sdp; + +import org.zoolu.tools.Parser; +import java.util.Vector; + +/** + * SDP media field. + *

+ *

+ * + *
+ *      media-field = "m=" media SP port ["/" integer] SP proto 1*(SP fmt) CRLF
+ * 
+ * + *
+ */ +public class MediaField extends SdpField { + /** Creates a new MediaField. */ + public MediaField(String media_field) { + super('m', media_field); + } + + /** Creates a new MediaField. */ + public MediaField(String media, int port, int num, String transport, + String formats) { + super('m', null); + value = media + " " + port; + if (num > 0) + value += "/" + num; + value += " " + transport + " " + formats; + } + + /** + * Creates a new MediaField. + * + * @param formatlist + * a Vector of media formats (properly a Vector of Strings) + */ + public MediaField(String media, int port, int num, String transport, + /* HSC CHANGE BEGINS */ + Vector formatlist) { + /* HSC CHANGE ENDS */ + super('m', null); + value = media + " " + port; + if (num > 0) + value += "/" + num; + value += " " + transport; + for (int i = 0; i < formatlist.size(); i++) + value += " " + formatlist.elementAt(i); + } + + /** Creates a new SdpField. */ + public MediaField(SdpField sf) { + super(sf); + } + + /** Gets the media type. */ + public String getMedia() { + return new Parser(value).getString(); + } + + /** Gets the media port. */ + public int getPort() { + String port = (new Parser(value)).skipString().getString(); + int i = port.indexOf('/'); + if (i < 0) + return Integer.parseInt(port); + else + return Integer.parseInt(port.substring(0, i)); + } + + /** Gets the transport protocol. */ + public String getTransport() { + return (new Parser(value)).skipString().skipString().getString(); + } + + /** Gets the media formats. */ + public String getFormats() { + return (new Parser(value)).skipString().skipString().skipString() + .skipWSP().getRemainingString(); + } + + /** Gets the media formats as a Vector of String. */ + /* HSC CHANGE BEGINS */ + public Vector getFormatList() { + Vector formatlist = new Vector(); + /* HSC CHANGE ENDS */ + Parser par = new Parser(value); + par.skipString().skipString().skipString(); + while (par.hasMore()) { + String fmt = par.getString(); + if (fmt != null && fmt.length() > 0) + formatlist.addElement(fmt); + } + return formatlist; + } + +} diff --git a/src/org/zoolu/sdp/OriginField.java b/app/src/main/java/org/zoolu/sdp/OriginField.java similarity index 96% rename from src/org/zoolu/sdp/OriginField.java rename to app/src/main/java/org/zoolu/sdp/OriginField.java index 9f0d2e9..2164510 100644 --- a/src/org/zoolu/sdp/OriginField.java +++ b/app/src/main/java/org/zoolu/sdp/OriginField.java @@ -1,92 +1,92 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sdp; - -import org.zoolu.tools.Parser; - -/** - * SDP origin field. - *

- *

- * - *
- *    origin-field = "o=" username SP sess-id SP sess-version SP
- *                        nettype SP addrtype SP unicast-address CRLF
- * 
- * - *
- */ -public class OriginField extends SdpField { - /** Creates a new OriginField. */ - public OriginField(String origin) { - super('o', origin); - } - - /** Creates a new OriginField. */ - public OriginField(String username, String sess_id, String sess_version, - String addrtype, String address) { - super('o', username + " " + sess_id + " " + sess_version + " IN " - + addrtype + " " + address); - } - - /** Creates a new OriginField. */ - public OriginField(String username, String sess_id, String sess_version, - String address) { - super('o', username + " " + sess_id + " " + sess_version + " IN IP4 " - + address); - } - - /** Creates a new OriginField. */ - public OriginField(SdpField sf) { - super(sf); - } - - /** Gets the user name. */ - public String getUserName() { - return (new Parser(value)).getString(); - } - - /** Gets the session id. */ - public String getSessionId() { - return (new Parser(value)).skipString().getString(); - } - - /** Gets the session version. */ - public String getSessionVersion() { - return (new Parser(value)).skipString().skipString().getString(); - } - - /** Gets the address type. */ - public String getAddressType() { - return (new Parser(value)).skipString().skipString().skipString() - .skipString().getString(); - } - - /** Gets the address. */ - public String getAddress() { - return (new Parser(value)).skipString().skipString().skipString() - .skipString().skipString().getString(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sdp; + +import org.zoolu.tools.Parser; + +/** + * SDP origin field. + *

+ *

+ * + *
+ *    origin-field = "o=" username SP sess-id SP sess-version SP
+ *                        nettype SP addrtype SP unicast-address CRLF
+ * 
+ * + *
+ */ +public class OriginField extends SdpField { + /** Creates a new OriginField. */ + public OriginField(String origin) { + super('o', origin); + } + + /** Creates a new OriginField. */ + public OriginField(String username, String sess_id, String sess_version, + String addrtype, String address) { + super('o', username + " " + sess_id + " " + sess_version + " IN " + + addrtype + " " + address); + } + + /** Creates a new OriginField. */ + public OriginField(String username, String sess_id, String sess_version, + String address) { + super('o', username + " " + sess_id + " " + sess_version + " IN IP4 " + + address); + } + + /** Creates a new OriginField. */ + public OriginField(SdpField sf) { + super(sf); + } + + /** Gets the user name. */ + public String getUserName() { + return (new Parser(value)).getString(); + } + + /** Gets the session id. */ + public String getSessionId() { + return (new Parser(value)).skipString().getString(); + } + + /** Gets the session version. */ + public String getSessionVersion() { + return (new Parser(value)).skipString().skipString().getString(); + } + + /** Gets the address type. */ + public String getAddressType() { + return (new Parser(value)).skipString().skipString().skipString() + .skipString().getString(); + } + + /** Gets the address. */ + public String getAddress() { + return (new Parser(value)).skipString().skipString().skipString() + .skipString().skipString().getString(); + } + +} diff --git a/src/org/zoolu/sdp/SdpField.java b/app/src/main/java/org/zoolu/sdp/SdpField.java similarity index 95% rename from src/org/zoolu/sdp/SdpField.java rename to app/src/main/java/org/zoolu/sdp/SdpField.java index 56eb8f0..a853464 100644 --- a/src/org/zoolu/sdp/SdpField.java +++ b/app/src/main/java/org/zoolu/sdp/SdpField.java @@ -1,129 +1,129 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sdp; - -/** - * SdpField rapresents a SDP line field. It is formed by a 'type' (char) and a - * 'value' (String). - *

- * A SDP line field is of the form <type> = <value> - */ -public class SdpField { - char type; - String value; - - /** - * Creates a new SdpField. - * - * @param s_type - * the field type - * @param s_value - * the field value - */ - public SdpField(char s_type, String s_value) { - type = s_type; - value = s_value; - } - - /** - * Creates a new SdpField. - * - * @param sf - * the SdpField clone - */ - public SdpField(SdpField sf) { - type = sf.type; - value = sf.value; - } - - /** - * Creates a new SdpField based on a SDP line of the form =. - * The SDP value terminats with the end of the String or with the first CR - * or LF char. - * - * @param str - * the <type> = <value> line - */ - public SdpField(String str) { - SdpParser par = new SdpParser(str); - SdpField sf = par.parseSdpField(); - type = sf.type; - value = sf.value; - } - - /** - * Creates and returns a copy of the SdpField. - * - * @return a SdpField clone - */ - public Object clone() { - return new SdpField(this); - } - - /** - * Whether the SdpField is equal to Object obj - * - * @return true if equal - */ - public boolean equals(Object obj) { - try { - SdpField sf = (SdpField) obj; - if (type != sf.type) - return false; - if (value != sf.value) - return false; - return true; - } catch (Exception e) { - return false; - } - } - - /** - * Gets the type of field - * - * @return the field type - */ - public char getType() { - return type; - } - - /** - * Gets the value - * - * @return the field value - */ - public String getValue() { - return value; - } - - /** - * Gets string representation of the SdpField - * - * @return the string representation - */ - public String toString() { - return type + "=" + value + "\r\n"; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sdp; + +/** + * SdpField rapresents a SDP line field. It is formed by a 'type' (char) and a + * 'value' (String). + *

+ * A SDP line field is of the form <type> = <value> + */ +public class SdpField { + char type; + String value; + + /** + * Creates a new SdpField. + * + * @param s_type + * the field type + * @param s_value + * the field value + */ + public SdpField(char s_type, String s_value) { + type = s_type; + value = s_value; + } + + /** + * Creates a new SdpField. + * + * @param sf + * the SdpField clone + */ + public SdpField(SdpField sf) { + type = sf.type; + value = sf.value; + } + + /** + * Creates a new SdpField based on a SDP line of the form =. + * The SDP value terminats with the end of the String or with the first CR + * or LF char. + * + * @param str + * the <type> = <value> line + */ + public SdpField(String str) { + SdpParser par = new SdpParser(str); + SdpField sf = par.parseSdpField(); + type = sf.type; + value = sf.value; + } + + /** + * Creates and returns a copy of the SdpField. + * + * @return a SdpField clone + */ + public Object clone() { + return new SdpField(this); + } + + /** + * Whether the SdpField is equal to Object obj + * + * @return true if equal + */ + public boolean equals(Object obj) { + try { + SdpField sf = (SdpField) obj; + if (type != sf.type) + return false; + if (value != sf.value) + return false; + return true; + } catch (Exception e) { + return false; + } + } + + /** + * Gets the type of field + * + * @return the field type + */ + public char getType() { + return type; + } + + /** + * Gets the value + * + * @return the field value + */ + public String getValue() { + return value; + } + + /** + * Gets string representation of the SdpField + * + * @return the string representation + */ + public String toString() { + return type + "=" + value + "\r\n"; + } + +} diff --git a/src/org/zoolu/sdp/SdpParser.java b/app/src/main/java/org/zoolu/sdp/SdpParser.java similarity index 96% rename from src/org/zoolu/sdp/SdpParser.java rename to app/src/main/java/org/zoolu/sdp/SdpParser.java index e541cc0..7b8a7ae 100644 --- a/src/org/zoolu/sdp/SdpParser.java +++ b/app/src/main/java/org/zoolu/sdp/SdpParser.java @@ -1,227 +1,227 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sdp; - -/* HSC CHANGE STARTS */ -// import org.zoolu.sdp.*; -/* HSC CHANGE ENDS */ -import org.zoolu.tools.Parser; -import java.util.Vector; - -/** - * Class SdpParser extends class Parser for parsing of SDP strings. - */ -class SdpParser extends Parser { - /** Creates a SdpParser based on String s */ - public SdpParser(String s) { - super(s); - } - - /** - * Creates a SdpParser based on String s and starting from position - * i - */ - public SdpParser(String s, int i) { - super(s, i); - } - - /** - * Returns the SdpField at the current position. The SDP value terminates - * with the end of the String or with the first CR or LF char. - *

- * Returns null if no SdpField is recognized. - */ - /* - * public SdpField parseSdpField() { Parser par=new Parser(str,index); while - * (par.length()>1 && par.charAt(1)!='=') par.goToNextLine(); if - * (!par.hasMore()) return null; char type=par.getChar(); par.skipChar(); - * String value=par.skipChar().getLine(); if (value==null) return null; - * index=par.getPos(); // for DEBUG //System.out.println("DEBUG: - * "+type+"="+value); return new SdpField(type,value); } - */ - - /** - * Returns the first SdpField. The SDP value terminates with the end of the - * String or with the first CR or LF char. - * - * @return the first SdpField, or null if no SdpField is recognized. - */ - public SdpField parseSdpField() { - int begin = index; - while (begin >= 0 && begin < str.length() - 1 - && str.charAt(begin + 1) != '=') - begin = str.indexOf("\n", begin); - if (begin < 0) - return null; - char type = str.charAt(begin); - begin += 2; - int end = str.length(); - int CR = str.indexOf('\r', begin); - if (CR > 0 && CR < end) - end = CR; - int LF = str.indexOf('\n', begin); - if (LF > 0 && LF < end) - end = LF; - String value = str.substring(begin, end).trim(); - if (value == null) - return null; - setPos(end); - goToNextLine(); - // for DEBUG - // System.out.println("DEBUG: "+type+"="+value); - return new SdpField(type, value); - } - - /** - * Returns the first SdpField of type type. The SDP value terminates - * with the end of the String or with the first CR or LF char. - * - * @return the first SdpField, or null if no type SdpField is found. - */ - public SdpField parseSdpField(char type) { - int begin = 0; - if (!str.startsWith(type + "=", index)) { - begin = str.indexOf("\n" + type + "=", index); - // if (begin<0) begin=str.indexOf("\r"+type+"=",index); - if (begin < 0) { // return null if no type SdpField has been - // found - return null; - } - index = begin + 1; - } - return parseSdpField(); - } - - /** - * Returns the first OriginField. - * - * @return the first OriginField, or null if no OriginField is found. - */ - public OriginField parseOriginField() { - SdpField sf = parseSdpField('o'); - if (sf != null) - return new OriginField(sf); - else - return null; - } - - /** - * Returns the first MediaField. - * - * @return the first MediaField, or null if no MediaField is found. - */ - public MediaField parseMediaField() { - SdpField sf = parseSdpField('m'); - if (sf != null) - return new MediaField(sf); - else - return null; - } - - /** - * Returns the first ConnectionField. - * - * @return the first ConnectionField, or null if no ConnectionField is - * found. - */ - public ConnectionField parseConnectionField() { - SdpField sf = parseSdpField('c'); - if (sf != null) - return new ConnectionField(sf); - else - return null; - } - - /** - * Returns the first SessionNameField. - * - * @return the first SessionNameField, or null if no SessionNameField is - * found. - */ - public SessionNameField parseSessionNameField() { - SdpField sf = parseSdpField('s'); - if (sf != null) - return new SessionNameField(sf); - else - return null; - } - - /** - * Returns the first TimeField. - * - * @return the first TimeField, or null if no TimeField is found. - */ - public TimeField parseTimeField() { - SdpField sf = parseSdpField('t'); - if (sf != null) - return new TimeField(sf); - else - return null; - } - - /** - * Returns the first AttributeField. - * - * @return the first AttributeField, or null if no AttributeField is found. - */ - public AttributeField parseAttributeField() { - SdpField sf = parseSdpField('a'); - if (sf != null) - return new AttributeField(sf); - else - return null; - } - - /** - * Returns the first MediaDescriptor. - * - * @return the first MediaDescriptor, or null if no MediaDescriptor is - * found. - */ - public MediaDescriptor parseMediaDescriptor() { - MediaField m = parseMediaField(); - if (m == null) - return null; - int begin = index; - int end = str.indexOf("\nm", begin); - if (end < 0) - end = str.length(); - else - end++; - index = end; - SdpParser par = new SdpParser(str.substring(begin, end)); - ConnectionField c = par.parseConnectionField(); - /* HSC CHANGE BEGINS */ - Vector av = new Vector(); - /* HSC CHANGE ENDS */ - AttributeField a = par.parseAttributeField(); - while (a != null) { - av.addElement(a); - a = par.parseAttributeField(); - } - return new MediaDescriptor(m, c, av); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sdp; + +/* HSC CHANGE STARTS */ +// import org.zoolu.sdp.*; +/* HSC CHANGE ENDS */ +import org.zoolu.tools.Parser; +import java.util.Vector; + +/** + * Class SdpParser extends class Parser for parsing of SDP strings. + */ +class SdpParser extends Parser { + /** Creates a SdpParser based on String s */ + public SdpParser(String s) { + super(s); + } + + /** + * Creates a SdpParser based on String s and starting from position + * i + */ + public SdpParser(String s, int i) { + super(s, i); + } + + /** + * Returns the SdpField at the current position. The SDP value terminates + * with the end of the String or with the first CR or LF char. + *

+ * Returns null if no SdpField is recognized. + */ + /* + * public SdpField parseSdpField() { Parser par=new Parser(str,index); while + * (par.length()>1 && par.charAt(1)!='=') par.goToNextLine(); if + * (!par.hasMore()) return null; char type=par.getChar(); par.skipChar(); + * String value=par.skipChar().getLine(); if (value==null) return null; + * index=par.getPos(); // for DEBUG //System.out.println("DEBUG: + * "+type+"="+value); return new SdpField(type,value); } + */ + + /** + * Returns the first SdpField. The SDP value terminates with the end of the + * String or with the first CR or LF char. + * + * @return the first SdpField, or null if no SdpField is recognized. + */ + public SdpField parseSdpField() { + int begin = index; + while (begin >= 0 && begin < str.length() - 1 + && str.charAt(begin + 1) != '=') + begin = str.indexOf("\n", begin); + if (begin < 0) + return null; + char type = str.charAt(begin); + begin += 2; + int end = str.length(); + int CR = str.indexOf('\r', begin); + if (CR > 0 && CR < end) + end = CR; + int LF = str.indexOf('\n', begin); + if (LF > 0 && LF < end) + end = LF; + String value = str.substring(begin, end).trim(); + if (value == null) + return null; + setPos(end); + goToNextLine(); + // for DEBUG + // System.out.println("DEBUG: "+type+"="+value); + return new SdpField(type, value); + } + + /** + * Returns the first SdpField of type type. The SDP value terminates + * with the end of the String or with the first CR or LF char. + * + * @return the first SdpField, or null if no type SdpField is found. + */ + public SdpField parseSdpField(char type) { + int begin = 0; + if (!str.startsWith(type + "=", index)) { + begin = str.indexOf("\n" + type + "=", index); + // if (begin<0) begin=str.indexOf("\r"+type+"=",index); + if (begin < 0) { // return null if no type SdpField has been + // found + return null; + } + index = begin + 1; + } + return parseSdpField(); + } + + /** + * Returns the first OriginField. + * + * @return the first OriginField, or null if no OriginField is found. + */ + public OriginField parseOriginField() { + SdpField sf = parseSdpField('o'); + if (sf != null) + return new OriginField(sf); + else + return null; + } + + /** + * Returns the first MediaField. + * + * @return the first MediaField, or null if no MediaField is found. + */ + public MediaField parseMediaField() { + SdpField sf = parseSdpField('m'); + if (sf != null) + return new MediaField(sf); + else + return null; + } + + /** + * Returns the first ConnectionField. + * + * @return the first ConnectionField, or null if no ConnectionField is + * found. + */ + public ConnectionField parseConnectionField() { + SdpField sf = parseSdpField('c'); + if (sf != null) + return new ConnectionField(sf); + else + return null; + } + + /** + * Returns the first SessionNameField. + * + * @return the first SessionNameField, or null if no SessionNameField is + * found. + */ + public SessionNameField parseSessionNameField() { + SdpField sf = parseSdpField('s'); + if (sf != null) + return new SessionNameField(sf); + else + return null; + } + + /** + * Returns the first TimeField. + * + * @return the first TimeField, or null if no TimeField is found. + */ + public TimeField parseTimeField() { + SdpField sf = parseSdpField('t'); + if (sf != null) + return new TimeField(sf); + else + return null; + } + + /** + * Returns the first AttributeField. + * + * @return the first AttributeField, or null if no AttributeField is found. + */ + public AttributeField parseAttributeField() { + SdpField sf = parseSdpField('a'); + if (sf != null) + return new AttributeField(sf); + else + return null; + } + + /** + * Returns the first MediaDescriptor. + * + * @return the first MediaDescriptor, or null if no MediaDescriptor is + * found. + */ + public MediaDescriptor parseMediaDescriptor() { + MediaField m = parseMediaField(); + if (m == null) + return null; + int begin = index; + int end = str.indexOf("\nm", begin); + if (end < 0) + end = str.length(); + else + end++; + index = end; + SdpParser par = new SdpParser(str.substring(begin, end)); + ConnectionField c = par.parseConnectionField(); + /* HSC CHANGE BEGINS */ + Vector av = new Vector(); + /* HSC CHANGE ENDS */ + AttributeField a = par.parseAttributeField(); + while (a != null) { + av.addElement(a); + a = par.parseAttributeField(); + } + return new MediaDescriptor(m, c, av); + } + +} diff --git a/src/org/zoolu/sdp/SessionDescriptor.java b/app/src/main/java/org/zoolu/sdp/SessionDescriptor.java similarity index 96% rename from src/org/zoolu/sdp/SessionDescriptor.java rename to app/src/main/java/org/zoolu/sdp/SessionDescriptor.java index f152da6..9f15d39 100644 --- a/src/org/zoolu/sdp/SessionDescriptor.java +++ b/app/src/main/java/org/zoolu/sdp/SessionDescriptor.java @@ -1,526 +1,526 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sdp; - -import java.util.Vector; - -import org.zoolu.net.IpAddress; - -/* HSC CHANGE STARTS */ -// import java.util.Enumeration; -// PersonalJava -// import java.util.HashSet; -// import java.util.Iterator; -// import org.zoolu.tools.HashSet; -// import org.zoolu.tools.Iterator; -/* HSC CHANGE ENDS */ - -/** - * Class SessionDescriptor handles SIP message bodys formatted according to the - * Session Description Protocol (SDP). - *

- * A session description consists of a session-level description (details that - * apply to the whole session and all media streams) and zero or more - * media-level descriptions (details that apply onto to a single media stream). - *

- * The session-level part starts with a `v=' line and continues to the first - * media-level section. The media description starts with an `m=' line and - * continues to the next media description or end of the whole session - * description. In general, session-level values are the default for all media - * unless overridden by an equivalent media-level value. - *

- * In the current implementation, the session-level description consists of the - * v, o, s, c, and t SDP fields (lines). - */ -public class SessionDescriptor { - /** Version filed. */ - SdpField v; - /** Origin filed. */ - OriginField o; - /** Session-name filed. */ - SessionNameField s; - /** Connection filed. */ - ConnectionField c; - /** Time filed. */ - TimeField t; - - /** Vector of session attributes (as Vector of SdpFields). */ - /* HSC CHANGE STARTS */ - Vector av; - /* HSC CHANGE ENDS */ - - /** Vector of MediaDescriptors. */ - /* HSC CHANGE STARTS */ - Vector media; - - /* HSC CHANGE ENDS */ - - /* - * private void init(String owner, String session, String connection, String - * time) { v=new SdpField('v',"0"); o=new SdpField('o',owner); s=new - * SdpField('s',session); c=new SdpField('c',connection); t=new - * SdpField('t',time); media=new HashSet(); } - */ - - /** Inits the SessionDescriptor. */ - private void init(OriginField origin, SessionNameField session, - ConnectionField connection, TimeField time) { - v = new SdpField('v', "0"); - o = origin; - s = session; - c = connection; - t = time; - /* HSC CHANGE STARTS */ - av = new Vector(); - media = new Vector(); - /* HSC CHANGE ENDS */ - } - - /** - * Creates a new SessionDescriptor. - * - * @param sd - * the SessionDescriptor clone - */ - public SessionDescriptor(SessionDescriptor sd) { - init(new OriginField(sd.o), new SessionNameField(sd.s), - new ConnectionField(sd.c), new TimeField(sd.t)); - for (int i = 0; i < sd.media.size(); i++) - media.addElement(new MediaDescriptor((MediaDescriptor) sd.media - .elementAt(i))); - } - - /** - * Creates a new SessionDescriptor specifing o, s, c, and t fields. - * - * @param origin - * the OriginField - * @param session - * the SessionNameField - * @param connection - * the ConnectionField - * @param time - * the TimeField - */ - public SessionDescriptor(OriginField origin, SessionNameField session, - ConnectionField connection, TimeField time) { - init(origin, session, connection, time); - } - - /** - * Creates a new SessionDescriptor specifing o, s, c, and t fields. - * - * @param origin - * the origin value - * @param session - * the session value - * @param connection - * the connection value - * @param time - * the time value - */ - public SessionDescriptor(String origin, String session, String connection, - String time) { - init(new OriginField(origin), new SessionNameField(session), - new ConnectionField(connection), new TimeField(time)); - } - - public void IncrementOLine() - { - String str = o.getSessionVersion(); - Integer intObj2 = Integer.valueOf(str); - intObj2++; - o = new OriginField(o.getUserName(), o.getSessionId(), Integer.toString(intObj2),o.getAddress()); - } - /** - * Creates a new SessionDescriptor. - *

- * with:
- * o=owner
- * s=Session SIP/SDP
- * c=IP4 address
- * t=0 0 - *

- * if address==null, '127.0.0.1' is used
- * if owner==null, 'user@'address is used - * - * @param owner - * the owner - * @param address - * the IPv4 address - */ - public SessionDescriptor(String owner, String address) { - if (address == null) - address = IpAddress.localIpAddress; - if (owner == null) - owner = "user@" + address; - init(new OriginField(owner, "0", "0", address), new SessionNameField( - "Session SIP/SDP"), new ConnectionField("IP4", address), - new TimeField()); - } - - /** - * Creates a default SessionDescriptor. - *

- * o=user@127.0.0.1 s=Session SIP/SDP c=127.0.0.1 t=0 0 - */ - public SessionDescriptor() { - String address = IpAddress.localIpAddress; - String owner = "user@" + address; - init(new OriginField(owner, "0", "0", address), new SessionNameField( - "Session SIP/SDP"), new ConnectionField("IP4", address), - new TimeField()); - } - - /** - * Creates a new SessionDescriptor from String sdp - * - * @param sdp - * the entire SDP - */ - public SessionDescriptor(String sdp) { - SdpParser par = new SdpParser(sdp); - // parse mandatory fields - v = par.parseSdpField('v'); - if (v == null) - v = new SdpField('v', "0"); - o = par.parseOriginField(); - if (o == null) - o = new OriginField("unknown"); - s = par.parseSessionNameField(); - if (s == null) - s = new SessionNameField(); - c = par.parseConnectionField(); - if (c == null) - c = new ConnectionField("IP4", "0.0.0.0"); - t = par.parseTimeField(); - if (t == null) - t = new TimeField(); - while (par.hasMore() - && (!par.startsWith("a=") && !par.startsWith("m="))) { // skip - // unknown - // lines.. - par.goToNextLine(); - } - // parse session attributes - av = new Vector(); - while (par.hasMore() && par.startsWith("a=")) { - AttributeField attribute = par.parseAttributeField(); - av.addElement(attribute); - } - // parse media descriptors - media = new Vector(); - MediaDescriptor md; - while ((md = par.parseMediaDescriptor()) != null) { - addMediaDescriptor(md); - } - } - - /** - * Sets the origin 'o' field. - * - * @param origin - * the OriginField - * @return this SessionDescriptor - */ - public SessionDescriptor setOrigin(OriginField origin) { - o = origin; - return this; - } - - /** Gets the origin 'o' field */ - public OriginField getOrigin() { // System.out.println("DEBUG: inside - // SessionDescriptor.getOwner(): - // sdp=\n"+toString()); - return o; - } - - /** - * Sets the session-name 's' field. - * - * @param session - * the SessionNameField - * @return this SessionDescriptor - */ - public SessionDescriptor setSessionName(SessionNameField session) { - s = session; - return this; - } - - /** Gets the session-name 's' field */ - public SessionNameField getSessionName() { - return s; - } - - /** - * Sets the connection-information 'c' field. - * - * @param connection - * the ConnectionField - * @return this SessionDescriptor - */ - public SessionDescriptor setConnection(ConnectionField connection) { - c = connection; - return this; - } - - /** Gets the connection-information 'c' field */ - public ConnectionField getConnection() { - return c; - } - - /** - * Sets the time 't' field. - * - * @param time - * the TimeField - * @return this SessionDescriptor - */ - public SessionDescriptor setTime(TimeField time) { - t = time; - return this; - } - - /** Gets the time 't' field */ - public TimeField getTime() { - return t; - } - - /** - * Adds a new attribute for a particular media - * - * @param media - * the MediaField - * @param attribute - * an AttributeField - * @return this SessionDescriptor - */ - public SessionDescriptor addMedia(MediaField media, AttributeField attribute) { // printlog("DEBUG: - // media: - // "+media,5); - // printlog("DEBUG: attribute: "+attribute,5); - addMediaDescriptor(new MediaDescriptor(media, null, attribute)); - return this; - } - - /** - * Adds a new media. - * - * @param media - * the MediaField - * @param attributes - * Vector of AttributeField - * @return this SessionDescriptor - */ - public SessionDescriptor addMedia(MediaField media, - Vector attributes) { - // printlog("DEBUG: - // printlog("DEBUG: attribute: "+attributes,5); - addMediaDescriptor(new MediaDescriptor(media, null, attributes)); - return this; - } - - /** - * Adds a new MediaDescriptor - * - * @param media_desc - * a MediaDescriptor - * @return this SessionDescriptor - */ - public SessionDescriptor addMediaDescriptor(MediaDescriptor media_desc) { // printlog("DEBUG: - // media - // desc: - // "+media_desc,5); - media.addElement(media_desc); - return this; - } - - /** - * Adds a Vector of MediaDescriptors - * - * @param media_descs - * Vector if MediaDescriptor - * @return this SessionDescriptor - */ - public SessionDescriptor addMediaDescriptors( - Vector media_descs) { - // media.addAll(media_descs); - for (int i = 0; i < media_descs.size(); i++) - media.addElement(media_descs.elementAt(i)); - return this; - } - - /** Gets all MediaDescriptors */ - public Vector getMediaDescriptors() { - return media; - } - - /** Removes all MediaDescriptors */ - public SessionDescriptor removeMediaDescriptor(String media_type) { - for (int i = media.size() - 1; i >= 0; i--) - if (((MediaDescriptor) media.elementAt(i)).getMedia().getMedia() - .equals(media_type)) - media.removeElementAt(i); - return this; - } - - /** Removes all MediaDescriptors */ - public SessionDescriptor removeMediaDescriptors() { // media.clear(); // not - // supported by J2ME.. - media.setSize(0); - return this; - } - - /** - * Gets the first MediaDescriptor of a particular media. - * - * @param media_type - * the media type - * @return the MediaDescriptor - */ - public MediaDescriptor getMediaDescriptor(String media_type) { - for (int i = 0; i < media.size(); i++) { - MediaDescriptor md = (MediaDescriptor) media.elementAt(i); - if (md.getMedia().getMedia().equals(media_type)) - return md; - } - return null; - } - - /** - * Adds a Vector of session attributes. - * - * @param attribute_fields - * Vector of AttributeFields - * @return this SessionDescriptor - */ - public SessionDescriptor addAttributes( - Vector attribute_fields) { - for (int i = 0; i < attribute_fields.size(); i++) - addAttribute(attribute_fields.elementAt(i)); - return this; - } - - /** - * Adds a new attribute - * - * @param attribute - * the new AttributeField - * @return this MediaDescriptor - */ - public SessionDescriptor addAttribute(AttributeField attribute) { - av.addElement(new AttributeField(attribute)); - return this; - } - - /** Removes all session attributes. */ - public SessionDescriptor removeAttributes() { - av.setSize(0); - return this; - } - - /** - * Gets a Vector of attribute values. - * - * @return a Vector of AttributeField - */ - public Vector getAttributes() { - Vector v = new Vector(av.size()); - for (int i = 0; i < av.size(); i++) - v.addElement((AttributeField) av.elementAt(i)); - return v; - } - - /** - * Whether it has a particular attribute - * - * @param a_name - * the attribute name - * @return true if found, otherwise returns null - */ - public boolean hasAttribute(String attribute_name) { - for (int i = 0; i < av.size(); i++) { - if (((AttributeField) av.elementAt(i)).getAttributeName().equals( - attribute_name)) - return true; - } - return false; - } - - /** - * Gets the first AttributeField of a particular attribute name. - * - * @param attribute_name - * the attribute name - * @return the AttributeField, or null if not found - */ - public AttributeField getAttribute(String attribute_name) { - for (int i = 0; i < av.size(); i++) { - AttributeField af = (AttributeField) av.elementAt(i); - if (af.getAttributeName().equals(attribute_name)) - return af; - } - return null; - } - - /** - * Gets a Vector of attribute values of a particular attribute name. - * - * @param a_name - * the attribute name - * @return a Vector of AttributeField - */ - public Vector getAttributes(String attribute_name) { - Vector v = new Vector(av.size()); - for (int i = 0; i < av.size(); i++) { - AttributeField a = (AttributeField) av.elementAt(i); - if (a.getAttributeName().equals(attribute_name)) - v.addElement(a); - } - return v; - } - - /** Gets a String rapresentation */ - public String toString() { - StringBuffer sb = new StringBuffer(); - if (v != null) - sb.append(v.toString()); - if (o != null) - sb.append(o.toString()); - if (s != null) - sb.append(s.toString()); - if (c != null) - sb.append(c.toString()); - if (t != null) - sb.append(t.toString()); - for (int i = 0; i < av.size(); i++) - sb.append(((AttributeField) av.elementAt(i)).toString()); - for (int i = 0; i < media.size(); i++) - sb.append(((MediaDescriptor) media.elementAt(i)).toString()); - return sb.toString(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sdp; + +import java.util.Vector; + +import org.zoolu.net.IpAddress; + +/* HSC CHANGE STARTS */ +// import java.util.Enumeration; +// PersonalJava +// import java.util.HashSet; +// import java.util.Iterator; +// import org.zoolu.tools.HashSet; +// import org.zoolu.tools.Iterator; +/* HSC CHANGE ENDS */ + +/** + * Class SessionDescriptor handles SIP message bodys formatted according to the + * Session Description Protocol (SDP). + *

+ * A session description consists of a session-level description (details that + * apply to the whole session and all media streams) and zero or more + * media-level descriptions (details that apply onto to a single media stream). + *

+ * The session-level part starts with a `v=' line and continues to the first + * media-level section. The media description starts with an `m=' line and + * continues to the next media description or end of the whole session + * description. In general, session-level values are the default for all media + * unless overridden by an equivalent media-level value. + *

+ * In the current implementation, the session-level description consists of the + * v, o, s, c, and t SDP fields (lines). + */ +public class SessionDescriptor { + /** Version filed. */ + SdpField v; + /** Origin filed. */ + OriginField o; + /** Session-name filed. */ + SessionNameField s; + /** Connection filed. */ + ConnectionField c; + /** Time filed. */ + TimeField t; + + /** Vector of session attributes (as Vector of SdpFields). */ + /* HSC CHANGE STARTS */ + Vector av; + /* HSC CHANGE ENDS */ + + /** Vector of MediaDescriptors. */ + /* HSC CHANGE STARTS */ + Vector media; + + /* HSC CHANGE ENDS */ + + /* + * private void init(String owner, String session, String connection, String + * time) { v=new SdpField('v',"0"); o=new SdpField('o',owner); s=new + * SdpField('s',session); c=new SdpField('c',connection); t=new + * SdpField('t',time); media=new HashSet(); } + */ + + /** Inits the SessionDescriptor. */ + private void init(OriginField origin, SessionNameField session, + ConnectionField connection, TimeField time) { + v = new SdpField('v', "0"); + o = origin; + s = session; + c = connection; + t = time; + /* HSC CHANGE STARTS */ + av = new Vector(); + media = new Vector(); + /* HSC CHANGE ENDS */ + } + + /** + * Creates a new SessionDescriptor. + * + * @param sd + * the SessionDescriptor clone + */ + public SessionDescriptor(SessionDescriptor sd) { + init(new OriginField(sd.o), new SessionNameField(sd.s), + new ConnectionField(sd.c), new TimeField(sd.t)); + for (int i = 0; i < sd.media.size(); i++) + media.addElement(new MediaDescriptor((MediaDescriptor) sd.media + .elementAt(i))); + } + + /** + * Creates a new SessionDescriptor specifing o, s, c, and t fields. + * + * @param origin + * the OriginField + * @param session + * the SessionNameField + * @param connection + * the ConnectionField + * @param time + * the TimeField + */ + public SessionDescriptor(OriginField origin, SessionNameField session, + ConnectionField connection, TimeField time) { + init(origin, session, connection, time); + } + + /** + * Creates a new SessionDescriptor specifing o, s, c, and t fields. + * + * @param origin + * the origin value + * @param session + * the session value + * @param connection + * the connection value + * @param time + * the time value + */ + public SessionDescriptor(String origin, String session, String connection, + String time) { + init(new OriginField(origin), new SessionNameField(session), + new ConnectionField(connection), new TimeField(time)); + } + + public void IncrementOLine() + { + String str = o.getSessionVersion(); + Integer intObj2 = Integer.valueOf(str); + intObj2++; + o = new OriginField(o.getUserName(), o.getSessionId(), Integer.toString(intObj2),o.getAddress()); + } + /** + * Creates a new SessionDescriptor. + *

+ * with:
+ * o=owner
+ * s=Session SIP/SDP
+ * c=IP4 address
+ * t=0 0 + *

+ * if address==null, '127.0.0.1' is used
+ * if owner==null, 'user@'address is used + * + * @param owner + * the owner + * @param address + * the IPv4 address + */ + public SessionDescriptor(String owner, String address) { + if (address == null) + address = IpAddress.localIpAddress; + if (owner == null) + owner = "user@" + address; + init(new OriginField(owner, "0", "0", address), new SessionNameField( + "Session SIP/SDP"), new ConnectionField("IP4", address), + new TimeField()); + } + + /** + * Creates a default SessionDescriptor. + *

+ * o=user@127.0.0.1 s=Session SIP/SDP c=127.0.0.1 t=0 0 + */ + public SessionDescriptor() { + String address = IpAddress.localIpAddress; + String owner = "user@" + address; + init(new OriginField(owner, "0", "0", address), new SessionNameField( + "Session SIP/SDP"), new ConnectionField("IP4", address), + new TimeField()); + } + + /** + * Creates a new SessionDescriptor from String sdp + * + * @param sdp + * the entire SDP + */ + public SessionDescriptor(String sdp) { + SdpParser par = new SdpParser(sdp); + // parse mandatory fields + v = par.parseSdpField('v'); + if (v == null) + v = new SdpField('v', "0"); + o = par.parseOriginField(); + if (o == null) + o = new OriginField("unknown"); + s = par.parseSessionNameField(); + if (s == null) + s = new SessionNameField(); + c = par.parseConnectionField(); + if (c == null) + c = new ConnectionField("IP4", "0.0.0.0"); + t = par.parseTimeField(); + if (t == null) + t = new TimeField(); + while (par.hasMore() + && (!par.startsWith("a=") && !par.startsWith("m="))) { // skip + // unknown + // lines.. + par.goToNextLine(); + } + // parse session attributes + av = new Vector(); + while (par.hasMore() && par.startsWith("a=")) { + AttributeField attribute = par.parseAttributeField(); + av.addElement(attribute); + } + // parse media descriptors + media = new Vector(); + MediaDescriptor md; + while ((md = par.parseMediaDescriptor()) != null) { + addMediaDescriptor(md); + } + } + + /** + * Sets the origin 'o' field. + * + * @param origin + * the OriginField + * @return this SessionDescriptor + */ + public SessionDescriptor setOrigin(OriginField origin) { + o = origin; + return this; + } + + /** Gets the origin 'o' field */ + public OriginField getOrigin() { // System.out.println("DEBUG: inside + // SessionDescriptor.getOwner(): + // sdp=\n"+toString()); + return o; + } + + /** + * Sets the session-name 's' field. + * + * @param session + * the SessionNameField + * @return this SessionDescriptor + */ + public SessionDescriptor setSessionName(SessionNameField session) { + s = session; + return this; + } + + /** Gets the session-name 's' field */ + public SessionNameField getSessionName() { + return s; + } + + /** + * Sets the connection-information 'c' field. + * + * @param connection + * the ConnectionField + * @return this SessionDescriptor + */ + public SessionDescriptor setConnection(ConnectionField connection) { + c = connection; + return this; + } + + /** Gets the connection-information 'c' field */ + public ConnectionField getConnection() { + return c; + } + + /** + * Sets the time 't' field. + * + * @param time + * the TimeField + * @return this SessionDescriptor + */ + public SessionDescriptor setTime(TimeField time) { + t = time; + return this; + } + + /** Gets the time 't' field */ + public TimeField getTime() { + return t; + } + + /** + * Adds a new attribute for a particular media + * + * @param media + * the MediaField + * @param attribute + * an AttributeField + * @return this SessionDescriptor + */ + public SessionDescriptor addMedia(MediaField media, AttributeField attribute) { // printlog("DEBUG: + // media: + // "+media,5); + // printlog("DEBUG: attribute: "+attribute,5); + addMediaDescriptor(new MediaDescriptor(media, null, attribute)); + return this; + } + + /** + * Adds a new media. + * + * @param media + * the MediaField + * @param attributes + * Vector of AttributeField + * @return this SessionDescriptor + */ + public SessionDescriptor addMedia(MediaField media, + Vector attributes) { + // printlog("DEBUG: + // printlog("DEBUG: attribute: "+attributes,5); + addMediaDescriptor(new MediaDescriptor(media, null, attributes)); + return this; + } + + /** + * Adds a new MediaDescriptor + * + * @param media_desc + * a MediaDescriptor + * @return this SessionDescriptor + */ + public SessionDescriptor addMediaDescriptor(MediaDescriptor media_desc) { // printlog("DEBUG: + // media + // desc: + // "+media_desc,5); + media.addElement(media_desc); + return this; + } + + /** + * Adds a Vector of MediaDescriptors + * + * @param media_descs + * Vector if MediaDescriptor + * @return this SessionDescriptor + */ + public SessionDescriptor addMediaDescriptors( + Vector media_descs) { + // media.addAll(media_descs); + for (int i = 0; i < media_descs.size(); i++) + media.addElement(media_descs.elementAt(i)); + return this; + } + + /** Gets all MediaDescriptors */ + public Vector getMediaDescriptors() { + return media; + } + + /** Removes all MediaDescriptors */ + public SessionDescriptor removeMediaDescriptor(String media_type) { + for (int i = media.size() - 1; i >= 0; i--) + if (((MediaDescriptor) media.elementAt(i)).getMedia().getMedia() + .equals(media_type)) + media.removeElementAt(i); + return this; + } + + /** Removes all MediaDescriptors */ + public SessionDescriptor removeMediaDescriptors() { // media.clear(); // not + // supported by J2ME.. + media.setSize(0); + return this; + } + + /** + * Gets the first MediaDescriptor of a particular media. + * + * @param media_type + * the media type + * @return the MediaDescriptor + */ + public MediaDescriptor getMediaDescriptor(String media_type) { + for (int i = 0; i < media.size(); i++) { + MediaDescriptor md = (MediaDescriptor) media.elementAt(i); + if (md.getMedia().getMedia().equals(media_type)) + return md; + } + return null; + } + + /** + * Adds a Vector of session attributes. + * + * @param attribute_fields + * Vector of AttributeFields + * @return this SessionDescriptor + */ + public SessionDescriptor addAttributes( + Vector attribute_fields) { + for (int i = 0; i < attribute_fields.size(); i++) + addAttribute(attribute_fields.elementAt(i)); + return this; + } + + /** + * Adds a new attribute + * + * @param attribute + * the new AttributeField + * @return this MediaDescriptor + */ + public SessionDescriptor addAttribute(AttributeField attribute) { + av.addElement(new AttributeField(attribute)); + return this; + } + + /** Removes all session attributes. */ + public SessionDescriptor removeAttributes() { + av.setSize(0); + return this; + } + + /** + * Gets a Vector of attribute values. + * + * @return a Vector of AttributeField + */ + public Vector getAttributes() { + Vector v = new Vector(av.size()); + for (int i = 0; i < av.size(); i++) + v.addElement((AttributeField) av.elementAt(i)); + return v; + } + + /** + * Whether it has a particular attribute + * + * @param a_name + * the attribute name + * @return true if found, otherwise returns null + */ + public boolean hasAttribute(String attribute_name) { + for (int i = 0; i < av.size(); i++) { + if (((AttributeField) av.elementAt(i)).getAttributeName().equals( + attribute_name)) + return true; + } + return false; + } + + /** + * Gets the first AttributeField of a particular attribute name. + * + * @param attribute_name + * the attribute name + * @return the AttributeField, or null if not found + */ + public AttributeField getAttribute(String attribute_name) { + for (int i = 0; i < av.size(); i++) { + AttributeField af = (AttributeField) av.elementAt(i); + if (af.getAttributeName().equals(attribute_name)) + return af; + } + return null; + } + + /** + * Gets a Vector of attribute values of a particular attribute name. + * + * @param a_name + * the attribute name + * @return a Vector of AttributeField + */ + public Vector getAttributes(String attribute_name) { + Vector v = new Vector(av.size()); + for (int i = 0; i < av.size(); i++) { + AttributeField a = (AttributeField) av.elementAt(i); + if (a.getAttributeName().equals(attribute_name)) + v.addElement(a); + } + return v; + } + + /** Gets a String rapresentation */ + public String toString() { + StringBuffer sb = new StringBuffer(); + if (v != null) + sb.append(v.toString()); + if (o != null) + sb.append(o.toString()); + if (s != null) + sb.append(s.toString()); + if (c != null) + sb.append(c.toString()); + if (t != null) + sb.append(t.toString()); + for (int i = 0; i < av.size(); i++) + sb.append(((AttributeField) av.elementAt(i)).toString()); + for (int i = 0; i < media.size(); i++) + sb.append(((MediaDescriptor) media.elementAt(i)).toString()); + return sb.toString(); + } + +} diff --git a/src/org/zoolu/sdp/SessionNameField.java b/app/src/main/java/org/zoolu/sdp/SessionNameField.java similarity index 96% rename from src/org/zoolu/sdp/SessionNameField.java rename to app/src/main/java/org/zoolu/sdp/SessionNameField.java index 4c7ac69..cadecc2 100644 --- a/src/org/zoolu/sdp/SessionNameField.java +++ b/app/src/main/java/org/zoolu/sdp/SessionNameField.java @@ -1,63 +1,63 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sdp; - -/* HSC CHANGE STARTS */ -// import org.zoolu.tools.Parser; -/* HSC CHANGES END */ - -/** - * SDP session name field. - *

- *

- * - *
- *    session-name-field = "s=" text CRLF
- * 
- * - *
- */ -public class SessionNameField extends SdpField { - /** Creates a new SessionNameField. */ - public SessionNameField(String session_name) { - super('s', session_name); - } - - /** Creates a new void SessionNameField. */ - public SessionNameField() { - super('s', " "); - } - - /** Creates a new SessionNameField. */ - public SessionNameField(SdpField sf) { - super(sf); - } - - /** Gets the session name. */ - public String getSession() { - return value; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sdp; + +/* HSC CHANGE STARTS */ +// import org.zoolu.tools.Parser; +/* HSC CHANGES END */ + +/** + * SDP session name field. + *

+ *

+ * + *
+ *    session-name-field = "s=" text CRLF
+ * 
+ * + *
+ */ +public class SessionNameField extends SdpField { + /** Creates a new SessionNameField. */ + public SessionNameField(String session_name) { + super('s', session_name); + } + + /** Creates a new void SessionNameField. */ + public SessionNameField() { + super('s', " "); + } + + /** Creates a new SessionNameField. */ + public SessionNameField(SdpField sf) { + super(sf); + } + + /** Gets the session name. */ + public String getSession() { + return value; + } + +} diff --git a/src/org/zoolu/sdp/TimeField.java b/app/src/main/java/org/zoolu/sdp/TimeField.java similarity index 96% rename from src/org/zoolu/sdp/TimeField.java rename to app/src/main/java/org/zoolu/sdp/TimeField.java index 0016df2..80d58b8 100644 --- a/src/org/zoolu/sdp/TimeField.java +++ b/app/src/main/java/org/zoolu/sdp/TimeField.java @@ -1,70 +1,70 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sdp; - -import org.zoolu.tools.Parser; - -/** - * SDP attribute field. - *

- *

- * - *
- *    time-fields = 1*( "t=" start-time SP stop-time *(CRLF repeat-fields) CRLF) [zone-adjustments CRLF]
- * 
- * - *
- */ -public class TimeField extends SdpField { - /** Creates a new TimeField. */ - public TimeField(String time_field) { - super('t', time_field); - } - - /** Creates a new TimeField. */ - public TimeField(String start, String stop) { - super('t', start + " " + stop); - } - - /** Creates a new void TimeField. */ - public TimeField() { - super('t', "0 0"); - } - - /** Creates a new TimeField. */ - public TimeField(SdpField sf) { - super(sf); - } - - /** Gets the start time. */ - public String getStartTime() { - return (new Parser(value)).getString(); - } - - /** Gets the stop time. */ - public String getStopTime() { - return (new Parser(value)).skipString().getString(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sdp; + +import org.zoolu.tools.Parser; + +/** + * SDP attribute field. + *

+ *

+ * + *
+ *    time-fields = 1*( "t=" start-time SP stop-time *(CRLF repeat-fields) CRLF) [zone-adjustments CRLF]
+ * 
+ * + *
+ */ +public class TimeField extends SdpField { + /** Creates a new TimeField. */ + public TimeField(String time_field) { + super('t', time_field); + } + + /** Creates a new TimeField. */ + public TimeField(String start, String stop) { + super('t', start + " " + stop); + } + + /** Creates a new void TimeField. */ + public TimeField() { + super('t', "0 0"); + } + + /** Creates a new TimeField. */ + public TimeField(SdpField sf) { + super(sf); + } + + /** Gets the start time. */ + public String getStartTime() { + return (new Parser(value)).getString(); + } + + /** Gets the stop time. */ + public String getStopTime() { + return (new Parser(value)).skipString().getString(); + } + +} diff --git a/src/org/zoolu/sip/address/NameAddress.java b/app/src/main/java/org/zoolu/sip/address/NameAddress.java similarity index 96% rename from src/org/zoolu/sip/address/NameAddress.java rename to app/src/main/java/org/zoolu/sip/address/NameAddress.java index 3da080d..0fd452d 100644 --- a/src/org/zoolu/sip/address/NameAddress.java +++ b/app/src/main/java/org/zoolu/sip/address/NameAddress.java @@ -1,134 +1,134 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.address; - -import org.zoolu.sip.provider.SipParser; - -/** - * Class NameAddress is used to rapresent any valid SIP Name Address. It - * contains a SIP URI and optionally a display name.
- * A SIP Name Address is a string of the form of:
- *
- * - *
- * &nbsp&nbsp [ display-name ] address
- * 
- * &nbsp&nbsp where address can be a valid SIP URL - *
- * - *
- */ -public class NameAddress { - String name; - SipURL url; - - public NameAddress(String displayname, SipURL sipurl) { - name = displayname; - url = sipurl; - } - - public NameAddress(SipURL sipurl) { - name = null; - url = sipurl; - } - - public NameAddress(NameAddress name_address) { - name = name_address.getDisplayName(); - url = name_address.getAddress(); - } - - public NameAddress(String naddr) { - SipParser par = new SipParser(naddr); - NameAddress na = par.getNameAddress(); - // DEBUG - // if (na==null) - // { System.out.println("DEBUG: NameAddress: - // par:\r\n"+par.getWholeString()); - // System.exit(0); - // } - name = na.name; - url = na.url; - } - - /** Creates and returns a copy of NameAddress */ - public Object clone() { - return new NameAddress(this); - } - - /** Indicates whether some other Object is "equal to" this NameAddress */ - public boolean equals(Object obj) { - NameAddress naddr = (NameAddress) obj; - return url.equals(naddr.getAddress()); - } - - /** Gets address of NameAddress */ - public SipURL getAddress() { - return url; - } - - /** - * Gets display name of NameAddress (Returns null id display name does not - * exist) - */ - public String getDisplayName() { - return name; - } - - /** Gets boolean value to indicate if NameAddress has display name */ - public boolean hasDisplayName() { - return name != null; - } - - /** Removes display name from NameAddress (if it exists) */ - public void removeDisplayName() { - name = null; - } - - /** Sets address of NameAddress */ - public void setAddress(SipURL address) { - url = address; - } - - /** Sets display name of Header */ - public void setDisplayName(String displayName) { - name = displayName; - } - - /** Whether two NameAddresses are equals */ - public boolean equals(NameAddress naddr) { - return (name == naddr.name && url == naddr.url); - } - - /** Gets string representation of NameAddress */ - public String toString() { - String str; - if (hasDisplayName()) - str = "\"" + name + "\" <" + url + ">"; - else - str = "<" + url + ">"; - - return str; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.address; + +import org.zoolu.sip.provider.SipParser; + +/** + * Class NameAddress is used to rapresent any valid SIP Name Address. It + * contains a SIP URI and optionally a display name.
+ * A SIP Name Address is a string of the form of:
+ *
+ * + *
+ * &nbsp&nbsp [ display-name ] address
+ * 
+ * &nbsp&nbsp where address can be a valid SIP URL + *
+ * + *
+ */ +public class NameAddress { + String name; + SipURL url; + + public NameAddress(String displayname, SipURL sipurl) { + name = displayname; + url = sipurl; + } + + public NameAddress(SipURL sipurl) { + name = null; + url = sipurl; + } + + public NameAddress(NameAddress name_address) { + name = name_address.getDisplayName(); + url = name_address.getAddress(); + } + + public NameAddress(String naddr) { + SipParser par = new SipParser(naddr); + NameAddress na = par.getNameAddress(); + // DEBUG + // if (na==null) + // { System.out.println("DEBUG: NameAddress: + // par:\r\n"+par.getWholeString()); + // System.exit(0); + // } + name = na.name; + url = na.url; + } + + /** Creates and returns a copy of NameAddress */ + public Object clone() { + return new NameAddress(this); + } + + /** Indicates whether some other Object is "equal to" this NameAddress */ + public boolean equals(Object obj) { + NameAddress naddr = (NameAddress) obj; + return url.equals(naddr.getAddress()); + } + + /** Gets address of NameAddress */ + public SipURL getAddress() { + return url; + } + + /** + * Gets display name of NameAddress (Returns null id display name does not + * exist) + */ + public String getDisplayName() { + return name; + } + + /** Gets boolean value to indicate if NameAddress has display name */ + public boolean hasDisplayName() { + return name != null; + } + + /** Removes display name from NameAddress (if it exists) */ + public void removeDisplayName() { + name = null; + } + + /** Sets address of NameAddress */ + public void setAddress(SipURL address) { + url = address; + } + + /** Sets display name of Header */ + public void setDisplayName(String displayName) { + name = displayName; + } + + /** Whether two NameAddresses are equals */ + public boolean equals(NameAddress naddr) { + return (name == naddr.name && url == naddr.url); + } + + /** Gets string representation of NameAddress */ + public String toString() { + String str; + if (hasDisplayName()) + str = "\"" + name + "\" <" + url + ">"; + else + str = "<" + url + ">"; + + return str; + } + +} diff --git a/src/org/zoolu/sip/address/SipURL.java b/app/src/main/java/org/zoolu/sip/address/SipURL.java similarity index 96% rename from src/org/zoolu/sip/address/SipURL.java rename to app/src/main/java/org/zoolu/sip/address/SipURL.java index 85ae548..6a8aa82 100644 --- a/src/org/zoolu/sip/address/SipURL.java +++ b/app/src/main/java/org/zoolu/sip/address/SipURL.java @@ -1,326 +1,326 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.address; - -import org.zoolu.sip.provider.SipParser; -import org.zoolu.tools.Parser; -import java.util.Vector; - -/** - *

- * Class SipURL implements SIP URLs. - *

- * A SIP URL is a string of the form of:
- *

- * - *
- * &nbsp&nbsp sip:[user@]hostname[:port][;parameters]
- * 
- * - *
- *

- * If port number is ommitted, -1 is returned - */ -public class SipURL { - protected String url; - - protected static final String transport_param = "transport"; - protected static final String maddr_param = "maddr"; - protected static final String ttl_param = "ttl"; - protected static final String lr_param = "lr"; - - /** - * Creates a new SipURL based on a hostname or on a sip url as - * sip:[user@]hostname[:port][;param1=value1].. - */ - public SipURL(String sipurl) { - if (sipurl.startsWith("sip:")) - url = new String(sipurl); - else - url = "sip:" + sipurl; - } - - /** Creates a new SipURL */ - public SipURL(String username, String hostname) { - init(username, hostname, -1); - } - - /** Creates a new SipURL */ - public SipURL(String hostname, int portnumber) { - init(null, hostname, portnumber); - } - - /** Creates a new SipURL */ - public SipURL(String username, String hostname, int portnumber) { - init(username, hostname, portnumber); - } - - /** Inits the SipURL */ - private void init(String username, String hostname, int portnumber) { - StringBuffer sb = new StringBuffer("sip:"); - if (username != null){ - sb.append(username); - if(username.indexOf('@') < 0){ - sb.append('@'); - sb.append(hostname); - } - } else - sb.append(hostname); - - if (portnumber > 0) - if(username == null || username.indexOf(':') < 0) - sb.append(":" + portnumber); - - url = sb.toString(); - } - - /** Creates and returns a copy of the URL */ - public Object clone() { - return new SipURL(url); - } - - /** Indicates whether some other Object is "equal to" this URL */ - public boolean equals(Object obj) { - SipURL newurl = (SipURL) obj; - return url.toString().equals(newurl.toString()); - } - - /** Gets user name of SipURL (Returns null if user name does not exist) */ - public String getUserName() { - int begin = 4; // skip "sip:" - int end = url.indexOf('@', begin); - if (end < 0) - return null; - else - return url.substring(begin, end); - } - - /** Gets host of SipURL */ - public String getHost() { - char[] host_terminators = { ':', ';', '?' }; - Parser par = new Parser(url); - int begin = par.indexOf('@'); // skip "sip:user@" - if (begin < 0) - begin = 4; // skip "sip:" - else - begin++; // skip "@" - par.setPos(begin); - int end = par.indexOf(host_terminators); - if (end < 0) - return url.substring(begin); - else - return url.substring(begin, end); - } - - /** Gets port of SipURL; returns -1 if port is not specidfied */ - public int getPort() { - char[] port_terminators = { ';', '?' }; - Parser par = new Parser(url, 4); // skip "sip:" - int begin = par.indexOf(':'); - if (begin < 0) - return -1; - else { - begin++; - par.setPos(begin); - int end = par.indexOf(port_terminators); - if (end < 0) - return Integer.parseInt(url.substring(begin)); - else - return Integer.parseInt(url.substring(begin, end)); - } - } - - /** Gets boolean value to indicate if SipURL has user name */ - public boolean hasUserName() { - return getUserName() != null; - } - - /** Gets boolean value to indicate if SipURL has port */ - public boolean hasPort() { - return getPort() >= 0; - } - - /** Whether two SipURLs are equals */ - public boolean equals(SipURL sip_url) { - return (url == sip_url.url); - } - - /** Gets string representation of URL */ - public String toString() { - return url; - } - - /** - * Gets the value of specified parameter. - * - * @return null if parameter does not exist. - */ - public String getParameter(String name) { - SipParser par = new SipParser(url); - return ((SipParser) par.goTo(';').skipChar()).getParameter(name); - } - - /** - * Gets a String Vector of parameter names. - * - * @return null if no parameter is present - */ - /* HSC CHANGES START */ - public Vector getParameters() { - SipParser par = new SipParser(url); - Vector result = ((SipParser) par.goTo(';').skipChar()) - .getParameters(); - /* HSC CHANGES END */ - return result; - } - - /** Whether there is the specified parameter */ - public boolean hasParameter(String name) { - SipParser par = new SipParser(url); - return ((SipParser) par.goTo(';').skipChar()).hasParameter(name); - } - - /** Whether there are any parameters */ - public boolean hasParameters() { - if (url != null && url.indexOf(';') >= 0) - return true; - else - return false; - } - - /** Adds a new parameter without a value */ - public void addParameter(String name) { - url = url + ";" + name; - } - - /** Adds a new parameter with value */ - public void addParameter(String name, String value) { - if (value != null) - url = url + ";" + name + "=" + value; - else - url = url + ";" + name; - } - - /** Removes all parameters (if any) */ - public void removeParameters() { - int index = url.indexOf(';'); - if (index >= 0) - url = url.substring(0, index); - } - - /** Removes specified parameter (if present) */ - public void removeParameter(String name) { - int index = url.indexOf(';'); - if (index < 0) - return; - Parser par = new Parser(url, index); - while (par.hasMore()) { - int begin_param = par.getPos(); - par.skipChar(); - if (par.getWord(SipParser.param_separators).equals(name)) { - String top = url.substring(0, begin_param); - par.goToSkippingQuoted(';'); - String bottom = ""; - if (par.hasMore()) - bottom = url.substring(par.getPos()); - url = top.concat(bottom); - return; - } - par.goTo(';'); - } - } - - /** - * Gets the value of transport parameter. - * - * @return null if no transport parameter is present. - */ - public String getTransport() { - return getParameter(transport_param); - } - - /** Whether transport parameter is present */ - public boolean hasTransport() { - return hasParameter(transport_param); - } - - /** Adds transport parameter */ - public void addTransport(String proto) { - addParameter(transport_param, proto.toLowerCase()); - } - - /** - * Gets the value of maddr parameter. - * - * @return null if no maddr parameter is present. - */ - public String getMaddr() { - return getParameter(maddr_param); - } - - /** Whether maddr parameter is present */ - public boolean hasMaddr() { - return hasParameter(maddr_param); - } - - /** Adds maddr parameter */ - public void addMaddr(String maddr) { - addParameter(maddr_param, maddr); - } - - /** - * Gets the value of ttl parameter. - * - * @return 1 if no ttl parameter is present. - */ - public int getTtl() { - try { - return Integer.parseInt(getParameter(ttl_param)); - } catch (Exception e) { - return 1; - } - } - - /** Whether ttl parameter is present */ - public boolean hasTtl() { - return hasParameter(ttl_param); - } - - /** Adds ttl parameter */ - public void addTtl(int ttl) { - addParameter(ttl_param, Integer.toString(ttl)); - } - - /** Whether lr (loose-route) parameter is present */ - public boolean hasLr() { - return hasParameter(lr_param); - } - - /** Adds lr parameter */ - public void addLr() { - addParameter(lr_param); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.address; + +import org.zoolu.sip.provider.SipParser; +import org.zoolu.tools.Parser; +import java.util.Vector; + +/** + *

+ * Class SipURL implements SIP URLs. + *

+ * A SIP URL is a string of the form of:
+ *

+ * + *
+ * &nbsp&nbsp sip:[user@]hostname[:port][;parameters]
+ * 
+ * + *
+ *

+ * If port number is ommitted, -1 is returned + */ +public class SipURL { + protected String url; + + protected static final String transport_param = "transport"; + protected static final String maddr_param = "maddr"; + protected static final String ttl_param = "ttl"; + protected static final String lr_param = "lr"; + + /** + * Creates a new SipURL based on a hostname or on a sip url as + * sip:[user@]hostname[:port][;param1=value1].. + */ + public SipURL(String sipurl) { + if (sipurl.startsWith("sip:")) + url = new String(sipurl); + else + url = "sip:" + sipurl; + } + + /** Creates a new SipURL */ + public SipURL(String username, String hostname) { + init(username, hostname, -1); + } + + /** Creates a new SipURL */ + public SipURL(String hostname, int portnumber) { + init(null, hostname, portnumber); + } + + /** Creates a new SipURL */ + public SipURL(String username, String hostname, int portnumber) { + init(username, hostname, portnumber); + } + + /** Inits the SipURL */ + private void init(String username, String hostname, int portnumber) { + StringBuffer sb = new StringBuffer("sip:"); + if (username != null){ + sb.append(username); + if(username.indexOf('@') < 0){ + sb.append('@'); + sb.append(hostname); + } + } else + sb.append(hostname); + + if (portnumber > 0) + if(username == null || username.indexOf(':') < 0) + sb.append(":" + portnumber); + + url = sb.toString(); + } + + /** Creates and returns a copy of the URL */ + public Object clone() { + return new SipURL(url); + } + + /** Indicates whether some other Object is "equal to" this URL */ + public boolean equals(Object obj) { + SipURL newurl = (SipURL) obj; + return url.toString().equals(newurl.toString()); + } + + /** Gets user name of SipURL (Returns null if user name does not exist) */ + public String getUserName() { + int begin = 4; // skip "sip:" + int end = url.indexOf('@', begin); + if (end < 0) + return null; + else + return url.substring(begin, end); + } + + /** Gets host of SipURL */ + public String getHost() { + char[] host_terminators = { ':', ';', '?' }; + Parser par = new Parser(url); + int begin = par.indexOf('@'); // skip "sip:user@" + if (begin < 0) + begin = 4; // skip "sip:" + else + begin++; // skip "@" + par.setPos(begin); + int end = par.indexOf(host_terminators); + if (end < 0) + return url.substring(begin); + else + return url.substring(begin, end); + } + + /** Gets port of SipURL; returns -1 if port is not specidfied */ + public int getPort() { + char[] port_terminators = { ';', '?' }; + Parser par = new Parser(url, 4); // skip "sip:" + int begin = par.indexOf(':'); + if (begin < 0) + return -1; + else { + begin++; + par.setPos(begin); + int end = par.indexOf(port_terminators); + if (end < 0) + return Integer.parseInt(url.substring(begin)); + else + return Integer.parseInt(url.substring(begin, end)); + } + } + + /** Gets boolean value to indicate if SipURL has user name */ + public boolean hasUserName() { + return getUserName() != null; + } + + /** Gets boolean value to indicate if SipURL has port */ + public boolean hasPort() { + return getPort() >= 0; + } + + /** Whether two SipURLs are equals */ + public boolean equals(SipURL sip_url) { + return (url == sip_url.url); + } + + /** Gets string representation of URL */ + public String toString() { + return url; + } + + /** + * Gets the value of specified parameter. + * + * @return null if parameter does not exist. + */ + public String getParameter(String name) { + SipParser par = new SipParser(url); + return ((SipParser) par.goTo(';').skipChar()).getParameter(name); + } + + /** + * Gets a String Vector of parameter names. + * + * @return null if no parameter is present + */ + /* HSC CHANGES START */ + public Vector getParameters() { + SipParser par = new SipParser(url); + Vector result = ((SipParser) par.goTo(';').skipChar()) + .getParameters(); + /* HSC CHANGES END */ + return result; + } + + /** Whether there is the specified parameter */ + public boolean hasParameter(String name) { + SipParser par = new SipParser(url); + return ((SipParser) par.goTo(';').skipChar()).hasParameter(name); + } + + /** Whether there are any parameters */ + public boolean hasParameters() { + if (url != null && url.indexOf(';') >= 0) + return true; + else + return false; + } + + /** Adds a new parameter without a value */ + public void addParameter(String name) { + url = url + ";" + name; + } + + /** Adds a new parameter with value */ + public void addParameter(String name, String value) { + if (value != null) + url = url + ";" + name + "=" + value; + else + url = url + ";" + name; + } + + /** Removes all parameters (if any) */ + public void removeParameters() { + int index = url.indexOf(';'); + if (index >= 0) + url = url.substring(0, index); + } + + /** Removes specified parameter (if present) */ + public void removeParameter(String name) { + int index = url.indexOf(';'); + if (index < 0) + return; + Parser par = new Parser(url, index); + while (par.hasMore()) { + int begin_param = par.getPos(); + par.skipChar(); + if (par.getWord(SipParser.param_separators).equals(name)) { + String top = url.substring(0, begin_param); + par.goToSkippingQuoted(';'); + String bottom = ""; + if (par.hasMore()) + bottom = url.substring(par.getPos()); + url = top.concat(bottom); + return; + } + par.goTo(';'); + } + } + + /** + * Gets the value of transport parameter. + * + * @return null if no transport parameter is present. + */ + public String getTransport() { + return getParameter(transport_param); + } + + /** Whether transport parameter is present */ + public boolean hasTransport() { + return hasParameter(transport_param); + } + + /** Adds transport parameter */ + public void addTransport(String proto) { + addParameter(transport_param, proto.toLowerCase()); + } + + /** + * Gets the value of maddr parameter. + * + * @return null if no maddr parameter is present. + */ + public String getMaddr() { + return getParameter(maddr_param); + } + + /** Whether maddr parameter is present */ + public boolean hasMaddr() { + return hasParameter(maddr_param); + } + + /** Adds maddr parameter */ + public void addMaddr(String maddr) { + addParameter(maddr_param, maddr); + } + + /** + * Gets the value of ttl parameter. + * + * @return 1 if no ttl parameter is present. + */ + public int getTtl() { + try { + return Integer.parseInt(getParameter(ttl_param)); + } catch (Exception e) { + return 1; + } + } + + /** Whether ttl parameter is present */ + public boolean hasTtl() { + return hasParameter(ttl_param); + } + + /** Adds ttl parameter */ + public void addTtl(int ttl) { + addParameter(ttl_param, Integer.toString(ttl)); + } + + /** Whether lr (loose-route) parameter is present */ + public boolean hasLr() { + return hasParameter(lr_param); + } + + /** Adds lr parameter */ + public void addLr() { + addParameter(lr_param); + } +} diff --git a/src/org/zoolu/sip/authentication/DigestAuthentication.java b/app/src/main/java/org/zoolu/sip/authentication/DigestAuthentication.java similarity index 96% rename from src/org/zoolu/sip/authentication/DigestAuthentication.java rename to app/src/main/java/org/zoolu/sip/authentication/DigestAuthentication.java index 80e5298..f129e74 100644 --- a/src/org/zoolu/sip/authentication/DigestAuthentication.java +++ b/app/src/main/java/org/zoolu/sip/authentication/DigestAuthentication.java @@ -1,319 +1,319 @@ -package org.zoolu.sip.authentication; - -import org.zoolu.sip.header.AuthenticationHeader; -import org.zoolu.sip.header.AuthorizationHeader; -import org.zoolu.sip.header.ProxyAuthorizationHeader; -import org.zoolu.sip.header.WwwAuthenticateHeader; -import org.zoolu.tools.MD5; -import org.zoolu.tools.Random; - -/** - * The HTTP Digest Authentication as defined in RFC2617. It can be used to i) - * calculate an authentication response from an authentication request, or ii) - * validate an authentication response.
in the former case the - * DigestAuthentication is created based on a WwwAuthenticationHeader (or - * ProxyAuthenticationHeader), while in the latter case it is created based on - * an AuthorizationHeader (or ProxyAuthorizationHeader). - */ -public class DigestAuthentication { - protected String method; - protected String username; - protected String passwd; - - protected String realm; - protected String nonce; // e.g. base 64 encoding of time-stamp H(time-stamp - // ":" ETag ":" private-key) - // protected String[] domain; - protected String opaque; - // protected boolean stale; // "true" | "false" - protected String algorithm; // "MD5" | "MD5-sess" | token - - protected String qop; // "auth" | "auth-int" | token - - protected String uri; - protected String cnonce; - protected String nc; - protected String response; - - protected String body; - - /** Costructs a new DigestAuthentication. */ - protected DigestAuthentication() { - } - - /** Costructs a new DigestAuthentication. */ - public DigestAuthentication(String method, AuthorizationHeader ah, - String body, String passwd) { - init(method, ah, body, passwd); - } - - /** Costructs a new DigestAuthentication. */ - public DigestAuthentication(String method, String uri, - WwwAuthenticateHeader ah, String qop, String body, String username, - String passwd) { - init(method, ah, body, passwd); - this.uri = uri; - this.qop = qop; - if (qop != null && cnonce == null) { - this.cnonce = Random.nextHexString(16); - /* - * A new cnonce is generated for every new instance so - * no need to keep track of the nc value. - */ - this.nc = "00000001"; - } - this.username = username; - } - - /** Costructs a new DigestAuthentication. */ - private void init(String method, AuthenticationHeader ah, String body, - String passwd) { - this.method = method; - this.username = ah.getUsernameParam(); - this.passwd = passwd; - this.realm = ah.getRealmParam(); - this.opaque = ah.getOpaqueParam(); - this.nonce = ah.getNonceParam(); - this.algorithm = ah.getAlgorithParam(); - this.qop = ah.getQopParam(); - this.uri = ah.getUriParam(); - this.cnonce = ah.getCnonceParam(); - this.nc = ah.getNcParam(); - this.response = ah.getResponseParam(); - this.body = body; - } - - /** Gets a String representation of the object. */ - public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append("method=").append(method).append("\n"); - sb.append("username=").append(username).append("\n"); - sb.append("passwd=").append(passwd).append("\n"); - sb.append("realm=").append(realm).append("\n"); - sb.append("nonce=").append(nonce).append("\n"); - sb.append("opaque=").append(opaque).append("\n"); - sb.append("algorithm=").append(algorithm).append("\n"); - sb.append("qop=").append(qop).append("\n"); - sb.append("uri=").append(uri).append("\n"); - sb.append("cnonce=").append(cnonce).append("\n"); - sb.append("nc=").append(nc).append("\n"); - sb.append("response=").append(response).append("\n"); - sb.append("body=").append(body).append("\n"); - return sb.toString(); - } - - /** Whether the digest-response in the 'response' parameter in correct. */ - public boolean checkResponse() { - if (response == null) - return false; - else - return response.equals(getResponse()); - } - - /** - * Gets a new AuthorizationHeader based on current authentication - * attributes. - */ - public AuthorizationHeader getAuthorizationHeader() { - AuthorizationHeader ah = new AuthorizationHeader("Digest"); - ah.addUsernameParam(username); - ah.addRealmParam(realm); - ah.addNonceParam(nonce); - ah.addUriParam(uri); - if (algorithm != null) - ah.addAlgorithParam(algorithm); - if (opaque != null) - ah.addOpaqueParam(opaque); - if (qop != null) - ah.addQopParam(qop); - if (nc != null) - ah.addNcParam(nc); - if (cnonce != null) - ah.addCnonceParam(cnonce); - String response = getResponse(); - ah.addResponseParam(response); - return ah; - } - - /** - * Gets a new ProxyAuthorizationHeader based on current authentication - * attributes. - */ - public ProxyAuthorizationHeader getProxyAuthorizationHeader() { - return new ProxyAuthorizationHeader(getAuthorizationHeader().getValue()); - } - - /** - * Calculates the digest-response. - *

- * If the "qop" value is "auth" or "auth-int":
- * KD ( H(A1), unq(nonce) ":" nc ":" unq(cnonce) ":" unq(qop) ":" H(A2) ) - * - *

- * If the "qop" directive is not present:
- * KD ( H(A1), unq(nonce) ":" H(A2) ) - */ - public String getResponse() { - String secret = HEX(MD5(A1())); - StringBuffer sb = new StringBuffer(); - if (nonce != null) - sb.append(nonce); - sb.append(":"); - if (qop != null) { - if (nc != null) - sb.append(nc); - sb.append(":"); - if (cnonce != null) - sb.append(cnonce); - sb.append(":"); - sb.append(qop); - sb.append(":"); - } - sb.append(HEX(MD5(A2()))); - String data = sb.toString(); - return HEX(KD(secret, data)); - } - - /** - * Calculates KD() value. - *

- * KD(secret, data) = H(concat(secret, ":", data)) - */ - private byte[] KD(String secret, String data) { - StringBuffer sb = new StringBuffer(); - sb.append(secret).append(":").append(data); - return MD5(sb.toString()); - } - - /** - * Calculates A1 value. - *

- * If the "algorithm" directive's value is "MD5" or is unspecified:
- * A1 = unq(username) ":" unq(realm) ":" passwd - * - *

- * If the "algorithm" directive's value is "MD5-sess":
- * A1 = H( unq(username) ":" unq(realm) ":" passwd ) ":" unq(nonce) ":" - * unq(cnonce) - */ - private byte[] A1() { - StringBuffer sb = new StringBuffer(); - if (username != null) - sb.append(username); - sb.append(":"); - if (realm != null) - sb.append(realm); - sb.append(":"); - if (passwd != null) - sb.append(passwd); - - if (algorithm == null || !algorithm.equalsIgnoreCase("MD5-sess")) { - return sb.toString().getBytes(); - } else { - StringBuffer sb2 = new StringBuffer(); - sb2.append(":"); - if (nonce != null) - sb2.append(nonce); - sb2.append(":"); - if (cnonce != null) - sb2.append(cnonce); - return cat(MD5(sb.toString()), sb2.toString().getBytes()); - } - } - - /** - * Calculates A2 value. - *

- * If the "qop" directive's value is "auth" or is unspecified:
- * A2 = Method ":" digest-uri - * - *

- * If the "qop" value is "auth-int":
- * A2 = Method ":" digest-uri ":" H(entity-body) - */ - private String A2() { - StringBuffer sb = new StringBuffer(); - sb.append(method); - sb.append(":"); - if (uri != null) - sb.append(uri); - - if (qop != null && qop.equalsIgnoreCase("auth-int")) { - sb.append(":"); - if (body == null) - sb.append(HEX(MD5(""))); - else - sb.append(HEX(MD5(body))); - } - return sb.toString(); - } - - /** Concatenates two arrays of bytes. */ - private static byte[] cat(byte[] a, byte[] b) { - int len = a.length + b.length; - byte[] c = new byte[len]; - for (int i = 0; i < a.length; i++) - c[i] = a[i]; - for (int i = 0; i < b.length; i++) - c[i + a.length] = b[i]; - return c; - } - - /** Calculates the MD5 of a String. */ - private static byte[] MD5(String str) { - return MD5.digest(str); - } - - /** Calculates the MD5 of an array of bytes. */ - private static byte[] MD5(byte[] bb) { - return MD5.digest(bb); - } - - /** Calculates the HEX of an array of bytes. */ - private static String HEX(byte[] bb) { - return MD5.asHex(bb); - } - - /** - * Main method. It tests DigestAuthentication with the example provided in - * the RFC2617. - */ - public static void main(String[] args) { - /* - * Authorization: Digest username="Mufasa", realm="testrealm@host.com", - * nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", uri="/dir/index.html", - * qop=auth, nc=00000001, cnonce="0a4f113b", - * response="6629fae49393a05397450978507c4ef1", - * opaque="5ccc069c403ebaf9f0171e9517f40e41" - */ - DigestAuthentication a = new DigestAuthentication(); - a.method = "GET"; - a.passwd = "Circle Of Life"; - a.realm = "testrealm@host.com"; - a.nonce = "dcd98b7102dd2f0e8b11d0f600bfb0c093"; - a.uri = "/dir/index.html"; - a.qop = "auth"; - a.nc = "00000001"; - a.cnonce = "0a4f113b"; - a.username = "Mufasa"; - - String response1 = a.getResponse(); - String response2 = "6629fae49393a05397450978507c4ef1"; - System.out.println(response1); - System.out.println(response2); - - System.out.println(" "); - - String ah_str = "Digest username=\"Mufasa\", realm=\"testrealm@host.com\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", uri=\"/dir/index.html\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", response=\"6629fae49393a05397450978507c4ef1\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"\n"; - - AuthorizationHeader ah = new AuthorizationHeader(ah_str); - a = new DigestAuthentication("GET", ah, null, "Circle Of Life"); - response1 = a.getResponse(); - response2 = "6629fae49393a05397450978507c4ef1"; - System.out.println(response1); - System.out.println(response2); - - System.out.println(a.checkResponse()); - - } -} +package org.zoolu.sip.authentication; + +import org.zoolu.sip.header.AuthenticationHeader; +import org.zoolu.sip.header.AuthorizationHeader; +import org.zoolu.sip.header.ProxyAuthorizationHeader; +import org.zoolu.sip.header.WwwAuthenticateHeader; +import org.zoolu.tools.MD5; +import org.zoolu.tools.Random; + +/** + * The HTTP Digest Authentication as defined in RFC2617. It can be used to i) + * calculate an authentication response from an authentication request, or ii) + * validate an authentication response.
in the former case the + * DigestAuthentication is created based on a WwwAuthenticationHeader (or + * ProxyAuthenticationHeader), while in the latter case it is created based on + * an AuthorizationHeader (or ProxyAuthorizationHeader). + */ +public class DigestAuthentication { + protected String method; + protected String username; + protected String passwd; + + protected String realm; + protected String nonce; // e.g. base 64 encoding of time-stamp H(time-stamp + // ":" ETag ":" private-key) + // protected String[] domain; + protected String opaque; + // protected boolean stale; // "true" | "false" + protected String algorithm; // "MD5" | "MD5-sess" | token + + protected String qop; // "auth" | "auth-int" | token + + protected String uri; + protected String cnonce; + protected String nc; + protected String response; + + protected String body; + + /** Costructs a new DigestAuthentication. */ + protected DigestAuthentication() { + } + + /** Costructs a new DigestAuthentication. */ + public DigestAuthentication(String method, AuthorizationHeader ah, + String body, String passwd) { + init(method, ah, body, passwd); + } + + /** Costructs a new DigestAuthentication. */ + public DigestAuthentication(String method, String uri, + WwwAuthenticateHeader ah, String qop, String body, String username, + String passwd) { + init(method, ah, body, passwd); + this.uri = uri; + this.qop = qop; + if (qop != null && cnonce == null) { + this.cnonce = Random.nextHexString(16); + /* + * A new cnonce is generated for every new instance so + * no need to keep track of the nc value. + */ + this.nc = "00000001"; + } + this.username = username; + } + + /** Costructs a new DigestAuthentication. */ + private void init(String method, AuthenticationHeader ah, String body, + String passwd) { + this.method = method; + this.username = ah.getUsernameParam(); + this.passwd = passwd; + this.realm = ah.getRealmParam(); + this.opaque = ah.getOpaqueParam(); + this.nonce = ah.getNonceParam(); + this.algorithm = ah.getAlgorithParam(); + this.qop = ah.getQopParam(); + this.uri = ah.getUriParam(); + this.cnonce = ah.getCnonceParam(); + this.nc = ah.getNcParam(); + this.response = ah.getResponseParam(); + this.body = body; + } + + /** Gets a String representation of the object. */ + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("method=").append(method).append("\n"); + sb.append("username=").append(username).append("\n"); + sb.append("passwd=").append(passwd).append("\n"); + sb.append("realm=").append(realm).append("\n"); + sb.append("nonce=").append(nonce).append("\n"); + sb.append("opaque=").append(opaque).append("\n"); + sb.append("algorithm=").append(algorithm).append("\n"); + sb.append("qop=").append(qop).append("\n"); + sb.append("uri=").append(uri).append("\n"); + sb.append("cnonce=").append(cnonce).append("\n"); + sb.append("nc=").append(nc).append("\n"); + sb.append("response=").append(response).append("\n"); + sb.append("body=").append(body).append("\n"); + return sb.toString(); + } + + /** Whether the digest-response in the 'response' parameter in correct. */ + public boolean checkResponse() { + if (response == null) + return false; + else + return response.equals(getResponse()); + } + + /** + * Gets a new AuthorizationHeader based on current authentication + * attributes. + */ + public AuthorizationHeader getAuthorizationHeader() { + AuthorizationHeader ah = new AuthorizationHeader("Digest"); + ah.addUsernameParam(username); + ah.addRealmParam(realm); + ah.addNonceParam(nonce); + ah.addUriParam(uri); + if (algorithm != null) + ah.addAlgorithParam(algorithm); + if (opaque != null) + ah.addOpaqueParam(opaque); + if (qop != null) + ah.addQopParam(qop); + if (nc != null) + ah.addNcParam(nc); + if (cnonce != null) + ah.addCnonceParam(cnonce); + String response = getResponse(); + ah.addResponseParam(response); + return ah; + } + + /** + * Gets a new ProxyAuthorizationHeader based on current authentication + * attributes. + */ + public ProxyAuthorizationHeader getProxyAuthorizationHeader() { + return new ProxyAuthorizationHeader(getAuthorizationHeader().getValue()); + } + + /** + * Calculates the digest-response. + *

+ * If the "qop" value is "auth" or "auth-int":
+ * KD ( H(A1), unq(nonce) ":" nc ":" unq(cnonce) ":" unq(qop) ":" H(A2) ) + * + *

+ * If the "qop" directive is not present:
+ * KD ( H(A1), unq(nonce) ":" H(A2) ) + */ + public String getResponse() { + String secret = HEX(MD5(A1())); + StringBuffer sb = new StringBuffer(); + if (nonce != null) + sb.append(nonce); + sb.append(":"); + if (qop != null) { + if (nc != null) + sb.append(nc); + sb.append(":"); + if (cnonce != null) + sb.append(cnonce); + sb.append(":"); + sb.append(qop); + sb.append(":"); + } + sb.append(HEX(MD5(A2()))); + String data = sb.toString(); + return HEX(KD(secret, data)); + } + + /** + * Calculates KD() value. + *

+ * KD(secret, data) = H(concat(secret, ":", data)) + */ + private byte[] KD(String secret, String data) { + StringBuffer sb = new StringBuffer(); + sb.append(secret).append(":").append(data); + return MD5(sb.toString()); + } + + /** + * Calculates A1 value. + *

+ * If the "algorithm" directive's value is "MD5" or is unspecified:
+ * A1 = unq(username) ":" unq(realm) ":" passwd + * + *

+ * If the "algorithm" directive's value is "MD5-sess":
+ * A1 = H( unq(username) ":" unq(realm) ":" passwd ) ":" unq(nonce) ":" + * unq(cnonce) + */ + private byte[] A1() { + StringBuffer sb = new StringBuffer(); + if (username != null) + sb.append(username); + sb.append(":"); + if (realm != null) + sb.append(realm); + sb.append(":"); + if (passwd != null) + sb.append(passwd); + + if (algorithm == null || !algorithm.equalsIgnoreCase("MD5-sess")) { + return sb.toString().getBytes(); + } else { + StringBuffer sb2 = new StringBuffer(); + sb2.append(":"); + if (nonce != null) + sb2.append(nonce); + sb2.append(":"); + if (cnonce != null) + sb2.append(cnonce); + return cat(MD5(sb.toString()), sb2.toString().getBytes()); + } + } + + /** + * Calculates A2 value. + *

+ * If the "qop" directive's value is "auth" or is unspecified:
+ * A2 = Method ":" digest-uri + * + *

+ * If the "qop" value is "auth-int":
+ * A2 = Method ":" digest-uri ":" H(entity-body) + */ + private String A2() { + StringBuffer sb = new StringBuffer(); + sb.append(method); + sb.append(":"); + if (uri != null) + sb.append(uri); + + if (qop != null && qop.equalsIgnoreCase("auth-int")) { + sb.append(":"); + if (body == null) + sb.append(HEX(MD5(""))); + else + sb.append(HEX(MD5(body))); + } + return sb.toString(); + } + + /** Concatenates two arrays of bytes. */ + private static byte[] cat(byte[] a, byte[] b) { + int len = a.length + b.length; + byte[] c = new byte[len]; + for (int i = 0; i < a.length; i++) + c[i] = a[i]; + for (int i = 0; i < b.length; i++) + c[i + a.length] = b[i]; + return c; + } + + /** Calculates the MD5 of a String. */ + private static byte[] MD5(String str) { + return MD5.digest(str); + } + + /** Calculates the MD5 of an array of bytes. */ + private static byte[] MD5(byte[] bb) { + return MD5.digest(bb); + } + + /** Calculates the HEX of an array of bytes. */ + private static String HEX(byte[] bb) { + return MD5.asHex(bb); + } + + /** + * Main method. It tests DigestAuthentication with the example provided in + * the RFC2617. + */ + public static void main(String[] args) { + /* + * Authorization: Digest username="Mufasa", realm="testrealm@host.com", + * nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", uri="/dir/index.html", + * qop=auth, nc=00000001, cnonce="0a4f113b", + * response="6629fae49393a05397450978507c4ef1", + * opaque="5ccc069c403ebaf9f0171e9517f40e41" + */ + DigestAuthentication a = new DigestAuthentication(); + a.method = "GET"; + a.passwd = "Circle Of Life"; + a.realm = "testrealm@host.com"; + a.nonce = "dcd98b7102dd2f0e8b11d0f600bfb0c093"; + a.uri = "/dir/index.html"; + a.qop = "auth"; + a.nc = "00000001"; + a.cnonce = "0a4f113b"; + a.username = "Mufasa"; + + String response1 = a.getResponse(); + String response2 = "6629fae49393a05397450978507c4ef1"; + System.out.println(response1); + System.out.println(response2); + + System.out.println(" "); + + String ah_str = "Digest username=\"Mufasa\", realm=\"testrealm@host.com\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", uri=\"/dir/index.html\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", response=\"6629fae49393a05397450978507c4ef1\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"\n"; + + AuthorizationHeader ah = new AuthorizationHeader(ah_str); + a = new DigestAuthentication("GET", ah, null, "Circle Of Life"); + response1 = a.getResponse(); + response2 = "6629fae49393a05397450978507c4ef1"; + System.out.println(response1); + System.out.println(response2); + + System.out.println(a.checkResponse()); + + } +} diff --git a/src/org/zoolu/sip/call/Call.java b/app/src/main/java/org/zoolu/sip/call/Call.java similarity index 96% rename from src/org/zoolu/sip/call/Call.java rename to app/src/main/java/org/zoolu/sip/call/Call.java index 446163e..54b19c9 100644 --- a/src/org/zoolu/sip/call/Call.java +++ b/app/src/main/java/org/zoolu/sip/call/Call.java @@ -1,532 +1,532 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.call; - -import org.zoolu.sip.dialog.*; -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.header.MultipleHeader; -import org.zoolu.tools.Log; -import org.zoolu.tools.LogLevel; - -/* HSC CHANGES START */ -// import org.zoolu.sdp.*; -// import java.util.Vector; -/* HSC CHANGES END */ - -/** - * Class Call implements SIP calls. - *

- * It handles both outgoing or incoming calls. - *

- * Both offer/answer models are supported, that is:
- * i) offer/answer in invite/2xx, or
- * ii) offer/answer in 2xx/ack - */ -public class Call implements InviteDialogListener { - /** Event logger. */ - Log log; - - /** The SipProvider used for the call */ - protected SipProvider sip_provider; - - /** The invite dialog (sip.dialog.InviteDialog) */ - protected InviteDialog dialog; - - /** The user url */ - protected String from_url; - - /** The user contact url */ - protected String contact_url; - - /** The local sdp */ - protected String local_sdp; - - /** The remote sdp */ - protected String remote_sdp; - - /** The call listener (sipx.call.CallListener) */ - CallListener listener; - - /** Creates a new Call. */ - public Call(SipProvider sip_provider, String from_url, String contact_url, - CallListener call_listener) { - this.sip_provider = sip_provider; - this.log = sip_provider.getLog(); - this.listener = call_listener; - this.from_url = from_url; - this.contact_url = contact_url; - this.dialog = null; - this.local_sdp = null; - this.remote_sdp = null; - } - - /** Creates a new Call specifing the sdp */ - /* - * public Call(SipProvider sip_provider, String from_url, String - * contact_url, String sdp, CallListener call_listener) { - * this.sip_provider=sip_provider; this.log=sip_provider.getLog(); - * this.listener=call_listener; this.from_url=from_url; - * this.contact_url=contact_url; local_sdp=sdp; } - */ - - /** Gets the current invite dialog */ - /* - * public InviteDialog getInviteDialog() { return dialog; } - */ - - /** Gets the current local session descriptor */ - public String getLocalSessionDescriptor() { - return local_sdp; - } - - /** Sets a new local session descriptor */ - public void setLocalSessionDescriptor(String sdp) { - local_sdp = sdp; - } - - /** Gets the current remote session descriptor */ - public String getRemoteSessionDescriptor() { - return remote_sdp; - } - - /** Whether the call is on (active). */ - public boolean isOnCall() { - return dialog.isSessionActive(); - } - - /** Waits for an incoming call */ - public void listen() { - dialog = new InviteDialog(sip_provider, this); - dialog.listen(); - } - - /** Starts a new call, inviting a remote user (callee) */ - public void call(String callee) { - call(callee, null, null, null, null); // modified by mandrajg - } - - /** Starts a new call, inviting a remote user (callee) */ - public void call(String callee, String sdp, String icsi) { // modified by mandrajg - call(callee, null, null, sdp, icsi); - } - - /** Starts a new call, inviting a remote user (callee) */ - public void call(String callee, String from, String contact, String sdp, String icsi) { // modified by mandrajg - printLog("calling " + callee, LogLevel.HIGH); - if (from == null) - from = from_url; - if (contact == null) - contact = contact_url; - if (sdp != null) - local_sdp = sdp; - dialog = new InviteDialog(sip_provider, this); - if (local_sdp != null) - dialog.invite(callee, from, contact, local_sdp, icsi); // modified by mandrajg - else - dialog.inviteWithoutOffer(callee, from, contact); - } - - /** Starts a new call with the invite message request */ - public void call(Message invite) { - dialog = new InviteDialog(sip_provider, this); - local_sdp = invite.getBody(); - if (local_sdp != null) - dialog.invite(invite); - else - dialog.inviteWithoutOffer(invite); - } - - /** Answers at the 2xx/offer (in the ack message) */ - public void ackWithAnswer(String sdp) { - local_sdp = sdp; - dialog.ackWithAnswer(contact_url, sdp); - } - - /** Rings back for the incoming call */ - public void ring(String sdp) { // modified - local_sdp = sdp; - if (dialog != null) - dialog.ring(sdp); - } - - /** Respond to a incoming call (invite) with resp */ - public void respond(Message resp) { - if (dialog != null) - dialog.respond(resp); - } - - /** Accepts the incoming call */ - /* - * public void accept() { accept(local_sdp); } - */ - - /** Accepts the incoming call */ - public void accept(String sdp) { - local_sdp = sdp; - if (dialog != null) - dialog.accept(contact_url, local_sdp); - } - - /** Redirects the incoming call */ - public void redirect(String redirect_url) { - if (dialog != null) - dialog.redirect(302, "Moved Temporarily", redirect_url); - } - - /** Refuses the incoming call */ - public void refuse() { - if (dialog != null) - dialog.refuse(); - } - - /** Cancels the outgoing call */ - public void cancel() { - if (dialog != null) - dialog.cancel(); - } - - /** Close the ongoing call */ - public void bye() { - if (dialog != null) - dialog.bye(); - } - - /** Modify the current call */ - public void modify(String contact, String sdp) { - local_sdp = sdp; - if (dialog != null) - dialog.reInvite(contact, local_sdp); - } - - /** - * Closes an ongoing or incoming/outgoing call - *

- * It trys to fires refuse(), cancel(), and bye() methods - */ - public void hangup() { - if (dialog != null) { // try dialog.refuse(), cancel(), and bye() - // methods.. - dialog.busy(); - dialog.cancel(); - dialog.bye(); - } - } - - public void busy() { - if (dialog != null) - dialog.busy(); // modified - } - - // ************** Inherited from InviteDialogListener ************** - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallIncoming()). - */ - public void onDlgInvite(InviteDialog d, NameAddress callee, - NameAddress caller, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - if (listener != null) - listener.onCallIncoming(this, callee, caller, sdp, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallModifying()). - */ - public void onDlgReInvite(InviteDialog d, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - if (listener != null) - listener.onCallModifying(this, sdp, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallRinging()). - */ - public void onDlgInviteProvisionalResponse(InviteDialog d, int code, - String reason, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - if (code == 180 || code == 183) // modified - if (listener != null) - listener.onCallRinging(this, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallAccepted()). - */ - public void onDlgInviteSuccessResponse(InviteDialog d, int code, - String reason, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - if (listener != null) - listener.onCallAccepted(this, sdp, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallRedirection()). - */ - public void onDlgInviteRedirectResponse(InviteDialog d, int code, - String reason, MultipleHeader contacts, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallRedirection(this, reason, contacts.getValues(), msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallRefused()). - */ - public void onDlgInviteFailureResponse(InviteDialog d, int code, - String reason, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallRefused(this, reason, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallTimeout()). - */ - public void onDlgTimeout(InviteDialog d) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallTimeout(this); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. - */ - public void onDlgReInviteProvisionalResponse(InviteDialog d, int code, - String reason, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallReInviteAccepted()). - */ - public void onDlgReInviteSuccessResponse(InviteDialog d, int code, - String reason, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - if (listener != null) - listener.onCallReInviteAccepted(this, sdp, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallReInviteRedirection()). - */ - // public void onDlgReInviteRedirectResponse(InviteDialog d, int code, - // String reason, MultipleHeader contacts, Message msg) - // { if (d!=dialog) { printLog("NOT the current dialog",LogLevel.HIGH); - // return; } - // if (listener!=null) - // listener.onCallReInviteRedirection(this,reason,contacts.getValues(),msg); - // } - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallReInviteRefused()). - */ - public void onDlgReInviteFailureResponse(InviteDialog d, int code, - String reason, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallReInviteRefused(this, reason, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallReInviteTimeout()). - */ - public void onDlgReInviteTimeout(InviteDialog d) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallReInviteTimeout(this); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallConfirmed()). - */ - public void onDlgAck(InviteDialog d, String sdp, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (sdp != null && sdp.length() != 0) - remote_sdp = sdp; - if (listener != null) - listener.onCallConfirmed(this, sdp, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onCallClosing()). - */ - public void onDlgCancel(InviteDialog d, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallCanceling(this, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onClosing()). - */ - public void onDlgBye(InviteDialog d, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallClosing(this, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onClosed()). - */ - public void onDlgByeFailureResponse(InviteDialog d, int code, - String reason, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallClosed(this, msg); - } - - /** - * Inherited from class InviteDialogListener and called by an InviteDialag. - * Normally you should not use it. Use specific callback methods instead - * (e.g. onClosed()). - */ - public void onDlgByeSuccessResponse(InviteDialog d, int code, - String reason, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - if (listener != null) - listener.onCallClosed(this, msg); - } - - // ----------------------------------------------------- - - /** When an incoming INVITE is accepted */ - // public void onDlgAccepted(InviteDialog dialog) {} - /** When an incoming INVITE is refused */ - // public void onDlgRefused(InviteDialog dialog) {} - /** When the INVITE handshake is successful terminated */ - public void onDlgCall(InviteDialog dialog) { - } - - /** When an incoming Re-INVITE is accepted */ - // public void onDlgReInviteAccepted(InviteDialog dialog) {} - /** When an incoming Re-INVITE is refused */ - // public void onDlgReInviteRefused(InviteDialog dialog) {} - /** When a BYE request traqnsaction has been started */ - // public void onDlgByeing(InviteDialog dialog) {} - /** When the dialog is finally closed */ - public void onDlgClose(InviteDialog dialog) { - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("Call: " + str, level + SipStack.LOG_LEVEL_CALL); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.call; + +import org.zoolu.sip.dialog.*; +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.header.MultipleHeader; +import org.zoolu.tools.Log; +import org.zoolu.tools.LogLevel; + +/* HSC CHANGES START */ +// import org.zoolu.sdp.*; +// import java.util.Vector; +/* HSC CHANGES END */ + +/** + * Class Call implements SIP calls. + *

+ * It handles both outgoing or incoming calls. + *

+ * Both offer/answer models are supported, that is:
+ * i) offer/answer in invite/2xx, or
+ * ii) offer/answer in 2xx/ack + */ +public class Call implements InviteDialogListener { + /** Event logger. */ + Log log; + + /** The SipProvider used for the call */ + protected SipProvider sip_provider; + + /** The invite dialog (sip.dialog.InviteDialog) */ + protected InviteDialog dialog; + + /** The user url */ + protected String from_url; + + /** The user contact url */ + protected String contact_url; + + /** The local sdp */ + protected String local_sdp; + + /** The remote sdp */ + protected String remote_sdp; + + /** The call listener (sipx.call.CallListener) */ + CallListener listener; + + /** Creates a new Call. */ + public Call(SipProvider sip_provider, String from_url, String contact_url, + CallListener call_listener) { + this.sip_provider = sip_provider; + this.log = sip_provider.getLog(); + this.listener = call_listener; + this.from_url = from_url; + this.contact_url = contact_url; + this.dialog = null; + this.local_sdp = null; + this.remote_sdp = null; + } + + /** Creates a new Call specifing the sdp */ + /* + * public Call(SipProvider sip_provider, String from_url, String + * contact_url, String sdp, CallListener call_listener) { + * this.sip_provider=sip_provider; this.log=sip_provider.getLog(); + * this.listener=call_listener; this.from_url=from_url; + * this.contact_url=contact_url; local_sdp=sdp; } + */ + + /** Gets the current invite dialog */ + /* + * public InviteDialog getInviteDialog() { return dialog; } + */ + + /** Gets the current local session descriptor */ + public String getLocalSessionDescriptor() { + return local_sdp; + } + + /** Sets a new local session descriptor */ + public void setLocalSessionDescriptor(String sdp) { + local_sdp = sdp; + } + + /** Gets the current remote session descriptor */ + public String getRemoteSessionDescriptor() { + return remote_sdp; + } + + /** Whether the call is on (active). */ + public boolean isOnCall() { + return dialog.isSessionActive(); + } + + /** Waits for an incoming call */ + public void listen() { + dialog = new InviteDialog(sip_provider, this); + dialog.listen(); + } + + /** Starts a new call, inviting a remote user (callee) */ + public void call(String callee) { + call(callee, null, null, null, null); // modified by mandrajg + } + + /** Starts a new call, inviting a remote user (callee) */ + public void call(String callee, String sdp, String icsi) { // modified by mandrajg + call(callee, null, null, sdp, icsi); + } + + /** Starts a new call, inviting a remote user (callee) */ + public void call(String callee, String from, String contact, String sdp, String icsi) { // modified by mandrajg + printLog("calling " + callee, LogLevel.HIGH); + if (from == null) + from = from_url; + if (contact == null) + contact = contact_url; + if (sdp != null) + local_sdp = sdp; + dialog = new InviteDialog(sip_provider, this); + if (local_sdp != null) + dialog.invite(callee, from, contact, local_sdp, icsi); // modified by mandrajg + else + dialog.inviteWithoutOffer(callee, from, contact); + } + + /** Starts a new call with the invite message request */ + public void call(Message invite) { + dialog = new InviteDialog(sip_provider, this); + local_sdp = invite.getBody(); + if (local_sdp != null) + dialog.invite(invite); + else + dialog.inviteWithoutOffer(invite); + } + + /** Answers at the 2xx/offer (in the ack message) */ + public void ackWithAnswer(String sdp) { + local_sdp = sdp; + dialog.ackWithAnswer(contact_url, sdp); + } + + /** Rings back for the incoming call */ + public void ring(String sdp) { // modified + local_sdp = sdp; + if (dialog != null) + dialog.ring(sdp); + } + + /** Respond to a incoming call (invite) with resp */ + public void respond(Message resp) { + if (dialog != null) + dialog.respond(resp); + } + + /** Accepts the incoming call */ + /* + * public void accept() { accept(local_sdp); } + */ + + /** Accepts the incoming call */ + public void accept(String sdp) { + local_sdp = sdp; + if (dialog != null) + dialog.accept(contact_url, local_sdp); + } + + /** Redirects the incoming call */ + public void redirect(String redirect_url) { + if (dialog != null) + dialog.redirect(302, "Moved Temporarily", redirect_url); + } + + /** Refuses the incoming call */ + public void refuse() { + if (dialog != null) + dialog.refuse(); + } + + /** Cancels the outgoing call */ + public void cancel() { + if (dialog != null) + dialog.cancel(); + } + + /** Close the ongoing call */ + public void bye() { + if (dialog != null) + dialog.bye(); + } + + /** Modify the current call */ + public void modify(String contact, String sdp) { + local_sdp = sdp; + if (dialog != null) + dialog.reInvite(contact, local_sdp); + } + + /** + * Closes an ongoing or incoming/outgoing call + *

+ * It trys to fires refuse(), cancel(), and bye() methods + */ + public void hangup() { + if (dialog != null) { // try dialog.refuse(), cancel(), and bye() + // methods.. + dialog.busy(); + dialog.cancel(); + dialog.bye(); + } + } + + public void busy() { + if (dialog != null) + dialog.busy(); // modified + } + + // ************** Inherited from InviteDialogListener ************** + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallIncoming()). + */ + public void onDlgInvite(InviteDialog d, NameAddress callee, + NameAddress caller, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + if (listener != null) + listener.onCallIncoming(this, callee, caller, sdp, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallModifying()). + */ + public void onDlgReInvite(InviteDialog d, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + if (listener != null) + listener.onCallModifying(this, sdp, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallRinging()). + */ + public void onDlgInviteProvisionalResponse(InviteDialog d, int code, + String reason, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + if (code == 180 || code == 183) // modified + if (listener != null) + listener.onCallRinging(this, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallAccepted()). + */ + public void onDlgInviteSuccessResponse(InviteDialog d, int code, + String reason, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + if (listener != null) + listener.onCallAccepted(this, sdp, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallRedirection()). + */ + public void onDlgInviteRedirectResponse(InviteDialog d, int code, + String reason, MultipleHeader contacts, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallRedirection(this, reason, contacts.getValues(), msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallRefused()). + */ + public void onDlgInviteFailureResponse(InviteDialog d, int code, + String reason, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallRefused(this, reason, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallTimeout()). + */ + public void onDlgTimeout(InviteDialog d) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallTimeout(this); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. + */ + public void onDlgReInviteProvisionalResponse(InviteDialog d, int code, + String reason, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallReInviteAccepted()). + */ + public void onDlgReInviteSuccessResponse(InviteDialog d, int code, + String reason, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + if (listener != null) + listener.onCallReInviteAccepted(this, sdp, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallReInviteRedirection()). + */ + // public void onDlgReInviteRedirectResponse(InviteDialog d, int code, + // String reason, MultipleHeader contacts, Message msg) + // { if (d!=dialog) { printLog("NOT the current dialog",LogLevel.HIGH); + // return; } + // if (listener!=null) + // listener.onCallReInviteRedirection(this,reason,contacts.getValues(),msg); + // } + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallReInviteRefused()). + */ + public void onDlgReInviteFailureResponse(InviteDialog d, int code, + String reason, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallReInviteRefused(this, reason, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallReInviteTimeout()). + */ + public void onDlgReInviteTimeout(InviteDialog d) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallReInviteTimeout(this); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallConfirmed()). + */ + public void onDlgAck(InviteDialog d, String sdp, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (sdp != null && sdp.length() != 0) + remote_sdp = sdp; + if (listener != null) + listener.onCallConfirmed(this, sdp, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onCallClosing()). + */ + public void onDlgCancel(InviteDialog d, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallCanceling(this, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onClosing()). + */ + public void onDlgBye(InviteDialog d, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallClosing(this, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onClosed()). + */ + public void onDlgByeFailureResponse(InviteDialog d, int code, + String reason, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallClosed(this, msg); + } + + /** + * Inherited from class InviteDialogListener and called by an InviteDialag. + * Normally you should not use it. Use specific callback methods instead + * (e.g. onClosed()). + */ + public void onDlgByeSuccessResponse(InviteDialog d, int code, + String reason, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + if (listener != null) + listener.onCallClosed(this, msg); + } + + // ----------------------------------------------------- + + /** When an incoming INVITE is accepted */ + // public void onDlgAccepted(InviteDialog dialog) {} + /** When an incoming INVITE is refused */ + // public void onDlgRefused(InviteDialog dialog) {} + /** When the INVITE handshake is successful terminated */ + public void onDlgCall(InviteDialog dialog) { + } + + /** When an incoming Re-INVITE is accepted */ + // public void onDlgReInviteAccepted(InviteDialog dialog) {} + /** When an incoming Re-INVITE is refused */ + // public void onDlgReInviteRefused(InviteDialog dialog) {} + /** When a BYE request traqnsaction has been started */ + // public void onDlgByeing(InviteDialog dialog) {} + /** When the dialog is finally closed */ + public void onDlgClose(InviteDialog dialog) { + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("Call: " + str, level + SipStack.LOG_LEVEL_CALL); + } +} diff --git a/src/org/zoolu/sip/call/CallListener.java b/app/src/main/java/org/zoolu/sip/call/CallListener.java similarity index 97% rename from src/org/zoolu/sip/call/CallListener.java rename to app/src/main/java/org/zoolu/sip/call/CallListener.java index b8e8408..5dd48de 100644 --- a/src/org/zoolu/sip/call/CallListener.java +++ b/app/src/main/java/org/zoolu/sip/call/CallListener.java @@ -1,93 +1,93 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.call; - -import org.zoolu.sip.message.*; -import org.zoolu.sip.address.NameAddress; -import java.util.Vector; - -/** - * Interface CallListener can be implemented to manage SIP calls - * (sipx.call.Call). - *

- * Objects of class Call use CallListener callback methods to signal specific - * call events. - */ -public interface CallListener { - /** - * Callback function called when arriving a new INVITE method (incoming - * call) - */ - public void onCallIncoming(Call call, NameAddress callee, - NameAddress caller, String sdp, Message invite); - - /** - * Callback function called when arriving a new Re-INVITE method - * (re-inviting/call modify) - */ - public void onCallModifying(Call call, String sdp, Message invite); - - /** Callback function called when arriving a 180 Ringing */ - public void onCallRinging(Call call, Message resp); - - /** Callback function called when arriving a 2xx (call accepted) */ - public void onCallAccepted(Call call, String sdp, Message resp); - - /** Callback function called when arriving a 4xx (call failure) */ - public void onCallRefused(Call call, String reason, Message resp); - - /** Callback function called when arriving a 3xx (call redirection) */ - public void onCallRedirection(Call call, String reason, - Vector contact_list, Message resp); - - /** Callback function called when arriving an ACK method (call confirmed) */ - public void onCallConfirmed(Call call, String sdp, Message ack); - - /** Callback function called when the invite expires */ - public void onCallTimeout(Call call); - - /** Callback function called when arriving a 2xx (re-invite/modify accepted) */ - public void onCallReInviteAccepted(Call call, String sdp, Message resp); - - /** Callback function called when arriving a 4xx (re-invite/modify failure) */ - public void onCallReInviteRefused(Call call, String reason, Message resp); - - /** Callback function called when a re-invite expires */ - public void onCallReInviteTimeout(Call call); - - /** Callback function called when arriving a 3xx (call redirection) */ - // public void onCallReInviteRedirection(Call call, String reason, Vector - // contact_list, Message resp); - /** Callback function called when arriving a CANCEL request */ - public void onCallCanceling(Call call, Message cancel); - - /** Callback function called when arriving a BYE request */ - public void onCallClosing(Call call, Message bye); - - /** - * Callback function called when arriving a response for the BYE request - * (call closed) - */ - public void onCallClosed(Call call, Message resp); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.call; + +import org.zoolu.sip.message.*; +import org.zoolu.sip.address.NameAddress; +import java.util.Vector; + +/** + * Interface CallListener can be implemented to manage SIP calls + * (sipx.call.Call). + *

+ * Objects of class Call use CallListener callback methods to signal specific + * call events. + */ +public interface CallListener { + /** + * Callback function called when arriving a new INVITE method (incoming + * call) + */ + public void onCallIncoming(Call call, NameAddress callee, + NameAddress caller, String sdp, Message invite); + + /** + * Callback function called when arriving a new Re-INVITE method + * (re-inviting/call modify) + */ + public void onCallModifying(Call call, String sdp, Message invite); + + /** Callback function called when arriving a 180 Ringing */ + public void onCallRinging(Call call, Message resp); + + /** Callback function called when arriving a 2xx (call accepted) */ + public void onCallAccepted(Call call, String sdp, Message resp); + + /** Callback function called when arriving a 4xx (call failure) */ + public void onCallRefused(Call call, String reason, Message resp); + + /** Callback function called when arriving a 3xx (call redirection) */ + public void onCallRedirection(Call call, String reason, + Vector contact_list, Message resp); + + /** Callback function called when arriving an ACK method (call confirmed) */ + public void onCallConfirmed(Call call, String sdp, Message ack); + + /** Callback function called when the invite expires */ + public void onCallTimeout(Call call); + + /** Callback function called when arriving a 2xx (re-invite/modify accepted) */ + public void onCallReInviteAccepted(Call call, String sdp, Message resp); + + /** Callback function called when arriving a 4xx (re-invite/modify failure) */ + public void onCallReInviteRefused(Call call, String reason, Message resp); + + /** Callback function called when a re-invite expires */ + public void onCallReInviteTimeout(Call call); + + /** Callback function called when arriving a 3xx (call redirection) */ + // public void onCallReInviteRedirection(Call call, String reason, Vector + // contact_list, Message resp); + /** Callback function called when arriving a CANCEL request */ + public void onCallCanceling(Call call, Message cancel); + + /** Callback function called when arriving a BYE request */ + public void onCallClosing(Call call, Message bye); + + /** + * Callback function called when arriving a response for the BYE request + * (call closed) + */ + public void onCallClosed(Call call, Message resp); +} diff --git a/src/org/zoolu/sip/call/CallListenerAdapter.java b/app/src/main/java/org/zoolu/sip/call/CallListenerAdapter.java similarity index 97% rename from src/org/zoolu/sip/call/CallListenerAdapter.java rename to app/src/main/java/org/zoolu/sip/call/CallListenerAdapter.java index 4788194..414e7c9 100644 --- a/src/org/zoolu/sip/call/CallListenerAdapter.java +++ b/app/src/main/java/org/zoolu/sip/call/CallListenerAdapter.java @@ -1,260 +1,260 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.call; - -/* HSC CHANGES START */ -// import org.zoolu.sip.call.*; -// import org.zoolu.sip.provider.SipStack; -/* HSC CHANGES END */ -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.message.Message; /* HSC CHANGES START */// import - // org.zoolu.tools.Log; -// import org.zoolu.tools.LogLevel; -/* HSC CHANGES END */ - -import org.zoolu.sdp.*; /* HSC CHANGES START */// import java.util.Iterator; -// import java.util.Enumeration; -/* HSC CHANGES END */ - -import java.util.Vector; - -/** - * Class CallListenerAdapter implements CallListener interface providing a dummy - * implementation of all Call callback functions used to capture Call events. - *

- * CallListenerAdapter can be extended to manage basic SIP calls. The callback - * methods defined in this class have basically a void implementation. This - * class exists as convenience for creating call listener objects.
- * You can extend this class overriding only methods corresponding to events you - * want to handle. - *

- * onCallIncoming(NameAddress,String) is the only non-empty method. It - * signals the receiver the ring status (by using method Call.ring()), adapts - * the sdp body and accepts the call (by using method Call.accept(sdp)). - */ -public abstract class CallListenerAdapter implements ExtendedCallListener { - - // ************************** Costructors *************************** - - /** Creates a new dummy call listener */ - protected CallListenerAdapter() { - } - - // ************************* Static methods ************************* - - /** - * Changes the current session descriptor specifing the receiving RTP/UDP - * port number, the AVP format, the codec, and the clock rate - */ - /* - * public static String audioSession(int port, int avp, String codec, int - * rate) { SessionDescriptor sdp=new SessionDescriptor(); sdp.addMedia(new - * MediaField("audio ",port,0,"RTP/AVP",String.valueOf(avp)),new - * AttributeField("rtpmap",avp+" "+codec+"/"+rate)); return sdp.toString(); } - */ - - /** - * Changes the current session descriptor specifing the receiving RTP/UDP - * port number, the AVP format, the codec, and the clock rate - */ - /* - * public static String audioSession(int port) { return - * audioSession(port,0,"PCMU",8000); } - */ - - // *********************** Callback functions *********************** - /** - * Accepts an incoming call. Callback function called when arriving a new - * INVITE method (incoming call) - */ - public void onCallIncoming(Call call, NameAddress callee, - NameAddress caller, String sdp, Message invite) { // printLog("INCOMING"); - String local_session; - if (sdp != null && sdp.length() > 0) { - SessionDescriptor remote_sdp = new SessionDescriptor(sdp); - SessionDescriptor local_sdp = new SessionDescriptor(call - .getLocalSessionDescriptor()); - SessionDescriptor new_sdp = new SessionDescriptor(remote_sdp - .getOrigin(), remote_sdp.getSessionName(), local_sdp - .getConnection(), local_sdp.getTime()); - new_sdp.addMediaDescriptors(local_sdp.getMediaDescriptors()); - new_sdp = SdpTools.sdpMediaProduct(new_sdp, remote_sdp - .getMediaDescriptors()); - new_sdp = SdpTools.sdpAttirbuteSelection(new_sdp, "rtpmap"); - local_session = new_sdp.toString(); - } else - local_session = call.getLocalSessionDescriptor(); - call.ring(local_session); - // accept immediatly - call.accept(local_session); - } - - /** - * Changes the call when remotly requested. Callback function called when - * arriving a new Re-INVITE method (re-inviting/call modify) - */ - public void onCallModifying(Call call, String sdp, Message invite) { // printLog("RE-INVITE/MODIFY"); - String local_session; - if (sdp != null && sdp.length() > 0) { - SessionDescriptor remote_sdp = new SessionDescriptor(sdp); - SessionDescriptor local_sdp = new SessionDescriptor(call - .getLocalSessionDescriptor()); - SessionDescriptor new_sdp = new SessionDescriptor(remote_sdp - .getOrigin(), remote_sdp.getSessionName(), local_sdp - .getConnection(), local_sdp.getTime()); - new_sdp.addMediaDescriptors(local_sdp.getMediaDescriptors()); - new_sdp = SdpTools.sdpMediaProduct(new_sdp, remote_sdp - .getMediaDescriptors()); - new_sdp = SdpTools.sdpAttirbuteSelection(new_sdp, "rtpmap"); - local_session = new_sdp.toString(); - } else - local_session = call.getLocalSessionDescriptor(); - // accept immediatly - call.accept(local_session); - } - - /** - * Does nothing. Callback function called when arriving a 180 Ringing - */ - public void onCallRinging(Call call, Message resp) { // printLog("RINGING"); - } - - /** - * Does nothing. Callback function called when arriving a 2xx (call - * accepted) - */ - public void onCallAccepted(Call call, String sdp, Message resp) { // printLog("ACCEPTED/CALL"); - } - - /** - * Does nothing. Callback function called when arriving a 4xx (call failure) - */ - public void onCallRefused(Call call, String reason, Message resp) { // printLog("REFUSED - // ("+reason+")"); - } - - /** - * Redirects the call when remotly requested. Callback function called when - * arriving a 3xx (call redirection) - */ - public void onCallRedirection(Call call, String reason, - Vector contact_list, Message resp) { // printLog("REDIRECTION - // ("+reason+")"); - call.call(contact_list.elementAt(0)); - } - - /** - * Does nothing. Callback function called when arriving an ACK method (call - * confirmed) - */ - public void onCallConfirmed(Call call, String sdp, Message ack) { // printLog("CONFIRMED/CALL"); - } - - /** - * Does nothing. Callback function called when the invite expires - */ - public void onCallTimeout(Call call) { // printLog("TIMEOUT/CLOSE"); - } - - /** - * Does nothing. Callback function called when arriving a 2xx - * (re-invite/modify accepted) - */ - public void onCallReInviteAccepted(Call call, String sdp, Message resp) { // printLog("RE-INVITE-ACCEPTED/CALL"); - } - - /** - * Does nothing. Callback function called when arriving a 4xx - * (re-invite/modify failure) - */ - public void onCallReInviteRefused(Call call, String reason, Message resp) { // printLog("RE-INVITE-REFUSED - // ("+reason+")/CALL"); - } - - /** - * Does nothing. Callback function called when a re-invite expires - */ - public void onCallReInviteTimeout(Call call) { // printLog("RE-INVITE-TIMEOUT/CALL"); - } - - /** - * Does nothing. Callback function called when arriving a CANCEL request - */ - public void onCallCanceling(Call call, Message cancel) { // printLog("CANCELING"); - } - - /** - * Does nothing. Callback function that may be overloaded (extended). Called - * when arriving a BYE request - */ - public void onCallClosing(Call call, Message bye) { // printLog("CLOSING"); - } - - /** - * Does nothing. Callback function that may be overloaded (extended). Called - * when arriving a response for a BYE request (call closed) - */ - public void onCallClosed(Call call, Message resp) { // printLog("CLOSED"); - } - - /** - * Does nothing. Callback function called when arriving a new REFER method - * (transfer request) - */ - public void onCallTransfer(ExtendedCall call, NameAddress refer_to, - NameAddress refered_by, Message refer) { // printLog("REFER-TO/TRANSFER"); - } - - /** - * Does nothing. Callback function called when a call transfer is accepted. - */ - public void onCallTransferAccepted(ExtendedCall call, Message resp) { - } - - /** - * Does nothing. Callback function called when a call transfer is refused. - */ - public void onCallTransferRefused(ExtendedCall call, String reason, - Message resp) { - } - - /** - * Does nothing. Callback function called when a call transfer is - * successfully completed - */ - public void onCallTransferSuccess(ExtendedCall call, Message notify) { // printLog("TRANSFER - // SUCCESS"); - } - - /** - * Does nothing. Callback function called when a call transfer is NOT - * sucessfully completed - */ - public void onCallTransferFailure(ExtendedCall call, String reason, - Message notify) { // printLog("TRANSFER FAILURE"); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.call; + +/* HSC CHANGES START */ +// import org.zoolu.sip.call.*; +// import org.zoolu.sip.provider.SipStack; +/* HSC CHANGES END */ +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.message.Message; /* HSC CHANGES START */// import + // org.zoolu.tools.Log; +// import org.zoolu.tools.LogLevel; +/* HSC CHANGES END */ + +import org.zoolu.sdp.*; /* HSC CHANGES START */// import java.util.Iterator; +// import java.util.Enumeration; +/* HSC CHANGES END */ + +import java.util.Vector; + +/** + * Class CallListenerAdapter implements CallListener interface providing a dummy + * implementation of all Call callback functions used to capture Call events. + *

+ * CallListenerAdapter can be extended to manage basic SIP calls. The callback + * methods defined in this class have basically a void implementation. This + * class exists as convenience for creating call listener objects.
+ * You can extend this class overriding only methods corresponding to events you + * want to handle. + *

+ * onCallIncoming(NameAddress,String) is the only non-empty method. It + * signals the receiver the ring status (by using method Call.ring()), adapts + * the sdp body and accepts the call (by using method Call.accept(sdp)). + */ +public abstract class CallListenerAdapter implements ExtendedCallListener { + + // ************************** Costructors *************************** + + /** Creates a new dummy call listener */ + protected CallListenerAdapter() { + } + + // ************************* Static methods ************************* + + /** + * Changes the current session descriptor specifing the receiving RTP/UDP + * port number, the AVP format, the codec, and the clock rate + */ + /* + * public static String audioSession(int port, int avp, String codec, int + * rate) { SessionDescriptor sdp=new SessionDescriptor(); sdp.addMedia(new + * MediaField("audio ",port,0,"RTP/AVP",String.valueOf(avp)),new + * AttributeField("rtpmap",avp+" "+codec+"/"+rate)); return sdp.toString(); } + */ + + /** + * Changes the current session descriptor specifing the receiving RTP/UDP + * port number, the AVP format, the codec, and the clock rate + */ + /* + * public static String audioSession(int port) { return + * audioSession(port,0,"PCMU",8000); } + */ + + // *********************** Callback functions *********************** + /** + * Accepts an incoming call. Callback function called when arriving a new + * INVITE method (incoming call) + */ + public void onCallIncoming(Call call, NameAddress callee, + NameAddress caller, String sdp, Message invite) { // printLog("INCOMING"); + String local_session; + if (sdp != null && sdp.length() > 0) { + SessionDescriptor remote_sdp = new SessionDescriptor(sdp); + SessionDescriptor local_sdp = new SessionDescriptor(call + .getLocalSessionDescriptor()); + SessionDescriptor new_sdp = new SessionDescriptor(remote_sdp + .getOrigin(), remote_sdp.getSessionName(), local_sdp + .getConnection(), local_sdp.getTime()); + new_sdp.addMediaDescriptors(local_sdp.getMediaDescriptors()); + new_sdp = SdpTools.sdpMediaProduct(new_sdp, remote_sdp + .getMediaDescriptors()); + new_sdp = SdpTools.sdpAttirbuteSelection(new_sdp, "rtpmap"); + local_session = new_sdp.toString(); + } else + local_session = call.getLocalSessionDescriptor(); + call.ring(local_session); + // accept immediatly + call.accept(local_session); + } + + /** + * Changes the call when remotly requested. Callback function called when + * arriving a new Re-INVITE method (re-inviting/call modify) + */ + public void onCallModifying(Call call, String sdp, Message invite) { // printLog("RE-INVITE/MODIFY"); + String local_session; + if (sdp != null && sdp.length() > 0) { + SessionDescriptor remote_sdp = new SessionDescriptor(sdp); + SessionDescriptor local_sdp = new SessionDescriptor(call + .getLocalSessionDescriptor()); + SessionDescriptor new_sdp = new SessionDescriptor(remote_sdp + .getOrigin(), remote_sdp.getSessionName(), local_sdp + .getConnection(), local_sdp.getTime()); + new_sdp.addMediaDescriptors(local_sdp.getMediaDescriptors()); + new_sdp = SdpTools.sdpMediaProduct(new_sdp, remote_sdp + .getMediaDescriptors()); + new_sdp = SdpTools.sdpAttirbuteSelection(new_sdp, "rtpmap"); + local_session = new_sdp.toString(); + } else + local_session = call.getLocalSessionDescriptor(); + // accept immediatly + call.accept(local_session); + } + + /** + * Does nothing. Callback function called when arriving a 180 Ringing + */ + public void onCallRinging(Call call, Message resp) { // printLog("RINGING"); + } + + /** + * Does nothing. Callback function called when arriving a 2xx (call + * accepted) + */ + public void onCallAccepted(Call call, String sdp, Message resp) { // printLog("ACCEPTED/CALL"); + } + + /** + * Does nothing. Callback function called when arriving a 4xx (call failure) + */ + public void onCallRefused(Call call, String reason, Message resp) { // printLog("REFUSED + // ("+reason+")"); + } + + /** + * Redirects the call when remotly requested. Callback function called when + * arriving a 3xx (call redirection) + */ + public void onCallRedirection(Call call, String reason, + Vector contact_list, Message resp) { // printLog("REDIRECTION + // ("+reason+")"); + call.call(contact_list.elementAt(0)); + } + + /** + * Does nothing. Callback function called when arriving an ACK method (call + * confirmed) + */ + public void onCallConfirmed(Call call, String sdp, Message ack) { // printLog("CONFIRMED/CALL"); + } + + /** + * Does nothing. Callback function called when the invite expires + */ + public void onCallTimeout(Call call) { // printLog("TIMEOUT/CLOSE"); + } + + /** + * Does nothing. Callback function called when arriving a 2xx + * (re-invite/modify accepted) + */ + public void onCallReInviteAccepted(Call call, String sdp, Message resp) { // printLog("RE-INVITE-ACCEPTED/CALL"); + } + + /** + * Does nothing. Callback function called when arriving a 4xx + * (re-invite/modify failure) + */ + public void onCallReInviteRefused(Call call, String reason, Message resp) { // printLog("RE-INVITE-REFUSED + // ("+reason+")/CALL"); + } + + /** + * Does nothing. Callback function called when a re-invite expires + */ + public void onCallReInviteTimeout(Call call) { // printLog("RE-INVITE-TIMEOUT/CALL"); + } + + /** + * Does nothing. Callback function called when arriving a CANCEL request + */ + public void onCallCanceling(Call call, Message cancel) { // printLog("CANCELING"); + } + + /** + * Does nothing. Callback function that may be overloaded (extended). Called + * when arriving a BYE request + */ + public void onCallClosing(Call call, Message bye) { // printLog("CLOSING"); + } + + /** + * Does nothing. Callback function that may be overloaded (extended). Called + * when arriving a response for a BYE request (call closed) + */ + public void onCallClosed(Call call, Message resp) { // printLog("CLOSED"); + } + + /** + * Does nothing. Callback function called when arriving a new REFER method + * (transfer request) + */ + public void onCallTransfer(ExtendedCall call, NameAddress refer_to, + NameAddress refered_by, Message refer) { // printLog("REFER-TO/TRANSFER"); + } + + /** + * Does nothing. Callback function called when a call transfer is accepted. + */ + public void onCallTransferAccepted(ExtendedCall call, Message resp) { + } + + /** + * Does nothing. Callback function called when a call transfer is refused. + */ + public void onCallTransferRefused(ExtendedCall call, String reason, + Message resp) { + } + + /** + * Does nothing. Callback function called when a call transfer is + * successfully completed + */ + public void onCallTransferSuccess(ExtendedCall call, Message notify) { // printLog("TRANSFER + // SUCCESS"); + } + + /** + * Does nothing. Callback function called when a call transfer is NOT + * sucessfully completed + */ + public void onCallTransferFailure(ExtendedCall call, String reason, + Message notify) { // printLog("TRANSFER FAILURE"); + } + +} diff --git a/src/org/zoolu/sip/call/ExtendedCall.java b/app/src/main/java/org/zoolu/sip/call/ExtendedCall.java similarity index 96% rename from src/org/zoolu/sip/call/ExtendedCall.java rename to app/src/main/java/org/zoolu/sip/call/ExtendedCall.java index 3d40780..3952f3c 100644 --- a/src/org/zoolu/sip/call/ExtendedCall.java +++ b/app/src/main/java/org/zoolu/sip/call/ExtendedCall.java @@ -1,251 +1,251 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.call; - -/* HSC CHANGES BEGIN */ -// import org.zoolu.sip.call.*; -/* HSC CHANGES END */ -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.dialog.ExtendedInviteDialog; -import org.zoolu.sip.dialog.ExtendedInviteDialogListener; -import org.zoolu.sip.header.StatusLine; -import org.zoolu.sip.message.Message; -import org.zoolu.sip.provider.SipProvider; -import org.zoolu.sip.provider.SipStack; -import org.zoolu.tools.LogLevel; - -/** - * Class ExtendedCall extends basic SIP calls. - *

- * It implements:
- call transfer (REFER/NOTIFY methods) - */ -public class ExtendedCall extends Call implements ExtendedInviteDialogListener { - - ExtendedCallListener xcall_listener; - - Message refer; - - /** User name. */ - String username; - - /** User name. */ - String realm; - - /** User's passwd. */ - String passwd; - - /** Nonce for the next authentication. */ - String next_nonce; - - /** Qop for the next authentication. */ - String qop; - - /** Creates a new ExtendedCall. */ - public ExtendedCall(SipProvider sip_provider, String from_url, - String contact_url, ExtendedCallListener call_listener) { - super(sip_provider, from_url, contact_url, call_listener); - this.xcall_listener = call_listener; - this.refer = null; - this.username = null; - this.realm = null; - this.passwd = null; - this.next_nonce = null; - this.qop = null; - } - - /** Creates a new ExtendedCall specifing the sdp. */ - /* - * public ExtendedCall(SipProvider sip_provider, String from_url, String - * contact_url, String sdp, ExtendedCallListener call_listener) { - * super(sip_provider,from_url,contact_url,sdp,call_listener); - * xcall_listener=call_listener; } - */ - - /** Creates a new ExtendedCall. */ - public ExtendedCall(SipProvider sip_provider, String from_url, - String contact_url, String username, String realm, String passwd, - ExtendedCallListener call_listener) { - super(sip_provider, from_url, contact_url, call_listener); - this.xcall_listener = call_listener; - this.refer = null; - this.username = username; - this.realm = realm; - this.passwd = passwd; - this.next_nonce = null; - this.qop = null; - } - - /** Waits for an incoming call */ - public void listen() { - if (username != null) - dialog = new ExtendedInviteDialog(sip_provider, username, realm, - passwd, this); - else - dialog = new ExtendedInviteDialog(sip_provider, this); - dialog.listen(); - } - - /** Starts a new call, inviting a remote user (r_user) */ - public void call(String r_user, String from, String contact, String sdp, String icsi) { // modified by mandrajg - printLog("calling " + r_user, LogLevel.MEDIUM); - if (username != null) - dialog = new ExtendedInviteDialog(sip_provider, username, realm, - passwd, this); - else - dialog = new ExtendedInviteDialog(sip_provider, this); - if (from == null) - from = from_url; - if (contact == null) - contact = contact_url; - if (sdp != null) - local_sdp = sdp; - if (local_sdp != null) - dialog.invite(r_user, from, contact, local_sdp, icsi); // modified by mandrajg - else - dialog.inviteWithoutOffer(r_user, from, contact); - } - - /** Starts a new call with the invite message request */ - public void call(Message invite) { - dialog = new ExtendedInviteDialog(sip_provider, this); - local_sdp = invite.getBody(); - if (local_sdp != null) - dialog.invite(invite); - else - dialog.inviteWithoutOffer(invite); - } - - public void info(char c, int duration) { - ((ExtendedInviteDialog) dialog).info(c, duration); - } - - /** Requests a call transfer */ - public void transfer(String transfer_to) { - ((ExtendedInviteDialog) dialog).refer(new NameAddress(transfer_to)); - } - - /** Accepts a call transfer request */ - public void acceptTransfer() { - ((ExtendedInviteDialog) dialog).acceptRefer(refer); - } - - /** Refuses a call transfer request */ - public void refuseTransfer() { - ((ExtendedInviteDialog) dialog).refuseRefer(refer); - } - - /** Notifies the satus of an other call */ - public void notify(int code, String reason) { - ((ExtendedInviteDialog) dialog).notify(code, reason); - } - - // ************** Inherited from InviteDialogListener ************** - - /** When an incoming REFER request is received within the dialog */ - public void onDlgRefer(org.zoolu.sip.dialog.InviteDialog d, - NameAddress refer_to, NameAddress referred_by, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - printLog("onDlgRefer(" + refer_to.toString() + ")", LogLevel.LOW); - refer = msg; - if (xcall_listener != null) - xcall_listener.onCallTransfer(this, refer_to, referred_by, msg); - } - - /** When a response is received for a REFER request within the dialog */ - public void onDlgReferResponse(org.zoolu.sip.dialog.InviteDialog d, - int code, String reason, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - printLog("onDlgReferResponse(" + code + " " + reason + ")", - LogLevel.LOW); - if (code >= 200 && code < 300) { - if (xcall_listener != null) - xcall_listener.onCallTransferAccepted(this, msg); - } else if (code >= 300) { - if (xcall_listener != null) - xcall_listener.onCallTransferRefused(this, reason, msg); - } - } - - /** When an incoming NOTIFY request is received within the dialog */ - public void onDlgNotify(org.zoolu.sip.dialog.InviteDialog d, String event, - String sipfragment, Message msg) { - if (d != dialog) { - printLog("NOT the current dialog", LogLevel.HIGH); - return; - } - printLog("onDlgNotify()", LogLevel.LOW); - if (event.equals("refer")) { - Message fragment = new Message(sipfragment); - printLog("Notify: " + sipfragment, LogLevel.HIGH); - if (fragment.isResponse()) { - StatusLine status_line = fragment.getStatusLine(); - int code = status_line.getCode(); - String reason = status_line.getReason(); - if (code >= 200 && code < 300) { - printLog("Call successfully transferred", LogLevel.MEDIUM); - if (xcall_listener != null) - xcall_listener.onCallTransferSuccess(this, msg); - } else if (code >= 300) { - printLog("Call NOT transferred", LogLevel.MEDIUM); - if (xcall_listener != null) - xcall_listener.onCallTransferFailure(this, reason, msg); - } - } - } - } - - /** - * When an incoming request is received within the dialog different from - * INVITE, CANCEL, ACK, BYE - */ - public void onDlgAltRequest(org.zoolu.sip.dialog.InviteDialog d, - String method, String body, Message msg) { - } - - /** - * When a response is received for a request within the dialog different - * from INVITE, CANCEL, ACK, BYE - */ - public void onDlgAltResponse(org.zoolu.sip.dialog.InviteDialog d, - String method, int code, String reason, String body, Message msg) { - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log - .println("ExtendedCall: " + str, level - + SipStack.LOG_LEVEL_CALL); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.call; + +/* HSC CHANGES BEGIN */ +// import org.zoolu.sip.call.*; +/* HSC CHANGES END */ +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.dialog.ExtendedInviteDialog; +import org.zoolu.sip.dialog.ExtendedInviteDialogListener; +import org.zoolu.sip.header.StatusLine; +import org.zoolu.sip.message.Message; +import org.zoolu.sip.provider.SipProvider; +import org.zoolu.sip.provider.SipStack; +import org.zoolu.tools.LogLevel; + +/** + * Class ExtendedCall extends basic SIP calls. + *

+ * It implements:
- call transfer (REFER/NOTIFY methods) + */ +public class ExtendedCall extends Call implements ExtendedInviteDialogListener { + + ExtendedCallListener xcall_listener; + + Message refer; + + /** User name. */ + String username; + + /** User name. */ + String realm; + + /** User's passwd. */ + String passwd; + + /** Nonce for the next authentication. */ + String next_nonce; + + /** Qop for the next authentication. */ + String qop; + + /** Creates a new ExtendedCall. */ + public ExtendedCall(SipProvider sip_provider, String from_url, + String contact_url, ExtendedCallListener call_listener) { + super(sip_provider, from_url, contact_url, call_listener); + this.xcall_listener = call_listener; + this.refer = null; + this.username = null; + this.realm = null; + this.passwd = null; + this.next_nonce = null; + this.qop = null; + } + + /** Creates a new ExtendedCall specifing the sdp. */ + /* + * public ExtendedCall(SipProvider sip_provider, String from_url, String + * contact_url, String sdp, ExtendedCallListener call_listener) { + * super(sip_provider,from_url,contact_url,sdp,call_listener); + * xcall_listener=call_listener; } + */ + + /** Creates a new ExtendedCall. */ + public ExtendedCall(SipProvider sip_provider, String from_url, + String contact_url, String username, String realm, String passwd, + ExtendedCallListener call_listener) { + super(sip_provider, from_url, contact_url, call_listener); + this.xcall_listener = call_listener; + this.refer = null; + this.username = username; + this.realm = realm; + this.passwd = passwd; + this.next_nonce = null; + this.qop = null; + } + + /** Waits for an incoming call */ + public void listen() { + if (username != null) + dialog = new ExtendedInviteDialog(sip_provider, username, realm, + passwd, this); + else + dialog = new ExtendedInviteDialog(sip_provider, this); + dialog.listen(); + } + + /** Starts a new call, inviting a remote user (r_user) */ + public void call(String r_user, String from, String contact, String sdp, String icsi) { // modified by mandrajg + printLog("calling " + r_user, LogLevel.MEDIUM); + if (username != null) + dialog = new ExtendedInviteDialog(sip_provider, username, realm, + passwd, this); + else + dialog = new ExtendedInviteDialog(sip_provider, this); + if (from == null) + from = from_url; + if (contact == null) + contact = contact_url; + if (sdp != null) + local_sdp = sdp; + if (local_sdp != null) + dialog.invite(r_user, from, contact, local_sdp, icsi); // modified by mandrajg + else + dialog.inviteWithoutOffer(r_user, from, contact); + } + + /** Starts a new call with the invite message request */ + public void call(Message invite) { + dialog = new ExtendedInviteDialog(sip_provider, this); + local_sdp = invite.getBody(); + if (local_sdp != null) + dialog.invite(invite); + else + dialog.inviteWithoutOffer(invite); + } + + public void info(char c, int duration) { + ((ExtendedInviteDialog) dialog).info(c, duration); + } + + /** Requests a call transfer */ + public void transfer(String transfer_to) { + ((ExtendedInviteDialog) dialog).refer(new NameAddress(transfer_to)); + } + + /** Accepts a call transfer request */ + public void acceptTransfer() { + ((ExtendedInviteDialog) dialog).acceptRefer(refer); + } + + /** Refuses a call transfer request */ + public void refuseTransfer() { + ((ExtendedInviteDialog) dialog).refuseRefer(refer); + } + + /** Notifies the satus of an other call */ + public void notify(int code, String reason) { + ((ExtendedInviteDialog) dialog).notify(code, reason); + } + + // ************** Inherited from InviteDialogListener ************** + + /** When an incoming REFER request is received within the dialog */ + public void onDlgRefer(org.zoolu.sip.dialog.InviteDialog d, + NameAddress refer_to, NameAddress referred_by, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + printLog("onDlgRefer(" + refer_to.toString() + ")", LogLevel.LOW); + refer = msg; + if (xcall_listener != null) + xcall_listener.onCallTransfer(this, refer_to, referred_by, msg); + } + + /** When a response is received for a REFER request within the dialog */ + public void onDlgReferResponse(org.zoolu.sip.dialog.InviteDialog d, + int code, String reason, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + printLog("onDlgReferResponse(" + code + " " + reason + ")", + LogLevel.LOW); + if (code >= 200 && code < 300) { + if (xcall_listener != null) + xcall_listener.onCallTransferAccepted(this, msg); + } else if (code >= 300) { + if (xcall_listener != null) + xcall_listener.onCallTransferRefused(this, reason, msg); + } + } + + /** When an incoming NOTIFY request is received within the dialog */ + public void onDlgNotify(org.zoolu.sip.dialog.InviteDialog d, String event, + String sipfragment, Message msg) { + if (d != dialog) { + printLog("NOT the current dialog", LogLevel.HIGH); + return; + } + printLog("onDlgNotify()", LogLevel.LOW); + if (event.equals("refer")) { + Message fragment = new Message(sipfragment); + printLog("Notify: " + sipfragment, LogLevel.HIGH); + if (fragment.isResponse()) { + StatusLine status_line = fragment.getStatusLine(); + int code = status_line.getCode(); + String reason = status_line.getReason(); + if (code >= 200 && code < 300) { + printLog("Call successfully transferred", LogLevel.MEDIUM); + if (xcall_listener != null) + xcall_listener.onCallTransferSuccess(this, msg); + } else if (code >= 300) { + printLog("Call NOT transferred", LogLevel.MEDIUM); + if (xcall_listener != null) + xcall_listener.onCallTransferFailure(this, reason, msg); + } + } + } + } + + /** + * When an incoming request is received within the dialog different from + * INVITE, CANCEL, ACK, BYE + */ + public void onDlgAltRequest(org.zoolu.sip.dialog.InviteDialog d, + String method, String body, Message msg) { + } + + /** + * When a response is received for a request within the dialog different + * from INVITE, CANCEL, ACK, BYE + */ + public void onDlgAltResponse(org.zoolu.sip.dialog.InviteDialog d, + String method, int code, String reason, String body, Message msg) { + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log + .println("ExtendedCall: " + str, level + + SipStack.LOG_LEVEL_CALL); + } +} diff --git a/src/org/zoolu/sip/call/ExtendedCallListener.java b/app/src/main/java/org/zoolu/sip/call/ExtendedCallListener.java similarity index 97% rename from src/org/zoolu/sip/call/ExtendedCallListener.java rename to app/src/main/java/org/zoolu/sip/call/ExtendedCallListener.java index 2a7c114..91271ac 100644 --- a/src/org/zoolu/sip/call/ExtendedCallListener.java +++ b/app/src/main/java/org/zoolu/sip/call/ExtendedCallListener.java @@ -1,70 +1,70 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.call; - -/* HSC CHANGES START */ -// import org.zoolu.sip.call.*; -/* HSC CHANGES END */ -import org.zoolu.sip.message.*; -import org.zoolu.sip.address.NameAddress; - -/* HSC CHANGES START */ -// import org.zoolu.sdp.*; -// import java.util.Vector; -/* HSC CHANGES END */ - -/** - * Interface ExtendedCallListener can be implemented to manage exteded SIP calls - * (sipx.call.ExtendedCall). - *

- * Objects of class ExtendedCall use ExtendedCallListener callback methods to - * signal specific call events. - */ -public interface ExtendedCallListener extends CallListener { - /** - * Callback function called when arriving a new REFER method (transfer - * request). - */ - public void onCallTransfer(ExtendedCall call, NameAddress refer_to, - NameAddress refered_by, Message refer); - - /** Callback function called when a call transfer is accepted. */ - public void onCallTransferAccepted(ExtendedCall call, Message resp); - - /** Callback function called when a call transfer is refused. */ - public void onCallTransferRefused(ExtendedCall call, String reason, - Message resp); - - /** Callback function called when a call transfer is successfully completed. */ - public void onCallTransferSuccess(ExtendedCall call, Message notify); - - /** - * Callback function called when a call transfer is NOT sucessfully - * completed. - */ - public void onCallTransferFailure(ExtendedCall call, String reason, - Message notify); - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.call; + +/* HSC CHANGES START */ +// import org.zoolu.sip.call.*; +/* HSC CHANGES END */ +import org.zoolu.sip.message.*; +import org.zoolu.sip.address.NameAddress; + +/* HSC CHANGES START */ +// import org.zoolu.sdp.*; +// import java.util.Vector; +/* HSC CHANGES END */ + +/** + * Interface ExtendedCallListener can be implemented to manage exteded SIP calls + * (sipx.call.ExtendedCall). + *

+ * Objects of class ExtendedCall use ExtendedCallListener callback methods to + * signal specific call events. + */ +public interface ExtendedCallListener extends CallListener { + /** + * Callback function called when arriving a new REFER method (transfer + * request). + */ + public void onCallTransfer(ExtendedCall call, NameAddress refer_to, + NameAddress refered_by, Message refer); + + /** Callback function called when a call transfer is accepted. */ + public void onCallTransferAccepted(ExtendedCall call, Message resp); + + /** Callback function called when a call transfer is refused. */ + public void onCallTransferRefused(ExtendedCall call, String reason, + Message resp); + + /** Callback function called when a call transfer is successfully completed. */ + public void onCallTransferSuccess(ExtendedCall call, Message notify); + + /** + * Callback function called when a call transfer is NOT sucessfully + * completed. + */ + public void onCallTransferFailure(ExtendedCall call, String reason, + Message notify); + +} diff --git a/src/org/zoolu/sip/call/SdpTools.java b/app/src/main/java/org/zoolu/sip/call/SdpTools.java similarity index 97% rename from src/org/zoolu/sip/call/SdpTools.java rename to app/src/main/java/org/zoolu/sip/call/SdpTools.java index d11dbc5..caf4728 100644 --- a/src/org/zoolu/sip/call/SdpTools.java +++ b/app/src/main/java/org/zoolu/sip/call/SdpTools.java @@ -1,155 +1,155 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.call; - -import org.zoolu.sdp.*; -import java.util.Enumeration; -import java.util.Vector; - -/** - * Class SdpTools collects some static methods for managing SDP materials. - */ -public class SdpTools { - /** - * Costructs a new SessionDescriptor from a given SessionDescriptor with - * olny media types and attribute values specified by a MediaDescriptor - * Vector. - *

- * If no attribute is specified for a particular media, all present - * attributes are kept.
- * If no attribute is present for a selected media, the media is kept - * (regardless any sepcified attributes). - * - * @param sdp - * the given SessionDescriptor - * @param m_descs - * Vector of MediaDescriptor with the selecting media types and - * attributes - * @return this SessionDescriptor - */ - /* HSC CHANGES START */ - public static SessionDescriptor sdpMediaProduct(SessionDescriptor sdp, - Vector m_descs) { - Vector new_media = new Vector(); - if (m_descs != null) { - for (Enumeration e = m_descs.elements(); e - .hasMoreElements();) { - MediaDescriptor spec_md = e.nextElement(); - // System.out.print("DEBUG: SDP: sdp_select: - // "+spec_md.toString()); - MediaDescriptor prev_md = sdp.getMediaDescriptor(spec_md - .getMedia().getMedia()); - // System.out.print("DEBUG: SDP: sdp_origin: - // "+prev_md.toString()); - if (prev_md != null) { - Vector spec_attributes = spec_md - .getAttributes(); - Vector prev_attributes = prev_md - .getAttributes(); - MediaField prev_mf = prev_md.getMedia(); - Vector new_formats = new Vector(prev_mf.getFormatList()); - new_formats.retainAll(spec_md.getMedia().getFormatList()); - - if (spec_attributes.size() == 0 - || prev_attributes.size() == 0) { - new_media.addElement(prev_md); - } else { - Vector new_attributes = new Vector(); - for (Enumeration i = spec_attributes - .elements(); i.hasMoreElements();) { - AttributeField spec_attr = i.nextElement(); - String spec_name = spec_attr.getAttributeName(); - String spec_value = spec_attr.getAttributeValue(); - for (Enumeration k = prev_attributes - .elements(); k.hasMoreElements();) { - AttributeField prev_attr = k.nextElement(); - String prev_name = prev_attr.getAttributeName(); - String prev_value = prev_attr - .getAttributeValue(); - if (prev_name.equals(spec_name) - && prev_value - .equalsIgnoreCase(spec_value)) { - new_attributes.addElement(prev_attr); - break; - } - } - } - MediaField new_mf = new MediaField(prev_mf.getMedia(), prev_mf.getPort(), 0, - prev_mf.getTransport(), new_formats); - if (new_attributes.size() > 0) - new_media.addElement(new MediaDescriptor(new_mf, prev_md.getConnection(), - new_attributes)); - else { - if(new_mf.getMedia().startsWith("audio") && new_formats.size() > 0) { - new_media.addElement(new MediaDescriptor(new_mf, prev_md.getConnection(), - new_attributes)); // new_attributes is empty but this is ok here. - } - } - } - } - } - } - SessionDescriptor new_sdp = new SessionDescriptor(sdp); - new_sdp.removeMediaDescriptors(); - new_sdp.addMediaDescriptors(new_media); - return new_sdp; - } - - /* HSC CHANGES END */ - /** - * Costructs a new SessionDescriptor from a given SessionDescriptor with - * olny the first specified media attribute. /** Keeps only the fisrt - * attribute of the specified type for each media. - *

- * If no attribute is present for a media, the media is dropped. - * - * @param sdp - * the given SessionDescriptor - * @param a_name - * the attribute name - * @return this SessionDescriptor - */ - /* HSC CHANGES START */ - public static SessionDescriptor sdpAttirbuteSelection( - SessionDescriptor sdp, String a_name) { - Vector new_media = new Vector(); - for (Enumeration e = sdp.getMediaDescriptors() - .elements(); e.hasMoreElements();) { - /* HSC CHANGES END */ - MediaDescriptor md = e.nextElement(); - AttributeField attr = md.getAttribute(a_name); - if (attr != null) { - new_media.addElement(new MediaDescriptor(md.getMedia(), md - .getConnection(), attr)); - } - } - SessionDescriptor new_sdp = new SessionDescriptor(sdp); - new_sdp.removeMediaDescriptors(); - new_sdp.addMediaDescriptors(new_media); - return new_sdp; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.call; + +import org.zoolu.sdp.*; +import java.util.Enumeration; +import java.util.Vector; + +/** + * Class SdpTools collects some static methods for managing SDP materials. + */ +public class SdpTools { + /** + * Costructs a new SessionDescriptor from a given SessionDescriptor with + * olny media types and attribute values specified by a MediaDescriptor + * Vector. + *

+ * If no attribute is specified for a particular media, all present + * attributes are kept.
+ * If no attribute is present for a selected media, the media is kept + * (regardless any sepcified attributes). + * + * @param sdp + * the given SessionDescriptor + * @param m_descs + * Vector of MediaDescriptor with the selecting media types and + * attributes + * @return this SessionDescriptor + */ + /* HSC CHANGES START */ + public static SessionDescriptor sdpMediaProduct(SessionDescriptor sdp, + Vector m_descs) { + Vector new_media = new Vector(); + if (m_descs != null) { + for (Enumeration e = m_descs.elements(); e + .hasMoreElements();) { + MediaDescriptor spec_md = e.nextElement(); + // System.out.print("DEBUG: SDP: sdp_select: + // "+spec_md.toString()); + MediaDescriptor prev_md = sdp.getMediaDescriptor(spec_md + .getMedia().getMedia()); + // System.out.print("DEBUG: SDP: sdp_origin: + // "+prev_md.toString()); + if (prev_md != null) { + Vector spec_attributes = spec_md + .getAttributes(); + Vector prev_attributes = prev_md + .getAttributes(); + MediaField prev_mf = prev_md.getMedia(); + Vector new_formats = new Vector(prev_mf.getFormatList()); + new_formats.retainAll(spec_md.getMedia().getFormatList()); + + if (spec_attributes.size() == 0 + || prev_attributes.size() == 0) { + new_media.addElement(prev_md); + } else { + Vector new_attributes = new Vector(); + for (Enumeration i = spec_attributes + .elements(); i.hasMoreElements();) { + AttributeField spec_attr = i.nextElement(); + String spec_name = spec_attr.getAttributeName(); + String spec_value = spec_attr.getAttributeValue(); + for (Enumeration k = prev_attributes + .elements(); k.hasMoreElements();) { + AttributeField prev_attr = k.nextElement(); + String prev_name = prev_attr.getAttributeName(); + String prev_value = prev_attr + .getAttributeValue(); + if (prev_name.equals(spec_name) + && prev_value + .equalsIgnoreCase(spec_value)) { + new_attributes.addElement(prev_attr); + break; + } + } + } + MediaField new_mf = new MediaField(prev_mf.getMedia(), prev_mf.getPort(), 0, + prev_mf.getTransport(), new_formats); + if (new_attributes.size() > 0) + new_media.addElement(new MediaDescriptor(new_mf, prev_md.getConnection(), + new_attributes)); + else { + if(new_mf.getMedia().startsWith("audio") && new_formats.size() > 0) { + new_media.addElement(new MediaDescriptor(new_mf, prev_md.getConnection(), + new_attributes)); // new_attributes is empty but this is ok here. + } + } + } + } + } + } + SessionDescriptor new_sdp = new SessionDescriptor(sdp); + new_sdp.removeMediaDescriptors(); + new_sdp.addMediaDescriptors(new_media); + return new_sdp; + } + + /* HSC CHANGES END */ + /** + * Costructs a new SessionDescriptor from a given SessionDescriptor with + * olny the first specified media attribute. /** Keeps only the fisrt + * attribute of the specified type for each media. + *

+ * If no attribute is present for a media, the media is dropped. + * + * @param sdp + * the given SessionDescriptor + * @param a_name + * the attribute name + * @return this SessionDescriptor + */ + /* HSC CHANGES START */ + public static SessionDescriptor sdpAttirbuteSelection( + SessionDescriptor sdp, String a_name) { + Vector new_media = new Vector(); + for (Enumeration e = sdp.getMediaDescriptors() + .elements(); e.hasMoreElements();) { + /* HSC CHANGES END */ + MediaDescriptor md = e.nextElement(); + AttributeField attr = md.getAttribute(a_name); + if (attr != null) { + new_media.addElement(new MediaDescriptor(md.getMedia(), md + .getConnection(), attr)); + } + } + SessionDescriptor new_sdp = new SessionDescriptor(sdp); + new_sdp.removeMediaDescriptors(); + new_sdp.addMediaDescriptors(new_media); + return new_sdp; + } + +} diff --git a/src/org/zoolu/sip/dialog/Dialog.java b/app/src/main/java/org/zoolu/sip/dialog/Dialog.java similarity index 96% rename from src/org/zoolu/sip/dialog/Dialog.java rename to app/src/main/java/org/zoolu/sip/dialog/Dialog.java index 4462a5d..66d6e3b 100644 --- a/src/org/zoolu/sip/dialog/Dialog.java +++ b/app/src/main/java/org/zoolu/sip/dialog/Dialog.java @@ -1,322 +1,322 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.dialog; - -import java.util.Vector; /* HSC CHANGES START */ -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.header.FromHeader; -import org.zoolu.sip.header.Header; -import org.zoolu.sip.header.RecordRouteHeader; -import org.zoolu.sip.header.ToHeader; -import org.zoolu.sip.message.Message; -import org.zoolu.sip.provider.DialogIdentifier; -import org.zoolu.sip.provider.SipProvider; -import org.zoolu.sip.provider.SipProviderListener; -import org.zoolu.sip.provider.SipStack; -import org.zoolu.tools.Log; -import org.zoolu.tools.LogLevel; - -/* HSC CHANGES END */ - -/** - * Class Dialog maintains a complete information status of a generic SIP dialog. - * It has the following attributes: - *

    - *
  • sip-provider
  • - *
  • call-id
  • - *
  • local and remote URLs
  • - *
  • local and remote contact URLs
  • - *
  • local and remote cseqs
  • - *
  • local and remote tags
  • - *
  • dialog-id
  • - *
  • route set
  • - *
- */ -public abstract class Dialog extends DialogInfo implements SipProviderListener { - - // ************************ Static attributes ************************* - - /** Dialogs counter */ - private static int dialog_counter = 0; - - /** Identifier for the transaction client side of a dialog (UAC). */ - public final static int UAC = 0; - /** Identifier for the transaction server side of a dialog (UAS). */ - public final static int UAS = 1; - - // *********************** Protected attributes *********************** - - /** Dialog sequence number */ - protected int dialog_sqn; - - /** Event logger. */ - protected Log log; - - /** SipProvider */ - protected SipProvider sip_provider; - - /** Internal dialog state. */ - protected int status; - - /** Dialog identifier */ - protected DialogIdentifier dialog_id; - - // ************************* Abstract methods ************************* - - /** Gets the dialog state */ - abstract protected String getStatusDescription(); - - abstract protected int getStatus(); - - /** Whether the dialog is in "early" state. */ - abstract public boolean isEarly(); - - /** Whether the dialog is in "confirmed" state. */ - abstract public boolean isConfirmed(); - - /** Whether the dialog is in "terminated" state. */ - abstract public boolean isTerminated(); - - /** When a new Message is received by the SipProvider. */ - abstract public void onReceivedMessage(SipProvider provider, Message message); - - // **************************** Costructors *************************** - - /** Creates a new empty Dialog */ - protected Dialog(SipProvider provider) { - super(); - this.sip_provider = provider; - this.log = sip_provider.getLog(); - this.dialog_sqn = dialog_counter++; - this.status = 0; - this.dialog_id = null; - } - - // ************************* Protected methods ************************ - - /** Changes the internal dialog state */ - protected void changeStatus(int newstatus) { - status = newstatus; - printLog("changed dialog state: " + getStatus(), LogLevel.MEDIUM); - - // remove the sip_provider listener when going to "terminated" state - if (isTerminated()) { - if (dialog_id != null - && sip_provider.getListeners().containsKey(dialog_id)) - sip_provider.removeSipProviderListener(dialog_id); - } else - // add sip_provider listener when going to "early" or "confirmed" state - if (isEarly() || isConfirmed()) { - if (dialog_id != null - && !sip_provider.getListeners().containsKey(dialog_id)) - sip_provider.addSipProviderListener(dialog_id, this); - } - } - - /** Whether the dialog state is equal to st */ - protected boolean statusIs(int st) { - return status == st; - } - - // ************************** Public methods ************************** - - /** Gets the SipProvider of this Dialog. */ - public SipProvider getSipProvider() { - return sip_provider; - } - - /** Gets the inique Dialog-ID */ - public DialogIdentifier getDialogID() { - return dialog_id; - } - - /** - * Updates empty attributes (tags, route set) and mutable attributes (cseqs, - * contacts), based on a new message. - * - * @param side - * indicates whether the Dialog is acting as transaction client - * or server for the current message (use constant values - * Dialog.UAC or Dialog.UAS) - * @param msg - * the message that is used to update the Dialog state - */ - public void update(int side, Message msg) { - if (isTerminated()) { - printWarning("trying to update a terminated dialog: do nothing.", - LogLevel.HIGH); - return; - } - // else - - // update call_id - if (call_id == null) - call_id = msg.getCallIdHeader().getCallId(); - - // update names and tags - if (side == UAC) { - if (remote_name == null || remote_tag == null) { - ToHeader to = msg.getToHeader(); - if (remote_name == null) - remote_name = to.getNameAddress(); - if (remote_tag == null) - remote_tag = to.getTag(); - } - if (local_name == null || local_tag == null) { - FromHeader from = msg.getFromHeader(); - if (local_name == null) - local_name = from.getNameAddress(); - if (local_tag == null) - local_tag = from.getTag(); - } - local_cseq = msg.getCSeqHeader().getSequenceNumber(); - // if (remote_cseq==-1) remote_cseq=SipProvider.pickInitialCSeq()-1; - } else { - if (local_name == null || local_tag == null) { - ToHeader to = msg.getToHeader(); - if (local_name == null) - local_name = to.getNameAddress(); - if (local_tag == null) - local_tag = to.getTag(); - } - if (remote_name == null || remote_tag == null) { - FromHeader from = msg.getFromHeader(); - if (remote_name == null) - remote_name = from.getNameAddress(); - if (remote_tag == null) - remote_tag = from.getTag(); - } - remote_cseq = msg.getCSeqHeader().getSequenceNumber(); - if (local_cseq == -1) - local_cseq = SipProvider.pickInitialCSeq() - 1; - } - // update contact - if (msg.hasContactHeader()) { - if ((side == UAC && msg.isRequest()) - || (side == UAS && msg.isResponse())) - local_contact = msg.getContactHeader().getNameAddress(); - else - remote_contact = msg.getContactHeader().getNameAddress(); - } - // update route or record-route - if (side == UAC) { - if (msg.isRequest() && msg.hasRouteHeader() && route == null) { - /* HSC CHANGES START */ - Vector route_s = msg.getRoutes().getValues(); - route = new Vector(route_s.size()); - int size = route_s.size(); - for (int i = 0; i < size; i++) { - route.insertElementAt( - new NameAddress(route_s.elementAt(i)), i); - } - /* HSC CHANGES END */ - } - if (side == UAC && msg.isResponse() && msg.hasRecordRouteHeader()) { - /* HSC CHANGES START */ - Vector
rr = msg.getRecordRoutes().getHeaders(); - int size = rr.size(); - route = new Vector(size); - /* HSC CHANGES END */ - for (int i = 0; i < size; i++) { - route.insertElementAt((new RecordRouteHeader((Header) rr - .elementAt(size - 1 - i))).getNameAddress(), i); - } - } - } else { - if (msg.isRequest() && msg.hasRouteHeader() && route == null) { - /* HSC CHANGES START */ - Vector reverse_route = msg.getRoutes().getValues(); - int size = reverse_route.size(); - route = new Vector(size); - for (int i = 0; i < size; i++) { - route.insertElementAt(new NameAddress(reverse_route - .elementAt(size - 1 - i)), i); - } - /* HSC CHANGES END */ - } - if (msg.isRequest() && msg.hasRecordRouteHeader()) { - /* HSC CHANGES START */ - Vector
rr = msg.getRecordRoutes().getHeaders(); - int size = rr.size(); - route = new Vector(size); - for (int i = 0; i < size; i++) { - route.insertElementAt((new RecordRouteHeader((Header) rr - .elementAt(i))).getNameAddress(), i); - } - /* HSC CHANGES END */ - } - } - - // update dialog_id and sip_provider listener - DialogIdentifier new_id = new DialogIdentifier(call_id, local_tag, - remote_tag); - if (dialog_id == null || !dialog_id.equals(new_id)) { - if (dialog_id != null && sip_provider != null - && sip_provider.getListeners().containsKey(dialog_id)) - sip_provider.removeSipProviderListener(dialog_id); - dialog_id = new_id; - printLog("new dialog id: " + dialog_id, LogLevel.HIGH); - if (sip_provider != null) - sip_provider.addSipProviderListener(dialog_id, this); - } - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("Dialog#" + dialog_sqn + ": " + str, level - + SipStack.LOG_LEVEL_DIALOG); - } - - /** Adds a Warning message to the default Log */ - protected final void printWarning(String str, int level) { - printLog("WARNING: " + str, level); - } - - /** Adds the Exception message to the default Log */ - protected final void printException(Exception e, int level) { - if (log != null) - log.printException(e, level + SipStack.LOG_LEVEL_DIALOG); - } - - /** Verifies the correct status; if not logs the event. */ - protected final boolean verifyStatus(boolean expression) { - return verifyThat(expression, "dialog state mismatching"); - } - - /** Verifies an event; if not logs it. */ - protected final boolean verifyThat(boolean expression, String str) { - if (!expression) { - if (str == null || str.length() == 0) - printWarning("expression check failed. ", 1); - else - printWarning(str, 1); - } - return expression; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.dialog; + +import java.util.Vector; /* HSC CHANGES START */ +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.header.FromHeader; +import org.zoolu.sip.header.Header; +import org.zoolu.sip.header.RecordRouteHeader; +import org.zoolu.sip.header.ToHeader; +import org.zoolu.sip.message.Message; +import org.zoolu.sip.provider.DialogIdentifier; +import org.zoolu.sip.provider.SipProvider; +import org.zoolu.sip.provider.SipProviderListener; +import org.zoolu.sip.provider.SipStack; +import org.zoolu.tools.Log; +import org.zoolu.tools.LogLevel; + +/* HSC CHANGES END */ + +/** + * Class Dialog maintains a complete information status of a generic SIP dialog. + * It has the following attributes: + *
    + *
  • sip-provider
  • + *
  • call-id
  • + *
  • local and remote URLs
  • + *
  • local and remote contact URLs
  • + *
  • local and remote cseqs
  • + *
  • local and remote tags
  • + *
  • dialog-id
  • + *
  • route set
  • + *
+ */ +public abstract class Dialog extends DialogInfo implements SipProviderListener { + + // ************************ Static attributes ************************* + + /** Dialogs counter */ + private static int dialog_counter = 0; + + /** Identifier for the transaction client side of a dialog (UAC). */ + public final static int UAC = 0; + /** Identifier for the transaction server side of a dialog (UAS). */ + public final static int UAS = 1; + + // *********************** Protected attributes *********************** + + /** Dialog sequence number */ + protected int dialog_sqn; + + /** Event logger. */ + protected Log log; + + /** SipProvider */ + protected SipProvider sip_provider; + + /** Internal dialog state. */ + protected int status; + + /** Dialog identifier */ + protected DialogIdentifier dialog_id; + + // ************************* Abstract methods ************************* + + /** Gets the dialog state */ + abstract protected String getStatusDescription(); + + abstract protected int getStatus(); + + /** Whether the dialog is in "early" state. */ + abstract public boolean isEarly(); + + /** Whether the dialog is in "confirmed" state. */ + abstract public boolean isConfirmed(); + + /** Whether the dialog is in "terminated" state. */ + abstract public boolean isTerminated(); + + /** When a new Message is received by the SipProvider. */ + abstract public void onReceivedMessage(SipProvider provider, Message message); + + // **************************** Costructors *************************** + + /** Creates a new empty Dialog */ + protected Dialog(SipProvider provider) { + super(); + this.sip_provider = provider; + this.log = sip_provider.getLog(); + this.dialog_sqn = dialog_counter++; + this.status = 0; + this.dialog_id = null; + } + + // ************************* Protected methods ************************ + + /** Changes the internal dialog state */ + protected void changeStatus(int newstatus) { + status = newstatus; + printLog("changed dialog state: " + getStatus(), LogLevel.MEDIUM); + + // remove the sip_provider listener when going to "terminated" state + if (isTerminated()) { + if (dialog_id != null + && sip_provider.getListeners().containsKey(dialog_id)) + sip_provider.removeSipProviderListener(dialog_id); + } else + // add sip_provider listener when going to "early" or "confirmed" state + if (isEarly() || isConfirmed()) { + if (dialog_id != null + && !sip_provider.getListeners().containsKey(dialog_id)) + sip_provider.addSipProviderListener(dialog_id, this); + } + } + + /** Whether the dialog state is equal to st */ + protected boolean statusIs(int st) { + return status == st; + } + + // ************************** Public methods ************************** + + /** Gets the SipProvider of this Dialog. */ + public SipProvider getSipProvider() { + return sip_provider; + } + + /** Gets the inique Dialog-ID */ + public DialogIdentifier getDialogID() { + return dialog_id; + } + + /** + * Updates empty attributes (tags, route set) and mutable attributes (cseqs, + * contacts), based on a new message. + * + * @param side + * indicates whether the Dialog is acting as transaction client + * or server for the current message (use constant values + * Dialog.UAC or Dialog.UAS) + * @param msg + * the message that is used to update the Dialog state + */ + public void update(int side, Message msg) { + if (isTerminated()) { + printWarning("trying to update a terminated dialog: do nothing.", + LogLevel.HIGH); + return; + } + // else + + // update call_id + if (call_id == null) + call_id = msg.getCallIdHeader().getCallId(); + + // update names and tags + if (side == UAC) { + if (remote_name == null || remote_tag == null) { + ToHeader to = msg.getToHeader(); + if (remote_name == null) + remote_name = to.getNameAddress(); + if (remote_tag == null) + remote_tag = to.getTag(); + } + if (local_name == null || local_tag == null) { + FromHeader from = msg.getFromHeader(); + if (local_name == null) + local_name = from.getNameAddress(); + if (local_tag == null) + local_tag = from.getTag(); + } + local_cseq = msg.getCSeqHeader().getSequenceNumber(); + // if (remote_cseq==-1) remote_cseq=SipProvider.pickInitialCSeq()-1; + } else { + if (local_name == null || local_tag == null) { + ToHeader to = msg.getToHeader(); + if (local_name == null) + local_name = to.getNameAddress(); + if (local_tag == null) + local_tag = to.getTag(); + } + if (remote_name == null || remote_tag == null) { + FromHeader from = msg.getFromHeader(); + if (remote_name == null) + remote_name = from.getNameAddress(); + if (remote_tag == null) + remote_tag = from.getTag(); + } + remote_cseq = msg.getCSeqHeader().getSequenceNumber(); + if (local_cseq == -1) + local_cseq = SipProvider.pickInitialCSeq() - 1; + } + // update contact + if (msg.hasContactHeader()) { + if ((side == UAC && msg.isRequest()) + || (side == UAS && msg.isResponse())) + local_contact = msg.getContactHeader().getNameAddress(); + else + remote_contact = msg.getContactHeader().getNameAddress(); + } + // update route or record-route + if (side == UAC) { + if (msg.isRequest() && msg.hasRouteHeader() && route == null) { + /* HSC CHANGES START */ + Vector route_s = msg.getRoutes().getValues(); + route = new Vector(route_s.size()); + int size = route_s.size(); + for (int i = 0; i < size; i++) { + route.insertElementAt( + new NameAddress(route_s.elementAt(i)), i); + } + /* HSC CHANGES END */ + } + if (side == UAC && msg.isResponse() && msg.hasRecordRouteHeader()) { + /* HSC CHANGES START */ + Vector
rr = msg.getRecordRoutes().getHeaders(); + int size = rr.size(); + route = new Vector(size); + /* HSC CHANGES END */ + for (int i = 0; i < size; i++) { + route.insertElementAt((new RecordRouteHeader((Header) rr + .elementAt(size - 1 - i))).getNameAddress(), i); + } + } + } else { + if (msg.isRequest() && msg.hasRouteHeader() && route == null) { + /* HSC CHANGES START */ + Vector reverse_route = msg.getRoutes().getValues(); + int size = reverse_route.size(); + route = new Vector(size); + for (int i = 0; i < size; i++) { + route.insertElementAt(new NameAddress(reverse_route + .elementAt(size - 1 - i)), i); + } + /* HSC CHANGES END */ + } + if (msg.isRequest() && msg.hasRecordRouteHeader()) { + /* HSC CHANGES START */ + Vector
rr = msg.getRecordRoutes().getHeaders(); + int size = rr.size(); + route = new Vector(size); + for (int i = 0; i < size; i++) { + route.insertElementAt((new RecordRouteHeader((Header) rr + .elementAt(i))).getNameAddress(), i); + } + /* HSC CHANGES END */ + } + } + + // update dialog_id and sip_provider listener + DialogIdentifier new_id = new DialogIdentifier(call_id, local_tag, + remote_tag); + if (dialog_id == null || !dialog_id.equals(new_id)) { + if (dialog_id != null && sip_provider != null + && sip_provider.getListeners().containsKey(dialog_id)) + sip_provider.removeSipProviderListener(dialog_id); + dialog_id = new_id; + printLog("new dialog id: " + dialog_id, LogLevel.HIGH); + if (sip_provider != null) + sip_provider.addSipProviderListener(dialog_id, this); + } + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("Dialog#" + dialog_sqn + ": " + str, level + + SipStack.LOG_LEVEL_DIALOG); + } + + /** Adds a Warning message to the default Log */ + protected final void printWarning(String str, int level) { + printLog("WARNING: " + str, level); + } + + /** Adds the Exception message to the default Log */ + protected final void printException(Exception e, int level) { + if (log != null) + log.printException(e, level + SipStack.LOG_LEVEL_DIALOG); + } + + /** Verifies the correct status; if not logs the event. */ + protected final boolean verifyStatus(boolean expression) { + return verifyThat(expression, "dialog state mismatching"); + } + + /** Verifies an event; if not logs it. */ + protected final boolean verifyThat(boolean expression, String str) { + if (!expression) { + if (str == null || str.length() == 0) + printWarning("expression check failed. ", 1); + else + printWarning(str, 1); + } + return expression; + } + +} diff --git a/src/org/zoolu/sip/dialog/DialogInfo.java b/app/src/main/java/org/zoolu/sip/dialog/DialogInfo.java similarity index 95% rename from src/org/zoolu/sip/dialog/DialogInfo.java rename to app/src/main/java/org/zoolu/sip/dialog/DialogInfo.java index 49dbdc5..c4c29b8 100644 --- a/src/org/zoolu/sip/dialog/DialogInfo.java +++ b/app/src/main/java/org/zoolu/sip/dialog/DialogInfo.java @@ -1,216 +1,216 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.address.*; -/* HSC CHANGES BEGIN */ -//import org.zoolu.sip.message.*; -//import org.zoolu.sip.header.*; -//import org.zoolu.sip.provider.*; -//import org.zoolu.tools.Log; -//import org.zoolu.tools.LogLevel; -//import org.zoolu.tools.AssertException; -/* HSC CHANGES END */ -import java.util.Vector; - -/** - * Class DialogInfo maintains a complete information status of a generic SIP - * dialog. It has the following attributes: - *
    - *
  • sip-provider
  • - *
  • call-id
  • - *
  • local and remote URLs
  • - *
  • local and remote contact URLs
  • - *
  • local and remote cseqs
  • - *
  • local and remote tags
  • - *
  • dialog-id
  • - *
  • route set
  • - *
- */ -public class DialogInfo { - - // ************************ Private attributes ************************ - - /** Local name */ - NameAddress local_name; - - /** Remote name */ - NameAddress remote_name; - - /** Local contact url */ - NameAddress local_contact; - - /** Remote contact url */ - NameAddress remote_contact; - - /** Call-id */ - String call_id; - - /** Local tag */ - String local_tag; - - /** Remote tag */ - String remote_tag; - /** Sets the remote tag */ - - /** Local CSeq number */ - long local_cseq; - - /** Remote CSeq number */ - long remote_cseq; - - /** Route set (Vector of NameAddresses) */ - /* HSC CHANGES START */ - Vector route; - - /* HSC CHANGES END */ - // **************************** Costructors *************************** - /** Creates a new empty DialogInfo */ - public DialogInfo() { - this.local_name = null; - this.remote_name = null; - this.local_contact = null; - this.remote_contact = null; - this.call_id = null; - this.local_tag = null; - this.remote_tag = null; - this.local_cseq = -1; - this.remote_cseq = -1; - this.route = null; - } - - // ************************** Public methods ************************** - - /** Sets the local name */ - public void setLocalName(NameAddress url) { - local_name = url; - } - - /** Gets the local name */ - public NameAddress getLocalName() { - return local_name; - } - - /** Sets the remote name */ - public void setRemoteName(NameAddress url) { - remote_name = url; - } - - /** Gets the remote name */ - public NameAddress getRemoteName() { - return remote_name; - } - - /** Sets the local contact url */ - public void setLocalContact(NameAddress name_address) { - local_contact = name_address; - } - - /** Gets the local contact url */ - public NameAddress getLocalContact() { - return local_contact; - } - - /** Sets the remote contact url */ - public void setRemoteContact(NameAddress name_address) { - remote_contact = name_address; - } - - /** Gets the remote contact url */ - public NameAddress getRemoteContact() { - return remote_contact; - } - - /** Sets the call-id */ - public void setCallID(String id) { - call_id = id; - } - - /** Gets the call-id */ - public String getCallID() { - return call_id; - } - - /** Sets the local tag */ - public void setLocalTag(String tag) { - local_tag = tag; - } - - /** Gets the local tag */ - public String getLocalTag() { - return local_tag; - } - - public void setRemoteTag(String tag) { - remote_tag = tag; - } - - /** Gets the remote tag */ - public String getRemoteTag() { - return remote_tag; - } - - /** Sets the local CSeq number */ - public void setLocalCSeq(long cseq) { - local_cseq = cseq; - } - - /** Increments the local CSeq number */ - public void incLocalCSeq() { - local_cseq++; - } - - /** Gets the local CSeq number */ - public long getLocalCSeq() { - return local_cseq; - } - - /** Sets the remote CSeq number */ - public void setRemoteCSeq(long cseq) { - remote_cseq = cseq; - } - - /** Increments the remote CSeq number */ - public void incRemoteCSeq() { - remote_cseq++; - } - - /** Gets the remote CSeq number */ - public long getRemoteCSeq() { - return remote_cseq; - } - - /* HSC CHANGES BEGIN */ - /** Sets the route set */ - public void setRoute(Vector r) { - route = r; - } - - /** Gets the route set */ - public Vector getRoute() { - return route; - } - /* HSC CHANGES END */ -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.address.*; +/* HSC CHANGES BEGIN */ +//import org.zoolu.sip.message.*; +//import org.zoolu.sip.header.*; +//import org.zoolu.sip.provider.*; +//import org.zoolu.tools.Log; +//import org.zoolu.tools.LogLevel; +//import org.zoolu.tools.AssertException; +/* HSC CHANGES END */ +import java.util.Vector; + +/** + * Class DialogInfo maintains a complete information status of a generic SIP + * dialog. It has the following attributes: + *
    + *
  • sip-provider
  • + *
  • call-id
  • + *
  • local and remote URLs
  • + *
  • local and remote contact URLs
  • + *
  • local and remote cseqs
  • + *
  • local and remote tags
  • + *
  • dialog-id
  • + *
  • route set
  • + *
+ */ +public class DialogInfo { + + // ************************ Private attributes ************************ + + /** Local name */ + NameAddress local_name; + + /** Remote name */ + NameAddress remote_name; + + /** Local contact url */ + NameAddress local_contact; + + /** Remote contact url */ + NameAddress remote_contact; + + /** Call-id */ + String call_id; + + /** Local tag */ + String local_tag; + + /** Remote tag */ + String remote_tag; + /** Sets the remote tag */ + + /** Local CSeq number */ + long local_cseq; + + /** Remote CSeq number */ + long remote_cseq; + + /** Route set (Vector of NameAddresses) */ + /* HSC CHANGES START */ + Vector route; + + /* HSC CHANGES END */ + // **************************** Costructors *************************** + /** Creates a new empty DialogInfo */ + public DialogInfo() { + this.local_name = null; + this.remote_name = null; + this.local_contact = null; + this.remote_contact = null; + this.call_id = null; + this.local_tag = null; + this.remote_tag = null; + this.local_cseq = -1; + this.remote_cseq = -1; + this.route = null; + } + + // ************************** Public methods ************************** + + /** Sets the local name */ + public void setLocalName(NameAddress url) { + local_name = url; + } + + /** Gets the local name */ + public NameAddress getLocalName() { + return local_name; + } + + /** Sets the remote name */ + public void setRemoteName(NameAddress url) { + remote_name = url; + } + + /** Gets the remote name */ + public NameAddress getRemoteName() { + return remote_name; + } + + /** Sets the local contact url */ + public void setLocalContact(NameAddress name_address) { + local_contact = name_address; + } + + /** Gets the local contact url */ + public NameAddress getLocalContact() { + return local_contact; + } + + /** Sets the remote contact url */ + public void setRemoteContact(NameAddress name_address) { + remote_contact = name_address; + } + + /** Gets the remote contact url */ + public NameAddress getRemoteContact() { + return remote_contact; + } + + /** Sets the call-id */ + public void setCallID(String id) { + call_id = id; + } + + /** Gets the call-id */ + public String getCallID() { + return call_id; + } + + /** Sets the local tag */ + public void setLocalTag(String tag) { + local_tag = tag; + } + + /** Gets the local tag */ + public String getLocalTag() { + return local_tag; + } + + public void setRemoteTag(String tag) { + remote_tag = tag; + } + + /** Gets the remote tag */ + public String getRemoteTag() { + return remote_tag; + } + + /** Sets the local CSeq number */ + public void setLocalCSeq(long cseq) { + local_cseq = cseq; + } + + /** Increments the local CSeq number */ + public void incLocalCSeq() { + local_cseq++; + } + + /** Gets the local CSeq number */ + public long getLocalCSeq() { + return local_cseq; + } + + /** Sets the remote CSeq number */ + public void setRemoteCSeq(long cseq) { + remote_cseq = cseq; + } + + /** Increments the remote CSeq number */ + public void incRemoteCSeq() { + remote_cseq++; + } + + /** Gets the remote CSeq number */ + public long getRemoteCSeq() { + return remote_cseq; + } + + /* HSC CHANGES BEGIN */ + /** Sets the route set */ + public void setRoute(Vector r) { + route = r; + } + + /** Gets the route set */ + public Vector getRoute() { + return route; + } + /* HSC CHANGES END */ +} diff --git a/src/org/zoolu/sip/dialog/ExtendedInviteDialog.java b/app/src/main/java/org/zoolu/sip/dialog/ExtendedInviteDialog.java similarity index 97% rename from src/org/zoolu/sip/dialog/ExtendedInviteDialog.java rename to app/src/main/java/org/zoolu/sip/dialog/ExtendedInviteDialog.java index 6c2a805..09878f2 100644 --- a/src/org/zoolu/sip/dialog/ExtendedInviteDialog.java +++ b/app/src/main/java/org/zoolu/sip/dialog/ExtendedInviteDialog.java @@ -1,241 +1,241 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.header.StatusLine; -import org.zoolu.sip.header.RequestLine; -import org.zoolu.sip.header.AuthorizationHeader; -import org.zoolu.sip.header.ViaHeader; -import org.zoolu.sip.header.WwwAuthenticateHeader; -import org.zoolu.sip.transaction.*; -import org.zoolu.sip.message.*; -import org.zoolu.sip.authentication.DigestAuthentication; -import org.zoolu.tools.LogLevel; - -import java.util.Hashtable; - -/** - * Class ExtendedInviteDialog can be used to manage extended invite dialogs. - *

- * An ExtendedInviteDialog allows the user:
- to handle authentication
- - * to handle refer/notify
- to capture all methods within the dialog - */ -public class ExtendedInviteDialog extends org.zoolu.sip.dialog.InviteDialog { - /** Max number of registration attempts. */ - static final int MAX_ATTEMPTS = 3; - - /** ExtendedInviteDialog listener. */ - ExtendedInviteDialogListener dialog_listener; - - /** Acive transactions. */ - /* HSC CHANGES START */ - Hashtable transactions; - /* HSC CHANGES END */ - /** User name. */ - String username; - - /** User name. */ - String realm; - - /** User's passwd. */ - String passwd; - - /** Nonce for the next authentication. */ - String next_nonce; - - /** Qop for the next authentication. */ - String qop; - - /** Number of authentication attempts. */ - int attempts; - - /** Creates a new ExtendedInviteDialog. */ - public ExtendedInviteDialog(SipProvider provider, - ExtendedInviteDialogListener listener) { - super(provider, listener); - init(listener); - } - - /** Creates a new ExtendedInviteDialog. */ - public ExtendedInviteDialog(SipProvider provider, String username, - String realm, String passwd, ExtendedInviteDialogListener listener) { - super(provider, listener); - init(listener); - this.username = username; - this.realm = realm; - this.passwd = passwd; - } - - /** Inits the ExtendedInviteDialog. */ - private void init(ExtendedInviteDialogListener listener) { - this.dialog_listener = listener; - this.transactions = new Hashtable(); - this.username = null; - this.realm = null; - this.passwd = null; - this.next_nonce = null; - this.qop = null; - this.attempts = 0; - } - - /** Sends a new request within the dialog */ - public void request(Message req) { - TransactionClient t = new TransactionClient(sip_provider, req, this); - transactions.put(t.getTransactionId(), t); - t.request(); - } - - /** Sends a new REFER within the dialog */ - public void refer(NameAddress refer_to) { - refer(refer_to, null); - } - - public void info(char c, int duration) // modified (again by Matthew Monacelli) - { - Message req = BaseMessageFactory.createRequest(this, SipMethods.INFO, null); - req.setBody("application/dtmf-relay","Signal="+c+"\r\n+Duration="+duration); - request(req); - } - - /** Sends a new REFER within the dialog */ - public void refer(NameAddress refer_to, NameAddress referred_by) { - Message req = MessageFactory.createReferRequest(this, refer_to, - referred_by); - request(req); - } - - /** Sends a new NOTIFY within the dialog */ - public void notify(int code, String reason) { - notify((new StatusLine(code, reason)).toString()); - } - - /** Sends a new NOTIFY within the dialog */ - public void notify(String sipfragment) { - Message req = MessageFactory.createNotifyRequest(this, "refer", null, - sipfragment); - request(req); - } - - /** Responds with resp */ - public void respond(Message resp) { - printLog("inside respond(resp)", LogLevel.MEDIUM); - String method = resp.getCSeqHeader().getMethod(); - if (method.equals(SipMethods.INVITE) - || method.equals(SipMethods.CANCEL) - || method.equals(SipMethods.BYE)) { - super.respond(resp); - } else { - TransactionIdentifier transaction_id = resp.getTransactionId(); - printLog("transaction-id=" + transaction_id, LogLevel.MEDIUM); - if (transactions.containsKey(transaction_id)) { - printLog("responding", LogLevel.LOW); - TransactionServer t = (TransactionServer) transactions - .get(transaction_id); - t.respondWith(resp); - } else - printLog("transaction server not found; message discarded", - LogLevel.MEDIUM); - } - } - - /** Accept a REFER */ - public void acceptRefer(Message req) { - printLog("inside acceptRefer(refer)", LogLevel.MEDIUM); - Message resp = MessageFactory.createResponse(req, 202, SipResponses - .reasonOf(200), null); - respond(resp); - } - - /** Refuse a REFER */ - public void refuseRefer(Message req) { - printLog("inside refuseRefer(refer)", LogLevel.MEDIUM); - Message resp = MessageFactory.createResponse(req, 603, SipResponses - .reasonOf(603), null); - respond(resp); - } - - /** Inherited from class SipProviderListener. */ - public void onReceivedMessage(SipProvider provider, Message msg) { - printLog( - "Message received: " - + msg.getFirstLine().substring(0, - msg.toString().indexOf('\r')), LogLevel.LOW); - if (msg.isResponse()) { - super.onReceivedMessage(provider, msg); - } else if (msg.isInvite() || msg.isAck() || msg.isCancel() - || msg.isBye()) { - super.onReceivedMessage(provider, msg); - } else { - TransactionServer t = new TransactionServer(sip_provider, msg, this); - transactions.put(t.getTransactionId(), t); - // t.listen(); - - if (msg.isRefer()) { // Message - // resp=MessageFactory.createResponse(msg,202,"Accepted",null,null); - // respond(resp); - NameAddress refer_to = msg.getReferToHeader().getNameAddress(); - NameAddress referred_by = null; - if (msg.hasReferredByHeader()) - referred_by = msg.getReferredByHeader().getNameAddress(); - dialog_listener.onDlgRefer(this, refer_to, referred_by, msg); - } else if (msg.isNotify()) { - Message resp = MessageFactory.createResponse(msg, 200, - SipResponses.reasonOf(200), null); - respond(resp); - String event = msg.getEventHeader().getValue(); - String sipfragment = msg.getBody(); - dialog_listener.onDlgNotify(this, event, sipfragment, msg); - } else { - printLog("Received alternative request " - + msg.getRequestLine().getMethod(), LogLevel.MEDIUM); - dialog_listener.onDlgAltRequest(this, msg.getRequestLine() - .getMethod(), msg.getBody(), msg); - } - } - } - - /** - * Inherited from TransactionClientListener. When the - * TransactionClientListener goes into the "Completed" state, receiving a - * failure response - */ - public void onTransFailureResponse(TransactionClient tc, Message msg) { - printLog("inside onTransFailureResponse(" + tc.getTransactionId() - + ",msg)", LogLevel.LOW); - String method = tc.getTransactionMethod(); - StatusLine status_line = msg.getStatusLine(); - int code = status_line.getCode(); - String reason = status_line.getReason(); +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.header.StatusLine; +import org.zoolu.sip.header.RequestLine; +import org.zoolu.sip.header.AuthorizationHeader; +import org.zoolu.sip.header.ViaHeader; +import org.zoolu.sip.header.WwwAuthenticateHeader; +import org.zoolu.sip.transaction.*; +import org.zoolu.sip.message.*; +import org.zoolu.sip.authentication.DigestAuthentication; +import org.zoolu.tools.LogLevel; + +import java.util.Hashtable; + +/** + * Class ExtendedInviteDialog can be used to manage extended invite dialogs. + *

+ * An ExtendedInviteDialog allows the user:
- to handle authentication
- + * to handle refer/notify
- to capture all methods within the dialog + */ +public class ExtendedInviteDialog extends org.zoolu.sip.dialog.InviteDialog { + /** Max number of registration attempts. */ + static final int MAX_ATTEMPTS = 3; + + /** ExtendedInviteDialog listener. */ + ExtendedInviteDialogListener dialog_listener; + + /** Acive transactions. */ + /* HSC CHANGES START */ + Hashtable transactions; + /* HSC CHANGES END */ + /** User name. */ + String username; + + /** User name. */ + String realm; + + /** User's passwd. */ + String passwd; + + /** Nonce for the next authentication. */ + String next_nonce; + + /** Qop for the next authentication. */ + String qop; + + /** Number of authentication attempts. */ + int attempts; + + /** Creates a new ExtendedInviteDialog. */ + public ExtendedInviteDialog(SipProvider provider, + ExtendedInviteDialogListener listener) { + super(provider, listener); + init(listener); + } + + /** Creates a new ExtendedInviteDialog. */ + public ExtendedInviteDialog(SipProvider provider, String username, + String realm, String passwd, ExtendedInviteDialogListener listener) { + super(provider, listener); + init(listener); + this.username = username; + this.realm = realm; + this.passwd = passwd; + } + + /** Inits the ExtendedInviteDialog. */ + private void init(ExtendedInviteDialogListener listener) { + this.dialog_listener = listener; + this.transactions = new Hashtable(); + this.username = null; + this.realm = null; + this.passwd = null; + this.next_nonce = null; + this.qop = null; + this.attempts = 0; + } + + /** Sends a new request within the dialog */ + public void request(Message req) { + TransactionClient t = new TransactionClient(sip_provider, req, this); + transactions.put(t.getTransactionId(), t); + t.request(); + } + + /** Sends a new REFER within the dialog */ + public void refer(NameAddress refer_to) { + refer(refer_to, null); + } + + public void info(char c, int duration) // modified (again by Matthew Monacelli) + { + Message req = BaseMessageFactory.createRequest(this, SipMethods.INFO, null); + req.setBody("application/dtmf-relay","Signal="+c+"\r\n+Duration="+duration); + request(req); + } + + /** Sends a new REFER within the dialog */ + public void refer(NameAddress refer_to, NameAddress referred_by) { + Message req = MessageFactory.createReferRequest(this, refer_to, + referred_by); + request(req); + } + + /** Sends a new NOTIFY within the dialog */ + public void notify(int code, String reason) { + notify((new StatusLine(code, reason)).toString()); + } + + /** Sends a new NOTIFY within the dialog */ + public void notify(String sipfragment) { + Message req = MessageFactory.createNotifyRequest(this, "refer", null, + sipfragment); + request(req); + } + + /** Responds with resp */ + public void respond(Message resp) { + printLog("inside respond(resp)", LogLevel.MEDIUM); + String method = resp.getCSeqHeader().getMethod(); + if (method.equals(SipMethods.INVITE) + || method.equals(SipMethods.CANCEL) + || method.equals(SipMethods.BYE)) { + super.respond(resp); + } else { + TransactionIdentifier transaction_id = resp.getTransactionId(); + printLog("transaction-id=" + transaction_id, LogLevel.MEDIUM); + if (transactions.containsKey(transaction_id)) { + printLog("responding", LogLevel.LOW); + TransactionServer t = (TransactionServer) transactions + .get(transaction_id); + t.respondWith(resp); + } else + printLog("transaction server not found; message discarded", + LogLevel.MEDIUM); + } + } + + /** Accept a REFER */ + public void acceptRefer(Message req) { + printLog("inside acceptRefer(refer)", LogLevel.MEDIUM); + Message resp = MessageFactory.createResponse(req, 202, SipResponses + .reasonOf(200), null); + respond(resp); + } + + /** Refuse a REFER */ + public void refuseRefer(Message req) { + printLog("inside refuseRefer(refer)", LogLevel.MEDIUM); + Message resp = MessageFactory.createResponse(req, 603, SipResponses + .reasonOf(603), null); + respond(resp); + } + + /** Inherited from class SipProviderListener. */ + public void onReceivedMessage(SipProvider provider, Message msg) { + printLog( + "Message received: " + + msg.getFirstLine().substring(0, + msg.toString().indexOf('\r')), LogLevel.LOW); + if (msg.isResponse()) { + super.onReceivedMessage(provider, msg); + } else if (msg.isInvite() || msg.isAck() || msg.isCancel() + || msg.isBye()) { + super.onReceivedMessage(provider, msg); + } else { + TransactionServer t = new TransactionServer(sip_provider, msg, this); + transactions.put(t.getTransactionId(), t); + // t.listen(); + + if (msg.isRefer()) { // Message + // resp=MessageFactory.createResponse(msg,202,"Accepted",null,null); + // respond(resp); + NameAddress refer_to = msg.getReferToHeader().getNameAddress(); + NameAddress referred_by = null; + if (msg.hasReferredByHeader()) + referred_by = msg.getReferredByHeader().getNameAddress(); + dialog_listener.onDlgRefer(this, refer_to, referred_by, msg); + } else if (msg.isNotify()) { + Message resp = MessageFactory.createResponse(msg, 200, + SipResponses.reasonOf(200), null); + respond(resp); + String event = msg.getEventHeader().getValue(); + String sipfragment = msg.getBody(); + dialog_listener.onDlgNotify(this, event, sipfragment, msg); + } else { + printLog("Received alternative request " + + msg.getRequestLine().getMethod(), LogLevel.MEDIUM); + dialog_listener.onDlgAltRequest(this, msg.getRequestLine() + .getMethod(), msg.getBody(), msg); + } + } + } + + /** + * Inherited from TransactionClientListener. When the + * TransactionClientListener goes into the "Completed" state, receiving a + * failure response + */ + public void onTransFailureResponse(TransactionClient tc, Message msg) { + printLog("inside onTransFailureResponse(" + tc.getTransactionId() + + ",msg)", LogLevel.LOW); + String method = tc.getTransactionMethod(); + StatusLine status_line = msg.getStatusLine(); + int code = status_line.getCode(); + String reason = status_line.getReason(); boolean isErr401 = false; boolean isErr407 = false; - - // AUTHENTICATION-BEGIN + + // AUTHENTICATION-BEGIN if (attempts < MAX_ATTEMPTS) { switch (code) { case 401: @@ -254,105 +254,105 @@ public void onTransFailureResponse(TransactionClient tc, Message msg) { } if (isErr401 | isErr407) { - attempts++; - Message req = tc.getRequestMessage(); - req.setCSeqHeader(req.getCSeqHeader().incSequenceNumber()); - ViaHeader vh=req.getViaHeader(); - String newbranch = SipProvider.pickBranch(); - vh.setBranch(newbranch); - req.removeViaHeader(); - - req.addViaHeader(vh); - WwwAuthenticateHeader wah; - if (code == 401) - wah = msg.getWwwAuthenticateHeader(); - else - wah = msg.getProxyAuthenticateHeader(); - String qop_options = wah.getQopOptionsParam(); - qop = (qop_options != null) ? "auth" : null; - RequestLine rl = req.getRequestLine(); - DigestAuthentication digest = new DigestAuthentication(rl - .getMethod(), rl.getAddress().toString(), wah, qop, null, - username, passwd); - AuthorizationHeader ah; - if (code == 401) - ah = digest.getAuthorizationHeader(); - else - ah = digest.getProxyAuthorizationHeader(); - req.setAuthorizationHeader(ah); - transactions.remove(tc.getTransactionId()); - tc = new TransactionClient(sip_provider, req, this); - transactions.put(tc.getTransactionId(), tc); - tc.request(); - invite_req = req; // modified - } else - // AUTHENTICATION-END - if (method.equals(SipMethods.INVITE) - || method.equals(SipMethods.CANCEL) - || method.equals(SipMethods.BYE)) { - super.onTransFailureResponse(tc, msg); - } else if (tc.getTransactionMethod().equals(SipMethods.REFER)) { - transactions.remove(tc.getTransactionId()); - dialog_listener.onDlgReferResponse(this, code, reason, msg); - } else { - String body = msg.getBody(); - transactions.remove(tc.getTransactionId()); - dialog_listener.onDlgAltResponse(this, method, code, reason, body, - msg); - } - } - - /** - * Inherited from TransactionClientListener. When an - * TransactionClientListener goes into the "Terminated" state, receiving a - * 2xx response - */ - public void onTransSuccessResponse(TransactionClient t, Message msg) { - printLog("inside onTransSuccessResponse(" + t.getTransactionId() - + ",msg)", LogLevel.LOW); - attempts = 0; - String method = t.getTransactionMethod(); - StatusLine status_line = msg.getStatusLine(); - int code = status_line.getCode(); - String reason = status_line.getReason(); - - if (method.equals(SipMethods.INVITE) - || method.equals(SipMethods.CANCEL) - || method.equals(SipMethods.BYE)) { - super.onTransSuccessResponse(t, msg); - } else if (t.getTransactionMethod().equals(SipMethods.REFER)) { - transactions.remove(t.getTransactionId()); - dialog_listener.onDlgReferResponse(this, code, reason, msg); - } else { - String body = msg.getBody(); - transactions.remove(t.getTransactionId()); - dialog_listener.onDlgAltResponse(this, method, code, reason, body, - msg); - } - } - - /** - * Inherited from TransactionClientListener. When the TransactionClient goes - * into the "Terminated" state, caused by transaction timeout - */ - public void onTransTimeout(TransactionClient t) { - printLog("inside onTransTimeout(" + t.getTransactionId() + ",msg)", - LogLevel.LOW); - String method = t.getTransactionMethod(); - if (method.equals(SipMethods.INVITE) || method.equals(SipMethods.BYE)) { - super.onTransTimeout(t); - } else { // do something.. - transactions.remove(t.getTransactionId()); - } - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("ExtendedInviteDialog#" + dialog_sqn + ": " + str, - level + SipStack.LOG_LEVEL_DIALOG); - } - -} + attempts++; + Message req = tc.getRequestMessage(); + req.setCSeqHeader(req.getCSeqHeader().incSequenceNumber()); + ViaHeader vh=req.getViaHeader(); + String newbranch = SipProvider.pickBranch(); + vh.setBranch(newbranch); + req.removeViaHeader(); + + req.addViaHeader(vh); + WwwAuthenticateHeader wah; + if (code == 401) + wah = msg.getWwwAuthenticateHeader(); + else + wah = msg.getProxyAuthenticateHeader(); + String qop_options = wah.getQopOptionsParam(); + qop = (qop_options != null) ? "auth" : null; + RequestLine rl = req.getRequestLine(); + DigestAuthentication digest = new DigestAuthentication(rl + .getMethod(), rl.getAddress().toString(), wah, qop, null, + username, passwd); + AuthorizationHeader ah; + if (code == 401) + ah = digest.getAuthorizationHeader(); + else + ah = digest.getProxyAuthorizationHeader(); + req.setAuthorizationHeader(ah); + transactions.remove(tc.getTransactionId()); + tc = new TransactionClient(sip_provider, req, this); + transactions.put(tc.getTransactionId(), tc); + tc.request(); + invite_req = req; // modified + } else + // AUTHENTICATION-END + if (method.equals(SipMethods.INVITE) + || method.equals(SipMethods.CANCEL) + || method.equals(SipMethods.BYE)) { + super.onTransFailureResponse(tc, msg); + } else if (tc.getTransactionMethod().equals(SipMethods.REFER)) { + transactions.remove(tc.getTransactionId()); + dialog_listener.onDlgReferResponse(this, code, reason, msg); + } else { + String body = msg.getBody(); + transactions.remove(tc.getTransactionId()); + dialog_listener.onDlgAltResponse(this, method, code, reason, body, + msg); + } + } + + /** + * Inherited from TransactionClientListener. When an + * TransactionClientListener goes into the "Terminated" state, receiving a + * 2xx response + */ + public void onTransSuccessResponse(TransactionClient t, Message msg) { + printLog("inside onTransSuccessResponse(" + t.getTransactionId() + + ",msg)", LogLevel.LOW); + attempts = 0; + String method = t.getTransactionMethod(); + StatusLine status_line = msg.getStatusLine(); + int code = status_line.getCode(); + String reason = status_line.getReason(); + + if (method.equals(SipMethods.INVITE) + || method.equals(SipMethods.CANCEL) + || method.equals(SipMethods.BYE)) { + super.onTransSuccessResponse(t, msg); + } else if (t.getTransactionMethod().equals(SipMethods.REFER)) { + transactions.remove(t.getTransactionId()); + dialog_listener.onDlgReferResponse(this, code, reason, msg); + } else { + String body = msg.getBody(); + transactions.remove(t.getTransactionId()); + dialog_listener.onDlgAltResponse(this, method, code, reason, body, + msg); + } + } + + /** + * Inherited from TransactionClientListener. When the TransactionClient goes + * into the "Terminated" state, caused by transaction timeout + */ + public void onTransTimeout(TransactionClient t) { + printLog("inside onTransTimeout(" + t.getTransactionId() + ",msg)", + LogLevel.LOW); + String method = t.getTransactionMethod(); + if (method.equals(SipMethods.INVITE) || method.equals(SipMethods.BYE)) { + super.onTransTimeout(t); + } else { // do something.. + transactions.remove(t.getTransactionId()); + } + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("ExtendedInviteDialog#" + dialog_sqn + ": " + str, + level + SipStack.LOG_LEVEL_DIALOG); + } + +} diff --git a/src/org/zoolu/sip/dialog/ExtendedInviteDialogListener.java b/app/src/main/java/org/zoolu/sip/dialog/ExtendedInviteDialogListener.java similarity index 97% rename from src/org/zoolu/sip/dialog/ExtendedInviteDialogListener.java rename to app/src/main/java/org/zoolu/sip/dialog/ExtendedInviteDialogListener.java index d5afcd3..ff4e706 100644 --- a/src/org/zoolu/sip/dialog/ExtendedInviteDialogListener.java +++ b/app/src/main/java/org/zoolu/sip/dialog/ExtendedInviteDialogListener.java @@ -1,72 +1,72 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.message.Message; - -/** - * An ExtendedInviteDialogListener listens for ExtendedInviteDialog events. It - * extends InviteDialogListener by adding ExtendedInviteDialog-specific callback - * functions. - */ -public interface ExtendedInviteDialogListener extends - org.zoolu.sip.dialog.InviteDialogListener { - - /** When an incoming REFER request is received within the dialog */ - public void onDlgRefer(org.zoolu.sip.dialog.InviteDialog dialog, - NameAddress refer_to, NameAddress referred_by, Message msg); - - /** When a response is received for a REFER request within the dialog */ - public void onDlgReferResponse(org.zoolu.sip.dialog.InviteDialog dialog, - int code, String reason, Message msg); - - /** When an incoming NOTIFY request is received within the dialog */ - public void onDlgNotify(org.zoolu.sip.dialog.InviteDialog dialog, - String event, String sipfragment, Message msg); - - /** When a response is received for a NOTIFY request within the dialog */ - // public void onDlgNotifyResponse(org.zoolu.sip.dialog.InviteDialog dialog, - // int code, String reason, Message msg); - /** - * When an incoming request is received within the dialog different from - * INVITE, CANCEL, ACK, BYE, REFER, NOTIFY - */ - public void onDlgAltRequest(org.zoolu.sip.dialog.InviteDialog dialog, - String method, String body, Message msg); - - /** - * When a response is received for a request within the dialog different - * from INVITE, CANCEL, ACK, BYE, REFER, NOTIFY - */ - public void onDlgAltResponse(org.zoolu.sip.dialog.InviteDialog dialog, - String method, int code, String reason, String body, Message msg); - - /** - * When a request timeout expires within the dialog different from INVITE, - * CANCEL, ACK, BYE, REFER, NOTIFY - */ - // public void onDlgAltTimeout(org.zoolu.sip.dialog.InviteDialog dialog, - // String method); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.message.Message; + +/** + * An ExtendedInviteDialogListener listens for ExtendedInviteDialog events. It + * extends InviteDialogListener by adding ExtendedInviteDialog-specific callback + * functions. + */ +public interface ExtendedInviteDialogListener extends + org.zoolu.sip.dialog.InviteDialogListener { + + /** When an incoming REFER request is received within the dialog */ + public void onDlgRefer(org.zoolu.sip.dialog.InviteDialog dialog, + NameAddress refer_to, NameAddress referred_by, Message msg); + + /** When a response is received for a REFER request within the dialog */ + public void onDlgReferResponse(org.zoolu.sip.dialog.InviteDialog dialog, + int code, String reason, Message msg); + + /** When an incoming NOTIFY request is received within the dialog */ + public void onDlgNotify(org.zoolu.sip.dialog.InviteDialog dialog, + String event, String sipfragment, Message msg); + + /** When a response is received for a NOTIFY request within the dialog */ + // public void onDlgNotifyResponse(org.zoolu.sip.dialog.InviteDialog dialog, + // int code, String reason, Message msg); + /** + * When an incoming request is received within the dialog different from + * INVITE, CANCEL, ACK, BYE, REFER, NOTIFY + */ + public void onDlgAltRequest(org.zoolu.sip.dialog.InviteDialog dialog, + String method, String body, Message msg); + + /** + * When a response is received for a request within the dialog different + * from INVITE, CANCEL, ACK, BYE, REFER, NOTIFY + */ + public void onDlgAltResponse(org.zoolu.sip.dialog.InviteDialog dialog, + String method, int code, String reason, String body, Message msg); + + /** + * When a request timeout expires within the dialog different from INVITE, + * CANCEL, ACK, BYE, REFER, NOTIFY + */ + // public void onDlgAltTimeout(org.zoolu.sip.dialog.InviteDialog dialog, + // String method); +} diff --git a/src/org/zoolu/sip/dialog/InviteDialog.java b/app/src/main/java/org/zoolu/sip/dialog/InviteDialog.java similarity index 96% rename from src/org/zoolu/sip/dialog/InviteDialog.java rename to app/src/main/java/org/zoolu/sip/dialog/InviteDialog.java index f1ef3d9..9484e0f 100644 --- a/src/org/zoolu/sip/dialog/InviteDialog.java +++ b/app/src/main/java/org/zoolu/sip/dialog/InviteDialog.java @@ -1,902 +1,902 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.transaction.*; -import org.zoolu.sip.message.*; -import org.zoolu.sip.header.*; -import org.zoolu.sip.provider.*; -import org.zoolu.tools.LogLevel; - -/** - * Class InviteDialog can be used to manage invite dialogs. An InviteDialog can - * be both client or server. (i.e. generating an INVITE request or responding to - * an incoming INVITE request). - *

- * An InviteDialog can be in state inviting/waiting/invited, accepted/refused, - * call, byed/byeing, and close. - *

- * InviteDialog supports the offer/answer model for the sip body, with the - * following rules:
- both INVITE-offer/2xx-answer and 2xx-offer/ACK-answer - * modes for incoming calls
- INVITE-offer/2xx-answer mode for outgoing - * calls. - */ -public class InviteDialog extends Dialog implements TransactionClientListener, - InviteTransactionServerListener, AckTransactionServerListener, - SipProviderListener { - /** The last invite message */ - Message invite_req; - /** The last ack message */ - Message ack_req; - - /** The InviteTransactionServer. */ - InviteTransactionServer invite_ts; - /** The AckTransactionServer. */ - AckTransactionServer ack_ts; - /** The BYE TransactionServer. */ - TransactionServer bye_ts; - - /** The InviteDialog listener */ - InviteDialogListener listener; - - /** Whether offer/answer are in INVITE/200_OK */ - boolean invite_offer; - - protected static final int D_INIT = 0; - protected static final int D_WAITING = 1; - protected static final int D_INVITING = 2; - protected static final int D_INVITED = 3; - protected static final int D_REFUSED = 4; - protected static final int D_ACCEPTED = 5; - protected static final int D_CALL = 6; - - protected static final int D_ReWAITING = 11; - protected static final int D_ReINVITING = 12; - protected static final int D_ReINVITED = 13; - protected static final int D_ReREFUSED = 14; - protected static final int D_ReACCEPTED = 15; - - protected static final int D_BYEING = 7; - protected static final int D_BYED = 8; - protected static final int D_CLOSE = 9; - - /** Gets the dialog state */ - protected String getStatusDescription() { - switch (status) { - case D_INIT: - return "D_INIT"; - case D_WAITING: - return "D_WAITING"; - case D_INVITING: - return "D_INVITING"; - case D_INVITED: - return "D_INVITED"; - case D_REFUSED: - return "D_REFUSED"; - case D_ACCEPTED: - return "D_ACCEPTED"; - case D_CALL: - return "D_CALL"; - case D_ReWAITING: - return "D_ReWAITING"; - case D_ReINVITING: - return "D_ReINVITING"; - case D_ReINVITED: - return "D_ReINVITED"; - case D_ReREFUSED: - return "D_ReREFUSED"; - case D_ReACCEPTED: - return "D_ReACCEPTED"; - case D_BYEING: - return "D_BYEING"; - case D_BYED: - return "D_BYED"; - case D_CLOSE: - return "D_CLOSE"; - default: - return null; - } - } - - protected int getStatus() - { - return status; - } - - // ************************** Public methods ************************** - - /** Whether the dialog is in "early" state. */ - public boolean isEarly() { - return status < D_ACCEPTED; - } - - /** Whether the dialog is in "confirmed" state. */ - public boolean isConfirmed() { - return status >= D_ACCEPTED && status < D_CLOSE; - } - - /** Whether the dialog is in "terminated" state. */ - public boolean isTerminated() { - return status == D_CLOSE; - } - - /** Whether the session is "active". */ - public boolean isSessionActive() { - return (status == D_CALL); - } - - /** Gets the invite message */ - public Message getInviteMessage() { - return invite_req; - } - - /** Creates a new InviteDialog. */ - public InviteDialog(SipProvider sip_provider, InviteDialogListener listener) { - super(sip_provider); - init(listener); - } - - /** - * Creates a new InviteDialog for the already received INVITE request - * invite. - */ - public InviteDialog(SipProvider sip_provider, Message invite, - InviteDialogListener listener) { - super(sip_provider); - init(listener); - - changeStatus(D_INVITED); - invite_req = invite; - invite_ts = new InviteTransactionServer(sip_provider, invite_req, this); - update(Dialog.UAS, invite_req); - } - - /** Inits the InviteDialog. */ - private void init(InviteDialogListener listener) { - log = sip_provider.getLog(); - this.listener = listener; - this.invite_req = null; - this.ack_req = null; - this.invite_offer = true; - changeStatus(D_INIT); - } - - /** Starts a new InviteTransactionServer. */ - public void listen() { - if (!statusIs(D_INIT)) - return; - // else - changeStatus(D_WAITING); - invite_ts = new InviteTransactionServer(sip_provider, this); - invite_ts.listen(); - } - - /** - * Starts a new InviteTransactionClient and initializes the dialog state - * information. - * - * @param callee - * the callee url (and display name) - * @param caller - * the caller url (and display name) - * @param contact - * the contact url OR the contact username - * @param session_descriptor - * SDP body - * @param icsi - * the ICSI for this session - * - */ - public void invite(String callee, String caller, String contact, - String session_descriptor, String icsi) { // modified by mandrajg - printLog("inside invite(callee,caller,contact,sdp)", LogLevel.MEDIUM); - if (!statusIs(D_INIT)) - return; - // else - NameAddress to_url = new NameAddress(callee); - NameAddress from_url = new NameAddress(caller); - SipURL request_uri = to_url.getAddress(); - - NameAddress contact_url = null; - if (contact != null) { - if (contact.indexOf("sip:") >= 0) - contact_url = new NameAddress(contact); - else - contact_url = new NameAddress(new SipURL(contact, sip_provider - .getViaAddress(), sip_provider.getPort())); - } else - contact_url = from_url; - - Message invite = MessageFactory.createInviteRequest(sip_provider, - request_uri, to_url, from_url, contact_url, session_descriptor, icsi); // modified by mandrajg - // do invite - invite(invite); - } - - /** - * Starts a new InviteTransactionClient and initializes the dialog state - * information - * - * @param invite - * the INVITE message - */ - public void invite(Message invite) { - printLog("inside invite(invite)", LogLevel.MEDIUM); - if (!statusIs(D_INIT)) - return; - // else - changeStatus(D_INVITING); - invite_req = invite; - update(Dialog.UAC, invite_req); - InviteTransactionClient invite_tc = new InviteTransactionClient( - sip_provider, invite_req, this); - invite_tc.request(); - } - - /** - * Starts a new InviteTransactionClient with offer/answer in 2xx/ack and - * initializes the dialog state information - */ - public void inviteWithoutOffer(String callee, String caller, String contact) { - invite_offer = false; - invite(callee, caller, contact, null, null); // modified by mandrajg - } - - /** - * Starts a new InviteTransactionClient with offer/answer in 2xx/ack and - * initializes the dialog state information - */ - public void inviteWithoutOffer(Message invite) { - invite_offer = false; - invite(invite); - } - - /** - * Re-invites the remote user. - *

- * Starts a new InviteTransactionClient and changes the dialog state - * information - *

- * Parameters:
- contact : the contact url OR the contact username; if - * null, the previous contact is used
- session_descriptor : the - * message body - */ - public void reInvite(String contact, String session_descriptor) { - printLog("inside reInvite(contact,sdp)", LogLevel.MEDIUM); - if (!statusIs(D_CALL)) - return; - // else - Message invite = MessageFactory.createInviteRequest(this, - session_descriptor); - if (contact != null) { - NameAddress contact_url; - if (contact.indexOf("sip:") >= 0) - contact_url = new NameAddress(contact); - else - contact_url = new NameAddress(new SipURL(contact, sip_provider - .getViaAddress(), sip_provider.getPort())); - invite.setContactHeader(new ContactHeader(contact_url)); - } - reInvite(invite); - } - - /** - * Re-invites the remote user. - *

- * Starts a new InviteTransactionClient and changes the dialog state - * information - */ - public void reInvite(Message invite) { - printLog("inside reInvite(invite)", LogLevel.MEDIUM); - if (!statusIs(D_CALL)) - return; - // else - changeStatus(D_ReINVITING); - invite_req = invite; - update(Dialog.UAC, invite_req); - InviteTransactionClient invite_tc = new InviteTransactionClient( - sip_provider, invite_req, this); - invite_tc.request(); - } - - /** - * Re-invites the remote user with offer/answer in 2xx/ack - *

- * Starts a new InviteTransactionClient and changes the dialog state - * information - */ - public void reInviteWithoutOffer(Message invite) { - invite_offer = false; - reInvite(invite); - } - - /** - * Re-invites the remote user with offer/answer in 2xx/ack - *

- * Starts a new InviteTransactionClient and changes the dialog state - * information - */ - public void reInviteWithoutOffer(String contact, String session_descriptor) { - invite_offer = false; - reInvite(contact, session_descriptor); - } - - /** Sends the ack when offer/answer is in 2xx/ack */ - public void ackWithAnswer(String contact, String session_descriptor) { - if (contact != null) - setLocalContact(new NameAddress(contact)); - Message ack = MessageFactory.create2xxAckRequest(this, - session_descriptor); - ackWithAnswer(ack); - } - - /** Sends the ack when offer/answer is in 2xx/ack */ - public void ackWithAnswer(Message ack) { - ack_req = ack; - // reset the offer/answer flag to the default value - invite_offer = true; - AckTransactionClient ack_tc = new AckTransactionClient(sip_provider, - ack, null); - ack_tc.request(); - } - - /** - * Responds with resp. This method can be called when the - * InviteDialog is in D_INVITED or D_BYED states. - *

- * If the CSeq method is INVITE and the response is 2xx, it moves to state - * D_ACCEPTED, adds a new listener to the SipProviderListener, and creates - * new AckTransactionServer - *

- * If the CSeq method is INVITE and the response is not 2xx, it moves to - * state D_REFUSED, and sends the response. - */ - public void respond(Message resp) - // private void respond(Message resp) - { - printLog("inside respond(resp)", LogLevel.MEDIUM); - String method = resp.getCSeqHeader().getMethod(); - if (method.equals(SipMethods.INVITE)) { - if (!verifyStatus(statusIs(D_INVITED) || statusIs(D_ReINVITED))) { - printLog( - "respond(): InviteDialog not in (re)invited state: No response now", - LogLevel.HIGH); - return; - } - - int code = resp.getStatusLine().getCode(); - // 1xx provisional responses - if (code >= 100 && code < 200) { - invite_ts.respondWith(resp); - return; - } - // For all final responses establish the dialog - if (code >= 200) { // changeStatus(D_ACCEPTED); - update(Dialog.UAS, resp); - } - // 2xx success responses - if (code >= 200 && code < 300) { - if (statusIs(D_INVITED)) - changeStatus(D_ACCEPTED); - else - changeStatus(D_ReACCEPTED); - // terminates the INVITE Transaction server and activates an ACK - // Transaction server - invite_ts.terminate(); - ConnectionIdentifier conn_id = invite_ts.getConnectionId(); - ack_ts = new AckTransactionServer(sip_provider, conn_id, resp, - this); - ack_ts.respond(); - // if (statusIs(D_ReACCEPTED)) - // listener.onDlgReInviteAccepted(this); - // else listener.onDlgAccepted(this); - return; - } else - // 300-699 failure responses - // if (code>=300) - { - if (statusIs(D_INVITED)) - changeStatus(D_REFUSED); - else - changeStatus(D_ReREFUSED); - invite_ts.respondWith(resp); - // if (statusIs(D_ReREFUSED)) - // listener.onDlgReInviteRefused(this); - // else listener.onDlgRefused(this); - return; - } - } - if (method.equals(SipMethods.BYE)) { - if (!verifyStatus(statusIs(D_BYED))) - return; - bye_ts.respondWith(resp); - } - } - - /** - * Responds with code and reason. This method can be called - * when the InviteDialog is in D_INVITED, D_ReINVITED states - */ - public void respond(int code, String reason, String contact, String sdp) { - printLog("inside respond(" + code + "," + reason + ")", LogLevel.MEDIUM); - if (statusIs(D_INVITED) || statusIs(D_ReINVITED)) { - NameAddress contact_address = null; - if (contact != null) - contact_address = new NameAddress(contact); - Message resp = MessageFactory.createResponse(invite_req, code, - reason, contact_address); - resp.setBody(sdp); - respond(resp); - } else - printWarning("Dialog isn't in \"invited\" state: cannot respond (" - + code + "/" + getStatus() + "/" + getDialogID() + ")", - LogLevel.MEDIUM); - } - - /** - * Signals that the phone is ringing. This method should be called when the - * InviteDialog is in D_INVITED or D_ReINVITED state - */ - public void ring(String sdp) { // modified - printLog("inside ring()", LogLevel.MEDIUM); - respond(180, SipResponses.reasonOf(180), null, sdp); - } - - /** - * Accepts the incoming call. This method should be called when the - * InviteDialog is in D_INVITED or D_ReINVITED state - */ - public void accept(String contact, String sdp) { - printLog("inside accept(sdp)", LogLevel.MEDIUM); - respond(200, SipResponses.reasonOf(200), contact, sdp); - } - - /** - * Refuses the incoming call. This method should be called when the - * InviteDialog is in D_INVITED or D_ReINVITED state - */ - public void refuse(int code, String reason) { - printLog("inside refuse(" + code + "," + reason + ")", LogLevel.MEDIUM); - respond(code, reason, null, null); - } - - /** - * Refuses the incoming call. This method should be called when the - * InviteDialog is in D_INVITED or D_ReINVITED state - */ - public void refuse() { - printLog("inside refuse()", LogLevel.MEDIUM); - // refuse(480,"Temporarily Unavailable"); - // refuse(603,"Decline"); - refuse(403, SipResponses.reasonOf(403)); - } - - public void busy() { - refuse(486, SipResponses.reasonOf(486)); // modified - } - - /** - * Termiante the call. This method should be called when the InviteDialog is - * in D_CALL state - *

- * Increments the Cseq, moves to state D_BYEING, and creates new BYE - * TransactionClient - */ - public void bye() { - printLog("inside bye()", LogLevel.MEDIUM); - if (statusIs(D_CALL)) { - Message bye = MessageFactory.createByeRequest(this); - bye(bye); - } - } - - /** - * Termiante the call. This method should be called when the InviteDialog is - * in D_CALL state - *

- * Increments the Cseq, moves to state D_BYEING, and creates new BYE - * TransactionClient - */ - public void bye(Message bye) { - printLog("inside bye(bye)", LogLevel.MEDIUM); - if (statusIs(D_CALL)) { - changeStatus(D_BYEING); - // dialog_state.incLocalCSeq(); // done by - // MessageFactory.createRequest() - TransactionClient tc = new TransactionClient(sip_provider, bye, - this); - tc.request(); - // listener.onDlgByeing(this); - } - } - - /** - * Cancel the ongoing call request or a call listening. This method should - * be called when the InviteDialog is in D_INVITING or D_ReINVITING state or - * in the D_WAITING state - */ - public void cancel() { - printLog("inside cancel()", LogLevel.MEDIUM); - if (statusIs(D_INVITING) || statusIs(D_ReINVITING)) { - Message cancel = MessageFactory.createCancelRequest(invite_req,this); // modified - cancel(cancel); - } else if (statusIs(D_WAITING) || statusIs(D_ReWAITING)) { - invite_ts.terminate(); - } - } - - /** - * Cancel the ongoing call request or a call listening. This method should - * be called when the InviteDialog is in D_INVITING or D_ReINVITING state or - * in the D_WAITING state - */ - public void cancel(Message cancel) { - printLog("inside cancel(cancel)", LogLevel.MEDIUM); - if (statusIs(D_INVITING) || statusIs(D_ReINVITING)) { // changeStatus(D_CANCELING); - TransactionClient tc = new TransactionClient(sip_provider, cancel, - null); - tc.request(); - } else if (statusIs(D_WAITING) || statusIs(D_ReWAITING)) { - invite_ts.terminate(); - } - } - - /** - * Redirects the incoming call , specifing the code and reason. - * This method can be called when the InviteDialog is in D_INVITED or - * D_ReINVITED state - */ - public void redirect(int code, String reason, String contact) { - printLog( - "inside redirect(" + code + "," + reason + "," + contact + ")", - LogLevel.MEDIUM); - respond(code, reason, contact, null); - } - - // ************** Inherited from SipProviderListener ************** - - /** - * Inherited from class SipProviderListener. Called when a new message is - * received (out of any ongoing transaction) for the current InviteDialog. - * Always checks for out-of-date methods (CSeq header sequence number). - *

- * If the message is ACK(2xx/INVITE) request, it moves to D_CALL state, and - * fires onDlgAck(this,body,msg). - *

- * If the message is 2xx(INVITE) response, it create a new - * AckTransactionClient - *

- * If the message is BYE, it moves to D_BYED state, removes the listener - * from SipProvider, fires onDlgBye(this,msg) then it responds with 200 OK, - * moves to D_CLOSE state and fires onDlgClose(this) - */ - public void onReceivedMessage(SipProvider sip_provider, Message msg) { - printLog("inside onReceivedMessage(sip_provider,message)", - LogLevel.MEDIUM); - if (msg.isRequest() && !(msg.isAck() || msg.isCancel()) - && msg.getCSeqHeader().getSequenceNumber() <= getRemoteCSeq()) { - printLog( - "Request message is too late (CSeq too small): Message discarded", - LogLevel.HIGH); - return; - } - // invite received - if (msg.isRequest() && msg.isInvite()) { - verifyStatus(statusIs(D_INIT) || statusIs(D_CALL)); - // NOTE: if the invite_ts.listen() is used, you should not arrive - // here with the D_INIT state.. - // however state D_INIT has been included for robustness against - // further changes. - if (statusIs(D_INIT)) - changeStatus(D_INVITED); - else - changeStatus(D_ReINVITED); - invite_req = msg; - invite_ts = new InviteTransactionServer(sip_provider, invite_req, - this); - // ((TransactionServer)transaction).listen(); - update(Dialog.UAS, invite_req); - if (statusIs(D_INVITED)) - listener.onDlgInvite(this, invite_req.getToHeader() - .getNameAddress(), invite_req.getFromHeader() - .getNameAddress(), invite_req.getBody(), invite_req); - else - listener.onDlgReInvite(this, invite_req.getBody(), invite_req); - } else - // ack (of 2xx of INVITE) - if (msg.isRequest() && msg.isAck()) { - if (!verifyStatus(statusIs(D_ACCEPTED) || statusIs(D_ReACCEPTED))) - return; - changeStatus(D_CALL); - // terminates the AckTransactionServer - ack_ts.terminate(); - listener.onDlgAck(this, msg.getBody(), msg); - listener.onDlgCall(this); - } else - // keep sending ACK (if already sent) for any "200 OK" received - if (msg.isResponse()) { - if (!verifyStatus(statusIs(D_CALL))) - return; - int code = msg.getStatusLine().getCode(); - verifyThat(code >= 200 && code < 300, "code 2xx was expected"); - if (ack_req != null) { - AckTransactionClient ack_tc = new AckTransactionClient( - sip_provider, ack_req, null); - ack_tc.request(); - } - } else - // bye received - if (msg.isRequest() && msg.isBye()) { - if (!verifyStatus(statusIs(D_CALL) || statusIs(D_BYEING))) - return; - changeStatus(D_BYED); - bye_ts = new TransactionServer(sip_provider, msg, this); - // automatically sends a 200 OK - Message resp = MessageFactory.createResponse(msg, 200, SipResponses - .reasonOf(200), null); - respond(resp); - listener.onDlgBye(this, msg); - changeStatus(D_CLOSE); - listener.onDlgClose(this); - } else - // cancel received - if (msg.isRequest() && msg.isCancel()) { - if (!verifyStatus(statusIs(D_INVITED) || statusIs(D_ReINVITED))) - return; - // create a CANCEL TransactionServer and send a 200 OK (CANCEL) - TransactionServer ts = new TransactionServer(sip_provider, msg, - null); - // ts.listen(); - ts.respondWith(MessageFactory.createResponse(msg, 200, SipResponses - .reasonOf(200), null)); - // automatically sends a 487 Cancelled - Message resp = MessageFactory.createResponse(invite_req, 487, - SipResponses.reasonOf(487), null); - respond(resp); - listener.onDlgCancel(this, msg); - } else - // any other request received - if (msg.isRequest()) { - TransactionServer ts = new TransactionServer(sip_provider, msg, - null); - // ts.listen(); - ts.respondWith(MessageFactory.createResponse(msg, 405, SipResponses - .reasonOf(405), null)); - } - } - - // ************** Inherited from InviteTransactionClientListener - // ************** - - /** - * Inherited from TransactionClientListener. When the - * TransactionClientListener is in "Proceeding" state and receives a new 1xx - * response - *

- * For INVITE transaction it fires - * onFailureResponse(this,code,reason,body,msg). - */ - public void onTransProvisionalResponse(TransactionClient tc, Message msg) { - printLog("inside onTransProvisionalResponse(tc,mdg)", LogLevel.LOW); - if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { - StatusLine statusline = msg.getStatusLine(); - listener.onDlgInviteProvisionalResponse(this, statusline.getCode(), - statusline.getReason(), msg.getBody(), msg); - } - } - - /** - * Inherited from TransactionClientListener. When the - * TransactionClientListener goes into the "Completed" state, receiving a - * failure response - *

- * If called for a INVITE transaction, it moves to D_CLOSE state, removes - * the listener from SipProvider. - *

- * If called for a BYE transaction, it moves to D_CLOSE state, removes the - * listener from SipProvider, and fires onClose(this,msg). - */ - public void onTransFailureResponse(TransactionClient tc, Message msg) { - printLog("inside onTransFailureResponse(" + tc.getTransactionId() - + ",msg)", LogLevel.LOW); - if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { - if (!verifyStatus(statusIs(D_INVITING) || statusIs(D_ReINVITING))) - return; - StatusLine statusline = msg.getStatusLine(); - int code = statusline.getCode(); - verifyThat(code >= 300 && code < 700, "error code was expected"); - if (statusIs(D_ReINVITING)) { - changeStatus(D_CALL); - listener.onDlgReInviteFailureResponse(this, code, statusline - .getReason(), msg); - } else { - changeStatus(D_CLOSE); - if (code >= 300 && code < 400) - listener.onDlgInviteRedirectResponse(this, code, statusline - .getReason(), msg.getContacts(), msg); - else - listener.onDlgInviteFailureResponse(this, code, statusline - .getReason(), msg); - listener.onDlgClose(this); - } - } else if (tc.getTransactionMethod().equals(SipMethods.BYE)) { - if (!verifyStatus(statusIs(D_BYEING))) - return; - StatusLine statusline = msg.getStatusLine(); - int code = statusline.getCode(); - verifyThat(code >= 300 && code < 700, "error code was expected"); - changeStatus(InviteDialog.D_CALL); - listener.onDlgByeFailureResponse(this, code, - statusline.getReason(), msg); - } - } - - /** - * Inherited from TransactionClientListener. When an - * TransactionClientListener goes into the "Terminated" state, receiving a - * 2xx response - *

- * If called for a INVITE transaction, it updates the dialog information, - * moves to D_CALL state, add a listener to the SipProvider, creates a new - * AckTransactionClient(ack,this), and fires - * onSuccessResponse(this,code,body,msg). - *

- * If called for a BYE transaction, it moves to D_CLOSE state, removes the - * listener from SipProvider, and fires onClose(this,msg). - */ - public void onTransSuccessResponse(TransactionClient tc, Message msg) { - printLog("inside onTransSuccessResponse(tc,msg)", LogLevel.LOW); - if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { - if (!verifyStatus(statusIs(D_INVITING) || statusIs(D_ReINVITING))) - return; - StatusLine statusline = msg.getStatusLine(); - int code = statusline.getCode(); - if (!verifyThat(code >= 200 && code < 300 - && msg.getTransactionMethod().equals(SipMethods.INVITE), - "2xx for invite was expected")) - return; - boolean re_inviting = statusIs(D_ReINVITING); - changeStatus(D_CALL); - update(Dialog.UAC, msg); - if (invite_offer) { // invite_req=MessageFactory.createRequest(SipMethods.ACK,dialog_state,sdp.toString()); - // ack=MessageFactory.createRequest(this,SipMethods.ACK,null); - ack_req = MessageFactory.create2xxAckRequest(this, null); - AckTransactionClient ack_tc = new AckTransactionClient( - sip_provider, ack_req, null); - ack_tc.request(); - } - if (!re_inviting) { - listener.onDlgInviteSuccessResponse(this, code, statusline - .getReason(), msg.getBody(), msg); - listener.onDlgCall(this); - } else - listener.onDlgReInviteSuccessResponse(this, code, statusline - .getReason(), msg.getBody(), msg); - } else if (tc.getTransactionMethod().equals(SipMethods.BYE)) { - if (!verifyStatus(statusIs(D_BYEING))) - return; - StatusLine statusline = msg.getStatusLine(); - int code = statusline.getCode(); - verifyThat(code >= 200 && code < 300, "2xx for bye was expected"); - changeStatus(D_CLOSE); - listener.onDlgByeSuccessResponse(this, code, - statusline.getReason(), msg); - listener.onDlgClose(this); - } - } - - /** - * Inherited from TransactionClientListener. When the TransactionClient goes - * into the "Terminated" state, caused by transaction timeout - */ - public void onTransTimeout(TransactionClient tc) { - printLog("inside onTransTimeout(tc,msg)", LogLevel.LOW); - if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { - if (!verifyStatus(statusIs(D_INVITING) || statusIs(D_ReINVITING))) - return; - cancel(); //modified - changeStatus(D_CLOSE); - listener.onDlgTimeout(this); - listener.onDlgClose(this); - } else if (tc.getTransactionMethod().equals(SipMethods.BYE)) { - if (!verifyStatus(statusIs(D_BYEING))) - return; - changeStatus(D_CLOSE); - listener.onDlgClose(this); - } - } - - // ************** Inherited from InviteTransactionServerListener - // ************** - - /** - * Inherited from TransactionServerListener. When the TransactionServer goes - * into the "Trying" state receiving a request - *

- * If called for a INVITE transaction, it initializes the dialog - * information,
- * moves to D_INVITED state, and add a listener to the SipProvider,
- * and fires onInvite(caller,body,msg). - */ - public void onTransRequest(TransactionServer ts, Message req) { - printLog("inside onTransRequest(ts,msg)", LogLevel.LOW); - if (ts.getTransactionMethod().equals(SipMethods.INVITE)) { - if (!verifyStatus(statusIs(D_WAITING))) - return; - changeStatus(D_INVITED); - invite_req = req; - update(Dialog.UAS, invite_req); - listener.onDlgInvite(this, invite_req.getToHeader() - .getNameAddress(), invite_req.getFromHeader() - .getNameAddress(), invite_req.getBody(), invite_req); - } - } - - /** - * Inherited from InviteTransactionServerListener. When an - * InviteTransactionServer goes into the "Confirmed" state receining an ACK - * for NON-2xx response - *

- * It moves to D_CLOSE state and removes the listener from SipProvider. - */ - public void onTransFailureAck(InviteTransactionServer ts, Message msg) { - printLog("inside onTransFailureAck(ts,msg)", LogLevel.LOW); - if (!verifyStatus(statusIs(D_REFUSED) || statusIs(D_ReREFUSED))) - return; - if (statusIs(D_ReREFUSED)) { - changeStatus(D_CALL); - } else { - changeStatus(D_CLOSE); - listener.onDlgClose(this); - } - } - - // ************ Inherited from AckTransactionServerListener ************ - - /** - * When the AckTransactionServer goes into the "Terminated" state, caused by - * transaction timeout - */ - public void onTransAckTimeout(AckTransactionServer ts) { - printLog("inside onAckSrvTimeout(ts)", LogLevel.LOW); - if (!verifyStatus(statusIs(D_ACCEPTED) || statusIs(D_ReACCEPTED) - || statusIs(D_REFUSED) || statusIs(D_ReREFUSED))) - return; - printLog("No ACK received..", LogLevel.HIGH); - changeStatus(D_CLOSE); - listener.onDlgClose(this); - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("InviteDialog#" + dialog_sqn + ": " + str, level - + SipStack.LOG_LEVEL_DIALOG); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.transaction.*; +import org.zoolu.sip.message.*; +import org.zoolu.sip.header.*; +import org.zoolu.sip.provider.*; +import org.zoolu.tools.LogLevel; + +/** + * Class InviteDialog can be used to manage invite dialogs. An InviteDialog can + * be both client or server. (i.e. generating an INVITE request or responding to + * an incoming INVITE request). + *

+ * An InviteDialog can be in state inviting/waiting/invited, accepted/refused, + * call, byed/byeing, and close. + *

+ * InviteDialog supports the offer/answer model for the sip body, with the + * following rules:
- both INVITE-offer/2xx-answer and 2xx-offer/ACK-answer + * modes for incoming calls
- INVITE-offer/2xx-answer mode for outgoing + * calls. + */ +public class InviteDialog extends Dialog implements TransactionClientListener, + InviteTransactionServerListener, AckTransactionServerListener, + SipProviderListener { + /** The last invite message */ + Message invite_req; + /** The last ack message */ + Message ack_req; + + /** The InviteTransactionServer. */ + InviteTransactionServer invite_ts; + /** The AckTransactionServer. */ + AckTransactionServer ack_ts; + /** The BYE TransactionServer. */ + TransactionServer bye_ts; + + /** The InviteDialog listener */ + InviteDialogListener listener; + + /** Whether offer/answer are in INVITE/200_OK */ + boolean invite_offer; + + protected static final int D_INIT = 0; + protected static final int D_WAITING = 1; + protected static final int D_INVITING = 2; + protected static final int D_INVITED = 3; + protected static final int D_REFUSED = 4; + protected static final int D_ACCEPTED = 5; + protected static final int D_CALL = 6; + + protected static final int D_ReWAITING = 11; + protected static final int D_ReINVITING = 12; + protected static final int D_ReINVITED = 13; + protected static final int D_ReREFUSED = 14; + protected static final int D_ReACCEPTED = 15; + + protected static final int D_BYEING = 7; + protected static final int D_BYED = 8; + protected static final int D_CLOSE = 9; + + /** Gets the dialog state */ + protected String getStatusDescription() { + switch (status) { + case D_INIT: + return "D_INIT"; + case D_WAITING: + return "D_WAITING"; + case D_INVITING: + return "D_INVITING"; + case D_INVITED: + return "D_INVITED"; + case D_REFUSED: + return "D_REFUSED"; + case D_ACCEPTED: + return "D_ACCEPTED"; + case D_CALL: + return "D_CALL"; + case D_ReWAITING: + return "D_ReWAITING"; + case D_ReINVITING: + return "D_ReINVITING"; + case D_ReINVITED: + return "D_ReINVITED"; + case D_ReREFUSED: + return "D_ReREFUSED"; + case D_ReACCEPTED: + return "D_ReACCEPTED"; + case D_BYEING: + return "D_BYEING"; + case D_BYED: + return "D_BYED"; + case D_CLOSE: + return "D_CLOSE"; + default: + return null; + } + } + + protected int getStatus() + { + return status; + } + + // ************************** Public methods ************************** + + /** Whether the dialog is in "early" state. */ + public boolean isEarly() { + return status < D_ACCEPTED; + } + + /** Whether the dialog is in "confirmed" state. */ + public boolean isConfirmed() { + return status >= D_ACCEPTED && status < D_CLOSE; + } + + /** Whether the dialog is in "terminated" state. */ + public boolean isTerminated() { + return status == D_CLOSE; + } + + /** Whether the session is "active". */ + public boolean isSessionActive() { + return (status == D_CALL); + } + + /** Gets the invite message */ + public Message getInviteMessage() { + return invite_req; + } + + /** Creates a new InviteDialog. */ + public InviteDialog(SipProvider sip_provider, InviteDialogListener listener) { + super(sip_provider); + init(listener); + } + + /** + * Creates a new InviteDialog for the already received INVITE request + * invite. + */ + public InviteDialog(SipProvider sip_provider, Message invite, + InviteDialogListener listener) { + super(sip_provider); + init(listener); + + changeStatus(D_INVITED); + invite_req = invite; + invite_ts = new InviteTransactionServer(sip_provider, invite_req, this); + update(Dialog.UAS, invite_req); + } + + /** Inits the InviteDialog. */ + private void init(InviteDialogListener listener) { + log = sip_provider.getLog(); + this.listener = listener; + this.invite_req = null; + this.ack_req = null; + this.invite_offer = true; + changeStatus(D_INIT); + } + + /** Starts a new InviteTransactionServer. */ + public void listen() { + if (!statusIs(D_INIT)) + return; + // else + changeStatus(D_WAITING); + invite_ts = new InviteTransactionServer(sip_provider, this); + invite_ts.listen(); + } + + /** + * Starts a new InviteTransactionClient and initializes the dialog state + * information. + * + * @param callee + * the callee url (and display name) + * @param caller + * the caller url (and display name) + * @param contact + * the contact url OR the contact username + * @param session_descriptor + * SDP body + * @param icsi + * the ICSI for this session + * + */ + public void invite(String callee, String caller, String contact, + String session_descriptor, String icsi) { // modified by mandrajg + printLog("inside invite(callee,caller,contact,sdp)", LogLevel.MEDIUM); + if (!statusIs(D_INIT)) + return; + // else + NameAddress to_url = new NameAddress(callee); + NameAddress from_url = new NameAddress(caller); + SipURL request_uri = to_url.getAddress(); + + NameAddress contact_url = null; + if (contact != null) { + if (contact.indexOf("sip:") >= 0) + contact_url = new NameAddress(contact); + else + contact_url = new NameAddress(new SipURL(contact, sip_provider + .getViaAddress(), sip_provider.getPort())); + } else + contact_url = from_url; + + Message invite = MessageFactory.createInviteRequest(sip_provider, + request_uri, to_url, from_url, contact_url, session_descriptor, icsi); // modified by mandrajg + // do invite + invite(invite); + } + + /** + * Starts a new InviteTransactionClient and initializes the dialog state + * information + * + * @param invite + * the INVITE message + */ + public void invite(Message invite) { + printLog("inside invite(invite)", LogLevel.MEDIUM); + if (!statusIs(D_INIT)) + return; + // else + changeStatus(D_INVITING); + invite_req = invite; + update(Dialog.UAC, invite_req); + InviteTransactionClient invite_tc = new InviteTransactionClient( + sip_provider, invite_req, this); + invite_tc.request(); + } + + /** + * Starts a new InviteTransactionClient with offer/answer in 2xx/ack and + * initializes the dialog state information + */ + public void inviteWithoutOffer(String callee, String caller, String contact) { + invite_offer = false; + invite(callee, caller, contact, null, null); // modified by mandrajg + } + + /** + * Starts a new InviteTransactionClient with offer/answer in 2xx/ack and + * initializes the dialog state information + */ + public void inviteWithoutOffer(Message invite) { + invite_offer = false; + invite(invite); + } + + /** + * Re-invites the remote user. + *

+ * Starts a new InviteTransactionClient and changes the dialog state + * information + *

+ * Parameters:
- contact : the contact url OR the contact username; if + * null, the previous contact is used
- session_descriptor : the + * message body + */ + public void reInvite(String contact, String session_descriptor) { + printLog("inside reInvite(contact,sdp)", LogLevel.MEDIUM); + if (!statusIs(D_CALL)) + return; + // else + Message invite = MessageFactory.createInviteRequest(this, + session_descriptor); + if (contact != null) { + NameAddress contact_url; + if (contact.indexOf("sip:") >= 0) + contact_url = new NameAddress(contact); + else + contact_url = new NameAddress(new SipURL(contact, sip_provider + .getViaAddress(), sip_provider.getPort())); + invite.setContactHeader(new ContactHeader(contact_url)); + } + reInvite(invite); + } + + /** + * Re-invites the remote user. + *

+ * Starts a new InviteTransactionClient and changes the dialog state + * information + */ + public void reInvite(Message invite) { + printLog("inside reInvite(invite)", LogLevel.MEDIUM); + if (!statusIs(D_CALL)) + return; + // else + changeStatus(D_ReINVITING); + invite_req = invite; + update(Dialog.UAC, invite_req); + InviteTransactionClient invite_tc = new InviteTransactionClient( + sip_provider, invite_req, this); + invite_tc.request(); + } + + /** + * Re-invites the remote user with offer/answer in 2xx/ack + *

+ * Starts a new InviteTransactionClient and changes the dialog state + * information + */ + public void reInviteWithoutOffer(Message invite) { + invite_offer = false; + reInvite(invite); + } + + /** + * Re-invites the remote user with offer/answer in 2xx/ack + *

+ * Starts a new InviteTransactionClient and changes the dialog state + * information + */ + public void reInviteWithoutOffer(String contact, String session_descriptor) { + invite_offer = false; + reInvite(contact, session_descriptor); + } + + /** Sends the ack when offer/answer is in 2xx/ack */ + public void ackWithAnswer(String contact, String session_descriptor) { + if (contact != null) + setLocalContact(new NameAddress(contact)); + Message ack = MessageFactory.create2xxAckRequest(this, + session_descriptor); + ackWithAnswer(ack); + } + + /** Sends the ack when offer/answer is in 2xx/ack */ + public void ackWithAnswer(Message ack) { + ack_req = ack; + // reset the offer/answer flag to the default value + invite_offer = true; + AckTransactionClient ack_tc = new AckTransactionClient(sip_provider, + ack, null); + ack_tc.request(); + } + + /** + * Responds with resp. This method can be called when the + * InviteDialog is in D_INVITED or D_BYED states. + *

+ * If the CSeq method is INVITE and the response is 2xx, it moves to state + * D_ACCEPTED, adds a new listener to the SipProviderListener, and creates + * new AckTransactionServer + *

+ * If the CSeq method is INVITE and the response is not 2xx, it moves to + * state D_REFUSED, and sends the response. + */ + public void respond(Message resp) + // private void respond(Message resp) + { + printLog("inside respond(resp)", LogLevel.MEDIUM); + String method = resp.getCSeqHeader().getMethod(); + if (method.equals(SipMethods.INVITE)) { + if (!verifyStatus(statusIs(D_INVITED) || statusIs(D_ReINVITED))) { + printLog( + "respond(): InviteDialog not in (re)invited state: No response now", + LogLevel.HIGH); + return; + } + + int code = resp.getStatusLine().getCode(); + // 1xx provisional responses + if (code >= 100 && code < 200) { + invite_ts.respondWith(resp); + return; + } + // For all final responses establish the dialog + if (code >= 200) { // changeStatus(D_ACCEPTED); + update(Dialog.UAS, resp); + } + // 2xx success responses + if (code >= 200 && code < 300) { + if (statusIs(D_INVITED)) + changeStatus(D_ACCEPTED); + else + changeStatus(D_ReACCEPTED); + // terminates the INVITE Transaction server and activates an ACK + // Transaction server + invite_ts.terminate(); + ConnectionIdentifier conn_id = invite_ts.getConnectionId(); + ack_ts = new AckTransactionServer(sip_provider, conn_id, resp, + this); + ack_ts.respond(); + // if (statusIs(D_ReACCEPTED)) + // listener.onDlgReInviteAccepted(this); + // else listener.onDlgAccepted(this); + return; + } else + // 300-699 failure responses + // if (code>=300) + { + if (statusIs(D_INVITED)) + changeStatus(D_REFUSED); + else + changeStatus(D_ReREFUSED); + invite_ts.respondWith(resp); + // if (statusIs(D_ReREFUSED)) + // listener.onDlgReInviteRefused(this); + // else listener.onDlgRefused(this); + return; + } + } + if (method.equals(SipMethods.BYE)) { + if (!verifyStatus(statusIs(D_BYED))) + return; + bye_ts.respondWith(resp); + } + } + + /** + * Responds with code and reason. This method can be called + * when the InviteDialog is in D_INVITED, D_ReINVITED states + */ + public void respond(int code, String reason, String contact, String sdp) { + printLog("inside respond(" + code + "," + reason + ")", LogLevel.MEDIUM); + if (statusIs(D_INVITED) || statusIs(D_ReINVITED)) { + NameAddress contact_address = null; + if (contact != null) + contact_address = new NameAddress(contact); + Message resp = MessageFactory.createResponse(invite_req, code, + reason, contact_address); + resp.setBody(sdp); + respond(resp); + } else + printWarning("Dialog isn't in \"invited\" state: cannot respond (" + + code + "/" + getStatus() + "/" + getDialogID() + ")", + LogLevel.MEDIUM); + } + + /** + * Signals that the phone is ringing. This method should be called when the + * InviteDialog is in D_INVITED or D_ReINVITED state + */ + public void ring(String sdp) { // modified + printLog("inside ring()", LogLevel.MEDIUM); + respond(180, SipResponses.reasonOf(180), null, sdp); + } + + /** + * Accepts the incoming call. This method should be called when the + * InviteDialog is in D_INVITED or D_ReINVITED state + */ + public void accept(String contact, String sdp) { + printLog("inside accept(sdp)", LogLevel.MEDIUM); + respond(200, SipResponses.reasonOf(200), contact, sdp); + } + + /** + * Refuses the incoming call. This method should be called when the + * InviteDialog is in D_INVITED or D_ReINVITED state + */ + public void refuse(int code, String reason) { + printLog("inside refuse(" + code + "," + reason + ")", LogLevel.MEDIUM); + respond(code, reason, null, null); + } + + /** + * Refuses the incoming call. This method should be called when the + * InviteDialog is in D_INVITED or D_ReINVITED state + */ + public void refuse() { + printLog("inside refuse()", LogLevel.MEDIUM); + // refuse(480,"Temporarily Unavailable"); + // refuse(603,"Decline"); + refuse(403, SipResponses.reasonOf(403)); + } + + public void busy() { + refuse(486, SipResponses.reasonOf(486)); // modified + } + + /** + * Termiante the call. This method should be called when the InviteDialog is + * in D_CALL state + *

+ * Increments the Cseq, moves to state D_BYEING, and creates new BYE + * TransactionClient + */ + public void bye() { + printLog("inside bye()", LogLevel.MEDIUM); + if (statusIs(D_CALL)) { + Message bye = MessageFactory.createByeRequest(this); + bye(bye); + } + } + + /** + * Termiante the call. This method should be called when the InviteDialog is + * in D_CALL state + *

+ * Increments the Cseq, moves to state D_BYEING, and creates new BYE + * TransactionClient + */ + public void bye(Message bye) { + printLog("inside bye(bye)", LogLevel.MEDIUM); + if (statusIs(D_CALL)) { + changeStatus(D_BYEING); + // dialog_state.incLocalCSeq(); // done by + // MessageFactory.createRequest() + TransactionClient tc = new TransactionClient(sip_provider, bye, + this); + tc.request(); + // listener.onDlgByeing(this); + } + } + + /** + * Cancel the ongoing call request or a call listening. This method should + * be called when the InviteDialog is in D_INVITING or D_ReINVITING state or + * in the D_WAITING state + */ + public void cancel() { + printLog("inside cancel()", LogLevel.MEDIUM); + if (statusIs(D_INVITING) || statusIs(D_ReINVITING)) { + Message cancel = MessageFactory.createCancelRequest(invite_req,this); // modified + cancel(cancel); + } else if (statusIs(D_WAITING) || statusIs(D_ReWAITING)) { + invite_ts.terminate(); + } + } + + /** + * Cancel the ongoing call request or a call listening. This method should + * be called when the InviteDialog is in D_INVITING or D_ReINVITING state or + * in the D_WAITING state + */ + public void cancel(Message cancel) { + printLog("inside cancel(cancel)", LogLevel.MEDIUM); + if (statusIs(D_INVITING) || statusIs(D_ReINVITING)) { // changeStatus(D_CANCELING); + TransactionClient tc = new TransactionClient(sip_provider, cancel, + null); + tc.request(); + } else if (statusIs(D_WAITING) || statusIs(D_ReWAITING)) { + invite_ts.terminate(); + } + } + + /** + * Redirects the incoming call , specifing the code and reason. + * This method can be called when the InviteDialog is in D_INVITED or + * D_ReINVITED state + */ + public void redirect(int code, String reason, String contact) { + printLog( + "inside redirect(" + code + "," + reason + "," + contact + ")", + LogLevel.MEDIUM); + respond(code, reason, contact, null); + } + + // ************** Inherited from SipProviderListener ************** + + /** + * Inherited from class SipProviderListener. Called when a new message is + * received (out of any ongoing transaction) for the current InviteDialog. + * Always checks for out-of-date methods (CSeq header sequence number). + *

+ * If the message is ACK(2xx/INVITE) request, it moves to D_CALL state, and + * fires onDlgAck(this,body,msg). + *

+ * If the message is 2xx(INVITE) response, it create a new + * AckTransactionClient + *

+ * If the message is BYE, it moves to D_BYED state, removes the listener + * from SipProvider, fires onDlgBye(this,msg) then it responds with 200 OK, + * moves to D_CLOSE state and fires onDlgClose(this) + */ + public void onReceivedMessage(SipProvider sip_provider, Message msg) { + printLog("inside onReceivedMessage(sip_provider,message)", + LogLevel.MEDIUM); + if (msg.isRequest() && !(msg.isAck() || msg.isCancel()) + && msg.getCSeqHeader().getSequenceNumber() <= getRemoteCSeq()) { + printLog( + "Request message is too late (CSeq too small): Message discarded", + LogLevel.HIGH); + return; + } + // invite received + if (msg.isRequest() && msg.isInvite()) { + verifyStatus(statusIs(D_INIT) || statusIs(D_CALL)); + // NOTE: if the invite_ts.listen() is used, you should not arrive + // here with the D_INIT state.. + // however state D_INIT has been included for robustness against + // further changes. + if (statusIs(D_INIT)) + changeStatus(D_INVITED); + else + changeStatus(D_ReINVITED); + invite_req = msg; + invite_ts = new InviteTransactionServer(sip_provider, invite_req, + this); + // ((TransactionServer)transaction).listen(); + update(Dialog.UAS, invite_req); + if (statusIs(D_INVITED)) + listener.onDlgInvite(this, invite_req.getToHeader() + .getNameAddress(), invite_req.getFromHeader() + .getNameAddress(), invite_req.getBody(), invite_req); + else + listener.onDlgReInvite(this, invite_req.getBody(), invite_req); + } else + // ack (of 2xx of INVITE) + if (msg.isRequest() && msg.isAck()) { + if (!verifyStatus(statusIs(D_ACCEPTED) || statusIs(D_ReACCEPTED))) + return; + changeStatus(D_CALL); + // terminates the AckTransactionServer + ack_ts.terminate(); + listener.onDlgAck(this, msg.getBody(), msg); + listener.onDlgCall(this); + } else + // keep sending ACK (if already sent) for any "200 OK" received + if (msg.isResponse()) { + if (!verifyStatus(statusIs(D_CALL))) + return; + int code = msg.getStatusLine().getCode(); + verifyThat(code >= 200 && code < 300, "code 2xx was expected"); + if (ack_req != null) { + AckTransactionClient ack_tc = new AckTransactionClient( + sip_provider, ack_req, null); + ack_tc.request(); + } + } else + // bye received + if (msg.isRequest() && msg.isBye()) { + if (!verifyStatus(statusIs(D_CALL) || statusIs(D_BYEING))) + return; + changeStatus(D_BYED); + bye_ts = new TransactionServer(sip_provider, msg, this); + // automatically sends a 200 OK + Message resp = MessageFactory.createResponse(msg, 200, SipResponses + .reasonOf(200), null); + respond(resp); + listener.onDlgBye(this, msg); + changeStatus(D_CLOSE); + listener.onDlgClose(this); + } else + // cancel received + if (msg.isRequest() && msg.isCancel()) { + if (!verifyStatus(statusIs(D_INVITED) || statusIs(D_ReINVITED))) + return; + // create a CANCEL TransactionServer and send a 200 OK (CANCEL) + TransactionServer ts = new TransactionServer(sip_provider, msg, + null); + // ts.listen(); + ts.respondWith(MessageFactory.createResponse(msg, 200, SipResponses + .reasonOf(200), null)); + // automatically sends a 487 Cancelled + Message resp = MessageFactory.createResponse(invite_req, 487, + SipResponses.reasonOf(487), null); + respond(resp); + listener.onDlgCancel(this, msg); + } else + // any other request received + if (msg.isRequest()) { + TransactionServer ts = new TransactionServer(sip_provider, msg, + null); + // ts.listen(); + ts.respondWith(MessageFactory.createResponse(msg, 405, SipResponses + .reasonOf(405), null)); + } + } + + // ************** Inherited from InviteTransactionClientListener + // ************** + + /** + * Inherited from TransactionClientListener. When the + * TransactionClientListener is in "Proceeding" state and receives a new 1xx + * response + *

+ * For INVITE transaction it fires + * onFailureResponse(this,code,reason,body,msg). + */ + public void onTransProvisionalResponse(TransactionClient tc, Message msg) { + printLog("inside onTransProvisionalResponse(tc,mdg)", LogLevel.LOW); + if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { + StatusLine statusline = msg.getStatusLine(); + listener.onDlgInviteProvisionalResponse(this, statusline.getCode(), + statusline.getReason(), msg.getBody(), msg); + } + } + + /** + * Inherited from TransactionClientListener. When the + * TransactionClientListener goes into the "Completed" state, receiving a + * failure response + *

+ * If called for a INVITE transaction, it moves to D_CLOSE state, removes + * the listener from SipProvider. + *

+ * If called for a BYE transaction, it moves to D_CLOSE state, removes the + * listener from SipProvider, and fires onClose(this,msg). + */ + public void onTransFailureResponse(TransactionClient tc, Message msg) { + printLog("inside onTransFailureResponse(" + tc.getTransactionId() + + ",msg)", LogLevel.LOW); + if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { + if (!verifyStatus(statusIs(D_INVITING) || statusIs(D_ReINVITING))) + return; + StatusLine statusline = msg.getStatusLine(); + int code = statusline.getCode(); + verifyThat(code >= 300 && code < 700, "error code was expected"); + if (statusIs(D_ReINVITING)) { + changeStatus(D_CALL); + listener.onDlgReInviteFailureResponse(this, code, statusline + .getReason(), msg); + } else { + changeStatus(D_CLOSE); + if (code >= 300 && code < 400) + listener.onDlgInviteRedirectResponse(this, code, statusline + .getReason(), msg.getContacts(), msg); + else + listener.onDlgInviteFailureResponse(this, code, statusline + .getReason(), msg); + listener.onDlgClose(this); + } + } else if (tc.getTransactionMethod().equals(SipMethods.BYE)) { + if (!verifyStatus(statusIs(D_BYEING))) + return; + StatusLine statusline = msg.getStatusLine(); + int code = statusline.getCode(); + verifyThat(code >= 300 && code < 700, "error code was expected"); + changeStatus(InviteDialog.D_CALL); + listener.onDlgByeFailureResponse(this, code, + statusline.getReason(), msg); + } + } + + /** + * Inherited from TransactionClientListener. When an + * TransactionClientListener goes into the "Terminated" state, receiving a + * 2xx response + *

+ * If called for a INVITE transaction, it updates the dialog information, + * moves to D_CALL state, add a listener to the SipProvider, creates a new + * AckTransactionClient(ack,this), and fires + * onSuccessResponse(this,code,body,msg). + *

+ * If called for a BYE transaction, it moves to D_CLOSE state, removes the + * listener from SipProvider, and fires onClose(this,msg). + */ + public void onTransSuccessResponse(TransactionClient tc, Message msg) { + printLog("inside onTransSuccessResponse(tc,msg)", LogLevel.LOW); + if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { + if (!verifyStatus(statusIs(D_INVITING) || statusIs(D_ReINVITING))) + return; + StatusLine statusline = msg.getStatusLine(); + int code = statusline.getCode(); + if (!verifyThat(code >= 200 && code < 300 + && msg.getTransactionMethod().equals(SipMethods.INVITE), + "2xx for invite was expected")) + return; + boolean re_inviting = statusIs(D_ReINVITING); + changeStatus(D_CALL); + update(Dialog.UAC, msg); + if (invite_offer) { // invite_req=MessageFactory.createRequest(SipMethods.ACK,dialog_state,sdp.toString()); + // ack=MessageFactory.createRequest(this,SipMethods.ACK,null); + ack_req = MessageFactory.create2xxAckRequest(this, null); + AckTransactionClient ack_tc = new AckTransactionClient( + sip_provider, ack_req, null); + ack_tc.request(); + } + if (!re_inviting) { + listener.onDlgInviteSuccessResponse(this, code, statusline + .getReason(), msg.getBody(), msg); + listener.onDlgCall(this); + } else + listener.onDlgReInviteSuccessResponse(this, code, statusline + .getReason(), msg.getBody(), msg); + } else if (tc.getTransactionMethod().equals(SipMethods.BYE)) { + if (!verifyStatus(statusIs(D_BYEING))) + return; + StatusLine statusline = msg.getStatusLine(); + int code = statusline.getCode(); + verifyThat(code >= 200 && code < 300, "2xx for bye was expected"); + changeStatus(D_CLOSE); + listener.onDlgByeSuccessResponse(this, code, + statusline.getReason(), msg); + listener.onDlgClose(this); + } + } + + /** + * Inherited from TransactionClientListener. When the TransactionClient goes + * into the "Terminated" state, caused by transaction timeout + */ + public void onTransTimeout(TransactionClient tc) { + printLog("inside onTransTimeout(tc,msg)", LogLevel.LOW); + if (tc.getTransactionMethod().equals(SipMethods.INVITE)) { + if (!verifyStatus(statusIs(D_INVITING) || statusIs(D_ReINVITING))) + return; + cancel(); //modified + changeStatus(D_CLOSE); + listener.onDlgTimeout(this); + listener.onDlgClose(this); + } else if (tc.getTransactionMethod().equals(SipMethods.BYE)) { + if (!verifyStatus(statusIs(D_BYEING))) + return; + changeStatus(D_CLOSE); + listener.onDlgClose(this); + } + } + + // ************** Inherited from InviteTransactionServerListener + // ************** + + /** + * Inherited from TransactionServerListener. When the TransactionServer goes + * into the "Trying" state receiving a request + *

+ * If called for a INVITE transaction, it initializes the dialog + * information,
+ * moves to D_INVITED state, and add a listener to the SipProvider,
+ * and fires onInvite(caller,body,msg). + */ + public void onTransRequest(TransactionServer ts, Message req) { + printLog("inside onTransRequest(ts,msg)", LogLevel.LOW); + if (ts.getTransactionMethod().equals(SipMethods.INVITE)) { + if (!verifyStatus(statusIs(D_WAITING))) + return; + changeStatus(D_INVITED); + invite_req = req; + update(Dialog.UAS, invite_req); + listener.onDlgInvite(this, invite_req.getToHeader() + .getNameAddress(), invite_req.getFromHeader() + .getNameAddress(), invite_req.getBody(), invite_req); + } + } + + /** + * Inherited from InviteTransactionServerListener. When an + * InviteTransactionServer goes into the "Confirmed" state receining an ACK + * for NON-2xx response + *

+ * It moves to D_CLOSE state and removes the listener from SipProvider. + */ + public void onTransFailureAck(InviteTransactionServer ts, Message msg) { + printLog("inside onTransFailureAck(ts,msg)", LogLevel.LOW); + if (!verifyStatus(statusIs(D_REFUSED) || statusIs(D_ReREFUSED))) + return; + if (statusIs(D_ReREFUSED)) { + changeStatus(D_CALL); + } else { + changeStatus(D_CLOSE); + listener.onDlgClose(this); + } + } + + // ************ Inherited from AckTransactionServerListener ************ + + /** + * When the AckTransactionServer goes into the "Terminated" state, caused by + * transaction timeout + */ + public void onTransAckTimeout(AckTransactionServer ts) { + printLog("inside onAckSrvTimeout(ts)", LogLevel.LOW); + if (!verifyStatus(statusIs(D_ACCEPTED) || statusIs(D_ReACCEPTED) + || statusIs(D_REFUSED) || statusIs(D_ReREFUSED))) + return; + printLog("No ACK received..", LogLevel.HIGH); + changeStatus(D_CLOSE); + listener.onDlgClose(this); + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("InviteDialog#" + dialog_sqn + ": " + str, level + + SipStack.LOG_LEVEL_DIALOG); + } + +} diff --git a/src/org/zoolu/sip/dialog/InviteDialogListener.java b/app/src/main/java/org/zoolu/sip/dialog/InviteDialogListener.java similarity index 97% rename from src/org/zoolu/sip/dialog/InviteDialogListener.java rename to app/src/main/java/org/zoolu/sip/dialog/InviteDialogListener.java index 467e796..7c2ff10 100644 --- a/src/org/zoolu/sip/dialog/InviteDialogListener.java +++ b/app/src/main/java/org/zoolu/sip/dialog/InviteDialogListener.java @@ -1,117 +1,117 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.address.NameAddress; -import org.zoolu.sip.message.Message; -import org.zoolu.sip.header.MultipleHeader; - -/** - * An InviteDialogListener listens for InviteDialog events. It collects all - * InviteDialog callback functions. - */ -public interface InviteDialogListener { - /** When an incoming INVITE is received */ - public void onDlgInvite(InviteDialog dialog, NameAddress callee, - NameAddress caller, String body, Message msg); - - /** When an incoming Re-INVITE is received */ - public void onDlgReInvite(InviteDialog dialog, String body, Message msg); - - /** When a 1xx response response is received for an INVITE transaction */ - public void onDlgInviteProvisionalResponse(InviteDialog dialog, int code, - String reason, String body, Message msg); - - /** - * When a 2xx successfull final response is received for an INVITE - * transaction - */ - public void onDlgInviteSuccessResponse(InviteDialog dialog, int code, - String reason, String body, Message msg); - - /** When a 3xx redirection response is received for an INVITE transaction */ - public void onDlgInviteRedirectResponse(InviteDialog dialog, int code, - String reason, MultipleHeader contacts, Message msg); - - /** When a 400-699 failure response is received for an INVITE transaction */ - public void onDlgInviteFailureResponse(InviteDialog dialog, int code, - String reason, Message msg); - - /** When INVITE transaction expires */ - public void onDlgTimeout(InviteDialog dialog); - - /** When a 1xx response response is received for a Re-INVITE transaction */ - public void onDlgReInviteProvisionalResponse(InviteDialog dialog, int code, - String reason, String body, Message msg); - - /** - * When a 2xx successfull final response is received for a Re-INVITE - * transaction - */ - public void onDlgReInviteSuccessResponse(InviteDialog dialog, int code, - String reason, String body, Message msg); - - /** When a 3xx redirection response is received for a Re-INVITE transaction */ - // public void onDlgReInviteRedirectResponse(InviteDialog dialog, int code, - // String reason, MultipleHeader contacts, Message msg); - /** When a 400-699 failure response is received for a Re-INVITE transaction */ - public void onDlgReInviteFailureResponse(InviteDialog dialog, int code, - String reason, Message msg); - - /** When a Re-INVITE transaction expires */ - public void onDlgReInviteTimeout(InviteDialog dialog); - - /** When an incoming INVITE is accepted */ - // public void onDlgAccepted(InviteDialog dialog); - /** When an incoming INVITE is refused */ - // public void onDlgRefused(InviteDialog dialog); - /** When an incoming Re-INVITE is accepted */ - // public void onDlgReInviteAccepted(InviteDialog dialog); - /** When an incoming Re-INVITE is refused */ - // public void onDlgReInviteRefused(InviteDialog dialog); - /** When an incoming ACK is received for an INVITE transaction */ - public void onDlgAck(InviteDialog dialog, String body, Message msg); - - /** When the INVITE handshake is successful terminated */ - public void onDlgCall(InviteDialog dialog); - - /** When an incoming CANCEL is received for an INVITE transaction */ - public void onDlgCancel(InviteDialog dialog, Message msg); - - /** When an incoming BYE is received */ - public void onDlgBye(InviteDialog dialog, Message msg); - - /** When a BYE request traqnsaction has been started */ - // public void onDlgByeing(InviteDialog dialog); - /** When a success response is received for a Bye request */ - public void onDlgByeSuccessResponse(InviteDialog dialog, int code, - String reason, Message msg); - - /** When a failure response is received for a Bye request */ - public void onDlgByeFailureResponse(InviteDialog dialog, int code, - String reason, Message msg); - - /** When the dialog is finally closed */ - public void onDlgClose(InviteDialog dialog); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.address.NameAddress; +import org.zoolu.sip.message.Message; +import org.zoolu.sip.header.MultipleHeader; + +/** + * An InviteDialogListener listens for InviteDialog events. It collects all + * InviteDialog callback functions. + */ +public interface InviteDialogListener { + /** When an incoming INVITE is received */ + public void onDlgInvite(InviteDialog dialog, NameAddress callee, + NameAddress caller, String body, Message msg); + + /** When an incoming Re-INVITE is received */ + public void onDlgReInvite(InviteDialog dialog, String body, Message msg); + + /** When a 1xx response response is received for an INVITE transaction */ + public void onDlgInviteProvisionalResponse(InviteDialog dialog, int code, + String reason, String body, Message msg); + + /** + * When a 2xx successfull final response is received for an INVITE + * transaction + */ + public void onDlgInviteSuccessResponse(InviteDialog dialog, int code, + String reason, String body, Message msg); + + /** When a 3xx redirection response is received for an INVITE transaction */ + public void onDlgInviteRedirectResponse(InviteDialog dialog, int code, + String reason, MultipleHeader contacts, Message msg); + + /** When a 400-699 failure response is received for an INVITE transaction */ + public void onDlgInviteFailureResponse(InviteDialog dialog, int code, + String reason, Message msg); + + /** When INVITE transaction expires */ + public void onDlgTimeout(InviteDialog dialog); + + /** When a 1xx response response is received for a Re-INVITE transaction */ + public void onDlgReInviteProvisionalResponse(InviteDialog dialog, int code, + String reason, String body, Message msg); + + /** + * When a 2xx successfull final response is received for a Re-INVITE + * transaction + */ + public void onDlgReInviteSuccessResponse(InviteDialog dialog, int code, + String reason, String body, Message msg); + + /** When a 3xx redirection response is received for a Re-INVITE transaction */ + // public void onDlgReInviteRedirectResponse(InviteDialog dialog, int code, + // String reason, MultipleHeader contacts, Message msg); + /** When a 400-699 failure response is received for a Re-INVITE transaction */ + public void onDlgReInviteFailureResponse(InviteDialog dialog, int code, + String reason, Message msg); + + /** When a Re-INVITE transaction expires */ + public void onDlgReInviteTimeout(InviteDialog dialog); + + /** When an incoming INVITE is accepted */ + // public void onDlgAccepted(InviteDialog dialog); + /** When an incoming INVITE is refused */ + // public void onDlgRefused(InviteDialog dialog); + /** When an incoming Re-INVITE is accepted */ + // public void onDlgReInviteAccepted(InviteDialog dialog); + /** When an incoming Re-INVITE is refused */ + // public void onDlgReInviteRefused(InviteDialog dialog); + /** When an incoming ACK is received for an INVITE transaction */ + public void onDlgAck(InviteDialog dialog, String body, Message msg); + + /** When the INVITE handshake is successful terminated */ + public void onDlgCall(InviteDialog dialog); + + /** When an incoming CANCEL is received for an INVITE transaction */ + public void onDlgCancel(InviteDialog dialog, Message msg); + + /** When an incoming BYE is received */ + public void onDlgBye(InviteDialog dialog, Message msg); + + /** When a BYE request traqnsaction has been started */ + // public void onDlgByeing(InviteDialog dialog); + /** When a success response is received for a Bye request */ + public void onDlgByeSuccessResponse(InviteDialog dialog, int code, + String reason, Message msg); + + /** When a failure response is received for a Bye request */ + public void onDlgByeFailureResponse(InviteDialog dialog, int code, + String reason, Message msg); + + /** When the dialog is finally closed */ + public void onDlgClose(InviteDialog dialog); +} diff --git a/src/org/zoolu/sip/dialog/NotifierDialog.java b/app/src/main/java/org/zoolu/sip/dialog/NotifierDialog.java similarity index 96% rename from src/org/zoolu/sip/dialog/NotifierDialog.java rename to app/src/main/java/org/zoolu/sip/dialog/NotifierDialog.java index 139f9ef..4e9341d 100644 --- a/src/org/zoolu/sip/dialog/NotifierDialog.java +++ b/app/src/main/java/org/zoolu/sip/dialog/NotifierDialog.java @@ -1,415 +1,415 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -/* Modified by: - * Daina Interrante (daina.interrante@studenti.unipr.it) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.transaction.*; -import org.zoolu.sip.message.*; -import org.zoolu.sip.header.*; -import org.zoolu.sip.provider.*; -import org.zoolu.tools.LogLevel; - -/** - * NotifierDialog. - */ -public class NotifierDialog extends Dialog implements TransactionClientListener/* - * , - * TransactionServerListener - */ -{ - /** String "active" */ - protected static final String ACTIVE = "active"; - /** String "pending" */ - protected static final String PENDING = "pending"; - /** String "terminated" */ - protected static final String TERMINATED = "terminated"; - - /** The SubscriberDialog listener */ - NotifierDialogListener listener; - - /** The current subscribe method */ - Message subscribe_req; - - /** The current subscribe transaction */ - TransactionServer subscribe_transaction; - - /** The current notify transaction */ - TransactionClient notify_transaction; - - /** The event name */ - String event; - - /** The subscription id */ - String id; - - /** Internal state D_INIT (the starting point) */ - protected static final int D_INIT = 0; - /** Internal state D_WAITING (listening for the first subscription request) */ - protected static final int D_WAITING = 1; - /** Internal state D_SUBSCRIBED (first subscription request arrived) */ - protected static final int D_SUBSCRIBED = 2; - /** Internal state D_PENDING (first subscription request has been accepted) */ - protected static final int D_PENDING = 3; - /** Internal state D_ACTIVE (subscription has been activated) */ - protected static final int D_ACTIVE = 4; - /** - * Internal state D_TERMINATED (first subscription request has been refused - * or subscription has been terminated) - */ - protected static final int D_TERMINATED = 9; - - // ************************* Protected methods ************************ - - /** Gets the dialog state */ - protected String getStatusDescription() { - switch (status) { - case D_INIT: - return "D_INIT"; - case D_WAITING: - return "D_WAITING"; - case D_SUBSCRIBED: - return "D_SUBSCRIBED"; - case D_PENDING: - return "D_PENDING"; - case D_ACTIVE: - return "D_ACTIVE"; - case D_TERMINATED: - return "D_TERMINATED"; - default: - return null; - } - } - - protected int getStatus() { - return status; - } - - // ************************** Public methods ************************** - - /** Whether the dialog is in "early" state. */ - public boolean isEarly() { - return (status < D_PENDING); - } - - /** Whether the dialog is in "confirmed" state. */ - public boolean isConfirmed() { - return (status >= D_PENDING && status < D_TERMINATED); - } - - /** Whether the dialog is in "active" state. */ - public boolean isTerminated() { - return (status == D_TERMINATED); - } - - /** Whether the subscription is "pending". */ - public boolean isSubscriptionPending() { - return (status >= D_SUBSCRIBED && status < D_ACTIVE); - } - - /** Whether the subscription is "active". */ - public boolean isSubscriptionActive() { - return (status == D_ACTIVE); - } - - /** Whether the subscription is "terminated". */ - public boolean isSubscriptionTerminated() { - return (status == D_TERMINATED); - } - - /** Gets event type. */ - public String getEvent() { - return event; - } - - /** Gets the event "id" parameter. */ - public String getId() { - return id; - } - - // **************************** Costructors **************************** - - /** Creates a new NotifierDialog. */ - public NotifierDialog(SipProvider sip_provider, - NotifierDialogListener listener) { - super(sip_provider); - init(listener); - } - - /** - * Creates a new NotifierDialog for the already received SUBSCRIBE request - * subscribe. - */ - public NotifierDialog(SipProvider sip_provider, Message subscribe, - NotifierDialogListener listener) { - super(sip_provider); - init(listener); - - changeStatus(D_SUBSCRIBED); - subscribe_req = subscribe; - subscribe_transaction = new TransactionServer(sip_provider, subscribe, - null); - update(Dialog.UAS, subscribe); - EventHeader eh = subscribe.getEventHeader(); - if (eh != null) { - event = eh.getEvent(); - id = eh.getId(); - } - } - - /** Inits the NotifierDialog. */ - private void init(NotifierDialogListener listener) { - this.listener = listener; - this.subscribe_transaction = null; - this.notify_transaction = null; - this.subscribe_req = null; - this.event = null; - this.id = null; - changeStatus(D_INIT); - } - - // *************************** Public methods ************************** - - /** Listen for the first subscription request. */ - public void listen() { - printLog("inside method listen()", LogLevel.MEDIUM); - if (!statusIs(D_INIT)) { - printLog("first subscription already received", LogLevel.MEDIUM); - return; - } - // else - changeStatus(D_WAITING); - // listen for the first SUBSCRIBE request - sip_provider.addSipProviderListener(new MethodIdentifier( - SipMethods.SUBSCRIBE), this); - } - - /** Accepts the subscription request (sends a "202 Accepted" response). */ - public void accept(int expires, String contact) { - printLog("inside accept()", LogLevel.MEDIUM); - respond(202, SipResponses.reasonOf(202), expires, contact, null, null); - } - - /** Refuses the subscription request. */ - public void refuse() { - printLog("inside refuse()", LogLevel.MEDIUM); - respond(403, SipResponses.reasonOf(403), -1, null, null, null); - } - - /** - * Responds with code and reason. This method can be called - * when the InviteDialog is in D_INVITED, D_ReINVITED states - */ - public void respond(int code, String reason, int expires, String contact, - String content_type, String body) { - printLog("inside respond(" + code + "," + reason + ")", LogLevel.MEDIUM); - NameAddress contact_url = null; - if (contact != null) - contact_url = new NameAddress(contact); - Message resp = MessageFactory.createResponse(subscribe_req, code, - SipResponses.reasonOf(code), contact_url); - if (expires >= 0) - resp.setExpiresHeader(new ExpiresHeader(expires)); - if (body != null) - resp.setBody(content_type, body); - respond(resp); - } - - /** Responds with resp. */ - public void respond(Message resp) { - printLog("inside respond(resp)", LogLevel.MEDIUM); - if (resp.getStatusLine().getCode() >= 200) - update(UAS, resp); - subscribe_transaction.respondWith(resp); - } - - /** Activates the subscription (subscription goes into 'active' state). */ - public void activate() { - activate(SipStack.default_expires); - } - - /** Activates the subscription (subscription goes into 'active' state). */ - public void activate(int expires) { - notify(ACTIVE, expires, null, null); - } - - /** Makes the subscription pending (subscription goes into 'pending' state). */ - public void pending() { - pending(SipStack.default_expires); - } - - /** Makes the subscription pending (subscription goes into 'pending' state). */ - public void pending(int expires) { - notify(PENDING, expires, null, null); - } - - /** Terminates the subscription (subscription goes into 'terminated' state). */ - public void terminate() { - terminate(null); - } - - /** Terminates the subscription (subscription goes into 'terminated' state). */ - public void terminate(String reason) { - Message req = MessageFactory.createNotifyRequest(this, event, id, null, - null); - SubscriptionStateHeader sh = new SubscriptionStateHeader(TERMINATED); - if (reason != null) - sh.setReason(reason); - // sh.setExpires(0); - req.setSubscriptionStateHeader(sh); - notify(req); - } - - /** Sends a NOTIFY. */ - public void notify(String state, int expires, String content_type, - String body) { - Message req = MessageFactory.createNotifyRequest(this, event, id, - content_type, body); - if (state != null) { - SubscriptionStateHeader sh = new SubscriptionStateHeader(state); - if (expires >= 0) - sh.setExpires(expires); - req.setSubscriptionStateHeader(sh); - } - notify(req); - } - - /** Sends a NOTIFY. */ - public void notify(Message req) { - String subscription_state = req.getSubscriptionStateHeader().getState(); - if (subscription_state.equalsIgnoreCase(ACTIVE) - && (statusIs(D_SUBSCRIBED) || statusIs(D_PENDING))) - changeStatus(D_ACTIVE); - else if (subscription_state.equalsIgnoreCase(PENDING) - && statusIs(D_SUBSCRIBED)) - changeStatus(D_PENDING); - else if (subscription_state.equalsIgnoreCase(TERMINATED) - && !statusIs(D_TERMINATED)) - changeStatus(D_TERMINATED); - - TransactionClient notify_transaction = new TransactionClient( - sip_provider, req, this); - notify_transaction.request(); - } - - // ************** Inherited from TransactionClientListener ************** - - /** - * When the TransactionClient is (or goes) in "Proceeding" state and - * receives a new 1xx provisional response - */ - public void onTransProvisionalResponse(TransactionClient tc, Message resp) { - printLog("onTransProvisionalResponse()", LogLevel.MEDIUM); - // do nothing. - } - - /** - * When the TransactionClient goes into the "Completed" state receiving a - * 2xx response - */ - public void onTransSuccessResponse(TransactionClient tc, Message resp) { - printLog("onTransSuccessResponse()", LogLevel.MEDIUM); - StatusLine status_line = resp.getStatusLine(); - if (listener != null) - listener.onDlgNotificationSuccess(this, status_line.getCode(), - status_line.getReason(), resp); - } - - /** - * When the TransactionClient goes into the "Completed" state receiving a - * 300-699 response - */ - public void onTransFailureResponse(TransactionClient tc, Message resp) { - printLog("onTransFailureResponse()", LogLevel.MEDIUM); - StatusLine status_line = resp.getStatusLine(); - if (listener != null) - listener.onDlgNotificationFailure(this, status_line.getCode(), - status_line.getReason(), resp); - } - - /** - * When the TransactionClient goes into the "Terminated" state, caused by - * transaction timeout - */ - public void onTransTimeout(TransactionClient tc) { - printLog("onTransTimeout()", LogLevel.MEDIUM); - if (!statusIs(D_TERMINATED)) { - changeStatus(D_TERMINATED); - if (listener != null) - listener.onDlgNotifyTimeout(this); - } - } - - // ************** Inherited from SipProviderListener ************** - - /** When a new Message is received by the SipProvider. */ - public void onReceivedMessage(SipProvider provider, Message msg) { - printLog("onReceivedMessage()", LogLevel.MEDIUM); - if (statusIs(D_TERMINATED)) { - printLog("subscription already terminated: message discarded", - LogLevel.MEDIUM); - return; - } - // else - if (msg.isRequest() && msg.isSubscribe()) { - if (statusIs(NotifierDialog.D_WAITING)) { // the first SUBSCRIBE - // request - changeStatus(D_SUBSCRIBED); - sip_provider.removeSipProviderListener(new MethodIdentifier( - SipMethods.SUBSCRIBE)); - } - subscribe_req = msg; - NameAddress target = msg.getToHeader().getNameAddress(); - NameAddress subscriber = msg.getFromHeader().getNameAddress(); - EventHeader eh = msg.getEventHeader(); - if (eh != null) { - event = eh.getEvent(); - id = eh.getId(); - } - update(UAS, msg); - subscribe_transaction = new TransactionServer(sip_provider, msg, - null); - if (listener != null) - listener.onDlgSubscribe(this, target, subscriber, event, id, - msg); - } else { - printLog("message is not a SUBSCRIBE: message discarded", - LogLevel.HIGH); - } - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("NotifierDialog#" + dialog_sqn + ": " + str, level - + SipStack.LOG_LEVEL_DIALOG); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +/* Modified by: + * Daina Interrante (daina.interrante@studenti.unipr.it) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.transaction.*; +import org.zoolu.sip.message.*; +import org.zoolu.sip.header.*; +import org.zoolu.sip.provider.*; +import org.zoolu.tools.LogLevel; + +/** + * NotifierDialog. + */ +public class NotifierDialog extends Dialog implements TransactionClientListener/* + * , + * TransactionServerListener + */ +{ + /** String "active" */ + protected static final String ACTIVE = "active"; + /** String "pending" */ + protected static final String PENDING = "pending"; + /** String "terminated" */ + protected static final String TERMINATED = "terminated"; + + /** The SubscriberDialog listener */ + NotifierDialogListener listener; + + /** The current subscribe method */ + Message subscribe_req; + + /** The current subscribe transaction */ + TransactionServer subscribe_transaction; + + /** The current notify transaction */ + TransactionClient notify_transaction; + + /** The event name */ + String event; + + /** The subscription id */ + String id; + + /** Internal state D_INIT (the starting point) */ + protected static final int D_INIT = 0; + /** Internal state D_WAITING (listening for the first subscription request) */ + protected static final int D_WAITING = 1; + /** Internal state D_SUBSCRIBED (first subscription request arrived) */ + protected static final int D_SUBSCRIBED = 2; + /** Internal state D_PENDING (first subscription request has been accepted) */ + protected static final int D_PENDING = 3; + /** Internal state D_ACTIVE (subscription has been activated) */ + protected static final int D_ACTIVE = 4; + /** + * Internal state D_TERMINATED (first subscription request has been refused + * or subscription has been terminated) + */ + protected static final int D_TERMINATED = 9; + + // ************************* Protected methods ************************ + + /** Gets the dialog state */ + protected String getStatusDescription() { + switch (status) { + case D_INIT: + return "D_INIT"; + case D_WAITING: + return "D_WAITING"; + case D_SUBSCRIBED: + return "D_SUBSCRIBED"; + case D_PENDING: + return "D_PENDING"; + case D_ACTIVE: + return "D_ACTIVE"; + case D_TERMINATED: + return "D_TERMINATED"; + default: + return null; + } + } + + protected int getStatus() { + return status; + } + + // ************************** Public methods ************************** + + /** Whether the dialog is in "early" state. */ + public boolean isEarly() { + return (status < D_PENDING); + } + + /** Whether the dialog is in "confirmed" state. */ + public boolean isConfirmed() { + return (status >= D_PENDING && status < D_TERMINATED); + } + + /** Whether the dialog is in "active" state. */ + public boolean isTerminated() { + return (status == D_TERMINATED); + } + + /** Whether the subscription is "pending". */ + public boolean isSubscriptionPending() { + return (status >= D_SUBSCRIBED && status < D_ACTIVE); + } + + /** Whether the subscription is "active". */ + public boolean isSubscriptionActive() { + return (status == D_ACTIVE); + } + + /** Whether the subscription is "terminated". */ + public boolean isSubscriptionTerminated() { + return (status == D_TERMINATED); + } + + /** Gets event type. */ + public String getEvent() { + return event; + } + + /** Gets the event "id" parameter. */ + public String getId() { + return id; + } + + // **************************** Costructors **************************** + + /** Creates a new NotifierDialog. */ + public NotifierDialog(SipProvider sip_provider, + NotifierDialogListener listener) { + super(sip_provider); + init(listener); + } + + /** + * Creates a new NotifierDialog for the already received SUBSCRIBE request + * subscribe. + */ + public NotifierDialog(SipProvider sip_provider, Message subscribe, + NotifierDialogListener listener) { + super(sip_provider); + init(listener); + + changeStatus(D_SUBSCRIBED); + subscribe_req = subscribe; + subscribe_transaction = new TransactionServer(sip_provider, subscribe, + null); + update(Dialog.UAS, subscribe); + EventHeader eh = subscribe.getEventHeader(); + if (eh != null) { + event = eh.getEvent(); + id = eh.getId(); + } + } + + /** Inits the NotifierDialog. */ + private void init(NotifierDialogListener listener) { + this.listener = listener; + this.subscribe_transaction = null; + this.notify_transaction = null; + this.subscribe_req = null; + this.event = null; + this.id = null; + changeStatus(D_INIT); + } + + // *************************** Public methods ************************** + + /** Listen for the first subscription request. */ + public void listen() { + printLog("inside method listen()", LogLevel.MEDIUM); + if (!statusIs(D_INIT)) { + printLog("first subscription already received", LogLevel.MEDIUM); + return; + } + // else + changeStatus(D_WAITING); + // listen for the first SUBSCRIBE request + sip_provider.addSipProviderListener(new MethodIdentifier( + SipMethods.SUBSCRIBE), this); + } + + /** Accepts the subscription request (sends a "202 Accepted" response). */ + public void accept(int expires, String contact) { + printLog("inside accept()", LogLevel.MEDIUM); + respond(202, SipResponses.reasonOf(202), expires, contact, null, null); + } + + /** Refuses the subscription request. */ + public void refuse() { + printLog("inside refuse()", LogLevel.MEDIUM); + respond(403, SipResponses.reasonOf(403), -1, null, null, null); + } + + /** + * Responds with code and reason. This method can be called + * when the InviteDialog is in D_INVITED, D_ReINVITED states + */ + public void respond(int code, String reason, int expires, String contact, + String content_type, String body) { + printLog("inside respond(" + code + "," + reason + ")", LogLevel.MEDIUM); + NameAddress contact_url = null; + if (contact != null) + contact_url = new NameAddress(contact); + Message resp = MessageFactory.createResponse(subscribe_req, code, + SipResponses.reasonOf(code), contact_url); + if (expires >= 0) + resp.setExpiresHeader(new ExpiresHeader(expires)); + if (body != null) + resp.setBody(content_type, body); + respond(resp); + } + + /** Responds with resp. */ + public void respond(Message resp) { + printLog("inside respond(resp)", LogLevel.MEDIUM); + if (resp.getStatusLine().getCode() >= 200) + update(UAS, resp); + subscribe_transaction.respondWith(resp); + } + + /** Activates the subscription (subscription goes into 'active' state). */ + public void activate() { + activate(SipStack.default_expires); + } + + /** Activates the subscription (subscription goes into 'active' state). */ + public void activate(int expires) { + notify(ACTIVE, expires, null, null); + } + + /** Makes the subscription pending (subscription goes into 'pending' state). */ + public void pending() { + pending(SipStack.default_expires); + } + + /** Makes the subscription pending (subscription goes into 'pending' state). */ + public void pending(int expires) { + notify(PENDING, expires, null, null); + } + + /** Terminates the subscription (subscription goes into 'terminated' state). */ + public void terminate() { + terminate(null); + } + + /** Terminates the subscription (subscription goes into 'terminated' state). */ + public void terminate(String reason) { + Message req = MessageFactory.createNotifyRequest(this, event, id, null, + null); + SubscriptionStateHeader sh = new SubscriptionStateHeader(TERMINATED); + if (reason != null) + sh.setReason(reason); + // sh.setExpires(0); + req.setSubscriptionStateHeader(sh); + notify(req); + } + + /** Sends a NOTIFY. */ + public void notify(String state, int expires, String content_type, + String body) { + Message req = MessageFactory.createNotifyRequest(this, event, id, + content_type, body); + if (state != null) { + SubscriptionStateHeader sh = new SubscriptionStateHeader(state); + if (expires >= 0) + sh.setExpires(expires); + req.setSubscriptionStateHeader(sh); + } + notify(req); + } + + /** Sends a NOTIFY. */ + public void notify(Message req) { + String subscription_state = req.getSubscriptionStateHeader().getState(); + if (subscription_state.equalsIgnoreCase(ACTIVE) + && (statusIs(D_SUBSCRIBED) || statusIs(D_PENDING))) + changeStatus(D_ACTIVE); + else if (subscription_state.equalsIgnoreCase(PENDING) + && statusIs(D_SUBSCRIBED)) + changeStatus(D_PENDING); + else if (subscription_state.equalsIgnoreCase(TERMINATED) + && !statusIs(D_TERMINATED)) + changeStatus(D_TERMINATED); + + TransactionClient notify_transaction = new TransactionClient( + sip_provider, req, this); + notify_transaction.request(); + } + + // ************** Inherited from TransactionClientListener ************** + + /** + * When the TransactionClient is (or goes) in "Proceeding" state and + * receives a new 1xx provisional response + */ + public void onTransProvisionalResponse(TransactionClient tc, Message resp) { + printLog("onTransProvisionalResponse()", LogLevel.MEDIUM); + // do nothing. + } + + /** + * When the TransactionClient goes into the "Completed" state receiving a + * 2xx response + */ + public void onTransSuccessResponse(TransactionClient tc, Message resp) { + printLog("onTransSuccessResponse()", LogLevel.MEDIUM); + StatusLine status_line = resp.getStatusLine(); + if (listener != null) + listener.onDlgNotificationSuccess(this, status_line.getCode(), + status_line.getReason(), resp); + } + + /** + * When the TransactionClient goes into the "Completed" state receiving a + * 300-699 response + */ + public void onTransFailureResponse(TransactionClient tc, Message resp) { + printLog("onTransFailureResponse()", LogLevel.MEDIUM); + StatusLine status_line = resp.getStatusLine(); + if (listener != null) + listener.onDlgNotificationFailure(this, status_line.getCode(), + status_line.getReason(), resp); + } + + /** + * When the TransactionClient goes into the "Terminated" state, caused by + * transaction timeout + */ + public void onTransTimeout(TransactionClient tc) { + printLog("onTransTimeout()", LogLevel.MEDIUM); + if (!statusIs(D_TERMINATED)) { + changeStatus(D_TERMINATED); + if (listener != null) + listener.onDlgNotifyTimeout(this); + } + } + + // ************** Inherited from SipProviderListener ************** + + /** When a new Message is received by the SipProvider. */ + public void onReceivedMessage(SipProvider provider, Message msg) { + printLog("onReceivedMessage()", LogLevel.MEDIUM); + if (statusIs(D_TERMINATED)) { + printLog("subscription already terminated: message discarded", + LogLevel.MEDIUM); + return; + } + // else + if (msg.isRequest() && msg.isSubscribe()) { + if (statusIs(NotifierDialog.D_WAITING)) { // the first SUBSCRIBE + // request + changeStatus(D_SUBSCRIBED); + sip_provider.removeSipProviderListener(new MethodIdentifier( + SipMethods.SUBSCRIBE)); + } + subscribe_req = msg; + NameAddress target = msg.getToHeader().getNameAddress(); + NameAddress subscriber = msg.getFromHeader().getNameAddress(); + EventHeader eh = msg.getEventHeader(); + if (eh != null) { + event = eh.getEvent(); + id = eh.getId(); + } + update(UAS, msg); + subscribe_transaction = new TransactionServer(sip_provider, msg, + null); + if (listener != null) + listener.onDlgSubscribe(this, target, subscriber, event, id, + msg); + } else { + printLog("message is not a SUBSCRIBE: message discarded", + LogLevel.HIGH); + } + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("NotifierDialog#" + dialog_sqn + ": " + str, level + + SipStack.LOG_LEVEL_DIALOG); + } + +} diff --git a/src/org/zoolu/sip/dialog/NotifierDialogListener.java b/app/src/main/java/org/zoolu/sip/dialog/NotifierDialogListener.java similarity index 97% rename from src/org/zoolu/sip/dialog/NotifierDialogListener.java rename to app/src/main/java/org/zoolu/sip/dialog/NotifierDialogListener.java index 4bcb8e3..18421e4 100644 --- a/src/org/zoolu/sip/dialog/NotifierDialogListener.java +++ b/app/src/main/java/org/zoolu/sip/dialog/NotifierDialogListener.java @@ -1,55 +1,55 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.message.Message; -import org.zoolu.sip.address.NameAddress; - -/** - * A NotifierDialogListener listens for NotifierDialog events. It collects all - * NOTIFY callback functions. - */ -public interface NotifierDialogListener { - /** When an incoming SUBSCRIBE is received. */ - public void onDlgSubscribe(NotifierDialog dialog, NameAddress target, - NameAddress subscriber, String event, String id, Message msg); - - /** When a re-SUBSCRIBE is received. */ - // public void onDlgReSubscribe(NotifierDialog dialog, Message msg); - /** When NOTIFY transaction expires without a final response. */ - public void onDlgNotifyTimeout(NotifierDialog dialog); - - /** When a 300-699 response is received for a NOTIFY transaction. */ - public void onDlgNotificationFailure(NotifierDialog dialog, int code, - String reason, Message msg); - - /** - * When a 2xx successfull final response is received for a NOTIFY - * transaction. - */ - public void onDlgNotificationSuccess(NotifierDialog dialog, int code, - String reason, Message msg); - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.message.Message; +import org.zoolu.sip.address.NameAddress; + +/** + * A NotifierDialogListener listens for NotifierDialog events. It collects all + * NOTIFY callback functions. + */ +public interface NotifierDialogListener { + /** When an incoming SUBSCRIBE is received. */ + public void onDlgSubscribe(NotifierDialog dialog, NameAddress target, + NameAddress subscriber, String event, String id, Message msg); + + /** When a re-SUBSCRIBE is received. */ + // public void onDlgReSubscribe(NotifierDialog dialog, Message msg); + /** When NOTIFY transaction expires without a final response. */ + public void onDlgNotifyTimeout(NotifierDialog dialog); + + /** When a 300-699 response is received for a NOTIFY transaction. */ + public void onDlgNotificationFailure(NotifierDialog dialog, int code, + String reason, Message msg); + + /** + * When a 2xx successfull final response is received for a NOTIFY + * transaction. + */ + public void onDlgNotificationSuccess(NotifierDialog dialog, int code, + String reason, Message msg); + +} diff --git a/src/org/zoolu/sip/dialog/SubscriberDialog.java b/app/src/main/java/org/zoolu/sip/dialog/SubscriberDialog.java similarity index 96% rename from src/org/zoolu/sip/dialog/SubscriberDialog.java rename to app/src/main/java/org/zoolu/sip/dialog/SubscriberDialog.java index 937d0f5..0a28f8a 100644 --- a/src/org/zoolu/sip/dialog/SubscriberDialog.java +++ b/app/src/main/java/org/zoolu/sip/dialog/SubscriberDialog.java @@ -1,351 +1,351 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -/* Modified by: - * Daina Interrante (daina.interrante@studenti.unipr.it) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.transaction.*; -import org.zoolu.sip.message.*; -import org.zoolu.sip.header.*; -import org.zoolu.sip.provider.*; -import org.zoolu.tools.LogLevel; - -/** - * SubscriberDialog. - */ -public class SubscriberDialog extends Dialog implements - TransactionClientListener { - /** String "active" */ - protected static final String ACTIVE = "active"; - /** String "pending" */ - protected static final String PENDING = "pending"; - /** String "terminated" */ - protected static final String TERMINATED = "terminated"; - - /** The current subscribe method */ - // Message subscribe=null; - /** The subscribe transaction */ - TransactionClient subscribe_transaction; - - /** The notify transaction */ - // TransactionServer notify_transaction=null; - /** The SubscriberDialog listener */ - SubscriberDialogListener listener; - - /** The event package name */ - String event; - - /** The subscription id */ - String id; - - /** Internal state D_INIT */ - protected static final int D_INIT = 0; - /** Internal state D_SUBSCRIBING */ - protected static final int D_SUBSCRIBING = 1; - /** Internal state D_SUBSCRIBED */ - protected static final int D_ACCEPTED = 2; - /** Internal state D_PENDING */ - protected static final int D_PENDING = 3; - /** Internal state D_ACTIVE */ - protected static final int D_ACTIVE = 4; - /** Internal state D_TERMINATED */ - protected static final int D_TERMINATED = 9; - - /** Gets the dialog state */ - protected String getStatusDescription() { - switch (status) { - case D_INIT: - return "D_INIT"; - case D_SUBSCRIBING: - return "D_SUBSCRIBING"; - case D_ACCEPTED: - return "D_ACCEPTED"; - case D_PENDING: - return "D_PENDING"; - case D_ACTIVE: - return "D_ACTIVE"; - case D_TERMINATED: - return "D_TERMINATED"; - default: - return null; - } - } - - protected int getStatus() - { - return status; - } - - // *************************** Public methods ************************** - - /** Whether the dialog is in "early" state. */ - public boolean isEarly() { - return (status < D_ACCEPTED); - } - - /** Whether the dialog is in "confirmed" state. */ - public boolean isConfirmed() { - return (status >= D_ACCEPTED && status < D_TERMINATED); - } - - /** Whether the dialog is in "active" state. */ - public boolean isTerminated() { - return (status == D_TERMINATED); - } - - /** Whether the subscription is "pending". */ - public boolean isSubscriptionPending() { - return (status >= D_ACCEPTED && status < D_ACTIVE); - } - - /** Whether the subscription is "active". */ - public boolean isSubscriptionActive() { - return (status == D_ACTIVE); - } - - /** Whether the subscription is "terminated". */ - public boolean isSubscriptionTerminated() { - return (status == D_TERMINATED); - } - - /** Gets event type. */ - public String getEvent() { - return event; - } - - /** Gets the event "id" parameter. */ - public String getId() { - return id; - } - - // **************************** Costructors **************************** - - /** Creates a new SubscriberDialog. */ - public SubscriberDialog(SipProvider sip_provider, /* - * String subscriber, - * String contact, - */ - String event, String id, SubscriberDialogListener listener) { - super(sip_provider); - this.listener = listener; - this.subscribe_transaction = null; - // this.from_url=new NameAddress(subscriber); - // if (contact!=null) this.contact_url=new NameAddress(contact); - // else this.contact_url=from_url; - this.event = event; - this.id = null; - changeStatus(D_INIT); - } - - // *************************** Public methods ************************** - - /** - * Sends a new SUBSCRIBE request (starts a new subscription). It also - * initializes the dialog state information. - * - * @param target - * the target url (and display name) - * @param subscriber - * the subscriber url (and display name) - * @param contact - * the contact url OR the contact user-name - */ - public void subscribe(String target, String subscriber, String contact, - int expires) { - printLog("inside subscribe(target=" + target + ",subscriber=" - + subscriber + ",contact=" + contact + ",id=" + id - + ",expires=" + expires + ")", LogLevel.MEDIUM); - SipURL request_uri = new SipURL(target); - NameAddress to_url = new NameAddress(target); - NameAddress from_url = new NameAddress(subscriber); - NameAddress contact_url; - if (contact != null) - contact_url = new NameAddress(contact); - else - contact_url = from_url; - String content_type = null; - String body = null; - Message req = MessageFactory.createSubscribeRequest(sip_provider, - request_uri, to_url, from_url, contact_url, event, id, - content_type, body); - req.setHeader(new AcceptHeader("application/pidf+xml")); - req.setExpiresHeader(new ExpiresHeader(expires)); - subscribe(req); - } - - /** - * Sends a new SUBSCRIBE request (starts a new subscription). It also - * initializes the dialog state information. - * - * @param req - * the SUBSCRIBE message - */ - public void subscribe(Message req) { - printLog("inside subscribe(req)", LogLevel.MEDIUM); - if (statusIs(D_TERMINATED)) { - printLog("subscription already terminated: request aborted", - LogLevel.MEDIUM); - return; - } - // else - if (statusIs(D_INIT)) { - changeStatus(D_SUBSCRIBING); - } - update(UAC, req); - // start client transaction - subscribe_transaction = new TransactionClient(sip_provider, req, this); - subscribe_transaction.request(); - } - - /** Sends a new SUBSCRIBE request (starts a new subscription). */ - public void reSubscribe(String target, String subscriber, String contact, - int expires) { - subscribe(target, subscriber, contact, expires); - } - - // ************** Inherited from TransactionClientListener ************** - - /** - * When the TransactionClient is (or goes) in "Proceeding" state and - * receives a new 1xx provisional response - */ - public void onTransProvisionalResponse(TransactionClient tc, Message resp) { - printLog("onTransProvisionalResponse()", LogLevel.MEDIUM); - // do nothing. - } - - /** - * When the TransactionClient goes into the "Completed" state receiving a - * 2xx response - */ - public void onTransSuccessResponse(TransactionClient tc, Message resp) { - printLog("onTransSuccessResponse()", LogLevel.MEDIUM); - if (!statusIs(D_ACTIVE)) { - changeStatus(D_ACCEPTED); - update(UAC, resp); - StatusLine status_line = resp.getStatusLine(); - if (listener != null) - listener.onDlgSubscriptionSuccess(this, status_line.getCode(), - status_line.getReason(), resp); - } else if (statusIs(D_ACTIVE)) { - StatusLine status_line = resp.getStatusLine(); - if (listener != null) - listener.onDlgSubscriptionSuccess(this, status_line.getCode(), - status_line.getReason(), resp); - } - } - - /** - * When the TransactionClient goes into the "Completed" state receiving a - * 300-699 response - */ - public void onTransFailureResponse(TransactionClient tc, Message resp) { - printLog("onTransFailureResponse()", LogLevel.MEDIUM); - changeStatus(D_TERMINATED); - StatusLine status_line = resp.getStatusLine(); - if (listener != null) - listener.onDlgSubscriptionFailure(this, status_line.getCode(), - status_line.getReason(), resp); - } - - /** - * When the TransactionClient goes into the "Terminated" state, caused by - * transaction timeout - */ - public void onTransTimeout(TransactionClient tc) { - printLog("onTransTimeout()", LogLevel.MEDIUM); - changeStatus(D_TERMINATED); - if (listener != null) - listener.onDlgSubscribeTimeout(this); - } - - // ***************** Inherited from SipProviderListener ***************** - - /** When a new Message is received by the SipProvider. */ - public void onReceivedMessage(SipProvider sip_provider, Message msg) { - printLog("onReceivedMessage()", LogLevel.MEDIUM); - if (statusIs(D_TERMINATED)) { - printLog("subscription already terminated: message discarded", - LogLevel.MEDIUM); - return; - } - // else - if (msg.isRequest() && msg.isNotify()) { - TransactionServer ts = new TransactionServer(sip_provider, msg, - null); - ts.respondWith(MessageFactory.createResponse(msg, 200, SipResponses - .reasonOf(200), null)); - - NameAddress to = msg.getToHeader().getNameAddress(); - NameAddress from = msg.getFromHeader().getNameAddress(); - NameAddress contact = null; - if (msg.hasContactHeader()) - contact = msg.getContactHeader().getNameAddress(); - String state = null; - if (msg.hasSubscriptionStateHeader()) - state = msg.getSubscriptionStateHeader().getState(); - String content_type = null; - if (msg.hasContentTypeHeader()) - content_type = msg.getContentTypeHeader().getContentType(); - String body = null; - if (msg.hasBody()) - body = msg.getBody(); - - if (listener != null) - listener.onDlgNotify(this, to, from, contact, state, - content_type, body, msg); - - if (state != null) { - if (state.equalsIgnoreCase(ACTIVE) && !statusIs(D_TERMINATED)) { - changeStatus(D_ACTIVE); - } else if (state.equalsIgnoreCase(PENDING) - && statusIs(D_ACCEPTED)) { - changeStatus(D_PENDING); - } else if (state.equalsIgnoreCase(TERMINATED) - && !statusIs(D_TERMINATED)) { - changeStatus(D_TERMINATED); - if (listener != null) - listener.onDlgSubscriptionTerminated(this); - } - } - } else { - printLog("message is not a NOTIFY: message discarded", - LogLevel.HIGH); - } - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("SubscriberDialog#" + dialog_sqn + ": " + str, level - + SipStack.LOG_LEVEL_DIALOG); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +/* Modified by: + * Daina Interrante (daina.interrante@studenti.unipr.it) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.transaction.*; +import org.zoolu.sip.message.*; +import org.zoolu.sip.header.*; +import org.zoolu.sip.provider.*; +import org.zoolu.tools.LogLevel; + +/** + * SubscriberDialog. + */ +public class SubscriberDialog extends Dialog implements + TransactionClientListener { + /** String "active" */ + protected static final String ACTIVE = "active"; + /** String "pending" */ + protected static final String PENDING = "pending"; + /** String "terminated" */ + protected static final String TERMINATED = "terminated"; + + /** The current subscribe method */ + // Message subscribe=null; + /** The subscribe transaction */ + TransactionClient subscribe_transaction; + + /** The notify transaction */ + // TransactionServer notify_transaction=null; + /** The SubscriberDialog listener */ + SubscriberDialogListener listener; + + /** The event package name */ + String event; + + /** The subscription id */ + String id; + + /** Internal state D_INIT */ + protected static final int D_INIT = 0; + /** Internal state D_SUBSCRIBING */ + protected static final int D_SUBSCRIBING = 1; + /** Internal state D_SUBSCRIBED */ + protected static final int D_ACCEPTED = 2; + /** Internal state D_PENDING */ + protected static final int D_PENDING = 3; + /** Internal state D_ACTIVE */ + protected static final int D_ACTIVE = 4; + /** Internal state D_TERMINATED */ + protected static final int D_TERMINATED = 9; + + /** Gets the dialog state */ + protected String getStatusDescription() { + switch (status) { + case D_INIT: + return "D_INIT"; + case D_SUBSCRIBING: + return "D_SUBSCRIBING"; + case D_ACCEPTED: + return "D_ACCEPTED"; + case D_PENDING: + return "D_PENDING"; + case D_ACTIVE: + return "D_ACTIVE"; + case D_TERMINATED: + return "D_TERMINATED"; + default: + return null; + } + } + + protected int getStatus() + { + return status; + } + + // *************************** Public methods ************************** + + /** Whether the dialog is in "early" state. */ + public boolean isEarly() { + return (status < D_ACCEPTED); + } + + /** Whether the dialog is in "confirmed" state. */ + public boolean isConfirmed() { + return (status >= D_ACCEPTED && status < D_TERMINATED); + } + + /** Whether the dialog is in "active" state. */ + public boolean isTerminated() { + return (status == D_TERMINATED); + } + + /** Whether the subscription is "pending". */ + public boolean isSubscriptionPending() { + return (status >= D_ACCEPTED && status < D_ACTIVE); + } + + /** Whether the subscription is "active". */ + public boolean isSubscriptionActive() { + return (status == D_ACTIVE); + } + + /** Whether the subscription is "terminated". */ + public boolean isSubscriptionTerminated() { + return (status == D_TERMINATED); + } + + /** Gets event type. */ + public String getEvent() { + return event; + } + + /** Gets the event "id" parameter. */ + public String getId() { + return id; + } + + // **************************** Costructors **************************** + + /** Creates a new SubscriberDialog. */ + public SubscriberDialog(SipProvider sip_provider, /* + * String subscriber, + * String contact, + */ + String event, String id, SubscriberDialogListener listener) { + super(sip_provider); + this.listener = listener; + this.subscribe_transaction = null; + // this.from_url=new NameAddress(subscriber); + // if (contact!=null) this.contact_url=new NameAddress(contact); + // else this.contact_url=from_url; + this.event = event; + this.id = null; + changeStatus(D_INIT); + } + + // *************************** Public methods ************************** + + /** + * Sends a new SUBSCRIBE request (starts a new subscription). It also + * initializes the dialog state information. + * + * @param target + * the target url (and display name) + * @param subscriber + * the subscriber url (and display name) + * @param contact + * the contact url OR the contact user-name + */ + public void subscribe(String target, String subscriber, String contact, + int expires) { + printLog("inside subscribe(target=" + target + ",subscriber=" + + subscriber + ",contact=" + contact + ",id=" + id + + ",expires=" + expires + ")", LogLevel.MEDIUM); + SipURL request_uri = new SipURL(target); + NameAddress to_url = new NameAddress(target); + NameAddress from_url = new NameAddress(subscriber); + NameAddress contact_url; + if (contact != null) + contact_url = new NameAddress(contact); + else + contact_url = from_url; + String content_type = null; + String body = null; + Message req = MessageFactory.createSubscribeRequest(sip_provider, + request_uri, to_url, from_url, contact_url, event, id, + content_type, body); + req.setHeader(new AcceptHeader("application/pidf+xml")); + req.setExpiresHeader(new ExpiresHeader(expires)); + subscribe(req); + } + + /** + * Sends a new SUBSCRIBE request (starts a new subscription). It also + * initializes the dialog state information. + * + * @param req + * the SUBSCRIBE message + */ + public void subscribe(Message req) { + printLog("inside subscribe(req)", LogLevel.MEDIUM); + if (statusIs(D_TERMINATED)) { + printLog("subscription already terminated: request aborted", + LogLevel.MEDIUM); + return; + } + // else + if (statusIs(D_INIT)) { + changeStatus(D_SUBSCRIBING); + } + update(UAC, req); + // start client transaction + subscribe_transaction = new TransactionClient(sip_provider, req, this); + subscribe_transaction.request(); + } + + /** Sends a new SUBSCRIBE request (starts a new subscription). */ + public void reSubscribe(String target, String subscriber, String contact, + int expires) { + subscribe(target, subscriber, contact, expires); + } + + // ************** Inherited from TransactionClientListener ************** + + /** + * When the TransactionClient is (or goes) in "Proceeding" state and + * receives a new 1xx provisional response + */ + public void onTransProvisionalResponse(TransactionClient tc, Message resp) { + printLog("onTransProvisionalResponse()", LogLevel.MEDIUM); + // do nothing. + } + + /** + * When the TransactionClient goes into the "Completed" state receiving a + * 2xx response + */ + public void onTransSuccessResponse(TransactionClient tc, Message resp) { + printLog("onTransSuccessResponse()", LogLevel.MEDIUM); + if (!statusIs(D_ACTIVE)) { + changeStatus(D_ACCEPTED); + update(UAC, resp); + StatusLine status_line = resp.getStatusLine(); + if (listener != null) + listener.onDlgSubscriptionSuccess(this, status_line.getCode(), + status_line.getReason(), resp); + } else if (statusIs(D_ACTIVE)) { + StatusLine status_line = resp.getStatusLine(); + if (listener != null) + listener.onDlgSubscriptionSuccess(this, status_line.getCode(), + status_line.getReason(), resp); + } + } + + /** + * When the TransactionClient goes into the "Completed" state receiving a + * 300-699 response + */ + public void onTransFailureResponse(TransactionClient tc, Message resp) { + printLog("onTransFailureResponse()", LogLevel.MEDIUM); + changeStatus(D_TERMINATED); + StatusLine status_line = resp.getStatusLine(); + if (listener != null) + listener.onDlgSubscriptionFailure(this, status_line.getCode(), + status_line.getReason(), resp); + } + + /** + * When the TransactionClient goes into the "Terminated" state, caused by + * transaction timeout + */ + public void onTransTimeout(TransactionClient tc) { + printLog("onTransTimeout()", LogLevel.MEDIUM); + changeStatus(D_TERMINATED); + if (listener != null) + listener.onDlgSubscribeTimeout(this); + } + + // ***************** Inherited from SipProviderListener ***************** + + /** When a new Message is received by the SipProvider. */ + public void onReceivedMessage(SipProvider sip_provider, Message msg) { + printLog("onReceivedMessage()", LogLevel.MEDIUM); + if (statusIs(D_TERMINATED)) { + printLog("subscription already terminated: message discarded", + LogLevel.MEDIUM); + return; + } + // else + if (msg.isRequest() && msg.isNotify()) { + TransactionServer ts = new TransactionServer(sip_provider, msg, + null); + ts.respondWith(MessageFactory.createResponse(msg, 200, SipResponses + .reasonOf(200), null)); + + NameAddress to = msg.getToHeader().getNameAddress(); + NameAddress from = msg.getFromHeader().getNameAddress(); + NameAddress contact = null; + if (msg.hasContactHeader()) + contact = msg.getContactHeader().getNameAddress(); + String state = null; + if (msg.hasSubscriptionStateHeader()) + state = msg.getSubscriptionStateHeader().getState(); + String content_type = null; + if (msg.hasContentTypeHeader()) + content_type = msg.getContentTypeHeader().getContentType(); + String body = null; + if (msg.hasBody()) + body = msg.getBody(); + + if (listener != null) + listener.onDlgNotify(this, to, from, contact, state, + content_type, body, msg); + + if (state != null) { + if (state.equalsIgnoreCase(ACTIVE) && !statusIs(D_TERMINATED)) { + changeStatus(D_ACTIVE); + } else if (state.equalsIgnoreCase(PENDING) + && statusIs(D_ACCEPTED)) { + changeStatus(D_PENDING); + } else if (state.equalsIgnoreCase(TERMINATED) + && !statusIs(D_TERMINATED)) { + changeStatus(D_TERMINATED); + if (listener != null) + listener.onDlgSubscriptionTerminated(this); + } + } + } else { + printLog("message is not a NOTIFY: message discarded", + LogLevel.HIGH); + } + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("SubscriberDialog#" + dialog_sqn + ": " + str, level + + SipStack.LOG_LEVEL_DIALOG); + } + +} diff --git a/src/org/zoolu/sip/dialog/SubscriberDialogListener.java b/app/src/main/java/org/zoolu/sip/dialog/SubscriberDialogListener.java similarity index 97% rename from src/org/zoolu/sip/dialog/SubscriberDialogListener.java rename to app/src/main/java/org/zoolu/sip/dialog/SubscriberDialogListener.java index 9aa6e40..4cd35f8 100644 --- a/src/org/zoolu/sip/dialog/SubscriberDialogListener.java +++ b/app/src/main/java/org/zoolu/sip/dialog/SubscriberDialogListener.java @@ -1,56 +1,56 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.dialog; - -import org.zoolu.sip.message.Message; -import org.zoolu.sip.address.NameAddress; - -/** - * A SubscriberDialogListener listens for SubscriberDialog events. It collects - * all SubscriberDialog callback functions. - */ -public interface SubscriberDialogListener { - /** - * When a 2xx successfull final response is received for an SUBSCRIBE - * transaction. - */ - public void onDlgSubscriptionSuccess(SubscriberDialog dialog, int code, - String reason, Message msg); - - /** When a 300-699 response is received for an SUBSCRIBE transaction. */ - public void onDlgSubscriptionFailure(SubscriberDialog dialog, int code, - String reason, Message msg); - - /** When SUBSCRIBE transaction expires without a final response. */ - public void onDlgSubscribeTimeout(SubscriberDialog dialog); - - /** When the dialog is terminated. */ - public void onDlgSubscriptionTerminated(SubscriberDialog dialog); - - /** When an incoming NOTIFY is received. */ - public void onDlgNotify(SubscriberDialog dialog, NameAddress target, - NameAddress notifier, NameAddress contact, String state, - String content_type, String body, Message msg); - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.dialog; + +import org.zoolu.sip.message.Message; +import org.zoolu.sip.address.NameAddress; + +/** + * A SubscriberDialogListener listens for SubscriberDialog events. It collects + * all SubscriberDialog callback functions. + */ +public interface SubscriberDialogListener { + /** + * When a 2xx successfull final response is received for an SUBSCRIBE + * transaction. + */ + public void onDlgSubscriptionSuccess(SubscriberDialog dialog, int code, + String reason, Message msg); + + /** When a 300-699 response is received for an SUBSCRIBE transaction. */ + public void onDlgSubscriptionFailure(SubscriberDialog dialog, int code, + String reason, Message msg); + + /** When SUBSCRIBE transaction expires without a final response. */ + public void onDlgSubscribeTimeout(SubscriberDialog dialog); + + /** When the dialog is terminated. */ + public void onDlgSubscriptionTerminated(SubscriberDialog dialog); + + /** When an incoming NOTIFY is received. */ + public void onDlgNotify(SubscriberDialog dialog, NameAddress target, + NameAddress notifier, NameAddress contact, String state, + String content_type, String body, Message msg); + +} diff --git a/src/org/zoolu/sip/header/AcceptContactHeader.java b/app/src/main/java/org/zoolu/sip/header/AcceptContactHeader.java similarity index 94% rename from src/org/zoolu/sip/header/AcceptContactHeader.java rename to app/src/main/java/org/zoolu/sip/header/AcceptContactHeader.java index 7373cdd..ccb844b 100644 --- a/src/org/zoolu/sip/header/AcceptContactHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AcceptContactHeader.java @@ -1,27 +1,27 @@ - -package org.zoolu.sip.header; - -/** - * SIP Header AcceptContact - * - * Added by mandrajg for Sipdroid open source project. - * Used with MMTel/IMS. - */ - -public class AcceptContactHeader extends ParametricHeader { - public AcceptContactHeader(String icsi) { - super(SipHeaders.Accept_Contact, "*"); - if (icsi != null) - this.setParameter("+g.3gpp.icsi-ref", icsi); - } - - public AcceptContactHeader() { - super(SipHeaders.Accept_Contact, "*"); - } - - public AcceptContactHeader(Header hd) { - super(hd); - } - - -} + +package org.zoolu.sip.header; + +/** + * SIP Header AcceptContact + * + * Added by mandrajg for Sipdroid open source project. + * Used with MMTel/IMS. + */ + +public class AcceptContactHeader extends ParametricHeader { + public AcceptContactHeader(String icsi) { + super(SipHeaders.Accept_Contact, "*"); + if (icsi != null) + this.setParameter("+g.3gpp.icsi-ref", icsi); + } + + public AcceptContactHeader() { + super(SipHeaders.Accept_Contact, "*"); + } + + public AcceptContactHeader(Header hd) { + super(hd); + } + + +} diff --git a/src/org/zoolu/sip/header/AcceptHeader.java b/app/src/main/java/org/zoolu/sip/header/AcceptHeader.java similarity index 96% rename from src/org/zoolu/sip/header/AcceptHeader.java rename to app/src/main/java/org/zoolu/sip/header/AcceptHeader.java index 4177ff9..ccf07ce 100644 --- a/src/org/zoolu/sip/header/AcceptHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AcceptHeader.java @@ -1,49 +1,49 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header Accept */ -public class AcceptHeader extends ParametricHeader { - public AcceptHeader() { - super(SipHeaders.Accept, "application/sdp"); - } - - public AcceptHeader(String hvalue) { - super(SipHeaders.Accept, hvalue); - } - - public AcceptHeader(Header hd) { - super(hd); - } - - /** Gets the accept-range */ - public String getAcceptRange() { - return value; - } - - /** Sets the accept-range */ - public void setAcceptRange(String range) { - value = range; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header Accept */ +public class AcceptHeader extends ParametricHeader { + public AcceptHeader() { + super(SipHeaders.Accept, "application/sdp"); + } + + public AcceptHeader(String hvalue) { + super(SipHeaders.Accept, hvalue); + } + + public AcceptHeader(Header hd) { + super(hd); + } + + /** Gets the accept-range */ + public String getAcceptRange() { + return value; + } + + /** Sets the accept-range */ + public void setAcceptRange(String range) { + value = range; + } +} diff --git a/src/org/zoolu/sip/header/AlertInfoHeader.java b/app/src/main/java/org/zoolu/sip/header/AlertInfoHeader.java similarity index 96% rename from src/org/zoolu/sip/header/AlertInfoHeader.java rename to app/src/main/java/org/zoolu/sip/header/AlertInfoHeader.java index c8d5bf8..711e50c 100644 --- a/src/org/zoolu/sip/header/AlertInfoHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AlertInfoHeader.java @@ -1,59 +1,59 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header Allert-Info */ -public class AlertInfoHeader extends ParametricHeader { - public AlertInfoHeader(String absolute_uri) { - super(SipHeaders.Alert_Info, null); - setAbsoluteURI(absolute_uri); - } - - public AlertInfoHeader(Header hd) { - super(hd); - } - - /** Gets the absoluteURI */ - public String getAbsoluteURI() { - int begin = value.indexOf("<"); - int end = value.indexOf(">"); - if (begin < 0) - begin = 0; - else - begin++; - if (end < 0) - end = value.length(); - return value.substring(begin, end); - } - - /** Sets the absoluteURI */ - public void setAbsoluteURI(String absolute_uri) { - absolute_uri = absolute_uri.trim(); - if (absolute_uri.indexOf("<") < 0) - absolute_uri = "<" + absolute_uri; - if (absolute_uri.indexOf(">") < 0) - absolute_uri = absolute_uri + ">"; - value = absolute_uri; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header Allert-Info */ +public class AlertInfoHeader extends ParametricHeader { + public AlertInfoHeader(String absolute_uri) { + super(SipHeaders.Alert_Info, null); + setAbsoluteURI(absolute_uri); + } + + public AlertInfoHeader(Header hd) { + super(hd); + } + + /** Gets the absoluteURI */ + public String getAbsoluteURI() { + int begin = value.indexOf("<"); + int end = value.indexOf(">"); + if (begin < 0) + begin = 0; + else + begin++; + if (end < 0) + end = value.length(); + return value.substring(begin, end); + } + + /** Sets the absoluteURI */ + public void setAbsoluteURI(String absolute_uri) { + absolute_uri = absolute_uri.trim(); + if (absolute_uri.indexOf("<") < 0) + absolute_uri = "<" + absolute_uri; + if (absolute_uri.indexOf(">") < 0) + absolute_uri = absolute_uri + ">"; + value = absolute_uri; + } +} diff --git a/src/org/zoolu/sip/header/AllowEventsHeader.java b/app/src/main/java/org/zoolu/sip/header/AllowEventsHeader.java similarity index 96% rename from src/org/zoolu/sip/header/AllowEventsHeader.java rename to app/src/main/java/org/zoolu/sip/header/AllowEventsHeader.java index 467037c..7076059 100644 --- a/src/org/zoolu/sip/header/AllowEventsHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AllowEventsHeader.java @@ -1,57 +1,57 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import java.util.Vector; - -/** SIP Header Allow-Events */ -public class AllowEventsHeader extends ListHeader { - public AllowEventsHeader(String hvalue) { - super(SipHeaders.Allow_Events, hvalue); - } - - public AllowEventsHeader(Header hd) { - super(hd); - } - - /** Gets list of events (as Vector of Strings). */ - /* HSC CHANGES BEGIN */ - public Vector getEvents() { - /* HSC CHANGES END */ - return super.getElements(); - } - - /** Sets the list of events. */ - /* HSC CHANGES BEGIN */ - public void setEvents(Vector events) { - /* HSC CHANGES END */ - super.setElements(events); - } - - /** Adds a new event to the event list. */ - public void addEvent(String event) { - super.addElement(event); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import java.util.Vector; + +/** SIP Header Allow-Events */ +public class AllowEventsHeader extends ListHeader { + public AllowEventsHeader(String hvalue) { + super(SipHeaders.Allow_Events, hvalue); + } + + public AllowEventsHeader(Header hd) { + super(hd); + } + + /** Gets list of events (as Vector of Strings). */ + /* HSC CHANGES BEGIN */ + public Vector getEvents() { + /* HSC CHANGES END */ + return super.getElements(); + } + + /** Sets the list of events. */ + /* HSC CHANGES BEGIN */ + public void setEvents(Vector events) { + /* HSC CHANGES END */ + super.setElements(events); + } + + /** Adds a new event to the event list. */ + public void addEvent(String event) { + super.addElement(event); + } +} diff --git a/src/org/zoolu/sip/header/AllowHeader.java b/app/src/main/java/org/zoolu/sip/header/AllowHeader.java similarity index 96% rename from src/org/zoolu/sip/header/AllowHeader.java rename to app/src/main/java/org/zoolu/sip/header/AllowHeader.java index a82e850..0c1ea0a 100644 --- a/src/org/zoolu/sip/header/AllowHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AllowHeader.java @@ -1,56 +1,56 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import java.util.Vector; - -/** SIP Header Allow */ -public class AllowHeader extends ListHeader { - public AllowHeader(String hvalue) { - super(SipHeaders.Allow, hvalue); - } - - public AllowHeader(Header hd) { - super(hd); - } - - /** Gets list of methods (as Vector of Strings). */ - /* HSC CHANGES START */ - public Vector getMethods() { - return super.getElements(); - } - - /** Sets the list of methods. */ - public void setMethod(Vector methods) { - super.setElements(methods); - } - - /* HSC CHANGES END */ - - /** Adds a new method to the methods list. */ - public void addMethod(String method) { - super.addElement(method); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import java.util.Vector; + +/** SIP Header Allow */ +public class AllowHeader extends ListHeader { + public AllowHeader(String hvalue) { + super(SipHeaders.Allow, hvalue); + } + + public AllowHeader(Header hd) { + super(hd); + } + + /** Gets list of methods (as Vector of Strings). */ + /* HSC CHANGES START */ + public Vector getMethods() { + return super.getElements(); + } + + /** Sets the list of methods. */ + public void setMethod(Vector methods) { + super.setElements(methods); + } + + /* HSC CHANGES END */ + + /** Adds a new method to the methods list. */ + public void addMethod(String method) { + super.addElement(method); + } +} diff --git a/src/org/zoolu/sip/header/AuthenticationHeader.java b/app/src/main/java/org/zoolu/sip/header/AuthenticationHeader.java similarity index 96% rename from src/org/zoolu/sip/header/AuthenticationHeader.java rename to app/src/main/java/org/zoolu/sip/header/AuthenticationHeader.java index f250a3f..90f65dc 100644 --- a/src/org/zoolu/sip/header/AuthenticationHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AuthenticationHeader.java @@ -1,407 +1,407 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; -import java.util.Vector; - -/** - * Abstract header for various authentication schemes - *

- * It is inherited by WwwAuthenticateHeader, AuthorizationHeader, etc. - */ -public abstract class AuthenticationHeader extends Header { - - /** Lienar white space separator inserted bethween parameters. */ - // public static String LWS_SEPARATOR="\r\n "; - public static String LWS_SEPARATOR = " "; - - /** Array of parameters that are quoted. */ - public static String[] QUOTED_PARAMETERS = { "auts", "cnonce", "nextnonce", - "nonce", "opaque", "realm", "response", "rspauth", "uri", - "username" }; - - /** Whether is a quoted parameter (i.e. belongs to QUOTED_PARAMETERS). */ - private static boolean isQuotedParameter(String param_name) { - for (int i = 0; i < QUOTED_PARAMETERS.length; i++) - if (param_name.equalsIgnoreCase(QUOTED_PARAMETERS[i])) - return true; - return false; - } - - /** Creates a new AuthenticationHeader. */ - public AuthenticationHeader(String hname, String hvalue) { - super(hname, hvalue); - } - - /** Creates a new AuthenticationHeader. */ - public AuthenticationHeader(Header hd) { - super(hd); - } - - /** - * Creates a new AuthenticationHeader. specifing the auth_scheme and - * the vector of authentication parameters. - *

- * auth_params is a vector of String of the form parm_name - * "=" parm_value - */ - public AuthenticationHeader(String hname, String auth_scheme, - /* HSC CHANGES START */ - Vector auth_params) { - /* HSC CHANGES END */ - super(hname, auth_scheme); - if (auth_params.size() > 0) - value += " " + (String) auth_params.elementAt(0); - for (int i = 1; i < auth_params.size(); i++) - value += "," + LWS_SEPARATOR + (String) auth_params.elementAt(i); - } - - /** - * Adds a parameter. If param_name belongs to QUOTED_PARAMETERS, - * param_value is quoted (if already not). - */ - public void addParameter(String param_name, String param_value) { - if (param_value.indexOf('"') < 0 && isQuotedParameter(param_name)) - addQuotedParameter(param_name, param_value); - else - addUnquotedParameter(param_name, param_value); - } - - /** Adds a parameter without inserting quotes. */ - public void addUnquotedParameter(String param_name, String param_value) { - if (value.indexOf('=') < 0) - value += " "; - else - value += "," + LWS_SEPARATOR; - value += param_name + "=" + param_value; - } - - /** Adds a parameter with quotes. */ - public void addQuotedParameter(String param_name, String param_value) { - if (value.indexOf('=') < 0) - value += " "; - else - value += "," + LWS_SEPARATOR; - if (param_value.indexOf('"') >= 0) - value += param_name + "=" + param_value; - else - value += param_name + "=\"" + param_value + "\""; - } - - /** Whether has parameter param_name */ - public boolean hasParameter(String param_name) { - char[] name_separators = { '=', ' ', '\t', '\r', '\n' }; - SipParser par = new SipParser(value); - par.skipString(); // skip the auth_scheme - par.skipWSPCRLF(); - while (par.hasMore()) { - String name = par.getWord(name_separators); - if (name.equals(param_name)) - return true; - par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); - } - return false; - } - - /** Returns the parameter param_name, without quotes. */ - public String getParameter(String param_name) { - char[] name_separators = { '=', ' ', '\t' }; - SipParser par = new SipParser(value); - par.skipString(); // skip the auth_scheme - par.skipWSPCRLF(); - while (par.hasMore()) { - String name = par.getWord(name_separators); - if (name.equals(param_name)) { - par.goTo('=').skipChar().skipWSP(); - int comma = par.indexOfCommaHeaderSeparator(); - if (comma >= 0) - par = new SipParser(par.getString(comma - par.getPos())); - return par.getStringUnquoted(); - } else - par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); - } - return null; - } - - /** - * Gets a String Vector of parameter names. - * - * @returns a Vector of String. - */ - /* HSC CHANGES START */ - public Vector getParameters() { - char[] name_separators = { '=', ' ', '\t' }; - SipParser par = new SipParser(value); - par.skipString(); // skip the auth_scheme - par.skipWSPCRLF(); - Vector names = new Vector(); - while (par.hasMore()) { - String name = par.getWord(name_separators); - names.addElement(name); - par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); - } - return names; - } - - /* HSC CHANGES END */ - - /** Gets the athentication scheme (i.e. the first token). */ - public String getAuthScheme() { - SipParser par = new SipParser(value); - return par.getString(); - } - - // ***************** quoted parameters ***************** - - /** Whether has realm */ - public boolean hasRealmParam() { - return hasParameter("realm"); - } - - /** Returns the realm (unquoted) */ - public String getRealmParam() { - return getParameter("realm"); - } - - /** Adds the realm */ - public void addRealmParam(String unquoted_realm) { - addQuotedParameter("realm", unquoted_realm); - } - - /** Whether has nonce */ - public boolean hasNonceParam() { - return hasParameter("nonce"); - } - - /** Returns the nonce (unquoted) */ - public String getNonceParam() { - return getParameter("nonce"); - } - - /** Adds the nonce */ - public void addNonceParam(String unquoted_nonce) { - addQuotedParameter("nonce", unquoted_nonce); - } - - /** Whether has opaque */ - public boolean hasOpaqueParam() { - return hasParameter("opaque"); - } - - /** Returns the opaque (unquoted) */ - public String getOpaqueParam() { - return getParameter("opaque"); - } - - /** Adds the opaque */ - public void addOpaqueParam(String unquoted_opaque) { - addQuotedParameter("opaque", unquoted_opaque); - } - - /** Whether has username */ - public boolean hasUsernameParam() { - return hasParameter("username"); - } - - /** Returns the username (unquoted) */ - public String getUsernameParam() { - return getParameter("username"); - } - - /** Adds the username */ - public void addUsernameParam(String unquoted_username) { - addQuotedParameter("username", unquoted_username); - } - - /** Whether has uri */ - public boolean hasUriParam() { - return hasParameter("uri"); - } - - /** Returns the uri (unquoted) */ - public String getUriParam() { - return getParameter("uri"); - } - - /** Adds the uri */ - public void addUriParam(String unquoted_uri) { - addQuotedParameter("uri", unquoted_uri); - } - - /** Whether has response */ - public boolean hasResponseParam() { - return hasParameter("response"); - } - - /** Returns the response (unquoted) */ - public String getResponseParam() { - return getParameter("response"); - } - - /** Adds the response */ - public void addResponseParam(String unquoted_response) { - addQuotedParameter("response", unquoted_response); - } - - /** Whether has cnonce */ - public boolean hasCnonceParam() { - return hasParameter("cnonce"); - } - - /** Returns the cnonce (unquoted) */ - public String getCnonceParam() { - return getParameter("cnonce"); - } - - /** Adds the cnonce */ - public void addCnonceParam(String unquoted_cnonce) { - addQuotedParameter("cnonce", unquoted_cnonce); - } - - /** Whether has rspauth */ - public boolean hasRspauthParam() { - return hasParameter("rspauth"); - } - - /** Returns the rspauth (unquoted) */ - public String getRspauthParam() { - return getParameter("rspauth"); - } - - /** Adds the rspauth */ - public void addRspauthParam(String unquoted_rspauth) { - addQuotedParameter("rspauth", unquoted_rspauth); - } - - /** Whether has auts */ - public boolean hasAutsParam() { - return hasParameter("auts"); - } - - /** Returns the auts */ - public String getAutsParam() { - return getParameter("auts"); - } - - /** Adds the auts */ - public void addAutsParam(String unquoted_auts) { - addQuotedParameter("auts", unquoted_auts); - } - - /** Whether has nextnonce */ - public boolean hasNextnonceParam() { - return hasParameter("nextnonce"); - } - - /** Returns the nextnonce */ - public String getNextnonceParam() { - return getParameter("nextnonce"); - } - - /** Adds the nextnonce */ - public void addNextnonceParam(String unquoted_nextnonce) { - addQuotedParameter("nextnonce", unquoted_nextnonce); - } - - /** Whether has qop-options */ - public boolean hasQopOptionsParam() { - return hasParameter("qop"); - } - - /** Gets the qop-options */ - /* - * public String[] getQopOptionsParam() { Vector aux=new Vector(); Parser - * par=new Parser(getParameter("qop")); char[] separators={','}; String - * qop=null; while ((qop=par.getWord(separators))!=null) - * aux.addElement(qop); if (aux.size()==0) return null; String[] - * qop_options=new String[aux.size()]; for (int i=0; i0) sb.append(","); sb.append(qop_options[i]); } - * addQuotedParameter("qop",sb.toString()); } - */ - /** Adds the qop-options */ - public void addQopOptionsParam(String unquoted_qop_options) { - addQuotedParameter("qop", unquoted_qop_options); - } - - // **************** unquoted parameters **************** - - /** Whether has qop */ - public boolean hasQopParam() { - return hasParameter("qop"); - } - - /** Returns the qop */ - public String getQopParam() { - return getParameter("qop"); - } - - /** Adds the qop */ - public void addQopParam(String qop) { - addUnquotedParameter("qop", qop); - } - - /** Whether has nc */ - public boolean hasNcParam() { - return hasParameter("nc"); - } - - /** Returns the nc */ - public String getNcParam() { - return getParameter("nc"); - } - - /** Adds the nc */ - public void addNcParam(String nc) { - addUnquotedParameter("nc", nc); - } - - /** Whether has algorithm */ - public boolean hasAlgorithmParam() { - return hasParameter("algorithm"); - } - - /** Returns the algorithm */ - public String getAlgorithParam() { - return getParameter("algorithm"); - } - - /** Adds the algorithm */ - public void addAlgorithParam(String algorithm) { - addUnquotedParameter("algorithm", algorithm); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; +import java.util.Vector; + +/** + * Abstract header for various authentication schemes + *

+ * It is inherited by WwwAuthenticateHeader, AuthorizationHeader, etc. + */ +public abstract class AuthenticationHeader extends Header { + + /** Lienar white space separator inserted bethween parameters. */ + // public static String LWS_SEPARATOR="\r\n "; + public static String LWS_SEPARATOR = " "; + + /** Array of parameters that are quoted. */ + public static String[] QUOTED_PARAMETERS = { "auts", "cnonce", "nextnonce", + "nonce", "opaque", "realm", "response", "rspauth", "uri", + "username" }; + + /** Whether is a quoted parameter (i.e. belongs to QUOTED_PARAMETERS). */ + private static boolean isQuotedParameter(String param_name) { + for (int i = 0; i < QUOTED_PARAMETERS.length; i++) + if (param_name.equalsIgnoreCase(QUOTED_PARAMETERS[i])) + return true; + return false; + } + + /** Creates a new AuthenticationHeader. */ + public AuthenticationHeader(String hname, String hvalue) { + super(hname, hvalue); + } + + /** Creates a new AuthenticationHeader. */ + public AuthenticationHeader(Header hd) { + super(hd); + } + + /** + * Creates a new AuthenticationHeader. specifing the auth_scheme and + * the vector of authentication parameters. + *

+ * auth_params is a vector of String of the form parm_name + * "=" parm_value + */ + public AuthenticationHeader(String hname, String auth_scheme, + /* HSC CHANGES START */ + Vector auth_params) { + /* HSC CHANGES END */ + super(hname, auth_scheme); + if (auth_params.size() > 0) + value += " " + (String) auth_params.elementAt(0); + for (int i = 1; i < auth_params.size(); i++) + value += "," + LWS_SEPARATOR + (String) auth_params.elementAt(i); + } + + /** + * Adds a parameter. If param_name belongs to QUOTED_PARAMETERS, + * param_value is quoted (if already not). + */ + public void addParameter(String param_name, String param_value) { + if (param_value.indexOf('"') < 0 && isQuotedParameter(param_name)) + addQuotedParameter(param_name, param_value); + else + addUnquotedParameter(param_name, param_value); + } + + /** Adds a parameter without inserting quotes. */ + public void addUnquotedParameter(String param_name, String param_value) { + if (value.indexOf('=') < 0) + value += " "; + else + value += "," + LWS_SEPARATOR; + value += param_name + "=" + param_value; + } + + /** Adds a parameter with quotes. */ + public void addQuotedParameter(String param_name, String param_value) { + if (value.indexOf('=') < 0) + value += " "; + else + value += "," + LWS_SEPARATOR; + if (param_value.indexOf('"') >= 0) + value += param_name + "=" + param_value; + else + value += param_name + "=\"" + param_value + "\""; + } + + /** Whether has parameter param_name */ + public boolean hasParameter(String param_name) { + char[] name_separators = { '=', ' ', '\t', '\r', '\n' }; + SipParser par = new SipParser(value); + par.skipString(); // skip the auth_scheme + par.skipWSPCRLF(); + while (par.hasMore()) { + String name = par.getWord(name_separators); + if (name.equals(param_name)) + return true; + par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); + } + return false; + } + + /** Returns the parameter param_name, without quotes. */ + public String getParameter(String param_name) { + char[] name_separators = { '=', ' ', '\t' }; + SipParser par = new SipParser(value); + par.skipString(); // skip the auth_scheme + par.skipWSPCRLF(); + while (par.hasMore()) { + String name = par.getWord(name_separators); + if (name.equals(param_name)) { + par.goTo('=').skipChar().skipWSP(); + int comma = par.indexOfCommaHeaderSeparator(); + if (comma >= 0) + par = new SipParser(par.getString(comma - par.getPos())); + return par.getStringUnquoted(); + } else + par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); + } + return null; + } + + /** + * Gets a String Vector of parameter names. + * + * @returns a Vector of String. + */ + /* HSC CHANGES START */ + public Vector getParameters() { + char[] name_separators = { '=', ' ', '\t' }; + SipParser par = new SipParser(value); + par.skipString(); // skip the auth_scheme + par.skipWSPCRLF(); + Vector names = new Vector(); + while (par.hasMore()) { + String name = par.getWord(name_separators); + names.addElement(name); + par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); + } + return names; + } + + /* HSC CHANGES END */ + + /** Gets the athentication scheme (i.e. the first token). */ + public String getAuthScheme() { + SipParser par = new SipParser(value); + return par.getString(); + } + + // ***************** quoted parameters ***************** + + /** Whether has realm */ + public boolean hasRealmParam() { + return hasParameter("realm"); + } + + /** Returns the realm (unquoted) */ + public String getRealmParam() { + return getParameter("realm"); + } + + /** Adds the realm */ + public void addRealmParam(String unquoted_realm) { + addQuotedParameter("realm", unquoted_realm); + } + + /** Whether has nonce */ + public boolean hasNonceParam() { + return hasParameter("nonce"); + } + + /** Returns the nonce (unquoted) */ + public String getNonceParam() { + return getParameter("nonce"); + } + + /** Adds the nonce */ + public void addNonceParam(String unquoted_nonce) { + addQuotedParameter("nonce", unquoted_nonce); + } + + /** Whether has opaque */ + public boolean hasOpaqueParam() { + return hasParameter("opaque"); + } + + /** Returns the opaque (unquoted) */ + public String getOpaqueParam() { + return getParameter("opaque"); + } + + /** Adds the opaque */ + public void addOpaqueParam(String unquoted_opaque) { + addQuotedParameter("opaque", unquoted_opaque); + } + + /** Whether has username */ + public boolean hasUsernameParam() { + return hasParameter("username"); + } + + /** Returns the username (unquoted) */ + public String getUsernameParam() { + return getParameter("username"); + } + + /** Adds the username */ + public void addUsernameParam(String unquoted_username) { + addQuotedParameter("username", unquoted_username); + } + + /** Whether has uri */ + public boolean hasUriParam() { + return hasParameter("uri"); + } + + /** Returns the uri (unquoted) */ + public String getUriParam() { + return getParameter("uri"); + } + + /** Adds the uri */ + public void addUriParam(String unquoted_uri) { + addQuotedParameter("uri", unquoted_uri); + } + + /** Whether has response */ + public boolean hasResponseParam() { + return hasParameter("response"); + } + + /** Returns the response (unquoted) */ + public String getResponseParam() { + return getParameter("response"); + } + + /** Adds the response */ + public void addResponseParam(String unquoted_response) { + addQuotedParameter("response", unquoted_response); + } + + /** Whether has cnonce */ + public boolean hasCnonceParam() { + return hasParameter("cnonce"); + } + + /** Returns the cnonce (unquoted) */ + public String getCnonceParam() { + return getParameter("cnonce"); + } + + /** Adds the cnonce */ + public void addCnonceParam(String unquoted_cnonce) { + addQuotedParameter("cnonce", unquoted_cnonce); + } + + /** Whether has rspauth */ + public boolean hasRspauthParam() { + return hasParameter("rspauth"); + } + + /** Returns the rspauth (unquoted) */ + public String getRspauthParam() { + return getParameter("rspauth"); + } + + /** Adds the rspauth */ + public void addRspauthParam(String unquoted_rspauth) { + addQuotedParameter("rspauth", unquoted_rspauth); + } + + /** Whether has auts */ + public boolean hasAutsParam() { + return hasParameter("auts"); + } + + /** Returns the auts */ + public String getAutsParam() { + return getParameter("auts"); + } + + /** Adds the auts */ + public void addAutsParam(String unquoted_auts) { + addQuotedParameter("auts", unquoted_auts); + } + + /** Whether has nextnonce */ + public boolean hasNextnonceParam() { + return hasParameter("nextnonce"); + } + + /** Returns the nextnonce */ + public String getNextnonceParam() { + return getParameter("nextnonce"); + } + + /** Adds the nextnonce */ + public void addNextnonceParam(String unquoted_nextnonce) { + addQuotedParameter("nextnonce", unquoted_nextnonce); + } + + /** Whether has qop-options */ + public boolean hasQopOptionsParam() { + return hasParameter("qop"); + } + + /** Gets the qop-options */ + /* + * public String[] getQopOptionsParam() { Vector aux=new Vector(); Parser + * par=new Parser(getParameter("qop")); char[] separators={','}; String + * qop=null; while ((qop=par.getWord(separators))!=null) + * aux.addElement(qop); if (aux.size()==0) return null; String[] + * qop_options=new String[aux.size()]; for (int i=0; i0) sb.append(","); sb.append(qop_options[i]); } + * addQuotedParameter("qop",sb.toString()); } + */ + /** Adds the qop-options */ + public void addQopOptionsParam(String unquoted_qop_options) { + addQuotedParameter("qop", unquoted_qop_options); + } + + // **************** unquoted parameters **************** + + /** Whether has qop */ + public boolean hasQopParam() { + return hasParameter("qop"); + } + + /** Returns the qop */ + public String getQopParam() { + return getParameter("qop"); + } + + /** Adds the qop */ + public void addQopParam(String qop) { + addUnquotedParameter("qop", qop); + } + + /** Whether has nc */ + public boolean hasNcParam() { + return hasParameter("nc"); + } + + /** Returns the nc */ + public String getNcParam() { + return getParameter("nc"); + } + + /** Adds the nc */ + public void addNcParam(String nc) { + addUnquotedParameter("nc", nc); + } + + /** Whether has algorithm */ + public boolean hasAlgorithmParam() { + return hasParameter("algorithm"); + } + + /** Returns the algorithm */ + public String getAlgorithParam() { + return getParameter("algorithm"); + } + + /** Adds the algorithm */ + public void addAlgorithParam(String algorithm) { + addUnquotedParameter("algorithm", algorithm); + } + +} diff --git a/src/org/zoolu/sip/header/AuthenticationInfoHeader.java b/app/src/main/java/org/zoolu/sip/header/AuthenticationInfoHeader.java similarity index 96% rename from src/org/zoolu/sip/header/AuthenticationInfoHeader.java rename to app/src/main/java/org/zoolu/sip/header/AuthenticationInfoHeader.java index 455c4bc..4753805 100644 --- a/src/org/zoolu/sip/header/AuthenticationInfoHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AuthenticationInfoHeader.java @@ -1,124 +1,124 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; -import java.util.Vector; - -/** SIP AuthenticationInfo header */ -public class AuthenticationInfoHeader extends AuthenticationHeader { - /** Creates a new AuthenticationInfoHeader */ - public AuthenticationInfoHeader() { - super(SipHeaders.Authentication_Info, ""); - } - - /** Creates a new AuthenticationInfoHeader */ - public AuthenticationInfoHeader(String hvalue) { - super(SipHeaders.Authentication_Info, hvalue); - } - - /** Creates a new AuthenticationInfoHeader */ - public AuthenticationInfoHeader(Header hd) { - super(hd); - } - - /** - * Creates a new AuthenticationInfoHeader specifing the auth_scheme - * and the vector of authentication parameters. - *

- * auth_param is a vector of String of the form parm_name - * "=" parm_value - */ - /* HSC CHANGES START */ - public AuthenticationInfoHeader(Vector auth_params) { - /* HSC CHANGES END */ - super(SipHeaders.Authentication_Info, "", auth_params); - } - - /** Whether has parameter param_name */ - public boolean hasParameter(String param_name) { - char[] name_separators = { '=', ' ', '\t', '\r', '\n' }; - SipParser par = new SipParser(value); - // par.skipString(); // skip the auth_scheme - par.skipWSPCRLF(); - while (par.hasMore()) { - String name = par.getWord(name_separators); - if (name.equals(param_name)) - return true; - par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); - } - return false; - } - - /** Returns the parameter param_name, in case removing quotes. */ - public String getParameter(String param_name) { - char[] name_separators = { '=', ' ', '\t' }; - SipParser par = new SipParser(value); - // par.skipString(); // skip the auth_scheme - par.skipWSPCRLF(); - while (par.hasMore()) { - String name = par.getWord(name_separators); - if (name.equals(param_name)) { - par.goTo('=').skipChar().skipWSP(); - int comma = par.indexOfCommaHeaderSeparator(); - if (comma >= 0) - par = new SipParser(par.getString(comma - par.getPos())); - return par.getStringUnquoted(); - } else - par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); - } - return null; - } - - /** - * Gets a String Vector of parameter names. - * - * @returns a Vector of String. - */ - /* HSC CHANGES BEGIN */ - public Vector getParameters() { - char[] name_separators = { '=', ' ', '\t' }; - SipParser par = new SipParser(value); - // par.skipString(); // skip the auth_scheme - par.skipWSPCRLF(); - Vector names = new Vector(); - while (par.hasMore()) { - String name = par.getWord(name_separators); - names.addElement(name); - par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); - } - return names; - } - - /* HSC CHANGES END */ - - /** - * Gets the athentication scheme. Note that for AuthenticationInfoHeader it - * always return null. - */ - public String getAuthScheme() { - return null; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; +import java.util.Vector; + +/** SIP AuthenticationInfo header */ +public class AuthenticationInfoHeader extends AuthenticationHeader { + /** Creates a new AuthenticationInfoHeader */ + public AuthenticationInfoHeader() { + super(SipHeaders.Authentication_Info, ""); + } + + /** Creates a new AuthenticationInfoHeader */ + public AuthenticationInfoHeader(String hvalue) { + super(SipHeaders.Authentication_Info, hvalue); + } + + /** Creates a new AuthenticationInfoHeader */ + public AuthenticationInfoHeader(Header hd) { + super(hd); + } + + /** + * Creates a new AuthenticationInfoHeader specifing the auth_scheme + * and the vector of authentication parameters. + *

+ * auth_param is a vector of String of the form parm_name + * "=" parm_value + */ + /* HSC CHANGES START */ + public AuthenticationInfoHeader(Vector auth_params) { + /* HSC CHANGES END */ + super(SipHeaders.Authentication_Info, "", auth_params); + } + + /** Whether has parameter param_name */ + public boolean hasParameter(String param_name) { + char[] name_separators = { '=', ' ', '\t', '\r', '\n' }; + SipParser par = new SipParser(value); + // par.skipString(); // skip the auth_scheme + par.skipWSPCRLF(); + while (par.hasMore()) { + String name = par.getWord(name_separators); + if (name.equals(param_name)) + return true; + par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); + } + return false; + } + + /** Returns the parameter param_name, in case removing quotes. */ + public String getParameter(String param_name) { + char[] name_separators = { '=', ' ', '\t' }; + SipParser par = new SipParser(value); + // par.skipString(); // skip the auth_scheme + par.skipWSPCRLF(); + while (par.hasMore()) { + String name = par.getWord(name_separators); + if (name.equals(param_name)) { + par.goTo('=').skipChar().skipWSP(); + int comma = par.indexOfCommaHeaderSeparator(); + if (comma >= 0) + par = new SipParser(par.getString(comma - par.getPos())); + return par.getStringUnquoted(); + } else + par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); + } + return null; + } + + /** + * Gets a String Vector of parameter names. + * + * @returns a Vector of String. + */ + /* HSC CHANGES BEGIN */ + public Vector getParameters() { + char[] name_separators = { '=', ' ', '\t' }; + SipParser par = new SipParser(value); + // par.skipString(); // skip the auth_scheme + par.skipWSPCRLF(); + Vector names = new Vector(); + while (par.hasMore()) { + String name = par.getWord(name_separators); + names.addElement(name); + par.goToCommaHeaderSeparator().skipChar().skipWSPCRLF(); + } + return names; + } + + /* HSC CHANGES END */ + + /** + * Gets the athentication scheme. Note that for AuthenticationInfoHeader it + * always return null. + */ + public String getAuthScheme() { + return null; + } +} diff --git a/src/org/zoolu/sip/header/AuthorizationHeader.java b/app/src/main/java/org/zoolu/sip/header/AuthorizationHeader.java similarity index 97% rename from src/org/zoolu/sip/header/AuthorizationHeader.java rename to app/src/main/java/org/zoolu/sip/header/AuthorizationHeader.java index 9dc4c32..96add80 100644 --- a/src/org/zoolu/sip/header/AuthorizationHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/AuthorizationHeader.java @@ -1,51 +1,51 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import java.util.Vector; - -/** SIP Authorization header */ -public class AuthorizationHeader extends AuthenticationHeader { - /** Creates a new AuthorizationHeader */ - public AuthorizationHeader(String hvalue) { - super(SipHeaders.Authorization, hvalue); - } - - /** Creates a new AuthorizationHeader */ - public AuthorizationHeader(Header hd) { - super(hd); - } - - /** - * Creates a new AuthorizationHeader specifing the auth_scheme and - * the vector of authentication parameters. - *

- * auth_param is a vector of String of the form parm_name - * "=" parm_value - */ - public AuthorizationHeader(String auth_scheme, Vector auth_params) { - super(SipHeaders.Authorization, auth_scheme, auth_params); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import java.util.Vector; + +/** SIP Authorization header */ +public class AuthorizationHeader extends AuthenticationHeader { + /** Creates a new AuthorizationHeader */ + public AuthorizationHeader(String hvalue) { + super(SipHeaders.Authorization, hvalue); + } + + /** Creates a new AuthorizationHeader */ + public AuthorizationHeader(Header hd) { + super(hd); + } + + /** + * Creates a new AuthorizationHeader specifing the auth_scheme and + * the vector of authentication parameters. + *

+ * auth_param is a vector of String of the form parm_name + * "=" parm_value + */ + public AuthorizationHeader(String auth_scheme, Vector auth_params) { + super(SipHeaders.Authorization, auth_scheme, auth_params); + } +} diff --git a/src/org/zoolu/sip/header/BaseSipHeaders.java b/app/src/main/java/org/zoolu/sip/header/BaseSipHeaders.java similarity index 97% rename from src/org/zoolu/sip/header/BaseSipHeaders.java rename to app/src/main/java/org/zoolu/sip/header/BaseSipHeaders.java index b20ff30..2b29658 100644 --- a/src/org/zoolu/sip/header/BaseSipHeaders.java +++ b/app/src/main/java/org/zoolu/sip/header/BaseSipHeaders.java @@ -1,249 +1,249 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SipHeaders simply collects all standard SIP header names. */ -public abstract class BaseSipHeaders { - /** String "Accept" */ - public static final String Accept = "Accept"; - /** String "Alert-Info" */ - public static final String Alert_Info = "Alert-Info"; - /** String "Allow" */ - public static final String Allow = "Allow"; - /** String "Authentication-Info" */ - public static final String Authentication_Info = "Authentication-Info"; - /** String "Authorization" */ - public static final String Authorization = "Authorization"; - /** String "Call-ID" */ - public static final String Call_ID = "Call-ID"; - /** String "i" */ - public static final String Call_ID_short = "i"; - /** String "Contact" */ - public static final String Contact = "Contact"; - /** String "m" */ - public static final String Contact_short = "m"; - /** String "Content-Length" */ - public static final String Content_Length = "Content-Length"; - /** String "l" */ - public static final String Content_Length_short = "l"; - /** String "Content-Type" */ - public static final String Content_Type = "Content-Type"; - /** String "c" */ - public static final String Content_Type_short = "c"; - /** String "CSeq" */ - public static final String CSeq = "CSeq"; - /** String "Date" */ - public static final String Date = "Date"; - /** String "Expires" */ - public static final String Expires = "Expires"; - /** String "From" */ - public static final String From = "From"; - /** String "f" */ - public static final String From_short = "f"; - /** String "User-Agent" */ - public static final String User_Agent = "User-Agent"; - /** String "Max-Forwards" */ - public static final String Max_Forwards = "Max-Forwards"; - /** String "Proxy-Authenticate" */ - public static final String Proxy_Authenticate = "Proxy-Authenticate"; - /** String "Proxy-Authorization" */ - public static final String Proxy_Authorization = "Proxy-Authorization"; - /** String "Proxy-Require" */ - public static final String Proxy_Require = "Proxy-Require"; - /** String "Record-Route" */ - public static final String Record_Route = "Record-Route"; - /** String "Require" */ - public static final String Require = "Require"; - /** String "Route" */ - public static final String Route = "Route"; - /** String "Server" */ - public static final String Server = "Server"; - /** String "Subject" */ - public static final String Subject = "Subject"; - /** String "s" */ - public static final String Subject_short = "s"; - /** String "Supported" */ - public static final String Supported = "Supported"; - /** String "k" */ - public static final String Supported_short = "k"; - /** String "To" */ - public static final String To = "To"; - /** String "t" */ - public static final String To_short = "t"; - /** String "Unsupported" */ - public static final String Unsupported = "Unsupported"; - /** String "Via" */ - public static final String Via = "Via"; - /** String "v" */ - public static final String Via_short = "v"; - /** String "WWW-Authenticate" */ - public static final String WWW_Authenticate = "WWW-Authenticate"; - - /** Whether s1 and s2 are case-unsensitive-equal. */ - protected static boolean same(String s1, String s2) { // return - // s1.compareToIgnoreCase(s2)==0; - return s1.equalsIgnoreCase(s2); - } - - /** Whether str is a Accept field */ - public static boolean isAccept(String str) { - return same(str, Accept); - } - - /** Whether str is a Alert_Info field */ - public static boolean isAlert_Info(String str) { - return same(str, Alert_Info); - } - - /** Whether str is a Allow field */ - public static boolean isAllow(String str) { - return same(str, Allow); - } - - /** Whether str is a Authentication_Info field */ - public static boolean isAuthentication_Info(String str) { - return same(str, Authentication_Info); - } - - /** Whether str is a Authorization field */ - public static boolean isAuthorization(String str) { - return same(str, Authorization); - } - - /** Whether str is a Call-ID field */ - public static boolean isCallId(String str) { - return same(str, Call_ID) || same(str, Call_ID_short); - } - - /** Whether str is a Contact field */ - public static boolean isContact(String str) { - return same(str, Contact) || same(str, Contact_short); - } - - /** Whether str is a Content_Length field */ - public static boolean isContent_Length(String str) { - return same(str, Content_Length) || same(str, Content_Length_short); - } - - /** Whether str is a Content_Type field */ - public static boolean isContent_Type(String str) { - return same(str, Content_Type) || same(str, Content_Type_short); - } - - /** Whether str is a CSeq field */ - public static boolean isCSeq(String str) { - return same(str, CSeq); - } - - /** Whether str is a Date field */ - public static boolean isDate(String str) { - return same(str, Date); - } - - /** Whether str is a Expires field */ - public static boolean isExpires(String str) { - return same(str, Expires); - } - - /** Whether str is a From field */ - public static boolean isFrom(String str) { - return same(str, From) || same(str, From_short); - } - - /** Whether str is a User_Agent field */ - public static boolean isUser_Agent(String str) { - return same(str, User_Agent); - } - - /** Whether str is a Max_Forwards field */ - public static boolean isMax_Forwards(String str) { - return same(str, Max_Forwards); - } - - /** Whether str is a Proxy_Authenticate field */ - public static boolean isProxy_Authenticate(String str) { - return same(str, Proxy_Authenticate); - } - - /** Whether str is a Proxy_Authorization field */ - public static boolean isProxy_Authorization(String str) { - return same(str, Proxy_Authorization); - } - - /** Whether str is a Proxy_Require field */ - public static boolean isProxy_Require(String str) { - return same(str, Proxy_Require); - } - - /** Whether str is a Record_Route field */ - public static boolean isRecord_Route(String str) { - return same(str, Record_Route); - } - - /** Whether str is a Require field */ - public static boolean isRequire(String str) { - return same(str, Require); - } - - /** Whether str is a Route field */ - public static boolean isRoute(String str) { - return same(str, Route); - } - - /** Whether str is a Server field */ - public static boolean isServer(String str) { - return same(str, Server); - } - - /** Whether str is a Subject field */ - public static boolean isSubject(String str) { - return same(str, Subject) || same(str, Subject_short); - } - - /** Whether str is a Supported field */ - public static boolean isSupported(String str) { - return same(str, Supported) || same(str, Supported_short); - } - - /** Whether str is a To field */ - public static boolean isTo(String str) { - return same(str, To) || same(str, To_short); - } - - /** Whether str is a Unsupported field */ - public static boolean isUnsupported(String str) { - return same(str, Unsupported); - } - - /** Whether str is a Via field */ - public static boolean isVia(String str) { - return same(str, Via) || same(str, Via_short); - } - - /** Whether str is a WWW_Authenticate field */ - public static boolean isWWW_Authenticate(String str) { - return same(str, WWW_Authenticate); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SipHeaders simply collects all standard SIP header names. */ +public abstract class BaseSipHeaders { + /** String "Accept" */ + public static final String Accept = "Accept"; + /** String "Alert-Info" */ + public static final String Alert_Info = "Alert-Info"; + /** String "Allow" */ + public static final String Allow = "Allow"; + /** String "Authentication-Info" */ + public static final String Authentication_Info = "Authentication-Info"; + /** String "Authorization" */ + public static final String Authorization = "Authorization"; + /** String "Call-ID" */ + public static final String Call_ID = "Call-ID"; + /** String "i" */ + public static final String Call_ID_short = "i"; + /** String "Contact" */ + public static final String Contact = "Contact"; + /** String "m" */ + public static final String Contact_short = "m"; + /** String "Content-Length" */ + public static final String Content_Length = "Content-Length"; + /** String "l" */ + public static final String Content_Length_short = "l"; + /** String "Content-Type" */ + public static final String Content_Type = "Content-Type"; + /** String "c" */ + public static final String Content_Type_short = "c"; + /** String "CSeq" */ + public static final String CSeq = "CSeq"; + /** String "Date" */ + public static final String Date = "Date"; + /** String "Expires" */ + public static final String Expires = "Expires"; + /** String "From" */ + public static final String From = "From"; + /** String "f" */ + public static final String From_short = "f"; + /** String "User-Agent" */ + public static final String User_Agent = "User-Agent"; + /** String "Max-Forwards" */ + public static final String Max_Forwards = "Max-Forwards"; + /** String "Proxy-Authenticate" */ + public static final String Proxy_Authenticate = "Proxy-Authenticate"; + /** String "Proxy-Authorization" */ + public static final String Proxy_Authorization = "Proxy-Authorization"; + /** String "Proxy-Require" */ + public static final String Proxy_Require = "Proxy-Require"; + /** String "Record-Route" */ + public static final String Record_Route = "Record-Route"; + /** String "Require" */ + public static final String Require = "Require"; + /** String "Route" */ + public static final String Route = "Route"; + /** String "Server" */ + public static final String Server = "Server"; + /** String "Subject" */ + public static final String Subject = "Subject"; + /** String "s" */ + public static final String Subject_short = "s"; + /** String "Supported" */ + public static final String Supported = "Supported"; + /** String "k" */ + public static final String Supported_short = "k"; + /** String "To" */ + public static final String To = "To"; + /** String "t" */ + public static final String To_short = "t"; + /** String "Unsupported" */ + public static final String Unsupported = "Unsupported"; + /** String "Via" */ + public static final String Via = "Via"; + /** String "v" */ + public static final String Via_short = "v"; + /** String "WWW-Authenticate" */ + public static final String WWW_Authenticate = "WWW-Authenticate"; + + /** Whether s1 and s2 are case-unsensitive-equal. */ + protected static boolean same(String s1, String s2) { // return + // s1.compareToIgnoreCase(s2)==0; + return s1.equalsIgnoreCase(s2); + } + + /** Whether str is a Accept field */ + public static boolean isAccept(String str) { + return same(str, Accept); + } + + /** Whether str is a Alert_Info field */ + public static boolean isAlert_Info(String str) { + return same(str, Alert_Info); + } + + /** Whether str is a Allow field */ + public static boolean isAllow(String str) { + return same(str, Allow); + } + + /** Whether str is a Authentication_Info field */ + public static boolean isAuthentication_Info(String str) { + return same(str, Authentication_Info); + } + + /** Whether str is a Authorization field */ + public static boolean isAuthorization(String str) { + return same(str, Authorization); + } + + /** Whether str is a Call-ID field */ + public static boolean isCallId(String str) { + return same(str, Call_ID) || same(str, Call_ID_short); + } + + /** Whether str is a Contact field */ + public static boolean isContact(String str) { + return same(str, Contact) || same(str, Contact_short); + } + + /** Whether str is a Content_Length field */ + public static boolean isContent_Length(String str) { + return same(str, Content_Length) || same(str, Content_Length_short); + } + + /** Whether str is a Content_Type field */ + public static boolean isContent_Type(String str) { + return same(str, Content_Type) || same(str, Content_Type_short); + } + + /** Whether str is a CSeq field */ + public static boolean isCSeq(String str) { + return same(str, CSeq); + } + + /** Whether str is a Date field */ + public static boolean isDate(String str) { + return same(str, Date); + } + + /** Whether str is a Expires field */ + public static boolean isExpires(String str) { + return same(str, Expires); + } + + /** Whether str is a From field */ + public static boolean isFrom(String str) { + return same(str, From) || same(str, From_short); + } + + /** Whether str is a User_Agent field */ + public static boolean isUser_Agent(String str) { + return same(str, User_Agent); + } + + /** Whether str is a Max_Forwards field */ + public static boolean isMax_Forwards(String str) { + return same(str, Max_Forwards); + } + + /** Whether str is a Proxy_Authenticate field */ + public static boolean isProxy_Authenticate(String str) { + return same(str, Proxy_Authenticate); + } + + /** Whether str is a Proxy_Authorization field */ + public static boolean isProxy_Authorization(String str) { + return same(str, Proxy_Authorization); + } + + /** Whether str is a Proxy_Require field */ + public static boolean isProxy_Require(String str) { + return same(str, Proxy_Require); + } + + /** Whether str is a Record_Route field */ + public static boolean isRecord_Route(String str) { + return same(str, Record_Route); + } + + /** Whether str is a Require field */ + public static boolean isRequire(String str) { + return same(str, Require); + } + + /** Whether str is a Route field */ + public static boolean isRoute(String str) { + return same(str, Route); + } + + /** Whether str is a Server field */ + public static boolean isServer(String str) { + return same(str, Server); + } + + /** Whether str is a Subject field */ + public static boolean isSubject(String str) { + return same(str, Subject) || same(str, Subject_short); + } + + /** Whether str is a Supported field */ + public static boolean isSupported(String str) { + return same(str, Supported) || same(str, Supported_short); + } + + /** Whether str is a To field */ + public static boolean isTo(String str) { + return same(str, To) || same(str, To_short); + } + + /** Whether str is a Unsupported field */ + public static boolean isUnsupported(String str) { + return same(str, Unsupported); + } + + /** Whether str is a Via field */ + public static boolean isVia(String str) { + return same(str, Via) || same(str, Via_short); + } + + /** Whether str is a WWW_Authenticate field */ + public static boolean isWWW_Authenticate(String str) { + return same(str, WWW_Authenticate); + } + +} diff --git a/src/org/zoolu/sip/header/CSeqHeader.java b/app/src/main/java/org/zoolu/sip/header/CSeqHeader.java similarity index 96% rename from src/org/zoolu/sip/header/CSeqHeader.java rename to app/src/main/java/org/zoolu/sip/header/CSeqHeader.java index ba898f9..cd4f6ae 100644 --- a/src/org/zoolu/sip/header/CSeqHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/CSeqHeader.java @@ -1,78 +1,78 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; - -/** - * SIP Header CSeq. The CSeq header field serves as a way to identify and order - * transactions. It consists of a sequence number and a method. The method MUST - * match that of the request. For non-REGISTER requests outside of a dialog, the - * sequence number value is arbitrary. - */ -public class CSeqHeader extends Header { - // public CSeqHeader() - // { super(SipHeaders.CSeq); - // } - - public CSeqHeader(String hvalue) { - super(SipHeaders.CSeq, hvalue); - } - - public CSeqHeader(Header hd) { - super(hd); - } - - public CSeqHeader(long seq, String method) { - super(SipHeaders.CSeq, String.valueOf(seq) + " " + method); - } - - /** Gets method of CSeqHeader */ - public String getMethod() { - SipParser par = new SipParser(value); - par.skipString(); // skip sequence number - return par.getString(); - } - - /** Gets sequence number of CSeqHeader */ - public long getSequenceNumber() { - return (new SipParser(value)).getInt(); - } - - /** Sets method of CSeqHeader */ - public void setMethod(String method) { - value = getSequenceNumber() + " " + method; - } - - /** Sets sequence number of CSeqHeader */ - public void setSequenceNumber(long sequenceNumber) { - value = String.valueOf(sequenceNumber) + " " + getMethod(); - } - - /** Increments sequence number of CSeqHeader */ - public CSeqHeader incSequenceNumber() { - value = String.valueOf(getSequenceNumber() + 1) + " " + getMethod(); - return this; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; + +/** + * SIP Header CSeq. The CSeq header field serves as a way to identify and order + * transactions. It consists of a sequence number and a method. The method MUST + * match that of the request. For non-REGISTER requests outside of a dialog, the + * sequence number value is arbitrary. + */ +public class CSeqHeader extends Header { + // public CSeqHeader() + // { super(SipHeaders.CSeq); + // } + + public CSeqHeader(String hvalue) { + super(SipHeaders.CSeq, hvalue); + } + + public CSeqHeader(Header hd) { + super(hd); + } + + public CSeqHeader(long seq, String method) { + super(SipHeaders.CSeq, String.valueOf(seq) + " " + method); + } + + /** Gets method of CSeqHeader */ + public String getMethod() { + SipParser par = new SipParser(value); + par.skipString(); // skip sequence number + return par.getString(); + } + + /** Gets sequence number of CSeqHeader */ + public long getSequenceNumber() { + return (new SipParser(value)).getInt(); + } + + /** Sets method of CSeqHeader */ + public void setMethod(String method) { + value = getSequenceNumber() + " " + method; + } + + /** Sets sequence number of CSeqHeader */ + public void setSequenceNumber(long sequenceNumber) { + value = String.valueOf(sequenceNumber) + " " + getMethod(); + } + + /** Increments sequence number of CSeqHeader */ + public CSeqHeader incSequenceNumber() { + value = String.valueOf(getSequenceNumber() + 1) + " " + getMethod(); + return this; + } +} diff --git a/src/org/zoolu/sip/header/CallIdHeader.java b/app/src/main/java/org/zoolu/sip/header/CallIdHeader.java similarity index 97% rename from src/org/zoolu/sip/header/CallIdHeader.java rename to app/src/main/java/org/zoolu/sip/header/CallIdHeader.java index 97f688d..fc391f6 100644 --- a/src/org/zoolu/sip/header/CallIdHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/CallIdHeader.java @@ -1,64 +1,64 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.tools.Parser; - -/** - * SIP Header Call-ID. The Call-ID header field acts as a unique identifier to - * group together a series of messages. It MUST be the same for all requests and - * responses sent by either UA in a dialog. It SHOULD be the same in each - * registration from a UA.
- * In a new request created by a UAC outside of any dialog, the Call-ID header - * field MUST be selected by the UAC as a globally unique identifier over space - * and time unless overridden by method-specific behavior.
- * Use of cryptographically random identifiers in the generation of Call-IDs is - * RECOMMENDED. Implementations MAY use the form "localid@host". Call-IDs are - * case-sensitive and are simply compared byte-by-byte. - */ -public class CallIdHeader extends Header { - /** Creates a CallIdHeader */ - // public CallIdHeader() - // { super(SipHeaders.Call_ID); - // } - /** Creates a CallIdHeader with value hvalue */ - public CallIdHeader(String hvalue) { - super(SipHeaders.Call_ID, hvalue); - } - - /** Creates a new CallIdHeader equal to CallIdHeader hd */ - public CallIdHeader(Header hd) { - super(hd); - } - - /** Gets Call-Id of CallIdHeader */ - public String getCallId() { - return (new Parser(value)).getString(); - } - - /** Sets Call-Id of CallIdHeader */ - public void setCallId(String callId) { - value = callId; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.tools.Parser; + +/** + * SIP Header Call-ID. The Call-ID header field acts as a unique identifier to + * group together a series of messages. It MUST be the same for all requests and + * responses sent by either UA in a dialog. It SHOULD be the same in each + * registration from a UA.
+ * In a new request created by a UAC outside of any dialog, the Call-ID header + * field MUST be selected by the UAC as a globally unique identifier over space + * and time unless overridden by method-specific behavior.
+ * Use of cryptographically random identifiers in the generation of Call-IDs is + * RECOMMENDED. Implementations MAY use the form "localid@host". Call-IDs are + * case-sensitive and are simply compared byte-by-byte. + */ +public class CallIdHeader extends Header { + /** Creates a CallIdHeader */ + // public CallIdHeader() + // { super(SipHeaders.Call_ID); + // } + /** Creates a CallIdHeader with value hvalue */ + public CallIdHeader(String hvalue) { + super(SipHeaders.Call_ID, hvalue); + } + + /** Creates a new CallIdHeader equal to CallIdHeader hd */ + public CallIdHeader(Header hd) { + super(hd); + } + + /** Gets Call-Id of CallIdHeader */ + public String getCallId() { + return (new Parser(value)).getString(); + } + + /** Sets Call-Id of CallIdHeader */ + public void setCallId(String callId) { + value = callId; + } +} diff --git a/src/org/zoolu/sip/header/ContactHeader.java b/app/src/main/java/org/zoolu/sip/header/ContactHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ContactHeader.java rename to app/src/main/java/org/zoolu/sip/header/ContactHeader.java index c99df45..afc5935 100644 --- a/src/org/zoolu/sip/header/ContactHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ContactHeader.java @@ -1,147 +1,147 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.provider.SipParser; -import org.zoolu.tools.Parser; -import org.zoolu.tools.DateFormat; -import java.util.Date; - -// import java.text.DateFormat; -// import java.text.SimpleDateFormat; - -/** - * SIP Header Contact. The Contact header field provides a SIP or SIPS URI that - * can be used to contact that specific instance of the UA for subsequent - * requests. The Contact header field MUST be present and contain exactly one - * SIP URI in any request that can result in the establishment of a dialog (i.e. - * INVITEs). - *

- * Note: for backward compatibility with legacy implementations the date format - * in 'expires' parameter is still supported although it has been deprecated in - * RFC 3261. - */ -public class ContactHeader extends EndPointHeader { - /** Creates a ContactHeader with '*' as contact value */ - public ContactHeader() { - super(new Header(SipHeaders.Contact, null)); - value = "*"; - } - - public ContactHeader(NameAddress nameaddr) { - super(SipHeaders.Contact, nameaddr); - } - - public ContactHeader(NameAddress nameaddr,String qvalue,String icsi) { - super(SipHeaders.Contact, nameaddr); - if (qvalue != null){ - setParameter("q", qvalue ); - } - setParameter("+g.3gpp.icsi-ref", icsi); - } - - public ContactHeader(SipURL url) { - super(SipHeaders.Contact, url); - } - - public ContactHeader(Header hd) { - super(hd); - } - - // public void setStar() - // { value="*"; - // } - - public ContactHeader setExpires(Date expire) { - setParameter("expires", "\"" + DateFormat.formatEEEddMMM(expire) + "\""); - return this; - } - - public ContactHeader setExpires(int secs) { - setParameter("expires", Integer.toString(secs)); - return this; - } - - public boolean isStar() { - if (value.indexOf('*') >= 0) - return true; - else - return false; - } - - public boolean hasExpires() { - return hasParameter("expires"); - } - - public boolean isExpired() { - if (getExpires() == 0) - return true; - else - return false; - } - - public int getExpires() { - int secs = -1; - String exp_param = getParameter("expires"); - if (exp_param != null) { - if (exp_param.indexOf("GMT") >= 0) { - Date date = (new SipParser((new Parser(exp_param)) - .getStringUnquoted())).getDate(); - secs = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); - if (secs < 0) - secs = 0; - } else - secs = (new SipParser(exp_param)).getInt(); - } - return secs; - } - - public Date getExpiresDate() { - Date date = null; - String exp_param = getParameter("expires"); - if (exp_param != null) { - if (exp_param.indexOf("GMT") >= 0) { - date = (new SipParser((new Parser(exp_param)) - .getStringUnquoted())).getDate(); - } else { - long secs = (new SipParser(exp_param)).getInt(); - if (secs >= 0) - date = new Date(System.currentTimeMillis() + secs * 1000); - } - } - return date; - } - - public ContactHeader removeExpires() { - removeParameter("expires"); - return this; - } - /* - * public static String toString(Vector clist) { String str="Contact: "; for - * (int i=0; i + * Note: for backward compatibility with legacy implementations the date format + * in 'expires' parameter is still supported although it has been deprecated in + * RFC 3261. + */ +public class ContactHeader extends EndPointHeader { + /** Creates a ContactHeader with '*' as contact value */ + public ContactHeader() { + super(new Header(SipHeaders.Contact, null)); + value = "*"; + } + + public ContactHeader(NameAddress nameaddr) { + super(SipHeaders.Contact, nameaddr); + } + + public ContactHeader(NameAddress nameaddr,String qvalue,String icsi) { + super(SipHeaders.Contact, nameaddr); + if (qvalue != null){ + setParameter("q", qvalue ); + } + setParameter("+g.3gpp.icsi-ref", icsi); + } + + public ContactHeader(SipURL url) { + super(SipHeaders.Contact, url); + } + + public ContactHeader(Header hd) { + super(hd); + } + + // public void setStar() + // { value="*"; + // } + + public ContactHeader setExpires(Date expire) { + setParameter("expires", "\"" + DateFormat.formatEEEddMMM(expire) + "\""); + return this; + } + + public ContactHeader setExpires(int secs) { + setParameter("expires", Integer.toString(secs)); + return this; + } + + public boolean isStar() { + if (value.indexOf('*') >= 0) + return true; + else + return false; + } + + public boolean hasExpires() { + return hasParameter("expires"); + } + + public boolean isExpired() { + if (getExpires() == 0) + return true; + else + return false; + } + + public int getExpires() { + int secs = -1; + String exp_param = getParameter("expires"); + if (exp_param != null) { + if (exp_param.indexOf("GMT") >= 0) { + Date date = (new SipParser((new Parser(exp_param)) + .getStringUnquoted())).getDate(); + secs = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); + if (secs < 0) + secs = 0; + } else + secs = (new SipParser(exp_param)).getInt(); + } + return secs; + } + + public Date getExpiresDate() { + Date date = null; + String exp_param = getParameter("expires"); + if (exp_param != null) { + if (exp_param.indexOf("GMT") >= 0) { + date = (new SipParser((new Parser(exp_param)) + .getStringUnquoted())).getDate(); + } else { + long secs = (new SipParser(exp_param)).getInt(); + if (secs >= 0) + date = new Date(System.currentTimeMillis() + secs * 1000); + } + } + return date; + } + + public ContactHeader removeExpires() { + removeParameter("expires"); + return this; + } + /* + * public static String toString(Vector clist) { String str="Contact: "; for + * (int i=0; i - * It extends the NameAddressHeader.getNameAddress() method, by removing - * eventual EndPointHeader field parameters (e.g. 'tag' param) from the - * returnerd NameAddress. - * - * @return the end point NameAddress or null if NameAddress does not exist - * (that leads to the wildcard in case of ContactHeader) - */ - public NameAddress getNameAddress() { - NameAddress naddr = (new SipParser(value)).getNameAddress(); - // patch for removing eventual 'tag' or other EndPointHeader parameters - // from NameAddress - SipURL url = naddr.getAddress(); - for (int i = 0; i < ENDPOINT_PARAMS.length; i++) { - if (url.hasParameter(ENDPOINT_PARAMS[i])) { - url.removeParameter(ENDPOINT_PARAMS[i]); - naddr = new NameAddress(naddr.getDisplayName(), url); - } - } - return naddr; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.provider.SipParser; + +/** + * Abstract EndPointHeader is the base Class for SIP Headers such as FromHeader, + * ToHeader. The "tag" parameter is used in the EndPointHeader. It serves as a + * general mechanism to identify a dialog, which is the combination of the + * Call-ID along with two tags, one from each participant in the dialog. + */ +public abstract class EndPointHeader extends NameAddressHeader { + /** + * EndPoint parameters that should be removed from the returned NameAddress. + * This tries to resolve a bug (?) of SIP when using SIP URL parameters in a + * name-address within an EndPointHeader that may have some header + * parameters. + */ + static final String[] ENDPOINT_PARAMS = { "tag", "expires" }; + + /** Creates a new EndPointHeader. */ + // public EndPointHeader(String hname) + // { super(hname); + // } + /** Creates a new EndPointHeader. */ + public EndPointHeader(String hname, NameAddress nameaddr) { + super(hname, nameaddr); + } + + /** Creates a new EndPointHeader. */ + public EndPointHeader(String hname, SipURL url) { + super(hname, url); + } + + /** Creates a new EndPointHeader. */ + public EndPointHeader(String hname, NameAddress nameaddr, String tag) { + super(hname, nameaddr); + if (tag != null) + setParameter("tag", tag); + } + + /** Creates a new EndPointHeader. */ + public EndPointHeader(String hname, SipURL url, String tag) { + super(hname, url); + if (tag != null) + setParameter("tag", tag); + } + + /** Creates a new EndPointHeader. */ + public EndPointHeader(Header hd) { + super(hd); + } + + /** Gets 'tag' parameter. */ + public String getTag() { + return this.getParameter("tag"); + } + + /** Whether it has 'tag' parameter. */ + public boolean hasTag() { + return this.hasParameter("tag"); + } + + /** + * Gets NameAddress from the EndPointHeader.
+ * It extends the NameAddressHeader.getNameAddress() method, by removing + * eventual EndPointHeader field parameters (e.g. 'tag' param) from the + * returnerd NameAddress. + * + * @return the end point NameAddress or null if NameAddress does not exist + * (that leads to the wildcard in case of ContactHeader) + */ + public NameAddress getNameAddress() { + NameAddress naddr = (new SipParser(value)).getNameAddress(); + // patch for removing eventual 'tag' or other EndPointHeader parameters + // from NameAddress + SipURL url = naddr.getAddress(); + for (int i = 0; i < ENDPOINT_PARAMS.length; i++) { + if (url.hasParameter(ENDPOINT_PARAMS[i])) { + url.removeParameter(ENDPOINT_PARAMS[i]); + naddr = new NameAddress(naddr.getDisplayName(), url); + } + } + return naddr; + } + +} diff --git a/src/org/zoolu/sip/header/EventHeader.java b/app/src/main/java/org/zoolu/sip/header/EventHeader.java similarity index 96% rename from src/org/zoolu/sip/header/EventHeader.java rename to app/src/main/java/org/zoolu/sip/header/EventHeader.java index e3202e0..265ac79 100644 --- a/src/org/zoolu/sip/header/EventHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/EventHeader.java @@ -1,70 +1,70 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.tools.Parser; - -/** - * SIP Event header (RFC 3265). - *

- * Event is a request header field (request-header). It appears in SUBSCRIBE and - * NOTIFY requests. It provides a event-package name. - */ -public class EventHeader extends ParametricHeader { - /** State delimiters. */ - private static final char[] delim = { ',', ';', ' ', '\t', '\n', '\r' }; - - /** Costructs a new EventHeader. */ - public EventHeader(String event_package) { - super(SipHeaders.Event, event_package); - } - - /** Costructs a new EventHeader. */ - public EventHeader(String event_package, String id) { - super(SipHeaders.Event, event_package); - if (id != null) - this.setParameter("id", id); - } - - /** Costructs a new EventHeader. */ - public EventHeader(Header hd) { - super(hd); - } - - /** Gets the event name. */ - public String getEvent() { - return new Parser(value).getWord(delim); - } - - /** Gets 'id' parameter. */ - public String getId() { - return this.getParameter("id"); - } - - /** Whether it has 'id' parameter. */ - public boolean hasId() { - return this.hasParameter("id"); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.tools.Parser; + +/** + * SIP Event header (RFC 3265). + *

+ * Event is a request header field (request-header). It appears in SUBSCRIBE and + * NOTIFY requests. It provides a event-package name. + */ +public class EventHeader extends ParametricHeader { + /** State delimiters. */ + private static final char[] delim = { ',', ';', ' ', '\t', '\n', '\r' }; + + /** Costructs a new EventHeader. */ + public EventHeader(String event_package) { + super(SipHeaders.Event, event_package); + } + + /** Costructs a new EventHeader. */ + public EventHeader(String event_package, String id) { + super(SipHeaders.Event, event_package); + if (id != null) + this.setParameter("id", id); + } + + /** Costructs a new EventHeader. */ + public EventHeader(Header hd) { + super(hd); + } + + /** Gets the event name. */ + public String getEvent() { + return new Parser(value).getWord(delim); + } + + /** Gets 'id' parameter. */ + public String getId() { + return this.getParameter("id"); + } + + /** Whether it has 'id' parameter. */ + public boolean hasId() { + return this.hasParameter("id"); + } + +} diff --git a/src/org/zoolu/sip/header/ExpiresHeader.java b/app/src/main/java/org/zoolu/sip/header/ExpiresHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ExpiresHeader.java rename to app/src/main/java/org/zoolu/sip/header/ExpiresHeader.java index 7453ff6..84ab321 100644 --- a/src/org/zoolu/sip/header/ExpiresHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ExpiresHeader.java @@ -1,97 +1,97 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; -import org.zoolu.tools.Parser; -import java.util.Date; - -/** - * SIP Header Expires. - *

- * Note: for backward compatibility with legacy implementations the date format - * is still supported although it has been deprecated in RFC 3261. - */ -public class ExpiresHeader extends SipDateHeader { - - - public ExpiresHeader(String hvalue) { - super(SipHeaders.Expires, hvalue); - } - - /** Creates a new ExpiresHeader based on a Date value. */ - public ExpiresHeader(Date date) { - super(SipHeaders.Expires, date); - } - - /** Creates a new ExpiresHeader with delta-seconds as value. */ - public ExpiresHeader(int seconds) { - super(SipHeaders.Expires, (String) null); - value = String.valueOf(seconds); - } - - public ExpiresHeader(Header hd) { - super(hd); - } - - /** - * Gets boolean value to indicate if expiry value of ExpiresHeader is in - * date format. - */ - public boolean isDate() { - if (value.indexOf("GMT") >= 0) - return true; - return false; - } - - /** Gets value of ExpiresHeader as delta-seconds */ - public int getDeltaSeconds() { - int secs = -1; - if (isDate()) { - Date date = (new SipParser((new Parser(value)).getStringUnquoted())) - .getDate(); - secs = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); - if (secs < 0) - secs = 0; - } else - secs = (new SipParser(value)).getInt(); - - return secs; - } - - /** Gets value of ExpiresHeader as absolute date */ - public Date getDate() { - Date date = null; - if (isDate()) { - date = (new SipParser((new Parser(value)).getStringUnquoted())) - .getDate(); - } else { - long secs = getDeltaSeconds(); - if (secs >= 0) - date = new Date(System.currentTimeMillis() + secs * 1000); - } - return date; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; +import org.zoolu.tools.Parser; +import java.util.Date; + +/** + * SIP Header Expires. + *

+ * Note: for backward compatibility with legacy implementations the date format + * is still supported although it has been deprecated in RFC 3261. + */ +public class ExpiresHeader extends SipDateHeader { + + + public ExpiresHeader(String hvalue) { + super(SipHeaders.Expires, hvalue); + } + + /** Creates a new ExpiresHeader based on a Date value. */ + public ExpiresHeader(Date date) { + super(SipHeaders.Expires, date); + } + + /** Creates a new ExpiresHeader with delta-seconds as value. */ + public ExpiresHeader(int seconds) { + super(SipHeaders.Expires, (String) null); + value = String.valueOf(seconds); + } + + public ExpiresHeader(Header hd) { + super(hd); + } + + /** + * Gets boolean value to indicate if expiry value of ExpiresHeader is in + * date format. + */ + public boolean isDate() { + if (value.indexOf("GMT") >= 0) + return true; + return false; + } + + /** Gets value of ExpiresHeader as delta-seconds */ + public int getDeltaSeconds() { + int secs = -1; + if (isDate()) { + Date date = (new SipParser((new Parser(value)).getStringUnquoted())) + .getDate(); + secs = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); + if (secs < 0) + secs = 0; + } else + secs = (new SipParser(value)).getInt(); + + return secs; + } + + /** Gets value of ExpiresHeader as absolute date */ + public Date getDate() { + Date date = null; + if (isDate()) { + date = (new SipParser((new Parser(value)).getStringUnquoted())) + .getDate(); + } else { + long secs = getDeltaSeconds(); + if (secs >= 0) + date = new Date(System.currentTimeMillis() + secs * 1000); + } + return date; + } + +} diff --git a/src/org/zoolu/sip/header/FromHeader.java b/app/src/main/java/org/zoolu/sip/header/FromHeader.java similarity index 96% rename from src/org/zoolu/sip/header/FromHeader.java rename to app/src/main/java/org/zoolu/sip/header/FromHeader.java index f162623..a0c06d9 100644 --- a/src/org/zoolu/sip/header/FromHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/FromHeader.java @@ -1,58 +1,58 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; - -/** - * SIP Header From. The From header field indicates the logical identity of the - * initiator of the request, possibly the user's address-of-record. Like the To - * header field, it contains a URI and optionally a display name.
- * The From field MUST contain a new "tag" parameter, chosen by the UAC. - */ -public class FromHeader extends EndPointHeader { - // public FromHeader() - // { super(SipHeaders.From); - // } - - public FromHeader(NameAddress nameaddr) { - super(SipHeaders.From, nameaddr); - } - - public FromHeader(SipURL url) { - super(SipHeaders.From, url); - } - - public FromHeader(NameAddress nameaddr, String tag) { - super(SipHeaders.From, nameaddr, tag); - } - - public FromHeader(SipURL url, String tag) { - super(SipHeaders.From, url, tag); - } - - public FromHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; + +/** + * SIP Header From. The From header field indicates the logical identity of the + * initiator of the request, possibly the user's address-of-record. Like the To + * header field, it contains a URI and optionally a display name.
+ * The From field MUST contain a new "tag" parameter, chosen by the UAC. + */ +public class FromHeader extends EndPointHeader { + // public FromHeader() + // { super(SipHeaders.From); + // } + + public FromHeader(NameAddress nameaddr) { + super(SipHeaders.From, nameaddr); + } + + public FromHeader(SipURL url) { + super(SipHeaders.From, url); + } + + public FromHeader(NameAddress nameaddr, String tag) { + super(SipHeaders.From, nameaddr, tag); + } + + public FromHeader(SipURL url, String tag) { + super(SipHeaders.From, url, tag); + } + + public FromHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/Header.java b/app/src/main/java/org/zoolu/sip/header/Header.java similarity index 96% rename from src/org/zoolu/sip/header/Header.java rename to app/src/main/java/org/zoolu/sip/header/Header.java index a76ef1d..92ec41d 100644 --- a/src/org/zoolu/sip/header/Header.java +++ b/app/src/main/java/org/zoolu/sip/header/Header.java @@ -1,91 +1,91 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** - * Header is the base Class for all SIP Headers - */ -public class Header { - /** The header type */ - protected String name; - /** The header string, without terminating CRLF */ - protected String value; - - /** Creates a void Header. */ - protected Header() { - name = null; - value = null; - } - - /** Creates a new Header. */ - public Header(String hname, String hvalue) { - name = hname; - value = hvalue; - } - - /** Creates a new Header. */ - public Header(Header hd) { - name = hd.getName(); - value = hd.getValue(); - } - - /** Creates and returns a copy of the Header */ - public Object clone() { - return new Header(getName(), getValue()); - } - - /** Whether the Header is equal to Object obj */ - public boolean equals(Object obj) { - try { - Header hd = (Header) obj; - if (hd.getName().equals(this.getName()) - && hd.getValue().equals(this.getValue())) - return true; - else - return false; - } catch (Exception e) { - return false; - } - } - - /** Gets name of Header */ - public String getName() { - return name; - } - - /** Gets value of Header */ - public String getValue() { - return value; - } - - /** Sets value of Header */ - public void setValue(String hvalue) { - value = hvalue; - } - - /** Gets string representation of Header */ - public String toString() { - return name + ": " + value + "\r\n"; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** + * Header is the base Class for all SIP Headers + */ +public class Header { + /** The header type */ + protected String name; + /** The header string, without terminating CRLF */ + protected String value; + + /** Creates a void Header. */ + protected Header() { + name = null; + value = null; + } + + /** Creates a new Header. */ + public Header(String hname, String hvalue) { + name = hname; + value = hvalue; + } + + /** Creates a new Header. */ + public Header(Header hd) { + name = hd.getName(); + value = hd.getValue(); + } + + /** Creates and returns a copy of the Header */ + public Object clone() { + return new Header(getName(), getValue()); + } + + /** Whether the Header is equal to Object obj */ + public boolean equals(Object obj) { + try { + Header hd = (Header) obj; + if (hd.getName().equals(this.getName()) + && hd.getValue().equals(this.getValue())) + return true; + else + return false; + } catch (Exception e) { + return false; + } + } + + /** Gets name of Header */ + public String getName() { + return name; + } + + /** Gets value of Header */ + public String getValue() { + return value; + } + + /** Sets value of Header */ + public void setValue(String hvalue) { + value = hvalue; + } + + /** Gets string representation of Header */ + public String toString() { + return name + ": " + value + "\r\n"; + } +} diff --git a/src/org/zoolu/sip/header/ListHeader.java b/app/src/main/java/org/zoolu/sip/header/ListHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ListHeader.java rename to app/src/main/java/org/zoolu/sip/header/ListHeader.java index 0966074..95fd810 100644 --- a/src/org/zoolu/sip/header/ListHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ListHeader.java @@ -1,72 +1,72 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import org.zoolu.tools.Parser; -import java.util.Vector; - -/** Generic SIP Header containing a list of tokens (Strings). */ -public abstract class ListHeader extends Header { - public ListHeader(String hname, String hvalue) { - super(hname, hvalue); - } - - public ListHeader(Header hd) { - super(hd); - } - - /** Gets list of tokens (as Vector of Strings). */ - public Vector getElements() { - Vector elements = new Vector(); - Parser par = new Parser(value); - char[] delim = { ',' }; - while (par.hasMore()) { - String elem = par.getWord(delim).trim(); - if (elem != null && elem.length() > 0) - elements.addElement(elem); - par.skipChar(); - } - return elements; - } - - /** Sets the list of tokens. */ - public void setElements(Vector elements) { - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < elements.size(); i++) { - if (i > 0) - sb.append(", "); - sb.append((String) elements.elementAt(i)); - } - value = sb.toString(); - } - - /** Adds a new token to the elements list. */ - public void addElement(String elem) { - if (value == null || value.length() == 0) - value = elem; - else - value += ", " + elem; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import org.zoolu.tools.Parser; +import java.util.Vector; + +/** Generic SIP Header containing a list of tokens (Strings). */ +public abstract class ListHeader extends Header { + public ListHeader(String hname, String hvalue) { + super(hname, hvalue); + } + + public ListHeader(Header hd) { + super(hd); + } + + /** Gets list of tokens (as Vector of Strings). */ + public Vector getElements() { + Vector elements = new Vector(); + Parser par = new Parser(value); + char[] delim = { ',' }; + while (par.hasMore()) { + String elem = par.getWord(delim).trim(); + if (elem != null && elem.length() > 0) + elements.addElement(elem); + par.skipChar(); + } + return elements; + } + + /** Sets the list of tokens. */ + public void setElements(Vector elements) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < elements.size(); i++) { + if (i > 0) + sb.append(", "); + sb.append((String) elements.elementAt(i)); + } + value = sb.toString(); + } + + /** Adds a new token to the elements list. */ + public void addElement(String elem) { + if (value == null || value.length() == 0) + value = elem; + else + value += ", " + elem; + } +} diff --git a/src/org/zoolu/sip/header/MaxForwardsHeader.java b/app/src/main/java/org/zoolu/sip/header/MaxForwardsHeader.java similarity index 96% rename from src/org/zoolu/sip/header/MaxForwardsHeader.java rename to app/src/main/java/org/zoolu/sip/header/MaxForwardsHeader.java index 25067da..a153201 100644 --- a/src/org/zoolu/sip/header/MaxForwardsHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/MaxForwardsHeader.java @@ -1,73 +1,73 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.tools.Parser; - -// import org.zoolu.sip.provider.SipStack; - -/** - * SIP Header Max-Forwards The Max-Forwards header field serves to limit the - * number of hops a request can transit on the way to its destination. It - * consists of an integer that is decremented by one at each hop. If the - * Max-Forwards value reaches 0 before the request reaches its destination, it - * will be rejected with a 483(Too Many Hops) error response. A default - * Max-Forwards value 70 is used. - */ -public class MaxForwardsHeader extends Header { - /** - * Creates a MaxForwardsHeader with value=SipStack.max_forwards - * (the default value is 70, as recommended in RFC3261) - */ - // public MaxForwardsHeader() - // { super("Max-Forwards",String.valueOf(SipStack.max_forwards)); - // } - /** Creates a MaxForwardsHeader with value=n */ - public MaxForwardsHeader(int n) { - super(SipHeaders.Max_Forwards, String.valueOf(n)); - } - - public MaxForwardsHeader(String hvalue) { - super(SipHeaders.Max_Forwards, hvalue); - } - - public MaxForwardsHeader(Header hd) { - super(hd); - } - - /** Sets Max-Forwards number */ - public void setNumber(int n) { - value = String.valueOf(n); - } - - /** Gets Max-Forwards number */ - public int getNumber() { - return (new Parser(value)).getInt(); - } - - /** Decrements the Max-Forwards number */ - public void decrement() { - value = String.valueOf(getNumber() - 1); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.tools.Parser; + +// import org.zoolu.sip.provider.SipStack; + +/** + * SIP Header Max-Forwards The Max-Forwards header field serves to limit the + * number of hops a request can transit on the way to its destination. It + * consists of an integer that is decremented by one at each hop. If the + * Max-Forwards value reaches 0 before the request reaches its destination, it + * will be rejected with a 483(Too Many Hops) error response. A default + * Max-Forwards value 70 is used. + */ +public class MaxForwardsHeader extends Header { + /** + * Creates a MaxForwardsHeader with value=SipStack.max_forwards + * (the default value is 70, as recommended in RFC3261) + */ + // public MaxForwardsHeader() + // { super("Max-Forwards",String.valueOf(SipStack.max_forwards)); + // } + /** Creates a MaxForwardsHeader with value=n */ + public MaxForwardsHeader(int n) { + super(SipHeaders.Max_Forwards, String.valueOf(n)); + } + + public MaxForwardsHeader(String hvalue) { + super(SipHeaders.Max_Forwards, hvalue); + } + + public MaxForwardsHeader(Header hd) { + super(hd); + } + + /** Sets Max-Forwards number */ + public void setNumber(int n) { + value = String.valueOf(n); + } + + /** Gets Max-Forwards number */ + public int getNumber() { + return (new Parser(value)).getInt(); + } + + /** Decrements the Max-Forwards number */ + public void decrement() { + value = String.valueOf(getNumber() - 1); + } +} diff --git a/src/org/zoolu/sip/header/MultipleHeader.java b/app/src/main/java/org/zoolu/sip/header/MultipleHeader.java similarity index 96% rename from src/org/zoolu/sip/header/MultipleHeader.java rename to app/src/main/java/org/zoolu/sip/header/MultipleHeader.java index a3c393b..eb12ad3 100644 --- a/src/org/zoolu/sip/header/MultipleHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/MultipleHeader.java @@ -1,267 +1,267 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; -import java.util.Vector; - -/** - * MultipleHeader can be used to handle SIP headers that support comma-separated - * (multiple-header) rapresentation, as explaned in section 7.3.1 of RFC 3261. - */ -public class MultipleHeader { - /** The header type */ - protected String name; - /** Vector of header values (as Strings) */ - protected Vector values; - /** - * whether to be rapresented with a comma-separated(compact) header line or - * multiple header lines - */ - protected boolean compact; - - protected MultipleHeader() { - name = null; - values = new Vector(); - compact = true; - } - - /** Costructs a MultipleHeader named hname */ - public MultipleHeader(String hname) { - name = hname; - values = new Vector(); - compact = true; - } - - /** - * Costructs a MultipleHeader named hname from a Vector of header - * values (as Strings). - */ - public MultipleHeader(String hname, Vector hvalues) { - name = hname; - values = hvalues; - compact = true; - } - - /** - * Costructs a MultipleHeader from a Vector of Headers. Each Header can be a - * single header or a multiple-comma-separated header. - */ - public MultipleHeader(Vector

headers) { - name = ((Header) headers.elementAt(0)).getName(); - values = new Vector(headers.size()); - for (int i = 0; i < headers.size(); i++) { - addBottom((Header) headers.elementAt(i)); - } - compact = false; - } - - /** Costructs a MultipleHeader from a comma-separated header */ - public MultipleHeader(Header hd) { - name = hd.getName(); - values = new Vector(); - SipParser par = new SipParser(hd.getValue()); - int comma = par.indexOfCommaHeaderSeparator(); - while (comma >= 0) { - values.addElement(par.getString(comma - par.getPos()).trim()); - par.skipChar(); // skip comma - comma = par.indexOfCommaHeaderSeparator(); - } - values.addElement(par.getRemainingString().trim()); - compact = true; - } - - /** Costructs a MultipleHeader from a MultipleHeader */ - public MultipleHeader(MultipleHeader mhd) { - name = mhd.getName(); - values = mhd.getValues(); - compact = mhd.isCommaSeparated(); - } - - /** Checks if Header hd contains comma-separated multi-header */ - public static boolean isCommaSeparated(Header hd) { - SipParser par = new SipParser(hd.getValue()); - return par.indexOfCommaHeaderSeparator() >= 0; - } - - /** - * Sets the MultipleHeader rappresentation as comma-separated or multiple - * headers - */ - public void setCommaSeparated(boolean comma_separated) { - compact = comma_separated; - } - - /** - * Whether the MultipleHeader rappresentation is comma-separated or multiple - * headers - */ - public boolean isCommaSeparated() { - return compact; - } - - /** Gets the size of th MultipleHeader */ - public int size() { - return values.size(); - } - - /** Whether it is empty */ - public boolean isEmpty() { - return values.isEmpty(); - } - - /** Creates and returns a copy of Header */ - public Object clone() { - return new MultipleHeader(getName(), getValues()); - } - - /** Indicates whether some other Object is "equal to" this Header */ - public boolean equals(Object obj) { - MultipleHeader hd = (MultipleHeader) obj; - if (hd.getName().equals(this.getName()) - && hd.getValues().equals(this.getValues())) - return true; - else - return false; - } - - /** Gets name of Header */ - public String getName() { - return name; - } - - /** Gets a vector of header values */ - public Vector getValues() { - return values; - } - - /** Sets header values */ - public void setValues(Vector v) { - values = v; - } - - /** Gets a vector of headers */ - public Vector
getHeaders() { - Vector
v = new Vector
(values.size()); - for (int i = 0; i < values.size(); i++) { - Header h = new Header(name, (String) values.elementAt(i)); - v.addElement(h); - } - return v; - } - - /** Sets header values */ - public void setHeaders(Vector
hdv) { - values = new Vector(hdv.size()); - for (int i = 0; i < hdv.size(); i++) { - values.addElement(((Header) hdv.elementAt(i)).getValue()); - } - } - - /** Gets the i-value */ - public String getValue(int i) { - return (String) values.elementAt(i); - } - - /** Adds top */ - // public void addTop(String value) - // { values.insertElementAt(value,0); - // } - /** Adds top */ - public void addTop(Header hd) { - values.insertElementAt(hd.getValue(), 0); - } - - /** Gets top Header */ - public Header getTop() { - return new Header(name, (String) values.firstElement()); - } - - /** Removes top Header */ - public void removeTop() { - values.removeElementAt(0); - } - - /** Adds bottom */ - // public void addBottom(String value) - // { values.addElement(value); - // } - /** Adds bottom */ - public void addBottom(Header hd) { - if (!MultipleHeader.isCommaSeparated(hd)) - values.addElement(hd.getValue()); - else - addBottom(new MultipleHeader(hd)); - } - - /** Adds other MultipleHeader at bottom */ - public void addBottom(MultipleHeader mhd) { - for (int i = 0; i < mhd.size(); i++) - values.addElement(mhd.getValue(i)); - } - - /** Gets bottom Header */ - public Header getBottom() { - return new Header(name, (String) values.lastElement()); - } - - /** Removes bottom Header */ - public void removeBottom() { - values.removeElementAt(values.size() - 1); - } - - /** Gets an Header containing the comma-separated(compact) representation. */ - public Header toHeader() { - String str = ""; - for (int i = 0; i < values.size() - 1; i++) - str += values.elementAt(i) + ", "; - if (values.size() > 0) - str += values.elementAt(values.size() - 1); - return new Header(name, str); - } - - /** - * Gets comma-separated(compact) or multi-headers(extended) representation.
- * Note that an empty header is rapresentated as:
- empty String (i.e. - * ""), for multi-headers(extended) rapresentation, - empty-value Header - * (i.e. "HeaderName: \r\n"), for comma-separated(compact) rapresentation. - */ - public String toString() { - if (compact) { - String str = name + ": "; - for (int i = 0; i < values.size() - 1; i++) - str += values.elementAt(i) + ", "; - if (values.size() > 0) - str += values.elementAt(values.size() - 1); - return str + "\r\n"; - } else { - String str = ""; - for (int i = 0; i < values.size(); i++) - str += name + ": " + values.elementAt(i) + "\r\n"; - return str; - } - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; +import java.util.Vector; + +/** + * MultipleHeader can be used to handle SIP headers that support comma-separated + * (multiple-header) rapresentation, as explaned in section 7.3.1 of RFC 3261. + */ +public class MultipleHeader { + /** The header type */ + protected String name; + /** Vector of header values (as Strings) */ + protected Vector values; + /** + * whether to be rapresented with a comma-separated(compact) header line or + * multiple header lines + */ + protected boolean compact; + + protected MultipleHeader() { + name = null; + values = new Vector(); + compact = true; + } + + /** Costructs a MultipleHeader named hname */ + public MultipleHeader(String hname) { + name = hname; + values = new Vector(); + compact = true; + } + + /** + * Costructs a MultipleHeader named hname from a Vector of header + * values (as Strings). + */ + public MultipleHeader(String hname, Vector hvalues) { + name = hname; + values = hvalues; + compact = true; + } + + /** + * Costructs a MultipleHeader from a Vector of Headers. Each Header can be a + * single header or a multiple-comma-separated header. + */ + public MultipleHeader(Vector
headers) { + name = ((Header) headers.elementAt(0)).getName(); + values = new Vector(headers.size()); + for (int i = 0; i < headers.size(); i++) { + addBottom((Header) headers.elementAt(i)); + } + compact = false; + } + + /** Costructs a MultipleHeader from a comma-separated header */ + public MultipleHeader(Header hd) { + name = hd.getName(); + values = new Vector(); + SipParser par = new SipParser(hd.getValue()); + int comma = par.indexOfCommaHeaderSeparator(); + while (comma >= 0) { + values.addElement(par.getString(comma - par.getPos()).trim()); + par.skipChar(); // skip comma + comma = par.indexOfCommaHeaderSeparator(); + } + values.addElement(par.getRemainingString().trim()); + compact = true; + } + + /** Costructs a MultipleHeader from a MultipleHeader */ + public MultipleHeader(MultipleHeader mhd) { + name = mhd.getName(); + values = mhd.getValues(); + compact = mhd.isCommaSeparated(); + } + + /** Checks if Header hd contains comma-separated multi-header */ + public static boolean isCommaSeparated(Header hd) { + SipParser par = new SipParser(hd.getValue()); + return par.indexOfCommaHeaderSeparator() >= 0; + } + + /** + * Sets the MultipleHeader rappresentation as comma-separated or multiple + * headers + */ + public void setCommaSeparated(boolean comma_separated) { + compact = comma_separated; + } + + /** + * Whether the MultipleHeader rappresentation is comma-separated or multiple + * headers + */ + public boolean isCommaSeparated() { + return compact; + } + + /** Gets the size of th MultipleHeader */ + public int size() { + return values.size(); + } + + /** Whether it is empty */ + public boolean isEmpty() { + return values.isEmpty(); + } + + /** Creates and returns a copy of Header */ + public Object clone() { + return new MultipleHeader(getName(), getValues()); + } + + /** Indicates whether some other Object is "equal to" this Header */ + public boolean equals(Object obj) { + MultipleHeader hd = (MultipleHeader) obj; + if (hd.getName().equals(this.getName()) + && hd.getValues().equals(this.getValues())) + return true; + else + return false; + } + + /** Gets name of Header */ + public String getName() { + return name; + } + + /** Gets a vector of header values */ + public Vector getValues() { + return values; + } + + /** Sets header values */ + public void setValues(Vector v) { + values = v; + } + + /** Gets a vector of headers */ + public Vector
getHeaders() { + Vector
v = new Vector
(values.size()); + for (int i = 0; i < values.size(); i++) { + Header h = new Header(name, (String) values.elementAt(i)); + v.addElement(h); + } + return v; + } + + /** Sets header values */ + public void setHeaders(Vector
hdv) { + values = new Vector(hdv.size()); + for (int i = 0; i < hdv.size(); i++) { + values.addElement(((Header) hdv.elementAt(i)).getValue()); + } + } + + /** Gets the i-value */ + public String getValue(int i) { + return (String) values.elementAt(i); + } + + /** Adds top */ + // public void addTop(String value) + // { values.insertElementAt(value,0); + // } + /** Adds top */ + public void addTop(Header hd) { + values.insertElementAt(hd.getValue(), 0); + } + + /** Gets top Header */ + public Header getTop() { + return new Header(name, (String) values.firstElement()); + } + + /** Removes top Header */ + public void removeTop() { + values.removeElementAt(0); + } + + /** Adds bottom */ + // public void addBottom(String value) + // { values.addElement(value); + // } + /** Adds bottom */ + public void addBottom(Header hd) { + if (!MultipleHeader.isCommaSeparated(hd)) + values.addElement(hd.getValue()); + else + addBottom(new MultipleHeader(hd)); + } + + /** Adds other MultipleHeader at bottom */ + public void addBottom(MultipleHeader mhd) { + for (int i = 0; i < mhd.size(); i++) + values.addElement(mhd.getValue(i)); + } + + /** Gets bottom Header */ + public Header getBottom() { + return new Header(name, (String) values.lastElement()); + } + + /** Removes bottom Header */ + public void removeBottom() { + values.removeElementAt(values.size() - 1); + } + + /** Gets an Header containing the comma-separated(compact) representation. */ + public Header toHeader() { + String str = ""; + for (int i = 0; i < values.size() - 1; i++) + str += values.elementAt(i) + ", "; + if (values.size() > 0) + str += values.elementAt(values.size() - 1); + return new Header(name, str); + } + + /** + * Gets comma-separated(compact) or multi-headers(extended) representation.
+ * Note that an empty header is rapresentated as:
- empty String (i.e. + * ""), for multi-headers(extended) rapresentation, - empty-value Header + * (i.e. "HeaderName: \r\n"), for comma-separated(compact) rapresentation. + */ + public String toString() { + if (compact) { + String str = name + ": "; + for (int i = 0; i < values.size() - 1; i++) + str += values.elementAt(i) + ", "; + if (values.size() > 0) + str += values.elementAt(values.size() - 1); + return str + "\r\n"; + } else { + String str = ""; + for (int i = 0; i < values.size(); i++) + str += name + ": " + values.elementAt(i) + "\r\n"; + return str; + } + } + +} diff --git a/src/org/zoolu/sip/header/NameAddressHeader.java b/app/src/main/java/org/zoolu/sip/header/NameAddressHeader.java similarity index 96% rename from src/org/zoolu/sip/header/NameAddressHeader.java rename to app/src/main/java/org/zoolu/sip/header/NameAddressHeader.java index 9e955f6..c86a967 100644 --- a/src/org/zoolu/sip/header/NameAddressHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/NameAddressHeader.java @@ -1,89 +1,89 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.provider.SipParser; -import org.zoolu.tools.Parser; - -/** - * Abstract NameAddress Header is the base Class for SIP Headers such as - * EndPointHeader. It contains a NameAddress, formed by a SIP URI and optionally - * a display name. - */ -public abstract class NameAddressHeader extends ParametricHeader { - /** Creates a new NameAddressHeader. */ - // public NameAddressHeader(String hname) - // { super(hname); - // } - /** Creates a new NameAddressHeader. */ - public NameAddressHeader(String hname, NameAddress nameaddr) { - super(hname, nameaddr.toString()); - } - - /** Creates a new NameAddressHeader. */ - public NameAddressHeader(String hname, SipURL url) { - super(hname, url.toString()); - } - - /** Creates a new NameAddressHeader. */ - public NameAddressHeader(Header hd) { - super(hd); - } - - /** - * Gets NameAddress of NameAddressHeader (Returns null if NameAddress does - * not exist - i.e. wildcard ContactHeader) - */ - public NameAddress getNameAddress() { - NameAddress naddr = (new SipParser(value)).getNameAddress(); - return naddr; - } - - /** Sets NameAddress of NameAddressHeader */ - public void setNameAddress(NameAddress naddr) { - value = naddr.toString(); - } - - // ***************** ParametricHeader's extended method ***************** - - /** - * Returns the index of the first semicolon before the first parameter. - * - * @returns the index of the semicolon before the first parameter, or -1 if - * no parameter is present. - */ - protected int indexOfFirstSemi() { - Parser par = new Parser(value); - par.goToSkippingQuoted('>'); - if (par.getPos() == value.length()) - par.setPos(0); - par.goToSkippingQuoted(';'); - if (par.getPos() < value.length()) - return par.getPos(); - else - return -1; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.provider.SipParser; +import org.zoolu.tools.Parser; + +/** + * Abstract NameAddress Header is the base Class for SIP Headers such as + * EndPointHeader. It contains a NameAddress, formed by a SIP URI and optionally + * a display name. + */ +public abstract class NameAddressHeader extends ParametricHeader { + /** Creates a new NameAddressHeader. */ + // public NameAddressHeader(String hname) + // { super(hname); + // } + /** Creates a new NameAddressHeader. */ + public NameAddressHeader(String hname, NameAddress nameaddr) { + super(hname, nameaddr.toString()); + } + + /** Creates a new NameAddressHeader. */ + public NameAddressHeader(String hname, SipURL url) { + super(hname, url.toString()); + } + + /** Creates a new NameAddressHeader. */ + public NameAddressHeader(Header hd) { + super(hd); + } + + /** + * Gets NameAddress of NameAddressHeader (Returns null if NameAddress does + * not exist - i.e. wildcard ContactHeader) + */ + public NameAddress getNameAddress() { + NameAddress naddr = (new SipParser(value)).getNameAddress(); + return naddr; + } + + /** Sets NameAddress of NameAddressHeader */ + public void setNameAddress(NameAddress naddr) { + value = naddr.toString(); + } + + // ***************** ParametricHeader's extended method ***************** + + /** + * Returns the index of the first semicolon before the first parameter. + * + * @returns the index of the semicolon before the first parameter, or -1 if + * no parameter is present. + */ + protected int indexOfFirstSemi() { + Parser par = new Parser(value); + par.goToSkippingQuoted('>'); + if (par.getPos() == value.length()) + par.setPos(0); + par.goToSkippingQuoted(';'); + if (par.getPos() < value.length()) + return par.getPos(); + else + return -1; + } + +} diff --git a/src/org/zoolu/sip/header/OptionHeader.java b/app/src/main/java/org/zoolu/sip/header/OptionHeader.java similarity index 96% rename from src/org/zoolu/sip/header/OptionHeader.java rename to app/src/main/java/org/zoolu/sip/header/OptionHeader.java index 2029ce0..3f1b1b5 100644 --- a/src/org/zoolu/sip/header/OptionHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/OptionHeader.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header that carries simply an option-tag */ -public abstract class OptionHeader extends Header { - public OptionHeader(String header, String option) { - super(header, option); - } - - public OptionHeader(Header hd) { - super(hd); - } - - /** Gets option */ - public String getOption() { - return value; - } - - /** Sets the option */ - public void setOption(String option) { - value = option; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header that carries simply an option-tag */ +public abstract class OptionHeader extends Header { + public OptionHeader(String header, String option) { + super(header, option); + } + + public OptionHeader(Header hd) { + super(hd); + } + + /** Gets option */ + public String getOption() { + return value; + } + + /** Sets the option */ + public void setOption(String option) { + value = option; + } +} diff --git a/src/org/zoolu/sip/header/ParametricHeader.java b/app/src/main/java/org/zoolu/sip/header/ParametricHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ParametricHeader.java rename to app/src/main/java/org/zoolu/sip/header/ParametricHeader.java index c976160..0d5b557 100644 --- a/src/org/zoolu/sip/header/ParametricHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ParametricHeader.java @@ -1,166 +1,166 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; -import org.zoolu.tools.Parser; -import java.util.*; - -/** - * Abstract ParametricHeader is the base class for all SIP Headers that include - * parameters - */ -public abstract class ParametricHeader extends Header { - // public ParametricHeader(String hname) - // { super(hname); - // } - - /** Costructs the abstract ParametricHeader. */ - protected ParametricHeader(String hname, String hvalue) { - super(hname, hvalue); - } - - /** Costructs the abstract ParametricHeader. */ - protected ParametricHeader(Header hd) { - super(hd); - } - - /** - * Gets the first word. - * - * @returns the first word or null if no value is present beafore - * parameters. - */ - /* - * protected String getFirstWord() { int index=indexOfFirstSemi(); if (index<0) - * return value; else return value.substring(0,index).trim(); } - */ - - /** - * Returns the index of the first semicolon before the first parameter. - * - * @returns the index of the semicolon before the first parameter, or -1 if - * no parameter is present. - */ - protected int indexOfFirstSemi() { // int index=(new - // Parser(value)).goToSkippingQuoted(';').skipChar().skipWSP().getPos(); - int index = (new Parser(value)).goToSkippingQuoted(';').getPos(); - return (index >= value.length()) ? -1 : index; - } - - /** - * Gets the value of specified parameter. - * - * @returns the parameter value or null if parameter does not exist or - * doesn't have a value (i.e. in case of flag parameter). - */ - public String getParameter(String name) { - int index = indexOfFirstSemi(); - if (index < 0) - return null; - return (new SipParser((new Parser(getValue(), index)).skipChar() - .skipWSP())).getParameter(name); - } - - /** - * Gets a String Vector of parameter names. - * - * @returns a Vector of String - */ - public Vector getParameterNames() { - int index = indexOfFirstSemi(); - if (index < 0) - return new Vector(); - return (new SipParser((new Parser(getValue(), index)).skipChar() - .skipWSP())).getParameters(); - } - - /** Whether there is the specified parameter */ - public boolean hasParameter(String name) { - int index = indexOfFirstSemi(); - if (index < 0) - return false; - return (new SipParser((new Parser(getValue(), index)).skipChar() - .skipWSP())).hasParameter(name); - } - - /** Whether there are any parameters */ - public boolean hasParameters() { - return indexOfFirstSemi() >= 0; - } - - /** Removes all parameters (if any) */ - public void removeParameters() { - if (!hasParameters()) - return; - String header = getValue(); - // System.out.println(header); - int i = header.indexOf(';'); - header = header.substring(0, i); - // System.out.println(header); - setValue(header); - } - - /** Removes specified parameter (if present) */ - public void removeParameter(String name) { - int index = indexOfFirstSemi(); - if (index < 0) - return; - String header = getValue(); - Parser par = new Parser(header, index); - while (par.hasMore()) { - int begin_param = par.getPos(); - par.skipChar(); - if (par.getWord(SipParser.param_separators).equals(name)) { - String top = header.substring(0, begin_param); - par.goToSkippingQuoted(';'); - String bottom = ""; - if (par.hasMore()) - bottom = header.substring(par.getPos()); - header = top.concat(bottom); - setValue(header); - return; - // par=new Parser(header,par.getPos()); - } - par.goTo(';'); - } - } - - /** - * Sets the value of a specified parameter. Zero-length String is returned - * in case of flag parameter (without value). - */ - public void setParameter(String name, String value) { - if (getValue() == null) - setValue(""); - if (hasParameter(name)) - removeParameter(name); - String header = getValue(); - header = header.concat(";" + name); - if (value != null) - header = header.concat("=" + value); - setValue(header); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; +import org.zoolu.tools.Parser; +import java.util.*; + +/** + * Abstract ParametricHeader is the base class for all SIP Headers that include + * parameters + */ +public abstract class ParametricHeader extends Header { + // public ParametricHeader(String hname) + // { super(hname); + // } + + /** Costructs the abstract ParametricHeader. */ + protected ParametricHeader(String hname, String hvalue) { + super(hname, hvalue); + } + + /** Costructs the abstract ParametricHeader. */ + protected ParametricHeader(Header hd) { + super(hd); + } + + /** + * Gets the first word. + * + * @returns the first word or null if no value is present beafore + * parameters. + */ + /* + * protected String getFirstWord() { int index=indexOfFirstSemi(); if (index<0) + * return value; else return value.substring(0,index).trim(); } + */ + + /** + * Returns the index of the first semicolon before the first parameter. + * + * @returns the index of the semicolon before the first parameter, or -1 if + * no parameter is present. + */ + protected int indexOfFirstSemi() { // int index=(new + // Parser(value)).goToSkippingQuoted(';').skipChar().skipWSP().getPos(); + int index = (new Parser(value)).goToSkippingQuoted(';').getPos(); + return (index >= value.length()) ? -1 : index; + } + + /** + * Gets the value of specified parameter. + * + * @returns the parameter value or null if parameter does not exist or + * doesn't have a value (i.e. in case of flag parameter). + */ + public String getParameter(String name) { + int index = indexOfFirstSemi(); + if (index < 0) + return null; + return (new SipParser((new Parser(getValue(), index)).skipChar() + .skipWSP())).getParameter(name); + } + + /** + * Gets a String Vector of parameter names. + * + * @returns a Vector of String + */ + public Vector getParameterNames() { + int index = indexOfFirstSemi(); + if (index < 0) + return new Vector(); + return (new SipParser((new Parser(getValue(), index)).skipChar() + .skipWSP())).getParameters(); + } + + /** Whether there is the specified parameter */ + public boolean hasParameter(String name) { + int index = indexOfFirstSemi(); + if (index < 0) + return false; + return (new SipParser((new Parser(getValue(), index)).skipChar() + .skipWSP())).hasParameter(name); + } + + /** Whether there are any parameters */ + public boolean hasParameters() { + return indexOfFirstSemi() >= 0; + } + + /** Removes all parameters (if any) */ + public void removeParameters() { + if (!hasParameters()) + return; + String header = getValue(); + // System.out.println(header); + int i = header.indexOf(';'); + header = header.substring(0, i); + // System.out.println(header); + setValue(header); + } + + /** Removes specified parameter (if present) */ + public void removeParameter(String name) { + int index = indexOfFirstSemi(); + if (index < 0) + return; + String header = getValue(); + Parser par = new Parser(header, index); + while (par.hasMore()) { + int begin_param = par.getPos(); + par.skipChar(); + if (par.getWord(SipParser.param_separators).equals(name)) { + String top = header.substring(0, begin_param); + par.goToSkippingQuoted(';'); + String bottom = ""; + if (par.hasMore()) + bottom = header.substring(par.getPos()); + header = top.concat(bottom); + setValue(header); + return; + // par=new Parser(header,par.getPos()); + } + par.goTo(';'); + } + } + + /** + * Sets the value of a specified parameter. Zero-length String is returned + * in case of flag parameter (without value). + */ + public void setParameter(String name, String value) { + if (getValue() == null) + setValue(""); + if (hasParameter(name)) + removeParameter(name); + String header = getValue(); + header = header.concat(";" + name); + if (value != null) + header = header.concat("=" + value); + setValue(header); + } +} diff --git a/src/org/zoolu/sip/header/ProxyAuthenticateHeader.java b/app/src/main/java/org/zoolu/sip/header/ProxyAuthenticateHeader.java similarity index 97% rename from src/org/zoolu/sip/header/ProxyAuthenticateHeader.java rename to app/src/main/java/org/zoolu/sip/header/ProxyAuthenticateHeader.java index c32d9c9..ec5c9d1 100644 --- a/src/org/zoolu/sip/header/ProxyAuthenticateHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ProxyAuthenticateHeader.java @@ -1,53 +1,53 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import java.util.Vector; - -/** SIP Proxy-Authenticate header */ -public class ProxyAuthenticateHeader extends WwwAuthenticateHeader { - /** Creates a new ProxyAuthenticateHeader */ - public ProxyAuthenticateHeader(String hvalue) { - super(hvalue); - name = SipHeaders.Proxy_Authenticate; - } - - /** Creates a new ProxyAuthenticateHeader */ - public ProxyAuthenticateHeader(Header hd) { - super(hd); - } - - /** - * Creates a new ProxyAuthenticateHeader specifing the auth_scheme - * and the vector of authentication parameters. - *

- * auth_param is a vector of String of the form parm_name - * "=" parm_value - */ - public ProxyAuthenticateHeader(String auth_scheme, - Vector auth_params) { - super(auth_scheme, auth_params); - name = SipHeaders.Proxy_Authenticate; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import java.util.Vector; + +/** SIP Proxy-Authenticate header */ +public class ProxyAuthenticateHeader extends WwwAuthenticateHeader { + /** Creates a new ProxyAuthenticateHeader */ + public ProxyAuthenticateHeader(String hvalue) { + super(hvalue); + name = SipHeaders.Proxy_Authenticate; + } + + /** Creates a new ProxyAuthenticateHeader */ + public ProxyAuthenticateHeader(Header hd) { + super(hd); + } + + /** + * Creates a new ProxyAuthenticateHeader specifing the auth_scheme + * and the vector of authentication parameters. + *

+ * auth_param is a vector of String of the form parm_name + * "=" parm_value + */ + public ProxyAuthenticateHeader(String auth_scheme, + Vector auth_params) { + super(auth_scheme, auth_params); + name = SipHeaders.Proxy_Authenticate; + } +} diff --git a/src/org/zoolu/sip/header/ProxyAuthorizationHeader.java b/app/src/main/java/org/zoolu/sip/header/ProxyAuthorizationHeader.java similarity index 97% rename from src/org/zoolu/sip/header/ProxyAuthorizationHeader.java rename to app/src/main/java/org/zoolu/sip/header/ProxyAuthorizationHeader.java index 163d94f..14b56ce 100644 --- a/src/org/zoolu/sip/header/ProxyAuthorizationHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ProxyAuthorizationHeader.java @@ -1,53 +1,53 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import java.util.Vector; - -/** SIP Proxy-Authorization header */ -public class ProxyAuthorizationHeader extends AuthorizationHeader { - /** Creates a new ProxyAuthorizationHeader */ - public ProxyAuthorizationHeader(String hvalue) { - super(hvalue); - name = SipHeaders.Proxy_Authorization; - } - - /** Creates a new ProxyAuthorizationHeader */ - public ProxyAuthorizationHeader(Header hd) { - super(hd); - } - - /** - * Creates a new ProxyAuthorizationHeader specifing the auth_scheme - * and the vector of authentication parameters. - *

- * auth_param is a vector of String of the form parm_name - * "=" parm_value - */ - public ProxyAuthorizationHeader(String auth_scheme, - Vector auth_params) { - super(auth_scheme, auth_params); - name = SipHeaders.Proxy_Authorization; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import java.util.Vector; + +/** SIP Proxy-Authorization header */ +public class ProxyAuthorizationHeader extends AuthorizationHeader { + /** Creates a new ProxyAuthorizationHeader */ + public ProxyAuthorizationHeader(String hvalue) { + super(hvalue); + name = SipHeaders.Proxy_Authorization; + } + + /** Creates a new ProxyAuthorizationHeader */ + public ProxyAuthorizationHeader(Header hd) { + super(hd); + } + + /** + * Creates a new ProxyAuthorizationHeader specifing the auth_scheme + * and the vector of authentication parameters. + *

+ * auth_param is a vector of String of the form parm_name + * "=" parm_value + */ + public ProxyAuthorizationHeader(String auth_scheme, + Vector auth_params) { + super(auth_scheme, auth_params); + name = SipHeaders.Proxy_Authorization; + } +} diff --git a/src/org/zoolu/sip/header/ProxyRequireHeader.java b/app/src/main/java/org/zoolu/sip/header/ProxyRequireHeader.java similarity index 97% rename from src/org/zoolu/sip/header/ProxyRequireHeader.java rename to app/src/main/java/org/zoolu/sip/header/ProxyRequireHeader.java index 144a8ea..baa6474 100644 --- a/src/org/zoolu/sip/header/ProxyRequireHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ProxyRequireHeader.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header Proxy-Require */ -public class ProxyRequireHeader extends OptionHeader { - public ProxyRequireHeader(String option) { - super(SipHeaders.Proxy_Require, option); - } - - public ProxyRequireHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header Proxy-Require */ +public class ProxyRequireHeader extends OptionHeader { + public ProxyRequireHeader(String option) { + super(SipHeaders.Proxy_Require, option); + } + + public ProxyRequireHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/RecordRouteHeader.java b/app/src/main/java/org/zoolu/sip/header/RecordRouteHeader.java similarity index 96% rename from src/org/zoolu/sip/header/RecordRouteHeader.java rename to app/src/main/java/org/zoolu/sip/header/RecordRouteHeader.java index b6b6a51..2e95e2a 100644 --- a/src/org/zoolu/sip/header/RecordRouteHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/RecordRouteHeader.java @@ -1,41 +1,41 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.NameAddress; - -/** SIP Header Record-Route */ -public class RecordRouteHeader extends NameAddressHeader { - // public RecordRouteHeader() - // { super(SipHeaders.Record_Route); - // } - - public RecordRouteHeader(NameAddress nameaddr) { - super(SipHeaders.Record_Route, nameaddr); - } - - public RecordRouteHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.NameAddress; + +/** SIP Header Record-Route */ +public class RecordRouteHeader extends NameAddressHeader { + // public RecordRouteHeader() + // { super(SipHeaders.Record_Route); + // } + + public RecordRouteHeader(NameAddress nameaddr) { + super(SipHeaders.Record_Route, nameaddr); + } + + public RecordRouteHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/ReferToHeader.java b/app/src/main/java/org/zoolu/sip/header/ReferToHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ReferToHeader.java rename to app/src/main/java/org/zoolu/sip/header/ReferToHeader.java index 1f0eca7..815c883 100644 --- a/src/org/zoolu/sip/header/ReferToHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ReferToHeader.java @@ -1,47 +1,47 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; - -/** - * SIP ReferTo header (RFC 3515). - *

- * Refer-To is a request header field (request-header). It appears in REFER - * requests. It provides a URL to reference. - */ -public class ReferToHeader extends NameAddressHeader { - - public ReferToHeader(NameAddress nameaddr) { - super(SipHeaders.Refer_To, nameaddr); - } - - public ReferToHeader(SipURL url) { - super(SipHeaders.Refer_To, url); - } - - public ReferToHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; + +/** + * SIP ReferTo header (RFC 3515). + *

+ * Refer-To is a request header field (request-header). It appears in REFER + * requests. It provides a URL to reference. + */ +public class ReferToHeader extends NameAddressHeader { + + public ReferToHeader(NameAddress nameaddr) { + super(SipHeaders.Refer_To, nameaddr); + } + + public ReferToHeader(SipURL url) { + super(SipHeaders.Refer_To, url); + } + + public ReferToHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/ReferredByHeader.java b/app/src/main/java/org/zoolu/sip/header/ReferredByHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ReferredByHeader.java rename to app/src/main/java/org/zoolu/sip/header/ReferredByHeader.java index 3a14ddb..231f223 100644 --- a/src/org/zoolu/sip/header/ReferredByHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ReferredByHeader.java @@ -1,50 +1,50 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; - -/** - * SIP Referred-By header (draft-ietf-sip-referredby). - *

- * Referred-By is a request header field (request-header). It appears in REFER - * requests. It provides the URL of the referrer. - */ -public class ReferredByHeader extends NameAddressHeader { - - /** Costructs a new ReferredByHeader. */ - public ReferredByHeader(NameAddress nameaddr) { - super(SipHeaders.Referred_By, nameaddr); - } - - /** Costructs a new ReferredByHeader. */ - public ReferredByHeader(SipURL url) { - super(SipHeaders.Referred_By, url); - } - - /** Costructs a new ReferredByHeader. */ - public ReferredByHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; + +/** + * SIP Referred-By header (draft-ietf-sip-referredby). + *

+ * Referred-By is a request header field (request-header). It appears in REFER + * requests. It provides the URL of the referrer. + */ +public class ReferredByHeader extends NameAddressHeader { + + /** Costructs a new ReferredByHeader. */ + public ReferredByHeader(NameAddress nameaddr) { + super(SipHeaders.Referred_By, nameaddr); + } + + /** Costructs a new ReferredByHeader. */ + public ReferredByHeader(SipURL url) { + super(SipHeaders.Referred_By, url); + } + + /** Costructs a new ReferredByHeader. */ + public ReferredByHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/RequestLine.java b/app/src/main/java/org/zoolu/sip/header/RequestLine.java similarity index 96% rename from src/org/zoolu/sip/header/RequestLine.java rename to app/src/main/java/org/zoolu/sip/header/RequestLine.java index ff48fea..920b3ae 100644 --- a/src/org/zoolu/sip/header/RequestLine.java +++ b/app/src/main/java/org/zoolu/sip/header/RequestLine.java @@ -1,80 +1,80 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; - -/** - * SIP Request-line, i.e. the first line of a request message
- * The initial Request-URI of the message SHOULD be set to the value of the URI - * in the To field. - */ -public class RequestLine { - protected String method; - protected SipURL url; - - /** Construct RequestLine request with sipurl as recipient */ - public RequestLine(String request, String sipUrl) { - method = request; - url = new SipURL(sipUrl); - } - - public RequestLine(String request, SipURL sipUrl) { - method = request; - url = sipUrl; - } - - /** Create a new copy of the RequestLine */ - public Object clone() { - return new RequestLine(getMethod(), getAddress()); - } - - /** Indicates whether some other Object is "equal to" this RequestLine */ - public boolean equals(Object obj) { // if - // (o.getClass().getSuperclass()!=this.getClass().getSuperclass()) - // return false; - try { - RequestLine r = (RequestLine) obj; - if (r.getMethod().equals(this.getMethod()) - && r.getAddress().equals(this.getAddress())) - return true; - else - return false; - } catch (Exception e) { - return false; - } - } - - public String toString() { - return method + " " + url + " SIP/2.0\r\n"; - } - - public String getMethod() { - return method; - } - - public SipURL getAddress() { - return url; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; + +/** + * SIP Request-line, i.e. the first line of a request message
+ * The initial Request-URI of the message SHOULD be set to the value of the URI + * in the To field. + */ +public class RequestLine { + protected String method; + protected SipURL url; + + /** Construct RequestLine request with sipurl as recipient */ + public RequestLine(String request, String sipUrl) { + method = request; + url = new SipURL(sipUrl); + } + + public RequestLine(String request, SipURL sipUrl) { + method = request; + url = sipUrl; + } + + /** Create a new copy of the RequestLine */ + public Object clone() { + return new RequestLine(getMethod(), getAddress()); + } + + /** Indicates whether some other Object is "equal to" this RequestLine */ + public boolean equals(Object obj) { // if + // (o.getClass().getSuperclass()!=this.getClass().getSuperclass()) + // return false; + try { + RequestLine r = (RequestLine) obj; + if (r.getMethod().equals(this.getMethod()) + && r.getAddress().equals(this.getAddress())) + return true; + else + return false; + } catch (Exception e) { + return false; + } + } + + public String toString() { + return method + " " + url + " SIP/2.0\r\n"; + } + + public String getMethod() { + return method; + } + + public SipURL getAddress() { + return url; + } +} diff --git a/src/org/zoolu/sip/header/RequireHeader.java b/app/src/main/java/org/zoolu/sip/header/RequireHeader.java similarity index 96% rename from src/org/zoolu/sip/header/RequireHeader.java rename to app/src/main/java/org/zoolu/sip/header/RequireHeader.java index d706740..5ce2547 100644 --- a/src/org/zoolu/sip/header/RequireHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/RequireHeader.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header Require */ -public class RequireHeader extends OptionHeader { - public RequireHeader(String option) { - super(SipHeaders.Require, option); - } - - public RequireHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header Require */ +public class RequireHeader extends OptionHeader { + public RequireHeader(String option) { + super(SipHeaders.Require, option); + } + + public RequireHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/RouteHeader.java b/app/src/main/java/org/zoolu/sip/header/RouteHeader.java similarity index 96% rename from src/org/zoolu/sip/header/RouteHeader.java rename to app/src/main/java/org/zoolu/sip/header/RouteHeader.java index 179056a..9a4662d 100644 --- a/src/org/zoolu/sip/header/RouteHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/RouteHeader.java @@ -1,41 +1,41 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.NameAddress; - -/** SIP Header Route */ -public class RouteHeader extends NameAddressHeader { - // public RouteHeader() - // { super(SipHeaders.Route); - // } - - public RouteHeader(NameAddress nameaddr) { - super(SipHeaders.Route, nameaddr); - } - - public RouteHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.NameAddress; + +/** SIP Header Route */ +public class RouteHeader extends NameAddressHeader { + // public RouteHeader() + // { super(SipHeaders.Route); + // } + + public RouteHeader(NameAddress nameaddr) { + super(SipHeaders.Route, nameaddr); + } + + public RouteHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/ServerHeader.java b/app/src/main/java/org/zoolu/sip/header/ServerHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ServerHeader.java rename to app/src/main/java/org/zoolu/sip/header/ServerHeader.java index c1039a2..8ac2713 100644 --- a/src/org/zoolu/sip/header/ServerHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ServerHeader.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** Server header that carries information about the UAS */ -public class ServerHeader extends Header { - public ServerHeader(String info) { - super(SipHeaders.Server, info); - } - - public ServerHeader(Header hd) { - super(hd); - } - - /** Gets UAS information */ - public String getInfo() { - return value; - } - - /** Sets the UAS information */ - public void setInfo(String info) { - value = info; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** Server header that carries information about the UAS */ +public class ServerHeader extends Header { + public ServerHeader(String info) { + super(SipHeaders.Server, info); + } + + public ServerHeader(Header hd) { + super(hd); + } + + /** Gets UAS information */ + public String getInfo() { + return value; + } + + /** Sets the UAS information */ + public void setInfo(String info) { + value = info; + } +} diff --git a/src/org/zoolu/sip/header/SipDateHeader.java b/app/src/main/java/org/zoolu/sip/header/SipDateHeader.java similarity index 96% rename from src/org/zoolu/sip/header/SipDateHeader.java rename to app/src/main/java/org/zoolu/sip/header/SipDateHeader.java index 01ea78b..8a81cf9 100644 --- a/src/org/zoolu/sip/header/SipDateHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/SipDateHeader.java @@ -1,72 +1,72 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.provider.SipParser; -import org.zoolu.tools.DateFormat; -import java.util.Date; - -// import java.text.DateFormat; -// import java.text.SimpleDateFormat; - -/** SIP Header Date */ -public abstract class SipDateHeader extends Header { - - // public SipDateHeader(String hname) - // { super(hname); - // } - - public SipDateHeader(String hname, String hvalue) { - super(hname, hvalue); - } - - public SipDateHeader(String hname, Date date) { - super(hname, null); - // DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss - // 'GMT'",Locale.US); - // value=df.format(date); - value = DateFormat.formatEEEddMMM(date); - } - - public SipDateHeader(Header hd) { - super(hd); - } - - /** Gets date value of DateHeader */ - public Date getDate() { - SipParser par = new SipParser(value); - return par.getDate(); - } - - /** Sets date of DateHeader */ - // public void setDate(Date date) - // { DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss - // 'GMT'",Locale.US); - // value=df.format(date); - // } - /** Sets date in string format of DateHeader */ - // public void setDate(String date) - // { value=date; - // } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.provider.SipParser; +import org.zoolu.tools.DateFormat; +import java.util.Date; + +// import java.text.DateFormat; +// import java.text.SimpleDateFormat; + +/** SIP Header Date */ +public abstract class SipDateHeader extends Header { + + // public SipDateHeader(String hname) + // { super(hname); + // } + + public SipDateHeader(String hname, String hvalue) { + super(hname, hvalue); + } + + public SipDateHeader(String hname, Date date) { + super(hname, null); + // DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss + // 'GMT'",Locale.US); + // value=df.format(date); + value = DateFormat.formatEEEddMMM(date); + } + + public SipDateHeader(Header hd) { + super(hd); + } + + /** Gets date value of DateHeader */ + public Date getDate() { + SipParser par = new SipParser(value); + return par.getDate(); + } + + /** Sets date of DateHeader */ + // public void setDate(Date date) + // { DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss + // 'GMT'",Locale.US); + // value=df.format(date); + // } + /** Sets date in string format of DateHeader */ + // public void setDate(String date) + // { value=date; + // } +} diff --git a/src/org/zoolu/sip/header/SipHeaders.java b/app/src/main/java/org/zoolu/sip/header/SipHeaders.java similarity index 96% rename from src/org/zoolu/sip/header/SipHeaders.java rename to app/src/main/java/org/zoolu/sip/header/SipHeaders.java index f4b8848..5be7d48 100644 --- a/src/org/zoolu/sip/header/SipHeaders.java +++ b/app/src/main/java/org/zoolu/sip/header/SipHeaders.java @@ -1,85 +1,85 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** - * SipHeaders extends class sip.header.SipHeaders by adding new SIP header - * names. - */ -public class SipHeaders extends BaseSipHeaders { - - // ****************************** Extensions - // *******************************/ - - /** String "Accept-Contact" */ - public static final String Accept_Contact = "Accept-Contact"; // added by mandrajg - - /** Whether str is "Accept-Contact" */ - public static boolean isAcceptContact(String str) { // added by mandrajg - return same(str, Accept_Contact); - } - - /** String "Refer-To" */ - public static final String Refer_To = "Refer-To"; - - /** Whether str is "Refer-To" */ - public static boolean isReferTo(String str) { - return same(str, Refer_To); - } - - /** String "Referred-By" */ - public static final String Referred_By = "Referred-By"; - - /** Whether str is "Referred-By" */ - public static boolean isReferredBy(String str) { - return same(str, Referred_By); - } - - /** String "Event" */ - public static final String Event = "Event"; - /** String "o" */ - public static final String Event_short = "o"; - - /** Whether str is an Event field */ - public static boolean isEvent(String str) { - return same(str, Event) || same(str, Event_short); - } - - /** String "Allow-Events" */ - public static final String Allow_Events = "Allow-Events"; - - /** Whether str is "Allow-Events" */ - public static boolean isAllowEvents(String str) { - return same(str, Allow_Events); - } - - /** String "Subscription-State" */ - public static final String Subscription_State = "Subscription-State"; - - /** Whether str is an Subscription_State field */ - public static boolean isSubscriptionState(String str) { - return same(str, Subscription_State); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** + * SipHeaders extends class sip.header.SipHeaders by adding new SIP header + * names. + */ +public class SipHeaders extends BaseSipHeaders { + + // ****************************** Extensions + // *******************************/ + + /** String "Accept-Contact" */ + public static final String Accept_Contact = "Accept-Contact"; // added by mandrajg + + /** Whether str is "Accept-Contact" */ + public static boolean isAcceptContact(String str) { // added by mandrajg + return same(str, Accept_Contact); + } + + /** String "Refer-To" */ + public static final String Refer_To = "Refer-To"; + + /** Whether str is "Refer-To" */ + public static boolean isReferTo(String str) { + return same(str, Refer_To); + } + + /** String "Referred-By" */ + public static final String Referred_By = "Referred-By"; + + /** Whether str is "Referred-By" */ + public static boolean isReferredBy(String str) { + return same(str, Referred_By); + } + + /** String "Event" */ + public static final String Event = "Event"; + /** String "o" */ + public static final String Event_short = "o"; + + /** Whether str is an Event field */ + public static boolean isEvent(String str) { + return same(str, Event) || same(str, Event_short); + } + + /** String "Allow-Events" */ + public static final String Allow_Events = "Allow-Events"; + + /** Whether str is "Allow-Events" */ + public static boolean isAllowEvents(String str) { + return same(str, Allow_Events); + } + + /** String "Subscription-State" */ + public static final String Subscription_State = "Subscription-State"; + + /** Whether str is an Subscription_State field */ + public static boolean isSubscriptionState(String str) { + return same(str, Subscription_State); + } + +} diff --git a/src/org/zoolu/sip/header/StatusLine.java b/app/src/main/java/org/zoolu/sip/header/StatusLine.java similarity index 96% rename from src/org/zoolu/sip/header/StatusLine.java rename to app/src/main/java/org/zoolu/sip/header/StatusLine.java index 62d7d59..6da50b2 100644 --- a/src/org/zoolu/sip/header/StatusLine.java +++ b/app/src/main/java/org/zoolu/sip/header/StatusLine.java @@ -1,69 +1,69 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Status-line, i.e. the first line of a response message */ -public class StatusLine { - protected int code; - protected String reason; - - /** Construct StatusLine */ - public StatusLine(int c, String r) { - code = c; - reason = r; - } - - /** Create a new copy of the request-line */ - public Object clone() { - return new StatusLine(getCode(), getReason()); - } - - /** Indicates whether some other Object is "equal to" this StatusLine */ - public boolean equals(Object obj) { // if - // (o.getClass().getSuperclass()!=this.getClass().getSuperclass()) - // return false; - try { - StatusLine r = (StatusLine) obj; - if (r.getCode() == (this.getCode()) - && r.getReason().equals(this.getReason())) - return true; - else - return false; - } catch (Exception e) { - return false; - } - } - - public String toString() { - return "SIP/2.0 " + code + " " + reason + "\r\n"; - } - - public int getCode() { - return code; - } - - public String getReason() { - return reason; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Status-line, i.e. the first line of a response message */ +public class StatusLine { + protected int code; + protected String reason; + + /** Construct StatusLine */ + public StatusLine(int c, String r) { + code = c; + reason = r; + } + + /** Create a new copy of the request-line */ + public Object clone() { + return new StatusLine(getCode(), getReason()); + } + + /** Indicates whether some other Object is "equal to" this StatusLine */ + public boolean equals(Object obj) { // if + // (o.getClass().getSuperclass()!=this.getClass().getSuperclass()) + // return false; + try { + StatusLine r = (StatusLine) obj; + if (r.getCode() == (this.getCode()) + && r.getReason().equals(this.getReason())) + return true; + else + return false; + } catch (Exception e) { + return false; + } + } + + public String toString() { + return "SIP/2.0 " + code + " " + reason + "\r\n"; + } + + public int getCode() { + return code; + } + + public String getReason() { + return reason; + } +} diff --git a/src/org/zoolu/sip/header/SubjectHeader.java b/app/src/main/java/org/zoolu/sip/header/SubjectHeader.java similarity index 96% rename from src/org/zoolu/sip/header/SubjectHeader.java rename to app/src/main/java/org/zoolu/sip/header/SubjectHeader.java index 66d6b15..f856a1d 100644 --- a/src/org/zoolu/sip/header/SubjectHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/SubjectHeader.java @@ -1,48 +1,48 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** - * SIP Header Subject. - */ -public class SubjectHeader extends Header { - /** Creates a SubjectHeader */ - // public SubjectHeader() - // { super(SipHeaders.Subject); - // } - /** Creates a SubjectHeader with value hvalue */ - public SubjectHeader(String hvalue) { - super(SipHeaders.Subject, hvalue); - } - - /** Creates a new SubjectHeader equal to SubjectHeader hd */ - public SubjectHeader(Header hd) { - super(hd); - } - - /** Gets the subject */ - public String getSubject() { - return value; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** + * SIP Header Subject. + */ +public class SubjectHeader extends Header { + /** Creates a SubjectHeader */ + // public SubjectHeader() + // { super(SipHeaders.Subject); + // } + /** Creates a SubjectHeader with value hvalue */ + public SubjectHeader(String hvalue) { + super(SipHeaders.Subject, hvalue); + } + + /** Creates a new SubjectHeader equal to SubjectHeader hd */ + public SubjectHeader(Header hd) { + super(hd); + } + + /** Gets the subject */ + public String getSubject() { + return value; + } +} diff --git a/src/org/zoolu/sip/header/SubscriptionStateHeader.java b/app/src/main/java/org/zoolu/sip/header/SubscriptionStateHeader.java similarity index 96% rename from src/org/zoolu/sip/header/SubscriptionStateHeader.java rename to app/src/main/java/org/zoolu/sip/header/SubscriptionStateHeader.java index a2985a5..a9fe4e3 100644 --- a/src/org/zoolu/sip/header/SubscriptionStateHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/SubscriptionStateHeader.java @@ -1,108 +1,108 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.tools.Parser; - -/** Subscription-State header (see RFC3265 for details). */ -public class SubscriptionStateHeader extends ParametricHeader { - /** State "active" */ - public static final String ACTIVE = "active"; - - /** State "pending" */ - public static final String PENDING = "pending"; - - /** State "terminated" */ - public static final String TERMINATED = "terminated"; - - /** State delimiters. */ - private static final char[] delim = { ',', ';', ' ', '\t', '\n', '\r' }; - - /** Costructs a new SubscriptionStateHeader. */ - public SubscriptionStateHeader(String state) { - super(SipHeaders.Subscription_State, state); - } - - /** Costructs a new SubscriptionStateHeader. */ - public SubscriptionStateHeader(Header hd) { - super(hd); - } - - /** Gets the subscription state. */ - public String getState() { - return new Parser(value).getWord(delim); - } - - /** Whether the subscription is active. */ - public boolean isActive() { - return getState().equals(ACTIVE); - } - - /** Whether the subscription is pending. */ - public boolean isPending() { - return getState().equals(PENDING); - } - - /** Whether the subscription is terminated. */ - public boolean isTerminated() { - return getState().equals(TERMINATED); - } - - /** Sets the 'expires' param. */ - public SubscriptionStateHeader setExpires(int secs) { - setParameter("expires", Integer.toString(secs)); - return this; - } - - /** Whether there is the 'expires' param. */ - public boolean hasExpires() { - return hasParameter("expires"); - } - - /** Gets the 'expires' param. */ - public int getExpires() { - String exp = getParameter("expires"); - if (exp != null) - return Integer.parseInt(exp); - else - return -1; - } - - /** Sets the 'reason' param. */ - public SubscriptionStateHeader setReason(String reason) { - setParameter("reason", reason); - return this; - } - - /** Whether there is the 'reason' param. */ - public boolean hasReason() { - return hasParameter("reason"); - } - - /** Gets the 'reason' param. */ - public String getReason() { - return getParameter("reason"); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.tools.Parser; + +/** Subscription-State header (see RFC3265 for details). */ +public class SubscriptionStateHeader extends ParametricHeader { + /** State "active" */ + public static final String ACTIVE = "active"; + + /** State "pending" */ + public static final String PENDING = "pending"; + + /** State "terminated" */ + public static final String TERMINATED = "terminated"; + + /** State delimiters. */ + private static final char[] delim = { ',', ';', ' ', '\t', '\n', '\r' }; + + /** Costructs a new SubscriptionStateHeader. */ + public SubscriptionStateHeader(String state) { + super(SipHeaders.Subscription_State, state); + } + + /** Costructs a new SubscriptionStateHeader. */ + public SubscriptionStateHeader(Header hd) { + super(hd); + } + + /** Gets the subscription state. */ + public String getState() { + return new Parser(value).getWord(delim); + } + + /** Whether the subscription is active. */ + public boolean isActive() { + return getState().equals(ACTIVE); + } + + /** Whether the subscription is pending. */ + public boolean isPending() { + return getState().equals(PENDING); + } + + /** Whether the subscription is terminated. */ + public boolean isTerminated() { + return getState().equals(TERMINATED); + } + + /** Sets the 'expires' param. */ + public SubscriptionStateHeader setExpires(int secs) { + setParameter("expires", Integer.toString(secs)); + return this; + } + + /** Whether there is the 'expires' param. */ + public boolean hasExpires() { + return hasParameter("expires"); + } + + /** Gets the 'expires' param. */ + public int getExpires() { + String exp = getParameter("expires"); + if (exp != null) + return Integer.parseInt(exp); + else + return -1; + } + + /** Sets the 'reason' param. */ + public SubscriptionStateHeader setReason(String reason) { + setParameter("reason", reason); + return this; + } + + /** Whether there is the 'reason' param. */ + public boolean hasReason() { + return hasParameter("reason"); + } + + /** Gets the 'reason' param. */ + public String getReason() { + return getParameter("reason"); + } + +} diff --git a/src/org/zoolu/sip/header/SupportedHeader.java b/app/src/main/java/org/zoolu/sip/header/SupportedHeader.java similarity index 97% rename from src/org/zoolu/sip/header/SupportedHeader.java rename to app/src/main/java/org/zoolu/sip/header/SupportedHeader.java index ce982b8..8fa14a5 100644 --- a/src/org/zoolu/sip/header/SupportedHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/SupportedHeader.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header Supported */ -public class SupportedHeader extends OptionHeader { - public SupportedHeader(String option) { - super(SipHeaders.Supported, option); - } - - public SupportedHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header Supported */ +public class SupportedHeader extends OptionHeader { + public SupportedHeader(String option) { + super(SipHeaders.Supported, option); + } + + public SupportedHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/ToHeader.java b/app/src/main/java/org/zoolu/sip/header/ToHeader.java similarity index 97% rename from src/org/zoolu/sip/header/ToHeader.java rename to app/src/main/java/org/zoolu/sip/header/ToHeader.java index 68fb13c..c19205d 100644 --- a/src/org/zoolu/sip/header/ToHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ToHeader.java @@ -1,71 +1,71 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.*; - -/** - * SIP Header To. The To header field specifies the desired "logical" recipient - * of the request, or the address-of-record of the user or resource that is the - * target of this request. This may or may not be the ultimate recipient of the - * request. Like the From header field, it contains a URI and optionally a - * display name.
- * A request outside of a dialog MUST NOT contain a To tag; the tag in the To - * field of a request identifies the peer of the dialog. Since no dialog is - * established, no tag is present. The original recipient may or may not be the - * UAS processing the request, due to call forwarding or other proxy operations. - * A UAS MAY apply any policy it wishes to determine whether to accept requests - * when the To header field is not the identity of the UAS. However, it is - * RECOMMENDED that a UAS accept requests even if they do not recognize the URI - * scheme (for example, a tel: URI) in the To header field, or if the To header - * field does not address a known or current user of this UAS. If, on the other - * hand, the UAS decides to reject the request, it SHOULD generate a response - * with a 403 (Forbidden) status code and pass it to the server transaction for - * transmission. - */ -public class ToHeader extends EndPointHeader { - // public ToHeader() - // { super(sip.header.SipHeaders.To); - // } - - public ToHeader(NameAddress nameaddr) { - super(SipHeaders.To, nameaddr); - } - - public ToHeader(SipURL url) { - super(SipHeaders.To, url); - } - - public ToHeader(NameAddress nameaddr, String tag) { - super(SipHeaders.To, nameaddr, tag); - } - - public ToHeader(SipURL url, String tag) { - super(SipHeaders.To, url, tag); - } - - public ToHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.*; + +/** + * SIP Header To. The To header field specifies the desired "logical" recipient + * of the request, or the address-of-record of the user or resource that is the + * target of this request. This may or may not be the ultimate recipient of the + * request. Like the From header field, it contains a URI and optionally a + * display name.
+ * A request outside of a dialog MUST NOT contain a To tag; the tag in the To + * field of a request identifies the peer of the dialog. Since no dialog is + * established, no tag is present. The original recipient may or may not be the + * UAS processing the request, due to call forwarding or other proxy operations. + * A UAS MAY apply any policy it wishes to determine whether to accept requests + * when the To header field is not the identity of the UAS. However, it is + * RECOMMENDED that a UAS accept requests even if they do not recognize the URI + * scheme (for example, a tel: URI) in the To header field, or if the To header + * field does not address a known or current user of this UAS. If, on the other + * hand, the UAS decides to reject the request, it SHOULD generate a response + * with a 403 (Forbidden) status code and pass it to the server transaction for + * transmission. + */ +public class ToHeader extends EndPointHeader { + // public ToHeader() + // { super(sip.header.SipHeaders.To); + // } + + public ToHeader(NameAddress nameaddr) { + super(SipHeaders.To, nameaddr); + } + + public ToHeader(SipURL url) { + super(SipHeaders.To, url); + } + + public ToHeader(NameAddress nameaddr, String tag) { + super(SipHeaders.To, nameaddr, tag); + } + + public ToHeader(SipURL url, String tag) { + super(SipHeaders.To, url, tag); + } + + public ToHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/UnsupportedHeader.java b/app/src/main/java/org/zoolu/sip/header/UnsupportedHeader.java similarity index 97% rename from src/org/zoolu/sip/header/UnsupportedHeader.java rename to app/src/main/java/org/zoolu/sip/header/UnsupportedHeader.java index 5dbd424..6281949 100644 --- a/src/org/zoolu/sip/header/UnsupportedHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/UnsupportedHeader.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** SIP Header Unsupported */ -public class UnsupportedHeader extends OptionHeader { - public UnsupportedHeader(String option) { - super(SipHeaders.Unsupported, option); - } - - public UnsupportedHeader(Header hd) { - super(hd); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** SIP Header Unsupported */ +public class UnsupportedHeader extends OptionHeader { + public UnsupportedHeader(String option) { + super(SipHeaders.Unsupported, option); + } + + public UnsupportedHeader(Header hd) { + super(hd); + } +} diff --git a/src/org/zoolu/sip/header/UserAgentHeader.java b/app/src/main/java/org/zoolu/sip/header/UserAgentHeader.java similarity index 96% rename from src/org/zoolu/sip/header/UserAgentHeader.java rename to app/src/main/java/org/zoolu/sip/header/UserAgentHeader.java index 7388432..b98eafb 100644 --- a/src/org/zoolu/sip/header/UserAgentHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/UserAgentHeader.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -/** User-Agent header that carries information about the UAC */ -public class UserAgentHeader extends Header { - public UserAgentHeader(String info) { - super(SipHeaders.User_Agent, info); - } - - public UserAgentHeader(Header hd) { - super(hd); - } - - /** Gets UAC information */ - public String getInfo() { - return value; - } - - /** Sets the UAC information */ - public void setInfo(String info) { - value = info; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +/** User-Agent header that carries information about the UAC */ +public class UserAgentHeader extends Header { + public UserAgentHeader(String info) { + super(SipHeaders.User_Agent, info); + } + + public UserAgentHeader(Header hd) { + super(hd); + } + + /** Gets UAC information */ + public String getInfo() { + return value; + } + + /** Sets the UAC information */ + public void setInfo(String info) { + value = info; + } +} diff --git a/src/org/zoolu/sip/header/ViaHeader.java b/app/src/main/java/org/zoolu/sip/header/ViaHeader.java similarity index 96% rename from src/org/zoolu/sip/header/ViaHeader.java rename to app/src/main/java/org/zoolu/sip/header/ViaHeader.java index 28ee908..7dbf445 100644 --- a/src/org/zoolu/sip/header/ViaHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/ViaHeader.java @@ -1,225 +1,225 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import org.zoolu.sip.address.SipURL; -import org.zoolu.sip.provider.SipParser; - -/** - * SIP Header Via. The Via header field indicates the transport used for the - * transaction and identifies the location where the response is to be sent. - *
- * When the UAC creates a request, it MUST insert a Via into that request. The - * protocol name and protocol version in the header field is SIP and 2.0, - * respectively.
- * The Via header field value MUST contain a branch parameter. This parameter is - * used to identify the transaction created by that request. This parameter is - * used by both the client and the server.
- * The branch parameter value MUST be unique across space and time for all - * requests sent by the UA. The exceptions to this rule are CANCEL and ACK for - * non-2xx responses. A CANCEL request will have the same value of the branch - * parameter as the request it cancels. An ACK for a non-2xx response will also - * have the same branch ID as the INVITE whose response it acknowledges. - */ -public class ViaHeader extends ParametricHeader { - protected static final String received_param = "received"; - protected static final String rport_param = "rport"; - protected static final String branch_param = "branch"; - protected static final String maddr_param = "maddr"; - protected static final String ttl_param = "ttl"; - - // public ViaHeader() - // { super(SipHeaders.Via); - // } - - public ViaHeader(String hvalue) { - super(SipHeaders.Via, hvalue); - } - - public ViaHeader(Header hd) { - super(hd); - } - - public ViaHeader(String host, int port) { - super(SipHeaders.Via, "SIP/2.0/UDP " + host + ":" + port); - } - - /* - * public ViaHeader(String host, int port, String branch) { - * super(SipHeaders.Via,"SIP/2.0/UDP "+host+":"+port+";branch="+branch); } - */ - - public ViaHeader(String proto, String host, int port) { - super(SipHeaders.Via, "SIP/2.0/" + proto.toUpperCase() + " " + host - + ":" + port); // modified - } - - /* - * public ViaHeader(String proto, String host, int port, String branch) { - * super(SipHeaders.Via,"SIP/2.0/"+proto.toUpperCase()+" - * "+host+":"+port+";branch="+branch); } - */ - - /** Gets the transport protocol */ - public String getProtocol() { - SipParser par = new SipParser(value); - return par.goTo('/').skipChar().goTo('/').skipChar().skipWSP() - .getString(); - } - - /** Gets "sent-by" parameter */ - public String getSentBy() { - SipParser par = new SipParser(value); - par.goTo('/').skipChar().goTo('/').skipString().skipWSP(); - if (!par.hasMore()) - return null; - String sentby = value.substring(par.getPos(), par.indexOfSeparator()); - return sentby; - } - - /** Gets host of ViaHeader */ - public String getHost() { - String sentby = getSentBy(); - SipParser par = new SipParser(sentby); - par.goTo(':'); - if (par.hasMore()) - return sentby.substring(0, par.getPos()); - else - return sentby; - } - - /** Returns boolean value indicating if ViaHeader has port */ - public boolean hasPort() { - String sentby = getSentBy(); - if (sentby.indexOf(":") > 0) - return true; - return false; - } - - /** Gets port of ViaHeader */ - public int getPort() { - SipParser par = new SipParser(getSentBy()); - par.goTo(':'); - if (par.hasMore()) - return par.skipChar().getInt(); - return -1; - } - - /** Makes a SipURL from ViaHeader */ - public SipURL getSipURL() { - return new SipURL(getHost(), getPort()); - } - - /** Checks if "branch" parameter is present */ - public boolean hasBranch() { - return hasParameter(branch_param); - } - - /** Gets "branch" parameter */ - public String getBranch() { - return getParameter(branch_param); - } - - /** Sets "branch" parameter */ - public void setBranch(String value) { - setParameter(branch_param, value); - } - - /** Checks if "received" parameter is present */ - public boolean hasReceived() { - return hasParameter(received_param); - } - - /** Gets "received" parameter */ - public String getReceived() { - return getParameter(received_param); - } - - /** Sets "received" parameter */ - public void setReceived(String value) { - setParameter(received_param, value); - } - - /** Checks if "rport" parameter is present */ - public boolean hasRport() { - return hasParameter(rport_param); - } - - /** Gets "rport" parameter */ - public int getRport() { - String value = getParameter(rport_param); - if (value != null) - return Integer.parseInt(value); - else - return -1; - } - - /** Sets "rport" parameter */ - public void setRport() { - setParameter(rport_param, null); - } - - /** Sets "rport" parameter */ - public void setRport(int port) { - if (port < 0) - setParameter(rport_param, null); - else - setParameter(rport_param, Integer.toString(port)); - } - - /** Checks if "maddr" parameter is present */ - public boolean hasMaddr() { - return hasParameter(maddr_param); - } - - /** Gets "maddr" parameter */ - public String getMaddr() { - return getParameter(maddr_param); - } - - /** Sets "maddr" parameter */ - public void setMaddr(String value) { - setParameter(maddr_param, value); - } - - /** Checks if "ttl" parameter is present */ - public boolean hasTtl() { - return hasParameter(ttl_param); - } - - /** Gets "ttl" parameter */ - public int getTtl() { - String value = getParameter(ttl_param); - if (value != null) - return Integer.parseInt(value); - else - return -1; - } - - /** Sets "ttl" parameter */ - public void setTtl(int ttl) { - setParameter(ttl_param, Integer.toString(ttl)); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import org.zoolu.sip.address.SipURL; +import org.zoolu.sip.provider.SipParser; + +/** + * SIP Header Via. The Via header field indicates the transport used for the + * transaction and identifies the location where the response is to be sent. + *
+ * When the UAC creates a request, it MUST insert a Via into that request. The + * protocol name and protocol version in the header field is SIP and 2.0, + * respectively.
+ * The Via header field value MUST contain a branch parameter. This parameter is + * used to identify the transaction created by that request. This parameter is + * used by both the client and the server.
+ * The branch parameter value MUST be unique across space and time for all + * requests sent by the UA. The exceptions to this rule are CANCEL and ACK for + * non-2xx responses. A CANCEL request will have the same value of the branch + * parameter as the request it cancels. An ACK for a non-2xx response will also + * have the same branch ID as the INVITE whose response it acknowledges. + */ +public class ViaHeader extends ParametricHeader { + protected static final String received_param = "received"; + protected static final String rport_param = "rport"; + protected static final String branch_param = "branch"; + protected static final String maddr_param = "maddr"; + protected static final String ttl_param = "ttl"; + + // public ViaHeader() + // { super(SipHeaders.Via); + // } + + public ViaHeader(String hvalue) { + super(SipHeaders.Via, hvalue); + } + + public ViaHeader(Header hd) { + super(hd); + } + + public ViaHeader(String host, int port) { + super(SipHeaders.Via, "SIP/2.0/UDP " + host + ":" + port); + } + + /* + * public ViaHeader(String host, int port, String branch) { + * super(SipHeaders.Via,"SIP/2.0/UDP "+host+":"+port+";branch="+branch); } + */ + + public ViaHeader(String proto, String host, int port) { + super(SipHeaders.Via, "SIP/2.0/" + proto.toUpperCase() + " " + host + + ":" + port); // modified + } + + /* + * public ViaHeader(String proto, String host, int port, String branch) { + * super(SipHeaders.Via,"SIP/2.0/"+proto.toUpperCase()+" + * "+host+":"+port+";branch="+branch); } + */ + + /** Gets the transport protocol */ + public String getProtocol() { + SipParser par = new SipParser(value); + return par.goTo('/').skipChar().goTo('/').skipChar().skipWSP() + .getString(); + } + + /** Gets "sent-by" parameter */ + public String getSentBy() { + SipParser par = new SipParser(value); + par.goTo('/').skipChar().goTo('/').skipString().skipWSP(); + if (!par.hasMore()) + return null; + String sentby = value.substring(par.getPos(), par.indexOfSeparator()); + return sentby; + } + + /** Gets host of ViaHeader */ + public String getHost() { + String sentby = getSentBy(); + SipParser par = new SipParser(sentby); + par.goTo(':'); + if (par.hasMore()) + return sentby.substring(0, par.getPos()); + else + return sentby; + } + + /** Returns boolean value indicating if ViaHeader has port */ + public boolean hasPort() { + String sentby = getSentBy(); + if (sentby.indexOf(":") > 0) + return true; + return false; + } + + /** Gets port of ViaHeader */ + public int getPort() { + SipParser par = new SipParser(getSentBy()); + par.goTo(':'); + if (par.hasMore()) + return par.skipChar().getInt(); + return -1; + } + + /** Makes a SipURL from ViaHeader */ + public SipURL getSipURL() { + return new SipURL(getHost(), getPort()); + } + + /** Checks if "branch" parameter is present */ + public boolean hasBranch() { + return hasParameter(branch_param); + } + + /** Gets "branch" parameter */ + public String getBranch() { + return getParameter(branch_param); + } + + /** Sets "branch" parameter */ + public void setBranch(String value) { + setParameter(branch_param, value); + } + + /** Checks if "received" parameter is present */ + public boolean hasReceived() { + return hasParameter(received_param); + } + + /** Gets "received" parameter */ + public String getReceived() { + return getParameter(received_param); + } + + /** Sets "received" parameter */ + public void setReceived(String value) { + setParameter(received_param, value); + } + + /** Checks if "rport" parameter is present */ + public boolean hasRport() { + return hasParameter(rport_param); + } + + /** Gets "rport" parameter */ + public int getRport() { + String value = getParameter(rport_param); + if (value != null) + return Integer.parseInt(value); + else + return -1; + } + + /** Sets "rport" parameter */ + public void setRport() { + setParameter(rport_param, null); + } + + /** Sets "rport" parameter */ + public void setRport(int port) { + if (port < 0) + setParameter(rport_param, null); + else + setParameter(rport_param, Integer.toString(port)); + } + + /** Checks if "maddr" parameter is present */ + public boolean hasMaddr() { + return hasParameter(maddr_param); + } + + /** Gets "maddr" parameter */ + public String getMaddr() { + return getParameter(maddr_param); + } + + /** Sets "maddr" parameter */ + public void setMaddr(String value) { + setParameter(maddr_param, value); + } + + /** Checks if "ttl" parameter is present */ + public boolean hasTtl() { + return hasParameter(ttl_param); + } + + /** Gets "ttl" parameter */ + public int getTtl() { + String value = getParameter(ttl_param); + if (value != null) + return Integer.parseInt(value); + else + return -1; + } + + /** Sets "ttl" parameter */ + public void setTtl(int ttl) { + setParameter(ttl_param, Integer.toString(ttl)); + } + +} diff --git a/src/org/zoolu/sip/header/WwwAuthenticateHeader.java b/app/src/main/java/org/zoolu/sip/header/WwwAuthenticateHeader.java similarity index 97% rename from src/org/zoolu/sip/header/WwwAuthenticateHeader.java rename to app/src/main/java/org/zoolu/sip/header/WwwAuthenticateHeader.java index 64173ce..38b6aff 100644 --- a/src/org/zoolu/sip/header/WwwAuthenticateHeader.java +++ b/app/src/main/java/org/zoolu/sip/header/WwwAuthenticateHeader.java @@ -1,50 +1,50 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.header; - -import java.util.Vector; - -/** SIP WWW-Authenticate header */ -public class WwwAuthenticateHeader extends AuthenticationHeader { - /** Creates a new WwwAuthenticateHeader */ - public WwwAuthenticateHeader(String hvalue) { - super(SipHeaders.WWW_Authenticate, hvalue); - } - - /** Creates a new WwwAuthenticateHeader */ - public WwwAuthenticateHeader(Header hd) { - super(hd); - } - - /** - * Creates a new WwwAuthenticateHeader specifing the auth_scheme and - * the vector of authentication parameters. - *

- * auth_param is a vector of String of the form parm_name - * "=" parm_value - */ - public WwwAuthenticateHeader(String auth_scheme, Vector auth_params) { - super(SipHeaders.WWW_Authenticate, auth_scheme, auth_params); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.header; + +import java.util.Vector; + +/** SIP WWW-Authenticate header */ +public class WwwAuthenticateHeader extends AuthenticationHeader { + /** Creates a new WwwAuthenticateHeader */ + public WwwAuthenticateHeader(String hvalue) { + super(SipHeaders.WWW_Authenticate, hvalue); + } + + /** Creates a new WwwAuthenticateHeader */ + public WwwAuthenticateHeader(Header hd) { + super(hd); + } + + /** + * Creates a new WwwAuthenticateHeader specifing the auth_scheme and + * the vector of authentication parameters. + *

+ * auth_param is a vector of String of the form parm_name + * "=" parm_value + */ + public WwwAuthenticateHeader(String auth_scheme, Vector auth_params) { + super(SipHeaders.WWW_Authenticate, auth_scheme, auth_params); + } +} diff --git a/src/org/zoolu/sip/message/BaseMessage.java b/app/src/main/java/org/zoolu/sip/message/BaseMessage.java similarity index 96% rename from src/org/zoolu/sip/message/BaseMessage.java rename to app/src/main/java/org/zoolu/sip/message/BaseMessage.java index 0002608..3afee8c 100644 --- a/src/org/zoolu/sip/message/BaseMessage.java +++ b/app/src/main/java/org/zoolu/sip/message/BaseMessage.java @@ -1,1465 +1,1465 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.message; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.header.*; -import org.zoolu.sip.address.*; -import org.zoolu.sip.message.SipMethods; -import org.zoolu.net.UdpPacket; -import java.util.*; - -/** Class BaseMessage implements a generic SIP Message. */ -public abstract class BaseMessage { - /** UDP */ - public static final String PROTO_UDP = "udp"; - /** TCP */ - public static final String PROTO_TCP = "tcp"; - /** TLS */ - public static final String PROTO_TLS = "tls"; - /** SCTP */ - public static final String PROTO_SCTP = "sctp"; - - /** Maximum receiving packet size */ - protected static int MAX_PKT_SIZE = 8000; - - /** The remote ip address */ - protected String remote_addr; - - /** The remote port */ - protected int remote_port; - - /** Transport protocol */ - protected String transport_proto; - - /** Connection identifier */ - protected ConnectionIdentifier connection_id; - - /** Packet length */ - // protected int packet_length; - /** The message string */ - private String message; - - /** Inits empty Message */ - private void init() { // message=""; - remote_addr = null; - remote_port = 0; - transport_proto = null; - connection_id = null; - } - - /** Costructs a new empty Message */ - public BaseMessage() { - init(); - message = ""; - } - - /** Costructs a new Message */ - public BaseMessage(byte[] data, int offset, int len) { - init(); - message = new String(data, offset, len); - } - - /** Costructs a new Message */ - public BaseMessage(UdpPacket packet) { - init(); - message = new String(packet.getData(), packet.getOffset(), packet - .getLength()); - } - - /** Costructs a new Message */ - public BaseMessage(String str) { - init(); - message = new String(str); - } - - /** Costructs a new Message */ - public BaseMessage(BaseMessage msg) { // message=new String(msg.message); - message = msg.message; - remote_addr = msg.remote_addr; - remote_port = msg.remote_port; - transport_proto = msg.transport_proto; - connection_id = msg.connection_id; - // packet_length=msg.packet_length; - } - - /** Creates and returns a clone of the Message */ - abstract public Object clone(); - - // { return new Message(message); - // } - - /** Sets the entire message */ - public void setMessage(String message) { - this.message = message; - } - - /** Gets string representation of Message */ - public String toString() { - return message; - } - - /** Gets remote ip address */ - public String getRemoteAddress() { - return remote_addr; - } - - /** Gets remote port */ - public int getRemotePort() { - return remote_port; - } - - /** Gets transport protocol */ - public String getTransportProtocol() { - return transport_proto; - } - - /** Gets connection identifier */ - public ConnectionIdentifier getConnectionId() { - return connection_id; - } - - /** Gets message length */ - public int getLength() { - return message.length(); - } - - /** Sets remote ip address */ - public void setRemoteAddress(String addr) { - remote_addr = addr; - } - - /** Sets remote port */ - public void setRemotePort(int port) { - remote_port = port; - } - - /** Sets transport protocol */ - public void setTransport(String proto) { - transport_proto = proto; - } - - /** Sets connection identifier */ - public void setConnectionId(ConnectionIdentifier conn_id) { - connection_id = conn_id; - } - - /** Gets the inique DialogIdentifier for an INCOMING message */ - public DialogIdentifier getDialogId() { - String call_id = getCallIdHeader().getCallId(); - String local_tag, remote_tag; - if (isRequest()) { - local_tag = getToHeader().getTag(); - remote_tag = getFromHeader().getTag(); - } else { - local_tag = getFromHeader().getTag(); - remote_tag = getToHeader().getTag(); - } - return new DialogIdentifier(call_id, local_tag, remote_tag); - } - - /** Gets the unique TransactionIdentifier */ - public TransactionIdentifier getTransactionId() { - String call_id = getCallIdHeader().getCallId(); - ViaHeader top_via = getViaHeader(); - String branch = null; - if (top_via != null && top_via.hasBranch()) - branch = top_via.getBranch(); -// String sent_by = top_via.getSentBy(); - CSeqHeader cseqh = getCSeqHeader(); - long seqn = cseqh.getSequenceNumber(); - String method = cseqh.getMethod(); - return new TransactionIdentifier(call_id, seqn, method, null /* sent_by modified */, branch); - } - - /** Gets the MethodIdentifier */ - public MethodIdentifier getMethodId() { - String method = getCSeqHeader().getMethod(); - return new MethodIdentifier(method); - } - - // **************************** Requests ****************************/ - - /** Whether Message is a Request */ - public boolean isRequest() throws NullPointerException { // Req-Line = - // Method ' ' - // SIP-URL ' ' - // "SIP/2.0" - // CRLF - if (message == null || isResponse()) - return false; - String firstline = (new SipParser(message)).getLine(); - String version = (new SipParser(firstline)).skipString().skipString() - .getString(); - if (version == null || version.length() < 4) - return false; - version = version.substring(0, 4); - String target = "SIP/"; - // if (version.compareToIgnoreCase(target)==0) return true; - if (version.equalsIgnoreCase(target)) - return true; - return false; - } - - /** Whether Message is a method request */ - public boolean isRequest(String method) { // if (message==null) return - // false; - if (message.startsWith(method)) - return true; - else - return false; - } - - /** Whether Message is a Method that creates a dialog */ - public boolean createsDialog() { - if (!isRequest()) - return false; - // else - String method = getRequestLine().getMethod(); - for (int i = 0; i < SipMethods.dialog_methods.length; i++) - if (method.equalsIgnoreCase(SipMethods.dialog_methods[i])) - return true; - // else - return false; - } - - /** Whether Message is an Invite */ - public boolean isInvite() { - return isRequest(SipMethods.INVITE); - } - - /** Whether Message is a Register */ - public boolean isRegister() { - return isRequest(SipMethods.REGISTER); - } - - /** Whether Message is a Cancel */ - public boolean isCancel() { - return isRequest(SipMethods.CANCEL); - } - - /** Whether Message is a Bye */ - public boolean isBye() { - return isRequest(SipMethods.BYE); - } - - /** Whether Message is an Ack */ - public boolean isAck() { - return isRequest(SipMethods.ACK); - } - - /** Whether Message is an Info */ - public boolean isInfo() { - return isRequest(SipMethods.INFO); - } - - /** Whether Message is an Option */ - public boolean isOption() { - return isRequest(SipMethods.OPTION); - } - - /** Whether Message has Request-line */ - protected boolean hasRequestLine() { - return isRequest(); - } - - /** - * Gets RequestLine in Message (Returns null if called for no request - * message) - */ - public RequestLine getRequestLine() { - if (!isRequest()) { // printWarning("getRequestLine(): called for no - // request message\n",1); - return null; - } - SipParser par = new SipParser(message); - String method = par.getString(); - par.skipWSP(); - par = new SipParser(par.subParser(par.indexOfEOH() - par.getPos())); - return new RequestLine(method, par.getSipURL()); - } - - /** Sets RequestLine of the Message */ - public void setRequestLine(RequestLine rl) { - if (hasRequestLine()) - removeRequestLine(); - String value = rl.toString(); - message = value + message; - } - - /** Removes RequestLine of the Message */ - public void removeRequestLine() { - if (!isRequest()) - return; - removeFirstLine(); - } - - // **************************** Responses ****************************/ - - /** Whether Message is a Response */ - public boolean isResponse() throws NullPointerException { // Status-Line = - // "SIP/2.0" ' ' - // Status-Code ' - // 'Reason-Phrase" - // CRLF - // if (message==null) return false; - if (message == null || message.length() < 4) - return false; - String version = message.substring(0, 4); - String target = "SIP/"; - // if (version.compareToIgnoreCase(target)==0) return true; - if (version.equalsIgnoreCase(target)) - return true; - - return false; - } - - /** Whether Message has Status-line */ - protected boolean hasStatusLine() { - return isResponse(); - } - - /** - * Gets StautsLine in Message (Returns null if called for no response - * message) - */ - public StatusLine getStatusLine() { - if (!isResponse()) { // printWarning("getStatusLine(): called for no - // response message\n",1); - return null; - } - SipParser par = new SipParser(message); - par.skipString().skipWSP(); // "SIP/2.0 " - int code = par.getInt(); - int begin = par.getPos(); - int end = par.indexOfEOH(); - String reason = message.substring(begin, end).trim(); - return new StatusLine(code, reason); - } - - /** Sets StatusLine of the Message */ - public void setStatusLine(StatusLine sl) { - if (hasStatusLine()) - removeStatusLine(); - message = sl.toString() + message; - } - - /** Removes StatusLine of the Message */ - public void removeStatusLine() { - if (!isResponse()) - return; - removeFirstLine(); - } - - // **************************** Generic Headers - // ****************************/ - - /** Returns the transaction method */ - public String getTransactionMethod() { - return getCSeqHeader().getMethod(); - } - - /** Gets the first line of the Message */ - public String getFirstLine() { - if (isRequest()) - return getRequestLine().toString(); - if (isResponse()) - return getStatusLine().toString(); - return null; - } - - /** Sets Request/Status Line of the Message */ - /* - * private void setFirstLine(String value) { message=value+" \r\n"+message; } - */ - - /** Removes Request\Status Line of the Message */ - protected void removeFirstLine() { - message = message.substring((new SipParser(message)) - .indexOfNextHeader()); - } - - /** Whether Message has any headers of specified name */ - public boolean hasHeader(String name) { - Header hd = getHeader(name); - if (hd == null) - return false; - else - return true; - } - - /** - * Gets the first Header of specified name (Returns null if no Header is - * found) - */ - public Header getHeader(String hname) { - SipParser par = new SipParser(message); - return par.getHeader(hname); - } - - /** - * Gets a Vector of all Headers of specified name (Returns empty Vector if - * no Header is found) - */ - public Vector

getHeaders(String hname) { - Vector
v = new Vector
(); - SipParser par = new SipParser(message); - Header h; - while ((h = par.getHeader(hname)) != null) { - v.addElement(h); - } - return v; - } - - /** - * Adds Header at the top/bottom. The bottom is considered before the - * Content-Length and Content-Type headers - */ - public void addHeader(Header header, boolean top) { - addHeaders(header.toString(), top); - } - - /** Adds a Vector of Headers at the top/bottom */ - public void addHeaders(Vector
headers, boolean top) { - String str = ""; - for (int i = 0; i < headers.size(); i++) - str += ((Header) headers.elementAt(i)).toString(); - addHeaders(str, top); - } - - /** Adds MultipleHeader(s) mheader at the top/bottom */ - public void addHeaders(MultipleHeader mheader, boolean top) { - addHeaders(mheader.toString(), top); - } - - /** - * Adds a one or more Headers at the top/bottom. The bottom is considered - * before the Content-Length and Content-Type headers - */ - protected void addHeaders(String str, boolean top) { - int i, aux; - if (top) { - if (this.hasRequestLine() || this.hasStatusLine()) { - SipParser par = new SipParser(message); - par.goToNextHeader(); - i = par.getPos(); - } else - i = 0; - } else { - SipParser par = new SipParser(message); - // index the end of headers - i = par.goToEndOfLastHeader().goToNextLine().getPos(); - par = new SipParser(message); - // if Content_Length is present, jump before - aux = par.indexOfHeader(SipHeaders.Content_Length); - if (aux < i) - i = aux; - // if Content_Type is present, jump before - aux = par.indexOfHeader(SipHeaders.Content_Type); - if (aux < i) - i = aux; - } - String head = message.substring(0, i); - String tail = message.substring(i); - String new_message = head.concat(str); - new_message = new_message.concat(tail); - message = new_message; - } - - /** Adds Headers on position index within the Message */ - protected void addHeaders(String str, int index) { - if (index > message.length()) - index = message.length(); - message = message.substring(0, index) + str + message.substring(index); - } - - /** - * Adds Header before the first header refer_header . - *

- * If there is no header of such type, it is added at top - */ - public void addHeaderBefore(Header new_header, String refer_header) { - addHeadersBefore(new_header.toString(), refer_header); - } - - /** - * Adds MultipleHeader(s) before the first header refer_header . - *

- * If there is no header of such type, they are added at top - */ - public void addHeadersBefore(MultipleHeader mheader, String refer_header) { - addHeadersBefore(mheader.toString(), refer_header); - } - - /** - * Adds Headers before the first header refer_header . - *

- * If there is no header of such type, they are added at top - */ - protected void addHeadersBefore(String str, String refer_header) { - if (!hasHeader(refer_header)) - addHeaders(str, true); - else { - SipParser par = new SipParser(message); - par.goTo(refer_header); - int here = par.getPos(); - message = message.substring(0, here) + str - + message.substring(here); - } - } - - /** - * Adds Header after the first header refer_header . - *

- * If there is no header of such type, it is added at bottom - */ - public void addHeaderAfter(Header new_header, String refer_header) { - addHeadersAfter(new_header.toString(), refer_header); - } - - /** - * Adds MultipleHeader(s) after the first header refer_header . - *

- * If there is no header of such type, they are added at bottom - */ - public void addHeadersAfter(MultipleHeader mheader, String refer_header) { - addHeadersAfter(mheader.toString(), refer_header); - } - - /** - * Adds Headers after the first header refer_header . - *

- * If there is no header of such type, they are added at bottom - */ - protected void addHeadersAfter(String str, String refer_header) { - if (!hasHeader(refer_header)) - addHeaders(str, false); - else { - SipParser par = new SipParser(message); - par.goTo(refer_header); - int here = par.indexOfNextHeader(); - message = message.substring(0, here) + str - + message.substring(here); - } - } - - /** Removes first Header of specified name */ - public void removeHeader(String hname) { - removeHeader(hname, true); - } - - /** Removes first (or last) Header of specified name */ - public void removeHeader(String hname, boolean first) { - String[] target = { '\n' + hname, '\r' + hname }; - SipParser par = new SipParser(message); - par.goTo(target); - if (!par.hasMore()) - return; - if (!first) - while (true) { - int next = par.indexOf(target); - if (next < 0) - break; - par.setPos(next); - } - par.skipChar(); - String head = message.substring(0, par.getPos()); - par.goToNextHeader(); - String tail = message.substring(par.getPos()); - message = head.concat(tail); - } - - /** Sets the new Header (removing any previous headers of the same name) */ - /* - * public void setHeader(Header hd) { if (hasHeader(hd.getName())) - * removeAllHeaders(hd.getName()); addHeader(hd,false); } - */ - - /** Sets the Header hd removing any previous headers of the same type */ - public void setHeader(Header hd) { - String hname = hd.getName(); - if (hasHeader(hname)) { - int index = (new SipParser(message)).indexOfHeader(hname); - removeAllHeaders(hname); - addHeaders(hd.toString(), index); - } else - addHeader(hd, false); - } - - /** Removes all Headers of specified name */ - public void removeAllHeaders(String hname) { - String[] target = { '\n' + hname, '\r' + hname }; - SipParser par = new SipParser(message); - par.goTo(target); - while (par.hasMore()) { - par.skipChar(); - String head = message.substring(0, par.getPos()); - String tail = message.substring(par.indexOfNextHeader()); - message = head.concat(tail); - par = new SipParser(message, par.getPos() - 1); - par.goTo(target); - } - } - - /** Sets MultipleHeader mheader */ - /* - * public void setHeaders(MultipleHeader mheader) { if - * (hasHeader(mheader.getName())) removeAllHeaders(mheader.getName()); - * addHeaders(mheader,false); } - */ - - /** Sets MultipleHeader mheader */ - public void setHeaders(MultipleHeader mheader) { - String hname = mheader.getName(); - if (hasHeader(hname)) { - int index = (new SipParser(message)).indexOfHeader(hname); - removeAllHeaders(hname); - addHeaders(mheader.toString(), index); - } else - addHeaders(mheader, false); - } - - // **************************** Specific Headers - // ****************************/ - - /** Whether Message has MaxForwardsHeader */ - public boolean hasMaxForwardsHeader() { - return hasHeader(SipHeaders.Max_Forwards); - } - - /** Gets MaxForwardsHeader of Message */ - public MaxForwardsHeader getMaxForwardsHeader() { - Header h = getHeader(SipHeaders.Max_Forwards); - if (h == null) - return null; - else - return new MaxForwardsHeader(h); - } - - /** Sets MaxForwardsHeader of Message */ - public void setMaxForwardsHeader(MaxForwardsHeader mfh) { - setHeader(mfh); - } - - /** Removes MaxForwardsHeader from Message */ - public void removeMaxForwardsHeader() { - removeHeader(SipHeaders.Max_Forwards); - } - - /** Whether Message has FromHeader */ - public boolean hasFromHeader() { - return hasHeader(SipHeaders.From); - } - - /** Gets FromHeader of Message */ - public FromHeader getFromHeader() { - Header h = getHeader(SipHeaders.From); - if (h == null) - return null; - else - return new FromHeader(h); - } - - /** Sets FromHeader of Message */ - public void setFromHeader(FromHeader fh) { - setHeader(fh); - } - - /** Removes FromHeader from Message */ - public void removeFromHeader() { - removeHeader(SipHeaders.From); - } - - /** Whether Message has ToHeader */ - public boolean hasToHeader() { - return hasHeader(SipHeaders.To); - } - - /** Gets ToHeader of Message */ - public ToHeader getToHeader() { - Header h = getHeader(SipHeaders.To); - if (h == null) - return null; - else - return new ToHeader(h); - } - - /** Sets ToHeader of Message */ - public void setToHeader(ToHeader th) { - setHeader(th); - } - - /** Removes ToHeader from Message */ - public void removeToHeader() { - removeHeader(SipHeaders.To); - } - - /** Whether Message has ContactHeader */ - public boolean hasContactHeader() { - return hasHeader(SipHeaders.Contact); - } - - /** - * Deprecated. Gets ContactHeader of Message. Use getContacts - * instead. - */ - public ContactHeader getContactHeader() { // Header - // h=getHeader(SipHeaders.Contact); - // if (h==null) return null; else return new ContactHeader(h); - MultipleHeader mh = getContacts(); - if (mh == null) - return null; - return new ContactHeader(mh.getTop()); - } - - /** Adds ContactHeader */ - public void addContactHeader(ContactHeader ch, boolean top) { - addHeader(ch, top); - } - - /** Sets ContactHeader */ - public void setContactHeader(ContactHeader ch) { - if (hasContactHeader()) - removeContacts(); - addHeader(ch, false); - } - - /** Gets a MultipleHeader of Contacts */ - public MultipleHeader getContacts() { - Vector

v = getHeaders(SipHeaders.Contact); - if (v.size() > 0) - return new MultipleHeader(v); - else - return null; - } - - /** Adds Contacts */ - public void addContacts(MultipleHeader contacts, boolean top) { - addHeaders(contacts, top); - } - - /** Sets Contacts */ - public void setContacts(MultipleHeader contacts) { - if (hasContactHeader()) - removeContacts(); - addContacts(contacts, false); - } - - /** Removes ContactHeaders from Message */ - public void removeContacts() { - removeAllHeaders(SipHeaders.Contact); - } - - /** Whether Message has ViaHeaders */ - public boolean hasViaHeader() { - return hasHeader(SipHeaders.Via); - } - - /** Adds ViaHeader at the top */ - public void addViaHeader(ViaHeader vh) { - addHeader(vh, true); - } - - /** Gets the first ViaHeader */ - public ViaHeader getViaHeader() { // Header h=getHeader(SipHeaders.Via); - // if (h==null) return null; else return new ViaHeader(h); - MultipleHeader mh = getVias(); - if (mh == null) - return null; - return new ViaHeader(mh.getTop()); - } - - /** Removes the top ViaHeader */ - public void removeViaHeader() { // removeHeader(SipHeaders.Via); - MultipleHeader mh = getVias(); - mh.removeTop(); - setVias(mh); - } - - /** Gets all Vias */ - public MultipleHeader getVias() { - Vector
v = getHeaders(SipHeaders.Via); - if (v.size() > 0) - return new MultipleHeader(v); - else - return null; - } - - /** Adds Vias */ - public void addVias(MultipleHeader vias, boolean top) { - addHeaders(vias, top); - } - - /** Sets Vias */ - public void setVias(MultipleHeader vias) { - if (hasViaHeader()) - removeVias(); - addContacts(vias, true); - } - - /** Removes ViaHeaders from Message (if any exists) */ - public void removeVias() { - removeAllHeaders(SipHeaders.Via); - } - - /** Whether Message has RouteHeader */ - public boolean hasRouteHeader() { - return hasHeader(SipHeaders.Route); - } - - /** Adds RouteHeader at the top */ - public void addRouteHeader(RouteHeader h) { - addHeaderAfter(h, SipHeaders.Via); - } - - /** Adds multiple Route headers at the top */ - public void addRoutes(MultipleHeader routes) { - addHeadersAfter(routes, SipHeaders.Via); - } - - /** Gets the top RouteHeader */ - public RouteHeader getRouteHeader() { // Header - // h=getHeader(SipHeaders.Route); - // if (h==null) return null; else return new RouteHeader(h); - MultipleHeader mh = getRoutes(); - if (mh == null) - return null; - return new RouteHeader(mh.getTop()); - } - - /** Gets the whole route */ - public MultipleHeader getRoutes() { - Vector
v = getHeaders(SipHeaders.Route); - if (v.size() > 0) - return new MultipleHeader(v); - else - return null; - } - - /** Removes the top RouteHeader */ - public void removeRouteHeader() { // removeHeader(SipHeaders.Route); - MultipleHeader mh = getRoutes(); - mh.removeTop(); - setRoutes(mh); - } - - /** Removes all RouteHeaders from Message (if any exists) */ - public void removeRoutes() { - removeAllHeaders(SipHeaders.Route); - } - - /** Sets the whole route */ - public void setRoutes(MultipleHeader routes) { - if (hasRouteHeader()) - removeRoutes(); - addRoutes(routes); - } - - /** Whether Message has RecordRouteHeader */ - public boolean hasRecordRouteHeader() { - return hasHeader(SipHeaders.Record_Route); - } - - /** Adds RecordRouteHeader at the top */ - public void addRecordRouteHeader(RecordRouteHeader rr) { // addHeaderAfter(rr,SipHeaders.Via); - addHeaderAfter(rr, SipHeaders.CSeq); - } - - /** Adds multiple RecordRoute headers at the top */ - public void addRecordRoutes(MultipleHeader routes) { // addHeadersAfter(routes,SipHeaders.Via); - addHeadersAfter(routes, SipHeaders.CSeq); - } - - /** Gets the top RecordRouteHeader */ - public RecordRouteHeader getRecordRouteHeader() { // Header - // h=getHeader(SipHeaders.Record_Route); - // if (h==null) return null; else return new RecordRouteHeader(h); - MultipleHeader mh = getRecordRoutes(); - if (mh == null) - return null; - return new RecordRouteHeader(mh.getTop()); - } - - /** Gets the whole RecordRoute headers */ - public MultipleHeader getRecordRoutes() { - Vector
v = getHeaders(SipHeaders.Record_Route); - if (v.size() > 0) - return new MultipleHeader(v); - else - return null; - } - - /** Removes the top RecordRouteHeader */ - public void removeRecordRouteHeader() { // removeHeader(SipHeaders.Record_Route); - MultipleHeader mh = getRecordRoutes(); - mh.removeTop(); - setRecordRoutes(mh); - } - - /** Removes all RecordRouteHeader from Message (if any exists) */ - public void removeRecordRoutes() { - removeAllHeaders(SipHeaders.Record_Route); - } - - /** Sets the whole RecordRoute headers */ - public void setRecordRoutes(MultipleHeader routes) { - if (hasRecordRouteHeader()) - removeRecordRoutes(); - addRecordRoutes(routes); - } - - /** Whether Message has CSeqHeader */ - public boolean hasCSeqHeader() { - return hasHeader(SipHeaders.CSeq); - } - - /** Gets CSeqHeader of Message */ - public CSeqHeader getCSeqHeader() { - Header h = getHeader(SipHeaders.CSeq); - if (h == null) - return null; - else - return new CSeqHeader(h); - } - - /** Sets CSeqHeader of Message */ - public void setCSeqHeader(CSeqHeader csh) { - setHeader(csh); - } - - /** Removes CSeqHeader from Message */ - public void removeCSeqHeader() { - removeHeader(SipHeaders.CSeq); - } - - /** Whether has CallIdHeader */ - public boolean hasCallIdHeader() { - return hasHeader(SipHeaders.Call_ID); - } - - /** Sets CallIdHeader of Message */ - public void setCallIdHeader(CallIdHeader cih) { - setHeader(cih); - } - - /** Gets CallIdHeader of Message */ - public CallIdHeader getCallIdHeader() { - Header h = getHeader(SipHeaders.Call_ID); - if (h == null) - return null; - else - return new CallIdHeader(h); - } - - /** Removes CallIdHeader from Message */ - public void removeCallIdHeader() { - removeHeader(SipHeaders.Call_ID); - } - - /** Whether Message has SubjectHeader */ - public boolean hasSubjectHeader() { - return hasHeader(SipHeaders.Subject); - } - - /** Sets SubjectHeader of Message */ - public void setSubjectHeader(SubjectHeader sh) { - setHeader(sh); - } - - /** Gets SubjectHeader of Message */ - public SubjectHeader getSubjectHeader() { - Header h = getHeader(SipHeaders.Subject); - if (h == null) - return null; - else - return new SubjectHeader(h); - } - - /** Removes SubjectHeader from Message */ - public void removeSubjectHeader() { - removeHeader(SipHeaders.Subject); - } - - /** Whether Message has DateHeader */ - public boolean hasDateHeader() { - return hasHeader(SipHeaders.Date); - } - - /** Gets DateHeader of Message */ - public DateHeader getDateHeader() { - Header h = getHeader(SipHeaders.Date); - if (h == null) - return null; - else - return new DateHeader(h); - } - - /** Sets DateHeader of Message */ - public void setDateHeader(DateHeader dh) { - setHeader(dh); - } - - /** Removes DateHeader from Message (if it exists) */ - public void removeDateHeader() { - removeHeader(SipHeaders.Date); - } - - /** Whether has UserAgentHeader */ - public boolean hasUserAgentHeader() { - return hasHeader(SipHeaders.User_Agent); - } - - /** Sets UserAgentHeader */ - public void setUserAgentHeader(UserAgentHeader h) { - setHeader(h); - } - - /** Gets UserAgentHeader */ - public UserAgentHeader getUserAgentHeader() { - Header h = getHeader(SipHeaders.User_Agent); - if (h == null) - return null; - else - return new UserAgentHeader(h); - } - - /** Removes UserAgentHeader */ - public void removeUserAgentHeader() { - removeHeader(SipHeaders.User_Agent); - } - - /** Whether has ServerHeader */ - public boolean hasServerHeader() { - return hasHeader(SipHeaders.Server); - } - - /** Sets ServerHeader */ - public void setServerHeader(ServerHeader h) { - setHeader(h); - } - - /** Gets ServerHeader */ - public ServerHeader getServerHeader() { - Header h = getHeader(SipHeaders.Server); - if (h == null) - return null; - else - return new ServerHeader(h); - } - - /** Removes ServerHeader */ - public void removeServerHeader() { - removeHeader(SipHeaders.Server); - } - - /** Sets AcceptContactHeader */ - public void setAcceptContactHeader(AcceptContactHeader h) { // added by mandrajg - setHeader(h); - } - - /** Whether has AcceptHeader */ - public boolean hasAcceptHeader() { - return hasHeader(SipHeaders.Accept); - } - - /** Sets AcceptHeader */ - public void setAcceptHeader(AcceptHeader h) { - setHeader(h); - } - - /** Gets AcceptHeader */ - public AcceptHeader getAcceptHeader() { - Header h = getHeader(SipHeaders.Accept); - if (h == null) - return null; - else - return new AcceptHeader(h); - } - - /** Removes AcceptHeader */ - public void removeAcceptHeader() { - removeHeader(SipHeaders.Accept); - } - - /** Whether has AlertInfoHeader */ - public boolean hasAlertInfoHeader() { - return hasHeader(SipHeaders.Alert_Info); - } - - /** Sets AlertInfoHeader */ - public void setAlertInfoHeader(AlertInfoHeader h) { - setHeader(h); - } - - /** Gets AlertInfoHeader */ - public AlertInfoHeader getAlertInfoHeader() { - Header h = getHeader(SipHeaders.Alert_Info); - if (h == null) - return null; - else - return new AlertInfoHeader(h); - } - - /** Removes AlertInfoHeader */ - public void removeAlertInfoHeader() { - removeHeader(SipHeaders.Alert_Info); - } - - /** Whether has AllowHeader */ - public boolean hasAllowHeader() { - return hasHeader(SipHeaders.Allow); - } - - /** Sets AllowHeader */ - public void setAllowHeader(AllowHeader h) { - setHeader(h); - } - - /** Gets AllowHeader */ - public AllowHeader getAllowHeader() { - Header h = getHeader(SipHeaders.Allow); - if (h == null) - return null; - else - return new AllowHeader(h); - } - - /** Removes AllowHeader */ - public void removeAllowHeader() { - removeHeader(SipHeaders.Allow); - } - - /** Whether Message has ExpiresHeader */ - public boolean hasExpiresHeader() { - return hasHeader(SipHeaders.Expires); - } - - /** Gets ExpiresHeader of Message */ - public ExpiresHeader getExpiresHeader() { - Header h = getHeader(SipHeaders.Expires); - if (h == null) - return null; - else - return new ExpiresHeader(h); - } - - /** Sets ExpiresHeader of Message */ - public void setExpiresHeader(ExpiresHeader eh) { - setHeader(eh); - } - - /** Removes ExpiresHeader from Message (if it exists) */ - public void removeExpiresHeader() { - removeHeader(SipHeaders.Expires); - } - - /** Whether Message has ContentTypeHeader */ - public boolean hasContentTypeHeader() { - return hasHeader(SipHeaders.Content_Type); - } - - /** Gets ContentTypeHeader of Message */ - public ContentTypeHeader getContentTypeHeader() { - Header h = getHeader(SipHeaders.Content_Type); - if (h == null) - return null; - else - return new ContentTypeHeader(h); - } - - /** Sets ContentTypeHeader of Message */ - protected void setContentTypeHeader(ContentTypeHeader cth) { - setHeader(cth); - } - - /** Removes ContentTypeHeader from Message (if it exists) */ - protected void removeContentTypeHeader() { - removeHeader(SipHeaders.Content_Type); - } - - /** Whether Message has ContentLengthHeader */ - public boolean hasContentLengthHeader() { - return hasHeader(SipHeaders.Content_Length); - } - - /** Gets ContentLengthHeader of Message */ - public ContentLengthHeader getContentLengthHeader() { - Header h = getHeader(SipHeaders.Content_Length); - if (h == null) - return null; - else - return new ContentLengthHeader(h); - } - - /** Sets ContentLengthHeader of Message */ - protected void setContentLengthHeader(ContentLengthHeader clh) { - setHeader(clh); - } - - /** Removes ContentLengthHeader from Message (if it exists) */ - protected void removeContentLengthHeader() { - removeHeader(SipHeaders.Content_Length); - } - - /** Whether Message has Body */ - public boolean hasBody() { - if (hasContentLengthHeader()) - return getContentLengthHeader().getContentLength() > 0; - else - return hasContentTypeHeader(); - } - - /** Gets body(content) type */ - public String getBodyType() { - return getContentTypeHeader().getContentType(); - } - - /** Sets the message body */ - public void setBody(String content_type, String body) { - removeBody(); - if (body != null && body.length() > 0) { - setContentTypeHeader(new ContentTypeHeader(content_type)); - setContentLengthHeader(new ContentLengthHeader(body.length())); - message = message + "\r\n" + body; - } else { - setContentLengthHeader(new ContentLengthHeader(0)); - message = message + "\r\n"; - } - } - - /** Sets sdp body */ - public void setBody(String body) { - setBody("application/sdp", body); - } - - /** - * Gets message body. The end of body is evaluated from the Content-Length - * header if present (SIP-RFC compliant), or from the end of message if no - * Content-Length header is present (non-SIP-RFC compliant) - */ - public String getBody() { // if (!hasBody()) return ""; - if (!hasBody()) - return null; - int begin = (new SipParser(message)).goToBody().getPos(); - int len; - // the following 'if' is for robustness with non SIP-compliant UAs; - // copliant UAs must insert Content-Length header when body is present.. - if (this.hasContentLengthHeader()) - len = getContentLengthHeader().getContentLength(); - else { // printWarning("No Content-Length header found for the - // Body",3); - len = message.length() - begin; - } - int end = begin + len; - if (end > message.length()) { // printWarning("Found a Message Body - // shorter than Content-Length",3); - end = message.length(); - } - return message.substring(begin, end); - } - - /** Removes the message body (if it exists) and the final empty line */ - public void removeBody() { - int pos = (new SipParser(message)).goToEndOfLastHeader().goToNextLine() - .getPos(); - message = message.substring(0, pos); - removeContentLengthHeader(); - removeContentTypeHeader(); - } - - // **************************** Authentication ****************************/ - - /** Whether has AuthenticationInfoHeader */ - public boolean hasAuthenticationInfoHeader() { - return hasHeader(SipHeaders.Authentication_Info); - } - - /** Sets AuthenticationInfoHeader */ - public void setAuthenticationInfoHeader(AuthenticationInfoHeader h) { - setHeader(h); - } - - /** Gets AuthenticationInfoHeader */ - public AuthenticationInfoHeader getAuthenticationInfoHeader() { - Header h = getHeader(SipHeaders.Authentication_Info); - if (h == null) - return null; - else - return new AuthenticationInfoHeader(h); - } - - /** Removes AuthenticationInfoHeader */ - public void removeAuthenticationInfoHeader() { - removeHeader(SipHeaders.Authentication_Info); - } - - /** Whether has AuthorizationHeader */ - public boolean hasAuthorizationHeader() { - return hasHeader(SipHeaders.Authorization); - } - - /** Sets AuthorizationHeader */ - public void setAuthorizationHeader(AuthorizationHeader h) { - setHeader(h); - } - - /** Gets AuthorizationHeader */ - public AuthorizationHeader getAuthorizationHeader() { - Header h = getHeader(SipHeaders.Authorization); - if (h == null) - return null; - else - return new AuthorizationHeader(h); - } - - /** Removes AuthorizationHeader */ - public void removeAuthorizationHeader() { - removeHeader(SipHeaders.Authorization); - } - - /** Whether has WwwAuthenticateHeader */ - public boolean hasWwwAuthenticateHeader() { - return hasHeader(SipHeaders.WWW_Authenticate); - } - - /** Sets WwwAuthenticateHeader */ - public void setWwwAuthenticateHeader(WwwAuthenticateHeader h) { - setHeader(h); - } - - /** Gets WwwAuthenticateHeader */ - public WwwAuthenticateHeader getWwwAuthenticateHeader() { - Header h = getHeader(SipHeaders.WWW_Authenticate); - if (h == null) - return null; - else - return new WwwAuthenticateHeader(h); - } - - /** Removes WwwAuthenticateHeader */ - public void removeWwwAuthenticateHeader() { - removeHeader(SipHeaders.WWW_Authenticate); - } - - /** Whether has ProxyAuthenticateHeader */ - public boolean hasProxyAuthenticateHeader() { - return hasHeader(SipHeaders.Proxy_Authenticate); - } - - /** Sets ProxyAuthenticateHeader */ - public void setProxyAuthenticateHeader(ProxyAuthenticateHeader h) { - setHeader(h); - } - - /** Gets ProxyAuthenticateHeader */ - public ProxyAuthenticateHeader getProxyAuthenticateHeader() { - Header h = getHeader(SipHeaders.Proxy_Authenticate); - if (h == null) - return null; - else - return new ProxyAuthenticateHeader(h); - } - - /** Removes ProxyAuthenticateHeader */ - public void removeProxyAuthenticateHeader() { - removeHeader(SipHeaders.Proxy_Authenticate); - } - - /** Whether has ProxyAuthorizationHeader */ - public boolean hasProxyAuthorizationHeader() { - return hasHeader(SipHeaders.Proxy_Authorization); - } - - /** Sets ProxyAuthorizationHeader */ - public void setProxyAuthorizationHeader(ProxyAuthorizationHeader h) { - setHeader(h); - } - - /** Gets ProxyAuthorizationHeader */ - public ProxyAuthorizationHeader getProxyAuthorizationHeader() { - Header h = getHeader(SipHeaders.Proxy_Authorization); - if (h == null) - return null; - else - return new ProxyAuthorizationHeader(h); - } - - /** Removes ProxyAuthorizationHeader */ - public void removeProxyAuthorizationHeader() { - removeHeader(SipHeaders.Proxy_Authorization); - } - - // **************************** RFC 2543 Legacy - // ****************************/ - - /** - * Checks whether the next Route is formed according to RFC2543 Strict Route - * and adapts the message. - */ - public void rfc2543RouteAdapt() { - if (hasRouteHeader()) { - MultipleHeader mrh = getRoutes(); - // RouteHeader rh = new RouteHeader(mrh.getTop()); - if (!(new RouteHeader(mrh.getTop())).getNameAddress().getAddress() - .hasLr()) { // re-format the message according to the - // RFC2543 Strict Route rule - SipURL next_hop = (new RouteHeader(mrh.getTop())) - .getNameAddress().getAddress(); - SipURL recipient = getRequestLine().getAddress(); - mrh.removeTop(); - mrh.addBottom(new RouteHeader(new NameAddress(recipient))); - setRoutes(mrh); - setRequestLine(new RequestLine(getRequestLine().getMethod(), - next_hop)); - } - } - } - - /** - * Changes form RFC2543 Strict Route to RFC3261 Lose Route. - *

- * The Request-URI is replaced with the last value from the Route header, - * and that value is removed from the Route header. - */ - public void rfc2543toRfc3261RouteUpdate() { // the message is formed - // according with RFC2543 strict - // route - // the next hop is the request-uri - // the recipient of the message is the last Route value - RequestLine request_line = getRequestLine(); - SipURL next_hop = request_line.getAddress(); - MultipleHeader mrh = getRoutes(); - SipURL target = (new RouteHeader(mrh.getBottom())).getNameAddress() - .getAddress(); - mrh.removeBottom(); - next_hop.addLr(); - mrh.addTop(new RouteHeader(new NameAddress(next_hop))); - removeRoutes(); - addRoutes(mrh); - setRequestLine(new RequestLine(request_line.getMethod(), target)); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.message; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.header.*; +import org.zoolu.sip.address.*; +import org.zoolu.sip.message.SipMethods; +import org.zoolu.net.UdpPacket; +import java.util.*; + +/** Class BaseMessage implements a generic SIP Message. */ +public abstract class BaseMessage { + /** UDP */ + public static final String PROTO_UDP = "udp"; + /** TCP */ + public static final String PROTO_TCP = "tcp"; + /** TLS */ + public static final String PROTO_TLS = "tls"; + /** SCTP */ + public static final String PROTO_SCTP = "sctp"; + + /** Maximum receiving packet size */ + protected static int MAX_PKT_SIZE = 8000; + + /** The remote ip address */ + protected String remote_addr; + + /** The remote port */ + protected int remote_port; + + /** Transport protocol */ + protected String transport_proto; + + /** Connection identifier */ + protected ConnectionIdentifier connection_id; + + /** Packet length */ + // protected int packet_length; + /** The message string */ + private String message; + + /** Inits empty Message */ + private void init() { // message=""; + remote_addr = null; + remote_port = 0; + transport_proto = null; + connection_id = null; + } + + /** Costructs a new empty Message */ + public BaseMessage() { + init(); + message = ""; + } + + /** Costructs a new Message */ + public BaseMessage(byte[] data, int offset, int len) { + init(); + message = new String(data, offset, len); + } + + /** Costructs a new Message */ + public BaseMessage(UdpPacket packet) { + init(); + message = new String(packet.getData(), packet.getOffset(), packet + .getLength()); + } + + /** Costructs a new Message */ + public BaseMessage(String str) { + init(); + message = new String(str); + } + + /** Costructs a new Message */ + public BaseMessage(BaseMessage msg) { // message=new String(msg.message); + message = msg.message; + remote_addr = msg.remote_addr; + remote_port = msg.remote_port; + transport_proto = msg.transport_proto; + connection_id = msg.connection_id; + // packet_length=msg.packet_length; + } + + /** Creates and returns a clone of the Message */ + abstract public Object clone(); + + // { return new Message(message); + // } + + /** Sets the entire message */ + public void setMessage(String message) { + this.message = message; + } + + /** Gets string representation of Message */ + public String toString() { + return message; + } + + /** Gets remote ip address */ + public String getRemoteAddress() { + return remote_addr; + } + + /** Gets remote port */ + public int getRemotePort() { + return remote_port; + } + + /** Gets transport protocol */ + public String getTransportProtocol() { + return transport_proto; + } + + /** Gets connection identifier */ + public ConnectionIdentifier getConnectionId() { + return connection_id; + } + + /** Gets message length */ + public int getLength() { + return message.length(); + } + + /** Sets remote ip address */ + public void setRemoteAddress(String addr) { + remote_addr = addr; + } + + /** Sets remote port */ + public void setRemotePort(int port) { + remote_port = port; + } + + /** Sets transport protocol */ + public void setTransport(String proto) { + transport_proto = proto; + } + + /** Sets connection identifier */ + public void setConnectionId(ConnectionIdentifier conn_id) { + connection_id = conn_id; + } + + /** Gets the inique DialogIdentifier for an INCOMING message */ + public DialogIdentifier getDialogId() { + String call_id = getCallIdHeader().getCallId(); + String local_tag, remote_tag; + if (isRequest()) { + local_tag = getToHeader().getTag(); + remote_tag = getFromHeader().getTag(); + } else { + local_tag = getFromHeader().getTag(); + remote_tag = getToHeader().getTag(); + } + return new DialogIdentifier(call_id, local_tag, remote_tag); + } + + /** Gets the unique TransactionIdentifier */ + public TransactionIdentifier getTransactionId() { + String call_id = getCallIdHeader().getCallId(); + ViaHeader top_via = getViaHeader(); + String branch = null; + if (top_via != null && top_via.hasBranch()) + branch = top_via.getBranch(); +// String sent_by = top_via.getSentBy(); + CSeqHeader cseqh = getCSeqHeader(); + long seqn = cseqh.getSequenceNumber(); + String method = cseqh.getMethod(); + return new TransactionIdentifier(call_id, seqn, method, null /* sent_by modified */, branch); + } + + /** Gets the MethodIdentifier */ + public MethodIdentifier getMethodId() { + String method = getCSeqHeader().getMethod(); + return new MethodIdentifier(method); + } + + // **************************** Requests ****************************/ + + /** Whether Message is a Request */ + public boolean isRequest() throws NullPointerException { // Req-Line = + // Method ' ' + // SIP-URL ' ' + // "SIP/2.0" + // CRLF + if (message == null || isResponse()) + return false; + String firstline = (new SipParser(message)).getLine(); + String version = (new SipParser(firstline)).skipString().skipString() + .getString(); + if (version == null || version.length() < 4) + return false; + version = version.substring(0, 4); + String target = "SIP/"; + // if (version.compareToIgnoreCase(target)==0) return true; + if (version.equalsIgnoreCase(target)) + return true; + return false; + } + + /** Whether Message is a method request */ + public boolean isRequest(String method) { // if (message==null) return + // false; + if (message.startsWith(method)) + return true; + else + return false; + } + + /** Whether Message is a Method that creates a dialog */ + public boolean createsDialog() { + if (!isRequest()) + return false; + // else + String method = getRequestLine().getMethod(); + for (int i = 0; i < SipMethods.dialog_methods.length; i++) + if (method.equalsIgnoreCase(SipMethods.dialog_methods[i])) + return true; + // else + return false; + } + + /** Whether Message is an Invite */ + public boolean isInvite() { + return isRequest(SipMethods.INVITE); + } + + /** Whether Message is a Register */ + public boolean isRegister() { + return isRequest(SipMethods.REGISTER); + } + + /** Whether Message is a Cancel */ + public boolean isCancel() { + return isRequest(SipMethods.CANCEL); + } + + /** Whether Message is a Bye */ + public boolean isBye() { + return isRequest(SipMethods.BYE); + } + + /** Whether Message is an Ack */ + public boolean isAck() { + return isRequest(SipMethods.ACK); + } + + /** Whether Message is an Info */ + public boolean isInfo() { + return isRequest(SipMethods.INFO); + } + + /** Whether Message is an Option */ + public boolean isOption() { + return isRequest(SipMethods.OPTION); + } + + /** Whether Message has Request-line */ + protected boolean hasRequestLine() { + return isRequest(); + } + + /** + * Gets RequestLine in Message (Returns null if called for no request + * message) + */ + public RequestLine getRequestLine() { + if (!isRequest()) { // printWarning("getRequestLine(): called for no + // request message\n",1); + return null; + } + SipParser par = new SipParser(message); + String method = par.getString(); + par.skipWSP(); + par = new SipParser(par.subParser(par.indexOfEOH() - par.getPos())); + return new RequestLine(method, par.getSipURL()); + } + + /** Sets RequestLine of the Message */ + public void setRequestLine(RequestLine rl) { + if (hasRequestLine()) + removeRequestLine(); + String value = rl.toString(); + message = value + message; + } + + /** Removes RequestLine of the Message */ + public void removeRequestLine() { + if (!isRequest()) + return; + removeFirstLine(); + } + + // **************************** Responses ****************************/ + + /** Whether Message is a Response */ + public boolean isResponse() throws NullPointerException { // Status-Line = + // "SIP/2.0" ' ' + // Status-Code ' + // 'Reason-Phrase" + // CRLF + // if (message==null) return false; + if (message == null || message.length() < 4) + return false; + String version = message.substring(0, 4); + String target = "SIP/"; + // if (version.compareToIgnoreCase(target)==0) return true; + if (version.equalsIgnoreCase(target)) + return true; + + return false; + } + + /** Whether Message has Status-line */ + protected boolean hasStatusLine() { + return isResponse(); + } + + /** + * Gets StautsLine in Message (Returns null if called for no response + * message) + */ + public StatusLine getStatusLine() { + if (!isResponse()) { // printWarning("getStatusLine(): called for no + // response message\n",1); + return null; + } + SipParser par = new SipParser(message); + par.skipString().skipWSP(); // "SIP/2.0 " + int code = par.getInt(); + int begin = par.getPos(); + int end = par.indexOfEOH(); + String reason = message.substring(begin, end).trim(); + return new StatusLine(code, reason); + } + + /** Sets StatusLine of the Message */ + public void setStatusLine(StatusLine sl) { + if (hasStatusLine()) + removeStatusLine(); + message = sl.toString() + message; + } + + /** Removes StatusLine of the Message */ + public void removeStatusLine() { + if (!isResponse()) + return; + removeFirstLine(); + } + + // **************************** Generic Headers + // ****************************/ + + /** Returns the transaction method */ + public String getTransactionMethod() { + return getCSeqHeader().getMethod(); + } + + /** Gets the first line of the Message */ + public String getFirstLine() { + if (isRequest()) + return getRequestLine().toString(); + if (isResponse()) + return getStatusLine().toString(); + return null; + } + + /** Sets Request/Status Line of the Message */ + /* + * private void setFirstLine(String value) { message=value+" \r\n"+message; } + */ + + /** Removes Request\Status Line of the Message */ + protected void removeFirstLine() { + message = message.substring((new SipParser(message)) + .indexOfNextHeader()); + } + + /** Whether Message has any headers of specified name */ + public boolean hasHeader(String name) { + Header hd = getHeader(name); + if (hd == null) + return false; + else + return true; + } + + /** + * Gets the first Header of specified name (Returns null if no Header is + * found) + */ + public Header getHeader(String hname) { + SipParser par = new SipParser(message); + return par.getHeader(hname); + } + + /** + * Gets a Vector of all Headers of specified name (Returns empty Vector if + * no Header is found) + */ + public Vector

getHeaders(String hname) { + Vector
v = new Vector
(); + SipParser par = new SipParser(message); + Header h; + while ((h = par.getHeader(hname)) != null) { + v.addElement(h); + } + return v; + } + + /** + * Adds Header at the top/bottom. The bottom is considered before the + * Content-Length and Content-Type headers + */ + public void addHeader(Header header, boolean top) { + addHeaders(header.toString(), top); + } + + /** Adds a Vector of Headers at the top/bottom */ + public void addHeaders(Vector
headers, boolean top) { + String str = ""; + for (int i = 0; i < headers.size(); i++) + str += ((Header) headers.elementAt(i)).toString(); + addHeaders(str, top); + } + + /** Adds MultipleHeader(s) mheader at the top/bottom */ + public void addHeaders(MultipleHeader mheader, boolean top) { + addHeaders(mheader.toString(), top); + } + + /** + * Adds a one or more Headers at the top/bottom. The bottom is considered + * before the Content-Length and Content-Type headers + */ + protected void addHeaders(String str, boolean top) { + int i, aux; + if (top) { + if (this.hasRequestLine() || this.hasStatusLine()) { + SipParser par = new SipParser(message); + par.goToNextHeader(); + i = par.getPos(); + } else + i = 0; + } else { + SipParser par = new SipParser(message); + // index the end of headers + i = par.goToEndOfLastHeader().goToNextLine().getPos(); + par = new SipParser(message); + // if Content_Length is present, jump before + aux = par.indexOfHeader(SipHeaders.Content_Length); + if (aux < i) + i = aux; + // if Content_Type is present, jump before + aux = par.indexOfHeader(SipHeaders.Content_Type); + if (aux < i) + i = aux; + } + String head = message.substring(0, i); + String tail = message.substring(i); + String new_message = head.concat(str); + new_message = new_message.concat(tail); + message = new_message; + } + + /** Adds Headers on position index within the Message */ + protected void addHeaders(String str, int index) { + if (index > message.length()) + index = message.length(); + message = message.substring(0, index) + str + message.substring(index); + } + + /** + * Adds Header before the first header refer_header . + *

+ * If there is no header of such type, it is added at top + */ + public void addHeaderBefore(Header new_header, String refer_header) { + addHeadersBefore(new_header.toString(), refer_header); + } + + /** + * Adds MultipleHeader(s) before the first header refer_header . + *

+ * If there is no header of such type, they are added at top + */ + public void addHeadersBefore(MultipleHeader mheader, String refer_header) { + addHeadersBefore(mheader.toString(), refer_header); + } + + /** + * Adds Headers before the first header refer_header . + *

+ * If there is no header of such type, they are added at top + */ + protected void addHeadersBefore(String str, String refer_header) { + if (!hasHeader(refer_header)) + addHeaders(str, true); + else { + SipParser par = new SipParser(message); + par.goTo(refer_header); + int here = par.getPos(); + message = message.substring(0, here) + str + + message.substring(here); + } + } + + /** + * Adds Header after the first header refer_header . + *

+ * If there is no header of such type, it is added at bottom + */ + public void addHeaderAfter(Header new_header, String refer_header) { + addHeadersAfter(new_header.toString(), refer_header); + } + + /** + * Adds MultipleHeader(s) after the first header refer_header . + *

+ * If there is no header of such type, they are added at bottom + */ + public void addHeadersAfter(MultipleHeader mheader, String refer_header) { + addHeadersAfter(mheader.toString(), refer_header); + } + + /** + * Adds Headers after the first header refer_header . + *

+ * If there is no header of such type, they are added at bottom + */ + protected void addHeadersAfter(String str, String refer_header) { + if (!hasHeader(refer_header)) + addHeaders(str, false); + else { + SipParser par = new SipParser(message); + par.goTo(refer_header); + int here = par.indexOfNextHeader(); + message = message.substring(0, here) + str + + message.substring(here); + } + } + + /** Removes first Header of specified name */ + public void removeHeader(String hname) { + removeHeader(hname, true); + } + + /** Removes first (or last) Header of specified name */ + public void removeHeader(String hname, boolean first) { + String[] target = { '\n' + hname, '\r' + hname }; + SipParser par = new SipParser(message); + par.goTo(target); + if (!par.hasMore()) + return; + if (!first) + while (true) { + int next = par.indexOf(target); + if (next < 0) + break; + par.setPos(next); + } + par.skipChar(); + String head = message.substring(0, par.getPos()); + par.goToNextHeader(); + String tail = message.substring(par.getPos()); + message = head.concat(tail); + } + + /** Sets the new Header (removing any previous headers of the same name) */ + /* + * public void setHeader(Header hd) { if (hasHeader(hd.getName())) + * removeAllHeaders(hd.getName()); addHeader(hd,false); } + */ + + /** Sets the Header hd removing any previous headers of the same type */ + public void setHeader(Header hd) { + String hname = hd.getName(); + if (hasHeader(hname)) { + int index = (new SipParser(message)).indexOfHeader(hname); + removeAllHeaders(hname); + addHeaders(hd.toString(), index); + } else + addHeader(hd, false); + } + + /** Removes all Headers of specified name */ + public void removeAllHeaders(String hname) { + String[] target = { '\n' + hname, '\r' + hname }; + SipParser par = new SipParser(message); + par.goTo(target); + while (par.hasMore()) { + par.skipChar(); + String head = message.substring(0, par.getPos()); + String tail = message.substring(par.indexOfNextHeader()); + message = head.concat(tail); + par = new SipParser(message, par.getPos() - 1); + par.goTo(target); + } + } + + /** Sets MultipleHeader mheader */ + /* + * public void setHeaders(MultipleHeader mheader) { if + * (hasHeader(mheader.getName())) removeAllHeaders(mheader.getName()); + * addHeaders(mheader,false); } + */ + + /** Sets MultipleHeader mheader */ + public void setHeaders(MultipleHeader mheader) { + String hname = mheader.getName(); + if (hasHeader(hname)) { + int index = (new SipParser(message)).indexOfHeader(hname); + removeAllHeaders(hname); + addHeaders(mheader.toString(), index); + } else + addHeaders(mheader, false); + } + + // **************************** Specific Headers + // ****************************/ + + /** Whether Message has MaxForwardsHeader */ + public boolean hasMaxForwardsHeader() { + return hasHeader(SipHeaders.Max_Forwards); + } + + /** Gets MaxForwardsHeader of Message */ + public MaxForwardsHeader getMaxForwardsHeader() { + Header h = getHeader(SipHeaders.Max_Forwards); + if (h == null) + return null; + else + return new MaxForwardsHeader(h); + } + + /** Sets MaxForwardsHeader of Message */ + public void setMaxForwardsHeader(MaxForwardsHeader mfh) { + setHeader(mfh); + } + + /** Removes MaxForwardsHeader from Message */ + public void removeMaxForwardsHeader() { + removeHeader(SipHeaders.Max_Forwards); + } + + /** Whether Message has FromHeader */ + public boolean hasFromHeader() { + return hasHeader(SipHeaders.From); + } + + /** Gets FromHeader of Message */ + public FromHeader getFromHeader() { + Header h = getHeader(SipHeaders.From); + if (h == null) + return null; + else + return new FromHeader(h); + } + + /** Sets FromHeader of Message */ + public void setFromHeader(FromHeader fh) { + setHeader(fh); + } + + /** Removes FromHeader from Message */ + public void removeFromHeader() { + removeHeader(SipHeaders.From); + } + + /** Whether Message has ToHeader */ + public boolean hasToHeader() { + return hasHeader(SipHeaders.To); + } + + /** Gets ToHeader of Message */ + public ToHeader getToHeader() { + Header h = getHeader(SipHeaders.To); + if (h == null) + return null; + else + return new ToHeader(h); + } + + /** Sets ToHeader of Message */ + public void setToHeader(ToHeader th) { + setHeader(th); + } + + /** Removes ToHeader from Message */ + public void removeToHeader() { + removeHeader(SipHeaders.To); + } + + /** Whether Message has ContactHeader */ + public boolean hasContactHeader() { + return hasHeader(SipHeaders.Contact); + } + + /** + * Deprecated. Gets ContactHeader of Message. Use getContacts + * instead. + */ + public ContactHeader getContactHeader() { // Header + // h=getHeader(SipHeaders.Contact); + // if (h==null) return null; else return new ContactHeader(h); + MultipleHeader mh = getContacts(); + if (mh == null) + return null; + return new ContactHeader(mh.getTop()); + } + + /** Adds ContactHeader */ + public void addContactHeader(ContactHeader ch, boolean top) { + addHeader(ch, top); + } + + /** Sets ContactHeader */ + public void setContactHeader(ContactHeader ch) { + if (hasContactHeader()) + removeContacts(); + addHeader(ch, false); + } + + /** Gets a MultipleHeader of Contacts */ + public MultipleHeader getContacts() { + Vector

v = getHeaders(SipHeaders.Contact); + if (v.size() > 0) + return new MultipleHeader(v); + else + return null; + } + + /** Adds Contacts */ + public void addContacts(MultipleHeader contacts, boolean top) { + addHeaders(contacts, top); + } + + /** Sets Contacts */ + public void setContacts(MultipleHeader contacts) { + if (hasContactHeader()) + removeContacts(); + addContacts(contacts, false); + } + + /** Removes ContactHeaders from Message */ + public void removeContacts() { + removeAllHeaders(SipHeaders.Contact); + } + + /** Whether Message has ViaHeaders */ + public boolean hasViaHeader() { + return hasHeader(SipHeaders.Via); + } + + /** Adds ViaHeader at the top */ + public void addViaHeader(ViaHeader vh) { + addHeader(vh, true); + } + + /** Gets the first ViaHeader */ + public ViaHeader getViaHeader() { // Header h=getHeader(SipHeaders.Via); + // if (h==null) return null; else return new ViaHeader(h); + MultipleHeader mh = getVias(); + if (mh == null) + return null; + return new ViaHeader(mh.getTop()); + } + + /** Removes the top ViaHeader */ + public void removeViaHeader() { // removeHeader(SipHeaders.Via); + MultipleHeader mh = getVias(); + mh.removeTop(); + setVias(mh); + } + + /** Gets all Vias */ + public MultipleHeader getVias() { + Vector
v = getHeaders(SipHeaders.Via); + if (v.size() > 0) + return new MultipleHeader(v); + else + return null; + } + + /** Adds Vias */ + public void addVias(MultipleHeader vias, boolean top) { + addHeaders(vias, top); + } + + /** Sets Vias */ + public void setVias(MultipleHeader vias) { + if (hasViaHeader()) + removeVias(); + addContacts(vias, true); + } + + /** Removes ViaHeaders from Message (if any exists) */ + public void removeVias() { + removeAllHeaders(SipHeaders.Via); + } + + /** Whether Message has RouteHeader */ + public boolean hasRouteHeader() { + return hasHeader(SipHeaders.Route); + } + + /** Adds RouteHeader at the top */ + public void addRouteHeader(RouteHeader h) { + addHeaderAfter(h, SipHeaders.Via); + } + + /** Adds multiple Route headers at the top */ + public void addRoutes(MultipleHeader routes) { + addHeadersAfter(routes, SipHeaders.Via); + } + + /** Gets the top RouteHeader */ + public RouteHeader getRouteHeader() { // Header + // h=getHeader(SipHeaders.Route); + // if (h==null) return null; else return new RouteHeader(h); + MultipleHeader mh = getRoutes(); + if (mh == null) + return null; + return new RouteHeader(mh.getTop()); + } + + /** Gets the whole route */ + public MultipleHeader getRoutes() { + Vector
v = getHeaders(SipHeaders.Route); + if (v.size() > 0) + return new MultipleHeader(v); + else + return null; + } + + /** Removes the top RouteHeader */ + public void removeRouteHeader() { // removeHeader(SipHeaders.Route); + MultipleHeader mh = getRoutes(); + mh.removeTop(); + setRoutes(mh); + } + + /** Removes all RouteHeaders from Message (if any exists) */ + public void removeRoutes() { + removeAllHeaders(SipHeaders.Route); + } + + /** Sets the whole route */ + public void setRoutes(MultipleHeader routes) { + if (hasRouteHeader()) + removeRoutes(); + addRoutes(routes); + } + + /** Whether Message has RecordRouteHeader */ + public boolean hasRecordRouteHeader() { + return hasHeader(SipHeaders.Record_Route); + } + + /** Adds RecordRouteHeader at the top */ + public void addRecordRouteHeader(RecordRouteHeader rr) { // addHeaderAfter(rr,SipHeaders.Via); + addHeaderAfter(rr, SipHeaders.CSeq); + } + + /** Adds multiple RecordRoute headers at the top */ + public void addRecordRoutes(MultipleHeader routes) { // addHeadersAfter(routes,SipHeaders.Via); + addHeadersAfter(routes, SipHeaders.CSeq); + } + + /** Gets the top RecordRouteHeader */ + public RecordRouteHeader getRecordRouteHeader() { // Header + // h=getHeader(SipHeaders.Record_Route); + // if (h==null) return null; else return new RecordRouteHeader(h); + MultipleHeader mh = getRecordRoutes(); + if (mh == null) + return null; + return new RecordRouteHeader(mh.getTop()); + } + + /** Gets the whole RecordRoute headers */ + public MultipleHeader getRecordRoutes() { + Vector
v = getHeaders(SipHeaders.Record_Route); + if (v.size() > 0) + return new MultipleHeader(v); + else + return null; + } + + /** Removes the top RecordRouteHeader */ + public void removeRecordRouteHeader() { // removeHeader(SipHeaders.Record_Route); + MultipleHeader mh = getRecordRoutes(); + mh.removeTop(); + setRecordRoutes(mh); + } + + /** Removes all RecordRouteHeader from Message (if any exists) */ + public void removeRecordRoutes() { + removeAllHeaders(SipHeaders.Record_Route); + } + + /** Sets the whole RecordRoute headers */ + public void setRecordRoutes(MultipleHeader routes) { + if (hasRecordRouteHeader()) + removeRecordRoutes(); + addRecordRoutes(routes); + } + + /** Whether Message has CSeqHeader */ + public boolean hasCSeqHeader() { + return hasHeader(SipHeaders.CSeq); + } + + /** Gets CSeqHeader of Message */ + public CSeqHeader getCSeqHeader() { + Header h = getHeader(SipHeaders.CSeq); + if (h == null) + return null; + else + return new CSeqHeader(h); + } + + /** Sets CSeqHeader of Message */ + public void setCSeqHeader(CSeqHeader csh) { + setHeader(csh); + } + + /** Removes CSeqHeader from Message */ + public void removeCSeqHeader() { + removeHeader(SipHeaders.CSeq); + } + + /** Whether has CallIdHeader */ + public boolean hasCallIdHeader() { + return hasHeader(SipHeaders.Call_ID); + } + + /** Sets CallIdHeader of Message */ + public void setCallIdHeader(CallIdHeader cih) { + setHeader(cih); + } + + /** Gets CallIdHeader of Message */ + public CallIdHeader getCallIdHeader() { + Header h = getHeader(SipHeaders.Call_ID); + if (h == null) + return null; + else + return new CallIdHeader(h); + } + + /** Removes CallIdHeader from Message */ + public void removeCallIdHeader() { + removeHeader(SipHeaders.Call_ID); + } + + /** Whether Message has SubjectHeader */ + public boolean hasSubjectHeader() { + return hasHeader(SipHeaders.Subject); + } + + /** Sets SubjectHeader of Message */ + public void setSubjectHeader(SubjectHeader sh) { + setHeader(sh); + } + + /** Gets SubjectHeader of Message */ + public SubjectHeader getSubjectHeader() { + Header h = getHeader(SipHeaders.Subject); + if (h == null) + return null; + else + return new SubjectHeader(h); + } + + /** Removes SubjectHeader from Message */ + public void removeSubjectHeader() { + removeHeader(SipHeaders.Subject); + } + + /** Whether Message has DateHeader */ + public boolean hasDateHeader() { + return hasHeader(SipHeaders.Date); + } + + /** Gets DateHeader of Message */ + public DateHeader getDateHeader() { + Header h = getHeader(SipHeaders.Date); + if (h == null) + return null; + else + return new DateHeader(h); + } + + /** Sets DateHeader of Message */ + public void setDateHeader(DateHeader dh) { + setHeader(dh); + } + + /** Removes DateHeader from Message (if it exists) */ + public void removeDateHeader() { + removeHeader(SipHeaders.Date); + } + + /** Whether has UserAgentHeader */ + public boolean hasUserAgentHeader() { + return hasHeader(SipHeaders.User_Agent); + } + + /** Sets UserAgentHeader */ + public void setUserAgentHeader(UserAgentHeader h) { + setHeader(h); + } + + /** Gets UserAgentHeader */ + public UserAgentHeader getUserAgentHeader() { + Header h = getHeader(SipHeaders.User_Agent); + if (h == null) + return null; + else + return new UserAgentHeader(h); + } + + /** Removes UserAgentHeader */ + public void removeUserAgentHeader() { + removeHeader(SipHeaders.User_Agent); + } + + /** Whether has ServerHeader */ + public boolean hasServerHeader() { + return hasHeader(SipHeaders.Server); + } + + /** Sets ServerHeader */ + public void setServerHeader(ServerHeader h) { + setHeader(h); + } + + /** Gets ServerHeader */ + public ServerHeader getServerHeader() { + Header h = getHeader(SipHeaders.Server); + if (h == null) + return null; + else + return new ServerHeader(h); + } + + /** Removes ServerHeader */ + public void removeServerHeader() { + removeHeader(SipHeaders.Server); + } + + /** Sets AcceptContactHeader */ + public void setAcceptContactHeader(AcceptContactHeader h) { // added by mandrajg + setHeader(h); + } + + /** Whether has AcceptHeader */ + public boolean hasAcceptHeader() { + return hasHeader(SipHeaders.Accept); + } + + /** Sets AcceptHeader */ + public void setAcceptHeader(AcceptHeader h) { + setHeader(h); + } + + /** Gets AcceptHeader */ + public AcceptHeader getAcceptHeader() { + Header h = getHeader(SipHeaders.Accept); + if (h == null) + return null; + else + return new AcceptHeader(h); + } + + /** Removes AcceptHeader */ + public void removeAcceptHeader() { + removeHeader(SipHeaders.Accept); + } + + /** Whether has AlertInfoHeader */ + public boolean hasAlertInfoHeader() { + return hasHeader(SipHeaders.Alert_Info); + } + + /** Sets AlertInfoHeader */ + public void setAlertInfoHeader(AlertInfoHeader h) { + setHeader(h); + } + + /** Gets AlertInfoHeader */ + public AlertInfoHeader getAlertInfoHeader() { + Header h = getHeader(SipHeaders.Alert_Info); + if (h == null) + return null; + else + return new AlertInfoHeader(h); + } + + /** Removes AlertInfoHeader */ + public void removeAlertInfoHeader() { + removeHeader(SipHeaders.Alert_Info); + } + + /** Whether has AllowHeader */ + public boolean hasAllowHeader() { + return hasHeader(SipHeaders.Allow); + } + + /** Sets AllowHeader */ + public void setAllowHeader(AllowHeader h) { + setHeader(h); + } + + /** Gets AllowHeader */ + public AllowHeader getAllowHeader() { + Header h = getHeader(SipHeaders.Allow); + if (h == null) + return null; + else + return new AllowHeader(h); + } + + /** Removes AllowHeader */ + public void removeAllowHeader() { + removeHeader(SipHeaders.Allow); + } + + /** Whether Message has ExpiresHeader */ + public boolean hasExpiresHeader() { + return hasHeader(SipHeaders.Expires); + } + + /** Gets ExpiresHeader of Message */ + public ExpiresHeader getExpiresHeader() { + Header h = getHeader(SipHeaders.Expires); + if (h == null) + return null; + else + return new ExpiresHeader(h); + } + + /** Sets ExpiresHeader of Message */ + public void setExpiresHeader(ExpiresHeader eh) { + setHeader(eh); + } + + /** Removes ExpiresHeader from Message (if it exists) */ + public void removeExpiresHeader() { + removeHeader(SipHeaders.Expires); + } + + /** Whether Message has ContentTypeHeader */ + public boolean hasContentTypeHeader() { + return hasHeader(SipHeaders.Content_Type); + } + + /** Gets ContentTypeHeader of Message */ + public ContentTypeHeader getContentTypeHeader() { + Header h = getHeader(SipHeaders.Content_Type); + if (h == null) + return null; + else + return new ContentTypeHeader(h); + } + + /** Sets ContentTypeHeader of Message */ + protected void setContentTypeHeader(ContentTypeHeader cth) { + setHeader(cth); + } + + /** Removes ContentTypeHeader from Message (if it exists) */ + protected void removeContentTypeHeader() { + removeHeader(SipHeaders.Content_Type); + } + + /** Whether Message has ContentLengthHeader */ + public boolean hasContentLengthHeader() { + return hasHeader(SipHeaders.Content_Length); + } + + /** Gets ContentLengthHeader of Message */ + public ContentLengthHeader getContentLengthHeader() { + Header h = getHeader(SipHeaders.Content_Length); + if (h == null) + return null; + else + return new ContentLengthHeader(h); + } + + /** Sets ContentLengthHeader of Message */ + protected void setContentLengthHeader(ContentLengthHeader clh) { + setHeader(clh); + } + + /** Removes ContentLengthHeader from Message (if it exists) */ + protected void removeContentLengthHeader() { + removeHeader(SipHeaders.Content_Length); + } + + /** Whether Message has Body */ + public boolean hasBody() { + if (hasContentLengthHeader()) + return getContentLengthHeader().getContentLength() > 0; + else + return hasContentTypeHeader(); + } + + /** Gets body(content) type */ + public String getBodyType() { + return getContentTypeHeader().getContentType(); + } + + /** Sets the message body */ + public void setBody(String content_type, String body) { + removeBody(); + if (body != null && body.length() > 0) { + setContentTypeHeader(new ContentTypeHeader(content_type)); + setContentLengthHeader(new ContentLengthHeader(body.length())); + message = message + "\r\n" + body; + } else { + setContentLengthHeader(new ContentLengthHeader(0)); + message = message + "\r\n"; + } + } + + /** Sets sdp body */ + public void setBody(String body) { + setBody("application/sdp", body); + } + + /** + * Gets message body. The end of body is evaluated from the Content-Length + * header if present (SIP-RFC compliant), or from the end of message if no + * Content-Length header is present (non-SIP-RFC compliant) + */ + public String getBody() { // if (!hasBody()) return ""; + if (!hasBody()) + return null; + int begin = (new SipParser(message)).goToBody().getPos(); + int len; + // the following 'if' is for robustness with non SIP-compliant UAs; + // copliant UAs must insert Content-Length header when body is present.. + if (this.hasContentLengthHeader()) + len = getContentLengthHeader().getContentLength(); + else { // printWarning("No Content-Length header found for the + // Body",3); + len = message.length() - begin; + } + int end = begin + len; + if (end > message.length()) { // printWarning("Found a Message Body + // shorter than Content-Length",3); + end = message.length(); + } + return message.substring(begin, end); + } + + /** Removes the message body (if it exists) and the final empty line */ + public void removeBody() { + int pos = (new SipParser(message)).goToEndOfLastHeader().goToNextLine() + .getPos(); + message = message.substring(0, pos); + removeContentLengthHeader(); + removeContentTypeHeader(); + } + + // **************************** Authentication ****************************/ + + /** Whether has AuthenticationInfoHeader */ + public boolean hasAuthenticationInfoHeader() { + return hasHeader(SipHeaders.Authentication_Info); + } + + /** Sets AuthenticationInfoHeader */ + public void setAuthenticationInfoHeader(AuthenticationInfoHeader h) { + setHeader(h); + } + + /** Gets AuthenticationInfoHeader */ + public AuthenticationInfoHeader getAuthenticationInfoHeader() { + Header h = getHeader(SipHeaders.Authentication_Info); + if (h == null) + return null; + else + return new AuthenticationInfoHeader(h); + } + + /** Removes AuthenticationInfoHeader */ + public void removeAuthenticationInfoHeader() { + removeHeader(SipHeaders.Authentication_Info); + } + + /** Whether has AuthorizationHeader */ + public boolean hasAuthorizationHeader() { + return hasHeader(SipHeaders.Authorization); + } + + /** Sets AuthorizationHeader */ + public void setAuthorizationHeader(AuthorizationHeader h) { + setHeader(h); + } + + /** Gets AuthorizationHeader */ + public AuthorizationHeader getAuthorizationHeader() { + Header h = getHeader(SipHeaders.Authorization); + if (h == null) + return null; + else + return new AuthorizationHeader(h); + } + + /** Removes AuthorizationHeader */ + public void removeAuthorizationHeader() { + removeHeader(SipHeaders.Authorization); + } + + /** Whether has WwwAuthenticateHeader */ + public boolean hasWwwAuthenticateHeader() { + return hasHeader(SipHeaders.WWW_Authenticate); + } + + /** Sets WwwAuthenticateHeader */ + public void setWwwAuthenticateHeader(WwwAuthenticateHeader h) { + setHeader(h); + } + + /** Gets WwwAuthenticateHeader */ + public WwwAuthenticateHeader getWwwAuthenticateHeader() { + Header h = getHeader(SipHeaders.WWW_Authenticate); + if (h == null) + return null; + else + return new WwwAuthenticateHeader(h); + } + + /** Removes WwwAuthenticateHeader */ + public void removeWwwAuthenticateHeader() { + removeHeader(SipHeaders.WWW_Authenticate); + } + + /** Whether has ProxyAuthenticateHeader */ + public boolean hasProxyAuthenticateHeader() { + return hasHeader(SipHeaders.Proxy_Authenticate); + } + + /** Sets ProxyAuthenticateHeader */ + public void setProxyAuthenticateHeader(ProxyAuthenticateHeader h) { + setHeader(h); + } + + /** Gets ProxyAuthenticateHeader */ + public ProxyAuthenticateHeader getProxyAuthenticateHeader() { + Header h = getHeader(SipHeaders.Proxy_Authenticate); + if (h == null) + return null; + else + return new ProxyAuthenticateHeader(h); + } + + /** Removes ProxyAuthenticateHeader */ + public void removeProxyAuthenticateHeader() { + removeHeader(SipHeaders.Proxy_Authenticate); + } + + /** Whether has ProxyAuthorizationHeader */ + public boolean hasProxyAuthorizationHeader() { + return hasHeader(SipHeaders.Proxy_Authorization); + } + + /** Sets ProxyAuthorizationHeader */ + public void setProxyAuthorizationHeader(ProxyAuthorizationHeader h) { + setHeader(h); + } + + /** Gets ProxyAuthorizationHeader */ + public ProxyAuthorizationHeader getProxyAuthorizationHeader() { + Header h = getHeader(SipHeaders.Proxy_Authorization); + if (h == null) + return null; + else + return new ProxyAuthorizationHeader(h); + } + + /** Removes ProxyAuthorizationHeader */ + public void removeProxyAuthorizationHeader() { + removeHeader(SipHeaders.Proxy_Authorization); + } + + // **************************** RFC 2543 Legacy + // ****************************/ + + /** + * Checks whether the next Route is formed according to RFC2543 Strict Route + * and adapts the message. + */ + public void rfc2543RouteAdapt() { + if (hasRouteHeader()) { + MultipleHeader mrh = getRoutes(); + // RouteHeader rh = new RouteHeader(mrh.getTop()); + if (!(new RouteHeader(mrh.getTop())).getNameAddress().getAddress() + .hasLr()) { // re-format the message according to the + // RFC2543 Strict Route rule + SipURL next_hop = (new RouteHeader(mrh.getTop())) + .getNameAddress().getAddress(); + SipURL recipient = getRequestLine().getAddress(); + mrh.removeTop(); + mrh.addBottom(new RouteHeader(new NameAddress(recipient))); + setRoutes(mrh); + setRequestLine(new RequestLine(getRequestLine().getMethod(), + next_hop)); + } + } + } + + /** + * Changes form RFC2543 Strict Route to RFC3261 Lose Route. + *

+ * The Request-URI is replaced with the last value from the Route header, + * and that value is removed from the Route header. + */ + public void rfc2543toRfc3261RouteUpdate() { // the message is formed + // according with RFC2543 strict + // route + // the next hop is the request-uri + // the recipient of the message is the last Route value + RequestLine request_line = getRequestLine(); + SipURL next_hop = request_line.getAddress(); + MultipleHeader mrh = getRoutes(); + SipURL target = (new RouteHeader(mrh.getBottom())).getNameAddress() + .getAddress(); + mrh.removeBottom(); + next_hop.addLr(); + mrh.addTop(new RouteHeader(new NameAddress(next_hop))); + removeRoutes(); + addRoutes(mrh); + setRequestLine(new RequestLine(request_line.getMethod(), target)); + } + +} diff --git a/src/org/zoolu/sip/message/BaseMessageFactory.java b/app/src/main/java/org/zoolu/sip/message/BaseMessageFactory.java similarity index 97% rename from src/org/zoolu/sip/message/BaseMessageFactory.java rename to app/src/main/java/org/zoolu/sip/message/BaseMessageFactory.java index ef44af9..4faf50c 100644 --- a/src/org/zoolu/sip/message/BaseMessageFactory.java +++ b/app/src/main/java/org/zoolu/sip/message/BaseMessageFactory.java @@ -1,513 +1,513 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.message; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.header.*; -import org.zoolu.sip.dialog.Dialog; -import org.zoolu.sip.provider.SipStack; -import org.zoolu.sip.provider.SipProvider; -import org.zoolu.sip.message.Message; -import org.zoolu.sip.message.SipMethods; - -import java.util.Enumeration; -import java.util.Vector; - -/** - * BaseMessageFactory is used to create SIP messages, requests and responses by - * means of two static methods: createRequest(), createResponse().
- * A valid SIP request sent by a UAC MUST, at least, contain the following - * header fields: To, From, CSeq, Call-ID, Max-Forwards, and Via; all of these - * header fields are mandatory in all SIP requests. These sip header fields are - * the fundamental building blocks of a SIP message, as they jointly provide for - * most of the critical message routing services including the addressing of - * messages, the routing of responses, limiting message propagation, ordering of - * messages, and the unique identification of transactions. These header fields - * are in addition to the mandatory request line, which contains the method, - * Request-URI, and SIP version. - */ -public abstract class BaseMessageFactory { - - /** - * Creates a SIP request message. - * - * @param method - * method name - * @param request_uri - * request-uri - * @param to - * ToHeader NameAddress - * @param from - * FromHeader NameAddress - * @param contact - * Contact NameAddress (if null, no ContactHeader is added) - * @param host_addr - * Via address - * @param host_port - * Via port number - * @param call_id - * Call-ID value - * @param cseq - * CSeq value - * @param local_tag - * tag in FromHeader - * @param remote_tag - * tag in ToHeader (if null, no tag is added) - * @param branch - * branch value (if null, a random value is picked) - * @param body - * body (if null, no body is added) - * @param qvalue - * Q value (if ICSI is null, no Q value is added) - * @param icsi - * ICSI (if null, no ICSI is added) - */ - public static Message createRequest(String method, SipURL request_uri, - NameAddress to, NameAddress from, NameAddress contact, - String proto, String via_addr, int host_port, boolean rport, - String call_id, long cseq, String local_tag, String remote_tag, - String branch, String body, String qvalue, String icsi) { // modified by mandrajg - Message req = new Message(); - // mandatory headers first (To, From, Via, Max-Forwards, Call-ID, CSeq): - req.setRequestLine(new RequestLine(method, request_uri)); - ViaHeader via = new ViaHeader(proto, via_addr, host_port); - if (rport) - via.setRport(); - if (branch == null) - branch = SipProvider.pickBranch(); - via.setBranch(branch); - req.addViaHeader(via); - req.setMaxForwardsHeader(new MaxForwardsHeader(70)); - if (remote_tag == null) - req.setToHeader(new ToHeader(to)); - else - req.setToHeader(new ToHeader(to, remote_tag)); - req.setFromHeader(new FromHeader(from, local_tag)); - req.setCallIdHeader(new CallIdHeader(call_id)); - req.setCSeqHeader(new CSeqHeader(cseq, method)); - // optional headers: - // start modification by mandrajg - if (contact != null) { - if (((method == "REGISTER")||(method == "INVITE")) && (icsi != null) ){ - MultipleHeader contacts = new MultipleHeader(SipHeaders.Contact); - contacts.addBottom(new ContactHeader(contact, qvalue, icsi)); - req.setContacts(contacts); - } - else{ - MultipleHeader contacts = new MultipleHeader(SipHeaders.Contact); - contacts.addBottom(new ContactHeader(contact)); - req.setContacts(contacts); - } - // System.out.println("DEBUG: Contact: "+contact.toString()); - } - if ((method == "INVITE") && (icsi != null) ){ - req.setAcceptContactHeader(new AcceptContactHeader(icsi)); - } - // end modifications by mandrajg - req.setExpiresHeader(new ExpiresHeader(String - .valueOf(SipStack.default_expires))); - // add User-Agent header field - if (SipStack.ua_info != null) - req.setUserAgentHeader(new UserAgentHeader(SipStack.ua_info)); - // if (body!=null) req.setBody(body); else req.setBody(""); - req.setBody(body); - // System.out.println("DEBUG: MessageFactory: request:\n"+req); - return req; - } - - /** - * Creates a SIP request message. Where - *

    - *
  • via address and port are taken from SipProvider - *
  • transport protocol is taken from request-uri (if transport parameter - * is present) or the default transport for the SipProvider is used. - *
- * - * @param sip_provider - * the SipProvider used to fill the Via field - * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,String,long,String,String,String,String) - */ - public static Message createRequest(SipProvider sip_provider, - String method, SipURL request_uri, NameAddress to, - NameAddress from, NameAddress contact, String call_id, long cseq, - String local_tag, String remote_tag, String branch, String body, String icsi) { // modified by mandrajg - String via_addr = sip_provider.getViaAddress(); - int host_port = sip_provider.getPort(); - boolean rport = sip_provider.isRportSet(); - String proto; - if (request_uri.hasTransport()) - proto = request_uri.getTransport(); - else - proto = sip_provider.getDefaultTransport(); - - return createRequest(method, request_uri, to, from, contact, proto, - via_addr, host_port, rport, call_id, cseq, local_tag, - remote_tag, branch, body, null, icsi); // modified by mandrajg - } - - /** - * Creates a SIP request message. Where - *
    - *
  • request-uri equals the To sip url - *
  • via address and port are taken from SipProvider - *
  • transport protocol is taken from request-uri (if transport parameter - * is present) or the default transport for the SipProvider is used. - *
  • call_id is picked random - *
  • cseq is picked random - *
  • local_tag is picked random - *
  • branch is picked random - *
- * - * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,String,long,String,String,String,String) - */ - public static Message createRequest(SipProvider sip_provider, - String method, SipURL request_uri, NameAddress to, - NameAddress from, NameAddress contact, String body) { // SipURL - // request_uri=to.getAddress(); - String call_id = sip_provider.pickCallId(); - int cseq = SipProvider.pickInitialCSeq(); - String local_tag = SipProvider.pickTag(); - // String branch=SipStack.pickBranch(); - return createRequest(sip_provider, method, request_uri, to, from, - contact, call_id, cseq, local_tag, null, null, body, null); // modified by mandrajg - } - - /** - * Creates a SIP request message. Where - *
    - *
  • request-uri equals the To sip url - *
  • via address and port are taken from SipProvider - *
  • transport protocol is taken from request-uri (if transport parameter - * is present) or the default transport for the SipProvider is used. - *
  • contact is formed by the 'From' user-name and by the address and - * port taken from SipProvider - *
  • call_id is picked random - *
  • cseq is picked random - *
  • local_tag is picked random - *
  • branch is picked random - *
- * - * @see #createRequest(SipProvider,String,NameAddress,NameAddress,NameAddress,String) - */ - public static Message createRequest(SipProvider sip_provider, - String method, NameAddress to, NameAddress from, String body) { - String contact_user = from.getAddress().getUserName(); - NameAddress contact = new NameAddress(new SipURL(contact_user, - sip_provider.getViaAddress(), sip_provider.getPort())); - return createRequest(sip_provider, method, to.getAddress(), to, from, - contact, body); - } - - /** - * Creates a SIP request message within a dialog, with a new branch - * via-parameter. - * - * @param dialog - * the Dialog used to compose the various Message headers - * @param method - * the request method - * @param body - * the message body - */ - public static Message createRequest(Dialog dialog, String method, - String body) { - NameAddress to = dialog.getRemoteName(); - NameAddress from = dialog.getLocalName(); - NameAddress target = dialog.getRemoteContact(); - if (target == null) - target = to; - SipURL request_uri = target.getAddress(); - if (request_uri == null) - request_uri = dialog.getRemoteName().getAddress(); - SipProvider sip_provider = dialog.getSipProvider(); - String via_addr = sip_provider.getViaAddress(); - int host_port = sip_provider.getPort(); - boolean rport = sip_provider.isRportSet(); - String proto; - if (target.getAddress().hasTransport()) - proto = target.getAddress().getTransport(); - else - proto = sip_provider.getDefaultTransport(); - NameAddress contact = dialog.getLocalContact(); - if (contact == null) - contact = from; - // increment the CSeq, if method is not ACK nor CANCEL - if (!SipMethods.isAck(method) && !SipMethods.isCancel(method)) - dialog.incLocalCSeq(); - String call_id = dialog.getCallID(); - long cseq = dialog.getLocalCSeq(); - String local_tag = dialog.getLocalTag(); - String remote_tag = dialog.getRemoteTag(); - // String branch=SipStack.pickBranch(); - Message req = createRequest(method, request_uri, to, from, contact, - proto, via_addr, host_port, rport, call_id, cseq, local_tag, - remote_tag, null, body, null, null); // modified by mandrajg - Vector route = dialog.getRoute(); - if (route != null && route.size() > 0) { - Vector route_s = new Vector(route.size()); - for (Enumeration e = route.elements(); - e.hasMoreElements(); ) { - NameAddress elem = e.nextElement(); - route_s.add(elem.toString()); - } - req.addRoutes(new MultipleHeader(SipHeaders.Route, route_s)); - } - req.rfc2543RouteAdapt(); - return req; - } - - /** - * Creates a new INVITE request out of any pre-existing dialogs. - * - * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,boolean,String,long,String,String,String,String) - */ - public static Message createInviteRequest(SipProvider sip_provider, - SipURL request_uri, NameAddress to, NameAddress from, - NameAddress contact, String body, String icsi) { // modified by mandrajg - String call_id = sip_provider.pickCallId(); - int cseq = SipProvider.pickInitialCSeq(); - String local_tag = SipProvider.pickTag(); - // String branch=SipStack.pickBranch(); - if (contact == null) - contact = from; - return createRequest(sip_provider, SipMethods.INVITE, request_uri, to, - from, contact, call_id, cseq, local_tag, null, null, body, icsi); // modified by mandrajg - } - - /** - * Creates a new INVITE request within a dialog (re-invite). - * - * @see #createRequest(Dialog,String,String) - */ - public static Message createInviteRequest(Dialog dialog, String body) { - return createRequest(dialog, SipMethods.INVITE, body); - } - - /** - * Creates an ACK request for a 2xx response. - * - * @see #createRequest(Dialog,String,String) - */ - public static Message create2xxAckRequest(Dialog dialog, String body) { - return createRequest(dialog, SipMethods.ACK, body); - } - - /** Creates an ACK request for a non-2xx response */ - public static Message createNon2xxAckRequest(SipProvider sip_provider, - Message method, Message resp) { - SipURL request_uri = method.getRequestLine().getAddress(); - FromHeader from = method.getFromHeader(); - ToHeader to = resp.getToHeader(); - String via_addr = sip_provider.getViaAddress(); - int host_port = sip_provider.getPort(); - boolean rport = sip_provider.isRportSet(); - String proto; - if (request_uri.hasTransport()) - proto = request_uri.getTransport(); - else - proto = sip_provider.getDefaultTransport(); - String branch = method.getViaHeader().getBranch(); - NameAddress contact = null; - Message ack = createRequest(SipMethods.ACK, request_uri, to - .getNameAddress(), from.getNameAddress(), contact, proto, - via_addr, host_port, rport, method.getCallIdHeader() - .getCallId(), method.getCSeqHeader() - .getSequenceNumber(), from.getParameter("tag"), to - .getParameter("tag"), branch, null, null, null); // modified by mandrajg - ack.removeExpiresHeader(); - if (method.hasRouteHeader()) - ack.setRoutes(method.getRoutes()); - return ack; - } - - /** - * Creates an ACK request for a 2xx-response. Contact value is taken from - * SipStack - */ - /* - * public static Message create2xxAckRequest(Message resp, String body) { - * ToHeader to=resp.getToHeader(); FromHeader from=resp.getFromHeader(); int - * code=resp.getStatusLine().getCode(); SipURL request_uri; - * request_uri=resp.getContactHeader().getNameAddress().getAddress(); if - * (request_uri==null) request_uri=to.getNameAddress().getAddress(); String - * branch=SipStack.pickBranch(); NameAddress contact=null; if - * (SipStack.contact_url!=null) contact=new - * NameAddress(SipStack.contact_url); return - * createRequest(SipMethods.ACK,request_uri,to.getNameAddress(),from.getNameAddress(),contact,resp.getCallIdHeader().getCallId(),resp.getCSeqHeader().getSequenceNumber(),from.getParameter("tag"),to.getParameter("tag"),branch,body); } - */ - - /** Creates an ACK request for a 2xx-response within a dialog */ - /* - * public static Message create2xxAckRequest(Dialog dialog, NameAddress - * contact, String body) { return - * createRequest(SipMethods.ACK,dialog,contact,body); } - */ - - /** Creates an ACK request for a 2xx-response within a dialog */ - /* - * public static Message create2xxAckRequest(Dialog dialog, String body) { - * return createRequest(SipMethods.ACK,dialog,body); } - */ - - /** Creates a CANCEL request. */ - public static Message createCancelRequest(Message method,Dialog dialog) { - ToHeader to = method.getToHeader(); - FromHeader from = method.getFromHeader(); - SipURL request_uri = method.getRequestLine().getAddress(); - NameAddress contact = method.getContactHeader().getNameAddress(); - ViaHeader via = method.getViaHeader(); - String host_addr = via.getHost(); - int host_port = via.getPort(); - boolean rport = via.hasRport(); - String proto = via.getProtocol(); - String branch = method.getViaHeader().getBranch(); - return createRequest(SipMethods.CANCEL, request_uri, to - .getNameAddress(), from.getNameAddress(), contact, proto, - host_addr, host_port, rport, method.getCallIdHeader() - .getCallId(), method.getCSeqHeader() - .getSequenceNumber(), from.getParameter("tag"), to - .getParameter("tag"), branch, "", null, null); // modified by mandrajg - } - - /** Creates a BYE request. */ - public static Message createByeRequest(Dialog dialog) { - Message msg = createRequest(dialog, SipMethods.BYE, null); - msg.removeExpiresHeader(); - msg.removeContacts(); - return msg; - } - - /** - * Creates a new REGISTER request. - *

- * If contact is null, set contact as star * (register all) - */ - public static Message createRegisterRequest(SipProvider sip_provider, - NameAddress to, NameAddress from, NameAddress contact, String qvalue, String icsi) { // modified by mandrajg - SipURL to_url = to.getAddress(); - SipURL registrar = new SipURL(to_url.getHost(), to_url.getPort()); - String via_addr = sip_provider.getViaAddress(); - int host_port = sip_provider.getPort(); - boolean rport = sip_provider.isRportSet(); - String proto; - if (to_url.hasTransport()) - proto = to_url.getTransport(); - else - proto = sip_provider.getDefaultTransport(); - String call_id = sip_provider.pickCallId(); - int cseq = SipProvider.pickInitialCSeq(); - String local_tag = SipProvider.pickTag(); - // String branch=SipStack.pickBranch(); - Message req = createRequest(SipMethods.REGISTER, registrar, to, from, - contact, proto, via_addr, host_port, rport, call_id, cseq, - local_tag, null, null, null, qvalue, icsi); // modified by mandrajg - // if no contact, deregister all - if (contact == null) { - ContactHeader star = new ContactHeader(); // contact is * - req.setContactHeader(star); - req.setExpiresHeader(new ExpiresHeader(String - .valueOf(SipStack.default_expires))); - } - return req; - } - - // ################ Can be removed? ################ - /** - * Creates a new REGISTER request. - *

- * If contact is null, set contact as star * (register all) - */ - /* - * public static Message createRegisterRequest(SipProvider sip_provider, - * NameAddress to, NameAddress contact) { return - * createRegisterRequest(sip_provider,to,to,contact); } - */ - - /** - * Creates a SIP response message. - * - * @param req - * the request message - * @param code - * the response code - * @param reason - * the response reason - * @param contact - * the contact address - * @param local_tag - * the local tag in the 'To' header - * @param body - * the message body - */ - public static Message createResponse(Message req, int code, String reason, - String local_tag, NameAddress contact, String content_type, - String body) { - Message resp = new Message(); - resp.setStatusLine(new StatusLine(code, reason)); - resp.setVias(req.getVias()); - if (code >= 180 && code < 300 && req.hasRecordRouteHeader()) - resp.setRecordRoutes(req.getRecordRoutes()); - ToHeader toh = req.getToHeader(); - if (local_tag != null) - toh.setParameter("tag", local_tag); - resp.setToHeader(toh); - resp.setFromHeader(req.getFromHeader()); - resp.setCallIdHeader(req.getCallIdHeader()); - resp.setCSeqHeader(req.getCSeqHeader()); - if (contact != null) - resp.setContactHeader(new ContactHeader(contact)); - // add Server header field - if (SipStack.server_info != null) - resp.setServerHeader(new ServerHeader(SipStack.server_info)); - // if (body!=null) resp.setBody(body); else resp.setBody(""); - if (content_type == null) - resp.setBody(body); - else - resp.setBody(content_type, body); - // System.out.println("DEBUG: MessageFactory: - // response:\n"+resp.toString()); - return resp; - } - - /** - * Creates a SIP response message. For 2xx responses generates the local tag - * by means of the SipStack.pickTag(req) method. - * - * @see #createResponse(Message,int,String,NameAddress,String,String body) - */ - public static Message createResponse(Message req, int code, String reason, - NameAddress contact) { // String - // reason=SipResponses.reasonOf(code); - String localtag = null; - if (req.createsDialog() && !req.getToHeader().hasTag()) { - //fix issue 425 - also add tag to 18x responses - if (SipStack.early_dialog || (code >= 101 && code < 300)) - localtag = SipProvider.pickTag(req); - } - return createResponse(req, code, reason, localtag, contact, null, null); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.message; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.header.*; +import org.zoolu.sip.dialog.Dialog; +import org.zoolu.sip.provider.SipStack; +import org.zoolu.sip.provider.SipProvider; +import org.zoolu.sip.message.Message; +import org.zoolu.sip.message.SipMethods; + +import java.util.Enumeration; +import java.util.Vector; + +/** + * BaseMessageFactory is used to create SIP messages, requests and responses by + * means of two static methods: createRequest(), createResponse().
+ * A valid SIP request sent by a UAC MUST, at least, contain the following + * header fields: To, From, CSeq, Call-ID, Max-Forwards, and Via; all of these + * header fields are mandatory in all SIP requests. These sip header fields are + * the fundamental building blocks of a SIP message, as they jointly provide for + * most of the critical message routing services including the addressing of + * messages, the routing of responses, limiting message propagation, ordering of + * messages, and the unique identification of transactions. These header fields + * are in addition to the mandatory request line, which contains the method, + * Request-URI, and SIP version. + */ +public abstract class BaseMessageFactory { + + /** + * Creates a SIP request message. + * + * @param method + * method name + * @param request_uri + * request-uri + * @param to + * ToHeader NameAddress + * @param from + * FromHeader NameAddress + * @param contact + * Contact NameAddress (if null, no ContactHeader is added) + * @param host_addr + * Via address + * @param host_port + * Via port number + * @param call_id + * Call-ID value + * @param cseq + * CSeq value + * @param local_tag + * tag in FromHeader + * @param remote_tag + * tag in ToHeader (if null, no tag is added) + * @param branch + * branch value (if null, a random value is picked) + * @param body + * body (if null, no body is added) + * @param qvalue + * Q value (if ICSI is null, no Q value is added) + * @param icsi + * ICSI (if null, no ICSI is added) + */ + public static Message createRequest(String method, SipURL request_uri, + NameAddress to, NameAddress from, NameAddress contact, + String proto, String via_addr, int host_port, boolean rport, + String call_id, long cseq, String local_tag, String remote_tag, + String branch, String body, String qvalue, String icsi) { // modified by mandrajg + Message req = new Message(); + // mandatory headers first (To, From, Via, Max-Forwards, Call-ID, CSeq): + req.setRequestLine(new RequestLine(method, request_uri)); + ViaHeader via = new ViaHeader(proto, via_addr, host_port); + if (rport) + via.setRport(); + if (branch == null) + branch = SipProvider.pickBranch(); + via.setBranch(branch); + req.addViaHeader(via); + req.setMaxForwardsHeader(new MaxForwardsHeader(70)); + if (remote_tag == null) + req.setToHeader(new ToHeader(to)); + else + req.setToHeader(new ToHeader(to, remote_tag)); + req.setFromHeader(new FromHeader(from, local_tag)); + req.setCallIdHeader(new CallIdHeader(call_id)); + req.setCSeqHeader(new CSeqHeader(cseq, method)); + // optional headers: + // start modification by mandrajg + if (contact != null) { + if (((method == "REGISTER")||(method == "INVITE")) && (icsi != null) ){ + MultipleHeader contacts = new MultipleHeader(SipHeaders.Contact); + contacts.addBottom(new ContactHeader(contact, qvalue, icsi)); + req.setContacts(contacts); + } + else{ + MultipleHeader contacts = new MultipleHeader(SipHeaders.Contact); + contacts.addBottom(new ContactHeader(contact)); + req.setContacts(contacts); + } + // System.out.println("DEBUG: Contact: "+contact.toString()); + } + if ((method == "INVITE") && (icsi != null) ){ + req.setAcceptContactHeader(new AcceptContactHeader(icsi)); + } + // end modifications by mandrajg + req.setExpiresHeader(new ExpiresHeader(String + .valueOf(SipStack.default_expires))); + // add User-Agent header field + if (SipStack.ua_info != null) + req.setUserAgentHeader(new UserAgentHeader(SipStack.ua_info)); + // if (body!=null) req.setBody(body); else req.setBody(""); + req.setBody(body); + // System.out.println("DEBUG: MessageFactory: request:\n"+req); + return req; + } + + /** + * Creates a SIP request message. Where + *

    + *
  • via address and port are taken from SipProvider + *
  • transport protocol is taken from request-uri (if transport parameter + * is present) or the default transport for the SipProvider is used. + *
+ * + * @param sip_provider + * the SipProvider used to fill the Via field + * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,String,long,String,String,String,String) + */ + public static Message createRequest(SipProvider sip_provider, + String method, SipURL request_uri, NameAddress to, + NameAddress from, NameAddress contact, String call_id, long cseq, + String local_tag, String remote_tag, String branch, String body, String icsi) { // modified by mandrajg + String via_addr = sip_provider.getViaAddress(); + int host_port = sip_provider.getPort(); + boolean rport = sip_provider.isRportSet(); + String proto; + if (request_uri.hasTransport()) + proto = request_uri.getTransport(); + else + proto = sip_provider.getDefaultTransport(); + + return createRequest(method, request_uri, to, from, contact, proto, + via_addr, host_port, rport, call_id, cseq, local_tag, + remote_tag, branch, body, null, icsi); // modified by mandrajg + } + + /** + * Creates a SIP request message. Where + *
    + *
  • request-uri equals the To sip url + *
  • via address and port are taken from SipProvider + *
  • transport protocol is taken from request-uri (if transport parameter + * is present) or the default transport for the SipProvider is used. + *
  • call_id is picked random + *
  • cseq is picked random + *
  • local_tag is picked random + *
  • branch is picked random + *
+ * + * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,String,long,String,String,String,String) + */ + public static Message createRequest(SipProvider sip_provider, + String method, SipURL request_uri, NameAddress to, + NameAddress from, NameAddress contact, String body) { // SipURL + // request_uri=to.getAddress(); + String call_id = sip_provider.pickCallId(); + int cseq = SipProvider.pickInitialCSeq(); + String local_tag = SipProvider.pickTag(); + // String branch=SipStack.pickBranch(); + return createRequest(sip_provider, method, request_uri, to, from, + contact, call_id, cseq, local_tag, null, null, body, null); // modified by mandrajg + } + + /** + * Creates a SIP request message. Where + *
    + *
  • request-uri equals the To sip url + *
  • via address and port are taken from SipProvider + *
  • transport protocol is taken from request-uri (if transport parameter + * is present) or the default transport for the SipProvider is used. + *
  • contact is formed by the 'From' user-name and by the address and + * port taken from SipProvider + *
  • call_id is picked random + *
  • cseq is picked random + *
  • local_tag is picked random + *
  • branch is picked random + *
+ * + * @see #createRequest(SipProvider,String,NameAddress,NameAddress,NameAddress,String) + */ + public static Message createRequest(SipProvider sip_provider, + String method, NameAddress to, NameAddress from, String body) { + String contact_user = from.getAddress().getUserName(); + NameAddress contact = new NameAddress(new SipURL(contact_user, + sip_provider.getViaAddress(), sip_provider.getPort())); + return createRequest(sip_provider, method, to.getAddress(), to, from, + contact, body); + } + + /** + * Creates a SIP request message within a dialog, with a new branch + * via-parameter. + * + * @param dialog + * the Dialog used to compose the various Message headers + * @param method + * the request method + * @param body + * the message body + */ + public static Message createRequest(Dialog dialog, String method, + String body) { + NameAddress to = dialog.getRemoteName(); + NameAddress from = dialog.getLocalName(); + NameAddress target = dialog.getRemoteContact(); + if (target == null) + target = to; + SipURL request_uri = target.getAddress(); + if (request_uri == null) + request_uri = dialog.getRemoteName().getAddress(); + SipProvider sip_provider = dialog.getSipProvider(); + String via_addr = sip_provider.getViaAddress(); + int host_port = sip_provider.getPort(); + boolean rport = sip_provider.isRportSet(); + String proto; + if (target.getAddress().hasTransport()) + proto = target.getAddress().getTransport(); + else + proto = sip_provider.getDefaultTransport(); + NameAddress contact = dialog.getLocalContact(); + if (contact == null) + contact = from; + // increment the CSeq, if method is not ACK nor CANCEL + if (!SipMethods.isAck(method) && !SipMethods.isCancel(method)) + dialog.incLocalCSeq(); + String call_id = dialog.getCallID(); + long cseq = dialog.getLocalCSeq(); + String local_tag = dialog.getLocalTag(); + String remote_tag = dialog.getRemoteTag(); + // String branch=SipStack.pickBranch(); + Message req = createRequest(method, request_uri, to, from, contact, + proto, via_addr, host_port, rport, call_id, cseq, local_tag, + remote_tag, null, body, null, null); // modified by mandrajg + Vector route = dialog.getRoute(); + if (route != null && route.size() > 0) { + Vector route_s = new Vector(route.size()); + for (Enumeration e = route.elements(); + e.hasMoreElements(); ) { + NameAddress elem = e.nextElement(); + route_s.add(elem.toString()); + } + req.addRoutes(new MultipleHeader(SipHeaders.Route, route_s)); + } + req.rfc2543RouteAdapt(); + return req; + } + + /** + * Creates a new INVITE request out of any pre-existing dialogs. + * + * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,boolean,String,long,String,String,String,String) + */ + public static Message createInviteRequest(SipProvider sip_provider, + SipURL request_uri, NameAddress to, NameAddress from, + NameAddress contact, String body, String icsi) { // modified by mandrajg + String call_id = sip_provider.pickCallId(); + int cseq = SipProvider.pickInitialCSeq(); + String local_tag = SipProvider.pickTag(); + // String branch=SipStack.pickBranch(); + if (contact == null) + contact = from; + return createRequest(sip_provider, SipMethods.INVITE, request_uri, to, + from, contact, call_id, cseq, local_tag, null, null, body, icsi); // modified by mandrajg + } + + /** + * Creates a new INVITE request within a dialog (re-invite). + * + * @see #createRequest(Dialog,String,String) + */ + public static Message createInviteRequest(Dialog dialog, String body) { + return createRequest(dialog, SipMethods.INVITE, body); + } + + /** + * Creates an ACK request for a 2xx response. + * + * @see #createRequest(Dialog,String,String) + */ + public static Message create2xxAckRequest(Dialog dialog, String body) { + return createRequest(dialog, SipMethods.ACK, body); + } + + /** Creates an ACK request for a non-2xx response */ + public static Message createNon2xxAckRequest(SipProvider sip_provider, + Message method, Message resp) { + SipURL request_uri = method.getRequestLine().getAddress(); + FromHeader from = method.getFromHeader(); + ToHeader to = resp.getToHeader(); + String via_addr = sip_provider.getViaAddress(); + int host_port = sip_provider.getPort(); + boolean rport = sip_provider.isRportSet(); + String proto; + if (request_uri.hasTransport()) + proto = request_uri.getTransport(); + else + proto = sip_provider.getDefaultTransport(); + String branch = method.getViaHeader().getBranch(); + NameAddress contact = null; + Message ack = createRequest(SipMethods.ACK, request_uri, to + .getNameAddress(), from.getNameAddress(), contact, proto, + via_addr, host_port, rport, method.getCallIdHeader() + .getCallId(), method.getCSeqHeader() + .getSequenceNumber(), from.getParameter("tag"), to + .getParameter("tag"), branch, null, null, null); // modified by mandrajg + ack.removeExpiresHeader(); + if (method.hasRouteHeader()) + ack.setRoutes(method.getRoutes()); + return ack; + } + + /** + * Creates an ACK request for a 2xx-response. Contact value is taken from + * SipStack + */ + /* + * public static Message create2xxAckRequest(Message resp, String body) { + * ToHeader to=resp.getToHeader(); FromHeader from=resp.getFromHeader(); int + * code=resp.getStatusLine().getCode(); SipURL request_uri; + * request_uri=resp.getContactHeader().getNameAddress().getAddress(); if + * (request_uri==null) request_uri=to.getNameAddress().getAddress(); String + * branch=SipStack.pickBranch(); NameAddress contact=null; if + * (SipStack.contact_url!=null) contact=new + * NameAddress(SipStack.contact_url); return + * createRequest(SipMethods.ACK,request_uri,to.getNameAddress(),from.getNameAddress(),contact,resp.getCallIdHeader().getCallId(),resp.getCSeqHeader().getSequenceNumber(),from.getParameter("tag"),to.getParameter("tag"),branch,body); } + */ + + /** Creates an ACK request for a 2xx-response within a dialog */ + /* + * public static Message create2xxAckRequest(Dialog dialog, NameAddress + * contact, String body) { return + * createRequest(SipMethods.ACK,dialog,contact,body); } + */ + + /** Creates an ACK request for a 2xx-response within a dialog */ + /* + * public static Message create2xxAckRequest(Dialog dialog, String body) { + * return createRequest(SipMethods.ACK,dialog,body); } + */ + + /** Creates a CANCEL request. */ + public static Message createCancelRequest(Message method,Dialog dialog) { + ToHeader to = method.getToHeader(); + FromHeader from = method.getFromHeader(); + SipURL request_uri = method.getRequestLine().getAddress(); + NameAddress contact = method.getContactHeader().getNameAddress(); + ViaHeader via = method.getViaHeader(); + String host_addr = via.getHost(); + int host_port = via.getPort(); + boolean rport = via.hasRport(); + String proto = via.getProtocol(); + String branch = method.getViaHeader().getBranch(); + return createRequest(SipMethods.CANCEL, request_uri, to + .getNameAddress(), from.getNameAddress(), contact, proto, + host_addr, host_port, rport, method.getCallIdHeader() + .getCallId(), method.getCSeqHeader() + .getSequenceNumber(), from.getParameter("tag"), to + .getParameter("tag"), branch, "", null, null); // modified by mandrajg + } + + /** Creates a BYE request. */ + public static Message createByeRequest(Dialog dialog) { + Message msg = createRequest(dialog, SipMethods.BYE, null); + msg.removeExpiresHeader(); + msg.removeContacts(); + return msg; + } + + /** + * Creates a new REGISTER request. + *

+ * If contact is null, set contact as star * (register all) + */ + public static Message createRegisterRequest(SipProvider sip_provider, + NameAddress to, NameAddress from, NameAddress contact, String qvalue, String icsi) { // modified by mandrajg + SipURL to_url = to.getAddress(); + SipURL registrar = new SipURL(to_url.getHost(), to_url.getPort()); + String via_addr = sip_provider.getViaAddress(); + int host_port = sip_provider.getPort(); + boolean rport = sip_provider.isRportSet(); + String proto; + if (to_url.hasTransport()) + proto = to_url.getTransport(); + else + proto = sip_provider.getDefaultTransport(); + String call_id = sip_provider.pickCallId(); + int cseq = SipProvider.pickInitialCSeq(); + String local_tag = SipProvider.pickTag(); + // String branch=SipStack.pickBranch(); + Message req = createRequest(SipMethods.REGISTER, registrar, to, from, + contact, proto, via_addr, host_port, rport, call_id, cseq, + local_tag, null, null, null, qvalue, icsi); // modified by mandrajg + // if no contact, deregister all + if (contact == null) { + ContactHeader star = new ContactHeader(); // contact is * + req.setContactHeader(star); + req.setExpiresHeader(new ExpiresHeader(String + .valueOf(SipStack.default_expires))); + } + return req; + } + + // ################ Can be removed? ################ + /** + * Creates a new REGISTER request. + *

+ * If contact is null, set contact as star * (register all) + */ + /* + * public static Message createRegisterRequest(SipProvider sip_provider, + * NameAddress to, NameAddress contact) { return + * createRegisterRequest(sip_provider,to,to,contact); } + */ + + /** + * Creates a SIP response message. + * + * @param req + * the request message + * @param code + * the response code + * @param reason + * the response reason + * @param contact + * the contact address + * @param local_tag + * the local tag in the 'To' header + * @param body + * the message body + */ + public static Message createResponse(Message req, int code, String reason, + String local_tag, NameAddress contact, String content_type, + String body) { + Message resp = new Message(); + resp.setStatusLine(new StatusLine(code, reason)); + resp.setVias(req.getVias()); + if (code >= 180 && code < 300 && req.hasRecordRouteHeader()) + resp.setRecordRoutes(req.getRecordRoutes()); + ToHeader toh = req.getToHeader(); + if (local_tag != null) + toh.setParameter("tag", local_tag); + resp.setToHeader(toh); + resp.setFromHeader(req.getFromHeader()); + resp.setCallIdHeader(req.getCallIdHeader()); + resp.setCSeqHeader(req.getCSeqHeader()); + if (contact != null) + resp.setContactHeader(new ContactHeader(contact)); + // add Server header field + if (SipStack.server_info != null) + resp.setServerHeader(new ServerHeader(SipStack.server_info)); + // if (body!=null) resp.setBody(body); else resp.setBody(""); + if (content_type == null) + resp.setBody(body); + else + resp.setBody(content_type, body); + // System.out.println("DEBUG: MessageFactory: + // response:\n"+resp.toString()); + return resp; + } + + /** + * Creates a SIP response message. For 2xx responses generates the local tag + * by means of the SipStack.pickTag(req) method. + * + * @see #createResponse(Message,int,String,NameAddress,String,String body) + */ + public static Message createResponse(Message req, int code, String reason, + NameAddress contact) { // String + // reason=SipResponses.reasonOf(code); + String localtag = null; + if (req.createsDialog() && !req.getToHeader().hasTag()) { + //fix issue 425 - also add tag to 18x responses + if (SipStack.early_dialog || (code >= 101 && code < 300)) + localtag = SipProvider.pickTag(req); + } + return createResponse(req, code, reason, localtag, contact, null, null); + } + +} diff --git a/src/org/zoolu/sip/message/BaseMessageOtp.java b/app/src/main/java/org/zoolu/sip/message/BaseMessageOtp.java similarity index 96% rename from src/org/zoolu/sip/message/BaseMessageOtp.java rename to app/src/main/java/org/zoolu/sip/message/BaseMessageOtp.java index 27e9ca4..cabe9f8 100644 --- a/src/org/zoolu/sip/message/BaseMessageOtp.java +++ b/app/src/main/java/org/zoolu/sip/message/BaseMessageOtp.java @@ -1,480 +1,480 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.message; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.header.*; -import org.zoolu.net.UdpPacket; -import java.util.*; - -/** - * Class BaseMessageOtp implements a generic SIP Message. It extends class - * BaseMessage adding one-time-parsing functionality (it parses the entire - * Message just when it is costructed).

At the contrary, class BaseMessage - * works in a just-in-time manner (it parses the message each time a particular - * header field is requested). - */ -public abstract class BaseMessageOtp extends BaseMessage { - - protected RequestLine request_line; - protected StatusLine status_line; - - protected Vector

headers; - protected String body; - - /** Inits empty Message */ - private void init() { - request_line = null; - status_line = null; - headers = null; - body = null; - } - - /** Costructs a new empty Message */ - public BaseMessageOtp() { - init(); - headers = new Vector
(); - } - - /** Costructs a new Message */ - public BaseMessageOtp(byte[] data, int offset, int len) { - init(); - parseIt(new String(data, offset, len)); - } - - /** Costructs a new Message */ - public BaseMessageOtp(UdpPacket packet) { - init(); - parseIt(new String(packet.getData(), packet.getOffset(), packet - .getLength())); - } - - /** Costructs a new Message */ - public BaseMessageOtp(String str) { - init(); - parseIt(str); - } - - /** Costructs a new Message */ - public BaseMessageOtp(BaseMessageOtp msg) { - init(); - remote_addr = msg.remote_addr; - remote_port = msg.remote_port; - transport_proto = msg.transport_proto; - connection_id = msg.connection_id; - // packet_length=msg.packet_length; - request_line = msg.request_line; - status_line = msg.status_line; - headers = new Vector
(); - for (int i = 0; i < msg.headers.size(); i++) - headers.addElement(msg.headers.elementAt(i)); - body = msg.body; - } - - /** Sets the entire message */ - public void setMessage(String str) { - parseIt(str); - } - - /** Parses the Message from a String. */ - private void parseIt(String str) { - SipParser par = new SipParser(str); - String version = str.substring(0, 4); - if (version.equalsIgnoreCase("SIP/")) - status_line = par.getStatusLine(); - else - request_line = par.getRequestLine(); - - headers = new Vector
(); - Header h = par.getHeader(); - while (h != null) { - headers.addElement(h); - h = par.getHeader(); - } - ContentLengthHeader clh = getContentLengthHeader(); - if (clh != null) { - int len = clh.getContentLength(); - body = par.getString(len); - } else if (getContentTypeHeader() != null) { - body = par.getRemainingString(); - if (body.length() == 0) - body = null; - } - } - - /** Gets string representation of Message */ - public String toString() { - StringBuffer str = new StringBuffer(); - if (request_line != null) - str.append(request_line.toString()); - else if (status_line != null) - str.append(status_line.toString()); - for (int i = 0; i < headers.size(); i++) - str.append(((Header) headers.elementAt(i)).toString()); - str.append("\r\n"); - if (body != null) - str.append(body); - return str.toString(); - } - - /** Gets message length */ - public int getLength() { - return toString().length(); - } - - // **************************** Requests ****************************/ - - /** Whether Message is a Request */ - public boolean isRequest() { - if (request_line != null) - return true; - else - return false; - } - - /** Whether Message is a method request */ - public boolean isRequest(String method) { - if (request_line != null - && request_line.getMethod().equalsIgnoreCase(method)) - return true; - else - return false; - } - - /** Whether Message has Request-line */ - protected boolean hasRequestLine() { - return request_line != null; - } - - /** - * Gets RequestLine in Message (Returns null if called for no request - * message) - */ - public RequestLine getRequestLine() { - return request_line; - } - - /** Sets RequestLine of the Message */ - public void setRequestLine(RequestLine rl) { - request_line = rl; - } - - /** Removes RequestLine of the Message */ - public void removeRequestLine() { - request_line = null; - } - - // **************************** Responses ****************************/ - - /** Whether Message is a Response */ - public boolean isResponse() throws NullPointerException { - if (status_line != null) - return true; - else - return false; - } - - /** Whether Message has Status-line */ - protected boolean hasStatusLine() { - return status_line != null; - } - - /** - * Gets StautsLine in Message (Returns null if called for no response - * message) - */ - public StatusLine getStatusLine() { - return status_line; - } - - /** Sets StatusLine of the Message */ - public void setStatusLine(StatusLine sl) { - status_line = sl; - } - - /** Removes StatusLine of the Message */ - public void removeStatusLine() { - status_line = null; - } - - // **************************** Generic Headers - // ****************************/ - - /** Removes Request\Status Line of the Message */ - protected void removeFirstLine() { - removeRequestLine(); - removeStatusLine(); - } - - /** Gets the position of header hname. */ - protected int indexOfHeader(String hname) { - for (int i = 0; i < headers.size(); i++) { - Header h = (Header) headers.elementAt(i); - if (hname.equalsIgnoreCase(h.getName())) - return i; - } - return -1; - } - - /** - * Gets the first Header of specified name (Returns null if no Header is - * found) - */ - public Header getHeader(String hname) { - int i = indexOfHeader(hname); - if (i < 0) - return null; - else - return (Header) headers.elementAt(i); - } - - /** - * Gets a Vector of all Headers of specified name (Returns empty Vector if - * no Header is found) - */ - public Vector
getHeaders(String hname) { - Vector
v = new Vector
(); - for (int i = 0; i < headers.size(); i++) { - Header h = (Header) headers.elementAt(i); - if (hname.equalsIgnoreCase(h.getName())) - v.addElement(h); - } - return v; - } - - /** - * Adds Header at the top/bottom. The bottom is considered before the - * Content-Length and Content-Type headers - */ - public void addHeader(Header header, boolean top) { - if (top) - headers.insertElementAt(header, 0); - else - headers.addElement(header); - } - - /** Adds a Vector of Headers at the top/bottom */ - public void addHeaders(Vector
headers, boolean top) { - for (int i = 0; i < headers.size(); i++) - if (top) - this.headers.insertElementAt(headers.elementAt(i), i); - else - this.headers.addElement(headers.elementAt(i)); - } - - /** Adds MultipleHeader(s) mheader at the top/bottom */ - public void addHeaders(MultipleHeader mheader, boolean top) { - if (mheader.isCommaSeparated()) - addHeader(mheader.toHeader(), top); - else - addHeaders(mheader.getHeaders(), top); - } - - /** - * Adds Header before the first header refer_hname . - *

- * If there is no header of such type, it is added at top - */ - public void addHeaderBefore(Header new_header, String refer_hname) { - int i = indexOfHeader(refer_hname); - if (i < 0) - i = 0; - headers.insertElementAt(new_header, i); - } - - /** - * Adds MultipleHeader(s) before the first header refer_hname . - *

- * If there is no header of such type, they are added at top - */ - public void addHeadersBefore(MultipleHeader mheader, String refer_hname) { - if (mheader.isCommaSeparated()) - addHeaderBefore(mheader.toHeader(), refer_hname); - else { - int index = indexOfHeader(refer_hname); - if (index < 0) - index = 0; - Vector

hs = mheader.getHeaders(); - for (int k = 0; k < hs.size(); k++) - headers.insertElementAt(hs.elementAt(k), index + k); - } - } - - /** - * Adds Header after the first header refer_hname . - *

- * If there is no header of such type, it is added at bottom - */ - public void addHeaderAfter(Header new_header, String refer_hname) { - int i = indexOfHeader(refer_hname); - if (i >= 0) - i++; - else - i = headers.size(); - headers.insertElementAt(new_header, i); - } - - /** - * Adds MultipleHeader(s) after the first header refer_hname . - *

- * If there is no header of such type, they are added at bottom - */ - public void addHeadersAfter(MultipleHeader mheader, String refer_hname) { - if (mheader.isCommaSeparated()) - addHeaderAfter(mheader.toHeader(), refer_hname); - else { - int index = indexOfHeader(refer_hname); - if (index >= 0) - index++; - else - index = headers.size(); - Vector

hs = mheader.getHeaders(); - for (int k = 0; k < hs.size(); k++) - headers.insertElementAt(hs.elementAt(k), index + k); - } - } - - /** Removes first Header of specified name */ - public void removeHeader(String hname) { - removeHeader(hname, true); - } - - /** Removes first (or last) Header of specified name. */ - public void removeHeader(String hname, boolean first) { - int index = -1; - for (int i = 0; i < headers.size(); i++) { - Header h = (Header) headers.elementAt(i); - if (hname.equalsIgnoreCase(h.getName())) { - index = i; - if (first) - i = headers.size(); - } - } - if (index >= 0) - headers.removeElementAt(index); - } - - /** Removes all Headers of specified name */ - public void removeAllHeaders(String hname) { - for (int i = 0; i < headers.size(); i++) { - Header h = (Header) headers.elementAt(i); - if (hname.equalsIgnoreCase(h.getName())) { - headers.removeElementAt(i); - i--; - } - } - } - - /** - * Sets the Header hd removing any previous headers of the same - * type. - */ - public void setHeader(Header hd) { - boolean first = true; - String hname = hd.getName(); - for (int i = 0; i < headers.size(); i++) { - Header h = (Header) headers.elementAt(i); - if (hname.equalsIgnoreCase(h.getName())) { - if (first) { // replace it - headers.setElementAt(h, i); - first = false; - } else { // remove it - headers.removeElementAt(i); - i--; - } - } - } - if (first) - headers.addElement(hd); - } - - /** Sets MultipleHeader mheader */ - public void setHeaders(MultipleHeader mheader) { - if (mheader.isCommaSeparated()) - setHeader(mheader.toHeader()); - else { - boolean first = true; - String hname = mheader.getName(); - for (int i = 0; i < headers.size(); i++) { - Header h = (Header) headers.elementAt(i); - if (hname.equalsIgnoreCase(h.getName())) { - if (first) { // replace it - Vector
hs = mheader.getHeaders(); - for (int k = 0; k < hs.size(); k++) - headers.insertElementAt(hs.elementAt(k), i + k); - first = false; - i += hs.size() - 1; - } else { // remove it - headers.removeElementAt(i); - i--; - } - } - } - } - } - - // **************************** Specific Headers - // ****************************/ - - /** Whether Message has Body */ - public boolean hasBody() { - return this.body != null; - } - - /** Gets body(content) type */ - public String getBodyType() { - return getContentTypeHeader().getContentType(); - } - - /** Sets the message body */ - public void setBody(String content_type, String body) { - removeBody(); - if (body != null && body.length() > 0) { - setContentTypeHeader(new ContentTypeHeader(content_type)); - setContentLengthHeader(new ContentLengthHeader(body.length())); - this.body = body; - } else { - setContentLengthHeader(new ContentLengthHeader(0)); - this.body = null; - } - } - - /** - * Gets message body. The end of body is evaluated from the Content-Length - * header if present (SIP-RFC compliant), or from the end of message if no - * Content-Length header is present (non-SIP-RFC compliant) - */ - public String getBody() { - return this.body; - } - - /** Removes the message body (if it exists) and the final empty line */ - public void removeBody() { - removeContentLengthHeader(); - removeContentTypeHeader(); - this.body = null; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.message; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.header.*; +import org.zoolu.net.UdpPacket; +import java.util.*; + +/** + * Class BaseMessageOtp implements a generic SIP Message. It extends class + * BaseMessage adding one-time-parsing functionality (it parses the entire + * Message just when it is costructed).

At the contrary, class BaseMessage + * works in a just-in-time manner (it parses the message each time a particular + * header field is requested). + */ +public abstract class BaseMessageOtp extends BaseMessage { + + protected RequestLine request_line; + protected StatusLine status_line; + + protected Vector

headers; + protected String body; + + /** Inits empty Message */ + private void init() { + request_line = null; + status_line = null; + headers = null; + body = null; + } + + /** Costructs a new empty Message */ + public BaseMessageOtp() { + init(); + headers = new Vector
(); + } + + /** Costructs a new Message */ + public BaseMessageOtp(byte[] data, int offset, int len) { + init(); + parseIt(new String(data, offset, len)); + } + + /** Costructs a new Message */ + public BaseMessageOtp(UdpPacket packet) { + init(); + parseIt(new String(packet.getData(), packet.getOffset(), packet + .getLength())); + } + + /** Costructs a new Message */ + public BaseMessageOtp(String str) { + init(); + parseIt(str); + } + + /** Costructs a new Message */ + public BaseMessageOtp(BaseMessageOtp msg) { + init(); + remote_addr = msg.remote_addr; + remote_port = msg.remote_port; + transport_proto = msg.transport_proto; + connection_id = msg.connection_id; + // packet_length=msg.packet_length; + request_line = msg.request_line; + status_line = msg.status_line; + headers = new Vector
(); + for (int i = 0; i < msg.headers.size(); i++) + headers.addElement(msg.headers.elementAt(i)); + body = msg.body; + } + + /** Sets the entire message */ + public void setMessage(String str) { + parseIt(str); + } + + /** Parses the Message from a String. */ + private void parseIt(String str) { + SipParser par = new SipParser(str); + String version = str.substring(0, 4); + if (version.equalsIgnoreCase("SIP/")) + status_line = par.getStatusLine(); + else + request_line = par.getRequestLine(); + + headers = new Vector
(); + Header h = par.getHeader(); + while (h != null) { + headers.addElement(h); + h = par.getHeader(); + } + ContentLengthHeader clh = getContentLengthHeader(); + if (clh != null) { + int len = clh.getContentLength(); + body = par.getString(len); + } else if (getContentTypeHeader() != null) { + body = par.getRemainingString(); + if (body.length() == 0) + body = null; + } + } + + /** Gets string representation of Message */ + public String toString() { + StringBuffer str = new StringBuffer(); + if (request_line != null) + str.append(request_line.toString()); + else if (status_line != null) + str.append(status_line.toString()); + for (int i = 0; i < headers.size(); i++) + str.append(((Header) headers.elementAt(i)).toString()); + str.append("\r\n"); + if (body != null) + str.append(body); + return str.toString(); + } + + /** Gets message length */ + public int getLength() { + return toString().length(); + } + + // **************************** Requests ****************************/ + + /** Whether Message is a Request */ + public boolean isRequest() { + if (request_line != null) + return true; + else + return false; + } + + /** Whether Message is a method request */ + public boolean isRequest(String method) { + if (request_line != null + && request_line.getMethod().equalsIgnoreCase(method)) + return true; + else + return false; + } + + /** Whether Message has Request-line */ + protected boolean hasRequestLine() { + return request_line != null; + } + + /** + * Gets RequestLine in Message (Returns null if called for no request + * message) + */ + public RequestLine getRequestLine() { + return request_line; + } + + /** Sets RequestLine of the Message */ + public void setRequestLine(RequestLine rl) { + request_line = rl; + } + + /** Removes RequestLine of the Message */ + public void removeRequestLine() { + request_line = null; + } + + // **************************** Responses ****************************/ + + /** Whether Message is a Response */ + public boolean isResponse() throws NullPointerException { + if (status_line != null) + return true; + else + return false; + } + + /** Whether Message has Status-line */ + protected boolean hasStatusLine() { + return status_line != null; + } + + /** + * Gets StautsLine in Message (Returns null if called for no response + * message) + */ + public StatusLine getStatusLine() { + return status_line; + } + + /** Sets StatusLine of the Message */ + public void setStatusLine(StatusLine sl) { + status_line = sl; + } + + /** Removes StatusLine of the Message */ + public void removeStatusLine() { + status_line = null; + } + + // **************************** Generic Headers + // ****************************/ + + /** Removes Request\Status Line of the Message */ + protected void removeFirstLine() { + removeRequestLine(); + removeStatusLine(); + } + + /** Gets the position of header hname. */ + protected int indexOfHeader(String hname) { + for (int i = 0; i < headers.size(); i++) { + Header h = (Header) headers.elementAt(i); + if (hname.equalsIgnoreCase(h.getName())) + return i; + } + return -1; + } + + /** + * Gets the first Header of specified name (Returns null if no Header is + * found) + */ + public Header getHeader(String hname) { + int i = indexOfHeader(hname); + if (i < 0) + return null; + else + return (Header) headers.elementAt(i); + } + + /** + * Gets a Vector of all Headers of specified name (Returns empty Vector if + * no Header is found) + */ + public Vector
getHeaders(String hname) { + Vector
v = new Vector
(); + for (int i = 0; i < headers.size(); i++) { + Header h = (Header) headers.elementAt(i); + if (hname.equalsIgnoreCase(h.getName())) + v.addElement(h); + } + return v; + } + + /** + * Adds Header at the top/bottom. The bottom is considered before the + * Content-Length and Content-Type headers + */ + public void addHeader(Header header, boolean top) { + if (top) + headers.insertElementAt(header, 0); + else + headers.addElement(header); + } + + /** Adds a Vector of Headers at the top/bottom */ + public void addHeaders(Vector
headers, boolean top) { + for (int i = 0; i < headers.size(); i++) + if (top) + this.headers.insertElementAt(headers.elementAt(i), i); + else + this.headers.addElement(headers.elementAt(i)); + } + + /** Adds MultipleHeader(s) mheader at the top/bottom */ + public void addHeaders(MultipleHeader mheader, boolean top) { + if (mheader.isCommaSeparated()) + addHeader(mheader.toHeader(), top); + else + addHeaders(mheader.getHeaders(), top); + } + + /** + * Adds Header before the first header refer_hname . + *

+ * If there is no header of such type, it is added at top + */ + public void addHeaderBefore(Header new_header, String refer_hname) { + int i = indexOfHeader(refer_hname); + if (i < 0) + i = 0; + headers.insertElementAt(new_header, i); + } + + /** + * Adds MultipleHeader(s) before the first header refer_hname . + *

+ * If there is no header of such type, they are added at top + */ + public void addHeadersBefore(MultipleHeader mheader, String refer_hname) { + if (mheader.isCommaSeparated()) + addHeaderBefore(mheader.toHeader(), refer_hname); + else { + int index = indexOfHeader(refer_hname); + if (index < 0) + index = 0; + Vector

hs = mheader.getHeaders(); + for (int k = 0; k < hs.size(); k++) + headers.insertElementAt(hs.elementAt(k), index + k); + } + } + + /** + * Adds Header after the first header refer_hname . + *

+ * If there is no header of such type, it is added at bottom + */ + public void addHeaderAfter(Header new_header, String refer_hname) { + int i = indexOfHeader(refer_hname); + if (i >= 0) + i++; + else + i = headers.size(); + headers.insertElementAt(new_header, i); + } + + /** + * Adds MultipleHeader(s) after the first header refer_hname . + *

+ * If there is no header of such type, they are added at bottom + */ + public void addHeadersAfter(MultipleHeader mheader, String refer_hname) { + if (mheader.isCommaSeparated()) + addHeaderAfter(mheader.toHeader(), refer_hname); + else { + int index = indexOfHeader(refer_hname); + if (index >= 0) + index++; + else + index = headers.size(); + Vector

hs = mheader.getHeaders(); + for (int k = 0; k < hs.size(); k++) + headers.insertElementAt(hs.elementAt(k), index + k); + } + } + + /** Removes first Header of specified name */ + public void removeHeader(String hname) { + removeHeader(hname, true); + } + + /** Removes first (or last) Header of specified name. */ + public void removeHeader(String hname, boolean first) { + int index = -1; + for (int i = 0; i < headers.size(); i++) { + Header h = (Header) headers.elementAt(i); + if (hname.equalsIgnoreCase(h.getName())) { + index = i; + if (first) + i = headers.size(); + } + } + if (index >= 0) + headers.removeElementAt(index); + } + + /** Removes all Headers of specified name */ + public void removeAllHeaders(String hname) { + for (int i = 0; i < headers.size(); i++) { + Header h = (Header) headers.elementAt(i); + if (hname.equalsIgnoreCase(h.getName())) { + headers.removeElementAt(i); + i--; + } + } + } + + /** + * Sets the Header hd removing any previous headers of the same + * type. + */ + public void setHeader(Header hd) { + boolean first = true; + String hname = hd.getName(); + for (int i = 0; i < headers.size(); i++) { + Header h = (Header) headers.elementAt(i); + if (hname.equalsIgnoreCase(h.getName())) { + if (first) { // replace it + headers.setElementAt(h, i); + first = false; + } else { // remove it + headers.removeElementAt(i); + i--; + } + } + } + if (first) + headers.addElement(hd); + } + + /** Sets MultipleHeader mheader */ + public void setHeaders(MultipleHeader mheader) { + if (mheader.isCommaSeparated()) + setHeader(mheader.toHeader()); + else { + boolean first = true; + String hname = mheader.getName(); + for (int i = 0; i < headers.size(); i++) { + Header h = (Header) headers.elementAt(i); + if (hname.equalsIgnoreCase(h.getName())) { + if (first) { // replace it + Vector
hs = mheader.getHeaders(); + for (int k = 0; k < hs.size(); k++) + headers.insertElementAt(hs.elementAt(k), i + k); + first = false; + i += hs.size() - 1; + } else { // remove it + headers.removeElementAt(i); + i--; + } + } + } + } + } + + // **************************** Specific Headers + // ****************************/ + + /** Whether Message has Body */ + public boolean hasBody() { + return this.body != null; + } + + /** Gets body(content) type */ + public String getBodyType() { + return getContentTypeHeader().getContentType(); + } + + /** Sets the message body */ + public void setBody(String content_type, String body) { + removeBody(); + if (body != null && body.length() > 0) { + setContentTypeHeader(new ContentTypeHeader(content_type)); + setContentLengthHeader(new ContentLengthHeader(body.length())); + this.body = body; + } else { + setContentLengthHeader(new ContentLengthHeader(0)); + this.body = null; + } + } + + /** + * Gets message body. The end of body is evaluated from the Content-Length + * header if present (SIP-RFC compliant), or from the end of message if no + * Content-Length header is present (non-SIP-RFC compliant) + */ + public String getBody() { + return this.body; + } + + /** Removes the message body (if it exists) and the final empty line */ + public void removeBody() { + removeContentLengthHeader(); + removeContentTypeHeader(); + this.body = null; + } + +} diff --git a/src/org/zoolu/sip/message/BaseSipMethods.java b/app/src/main/java/org/zoolu/sip/message/BaseSipMethods.java similarity index 96% rename from src/org/zoolu/sip/message/BaseSipMethods.java rename to app/src/main/java/org/zoolu/sip/message/BaseSipMethods.java index 08eecc9..27805fa 100644 --- a/src/org/zoolu/sip/message/BaseSipMethods.java +++ b/app/src/main/java/org/zoolu/sip/message/BaseSipMethods.java @@ -1,115 +1,115 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.message; - -/** - * Class BaseSipMethods collects all SIP method names. - */ -public abstract class BaseSipMethods { - /** String "INVITE" */ - public static final String INVITE = "INVITE"; - - /** Whether str is INVITE */ - public static boolean isInvite(String str) { - return same(str, INVITE); - } - - /** String "ACK" */ - public static final String ACK = "ACK"; - - /** Whether str is ACK */ - public static boolean isAck(String str) { - return same(str, ACK); - } - - /** String "CANCEL" */ - public static final String CANCEL = "CANCEL"; - - /** Whether str is CANCEL */ - public static boolean isCancel(String str) { - return same(str, CANCEL); - } - - /** String "BYE" */ - public static final String BYE = "BYE"; - - /** Whether str is BYE */ - public static boolean isBye(String str) { - return same(str, BYE); - } - - /** String "INFO" */ - public static final String INFO = "INFO"; - - /** Whether str is INFO */ - public static boolean isInfo(String str) { - return same(str, INFO); - } - - /** String "OPTION" */ - public static final String OPTION = "OPTION"; - - /** Whether str is OPTION */ - public static boolean isOption(String str) { - return same(str, OPTION); - } - - /** String "OPTION" */ - public static final String OPTIONS = "OPTIONS"; - - /** Whether str is OPTIONS */ - public static boolean isOptions(String str) { - return same(str, OPTIONS); - } - - /** String "REGISTER" */ - public static final String REGISTER = "REGISTER"; - - /** Whether str is REGISTER */ - public static boolean isRegister(String str) { - return same(str, REGISTER); - } - - /** String "UPDATE" */ - public static final String UPDATE = "UPDATE"; - - /** Whether str is UPDATE */ - public static boolean isUpdate(String str) { - return same(str, UPDATE); - } - - /** Whether s1 and s2 are case-unsensitive-equal. */ - protected static boolean same(String s1, String s2) { // return - // s1.compareToIgnoreCase(s2)==0; - return s1.equalsIgnoreCase(s2); - } - - /** Array of standard methods */ - public static final String[] methods = { INVITE, ACK, CANCEL, BYE, INFO, - OPTION, REGISTER, UPDATE }; - - /** Array of standards methods that creates a dialog */ - public static final String[] dialog_methods = { INVITE }; -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.message; + +/** + * Class BaseSipMethods collects all SIP method names. + */ +public abstract class BaseSipMethods { + /** String "INVITE" */ + public static final String INVITE = "INVITE"; + + /** Whether str is INVITE */ + public static boolean isInvite(String str) { + return same(str, INVITE); + } + + /** String "ACK" */ + public static final String ACK = "ACK"; + + /** Whether str is ACK */ + public static boolean isAck(String str) { + return same(str, ACK); + } + + /** String "CANCEL" */ + public static final String CANCEL = "CANCEL"; + + /** Whether str is CANCEL */ + public static boolean isCancel(String str) { + return same(str, CANCEL); + } + + /** String "BYE" */ + public static final String BYE = "BYE"; + + /** Whether str is BYE */ + public static boolean isBye(String str) { + return same(str, BYE); + } + + /** String "INFO" */ + public static final String INFO = "INFO"; + + /** Whether str is INFO */ + public static boolean isInfo(String str) { + return same(str, INFO); + } + + /** String "OPTION" */ + public static final String OPTION = "OPTION"; + + /** Whether str is OPTION */ + public static boolean isOption(String str) { + return same(str, OPTION); + } + + /** String "OPTION" */ + public static final String OPTIONS = "OPTIONS"; + + /** Whether str is OPTIONS */ + public static boolean isOptions(String str) { + return same(str, OPTIONS); + } + + /** String "REGISTER" */ + public static final String REGISTER = "REGISTER"; + + /** Whether str is REGISTER */ + public static boolean isRegister(String str) { + return same(str, REGISTER); + } + + /** String "UPDATE" */ + public static final String UPDATE = "UPDATE"; + + /** Whether str is UPDATE */ + public static boolean isUpdate(String str) { + return same(str, UPDATE); + } + + /** Whether s1 and s2 are case-unsensitive-equal. */ + protected static boolean same(String s1, String s2) { // return + // s1.compareToIgnoreCase(s2)==0; + return s1.equalsIgnoreCase(s2); + } + + /** Array of standard methods */ + public static final String[] methods = { INVITE, ACK, CANCEL, BYE, INFO, + OPTION, REGISTER, UPDATE }; + + /** Array of standards methods that creates a dialog */ + public static final String[] dialog_methods = { INVITE }; +} diff --git a/src/org/zoolu/sip/message/BaseSipResponses.java b/app/src/main/java/org/zoolu/sip/message/BaseSipResponses.java similarity index 96% rename from src/org/zoolu/sip/message/BaseSipResponses.java rename to app/src/main/java/org/zoolu/sip/message/BaseSipResponses.java index 7662fd8..61e0f6c 100644 --- a/src/org/zoolu/sip/message/BaseSipResponses.java +++ b/app/src/main/java/org/zoolu/sip/message/BaseSipResponses.java @@ -1,131 +1,131 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.message; - -/** - * Class SipResponses provides all raeson-phrases corrspondent to the various - * SIP response codes - */ -public abstract class BaseSipResponses { - // static Hashtable reasons; - protected static String[] reasons; - - private static boolean is_init = false; - - protected static void init() { - if (is_init) - return; - // else - - // reasons=new Hashtable(); - // reasons.put(new Integer(100),"Trying"); - // .. - reasons = new String[700]; - for (int i = 0; i < 700; i++) - reasons[i] = null; - - // Not defined (included just to robustness) - reasons[0] = "Internal error"; - - // Informational - reasons[100] = "Trying"; - reasons[180] = "Ringing"; - reasons[181] = "Call Is Being Forwarded"; - reasons[182] = "Queued"; - reasons[183] = "Session Progress"; - - // Success - reasons[200] = "OK"; - - // Redirection - reasons[300] = "Multiple Choices"; - reasons[301] = "Moved Permanently"; - reasons[302] = "Moved Temporarily"; - reasons[305] = "Use Proxy"; - reasons[380] = "Alternative Service"; - - // Client-Error - reasons[400] = "Bad Request"; - reasons[401] = "Unauthorized"; - reasons[402] = "Payment Required"; - reasons[403] = "Forbidden"; - reasons[404] = "Not Found"; - reasons[405] = "Method Not Allowed"; - reasons[406] = "Not Acceptable"; - reasons[407] = "Proxy Authentication Required"; - reasons[408] = "Request Timeout"; - reasons[410] = "Gone"; - reasons[413] = "Request Entity Too Large"; - reasons[414] = "Request-URI Too Large"; - reasons[415] = "Unsupported Media Type"; - reasons[416] = "Unsupported URI Scheme"; - reasons[420] = "Bad Extension"; - reasons[421] = "Extension Required"; - reasons[423] = "Interval Too Brief"; - reasons[480] = "Temporarily not available"; - reasons[481] = "Call Leg/Transaction Does Not Exist"; - reasons[482] = "Loop Detected"; - reasons[483] = "Too Many Hops"; - reasons[484] = "Address Incomplete"; - reasons[485] = "Ambiguous"; - reasons[486] = "Busy Here"; - reasons[487] = "Request Terminated"; - reasons[488] = "Not Acceptable Here"; - reasons[491] = "Request Pending"; - reasons[493] = "Undecipherable"; - - // Server-Error - reasons[500] = "Internal Server Error"; - reasons[501] = "Not Implemented"; - reasons[502] = "Bad Gateway"; - reasons[503] = "Service Unavailable"; - reasons[504] = "Server Time-out"; - reasons[505] = "SIP Version not supported"; - reasons[513] = "Message Too Large"; - - // Global-Failure - reasons[600] = "Busy Everywhere"; - reasons[603] = "Decline"; - reasons[604] = "Does not exist anywhere"; - reasons[606] = "Not Acceptable"; - - is_init = true; - } - - /** Gets the reason phrase of a given response code */ - public static String reasonOf(int code) { - if (!is_init) - init(); - if (reasons[code] != null) - return reasons[code]; - else - return reasons[((int) (code / 100)) * 100]; - } - - /** Sets the reason phrase for a given response code */ - /* - * public static void setReason(int code, String reason) { - * reasons[((int)(code/100))*100]=reason; } - */ +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.message; + +/** + * Class SipResponses provides all raeson-phrases corrspondent to the various + * SIP response codes + */ +public abstract class BaseSipResponses { + // static Hashtable reasons; + protected static String[] reasons; + + private static boolean is_init = false; + + protected static void init() { + if (is_init) + return; + // else + + // reasons=new Hashtable(); + // reasons.put(new Integer(100),"Trying"); + // .. + reasons = new String[700]; + for (int i = 0; i < 700; i++) + reasons[i] = null; + + // Not defined (included just to robustness) + reasons[0] = "Internal error"; + + // Informational + reasons[100] = "Trying"; + reasons[180] = "Ringing"; + reasons[181] = "Call Is Being Forwarded"; + reasons[182] = "Queued"; + reasons[183] = "Session Progress"; + + // Success + reasons[200] = "OK"; + + // Redirection + reasons[300] = "Multiple Choices"; + reasons[301] = "Moved Permanently"; + reasons[302] = "Moved Temporarily"; + reasons[305] = "Use Proxy"; + reasons[380] = "Alternative Service"; + + // Client-Error + reasons[400] = "Bad Request"; + reasons[401] = "Unauthorized"; + reasons[402] = "Payment Required"; + reasons[403] = "Forbidden"; + reasons[404] = "Not Found"; + reasons[405] = "Method Not Allowed"; + reasons[406] = "Not Acceptable"; + reasons[407] = "Proxy Authentication Required"; + reasons[408] = "Request Timeout"; + reasons[410] = "Gone"; + reasons[413] = "Request Entity Too Large"; + reasons[414] = "Request-URI Too Large"; + reasons[415] = "Unsupported Media Type"; + reasons[416] = "Unsupported URI Scheme"; + reasons[420] = "Bad Extension"; + reasons[421] = "Extension Required"; + reasons[423] = "Interval Too Brief"; + reasons[480] = "Temporarily not available"; + reasons[481] = "Call Leg/Transaction Does Not Exist"; + reasons[482] = "Loop Detected"; + reasons[483] = "Too Many Hops"; + reasons[484] = "Address Incomplete"; + reasons[485] = "Ambiguous"; + reasons[486] = "Busy Here"; + reasons[487] = "Request Terminated"; + reasons[488] = "Not Acceptable Here"; + reasons[491] = "Request Pending"; + reasons[493] = "Undecipherable"; + + // Server-Error + reasons[500] = "Internal Server Error"; + reasons[501] = "Not Implemented"; + reasons[502] = "Bad Gateway"; + reasons[503] = "Service Unavailable"; + reasons[504] = "Server Time-out"; + reasons[505] = "SIP Version not supported"; + reasons[513] = "Message Too Large"; + + // Global-Failure + reasons[600] = "Busy Everywhere"; + reasons[603] = "Decline"; + reasons[604] = "Does not exist anywhere"; + reasons[606] = "Not Acceptable"; + + is_init = true; + } + + /** Gets the reason phrase of a given response code */ + public static String reasonOf(int code) { + if (!is_init) + init(); + if (reasons[code] != null) + return reasons[code]; + else + return reasons[((int) (code / 100)) * 100]; + } + + /** Sets the reason phrase for a given response code */ + /* + * public static void setReason(int code, String reason) { + * reasons[((int)(code/100))*100]=reason; } + */ } \ No newline at end of file diff --git a/src/org/zoolu/sip/message/Message.java b/app/src/main/java/org/zoolu/sip/message/Message.java similarity index 96% rename from src/org/zoolu/sip/message/Message.java rename to app/src/main/java/org/zoolu/sip/message/Message.java index 2c03174..ff0ebb2 100644 --- a/src/org/zoolu/sip/message/Message.java +++ b/app/src/main/java/org/zoolu/sip/message/Message.java @@ -1,234 +1,234 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.message; - -import org.zoolu.sip.header.Header; -import org.zoolu.sip.header.ReferToHeader; -import org.zoolu.sip.header.ReferredByHeader; -import org.zoolu.sip.header.EventHeader; -import org.zoolu.sip.header.AllowEventsHeader; -import org.zoolu.sip.header.SubscriptionStateHeader; -import org.zoolu.sip.header.SipHeaders; -import org.zoolu.net.UdpPacket; - -/** - * Class Message extends class sip.message.BaseMessage adding some SIP - * extensions. - *

- * Class Message supports all methods and header definened in RFC3261, plus: - *

    - *
  • method MESSAGE (RFC3428) - *
  • method REFER (RFC3515) - *
  • header Refer-To - *
  • header Referred-By - *
  • header Event - *
- */ -public class Message extends org.zoolu.sip.message.BaseMessage { - /** Costructs a new empty Message */ - public Message() { - super(); - } - - /** Costructs a new Message */ - public Message(String str) { - super(str); - } - - /** Costructs a new Message */ - public Message(byte[] buff, int offset, int len) { - super(buff, offset, len); - } - - /** Costructs a new Message */ - public Message(UdpPacket packet) { - super(packet); - } - - /** Costructs a new Message */ - public Message(Message msg) { - super(msg); - } - - /** Creates and returns a clone of the Message */ - public Object clone() { - return new Message(this); - } - - // ****************************** Extensions - // *******************************/ - - /** - * Returns boolean value to indicate if Message is a MESSAGE request - * (RFC3428) - */ - public boolean isMessage() throws NullPointerException { - return isRequest(SipMethods.MESSAGE); - } - - /** Returns boolean value to indicate if Message is a REFER request (RFC3515) */ - public boolean isRefer() throws NullPointerException { - return isRequest(SipMethods.REFER); - } - - /** - * Returns boolean value to indicate if Message is a NOTIFY request - * (RFC3265) - */ - public boolean isNotify() throws NullPointerException { - return isRequest(SipMethods.NOTIFY); - } - - /** - * Returns boolean value to indicate if Message is a SUBSCRIBE request - * (RFC3265) - */ - public boolean isSubscribe() throws NullPointerException { - return isRequest(SipMethods.SUBSCRIBE); - } - - /** - * Returns boolean value to indicate if Message is a PUBLISH request - * (RFC3903) - */ - public boolean isPublish() throws NullPointerException { - return isRequest(SipMethods.PUBLISH); - } - - /** Whether the message has the Refer-To header */ - public boolean hasReferToHeader() { - return hasHeader(SipHeaders.Refer_To); - } - - /** Gets ReferToHeader */ - public ReferToHeader getReferToHeader() { - Header h = getHeader(SipHeaders.Refer_To); - if (h == null) - return null; - return new ReferToHeader(h); - } - - /** Sets ReferToHeader */ - public void setReferToHeader(ReferToHeader h) { - setHeader(h); - } - - /** Removes ReferToHeader from Message (if it exists) */ - public void removeReferToHeader() { - removeHeader(SipHeaders.Refer_To); - } - - /** Whether the message has the Referred-By header */ - public boolean hasReferredByHeader() { - return hasHeader(SipHeaders.Refer_To); - } - - /** Gets ReferredByHeader */ - public ReferredByHeader getReferredByHeader() { - Header h = getHeader(SipHeaders.Referred_By); - if (h == null) - return null; - return new ReferredByHeader(h); - } - - /** Sets ReferredByHeader */ - public void setReferredByHeader(ReferredByHeader h) { - setHeader(h); - } - - /** Removes ReferredByHeader from Message (if it exists) */ - public void removeReferredByHeader() { - removeHeader(SipHeaders.Referred_By); - } - - /** Whether the message has the EventHeader */ - public boolean hasEventHeader() { - return hasHeader(SipHeaders.Event); - } - - /** Gets EventHeader */ - public EventHeader getEventHeader() { - Header h = getHeader(SipHeaders.Event); - if (h == null) - return null; - return new EventHeader(h); - } - - /** Sets EventHeader */ - public void setEventHeader(EventHeader h) { - setHeader(h); - } - - /** Removes EventHeader from Message (if it exists) */ - public void removeEventHeader() { - removeHeader(SipHeaders.Event); - } - - /** Whether the message has the AllowEventsHeader */ - public boolean hasAllowEventsHeader() { - return hasHeader(SipHeaders.Allow_Events); - } - - /** Gets AllowEventsHeader */ - public AllowEventsHeader getAllowEventsHeader() { - Header h = getHeader(SipHeaders.Allow_Events); - if (h == null) - return null; - return new AllowEventsHeader(h); - } - - /** Sets AllowEventsHeader */ - public void setAllowEventsHeader(AllowEventsHeader h) { - setHeader(h); - } - - /** Removes AllowEventsHeader from Message (if it exists) */ - public void removeAllowEventsHeader() { - removeHeader(SipHeaders.Allow_Events); - } - - /** Whether the message has the Subscription-State header */ - public boolean hasSubscriptionStateHeader() { - return hasHeader(SipHeaders.Subscription_State); - } - - /** Gets SubscriptionStateHeader */ - public SubscriptionStateHeader getSubscriptionStateHeader() { - Header h = getHeader(SipHeaders.Subscription_State); - if (h == null) - return null; - return new SubscriptionStateHeader(h); - } - - /** Sets SubscriptionStateHeader */ - public void setSubscriptionStateHeader(SubscriptionStateHeader h) { - setHeader(h); - } - - /** Removes SubscriptionStateHeader from Message (if it exists) */ - public void removeSubscriptionStateHeader() { - removeHeader(SipHeaders.Subscription_State); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.message; + +import org.zoolu.sip.header.Header; +import org.zoolu.sip.header.ReferToHeader; +import org.zoolu.sip.header.ReferredByHeader; +import org.zoolu.sip.header.EventHeader; +import org.zoolu.sip.header.AllowEventsHeader; +import org.zoolu.sip.header.SubscriptionStateHeader; +import org.zoolu.sip.header.SipHeaders; +import org.zoolu.net.UdpPacket; + +/** + * Class Message extends class sip.message.BaseMessage adding some SIP + * extensions. + *

+ * Class Message supports all methods and header definened in RFC3261, plus: + *

    + *
  • method MESSAGE (RFC3428) + *
  • method REFER (RFC3515) + *
  • header Refer-To + *
  • header Referred-By + *
  • header Event + *
+ */ +public class Message extends org.zoolu.sip.message.BaseMessage { + /** Costructs a new empty Message */ + public Message() { + super(); + } + + /** Costructs a new Message */ + public Message(String str) { + super(str); + } + + /** Costructs a new Message */ + public Message(byte[] buff, int offset, int len) { + super(buff, offset, len); + } + + /** Costructs a new Message */ + public Message(UdpPacket packet) { + super(packet); + } + + /** Costructs a new Message */ + public Message(Message msg) { + super(msg); + } + + /** Creates and returns a clone of the Message */ + public Object clone() { + return new Message(this); + } + + // ****************************** Extensions + // *******************************/ + + /** + * Returns boolean value to indicate if Message is a MESSAGE request + * (RFC3428) + */ + public boolean isMessage() throws NullPointerException { + return isRequest(SipMethods.MESSAGE); + } + + /** Returns boolean value to indicate if Message is a REFER request (RFC3515) */ + public boolean isRefer() throws NullPointerException { + return isRequest(SipMethods.REFER); + } + + /** + * Returns boolean value to indicate if Message is a NOTIFY request + * (RFC3265) + */ + public boolean isNotify() throws NullPointerException { + return isRequest(SipMethods.NOTIFY); + } + + /** + * Returns boolean value to indicate if Message is a SUBSCRIBE request + * (RFC3265) + */ + public boolean isSubscribe() throws NullPointerException { + return isRequest(SipMethods.SUBSCRIBE); + } + + /** + * Returns boolean value to indicate if Message is a PUBLISH request + * (RFC3903) + */ + public boolean isPublish() throws NullPointerException { + return isRequest(SipMethods.PUBLISH); + } + + /** Whether the message has the Refer-To header */ + public boolean hasReferToHeader() { + return hasHeader(SipHeaders.Refer_To); + } + + /** Gets ReferToHeader */ + public ReferToHeader getReferToHeader() { + Header h = getHeader(SipHeaders.Refer_To); + if (h == null) + return null; + return new ReferToHeader(h); + } + + /** Sets ReferToHeader */ + public void setReferToHeader(ReferToHeader h) { + setHeader(h); + } + + /** Removes ReferToHeader from Message (if it exists) */ + public void removeReferToHeader() { + removeHeader(SipHeaders.Refer_To); + } + + /** Whether the message has the Referred-By header */ + public boolean hasReferredByHeader() { + return hasHeader(SipHeaders.Refer_To); + } + + /** Gets ReferredByHeader */ + public ReferredByHeader getReferredByHeader() { + Header h = getHeader(SipHeaders.Referred_By); + if (h == null) + return null; + return new ReferredByHeader(h); + } + + /** Sets ReferredByHeader */ + public void setReferredByHeader(ReferredByHeader h) { + setHeader(h); + } + + /** Removes ReferredByHeader from Message (if it exists) */ + public void removeReferredByHeader() { + removeHeader(SipHeaders.Referred_By); + } + + /** Whether the message has the EventHeader */ + public boolean hasEventHeader() { + return hasHeader(SipHeaders.Event); + } + + /** Gets EventHeader */ + public EventHeader getEventHeader() { + Header h = getHeader(SipHeaders.Event); + if (h == null) + return null; + return new EventHeader(h); + } + + /** Sets EventHeader */ + public void setEventHeader(EventHeader h) { + setHeader(h); + } + + /** Removes EventHeader from Message (if it exists) */ + public void removeEventHeader() { + removeHeader(SipHeaders.Event); + } + + /** Whether the message has the AllowEventsHeader */ + public boolean hasAllowEventsHeader() { + return hasHeader(SipHeaders.Allow_Events); + } + + /** Gets AllowEventsHeader */ + public AllowEventsHeader getAllowEventsHeader() { + Header h = getHeader(SipHeaders.Allow_Events); + if (h == null) + return null; + return new AllowEventsHeader(h); + } + + /** Sets AllowEventsHeader */ + public void setAllowEventsHeader(AllowEventsHeader h) { + setHeader(h); + } + + /** Removes AllowEventsHeader from Message (if it exists) */ + public void removeAllowEventsHeader() { + removeHeader(SipHeaders.Allow_Events); + } + + /** Whether the message has the Subscription-State header */ + public boolean hasSubscriptionStateHeader() { + return hasHeader(SipHeaders.Subscription_State); + } + + /** Gets SubscriptionStateHeader */ + public SubscriptionStateHeader getSubscriptionStateHeader() { + Header h = getHeader(SipHeaders.Subscription_State); + if (h == null) + return null; + return new SubscriptionStateHeader(h); + } + + /** Sets SubscriptionStateHeader */ + public void setSubscriptionStateHeader(SubscriptionStateHeader h) { + setHeader(h); + } + + /** Removes SubscriptionStateHeader from Message (if it exists) */ + public void removeSubscriptionStateHeader() { + removeHeader(SipHeaders.Subscription_State); + } + +} diff --git a/src/org/zoolu/sip/message/MessageFactory.java b/app/src/main/java/org/zoolu/sip/message/MessageFactory.java similarity index 97% rename from src/org/zoolu/sip/message/MessageFactory.java rename to app/src/main/java/org/zoolu/sip/message/MessageFactory.java index 5fe27bd..8f6343d 100644 --- a/src/org/zoolu/sip/message/MessageFactory.java +++ b/app/src/main/java/org/zoolu/sip/message/MessageFactory.java @@ -1,152 +1,152 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.message; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.header.SubjectHeader; -import org.zoolu.sip.header.ReferToHeader; -import org.zoolu.sip.header.ReferredByHeader; -import org.zoolu.sip.header.EventHeader; -import org.zoolu.sip.dialog.Dialog; -import org.zoolu.sip.provider.SipProvider; - -/** - * Class sipx.message.MessageFactory extends class - * sip.message.BaseMessageFactory. - *

- * MessageFactory is used to create SIP messages (requests and responses). - *
- * A valid SIP request sent by a UAC MUST, at least, contain the following - * header fields: To, From, CSeq, Call-ID, Max-Forwards, and Via; all of these - * header fields are mandatory in all SIP requests. These sip header fields are - * the fundamental building blocks of a SIP message, as they jointly provide for - * most of the critical message routing services including the addressing of - * messages, the routing of responses, limiting message propagation, ordering of - * messages, and the unique identification of transactions. These header fields - * are in addition to the mandatory request line, which contains the method, - * Request-URI, and SIP version. - */ -public class MessageFactory extends org.zoolu.sip.message.BaseMessageFactory { - - /** Creates a new MESSAGE request (RFC3428) */ - public static Message createMessageRequest(SipProvider sip_provider, - NameAddress recipient, NameAddress from, String subject, - String type, String body) { - SipURL request_uri = recipient.getAddress(); - String callid = sip_provider.pickCallId(); - int cseq = SipProvider.pickInitialCSeq(); - String localtag = SipProvider.pickTag(); - // String branch=SipStack.pickBranch(); - Message req = createRequest(sip_provider, SipMethods.MESSAGE, - request_uri, recipient, from, null, callid, cseq, localtag, - null, null, null, null); // modified by mandrajg - if (subject != null) - req.setSubjectHeader(new SubjectHeader(subject)); - req.setBody(type, body); - return req; - } - - /** Creates a new REFER request (RFC3515) */ - public static Message createReferRequest(SipProvider sip_provider, - NameAddress recipient, NameAddress from, NameAddress contact, - NameAddress refer_to/* , NameAddress referred_by */) { - SipURL request_uri = recipient.getAddress(); - String callid = sip_provider.pickCallId(); - int cseq = SipProvider.pickInitialCSeq(); - String localtag = SipProvider.pickTag(); - // String branch=SipStack.pickBranch(); - Message req = createRequest(sip_provider, SipMethods.REFER, - request_uri, recipient, from, contact, callid, cseq, localtag, - null, null, null, null); // modified by mandrajg - req.setReferToHeader(new ReferToHeader(refer_to)); - // if (referred_by!=null) req.setReferredByHeader(new - // ReferredByHeader(referred_by)); - req.setReferredByHeader(new ReferredByHeader(from)); - return req; - } - - /** - * Creates a new REFER request (RFC3515) within a dialog - *

- * parameters:
- refer_to mandatory
- referred_by - * optional - */ - public static Message createReferRequest(Dialog dialog, - NameAddress refer_to, NameAddress referred_by) { - Message req = createRequest(dialog, SipMethods.REFER, null); - req.setReferToHeader(new ReferToHeader(refer_to)); - if (referred_by != null) - req.setReferredByHeader(new ReferredByHeader(referred_by)); - else - req - .setReferredByHeader(new ReferredByHeader(dialog - .getLocalName())); - return req; - } - - /** - * Creates a new SUBSCRIBE request (RFC3265) out of any pre-existing - * dialogs. - */ - public static Message createSubscribeRequest(SipProvider sip_provider, - SipURL recipient, NameAddress to, NameAddress from, - NameAddress contact, String event, String id, String content_type, - String body) { - Message req = createRequest(sip_provider, SipMethods.SUBSCRIBE, - recipient, to, from, contact, null); - req.setEventHeader(new EventHeader(event, id)); - req.setBody(content_type, body); - return req; - } - - /** Creates a new SUBSCRIBE request (RFC3265) within a dialog (re-subscribe). */ - public static Message createSubscribeRequest(Dialog dialog, String event, - String id, String content_type, String body) { - Message req = createRequest(dialog, SipMethods.SUBSCRIBE, null); - req.setEventHeader(new EventHeader(event, id)); - req.setBody(content_type, body); - return req; - } - - /** Creates a new NOTIFY request (RFC3265) within a dialog */ - public static Message createNotifyRequest(Dialog dialog, String event, - String id, String content_type, String body) { - Message req = createRequest(dialog, SipMethods.NOTIFY, null); - req.removeExpiresHeader(); - req.setEventHeader(new EventHeader(event, id)); - req.setBody(content_type, body); - return req; - } - - /** Creates a new NOTIFY request (RFC3265) within a dialog */ - public static Message createNotifyRequest(Dialog dialog, String event, - String id, String sipfragment) { - Message req = createRequest(dialog, SipMethods.NOTIFY, null); - req.removeExpiresHeader(); - req.setEventHeader(new EventHeader(event, id)); - req.setBody("message/sipfrag;version=2.0", sipfragment); - return req; - } - +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.message; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.header.SubjectHeader; +import org.zoolu.sip.header.ReferToHeader; +import org.zoolu.sip.header.ReferredByHeader; +import org.zoolu.sip.header.EventHeader; +import org.zoolu.sip.dialog.Dialog; +import org.zoolu.sip.provider.SipProvider; + +/** + * Class sipx.message.MessageFactory extends class + * sip.message.BaseMessageFactory. + *

+ * MessageFactory is used to create SIP messages (requests and responses). + *
+ * A valid SIP request sent by a UAC MUST, at least, contain the following + * header fields: To, From, CSeq, Call-ID, Max-Forwards, and Via; all of these + * header fields are mandatory in all SIP requests. These sip header fields are + * the fundamental building blocks of a SIP message, as they jointly provide for + * most of the critical message routing services including the addressing of + * messages, the routing of responses, limiting message propagation, ordering of + * messages, and the unique identification of transactions. These header fields + * are in addition to the mandatory request line, which contains the method, + * Request-URI, and SIP version. + */ +public class MessageFactory extends org.zoolu.sip.message.BaseMessageFactory { + + /** Creates a new MESSAGE request (RFC3428) */ + public static Message createMessageRequest(SipProvider sip_provider, + NameAddress recipient, NameAddress from, String subject, + String type, String body) { + SipURL request_uri = recipient.getAddress(); + String callid = sip_provider.pickCallId(); + int cseq = SipProvider.pickInitialCSeq(); + String localtag = SipProvider.pickTag(); + // String branch=SipStack.pickBranch(); + Message req = createRequest(sip_provider, SipMethods.MESSAGE, + request_uri, recipient, from, null, callid, cseq, localtag, + null, null, null, null); // modified by mandrajg + if (subject != null) + req.setSubjectHeader(new SubjectHeader(subject)); + req.setBody(type, body); + return req; + } + + /** Creates a new REFER request (RFC3515) */ + public static Message createReferRequest(SipProvider sip_provider, + NameAddress recipient, NameAddress from, NameAddress contact, + NameAddress refer_to/* , NameAddress referred_by */) { + SipURL request_uri = recipient.getAddress(); + String callid = sip_provider.pickCallId(); + int cseq = SipProvider.pickInitialCSeq(); + String localtag = SipProvider.pickTag(); + // String branch=SipStack.pickBranch(); + Message req = createRequest(sip_provider, SipMethods.REFER, + request_uri, recipient, from, contact, callid, cseq, localtag, + null, null, null, null); // modified by mandrajg + req.setReferToHeader(new ReferToHeader(refer_to)); + // if (referred_by!=null) req.setReferredByHeader(new + // ReferredByHeader(referred_by)); + req.setReferredByHeader(new ReferredByHeader(from)); + return req; + } + + /** + * Creates a new REFER request (RFC3515) within a dialog + *

+ * parameters:
- refer_to mandatory
- referred_by + * optional + */ + public static Message createReferRequest(Dialog dialog, + NameAddress refer_to, NameAddress referred_by) { + Message req = createRequest(dialog, SipMethods.REFER, null); + req.setReferToHeader(new ReferToHeader(refer_to)); + if (referred_by != null) + req.setReferredByHeader(new ReferredByHeader(referred_by)); + else + req + .setReferredByHeader(new ReferredByHeader(dialog + .getLocalName())); + return req; + } + + /** + * Creates a new SUBSCRIBE request (RFC3265) out of any pre-existing + * dialogs. + */ + public static Message createSubscribeRequest(SipProvider sip_provider, + SipURL recipient, NameAddress to, NameAddress from, + NameAddress contact, String event, String id, String content_type, + String body) { + Message req = createRequest(sip_provider, SipMethods.SUBSCRIBE, + recipient, to, from, contact, null); + req.setEventHeader(new EventHeader(event, id)); + req.setBody(content_type, body); + return req; + } + + /** Creates a new SUBSCRIBE request (RFC3265) within a dialog (re-subscribe). */ + public static Message createSubscribeRequest(Dialog dialog, String event, + String id, String content_type, String body) { + Message req = createRequest(dialog, SipMethods.SUBSCRIBE, null); + req.setEventHeader(new EventHeader(event, id)); + req.setBody(content_type, body); + return req; + } + + /** Creates a new NOTIFY request (RFC3265) within a dialog */ + public static Message createNotifyRequest(Dialog dialog, String event, + String id, String content_type, String body) { + Message req = createRequest(dialog, SipMethods.NOTIFY, null); + req.removeExpiresHeader(); + req.setEventHeader(new EventHeader(event, id)); + req.setBody(content_type, body); + return req; + } + + /** Creates a new NOTIFY request (RFC3265) within a dialog */ + public static Message createNotifyRequest(Dialog dialog, String event, + String id, String sipfragment) { + Message req = createRequest(dialog, SipMethods.NOTIFY, null); + req.removeExpiresHeader(); + req.setEventHeader(new EventHeader(event, id)); + req.setBody("message/sipfrag;version=2.0", sipfragment); + return req; + } + } \ No newline at end of file diff --git a/src/org/zoolu/sip/message/SipMethods.java b/app/src/main/java/org/zoolu/sip/message/SipMethods.java similarity index 96% rename from src/org/zoolu/sip/message/SipMethods.java rename to app/src/main/java/org/zoolu/sip/message/SipMethods.java index 9ae7dac..dd34a0b 100644 --- a/src/org/zoolu/sip/message/SipMethods.java +++ b/app/src/main/java/org/zoolu/sip/message/SipMethods.java @@ -1,82 +1,82 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.message; - -/** - * Class SipMethods extends org.zoolu.sip.message.BaseSipMethods and collects - * all SIP method names. - */ -public class SipMethods extends BaseSipMethods { - - // ****************************** Extensions - // *******************************/ - - /** String "SUBSCRIBE" */ - public static final String SUBSCRIBE = "SUBSCRIBE"; - - /** Whether str is SUBSCRIBE */ - public static boolean isSubscribe(String str) { - return same(str, SUBSCRIBE); - } - - /** String "NOTIFY" */ - public static final String NOTIFY = "NOTIFY"; - - /** Whether str is NOTIFY */ - public static boolean isNotify(String str) { - return same(str, NOTIFY); - } - - /** String "MESSAGE" for method MESSAGE defined in RFC3428 */ - public static final String MESSAGE = "MESSAGE"; - - /** Whether str is MESSAGE */ - public static boolean isMessage(String str) { - return same(str, MESSAGE); - } - - /** String "REFER" for method REFER defined in RFC3515 */ - public static final String REFER = "REFER"; - - /** Whether str is REFER */ - public static boolean isRefer(String str) { - return same(str, REFER); - } - - /** String "PUBLISH" for method PUBLISH defined in RFC3903 */ - public static final String PUBLISH = "PUBLISH"; - - /** Whether str is PUBLISH */ - public static boolean isPublish(String str) { - return same(str, PUBLISH); - } - - /** Array of all methods ( standard (RFC3261) + new (RFC3428,..) ) */ - public static final String[] methods = { INVITE, ACK, CANCEL, BYE, INFO, - OPTION, REGISTER, UPDATE, SUBSCRIBE, NOTIFY, MESSAGE, REFER, - PUBLISH }; - - /** Array of all methods that create a dialog */ - public static final String[] dialog_methods = { INVITE, SUBSCRIBE }; -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.message; + +/** + * Class SipMethods extends org.zoolu.sip.message.BaseSipMethods and collects + * all SIP method names. + */ +public class SipMethods extends BaseSipMethods { + + // ****************************** Extensions + // *******************************/ + + /** String "SUBSCRIBE" */ + public static final String SUBSCRIBE = "SUBSCRIBE"; + + /** Whether str is SUBSCRIBE */ + public static boolean isSubscribe(String str) { + return same(str, SUBSCRIBE); + } + + /** String "NOTIFY" */ + public static final String NOTIFY = "NOTIFY"; + + /** Whether str is NOTIFY */ + public static boolean isNotify(String str) { + return same(str, NOTIFY); + } + + /** String "MESSAGE" for method MESSAGE defined in RFC3428 */ + public static final String MESSAGE = "MESSAGE"; + + /** Whether str is MESSAGE */ + public static boolean isMessage(String str) { + return same(str, MESSAGE); + } + + /** String "REFER" for method REFER defined in RFC3515 */ + public static final String REFER = "REFER"; + + /** Whether str is REFER */ + public static boolean isRefer(String str) { + return same(str, REFER); + } + + /** String "PUBLISH" for method PUBLISH defined in RFC3903 */ + public static final String PUBLISH = "PUBLISH"; + + /** Whether str is PUBLISH */ + public static boolean isPublish(String str) { + return same(str, PUBLISH); + } + + /** Array of all methods ( standard (RFC3261) + new (RFC3428,..) ) */ + public static final String[] methods = { INVITE, ACK, CANCEL, BYE, INFO, + OPTION, REGISTER, UPDATE, SUBSCRIBE, NOTIFY, MESSAGE, REFER, + PUBLISH }; + + /** Array of all methods that create a dialog */ + public static final String[] dialog_methods = { INVITE, SUBSCRIBE }; +} diff --git a/src/org/zoolu/sip/message/SipResponses.java b/app/src/main/java/org/zoolu/sip/message/SipResponses.java similarity index 96% rename from src/org/zoolu/sip/message/SipResponses.java rename to app/src/main/java/org/zoolu/sip/message/SipResponses.java index 35f0894..adf21de 100644 --- a/src/org/zoolu/sip/message/SipResponses.java +++ b/app/src/main/java/org/zoolu/sip/message/SipResponses.java @@ -1,61 +1,61 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.message; - -/** - * Class SipResponses provides all raeson-phrases corrspondent to the various - * SIP response codes - */ -public class SipResponses extends BaseSipResponses { - private static boolean is_init = false; - - public static void init() { - if (is_init) - return; - // else - - BaseSipResponses.init(); - - // New response codes - // reasons[xxx]="This Reason"; - // reasons[yyy]="A Second Reason"; - // .. - - // Success - reasons[202] = "Accepted"; - - // Failure - reasons[489] = "Bad Event"; - - is_init = true; - } - - /** Gets the reason phrase of a response code */ - public static String reasonOf(int code) { - if (!is_init) - init(); - return BaseSipResponses.reasonOf(code); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.message; + +/** + * Class SipResponses provides all raeson-phrases corrspondent to the various + * SIP response codes + */ +public class SipResponses extends BaseSipResponses { + private static boolean is_init = false; + + public static void init() { + if (is_init) + return; + // else + + BaseSipResponses.init(); + + // New response codes + // reasons[xxx]="This Reason"; + // reasons[yyy]="A Second Reason"; + // .. + + // Success + reasons[202] = "Accepted"; + + // Failure + reasons[489] = "Bad Event"; + + is_init = true; + } + + /** Gets the reason phrase of a response code */ + public static String reasonOf(int code) { + if (!is_init) + init(); + return BaseSipResponses.reasonOf(code); + } + +} diff --git a/src/org/zoolu/sip/provider/ConnectedTransport.java b/app/src/main/java/org/zoolu/sip/provider/ConnectedTransport.java similarity index 96% rename from src/org/zoolu/sip/provider/ConnectedTransport.java rename to app/src/main/java/org/zoolu/sip/provider/ConnectedTransport.java index 45b7d43..f15d05a 100644 --- a/src/org/zoolu/sip/provider/ConnectedTransport.java +++ b/app/src/main/java/org/zoolu/sip/provider/ConnectedTransport.java @@ -1,46 +1,46 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; -import org.zoolu.net.IpAddress; -import java.io.IOException; - -/** - * ConnectedTransport is a generic CO transport service for SIP. - */ -interface ConnectedTransport extends Transport { - /** Gets the remote IpAddress */ - public IpAddress getRemoteAddress(); - - /** Gets the remote port */ - public int getRemotePort(); - - /** Gets the last time the ConnectedTransport has been used (in millisconds) */ - public long getLastTimeMillis(); - - /** Sends a Message */ - public void sendMessage(Message msg) throws IOException; - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; +import org.zoolu.net.IpAddress; +import java.io.IOException; + +/** + * ConnectedTransport is a generic CO transport service for SIP. + */ +interface ConnectedTransport extends Transport { + /** Gets the remote IpAddress */ + public IpAddress getRemoteAddress(); + + /** Gets the remote port */ + public int getRemotePort(); + + /** Gets the last time the ConnectedTransport has been used (in millisconds) */ + public long getLastTimeMillis(); + + /** Sends a Message */ + public void sendMessage(Message msg) throws IOException; + +} diff --git a/src/org/zoolu/sip/provider/ConnectionIdentifier.java b/app/src/main/java/org/zoolu/sip/provider/ConnectionIdentifier.java similarity index 96% rename from src/org/zoolu/sip/provider/ConnectionIdentifier.java rename to app/src/main/java/org/zoolu/sip/provider/ConnectionIdentifier.java index 793b73c..5022e1c 100644 --- a/src/org/zoolu/sip/provider/ConnectionIdentifier.java +++ b/app/src/main/java/org/zoolu/sip/provider/ConnectionIdentifier.java @@ -1,60 +1,60 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.net.IpAddress; - -/** - * ConnectionIdentifier is the reference for active transport connections. - */ -public class ConnectionIdentifier extends Identifier { - /** Costructs a new ConnectionIdentifier. */ - public ConnectionIdentifier(String protocol, IpAddress remote_ipaddr, - int remote_port) { - super(getId(protocol, remote_ipaddr, remote_port)); - } - - /** Costructs a new ConnectionIdentifier. */ - public ConnectionIdentifier(ConnectionIdentifier conn_id) { - super(conn_id); - } - - /** Costructs a new ConnectionIdentifier. */ - public ConnectionIdentifier(String id) { - super(id); - } - - /** Costructs a new ConnectionIdentifier. */ - public ConnectionIdentifier(ConnectedTransport conn) { - super(getId(conn.getProtocol(), conn.getRemoteAddress(), conn - .getRemotePort())); - } - - /** Gets the id. */ - private static String getId(String protocol, IpAddress remote_ipaddr, - int remote_port) { - return protocol + ":" + remote_ipaddr + ":" + remote_port; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.net.IpAddress; + +/** + * ConnectionIdentifier is the reference for active transport connections. + */ +public class ConnectionIdentifier extends Identifier { + /** Costructs a new ConnectionIdentifier. */ + public ConnectionIdentifier(String protocol, IpAddress remote_ipaddr, + int remote_port) { + super(getId(protocol, remote_ipaddr, remote_port)); + } + + /** Costructs a new ConnectionIdentifier. */ + public ConnectionIdentifier(ConnectionIdentifier conn_id) { + super(conn_id); + } + + /** Costructs a new ConnectionIdentifier. */ + public ConnectionIdentifier(String id) { + super(id); + } + + /** Costructs a new ConnectionIdentifier. */ + public ConnectionIdentifier(ConnectedTransport conn) { + super(getId(conn.getProtocol(), conn.getRemoteAddress(), conn + .getRemotePort())); + } + + /** Gets the id. */ + private static String getId(String protocol, IpAddress remote_ipaddr, + int remote_port) { + return protocol + ":" + remote_ipaddr + ":" + remote_port; + } + +} diff --git a/src/org/zoolu/sip/provider/DialogIdentifier.java b/app/src/main/java/org/zoolu/sip/provider/DialogIdentifier.java similarity index 97% rename from src/org/zoolu/sip/provider/DialogIdentifier.java rename to app/src/main/java/org/zoolu/sip/provider/DialogIdentifier.java index fe6898e..56ac62d 100644 --- a/src/org/zoolu/sip/provider/DialogIdentifier.java +++ b/app/src/main/java/org/zoolu/sip/provider/DialogIdentifier.java @@ -1,39 +1,39 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -/** - * DialogIdentifier is used to address specific dialogs to the SipProvider. - */ -public class DialogIdentifier extends Identifier { - /** Costructs a new DialogIdentifier based on call-id, local and remote tags. */ - public DialogIdentifier(String call_id, String local_tag, String remote_tag) { - id = call_id + "-" + local_tag + "-" + remote_tag; - } - - /** Costructs a new DialogIdentifier. */ - public DialogIdentifier(DialogIdentifier i) { - super(i); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +/** + * DialogIdentifier is used to address specific dialogs to the SipProvider. + */ +public class DialogIdentifier extends Identifier { + /** Costructs a new DialogIdentifier based on call-id, local and remote tags. */ + public DialogIdentifier(String call_id, String local_tag, String remote_tag) { + id = call_id + "-" + local_tag + "-" + remote_tag; + } + + /** Costructs a new DialogIdentifier. */ + public DialogIdentifier(DialogIdentifier i) { + super(i); + } +} diff --git a/src/org/zoolu/sip/provider/Identifier.java b/app/src/main/java/org/zoolu/sip/provider/Identifier.java similarity index 96% rename from src/org/zoolu/sip/provider/Identifier.java rename to app/src/main/java/org/zoolu/sip/provider/Identifier.java index 5d42532..d652cdd 100644 --- a/src/org/zoolu/sip/provider/Identifier.java +++ b/app/src/main/java/org/zoolu/sip/provider/Identifier.java @@ -1,66 +1,66 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -/** - * Generic Identifier. - */ -public class Identifier { - /** The actual id */ - String id; - - /** Costructs a new void Identifier. */ - Identifier() { - } - - /** Costructs a new Identifier. */ - Identifier(String id) { - this.id = id; - } - - /** Costructs a new Identifier. */ - Identifier(Identifier i) { - this.id = i.id; - } - - /** Whether the Identifier equals to obj. */ - public boolean equals(Object obj) { - try { - Identifier i = (Identifier) obj; - return id.equals(i.id); - } catch (Exception e) { - return false; - } - } - - /** Gets an int hashCode for the Identifier. */ - public int hashCode() { - return id.hashCode(); - } - - /** Gets a String value for the Identifier */ - public String toString() { - return id; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +/** + * Generic Identifier. + */ +public class Identifier { + /** The actual id */ + String id; + + /** Costructs a new void Identifier. */ + Identifier() { + } + + /** Costructs a new Identifier. */ + Identifier(String id) { + this.id = id; + } + + /** Costructs a new Identifier. */ + Identifier(Identifier i) { + this.id = i.id; + } + + /** Whether the Identifier equals to obj. */ + public boolean equals(Object obj) { + try { + Identifier i = (Identifier) obj; + return id.equals(i.id); + } catch (Exception e) { + return false; + } + } + + /** Gets an int hashCode for the Identifier. */ + public int hashCode() { + return id.hashCode(); + } + + /** Gets a String value for the Identifier */ + public String toString() { + return id; + } +} diff --git a/src/org/zoolu/sip/provider/MethodIdentifier.java b/app/src/main/java/org/zoolu/sip/provider/MethodIdentifier.java similarity index 97% rename from src/org/zoolu/sip/provider/MethodIdentifier.java rename to app/src/main/java/org/zoolu/sip/provider/MethodIdentifier.java index 7378061..5ac37fa 100644 --- a/src/org/zoolu/sip/provider/MethodIdentifier.java +++ b/app/src/main/java/org/zoolu/sip/provider/MethodIdentifier.java @@ -1,39 +1,39 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -/** - * MethodIdentifier is used to address specific methods to the SipProvider. - */ -public class MethodIdentifier extends Identifier { - /** Costructs a new MethodIdentifier. */ - public MethodIdentifier(String method) { - super(method); - } - - /** Costructs a new MethodIdentifier. */ - public MethodIdentifier(MethodIdentifier i) { - super(i); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +/** + * MethodIdentifier is used to address specific methods to the SipProvider. + */ +public class MethodIdentifier extends Identifier { + /** Costructs a new MethodIdentifier. */ + public MethodIdentifier(String method) { + super(method); + } + + /** Costructs a new MethodIdentifier. */ + public MethodIdentifier(MethodIdentifier i) { + super(i); + } +} diff --git a/src/org/zoolu/sip/provider/SipInterface.java b/app/src/main/java/org/zoolu/sip/provider/SipInterface.java similarity index 97% rename from src/org/zoolu/sip/provider/SipInterface.java rename to app/src/main/java/org/zoolu/sip/provider/SipInterface.java index 6ac1b57..f2105e7 100644 --- a/src/org/zoolu/sip/provider/SipInterface.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipInterface.java @@ -1,161 +1,161 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; - -/** - * SipInterface is actually the SIP SAP (Service Access Point) and can be used - * to send and receive SIP messages associated with a specific method, - * transaction, or dialog.

SipInterface provides a simple interface to the - * multiplexing function provided by the SipProvider layer.
It simply - * wraps the SipProvider by adding and removing the listener for capturing - * received SIP messages.

When creating a new SipInterface the following - * parameters, have to be specified: - sip_provider is the - * SipProvider the SipInterface has to be bound to, - id is the SIP - * interface identifier the SipInterface has to be bound to, - listener - * is the SipInterfaceListener that received messages are passed to.

The - * SIP interface id specifies the type of messages the listener is going - * to receive for. Together with the sip_provider, it represents the - * complete SIP Service Access Point (SAP) address/identifier used for - * demultiplexing SIP messages at receiving side.

The identifier can be of - * one of the three following types: transaction_id, dialog_id, or method_id. - * These types of identifiers characterize respectively:
- messages within - * a specific transaction,
- messages within a specific dialog,
- - * messages related to a specific SIP method. It is also possible to use the the - * identifier ANY to specify
- all messages that are out of any - * transactions, dialogs, or already specified method types. - *

- * When receiving a message, the underling SipProvider first tries to look for a - * SipInterface associated to the corresponding transaction, then looks for a - * SipInterface associated to the corresponding dialog, then for a SipInterface - * associated to the corresponding method type, and finally for a SipInterface - * associated to ANY messages. If the present SipInterface id matches, the - * SipInterfaceListener method onReceivedMessage() is fired. - */ -public class SipInterface implements SipProviderListener { - - /** SipProvider */ - SipProvider sip_provider; - - /** Identifier */ - Identifier id; - - /** SipInterfaceListener */ - SipInterfaceListener listener; - - // *************************** Costructors *************************** - - /** Creates a new SipInterface. */ - public SipInterface(SipProvider sip_provider, SipInterfaceListener listener) { - this.sip_provider = sip_provider; - this.listener = listener; - id = SipProvider.ANY; - sip_provider.addSipProviderListener(id, this); - } - - /** Creates a new SipInterface. */ - public SipInterface(SipProvider sip_provider, Identifier id, - SipInterfaceListener listener) { - this.sip_provider = sip_provider; - this.listener = listener; - this.id = id; - sip_provider.addSipProviderListener(id, this); - } - - // ************************** Public methods ************************* - - /** Close the SipInterface. */ - public void close() { - sip_provider.removeSipProviderListener(id); - } - - /** Gets the SipProvider. */ - public SipProvider getSipProvider() { - return sip_provider; - } - - /** - * Sends a Message, specifing the transport portocol, nexthop address and - * port. - *

- * This is a low level method and forces the message to be routed to a - * specific nexthop address, port and transport, regardless whatever the - * Via, Route, or request-uri, address to. - *

- * In case of connection-oriented transport, the connection is selected as - * follows:
- if an existing connection is found matching the - * destination end point (socket), such connection is used, otherwise
- - * a new connection is established - * - * @return It returns a Connection in case of connection-oriented delivery - * (e.g. TCP) or null in case of connection-less delivery (e.g. UDP) - */ - public ConnectionIdentifier sendMessage(Message msg, String proto, - String dest_addr, int dest_port, int ttl) { - return sip_provider.sendMessage(msg, proto, dest_addr, dest_port, ttl); - } - - /** - * Sends the message msg. - *

- * The destination for the request is computed as follows:
- if - * outbound_addr is set, outbound_addr and outbound_port - * are used, otherwise
- if message has Route header with lr option - * parameter (i.e. RFC3261 compliant), the first Route address is used, - * otherwise
- the request's Request-URI is considered. - *

- * The destination for the response is computed based on the sent-by - * parameter in the Via header field (RFC3261 compliant) - *

- * As transport it is used the protocol specified in the 'via' header field - *

- * In case of connection-oriented transport:
- if an already - * established connection is found matching the destination end point - * (socket), such connection is used, otherwise
- a new connection is - * established - * - * @return Returns a ConnectionIdentifier in case of connection-oriented - * delivery (e.g. TCP) or null in case of connection-less delivery - * (e.g. UDP) - */ - public ConnectionIdentifier sendMessage(Message msg) { - return sip_provider.sendMessage(msg); - } - - /** Sends the message msg using the specified connection. */ - public ConnectionIdentifier sendMessage(Message msg, - ConnectionIdentifier conn_id) { - return sip_provider.sendMessage(msg, conn_id); - } - - // ************************* Callback methods ************************* - - /** When a new Message is received by the SipProvider. */ - public void onReceivedMessage(SipProvider sip_provider, Message message) { - if (listener != null) - listener.onReceivedMessage(this, message); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; + +/** + * SipInterface is actually the SIP SAP (Service Access Point) and can be used + * to send and receive SIP messages associated with a specific method, + * transaction, or dialog.

SipInterface provides a simple interface to the + * multiplexing function provided by the SipProvider layer.
It simply + * wraps the SipProvider by adding and removing the listener for capturing + * received SIP messages.

When creating a new SipInterface the following + * parameters, have to be specified: - sip_provider is the + * SipProvider the SipInterface has to be bound to, - id is the SIP + * interface identifier the SipInterface has to be bound to, - listener + * is the SipInterfaceListener that received messages are passed to.

The + * SIP interface id specifies the type of messages the listener is going + * to receive for. Together with the sip_provider, it represents the + * complete SIP Service Access Point (SAP) address/identifier used for + * demultiplexing SIP messages at receiving side.

The identifier can be of + * one of the three following types: transaction_id, dialog_id, or method_id. + * These types of identifiers characterize respectively:
- messages within + * a specific transaction,
- messages within a specific dialog,
- + * messages related to a specific SIP method. It is also possible to use the the + * identifier ANY to specify
- all messages that are out of any + * transactions, dialogs, or already specified method types. + *

+ * When receiving a message, the underling SipProvider first tries to look for a + * SipInterface associated to the corresponding transaction, then looks for a + * SipInterface associated to the corresponding dialog, then for a SipInterface + * associated to the corresponding method type, and finally for a SipInterface + * associated to ANY messages. If the present SipInterface id matches, the + * SipInterfaceListener method onReceivedMessage() is fired. + */ +public class SipInterface implements SipProviderListener { + + /** SipProvider */ + SipProvider sip_provider; + + /** Identifier */ + Identifier id; + + /** SipInterfaceListener */ + SipInterfaceListener listener; + + // *************************** Costructors *************************** + + /** Creates a new SipInterface. */ + public SipInterface(SipProvider sip_provider, SipInterfaceListener listener) { + this.sip_provider = sip_provider; + this.listener = listener; + id = SipProvider.ANY; + sip_provider.addSipProviderListener(id, this); + } + + /** Creates a new SipInterface. */ + public SipInterface(SipProvider sip_provider, Identifier id, + SipInterfaceListener listener) { + this.sip_provider = sip_provider; + this.listener = listener; + this.id = id; + sip_provider.addSipProviderListener(id, this); + } + + // ************************** Public methods ************************* + + /** Close the SipInterface. */ + public void close() { + sip_provider.removeSipProviderListener(id); + } + + /** Gets the SipProvider. */ + public SipProvider getSipProvider() { + return sip_provider; + } + + /** + * Sends a Message, specifing the transport portocol, nexthop address and + * port. + *

+ * This is a low level method and forces the message to be routed to a + * specific nexthop address, port and transport, regardless whatever the + * Via, Route, or request-uri, address to. + *

+ * In case of connection-oriented transport, the connection is selected as + * follows:
- if an existing connection is found matching the + * destination end point (socket), such connection is used, otherwise
- + * a new connection is established + * + * @return It returns a Connection in case of connection-oriented delivery + * (e.g. TCP) or null in case of connection-less delivery (e.g. UDP) + */ + public ConnectionIdentifier sendMessage(Message msg, String proto, + String dest_addr, int dest_port, int ttl) { + return sip_provider.sendMessage(msg, proto, dest_addr, dest_port, ttl); + } + + /** + * Sends the message msg. + *

+ * The destination for the request is computed as follows:
- if + * outbound_addr is set, outbound_addr and outbound_port + * are used, otherwise
- if message has Route header with lr option + * parameter (i.e. RFC3261 compliant), the first Route address is used, + * otherwise
- the request's Request-URI is considered. + *

+ * The destination for the response is computed based on the sent-by + * parameter in the Via header field (RFC3261 compliant) + *

+ * As transport it is used the protocol specified in the 'via' header field + *

+ * In case of connection-oriented transport:
- if an already + * established connection is found matching the destination end point + * (socket), such connection is used, otherwise
- a new connection is + * established + * + * @return Returns a ConnectionIdentifier in case of connection-oriented + * delivery (e.g. TCP) or null in case of connection-less delivery + * (e.g. UDP) + */ + public ConnectionIdentifier sendMessage(Message msg) { + return sip_provider.sendMessage(msg); + } + + /** Sends the message msg using the specified connection. */ + public ConnectionIdentifier sendMessage(Message msg, + ConnectionIdentifier conn_id) { + return sip_provider.sendMessage(msg, conn_id); + } + + // ************************* Callback methods ************************* + + /** When a new Message is received by the SipProvider. */ + public void onReceivedMessage(SipProvider sip_provider, Message message) { + if (listener != null) + listener.onReceivedMessage(this, message); + } + +} diff --git a/src/org/zoolu/sip/provider/SipInterfaceListener.java b/app/src/main/java/org/zoolu/sip/provider/SipInterfaceListener.java similarity index 97% rename from src/org/zoolu/sip/provider/SipInterfaceListener.java rename to app/src/main/java/org/zoolu/sip/provider/SipInterfaceListener.java index a5ed444..67f2259 100644 --- a/src/org/zoolu/sip/provider/SipInterfaceListener.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipInterfaceListener.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; - -/** - * A SipInterfaceListener listens for SipInterface - * onReceivedMessage(SipInterfaceListener,Message) events. - */ -public interface SipInterfaceListener { - /** When a new Message is received by the SipInterface. */ - public void onReceivedMessage(SipInterface sip, Message message); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; + +/** + * A SipInterfaceListener listens for SipInterface + * onReceivedMessage(SipInterfaceListener,Message) events. + */ +public interface SipInterfaceListener { + /** When a new Message is received by the SipInterface. */ + public void onReceivedMessage(SipInterface sip, Message message); +} diff --git a/src/org/zoolu/sip/provider/SipParser.java b/app/src/main/java/org/zoolu/sip/provider/SipParser.java similarity index 96% rename from src/org/zoolu/sip/provider/SipParser.java rename to app/src/main/java/org/zoolu/sip/provider/SipParser.java index 1d3be7c..e7a9b8b 100644 --- a/src/org/zoolu/sip/provider/SipParser.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipParser.java @@ -1,480 +1,480 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.address.*; -import org.zoolu.sip.header.Header; -import org.zoolu.sip.header.RequestLine; -import org.zoolu.sip.header.StatusLine; -import org.zoolu.sip.message.Message; -import org.zoolu.tools.DateFormat; -import java.util.Vector; -import java.util.Date; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.zoolu.tools.Parser; - -/** - * Class SipParser extends class Parser for parsing of SIP messages. - */ -public class SipParser extends Parser { - /** Creates a new SipParser based on String s */ - public SipParser(String s) { - super(s); - } - - /** - * Creates a new SipParser based on String s and starting from - * position i - */ - public SipParser(String s, int i) { - super(s, i); - } - - /** Creates a new SipParser based on StringBuffer sb */ - public SipParser(StringBuffer sb) { - super(sb); - } - - /** - * Creates a new SipParser based on StringBuffer sb and starting - * from position i - */ - public SipParser(StringBuffer sb, int i) { - super(sb, i); - } - - /** Creates a new SipParser starting from the current position. */ - public SipParser(Parser p) { - super(p.getWholeString(), p.getPos()); - } - - /** MARK char[], composed by: '-' , '_' , '.' , '!' , '~' , '*' , '\'' , '|' */ - public static char[] MARK = { '-', '_', '.', '!', '~', '*', '\'', '|' }; - - /** SEPARATOR char[], composed by: ' ','\t','\r','\n','(',')','<','>',',',';','\\','"','/','[',']','?','=','{','}' */ - public static char[] SEPARATOR = { ' ', '\t', '\r', '\n', '(', ')', '<', - '>', ',', ';', '\\', '"', '/', '[', ']', '?', '=', '{', '}' }; - - /** Checks whether a char is any MARK */ - public static boolean isMark(char c) { // return (c=='-' || c=='_' || - // c=='.' || c=='!' || c=='~' || - // c=='*' || c=='\'' || c=='|'); - return isAnyOf(MARK, c); - } - - /** Unreserved char; that is an alphanum or a mark */ - public static boolean isUnreserved(char c) { - return (isAlphanum(c) || isMark(c)); - } - - /** - * Separator; differently form RFC2543, do not include '@' and ':', while - * include '\r' and '\n' - */ - public static boolean isSeparator(char c) { // return (isSpace(c) || - // isCRLF(c) || c=='(' || c==')' - // || c=='<' || c=='>' || c==',' - // || c==';' || c=='\\' || - // c=='"' || c=='/' || c=='[' || - // c==']' || c=='?' || c=='=' || - // c=='{' || c=='}'); - return isAnyOf(SEPARATOR, c); - } - - /** Returns the first occurence of a separator or the end of the string */ - public int indexOfSeparator() { - int begin = index; - while (begin < str.length() && !isSeparator(str.charAt(begin))) - begin++; - return begin; - } - - /** Index of the end of the header (EOH) */ - public int indexOfEOH() { - SipParser par = new SipParser(str, index); - while (true) { - par.goTo(CRLF); // find the first CR or LF - if (!par.hasMore()) - return str.length(); // if no CR/LF found return the end of - // string - int end = par.getPos(); - par.goToNextLine(); // skip the CR/LF chars - if (!par.hasMore() || !isWSP(par.nextChar())) - return end; - } - } - - /** Returns the begin of next header */ - public int indexOfNextHeader() { - SipParser par = new SipParser(str, index); - par.goToNextHeader(); - return par.getPos(); - } - - /** - * Returns the index of the begin of the first occurence of the Header - * hname - */ - public int indexOfHeader(String hname) { - Pattern p = Pattern.compile("^" + hname + ": ", - Pattern.CASE_INSENSITIVE - | Pattern.MULTILINE); - Matcher m = p.matcher(str); - if (m.find(index)) - return m.start(); - return str.length(); - } - - /** Goes to the begin of next header */ - public SipParser goToNextHeader() { - index = indexOfEOH(); - goToNextLine(); - return this; - } - - /** - * Go to the end of the last header. The final empty line delimiter is not - * considered as header - */ - public SipParser goToEndOfLastHeader() { - String[] delimiters = { "\r\n\r\n", "\n\n" }; // double newline - goTo(delimiters); - if (!hasMore()) // no double newline found - { - if (str.startsWith("\r\n", str.length() - 2)) - index = str.length() - 2; - else if (str.charAt(str.length() - 1) == '\n') - index = str.length() - 1; - else - index = str.length(); - } - return this; - } - - /** Go to the begin (first char of) Message Body */ - public SipParser goToBody() { - goToEndOfLastHeader(); - goTo('\n').skipChar(); - goTo('\n').skipChar(); - return this; - } - - /** Returns the first header and goes to the next line. */ - public Header getHeader() { - if (!hasMore()) - return null; - int begin = getPos(); - int end = indexOfEOH(); - String header_str = getString(end - begin); - goToNextLine(); - int colon = header_str.indexOf(':'); - if (colon < 0) - return null; - String hname = header_str.substring(0, colon).trim(); - String hvalue = header_str.substring(++colon).trim(); - return new Header(hname, hvalue); - } - - /** Returns the first occurence of Header hname. */ - public Header getHeader(String hname) { - SipParser par = new SipParser(str, indexOfHeader(hname)); - if (!par.hasMore()) - return null; - par.skipN(hname.length()); - int begin = par.indexOf(':') + 1; - int end = par.indexOfEOH(); - if (begin > end) - return null; - String hvalue = str.substring(begin, end).trim(); - index = end; - return new Header(hname, hvalue); - } - - // ************************ first-line ************************ - - /** Returns the request-line. */ - public RequestLine getRequestLine() { - String method = getString(); - skipWSP(); - int begin = getPos(); - int end = indexOfEOH(); - String request_uri = getString(end - begin); - goToNextLine(); - return new RequestLine(method, (new SipParser(request_uri)).getSipURL()); - } - - /** Returns the status-line or null (if it doesn't start with "SIP/"). */ - public StatusLine getStatusLine() { - String version = getString(4); - if (!version.equalsIgnoreCase("SIP/")) { - index = str.length(); - return null; - } - skipString().skipWSP(); // "SIP/2.0 " - int code = getInt(); - int begin = getPos(); - int end = indexOfEOH(); - String reason = getString(end - begin).trim(); - goToNextLine(); - return new StatusLine(code, reason); - } - - // *************************** URIs *************************** - - public static char[] uri_separators = { ' ', '>', '\n', '\r' }; - - /** - * Returns the first URL. If no URL is found, it returns null - */ - public SipURL getSipURL() { - goTo("sip:"); - if (!hasMore()) - return null; - int begin = getPos(); - int end = indexOf(uri_separators); - if (end < 0) - end = str.length(); - String url = getString(end - begin); - if (hasMore()) - skipChar(); - return new SipURL(url); - } - - /* - * public static SipURL parseSipURL(String s) { SipParser par=new - * SipParser(s); return par.parseSipURL(); } - */ - /** - * Returns the first NameAddress in the string str. If no - * NameAddress is found, it returns null. A NameAddress is a string - * of the form of:
- *

- * - *
-	 * &nbsp&nbsp "user's name" <sip url>
-	 * 
- * - *
- */ - public NameAddress getNameAddress() { - String text = null; - SipURL url = null; - int begin = getPos(); - int begin_url = indexOf("str; if no - * NameAddress is found, it returns null - */ - /* - * public String parseQuotedString() { int begin=str.indexOf('\"',index); if - * (begin<0) return null; begin++; int end=str.indexOf('\"',begin); String - * quotedtext=str.substring(begin,end); index=end; return quotedtext; } - */ - - // *************************** DATE *************************** - /** Returns a Date object according with the SIP standard date format */ - public Date getDate() { - // DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss - // 'GMT'",Locale.US); - try { // Date d=df.parse(str,new ParsePosition(index)); - Date d = DateFormat.parseEEEddMMM(str, index); - index = str.indexOf("GMT", index) + 3; - return d; - } catch (Exception e) { - e.printStackTrace(); - index = str.length(); - return null; - } - } - - // *************************** PARAMETERS *************************** - - public static char[] param_separators = { ' ', '=', ';', ',', '\n', '\r' }; - - /** - * Gets the value of specified parameter. - * - * @returns the parameter value or null if parameter does not exist or - * doesn't have a value (i.e. in case of flag parameter). - */ - public String getParameter(String name) { - while (hasMore()) { - if (getWord(param_separators).equals(name)) { - skipWSP(); - if (nextChar() == '=') { - skipChar(); - return getWordSkippingQuoted(param_separators); - } else - return null; - } - goToSkippingQuoted(';'); - if (hasMore()) - skipChar(); // skip ';' - } - return null; - } - - /** - * Gets a String Vector of parameter names.
- * Returns null if no parameter is present - */ - public Vector getParameters() { - String name; - Vector params = new Vector(); - while (hasMore()) { - name = getWord(param_separators); - if (name.length() > 0) - params.addElement(new String(name)); - goToSkippingQuoted(';'); - if (hasMore()) - skipChar(); // skip ';' - } - return params; - } - - /** Whether there is the specified parameter */ - public boolean hasParameter(String name) { - while (hasMore()) { - if (getWord(param_separators).equals(name)) - return true; - goToSkippingQuoted(';'); - if (hasMore()) - skipChar(); // skip ';' - } - return false; - } - - // ************************ MULTIPLE HEADERS ************************ - - /** Finds the first comma-separator. Return -1 if no comma is found. */ - public int indexOfCommaHeaderSeparator() { - boolean inside_quoted_string = false; - for (int i = index; i < str.length(); i++) { - char c = str.charAt(i); - if (c == '"') - inside_quoted_string = !inside_quoted_string; - if (!inside_quoted_string && c == ',') - return i; - } - return -1; - } - - /** - * Goes to the first comma-separator. Goes to the end of string if no comma - * is found. - */ - public SipParser goToCommaHeaderSeparator() { - int comma = indexOfCommaHeaderSeparator(); - if (comma < 0) - index = str.length(); - else - index = comma; - return this; - } - - // ************************** SIP MESSAGE *************************** - - /** - * Gets the first SIP message (all bytes until the first end of SIP - * message), if a SIP message delimiter is found. - *

- * The message begins from the first non-CRLF char. - */ - public Message getSipMessage() { // skip any CRLF sequence - skipCRLF(); - // Get content length; if no Content-Length header found return null - String text; - if (getPos() == 0) - text = str; - else - text = getRemainingString(); - Message msg = new Message(text); - if (!msg.hasContentLengthHeader()) - return null; - int body_len = msg.getContentLengthHeader().getContentLength(); - - // gets the message (and go ahead), or returns null - int begin = getPos(); - goToEndOfLastHeader(); - if (!hasMore()) - return null; - goTo('\n'); - if (!hasMore()) - return null; - skipChar().goTo('\n'); // skip the LF of last header and go the the new - // line - if (!hasMore()) - return null; - int body_pos = skipChar().getPos(); // skip the LF of the empty line and - // go the the body - - int end = body_pos + body_len; - if (end <= str.length()) { - index = end; - return new Message(str.substring(begin, end)); - } else - return null; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.address.*; +import org.zoolu.sip.header.Header; +import org.zoolu.sip.header.RequestLine; +import org.zoolu.sip.header.StatusLine; +import org.zoolu.sip.message.Message; +import org.zoolu.tools.DateFormat; +import java.util.Vector; +import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.zoolu.tools.Parser; + +/** + * Class SipParser extends class Parser for parsing of SIP messages. + */ +public class SipParser extends Parser { + /** Creates a new SipParser based on String s */ + public SipParser(String s) { + super(s); + } + + /** + * Creates a new SipParser based on String s and starting from + * position i + */ + public SipParser(String s, int i) { + super(s, i); + } + + /** Creates a new SipParser based on StringBuffer sb */ + public SipParser(StringBuffer sb) { + super(sb); + } + + /** + * Creates a new SipParser based on StringBuffer sb and starting + * from position i + */ + public SipParser(StringBuffer sb, int i) { + super(sb, i); + } + + /** Creates a new SipParser starting from the current position. */ + public SipParser(Parser p) { + super(p.getWholeString(), p.getPos()); + } + + /** MARK char[], composed by: '-' , '_' , '.' , '!' , '~' , '*' , '\'' , '|' */ + public static char[] MARK = { '-', '_', '.', '!', '~', '*', '\'', '|' }; + + /** SEPARATOR char[], composed by: ' ','\t','\r','\n','(',')','<','>',',',';','\\','"','/','[',']','?','=','{','}' */ + public static char[] SEPARATOR = { ' ', '\t', '\r', '\n', '(', ')', '<', + '>', ',', ';', '\\', '"', '/', '[', ']', '?', '=', '{', '}' }; + + /** Checks whether a char is any MARK */ + public static boolean isMark(char c) { // return (c=='-' || c=='_' || + // c=='.' || c=='!' || c=='~' || + // c=='*' || c=='\'' || c=='|'); + return isAnyOf(MARK, c); + } + + /** Unreserved char; that is an alphanum or a mark */ + public static boolean isUnreserved(char c) { + return (isAlphanum(c) || isMark(c)); + } + + /** + * Separator; differently form RFC2543, do not include '@' and ':', while + * include '\r' and '\n' + */ + public static boolean isSeparator(char c) { // return (isSpace(c) || + // isCRLF(c) || c=='(' || c==')' + // || c=='<' || c=='>' || c==',' + // || c==';' || c=='\\' || + // c=='"' || c=='/' || c=='[' || + // c==']' || c=='?' || c=='=' || + // c=='{' || c=='}'); + return isAnyOf(SEPARATOR, c); + } + + /** Returns the first occurence of a separator or the end of the string */ + public int indexOfSeparator() { + int begin = index; + while (begin < str.length() && !isSeparator(str.charAt(begin))) + begin++; + return begin; + } + + /** Index of the end of the header (EOH) */ + public int indexOfEOH() { + SipParser par = new SipParser(str, index); + while (true) { + par.goTo(CRLF); // find the first CR or LF + if (!par.hasMore()) + return str.length(); // if no CR/LF found return the end of + // string + int end = par.getPos(); + par.goToNextLine(); // skip the CR/LF chars + if (!par.hasMore() || !isWSP(par.nextChar())) + return end; + } + } + + /** Returns the begin of next header */ + public int indexOfNextHeader() { + SipParser par = new SipParser(str, index); + par.goToNextHeader(); + return par.getPos(); + } + + /** + * Returns the index of the begin of the first occurence of the Header + * hname + */ + public int indexOfHeader(String hname) { + Pattern p = Pattern.compile("^" + hname + ": ", + Pattern.CASE_INSENSITIVE + | Pattern.MULTILINE); + Matcher m = p.matcher(str); + if (m.find(index)) + return m.start(); + return str.length(); + } + + /** Goes to the begin of next header */ + public SipParser goToNextHeader() { + index = indexOfEOH(); + goToNextLine(); + return this; + } + + /** + * Go to the end of the last header. The final empty line delimiter is not + * considered as header + */ + public SipParser goToEndOfLastHeader() { + String[] delimiters = { "\r\n\r\n", "\n\n" }; // double newline + goTo(delimiters); + if (!hasMore()) // no double newline found + { + if (str.startsWith("\r\n", str.length() - 2)) + index = str.length() - 2; + else if (str.charAt(str.length() - 1) == '\n') + index = str.length() - 1; + else + index = str.length(); + } + return this; + } + + /** Go to the begin (first char of) Message Body */ + public SipParser goToBody() { + goToEndOfLastHeader(); + goTo('\n').skipChar(); + goTo('\n').skipChar(); + return this; + } + + /** Returns the first header and goes to the next line. */ + public Header getHeader() { + if (!hasMore()) + return null; + int begin = getPos(); + int end = indexOfEOH(); + String header_str = getString(end - begin); + goToNextLine(); + int colon = header_str.indexOf(':'); + if (colon < 0) + return null; + String hname = header_str.substring(0, colon).trim(); + String hvalue = header_str.substring(++colon).trim(); + return new Header(hname, hvalue); + } + + /** Returns the first occurence of Header hname. */ + public Header getHeader(String hname) { + SipParser par = new SipParser(str, indexOfHeader(hname)); + if (!par.hasMore()) + return null; + par.skipN(hname.length()); + int begin = par.indexOf(':') + 1; + int end = par.indexOfEOH(); + if (begin > end) + return null; + String hvalue = str.substring(begin, end).trim(); + index = end; + return new Header(hname, hvalue); + } + + // ************************ first-line ************************ + + /** Returns the request-line. */ + public RequestLine getRequestLine() { + String method = getString(); + skipWSP(); + int begin = getPos(); + int end = indexOfEOH(); + String request_uri = getString(end - begin); + goToNextLine(); + return new RequestLine(method, (new SipParser(request_uri)).getSipURL()); + } + + /** Returns the status-line or null (if it doesn't start with "SIP/"). */ + public StatusLine getStatusLine() { + String version = getString(4); + if (!version.equalsIgnoreCase("SIP/")) { + index = str.length(); + return null; + } + skipString().skipWSP(); // "SIP/2.0 " + int code = getInt(); + int begin = getPos(); + int end = indexOfEOH(); + String reason = getString(end - begin).trim(); + goToNextLine(); + return new StatusLine(code, reason); + } + + // *************************** URIs *************************** + + public static char[] uri_separators = { ' ', '>', '\n', '\r' }; + + /** + * Returns the first URL. If no URL is found, it returns null + */ + public SipURL getSipURL() { + goTo("sip:"); + if (!hasMore()) + return null; + int begin = getPos(); + int end = indexOf(uri_separators); + if (end < 0) + end = str.length(); + String url = getString(end - begin); + if (hasMore()) + skipChar(); + return new SipURL(url); + } + + /* + * public static SipURL parseSipURL(String s) { SipParser par=new + * SipParser(s); return par.parseSipURL(); } + */ + /** + * Returns the first NameAddress in the string str. If no + * NameAddress is found, it returns null. A NameAddress is a string + * of the form of:
+ *

+ * + *
+	 * &nbsp&nbsp "user's name" <sip url>
+	 * 
+ * + *
+ */ + public NameAddress getNameAddress() { + String text = null; + SipURL url = null; + int begin = getPos(); + int begin_url = indexOf("str
; if no + * NameAddress is found, it returns null + */ + /* + * public String parseQuotedString() { int begin=str.indexOf('\"',index); if + * (begin<0) return null; begin++; int end=str.indexOf('\"',begin); String + * quotedtext=str.substring(begin,end); index=end; return quotedtext; } + */ + + // *************************** DATE *************************** + /** Returns a Date object according with the SIP standard date format */ + public Date getDate() { + // DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss + // 'GMT'",Locale.US); + try { // Date d=df.parse(str,new ParsePosition(index)); + Date d = DateFormat.parseEEEddMMM(str, index); + index = str.indexOf("GMT", index) + 3; + return d; + } catch (Exception e) { + e.printStackTrace(); + index = str.length(); + return null; + } + } + + // *************************** PARAMETERS *************************** + + public static char[] param_separators = { ' ', '=', ';', ',', '\n', '\r' }; + + /** + * Gets the value of specified parameter. + * + * @returns the parameter value or null if parameter does not exist or + * doesn't have a value (i.e. in case of flag parameter). + */ + public String getParameter(String name) { + while (hasMore()) { + if (getWord(param_separators).equals(name)) { + skipWSP(); + if (nextChar() == '=') { + skipChar(); + return getWordSkippingQuoted(param_separators); + } else + return null; + } + goToSkippingQuoted(';'); + if (hasMore()) + skipChar(); // skip ';' + } + return null; + } + + /** + * Gets a String Vector of parameter names.
+ * Returns null if no parameter is present + */ + public Vector getParameters() { + String name; + Vector params = new Vector(); + while (hasMore()) { + name = getWord(param_separators); + if (name.length() > 0) + params.addElement(new String(name)); + goToSkippingQuoted(';'); + if (hasMore()) + skipChar(); // skip ';' + } + return params; + } + + /** Whether there is the specified parameter */ + public boolean hasParameter(String name) { + while (hasMore()) { + if (getWord(param_separators).equals(name)) + return true; + goToSkippingQuoted(';'); + if (hasMore()) + skipChar(); // skip ';' + } + return false; + } + + // ************************ MULTIPLE HEADERS ************************ + + /** Finds the first comma-separator. Return -1 if no comma is found. */ + public int indexOfCommaHeaderSeparator() { + boolean inside_quoted_string = false; + for (int i = index; i < str.length(); i++) { + char c = str.charAt(i); + if (c == '"') + inside_quoted_string = !inside_quoted_string; + if (!inside_quoted_string && c == ',') + return i; + } + return -1; + } + + /** + * Goes to the first comma-separator. Goes to the end of string if no comma + * is found. + */ + public SipParser goToCommaHeaderSeparator() { + int comma = indexOfCommaHeaderSeparator(); + if (comma < 0) + index = str.length(); + else + index = comma; + return this; + } + + // ************************** SIP MESSAGE *************************** + + /** + * Gets the first SIP message (all bytes until the first end of SIP + * message), if a SIP message delimiter is found. + *

+ * The message begins from the first non-CRLF char. + */ + public Message getSipMessage() { // skip any CRLF sequence + skipCRLF(); + // Get content length; if no Content-Length header found return null + String text; + if (getPos() == 0) + text = str; + else + text = getRemainingString(); + Message msg = new Message(text); + if (!msg.hasContentLengthHeader()) + return null; + int body_len = msg.getContentLengthHeader().getContentLength(); + + // gets the message (and go ahead), or returns null + int begin = getPos(); + goToEndOfLastHeader(); + if (!hasMore()) + return null; + goTo('\n'); + if (!hasMore()) + return null; + skipChar().goTo('\n'); // skip the LF of last header and go the the new + // line + if (!hasMore()) + return null; + int body_pos = skipChar().getPos(); // skip the LF of the empty line and + // go the the body + + int end = body_pos + body_len; + if (end <= str.length()) { + index = end; + return new Message(str.substring(begin, end)); + } else + return null; + } +} diff --git a/src/org/zoolu/sip/provider/SipPromisqueInterface.java b/app/src/main/java/org/zoolu/sip/provider/SipPromisqueInterface.java similarity index 97% rename from src/org/zoolu/sip/provider/SipPromisqueInterface.java rename to app/src/main/java/org/zoolu/sip/provider/SipPromisqueInterface.java index d0e271f..28085f8 100644 --- a/src/org/zoolu/sip/provider/SipPromisqueInterface.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipPromisqueInterface.java @@ -1,38 +1,38 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -/** - * SipPromisqueInterface is the SipInterface for capturing all SIP messages in - * PROMISQUE mode. All incoming messages are passed to the listener associated - * to the SipPromisqueInterface regardless of any other opened SipInterface. - *

More than one SipPromisqueInterface can be open concurrently. - */ -public class SipPromisqueInterface extends SipInterface { - /** Creates a new SipPromisqueInterface. */ - public SipPromisqueInterface(SipProvider sip_provider, - SipInterfaceListener listener) { - super(sip_provider, SipProvider.PROMISQUE, listener); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +/** + * SipPromisqueInterface is the SipInterface for capturing all SIP messages in + * PROMISQUE mode. All incoming messages are passed to the listener associated + * to the SipPromisqueInterface regardless of any other opened SipInterface. + *

More than one SipPromisqueInterface can be open concurrently. + */ +public class SipPromisqueInterface extends SipInterface { + /** Creates a new SipPromisqueInterface. */ + public SipPromisqueInterface(SipProvider sip_provider, + SipInterfaceListener listener) { + super(sip_provider, SipProvider.PROMISQUE, listener); + } +} diff --git a/src/org/zoolu/sip/provider/SipProvider.java b/app/src/main/java/org/zoolu/sip/provider/SipProvider.java similarity index 97% rename from src/org/zoolu/sip/provider/SipProvider.java rename to app/src/main/java/org/zoolu/sip/provider/SipProvider.java index fa387b5..3780246 100644 --- a/src/org/zoolu/sip/provider/SipProvider.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipProvider.java @@ -1,1421 +1,1421 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.sip.provider; - -import org.sipdroid.sipua.ui.Receiver; -import org.sipdroid.sipua.ui.Sipdroid; -import org.zoolu.net.*; -import org.zoolu.sip.header.*; -import org.zoolu.sip.message.Message; -import org.zoolu.sip.address.*; -import org.zoolu.tools.Configure; -import org.zoolu.tools.Configurable; -import org.zoolu.tools.Parser; -import org.zoolu.tools.Random; -import org.zoolu.tools.Log; -import org.zoolu.tools.LogLevel; -import org.zoolu.tools.RotatingLog; -import org.zoolu.tools.SimpleDigest; -import org.zoolu.tools.DateFormat; - -import java.util.Hashtable; -import java.io.IOException; -import org.zoolu.tools.HashSet; -import org.zoolu.tools.Iterator; - -import android.content.Context; -import android.os.PowerManager; - -import java.util.Enumeration; -import java.util.Date; - -/** - * SipProvider implements the SIP transport layer, that is the layer responsable - * for sending and receiving SIP messages. Messages are received by the callback - * function defined in the interface SipProviderListener. - *

- * SipProvider implements also multiplexing/demultiplexing service through the - * use of SIP interface identifiers and onReceivedMessage() callback - * function of specific SipProviderListener. - *

- * A SipProviderListener can be added to a SipProvider through the - * addSipProviderListener(id,listener) method, where: - id is the - * SIP interface identifier the listener has to be bound to, - listener - * is the SipProviderListener that received messages are passed to.

The SIP - * interface identifier specifies the type of messages the listener is going to - * receive for. Together with the specific SipProvider, it represents the - * complete SIP Service Access Point (SAP) address/identifier used for - * demultiplexing SIP messages at receiving side.

The identifier can be of - * one of the three following types: transaction_id, dialog_id, or method_id. - * These types of identifiers characterize respectively:
- messages within - * a specific transaction,
- messages within a specific dialog,
- - * messages related to a specific SIP method. It is also possible to use the the - * identifier ANY to specify
- all messages that are out of any - * transactions, dialogs, or already specified method types. - *

- * When receiving a message, the SipProvider first tries to look for a matching - * transaction, then looks for a matching dialog, then for a matching method - * type, and finally for a default listener (i.e. that with identifier ANY). For - * the matched SipProviderListener, the method onReceivedMessage() is - * fired. - *

- * Note: no 482 (Loop Detected) responses are generated for requests that does - * not properly match any ongoing transactions, dialogs, nor method types. - */ -public class SipProvider implements Configurable, TransportListener, - TcpServerListener { - - // **************************** Constants **************************** - - /** UDP protocol type */ - public static final String PROTO_UDP = "udp"; - /** TCP protocol type */ - public static final String PROTO_TCP = "tcp"; - /** TLS protocol type */ - public static final String PROTO_TLS = "tls"; - /** SCTP protocol type */ - public static final String PROTO_SCTP = "sctp"; - - /** - * String value "auto-configuration" used for auto configuration of the host - * address. - */ - public static final String AUTO_CONFIGURATION = "AUTO-CONFIGURATION"; - - /** - * String value "auto-configuration" used for auto configuration of the host - * address. - */ - public static final String ALL_INTERFACES = "ALL-INTERFACES"; - - /** String value "NO-OUTBOUND" used for setting no outbound proxy. */ - // public static final String NO_OUTBOUND="NO-OUTBOUND"; - /** - * Identifier used as listener id for capturing ANY incoming messages that - * does not match any active method_id, transaction_id, nor dialog_id.
- * In this context, "active" means that there is a active listener for that - * specific method, transaction, or dialog. - */ - public static final Identifier ANY = new Identifier("ANY"); - - /** - * Identifier used as listener id for capturing any incoming messages in - * PROMISQUE mode, that means that messages are passed to the present - * listener regardless of any other active SipProviderListeners for specific - * messages.

More than one SipProviderListener can be added and be - * active concurrently for capturing messages in PROMISQUE mode. - */ - public static final Identifier PROMISQUE = new Identifier("PROMISQUE"); - - /** Minimum length for a valid SIP message. */ - private static final int MIN_MESSAGE_LENGTH = 12; - - // ***************** Readable/configurable attributes ***************** - - /** - * Via address/name. Use 'auto-configuration' for auto detection, or let it - * undefined. - */ - String via_addr = null; - - /** Local SIP port */ - int host_port = 0; - - /** - * Network interface (IP address) used by SIP. Use 'ALL-INTERFACES' for - * binding SIP to all interfaces (or let it undefined). - */ - String host_ifaddr = null; - - /** Transport protocols (the first protocol is used as default) */ - String[] transport_protocols = null; - - /** Max number of (contemporary) open connections */ - int nmax_connections = 0; - - /** - * Outbound proxy (host_addr[:host_port]). Use 'NONE' for not using an - * outbound proxy (or let it undefined). - */ - SocketAddress outbound_proxy = null; - - /** Whether logging all packets (including non-SIP keepalive tokens). */ - boolean log_all_packets = false; - - // for backward compatibility: - - /** Outbound proxy addr (for backward compatibility). */ - private String outbound_addr = null; - /** Outbound proxy port (for backward compatibility). */ - private int outbound_port = -1; - - // ********************* Non-readable attributes ********************* - - /** Event Loger */ - protected Log event_log = null; - - /** Message Loger */ - protected Log message_log = null; - - /** Network interface (IP address) used by SIP. */ - IpAddress host_ipaddr = null; - - /** Default transport */ - String default_transport = null; - - /** Whether using UDP as transport protocol */ - boolean transport_udp = false; - /** Whether using TCP as transport protocol */ - boolean transport_tcp = false; - /** Whether using TLS as transport protocol */ - boolean transport_tls = false; - /** Whether using SCTP as transport protocol */ - boolean transport_sctp = false; - - /** Whether adding 'rport' parameter on outgoing requests. */ - boolean rport = true; - - /** - * Whether forcing 'rport' parameter on incoming requests ('force-rport' - * mode). - */ - boolean force_rport = false; - - /** List of provider listeners */ - Hashtable listeners = null; - - /** List of exception listeners */ - HashSet exception_listeners = null; - - /** UDP transport */ - UdpTransport udp = null; - - /** Tcp server */ - TcpServer tcp_server = null; - - /** Connections */ - Hashtable connections = null; - - // *************************** Costructors *************************** - - /** Creates a void SipProvider. */ - /* - * protected SipProvider() { } - */ - - /** Creates a new SipProvider. */ - public SipProvider(String via_addr, int port) { - init(via_addr, port, null, null); - initlog(); - startTrasport(); - } - - /** - * Creates a new SipProvider. Costructs the SipProvider, initializing the - * SipProviderListeners, the transport protocols, and other attributes. - */ - public SipProvider(String via_addr, int port, String[] protocols, - String ifaddr) { - init(via_addr, port, protocols, ifaddr); - initlog(); - startTrasport(); - } - - /** - * Creates a new SipProvider. The SipProvider attributres are read from - * file. - */ - public SipProvider(String file) { - if (!SipStack.isInit()) - SipStack.init(file); - new Configure(this, file); - init(via_addr, host_port, transport_protocols, host_ifaddr); - initlog(); - startTrasport(); - } - - /** - * Inits the SipProvider, initializing the SipProviderListeners, the - * transport protocols, the outbound proxy, and other attributes. - */ - private void init(String viaddr, int port, String[] protocols, String ifaddr) { - if (!SipStack.isInit()) - SipStack.init(); - via_addr = viaddr; - if (via_addr == null || via_addr.equalsIgnoreCase(AUTO_CONFIGURATION)) - via_addr = IpAddress.localIpAddress; - host_port = port; - if (host_port < 0) // modified - host_port = SipStack.default_port; - host_ipaddr = null; - if (ifaddr != null && !ifaddr.equalsIgnoreCase(ALL_INTERFACES)) { - try { - host_ipaddr = IpAddress.getByName(ifaddr); - } catch (IOException e) { - e.printStackTrace(); - host_ipaddr = null; - } - } - transport_protocols = protocols; - if (transport_protocols == null) - transport_protocols = SipStack.default_transport_protocols; - default_transport = transport_protocols[0]; - for (int i = 0; i < transport_protocols.length; i++) { - transport_protocols[i] = transport_protocols[i].toLowerCase(); - if (transport_protocols[i].equals(PROTO_UDP)) - transport_udp = true; - else if (transport_protocols[i].equals(PROTO_TCP)) - transport_tcp = true; - /* - * else if (transport_protocols[i].equals(PROTO_TLS)) - * transport_tls=true; else if - * (transport_protocols[i].equals(PROTO_SCTP)) transport_sctp=true; - */ - } - if (nmax_connections <= 0) - nmax_connections = SipStack.default_nmax_connections; - - // just for backward compatibility.. - if (outbound_port < 0) - outbound_port = SipStack.default_port; - if (outbound_addr != null) { - if (outbound_addr.equalsIgnoreCase(Configure.NONE) - || outbound_addr.equalsIgnoreCase("NO-OUTBOUND")) - outbound_proxy = null; - else - outbound_proxy = new SocketAddress(outbound_addr, outbound_port); - } - - rport = SipStack.use_rport; - force_rport = SipStack.force_rport; - - exception_listeners = new HashSet(); - listeners = new Hashtable(10); - - connections = new Hashtable(10); - } - - /** Inits logs. */ - private void initlog() { - if (SipStack.debug_level > 0) { - String filename = SipStack.log_path + "//" + via_addr + "." - + host_port; - event_log = new RotatingLog(filename + "_events.log", null, - SipStack.debug_level, SipStack.max_logsize * 1024, - SipStack.log_rotations, SipStack.rotation_scale, - SipStack.rotation_time); - message_log = new RotatingLog(filename + "_messages.log", null, - SipStack.debug_level, SipStack.max_logsize * 1024, - SipStack.log_rotations, SipStack.rotation_scale, - SipStack.rotation_time); - } - printLog("Date: " + DateFormat.formatHHMMSS(new Date()), LogLevel.HIGH); - printLog("SipStack: " + SipStack.release, LogLevel.HIGH); - printLog("new SipProvider(): " + toString(), LogLevel.HIGH); - } - - /** Starts the transport services. */ - private void startTrasport() { - // start udp - if (transport_udp) { - try { - if (host_ipaddr == null) - udp = new UdpTransport(host_port, this); - else - udp = new UdpTransport(host_port, host_ipaddr, this); - host_port = udp.getPort(); - printLog("udp is up", LogLevel.MEDIUM); - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - // start tcp - if (transport_tcp) { - try { - if (host_ipaddr == null) - tcp_server = new TcpServer(host_port, this); - else - tcp_server = new TcpServer(host_port, host_ipaddr, this); - host_port = tcp_server.getPort(); - printLog("tcp is up", LogLevel.MEDIUM); - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - // printLog("transport is up",LogLevel.MEDIUM); - } - - /** Stops the transport services. */ - private void stopTrasport() { - // stop udp - if (udp != null) { - printLog("udp is going down", LogLevel.LOWER); - udp.halt(); - udp = null; - } - // stop tcp - if (tcp_server != null) { - printLog("tcp is going down", LogLevel.LOWER); - tcp_server.halt(); - tcp_server = null; - } - haltConnections(); - connections = null; - } - - public void haltConnections() { // modified - if (connections != null) { - printLog("connections are going down", LogLevel.LOWER); - for (Enumeration e = connections.elements(); e - .hasMoreElements();) { - ConnectedTransport c = e.nextElement(); - c.halt(); - } - connections = new Hashtable(10); - } - } - - /** Stops the SipProviders. */ - public void halt() { - printLog("halt: SipProvider is going down", LogLevel.MEDIUM); - stopTrasport(); - listeners = new Hashtable(10); - exception_listeners = new HashSet(); - } - - /** Parses a single line (loaded from the config file) */ - public void parseLine(String line) { - String attribute; - Parser par; - int index = line.indexOf("="); - if (index > 0) { - attribute = line.substring(0, index).trim(); - par = new Parser(line, index + 1); - } else { - attribute = line; - par = new Parser(""); - } - char[] delim = { ' ', ',' }; - - if (attribute.equals("via_addr")) { - via_addr = par.getString(); - return; - } - if (attribute.equals("host_port")) { - host_port = par.getInt(); - return; - } - if (attribute.equals("host_ifaddr")) { - host_ifaddr = par.getString(); - return; - } - if (attribute.equals("transport_protocols")) { - transport_protocols = par.getWordArray(delim); - return; - } - if (attribute.equals("nmax_connections")) { - nmax_connections = par.getInt(); - return; - } - if (attribute.equals("outbound_proxy")) { - String soaddr = par.getString(); - if (soaddr == null || soaddr.length() == 0 - || soaddr.equalsIgnoreCase(Configure.NONE) - || soaddr.equalsIgnoreCase("NO-OUTBOUND")) - outbound_proxy = null; - else - outbound_proxy = new SocketAddress(soaddr); - return; - } - if (attribute.equals("log_all_packets")) { - log_all_packets = (par.getString().toLowerCase().startsWith("y")); - return; - } - - // old parameters - if (attribute.equals("host_addr")) - System.err - .println("WARNING: parameter 'host_addr' is no more supported; use 'via_addr' instead."); - if (attribute.equals("all_interfaces")) - System.err - .println("WARNING: parameter 'all_interfaces' is no more supported; use 'host_iaddr' for setting a specific interface or let it undefined."); - if (attribute.equals("use_outbound")) - System.err - .println("WARNING: parameter 'use_outbound' is no more supported; use 'outbound_proxy' for setting an outbound proxy or let it undefined."); - if (attribute.equals("outbound_addr")) { - System.err - .println("WARNING: parameter 'outbound_addr' has been deprecated; use 'outbound_proxy=[:]' instead."); - outbound_addr = par.getString(); - return; - } - if (attribute.equals("outbound_port")) { - System.err - .println("WARNING: parameter 'outbound_port' has been deprecated; use 'outbound_proxy=[:]' instead."); - outbound_port = par.getInt(); - return; - } - } - - /** Converts the entire object into lines (to be saved into the config file) */ - protected String toLines() { // currently not implemented.. - return toString(); - } - - /** Gets a String with the list of transport protocols. */ - private String transportProtocolsToString() { - String list = transport_protocols[0]; - for (int i = 1; i < transport_protocols.length; i++) - list += "/" + transport_protocols[i]; - return list; - } - - // ************************** Public methods ************************* - - /** Gets via address. */ - public String getViaAddress() { - via_addr = IpAddress.localIpAddress; - - return via_addr; - } - - /** Sets via address. */ - /* - * public void setViaAddress(String addr) { via_addr=addr; } - */ - - /** Gets host port. */ - public int getPort() { - return host_port; - } - - /** - * Whether binding the sip provider to all interfaces or only on the - * specified host address. - */ - public boolean isAllInterfaces() { - return host_ipaddr == null; - } - - /** Gets host interface IpAddress. */ - public IpAddress getInterfaceAddress() { - return host_ipaddr; - } - - /** Gets array of transport protocols. */ - public String[] getTransportProtocols() { - return transport_protocols; - } - - /** Gets the default transport protocol. */ - public String getDefaultTransport() { - return default_transport; - } - - /** Gets the default transport protocol. */ - public void setDefaultTransport(String proto) { - default_transport = proto; - } - - /** Sets rport support. */ - public void setRport(boolean flag) { - rport = flag; - } - - /** Whether using rport. */ - public boolean isRportSet() { - return rport; - } - - /** Sets 'force-rport' mode. */ - public void setForceRport(boolean flag) { - force_rport = flag; - } - - /** Whether using 'force-rport' mode. */ - public boolean isForceRportSet() { - return force_rport; - } - - /** Whether has outbound proxy. */ - public boolean hasOutboundProxy() { - return outbound_proxy != null; - } - - /** Gets the outbound proxy. */ - public SocketAddress getOutboundProxy() { - return outbound_proxy; - } - - String server; - - /** Sets the outbound proxy. Use 'null' for not using any outbound proxy. */ - public void setOutboundProxy(SocketAddress soaddr,String host) { - outbound_proxy = soaddr; - server = host; - } - - /** Removes the outbound proxy. */ - /* - * public void removeOutboundProxy() { setOutboundProxy(null); } - */ - - /** Gets the max number of (contemporary) open connections. */ - public int getNMaxConnections() { - return nmax_connections; - } - - /** Sets the max number of (contemporary) open connections. */ - public void setNMaxConnections(int n) { - nmax_connections = n; - } - - /** Gets event log. */ - public Log getLog() { - return event_log; - } - - /** Returns the list (Hashtable) of active listener_IDs. */ - public Hashtable getListeners() { - return listeners; - } - - /** - * Adds a new listener to the SipProvider for caputering any message in - * PROMISQUE mode. It is the same as using method - * addSipProviderListener(SipProvider.PROMISQUE,listener).

When - * capturing messages in promisque mode all messages are passed to the - * SipProviderListener before passing them to the specific listener (if - * present).
Note that more that one SipProviderListener can be active - * in promisque mode at the same time;in that case the same message is - * passed to all PROMISQUE SipProviderListeners. - * - * @param listener - * is the SipProviderListener. - * @return It returns true if the SipProviderListener is added, - * false if the listener_ID is already in use. - */ - public boolean addSipProviderPromisqueListener(SipProviderListener listener) { - return addSipProviderListener(PROMISQUE, listener); - } - - /** - * Adds a new listener to the SipProvider for caputering ANY message. It is - * the same as using method - * addSipProviderListener(SipProvider.ANY,listener). - * - * @param listener - * is the SipProviderListener. - * @return It returns true if the SipProviderListener is added, - * false if the listener_ID is already in use. - */ - public boolean addSipProviderListener(SipProviderListener listener) { - return addSipProviderListener(ANY, listener); - } - - /** - * Adds a new listener to the SipProvider. - * - * @param id - * is the unique identifier for the messages which the listener - * as to be associated to. It is used as key. It can identify a - * method, a transaction, or a dialog. Use SipProvider.ANY to - * capture all messages. Use SipProvider.PROMISQUE if you want to - * capture all message in promisque mode (letting other listeners - * to capture the same received messages). - * @param listener - * is the SipProviderListener for this message id. - * @return It returns true if the SipProviderListener is added, - * false if the listener_ID is already in use. - */ - public boolean addSipProviderListener(Identifier id, - SipProviderListener listener) { - printLog("adding SipProviderListener: " + id, LogLevel.MEDIUM); - boolean ret; - Identifier key = id; - if (listeners.containsKey(key)) { - printWarning( - "trying to add a SipProviderListener with a id that is already in use.", - LogLevel.HIGH); - ret = false; - } else { - listeners.put(key, listener); - ret = true; - } - - /* - if (listeners != null) { - String list = ""; - for (Enumeration e = listeners.keys(); e - .hasMoreElements();) - list += e.nextElement() + ", "; - printLog(listeners.size() + " listeners: " + list, LogLevel.LOW); - } - */ - return ret; - } - - /** - * Removes a SipProviderListener. - * - * @param id - * is the unique identifier used to select the listened messages. - * @return It returns true if the SipProviderListener is removed, - * false if the identifier is missed. - */ - public boolean removeSipProviderListener(Identifier id) { - printLog("removing SipProviderListener: " + id, LogLevel.MEDIUM); - boolean ret; - Identifier key = id; - if (!listeners.containsKey(key)) { - printWarning("trying to remove a missed SipProviderListener.", - LogLevel.HIGH); - ret = false; - } else { - listeners.remove(key); - ret = true; - } - - if (listeners != null) { - String list = ""; - for (Enumeration e = listeners.keys(); e - .hasMoreElements();) - list += e.nextElement() + ", "; - printLog(listeners.size() + " listeners: " + list, LogLevel.LOW); - } - return ret; - } - - /** - * Sets the SipProviderExceptionListener. The SipProviderExceptionListener - * is the listener for all exceptions thrown by the SipProviders. - * - * @param e_listener - * is the SipProviderExceptionListener. - * @return It returns true if the SipProviderListener has been - * correctly set, false if the SipProviderListener was - * already set. - */ - public boolean addSipProviderExceptionListener( - SipProviderExceptionListener e_listener) { - printLog("adding SipProviderExceptionListener", LogLevel.MEDIUM); - if (exception_listeners.contains(e_listener)) { - printWarning( - "trying to add an already present SipProviderExceptionListener.", - LogLevel.HIGH); - return false; - } else { - exception_listeners.add(e_listener); - return true; - } - } - - /** - * Removes a SipProviderExceptionListener. - * - * @param e_listener - * is the SipProviderExceptionListener. - * @return It returns true if the SipProviderExceptionListener has - * been correctly removed, false if the - * SipProviderExceptionListener is missed. - */ - public boolean removeSipProviderExceptionListener( - SipProviderExceptionListener e_listener) { - printLog("removing SipProviderExceptionListener", LogLevel.MEDIUM); - if (!exception_listeners.contains(e_listener)) { - printWarning( - "trying to remove a missed SipProviderExceptionListener.", - LogLevel.HIGH); - return false; - } else { - exception_listeners.remove(e_listener); - return true; - } - } - - /** - * Sends a Message, specifing the transport portocol, nexthop address and - * port. - *

- * This is a low level method and forces the message to be routed to a - * specific nexthop address, port and transport, regardless whatever the - * Via, Route, or request-uri, address to. - *

- * In case of connection-oriented transport, the connection is selected as - * follows:
- if an existing connection is found matching the - * destination end point (socket), such connection is used, otherwise
- - * a new connection is established - * - * @return It returns a Connection in case of connection-oriented delivery - * (e.g. TCP) or null in case of connection-less delivery (e.g. UDP) - */ - public ConnectionIdentifier sendMessage(Message msg, String proto, - String dest_addr, int dest_port, int ttl) { - if (log_all_packets || msg.getLength() > MIN_MESSAGE_LENGTH) - printLog("Resolving host address '" + dest_addr + "'", - LogLevel.MEDIUM); - try { - IpAddress dest_ipaddr = IpAddress.getByName(dest_addr); - return sendMessage(msg, proto, dest_ipaddr, dest_port, ttl); - } catch (Exception e) { - printException(e, LogLevel.HIGH); - return null; - } - } - - /** - * Sends a Message, specifing the transport portocol, nexthop address and - * port. - */ - private ConnectionIdentifier sendMessage(Message msg, String proto, - IpAddress dest_ipaddr, int dest_port, int ttl) { - ConnectionIdentifier conn_id = new ConnectionIdentifier(proto, - dest_ipaddr, dest_port); - if (log_all_packets || msg.getLength() > MIN_MESSAGE_LENGTH) - printLog("Sending message to " + conn_id, LogLevel.MEDIUM); - - if (transport_udp && proto.equals(PROTO_UDP)) { // UDP - // printLog("using UDP",LogLevel.LOW); - conn_id = null; - try { // if (ttl>0 && multicast_address) do something? - udp.sendMessage(msg, dest_ipaddr, dest_port); - } catch (IOException e) { - printException(e, LogLevel.HIGH); - return null; - } - } else if (transport_tcp && proto.equals(PROTO_TCP)) { // TCP - // printLog("using TCP",LogLevel.LOW); - if (connections == null || !connections.containsKey(conn_id)) { // modified - printLog("no active connection found matching " + conn_id, - LogLevel.MEDIUM); - printLog("open " + proto + " connection to " + dest_ipaddr - + ":" + dest_port, LogLevel.MEDIUM); - TcpTransport conn = null; - try { - conn = new TcpTransport(dest_ipaddr, dest_port, this, server); - } catch (Exception e) { - printLog("connection setup FAILED", LogLevel.HIGH); - return null; - } - printLog("connection " + conn + " opened", LogLevel.HIGH); - addConnection(conn); - if (!msg.isRegister()) - Receiver.engine(Receiver.mContext).register(); // modified - } else { - printLog("active connection found matching " + conn_id, - LogLevel.MEDIUM); - } - ConnectedTransport conn = (ConnectedTransport) connections - .get(conn_id); - if (conn != null) { - printLog("sending data through conn " + conn, LogLevel.MEDIUM); - try { - conn.sendMessage(msg); - conn_id = new ConnectionIdentifier(conn); - } catch (IOException e) { - printException(e, LogLevel.HIGH); - return null; - } - } else { // this point has not to be reached - printLog("ERROR: conn " + conn_id + " not found: abort.", - LogLevel.MEDIUM); - return null; - } - } else { // otherwise - printWarning("Unsupported protocol (" + proto - + "): Message discarded", LogLevel.HIGH); - return null; - } - // logs - String dest_addr = dest_ipaddr.toString(); - printMessageLog(proto, dest_addr, dest_port, msg.getLength(), msg, - "sent"); - return conn_id; - } - - /** - * Sends the message msg. - *

- * The destination for the request is computed as follows:
- if - * outbound_addr is set, outbound_addr and outbound_port - * are used, otherwise
- if message has Route header with lr option - * parameter (i.e. RFC3261 compliant), the first Route address is used, - * otherwise
- the request's Request-URI is considered. - *

- * The destination for the response is computed based on the sent-by - * parameter in the Via header field (RFC3261 compliant) - *

- * As transport it is used the protocol specified in the 'via' header field - *

- * In case of connection-oriented transport:
- if an already - * established connection is found matching the destination end point - * (socket), such connection is used, otherwise
- a new connection is - * established - * - * @return Returns a ConnectionIdentifier in case of connection-oriented - * delivery (e.g. TCP) or null in case of connection-less delivery - * (e.g. UDP) - */ - public ConnectionIdentifier sendMessage(Message msg) { - printLog("Sending message:\r\n" + msg.toString(), LogLevel.LOWER); - - // select the transport protocol - ViaHeader via = msg.getViaHeader(); - String proto; - if (via != null) - proto = via.getProtocol().toLowerCase(); - else - proto = getDefaultTransport().toLowerCase(); // modified - printLog("using transport " + proto, LogLevel.MEDIUM); - - // select the destination address and port - String dest_addr = null; - int dest_port = 0; - int ttl = 0; - - if (!msg.isResponse()) { // modified - if (outbound_proxy != null) { - dest_addr = outbound_proxy.getAddress().toString(); - dest_port = outbound_proxy.getPort(); - } else { - if (msg.hasRouteHeader() - && msg.getRouteHeader().getNameAddress().getAddress() - .hasLr()) { - SipURL url = msg.getRouteHeader().getNameAddress() - .getAddress(); - dest_addr = url.getHost(); - dest_port = url.getPort(); - } else { - SipURL url = msg.getRequestLine().getAddress(); - dest_addr = url.getHost(); - dest_port = url.getPort(); - if (url.hasMaddr()) { - dest_addr = url.getMaddr(); - if (url.hasTtl()) - ttl = url.getTtl(); - // update the via header by adding maddr and ttl params - via.setMaddr(dest_addr); - if (ttl > 0) - via.setTtl(ttl); - msg.removeViaHeader(); - msg.addViaHeader(via); - } - } - } - } else { // RESPONSES - SipURL url = via.getSipURL(); - if (via.hasReceived()) - dest_addr = via.getReceived(); - else - dest_addr = url.getHost(); - if (via.hasRport()) - dest_port = via.getRport(); - if (dest_port <= 0) - dest_port = url.getPort(); - } - - if (dest_port <= 0) - dest_port = SipStack.default_port; - - return sendMessage(msg, proto, dest_addr, dest_port, ttl); - } - - /** Sends the message msg using the specified connection. */ - public ConnectionIdentifier sendMessage(Message msg, - ConnectionIdentifier conn_id) { - if (log_all_packets || msg.getLength() > MIN_MESSAGE_LENGTH) - printLog("Sending message through conn " + conn_id, LogLevel.HIGH); - printLog("message:\r\n" + msg.toString(), LogLevel.LOWER); - - if (conn_id != null && connections.containsKey(conn_id)) { // connection - // exists - printLog("active connection found matching " + conn_id, - LogLevel.MEDIUM); - ConnectedTransport conn = (ConnectedTransport) connections - .get(conn_id); - try { - conn.sendMessage(msg); - // logs - // String proto=conn.getProtocol(); - String proto = conn.getProtocol(); - String dest_addr = conn.getRemoteAddress().toString(); - int dest_port = conn.getRemotePort(); - printMessageLog(proto, dest_addr, dest_port, msg.getLength(), - msg, "sent"); - return conn_id; - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - // else - printLog("no active connection found matching " + conn_id, - LogLevel.MEDIUM); - return sendMessage(msg); - } - - /** - * Processes the message received. It is called each time a new message is - * received by the transport layer, and it performs the actual message - * processing. - */ - protected void processReceivedMessage(Message msg) { - try { // logs - printMessageLog(msg.getTransportProtocol(), msg.getRemoteAddress(), - msg.getRemotePort(), msg.getLength(), msg, "received"); - - // discard too short messages - if (msg.getLength() <= 2) { - if (log_all_packets) - printLog("message too short: discarded\r\n", LogLevel.LOW); - return; - } - // discard non-SIP messages - String first_line = msg.getFirstLine(); - if (first_line == null - || first_line.toUpperCase().indexOf("SIP/2.0") < 0) { - if (log_all_packets) - printLog("NOT a SIP message: discarded\r\n", LogLevel.LOW); - return; - } - if (!Sipdroid.release) - printLog("received new SIP message "+msg.getRequestLine()+" "+msg.getStatusLine(), LogLevel.HIGH); // modified - printLog("message:\r\n" + msg.toString(), LogLevel.LOWER); - - // if a request, handle "received" and "rport" parameters - if (msg.isRequest()) { - ViaHeader vh = msg.getViaHeader(); - boolean via_changed = false; - String src_addr = msg.getRemoteAddress(); - int src_port = msg.getRemotePort(); - String via_addr = vh.getHost(); - int via_port = vh.getPort(); - if (via_port <= 0) - via_port = SipStack.default_port; - - if (!via_addr.equals(src_addr)) { - vh.setReceived(src_addr); - via_changed = true; - } - - if (vh.hasRport()) { - vh.setRport(src_port); - via_changed = true; - } else { - if (force_rport && via_port != src_port) { - vh.setRport(src_port); - via_changed = true; - } - } - - if (via_changed) { - msg.removeViaHeader(); - msg.addViaHeader(vh); - } - } - - // is there any listeners? - if (listeners == null || listeners.size() == 0) { - printLog("no listener found: message discarded.", LogLevel.HIGH); - return; - } - - // try to look for a UA in promisque mode - if (listeners.containsKey(PROMISQUE)) { - printLog("message passed to uas: " + PROMISQUE, LogLevel.MEDIUM); - ((SipProviderListener) listeners.get(PROMISQUE)) - .onReceivedMessage(this, msg); - } - - // after the callback check if the message is still valid - if (!msg.isRequest() && !msg.isResponse()) { - printLog("No valid SIP message: message discarded.", - LogLevel.HIGH); - return; - } - - // this was the promisque listener; now keep on looking for a - // tighter listener.. - - // try to look for a transaction - Identifier key = msg.getTransactionId(); - printLog("DEBUG: transaction-id: " + key, LogLevel.MEDIUM); - if (listeners.containsKey(key)) { - printLog("message passed to transaction: " + key, - LogLevel.MEDIUM); - SipProviderListener sip_listener = (SipProviderListener) listeners.get(key); - if (sip_listener != null) - { - sip_listener.onReceivedMessage( - this, msg); - } - return; - } - // try to look for a dialog - key = msg.getDialogId(); - printLog("DEBUG: dialog-id: " + key, LogLevel.MEDIUM); - if (listeners.containsKey(key)) { - printLog("message passed to dialog: " + key, LogLevel.MEDIUM); - ((SipProviderListener) listeners.get(key)).onReceivedMessage( - this, msg); - return; - } - // try to look for a UAS - key = msg.getMethodId(); - if (listeners.containsKey(key)) { - printLog("message passed to uas: " + key, LogLevel.MEDIUM); - ((SipProviderListener) listeners.get(key)).onReceivedMessage( - this, msg); - return; - } - // try to look for a default UA - if (listeners.containsKey(ANY)) { - printLog("message passed to uas: " + ANY, LogLevel.MEDIUM); - ((SipProviderListener) listeners.get(ANY)).onReceivedMessage( - this, msg); - return; - } - - // if we are here, no listener_ID matched.. - printLog( - "No SipListener found matching that message: message DISCARDED", - LogLevel.HIGH); - // printLog("Pending SipProviderListeners= - // "+getListeners().size(),3); - printLog("Pending SipProviderListeners= " + listeners.size(), - LogLevel.MEDIUM); - } catch (Exception e) { - printWarning("Error handling a new incoming message", LogLevel.HIGH); - printException(e, LogLevel.MEDIUM); - if (exception_listeners == null || exception_listeners.size() == 0) { - System.err.println("Error handling a new incoming message"); - e.printStackTrace(); - } else { - for (Iterator i = exception_listeners.iterator(); i.hasNext();) - try { - ((SipProviderExceptionListener) i.next()) - .onMessageException(msg, e); - } catch (Exception e2) { - printWarning("Error handling handling the Exception", - LogLevel.HIGH); - printException(e2, LogLevel.MEDIUM); - } - } - } - } - - /** Adds a new Connection */ - private void addConnection(ConnectedTransport conn) { - ConnectionIdentifier conn_id = new ConnectionIdentifier(conn); - if (connections.containsKey(conn_id)) { // remove the previous - // connection - printLog("trying to add the already established connection " - + conn_id, LogLevel.HIGH); - printLog("connection " + conn_id + " will be replaced", - LogLevel.HIGH); - ConnectedTransport old_conn = (ConnectedTransport) connections - .get(conn_id); - old_conn.halt(); - connections.remove(conn_id); - } else if (connections.size() >= nmax_connections) { // remove the - // older unused - // connection - printLog( - "reached the maximum number of connection: removing the older unused connection", - LogLevel.HIGH); - long older_time = System.currentTimeMillis(); - ConnectionIdentifier older_id = null; - for (Enumeration e = connections.elements(); e - .hasMoreElements();) { - ConnectedTransport co = e.nextElement(); - if (co.getLastTimeMillis() < older_time) - older_id = new ConnectionIdentifier(co); - } - if (older_id != null) - removeConnection(older_id); - } - connections.put(conn_id, conn); - conn_id = new ConnectionIdentifier(conn); - conn = (ConnectedTransport) connections.get(conn_id); - // DEBUG log: - printLog("active connenctions:", LogLevel.LOW); - for (Enumeration e = connections.keys(); e - .hasMoreElements();) { - ConnectionIdentifier id = (ConnectionIdentifier) e.nextElement(); - printLog("conn-id=" + id + ": " - + ((ConnectedTransport) connections.get(id)).toString(), - LogLevel.LOW); - } - } - - /** Removes a Connection */ - private void removeConnection(ConnectionIdentifier conn_id) { - if (connections != null && connections.containsKey(conn_id)) { // modified - ConnectedTransport conn = (ConnectedTransport) connections - .get(conn_id); - if (conn != null) conn.halt(); - connections.remove(conn_id); - // DEBUG log: - printLog("active connenctions:", LogLevel.LOW); - for (Enumeration e = connections.elements(); e - .hasMoreElements();) { - ConnectedTransport co = (ConnectedTransport) e.nextElement(); - printLog("conn " + co.toString(), LogLevel.LOW); - } - } - } - - // ************************* Callback methods ************************* - PowerManager pm; - PowerManager.WakeLock wl; - - /** When a new SIP message is received. */ - public void onReceivedMessage(Transport transport, Message msg) { - if (pm == null) { - pm = (PowerManager) Receiver.mContext.getSystemService(Context.POWER_SERVICE); - wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Sipdroid.SipProvider"); - } - wl.acquire(); // modified - processReceivedMessage(msg); - wl.release(); - } - - /** When Transport terminates. */ - public void onTransportTerminated(Transport transport, Exception error) { - printLog("transport " + transport + " terminated", LogLevel.MEDIUM); - if (transport.getProtocol().equals(PROTO_TCP)) { - ConnectionIdentifier conn_id = new ConnectionIdentifier( - (ConnectedTransport) transport); - removeConnection(conn_id); - if (Sipdroid.on(Receiver.mContext)) - Receiver.engine(Receiver.mContext).register(); // modified - } - if (error != null) - printException(error, LogLevel.HIGH); - } - - /** When a new incoming Connection is established */ - public void onIncomingConnection(TcpServer tcp_server, TcpSocket socket) { - printLog("incoming connection from " + socket.getAddress() + ":" - + socket.getPort(), LogLevel.MEDIUM); - ConnectedTransport conn = new TcpTransport(socket, this); - printLog("tcp connection " + conn + " opened", LogLevel.MEDIUM); - addConnection(conn); - } - - /** When TcpServer terminates. */ - public void onServerTerminated(TcpServer tcp_server, Exception error) { - printLog("tcp server " + tcp_server + " terminated", LogLevel.MEDIUM); - } - - // ************************** Other methods *************************** - - /** - * Picks a fresh branch value. The branch ID MUST be unique across space and - * time for all requests sent by the UA. The branch ID always begin with the - * characters "z9hG4bK". These 7 characters are used by RFC 3261 as a magic - * cookie. - */ - public static String pickBranch() { // String - // str=Long.toString(Math.abs(Random.nextLong()),16); - // if (str.length()<5) str+="00000"; - // return "z9hG4bK"+str.substring(0,5); - return "z9hG4bK" + Random.nextNumString(5); - } - - /** - * Picks an unique branch value based on a SIP message. This value could - * also be used as transaction ID - */ - public String pickBranch(Message msg) { - StringBuffer sb = new StringBuffer(); - sb.append(msg.getRequestLine().getAddress().toString()); - sb.append(getViaAddress() + getPort()); - ViaHeader top_via = msg.getViaHeader(); - if (top_via.hasBranch()) - sb.append(top_via.getBranch()); - else { - sb.append(top_via.getHost() + top_via.getPort()); - sb.append(msg.getCSeqHeader().getSequenceNumber()); - sb.append(msg.getCallIdHeader().getCallId()); - sb.append(msg.getFromHeader().getTag()); - sb.append(msg.getToHeader().getTag()); - } - // return "z9hG4bK"+(new MD5(unique_str)).asHex().substring(0,9); - return "z9hG4bK" + (new SimpleDigest(5, sb.toString())).asHex(); - } - - /** - * Picks a new tag. A tag MUST be globally unique and cryptographically - * random with at least 32 bits of randomness. A property of this selection - * requirement is that a UA will place a different tag into the From header - * of an INVITE than it would place into the To header of the response to - * the same INVITE. This is needed in order for a UA to invite itself to a - * session. - */ - public static String pickTag() { // String - // str=Long.toString(Math.abs(Random.nextLong()),16); - // if (str.length()<8) str+="00000000"; - // return str.substring(0,8); - return "z9hG4bK" + Random.nextNumString(8); - } - - /** - * Picks a new tag. The tag is generated uniquely based on message req. - * This tag can be generated for responses in a stateless manner - in a - * manner that will generate the same tag for the same request consistently. - */ - public static String pickTag(Message req) { // return - // String.valueOf(tag_generator++); - // return (new MD5(request.toString())).asHex().substring(0,8); - return (new SimpleDigest(8, req.toString())).asHex(); - } - - /** - * Picks a new call-id. The call-id is a globally unique identifier over - * space and time. It is implemented in the form "localid@host". Call-id - * must be considered case-sensitive and is compared byte-by-byte. - */ - public String pickCallId() { // String - // str=Long.toString(Math.abs(Random.nextLong()),16); - // if (str.length()<12) str+="000000000000"; - // return str.substring(0,12)+"@"+getViaAddress(); - return Random.nextNumString(12) + "@" + getViaAddress(); - } - - /** picks an initial CSeq */ - public static int pickInitialCSeq() { - return 1; - } - - /** - * (Deprecated) Constructs a NameAddress based on an input string. - * The input string can be a:
- user name,
- - * user@address url,
- "Name" <sip:user@address> - * address, - *

- * In the former case, a SIP URL is costructed using the outbound proxy as - * host address if present, otherwise the local via address is used. - */ - public NameAddress completeNameAddress(String str) { - if (str.indexOf("= 0) - return new NameAddress(str); - else { - SipURL url = completeSipURL(str); - return new NameAddress(url); - } - } - - /** Constructs a SipURL based on an input string. */ - private SipURL completeSipURL(String str) { // in case it is passed only the - // 'user' field, add - // '@'[':'] - if (!str.startsWith("sip:") && str.indexOf("@") < 0 - && str.indexOf(".") < 0 && str.indexOf(":") < 0) { // may be it - // is just - // the user - // name.. - String url = "sip:" + str + "@"; - if (outbound_proxy != null) { - url += outbound_proxy.getAddress().toString(); - int port = outbound_proxy.getPort(); - if (port > 0 && port != SipStack.default_port) - url += ":" + port; - } else { - url += getViaAddress(); - if (host_port > 0 && host_port != SipStack.default_port) - url += ":" + host_port; - } - return new SipURL(url); - } else - return new SipURL(str); - } - - /** - * Constructs a SipURL for the given username on the local SIP UA. - * If username is null, only host address and port are used. - */ - /* - * public SipURL getSipURL(String user_name) { return new - * SipURL(user_name,via_addr,(host_port!=SipStack.default_port)?host_port:-1); } - */ - - // ******************************* Logs ******************************* - /** Gets a String value for this object */ - public String toString() { - if (host_ipaddr == null) - return host_port + "/" + transportProtocolsToString(); - else - return host_ipaddr.toString() + ":" + host_port + "/" - + transportProtocolsToString(); - } - - /** Adds a new string to the default Log */ - private final void printLog(String str, int level) { - if (Sipdroid.release) return; - if (event_log != null) { - String provider_id = (host_ipaddr == null) ? Integer - .toString(host_port) : host_ipaddr.toString() + ":" - + host_port; - event_log.println("SipProvider-" + provider_id + ": " + str, level - + SipStack.LOG_LEVEL_TRANSPORT); - } - if (level <= LogLevel.HIGH) System.out.println("SipProvider: " + str); // modified - } - - /** Adds a WARNING to the default Log */ - private final void printWarning(String str, int level) { - printLog("WARNING: " + str, level); - } - - /** Adds the Exception message to the default Log */ - private final void printException(Exception e, int level) { - if (event_log != null) - event_log.printException(e, level + SipStack.LOG_LEVEL_TRANSPORT); - } - - /** Adds the SIP message to the messageslog */ - private final void printMessageLog(String proto, String addr, int port, - int len, Message msg, String str) { - if (log_all_packets || len >= MIN_MESSAGE_LENGTH) { - if (message_log != null) { - message_log.printPacketTimestamp(proto, addr, port, len, str - + "\r\n" + msg.toString() - + "-----End-of-message-----\r\n", 1); - } - if (event_log != null) { - String first_line = msg.getFirstLine(); - if (first_line != null) - first_line = first_line.trim(); - else - first_line = "NOT a SIP message"; - event_log.print("\r\n"); - event_log.printPacketTimestamp(proto, addr, port, len, - first_line + ", " + str, 1); - event_log.print("\r\n"); - } - } - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.sip.provider; + +import org.sipdroid.sipua.ui.Receiver; +import org.sipdroid.sipua.ui.Sipdroid; +import org.zoolu.net.*; +import org.zoolu.sip.header.*; +import org.zoolu.sip.message.Message; +import org.zoolu.sip.address.*; +import org.zoolu.tools.Configure; +import org.zoolu.tools.Configurable; +import org.zoolu.tools.Parser; +import org.zoolu.tools.Random; +import org.zoolu.tools.Log; +import org.zoolu.tools.LogLevel; +import org.zoolu.tools.RotatingLog; +import org.zoolu.tools.SimpleDigest; +import org.zoolu.tools.DateFormat; + +import java.util.Hashtable; +import java.io.IOException; +import org.zoolu.tools.HashSet; +import org.zoolu.tools.Iterator; + +import android.content.Context; +import android.os.PowerManager; + +import java.util.Enumeration; +import java.util.Date; + +/** + * SipProvider implements the SIP transport layer, that is the layer responsable + * for sending and receiving SIP messages. Messages are received by the callback + * function defined in the interface SipProviderListener. + *

+ * SipProvider implements also multiplexing/demultiplexing service through the + * use of SIP interface identifiers and onReceivedMessage() callback + * function of specific SipProviderListener. + *

+ * A SipProviderListener can be added to a SipProvider through the + * addSipProviderListener(id,listener) method, where: - id is the + * SIP interface identifier the listener has to be bound to, - listener + * is the SipProviderListener that received messages are passed to.

The SIP + * interface identifier specifies the type of messages the listener is going to + * receive for. Together with the specific SipProvider, it represents the + * complete SIP Service Access Point (SAP) address/identifier used for + * demultiplexing SIP messages at receiving side.

The identifier can be of + * one of the three following types: transaction_id, dialog_id, or method_id. + * These types of identifiers characterize respectively:
- messages within + * a specific transaction,
- messages within a specific dialog,
- + * messages related to a specific SIP method. It is also possible to use the the + * identifier ANY to specify
- all messages that are out of any + * transactions, dialogs, or already specified method types. + *

+ * When receiving a message, the SipProvider first tries to look for a matching + * transaction, then looks for a matching dialog, then for a matching method + * type, and finally for a default listener (i.e. that with identifier ANY). For + * the matched SipProviderListener, the method onReceivedMessage() is + * fired. + *

+ * Note: no 482 (Loop Detected) responses are generated for requests that does + * not properly match any ongoing transactions, dialogs, nor method types. + */ +public class SipProvider implements Configurable, TransportListener, + TcpServerListener { + + // **************************** Constants **************************** + + /** UDP protocol type */ + public static final String PROTO_UDP = "udp"; + /** TCP protocol type */ + public static final String PROTO_TCP = "tcp"; + /** TLS protocol type */ + public static final String PROTO_TLS = "tls"; + /** SCTP protocol type */ + public static final String PROTO_SCTP = "sctp"; + + /** + * String value "auto-configuration" used for auto configuration of the host + * address. + */ + public static final String AUTO_CONFIGURATION = "AUTO-CONFIGURATION"; + + /** + * String value "auto-configuration" used for auto configuration of the host + * address. + */ + public static final String ALL_INTERFACES = "ALL-INTERFACES"; + + /** String value "NO-OUTBOUND" used for setting no outbound proxy. */ + // public static final String NO_OUTBOUND="NO-OUTBOUND"; + /** + * Identifier used as listener id for capturing ANY incoming messages that + * does not match any active method_id, transaction_id, nor dialog_id.
+ * In this context, "active" means that there is a active listener for that + * specific method, transaction, or dialog. + */ + public static final Identifier ANY = new Identifier("ANY"); + + /** + * Identifier used as listener id for capturing any incoming messages in + * PROMISQUE mode, that means that messages are passed to the present + * listener regardless of any other active SipProviderListeners for specific + * messages.

More than one SipProviderListener can be added and be + * active concurrently for capturing messages in PROMISQUE mode. + */ + public static final Identifier PROMISQUE = new Identifier("PROMISQUE"); + + /** Minimum length for a valid SIP message. */ + private static final int MIN_MESSAGE_LENGTH = 12; + + // ***************** Readable/configurable attributes ***************** + + /** + * Via address/name. Use 'auto-configuration' for auto detection, or let it + * undefined. + */ + String via_addr = null; + + /** Local SIP port */ + int host_port = 0; + + /** + * Network interface (IP address) used by SIP. Use 'ALL-INTERFACES' for + * binding SIP to all interfaces (or let it undefined). + */ + String host_ifaddr = null; + + /** Transport protocols (the first protocol is used as default) */ + String[] transport_protocols = null; + + /** Max number of (contemporary) open connections */ + int nmax_connections = 0; + + /** + * Outbound proxy (host_addr[:host_port]). Use 'NONE' for not using an + * outbound proxy (or let it undefined). + */ + SocketAddress outbound_proxy = null; + + /** Whether logging all packets (including non-SIP keepalive tokens). */ + boolean log_all_packets = false; + + // for backward compatibility: + + /** Outbound proxy addr (for backward compatibility). */ + private String outbound_addr = null; + /** Outbound proxy port (for backward compatibility). */ + private int outbound_port = -1; + + // ********************* Non-readable attributes ********************* + + /** Event Loger */ + protected Log event_log = null; + + /** Message Loger */ + protected Log message_log = null; + + /** Network interface (IP address) used by SIP. */ + IpAddress host_ipaddr = null; + + /** Default transport */ + String default_transport = null; + + /** Whether using UDP as transport protocol */ + boolean transport_udp = false; + /** Whether using TCP as transport protocol */ + boolean transport_tcp = false; + /** Whether using TLS as transport protocol */ + boolean transport_tls = false; + /** Whether using SCTP as transport protocol */ + boolean transport_sctp = false; + + /** Whether adding 'rport' parameter on outgoing requests. */ + boolean rport = true; + + /** + * Whether forcing 'rport' parameter on incoming requests ('force-rport' + * mode). + */ + boolean force_rport = false; + + /** List of provider listeners */ + Hashtable listeners = null; + + /** List of exception listeners */ + HashSet exception_listeners = null; + + /** UDP transport */ + UdpTransport udp = null; + + /** Tcp server */ + TcpServer tcp_server = null; + + /** Connections */ + Hashtable connections = null; + + // *************************** Costructors *************************** + + /** Creates a void SipProvider. */ + /* + * protected SipProvider() { } + */ + + /** Creates a new SipProvider. */ + public SipProvider(String via_addr, int port) { + init(via_addr, port, null, null); + initlog(); + startTrasport(); + } + + /** + * Creates a new SipProvider. Costructs the SipProvider, initializing the + * SipProviderListeners, the transport protocols, and other attributes. + */ + public SipProvider(String via_addr, int port, String[] protocols, + String ifaddr) { + init(via_addr, port, protocols, ifaddr); + initlog(); + startTrasport(); + } + + /** + * Creates a new SipProvider. The SipProvider attributres are read from + * file. + */ + public SipProvider(String file) { + if (!SipStack.isInit()) + SipStack.init(file); + new Configure(this, file); + init(via_addr, host_port, transport_protocols, host_ifaddr); + initlog(); + startTrasport(); + } + + /** + * Inits the SipProvider, initializing the SipProviderListeners, the + * transport protocols, the outbound proxy, and other attributes. + */ + private void init(String viaddr, int port, String[] protocols, String ifaddr) { + if (!SipStack.isInit()) + SipStack.init(); + via_addr = viaddr; + if (via_addr == null || via_addr.equalsIgnoreCase(AUTO_CONFIGURATION)) + via_addr = IpAddress.localIpAddress; + host_port = port; + if (host_port < 0) // modified + host_port = SipStack.default_port; + host_ipaddr = null; + if (ifaddr != null && !ifaddr.equalsIgnoreCase(ALL_INTERFACES)) { + try { + host_ipaddr = IpAddress.getByName(ifaddr); + } catch (IOException e) { + e.printStackTrace(); + host_ipaddr = null; + } + } + transport_protocols = protocols; + if (transport_protocols == null) + transport_protocols = SipStack.default_transport_protocols; + default_transport = transport_protocols[0]; + for (int i = 0; i < transport_protocols.length; i++) { + transport_protocols[i] = transport_protocols[i].toLowerCase(); + if (transport_protocols[i].equals(PROTO_UDP)) + transport_udp = true; + else if (transport_protocols[i].equals(PROTO_TCP)) + transport_tcp = true; + /* + * else if (transport_protocols[i].equals(PROTO_TLS)) + * transport_tls=true; else if + * (transport_protocols[i].equals(PROTO_SCTP)) transport_sctp=true; + */ + } + if (nmax_connections <= 0) + nmax_connections = SipStack.default_nmax_connections; + + // just for backward compatibility.. + if (outbound_port < 0) + outbound_port = SipStack.default_port; + if (outbound_addr != null) { + if (outbound_addr.equalsIgnoreCase(Configure.NONE) + || outbound_addr.equalsIgnoreCase("NO-OUTBOUND")) + outbound_proxy = null; + else + outbound_proxy = new SocketAddress(outbound_addr, outbound_port); + } + + rport = SipStack.use_rport; + force_rport = SipStack.force_rport; + + exception_listeners = new HashSet(); + listeners = new Hashtable(10); + + connections = new Hashtable(10); + } + + /** Inits logs. */ + private void initlog() { + if (SipStack.debug_level > 0) { + String filename = SipStack.log_path + "//" + via_addr + "." + + host_port; + event_log = new RotatingLog(filename + "_events.log", null, + SipStack.debug_level, SipStack.max_logsize * 1024, + SipStack.log_rotations, SipStack.rotation_scale, + SipStack.rotation_time); + message_log = new RotatingLog(filename + "_messages.log", null, + SipStack.debug_level, SipStack.max_logsize * 1024, + SipStack.log_rotations, SipStack.rotation_scale, + SipStack.rotation_time); + } + printLog("Date: " + DateFormat.formatHHMMSS(new Date()), LogLevel.HIGH); + printLog("SipStack: " + SipStack.release, LogLevel.HIGH); + printLog("new SipProvider(): " + toString(), LogLevel.HIGH); + } + + /** Starts the transport services. */ + private void startTrasport() { + // start udp + if (transport_udp) { + try { + if (host_ipaddr == null) + udp = new UdpTransport(host_port, this); + else + udp = new UdpTransport(host_port, host_ipaddr, this); + host_port = udp.getPort(); + printLog("udp is up", LogLevel.MEDIUM); + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + // start tcp + if (transport_tcp) { + try { + if (host_ipaddr == null) + tcp_server = new TcpServer(host_port, this); + else + tcp_server = new TcpServer(host_port, host_ipaddr, this); + host_port = tcp_server.getPort(); + printLog("tcp is up", LogLevel.MEDIUM); + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + // printLog("transport is up",LogLevel.MEDIUM); + } + + /** Stops the transport services. */ + private void stopTrasport() { + // stop udp + if (udp != null) { + printLog("udp is going down", LogLevel.LOWER); + udp.halt(); + udp = null; + } + // stop tcp + if (tcp_server != null) { + printLog("tcp is going down", LogLevel.LOWER); + tcp_server.halt(); + tcp_server = null; + } + haltConnections(); + connections = null; + } + + public void haltConnections() { // modified + if (connections != null) { + printLog("connections are going down", LogLevel.LOWER); + for (Enumeration e = connections.elements(); e + .hasMoreElements();) { + ConnectedTransport c = e.nextElement(); + c.halt(); + } + connections = new Hashtable(10); + } + } + + /** Stops the SipProviders. */ + public void halt() { + printLog("halt: SipProvider is going down", LogLevel.MEDIUM); + stopTrasport(); + listeners = new Hashtable(10); + exception_listeners = new HashSet(); + } + + /** Parses a single line (loaded from the config file) */ + public void parseLine(String line) { + String attribute; + Parser par; + int index = line.indexOf("="); + if (index > 0) { + attribute = line.substring(0, index).trim(); + par = new Parser(line, index + 1); + } else { + attribute = line; + par = new Parser(""); + } + char[] delim = { ' ', ',' }; + + if (attribute.equals("via_addr")) { + via_addr = par.getString(); + return; + } + if (attribute.equals("host_port")) { + host_port = par.getInt(); + return; + } + if (attribute.equals("host_ifaddr")) { + host_ifaddr = par.getString(); + return; + } + if (attribute.equals("transport_protocols")) { + transport_protocols = par.getWordArray(delim); + return; + } + if (attribute.equals("nmax_connections")) { + nmax_connections = par.getInt(); + return; + } + if (attribute.equals("outbound_proxy")) { + String soaddr = par.getString(); + if (soaddr == null || soaddr.length() == 0 + || soaddr.equalsIgnoreCase(Configure.NONE) + || soaddr.equalsIgnoreCase("NO-OUTBOUND")) + outbound_proxy = null; + else + outbound_proxy = new SocketAddress(soaddr); + return; + } + if (attribute.equals("log_all_packets")) { + log_all_packets = (par.getString().toLowerCase().startsWith("y")); + return; + } + + // old parameters + if (attribute.equals("host_addr")) + System.err + .println("WARNING: parameter 'host_addr' is no more supported; use 'via_addr' instead."); + if (attribute.equals("all_interfaces")) + System.err + .println("WARNING: parameter 'all_interfaces' is no more supported; use 'host_iaddr' for setting a specific interface or let it undefined."); + if (attribute.equals("use_outbound")) + System.err + .println("WARNING: parameter 'use_outbound' is no more supported; use 'outbound_proxy' for setting an outbound proxy or let it undefined."); + if (attribute.equals("outbound_addr")) { + System.err + .println("WARNING: parameter 'outbound_addr' has been deprecated; use 'outbound_proxy=[:]' instead."); + outbound_addr = par.getString(); + return; + } + if (attribute.equals("outbound_port")) { + System.err + .println("WARNING: parameter 'outbound_port' has been deprecated; use 'outbound_proxy=[:]' instead."); + outbound_port = par.getInt(); + return; + } + } + + /** Converts the entire object into lines (to be saved into the config file) */ + protected String toLines() { // currently not implemented.. + return toString(); + } + + /** Gets a String with the list of transport protocols. */ + private String transportProtocolsToString() { + String list = transport_protocols[0]; + for (int i = 1; i < transport_protocols.length; i++) + list += "/" + transport_protocols[i]; + return list; + } + + // ************************** Public methods ************************* + + /** Gets via address. */ + public String getViaAddress() { + via_addr = IpAddress.localIpAddress; + + return via_addr; + } + + /** Sets via address. */ + /* + * public void setViaAddress(String addr) { via_addr=addr; } + */ + + /** Gets host port. */ + public int getPort() { + return host_port; + } + + /** + * Whether binding the sip provider to all interfaces or only on the + * specified host address. + */ + public boolean isAllInterfaces() { + return host_ipaddr == null; + } + + /** Gets host interface IpAddress. */ + public IpAddress getInterfaceAddress() { + return host_ipaddr; + } + + /** Gets array of transport protocols. */ + public String[] getTransportProtocols() { + return transport_protocols; + } + + /** Gets the default transport protocol. */ + public String getDefaultTransport() { + return default_transport; + } + + /** Gets the default transport protocol. */ + public void setDefaultTransport(String proto) { + default_transport = proto; + } + + /** Sets rport support. */ + public void setRport(boolean flag) { + rport = flag; + } + + /** Whether using rport. */ + public boolean isRportSet() { + return rport; + } + + /** Sets 'force-rport' mode. */ + public void setForceRport(boolean flag) { + force_rport = flag; + } + + /** Whether using 'force-rport' mode. */ + public boolean isForceRportSet() { + return force_rport; + } + + /** Whether has outbound proxy. */ + public boolean hasOutboundProxy() { + return outbound_proxy != null; + } + + /** Gets the outbound proxy. */ + public SocketAddress getOutboundProxy() { + return outbound_proxy; + } + + String server; + + /** Sets the outbound proxy. Use 'null' for not using any outbound proxy. */ + public void setOutboundProxy(SocketAddress soaddr,String host) { + outbound_proxy = soaddr; + server = host; + } + + /** Removes the outbound proxy. */ + /* + * public void removeOutboundProxy() { setOutboundProxy(null); } + */ + + /** Gets the max number of (contemporary) open connections. */ + public int getNMaxConnections() { + return nmax_connections; + } + + /** Sets the max number of (contemporary) open connections. */ + public void setNMaxConnections(int n) { + nmax_connections = n; + } + + /** Gets event log. */ + public Log getLog() { + return event_log; + } + + /** Returns the list (Hashtable) of active listener_IDs. */ + public Hashtable getListeners() { + return listeners; + } + + /** + * Adds a new listener to the SipProvider for caputering any message in + * PROMISQUE mode. It is the same as using method + * addSipProviderListener(SipProvider.PROMISQUE,listener).

When + * capturing messages in promisque mode all messages are passed to the + * SipProviderListener before passing them to the specific listener (if + * present).
Note that more that one SipProviderListener can be active + * in promisque mode at the same time;in that case the same message is + * passed to all PROMISQUE SipProviderListeners. + * + * @param listener + * is the SipProviderListener. + * @return It returns true if the SipProviderListener is added, + * false if the listener_ID is already in use. + */ + public boolean addSipProviderPromisqueListener(SipProviderListener listener) { + return addSipProviderListener(PROMISQUE, listener); + } + + /** + * Adds a new listener to the SipProvider for caputering ANY message. It is + * the same as using method + * addSipProviderListener(SipProvider.ANY,listener). + * + * @param listener + * is the SipProviderListener. + * @return It returns true if the SipProviderListener is added, + * false if the listener_ID is already in use. + */ + public boolean addSipProviderListener(SipProviderListener listener) { + return addSipProviderListener(ANY, listener); + } + + /** + * Adds a new listener to the SipProvider. + * + * @param id + * is the unique identifier for the messages which the listener + * as to be associated to. It is used as key. It can identify a + * method, a transaction, or a dialog. Use SipProvider.ANY to + * capture all messages. Use SipProvider.PROMISQUE if you want to + * capture all message in promisque mode (letting other listeners + * to capture the same received messages). + * @param listener + * is the SipProviderListener for this message id. + * @return It returns true if the SipProviderListener is added, + * false if the listener_ID is already in use. + */ + public boolean addSipProviderListener(Identifier id, + SipProviderListener listener) { + printLog("adding SipProviderListener: " + id, LogLevel.MEDIUM); + boolean ret; + Identifier key = id; + if (listeners.containsKey(key)) { + printWarning( + "trying to add a SipProviderListener with a id that is already in use.", + LogLevel.HIGH); + ret = false; + } else { + listeners.put(key, listener); + ret = true; + } + + /* + if (listeners != null) { + String list = ""; + for (Enumeration e = listeners.keys(); e + .hasMoreElements();) + list += e.nextElement() + ", "; + printLog(listeners.size() + " listeners: " + list, LogLevel.LOW); + } + */ + return ret; + } + + /** + * Removes a SipProviderListener. + * + * @param id + * is the unique identifier used to select the listened messages. + * @return It returns true if the SipProviderListener is removed, + * false if the identifier is missed. + */ + public boolean removeSipProviderListener(Identifier id) { + printLog("removing SipProviderListener: " + id, LogLevel.MEDIUM); + boolean ret; + Identifier key = id; + if (!listeners.containsKey(key)) { + printWarning("trying to remove a missed SipProviderListener.", + LogLevel.HIGH); + ret = false; + } else { + listeners.remove(key); + ret = true; + } + + if (listeners != null) { + String list = ""; + for (Enumeration e = listeners.keys(); e + .hasMoreElements();) + list += e.nextElement() + ", "; + printLog(listeners.size() + " listeners: " + list, LogLevel.LOW); + } + return ret; + } + + /** + * Sets the SipProviderExceptionListener. The SipProviderExceptionListener + * is the listener for all exceptions thrown by the SipProviders. + * + * @param e_listener + * is the SipProviderExceptionListener. + * @return It returns true if the SipProviderListener has been + * correctly set, false if the SipProviderListener was + * already set. + */ + public boolean addSipProviderExceptionListener( + SipProviderExceptionListener e_listener) { + printLog("adding SipProviderExceptionListener", LogLevel.MEDIUM); + if (exception_listeners.contains(e_listener)) { + printWarning( + "trying to add an already present SipProviderExceptionListener.", + LogLevel.HIGH); + return false; + } else { + exception_listeners.add(e_listener); + return true; + } + } + + /** + * Removes a SipProviderExceptionListener. + * + * @param e_listener + * is the SipProviderExceptionListener. + * @return It returns true if the SipProviderExceptionListener has + * been correctly removed, false if the + * SipProviderExceptionListener is missed. + */ + public boolean removeSipProviderExceptionListener( + SipProviderExceptionListener e_listener) { + printLog("removing SipProviderExceptionListener", LogLevel.MEDIUM); + if (!exception_listeners.contains(e_listener)) { + printWarning( + "trying to remove a missed SipProviderExceptionListener.", + LogLevel.HIGH); + return false; + } else { + exception_listeners.remove(e_listener); + return true; + } + } + + /** + * Sends a Message, specifing the transport portocol, nexthop address and + * port. + *

+ * This is a low level method and forces the message to be routed to a + * specific nexthop address, port and transport, regardless whatever the + * Via, Route, or request-uri, address to. + *

+ * In case of connection-oriented transport, the connection is selected as + * follows:
- if an existing connection is found matching the + * destination end point (socket), such connection is used, otherwise
- + * a new connection is established + * + * @return It returns a Connection in case of connection-oriented delivery + * (e.g. TCP) or null in case of connection-less delivery (e.g. UDP) + */ + public ConnectionIdentifier sendMessage(Message msg, String proto, + String dest_addr, int dest_port, int ttl) { + if (log_all_packets || msg.getLength() > MIN_MESSAGE_LENGTH) + printLog("Resolving host address '" + dest_addr + "'", + LogLevel.MEDIUM); + try { + IpAddress dest_ipaddr = IpAddress.getByName(dest_addr); + return sendMessage(msg, proto, dest_ipaddr, dest_port, ttl); + } catch (Exception e) { + printException(e, LogLevel.HIGH); + return null; + } + } + + /** + * Sends a Message, specifing the transport portocol, nexthop address and + * port. + */ + private ConnectionIdentifier sendMessage(Message msg, String proto, + IpAddress dest_ipaddr, int dest_port, int ttl) { + ConnectionIdentifier conn_id = new ConnectionIdentifier(proto, + dest_ipaddr, dest_port); + if (log_all_packets || msg.getLength() > MIN_MESSAGE_LENGTH) + printLog("Sending message to " + conn_id, LogLevel.MEDIUM); + + if (transport_udp && proto.equals(PROTO_UDP)) { // UDP + // printLog("using UDP",LogLevel.LOW); + conn_id = null; + try { // if (ttl>0 && multicast_address) do something? + udp.sendMessage(msg, dest_ipaddr, dest_port); + } catch (IOException e) { + printException(e, LogLevel.HIGH); + return null; + } + } else if (transport_tcp && proto.equals(PROTO_TCP)) { // TCP + // printLog("using TCP",LogLevel.LOW); + if (connections == null || !connections.containsKey(conn_id)) { // modified + printLog("no active connection found matching " + conn_id, + LogLevel.MEDIUM); + printLog("open " + proto + " connection to " + dest_ipaddr + + ":" + dest_port, LogLevel.MEDIUM); + TcpTransport conn = null; + try { + conn = new TcpTransport(dest_ipaddr, dest_port, this, server); + } catch (Exception e) { + printLog("connection setup FAILED", LogLevel.HIGH); + return null; + } + printLog("connection " + conn + " opened", LogLevel.HIGH); + addConnection(conn); + if (!msg.isRegister()) + Receiver.engine(Receiver.mContext).register(); // modified + } else { + printLog("active connection found matching " + conn_id, + LogLevel.MEDIUM); + } + ConnectedTransport conn = (ConnectedTransport) connections + .get(conn_id); + if (conn != null) { + printLog("sending data through conn " + conn, LogLevel.MEDIUM); + try { + conn.sendMessage(msg); + conn_id = new ConnectionIdentifier(conn); + } catch (IOException e) { + printException(e, LogLevel.HIGH); + return null; + } + } else { // this point has not to be reached + printLog("ERROR: conn " + conn_id + " not found: abort.", + LogLevel.MEDIUM); + return null; + } + } else { // otherwise + printWarning("Unsupported protocol (" + proto + + "): Message discarded", LogLevel.HIGH); + return null; + } + // logs + String dest_addr = dest_ipaddr.toString(); + printMessageLog(proto, dest_addr, dest_port, msg.getLength(), msg, + "sent"); + return conn_id; + } + + /** + * Sends the message msg. + *

+ * The destination for the request is computed as follows:
- if + * outbound_addr is set, outbound_addr and outbound_port + * are used, otherwise
- if message has Route header with lr option + * parameter (i.e. RFC3261 compliant), the first Route address is used, + * otherwise
- the request's Request-URI is considered. + *

+ * The destination for the response is computed based on the sent-by + * parameter in the Via header field (RFC3261 compliant) + *

+ * As transport it is used the protocol specified in the 'via' header field + *

+ * In case of connection-oriented transport:
- if an already + * established connection is found matching the destination end point + * (socket), such connection is used, otherwise
- a new connection is + * established + * + * @return Returns a ConnectionIdentifier in case of connection-oriented + * delivery (e.g. TCP) or null in case of connection-less delivery + * (e.g. UDP) + */ + public ConnectionIdentifier sendMessage(Message msg) { + printLog("Sending message:\r\n" + msg.toString(), LogLevel.LOWER); + + // select the transport protocol + ViaHeader via = msg.getViaHeader(); + String proto; + if (via != null) + proto = via.getProtocol().toLowerCase(); + else + proto = getDefaultTransport().toLowerCase(); // modified + printLog("using transport " + proto, LogLevel.MEDIUM); + + // select the destination address and port + String dest_addr = null; + int dest_port = 0; + int ttl = 0; + + if (!msg.isResponse()) { // modified + if (outbound_proxy != null) { + dest_addr = outbound_proxy.getAddress().toString(); + dest_port = outbound_proxy.getPort(); + } else { + if (msg.hasRouteHeader() + && msg.getRouteHeader().getNameAddress().getAddress() + .hasLr()) { + SipURL url = msg.getRouteHeader().getNameAddress() + .getAddress(); + dest_addr = url.getHost(); + dest_port = url.getPort(); + } else { + SipURL url = msg.getRequestLine().getAddress(); + dest_addr = url.getHost(); + dest_port = url.getPort(); + if (url.hasMaddr()) { + dest_addr = url.getMaddr(); + if (url.hasTtl()) + ttl = url.getTtl(); + // update the via header by adding maddr and ttl params + via.setMaddr(dest_addr); + if (ttl > 0) + via.setTtl(ttl); + msg.removeViaHeader(); + msg.addViaHeader(via); + } + } + } + } else { // RESPONSES + SipURL url = via.getSipURL(); + if (via.hasReceived()) + dest_addr = via.getReceived(); + else + dest_addr = url.getHost(); + if (via.hasRport()) + dest_port = via.getRport(); + if (dest_port <= 0) + dest_port = url.getPort(); + } + + if (dest_port <= 0) + dest_port = SipStack.default_port; + + return sendMessage(msg, proto, dest_addr, dest_port, ttl); + } + + /** Sends the message msg using the specified connection. */ + public ConnectionIdentifier sendMessage(Message msg, + ConnectionIdentifier conn_id) { + if (log_all_packets || msg.getLength() > MIN_MESSAGE_LENGTH) + printLog("Sending message through conn " + conn_id, LogLevel.HIGH); + printLog("message:\r\n" + msg.toString(), LogLevel.LOWER); + + if (conn_id != null && connections.containsKey(conn_id)) { // connection + // exists + printLog("active connection found matching " + conn_id, + LogLevel.MEDIUM); + ConnectedTransport conn = (ConnectedTransport) connections + .get(conn_id); + try { + conn.sendMessage(msg); + // logs + // String proto=conn.getProtocol(); + String proto = conn.getProtocol(); + String dest_addr = conn.getRemoteAddress().toString(); + int dest_port = conn.getRemotePort(); + printMessageLog(proto, dest_addr, dest_port, msg.getLength(), + msg, "sent"); + return conn_id; + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + // else + printLog("no active connection found matching " + conn_id, + LogLevel.MEDIUM); + return sendMessage(msg); + } + + /** + * Processes the message received. It is called each time a new message is + * received by the transport layer, and it performs the actual message + * processing. + */ + protected void processReceivedMessage(Message msg) { + try { // logs + printMessageLog(msg.getTransportProtocol(), msg.getRemoteAddress(), + msg.getRemotePort(), msg.getLength(), msg, "received"); + + // discard too short messages + if (msg.getLength() <= 2) { + if (log_all_packets) + printLog("message too short: discarded\r\n", LogLevel.LOW); + return; + } + // discard non-SIP messages + String first_line = msg.getFirstLine(); + if (first_line == null + || first_line.toUpperCase().indexOf("SIP/2.0") < 0) { + if (log_all_packets) + printLog("NOT a SIP message: discarded\r\n", LogLevel.LOW); + return; + } + if (!Sipdroid.release) + printLog("received new SIP message "+msg.getRequestLine()+" "+msg.getStatusLine(), LogLevel.HIGH); // modified + printLog("message:\r\n" + msg.toString(), LogLevel.LOWER); + + // if a request, handle "received" and "rport" parameters + if (msg.isRequest()) { + ViaHeader vh = msg.getViaHeader(); + boolean via_changed = false; + String src_addr = msg.getRemoteAddress(); + int src_port = msg.getRemotePort(); + String via_addr = vh.getHost(); + int via_port = vh.getPort(); + if (via_port <= 0) + via_port = SipStack.default_port; + + if (!via_addr.equals(src_addr)) { + vh.setReceived(src_addr); + via_changed = true; + } + + if (vh.hasRport()) { + vh.setRport(src_port); + via_changed = true; + } else { + if (force_rport && via_port != src_port) { + vh.setRport(src_port); + via_changed = true; + } + } + + if (via_changed) { + msg.removeViaHeader(); + msg.addViaHeader(vh); + } + } + + // is there any listeners? + if (listeners == null || listeners.size() == 0) { + printLog("no listener found: message discarded.", LogLevel.HIGH); + return; + } + + // try to look for a UA in promisque mode + if (listeners.containsKey(PROMISQUE)) { + printLog("message passed to uas: " + PROMISQUE, LogLevel.MEDIUM); + ((SipProviderListener) listeners.get(PROMISQUE)) + .onReceivedMessage(this, msg); + } + + // after the callback check if the message is still valid + if (!msg.isRequest() && !msg.isResponse()) { + printLog("No valid SIP message: message discarded.", + LogLevel.HIGH); + return; + } + + // this was the promisque listener; now keep on looking for a + // tighter listener.. + + // try to look for a transaction + Identifier key = msg.getTransactionId(); + printLog("DEBUG: transaction-id: " + key, LogLevel.MEDIUM); + if (listeners.containsKey(key)) { + printLog("message passed to transaction: " + key, + LogLevel.MEDIUM); + SipProviderListener sip_listener = (SipProviderListener) listeners.get(key); + if (sip_listener != null) + { + sip_listener.onReceivedMessage( + this, msg); + } + return; + } + // try to look for a dialog + key = msg.getDialogId(); + printLog("DEBUG: dialog-id: " + key, LogLevel.MEDIUM); + if (listeners.containsKey(key)) { + printLog("message passed to dialog: " + key, LogLevel.MEDIUM); + ((SipProviderListener) listeners.get(key)).onReceivedMessage( + this, msg); + return; + } + // try to look for a UAS + key = msg.getMethodId(); + if (listeners.containsKey(key)) { + printLog("message passed to uas: " + key, LogLevel.MEDIUM); + ((SipProviderListener) listeners.get(key)).onReceivedMessage( + this, msg); + return; + } + // try to look for a default UA + if (listeners.containsKey(ANY)) { + printLog("message passed to uas: " + ANY, LogLevel.MEDIUM); + ((SipProviderListener) listeners.get(ANY)).onReceivedMessage( + this, msg); + return; + } + + // if we are here, no listener_ID matched.. + printLog( + "No SipListener found matching that message: message DISCARDED", + LogLevel.HIGH); + // printLog("Pending SipProviderListeners= + // "+getListeners().size(),3); + printLog("Pending SipProviderListeners= " + listeners.size(), + LogLevel.MEDIUM); + } catch (Exception e) { + printWarning("Error handling a new incoming message", LogLevel.HIGH); + printException(e, LogLevel.MEDIUM); + if (exception_listeners == null || exception_listeners.size() == 0) { + System.err.println("Error handling a new incoming message"); + e.printStackTrace(); + } else { + for (Iterator i = exception_listeners.iterator(); i.hasNext();) + try { + ((SipProviderExceptionListener) i.next()) + .onMessageException(msg, e); + } catch (Exception e2) { + printWarning("Error handling handling the Exception", + LogLevel.HIGH); + printException(e2, LogLevel.MEDIUM); + } + } + } + } + + /** Adds a new Connection */ + private void addConnection(ConnectedTransport conn) { + ConnectionIdentifier conn_id = new ConnectionIdentifier(conn); + if (connections.containsKey(conn_id)) { // remove the previous + // connection + printLog("trying to add the already established connection " + + conn_id, LogLevel.HIGH); + printLog("connection " + conn_id + " will be replaced", + LogLevel.HIGH); + ConnectedTransport old_conn = (ConnectedTransport) connections + .get(conn_id); + old_conn.halt(); + connections.remove(conn_id); + } else if (connections.size() >= nmax_connections) { // remove the + // older unused + // connection + printLog( + "reached the maximum number of connection: removing the older unused connection", + LogLevel.HIGH); + long older_time = System.currentTimeMillis(); + ConnectionIdentifier older_id = null; + for (Enumeration e = connections.elements(); e + .hasMoreElements();) { + ConnectedTransport co = e.nextElement(); + if (co.getLastTimeMillis() < older_time) + older_id = new ConnectionIdentifier(co); + } + if (older_id != null) + removeConnection(older_id); + } + connections.put(conn_id, conn); + conn_id = new ConnectionIdentifier(conn); + conn = (ConnectedTransport) connections.get(conn_id); + // DEBUG log: + printLog("active connenctions:", LogLevel.LOW); + for (Enumeration e = connections.keys(); e + .hasMoreElements();) { + ConnectionIdentifier id = (ConnectionIdentifier) e.nextElement(); + printLog("conn-id=" + id + ": " + + ((ConnectedTransport) connections.get(id)).toString(), + LogLevel.LOW); + } + } + + /** Removes a Connection */ + private void removeConnection(ConnectionIdentifier conn_id) { + if (connections != null && connections.containsKey(conn_id)) { // modified + ConnectedTransport conn = (ConnectedTransport) connections + .get(conn_id); + if (conn != null) conn.halt(); + connections.remove(conn_id); + // DEBUG log: + printLog("active connenctions:", LogLevel.LOW); + for (Enumeration e = connections.elements(); e + .hasMoreElements();) { + ConnectedTransport co = (ConnectedTransport) e.nextElement(); + printLog("conn " + co.toString(), LogLevel.LOW); + } + } + } + + // ************************* Callback methods ************************* + PowerManager pm; + PowerManager.WakeLock wl; + + /** When a new SIP message is received. */ + public void onReceivedMessage(Transport transport, Message msg) { + if (pm == null) { + pm = (PowerManager) Receiver.mContext.getSystemService(Context.POWER_SERVICE); + wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Sipdroid.SipProvider"); + } + wl.acquire(); // modified + processReceivedMessage(msg); + wl.release(); + } + + /** When Transport terminates. */ + public void onTransportTerminated(Transport transport, Exception error) { + printLog("transport " + transport + " terminated", LogLevel.MEDIUM); + if (transport.getProtocol().equals(PROTO_TCP)) { + ConnectionIdentifier conn_id = new ConnectionIdentifier( + (ConnectedTransport) transport); + removeConnection(conn_id); + if (Sipdroid.on(Receiver.mContext)) + Receiver.engine(Receiver.mContext).register(); // modified + } + if (error != null) + printException(error, LogLevel.HIGH); + } + + /** When a new incoming Connection is established */ + public void onIncomingConnection(TcpServer tcp_server, TcpSocket socket) { + printLog("incoming connection from " + socket.getAddress() + ":" + + socket.getPort(), LogLevel.MEDIUM); + ConnectedTransport conn = new TcpTransport(socket, this); + printLog("tcp connection " + conn + " opened", LogLevel.MEDIUM); + addConnection(conn); + } + + /** When TcpServer terminates. */ + public void onServerTerminated(TcpServer tcp_server, Exception error) { + printLog("tcp server " + tcp_server + " terminated", LogLevel.MEDIUM); + } + + // ************************** Other methods *************************** + + /** + * Picks a fresh branch value. The branch ID MUST be unique across space and + * time for all requests sent by the UA. The branch ID always begin with the + * characters "z9hG4bK". These 7 characters are used by RFC 3261 as a magic + * cookie. + */ + public static String pickBranch() { // String + // str=Long.toString(Math.abs(Random.nextLong()),16); + // if (str.length()<5) str+="00000"; + // return "z9hG4bK"+str.substring(0,5); + return "z9hG4bK" + Random.nextNumString(5); + } + + /** + * Picks an unique branch value based on a SIP message. This value could + * also be used as transaction ID + */ + public String pickBranch(Message msg) { + StringBuffer sb = new StringBuffer(); + sb.append(msg.getRequestLine().getAddress().toString()); + sb.append(getViaAddress() + getPort()); + ViaHeader top_via = msg.getViaHeader(); + if (top_via.hasBranch()) + sb.append(top_via.getBranch()); + else { + sb.append(top_via.getHost() + top_via.getPort()); + sb.append(msg.getCSeqHeader().getSequenceNumber()); + sb.append(msg.getCallIdHeader().getCallId()); + sb.append(msg.getFromHeader().getTag()); + sb.append(msg.getToHeader().getTag()); + } + // return "z9hG4bK"+(new MD5(unique_str)).asHex().substring(0,9); + return "z9hG4bK" + (new SimpleDigest(5, sb.toString())).asHex(); + } + + /** + * Picks a new tag. A tag MUST be globally unique and cryptographically + * random with at least 32 bits of randomness. A property of this selection + * requirement is that a UA will place a different tag into the From header + * of an INVITE than it would place into the To header of the response to + * the same INVITE. This is needed in order for a UA to invite itself to a + * session. + */ + public static String pickTag() { // String + // str=Long.toString(Math.abs(Random.nextLong()),16); + // if (str.length()<8) str+="00000000"; + // return str.substring(0,8); + return "z9hG4bK" + Random.nextNumString(8); + } + + /** + * Picks a new tag. The tag is generated uniquely based on message req. + * This tag can be generated for responses in a stateless manner - in a + * manner that will generate the same tag for the same request consistently. + */ + public static String pickTag(Message req) { // return + // String.valueOf(tag_generator++); + // return (new MD5(request.toString())).asHex().substring(0,8); + return (new SimpleDigest(8, req.toString())).asHex(); + } + + /** + * Picks a new call-id. The call-id is a globally unique identifier over + * space and time. It is implemented in the form "localid@host". Call-id + * must be considered case-sensitive and is compared byte-by-byte. + */ + public String pickCallId() { // String + // str=Long.toString(Math.abs(Random.nextLong()),16); + // if (str.length()<12) str+="000000000000"; + // return str.substring(0,12)+"@"+getViaAddress(); + return Random.nextNumString(12) + "@" + getViaAddress(); + } + + /** picks an initial CSeq */ + public static int pickInitialCSeq() { + return 1; + } + + /** + * (Deprecated) Constructs a NameAddress based on an input string. + * The input string can be a:
- user name,
- + * user@address url,
- "Name" <sip:user@address> + * address, + *

+ * In the former case, a SIP URL is costructed using the outbound proxy as + * host address if present, otherwise the local via address is used. + */ + public NameAddress completeNameAddress(String str) { + if (str.indexOf("= 0) + return new NameAddress(str); + else { + SipURL url = completeSipURL(str); + return new NameAddress(url); + } + } + + /** Constructs a SipURL based on an input string. */ + private SipURL completeSipURL(String str) { // in case it is passed only the + // 'user' field, add + // '@'[':'] + if (!str.startsWith("sip:") && str.indexOf("@") < 0 + && str.indexOf(".") < 0 && str.indexOf(":") < 0) { // may be it + // is just + // the user + // name.. + String url = "sip:" + str + "@"; + if (outbound_proxy != null) { + url += outbound_proxy.getAddress().toString(); + int port = outbound_proxy.getPort(); + if (port > 0 && port != SipStack.default_port) + url += ":" + port; + } else { + url += getViaAddress(); + if (host_port > 0 && host_port != SipStack.default_port) + url += ":" + host_port; + } + return new SipURL(url); + } else + return new SipURL(str); + } + + /** + * Constructs a SipURL for the given username on the local SIP UA. + * If username is null, only host address and port are used. + */ + /* + * public SipURL getSipURL(String user_name) { return new + * SipURL(user_name,via_addr,(host_port!=SipStack.default_port)?host_port:-1); } + */ + + // ******************************* Logs ******************************* + /** Gets a String value for this object */ + public String toString() { + if (host_ipaddr == null) + return host_port + "/" + transportProtocolsToString(); + else + return host_ipaddr.toString() + ":" + host_port + "/" + + transportProtocolsToString(); + } + + /** Adds a new string to the default Log */ + private final void printLog(String str, int level) { + if (Sipdroid.release) return; + if (event_log != null) { + String provider_id = (host_ipaddr == null) ? Integer + .toString(host_port) : host_ipaddr.toString() + ":" + + host_port; + event_log.println("SipProvider-" + provider_id + ": " + str, level + + SipStack.LOG_LEVEL_TRANSPORT); + } + if (level <= LogLevel.HIGH) System.out.println("SipProvider: " + str); // modified + } + + /** Adds a WARNING to the default Log */ + private final void printWarning(String str, int level) { + printLog("WARNING: " + str, level); + } + + /** Adds the Exception message to the default Log */ + private final void printException(Exception e, int level) { + if (event_log != null) + event_log.printException(e, level + SipStack.LOG_LEVEL_TRANSPORT); + } + + /** Adds the SIP message to the messageslog */ + private final void printMessageLog(String proto, String addr, int port, + int len, Message msg, String str) { + if (log_all_packets || len >= MIN_MESSAGE_LENGTH) { + if (message_log != null) { + message_log.printPacketTimestamp(proto, addr, port, len, str + + "\r\n" + msg.toString() + + "-----End-of-message-----\r\n", 1); + } + if (event_log != null) { + String first_line = msg.getFirstLine(); + if (first_line != null) + first_line = first_line.trim(); + else + first_line = "NOT a SIP message"; + event_log.print("\r\n"); + event_log.printPacketTimestamp(proto, addr, port, len, + first_line + ", " + str, 1); + event_log.print("\r\n"); + } + } + } + +} diff --git a/src/org/zoolu/sip/provider/SipProviderExceptionListener.java b/app/src/main/java/org/zoolu/sip/provider/SipProviderExceptionListener.java similarity index 97% rename from src/org/zoolu/sip/provider/SipProviderExceptionListener.java rename to app/src/main/java/org/zoolu/sip/provider/SipProviderExceptionListener.java index e42400a..5603748 100644 --- a/src/org/zoolu/sip/provider/SipProviderExceptionListener.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipProviderExceptionListener.java @@ -1,34 +1,34 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; - -/** - * A SipProviderExceptionListener listens for SipProvider - * onMessageException(Message,Exception) events. - */ -public interface SipProviderExceptionListener { - public void onMessageException(Message msg, Exception e); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; + +/** + * A SipProviderExceptionListener listens for SipProvider + * onMessageException(Message,Exception) events. + */ +public interface SipProviderExceptionListener { + public void onMessageException(Message msg, Exception e); +} diff --git a/src/org/zoolu/sip/provider/SipProviderListener.java b/app/src/main/java/org/zoolu/sip/provider/SipProviderListener.java similarity index 97% rename from src/org/zoolu/sip/provider/SipProviderListener.java rename to app/src/main/java/org/zoolu/sip/provider/SipProviderListener.java index 46efc6e..704a486 100644 --- a/src/org/zoolu/sip/provider/SipProviderListener.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipProviderListener.java @@ -1,35 +1,35 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; - -/** - * A SipProviderListener listens for SipProvider - * onReceivedMessage(SipProvider,Message) events. - */ -public interface SipProviderListener { - /** When a new Message is received by the SipProvider. */ - public void onReceivedMessage(SipProvider sip_provider, Message message); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; + +/** + * A SipProviderListener listens for SipProvider + * onReceivedMessage(SipProvider,Message) events. + */ +public interface SipProviderListener { + /** When a new Message is received by the SipProvider. */ + public void onReceivedMessage(SipProvider sip_provider, Message message); +} diff --git a/src/org/zoolu/sip/provider/SipStack.java b/app/src/main/java/org/zoolu/sip/provider/SipStack.java similarity index 97% rename from src/org/zoolu/sip/provider/SipStack.java rename to app/src/main/java/org/zoolu/sip/provider/SipStack.java index 9371702..00ea4a8 100644 --- a/src/org/zoolu/sip/provider/SipStack.java +++ b/app/src/main/java/org/zoolu/sip/provider/SipStack.java @@ -1,351 +1,351 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.tools.Configure; -import org.zoolu.tools.Parser; -import org.zoolu.tools.RotatingLog; -import org.zoolu.tools.Timer; - -/** - * SipStack includes all static attributes used by the sip stack. - *

- * Static attributes includes the logging configuration, default SIP port, - * deafult supported transport protocols, timeouts, etc. - */ -public class SipStack extends Configure { - // ********************** private attributes ********************** - - /** Whether SipStack configuration has been already loaded */ - private static boolean is_init = false; - - /** The default SipProvider */ - // private static SipProvider provider=null; - // *********************** software release *********************** - /** Release */ - public static final String release = "mjsip stack 1.6"; - /** Authors */ - public static final String authors = "Luca Veltri - University of Parma (Italy)"; - - // ********************** static attributes *********************** - - /** String value "no-ua-info" used for setting no 'User-Agent' header filed. */ - // public static final String NO_UA_INFO="NO-UA-INFO"; - /** String value "no-server-info" used for setting no 'Server' header filed. */ - // public static final String NO_SERVER_INFO="NO-SERVER-INFO"; - // ************* default sip provider configurations ************** - /** - * Default SIP port. Note that this is not the port used by the running - * stack, but simply the standard default SIP port.
- * Normally it sould be set to 5060 as defined by RFC 3261. Using a - * different value may cause some problems when interacting with other - * unaware SIP UAs. - */ - public static int default_port = 5060; - /** Default supported transport protocols. */ - public static String[] default_transport_protocols = { - SipProvider.PROTO_UDP, SipProvider.PROTO_TCP }; - /** Default max number of contemporary open transport connections. */ - public static int default_nmax_connections = 32; - /** - * Whether adding 'rport' parameter on via header fields of outgoing - * requests. - */ - public static boolean use_rport = true; - /** - * Whether adding (forcing) 'rport' parameter on via header fields of - * incoming requests. - */ - public static boolean force_rport = false; - - // ******************** general configurations ******************** - - /** default max-forwards value (RFC3261 recommends value 70) */ - public static int max_forwards = 70; - /** - * starting retransmission timeout (milliseconds); called T1 in RFC2361; - * they suggest T1=500ms - */ - public static long retransmission_timeout = 2000; - /** - * maximum retransmission timeout (milliseconds); called T2 in RFC2361; they - * suggest T2=4sec - */ - public static long max_retransmission_timeout = 16000; - /** transaction timeout (milliseconds); RFC2361 suggests 64*T1=32000ms */ - public static long transaction_timeout = 128000; - /** clearing timeout (milliseconds); T4 in RFC2361; they suggest T4=5sec */ - public static long clearing_timeout = 5000; - - /** Whether using only one thread for all timer instances. */ - public static boolean single_timer = false; - - /** - * Whether 1xx responses create an "early dialog" for methods that create - * dialog. - */ - public static boolean early_dialog = false; - - /** - * Default 'expires' value in seconds. RFC2361 suggests 3600s as default - * value. - */ - public static int default_expires = 3600; - - /** - * UA info included in request messages in the 'User-Agent' header field. - * Use "NONE" if the 'User-Agent' header filed must not be added. - */ - public static String ua_info = release; - /** - * Server info included in response messages in the 'Server' header field - * Use "NONE" if the 'Server' header filed must not be added. - */ - public static String server_info = release; - - // ************************ debug and logs ************************ - - /** Base level (offset) for logging Transport events */ - public static int LOG_LEVEL_TRANSPORT = 1; - /** Base level (offset) for logging Transaction events */ - public static int LOG_LEVEL_TRANSACTION = 2; - /** Base level (offset) for logging Dialog events */ - public static int LOG_LEVEL_DIALOG = 2; - /** Base level (offset) for logging Call events */ - public static int LOG_LEVEL_CALL = 1; - /** Base level (offset) for logging UA events */ - public static int LOG_LEVEL_UA = 0; - - /** Log level. Only logs with a level less or equal to this are written. */ - public static int debug_level = 1; - /** - * Path for the log folder where log files are written. By default, it is - * used the "./log" folder. Use ".", to store logs in the current root - * folder. - */ - public static String log_path = "log"; - /** The size limit of the log file [kB] */ - public static int max_logsize = 2048; // 2MB - /** - * The number of rotations of log files. Use '0' for NO rotation, '1' for - * rotating a single file - */ - public static int log_rotations = 0; // no rotation - /** - * The rotation period, in MONTHs or DAYs or HOURs or MINUTEs examples: - * log_rotation_time=3 MONTHS, log_rotations=90 DAYS Default value: - * log_rotation_time=2 MONTHS - */ - private static String log_rotation_time = null; - /** The rotation time scale */ - public static int rotation_scale = RotatingLog.MONTH; - /** The rotation time value */ - public static int rotation_time = 2; - - // ************************** costructor ************************** - - /** Parses a single text line (read from the config file) */ - protected void parseLine(String line) { - String attribute; - Parser par; - int index = line.indexOf("="); - if (index > 0) { - attribute = line.substring(0, index).trim(); - par = new Parser(line, index + 1); - } else { - attribute = line; - par = new Parser(""); - } - char[] delim = { ' ', ',' }; - - // general configurations - if (attribute.equals("default_port")) { - default_port = par.getInt(); - return; - } - if (attribute.equals("default_transport_protocols")) { - default_transport_protocols = par.getWordArray(delim); - return; - } - if (attribute.equals("default_nmax_connections")) { - default_nmax_connections = par.getInt(); - return; - } - if (attribute.equals("use_rport")) { - use_rport = (par.getString().toLowerCase().startsWith("y")); - return; - } - if (attribute.equals("force_rport")) { - force_rport = (par.getString().toLowerCase().startsWith("y")); - return; - } - if (attribute.equals("max_forwards")) { - max_forwards = par.getInt(); - return; - } - if (attribute.equals("retransmission_timeout")) { - retransmission_timeout = par.getInt(); - return; - } - if (attribute.equals("max_retransmission_timeout")) { - max_retransmission_timeout = par.getInt(); - return; - } - if (attribute.equals("transaction_timeout")) { - transaction_timeout = par.getInt(); - return; - } - if (attribute.equals("clearing_timeout")) { - clearing_timeout = par.getInt(); - return; - } - if (attribute.equals("single_timer")) { - single_timer = (par.getString().toLowerCase().startsWith("y")); - return; - } - if (attribute.equals("early_dialog")) { - early_dialog = (par.getString().toLowerCase().startsWith("y")); - return; - } - if (attribute.equals("default_expires")) { - default_expires = par.getInt(); - return; - } - if (attribute.equals("ua_info")) { - ua_info = par.getRemainingString().trim(); - return; - } - if (attribute.equals("server_info")) { - server_info = par.getRemainingString().trim(); - return; - } - - // debug and logs - if (attribute.equals("debug_level")) { - debug_level = par.getInt(); - return; - } - if (attribute.equals("log_path")) { - log_path = par.getString(); - return; - } - if (attribute.equals("max_logsize")) { - max_logsize = par.getInt(); - return; - } - if (attribute.equals("log_rotations")) { - log_rotations = par.getInt(); - return; - } - if (attribute.equals("log_rotation_time")) { - log_rotation_time = par.getRemainingString(); - return; - } - - // old parameters - if (attribute.equals("host_addr")) - printLog("WARNING: parameter 'host_addr' is no more supported; use 'via_addr' instead."); - if (attribute.equals("all_interfaces")) - printLog("WARNING: parameter 'all_interfaces' is no more supported; use 'host_iaddr' for setting a specific interface or let it undefined."); - if (attribute.equals("use_outbound")) - printLog("WARNING: parameter 'use_outbound' is no more supported; use 'outbound_addr' for setting an outbound proxy or let it undefined."); - if (attribute.equals("log_file")) - printLog("WARNING: parameter 'log_file' is no more supported."); - } - - /** Converts the entire object into lines (to be saved into the config file) */ - protected String toLines() { // currently not implemented.. - return "SipStack/" + release; - } - - /** Costructs a non-static SipStack */ - private SipStack() { - } - - /** Inits SipStack */ - public static void init() { - init(null); - } - - /** Inits SipStack from the specified file */ - public static void init(String file) { - (new SipStack()).loadFile(file); - - // user-agent info - if (ua_info != null - && (ua_info.length() == 0 - || ua_info.equalsIgnoreCase(Configure.NONE) || ua_info - .equalsIgnoreCase("NO-UA-INFO"))) - ua_info = null; - - // server info - if (server_info != null - && (server_info.length() == 0 - || server_info.equalsIgnoreCase(Configure.NONE) || server_info - .equalsIgnoreCase("NO-SERVER-INFO"))) - server_info = null; - - // timers - Timer.SINGLE_THREAD = single_timer; - - // logs - if (debug_level > 0) { - if (log_rotation_time != null) { - SipParser par = new SipParser(log_rotation_time); - rotation_time = par.getInt(); - String scale = par.getString(); - if (scale == null) - scale = "null"; - if (scale.toUpperCase().startsWith("MONTH")) - rotation_scale = RotatingLog.MONTH; - else if (scale.toUpperCase().startsWith("DAY")) - rotation_scale = RotatingLog.DAY; - else if (scale.toUpperCase().startsWith("HOUR")) - rotation_scale = RotatingLog.HOUR; - else if (scale.toUpperCase().startsWith("MINUTE")) - rotation_scale = RotatingLog.MINUTE; - else { - rotation_time = 7; - rotation_scale = RotatingLog.DAY; - printLog("Error with the log rotation time. Logs will rotate every week."); - } - } - } - - is_init = true; - // if (file!=null) printLog("SipStack loaded",1); - } - - /** Whether SipStack has been already initialized */ - public static boolean isInit() { - return is_init; - } - - // ************************ private methods *********************** - - /** Logs a string message. */ - private static void printLog(String str) { - System.out.println("SipStack: " + str); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.tools.Configure; +import org.zoolu.tools.Parser; +import org.zoolu.tools.RotatingLog; +import org.zoolu.tools.Timer; + +/** + * SipStack includes all static attributes used by the sip stack. + *

+ * Static attributes includes the logging configuration, default SIP port, + * deafult supported transport protocols, timeouts, etc. + */ +public class SipStack extends Configure { + // ********************** private attributes ********************** + + /** Whether SipStack configuration has been already loaded */ + private static boolean is_init = false; + + /** The default SipProvider */ + // private static SipProvider provider=null; + // *********************** software release *********************** + /** Release */ + public static final String release = "mjsip stack 1.6"; + /** Authors */ + public static final String authors = "Luca Veltri - University of Parma (Italy)"; + + // ********************** static attributes *********************** + + /** String value "no-ua-info" used for setting no 'User-Agent' header filed. */ + // public static final String NO_UA_INFO="NO-UA-INFO"; + /** String value "no-server-info" used for setting no 'Server' header filed. */ + // public static final String NO_SERVER_INFO="NO-SERVER-INFO"; + // ************* default sip provider configurations ************** + /** + * Default SIP port. Note that this is not the port used by the running + * stack, but simply the standard default SIP port.
+ * Normally it sould be set to 5060 as defined by RFC 3261. Using a + * different value may cause some problems when interacting with other + * unaware SIP UAs. + */ + public static int default_port = 5060; + /** Default supported transport protocols. */ + public static String[] default_transport_protocols = { + SipProvider.PROTO_UDP, SipProvider.PROTO_TCP }; + /** Default max number of contemporary open transport connections. */ + public static int default_nmax_connections = 32; + /** + * Whether adding 'rport' parameter on via header fields of outgoing + * requests. + */ + public static boolean use_rport = true; + /** + * Whether adding (forcing) 'rport' parameter on via header fields of + * incoming requests. + */ + public static boolean force_rport = false; + + // ******************** general configurations ******************** + + /** default max-forwards value (RFC3261 recommends value 70) */ + public static int max_forwards = 70; + /** + * starting retransmission timeout (milliseconds); called T1 in RFC2361; + * they suggest T1=500ms + */ + public static long retransmission_timeout = 2000; + /** + * maximum retransmission timeout (milliseconds); called T2 in RFC2361; they + * suggest T2=4sec + */ + public static long max_retransmission_timeout = 16000; + /** transaction timeout (milliseconds); RFC2361 suggests 64*T1=32000ms */ + public static long transaction_timeout = 128000; + /** clearing timeout (milliseconds); T4 in RFC2361; they suggest T4=5sec */ + public static long clearing_timeout = 5000; + + /** Whether using only one thread for all timer instances. */ + public static boolean single_timer = false; + + /** + * Whether 1xx responses create an "early dialog" for methods that create + * dialog. + */ + public static boolean early_dialog = false; + + /** + * Default 'expires' value in seconds. RFC2361 suggests 3600s as default + * value. + */ + public static int default_expires = 3600; + + /** + * UA info included in request messages in the 'User-Agent' header field. + * Use "NONE" if the 'User-Agent' header filed must not be added. + */ + public static String ua_info = release; + /** + * Server info included in response messages in the 'Server' header field + * Use "NONE" if the 'Server' header filed must not be added. + */ + public static String server_info = release; + + // ************************ debug and logs ************************ + + /** Base level (offset) for logging Transport events */ + public static int LOG_LEVEL_TRANSPORT = 1; + /** Base level (offset) for logging Transaction events */ + public static int LOG_LEVEL_TRANSACTION = 2; + /** Base level (offset) for logging Dialog events */ + public static int LOG_LEVEL_DIALOG = 2; + /** Base level (offset) for logging Call events */ + public static int LOG_LEVEL_CALL = 1; + /** Base level (offset) for logging UA events */ + public static int LOG_LEVEL_UA = 0; + + /** Log level. Only logs with a level less or equal to this are written. */ + public static int debug_level = 1; + /** + * Path for the log folder where log files are written. By default, it is + * used the "./log" folder. Use ".", to store logs in the current root + * folder. + */ + public static String log_path = "log"; + /** The size limit of the log file [kB] */ + public static int max_logsize = 2048; // 2MB + /** + * The number of rotations of log files. Use '0' for NO rotation, '1' for + * rotating a single file + */ + public static int log_rotations = 0; // no rotation + /** + * The rotation period, in MONTHs or DAYs or HOURs or MINUTEs examples: + * log_rotation_time=3 MONTHS, log_rotations=90 DAYS Default value: + * log_rotation_time=2 MONTHS + */ + private static String log_rotation_time = null; + /** The rotation time scale */ + public static int rotation_scale = RotatingLog.MONTH; + /** The rotation time value */ + public static int rotation_time = 2; + + // ************************** costructor ************************** + + /** Parses a single text line (read from the config file) */ + protected void parseLine(String line) { + String attribute; + Parser par; + int index = line.indexOf("="); + if (index > 0) { + attribute = line.substring(0, index).trim(); + par = new Parser(line, index + 1); + } else { + attribute = line; + par = new Parser(""); + } + char[] delim = { ' ', ',' }; + + // general configurations + if (attribute.equals("default_port")) { + default_port = par.getInt(); + return; + } + if (attribute.equals("default_transport_protocols")) { + default_transport_protocols = par.getWordArray(delim); + return; + } + if (attribute.equals("default_nmax_connections")) { + default_nmax_connections = par.getInt(); + return; + } + if (attribute.equals("use_rport")) { + use_rport = (par.getString().toLowerCase().startsWith("y")); + return; + } + if (attribute.equals("force_rport")) { + force_rport = (par.getString().toLowerCase().startsWith("y")); + return; + } + if (attribute.equals("max_forwards")) { + max_forwards = par.getInt(); + return; + } + if (attribute.equals("retransmission_timeout")) { + retransmission_timeout = par.getInt(); + return; + } + if (attribute.equals("max_retransmission_timeout")) { + max_retransmission_timeout = par.getInt(); + return; + } + if (attribute.equals("transaction_timeout")) { + transaction_timeout = par.getInt(); + return; + } + if (attribute.equals("clearing_timeout")) { + clearing_timeout = par.getInt(); + return; + } + if (attribute.equals("single_timer")) { + single_timer = (par.getString().toLowerCase().startsWith("y")); + return; + } + if (attribute.equals("early_dialog")) { + early_dialog = (par.getString().toLowerCase().startsWith("y")); + return; + } + if (attribute.equals("default_expires")) { + default_expires = par.getInt(); + return; + } + if (attribute.equals("ua_info")) { + ua_info = par.getRemainingString().trim(); + return; + } + if (attribute.equals("server_info")) { + server_info = par.getRemainingString().trim(); + return; + } + + // debug and logs + if (attribute.equals("debug_level")) { + debug_level = par.getInt(); + return; + } + if (attribute.equals("log_path")) { + log_path = par.getString(); + return; + } + if (attribute.equals("max_logsize")) { + max_logsize = par.getInt(); + return; + } + if (attribute.equals("log_rotations")) { + log_rotations = par.getInt(); + return; + } + if (attribute.equals("log_rotation_time")) { + log_rotation_time = par.getRemainingString(); + return; + } + + // old parameters + if (attribute.equals("host_addr")) + printLog("WARNING: parameter 'host_addr' is no more supported; use 'via_addr' instead."); + if (attribute.equals("all_interfaces")) + printLog("WARNING: parameter 'all_interfaces' is no more supported; use 'host_iaddr' for setting a specific interface or let it undefined."); + if (attribute.equals("use_outbound")) + printLog("WARNING: parameter 'use_outbound' is no more supported; use 'outbound_addr' for setting an outbound proxy or let it undefined."); + if (attribute.equals("log_file")) + printLog("WARNING: parameter 'log_file' is no more supported."); + } + + /** Converts the entire object into lines (to be saved into the config file) */ + protected String toLines() { // currently not implemented.. + return "SipStack/" + release; + } + + /** Costructs a non-static SipStack */ + private SipStack() { + } + + /** Inits SipStack */ + public static void init() { + init(null); + } + + /** Inits SipStack from the specified file */ + public static void init(String file) { + (new SipStack()).loadFile(file); + + // user-agent info + if (ua_info != null + && (ua_info.length() == 0 + || ua_info.equalsIgnoreCase(Configure.NONE) || ua_info + .equalsIgnoreCase("NO-UA-INFO"))) + ua_info = null; + + // server info + if (server_info != null + && (server_info.length() == 0 + || server_info.equalsIgnoreCase(Configure.NONE) || server_info + .equalsIgnoreCase("NO-SERVER-INFO"))) + server_info = null; + + // timers + Timer.SINGLE_THREAD = single_timer; + + // logs + if (debug_level > 0) { + if (log_rotation_time != null) { + SipParser par = new SipParser(log_rotation_time); + rotation_time = par.getInt(); + String scale = par.getString(); + if (scale == null) + scale = "null"; + if (scale.toUpperCase().startsWith("MONTH")) + rotation_scale = RotatingLog.MONTH; + else if (scale.toUpperCase().startsWith("DAY")) + rotation_scale = RotatingLog.DAY; + else if (scale.toUpperCase().startsWith("HOUR")) + rotation_scale = RotatingLog.HOUR; + else if (scale.toUpperCase().startsWith("MINUTE")) + rotation_scale = RotatingLog.MINUTE; + else { + rotation_time = 7; + rotation_scale = RotatingLog.DAY; + printLog("Error with the log rotation time. Logs will rotate every week."); + } + } + } + + is_init = true; + // if (file!=null) printLog("SipStack loaded",1); + } + + /** Whether SipStack has been already initialized */ + public static boolean isInit() { + return is_init; + } + + // ************************ private methods *********************** + + /** Logs a string message. */ + private static void printLog(String str) { + System.out.println("SipStack: " + str); + } +} diff --git a/src/org/zoolu/sip/provider/TcpTransport.java b/app/src/main/java/org/zoolu/sip/provider/TcpTransport.java similarity index 96% rename from src/org/zoolu/sip/provider/TcpTransport.java rename to app/src/main/java/org/zoolu/sip/provider/TcpTransport.java index 41bc4df..ec3ea9a 100644 --- a/src/org/zoolu/sip/provider/TcpTransport.java +++ b/app/src/main/java/org/zoolu/sip/provider/TcpTransport.java @@ -1,171 +1,171 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.net.*; -import org.zoolu.sip.message.Message; -import java.io.IOException; - -/** - * TcpTransport provides a TCP trasport service for SIP. - */ -class TcpTransport implements ConnectedTransport, TcpConnectionListener { - /** TCP protocol type */ - public static final String PROTO_TCP = "tcp"; - - /** TCP connection */ - TcpConnection tcp_conn; - - /** TCP connection */ - ConnectionIdentifier connection_id; - - /** The last time that has been used (in milliseconds) */ - long last_time; - - /** the current received text. */ - String text; - - /** Transport listener */ - TransportListener listener; - - /** Creates a new TcpTransport */ - public TcpTransport(IpAddress remote_ipaddr, int remote_port, - TransportListener listener, String host) throws IOException { - this.listener = listener; - TcpSocket socket = new TcpSocket(remote_ipaddr, remote_port, host); - tcp_conn = new TcpConnection(socket, this); - connection_id = new ConnectionIdentifier(this); - last_time = System.currentTimeMillis(); - text = ""; - } - - /** Costructs a new TcpTransport */ - public TcpTransport(TcpSocket socket, TransportListener listener) { - this.listener = listener; - tcp_conn = new TcpConnection(socket, this); - connection_id = null; - last_time = System.currentTimeMillis(); - text = ""; - } - - /** Gets protocol type */ - public String getProtocol() { - return PROTO_TCP; - } - - /** Gets the remote IpAddress */ - public IpAddress getRemoteAddress() { - if (tcp_conn != null) - return tcp_conn.getRemoteAddress(); - else - return null; - } - - /** Gets the remote port */ - public int getRemotePort() { - if (tcp_conn != null) - return tcp_conn.getRemotePort(); - else - return 0; - } - - /** Gets the last time the Connection has been used (in millisconds) */ - public long getLastTimeMillis() { - return last_time; - } - - /** - * Sends a Message through the connection. Parameters dest_addr/dest_addr - * are not used, and the message is addressed to the connection remote peer. - *

- * Better use sendMessage(Message msg) method instead. - */ - public void sendMessage(Message msg, IpAddress dest_ipaddr, int dest_port) - throws IOException { - sendMessage(msg); - } - - /** Sends a Message */ - public void sendMessage(Message msg) throws IOException { - if (tcp_conn != null) { - last_time = System.currentTimeMillis(); - byte[] data = msg.toString().getBytes(); - tcp_conn.send(data); - } - } - - /** Stops running */ - public void halt() { - if (tcp_conn != null) - tcp_conn.halt(); - } - - /** Gets a String representation of the Object */ - public String toString() { - if (tcp_conn != null) - return tcp_conn.toString(); - else - return null; - } - - // ************************* Callback methods ************************* - - /** When new data is received through the TcpConnection. */ - public void onReceivedData(TcpConnection tcp_conn, byte[] data, int len) { - last_time = System.currentTimeMillis(); - - text += new String(data, 0, len); - SipParser par = new SipParser(text); - Message msg = par.getSipMessage(); - while (msg != null) { // System.out.println("DEBUG: message len: - // "+msg.getLength()); - msg.setRemoteAddress(tcp_conn.getRemoteAddress().toString()); - msg.setRemotePort(tcp_conn.getRemotePort()); - msg.setTransport(PROTO_TCP); - msg.setConnectionId(connection_id); - if (listener != null) - listener.onReceivedMessage(this, msg); - - text = par.getRemainingString(); - // System.out.println("DEBUG: text left: "+text.length()); - par = new SipParser(text); - msg = par.getSipMessage(); - } - } - - /** When TcpConnection terminates. */ - public void onConnectionTerminated(TcpConnection tcp_conn, Exception error) { - if (listener != null) - listener.onTransportTerminated(this, error); - TcpSocket socket = tcp_conn.getSocket(); - if (socket != null) - try { - socket.close(); - } catch (Exception e) { - } - this.tcp_conn = null; - this.listener = null; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.net.*; +import org.zoolu.sip.message.Message; +import java.io.IOException; + +/** + * TcpTransport provides a TCP trasport service for SIP. + */ +class TcpTransport implements ConnectedTransport, TcpConnectionListener { + /** TCP protocol type */ + public static final String PROTO_TCP = "tcp"; + + /** TCP connection */ + TcpConnection tcp_conn; + + /** TCP connection */ + ConnectionIdentifier connection_id; + + /** The last time that has been used (in milliseconds) */ + long last_time; + + /** the current received text. */ + String text; + + /** Transport listener */ + TransportListener listener; + + /** Creates a new TcpTransport */ + public TcpTransport(IpAddress remote_ipaddr, int remote_port, + TransportListener listener, String host) throws IOException { + this.listener = listener; + TcpSocket socket = new TcpSocket(remote_ipaddr, remote_port, host); + tcp_conn = new TcpConnection(socket, this); + connection_id = new ConnectionIdentifier(this); + last_time = System.currentTimeMillis(); + text = ""; + } + + /** Costructs a new TcpTransport */ + public TcpTransport(TcpSocket socket, TransportListener listener) { + this.listener = listener; + tcp_conn = new TcpConnection(socket, this); + connection_id = null; + last_time = System.currentTimeMillis(); + text = ""; + } + + /** Gets protocol type */ + public String getProtocol() { + return PROTO_TCP; + } + + /** Gets the remote IpAddress */ + public IpAddress getRemoteAddress() { + if (tcp_conn != null) + return tcp_conn.getRemoteAddress(); + else + return null; + } + + /** Gets the remote port */ + public int getRemotePort() { + if (tcp_conn != null) + return tcp_conn.getRemotePort(); + else + return 0; + } + + /** Gets the last time the Connection has been used (in millisconds) */ + public long getLastTimeMillis() { + return last_time; + } + + /** + * Sends a Message through the connection. Parameters dest_addr/dest_addr + * are not used, and the message is addressed to the connection remote peer. + *

+ * Better use sendMessage(Message msg) method instead. + */ + public void sendMessage(Message msg, IpAddress dest_ipaddr, int dest_port) + throws IOException { + sendMessage(msg); + } + + /** Sends a Message */ + public void sendMessage(Message msg) throws IOException { + if (tcp_conn != null) { + last_time = System.currentTimeMillis(); + byte[] data = msg.toString().getBytes(); + tcp_conn.send(data); + } + } + + /** Stops running */ + public void halt() { + if (tcp_conn != null) + tcp_conn.halt(); + } + + /** Gets a String representation of the Object */ + public String toString() { + if (tcp_conn != null) + return tcp_conn.toString(); + else + return null; + } + + // ************************* Callback methods ************************* + + /** When new data is received through the TcpConnection. */ + public void onReceivedData(TcpConnection tcp_conn, byte[] data, int len) { + last_time = System.currentTimeMillis(); + + text += new String(data, 0, len); + SipParser par = new SipParser(text); + Message msg = par.getSipMessage(); + while (msg != null) { // System.out.println("DEBUG: message len: + // "+msg.getLength()); + msg.setRemoteAddress(tcp_conn.getRemoteAddress().toString()); + msg.setRemotePort(tcp_conn.getRemotePort()); + msg.setTransport(PROTO_TCP); + msg.setConnectionId(connection_id); + if (listener != null) + listener.onReceivedMessage(this, msg); + + text = par.getRemainingString(); + // System.out.println("DEBUG: text left: "+text.length()); + par = new SipParser(text); + msg = par.getSipMessage(); + } + } + + /** When TcpConnection terminates. */ + public void onConnectionTerminated(TcpConnection tcp_conn, Exception error) { + if (listener != null) + listener.onTransportTerminated(this, error); + TcpSocket socket = tcp_conn.getSocket(); + if (socket != null) + try { + socket.close(); + } catch (Exception e) { + } + this.tcp_conn = null; + this.listener = null; + } + +} diff --git a/src/org/zoolu/sip/provider/TransactionIdentifier.java b/app/src/main/java/org/zoolu/sip/provider/TransactionIdentifier.java similarity index 96% rename from src/org/zoolu/sip/provider/TransactionIdentifier.java rename to app/src/main/java/org/zoolu/sip/provider/TransactionIdentifier.java index e4ce20f..e51f71f 100644 --- a/src/org/zoolu/sip/provider/TransactionIdentifier.java +++ b/app/src/main/java/org/zoolu/sip/provider/TransactionIdentifier.java @@ -1,55 +1,55 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.SipMethods; - -/** - * TransactionIdentifier is used to address specific transaction to the - * SipProvider. - */ -public class TransactionIdentifier extends Identifier { - /** Costructs a new TransactionIdentifier. */ - public TransactionIdentifier(TransactionIdentifier i) { - super(i); - } - - /** Costructs a new TransactionIdentifier based only on method name. */ - public TransactionIdentifier(String method) { - id = method; - } - - /** - * Costructs a new TransactionIdentifier based on call-id, seqn, method, - * sent-by, and branch. - */ - public TransactionIdentifier(String call_id, long seqn, String method, - String sent_by, String branch) { - if (branch == null) - branch = ""; - if (method.equals(SipMethods.ACK)) - method = SipMethods.INVITE; - id = call_id + "-" + seqn + "-" + method + "-" + sent_by + "-" + branch; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.SipMethods; + +/** + * TransactionIdentifier is used to address specific transaction to the + * SipProvider. + */ +public class TransactionIdentifier extends Identifier { + /** Costructs a new TransactionIdentifier. */ + public TransactionIdentifier(TransactionIdentifier i) { + super(i); + } + + /** Costructs a new TransactionIdentifier based only on method name. */ + public TransactionIdentifier(String method) { + id = method; + } + + /** + * Costructs a new TransactionIdentifier based on call-id, seqn, method, + * sent-by, and branch. + */ + public TransactionIdentifier(String call_id, long seqn, String method, + String sent_by, String branch) { + if (branch == null) + branch = ""; + if (method.equals(SipMethods.ACK)) + method = SipMethods.INVITE; + id = call_id + "-" + seqn + "-" + method + "-" + sent_by + "-" + branch; + } +} diff --git a/src/org/zoolu/sip/provider/Transport.java b/app/src/main/java/org/zoolu/sip/provider/Transport.java similarity index 96% rename from src/org/zoolu/sip/provider/Transport.java rename to app/src/main/java/org/zoolu/sip/provider/Transport.java index 6e011e1..5c42847 100644 --- a/src/org/zoolu/sip/provider/Transport.java +++ b/app/src/main/java/org/zoolu/sip/provider/Transport.java @@ -1,46 +1,46 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; -import org.zoolu.net.IpAddress; -import java.io.IOException; - -/** - * Transport is a generic transport service for SIP. - */ -interface Transport { - /** Gets protocol type */ - public String getProtocol(); - - /** Stops running */ - public void halt(); - - /** Sends a Message to a destination address and port */ - public void sendMessage(Message msg, IpAddress dest_ipaddr, int dest_port) - throws IOException; - - /** Gets a String representation of the Object */ - public String toString(); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; +import org.zoolu.net.IpAddress; +import java.io.IOException; + +/** + * Transport is a generic transport service for SIP. + */ +interface Transport { + /** Gets protocol type */ + public String getProtocol(); + + /** Stops running */ + public void halt(); + + /** Sends a Message to a destination address and port */ + public void sendMessage(Message msg, IpAddress dest_ipaddr, int dest_port) + throws IOException; + + /** Gets a String representation of the Object */ + public String toString(); +} diff --git a/src/org/zoolu/sip/provider/TransportListener.java b/app/src/main/java/org/zoolu/sip/provider/TransportListener.java similarity index 97% rename from src/org/zoolu/sip/provider/TransportListener.java rename to app/src/main/java/org/zoolu/sip/provider/TransportListener.java index db04196..df19d40 100644 --- a/src/org/zoolu/sip/provider/TransportListener.java +++ b/app/src/main/java/org/zoolu/sip/provider/TransportListener.java @@ -1,37 +1,37 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.sip.message.Message; - -/** - * Listener for Transport events. - */ -interface TransportListener { - /** When a new SIP message is received. */ - public void onReceivedMessage(Transport transport, Message msg); - - /** When Transport terminates. */ - public void onTransportTerminated(Transport transport, Exception error); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.sip.message.Message; + +/** + * Listener for Transport events. + */ +interface TransportListener { + /** When a new SIP message is received. */ + public void onReceivedMessage(Transport transport, Message msg); + + /** When Transport terminates. */ + public void onTransportTerminated(Transport transport, Exception error); +} diff --git a/src/org/zoolu/sip/provider/UdpTransport.java b/app/src/main/java/org/zoolu/sip/provider/UdpTransport.java similarity index 96% rename from src/org/zoolu/sip/provider/UdpTransport.java rename to app/src/main/java/org/zoolu/sip/provider/UdpTransport.java index 32191b6..18a02cf 100644 --- a/src/org/zoolu/sip/provider/UdpTransport.java +++ b/app/src/main/java/org/zoolu/sip/provider/UdpTransport.java @@ -1,134 +1,134 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.provider; - -import org.zoolu.net.*; -import org.zoolu.sip.message.Message; -import java.io.IOException; - -/** - * UdpTransport provides an UDP transport service for SIP. - */ -class UdpTransport implements Transport, UdpProviderListener { - /** UDP protocol type */ - public static final String PROTO_UDP = "udp"; - - /** UDP provider */ - UdpProvider udp_provider; - - /** The protocol type */ - String proto; - - /** Transport listener */ - TransportListener listener; - int port; // modified - - /** Creates a new UdpTransport */ - public UdpTransport(int port, TransportListener listener) - throws IOException { - this.listener = listener; - UdpSocket socket = new UdpSocket(port); - udp_provider = new UdpProvider(socket, this); - this.port = socket.getLocalPort(); - } - - /** Creates a new UdpTransport */ - public UdpTransport(int port, IpAddress ipaddr, TransportListener listener) - throws IOException { - this.listener = listener; - UdpSocket socket = new UdpSocket(port, ipaddr); - udp_provider = new UdpProvider(socket, this); - this.port = socket.getLocalPort(); - } - - /** Creates a new UdpTransport */ - public UdpTransport(UdpSocket socket, TransportListener listener) { - this.listener = listener; - udp_provider = new UdpProvider(socket, this); - this.port = socket.getLocalPort(); - } - - /** Gets protocol type */ - public String getProtocol() { - return PROTO_UDP; - } - - public int getPort() { - return port; - } - - /** Sends a Message to a destination address and port */ - public void sendMessage(Message msg, IpAddress dest_ipaddr, int dest_port) - throws IOException { - if (udp_provider != null) { - byte[] data = msg.toString().getBytes(); - UdpPacket packet = new UdpPacket(data, data.length); - packet.setIpAddress(dest_ipaddr); - packet.setPort(dest_port); - udp_provider.send(packet); - } - } - - /** Stops running */ - public void halt() { - if (udp_provider != null) - udp_provider.halt(); - } - - /** Gets a String representation of the Object */ - public String toString() { - if (udp_provider != null) - return udp_provider.toString(); - else - return null; - } - - // ************************* Callback methods ************************* - - /** When a new UDP datagram is received. */ - public void onReceivedPacket(UdpProvider udp, UdpPacket packet) { - Message msg = new Message(packet); - msg.setRemoteAddress(packet.getIpAddress().toString()); - msg.setRemotePort(packet.getPort()); - msg.setTransport(PROTO_UDP); - if (listener != null) - listener.onReceivedMessage(this, msg); - } - - /** When DatagramService stops receiving UDP datagrams. */ - public void onServiceTerminated(UdpProvider udp, Exception error) { - if (listener != null) - listener.onTransportTerminated(this, error); - UdpSocket socket = udp.getUdpSocket(); - if (socket != null) - try { - socket.close(); - } catch (Exception e) { - } - this.udp_provider = null; - this.listener = null; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.provider; + +import org.zoolu.net.*; +import org.zoolu.sip.message.Message; +import java.io.IOException; + +/** + * UdpTransport provides an UDP transport service for SIP. + */ +class UdpTransport implements Transport, UdpProviderListener { + /** UDP protocol type */ + public static final String PROTO_UDP = "udp"; + + /** UDP provider */ + UdpProvider udp_provider; + + /** The protocol type */ + String proto; + + /** Transport listener */ + TransportListener listener; + int port; // modified + + /** Creates a new UdpTransport */ + public UdpTransport(int port, TransportListener listener) + throws IOException { + this.listener = listener; + UdpSocket socket = new UdpSocket(port); + udp_provider = new UdpProvider(socket, this); + this.port = socket.getLocalPort(); + } + + /** Creates a new UdpTransport */ + public UdpTransport(int port, IpAddress ipaddr, TransportListener listener) + throws IOException { + this.listener = listener; + UdpSocket socket = new UdpSocket(port, ipaddr); + udp_provider = new UdpProvider(socket, this); + this.port = socket.getLocalPort(); + } + + /** Creates a new UdpTransport */ + public UdpTransport(UdpSocket socket, TransportListener listener) { + this.listener = listener; + udp_provider = new UdpProvider(socket, this); + this.port = socket.getLocalPort(); + } + + /** Gets protocol type */ + public String getProtocol() { + return PROTO_UDP; + } + + public int getPort() { + return port; + } + + /** Sends a Message to a destination address and port */ + public void sendMessage(Message msg, IpAddress dest_ipaddr, int dest_port) + throws IOException { + if (udp_provider != null) { + byte[] data = msg.toString().getBytes(); + UdpPacket packet = new UdpPacket(data, data.length); + packet.setIpAddress(dest_ipaddr); + packet.setPort(dest_port); + udp_provider.send(packet); + } + } + + /** Stops running */ + public void halt() { + if (udp_provider != null) + udp_provider.halt(); + } + + /** Gets a String representation of the Object */ + public String toString() { + if (udp_provider != null) + return udp_provider.toString(); + else + return null; + } + + // ************************* Callback methods ************************* + + /** When a new UDP datagram is received. */ + public void onReceivedPacket(UdpProvider udp, UdpPacket packet) { + Message msg = new Message(packet); + msg.setRemoteAddress(packet.getIpAddress().toString()); + msg.setRemotePort(packet.getPort()); + msg.setTransport(PROTO_UDP); + if (listener != null) + listener.onReceivedMessage(this, msg); + } + + /** When DatagramService stops receiving UDP datagrams. */ + public void onServiceTerminated(UdpProvider udp, Exception error) { + if (listener != null) + listener.onTransportTerminated(this, error); + UdpSocket socket = udp.getUdpSocket(); + if (socket != null) + try { + socket.close(); + } catch (Exception e) { + } + this.udp_provider = null; + this.listener = null; + } + +} diff --git a/src/org/zoolu/sip/transaction/AckTransactionClient.java b/app/src/main/java/org/zoolu/sip/transaction/AckTransactionClient.java similarity index 97% rename from src/org/zoolu/sip/transaction/AckTransactionClient.java rename to app/src/main/java/org/zoolu/sip/transaction/AckTransactionClient.java index d4bce66..ca4c3f1 100644 --- a/src/org/zoolu/sip/transaction/AckTransactionClient.java +++ b/app/src/main/java/org/zoolu/sip/transaction/AckTransactionClient.java @@ -1,81 +1,81 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.LogLevel; - -/** - * ACK client transaction should follow an INVITE client transaction within an - * INVITE Dialog in a SIP UAC. The AckTransactionClient simply sends an ACK - * request message and terminates. - */ -public class AckTransactionClient extends Transaction { - - /** - * the TransactionClientListener that captures the events fired by the - * AckTransactionClient - */ - TransactionClientListener transaction_listener; - - /** Creates a new AckTransactionClient. */ - public AckTransactionClient(SipProvider sip_provider, Message ack, - TransactionClientListener listener) { - super(sip_provider); - request = new Message(ack); - transaction_listener = listener; - transaction_id = request.getTransactionId(); - printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); - printLog("created", LogLevel.HIGH); - } - - /** Starts the AckTransactionClient and sends the ACK request. */ - public void request() { - printLog("start", LogLevel.LOW); - sip_provider.sendMessage(request); - changeStatus(STATE_TERMINATED); - // if (transaction_listener!=null) - // transaction_listener.onAckCltTerminated(this); - // (CHANGE-040421) free the link to transaction_listener - transaction_listener = null; - } - - /** Method used to drop an active transaction. */ - public void terminate() { - changeStatus(STATE_TERMINATED); - // (CHANGE-040421) free the link to transaction_listener - transaction_listener = null; - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("AckTransactionClient#" + transaction_sqn + ": " + str, - level + SipStack.LOG_LEVEL_TRANSACTION); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.LogLevel; + +/** + * ACK client transaction should follow an INVITE client transaction within an + * INVITE Dialog in a SIP UAC. The AckTransactionClient simply sends an ACK + * request message and terminates. + */ +public class AckTransactionClient extends Transaction { + + /** + * the TransactionClientListener that captures the events fired by the + * AckTransactionClient + */ + TransactionClientListener transaction_listener; + + /** Creates a new AckTransactionClient. */ + public AckTransactionClient(SipProvider sip_provider, Message ack, + TransactionClientListener listener) { + super(sip_provider); + request = new Message(ack); + transaction_listener = listener; + transaction_id = request.getTransactionId(); + printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); + printLog("created", LogLevel.HIGH); + } + + /** Starts the AckTransactionClient and sends the ACK request. */ + public void request() { + printLog("start", LogLevel.LOW); + sip_provider.sendMessage(request); + changeStatus(STATE_TERMINATED); + // if (transaction_listener!=null) + // transaction_listener.onAckCltTerminated(this); + // (CHANGE-040421) free the link to transaction_listener + transaction_listener = null; + } + + /** Method used to drop an active transaction. */ + public void terminate() { + changeStatus(STATE_TERMINATED); + // (CHANGE-040421) free the link to transaction_listener + transaction_listener = null; + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("AckTransactionClient#" + transaction_sqn + ": " + str, + level + SipStack.LOG_LEVEL_TRANSACTION); + } + +} diff --git a/src/org/zoolu/sip/transaction/AckTransactionServer.java b/app/src/main/java/org/zoolu/sip/transaction/AckTransactionServer.java similarity index 97% rename from src/org/zoolu/sip/transaction/AckTransactionServer.java rename to app/src/main/java/org/zoolu/sip/transaction/AckTransactionServer.java index e3b3438..e211b19 100644 --- a/src/org/zoolu/sip/transaction/AckTransactionServer.java +++ b/app/src/main/java/org/zoolu/sip/transaction/AckTransactionServer.java @@ -1,166 +1,166 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.Timer; -import org.zoolu.tools.LogLevel; - -/** - * ACK server transaction should follow an INVITE server transaction within an - * INVITE Dialog in a SIP UAC. The AckTransactionServer sends the final response - * message and retransmits it several times until the method terminate() is - * called or the trasaction timeout fires. - */ -public class AckTransactionServer extends Transaction { - /** - * the TransactionServerListener that captures the events fired by the - * AckTransactionServer - */ - AckTransactionServerListener transaction_listener; - - /** last response message */ - Message response; - - /** retransmission timeout */ - Timer retransmission_to; - /** transaction timeout */ - Timer transaction_to; - - /** Initializes timeouts and state */ - /** - * Creates a new AckTransactionServer. The AckTransactionServer starts - * sending a the resp message. It retransmits the resp several times - * if no ACK request is received. - */ - public AckTransactionServer(SipProvider sip_provider, Message resp, - AckTransactionServerListener listener) { - super(sip_provider); - response = resp; - init(listener, new TransactionIdentifier(SipMethods.ACK), null); - } - - /** - * Creates a new AckTransactionServer. The AckTransactionServer starts - * sending the response message resp through the connection - * conn_id. - */ - public AckTransactionServer(SipProvider sip_provider, - ConnectionIdentifier connection_id, Message resp, - AckTransactionServerListener listener) { - super(sip_provider); - response = resp; - init(listener, new TransactionIdentifier(SipMethods.ACK), connection_id); - } - - /** Initializes timeouts and listener. */ - void init(AckTransactionServerListener listener, - TransactionIdentifier transaction_id, - ConnectionIdentifier connection_id) { - this.transaction_listener = listener; - this.transaction_id = transaction_id; - this.connection_id = connection_id; - transaction_to = new Timer(SipStack.transaction_timeout, "Transaction", - this); - retransmission_to = new Timer(SipStack.retransmission_timeout, - "Retransmission", this); - // (CHANGE-040905) now timeouts started in listen() - // transaction_to.start(); - // if (connection_id==null) retransmission_to.start(); - printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); - printLog("created", LogLevel.HIGH); - } - - /** Starts the AckTransactionServer. */ - public void respond() { - printLog("start", LogLevel.LOW); - changeStatus(STATE_PROCEEDING); - // transaction_id=null; // it is not required since no - // SipProviderListener is implemented - // (CHANGE-040905) now timeouts started in listen() - transaction_to.start(); - if (true || connection_id == null) // modified - retransmission_to.start(); - - sip_provider.sendMessage(response, connection_id); - } - - /** - * Method derived from interface TimerListener. It's fired from an active - * Timer. - */ - public void onTimeout(Timer to) { - try { - if (to.equals(retransmission_to) && statusIs(STATE_PROCEEDING)) { - printLog("Retransmission timeout expired", LogLevel.HIGH); - long timeout = 2 * retransmission_to.getTime(); - if (timeout > SipStack.max_retransmission_timeout) - timeout = SipStack.max_retransmission_timeout; - retransmission_to = new Timer(timeout, retransmission_to - .getLabel(), this); - retransmission_to.start(); - sip_provider.sendMessage(response, connection_id); - } - if (to.equals(transaction_to) && statusIs(STATE_PROCEEDING)) { - printLog("Transaction timeout expired", LogLevel.HIGH); - changeStatus(STATE_TERMINATED); - if (transaction_listener != null) - transaction_listener.onTransAckTimeout(this); - // (CHANGE-040421) now it can free links to transaction_listener - // and timers - transaction_listener = null; - // retransmission_to=null; - // transaction_to=null; - } - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - - /** Method used to drop an active transaction. */ - public void terminate() { - retransmission_to.halt(); - transaction_to.halt(); - changeStatus(STATE_TERMINATED); - // if (transaction_listener!=null) - // transaction_listener.onAckSrvTerminated(this); - // (CHANGE-040421) now it can free links to transaction_listener and - // timers - transaction_listener = null; - // retransmission_to=null; - // transaction_to=null; - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("AckTransactionServer#" + transaction_sqn + ": " + str, - level + SipStack.LOG_LEVEL_TRANSACTION); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.Timer; +import org.zoolu.tools.LogLevel; + +/** + * ACK server transaction should follow an INVITE server transaction within an + * INVITE Dialog in a SIP UAC. The AckTransactionServer sends the final response + * message and retransmits it several times until the method terminate() is + * called or the trasaction timeout fires. + */ +public class AckTransactionServer extends Transaction { + /** + * the TransactionServerListener that captures the events fired by the + * AckTransactionServer + */ + AckTransactionServerListener transaction_listener; + + /** last response message */ + Message response; + + /** retransmission timeout */ + Timer retransmission_to; + /** transaction timeout */ + Timer transaction_to; + + /** Initializes timeouts and state */ + /** + * Creates a new AckTransactionServer. The AckTransactionServer starts + * sending a the resp message. It retransmits the resp several times + * if no ACK request is received. + */ + public AckTransactionServer(SipProvider sip_provider, Message resp, + AckTransactionServerListener listener) { + super(sip_provider); + response = resp; + init(listener, new TransactionIdentifier(SipMethods.ACK), null); + } + + /** + * Creates a new AckTransactionServer. The AckTransactionServer starts + * sending the response message resp through the connection + * conn_id. + */ + public AckTransactionServer(SipProvider sip_provider, + ConnectionIdentifier connection_id, Message resp, + AckTransactionServerListener listener) { + super(sip_provider); + response = resp; + init(listener, new TransactionIdentifier(SipMethods.ACK), connection_id); + } + + /** Initializes timeouts and listener. */ + void init(AckTransactionServerListener listener, + TransactionIdentifier transaction_id, + ConnectionIdentifier connection_id) { + this.transaction_listener = listener; + this.transaction_id = transaction_id; + this.connection_id = connection_id; + transaction_to = new Timer(SipStack.transaction_timeout, "Transaction", + this); + retransmission_to = new Timer(SipStack.retransmission_timeout, + "Retransmission", this); + // (CHANGE-040905) now timeouts started in listen() + // transaction_to.start(); + // if (connection_id==null) retransmission_to.start(); + printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); + printLog("created", LogLevel.HIGH); + } + + /** Starts the AckTransactionServer. */ + public void respond() { + printLog("start", LogLevel.LOW); + changeStatus(STATE_PROCEEDING); + // transaction_id=null; // it is not required since no + // SipProviderListener is implemented + // (CHANGE-040905) now timeouts started in listen() + transaction_to.start(); + if (true || connection_id == null) // modified + retransmission_to.start(); + + sip_provider.sendMessage(response, connection_id); + } + + /** + * Method derived from interface TimerListener. It's fired from an active + * Timer. + */ + public void onTimeout(Timer to) { + try { + if (to.equals(retransmission_to) && statusIs(STATE_PROCEEDING)) { + printLog("Retransmission timeout expired", LogLevel.HIGH); + long timeout = 2 * retransmission_to.getTime(); + if (timeout > SipStack.max_retransmission_timeout) + timeout = SipStack.max_retransmission_timeout; + retransmission_to = new Timer(timeout, retransmission_to + .getLabel(), this); + retransmission_to.start(); + sip_provider.sendMessage(response, connection_id); + } + if (to.equals(transaction_to) && statusIs(STATE_PROCEEDING)) { + printLog("Transaction timeout expired", LogLevel.HIGH); + changeStatus(STATE_TERMINATED); + if (transaction_listener != null) + transaction_listener.onTransAckTimeout(this); + // (CHANGE-040421) now it can free links to transaction_listener + // and timers + transaction_listener = null; + // retransmission_to=null; + // transaction_to=null; + } + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + + /** Method used to drop an active transaction. */ + public void terminate() { + retransmission_to.halt(); + transaction_to.halt(); + changeStatus(STATE_TERMINATED); + // if (transaction_listener!=null) + // transaction_listener.onAckSrvTerminated(this); + // (CHANGE-040421) now it can free links to transaction_listener and + // timers + transaction_listener = null; + // retransmission_to=null; + // transaction_to=null; + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("AckTransactionServer#" + transaction_sqn + ": " + str, + level + SipStack.LOG_LEVEL_TRANSACTION); + } + +} diff --git a/src/org/zoolu/sip/transaction/AckTransactionServerListener.java b/app/src/main/java/org/zoolu/sip/transaction/AckTransactionServerListener.java similarity index 97% rename from src/org/zoolu/sip/transaction/AckTransactionServerListener.java rename to app/src/main/java/org/zoolu/sip/transaction/AckTransactionServerListener.java index e70ad90..19a4e59 100644 --- a/src/org/zoolu/sip/transaction/AckTransactionServerListener.java +++ b/app/src/main/java/org/zoolu/sip/transaction/AckTransactionServerListener.java @@ -1,36 +1,36 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -/** - * A AckTransactionServerListener listens for AckTransactionServer - * onTransAckTimeout(AckTransactionServer) events. - */ -public interface AckTransactionServerListener { - /** - * When the AckTransactionServer goes into the "Terminated" state, caused by - * transaction timeout - */ - public void onTransAckTimeout(AckTransactionServer transaction); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +/** + * A AckTransactionServerListener listens for AckTransactionServer + * onTransAckTimeout(AckTransactionServer) events. + */ +public interface AckTransactionServerListener { + /** + * When the AckTransactionServer goes into the "Terminated" state, caused by + * transaction timeout + */ + public void onTransAckTimeout(AckTransactionServer transaction); +} diff --git a/src/org/zoolu/sip/transaction/InviteTransactionClient.java b/app/src/main/java/org/zoolu/sip/transaction/InviteTransactionClient.java similarity index 97% rename from src/org/zoolu/sip/transaction/InviteTransactionClient.java rename to app/src/main/java/org/zoolu/sip/transaction/InviteTransactionClient.java index 3ea957a..af7ad3b 100644 --- a/src/org/zoolu/sip/transaction/InviteTransactionClient.java +++ b/app/src/main/java/org/zoolu/sip/transaction/InviteTransactionClient.java @@ -1,206 +1,206 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.Timer; -import org.zoolu.tools.LogLevel; - -/** - * INVITE client transaction as defined in RFC 3261 (Section 17.2.1).
- * An InviteTransactionClient is responsable to create a new SIP invite - * transaction, starting with a invite message sent through the SipProvider and - * ending with a final response.
- * The changes of the internal status and the received messages are fired to the - * TransactionListener passed to the InviteTransactionClient object. - */ -public class InviteTransactionClient extends TransactionClient { - /** - * the TransactionClientListener that captures the events fired by the - * InviteTransactionClient - */ - TransactionClientListener transaction_listener; - - /** ack message */ - Message ack; - - /** retransmission timeout ("Timer A" in RFC 3261) */ - // Timer retransmission_to; - /** transaction timeout ("Timer B" in RFC 3261) */ - // Timer transaction_to; - /** end timeout for invite transactions ("Timer D" in RFC 3261) */ - Timer end_to; - - /** Creates a new ClientTransaction */ - public InviteTransactionClient(SipProvider sip_provider, Message req, - TransactionClientListener listener) { - super(sip_provider); - request = new Message(req); - init(listener, request.getTransactionId()); - } - - /** Initializes timeouts and listener. */ - void init(TransactionClientListener listener, - TransactionIdentifier transaction_id) { - this.transaction_listener = listener; - this.transaction_id = transaction_id; - this.ack = null; - retransmission_to = new Timer(SipStack.retransmission_timeout, - "Retransmission", this); - transaction_to = new Timer(SipStack.transaction_timeout, "Transaction", - this); - end_to = new Timer(SipStack.transaction_timeout, "End", this); - printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); - printLog("created", LogLevel.HIGH); - } - - /** Starts the InviteTransactionClient and sends the invite request. */ - public void request() { - printLog("start", LogLevel.LOW); - changeStatus(STATE_TRYING); - retransmission_to.start(); - transaction_to.start(); - - sip_provider.addSipProviderListener(transaction_id, this); - connection_id = sip_provider.sendMessage(request); - } - - /** - * Method derived from interface SipListener. It's fired from the - * SipProvider when a new message is catch for to the present - * ServerTransaction. - */ - public void onReceivedMessage(SipProvider provider, Message msg) { - if (msg.isResponse()) { - int code = msg.getStatusLine().getCode(); - if (code >= 100 && code < 200 - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { - if (statusIs(STATE_TRYING)) { - retransmission_to.halt(); - transaction_to.halt(); - changeStatus(STATE_PROCEEDING); - } - if (transaction_listener != null) - transaction_listener.onTransProvisionalResponse(this, msg); - return; - } - if (code >= 300 - && code < 700 - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING) || statusIs(STATE_COMPLETED))) { - if (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING)) { - retransmission_to.halt(); - transaction_to.halt(); - ack = MessageFactory.createNon2xxAckRequest(sip_provider, - request, msg); - changeStatus(STATE_COMPLETED); - connection_id = sip_provider.sendMessage(ack); - if (transaction_listener != null) - transaction_listener.onTransFailureResponse(this, msg); - transaction_listener = null; - if (true || connection_id == null) // modified - end_to.start(); - else { - printLog("end_to=0 for reliable transport", - LogLevel.LOW); - onTimeout(end_to); - } - } else { // retransmit ACK only in case of unreliable - // transport - if (true || connection_id == null) // modified - sip_provider.sendMessage(ack); - } - return; - } - if (code >= 200 && code < 300 - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { - retransmission_to.halt(); - transaction_to.halt(); - end_to.halt(); - changeStatus(STATE_TERMINATED); - sip_provider.removeSipProviderListener(transaction_id); - if (transaction_listener != null) - transaction_listener.onTransSuccessResponse(this, msg); - transaction_listener = null; - return; - } - } - } - - /** - * Method derived from interface TimerListener. It's fired from an active - * Timer. - */ - public void onTimeout(Timer to) { - try { - if (to.equals(retransmission_to) && statusIs(STATE_TRYING)) { - printLog("Retransmission timeout expired", LogLevel.HIGH); - // retransmission only in case of unreliable transport - if (true || connection_id == null) { // modified - sip_provider.sendMessage(request); - long timeout = 2 * retransmission_to.getTime(); - retransmission_to = new Timer(timeout, retransmission_to - .getLabel(), this); - retransmission_to.start(); - } else - printLog("No retransmissions for reliable transport (" - + connection_id + ")", LogLevel.LOW); - } - if (to.equals(transaction_to)) { - printLog("Transaction timeout expired", LogLevel.HIGH); - retransmission_to.halt(); - end_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - if (transaction_listener != null) - transaction_listener.onTransTimeout(this); - transaction_listener = null; - } - if (to.equals(end_to)) { - printLog("End timeout expired", LogLevel.HIGH); - retransmission_to.halt(); - transaction_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; // already null.. - } - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - - /** Terminates the transaction. */ - public void terminate() { - if (!statusIs(STATE_TERMINATED)) { - retransmission_to.halt(); - transaction_to.halt(); - end_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - } - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.Timer; +import org.zoolu.tools.LogLevel; + +/** + * INVITE client transaction as defined in RFC 3261 (Section 17.2.1).
+ * An InviteTransactionClient is responsable to create a new SIP invite + * transaction, starting with a invite message sent through the SipProvider and + * ending with a final response.
+ * The changes of the internal status and the received messages are fired to the + * TransactionListener passed to the InviteTransactionClient object. + */ +public class InviteTransactionClient extends TransactionClient { + /** + * the TransactionClientListener that captures the events fired by the + * InviteTransactionClient + */ + TransactionClientListener transaction_listener; + + /** ack message */ + Message ack; + + /** retransmission timeout ("Timer A" in RFC 3261) */ + // Timer retransmission_to; + /** transaction timeout ("Timer B" in RFC 3261) */ + // Timer transaction_to; + /** end timeout for invite transactions ("Timer D" in RFC 3261) */ + Timer end_to; + + /** Creates a new ClientTransaction */ + public InviteTransactionClient(SipProvider sip_provider, Message req, + TransactionClientListener listener) { + super(sip_provider); + request = new Message(req); + init(listener, request.getTransactionId()); + } + + /** Initializes timeouts and listener. */ + void init(TransactionClientListener listener, + TransactionIdentifier transaction_id) { + this.transaction_listener = listener; + this.transaction_id = transaction_id; + this.ack = null; + retransmission_to = new Timer(SipStack.retransmission_timeout, + "Retransmission", this); + transaction_to = new Timer(SipStack.transaction_timeout, "Transaction", + this); + end_to = new Timer(SipStack.transaction_timeout, "End", this); + printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); + printLog("created", LogLevel.HIGH); + } + + /** Starts the InviteTransactionClient and sends the invite request. */ + public void request() { + printLog("start", LogLevel.LOW); + changeStatus(STATE_TRYING); + retransmission_to.start(); + transaction_to.start(); + + sip_provider.addSipProviderListener(transaction_id, this); + connection_id = sip_provider.sendMessage(request); + } + + /** + * Method derived from interface SipListener. It's fired from the + * SipProvider when a new message is catch for to the present + * ServerTransaction. + */ + public void onReceivedMessage(SipProvider provider, Message msg) { + if (msg.isResponse()) { + int code = msg.getStatusLine().getCode(); + if (code >= 100 && code < 200 + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { + if (statusIs(STATE_TRYING)) { + retransmission_to.halt(); + transaction_to.halt(); + changeStatus(STATE_PROCEEDING); + } + if (transaction_listener != null) + transaction_listener.onTransProvisionalResponse(this, msg); + return; + } + if (code >= 300 + && code < 700 + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING) || statusIs(STATE_COMPLETED))) { + if (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING)) { + retransmission_to.halt(); + transaction_to.halt(); + ack = MessageFactory.createNon2xxAckRequest(sip_provider, + request, msg); + changeStatus(STATE_COMPLETED); + connection_id = sip_provider.sendMessage(ack); + if (transaction_listener != null) + transaction_listener.onTransFailureResponse(this, msg); + transaction_listener = null; + if (true || connection_id == null) // modified + end_to.start(); + else { + printLog("end_to=0 for reliable transport", + LogLevel.LOW); + onTimeout(end_to); + } + } else { // retransmit ACK only in case of unreliable + // transport + if (true || connection_id == null) // modified + sip_provider.sendMessage(ack); + } + return; + } + if (code >= 200 && code < 300 + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { + retransmission_to.halt(); + transaction_to.halt(); + end_to.halt(); + changeStatus(STATE_TERMINATED); + sip_provider.removeSipProviderListener(transaction_id); + if (transaction_listener != null) + transaction_listener.onTransSuccessResponse(this, msg); + transaction_listener = null; + return; + } + } + } + + /** + * Method derived from interface TimerListener. It's fired from an active + * Timer. + */ + public void onTimeout(Timer to) { + try { + if (to.equals(retransmission_to) && statusIs(STATE_TRYING)) { + printLog("Retransmission timeout expired", LogLevel.HIGH); + // retransmission only in case of unreliable transport + if (true || connection_id == null) { // modified + sip_provider.sendMessage(request); + long timeout = 2 * retransmission_to.getTime(); + retransmission_to = new Timer(timeout, retransmission_to + .getLabel(), this); + retransmission_to.start(); + } else + printLog("No retransmissions for reliable transport (" + + connection_id + ")", LogLevel.LOW); + } + if (to.equals(transaction_to)) { + printLog("Transaction timeout expired", LogLevel.HIGH); + retransmission_to.halt(); + end_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + if (transaction_listener != null) + transaction_listener.onTransTimeout(this); + transaction_listener = null; + } + if (to.equals(end_to)) { + printLog("End timeout expired", LogLevel.HIGH); + retransmission_to.halt(); + transaction_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; // already null.. + } + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + + /** Terminates the transaction. */ + public void terminate() { + if (!statusIs(STATE_TERMINATED)) { + retransmission_to.halt(); + transaction_to.halt(); + end_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + } + } + +} diff --git a/src/org/zoolu/sip/transaction/InviteTransactionServer.java b/app/src/main/java/org/zoolu/sip/transaction/InviteTransactionServer.java similarity index 97% rename from src/org/zoolu/sip/transaction/InviteTransactionServer.java rename to app/src/main/java/org/zoolu/sip/transaction/InviteTransactionServer.java index 81599a1..9b90541 100644 --- a/src/org/zoolu/sip/transaction/InviteTransactionServer.java +++ b/app/src/main/java/org/zoolu/sip/transaction/InviteTransactionServer.java @@ -1,293 +1,293 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.Timer; -import org.zoolu.tools.LogLevel; -import org.zoolu.sip.header.ContactHeader; - -/** - * INVITE server transaction as defined in RFC 3261 (Section 17.2.1).
- * An InviteTransactionServer is responsable to create a new SIP invite - * transaction that starts with a INVITE message received by the SipProvider and - * ends sending a final response.
- * The changes of the internal status and the received messages are fired to the - * TransactionListener passed to the InviteTransactionServer object.
- * This implementation of InviteTransactionServer automatically generates a "100 - * Trying" response when the INVITE message is received (as suggested by - * RFC3261) - */ -public class InviteTransactionServer extends TransactionServer { - /** Default behavior for automatically sending 100 Trying on INVITE. */ - public static boolean AUTO_TRYING = true; - - /** - * the TransactionServerListener that captures the events fired by the - * InviteTransactionServer - */ - InviteTransactionServerListener transaction_listener; - - /** last response message */ - // Message response=null; - /** retransmission timeout ("Timer G" in RFC 3261) */ - Timer retransmission_to; - /** end timeout ("Timer H" in RFC 3261) */ - Timer end_to; - /** clearing timeout ("Timer I" in RFC 3261) */ - // Timer clearing_to; - /** Whether automatically sending 100 Trying on INVITE. */ - boolean auto_trying; - - /** Creates a new InviteTransactionServer. */ - public InviteTransactionServer(SipProvider sip_provider, - InviteTransactionServerListener listener) { - super(sip_provider); - init(listener, new TransactionIdentifier(SipMethods.INVITE), null); - } - - /** - * Creates a new InviteTransactionServer for the already received INVITE - * request invite. - */ - public InviteTransactionServer(SipProvider sip_provider, Message invite, - InviteTransactionServerListener listener) { - super(sip_provider); - request = new Message(invite); - init(listener, request.getTransactionId(), request.getConnectionId()); - - changeStatus(STATE_TRYING); - sip_provider.addSipProviderListener(transaction_id, this); - // automatically send "100 Tryng" response and go to STATE_PROCEEDING - if (auto_trying) { - Message trying100 = MessageFactory.createResponse(request, 100, - SipResponses.reasonOf(100), null); - respondWith(trying100); // this method makes it going automatically - // to STATE_PROCEEDING - } - } - - /** - * Creates a new InviteTransactionServer for the already received INVITE - * request invite. - */ - public InviteTransactionServer(SipProvider sip_provider, Message invite, - boolean auto_trying, InviteTransactionServerListener listener) { - super(sip_provider); - request = new Message(invite); - init(listener, request.getTransactionId(), request.getConnectionId()); - this.auto_trying = auto_trying; - - changeStatus(STATE_TRYING); - sip_provider.addSipProviderListener(transaction_id, this); - // automatically send "100 Tryng" response and go to STATE_PROCEEDING - if (auto_trying) { - Message trying100 = MessageFactory.createResponse(request, 100, - SipResponses.reasonOf(100), null); - respondWith(trying100); // this method makes it going automatically - // to STATE_PROCEEDING - } - } - - /** Initializes timeouts and listener. */ - void init(InviteTransactionServerListener listener, - TransactionIdentifier transaction_id, - ConnectionIdentifier connection_id) { - this.transaction_listener = listener; - this.transaction_id = transaction_id; - this.connection_id = connection_id; - auto_trying = AUTO_TRYING; - retransmission_to = new Timer(SipStack.retransmission_timeout, - "Retransmission", this); - end_to = new Timer(SipStack.transaction_timeout, "End", this); - clearing_to = new Timer(SipStack.clearing_timeout, "Clearing", this); - printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); - printLog("created", LogLevel.HIGH); - } - - /** Whether automatically sending 100 Trying on INVITE. */ - public void setAutoTrying(boolean auto_trying) { - this.auto_trying = auto_trying; - } - - /** Starts the InviteTransactionServer. */ - public void listen() { - printLog("start", LogLevel.LOW); - if (statusIs(STATE_IDLE)) { - changeStatus(STATE_WAITING); - sip_provider.addSipProviderListener(new TransactionIdentifier( - SipMethods.INVITE), this); - sip_provider.addSipProviderListener(new TransactionIdentifier( - SipMethods.OPTIONS), this); - - } - } - - /** Sends a response message */ - public void respondWith(Message resp) { - response = resp; - int code = response.getStatusLine().getCode(); - if (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING)) - sip_provider.sendMessage(response, connection_id); - if (code >= 100 && code < 200 && statusIs(STATE_TRYING)) { - changeStatus(STATE_PROCEEDING); - return; - } - if (code >= 200 && code < 300 - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - return; - } - if (code >= 300 && code < 700 - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { - changeStatus(STATE_COMPLETED); - // retransmission only in case of unreliable transport - if (true || connection_id == null) { // modified - retransmission_to.start(); - end_to.start(); - } else { - printLog("No retransmissions for reliable transport (" - + connection_id + ")", LogLevel.LOW); - onTimeout(end_to); - } - } - } - - /** - * Method derived from interface SipListener. It's fired from the - * SipProvider when a new message is catch for to the present - * ServerTransaction. - */ - public void onReceivedMessage(SipProvider provider, Message msg) { - if (msg.isRequest()) { - String req_method = msg.getRequestLine().getMethod(); - - // invite received - if (req_method.equals(SipMethods.INVITE)) { - if (statusIs(STATE_WAITING)) { - request = new Message(msg); - connection_id = request.getConnectionId(); - transaction_id = request.getTransactionId(); - sip_provider.addSipProviderListener(transaction_id, this); - sip_provider - .removeSipProviderListener(new TransactionIdentifier( - SipMethods.INVITE)); - changeStatus(STATE_TRYING); - // automatically send "100 Tryng" response and go to - // STATE_PROCEEDING - if (auto_trying) { - Message trying100 = MessageFactory.createResponse( - request, 100, SipResponses.reasonOf(100), null); - respondWith(trying100); // this method makes it going - // automatically to - // STATE_PROCEEDING - } - if (transaction_listener != null) - transaction_listener.onTransRequest(this, msg); - return; - } - if (statusIs(STATE_PROCEEDING) || statusIs(STATE_COMPLETED)) { // retransmission - // of - // the - // last - // response - sip_provider.sendMessage(response, connection_id); - return; - } - } - - if (req_method.equals(SipMethods.OPTIONS)) { - Message ok200 = MessageFactory.createResponse(msg, 200, SipResponses.reasonOf(200), null); - ok200.removeServerHeader(); - ok200.addContactHeader(new ContactHeader(ok200.getToHeader().getNameAddress()), false); - sip_provider.sendMessage(ok200, connection_id); - return; - } - - // ack received - if (req_method.equals(SipMethods.ACK) && statusIs(STATE_COMPLETED)) { - retransmission_to.halt(); - end_to.halt(); - changeStatus(STATE_CONFIRMED); - if (transaction_listener != null) - transaction_listener.onTransFailureAck(this, msg); - clearing_to.start(); - return; - } - } - } - - /** - * Method derived from interface TimerListener. It's fired from an active - * Timer. - */ - public void onTimeout(Timer to) { - try { - if (to.equals(retransmission_to) && statusIs(STATE_COMPLETED)) { - printLog("Retransmission timeout expired", LogLevel.HIGH); - long timeout = 2 * retransmission_to.getTime(); - if (timeout > SipStack.max_retransmission_timeout) - timeout = SipStack.max_retransmission_timeout; - retransmission_to = new Timer(timeout, retransmission_to - .getLabel(), this); - retransmission_to.start(); - sip_provider.sendMessage(response, connection_id); - } - if (to.equals(end_to) && statusIs(STATE_COMPLETED)) { - printLog("End timeout expired", LogLevel.HIGH); - retransmission_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - } - if (to.equals(clearing_to) && statusIs(STATE_CONFIRMED)) { - printLog("Clearing timeout expired", LogLevel.HIGH); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - } - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - - /** Method used to drop an active transaction */ - public void terminate() { - retransmission_to.halt(); - clearing_to.halt(); - end_to.halt(); - if (statusIs(STATE_TRYING)) - sip_provider.removeSipProviderListener(new TransactionIdentifier( - SipMethods.INVITE)); - else - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.Timer; +import org.zoolu.tools.LogLevel; +import org.zoolu.sip.header.ContactHeader; + +/** + * INVITE server transaction as defined in RFC 3261 (Section 17.2.1).
+ * An InviteTransactionServer is responsable to create a new SIP invite + * transaction that starts with a INVITE message received by the SipProvider and + * ends sending a final response.
+ * The changes of the internal status and the received messages are fired to the + * TransactionListener passed to the InviteTransactionServer object.
+ * This implementation of InviteTransactionServer automatically generates a "100 + * Trying" response when the INVITE message is received (as suggested by + * RFC3261) + */ +public class InviteTransactionServer extends TransactionServer { + /** Default behavior for automatically sending 100 Trying on INVITE. */ + public static boolean AUTO_TRYING = true; + + /** + * the TransactionServerListener that captures the events fired by the + * InviteTransactionServer + */ + InviteTransactionServerListener transaction_listener; + + /** last response message */ + // Message response=null; + /** retransmission timeout ("Timer G" in RFC 3261) */ + Timer retransmission_to; + /** end timeout ("Timer H" in RFC 3261) */ + Timer end_to; + /** clearing timeout ("Timer I" in RFC 3261) */ + // Timer clearing_to; + /** Whether automatically sending 100 Trying on INVITE. */ + boolean auto_trying; + + /** Creates a new InviteTransactionServer. */ + public InviteTransactionServer(SipProvider sip_provider, + InviteTransactionServerListener listener) { + super(sip_provider); + init(listener, new TransactionIdentifier(SipMethods.INVITE), null); + } + + /** + * Creates a new InviteTransactionServer for the already received INVITE + * request invite. + */ + public InviteTransactionServer(SipProvider sip_provider, Message invite, + InviteTransactionServerListener listener) { + super(sip_provider); + request = new Message(invite); + init(listener, request.getTransactionId(), request.getConnectionId()); + + changeStatus(STATE_TRYING); + sip_provider.addSipProviderListener(transaction_id, this); + // automatically send "100 Tryng" response and go to STATE_PROCEEDING + if (auto_trying) { + Message trying100 = MessageFactory.createResponse(request, 100, + SipResponses.reasonOf(100), null); + respondWith(trying100); // this method makes it going automatically + // to STATE_PROCEEDING + } + } + + /** + * Creates a new InviteTransactionServer for the already received INVITE + * request invite. + */ + public InviteTransactionServer(SipProvider sip_provider, Message invite, + boolean auto_trying, InviteTransactionServerListener listener) { + super(sip_provider); + request = new Message(invite); + init(listener, request.getTransactionId(), request.getConnectionId()); + this.auto_trying = auto_trying; + + changeStatus(STATE_TRYING); + sip_provider.addSipProviderListener(transaction_id, this); + // automatically send "100 Tryng" response and go to STATE_PROCEEDING + if (auto_trying) { + Message trying100 = MessageFactory.createResponse(request, 100, + SipResponses.reasonOf(100), null); + respondWith(trying100); // this method makes it going automatically + // to STATE_PROCEEDING + } + } + + /** Initializes timeouts and listener. */ + void init(InviteTransactionServerListener listener, + TransactionIdentifier transaction_id, + ConnectionIdentifier connection_id) { + this.transaction_listener = listener; + this.transaction_id = transaction_id; + this.connection_id = connection_id; + auto_trying = AUTO_TRYING; + retransmission_to = new Timer(SipStack.retransmission_timeout, + "Retransmission", this); + end_to = new Timer(SipStack.transaction_timeout, "End", this); + clearing_to = new Timer(SipStack.clearing_timeout, "Clearing", this); + printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); + printLog("created", LogLevel.HIGH); + } + + /** Whether automatically sending 100 Trying on INVITE. */ + public void setAutoTrying(boolean auto_trying) { + this.auto_trying = auto_trying; + } + + /** Starts the InviteTransactionServer. */ + public void listen() { + printLog("start", LogLevel.LOW); + if (statusIs(STATE_IDLE)) { + changeStatus(STATE_WAITING); + sip_provider.addSipProviderListener(new TransactionIdentifier( + SipMethods.INVITE), this); + sip_provider.addSipProviderListener(new TransactionIdentifier( + SipMethods.OPTIONS), this); + + } + } + + /** Sends a response message */ + public void respondWith(Message resp) { + response = resp; + int code = response.getStatusLine().getCode(); + if (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING)) + sip_provider.sendMessage(response, connection_id); + if (code >= 100 && code < 200 && statusIs(STATE_TRYING)) { + changeStatus(STATE_PROCEEDING); + return; + } + if (code >= 200 && code < 300 + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + return; + } + if (code >= 300 && code < 700 + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { + changeStatus(STATE_COMPLETED); + // retransmission only in case of unreliable transport + if (true || connection_id == null) { // modified + retransmission_to.start(); + end_to.start(); + } else { + printLog("No retransmissions for reliable transport (" + + connection_id + ")", LogLevel.LOW); + onTimeout(end_to); + } + } + } + + /** + * Method derived from interface SipListener. It's fired from the + * SipProvider when a new message is catch for to the present + * ServerTransaction. + */ + public void onReceivedMessage(SipProvider provider, Message msg) { + if (msg.isRequest()) { + String req_method = msg.getRequestLine().getMethod(); + + // invite received + if (req_method.equals(SipMethods.INVITE)) { + if (statusIs(STATE_WAITING)) { + request = new Message(msg); + connection_id = request.getConnectionId(); + transaction_id = request.getTransactionId(); + sip_provider.addSipProviderListener(transaction_id, this); + sip_provider + .removeSipProviderListener(new TransactionIdentifier( + SipMethods.INVITE)); + changeStatus(STATE_TRYING); + // automatically send "100 Tryng" response and go to + // STATE_PROCEEDING + if (auto_trying) { + Message trying100 = MessageFactory.createResponse( + request, 100, SipResponses.reasonOf(100), null); + respondWith(trying100); // this method makes it going + // automatically to + // STATE_PROCEEDING + } + if (transaction_listener != null) + transaction_listener.onTransRequest(this, msg); + return; + } + if (statusIs(STATE_PROCEEDING) || statusIs(STATE_COMPLETED)) { // retransmission + // of + // the + // last + // response + sip_provider.sendMessage(response, connection_id); + return; + } + } + + if (req_method.equals(SipMethods.OPTIONS)) { + Message ok200 = MessageFactory.createResponse(msg, 200, SipResponses.reasonOf(200), null); + ok200.removeServerHeader(); + ok200.addContactHeader(new ContactHeader(ok200.getToHeader().getNameAddress()), false); + sip_provider.sendMessage(ok200, connection_id); + return; + } + + // ack received + if (req_method.equals(SipMethods.ACK) && statusIs(STATE_COMPLETED)) { + retransmission_to.halt(); + end_to.halt(); + changeStatus(STATE_CONFIRMED); + if (transaction_listener != null) + transaction_listener.onTransFailureAck(this, msg); + clearing_to.start(); + return; + } + } + } + + /** + * Method derived from interface TimerListener. It's fired from an active + * Timer. + */ + public void onTimeout(Timer to) { + try { + if (to.equals(retransmission_to) && statusIs(STATE_COMPLETED)) { + printLog("Retransmission timeout expired", LogLevel.HIGH); + long timeout = 2 * retransmission_to.getTime(); + if (timeout > SipStack.max_retransmission_timeout) + timeout = SipStack.max_retransmission_timeout; + retransmission_to = new Timer(timeout, retransmission_to + .getLabel(), this); + retransmission_to.start(); + sip_provider.sendMessage(response, connection_id); + } + if (to.equals(end_to) && statusIs(STATE_COMPLETED)) { + printLog("End timeout expired", LogLevel.HIGH); + retransmission_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + } + if (to.equals(clearing_to) && statusIs(STATE_CONFIRMED)) { + printLog("Clearing timeout expired", LogLevel.HIGH); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + } + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + + /** Method used to drop an active transaction */ + public void terminate() { + retransmission_to.halt(); + clearing_to.halt(); + end_to.halt(); + if (statusIs(STATE_TRYING)) + sip_provider.removeSipProviderListener(new TransactionIdentifier( + SipMethods.INVITE)); + else + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + } + +} diff --git a/src/org/zoolu/sip/transaction/InviteTransactionServerListener.java b/app/src/main/java/org/zoolu/sip/transaction/InviteTransactionServerListener.java similarity index 97% rename from src/org/zoolu/sip/transaction/InviteTransactionServerListener.java rename to app/src/main/java/org/zoolu/sip/transaction/InviteTransactionServerListener.java index df401f8..1b8a2eb 100644 --- a/src/org/zoolu/sip/transaction/InviteTransactionServerListener.java +++ b/app/src/main/java/org/zoolu/sip/transaction/InviteTransactionServerListener.java @@ -1,41 +1,41 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.message.Message; - -/** - * A TransactionServerListener listens for InviteTransactionServer events. It - * extends TransactionServerListener by adding the - * onTransFailureAck(InviteTransactionServer,Message) method. - */ -public interface InviteTransactionServerListener extends - TransactionServerListener { - /** - * When an InviteTransactionServer goes into the "Confirmed" state receining - * an ACK for NON-2xx response - */ - public void onTransFailureAck(InviteTransactionServer ts, Message ack); - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.message.Message; + +/** + * A TransactionServerListener listens for InviteTransactionServer events. It + * extends TransactionServerListener by adding the + * onTransFailureAck(InviteTransactionServer,Message) method. + */ +public interface InviteTransactionServerListener extends + TransactionServerListener { + /** + * When an InviteTransactionServer goes into the "Confirmed" state receining + * an ACK for NON-2xx response + */ + public void onTransFailureAck(InviteTransactionServer ts, Message ack); + +} diff --git a/src/org/zoolu/sip/transaction/Transaction.java b/app/src/main/java/org/zoolu/sip/transaction/Transaction.java similarity index 96% rename from src/org/zoolu/sip/transaction/Transaction.java rename to app/src/main/java/org/zoolu/sip/transaction/Transaction.java index 9f02d38..11f1f13 100644 --- a/src/org/zoolu/sip/transaction/Transaction.java +++ b/app/src/main/java/org/zoolu/sip/transaction/Transaction.java @@ -1,199 +1,199 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.Timer; -import org.zoolu.tools.TimerListener; -import org.zoolu.tools.Log; -import org.zoolu.tools.LogLevel; - -/** - * Abstract class Transaction is hinerited by classes ClientTransaction, - * ServerTransaction, InviteClientTransaction and InviteServerTransaction. An - * Object Transaction is responsable to handle a new SIP transaction.
- * The changes of the internal status and the received messages are fired to the - * TransactionListener passed to the Transaction Objects.
- */ -public abstract class Transaction implements SipProviderListener, TimerListener { - /** Transactions counter */ - protected static int transaction_counter = 0; - - // all transaction states: - /** State Waiting, used only by server transactions */ - static final int STATE_IDLE = 0; - /** State Waiting, used only by server transactions */ - static final int STATE_WAITING = 1; - /** State Trying */ - static final int STATE_TRYING = 2; - /** State Proceeding */ - static final int STATE_PROCEEDING = 3; - /** State Completed */ - static final int STATE_COMPLETED = 4; - /** State Confirmed, used only by invite server transactions */ - static final int STATE_CONFIRMED = 5; - /** State Waiting. */ - static final int STATE_TERMINATED = 7; - - /** Gets the transaction state. */ - static String getStatus(int st) { - switch (st) { - case STATE_IDLE: - return "T_Idle"; - case STATE_WAITING: - return "T_Waiting"; - case STATE_TRYING: - return "T_Trying"; - case STATE_PROCEEDING: - return "T_Proceeding"; - case STATE_COMPLETED: - return "T_Completed"; - case STATE_CONFIRMED: - return "T_Confirmed"; - case STATE_TERMINATED: - return "T_Terminated"; - default: - return null; - } - } - - /** Transaction sequence number */ - int transaction_sqn; - - /** Event logger. */ - Log log; - - /** - * Lower layer dispatcher that sends and receive messages. The messages - * received by the SipProvider are fired to the Transaction by means of the - * onReceivedMessage() method. - */ - SipProvider sip_provider; - - /** Internal state-machine status */ - int status; - - /** transaction request message/method */ - Message request; - - /** the Transaction ID */ - TransactionIdentifier transaction_id; - - /** Transaction connection id */ - ConnectionIdentifier connection_id; - - /** Costructs a new Transaction */ - protected Transaction(SipProvider sip_provider) { - this.sip_provider = sip_provider; - log = sip_provider.getLog(); - this.transaction_id = null; - this.request = null; - this.connection_id = null; - this.transaction_sqn = transaction_counter++; - this.status = STATE_IDLE; - } - - /** Changes the internal status */ - void changeStatus(int newstatus) { - status = newstatus; - // transaction_listener.onChangedTransactionStatus(status); - printLog("changed transaction state: " + getStatus(), LogLevel.MEDIUM); - } - - /** Whether the internal status is equal to st */ - boolean statusIs(int st) { - return status == st; - } - - /** Gets the current transaction state. */ - String getStatus() { - return getStatus(status); - } - - /** Gets the SipProvider of this Transaction. */ - public SipProvider getSipProvider() { - return sip_provider; - } - - /** Gets the Transaction request message */ - public Message getRequestMessage() { - return request; - } - - /** Gets the Transaction method */ - public String getTransactionMethod() { - return request.getTransactionMethod(); - } - - /** Gets the transaction-ID */ - public TransactionIdentifier getTransactionId() { - return transaction_id; - } - - /** Gets the transaction connection id */ - public ConnectionIdentifier getConnectionId() { - return connection_id; - } - - /** - * Method derived from interface SipListener. It's fired from the - * SipProvider when a new message is catch for to the present - * ServerTransaction. - */ - public void onReceivedMessage(SipProvider provider, Message msg) { // do - // nothing - } - - /** - * Method derived from interface TimerListener. It's fired from an active - * Timer. - */ - public void onTimeout(Timer to) { // do nothing - } - - /** Terminates the transaction. */ - public abstract void terminate(); - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("Transaction#" + transaction_sqn + ": " + str, level - + SipStack.LOG_LEVEL_TRANSACTION); - } - - /** Adds a WARNING to the default Log */ - protected void printWarning(String str, int level) { - printLog("WARNING: " + str, level); - } - - /** Adds the Exception to the log file */ - protected void printException(Exception e, int level) { - if (log != null) - log.printException(e, level + SipStack.LOG_LEVEL_TRANSACTION); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.Timer; +import org.zoolu.tools.TimerListener; +import org.zoolu.tools.Log; +import org.zoolu.tools.LogLevel; + +/** + * Abstract class Transaction is hinerited by classes ClientTransaction, + * ServerTransaction, InviteClientTransaction and InviteServerTransaction. An + * Object Transaction is responsable to handle a new SIP transaction.
+ * The changes of the internal status and the received messages are fired to the + * TransactionListener passed to the Transaction Objects.
+ */ +public abstract class Transaction implements SipProviderListener, TimerListener { + /** Transactions counter */ + protected static int transaction_counter = 0; + + // all transaction states: + /** State Waiting, used only by server transactions */ + static final int STATE_IDLE = 0; + /** State Waiting, used only by server transactions */ + static final int STATE_WAITING = 1; + /** State Trying */ + static final int STATE_TRYING = 2; + /** State Proceeding */ + static final int STATE_PROCEEDING = 3; + /** State Completed */ + static final int STATE_COMPLETED = 4; + /** State Confirmed, used only by invite server transactions */ + static final int STATE_CONFIRMED = 5; + /** State Waiting. */ + static final int STATE_TERMINATED = 7; + + /** Gets the transaction state. */ + static String getStatus(int st) { + switch (st) { + case STATE_IDLE: + return "T_Idle"; + case STATE_WAITING: + return "T_Waiting"; + case STATE_TRYING: + return "T_Trying"; + case STATE_PROCEEDING: + return "T_Proceeding"; + case STATE_COMPLETED: + return "T_Completed"; + case STATE_CONFIRMED: + return "T_Confirmed"; + case STATE_TERMINATED: + return "T_Terminated"; + default: + return null; + } + } + + /** Transaction sequence number */ + int transaction_sqn; + + /** Event logger. */ + Log log; + + /** + * Lower layer dispatcher that sends and receive messages. The messages + * received by the SipProvider are fired to the Transaction by means of the + * onReceivedMessage() method. + */ + SipProvider sip_provider; + + /** Internal state-machine status */ + int status; + + /** transaction request message/method */ + Message request; + + /** the Transaction ID */ + TransactionIdentifier transaction_id; + + /** Transaction connection id */ + ConnectionIdentifier connection_id; + + /** Costructs a new Transaction */ + protected Transaction(SipProvider sip_provider) { + this.sip_provider = sip_provider; + log = sip_provider.getLog(); + this.transaction_id = null; + this.request = null; + this.connection_id = null; + this.transaction_sqn = transaction_counter++; + this.status = STATE_IDLE; + } + + /** Changes the internal status */ + void changeStatus(int newstatus) { + status = newstatus; + // transaction_listener.onChangedTransactionStatus(status); + printLog("changed transaction state: " + getStatus(), LogLevel.MEDIUM); + } + + /** Whether the internal status is equal to st */ + boolean statusIs(int st) { + return status == st; + } + + /** Gets the current transaction state. */ + String getStatus() { + return getStatus(status); + } + + /** Gets the SipProvider of this Transaction. */ + public SipProvider getSipProvider() { + return sip_provider; + } + + /** Gets the Transaction request message */ + public Message getRequestMessage() { + return request; + } + + /** Gets the Transaction method */ + public String getTransactionMethod() { + return request.getTransactionMethod(); + } + + /** Gets the transaction-ID */ + public TransactionIdentifier getTransactionId() { + return transaction_id; + } + + /** Gets the transaction connection id */ + public ConnectionIdentifier getConnectionId() { + return connection_id; + } + + /** + * Method derived from interface SipListener. It's fired from the + * SipProvider when a new message is catch for to the present + * ServerTransaction. + */ + public void onReceivedMessage(SipProvider provider, Message msg) { // do + // nothing + } + + /** + * Method derived from interface TimerListener. It's fired from an active + * Timer. + */ + public void onTimeout(Timer to) { // do nothing + } + + /** Terminates the transaction. */ + public abstract void terminate(); + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("Transaction#" + transaction_sqn + ": " + str, level + + SipStack.LOG_LEVEL_TRANSACTION); + } + + /** Adds a WARNING to the default Log */ + protected void printWarning(String str, int level) { + printLog("WARNING: " + str, level); + } + + /** Adds the Exception to the log file */ + protected void printException(Exception e, int level) { + if (log != null) + log.printException(e, level + SipStack.LOG_LEVEL_TRANSACTION); + } + +} diff --git a/src/org/zoolu/sip/transaction/TransactionClient.java b/app/src/main/java/org/zoolu/sip/transaction/TransactionClient.java similarity index 97% rename from src/org/zoolu/sip/transaction/TransactionClient.java rename to app/src/main/java/org/zoolu/sip/transaction/TransactionClient.java index aa2c824..2765376 100644 --- a/src/org/zoolu/sip/transaction/TransactionClient.java +++ b/app/src/main/java/org/zoolu/sip/transaction/TransactionClient.java @@ -1,210 +1,210 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.Timer; -import org.zoolu.tools.LogLevel; - -/** - * Generic client transaction as defined in RFC 3261 (Section 17.1.2). A - * TransactionClient is responsable to create a new SIP transaction, starting - * with a request message sent through the SipProvider and ending with a final - * response.
- * The changes of the internal status and the received messages are fired to the - * TransactionListener passed to the TransactionClient object.
- */ - -public class TransactionClient extends Transaction { - /** - * the TransactionClientListener that captures the events fired by the - * TransactionClient - */ - TransactionClientListener transaction_listener; - - /** retransmission timeout ("Timer E" in RFC 3261) */ - Timer retransmission_to; - /** transaction timeout ("Timer F" in RFC 3261) */ - Timer transaction_to; - /** clearing timeout ("Timer K" in RFC 3261) */ - Timer clearing_to; - - /** Costructs a new TransactionClient. */ - protected TransactionClient(SipProvider sip_provider) { - super(sip_provider); - transaction_listener = null; - } - - /** Creates a new TransactionClient */ - public TransactionClient(SipProvider sip_provider, Message req, - TransactionClientListener listener) { - super(sip_provider); - request = new Message(req); - init(listener, request.getTransactionId()); - } - - public TransactionClient(SipProvider sip_provider, Message req, - TransactionClientListener listener, int timeout) { - super(sip_provider); - request = new Message(req); - init(listener, request.getTransactionId()); - transaction_to = new Timer(timeout, "Transaction", this); - } - - /** Initializes timeouts and listener. */ - void init(TransactionClientListener listener, - TransactionIdentifier transaction_id) { - this.transaction_listener = listener; - this.transaction_id = transaction_id; - retransmission_to = new Timer(SipStack.retransmission_timeout, - "Retransmission", this); - transaction_to = new Timer(SipStack.transaction_timeout, "Transaction", - this); - clearing_to = new Timer(SipStack.clearing_timeout, "Clearing", this); - printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); - printLog("created", LogLevel.HIGH); - } - - /** Starts the TransactionClient and sends the transaction request. */ - public void request() { - printLog("start", LogLevel.LOW); - changeStatus(STATE_TRYING); - retransmission_to.start(); - transaction_to.start(); - - sip_provider.addSipProviderListener(transaction_id, this); - connection_id = sip_provider.sendMessage(request); - } - - /** - * Method derived from interface SipListener. It's fired from the - * SipProvider when a new message is received for to the present - * TransactionClient. - */ - public void onReceivedMessage(SipProvider provider, Message msg) { - if (msg.isResponse()) - { - int code = msg.getStatusLine().getCode(); - if (code >= 100 && code < 200 - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { - if (statusIs(STATE_TRYING)) - changeStatus(STATE_PROCEEDING); - if (transaction_listener != null) - transaction_listener.onTransProvisionalResponse(this, msg); - return; - } - if (code >= 200 && code < 700 && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) - { - retransmission_to.halt(); - transaction_to.halt(); - changeStatus(STATE_COMPLETED); - if (code < 300) { - if (transaction_listener != null) - transaction_listener.onTransSuccessResponse(this, msg); - } else { - if (transaction_listener != null) - transaction_listener.onTransFailureResponse(this, msg); - } - transaction_listener = null; - if (true || connection_id == null) // modified - clearing_to.start(); - else { - printLog("clearing_to=0 for reliable transport", - LogLevel.LOW); - onTimeout(clearing_to); - } - return; - } - } - } - - /** - * Method derived from interface TimerListener. It's fired from an active - * Timer. - */ - public void onTimeout(Timer to) { - try { - if (to.equals(retransmission_to) - && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { - printLog("Retransmission timeout expired", LogLevel.HIGH); - // retransmission only for unreliable transport - if (true || connection_id == null) { // modified - sip_provider.sendMessage(request); - long timeout = 2 * retransmission_to.getTime(); - if (timeout > SipStack.max_retransmission_timeout - || statusIs(STATE_PROCEEDING)) - timeout = SipStack.max_retransmission_timeout; - retransmission_to = new Timer(timeout, retransmission_to - .getLabel(), this); - retransmission_to.start(); - } else - printLog("No retransmissions for reliable transport (" - + connection_id + ")", LogLevel.LOW); - } - if (to.equals(transaction_to)) { - printLog("Transaction timeout expired", LogLevel.HIGH); - retransmission_to.halt(); - clearing_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - if (transaction_listener != null) - transaction_listener.onTransTimeout(this); - transaction_listener = null; - } - if (to.equals(clearing_to)) { - printLog("Clearing timeout expired", LogLevel.HIGH); - retransmission_to.halt(); - transaction_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - } - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - - /** Terminates the transaction. */ - public void terminate() { - if (!statusIs(STATE_TERMINATED)) { - retransmission_to.halt(); - transaction_to.halt(); - clearing_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - } - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("TransactionClient#" + transaction_sqn + ": " + str, - level + SipStack.LOG_LEVEL_TRANSACTION); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.Timer; +import org.zoolu.tools.LogLevel; + +/** + * Generic client transaction as defined in RFC 3261 (Section 17.1.2). A + * TransactionClient is responsable to create a new SIP transaction, starting + * with a request message sent through the SipProvider and ending with a final + * response.
+ * The changes of the internal status and the received messages are fired to the + * TransactionListener passed to the TransactionClient object.
+ */ + +public class TransactionClient extends Transaction { + /** + * the TransactionClientListener that captures the events fired by the + * TransactionClient + */ + TransactionClientListener transaction_listener; + + /** retransmission timeout ("Timer E" in RFC 3261) */ + Timer retransmission_to; + /** transaction timeout ("Timer F" in RFC 3261) */ + Timer transaction_to; + /** clearing timeout ("Timer K" in RFC 3261) */ + Timer clearing_to; + + /** Costructs a new TransactionClient. */ + protected TransactionClient(SipProvider sip_provider) { + super(sip_provider); + transaction_listener = null; + } + + /** Creates a new TransactionClient */ + public TransactionClient(SipProvider sip_provider, Message req, + TransactionClientListener listener) { + super(sip_provider); + request = new Message(req); + init(listener, request.getTransactionId()); + } + + public TransactionClient(SipProvider sip_provider, Message req, + TransactionClientListener listener, int timeout) { + super(sip_provider); + request = new Message(req); + init(listener, request.getTransactionId()); + transaction_to = new Timer(timeout, "Transaction", this); + } + + /** Initializes timeouts and listener. */ + void init(TransactionClientListener listener, + TransactionIdentifier transaction_id) { + this.transaction_listener = listener; + this.transaction_id = transaction_id; + retransmission_to = new Timer(SipStack.retransmission_timeout, + "Retransmission", this); + transaction_to = new Timer(SipStack.transaction_timeout, "Transaction", + this); + clearing_to = new Timer(SipStack.clearing_timeout, "Clearing", this); + printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); + printLog("created", LogLevel.HIGH); + } + + /** Starts the TransactionClient and sends the transaction request. */ + public void request() { + printLog("start", LogLevel.LOW); + changeStatus(STATE_TRYING); + retransmission_to.start(); + transaction_to.start(); + + sip_provider.addSipProviderListener(transaction_id, this); + connection_id = sip_provider.sendMessage(request); + } + + /** + * Method derived from interface SipListener. It's fired from the + * SipProvider when a new message is received for to the present + * TransactionClient. + */ + public void onReceivedMessage(SipProvider provider, Message msg) { + if (msg.isResponse()) + { + int code = msg.getStatusLine().getCode(); + if (code >= 100 && code < 200 + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { + if (statusIs(STATE_TRYING)) + changeStatus(STATE_PROCEEDING); + if (transaction_listener != null) + transaction_listener.onTransProvisionalResponse(this, msg); + return; + } + if (code >= 200 && code < 700 && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) + { + retransmission_to.halt(); + transaction_to.halt(); + changeStatus(STATE_COMPLETED); + if (code < 300) { + if (transaction_listener != null) + transaction_listener.onTransSuccessResponse(this, msg); + } else { + if (transaction_listener != null) + transaction_listener.onTransFailureResponse(this, msg); + } + transaction_listener = null; + if (true || connection_id == null) // modified + clearing_to.start(); + else { + printLog("clearing_to=0 for reliable transport", + LogLevel.LOW); + onTimeout(clearing_to); + } + return; + } + } + } + + /** + * Method derived from interface TimerListener. It's fired from an active + * Timer. + */ + public void onTimeout(Timer to) { + try { + if (to.equals(retransmission_to) + && (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING))) { + printLog("Retransmission timeout expired", LogLevel.HIGH); + // retransmission only for unreliable transport + if (true || connection_id == null) { // modified + sip_provider.sendMessage(request); + long timeout = 2 * retransmission_to.getTime(); + if (timeout > SipStack.max_retransmission_timeout + || statusIs(STATE_PROCEEDING)) + timeout = SipStack.max_retransmission_timeout; + retransmission_to = new Timer(timeout, retransmission_to + .getLabel(), this); + retransmission_to.start(); + } else + printLog("No retransmissions for reliable transport (" + + connection_id + ")", LogLevel.LOW); + } + if (to.equals(transaction_to)) { + printLog("Transaction timeout expired", LogLevel.HIGH); + retransmission_to.halt(); + clearing_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + if (transaction_listener != null) + transaction_listener.onTransTimeout(this); + transaction_listener = null; + } + if (to.equals(clearing_to)) { + printLog("Clearing timeout expired", LogLevel.HIGH); + retransmission_to.halt(); + transaction_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + } + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + + /** Terminates the transaction. */ + public void terminate() { + if (!statusIs(STATE_TERMINATED)) { + retransmission_to.halt(); + transaction_to.halt(); + clearing_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + } + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("TransactionClient#" + transaction_sqn + ": " + str, + level + SipStack.LOG_LEVEL_TRANSACTION); + } + +} diff --git a/src/org/zoolu/sip/transaction/TransactionClientListener.java b/app/src/main/java/org/zoolu/sip/transaction/TransactionClientListener.java similarity index 97% rename from src/org/zoolu/sip/transaction/TransactionClientListener.java rename to app/src/main/java/org/zoolu/sip/transaction/TransactionClientListener.java index a4340f7..3efd5e2 100644 --- a/src/org/zoolu/sip/transaction/TransactionClientListener.java +++ b/app/src/main/java/org/zoolu/sip/transaction/TransactionClientListener.java @@ -1,56 +1,56 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.message.Message; - -/** - * A TransactionClientListener listens for TransactionClient events. It collects - * all TransactionClient callback functions. - */ -public interface TransactionClientListener { - /** - * When the TransactionClient is (or goes) in "Proceeding" state and - * receives a new 1xx provisional response - */ - public void onTransProvisionalResponse(TransactionClient tc, Message resp); - - /** - * When the TransactionClient goes into the "Completed" state receiving a - * 2xx response - */ - public void onTransSuccessResponse(TransactionClient tc, Message resp); - - /** - * When the TransactionClient goes into the "Completed" state receiving a - * 300-699 response - */ - public void onTransFailureResponse(TransactionClient tc, Message resp); - - /** - * When the TransactionClient goes into the "Terminated" state, caused by - * transaction timeout - */ - public void onTransTimeout(TransactionClient tc); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.message.Message; + +/** + * A TransactionClientListener listens for TransactionClient events. It collects + * all TransactionClient callback functions. + */ +public interface TransactionClientListener { + /** + * When the TransactionClient is (or goes) in "Proceeding" state and + * receives a new 1xx provisional response + */ + public void onTransProvisionalResponse(TransactionClient tc, Message resp); + + /** + * When the TransactionClient goes into the "Completed" state receiving a + * 2xx response + */ + public void onTransSuccessResponse(TransactionClient tc, Message resp); + + /** + * When the TransactionClient goes into the "Completed" state receiving a + * 300-699 response + */ + public void onTransFailureResponse(TransactionClient tc, Message resp); + + /** + * When the TransactionClient goes into the "Terminated" state, caused by + * transaction timeout + */ + public void onTransTimeout(TransactionClient tc); +} diff --git a/src/org/zoolu/sip/transaction/TransactionServer.java b/app/src/main/java/org/zoolu/sip/transaction/TransactionServer.java similarity index 96% rename from src/org/zoolu/sip/transaction/TransactionServer.java rename to app/src/main/java/org/zoolu/sip/transaction/TransactionServer.java index 3fd5648..fc27c37 100644 --- a/src/org/zoolu/sip/transaction/TransactionServer.java +++ b/app/src/main/java/org/zoolu/sip/transaction/TransactionServer.java @@ -1,194 +1,194 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.tools.Timer; -import org.zoolu.sip.provider.*; -import org.zoolu.sip.message.*; -import org.zoolu.tools.LogLevel; - -/** - * Generic server transaction as defined in RFC 3261 (Section 17.2.2). A - * TransactionServer is responsable to create a new SIP transaction that starts - * with a request message received by the SipProvider and ends sending a final - * response.
- * The changes of the internal status and the received messages are fired to the - * TransactionListener passed to the TransactionServer object.
- * When costructing a new TransactionServer, the transaction type is passed as - * String parameter to the costructor (e.g. "CANCEL", "BYE", etc..) - */ - -public class TransactionServer extends Transaction { - /** - * the TransactionServerListener that captures the events fired by the - * TransactionServer - */ - TransactionServerListener transaction_listener; - - /** last response message */ - Message response; - - /** clearing timeout ("Timer J" in RFC 3261) */ - Timer clearing_to; - - /** Costructs a new TransactionServer. */ - protected TransactionServer(SipProvider sip_provider) { - super(sip_provider); - transaction_listener = null; - response = null; - } - - /** Creates a new TransactionServer of type method. */ - public TransactionServer(SipProvider sip_provider, String method, - TransactionServerListener listener) { - super(sip_provider); - init(listener, new TransactionIdentifier(method), null); - } - - /** Creates a new TransactionServer for the already received request req. */ - public TransactionServer(SipProvider provider, Message req, - TransactionServerListener listener) { - super(provider); - request = new Message(req); - init(listener, request.getTransactionId(), request.getConnectionId()); - - printLog("start", LogLevel.LOW); - changeStatus(STATE_TRYING); - sip_provider.addSipProviderListener(transaction_id, this); - } - - /** Initializes timeouts and listener. */ - void init(TransactionServerListener listener, - TransactionIdentifier transaction_id, - ConnectionIdentifier connection_id) { - this.transaction_listener = listener; - this.transaction_id = transaction_id; - this.connection_id = connection_id; - this.response = null; - clearing_to = new Timer(SipStack.transaction_timeout, "Clearing", this); - printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); - printLog("created", LogLevel.HIGH); - } - - /** Starts the TransactionServer. */ - public void listen() { - if (statusIs(STATE_IDLE)) { - printLog("start", LogLevel.LOW); - changeStatus(STATE_WAITING); - sip_provider.addSipProviderListener(transaction_id, this); - } - } - - /** Sends a response message */ - public void respondWith(Message resp) { - response = resp; - if (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING)) { - sip_provider.sendMessage(response, connection_id); - int code = response.getStatusLine().getCode(); - if (code >= 100 && code < 200 && statusIs(STATE_TRYING)) { - changeStatus(STATE_PROCEEDING); - } - if (code >= 200 && code < 700) { - changeStatus(STATE_COMPLETED); - if (true || connection_id == null) // modified - clearing_to.start(); - else { - printLog("clearing_to=0 for reliable transport", - LogLevel.LOW); - onTimeout(clearing_to); - } - } - } - } - - /** - * Method derived from interface SipListener. It's fired from the - * SipProvider when a new message is received for to the present - * TransactionServer. - */ - public void onReceivedMessage(SipProvider provider, Message msg) { - if (msg.isRequest()) { - if (statusIs(STATE_WAITING)) { - request = new Message(msg); - connection_id = msg.getConnectionId(); - sip_provider.removeSipProviderListener(transaction_id); - transaction_id = request.getTransactionId(); - sip_provider.addSipProviderListener(transaction_id, this); - changeStatus(STATE_TRYING); - if (transaction_listener != null) - transaction_listener.onTransRequest(this, msg); - return; - } - if (statusIs(STATE_PROCEEDING) || statusIs(STATE_COMPLETED)) { // retransmission - // of - // the - // last - // response - printLog("response retransmission", LogLevel.LOW); - sip_provider.sendMessage(response, connection_id); - return; - } - } - } - - /** - * Method derived from interface TimerListener. It's fired from an active - * Timer. - */ - public void onTimeout(Timer to) { - try { - if (to.equals(clearing_to)) { - printLog("Clearing timeout expired", LogLevel.HIGH); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - // clearing_to=null; - } - } catch (Exception e) { - printException(e, LogLevel.HIGH); - } - } - - /** Terminates the transaction. */ - public void terminate() { - if (!statusIs(STATE_TERMINATED)) { - clearing_to.halt(); - sip_provider.removeSipProviderListener(transaction_id); - changeStatus(STATE_TERMINATED); - transaction_listener = null; - // clearing_to=null; - } - } - - // **************************** Logs ****************************/ - - /** Adds a new string to the default Log */ - protected void printLog(String str, int level) { - if (log != null) - log.println("TransactionServer#" + transaction_sqn + ": " + str, - level + SipStack.LOG_LEVEL_TRANSACTION); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.tools.Timer; +import org.zoolu.sip.provider.*; +import org.zoolu.sip.message.*; +import org.zoolu.tools.LogLevel; + +/** + * Generic server transaction as defined in RFC 3261 (Section 17.2.2). A + * TransactionServer is responsable to create a new SIP transaction that starts + * with a request message received by the SipProvider and ends sending a final + * response.
+ * The changes of the internal status and the received messages are fired to the + * TransactionListener passed to the TransactionServer object.
+ * When costructing a new TransactionServer, the transaction type is passed as + * String parameter to the costructor (e.g. "CANCEL", "BYE", etc..) + */ + +public class TransactionServer extends Transaction { + /** + * the TransactionServerListener that captures the events fired by the + * TransactionServer + */ + TransactionServerListener transaction_listener; + + /** last response message */ + Message response; + + /** clearing timeout ("Timer J" in RFC 3261) */ + Timer clearing_to; + + /** Costructs a new TransactionServer. */ + protected TransactionServer(SipProvider sip_provider) { + super(sip_provider); + transaction_listener = null; + response = null; + } + + /** Creates a new TransactionServer of type method. */ + public TransactionServer(SipProvider sip_provider, String method, + TransactionServerListener listener) { + super(sip_provider); + init(listener, new TransactionIdentifier(method), null); + } + + /** Creates a new TransactionServer for the already received request req. */ + public TransactionServer(SipProvider provider, Message req, + TransactionServerListener listener) { + super(provider); + request = new Message(req); + init(listener, request.getTransactionId(), request.getConnectionId()); + + printLog("start", LogLevel.LOW); + changeStatus(STATE_TRYING); + sip_provider.addSipProviderListener(transaction_id, this); + } + + /** Initializes timeouts and listener. */ + void init(TransactionServerListener listener, + TransactionIdentifier transaction_id, + ConnectionIdentifier connection_id) { + this.transaction_listener = listener; + this.transaction_id = transaction_id; + this.connection_id = connection_id; + this.response = null; + clearing_to = new Timer(SipStack.transaction_timeout, "Clearing", this); + printLog("id: " + String.valueOf(transaction_id), LogLevel.HIGH); + printLog("created", LogLevel.HIGH); + } + + /** Starts the TransactionServer. */ + public void listen() { + if (statusIs(STATE_IDLE)) { + printLog("start", LogLevel.LOW); + changeStatus(STATE_WAITING); + sip_provider.addSipProviderListener(transaction_id, this); + } + } + + /** Sends a response message */ + public void respondWith(Message resp) { + response = resp; + if (statusIs(STATE_TRYING) || statusIs(STATE_PROCEEDING)) { + sip_provider.sendMessage(response, connection_id); + int code = response.getStatusLine().getCode(); + if (code >= 100 && code < 200 && statusIs(STATE_TRYING)) { + changeStatus(STATE_PROCEEDING); + } + if (code >= 200 && code < 700) { + changeStatus(STATE_COMPLETED); + if (true || connection_id == null) // modified + clearing_to.start(); + else { + printLog("clearing_to=0 for reliable transport", + LogLevel.LOW); + onTimeout(clearing_to); + } + } + } + } + + /** + * Method derived from interface SipListener. It's fired from the + * SipProvider when a new message is received for to the present + * TransactionServer. + */ + public void onReceivedMessage(SipProvider provider, Message msg) { + if (msg.isRequest()) { + if (statusIs(STATE_WAITING)) { + request = new Message(msg); + connection_id = msg.getConnectionId(); + sip_provider.removeSipProviderListener(transaction_id); + transaction_id = request.getTransactionId(); + sip_provider.addSipProviderListener(transaction_id, this); + changeStatus(STATE_TRYING); + if (transaction_listener != null) + transaction_listener.onTransRequest(this, msg); + return; + } + if (statusIs(STATE_PROCEEDING) || statusIs(STATE_COMPLETED)) { // retransmission + // of + // the + // last + // response + printLog("response retransmission", LogLevel.LOW); + sip_provider.sendMessage(response, connection_id); + return; + } + } + } + + /** + * Method derived from interface TimerListener. It's fired from an active + * Timer. + */ + public void onTimeout(Timer to) { + try { + if (to.equals(clearing_to)) { + printLog("Clearing timeout expired", LogLevel.HIGH); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + // clearing_to=null; + } + } catch (Exception e) { + printException(e, LogLevel.HIGH); + } + } + + /** Terminates the transaction. */ + public void terminate() { + if (!statusIs(STATE_TERMINATED)) { + clearing_to.halt(); + sip_provider.removeSipProviderListener(transaction_id); + changeStatus(STATE_TERMINATED); + transaction_listener = null; + // clearing_to=null; + } + } + + // **************************** Logs ****************************/ + + /** Adds a new string to the default Log */ + protected void printLog(String str, int level) { + if (log != null) + log.println("TransactionServer#" + transaction_sqn + ": " + str, + level + SipStack.LOG_LEVEL_TRANSACTION); + } + +} diff --git a/src/org/zoolu/sip/transaction/TransactionServerListener.java b/app/src/main/java/org/zoolu/sip/transaction/TransactionServerListener.java similarity index 97% rename from src/org/zoolu/sip/transaction/TransactionServerListener.java rename to app/src/main/java/org/zoolu/sip/transaction/TransactionServerListener.java index 56df2d3..6829aa2 100644 --- a/src/org/zoolu/sip/transaction/TransactionServerListener.java +++ b/app/src/main/java/org/zoolu/sip/transaction/TransactionServerListener.java @@ -1,39 +1,39 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.sip.transaction; - -import org.zoolu.sip.message.Message; - -/** - * A TransactionServerListener listens for TransactionServer events. It collects - * all TransactionServer callback functions. - */ -public interface TransactionServerListener { - /** - * When the TransactionServer goes into the "Trying" state receiving a - * request - */ - public void onTransRequest(TransactionServer ts, Message req); - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.sip.transaction; + +import org.zoolu.sip.message.Message; + +/** + * A TransactionServerListener listens for TransactionServer events. It collects + * all TransactionServer callback functions. + */ +public interface TransactionServerListener { + /** + * When the TransactionServer goes into the "Trying" state receiving a + * request + */ + public void onTransRequest(TransactionServer ts, Message req); + +} diff --git a/src/org/zoolu/tools/Archive.java b/app/src/main/java/org/zoolu/tools/Archive.java similarity index 97% rename from src/org/zoolu/tools/Archive.java rename to app/src/main/java/org/zoolu/tools/Archive.java index 52ddf6b..cf652f3 100644 --- a/src/org/zoolu/tools/Archive.java +++ b/app/src/main/java/org/zoolu/tools/Archive.java @@ -1,137 +1,137 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.io.File; -import java.io.InputStream; -import java.net.URL; - -/** - * Collection of static methods for handling files and jar archives. - */ -public class Archive { - /** The base path */ - public static String BASE_PATH = (new File("")).getAbsolutePath(); - - // **** PersonalJava **** - // public static String BASE_PATH="\\My Documents\\Lavoro\\"; - - /** Gets the complete url of a file included within a jar archive. */ - public static URL getJarURL(String jar_archive, String file_name) { - if (jar_archive == null || file_name == null) - return null; - // else - String url = "jar:file:" + BASE_PATH + "/" + jar_archive + "!/" - + file_name; - try { - return new URL(url); - } catch (java.net.MalformedURLException e) { - System.err.println("ERROR: malformed url " + url); - return null; - } - } - - /** Gets the complete url of a file. */ - public static URL getFileURL(String file_name) { - if (file_name == null) - return null; - // else - String url = "file:" + BASE_PATH + "/" + file_name; - try { - return new URL("file:" + BASE_PATH + "/" + file_name); - } catch (java.net.MalformedURLException e) { - System.err.println("ERROR: malformed url " + url); - return null; - } - } - - /** Gets an Image from file */ - /* - * public static Image getImage(String file_name) { if (file_name==null) - * return null; //else Toolkit toolkit=Toolkit.getDefaultToolkit(); Image - * image=null; file_name=BASE_PATH+"/"+file_name; if ((new - * File(file_name)).canRead()) { image=toolkit.getImage(file_name); // wait - * for image loading (4 attempts with 80ms of delay) for (int i=0; i<4 && - * image.getWidth(null)<0; i++) try { Thread.sleep(80); } catch (Exception - * e) {} } return image; } - */ - - /** Gets an Image from a URL. */ - /* - * public static Image getImage(URL url) { if (url==null) return null; - * //else Toolkit toolkit=Toolkit.getDefaultToolkit(); Image image=null; try { - * url.openConnection().connect(); image=toolkit.getImage(url); // wait for - * image loading (4 attempts with 80ms of delay) for (int i=0; i<4 && - * image.getWidth(null)<0; i++) try { Thread.sleep(80); } catch (Exception - * e) {} } catch (java.io.IOException e) { System.err.println("ERROR: can't - * read the file "+url.toString()); } return image; } - */ - - /** Gets an ImageIcon from file */ - /* - * public static ImageIcon getImageIcon(String file_name) { - * file_name=BASE_PATH+"/"+file_name; return new ImageIcon(file_name); } - */ - - /** Gets an ImageIcon from an URL */ - /* - * public static ImageIcon getImageIcon(URL url) { if (url==null) return - * null; //else ImageIcon icon=null; try { url.openConnection().connect(); - * icon=new ImageIcon(url); } catch (java.io.IOException e) { - * System.err.println("ERROR: can't read the file "+url.toString()); } - * return icon; } - */ - - /** Gets an InputStream from an URL */ - public static InputStream getInputStream(URL url) { - if (url == null) - return null; - // else - InputStream in = null; - try { - in = (url).openStream(); - } catch (java.io.IOException e) { - System.err.println("ERROR: can't read the file " + url.toString()); - } - return in; - } - - /** Gets an AudioInputStream from an URL */ - /* - public static AudioInputStream getAudioInputStream(URL url) - throws javax.sound.sampled.UnsupportedAudioFileException { - if (url == null) - return null; - // else - AudioInputStream in = null; - try { - in = AudioSystem.getAudioInputStream(url); - } catch (java.io.IOException e) { - System.err.println("ERROR: can't read the file " + url.toString()); - } - return in; - } - */ - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.io.File; +import java.io.InputStream; +import java.net.URL; + +/** + * Collection of static methods for handling files and jar archives. + */ +public class Archive { + /** The base path */ + public static String BASE_PATH = (new File("")).getAbsolutePath(); + + // **** PersonalJava **** + // public static String BASE_PATH="\\My Documents\\Lavoro\\"; + + /** Gets the complete url of a file included within a jar archive. */ + public static URL getJarURL(String jar_archive, String file_name) { + if (jar_archive == null || file_name == null) + return null; + // else + String url = "jar:file:" + BASE_PATH + "/" + jar_archive + "!/" + + file_name; + try { + return new URL(url); + } catch (java.net.MalformedURLException e) { + System.err.println("ERROR: malformed url " + url); + return null; + } + } + + /** Gets the complete url of a file. */ + public static URL getFileURL(String file_name) { + if (file_name == null) + return null; + // else + String url = "file:" + BASE_PATH + "/" + file_name; + try { + return new URL("file:" + BASE_PATH + "/" + file_name); + } catch (java.net.MalformedURLException e) { + System.err.println("ERROR: malformed url " + url); + return null; + } + } + + /** Gets an Image from file */ + /* + * public static Image getImage(String file_name) { if (file_name==null) + * return null; //else Toolkit toolkit=Toolkit.getDefaultToolkit(); Image + * image=null; file_name=BASE_PATH+"/"+file_name; if ((new + * File(file_name)).canRead()) { image=toolkit.getImage(file_name); // wait + * for image loading (4 attempts with 80ms of delay) for (int i=0; i<4 && + * image.getWidth(null)<0; i++) try { Thread.sleep(80); } catch (Exception + * e) {} } return image; } + */ + + /** Gets an Image from a URL. */ + /* + * public static Image getImage(URL url) { if (url==null) return null; + * //else Toolkit toolkit=Toolkit.getDefaultToolkit(); Image image=null; try { + * url.openConnection().connect(); image=toolkit.getImage(url); // wait for + * image loading (4 attempts with 80ms of delay) for (int i=0; i<4 && + * image.getWidth(null)<0; i++) try { Thread.sleep(80); } catch (Exception + * e) {} } catch (java.io.IOException e) { System.err.println("ERROR: can't + * read the file "+url.toString()); } return image; } + */ + + /** Gets an ImageIcon from file */ + /* + * public static ImageIcon getImageIcon(String file_name) { + * file_name=BASE_PATH+"/"+file_name; return new ImageIcon(file_name); } + */ + + /** Gets an ImageIcon from an URL */ + /* + * public static ImageIcon getImageIcon(URL url) { if (url==null) return + * null; //else ImageIcon icon=null; try { url.openConnection().connect(); + * icon=new ImageIcon(url); } catch (java.io.IOException e) { + * System.err.println("ERROR: can't read the file "+url.toString()); } + * return icon; } + */ + + /** Gets an InputStream from an URL */ + public static InputStream getInputStream(URL url) { + if (url == null) + return null; + // else + InputStream in = null; + try { + in = (url).openStream(); + } catch (java.io.IOException e) { + System.err.println("ERROR: can't read the file " + url.toString()); + } + return in; + } + + /** Gets an AudioInputStream from an URL */ + /* + public static AudioInputStream getAudioInputStream(URL url) + throws javax.sound.sampled.UnsupportedAudioFileException { + if (url == null) + return null; + // else + AudioInputStream in = null; + try { + in = AudioSystem.getAudioInputStream(url); + } catch (java.io.IOException e) { + System.err.println("ERROR: can't read the file " + url.toString()); + } + return in; + } + */ + +} diff --git a/src/org/zoolu/tools/Assert.java b/app/src/main/java/org/zoolu/tools/Assert.java similarity index 96% rename from src/org/zoolu/tools/Assert.java rename to app/src/main/java/org/zoolu/tools/Assert.java index f0c58e9..a55f49d 100644 --- a/src/org/zoolu/tools/Assert.java +++ b/app/src/main/java/org/zoolu/tools/Assert.java @@ -1,72 +1,72 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Class Assert provides some static methods to check some inline conditions. - * When an assertion fails an AssertionException is throws.

Such a tool - * could be helpful for debugging. - */ -public class Assert { - /** - * Check that exp is true, otherwise an AssertionException is - * thrown. - */ - public final static void isTrue(boolean exp) { - if (!exp) - onError("Assertion failed"); - } - - /** - * Check that exp is false, otherwise an AssertionException is - * thrown. - */ - public final static void isFalse(boolean exp) { - if (exp) - onError("Assertion failed"); - } - - /** - * Check that exp is true, otherwise an AssertionException is - * thrown. - */ - public final static void isTrue(boolean exp, String msg) { - if (!exp) - onError("Assertion failed: " + msg); - } - - /** - * Check that exp is false, otherwise an AssertionException is - * thrown. - */ - public final static void isFalse(boolean exp, String msg) { - if (exp) - onError("Assertion failed: " + msg); - } - - private static void onError(String msg) { - throw new AssertException(msg); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Class Assert provides some static methods to check some inline conditions. + * When an assertion fails an AssertionException is throws.

Such a tool + * could be helpful for debugging. + */ +public class Assert { + /** + * Check that exp is true, otherwise an AssertionException is + * thrown. + */ + public final static void isTrue(boolean exp) { + if (!exp) + onError("Assertion failed"); + } + + /** + * Check that exp is false, otherwise an AssertionException is + * thrown. + */ + public final static void isFalse(boolean exp) { + if (exp) + onError("Assertion failed"); + } + + /** + * Check that exp is true, otherwise an AssertionException is + * thrown. + */ + public final static void isTrue(boolean exp, String msg) { + if (!exp) + onError("Assertion failed: " + msg); + } + + /** + * Check that exp is false, otherwise an AssertionException is + * thrown. + */ + public final static void isFalse(boolean exp, String msg) { + if (exp) + onError("Assertion failed: " + msg); + } + + private static void onError(String msg) { + throw new AssertException(msg); + } + +} diff --git a/src/org/zoolu/tools/AssertException.java b/app/src/main/java/org/zoolu/tools/AssertException.java similarity index 97% rename from src/org/zoolu/tools/AssertException.java rename to app/src/main/java/org/zoolu/tools/AssertException.java index 4df9ed1..15eb635 100644 --- a/src/org/zoolu/tools/AssertException.java +++ b/app/src/main/java/org/zoolu/tools/AssertException.java @@ -1,40 +1,40 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * AssertException is used to indicate that a condition is not met.
It is - * thrown by methods isTrue() and isFalse() of class Assert. - */ -public class AssertException extends java.lang.RuntimeException { - /** Costructs a new AssertException. */ - public AssertException(String msg) { - super(msg); - } - - /** Costructs a new AssertException. */ - public AssertException() { - super(); - } +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * AssertException is used to indicate that a condition is not met.
It is + * thrown by methods isTrue() and isFalse() of class Assert. + */ +public class AssertException extends java.lang.RuntimeException { + /** Costructs a new AssertException. */ + public AssertException(String msg) { + super(msg); + } + + /** Costructs a new AssertException. */ + public AssertException() { + super(); + } } \ No newline at end of file diff --git a/src/org/zoolu/tools/Base64.java b/app/src/main/java/org/zoolu/tools/Base64.java similarity index 96% rename from src/org/zoolu/tools/Base64.java rename to app/src/main/java/org/zoolu/tools/Base64.java index cb2e2e2..e180992 100644 --- a/src/org/zoolu/tools/Base64.java +++ b/app/src/main/java/org/zoolu/tools/Base64.java @@ -1,171 +1,171 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Class Base64 can be used for base64-encoding a byte array and/or for - * base64-decoding a base64-string. - * - * @author Camilla Ferramola, Univeristy of Parma, Italy - 2004 - */ -public class Base64 { - private static final String base64codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - private static int[] aux = new int[4]; - - /** Base64 encoder */ - public static String encode(byte[] input) { - - String stringacod = ""; - byte[] bin = new byte[3]; - - int iter = (input.length) / 3; - int nzero = (input.length) % 3; - int i = 0; - - for (i = 0; i < iter; i++) { - bin[0] = input[i * 3]; - bin[1] = input[i * 3 + 1]; - bin[2] = input[i * 3 + 2]; - - aux[0] = ((bin[0] >>> 2) & 63); - aux[1] = ((bin[0] & 3) << 4) + ((bin[1] >>> 4) & 15); - aux[2] = ((bin[1] & 15) << 2) + ((bin[2] >>> 6) & 3); - aux[3] = (bin[2] & 63); - - // uso gli interi memorizzati in aux[] come indice della stringa - // base64codes - // System.out.println("aux[0]="+aux[0]+" aux[1]="+aux[1]); - stringacod = stringacod + base64codes.charAt(aux[0]) - + base64codes.charAt(aux[1]) + base64codes.charAt(aux[2]) - + base64codes.charAt(aux[3]); - } - - if (i == iter) { - if (nzero == 0) { - } else if (nzero == 1) { // l'ultimo pacchetto da analizzare ha 8 - // bit - // quindi ottengo due caratteri in base64 e due caratteri - // padding "=" - aux[0] = ((input[iter * 3] >>> 2) & 63); - aux[1] = (input[iter * 3] & 3) << 4; - - stringacod = stringacod + base64codes.charAt(aux[0]) - + base64codes.charAt(aux[1]) + "=="; - } else if (nzero == 2) { // l'ultimo pacchetto da analizzare ha - // 16 bit quindi ottengo - // tre caratteri in base 64 e uno di padding "=" - aux[0] = ((input[iter * 3] >>> 2) & 63); - aux[1] = ((input[iter * 3] & 3) << 4) - + ((input[iter * 3 + 1] >>> 4) & 15); - aux[2] = (input[iter * 3 + 1] & 15) << 2; - - stringacod = stringacod + base64codes.charAt(aux[0]) - + base64codes.charAt(aux[1]) - + base64codes.charAt(aux[2]) + "="; - } - } - - return stringacod; - - } - - /** Base64 decoder */ - public static byte[] decode(String stringacod) { - // tolgo gli eventuali "=" alla fine della stringa - int uguale = stringacod.indexOf("="); - if (uguale != -1) - stringacod = stringacod.substring(0, uguale); - - int[] bin = new int[3]; - int iter = (stringacod.length()) / 4; - int resto = (stringacod.length()) % 4; - - int nzero = 0; - if (resto != 0) - nzero = 1; - byte[] output = new byte[iter * 3 + nzero * (resto - 1)]; - - int i = 0; - for (i = 0; i < iter; i++) { - aux[0] = base64codes.indexOf(stringacod.charAt(i * 4)); - aux[1] = base64codes.indexOf(stringacod.charAt(i * 4 + 1)); - aux[2] = base64codes.indexOf(stringacod.charAt(i * 4 + 2)); - aux[3] = base64codes.indexOf(stringacod.charAt(i * 4 + 3)); - - bin[0] = (aux[0] << 2) + (aux[1] >>> 4); - bin[1] = (aux[1] % 16 << 4) + (aux[2] >>> 2); - bin[2] = (aux[2] % 4 << 6) + aux[3]; - - output[i * 3] = (byte) bin[0]; - output[i * 3 + 1] = (byte) bin[1]; - output[i * 3 + 2] = (byte) bin[2]; - } - - if (i == iter) { - // per come e costruita la codifica a base64 - // togliendo gli eventuali caratteri "=" di padding - // il resto puo essere 0, 2 o 3 - if (resto == 0) { - } - if (resto == 2) { - aux[0] = base64codes.indexOf(stringacod.charAt(i * 4)); - aux[1] = base64codes.indexOf(stringacod.charAt(i * 4 + 1)); - - bin[0] = (aux[0] << 2) + (aux[1] >>> 4); - - output[i * 3] = (byte) bin[0]; - } - - if (resto == 3) { - aux[0] = base64codes.indexOf(stringacod.charAt(i * 4)); - aux[1] = base64codes.indexOf(stringacod.charAt(i * 4 + 1)); - aux[2] = base64codes.indexOf(stringacod.charAt(i * 4 + 2)); - - bin[0] = (aux[0] << 2) + (aux[1] >>> 4); - bin[1] = (aux[1] % 16 << 4) + (aux[2] >>> 2); - - output[i * 3] = (byte) bin[0]; - output[i * 3 + 1] = (byte) bin[1]; - } - } - return output; - } - - // ******************************* MAIN ******************************* - - public static void main(String[] args) { - String messaggio = args[0]; - byte[] bmess = messaggio.getBytes(); - String mess64 = encode(bmess); - System.out.println("messaggio codificato: " + mess64); - byte[] decodificato = decode(mess64); - String strdecodificato = ""; - try { - strdecodificato = new String(decodificato, "ISO-8859-1"); - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println("messaggio decodificato e: " + strdecodificato); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Class Base64 can be used for base64-encoding a byte array and/or for + * base64-decoding a base64-string. + * + * @author Camilla Ferramola, Univeristy of Parma, Italy - 2004 + */ +public class Base64 { + private static final String base64codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + private static int[] aux = new int[4]; + + /** Base64 encoder */ + public static String encode(byte[] input) { + + String stringacod = ""; + byte[] bin = new byte[3]; + + int iter = (input.length) / 3; + int nzero = (input.length) % 3; + int i = 0; + + for (i = 0; i < iter; i++) { + bin[0] = input[i * 3]; + bin[1] = input[i * 3 + 1]; + bin[2] = input[i * 3 + 2]; + + aux[0] = ((bin[0] >>> 2) & 63); + aux[1] = ((bin[0] & 3) << 4) + ((bin[1] >>> 4) & 15); + aux[2] = ((bin[1] & 15) << 2) + ((bin[2] >>> 6) & 3); + aux[3] = (bin[2] & 63); + + // uso gli interi memorizzati in aux[] come indice della stringa + // base64codes + // System.out.println("aux[0]="+aux[0]+" aux[1]="+aux[1]); + stringacod = stringacod + base64codes.charAt(aux[0]) + + base64codes.charAt(aux[1]) + base64codes.charAt(aux[2]) + + base64codes.charAt(aux[3]); + } + + if (i == iter) { + if (nzero == 0) { + } else if (nzero == 1) { // l'ultimo pacchetto da analizzare ha 8 + // bit + // quindi ottengo due caratteri in base64 e due caratteri + // padding "=" + aux[0] = ((input[iter * 3] >>> 2) & 63); + aux[1] = (input[iter * 3] & 3) << 4; + + stringacod = stringacod + base64codes.charAt(aux[0]) + + base64codes.charAt(aux[1]) + "=="; + } else if (nzero == 2) { // l'ultimo pacchetto da analizzare ha + // 16 bit quindi ottengo + // tre caratteri in base 64 e uno di padding "=" + aux[0] = ((input[iter * 3] >>> 2) & 63); + aux[1] = ((input[iter * 3] & 3) << 4) + + ((input[iter * 3 + 1] >>> 4) & 15); + aux[2] = (input[iter * 3 + 1] & 15) << 2; + + stringacod = stringacod + base64codes.charAt(aux[0]) + + base64codes.charAt(aux[1]) + + base64codes.charAt(aux[2]) + "="; + } + } + + return stringacod; + + } + + /** Base64 decoder */ + public static byte[] decode(String stringacod) { + // tolgo gli eventuali "=" alla fine della stringa + int uguale = stringacod.indexOf("="); + if (uguale != -1) + stringacod = stringacod.substring(0, uguale); + + int[] bin = new int[3]; + int iter = (stringacod.length()) / 4; + int resto = (stringacod.length()) % 4; + + int nzero = 0; + if (resto != 0) + nzero = 1; + byte[] output = new byte[iter * 3 + nzero * (resto - 1)]; + + int i = 0; + for (i = 0; i < iter; i++) { + aux[0] = base64codes.indexOf(stringacod.charAt(i * 4)); + aux[1] = base64codes.indexOf(stringacod.charAt(i * 4 + 1)); + aux[2] = base64codes.indexOf(stringacod.charAt(i * 4 + 2)); + aux[3] = base64codes.indexOf(stringacod.charAt(i * 4 + 3)); + + bin[0] = (aux[0] << 2) + (aux[1] >>> 4); + bin[1] = (aux[1] % 16 << 4) + (aux[2] >>> 2); + bin[2] = (aux[2] % 4 << 6) + aux[3]; + + output[i * 3] = (byte) bin[0]; + output[i * 3 + 1] = (byte) bin[1]; + output[i * 3 + 2] = (byte) bin[2]; + } + + if (i == iter) { + // per come e costruita la codifica a base64 + // togliendo gli eventuali caratteri "=" di padding + // il resto puo essere 0, 2 o 3 + if (resto == 0) { + } + if (resto == 2) { + aux[0] = base64codes.indexOf(stringacod.charAt(i * 4)); + aux[1] = base64codes.indexOf(stringacod.charAt(i * 4 + 1)); + + bin[0] = (aux[0] << 2) + (aux[1] >>> 4); + + output[i * 3] = (byte) bin[0]; + } + + if (resto == 3) { + aux[0] = base64codes.indexOf(stringacod.charAt(i * 4)); + aux[1] = base64codes.indexOf(stringacod.charAt(i * 4 + 1)); + aux[2] = base64codes.indexOf(stringacod.charAt(i * 4 + 2)); + + bin[0] = (aux[0] << 2) + (aux[1] >>> 4); + bin[1] = (aux[1] % 16 << 4) + (aux[2] >>> 2); + + output[i * 3] = (byte) bin[0]; + output[i * 3 + 1] = (byte) bin[1]; + } + } + return output; + } + + // ******************************* MAIN ******************************* + + public static void main(String[] args) { + String messaggio = args[0]; + byte[] bmess = messaggio.getBytes(); + String mess64 = encode(bmess); + System.out.println("messaggio codificato: " + mess64); + byte[] decodificato = decode(mess64); + String strdecodificato = ""; + try { + strdecodificato = new String(decodificato, "ISO-8859-1"); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("messaggio decodificato e: " + strdecodificato); + } +} diff --git a/src/org/zoolu/tools/Configurable.java b/app/src/main/java/org/zoolu/tools/Configurable.java similarity index 97% rename from src/org/zoolu/tools/Configurable.java rename to app/src/main/java/org/zoolu/tools/Configurable.java index cff40ef..5c81266 100644 --- a/src/org/zoolu/tools/Configurable.java +++ b/app/src/main/java/org/zoolu/tools/Configurable.java @@ -1,33 +1,33 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Configurable is the base interface for classes that can be configurated by a - * text file. - */ -public interface Configurable { - /** Parses a single text line. */ - public void parseLine(String line); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Configurable is the base interface for classes that can be configurated by a + * text file. + */ +public interface Configurable { + /** Parses a single text line. */ + public void parseLine(String line); +} diff --git a/src/org/zoolu/tools/Configure.java b/app/src/main/java/org/zoolu/tools/Configure.java similarity index 96% rename from src/org/zoolu/tools/Configure.java rename to app/src/main/java/org/zoolu/tools/Configure.java index de28cd2..71c8862 100644 --- a/src/org/zoolu/tools/Configure.java +++ b/app/src/main/java/org/zoolu/tools/Configure.java @@ -1,113 +1,113 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.io.*; - -/** - * Configure helps the loading and saving of configuration data. - */ -public class Configure { - - /** String 'NONE' used as undefined value (i.e. null). */ - public static String NONE = "NONE"; - - /** The object that should be configured */ - Configurable configurable; - - /** Parses a single text line (read from the config file) */ - protected void parseLine(String line) { // parse the text line.. - } - - /** Converts the entire object into lines (to be saved into the config file) */ - protected String toLines() { // convert the object into to one or more - // text line.. - return ""; - } - - /** Costructs a Configure container */ - protected Configure() { - this.configurable = null; - } - - /** Costructs a Configure container */ - public Configure(Configurable configurable, String file) { - this.configurable = configurable; - loadFile(file); - } - - /** Loads Configure attributes from the specified file */ - protected void loadFile(String file) { // System.out.println("DEBUG: - // loading Configuration"); - if (file == null) { // System.out.println("DEBUG: no Configuration - // file"); - return; - } - // else - BufferedReader in = null; - try { - in = new BufferedReader(new FileReader(file)); - - while (true) { - String line = null; - try { - line = in.readLine(); - } catch (Exception e) { - e.printStackTrace(); - System.exit(0); - } - if (line == null) - break; - - if (!line.startsWith("#")) { - if (configurable == null) - parseLine(line); - else - configurable.parseLine(line); - } - } - in.close(); - } catch (Exception e) { - System.err.println("WARNING: error reading file \"" + file + "\""); - // System.exit(0); - return; - } - // System.out.println("DEBUG: loading Configuration: done."); - } - - /** Saves Configure attributes on the specified file */ - protected void saveFile(String file) { - if (file == null) - return; - // else - try { - BufferedWriter out = new BufferedWriter(new FileWriter(file)); - out.write(toLines()); - out.close(); - } catch (IOException e) { - System.err.println("ERROR writing on file \"" + file + "\""); - } - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.io.*; + +/** + * Configure helps the loading and saving of configuration data. + */ +public class Configure { + + /** String 'NONE' used as undefined value (i.e. null). */ + public static String NONE = "NONE"; + + /** The object that should be configured */ + Configurable configurable; + + /** Parses a single text line (read from the config file) */ + protected void parseLine(String line) { // parse the text line.. + } + + /** Converts the entire object into lines (to be saved into the config file) */ + protected String toLines() { // convert the object into to one or more + // text line.. + return ""; + } + + /** Costructs a Configure container */ + protected Configure() { + this.configurable = null; + } + + /** Costructs a Configure container */ + public Configure(Configurable configurable, String file) { + this.configurable = configurable; + loadFile(file); + } + + /** Loads Configure attributes from the specified file */ + protected void loadFile(String file) { // System.out.println("DEBUG: + // loading Configuration"); + if (file == null) { // System.out.println("DEBUG: no Configuration + // file"); + return; + } + // else + BufferedReader in = null; + try { + in = new BufferedReader(new FileReader(file)); + + while (true) { + String line = null; + try { + line = in.readLine(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(0); + } + if (line == null) + break; + + if (!line.startsWith("#")) { + if (configurable == null) + parseLine(line); + else + configurable.parseLine(line); + } + } + in.close(); + } catch (Exception e) { + System.err.println("WARNING: error reading file \"" + file + "\""); + // System.exit(0); + return; + } + // System.out.println("DEBUG: loading Configuration: done."); + } + + /** Saves Configure attributes on the specified file */ + protected void saveFile(String file) { + if (file == null) + return; + // else + try { + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + out.write(toLines()); + out.close(); + } catch (IOException e) { + System.err.println("ERROR writing on file \"" + file + "\""); + } + } + +} diff --git a/src/org/zoolu/tools/DateFormat.java b/app/src/main/java/org/zoolu/tools/DateFormat.java similarity index 97% rename from src/org/zoolu/tools/DateFormat.java rename to app/src/main/java/org/zoolu/tools/DateFormat.java index f03729d..ddac231 100644 --- a/src/org/zoolu/tools/DateFormat.java +++ b/app/src/main/java/org/zoolu/tools/DateFormat.java @@ -1,188 +1,188 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.util.Date; -import java.util.Calendar; - -/** - * Class DateFormat replaces the format method of java.text.DateFormat. - */ -public class DateFormat { - - /** Months */ - private static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", - "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - - /** Days of the week */ - private static final String[] WEEKDAYS = { "Sun", "Mon", "Tue", "Wed", - "Thu", "Fri", "Sat" }; - - /** Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date */ - public static String formatHHMMSS(Date date) { // DateFormat df=new - // SimpleDateFormat("HH:mm:ss.SSS - // EEE dd MMM - // yyyy",Locale.US); - // return df.format(date); - /* - * String str=date.toString(); // dow mon dd hh:mm:ss zzz yyyy int - * len=str.length(); String weekday=str.substring(0,3); String - * month=str.substring(4,7); String day=str.substring(8,10); String - * time=str.substring(11,19); String - * millisec=Integer.toString((int)(date.getTime()%1000)); if - * (millisec.length()==1) millisec="00"+millisec; else if - * (millisec.length()==2) millisec="0"+millisec; String - * year=str.substring(len-4,len); - * - * return time+"."+millisec+" "+weekday+" "+day+" "+month+" "+year; - */ - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - String weekday = WEEKDAYS[cal.get(Calendar.DAY_OF_WEEK) - 1]; - String month = MONTHS[cal.get(Calendar.MONTH)]; - String year = Integer.toString(cal.get(Calendar.YEAR)); - String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); - String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)); - String min = Integer.toString(cal.get(Calendar.MINUTE)); - String sec = Integer.toString(cal.get(Calendar.SECOND)); - String millisec = Integer.toString(cal.get(Calendar.MILLISECOND)); - if (day.length() == 1) - day = "0" + day; - if (hour.length() == 1) - hour = "0" + hour; - if (min.length() == 1) - min = "0" + min; - if (sec.length() == 1) - sec = "0" + sec; - if (millisec.length() == 1) - millisec = "00" + millisec; - else if (millisec.length() == 2) - millisec = "0" + millisec; - - return hour + ":" + min + ":" + sec + "." + millisec + " " + weekday - + " " + day + " " + month + " " + year; - } - - /** Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date */ - public static String formatYYYYMMDD(Date date) { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - // String month=MONTHS[cal.get(Calendar.MONTH)]; - String year = Integer.toString(cal.get(Calendar.YEAR)); - String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); - String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)); - String min = Integer.toString(cal.get(Calendar.MINUTE)); - String sec = Integer.toString(cal.get(Calendar.SECOND)); - String millisec = Integer.toString(cal.get(Calendar.MILLISECOND)); - if (day.length() == 1) - day = "0" + day; - if (hour.length() == 1) - hour = "0" + hour; - if (min.length() == 1) - min = "0" + min; - if (sec.length() == 1) - sec = "0" + sec; - if (millisec.length() == 1) - millisec = "00" + millisec; - else if (millisec.length() == 2) - millisec = "0" + millisec; - - String month = Integer.toString(cal.get(Calendar.MONTH) + 1); - if (month.length() == 1) - month = "0" + month; - - return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" - + sec + "." + millisec; - } - - /** Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date */ - public static String formatEEEddMMM(Date date) { // DateFormat df=new - // SimpleDateFormat("EEE, - // dd MMM yyyy hh:mm:ss - // 'GMT'",Locale.US); - // return df.format(date); - /* - * String str=date.toString(); // dow mon dd hh:mm:ss zzz yyyy int - * len=str.length(); String weekday=str.substring(0,3); String - * month=str.substring(4,7); String day=str.substring(8,10); String - * time=str.substring(11,19); String year=str.substring(len-4,len); - * return weekday+", "+day+" "+month+" "+year+" "+time+" GMT"; - */ - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - String weekday = WEEKDAYS[cal.get(Calendar.DAY_OF_WEEK) - 1]; - String month = MONTHS[cal.get(Calendar.MONTH)]; - String year = Integer.toString(cal.get(Calendar.YEAR)); - String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); - String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)); - String min = Integer.toString(cal.get(Calendar.MINUTE)); - String sec = Integer.toString(cal.get(Calendar.SECOND)); - if (day.length() == 1) - day = "0" + day; - if (hour.length() == 1) - hour = "0" + hour; - if (min.length() == 1) - min = "0" + min; - if (sec.length() == 1) - sec = "0" + sec; - - return weekday + ", " + day + " " + month + " " + year + " " + hour - + ":" + min + ":" + sec + " GMT"; - } - - /** Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date */ - public static Date parseEEEddMMM(String str, int index) { // DateFormat - // df=new - // SimpleDateFormat("EEE, - // dd MMM yyyy - // hh:mm:ss - // 'GMT'",Locale.US); - // return df.format(date); - Calendar cal = Calendar.getInstance(); - char[] delim = { ' ', ',', ':' }; - Parser par = new Parser(str, index); - int day = par.getInt(); // day of the month - String MMM = par.getString(); // month - int month = 0; - for (; month < 12; month++) - if (MMM.equalsIgnoreCase(MONTHS[month])) - break; - if (month == 12) - return null; // ERROR.. - // else - int year = par.getInt(); - int hour = Integer.parseInt(par.getWord(delim)); - int min = Integer.parseInt(par.getWord(delim)); - int sec = Integer.parseInt(par.getWord(delim)); - - cal.set(Calendar.YEAR, year); - cal.set(Calendar.MONTH, month); - cal.set(Calendar.DAY_OF_MONTH, day); - cal.set(Calendar.HOUR_OF_DAY, hour); - cal.set(Calendar.MINUTE, min); - cal.set(Calendar.SECOND, sec); - - return cal.getTime(); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.util.Date; +import java.util.Calendar; + +/** + * Class DateFormat replaces the format method of java.text.DateFormat. + */ +public class DateFormat { + + /** Months */ + private static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", + "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + + /** Days of the week */ + private static final String[] WEEKDAYS = { "Sun", "Mon", "Tue", "Wed", + "Thu", "Fri", "Sat" }; + + /** Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date */ + public static String formatHHMMSS(Date date) { // DateFormat df=new + // SimpleDateFormat("HH:mm:ss.SSS + // EEE dd MMM + // yyyy",Locale.US); + // return df.format(date); + /* + * String str=date.toString(); // dow mon dd hh:mm:ss zzz yyyy int + * len=str.length(); String weekday=str.substring(0,3); String + * month=str.substring(4,7); String day=str.substring(8,10); String + * time=str.substring(11,19); String + * millisec=Integer.toString((int)(date.getTime()%1000)); if + * (millisec.length()==1) millisec="00"+millisec; else if + * (millisec.length()==2) millisec="0"+millisec; String + * year=str.substring(len-4,len); + * + * return time+"."+millisec+" "+weekday+" "+day+" "+month+" "+year; + */ + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + String weekday = WEEKDAYS[cal.get(Calendar.DAY_OF_WEEK) - 1]; + String month = MONTHS[cal.get(Calendar.MONTH)]; + String year = Integer.toString(cal.get(Calendar.YEAR)); + String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); + String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)); + String min = Integer.toString(cal.get(Calendar.MINUTE)); + String sec = Integer.toString(cal.get(Calendar.SECOND)); + String millisec = Integer.toString(cal.get(Calendar.MILLISECOND)); + if (day.length() == 1) + day = "0" + day; + if (hour.length() == 1) + hour = "0" + hour; + if (min.length() == 1) + min = "0" + min; + if (sec.length() == 1) + sec = "0" + sec; + if (millisec.length() == 1) + millisec = "00" + millisec; + else if (millisec.length() == 2) + millisec = "0" + millisec; + + return hour + ":" + min + ":" + sec + "." + millisec + " " + weekday + + " " + day + " " + month + " " + year; + } + + /** Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date */ + public static String formatYYYYMMDD(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + // String month=MONTHS[cal.get(Calendar.MONTH)]; + String year = Integer.toString(cal.get(Calendar.YEAR)); + String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); + String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)); + String min = Integer.toString(cal.get(Calendar.MINUTE)); + String sec = Integer.toString(cal.get(Calendar.SECOND)); + String millisec = Integer.toString(cal.get(Calendar.MILLISECOND)); + if (day.length() == 1) + day = "0" + day; + if (hour.length() == 1) + hour = "0" + hour; + if (min.length() == 1) + min = "0" + min; + if (sec.length() == 1) + sec = "0" + sec; + if (millisec.length() == 1) + millisec = "00" + millisec; + else if (millisec.length() == 2) + millisec = "0" + millisec; + + String month = Integer.toString(cal.get(Calendar.MONTH) + 1); + if (month.length() == 1) + month = "0" + month; + + return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + + sec + "." + millisec; + } + + /** Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date */ + public static String formatEEEddMMM(Date date) { // DateFormat df=new + // SimpleDateFormat("EEE, + // dd MMM yyyy hh:mm:ss + // 'GMT'",Locale.US); + // return df.format(date); + /* + * String str=date.toString(); // dow mon dd hh:mm:ss zzz yyyy int + * len=str.length(); String weekday=str.substring(0,3); String + * month=str.substring(4,7); String day=str.substring(8,10); String + * time=str.substring(11,19); String year=str.substring(len-4,len); + * return weekday+", "+day+" "+month+" "+year+" "+time+" GMT"; + */ + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + String weekday = WEEKDAYS[cal.get(Calendar.DAY_OF_WEEK) - 1]; + String month = MONTHS[cal.get(Calendar.MONTH)]; + String year = Integer.toString(cal.get(Calendar.YEAR)); + String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); + String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)); + String min = Integer.toString(cal.get(Calendar.MINUTE)); + String sec = Integer.toString(cal.get(Calendar.SECOND)); + if (day.length() == 1) + day = "0" + day; + if (hour.length() == 1) + hour = "0" + hour; + if (min.length() == 1) + min = "0" + min; + if (sec.length() == 1) + sec = "0" + sec; + + return weekday + ", " + day + " " + month + " " + year + " " + hour + + ":" + min + ":" + sec + " GMT"; + } + + /** Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date */ + public static Date parseEEEddMMM(String str, int index) { // DateFormat + // df=new + // SimpleDateFormat("EEE, + // dd MMM yyyy + // hh:mm:ss + // 'GMT'",Locale.US); + // return df.format(date); + Calendar cal = Calendar.getInstance(); + char[] delim = { ' ', ',', ':' }; + Parser par = new Parser(str, index); + int day = par.getInt(); // day of the month + String MMM = par.getString(); // month + int month = 0; + for (; month < 12; month++) + if (MMM.equalsIgnoreCase(MONTHS[month])) + break; + if (month == 12) + return null; // ERROR.. + // else + int year = par.getInt(); + int hour = Integer.parseInt(par.getWord(delim)); + int min = Integer.parseInt(par.getWord(delim)); + int sec = Integer.parseInt(par.getWord(delim)); + + cal.set(Calendar.YEAR, year); + cal.set(Calendar.MONTH, month); + cal.set(Calendar.DAY_OF_MONTH, day); + cal.set(Calendar.HOUR_OF_DAY, hour); + cal.set(Calendar.MINUTE, min); + cal.set(Calendar.SECOND, sec); + + return cal.getTime(); + } +} diff --git a/src/org/zoolu/tools/ExceptionPrinter.java b/app/src/main/java/org/zoolu/tools/ExceptionPrinter.java similarity index 97% rename from src/org/zoolu/tools/ExceptionPrinter.java rename to app/src/main/java/org/zoolu/tools/ExceptionPrinter.java index 2ec6a85..730a4a8 100644 --- a/src/org/zoolu/tools/ExceptionPrinter.java +++ b/app/src/main/java/org/zoolu/tools/ExceptionPrinter.java @@ -1,46 +1,46 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -/** - * Class ExceptionPrinter just contains method getStackTrace(Exception e) - * for getting a String reporting the stack-trace and description of Exception - * e. - */ -public class ExceptionPrinter { - /** - * Gets the stack-trace and description of Exception e. - * - * @return It returns a String with the stack-trace and description of the - * Exception. - */ - public static String getStackTraceOf(Exception e) { // return e.toString(); - ByteArrayOutputStream err = new ByteArrayOutputStream(); - e.printStackTrace(new PrintStream(err)); - return err.toString(); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +/** + * Class ExceptionPrinter just contains method getStackTrace(Exception e) + * for getting a String reporting the stack-trace and description of Exception + * e. + */ +public class ExceptionPrinter { + /** + * Gets the stack-trace and description of Exception e. + * + * @return It returns a String with the stack-trace and description of the + * Exception. + */ + public static String getStackTraceOf(Exception e) { // return e.toString(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + e.printStackTrace(new PrintStream(err)); + return err.toString(); + } +} diff --git a/src/org/zoolu/tools/HashSet.java b/app/src/main/java/org/zoolu/tools/HashSet.java similarity index 95% rename from src/org/zoolu/tools/HashSet.java rename to app/src/main/java/org/zoolu/tools/HashSet.java index 75d9cef..9ada2be 100644 --- a/src/org/zoolu/tools/HashSet.java +++ b/app/src/main/java/org/zoolu/tools/HashSet.java @@ -1,62 +1,62 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.util.Vector; - -/** - * HashSet - */ -public class HashSet { - Vector set; - - public HashSet() { - set = new Vector(); - } - - public int size() { - return set.size(); - } - - public boolean isEmpty() { - return set.isEmpty(); - } - - public boolean add(Object o) { - set.addElement(o); - return true; - } - - public boolean remove(Object o) { - return set.removeElement(o); - } - - public boolean contains(Object o) { - return set.contains(o); - } - - public Iterator iterator() { - return new Iterator(set); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.util.Vector; + +/** + * HashSet + */ +public class HashSet { + Vector set; + + public HashSet() { + set = new Vector(); + } + + public int size() { + return set.size(); + } + + public boolean isEmpty() { + return set.isEmpty(); + } + + public boolean add(Object o) { + set.addElement(o); + return true; + } + + public boolean remove(Object o) { + return set.removeElement(o); + } + + public boolean contains(Object o) { + return set.contains(o); + } + + public Iterator iterator() { + return new Iterator(set); + } +} diff --git a/src/org/zoolu/tools/InnerTimer.java b/app/src/main/java/org/zoolu/tools/InnerTimer.java similarity index 96% rename from src/org/zoolu/tools/InnerTimer.java rename to app/src/main/java/org/zoolu/tools/InnerTimer.java index 3752d74..a3acf49 100644 --- a/src/org/zoolu/tools/InnerTimer.java +++ b/app/src/main/java/org/zoolu/tools/InnerTimer.java @@ -1,49 +1,49 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** Class InnerTimer implements a separated-thread timer */ -class InnerTimer extends Thread { - long timeout; - InnerTimerListener listener; - - public InnerTimer(long timeout, InnerTimerListener listener) { - this.timeout = timeout; - this.listener = listener; - start(); - } - - public void run() { - if (listener != null) { - try { - Thread.sleep(timeout); - listener.onInnerTimeout(); - } catch (Exception e) { - - e.printStackTrace(); - } - listener = null; - } - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** Class InnerTimer implements a separated-thread timer */ +class InnerTimer extends Thread { + long timeout; + InnerTimerListener listener; + + public InnerTimer(long timeout, InnerTimerListener listener) { + this.timeout = timeout; + this.listener = listener; + start(); + } + + public void run() { + if (listener != null) { + try { + Thread.sleep(timeout); + listener.onInnerTimeout(); + } catch (Exception e) { + + e.printStackTrace(); + } + listener = null; + } + } +} diff --git a/src/org/zoolu/tools/InnerTimerListener.java b/app/src/main/java/org/zoolu/tools/InnerTimerListener.java similarity index 97% rename from src/org/zoolu/tools/InnerTimerListener.java rename to app/src/main/java/org/zoolu/tools/InnerTimerListener.java index f6ce3f9..4aebd4d 100644 --- a/src/org/zoolu/tools/InnerTimerListener.java +++ b/app/src/main/java/org/zoolu/tools/InnerTimerListener.java @@ -1,30 +1,30 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** Listen for an InnerTimer */ -interface InnerTimerListener { - /** When the Timeout fires */ - void onInnerTimeout(); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** Listen for an InnerTimer */ +interface InnerTimerListener { + /** When the Timeout fires */ + void onInnerTimeout(); +} diff --git a/src/org/zoolu/tools/InnerTimerST.java b/app/src/main/java/org/zoolu/tools/InnerTimerST.java similarity index 96% rename from src/org/zoolu/tools/InnerTimerST.java rename to app/src/main/java/org/zoolu/tools/InnerTimerST.java index d2873ff..a5b04bb 100644 --- a/src/org/zoolu/tools/InnerTimerST.java +++ b/app/src/main/java/org/zoolu/tools/InnerTimerST.java @@ -1,47 +1,47 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Class InnerTimerST implements a single-thread timer. The same thread is used - * for all instances of class InnerTimerST. - */ -class InnerTimerST extends java.util.TimerTask { - static java.util.Timer single_timer = new java.util.Timer(true); - - // long timeout; - InnerTimerListener listener; - - public InnerTimerST(long timeout, InnerTimerListener listener) { // this.timeout=timeout; - this.listener = listener; - single_timer.schedule(this, timeout); - } - - public void run() { - if (listener != null) { - listener.onInnerTimeout(); - listener = null; - } - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Class InnerTimerST implements a single-thread timer. The same thread is used + * for all instances of class InnerTimerST. + */ +class InnerTimerST extends java.util.TimerTask { + static java.util.Timer single_timer = new java.util.Timer(true); + + // long timeout; + InnerTimerListener listener; + + public InnerTimerST(long timeout, InnerTimerListener listener) { // this.timeout=timeout; + this.listener = listener; + single_timer.schedule(this, timeout); + } + + public void run() { + if (listener != null) { + listener.onInnerTimeout(); + listener = null; + } + } +} diff --git a/src/org/zoolu/tools/Iterator.java b/app/src/main/java/org/zoolu/tools/Iterator.java similarity index 95% rename from src/org/zoolu/tools/Iterator.java rename to app/src/main/java/org/zoolu/tools/Iterator.java index 6d54dde..5c733c4 100644 --- a/src/org/zoolu/tools/Iterator.java +++ b/app/src/main/java/org/zoolu/tools/Iterator.java @@ -1,55 +1,55 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.util.Vector; - -/** - * Iterator - */ -public class Iterator { - Vector v; - int i; - - public Iterator(Vector vector) { - v = vector; - i = -1; - } - - public boolean hasNext() { - return i < (v.size() - 1); - } - - public Object next() { - if (++i < v.size()) - return v.elementAt(i); - else - return null; - } - - public void remove() { - v.removeElementAt(i); - i--; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.util.Vector; + +/** + * Iterator + */ +public class Iterator { + Vector v; + int i; + + public Iterator(Vector vector) { + v = vector; + i = -1; + } + + public boolean hasNext() { + return i < (v.size() - 1); + } + + public Object next() { + if (++i < v.size()) + return v.elementAt(i); + else + return null; + } + + public void remove() { + v.removeElementAt(i); + i--; + } +} diff --git a/src/org/zoolu/tools/Log.java b/app/src/main/java/org/zoolu/tools/Log.java similarity index 96% rename from src/org/zoolu/tools/Log.java rename to app/src/main/java/org/zoolu/tools/Log.java index 3f6ace3..42842ec 100644 --- a/src/org/zoolu/tools/Log.java +++ b/app/src/main/java/org/zoolu/tools/Log.java @@ -1,250 +1,250 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.io.*; -import java.util.Date; - -// import java.util.Locale; -// import java.text.DateFormat; -// import java.text.SimpleDateFormat; - -/** - * Class Log allows the printing of log messages onto standard output or files - * or any PrintStream. - *

- * Every Log has a verboselevel associated with it; any log request with - * loglevel less or equal to the verbose-level is logged.
- * Verbose level 0 indicates no log. The log levels should be greater than 0. - *

- * Parameter logname, if non-null, is used as log header (i.e. written - * at the begin of each log row). - */ -public class Log { - - /** ***************************** Attributes ****************************** */ - - /** (static) Default maximum log file size (4MB) */ - // public static final long MAX_SIZE=4096*1024; // 4MB - public static final long MAX_SIZE = 1024 * 1024; // 1MB - - /** The log output stream */ - PrintStream out_stream; - - /** The log tag at the beginning of each log row */ - String log_tag; - - /** - * The log_level. - *

- * Only messages with a level less or equal this log_level are - * effectively logged - */ - int verbose_level; - - /** - * The maximum size of the log stream/file [bytes] Value -1 indicates no - * maximum size - */ - long max_size; - - /** The size of the log tag (e.g. "MyLog: " has tag_size=7) */ - int tag_size; - - /** Whether messages are logged */ - boolean do_log; - - /** The char counter of the already logged data */ - long counter; - - /** **************************** Constructors ***************************** */ - - /** - * Associates a new Log to the PrintStream outstream. Log size has - * no bound - */ - - public Log(PrintStream out_stream, String log_tag, int verbose_level) { - init(out_stream, log_tag, verbose_level, -1); - } - - /** - * Associates a new Log to the file filename. Log size is limited to - * the MAX_SIZE - */ - public Log(String file_name, String log_tag, int verbose_level) { - PrintStream os = null; - if (verbose_level > 0) { - try { - os = new PrintStream(new FileOutputStream(file_name)); - } catch (IOException e) { - e.printStackTrace(); - } - init(os, log_tag, verbose_level, MAX_SIZE); - } - } - - /** - * Associates a new Log to the file filename. Log size is limited to - * logsize [bytes] - */ - - public Log(String file_name, String log_tag, int verbose_level, - long max_size) { - PrintStream os = null; - if (verbose_level > 0) { - try { - os = new PrintStream(new FileOutputStream(file_name)); - } catch (IOException e) { - e.printStackTrace(); - } - init(os, log_tag, verbose_level, max_size); - } else { - init(null, log_tag, 0, 0); - do_log = false; - } - } - - /** - * Associates a new Log to the file filename. Log size is limited to - * logsize [bytes] - */ - - public Log(String file_name, String log_tag, int verbose_level, - long max_size, boolean append) { - PrintStream os = null; - if (verbose_level > 0) { - try { - os = new PrintStream(new FileOutputStream(file_name, append)); - } catch (IOException e) { - e.printStackTrace(); - } - init(os, log_tag, verbose_level, max_size); - } else { - init(null, log_tag, 0, 0); - do_log = false; - } - } - - /** ************************** Protected methods *************************** */ - - /** Initializes the log */ - protected void init(PrintStream out_stream, String log_tag, - int verbose_level, long max_size) { - this.out_stream = out_stream; - this.log_tag = log_tag; - this.verbose_level = verbose_level; - this.max_size = max_size; - - if (log_tag != null) - tag_size = log_tag.length() + 2; - else - tag_size = 0; - do_log = true; - counter = 0; - } - - /** Flushes */ - protected Log flush() { - if (verbose_level > 0) - out_stream.flush(); - return this; - } - - /** *************************** Public methods **************************** */ - - /** Closes the log */ - public void close() { - do_log = false; - out_stream.close(); - } - - /** Logs the Exception */ - public Log printException(Exception e, int level) { // ByteArrayOutputStream - // err=new - // ByteArrayOutputStream(); - // e.printStackTrace(new PrintStream(err)); - // return println("Exception: "+err.toString(),level); - return println("Exception: " + ExceptionPrinter.getStackTraceOf(e), - level); - } - - /** Logs the Exception.toString() and Exception.printStackTrace() */ - public Log printException(Exception e) { - return printException(e, 1); - } - - /** Logs the packet timestamp */ - public Log printPacketTimestamp(String proto, String remote_addr, - int remote_port, int len, String message, int level) { - String str = remote_addr + ":" + remote_port + "/" + proto + " (" + len - + " bytes)"; - if (message != null) - str += ": " + message; - println(DateFormat.formatHHMMSS(new Date()) + ", " + str, level); - return this; - } - - /** - * Prints the log if level isn't greater than the Log - * verbose_level - */ - public Log println(String message, int level) { - return print(message + "\r\n", level).flush(); - } - - /** Prints the log if the Log verbose_level is greater than 0 */ - public Log println(String message) { - return println(message, 1); - } - - /** Prints the log if the Log verbose_level is greater than 0 */ - public Log print(String message) { - return print(message, 1); - } - - /** - * Prints the log if level isn't greater than the Log - * verbose_level - */ - public Log print(String message, int level) { - if (do_log && level <= verbose_level) { - if (log_tag != null) - out_stream.print(log_tag + ": " + message); - else - out_stream.print(message); - - if (max_size >= 0) { - counter += tag_size + message.length(); - if (counter > max_size) { - out_stream - .println("\r\n----MAXIMUM LOG SIZE----\r\nSuccessive logs are lost."); - do_log = false; - } - } - } - return this; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.io.*; +import java.util.Date; + +// import java.util.Locale; +// import java.text.DateFormat; +// import java.text.SimpleDateFormat; + +/** + * Class Log allows the printing of log messages onto standard output or files + * or any PrintStream. + *

+ * Every Log has a verboselevel associated with it; any log request with + * loglevel less or equal to the verbose-level is logged.
+ * Verbose level 0 indicates no log. The log levels should be greater than 0. + *

+ * Parameter logname, if non-null, is used as log header (i.e. written + * at the begin of each log row). + */ +public class Log { + + /** ***************************** Attributes ****************************** */ + + /** (static) Default maximum log file size (4MB) */ + // public static final long MAX_SIZE=4096*1024; // 4MB + public static final long MAX_SIZE = 1024 * 1024; // 1MB + + /** The log output stream */ + PrintStream out_stream; + + /** The log tag at the beginning of each log row */ + String log_tag; + + /** + * The log_level. + *

+ * Only messages with a level less or equal this log_level are + * effectively logged + */ + int verbose_level; + + /** + * The maximum size of the log stream/file [bytes] Value -1 indicates no + * maximum size + */ + long max_size; + + /** The size of the log tag (e.g. "MyLog: " has tag_size=7) */ + int tag_size; + + /** Whether messages are logged */ + boolean do_log; + + /** The char counter of the already logged data */ + long counter; + + /** **************************** Constructors ***************************** */ + + /** + * Associates a new Log to the PrintStream outstream. Log size has + * no bound + */ + + public Log(PrintStream out_stream, String log_tag, int verbose_level) { + init(out_stream, log_tag, verbose_level, -1); + } + + /** + * Associates a new Log to the file filename. Log size is limited to + * the MAX_SIZE + */ + public Log(String file_name, String log_tag, int verbose_level) { + PrintStream os = null; + if (verbose_level > 0) { + try { + os = new PrintStream(new FileOutputStream(file_name)); + } catch (IOException e) { + e.printStackTrace(); + } + init(os, log_tag, verbose_level, MAX_SIZE); + } + } + + /** + * Associates a new Log to the file filename. Log size is limited to + * logsize [bytes] + */ + + public Log(String file_name, String log_tag, int verbose_level, + long max_size) { + PrintStream os = null; + if (verbose_level > 0) { + try { + os = new PrintStream(new FileOutputStream(file_name)); + } catch (IOException e) { + e.printStackTrace(); + } + init(os, log_tag, verbose_level, max_size); + } else { + init(null, log_tag, 0, 0); + do_log = false; + } + } + + /** + * Associates a new Log to the file filename. Log size is limited to + * logsize [bytes] + */ + + public Log(String file_name, String log_tag, int verbose_level, + long max_size, boolean append) { + PrintStream os = null; + if (verbose_level > 0) { + try { + os = new PrintStream(new FileOutputStream(file_name, append)); + } catch (IOException e) { + e.printStackTrace(); + } + init(os, log_tag, verbose_level, max_size); + } else { + init(null, log_tag, 0, 0); + do_log = false; + } + } + + /** ************************** Protected methods *************************** */ + + /** Initializes the log */ + protected void init(PrintStream out_stream, String log_tag, + int verbose_level, long max_size) { + this.out_stream = out_stream; + this.log_tag = log_tag; + this.verbose_level = verbose_level; + this.max_size = max_size; + + if (log_tag != null) + tag_size = log_tag.length() + 2; + else + tag_size = 0; + do_log = true; + counter = 0; + } + + /** Flushes */ + protected Log flush() { + if (verbose_level > 0) + out_stream.flush(); + return this; + } + + /** *************************** Public methods **************************** */ + + /** Closes the log */ + public void close() { + do_log = false; + out_stream.close(); + } + + /** Logs the Exception */ + public Log printException(Exception e, int level) { // ByteArrayOutputStream + // err=new + // ByteArrayOutputStream(); + // e.printStackTrace(new PrintStream(err)); + // return println("Exception: "+err.toString(),level); + return println("Exception: " + ExceptionPrinter.getStackTraceOf(e), + level); + } + + /** Logs the Exception.toString() and Exception.printStackTrace() */ + public Log printException(Exception e) { + return printException(e, 1); + } + + /** Logs the packet timestamp */ + public Log printPacketTimestamp(String proto, String remote_addr, + int remote_port, int len, String message, int level) { + String str = remote_addr + ":" + remote_port + "/" + proto + " (" + len + + " bytes)"; + if (message != null) + str += ": " + message; + println(DateFormat.formatHHMMSS(new Date()) + ", " + str, level); + return this; + } + + /** + * Prints the log if level isn't greater than the Log + * verbose_level + */ + public Log println(String message, int level) { + return print(message + "\r\n", level).flush(); + } + + /** Prints the log if the Log verbose_level is greater than 0 */ + public Log println(String message) { + return println(message, 1); + } + + /** Prints the log if the Log verbose_level is greater than 0 */ + public Log print(String message) { + return print(message, 1); + } + + /** + * Prints the log if level isn't greater than the Log + * verbose_level + */ + public Log print(String message, int level) { + if (do_log && level <= verbose_level) { + if (log_tag != null) + out_stream.print(log_tag + ": " + message); + else + out_stream.print(message); + + if (max_size >= 0) { + counter += tag_size + message.length(); + if (counter > max_size) { + out_stream + .println("\r\n----MAXIMUM LOG SIZE----\r\nSuccessive logs are lost."); + do_log = false; + } + } + } + return this; + } + +} diff --git a/src/org/zoolu/tools/LogLevel.java b/app/src/main/java/org/zoolu/tools/LogLevel.java similarity index 97% rename from src/org/zoolu/tools/LogLevel.java rename to app/src/main/java/org/zoolu/tools/LogLevel.java index f0bfd4c..9f27e80 100644 --- a/src/org/zoolu/tools/LogLevel.java +++ b/app/src/main/java/org/zoolu/tools/LogLevel.java @@ -1,38 +1,38 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * LogLevel provides a set of static log levels that can be used as default. - */ -public class LogLevel { - /** Default level for hight priority logs. */ - public static final int HIGH = 1; - /** Default level for medium priority logs. */ - public static final int MEDIUM = 3; - /** Default level for low priority logs. */ - public static final int LOW = 5; - /** Default level for very low priority logs. */ - public static final int LOWER = 9; -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * LogLevel provides a set of static log levels that can be used as default. + */ +public class LogLevel { + /** Default level for hight priority logs. */ + public static final int HIGH = 1; + /** Default level for medium priority logs. */ + public static final int MEDIUM = 3; + /** Default level for low priority logs. */ + public static final int LOW = 5; + /** Default level for very low priority logs. */ + public static final int LOWER = 9; +} diff --git a/src/org/zoolu/tools/MD5.java b/app/src/main/java/org/zoolu/tools/MD5.java similarity index 97% rename from src/org/zoolu/tools/MD5.java rename to app/src/main/java/org/zoolu/tools/MD5.java index 608cd3f..60f7b02 100644 --- a/src/org/zoolu/tools/MD5.java +++ b/app/src/main/java/org/zoolu/tools/MD5.java @@ -1,591 +1,591 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * MD5 hash algorithm. - *

- * Implements the RSA Data Security, Inc. MD5 Message-Digest Algorithm. This is - * almoust straight implementation of the reference implementation given in - * RFC1321 by RSA. - */ -public class MD5 extends MessageDigest { - - // ********************** Mangle functions ********************** - - /** Rotates w, shifting n bits left. */ - // private static int rotateLeft(int w, int n) { return (w << n) | (w >>> - // (32-n)); } - /** Rotates w, shifting n bits right. */ - // private static int rotateRight(int w, int n) { return (w >>> n) | (w << - // (32-n)); } - /** Rotates an array of words, shifting 1 word left. */ - /* - * private static int[] rotateLeft(int[] w) { int len=w.length; int - * w1=w[len-1]; for (int i=len-1; i>0; i--) w[i]=w[i-1]; w[0]=w1; return w; } - */ - - /** Rotates an array of words, shifting 1 word right. */ - /* - * private static int[] rotateRight(int[] w) { int len=w.length; int - * w0=w[0]; for (int i=1; isrc into array dst with offset - * offset - */ - // public static void copyBytes(byte[] src, byte[] dst, int offset) { for - // (int k=0; klen bytes of array src into array dst - * with offset offset - */ - // public static void copyBytes(byte[] src, byte[] dst, int offset, int len) - // { for (int k=0; k>24); b[2]=(byte)((n>>16)%256); b[1]=(byte)((n>>8)%256); - * b[0]=(byte)(n%256); return b; } - */ - - // ************************* Attributes ************************* - /** The digest */ - byte[] message_digest; - - /** byte counter mod 2^64 */ - long count; - - /** 128bit state (A,B,C, and D words) */ - int state[]; - - /** 64B (512 bits) chunk of the input message */ - byte block[]; - - /** Number of bytes remained into the chunk */ - int block_offset; - - /** Padding */ - static byte zeropadding[] = { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 }; - - /* Constants for MD5 transformation */ - /* - * static final int S11=7; static final int S12=12; static final int S13=17; - * static final int S14=22; static final int S21=5; static final int S22=9; - * static final int S23=14; static final int S24=20; static final int S31=4; - * static final int S32=11; static final int S33=16; static final int - * S34=23; static final int S41=6; static final int S42=10; static final int - * S43=15; static final int S44=21; - */ - - // *********************** Public methods *********************** - /** Constructor */ - public MD5() { - init(); - } - - /** Constructor */ - public MD5(byte[] buffer) { - init(); - update(buffer); - } - - /** Constructor */ - public MD5(byte[] buffer, int offset, int len) { - init(); - update(buffer, offset, len); - } - - /** Constructor */ - public MD5(String str) { - init(); - update(str); - } - - /** Inits the MD5 */ - private void init() { - count = 0; - // init the first block - block = new byte[64]; - block_offset = 0; - // load magic initialization constants - state = new int[4]; - state[0] = 0x67452301; - state[1] = 0xefcdab89; - state[2] = 0x98badcfe; - state[3] = 0x10325476; - - message_digest = null; - } - - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - /* - * public MD5 update(String str) { byte[] buf=str.getBytes(); return - * update(buf,0,buf.length); } - */ - - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - /* - * public MD5 update(byte[] buffer) { return update(buffer,0,buffer.length); } - */ - - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - public MessageDigest update(byte[] buffer, int offset, int len) { - if (message_digest != null) - return this; - // else - - count += len; - - // num of remaining bytes to be processed - int size = block.length - block_offset; - - while (len >= size) { // fill a block - for (int i = 0; i < size; i++) - block[block_offset + i] = buffer[offset + i]; - - // process the block - transform(state, block); - - // update offset, len, etc. - offset += size; - len -= size; - block_offset = 0; - size = block.length - block_offset; - } - // copy the remaining bytes - for (int i = 0; i < len; i++) - block[block_offset + i] = buffer[offset + i]; - block_offset += len; - - return this; - } - - /** Gets the MessageDigest. The same as doFinal(). */ - /* - * public byte[] getDigest() { return doFinal(); } - */ - - /** - * MessageDigest finalization. Ends a message-digest operation, writing the - * the message digest and zeroizing the context. - */ - public byte[] doFinal() { - if (message_digest != null) - return message_digest; - // else - - // num of padding zeros (node: block_offset is at most 64) - int npad = 64 - ((block_offset + 8) % 64); - - // set total bit length into the last 8 bytes - long len = count * 8; - byte[] len_field = new byte[8]; - for (int i = 0; i < 8; i++) { - len_field[i] = (byte) (len % 256); - len >>= 8; - } - - // process the last chunk, i.e. zero-pading(1-64B) + length-field(8B) (1 - // or 2 blocks) - update(zeropadding, 0, npad); - update(len_field, 0, 8); - - message_digest = new byte[16]; - // convert 4 words to 16 bytes - // for (int i=0; i<4; i++) { - // copyBytes(wordToBytes(state[i]),message_digest,i*4); } - int k = 0; - for (int i = 0; i < 4; i++) { - message_digest[k++] = (byte) ((state[i]) & 0xff); - message_digest[k++] = (byte) ((state[i] >>> 8) & 0xff); - message_digest[k++] = (byte) ((state[i] >>> 16) & 0xff); - message_digest[k++] = (byte) ((state[i] >>> 24) & 0xff); - } - return message_digest; - } - - /** MD5 basic transformation. Transforms state based on block. */ - private static void transform(int[] state, byte[] block) { - int a = state[0]; - int b = state[1]; - int c = state[2]; - int d = state[3]; - - int[] x = new int[16]; - x[0] = ((int) (block[0] & 0xff)) | (((int) (block[1] & 0xff)) << 8) - | (((int) (block[2] & 0xff)) << 16) - | (((int) (block[3])) << 24); - x[1] = ((int) (block[4] & 0xff)) | (((int) (block[5] & 0xff)) << 8) - | (((int) (block[6] & 0xff)) << 16) - | (((int) (block[7])) << 24); - x[2] = ((int) (block[8] & 0xff)) | (((int) (block[9] & 0xff)) << 8) - | (((int) (block[10] & 0xff)) << 16) - | (((int) (block[11])) << 24); - x[3] = ((int) (block[12] & 0xff)) | (((int) (block[13] & 0xff)) << 8) - | (((int) (block[14] & 0xff)) << 16) - | (((int) (block[15])) << 24); - x[4] = ((int) (block[16] & 0xff)) | (((int) (block[17] & 0xff)) << 8) - | (((int) (block[18] & 0xff)) << 16) - | (((int) (block[19])) << 24); - x[5] = ((int) (block[20] & 0xff)) | (((int) (block[21] & 0xff)) << 8) - | (((int) (block[22] & 0xff)) << 16) - | (((int) (block[23])) << 24); - x[6] = ((int) (block[24] & 0xff)) | (((int) (block[25] & 0xff)) << 8) - | (((int) (block[26] & 0xff)) << 16) - | (((int) (block[27])) << 24); - x[7] = ((int) (block[28] & 0xff)) | (((int) (block[29] & 0xff)) << 8) - | (((int) (block[30] & 0xff)) << 16) - | (((int) (block[31])) << 24); - x[8] = ((int) (block[32] & 0xff)) | (((int) (block[33] & 0xff)) << 8) - | (((int) (block[34] & 0xff)) << 16) - | (((int) (block[35])) << 24); - x[9] = ((int) (block[36] & 0xff)) | (((int) (block[37] & 0xff)) << 8) - | (((int) (block[38] & 0xff)) << 16) - | (((int) (block[39])) << 24); - x[10] = ((int) (block[40] & 0xff)) | (((int) (block[41] & 0xff)) << 8) - | (((int) (block[42] & 0xff)) << 16) - | (((int) (block[43])) << 24); - x[11] = ((int) (block[44] & 0xff)) | (((int) (block[45] & 0xff)) << 8) - | (((int) (block[46] & 0xff)) << 16) - | (((int) (block[47])) << 24); - x[12] = ((int) (block[48] & 0xff)) | (((int) (block[49] & 0xff)) << 8) - | (((int) (block[50] & 0xff)) << 16) - | (((int) (block[51])) << 24); - x[13] = ((int) (block[52] & 0xff)) | (((int) (block[53] & 0xff)) << 8) - | (((int) (block[54] & 0xff)) << 16) - | (((int) (block[55])) << 24); - x[14] = ((int) (block[56] & 0xff)) | (((int) (block[57] & 0xff)) << 8) - | (((int) (block[58] & 0xff)) << 16) - | (((int) (block[59])) << 24); - x[15] = ((int) (block[60] & 0xff)) | (((int) (block[61] & 0xff)) << 8) - | (((int) (block[62] & 0xff)) << 16) - | (((int) (block[63])) << 24); - - /* Round 1 */ - a += ((b & c) | (~b & d)) + x[0] + 0xd76aa478; - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[1] + 0xe8c7b756; - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[2] + 0x242070db; - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[3] + 0xc1bdceee; - b = ((b << 22) | (b >>> 10)) + c; - - a += ((b & c) | (~b & d)) + x[4] + 0xf57c0faf; - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[5] + 0x4787c62a; - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[6] + 0xa8304613; - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[7] + 0xfd469501; - b = ((b << 22) | (b >>> 10)) + c; - - a += ((b & c) | (~b & d)) + x[8] + 0x698098d8; - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[9] + 0x8b44f7af; - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; - b = ((b << 22) | (b >>> 10)) + c; - - a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; - a = ((a << 7) | (a >>> 25)) + b; - d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; - d = ((d << 12) | (d >>> 20)) + a; - c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; - c = ((c << 17) | (c >>> 15)) + d; - b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; - b = ((b << 22) | (b >>> 10)) + c; - - /* Round 2 */ - a += ((b & d) | (c & ~d)) + x[1] + 0xf61e2562; - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[6] + 0xc040b340; - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[0] + 0xe9b6c7aa; - b = ((b << 20) | (b >>> 12)) + c; - - a += ((b & d) | (c & ~d)) + x[5] + 0xd62f105d; - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[4] + 0xe7d3fbc8; - b = ((b << 20) | (b >>> 12)) + c; - - a += ((b & d) | (c & ~d)) + x[9] + 0x21e1cde6; - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[3] + 0xf4d50d87; - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[8] + 0x455a14ed; - b = ((b << 20) | (b >>> 12)) + c; - - a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; - a = ((a << 5) | (a >>> 27)) + b; - d += ((a & c) | (b & ~c)) + x[2] + 0xfcefa3f8; - d = ((d << 9) | (d >>> 23)) + a; - c += ((d & b) | (a & ~b)) + x[7] + 0x676f02d9; - c = ((c << 14) | (c >>> 18)) + d; - b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; - b = ((b << 20) | (b >>> 12)) + c; - - /* Round 3 */ - a += (b ^ c ^ d) + x[5] + 0xfffa3942; - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[8] + 0x8771f681; - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[11] + 0x6d9d6122; - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[14] + 0xfde5380c; - b = ((b << 23) | (b >>> 9)) + c; - - a += (b ^ c ^ d) + x[1] + 0xa4beea44; - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[4] + 0x4bdecfa9; - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[7] + 0xf6bb4b60; - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[10] + 0xbebfbc70; - b = ((b << 23) | (b >>> 9)) + c; - - a += (b ^ c ^ d) + x[13] + 0x289b7ec6; - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[0] + 0xeaa127fa; - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[3] + 0xd4ef3085; - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[6] + 0x04881d05; - b = ((b << 23) | (b >>> 9)) + c; - - a += (b ^ c ^ d) + x[9] + 0xd9d4d039; - a = ((a << 4) | (a >>> 28)) + b; - d += (a ^ b ^ c) + x[12] + 0xe6db99e5; - d = ((d << 11) | (d >>> 21)) + a; - c += (d ^ a ^ b) + x[15] + 0x1fa27cf8; - c = ((c << 16) | (c >>> 16)) + d; - b += (c ^ d ^ a) + x[2] + 0xc4ac5665; - b = ((b << 23) | (b >>> 9)) + c; - - /* Round 4 */ - a += (c ^ (b | ~d)) + x[0] + 0xf4292244; - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[7] + 0x432aff97; - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[14] + 0xab9423a7; - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[5] + 0xfc93a039; - b = ((b << 21) | (b >>> 11)) + c; - - a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[3] + 0x8f0ccc92; - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[10] + 0xffeff47d; - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[1] + 0x85845dd1; - b = ((b << 21) | (b >>> 11)) + c; - - a += (c ^ (b | ~d)) + x[8] + 0x6fa87e4f; - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[15] + 0xfe2ce6e0; - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[6] + 0xa3014314; - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; - b = ((b << 21) | (b >>> 11)) + c; - - a += (c ^ (b | ~d)) + x[4] + 0xf7537e82; - a = ((a << 6) | (a >>> 26)) + b; - d += (b ^ (a | ~c)) + x[11] + 0xbd3af235; - d = ((d << 10) | (d >>> 22)) + a; - c += (a ^ (d | ~b)) + x[2] + 0x2ad7d2bb; - c = ((c << 15) | (c >>> 17)) + d; - b += (d ^ (c | ~a)) + x[9] + 0xeb86d391; - b = ((b << 21) | (b >>> 11)) + c; - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - } - - /** F MD5 function. */ - // private static int F(int x, int y, int z) { return (x & y) | ((~x) & z); - // } - /** G MD5 function. */ - // private static int G(int x, int y, int z) { return (x & z) | (y & (~z)); - // } - /** H MD5 function. */ - // private static int H(int x, int y, int z) { return (x ^ y ^ z); } - /** I MD5 function. */ - // private static int I(int x, int y, int z) { return y ^ (x | (~z)); } - /** - * FF transformation for round 1. Rotation is separate from addition to - * prevent recomputation. - */ - /* - * private static int[] FF(int[] w, int x, int s, int t) { w[0] += - * F(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return - * rotateLeft(w); } - */ - /** - * GG transformation for round 2. Rotation is separate from addition to - * prevent recomputation. - */ - /* - * private static int[] GG(int[] w, int x, int s, int t) { w[0] += - * G(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return - * rotateLeft(w); }* /** HH transformation for round 3. Rotation is separate - * from addition to prevent recomputation. - */ - /* - * private static int[] HH(int[] w, int x, int s, int t) { w[0] += - * H(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return - * rotateLeft(w); }* /** II transformation for round 4. Rotation is separate - * from addition to prevent recomputation. - */ - /* - * private static int[] II(int[] w, int x, int s, int t) { w[0] += - * I(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return - * rotateLeft(w); } - */ - - /** MD5 basic transformation. Transforms state based on block. */ - /* - * private static void transform(int[] state, byte[] block) { int[] x=new - * int[16]; for (int i=0; i<16; i++) x[i]=(int)bytesToWord(block,(i*4)); // - * make a copy of the state int[] state_cp=new int[4]; for (int i=0; i<4; - * i++) state_cp[i]=state[i]; // Round 1 FF (state, x[ 0], S11, 0xd76aa478); - * FF (state, x[ 1], S12, 0xe8c7b756); FF (state, x[ 2], S13, 0x242070db); - * FF (state, x[ 3], S14, 0xc1bdceee); FF (state, x[ 4], S11, 0xf57c0faf); - * FF (state, x[ 5], S12, 0x4787c62a); FF (state, x[ 6], S13, 0xa8304613); - * FF (state, x[ 7], S14, 0xfd469501); FF (state, x[ 8], S11, 0x698098d8); - * FF (state, x[ 9], S12, 0x8b44f7af); FF (state, x[10], S13, 0xffff5bb1); - * FF (state, x[11], S14, 0x895cd7be); FF (state, x[12], S11, 0x6b901122); - * FF (state, x[13], S12, 0xfd987193); FF (state, x[14], S13, 0xa679438e); - * FF (state, x[15], S14, 0x49b40821); // Round 2 GG (state, x[ 1], S21, - * 0xf61e2562); GG (state, x[ 6], S22, 0xc040b340); GG (state, x[11], S23, - * 0x265e5a51); GG (state, x[ 0], S24, 0xe9b6c7aa); GG (state, x[ 5], S21, - * 0xd62f105d); GG (state, x[10], S22, 0x2441453); GG (state, x[15], S23, - * 0xd8a1e681); GG (state, x[ 4], S24, 0xe7d3fbc8); GG (state, x[ 9], S21, - * 0x21e1cde6); GG (state, x[14], S22, 0xc33707d6); GG (state, x[ 3], S23, - * 0xf4d50d87); GG (state, x[ 8], S24, 0x455a14ed); GG (state, x[13], S21, - * 0xa9e3e905); GG (state, x[ 2], S22, 0xfcefa3f8); GG (state, x[ 7], S23, - * 0x676f02d9); GG (state, x[12], S24, 0x8d2a4c8a); // Round 3 HH (state, x[ - * 5], S31, 0xfffa3942); HH (state, x[ 8], S32, 0x8771f681); HH (state, - * x[11], S33, 0x6d9d6122); HH (state, x[14], S34, 0xfde5380c); HH (state, - * x[ 1], S31, 0xa4beea44); HH (state, x[ 4], S32, 0x4bdecfa9); HH (state, - * x[ 7], S33, 0xf6bb4b60); HH (state, x[10], S34, 0xbebfbc70); HH (state, - * x[13], S31, 0x289b7ec6); HH (state, x[ 0], S32, 0xeaa127fa); HH (state, - * x[ 3], S33, 0xd4ef3085); HH (state, x[ 6], S34, 0x4881d05); HH (state, x[ - * 9], S31, 0xd9d4d039); HH (state, x[12], S32, 0xe6db99e5); HH (state, - * x[15], S33, 0x1fa27cf8); HH (state, x[ 2], S34, 0xc4ac5665); // Round 4 - * II (state, x[ 0], S41, 0xf4292244); II (state, x[ 7], S42, 0x432aff97); - * II (state, x[14], S43, 0xab9423a7); II (state, x[ 5], S44, 0xfc93a039); - * II (state, x[12], S41, 0x655b59c3); II (state, x[ 3], S42, 0x8f0ccc92); - * II (state, x[10], S43, 0xffeff47d); II (state, x[ 1], S44, 0x85845dd1); - * II (state, x[ 8], S41, 0x6fa87e4f); II (state, x[15], S42, 0xfe2ce6e0); - * II (state, x[ 6], S43, 0xa3014314); II (state, x[13], S44, 0x4e0811a1); - * II (state, x[ 4], S41, 0xf7537e82); II (state, x[11], S42, 0xbd3af235); - * II (state, x[ 2], S43, 0x2ad7d2bb); II (state, x[ 9], S44, 0xeb86d391); - * - * for (int i=0; i<4; i++) state[i]+=state_cp[i]; } - */ - - /** Calculates the MD5. */ - public static byte[] digest(byte[] buffer, int offset, int len) { - MD5 md5 = new MD5(buffer, offset, len); - return md5.doFinal(); - } - - /** Calculates the MD5. */ - public static byte[] digest(byte[] buffer) { - return digest(buffer, 0, buffer.length); - } - - /** Calculates the MD5. */ - public static byte[] digest(String str) { - MD5 md5 = new MD5(str); - return md5.doFinal(); - } - - /** Gets the message-digest as string of hex values */ - /* - * public String asHex() { return asHex(doFinal()); } - */ - - /** Transforms an array of bytes into a string of hex values */ - /* - * public static String asHex(byte[] buf) { String str=new String(); for - * (int i=0; i>>4)&0x0F); - * str+=Integer.toHexString(buf[i]&0x0F); } return str; } - */ - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * MD5 hash algorithm. + *

+ * Implements the RSA Data Security, Inc. MD5 Message-Digest Algorithm. This is + * almoust straight implementation of the reference implementation given in + * RFC1321 by RSA. + */ +public class MD5 extends MessageDigest { + + // ********************** Mangle functions ********************** + + /** Rotates w, shifting n bits left. */ + // private static int rotateLeft(int w, int n) { return (w << n) | (w >>> + // (32-n)); } + /** Rotates w, shifting n bits right. */ + // private static int rotateRight(int w, int n) { return (w >>> n) | (w << + // (32-n)); } + /** Rotates an array of words, shifting 1 word left. */ + /* + * private static int[] rotateLeft(int[] w) { int len=w.length; int + * w1=w[len-1]; for (int i=len-1; i>0; i--) w[i]=w[i-1]; w[0]=w1; return w; } + */ + + /** Rotates an array of words, shifting 1 word right. */ + /* + * private static int[] rotateRight(int[] w) { int len=w.length; int + * w0=w[0]; for (int i=1; isrc into array dst with offset + * offset + */ + // public static void copyBytes(byte[] src, byte[] dst, int offset) { for + // (int k=0; klen bytes of array src into array dst + * with offset offset + */ + // public static void copyBytes(byte[] src, byte[] dst, int offset, int len) + // { for (int k=0; k>24); b[2]=(byte)((n>>16)%256); b[1]=(byte)((n>>8)%256); + * b[0]=(byte)(n%256); return b; } + */ + + // ************************* Attributes ************************* + /** The digest */ + byte[] message_digest; + + /** byte counter mod 2^64 */ + long count; + + /** 128bit state (A,B,C, and D words) */ + int state[]; + + /** 64B (512 bits) chunk of the input message */ + byte block[]; + + /** Number of bytes remained into the chunk */ + int block_offset; + + /** Padding */ + static byte zeropadding[] = { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 }; + + /* Constants for MD5 transformation */ + /* + * static final int S11=7; static final int S12=12; static final int S13=17; + * static final int S14=22; static final int S21=5; static final int S22=9; + * static final int S23=14; static final int S24=20; static final int S31=4; + * static final int S32=11; static final int S33=16; static final int + * S34=23; static final int S41=6; static final int S42=10; static final int + * S43=15; static final int S44=21; + */ + + // *********************** Public methods *********************** + /** Constructor */ + public MD5() { + init(); + } + + /** Constructor */ + public MD5(byte[] buffer) { + init(); + update(buffer); + } + + /** Constructor */ + public MD5(byte[] buffer, int offset, int len) { + init(); + update(buffer, offset, len); + } + + /** Constructor */ + public MD5(String str) { + init(); + update(str); + } + + /** Inits the MD5 */ + private void init() { + count = 0; + // init the first block + block = new byte[64]; + block_offset = 0; + // load magic initialization constants + state = new int[4]; + state[0] = 0x67452301; + state[1] = 0xefcdab89; + state[2] = 0x98badcfe; + state[3] = 0x10325476; + + message_digest = null; + } + + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + /* + * public MD5 update(String str) { byte[] buf=str.getBytes(); return + * update(buf,0,buf.length); } + */ + + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + /* + * public MD5 update(byte[] buffer) { return update(buffer,0,buffer.length); } + */ + + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + public MessageDigest update(byte[] buffer, int offset, int len) { + if (message_digest != null) + return this; + // else + + count += len; + + // num of remaining bytes to be processed + int size = block.length - block_offset; + + while (len >= size) { // fill a block + for (int i = 0; i < size; i++) + block[block_offset + i] = buffer[offset + i]; + + // process the block + transform(state, block); + + // update offset, len, etc. + offset += size; + len -= size; + block_offset = 0; + size = block.length - block_offset; + } + // copy the remaining bytes + for (int i = 0; i < len; i++) + block[block_offset + i] = buffer[offset + i]; + block_offset += len; + + return this; + } + + /** Gets the MessageDigest. The same as doFinal(). */ + /* + * public byte[] getDigest() { return doFinal(); } + */ + + /** + * MessageDigest finalization. Ends a message-digest operation, writing the + * the message digest and zeroizing the context. + */ + public byte[] doFinal() { + if (message_digest != null) + return message_digest; + // else + + // num of padding zeros (node: block_offset is at most 64) + int npad = 64 - ((block_offset + 8) % 64); + + // set total bit length into the last 8 bytes + long len = count * 8; + byte[] len_field = new byte[8]; + for (int i = 0; i < 8; i++) { + len_field[i] = (byte) (len % 256); + len >>= 8; + } + + // process the last chunk, i.e. zero-pading(1-64B) + length-field(8B) (1 + // or 2 blocks) + update(zeropadding, 0, npad); + update(len_field, 0, 8); + + message_digest = new byte[16]; + // convert 4 words to 16 bytes + // for (int i=0; i<4; i++) { + // copyBytes(wordToBytes(state[i]),message_digest,i*4); } + int k = 0; + for (int i = 0; i < 4; i++) { + message_digest[k++] = (byte) ((state[i]) & 0xff); + message_digest[k++] = (byte) ((state[i] >>> 8) & 0xff); + message_digest[k++] = (byte) ((state[i] >>> 16) & 0xff); + message_digest[k++] = (byte) ((state[i] >>> 24) & 0xff); + } + return message_digest; + } + + /** MD5 basic transformation. Transforms state based on block. */ + private static void transform(int[] state, byte[] block) { + int a = state[0]; + int b = state[1]; + int c = state[2]; + int d = state[3]; + + int[] x = new int[16]; + x[0] = ((int) (block[0] & 0xff)) | (((int) (block[1] & 0xff)) << 8) + | (((int) (block[2] & 0xff)) << 16) + | (((int) (block[3])) << 24); + x[1] = ((int) (block[4] & 0xff)) | (((int) (block[5] & 0xff)) << 8) + | (((int) (block[6] & 0xff)) << 16) + | (((int) (block[7])) << 24); + x[2] = ((int) (block[8] & 0xff)) | (((int) (block[9] & 0xff)) << 8) + | (((int) (block[10] & 0xff)) << 16) + | (((int) (block[11])) << 24); + x[3] = ((int) (block[12] & 0xff)) | (((int) (block[13] & 0xff)) << 8) + | (((int) (block[14] & 0xff)) << 16) + | (((int) (block[15])) << 24); + x[4] = ((int) (block[16] & 0xff)) | (((int) (block[17] & 0xff)) << 8) + | (((int) (block[18] & 0xff)) << 16) + | (((int) (block[19])) << 24); + x[5] = ((int) (block[20] & 0xff)) | (((int) (block[21] & 0xff)) << 8) + | (((int) (block[22] & 0xff)) << 16) + | (((int) (block[23])) << 24); + x[6] = ((int) (block[24] & 0xff)) | (((int) (block[25] & 0xff)) << 8) + | (((int) (block[26] & 0xff)) << 16) + | (((int) (block[27])) << 24); + x[7] = ((int) (block[28] & 0xff)) | (((int) (block[29] & 0xff)) << 8) + | (((int) (block[30] & 0xff)) << 16) + | (((int) (block[31])) << 24); + x[8] = ((int) (block[32] & 0xff)) | (((int) (block[33] & 0xff)) << 8) + | (((int) (block[34] & 0xff)) << 16) + | (((int) (block[35])) << 24); + x[9] = ((int) (block[36] & 0xff)) | (((int) (block[37] & 0xff)) << 8) + | (((int) (block[38] & 0xff)) << 16) + | (((int) (block[39])) << 24); + x[10] = ((int) (block[40] & 0xff)) | (((int) (block[41] & 0xff)) << 8) + | (((int) (block[42] & 0xff)) << 16) + | (((int) (block[43])) << 24); + x[11] = ((int) (block[44] & 0xff)) | (((int) (block[45] & 0xff)) << 8) + | (((int) (block[46] & 0xff)) << 16) + | (((int) (block[47])) << 24); + x[12] = ((int) (block[48] & 0xff)) | (((int) (block[49] & 0xff)) << 8) + | (((int) (block[50] & 0xff)) << 16) + | (((int) (block[51])) << 24); + x[13] = ((int) (block[52] & 0xff)) | (((int) (block[53] & 0xff)) << 8) + | (((int) (block[54] & 0xff)) << 16) + | (((int) (block[55])) << 24); + x[14] = ((int) (block[56] & 0xff)) | (((int) (block[57] & 0xff)) << 8) + | (((int) (block[58] & 0xff)) << 16) + | (((int) (block[59])) << 24); + x[15] = ((int) (block[60] & 0xff)) | (((int) (block[61] & 0xff)) << 8) + | (((int) (block[62] & 0xff)) << 16) + | (((int) (block[63])) << 24); + + /* Round 1 */ + a += ((b & c) | (~b & d)) + x[0] + 0xd76aa478; + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[1] + 0xe8c7b756; + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[2] + 0x242070db; + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[3] + 0xc1bdceee; + b = ((b << 22) | (b >>> 10)) + c; + + a += ((b & c) | (~b & d)) + x[4] + 0xf57c0faf; + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[5] + 0x4787c62a; + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[6] + 0xa8304613; + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[7] + 0xfd469501; + b = ((b << 22) | (b >>> 10)) + c; + + a += ((b & c) | (~b & d)) + x[8] + 0x698098d8; + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[9] + 0x8b44f7af; + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; + b = ((b << 22) | (b >>> 10)) + c; + + a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; + a = ((a << 7) | (a >>> 25)) + b; + d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; + d = ((d << 12) | (d >>> 20)) + a; + c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; + c = ((c << 17) | (c >>> 15)) + d; + b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; + b = ((b << 22) | (b >>> 10)) + c; + + /* Round 2 */ + a += ((b & d) | (c & ~d)) + x[1] + 0xf61e2562; + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[6] + 0xc040b340; + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[0] + 0xe9b6c7aa; + b = ((b << 20) | (b >>> 12)) + c; + + a += ((b & d) | (c & ~d)) + x[5] + 0xd62f105d; + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[4] + 0xe7d3fbc8; + b = ((b << 20) | (b >>> 12)) + c; + + a += ((b & d) | (c & ~d)) + x[9] + 0x21e1cde6; + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[3] + 0xf4d50d87; + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[8] + 0x455a14ed; + b = ((b << 20) | (b >>> 12)) + c; + + a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; + a = ((a << 5) | (a >>> 27)) + b; + d += ((a & c) | (b & ~c)) + x[2] + 0xfcefa3f8; + d = ((d << 9) | (d >>> 23)) + a; + c += ((d & b) | (a & ~b)) + x[7] + 0x676f02d9; + c = ((c << 14) | (c >>> 18)) + d; + b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; + b = ((b << 20) | (b >>> 12)) + c; + + /* Round 3 */ + a += (b ^ c ^ d) + x[5] + 0xfffa3942; + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[8] + 0x8771f681; + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[11] + 0x6d9d6122; + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[14] + 0xfde5380c; + b = ((b << 23) | (b >>> 9)) + c; + + a += (b ^ c ^ d) + x[1] + 0xa4beea44; + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[4] + 0x4bdecfa9; + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[7] + 0xf6bb4b60; + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[10] + 0xbebfbc70; + b = ((b << 23) | (b >>> 9)) + c; + + a += (b ^ c ^ d) + x[13] + 0x289b7ec6; + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[0] + 0xeaa127fa; + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[3] + 0xd4ef3085; + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[6] + 0x04881d05; + b = ((b << 23) | (b >>> 9)) + c; + + a += (b ^ c ^ d) + x[9] + 0xd9d4d039; + a = ((a << 4) | (a >>> 28)) + b; + d += (a ^ b ^ c) + x[12] + 0xe6db99e5; + d = ((d << 11) | (d >>> 21)) + a; + c += (d ^ a ^ b) + x[15] + 0x1fa27cf8; + c = ((c << 16) | (c >>> 16)) + d; + b += (c ^ d ^ a) + x[2] + 0xc4ac5665; + b = ((b << 23) | (b >>> 9)) + c; + + /* Round 4 */ + a += (c ^ (b | ~d)) + x[0] + 0xf4292244; + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[7] + 0x432aff97; + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[14] + 0xab9423a7; + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[5] + 0xfc93a039; + b = ((b << 21) | (b >>> 11)) + c; + + a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[3] + 0x8f0ccc92; + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[10] + 0xffeff47d; + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[1] + 0x85845dd1; + b = ((b << 21) | (b >>> 11)) + c; + + a += (c ^ (b | ~d)) + x[8] + 0x6fa87e4f; + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[15] + 0xfe2ce6e0; + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[6] + 0xa3014314; + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; + b = ((b << 21) | (b >>> 11)) + c; + + a += (c ^ (b | ~d)) + x[4] + 0xf7537e82; + a = ((a << 6) | (a >>> 26)) + b; + d += (b ^ (a | ~c)) + x[11] + 0xbd3af235; + d = ((d << 10) | (d >>> 22)) + a; + c += (a ^ (d | ~b)) + x[2] + 0x2ad7d2bb; + c = ((c << 15) | (c >>> 17)) + d; + b += (d ^ (c | ~a)) + x[9] + 0xeb86d391; + b = ((b << 21) | (b >>> 11)) + c; + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + /** F MD5 function. */ + // private static int F(int x, int y, int z) { return (x & y) | ((~x) & z); + // } + /** G MD5 function. */ + // private static int G(int x, int y, int z) { return (x & z) | (y & (~z)); + // } + /** H MD5 function. */ + // private static int H(int x, int y, int z) { return (x ^ y ^ z); } + /** I MD5 function. */ + // private static int I(int x, int y, int z) { return y ^ (x | (~z)); } + /** + * FF transformation for round 1. Rotation is separate from addition to + * prevent recomputation. + */ + /* + * private static int[] FF(int[] w, int x, int s, int t) { w[0] += + * F(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return + * rotateLeft(w); } + */ + /** + * GG transformation for round 2. Rotation is separate from addition to + * prevent recomputation. + */ + /* + * private static int[] GG(int[] w, int x, int s, int t) { w[0] += + * G(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return + * rotateLeft(w); }* /** HH transformation for round 3. Rotation is separate + * from addition to prevent recomputation. + */ + /* + * private static int[] HH(int[] w, int x, int s, int t) { w[0] += + * H(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return + * rotateLeft(w); }* /** II transformation for round 4. Rotation is separate + * from addition to prevent recomputation. + */ + /* + * private static int[] II(int[] w, int x, int s, int t) { w[0] += + * I(w[1],w[2],w[3]) + x + t; w[0] = rotateLeft(w[0],s) + w[1]; return + * rotateLeft(w); } + */ + + /** MD5 basic transformation. Transforms state based on block. */ + /* + * private static void transform(int[] state, byte[] block) { int[] x=new + * int[16]; for (int i=0; i<16; i++) x[i]=(int)bytesToWord(block,(i*4)); // + * make a copy of the state int[] state_cp=new int[4]; for (int i=0; i<4; + * i++) state_cp[i]=state[i]; // Round 1 FF (state, x[ 0], S11, 0xd76aa478); + * FF (state, x[ 1], S12, 0xe8c7b756); FF (state, x[ 2], S13, 0x242070db); + * FF (state, x[ 3], S14, 0xc1bdceee); FF (state, x[ 4], S11, 0xf57c0faf); + * FF (state, x[ 5], S12, 0x4787c62a); FF (state, x[ 6], S13, 0xa8304613); + * FF (state, x[ 7], S14, 0xfd469501); FF (state, x[ 8], S11, 0x698098d8); + * FF (state, x[ 9], S12, 0x8b44f7af); FF (state, x[10], S13, 0xffff5bb1); + * FF (state, x[11], S14, 0x895cd7be); FF (state, x[12], S11, 0x6b901122); + * FF (state, x[13], S12, 0xfd987193); FF (state, x[14], S13, 0xa679438e); + * FF (state, x[15], S14, 0x49b40821); // Round 2 GG (state, x[ 1], S21, + * 0xf61e2562); GG (state, x[ 6], S22, 0xc040b340); GG (state, x[11], S23, + * 0x265e5a51); GG (state, x[ 0], S24, 0xe9b6c7aa); GG (state, x[ 5], S21, + * 0xd62f105d); GG (state, x[10], S22, 0x2441453); GG (state, x[15], S23, + * 0xd8a1e681); GG (state, x[ 4], S24, 0xe7d3fbc8); GG (state, x[ 9], S21, + * 0x21e1cde6); GG (state, x[14], S22, 0xc33707d6); GG (state, x[ 3], S23, + * 0xf4d50d87); GG (state, x[ 8], S24, 0x455a14ed); GG (state, x[13], S21, + * 0xa9e3e905); GG (state, x[ 2], S22, 0xfcefa3f8); GG (state, x[ 7], S23, + * 0x676f02d9); GG (state, x[12], S24, 0x8d2a4c8a); // Round 3 HH (state, x[ + * 5], S31, 0xfffa3942); HH (state, x[ 8], S32, 0x8771f681); HH (state, + * x[11], S33, 0x6d9d6122); HH (state, x[14], S34, 0xfde5380c); HH (state, + * x[ 1], S31, 0xa4beea44); HH (state, x[ 4], S32, 0x4bdecfa9); HH (state, + * x[ 7], S33, 0xf6bb4b60); HH (state, x[10], S34, 0xbebfbc70); HH (state, + * x[13], S31, 0x289b7ec6); HH (state, x[ 0], S32, 0xeaa127fa); HH (state, + * x[ 3], S33, 0xd4ef3085); HH (state, x[ 6], S34, 0x4881d05); HH (state, x[ + * 9], S31, 0xd9d4d039); HH (state, x[12], S32, 0xe6db99e5); HH (state, + * x[15], S33, 0x1fa27cf8); HH (state, x[ 2], S34, 0xc4ac5665); // Round 4 + * II (state, x[ 0], S41, 0xf4292244); II (state, x[ 7], S42, 0x432aff97); + * II (state, x[14], S43, 0xab9423a7); II (state, x[ 5], S44, 0xfc93a039); + * II (state, x[12], S41, 0x655b59c3); II (state, x[ 3], S42, 0x8f0ccc92); + * II (state, x[10], S43, 0xffeff47d); II (state, x[ 1], S44, 0x85845dd1); + * II (state, x[ 8], S41, 0x6fa87e4f); II (state, x[15], S42, 0xfe2ce6e0); + * II (state, x[ 6], S43, 0xa3014314); II (state, x[13], S44, 0x4e0811a1); + * II (state, x[ 4], S41, 0xf7537e82); II (state, x[11], S42, 0xbd3af235); + * II (state, x[ 2], S43, 0x2ad7d2bb); II (state, x[ 9], S44, 0xeb86d391); + * + * for (int i=0; i<4; i++) state[i]+=state_cp[i]; } + */ + + /** Calculates the MD5. */ + public static byte[] digest(byte[] buffer, int offset, int len) { + MD5 md5 = new MD5(buffer, offset, len); + return md5.doFinal(); + } + + /** Calculates the MD5. */ + public static byte[] digest(byte[] buffer) { + return digest(buffer, 0, buffer.length); + } + + /** Calculates the MD5. */ + public static byte[] digest(String str) { + MD5 md5 = new MD5(str); + return md5.doFinal(); + } + + /** Gets the message-digest as string of hex values */ + /* + * public String asHex() { return asHex(doFinal()); } + */ + + /** Transforms an array of bytes into a string of hex values */ + /* + * public static String asHex(byte[] buf) { String str=new String(); for + * (int i=0; i>>4)&0x0F); + * str+=Integer.toHexString(buf[i]&0x0F); } return str; } + */ + +} diff --git a/src/org/zoolu/tools/MD5OTP.java b/app/src/main/java/org/zoolu/tools/MD5OTP.java similarity index 96% rename from src/org/zoolu/tools/MD5OTP.java rename to app/src/main/java/org/zoolu/tools/MD5OTP.java index fb5a065..7688ce1 100644 --- a/src/org/zoolu/tools/MD5OTP.java +++ b/app/src/main/java/org/zoolu/tools/MD5OTP.java @@ -1,199 +1,199 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.io.*; -import java.util.Random; - -/** - * OTP (One Time Pad) encryption algorithm based on MD5 hash function. It uses a - * PRG (Pseudo-Random-Generator) function in OFB (Output Feadback) to genarate a - * byte-stream (the OTP) that is XORed with the plaintext.
- * The PRG is based on MD5. - * - *

- * The OTP is calculated starting from a key and an IV, as follows:
- * h_0=hash(skey|iv)
- * h_i=hash(skey|h_i-1) - * - *

- * where:
- * hash(.)==MD5(.)
- * skey==key - * - *

- * while the ciphertext is calculated as follows:
- * c_0=iv
- * c_i=m_i XOR h_i with i=1,2,.. - * - *

- * Note that optionally it could modified initializing the skey as follows:
- * skey==hash(key|iv)
- * in order to not keep in memory the secret key for long time - */ -public class MD5OTP { - /** Block size in bytes */ - static int size; - /** the OTP stream key */ - byte[] skey; - /** pseudorandom-stream (OTP) block */ - byte[] h; - /** index within a single block */ - int index; - - /** Creates a new MD5OTP */ - /* - * public MD5OTP(int bsize, byte[] key, byte[] iv) { init(bsize,key,iv); } - */ - - /** Creates a new MD5OTP */ - public MD5OTP(byte[] skey, byte[] iv) { - init(16, skey, iv); - } - - /** Creates a new MD5OTP with IV=0 */ - public MD5OTP(byte[] skey) { - init(16, skey, null); - } - - /** Inits the MD5OTP algorithm */ - private void init(int size, byte[] skey, byte[] iv) { - MD5OTP.size = size; - if (iv == null) { - iv = new byte[size]; - for (int i = 0; i < size; i++) - iv[i] = 0; - } - this.skey = skey; - // skey=hash(cat(key,iv)); - h = clone(iv); - index = size - 1; - } - - /** Makes a clone of a byte array */ - private static byte[] clone(byte[] bb) { - byte[] cc = new byte[bb.length]; - for (int i = 0; i < bb.length; i++) - cc[i] = bb[i]; - return cc; - } - - /** Concatenates two byte arrays */ - private static byte[] cat(byte[] aa, byte[] bb) { - byte[] cc = new byte[aa.length + bb.length]; - for (int i = 0; i < aa.length; i++) - cc[i] = aa[i]; - for (int i = 0; i < bb.length; i++) - cc[i + aa.length] = bb[i]; - return cc; - } - - /** Returns a sub array */ - private static byte[] sub(byte[] bb, int begin, int end) { - byte[] cc = new byte[end - begin]; - for (int i = begin; i < end; i++) - cc[i - begin] = bb[i]; - return cc; - } - - /** Return an hash of the byte array */ - private static byte[] hash(byte[] bb) { - return MD5.digest(bb); - } - - /** Encrypts a block of bytes */ - public byte[] update(byte[] m) { - byte[] c = new byte[m.length]; - for (int i = 0; i < m.length; i++) { - index++; - if (index == size) { // calculate new h_block - h = hash(cat(skey, h)); - index = 0; - } - c[i] = (byte) (m[i] ^ h[index]); - } - return c; - } - - /** Encrypts a byte stream */ - public void update(InputStream in, OutputStream out) { - byte[] buff = new byte[2048]; - int len; - try { - while ((len = in.read(buff)) > 0) - out.write(update(sub(buff, 0, len))); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** Encrypts an array of bytes. An IV is chosen and saved at top. */ - public static byte[] encrypt(byte[] m, byte[] key) { // choose a random - // IV - byte[] iv = MD5.digest(Long.toString((new Random()).nextLong())); - // do encryption - byte[] c = (new MD5OTP(key, iv)).update(m); - return cat(iv, c); - } - - /** Decrypts an array of bytes with the IV at top. */ - public static byte[] decrypt(byte[] c, byte[] key) { // read the IV - byte[] iv = sub(c, 0, 16); - byte[] buf = sub(c, 16, c.length); - return (new MD5OTP(key, iv)).update(buf); - } - - /** Returns an hex representation of the byte array */ - private static String asHex(byte[] bb) { - StringBuffer buf = new StringBuffer(bb.length * 2); - for (int i = 0; i < bb.length; i++) { - if (((int) bb[i] & 0xff) < 0x10) - buf.append("0"); - // buf.append(Long.toString((int)bb[i] & 0xff, 16)); - buf.append(Integer.toHexString((int) bb[i] & 0xff)); - } - return buf.toString(); - } - - public static void main(String[] args) { - if (args.length < 2) { - System.out - .println("Usage:\n\n java MD5OTP []"); - System.exit(0); - } - - byte[] msg = args[0].getBytes(); - byte[] key = args[1].getBytes(); - byte[] iv = null; - if (args.length > 2) - iv = args[2].getBytes(); - - System.out.println("m= " + asHex(msg) + " (" + new String(msg) + ")"); - byte[] cip = (new MD5OTP(key, iv)).update(msg); - System.out.println("c= " + asHex(cip)); - cip = (new MD5OTP(key, iv)).update(cip); - System.out.println("m= " + asHex(cip) + " (" + new String(cip) + ")"); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.io.*; +import java.util.Random; + +/** + * OTP (One Time Pad) encryption algorithm based on MD5 hash function. It uses a + * PRG (Pseudo-Random-Generator) function in OFB (Output Feadback) to genarate a + * byte-stream (the OTP) that is XORed with the plaintext.
+ * The PRG is based on MD5. + * + *

+ * The OTP is calculated starting from a key and an IV, as follows:
+ * h_0=hash(skey|iv)
+ * h_i=hash(skey|h_i-1) + * + *

+ * where:
+ * hash(.)==MD5(.)
+ * skey==key + * + *

+ * while the ciphertext is calculated as follows:
+ * c_0=iv
+ * c_i=m_i XOR h_i with i=1,2,.. + * + *

+ * Note that optionally it could modified initializing the skey as follows:
+ * skey==hash(key|iv)
+ * in order to not keep in memory the secret key for long time + */ +public class MD5OTP { + /** Block size in bytes */ + static int size; + /** the OTP stream key */ + byte[] skey; + /** pseudorandom-stream (OTP) block */ + byte[] h; + /** index within a single block */ + int index; + + /** Creates a new MD5OTP */ + /* + * public MD5OTP(int bsize, byte[] key, byte[] iv) { init(bsize,key,iv); } + */ + + /** Creates a new MD5OTP */ + public MD5OTP(byte[] skey, byte[] iv) { + init(16, skey, iv); + } + + /** Creates a new MD5OTP with IV=0 */ + public MD5OTP(byte[] skey) { + init(16, skey, null); + } + + /** Inits the MD5OTP algorithm */ + private void init(int size, byte[] skey, byte[] iv) { + MD5OTP.size = size; + if (iv == null) { + iv = new byte[size]; + for (int i = 0; i < size; i++) + iv[i] = 0; + } + this.skey = skey; + // skey=hash(cat(key,iv)); + h = clone(iv); + index = size - 1; + } + + /** Makes a clone of a byte array */ + private static byte[] clone(byte[] bb) { + byte[] cc = new byte[bb.length]; + for (int i = 0; i < bb.length; i++) + cc[i] = bb[i]; + return cc; + } + + /** Concatenates two byte arrays */ + private static byte[] cat(byte[] aa, byte[] bb) { + byte[] cc = new byte[aa.length + bb.length]; + for (int i = 0; i < aa.length; i++) + cc[i] = aa[i]; + for (int i = 0; i < bb.length; i++) + cc[i + aa.length] = bb[i]; + return cc; + } + + /** Returns a sub array */ + private static byte[] sub(byte[] bb, int begin, int end) { + byte[] cc = new byte[end - begin]; + for (int i = begin; i < end; i++) + cc[i - begin] = bb[i]; + return cc; + } + + /** Return an hash of the byte array */ + private static byte[] hash(byte[] bb) { + return MD5.digest(bb); + } + + /** Encrypts a block of bytes */ + public byte[] update(byte[] m) { + byte[] c = new byte[m.length]; + for (int i = 0; i < m.length; i++) { + index++; + if (index == size) { // calculate new h_block + h = hash(cat(skey, h)); + index = 0; + } + c[i] = (byte) (m[i] ^ h[index]); + } + return c; + } + + /** Encrypts a byte stream */ + public void update(InputStream in, OutputStream out) { + byte[] buff = new byte[2048]; + int len; + try { + while ((len = in.read(buff)) > 0) + out.write(update(sub(buff, 0, len))); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** Encrypts an array of bytes. An IV is chosen and saved at top. */ + public static byte[] encrypt(byte[] m, byte[] key) { // choose a random + // IV + byte[] iv = MD5.digest(Long.toString((new Random()).nextLong())); + // do encryption + byte[] c = (new MD5OTP(key, iv)).update(m); + return cat(iv, c); + } + + /** Decrypts an array of bytes with the IV at top. */ + public static byte[] decrypt(byte[] c, byte[] key) { // read the IV + byte[] iv = sub(c, 0, 16); + byte[] buf = sub(c, 16, c.length); + return (new MD5OTP(key, iv)).update(buf); + } + + /** Returns an hex representation of the byte array */ + private static String asHex(byte[] bb) { + StringBuffer buf = new StringBuffer(bb.length * 2); + for (int i = 0; i < bb.length; i++) { + if (((int) bb[i] & 0xff) < 0x10) + buf.append("0"); + // buf.append(Long.toString((int)bb[i] & 0xff, 16)); + buf.append(Integer.toHexString((int) bb[i] & 0xff)); + } + return buf.toString(); + } + + public static void main(String[] args) { + if (args.length < 2) { + System.out + .println("Usage:\n\n java MD5OTP []"); + System.exit(0); + } + + byte[] msg = args[0].getBytes(); + byte[] key = args[1].getBytes(); + byte[] iv = null; + if (args.length > 2) + iv = args[2].getBytes(); + + System.out.println("m= " + asHex(msg) + " (" + new String(msg) + ")"); + byte[] cip = (new MD5OTP(key, iv)).update(msg); + System.out.println("c= " + asHex(cip)); + cip = (new MD5OTP(key, iv)).update(cip); + System.out.println("m= " + asHex(cip) + " (" + new String(cip) + ")"); + } + +} diff --git a/src/org/zoolu/tools/Mangle.java b/app/src/main/java/org/zoolu/tools/Mangle.java similarity index 96% rename from src/org/zoolu/tools/Mangle.java rename to app/src/main/java/org/zoolu/tools/Mangle.java index 4a77ac0..1d857d3 100644 --- a/src/org/zoolu/tools/Mangle.java +++ b/app/src/main/java/org/zoolu/tools/Mangle.java @@ -1,334 +1,334 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Mangle collects some static methods for mangling binary-data structures - */ -public class Mangle { - - /** Compares two arrays of bytes */ - public static boolean compare(byte[] a, byte[] b) { - if (a.length != b.length) - return false; - for (int i = 0; i < a.length; i++) - if (a[i] != b[i]) - return false; - return true; - } - - /** Initalizes a byte array with value value */ - public static byte[] initBytes(byte[] b, int value) { - for (int i = 0; i < b.length; i++) - b[i] = (byte) value; - return b; - } - - /** Gets the unsigned representatin of a byte (into a short) */ - public static short uByte(byte b) { - return (short) (((short) b + 256) % 256); - } - - /** Gets the unsigned representatin of a 32-bit word (into a long) */ - public static long uWord(int n) { - long wmask = 0x10000; - wmask *= wmask; - return (long) (((long) n + wmask) % wmask); - } - - /** Rotates w left n bits. */ - private static int rotateLeft(int w, int n) { - return (w << n) | (w >>> (32 - n)); - } - - /** Rotates w right n bits. */ - private static int rotateRight(int w, int n) { - return (w >>> n) | (w << (32 - n)); - } - - /** Rotates an array of int (words), shifting 1 word left. */ - private static int[] rotateLeft(int[] w) { - int len = w.length; - int w1 = w[len - 1]; - for (int i = len - 1; i > 1; i--) - w[i] = w[i - 1]; - w[0] = w1; - return w; - } - - /** Rotates an array of int (words), shifting 1 word right. */ - private static int[] rotateRight(int[] w) { - int len = w.length; - int w0 = w[0]; - for (int i = 1; i < len; i++) - w[i - 1] = w[i]; - w[len - 1] = w0; - return w; - } - - /** Rotates an array of bytes, shifting 1 byte left. */ - private static byte[] rotateLeft(byte[] b) { - int len = b.length; - byte b1 = b[len - 1]; - for (int i = len - 1; i > 1; i--) - b[i] = b[i - 1]; - b[0] = b1; - return b; - } - - /** Rotates an array of bytes, shifting 1 byte right. */ - private static byte[] rotateRight(byte[] b) { - int len = b.length; - byte b0 = b[0]; - for (int i = 1; i < len; i++) - b[i - 1] = b[i]; - b[len - 1] = b0; - return b; - } - - /** Returns a copy of an array of bytes b */ - public static byte[] clone(byte[] b) { - return getBytes(b, 0, b.length); - } - - /** - * Returns a len-byte array from array b with offset - * offset - */ - public static byte[] getBytes(byte[] b, int offset, int len) { - byte[] bb = new byte[len]; - for (int k = 0; k < len; k++) - bb[k] = b[offset + k]; - return bb; - } - - /** Returns a 2-byte array from array b with offset offset */ - public static byte[] twoBytes(byte[] b, int offset) { - return getBytes(b, offset, 2); - } - - /** Returns a 4-byte array from array b with offset offset */ - public static byte[] fourBytes(byte[] b, int offset) { - return getBytes(b, offset, 4); - } - - /** - * Copies all bytes of array src into array dst with offset - * offset - */ - public static void copyBytes(byte[] src, byte[] dst, int offset) { - for (int k = 0; k < src.length; k++) - dst[offset + k] = src[k]; - } - - /** - * Copies the first len bytes of array src into array dst - * with offset offset - */ - public static void copyBytes(byte[] src, byte[] dst, int offset, int len) { - for (int k = 0; k < len; k++) - dst[offset + k] = src[k]; - } - - /** - * Copies the first 2 bytes of array src into array dst with - * offset offset - */ - public static void copyTwoBytes(byte[] src, byte[] dst, int offset) { - copyBytes(src, dst, offset, 2); - } - - /** - * Copies a the first 4 bytes of array src into array dst - * with offset index - */ - public static void copyFourBytes(byte[] src, byte[] dst, int offset) { - copyBytes(src, dst, offset, 4); - } - - /** - * Transforms the first len bytes of an array into a string of hex - * values - */ - public static String bytesToHexString(byte[] b, int len) { - String s = new String(); - for (int i = 0; i < len; i++) { - s += Integer.toHexString((((b[i] + 256) % 256) / 16) % 16); - s += Integer.toHexString(((b[i] + 256) % 256) % 16); - } - return s; - } - - /** Transforms a byte array into a string of hex values */ - public static String bytesToHexString(byte[] b) { - return bytesToHexString(b, b.length); - } - - /** - * Transforms a string of hex values into an array of bytes of max length - * len. The string may include ':' chars. If len is set to - * -1, all string is converted. - */ - public static byte[] hexStringToBytes(String str, int len) { // if the - // string is - // of the - // form - // xx:yy:zz:ww.., - // remove - // all ':' - // first - if (str.indexOf(":") >= 0) { - String aux = ""; - char c; - for (int i = 0; i < str.length(); i++) - if ((c = str.charAt(i)) != ':') - aux += c; - str = aux; - } - // if len=-1, set the len value - if (len < 0) - len = str.length() / 2; - byte[] b = new byte[len]; - for (int i = 0; i < len; i++) { - if (len < str.length() / 2) - b[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), - 16); - else - b[i] = 0; - } - return b; - } - - /** - * Transforms a string of hex values into an array of bytes. The string may - * include ':' chars. - */ - public static byte[] hexStringToBytes(String str) { - return hexStringToBytes(str, -1); - } - - /** Transforms a four-bytes array into a dotted four-decimals string */ - public static String bytesToAddress(byte[] b) { - return Integer.toString(uByte(b[0])) + "." - + Integer.toString(uByte(b[1])) + "." - + Integer.toString(uByte(b[2])) + "." - + Integer.toString(uByte(b[3])); - } - - /** Transforms a dotted four-decimals string into a four-bytes array */ - public static byte[] addressToBytes(String addr) { - int begin = 0, end; - byte[] b = new byte[4]; - for (int i = 0; i < 4; i++) { - String num; - if (i < 3) { - end = addr.indexOf('.', begin); - b[i] = (byte) Integer.parseInt(addr.substring(begin, end)); - begin = end + 1; - } else - b[3] = (byte) Integer.parseInt(addr.substring(begin)); - } - return b; - } - - /** Transforms a 4-bytes array into a 32-bit int */ - public static long bytesToInt(byte[] b) { - return ((((((long) uByte(b[0]) << 8) + uByte(b[1])) << 8) + uByte(b[2])) << 8) - + uByte(b[3]); - } - - /** Transforms a 32-bit int into a 4-bytes array */ - public static byte[] intToBytes(long n) { - byte[] b = new byte[4]; - b[0] = (byte) (n >> 24); - b[1] = (byte) ((n >> 16) % 256); - b[2] = (byte) ((n >> 8) % 256); - b[3] = (byte) (n % 256); - return b; - } - - /** - * Transforms a 4-bytes array into a 32-bit word (with the more - * significative byte at left) - */ - public static long bytesToWord(byte[] b, int offset) { - return ((((((long) uByte(b[offset + 3]) << 8) + uByte(b[offset + 2])) << 8) + uByte(b[offset + 1])) << 8) - + uByte(b[offset + 0]); - } - - /** - * Transforms a 4-bytes array into a 32-bit word (with the more - * significative byte at left) - */ - public static long bytesToWord(byte[] b) { - return ((((((long) uByte(b[3]) << 8) + uByte(b[2])) << 8) + uByte(b[1])) << 8) - + uByte(b[0]); - } - - /** - * Transforms a 32-bit word (with the more significative byte at left) into - * a 4-bytes array - */ - public static byte[] wordToBytes(long n) { - byte[] b = new byte[4]; - b[3] = (byte) (n >> 24); - b[2] = (byte) ((n >> 16) % 256); - b[1] = (byte) ((n >> 8) % 256); - b[0] = (byte) (n % 256); - return b; - } - - private static void print(String str) { - System.out.println(str); - } - - // *************************** MAIN **************************** - - private static void decode(byte buffer[], int[] out) { - int offset = 0; - int len = 64; - for (int i = 0; offset < len; i++, offset += 4) { - out[i] = ((int) (buffer[offset] & 0xff)) - | (((int) (buffer[offset + 1] & 0xff)) << 8) - | (((int) (buffer[offset + 2] & 0xff)) << 16) - | (((int) buffer[offset + 3]) << 24); - } - } - - public static void main(String[] args) { - byte[] buff = new byte[64]; - for (int i = 0; i < 64; i++) - buff[i] = (byte) i; - - int[] x = new int[16]; - for (int i = 0; i < 16; i++) - x[i] = (int) bytesToWord(buff, (i * 4)); - - for (int i = 0; i < 16; i++) - print("x[" + i + "]: " + bytesToHexString(wordToBytes(x[i]))); - decode(buff, x); - for (int i = 0; i < 16; i++) - print("x[" + i + "]: " + bytesToHexString(wordToBytes(x[i]))); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Mangle collects some static methods for mangling binary-data structures + */ +public class Mangle { + + /** Compares two arrays of bytes */ + public static boolean compare(byte[] a, byte[] b) { + if (a.length != b.length) + return false; + for (int i = 0; i < a.length; i++) + if (a[i] != b[i]) + return false; + return true; + } + + /** Initalizes a byte array with value value */ + public static byte[] initBytes(byte[] b, int value) { + for (int i = 0; i < b.length; i++) + b[i] = (byte) value; + return b; + } + + /** Gets the unsigned representatin of a byte (into a short) */ + public static short uByte(byte b) { + return (short) (((short) b + 256) % 256); + } + + /** Gets the unsigned representatin of a 32-bit word (into a long) */ + public static long uWord(int n) { + long wmask = 0x10000; + wmask *= wmask; + return (long) (((long) n + wmask) % wmask); + } + + /** Rotates w left n bits. */ + private static int rotateLeft(int w, int n) { + return (w << n) | (w >>> (32 - n)); + } + + /** Rotates w right n bits. */ + private static int rotateRight(int w, int n) { + return (w >>> n) | (w << (32 - n)); + } + + /** Rotates an array of int (words), shifting 1 word left. */ + private static int[] rotateLeft(int[] w) { + int len = w.length; + int w1 = w[len - 1]; + for (int i = len - 1; i > 1; i--) + w[i] = w[i - 1]; + w[0] = w1; + return w; + } + + /** Rotates an array of int (words), shifting 1 word right. */ + private static int[] rotateRight(int[] w) { + int len = w.length; + int w0 = w[0]; + for (int i = 1; i < len; i++) + w[i - 1] = w[i]; + w[len - 1] = w0; + return w; + } + + /** Rotates an array of bytes, shifting 1 byte left. */ + private static byte[] rotateLeft(byte[] b) { + int len = b.length; + byte b1 = b[len - 1]; + for (int i = len - 1; i > 1; i--) + b[i] = b[i - 1]; + b[0] = b1; + return b; + } + + /** Rotates an array of bytes, shifting 1 byte right. */ + private static byte[] rotateRight(byte[] b) { + int len = b.length; + byte b0 = b[0]; + for (int i = 1; i < len; i++) + b[i - 1] = b[i]; + b[len - 1] = b0; + return b; + } + + /** Returns a copy of an array of bytes b */ + public static byte[] clone(byte[] b) { + return getBytes(b, 0, b.length); + } + + /** + * Returns a len-byte array from array b with offset + * offset + */ + public static byte[] getBytes(byte[] b, int offset, int len) { + byte[] bb = new byte[len]; + for (int k = 0; k < len; k++) + bb[k] = b[offset + k]; + return bb; + } + + /** Returns a 2-byte array from array b with offset offset */ + public static byte[] twoBytes(byte[] b, int offset) { + return getBytes(b, offset, 2); + } + + /** Returns a 4-byte array from array b with offset offset */ + public static byte[] fourBytes(byte[] b, int offset) { + return getBytes(b, offset, 4); + } + + /** + * Copies all bytes of array src into array dst with offset + * offset + */ + public static void copyBytes(byte[] src, byte[] dst, int offset) { + for (int k = 0; k < src.length; k++) + dst[offset + k] = src[k]; + } + + /** + * Copies the first len bytes of array src into array dst + * with offset offset + */ + public static void copyBytes(byte[] src, byte[] dst, int offset, int len) { + for (int k = 0; k < len; k++) + dst[offset + k] = src[k]; + } + + /** + * Copies the first 2 bytes of array src into array dst with + * offset offset + */ + public static void copyTwoBytes(byte[] src, byte[] dst, int offset) { + copyBytes(src, dst, offset, 2); + } + + /** + * Copies a the first 4 bytes of array src into array dst + * with offset index + */ + public static void copyFourBytes(byte[] src, byte[] dst, int offset) { + copyBytes(src, dst, offset, 4); + } + + /** + * Transforms the first len bytes of an array into a string of hex + * values + */ + public static String bytesToHexString(byte[] b, int len) { + String s = new String(); + for (int i = 0; i < len; i++) { + s += Integer.toHexString((((b[i] + 256) % 256) / 16) % 16); + s += Integer.toHexString(((b[i] + 256) % 256) % 16); + } + return s; + } + + /** Transforms a byte array into a string of hex values */ + public static String bytesToHexString(byte[] b) { + return bytesToHexString(b, b.length); + } + + /** + * Transforms a string of hex values into an array of bytes of max length + * len. The string may include ':' chars. If len is set to + * -1, all string is converted. + */ + public static byte[] hexStringToBytes(String str, int len) { // if the + // string is + // of the + // form + // xx:yy:zz:ww.., + // remove + // all ':' + // first + if (str.indexOf(":") >= 0) { + String aux = ""; + char c; + for (int i = 0; i < str.length(); i++) + if ((c = str.charAt(i)) != ':') + aux += c; + str = aux; + } + // if len=-1, set the len value + if (len < 0) + len = str.length() / 2; + byte[] b = new byte[len]; + for (int i = 0; i < len; i++) { + if (len < str.length() / 2) + b[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), + 16); + else + b[i] = 0; + } + return b; + } + + /** + * Transforms a string of hex values into an array of bytes. The string may + * include ':' chars. + */ + public static byte[] hexStringToBytes(String str) { + return hexStringToBytes(str, -1); + } + + /** Transforms a four-bytes array into a dotted four-decimals string */ + public static String bytesToAddress(byte[] b) { + return Integer.toString(uByte(b[0])) + "." + + Integer.toString(uByte(b[1])) + "." + + Integer.toString(uByte(b[2])) + "." + + Integer.toString(uByte(b[3])); + } + + /** Transforms a dotted four-decimals string into a four-bytes array */ + public static byte[] addressToBytes(String addr) { + int begin = 0, end; + byte[] b = new byte[4]; + for (int i = 0; i < 4; i++) { + String num; + if (i < 3) { + end = addr.indexOf('.', begin); + b[i] = (byte) Integer.parseInt(addr.substring(begin, end)); + begin = end + 1; + } else + b[3] = (byte) Integer.parseInt(addr.substring(begin)); + } + return b; + } + + /** Transforms a 4-bytes array into a 32-bit int */ + public static long bytesToInt(byte[] b) { + return ((((((long) uByte(b[0]) << 8) + uByte(b[1])) << 8) + uByte(b[2])) << 8) + + uByte(b[3]); + } + + /** Transforms a 32-bit int into a 4-bytes array */ + public static byte[] intToBytes(long n) { + byte[] b = new byte[4]; + b[0] = (byte) (n >> 24); + b[1] = (byte) ((n >> 16) % 256); + b[2] = (byte) ((n >> 8) % 256); + b[3] = (byte) (n % 256); + return b; + } + + /** + * Transforms a 4-bytes array into a 32-bit word (with the more + * significative byte at left) + */ + public static long bytesToWord(byte[] b, int offset) { + return ((((((long) uByte(b[offset + 3]) << 8) + uByte(b[offset + 2])) << 8) + uByte(b[offset + 1])) << 8) + + uByte(b[offset + 0]); + } + + /** + * Transforms a 4-bytes array into a 32-bit word (with the more + * significative byte at left) + */ + public static long bytesToWord(byte[] b) { + return ((((((long) uByte(b[3]) << 8) + uByte(b[2])) << 8) + uByte(b[1])) << 8) + + uByte(b[0]); + } + + /** + * Transforms a 32-bit word (with the more significative byte at left) into + * a 4-bytes array + */ + public static byte[] wordToBytes(long n) { + byte[] b = new byte[4]; + b[3] = (byte) (n >> 24); + b[2] = (byte) ((n >> 16) % 256); + b[1] = (byte) ((n >> 8) % 256); + b[0] = (byte) (n % 256); + return b; + } + + private static void print(String str) { + System.out.println(str); + } + + // *************************** MAIN **************************** + + private static void decode(byte buffer[], int[] out) { + int offset = 0; + int len = 64; + for (int i = 0; offset < len; i++, offset += 4) { + out[i] = ((int) (buffer[offset] & 0xff)) + | (((int) (buffer[offset + 1] & 0xff)) << 8) + | (((int) (buffer[offset + 2] & 0xff)) << 16) + | (((int) buffer[offset + 3]) << 24); + } + } + + public static void main(String[] args) { + byte[] buff = new byte[64]; + for (int i = 0; i < 64; i++) + buff[i] = (byte) i; + + int[] x = new int[16]; + for (int i = 0; i < 16; i++) + x[i] = (int) bytesToWord(buff, (i * 4)); + + for (int i = 0; i < 16; i++) + print("x[" + i + "]: " + bytesToHexString(wordToBytes(x[i]))); + decode(buff, x); + for (int i = 0; i < 16; i++) + print("x[" + i + "]: " + bytesToHexString(wordToBytes(x[i]))); + } +} diff --git a/src/org/zoolu/tools/MessageDigest.java b/app/src/main/java/org/zoolu/tools/MessageDigest.java similarity index 96% rename from src/org/zoolu/tools/MessageDigest.java rename to app/src/main/java/org/zoolu/tools/MessageDigest.java index 913d093..38b3714 100644 --- a/src/org/zoolu/tools/MessageDigest.java +++ b/app/src/main/java/org/zoolu/tools/MessageDigest.java @@ -1,79 +1,79 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Generic hash/message-digest algorithm. - */ -public abstract class MessageDigest { - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - abstract public MessageDigest update(byte[] buffer, int offset, int len); - - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - public MessageDigest update(String str) { - byte[] buf = str.getBytes(); - return update(buf, 0, buf.length); - } - - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - public MessageDigest update(byte[] buffer) { - return update(buffer, 0, buffer.length); - } - - /** - * MessageDigest finalization. Ends a message-digest operation, writing the - * the message digest and zeroizing the context. - */ - abstract public byte[] doFinal(); - - /** Gets the MessageDigest. The same as doFinal(). */ - public byte[] getDigest() { - return doFinal(); - } - - /** Gets the Message Digest as string of hex values. */ - public String asHex() { - return asHex(doFinal()); - } - - /** Transforms an array of bytes into a string of hex values. */ - public static String asHex(byte[] buf) { - String str = new String(); - for (int i = 0; i < buf.length; i++) { - str += Integer.toHexString((buf[i] >>> 4) & 0x0F); - str += Integer.toHexString(buf[i] & 0x0F); - } - return str; - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Generic hash/message-digest algorithm. + */ +public abstract class MessageDigest { + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + abstract public MessageDigest update(byte[] buffer, int offset, int len); + + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + public MessageDigest update(String str) { + byte[] buf = str.getBytes(); + return update(buf, 0, buf.length); + } + + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + public MessageDigest update(byte[] buffer) { + return update(buffer, 0, buffer.length); + } + + /** + * MessageDigest finalization. Ends a message-digest operation, writing the + * the message digest and zeroizing the context. + */ + abstract public byte[] doFinal(); + + /** Gets the MessageDigest. The same as doFinal(). */ + public byte[] getDigest() { + return doFinal(); + } + + /** Gets the Message Digest as string of hex values. */ + public String asHex() { + return asHex(doFinal()); + } + + /** Transforms an array of bytes into a string of hex values. */ + public static String asHex(byte[] buf) { + String str = new String(); + for (int i = 0; i < buf.length; i++) { + str += Integer.toHexString((buf[i] >>> 4) & 0x0F); + str += Integer.toHexString(buf[i] & 0x0F); + } + return str; + } + +} diff --git a/src/org/zoolu/tools/Parser.java b/app/src/main/java/org/zoolu/tools/Parser.java similarity index 96% rename from src/org/zoolu/tools/Parser.java rename to app/src/main/java/org/zoolu/tools/Parser.java index 6106e23..4eb5b38 100644 --- a/src/org/zoolu/tools/Parser.java +++ b/app/src/main/java/org/zoolu/tools/Parser.java @@ -1,678 +1,678 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -import java.util.*; - -/** - * Class Parser allows the parsing of String objects.
- * An object Parser is costructed from a String object and provides various - * methods for parsing the String in a stream oriented manner. The class Parser - * also collects different static methods for parsing non-pre-associated - * strings.
- * Parser uses the following definitions: - * - *

- *   
- * <i>string</i> = any string of chars included between ' ' and '˜' - *
- * <i>word</i> = any string of chars without separators - *
- * <i>separators</i> = a vector of chars; e.g. ( ) < > @ , ; : \ " / | [ ] ? = { } HT SP - *
- * <i>alpha</i> = a-z, A-Z - *
- * <i>digit</i> = 0-9 - *
- * <i>integer</i> = any <i>digit word</i> parsed by {@link java.lang.Integer Integer.parseInt(String)} - *
- */ -public class Parser { - - /** The string that is being parsed. */ - protected String str; - /** The the current pointer to the next char within the string. */ - protected int index; - - /** - * Creates the Parser from the String s and point to the beginning - * of the string. - */ - public Parser(String s) { - if (s == null) - throw (new RuntimeException( - "Tried to costruct a new Parser with a null String")); - str = s; - index = 0; - } - - /** - * Creates the Parser from the String s and point to the position - * i. - */ - public Parser(String s, int i) { - if (s == null) - throw (new RuntimeException( - "Tried to costruct a new Parser with a null String")); - str = s; - index = i; - } - - /** - * Creates the Parser from the StringBuffer sb and point to the - * beginning of the string. - */ - public Parser(StringBuffer sb) { - if (sb == null) - throw (new RuntimeException( - "Tried to costruct a new Parser with a null StringBuffer")); - str = sb.toString(); - index = 0; - } - - /** - * Creates the Parser from the StringBuffer sb and point to the - * position i. - */ - public Parser(StringBuffer sb, int i) { - if (sb == null) - throw (new RuntimeException( - "Tried to costruct a new Parser with a null StringBuffer")); - str = sb.toString(); - index = i; - } - - /** Gets the current index position. */ - public int getPos() { - return index; - } - - /** Gets the entire string */ - public String getWholeString() { - return str; - } - - /** Gets the rest of the (unparsed) string. */ - public String getRemainingString() { - return str.substring(index); - } - - /** - * Returns a new the Parser of len chars statirng from the current - * position. - */ - public Parser subParser(int len) { - return new Parser(str.substring(index, index + len)); - } - - /** Length of unparsed string. */ - public int length() { - return (str.length() - index); - } - - /** Whether there are more chars to parse. */ - public boolean hasMore() { - return length() > 0; - } - - /** Gets the next char and go over */ - public char getChar() { - return str.charAt(index++); - } - - /** Gets the char at distance n WITHOUT going over */ - public char charAt(int n) { - return str.charAt(index + n); - } - - /** Gets the next char WITHOUT going over */ - public char nextChar() { - return charAt(0); - } - - /** Goes to position i */ - public Parser setPos(int i) { - index = i; - return this; - } - - /** Goes to the next occurence of char c */ - public Parser goTo(char c) { - index = str.indexOf(c, index); - if (index < 0) - index = str.length(); - return this; - } - - /** Goes to the next occurence of any char of array cc */ - public Parser goTo(char[] cc) { - index = indexOf(cc); - if (index < 0) - index = str.length(); - return this; - } - - /** Goes to the next occurence of String s */ - public Parser goTo(String s) { - index = str.indexOf(s, index); - if (index < 0) - index = str.length(); - return this; - } - - /** Goes to the next occurence of any string of array ss */ - public Parser goTo(String[] ss) { - index = indexOf(ss); - if (index < 0) - index = str.length(); - return this; - } - - /** Goes to the next occurence of String s */ - public Parser goToIgnoreCase(String s) { - index = indexOfIgnoreCase(s); - if (index < 0) - index = str.length(); - return this; - } - - /** Goes to the next occurence of any string of array ss */ - public Parser goToIgnoreCase(String[] ss) { - index = indexOfIgnoreCase(ss); - if (index < 0) - index = str.length(); - return this; - } - - /** Goes to the begin of the new line */ - public Parser goToNextLine() { - while (index < str.length() && !isCRLF(str.charAt(index))) - index++; - // skip the end of the line (i.e. '\r' OR '\n' OR '\r\n') - if (index < str.length()) { - if (str.startsWith("\r\n", index)) - index += 2; - else - index++; - } - return this; - } - - /** Characters space (SP) and tab (HT). */ - public static char[] WSP = { ' ', '\t' }; - /** The same as WSP (for legacy) */ - public static char[] SPACE = WSP; - /** Characters CR and LF. */ - public static char[] CRLF = { '\r', '\n' }; - /** Characters white-space, tab, CR, and LF. */ - public static char[] WSPCRLF = { ' ', '\t', '\r', '\n' }; - - /** True if char ch is any char of array ca */ - public static boolean isAnyOf(char[] ca, char ch) { - boolean found = false; - for (int i = 0; i < ca.length; i++) - if (ca[i] == ch) { - found = true; - break; - } - return found; - } - - /** Up alpha */ - public static boolean isUpAlpha(char c) { - return (c >= 'A' && c <= 'Z'); - } - - /** Low alpha */ - public static boolean isLowAlpha(char c) { - return (c >= 'a' && c <= 'z'); - } - - /** Alpha */ - public static boolean isAlpha(char c) { - return (isUpAlpha(c) || isLowAlpha(c)); - } - - /** Alphanum */ - public static boolean isAlphanum(char c) { - return (isAlpha(c) || isDigit(c)); - } - - /** Digit */ - public static boolean isDigit(char c) { - return (c >= '0' && c <= '9'); - } - - /** Valid ASCII char */ - public static boolean isChar(char c) { - return (c > ' ' && c <= '~'); - } - - /** CR */ - public static boolean isCR(char c) { - return (c == '\r'); - } - - /** LF */ - public static boolean isLF(char c) { - return (c == '\n'); - } - - /** CR or LF */ - public static boolean isCRLF(char c) { - return isAnyOf(CRLF, c); - } - - /** HT */ - public static boolean isHT(char c) { - return (c == '\t'); - } - - /** SP */ - public static boolean isSP(char c) { - return (c == ' '); - } - - /** SP or tab */ - public static boolean isWSP(char c) { - return isAnyOf(WSP, c); - } - - /** SP, tab, CR, or LF */ - public static boolean isWSPCRLF(char c) { - return isAnyOf(WSPCRLF, c); - } - - /** Compares two chars ignoring case */ - public static int compareIgnoreCase(char c1, char c2) { - if (isUpAlpha(c1)) - c1 += 32; - if (isUpAlpha(c2)) - c2 += 32; - return c1 - c2; - } - - /* - * private boolean isUpAlpha(int i) { return isUpAlpha(str.charAt(i)); } - * private boolean isLowAlpha(int i) { return isLowAlpha(str.charAt(i)); } - * private boolean isAlpha(int i) { return isAlpha(str.charAt(i)); } private - * boolean isDigit(int i) { return isDigit(str.charAt(i)); } private boolean - * isChar(int i) { return isChar(str.charAt(i)); } private boolean isCR(int - * i) { return isCR(str.charAt(i)); } private boolean isLF(int i) { return - * isLF(str.charAt(i)); } private boolean isHT(int i) { return - * isHT(str.charAt(i)); } private boolean isSP(int i) { return - * isSP(str.charAt(i)); } private boolean isCRLF(int i) { return - * (isCR(str.charAt(i)) && isLF(str.charAt(i+1))); } private boolean - * isSeparator(int i) { return isSeparator(str.charAt(i)); } - */ - - // ************************ Indexes ************************ - /** Gets the index of the first occurence of char c */ - public int indexOf(char c) { - return str.indexOf(c, index); - } - - /** - * Gets the index of the first occurence of any char of array cc - * within string str starting form begin; return -1 if no - * occurence is found - */ - public int indexOf(char[] cc) { - boolean found = false; - int begin = index; - while (begin < str.length() && !found) { - for (int i = 0; i < cc.length; i++) - if (str.charAt(begin) == cc[i]) { - found = true; - break; - } - begin++; - } - return (found) ? (begin - 1) : -1; - } - - /** Gets the index of the first occurence of String s */ - public int indexOf(String s) { - return str.indexOf(s, index); - } - - /** - * Gets the index of the first occurence of any string of array ss - * within string str; return -1 if no occurence is found. - */ - public int indexOf(String[] ss) { - boolean found = false; - int begin = index; - while (begin < str.length() && !found) { - for (int i = 0; i < ss.length; i++) - if (str.startsWith(ss[i], begin)) { - found = true; - break; - } - begin++; - } - return (found) ? (begin - 1) : -1; - } - - /** Gets the index of the first occurence of String s ignoring case. */ - public int indexOfIgnoreCase(String s) { - Parser par = new Parser(str, index); - while (par.hasMore()) { - if (par.startsWithIgnoreCase(s)) - return par.getPos(); - else - par.skipChar(); - } - return -1; - } - - /** - * Gets the index of the first occurence of any string of array ss - * ignoring case. - */ - public int indexOfIgnoreCase(String[] ss) { - Parser par = new Parser(str, index); - while (par.hasMore()) { - if (par.startsWithIgnoreCase(ss)) - return par.getPos(); - else - par.skipChar(); - } - return -1; - } - - /** Gets the begin of next line */ - public int indexOfNextLine() { - Parser par = new Parser(str, index); - par.goToNextLine(); - int i = par.getPos(); - return (i < str.length()) ? i : -1; - } - - // ********************* Starts with ********************* - - /** Whether next chars equal to a specific String s. */ - public boolean startsWith(String s) { - return str.startsWith(s, index); - } - - /** Whether next chars equal to any string of array ss. */ - public boolean startsWith(String[] ss) { - for (int i = 0; i < ss.length; i++) - if (str.startsWith(ss[i], index)) - return true; - return false; - } - - /** Whether next chars equal to a specific String s ignoring case. */ - public boolean startsWithIgnoreCase(String s) { - for (int k = 0; k < s.length() && (index + k) < str.length(); k++) { - if (compareIgnoreCase(s.charAt(k), str.charAt(index + k)) != 0) - return false; - } - return true; - } - - /** Whether next chars equal to any string of array ss ignoring case. */ - public boolean startsWithIgnoreCase(String[] ss) { - for (int i = 0; i < ss.length; i++) { - boolean equal = true; - for (int k = 0; k < ss[i].length() && (index + k) < str.length(); k++) { - if (!(equal = (compareIgnoreCase(ss[i].charAt(k), str - .charAt(index + k)) == 0))) - break; - } - if (equal) - return true; - } - return false; - } - - // ************************ Skips ************************ - - /** Skips one char */ - public Parser skipChar() { - if (index < str.length()) - index++; - return this; - } - - /** Skips N chars */ - public Parser skipN(int n) { - index += n; - if (index > str.length()) - index = str.length(); - return this; - } - - /** Skips all spaces */ - public Parser skipWSP() { - while (index < str.length() && isSP(str.charAt(index))) - index++; - return this; - } - - /** The same as skipWSP() (for legacy) */ - /* - * public Parser skipSP() { return skipWSP(); } - */ - /** Skips return lines */ - public Parser skipCRLF() { - while (index < str.length() && isCRLF(str.charAt(index))) - index++; - return this; - } - - /** Skips white spaces or return lines */ - public Parser skipWSPCRLF() { - while (index < str.length() && isWSPCRLF(str.charAt(index))) - index++; - return this; - } - - /** Skips any selected chars */ - public Parser skipChars(char[] cc) { - while (index < str.length() && isAnyOf(cc, nextChar())) - index++; - return this; - } - - /** Skips a continuous string of char and go to the next "blank" char */ - public Parser skipString() { - getString(); - return this; - } - - // ************************ Gets ************************ - - /** Gets a continuous string of char and go to the next char */ - public String getString() { - int begin = index; - while (begin < str.length() && !isChar(str.charAt(begin))) - begin++; - int end = begin; - while (end < str.length() && isChar(str.charAt(end))) - end++; - index = end; - return str.substring(begin, end); - } - - /** Gets a string of length len and move over. */ - public String getString(int len) { - int start = index; - index = start + len; - return str.substring(start, index); - } - - /** Gets a string of chars separated by any of chars of separators */ - public String getWord(char[] separators) { - int begin = index; - while (begin < str.length() && isAnyOf(separators, str.charAt(begin))) - begin++; - int end = begin; - while (end < str.length() && !isAnyOf(separators, str.charAt(end))) - end++; - index = end; - return str.substring(begin, end); - } - - /** Gets an integer and point to the next char */ - public int getInt() { - return Integer.parseInt(this.getString()); - } - - /** Gets a double and point to the next char */ - public double getDouble() { - return Double.parseDouble(this.getString()); - } - - /** - * Gets all chars until the end of the line (or the end of the parser) and - * go to the next line. - */ - public String getLine() { - int end = index; - while (end < str.length() && !isCRLF(str.charAt(end))) - end++; - String line = str.substring(index, end); - index = end; - // skip the end of the line (i.e. '\r' OR '\n' OR '\r\n') - if (index < str.length()) { - if (str.startsWith("\r\n", index)) - index += 2; - else - index++; - } - return line; - } - - // ********************** Vectors/arrays ********************** - - /** Gets all string of chars separated by any char belonging to separators */ - public Vector getWordVector(char[] separators) { - Vector list = new Vector(); - do { - list.addElement(getWord(separators)); - } while (hasMore()); - return list; - } - - /** Gets all string of chars separated by any char belonging to separators */ - public String[] getWordArray(char[] separators) { - Vector list = getWordVector(separators); - String[] array = new String[list.size()]; - for (int i = 0; i < list.size(); i++) - array[i] = (String) list.elementAt(i); - return array; - } - - /** Gets all strings */ - public Vector getStringVector() { - Vector list = new Vector(); - do { - list.addElement(getString()); - } while (hasMore()); - return list; - } - - /** Gets all string */ - public String[] getStringArray() { - Vector list = getStringVector(); - String[] array = new String[list.size()]; - for (int i = 0; i < list.size(); i++) - array[i] = (String) list.elementAt(i); - return array; - } - - // ********************** Quoted Strings ********************** - - /** - * Gets a string of chars separated by any of chars in separators , - * skipping any separator inside possible quoted texts. - */ - public String getWordSkippingQuoted(char[] separators) { - int begin = index; - while (begin < str.length() && isAnyOf(separators, str.charAt(begin))) - begin++; - boolean inside_quoted_string = false; - int end = begin; - while (end < str.length() - && (!isAnyOf(separators, str.charAt(end)) || inside_quoted_string)) { - if (str.charAt(end) == '"') - inside_quoted_string = !inside_quoted_string; - end++; - } - index = end; - return str.substring(begin, end); - } - - /** - * Gets the first quatable string, that is a normal string, or text in - * quotes.
- * In the latter case, quotes are dropped. - */ - public String getStringUnquoted() { // jump possible "non-chars" - while (index < str.length() && !isChar(str.charAt(index))) - index++; - if (index == str.length()) - return str.substring(index, index); - // check whether is a quoted string - int next_qmark; - if (str.charAt(index) == '"' - && (next_qmark = str.indexOf("\"", index + 1)) > 0) { // is - // quoted - // text - String qtext = str.substring(index + 1, next_qmark); - index = next_qmark + 1; - return qtext; - } else { // is not a quoted text - return getString(); - } - } - - /** Points to the next occurence of char c not in quotes. */ - public Parser goToSkippingQuoted(char c) { - boolean inside_quotes = false; - try { - while (index < str.length() - && (!(nextChar() == c) || inside_quotes)) { - if (nextChar() == '"') - inside_quotes = !inside_quotes; - index++; - } - } catch (RuntimeException e) { - System.out.println("len= " + str.length()); - System.out.println("index= " + index); - throw e; - } - return this; - } - - // ************************* toString ************************* - - /** convert the rest of the unparsed chars into a string */ - public String toString() { - return getRemainingString(); - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.util.*; + +/** + * Class Parser allows the parsing of String objects.
+ * An object Parser is costructed from a String object and provides various + * methods for parsing the String in a stream oriented manner. The class Parser + * also collects different static methods for parsing non-pre-associated + * strings.
+ * Parser uses the following definitions: + * + *
+ *   
+ * <i>string</i> = any string of chars included between ' ' and '˜' + *
+ * <i>word</i> = any string of chars without separators + *
+ * <i>separators</i> = a vector of chars; e.g. ( ) < > @ , ; : \ " / | [ ] ? = { } HT SP + *
+ * <i>alpha</i> = a-z, A-Z + *
+ * <i>digit</i> = 0-9 + *
+ * <i>integer</i> = any <i>digit word</i> parsed by {@link java.lang.Integer Integer.parseInt(String)} + *
+ */ +public class Parser { + + /** The string that is being parsed. */ + protected String str; + /** The the current pointer to the next char within the string. */ + protected int index; + + /** + * Creates the Parser from the String s and point to the beginning + * of the string. + */ + public Parser(String s) { + if (s == null) + throw (new RuntimeException( + "Tried to costruct a new Parser with a null String")); + str = s; + index = 0; + } + + /** + * Creates the Parser from the String s and point to the position + * i. + */ + public Parser(String s, int i) { + if (s == null) + throw (new RuntimeException( + "Tried to costruct a new Parser with a null String")); + str = s; + index = i; + } + + /** + * Creates the Parser from the StringBuffer sb and point to the + * beginning of the string. + */ + public Parser(StringBuffer sb) { + if (sb == null) + throw (new RuntimeException( + "Tried to costruct a new Parser with a null StringBuffer")); + str = sb.toString(); + index = 0; + } + + /** + * Creates the Parser from the StringBuffer sb and point to the + * position i. + */ + public Parser(StringBuffer sb, int i) { + if (sb == null) + throw (new RuntimeException( + "Tried to costruct a new Parser with a null StringBuffer")); + str = sb.toString(); + index = i; + } + + /** Gets the current index position. */ + public int getPos() { + return index; + } + + /** Gets the entire string */ + public String getWholeString() { + return str; + } + + /** Gets the rest of the (unparsed) string. */ + public String getRemainingString() { + return str.substring(index); + } + + /** + * Returns a new the Parser of len chars statirng from the current + * position. + */ + public Parser subParser(int len) { + return new Parser(str.substring(index, index + len)); + } + + /** Length of unparsed string. */ + public int length() { + return (str.length() - index); + } + + /** Whether there are more chars to parse. */ + public boolean hasMore() { + return length() > 0; + } + + /** Gets the next char and go over */ + public char getChar() { + return str.charAt(index++); + } + + /** Gets the char at distance n WITHOUT going over */ + public char charAt(int n) { + return str.charAt(index + n); + } + + /** Gets the next char WITHOUT going over */ + public char nextChar() { + return charAt(0); + } + + /** Goes to position i */ + public Parser setPos(int i) { + index = i; + return this; + } + + /** Goes to the next occurence of char c */ + public Parser goTo(char c) { + index = str.indexOf(c, index); + if (index < 0) + index = str.length(); + return this; + } + + /** Goes to the next occurence of any char of array cc */ + public Parser goTo(char[] cc) { + index = indexOf(cc); + if (index < 0) + index = str.length(); + return this; + } + + /** Goes to the next occurence of String s */ + public Parser goTo(String s) { + index = str.indexOf(s, index); + if (index < 0) + index = str.length(); + return this; + } + + /** Goes to the next occurence of any string of array ss */ + public Parser goTo(String[] ss) { + index = indexOf(ss); + if (index < 0) + index = str.length(); + return this; + } + + /** Goes to the next occurence of String s */ + public Parser goToIgnoreCase(String s) { + index = indexOfIgnoreCase(s); + if (index < 0) + index = str.length(); + return this; + } + + /** Goes to the next occurence of any string of array ss */ + public Parser goToIgnoreCase(String[] ss) { + index = indexOfIgnoreCase(ss); + if (index < 0) + index = str.length(); + return this; + } + + /** Goes to the begin of the new line */ + public Parser goToNextLine() { + while (index < str.length() && !isCRLF(str.charAt(index))) + index++; + // skip the end of the line (i.e. '\r' OR '\n' OR '\r\n') + if (index < str.length()) { + if (str.startsWith("\r\n", index)) + index += 2; + else + index++; + } + return this; + } + + /** Characters space (SP) and tab (HT). */ + public static char[] WSP = { ' ', '\t' }; + /** The same as WSP (for legacy) */ + public static char[] SPACE = WSP; + /** Characters CR and LF. */ + public static char[] CRLF = { '\r', '\n' }; + /** Characters white-space, tab, CR, and LF. */ + public static char[] WSPCRLF = { ' ', '\t', '\r', '\n' }; + + /** True if char ch is any char of array ca */ + public static boolean isAnyOf(char[] ca, char ch) { + boolean found = false; + for (int i = 0; i < ca.length; i++) + if (ca[i] == ch) { + found = true; + break; + } + return found; + } + + /** Up alpha */ + public static boolean isUpAlpha(char c) { + return (c >= 'A' && c <= 'Z'); + } + + /** Low alpha */ + public static boolean isLowAlpha(char c) { + return (c >= 'a' && c <= 'z'); + } + + /** Alpha */ + public static boolean isAlpha(char c) { + return (isUpAlpha(c) || isLowAlpha(c)); + } + + /** Alphanum */ + public static boolean isAlphanum(char c) { + return (isAlpha(c) || isDigit(c)); + } + + /** Digit */ + public static boolean isDigit(char c) { + return (c >= '0' && c <= '9'); + } + + /** Valid ASCII char */ + public static boolean isChar(char c) { + return (c > ' ' && c <= '~'); + } + + /** CR */ + public static boolean isCR(char c) { + return (c == '\r'); + } + + /** LF */ + public static boolean isLF(char c) { + return (c == '\n'); + } + + /** CR or LF */ + public static boolean isCRLF(char c) { + return isAnyOf(CRLF, c); + } + + /** HT */ + public static boolean isHT(char c) { + return (c == '\t'); + } + + /** SP */ + public static boolean isSP(char c) { + return (c == ' '); + } + + /** SP or tab */ + public static boolean isWSP(char c) { + return isAnyOf(WSP, c); + } + + /** SP, tab, CR, or LF */ + public static boolean isWSPCRLF(char c) { + return isAnyOf(WSPCRLF, c); + } + + /** Compares two chars ignoring case */ + public static int compareIgnoreCase(char c1, char c2) { + if (isUpAlpha(c1)) + c1 += 32; + if (isUpAlpha(c2)) + c2 += 32; + return c1 - c2; + } + + /* + * private boolean isUpAlpha(int i) { return isUpAlpha(str.charAt(i)); } + * private boolean isLowAlpha(int i) { return isLowAlpha(str.charAt(i)); } + * private boolean isAlpha(int i) { return isAlpha(str.charAt(i)); } private + * boolean isDigit(int i) { return isDigit(str.charAt(i)); } private boolean + * isChar(int i) { return isChar(str.charAt(i)); } private boolean isCR(int + * i) { return isCR(str.charAt(i)); } private boolean isLF(int i) { return + * isLF(str.charAt(i)); } private boolean isHT(int i) { return + * isHT(str.charAt(i)); } private boolean isSP(int i) { return + * isSP(str.charAt(i)); } private boolean isCRLF(int i) { return + * (isCR(str.charAt(i)) && isLF(str.charAt(i+1))); } private boolean + * isSeparator(int i) { return isSeparator(str.charAt(i)); } + */ + + // ************************ Indexes ************************ + /** Gets the index of the first occurence of char c */ + public int indexOf(char c) { + return str.indexOf(c, index); + } + + /** + * Gets the index of the first occurence of any char of array cc + * within string str starting form begin; return -1 if no + * occurence is found + */ + public int indexOf(char[] cc) { + boolean found = false; + int begin = index; + while (begin < str.length() && !found) { + for (int i = 0; i < cc.length; i++) + if (str.charAt(begin) == cc[i]) { + found = true; + break; + } + begin++; + } + return (found) ? (begin - 1) : -1; + } + + /** Gets the index of the first occurence of String s */ + public int indexOf(String s) { + return str.indexOf(s, index); + } + + /** + * Gets the index of the first occurence of any string of array ss + * within string str; return -1 if no occurence is found. + */ + public int indexOf(String[] ss) { + boolean found = false; + int begin = index; + while (begin < str.length() && !found) { + for (int i = 0; i < ss.length; i++) + if (str.startsWith(ss[i], begin)) { + found = true; + break; + } + begin++; + } + return (found) ? (begin - 1) : -1; + } + + /** Gets the index of the first occurence of String s ignoring case. */ + public int indexOfIgnoreCase(String s) { + Parser par = new Parser(str, index); + while (par.hasMore()) { + if (par.startsWithIgnoreCase(s)) + return par.getPos(); + else + par.skipChar(); + } + return -1; + } + + /** + * Gets the index of the first occurence of any string of array ss + * ignoring case. + */ + public int indexOfIgnoreCase(String[] ss) { + Parser par = new Parser(str, index); + while (par.hasMore()) { + if (par.startsWithIgnoreCase(ss)) + return par.getPos(); + else + par.skipChar(); + } + return -1; + } + + /** Gets the begin of next line */ + public int indexOfNextLine() { + Parser par = new Parser(str, index); + par.goToNextLine(); + int i = par.getPos(); + return (i < str.length()) ? i : -1; + } + + // ********************* Starts with ********************* + + /** Whether next chars equal to a specific String s. */ + public boolean startsWith(String s) { + return str.startsWith(s, index); + } + + /** Whether next chars equal to any string of array ss. */ + public boolean startsWith(String[] ss) { + for (int i = 0; i < ss.length; i++) + if (str.startsWith(ss[i], index)) + return true; + return false; + } + + /** Whether next chars equal to a specific String s ignoring case. */ + public boolean startsWithIgnoreCase(String s) { + for (int k = 0; k < s.length() && (index + k) < str.length(); k++) { + if (compareIgnoreCase(s.charAt(k), str.charAt(index + k)) != 0) + return false; + } + return true; + } + + /** Whether next chars equal to any string of array ss ignoring case. */ + public boolean startsWithIgnoreCase(String[] ss) { + for (int i = 0; i < ss.length; i++) { + boolean equal = true; + for (int k = 0; k < ss[i].length() && (index + k) < str.length(); k++) { + if (!(equal = (compareIgnoreCase(ss[i].charAt(k), str + .charAt(index + k)) == 0))) + break; + } + if (equal) + return true; + } + return false; + } + + // ************************ Skips ************************ + + /** Skips one char */ + public Parser skipChar() { + if (index < str.length()) + index++; + return this; + } + + /** Skips N chars */ + public Parser skipN(int n) { + index += n; + if (index > str.length()) + index = str.length(); + return this; + } + + /** Skips all spaces */ + public Parser skipWSP() { + while (index < str.length() && isSP(str.charAt(index))) + index++; + return this; + } + + /** The same as skipWSP() (for legacy) */ + /* + * public Parser skipSP() { return skipWSP(); } + */ + /** Skips return lines */ + public Parser skipCRLF() { + while (index < str.length() && isCRLF(str.charAt(index))) + index++; + return this; + } + + /** Skips white spaces or return lines */ + public Parser skipWSPCRLF() { + while (index < str.length() && isWSPCRLF(str.charAt(index))) + index++; + return this; + } + + /** Skips any selected chars */ + public Parser skipChars(char[] cc) { + while (index < str.length() && isAnyOf(cc, nextChar())) + index++; + return this; + } + + /** Skips a continuous string of char and go to the next "blank" char */ + public Parser skipString() { + getString(); + return this; + } + + // ************************ Gets ************************ + + /** Gets a continuous string of char and go to the next char */ + public String getString() { + int begin = index; + while (begin < str.length() && !isChar(str.charAt(begin))) + begin++; + int end = begin; + while (end < str.length() && isChar(str.charAt(end))) + end++; + index = end; + return str.substring(begin, end); + } + + /** Gets a string of length len and move over. */ + public String getString(int len) { + int start = index; + index = start + len; + return str.substring(start, index); + } + + /** Gets a string of chars separated by any of chars of separators */ + public String getWord(char[] separators) { + int begin = index; + while (begin < str.length() && isAnyOf(separators, str.charAt(begin))) + begin++; + int end = begin; + while (end < str.length() && !isAnyOf(separators, str.charAt(end))) + end++; + index = end; + return str.substring(begin, end); + } + + /** Gets an integer and point to the next char */ + public int getInt() { + return Integer.parseInt(this.getString()); + } + + /** Gets a double and point to the next char */ + public double getDouble() { + return Double.parseDouble(this.getString()); + } + + /** + * Gets all chars until the end of the line (or the end of the parser) and + * go to the next line. + */ + public String getLine() { + int end = index; + while (end < str.length() && !isCRLF(str.charAt(end))) + end++; + String line = str.substring(index, end); + index = end; + // skip the end of the line (i.e. '\r' OR '\n' OR '\r\n') + if (index < str.length()) { + if (str.startsWith("\r\n", index)) + index += 2; + else + index++; + } + return line; + } + + // ********************** Vectors/arrays ********************** + + /** Gets all string of chars separated by any char belonging to separators */ + public Vector getWordVector(char[] separators) { + Vector list = new Vector(); + do { + list.addElement(getWord(separators)); + } while (hasMore()); + return list; + } + + /** Gets all string of chars separated by any char belonging to separators */ + public String[] getWordArray(char[] separators) { + Vector list = getWordVector(separators); + String[] array = new String[list.size()]; + for (int i = 0; i < list.size(); i++) + array[i] = (String) list.elementAt(i); + return array; + } + + /** Gets all strings */ + public Vector getStringVector() { + Vector list = new Vector(); + do { + list.addElement(getString()); + } while (hasMore()); + return list; + } + + /** Gets all string */ + public String[] getStringArray() { + Vector list = getStringVector(); + String[] array = new String[list.size()]; + for (int i = 0; i < list.size(); i++) + array[i] = (String) list.elementAt(i); + return array; + } + + // ********************** Quoted Strings ********************** + + /** + * Gets a string of chars separated by any of chars in separators , + * skipping any separator inside possible quoted texts. + */ + public String getWordSkippingQuoted(char[] separators) { + int begin = index; + while (begin < str.length() && isAnyOf(separators, str.charAt(begin))) + begin++; + boolean inside_quoted_string = false; + int end = begin; + while (end < str.length() + && (!isAnyOf(separators, str.charAt(end)) || inside_quoted_string)) { + if (str.charAt(end) == '"') + inside_quoted_string = !inside_quoted_string; + end++; + } + index = end; + return str.substring(begin, end); + } + + /** + * Gets the first quatable string, that is a normal string, or text in + * quotes.
+ * In the latter case, quotes are dropped. + */ + public String getStringUnquoted() { // jump possible "non-chars" + while (index < str.length() && !isChar(str.charAt(index))) + index++; + if (index == str.length()) + return str.substring(index, index); + // check whether is a quoted string + int next_qmark; + if (str.charAt(index) == '"' + && (next_qmark = str.indexOf("\"", index + 1)) > 0) { // is + // quoted + // text + String qtext = str.substring(index + 1, next_qmark); + index = next_qmark + 1; + return qtext; + } else { // is not a quoted text + return getString(); + } + } + + /** Points to the next occurence of char c not in quotes. */ + public Parser goToSkippingQuoted(char c) { + boolean inside_quotes = false; + try { + while (index < str.length() + && (!(nextChar() == c) || inside_quotes)) { + if (nextChar() == '"') + inside_quotes = !inside_quotes; + index++; + } + } catch (RuntimeException e) { + System.out.println("len= " + str.length()); + System.out.println("index= " + index); + throw e; + } + return this; + } + + // ************************* toString ************************* + + /** convert the rest of the unparsed chars into a string */ + public String toString() { + return getRemainingString(); + } +} diff --git a/src/org/zoolu/tools/Random.java b/app/src/main/java/org/zoolu/tools/Random.java similarity index 96% rename from src/org/zoolu/tools/Random.java rename to app/src/main/java/org/zoolu/tools/Random.java index 2a2d75d..812f43a 100644 --- a/src/org/zoolu/tools/Random.java +++ b/app/src/main/java/org/zoolu/tools/Random.java @@ -1,110 +1,110 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Class Random collects some static methods for generating random numbers and - * other stuff. - */ -public class Random { - /** The random seed */ - static final long seed = System.currentTimeMillis(); - // static final long seed=0; - - static java.util.Random rand = new java.util.Random(seed); - - // static java.util.Random rand=new java.util.Random(); - - /** Returns a random integer between 0 and n-1 */ - /* - * static public int nextInt(int n) { seed=(seed*37)%987654321; return - * (int)(seed%n); } - */ - - /** Returns true or false respectively with probability p/100 and (1-p/100) */ - /* - * static boolean percent(int p) { return integer(100)filename . RotatingLog size is - * limited to logsize [bytes] - */ - public RotatingLog(String filename, String logname, int loglevel, - long logsize, int n_rotations, int t_scale, int t_value) { - super(filename, logname, loglevel, logsize); - rInit(filename, n_rotations, t_scale, t_value); - } - - /** Rotates logs */ - public RotatingLog rotate() { - if (num_rotations > 0) { - for (int i = num_rotations - 2; i > 0; i--) { // rename back files - rename(file_name + i, file_name + (i + 1)); - } - // save and close current log file - if (out_stream != null) - out_stream.close(); - // rename current log file - if (num_rotations > 1) - rename(file_name, file_name + 1); - // reset the log - try { - out_stream = new PrintStream(new FileOutputStream(file_name)); - } catch (IOException e) { - e.printStackTrace(); - } - init(out_stream, log_tag, verbose_level, max_size); - } - return this; - } - - /** - * Prints the log if level isn't greater than the Log - * verbose_level - */ - public Log print(String message, int level) { // long - // now=GregorianCalendar.getInstance().getTime().getTime(); - long now = Calendar.getInstance().getTime().getTime(); - if (now > next_rotation) { - rotate(); - updateNextRotationTime(); - } - return super.print(message, level); - } - - /** ************************* Private methods ************************** */ - - /** Inits rotation */ - private void rInit(String f_name, int n_rotations, int t_scale, int t_value) { - file_name = f_name; - num_rotations = n_rotations; - time_scale = t_scale; - time_value = t_value; - updateNextRotationTime(); - } - - /** Renames a file */ - private static void rename(String src_file, String dst_file) { - File src = new File(src_file); - if (src.exists()) { - File dst = new File(dst_file); - if (dst.exists()) - dst.delete(); - src.renameTo(dst); - } - } - - /** Updates the next rotation date */ - private void updateNextRotationTime() { // Calendar - // cal=GregorianCalendar.getInstance(); - Calendar cal = Calendar.getInstance(); - cal.add(time_scale, time_value); - next_rotation = cal.getTime().getTime(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +import java.io.*; +import java.util.Calendar; + +// import java.util.GregorianCalendar; + +/** + * Class RotatingLog extends Log with log file rotation. + */ +public class RotatingLog extends Log { + + /** Month */ + // public static final int MONTH=GregorianCalendar.MONTH; + public static final int MONTH = Calendar.MONTH; + /** Day */ + // public static final int DAY=GregorianCalendar.DAY_OF_MONTH; + public static final int DAY = Calendar.DAY_OF_MONTH; + /** Hour */ + // public static final int HOUR=GregorianCalendar.HOUR; + public static final int HOUR = Calendar.HOUR; + /** Minute */ + // public static final int MINUTE=GregorianCalendar.MINUTE; + public static final int MINUTE = Calendar.MINUTE; + + /** Number of log file rotations (value 0 means no rotation) */ + int num_rotations; + + /** Rotates log files */ + String file_name; + + /** Time scale (MONTH, DAY, HOUR, or MINUTE) */ + int time_scale; + + /** Time value that log files are rotated (time = time_scale * time_value) */ + int time_value; + + /** Date of the next rotation */ + long next_rotation; + + /** **************************** Constructors ***************************** */ + + /** + * Creates a new RotatingLog file filename . RotatingLog size is + * limited to logsize [bytes] + */ + public RotatingLog(String filename, String logname, int loglevel, + long logsize, int n_rotations, int t_scale, int t_value) { + super(filename, logname, loglevel, logsize); + rInit(filename, n_rotations, t_scale, t_value); + } + + /** Rotates logs */ + public RotatingLog rotate() { + if (num_rotations > 0) { + for (int i = num_rotations - 2; i > 0; i--) { // rename back files + rename(file_name + i, file_name + (i + 1)); + } + // save and close current log file + if (out_stream != null) + out_stream.close(); + // rename current log file + if (num_rotations > 1) + rename(file_name, file_name + 1); + // reset the log + try { + out_stream = new PrintStream(new FileOutputStream(file_name)); + } catch (IOException e) { + e.printStackTrace(); + } + init(out_stream, log_tag, verbose_level, max_size); + } + return this; + } + + /** + * Prints the log if level isn't greater than the Log + * verbose_level + */ + public Log print(String message, int level) { // long + // now=GregorianCalendar.getInstance().getTime().getTime(); + long now = Calendar.getInstance().getTime().getTime(); + if (now > next_rotation) { + rotate(); + updateNextRotationTime(); + } + return super.print(message, level); + } + + /** ************************* Private methods ************************** */ + + /** Inits rotation */ + private void rInit(String f_name, int n_rotations, int t_scale, int t_value) { + file_name = f_name; + num_rotations = n_rotations; + time_scale = t_scale; + time_value = t_value; + updateNextRotationTime(); + } + + /** Renames a file */ + private static void rename(String src_file, String dst_file) { + File src = new File(src_file); + if (src.exists()) { + File dst = new File(dst_file); + if (dst.exists()) + dst.delete(); + src.renameTo(dst); + } + } + + /** Updates the next rotation date */ + private void updateNextRotationTime() { // Calendar + // cal=GregorianCalendar.getInstance(); + Calendar cal = Calendar.getInstance(); + cal.add(time_scale, time_value); + next_rotation = cal.getTime().getTime(); + } + +} diff --git a/src/org/zoolu/tools/SimpleDigest.java b/app/src/main/java/org/zoolu/tools/SimpleDigest.java similarity index 96% rename from src/org/zoolu/tools/SimpleDigest.java rename to app/src/main/java/org/zoolu/tools/SimpleDigest.java index bc6e2d4..d3ed63c 100644 --- a/src/org/zoolu/tools/SimpleDigest.java +++ b/app/src/main/java/org/zoolu/tools/SimpleDigest.java @@ -1,137 +1,137 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** - * Simple and fast hash/message-digest algorithm. - */ -public class SimpleDigest extends MessageDigest { - - // ************************* Attributes ************************* - - /** The digest */ - byte[] message_digest; - - /** Whether is finalized. */ - boolean is_done; - - /** Index within the message_digest. */ - int index; - - /** Add term */ - byte add_term; - - // *********************** Public methods *********************** - - /** Constructor */ - public SimpleDigest(int size) { - init(size); - } - - /** Constructor */ - public SimpleDigest(int size, byte[] buffer) { - init(size); - update(buffer); - } - - /** Constructor */ - public SimpleDigest(int size, byte[] buffer, int offset, int len) { - init(size); - update(buffer, offset, len); - } - - /** Constructor */ - public SimpleDigest(int size, String str) { - init(size); - update(str); - } - - /** Inits the SimpleDigest */ - private void init(int size) { - is_done = false; - message_digest = new byte[size]; - for (int i = 0; i < size; i++) - message_digest[i] = (byte) (i); - index = 0; - add_term = 0; - } - - /** - * MessageDigest block update operation. Continues a message-digest - * operation, processing another message block, and updating the context. - */ - public MessageDigest update(byte[] buffer, int offset, int len) { - if (is_done) - return this; - // else - - for (int i = 0; i < len; i++) { - if (index == message_digest.length) - index = 0; - add_term += buffer[offset + i]; - message_digest[index] = (byte) (message_digest[index] ^ add_term); - index++; - } - return this; - } - - /** - * MessageDigest finalization. Ends a message-digest operation, writing the - * the message digest and zeroizing the context. - */ - public byte[] doFinal() { - if (is_done) - return message_digest; - // else - - int k = message_digest.length - index; - while (index < message_digest.length) { - message_digest[index] = (byte) (message_digest[index] ^ (k)); - index++; - k++; - } - for (int i = 0; i < message_digest.length; i++) - message_digest[i] = (byte) (message_digest[i] ^ add_term); - - return message_digest; - } - - /** Calculates the SimpleDigest. */ - public static byte[] digest(int size, byte[] buffer, int offset, int len) { - MessageDigest md = new SimpleDigest(size, buffer, offset, len); - return md.doFinal(); - } - - /** Calculates the SimpleDigest. */ - public static byte[] digest(int size, byte[] buffer) { - return digest(size, buffer, 0, buffer.length); - } - - /** Calculates the SimpleDigest. */ - public static byte[] digest(int size, String str) { - MessageDigest md = new SimpleDigest(size, str); - return md.doFinal(); - } - -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** + * Simple and fast hash/message-digest algorithm. + */ +public class SimpleDigest extends MessageDigest { + + // ************************* Attributes ************************* + + /** The digest */ + byte[] message_digest; + + /** Whether is finalized. */ + boolean is_done; + + /** Index within the message_digest. */ + int index; + + /** Add term */ + byte add_term; + + // *********************** Public methods *********************** + + /** Constructor */ + public SimpleDigest(int size) { + init(size); + } + + /** Constructor */ + public SimpleDigest(int size, byte[] buffer) { + init(size); + update(buffer); + } + + /** Constructor */ + public SimpleDigest(int size, byte[] buffer, int offset, int len) { + init(size); + update(buffer, offset, len); + } + + /** Constructor */ + public SimpleDigest(int size, String str) { + init(size); + update(str); + } + + /** Inits the SimpleDigest */ + private void init(int size) { + is_done = false; + message_digest = new byte[size]; + for (int i = 0; i < size; i++) + message_digest[i] = (byte) (i); + index = 0; + add_term = 0; + } + + /** + * MessageDigest block update operation. Continues a message-digest + * operation, processing another message block, and updating the context. + */ + public MessageDigest update(byte[] buffer, int offset, int len) { + if (is_done) + return this; + // else + + for (int i = 0; i < len; i++) { + if (index == message_digest.length) + index = 0; + add_term += buffer[offset + i]; + message_digest[index] = (byte) (message_digest[index] ^ add_term); + index++; + } + return this; + } + + /** + * MessageDigest finalization. Ends a message-digest operation, writing the + * the message digest and zeroizing the context. + */ + public byte[] doFinal() { + if (is_done) + return message_digest; + // else + + int k = message_digest.length - index; + while (index < message_digest.length) { + message_digest[index] = (byte) (message_digest[index] ^ (k)); + index++; + k++; + } + for (int i = 0; i < message_digest.length; i++) + message_digest[i] = (byte) (message_digest[i] ^ add_term); + + return message_digest; + } + + /** Calculates the SimpleDigest. */ + public static byte[] digest(int size, byte[] buffer, int offset, int len) { + MessageDigest md = new SimpleDigest(size, buffer, offset, len); + return md.doFinal(); + } + + /** Calculates the SimpleDigest. */ + public static byte[] digest(int size, byte[] buffer) { + return digest(size, buffer, 0, buffer.length); + } + + /** Calculates the SimpleDigest. */ + public static byte[] digest(int size, String str) { + MessageDigest md = new SimpleDigest(size, str); + return md.doFinal(); + } + +} diff --git a/src/org/zoolu/tools/Timer.java b/app/src/main/java/org/zoolu/tools/Timer.java similarity index 96% rename from src/org/zoolu/tools/Timer.java rename to app/src/main/java/org/zoolu/tools/Timer.java index c81abce..eca081d 100644 --- a/src/org/zoolu/tools/Timer.java +++ b/app/src/main/java/org/zoolu/tools/Timer.java @@ -1,131 +1,131 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) - */ - -package org.zoolu.tools; - -/** - * A Timer is a simple object that fires an onTimeout() method to its - * TimerListener when the time expires. A Timer have to be started and can be - * halted before expired. - */ -public class Timer implements InnerTimerListener { - /** Whether using single thread for all timer instances. */ - public static boolean SINGLE_THREAD = true; - - // HashSet listener_list=null; - TimerListener listener; - long time; - String label; - boolean active; - - void init(long t_msec, String t_label, TimerListener t_listener) { // listener_list=new - // HashSet(); - // if (t_listener!=null) addTimerListener(listener); - listener = t_listener; - time = t_msec; - label = t_label; - active = false; - } - - /** - * Creates a new Timer of msec milliseconds. The Timer is not - * started. You need to fire the start() method. - */ - /* - * public Timer(long t_msec) { init(t_msec,null,null); } - */ - - /** - * Creates a new Timer of msec milliseconds, with a label t_event. - * The Timer is not started. You need to fire the start() method. - */ - /* - * public Timer(long t_msec, String t_label) { init(t_msec,t_label,null); } - */ - - /** - * Creates a new Timer of msec milliseconds with TimerListener - * listener The Timer is not started. You need to fire the start() - * method. - */ - public Timer(long t_msec, TimerListener t_listener) { - init(t_msec, null, t_listener); - } - - /** - * Creates a new Timer of msec milliseconds, with a label t_event, - * and with TimerListener listener The Timer is not started. You - * need to fire the start() method. - */ - public Timer(long t_msec, String t_label, TimerListener t_listener) { - init(t_msec, t_label, t_listener); - } - - /** Gets the Timer label. */ - public String getLabel() { - return label; - } - - /** Gets the initial time (in milliseconds). */ - public long getTime() { - return time; - } - - /** Adds a new listener (TimerListener). */ - /* - * public void addTimerListener(TimerListener listener) { - * listener_list.add(listener); } - */ - - /** Removes the specific listener (TimerListener). */ - /* - * public void removeTimerListener(TimerListener listener) { - * listener_list.remove(listener); } - */ - - /** Stops the Timer. The onTimeout() method will not be fired. */ - public void halt() { - active = false; - // (CHANGE-040421) now it can free the link to Timer listeners - // listener_list=null; - listener = null; - } - - /** Starts the timer */ - public void start() { - active = true; - if (SINGLE_THREAD) - new InnerTimerST(time, this); - else - new InnerTimer(time, this); - } - - /** When the Timeout fires */ - public void onInnerTimeout() { - if (active && listener != null) - listener.onTimeout(this); - listener = null; - active = false; - } -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + * Nitin Khanna, Hughes Systique Corp. (Reason: Android specific change, optmization, bug fix) + */ + +package org.zoolu.tools; + +/** + * A Timer is a simple object that fires an onTimeout() method to its + * TimerListener when the time expires. A Timer have to be started and can be + * halted before expired. + */ +public class Timer implements InnerTimerListener { + /** Whether using single thread for all timer instances. */ + public static boolean SINGLE_THREAD = true; + + // HashSet listener_list=null; + TimerListener listener; + long time; + String label; + boolean active; + + void init(long t_msec, String t_label, TimerListener t_listener) { // listener_list=new + // HashSet(); + // if (t_listener!=null) addTimerListener(listener); + listener = t_listener; + time = t_msec; + label = t_label; + active = false; + } + + /** + * Creates a new Timer of msec milliseconds. The Timer is not + * started. You need to fire the start() method. + */ + /* + * public Timer(long t_msec) { init(t_msec,null,null); } + */ + + /** + * Creates a new Timer of msec milliseconds, with a label t_event. + * The Timer is not started. You need to fire the start() method. + */ + /* + * public Timer(long t_msec, String t_label) { init(t_msec,t_label,null); } + */ + + /** + * Creates a new Timer of msec milliseconds with TimerListener + * listener The Timer is not started. You need to fire the start() + * method. + */ + public Timer(long t_msec, TimerListener t_listener) { + init(t_msec, null, t_listener); + } + + /** + * Creates a new Timer of msec milliseconds, with a label t_event, + * and with TimerListener listener The Timer is not started. You + * need to fire the start() method. + */ + public Timer(long t_msec, String t_label, TimerListener t_listener) { + init(t_msec, t_label, t_listener); + } + + /** Gets the Timer label. */ + public String getLabel() { + return label; + } + + /** Gets the initial time (in milliseconds). */ + public long getTime() { + return time; + } + + /** Adds a new listener (TimerListener). */ + /* + * public void addTimerListener(TimerListener listener) { + * listener_list.add(listener); } + */ + + /** Removes the specific listener (TimerListener). */ + /* + * public void removeTimerListener(TimerListener listener) { + * listener_list.remove(listener); } + */ + + /** Stops the Timer. The onTimeout() method will not be fired. */ + public void halt() { + active = false; + // (CHANGE-040421) now it can free the link to Timer listeners + // listener_list=null; + listener = null; + } + + /** Starts the timer */ + public void start() { + active = true; + if (SINGLE_THREAD) + new InnerTimerST(time, this); + else + new InnerTimer(time, this); + } + + /** When the Timeout fires */ + public void onInnerTimeout() { + if (active && listener != null) + listener.onTimeout(this); + listener = null; + active = false; + } +} diff --git a/src/org/zoolu/tools/TimerListener.java b/app/src/main/java/org/zoolu/tools/TimerListener.java similarity index 97% rename from src/org/zoolu/tools/TimerListener.java rename to app/src/main/java/org/zoolu/tools/TimerListener.java index 091d3e8..1fc905f 100644 --- a/src/org/zoolu/tools/TimerListener.java +++ b/app/src/main/java/org/zoolu/tools/TimerListener.java @@ -1,30 +1,30 @@ -/* - * Copyright (C) 2005 Luca Veltri - University of Parma - Italy - * - * This file is part of MjSip (http://www.mjsip.org) - * - * MjSip is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MjSip 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 MjSip; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author(s): - * Luca Veltri (luca.veltri@unipr.it) - */ - -package org.zoolu.tools; - -/** Listens for a Timer events */ -public interface TimerListener { - /** When the Timer exceeds */ - public void onTimeout(Timer t); -} +/* + * Copyright (C) 2005 Luca Veltri - University of Parma - Italy + * + * This file is part of MjSip (http://www.mjsip.org) + * + * MjSip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MjSip 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 MjSip; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author(s): + * Luca Veltri (luca.veltri@unipr.it) + */ + +package org.zoolu.tools; + +/** Listens for a Timer events */ +public interface TimerListener { + /** When the Timer exceeds */ + public void onTimeout(Timer t); +} diff --git a/jni/Android.mk b/app/src/main/jni/Android.mk similarity index 100% rename from jni/Android.mk rename to app/src/main/jni/Android.mk diff --git a/jni/Application.mk b/app/src/main/jni/Application.mk similarity index 100% rename from jni/Application.mk rename to app/src/main/jni/Application.mk diff --git a/jni/OSNetworkSystem.cpp b/app/src/main/jni/OSNetworkSystem.cpp similarity index 97% rename from jni/OSNetworkSystem.cpp rename to app/src/main/jni/OSNetworkSystem.cpp index 4b60b95..788fdbb 100644 --- a/jni/OSNetworkSystem.cpp +++ b/app/src/main/jni/OSNetworkSystem.cpp @@ -1,3670 +1,3670 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * Copyright (C) 2007 The Android Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#define LOG_TAG "OSNetworkSystem" - -//#include "JNIHelp.h" -#include "jni.h" -#include "errno.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -//#include -//#include -//#include -//#include "AndroidSystemNatives.h" - -/** - * @name Socket Errors - * Error codes for socket operations - * - * @internal SOCKERR* range from -200 to -299 avoid overlap - */ -#define SOCKERR_BADSOCKET -200 /* generic error */ -#define SOCKERR_NOTINITIALIZED -201 /* socket library uninitialized */ -#define SOCKERR_BADAF -202 /* bad address family */ -#define SOCKERR_BADPROTO -203 /* bad protocol */ -#define SOCKERR_BADTYPE -204 /* bad type */ -#define SOCKERR_SYSTEMBUSY -205 /* system busy handling requests */ -#define SOCKERR_SYSTEMFULL -206 /* too many sockets */ -#define SOCKERR_NOTCONNECTED -207 /* socket is not connected */ -#define SOCKERR_INTERRUPTED -208 /* the call was cancelled */ -#define SOCKERR_TIMEOUT -209 /* the operation timed out */ -#define SOCKERR_CONNRESET -210 /* the connection was reset */ -#define SOCKERR_WOULDBLOCK -211 /* the socket is marked as nonblocking operation would block */ -#define SOCKERR_ADDRNOTAVAIL -212 /* address not available */ -#define SOCKERR_ADDRINUSE -213 /* address already in use */ -#define SOCKERR_NOTBOUND -214 /* the socket is not bound */ -#define SOCKERR_UNKNOWNSOCKET -215 /* resolution of fileDescriptor to socket failed */ -#define SOCKERR_INVALIDTIMEOUT -216 /* the specified timeout is invalid */ -#define SOCKERR_FDSETFULL -217 /* Unable to create an FDSET */ -#define SOCKERR_TIMEVALFULL -218 /* Unable to create a TIMEVAL */ -#define SOCKERR_REMSOCKSHUTDOWN -219 /* The remote socket has shutdown gracefully */ -#define SOCKERR_NOTLISTENING -220 /* listen() was not invoked prior to accept() */ -#define SOCKERR_NOTSTREAMSOCK -221 /* The socket does not support connection-oriented service */ -#define SOCKERR_ALREADYBOUND -222 /* The socket is already bound to an address */ -#define SOCKERR_NBWITHLINGER -223 /* The socket is marked non-blocking & SO_LINGER is non-zero */ -#define SOCKERR_ISCONNECTED -224 /* The socket is already connected */ -#define SOCKERR_NOBUFFERS -225 /* No buffer space is available */ -#define SOCKERR_HOSTNOTFOUND -226 /* Authoritative Answer Host not found */ -#define SOCKERR_NODATA -227 /* Valid name, no data record of requested type */ -#define SOCKERR_BOUNDORCONN -228 /* The socket has not been bound or is already connected */ -#define SOCKERR_OPNOTSUPP -229 /* The socket does not support the operation */ -#define SOCKERR_OPTUNSUPP -230 /* The socket option is not supported */ -#define SOCKERR_OPTARGSINVALID -231 /* The socket option arguments are invalid */ -#define SOCKERR_SOCKLEVELINVALID -232 /* The socket level is invalid */ -#define SOCKERR_TIMEOUTFAILURE -233 -#define SOCKERR_SOCKADDRALLOCFAIL -234 /* Unable to allocate the sockaddr structure */ -#define SOCKERR_FDSET_SIZEBAD -235 /* The calculated maximum size of the file descriptor set is bad */ -#define SOCKERR_UNKNOWNFLAG -236 /* The flag is unknown */ -#define SOCKERR_MSGSIZE -237 /* The datagram was too big to fit the specified buffer & was truncated. */ -#define SOCKERR_NORECOVERY -238 /* The operation failed with no recovery possible */ -#define SOCKERR_ARGSINVALID -239 /* The arguments are invalid */ -#define SOCKERR_BADDESC -240 /* The socket argument is not a valid file descriptor */ -#define SOCKERR_NOTSOCK -241 /* The socket argument is not a socket */ -#define SOCKERR_HOSTENTALLOCFAIL -242 /* Unable to allocate the hostent structure */ -#define SOCKERR_TIMEVALALLOCFAIL -243 /* Unable to allocate the timeval structure */ -#define SOCKERR_LINGERALLOCFAIL -244 /* Unable to allocate the linger structure */ -#define SOCKERR_IPMREQALLOCFAIL -245 /* Unable to allocate the ipmreq structure */ -#define SOCKERR_FDSETALLOCFAIL -246 /* Unable to allocate the fdset structure */ -#define SOCKERR_OPFAILED -247 /* Operation failed */ -#define SOCKERR_VALUE_NULL -248 /* The value indexed was NULL */ -#define SOCKERR_CONNECTION_REFUSED -249 /* connection was refused */ -#define SOCKERR_ENETUNREACH -250 /* network is not reachable */ -#define SOCKERR_EACCES -251 /* permissions do not allow action on socket */ -#define SOCKERR_EHOSTUNREACH -252 /* no route to host */ -#define SOCKERR_EPIPE -253 /* broken pipe */ - -#define JAVASOCKOPT_TCP_NODELAY 1 -#define JAVASOCKOPT_IP_TOS 3 -#define JAVASOCKOPT_SO_REUSEADDR 4 -#define JAVASOCKOPT_SO_KEEPALIVE 8 -#define JAVASOCKOPT_MCAST_TIME_TO_LIVE 10 /* Currently unused */ -#define JAVASOCKOPT_SO_BINDADDR 15 -#define JAVASOCKOPT_MCAST_INTERFACE 16 -#define JAVASOCKOPT_MCAST_TTL 17 -#define JAVASOCKOPT_IP_MULTICAST_LOOP 18 -#define JAVASOCKOPT_MCAST_ADD_MEMBERSHIP 19 -#define JAVASOCKOPT_MCAST_DROP_MEMBERSHIP 20 -#define JAVASOCKOPT_IP_MULTICAST_IF2 31 -#define JAVASOCKOPT_SO_BROADCAST 32 -#define JAVASOCKOPT_SO_LINGER 128 -#define JAVASOCKOPT_REUSEADDR_AND_REUSEPORT 10001 -#define JAVASOCKOPT_SO_SNDBUF 4097 -#define JAVASOCKOPT_SO_RCVBUF 4098 -#define JAVASOCKOPT_SO_RCVTIMEOUT 4102 -#define JAVASOCKOPT_SO_OOBINLINE 4099 - -/* constants for calling multi-call functions */ -#define SOCKET_STEP_START 10 -#define SOCKET_STEP_CHECK 20 -#define SOCKET_STEP_DONE 30 - -#define BROKEN_MULTICAST_IF 1 -#define BROKEN_MULTICAST_TTL 2 -#define BROKEN_TCP_NODELAY 4 - -#define SOCKET_CONNECT_STEP_START 0 -#define SOCKET_CONNECT_STEP_CHECK 1 - -#define SOCKET_OP_NONE 0 -#define SOCKET_OP_READ 1 -#define SOCKET_OP_WRITE 2 -#define SOCKET_READ_WRITE 3 - -#define SOCKET_MSG_PEEK 1 -#define SOCKET_MSG_OOB 2 - -#define SOCKET_NOFLAGS 0 - -#undef BUFFERSIZE -#define BUFFERSIZE 2048 - -// wait for 500000 usec = 0.5 second -#define SEND_RETRY_TIME 500000 - - -struct CachedFields { - jfieldID fd_descriptor; - jclass iaddr_class; - jmethodID iaddr_class_init; - jmethodID iaddr_getbyaddress; - jfieldID iaddr_ipaddress; - jclass genericipmreq_class; - jclass integer_class; - jmethodID integer_class_init; - jfieldID integer_class_value; - jclass boolean_class; - jmethodID boolean_class_init; - jfieldID boolean_class_value; - jclass byte_class; - jmethodID byte_class_init; - jfieldID byte_class_value; - jclass string_class; - jmethodID string_class_init; - jfieldID socketimpl_address; - jfieldID socketimpl_port; - jclass dpack_class; - jfieldID dpack_address; - jfieldID dpack_port; - jfieldID dpack_length; - jclass fd_class; - jfieldID descriptor; -} gCachedFields; - -static int useAdbNetworking = 0; - -/* needed for connecting with timeout */ -typedef struct selectFDSet { - int nfds; - int sock; - fd_set writeSet; - fd_set readSet; - fd_set exceptionSet; -} selectFDSet; - -static const char * netLookupErrorString(int anErrorNum); - -#define log_socket_close(a,b) -#define log_socket_connect(a,b,c) -#define add_send_stats(a,b) -#define add_recv_stats(a,b) -#define adb_networking_connect_fd(a,b) 0 -#define adb_networking_gethostbyname(a,b) 0 -#define PROPERTY_VALUE_MAX 1 -#define property_get(a,b,c) -#define assert(a) -/* - * Throw an exception with the specified class and an optional message. - */ -int jniThrowException(JNIEnv* env, const char* className, const char* msg) -{ - jclass exceptionClass; - - exceptionClass = env->FindClass(className); - if (exceptionClass == NULL) { -// LOGE("Unable to find exception class %s\n", className); - assert(0); /* fatal during dev; should always be fatal? */ - return -1; - } - - if (env->ThrowNew(exceptionClass, msg) != JNI_OK) { -// LOGE("Failed throwing '%s' '%s'\n", className, msg); - assert(!"failed to throw"); - } - return 0; -} - -/* - * Internal helper function. - * - * Get the file descriptor. - */ -static inline int getFd(JNIEnv* env, jobject obj) -{ - return env->GetIntField(obj, gCachedFields.descriptor); -} - -/* - * Internal helper function. - * - * Set the file descriptor. - */ -static inline void setFd(JNIEnv* env, jobject obj, jint value) -{ - env->SetIntField(obj, gCachedFields.descriptor, value); -} - -/* - * For JNIHelp.c - * Get an int file descriptor from a java.io.FileDescriptor - */ - -static int jniGetFDFromFileDescriptor (JNIEnv* env, jobject fileDescriptor) { - - return getFd(env, fileDescriptor); -} - -/* - * For JNIHelp.c - * Set the descriptor of a java.io.FileDescriptor - */ - -static void jniSetFileDescriptorOfFD (JNIEnv* env, jobject fileDescriptor, int value) { - - setFd(env, fileDescriptor, value); -} - -/** - * Throws an SocketException with the message affiliated with the errorCode. - */ -static void throwSocketException(JNIEnv *env, int errorCode) { - jniThrowException(env, "java/net/SocketException", - netLookupErrorString(errorCode)); -} - -/** - * Throws an IOException with the given message. - */ -static void throwIOExceptionStr(JNIEnv *env, const char *message) { - jniThrowException(env, "java/io/IOException", message); -} - -/** - * Throws a NullPointerException. - */ -static void throwNullPointerException(JNIEnv *env) { - jniThrowException(env, "java/lang/NullPointerException", NULL); -} - -/** - * Converts a 4-byte array to a native address structure. Throws a - * NullPointerException or an IOException in case of error. This is - * signaled by a return value of -1. The normal return value is 0. - */ -static int javaAddressToStructIn( - JNIEnv *env, jbyteArray java_address, struct in_addr *address) { - - memset(address, 0, sizeof(address)); - - if (java_address == NULL) { - return -1; - } - - if (env->GetArrayLength(java_address) != sizeof(address->s_addr)) { - return -1; - } - - jbyte * java_address_bytes - = env->GetByteArrayElements(java_address, NULL); - - memcpy(&(address->s_addr), - java_address_bytes, - sizeof(address->s_addr)); - - env->ReleaseByteArrayElements(java_address, java_address_bytes, JNI_ABORT); - - return 0; -} - -/** - * Converts a native address structure to a 4-byte array. Throws a - * NullPointerException or an IOException in case of error. This is - * signaled by a return value of -1. The normal return value is 0. - */ -static int structInToJavaAddress( - JNIEnv *env, struct in_addr *address, jbyteArray java_address) { - - if (java_address == NULL) { - return -1; - } - - if (env->GetArrayLength(java_address) != sizeof(address->s_addr)) { - return -1; - } - - jbyte *java_address_bytes; - - java_address_bytes = env->GetByteArrayElements(java_address, NULL); - - memcpy(java_address_bytes, &(address->s_addr), sizeof(address->s_addr)); - - env->ReleaseByteArrayElements(java_address, java_address_bytes, 0); - - return 0; -} - -/** - * Converts a native address structure to an InetAddress object. - * Throws a NullPointerException or an IOException in case of - * error. This is signaled by a return value of -1. The normal - * return value is 0. - */ -static int socketAddressToInetAddress(JNIEnv *env, - struct sockaddr_in *sockaddress, jobject inetaddress, int *port) { - - jbyteArray ipaddress; - int result; - - ipaddress = (jbyteArray)env->GetObjectField(inetaddress, - gCachedFields.iaddr_ipaddress); - - if (structInToJavaAddress(env, &sockaddress->sin_addr, ipaddress) < 0) { - return -1; - } - - *port = ntohs(sockaddress->sin_port); - - return 0; -} - -/** - * Converts an InetAddress object to a native address structure. - * Throws a NullPointerException or an IOException in case of - * error. This is signaled by a return value of -1. The normal - * return value is 0. - */ -static int inetAddressToSocketAddress(JNIEnv *env, - jobject inetaddress, int port, struct sockaddr_in *sockaddress) { - - jbyteArray ipaddress; - int result; - - ipaddress = (jbyteArray)env->GetObjectField(inetaddress, - gCachedFields.iaddr_ipaddress); - - memset(sockaddress, 0, sizeof(sockaddress)); - - sockaddress->sin_family = AF_INET; - sockaddress->sin_port = htons(port); - - if (javaAddressToStructIn(env, ipaddress, &(sockaddress->sin_addr)) < 0) { - return -1; - } - - return 0; -} - -static jobject structInToInetAddress(JNIEnv *env, struct in_addr *address) { - jbyteArray bytes; - int success; - - bytes = env->NewByteArray(4); - - if (bytes == NULL) { - return NULL; - } - - if (structInToJavaAddress(env, address, bytes) < 0) { - return NULL; - } - - return env->CallStaticObjectMethod(gCachedFields.iaddr_class, - gCachedFields.iaddr_getbyaddress, bytes); -} - -/** - * Answer a new java.lang.Boolean object. - * - * @param env pointer to the JNI library - * @param anInt the Boolean constructor argument - * - * @return the new Boolean - */ - -static jobject newJavaLangBoolean(JNIEnv * env, jint anInt) { - jclass tempClass; - jmethodID tempMethod; - - tempClass = gCachedFields.boolean_class; - tempMethod = gCachedFields.boolean_class_init; - return env->NewObject(tempClass, tempMethod, (jboolean) (anInt != 0)); -} - -/** - * Answer a new java.lang.Byte object. - * - * @param env pointer to the JNI library - * @param anInt the Byte constructor argument - * - * @return the new Byte - */ - -static jobject newJavaLangByte(JNIEnv * env, jbyte val) { - jclass tempClass; - jmethodID tempMethod; - - tempClass = gCachedFields.byte_class; - tempMethod = gCachedFields.byte_class_init; - return env->NewObject(tempClass, tempMethod, val); -} - -/** - * Answer a new java.lang.Integer object. - * - * @param env pointer to the JNI library - * @param anInt the Integer constructor argument - * - * @return the new Integer - */ - -static jobject newJavaLangInteger(JNIEnv * env, jint anInt) { - jclass tempClass; - jmethodID tempMethod; - - tempClass = gCachedFields.integer_class; - tempMethod = gCachedFields.integer_class_init; - return env->NewObject(tempClass, tempMethod, anInt); -} - -/** - * Answer a new java.lang.String object. - * - * @param env pointer to the JNI library - * @param anInt the byte[] constructor argument - * - * @return the new String - */ - -static jobject newJavaLangString(JNIEnv * env, jbyteArray bytes) { - jclass tempClass; - jmethodID tempMethod; - - tempClass = gCachedFields.string_class; - tempMethod = gCachedFields.string_class_init; - return env->NewObject(tempClass, tempMethod, (jbyteArray) bytes); -} - -/** - * Query OS for timestamp. - * Retrieve the current value of system clock and convert to milliseconds. - * - * @param[in] portLibrary The port library. - * - * @return 0 on failure, time value in milliseconds on success. - * @deprecated Use @ref time_hires_clock and @ref time_hires_delta - * - * technically, this should return I_64 since both timeval.tv_sec and - * timeval.tv_usec are long - */ - -static int time_msec_clock() { - struct timeval tp; - struct timezone tzp; - - gettimeofday(&tp, &tzp); - return (tp.tv_sec * 1000) + (tp.tv_usec / 1000); -} - -/** - * check if the passed sockaddr_in struct contains a localhost address - * - * @param[in] address pointer to the address to check - * - * @return 0 if the passed address isn't a localhost address - */ -static int isLocalhost(struct sockaddr_in *address) { - // return address == 127.0.0.1 - return (unsigned int) address->sin_addr.s_addr == 16777343; -} - -/** - * Answer the errorString corresponding to the errorNumber, if available. - * This function will answer a default error string, if the errorNumber is not - * recognized. - * - * This function will have to be reworked to handle internationalization - * properly, removing the explicit strings. - * - * @param anErrorNum the error code to resolve to a human readable string - * - * @return a human readable error string - */ - -static const char * netLookupErrorString(int anErrorNum) { - switch (anErrorNum) { - case SOCKERR_BADSOCKET: - return "Bad socket"; - case SOCKERR_NOTINITIALIZED: - return "Socket library uninitialized"; - case SOCKERR_BADAF: - return "Bad address family"; - case SOCKERR_BADPROTO: - return "Bad protocol"; - case SOCKERR_BADTYPE: - return "Bad type"; - case SOCKERR_SYSTEMBUSY: - return "System busy handling requests"; - case SOCKERR_SYSTEMFULL: - return "Too many sockets allocated"; - case SOCKERR_NOTCONNECTED: - return "Socket is not connected"; - case SOCKERR_INTERRUPTED: - return "The system call was cancelled"; - case SOCKERR_TIMEOUT: - return "The operation timed out"; - case SOCKERR_CONNRESET: - return "The connection was reset"; - case SOCKERR_WOULDBLOCK: - return "The nonblocking operation would block"; - case SOCKERR_ADDRNOTAVAIL: - return "The address is not available"; - case SOCKERR_ADDRINUSE: - return "The address is already in use"; - case SOCKERR_NOTBOUND: - return "The socket is not bound"; - case SOCKERR_UNKNOWNSOCKET: - return "Resolution of the FileDescriptor to socket failed"; - case SOCKERR_INVALIDTIMEOUT: - return "The specified timeout is invalid"; - case SOCKERR_FDSETFULL: - return "Unable to create an FDSET"; - case SOCKERR_TIMEVALFULL: - return "Unable to create a TIMEVAL"; - case SOCKERR_REMSOCKSHUTDOWN: - return "The remote socket has shutdown gracefully"; - case SOCKERR_NOTLISTENING: - return "Listen() was not invoked prior to accept()"; - case SOCKERR_NOTSTREAMSOCK: - return "The socket does not support connection-oriented service"; - case SOCKERR_ALREADYBOUND: - return "The socket is already bound to an address"; - case SOCKERR_NBWITHLINGER: - return "The socket is marked non-blocking & SO_LINGER is non-zero"; - case SOCKERR_ISCONNECTED: - return "The socket is already connected"; - case SOCKERR_NOBUFFERS: - return "No buffer space is available"; - case SOCKERR_HOSTNOTFOUND: - return "Authoritative Answer Host not found"; - case SOCKERR_NODATA: - return "Valid name, no data record of requested type"; - case SOCKERR_BOUNDORCONN: - return "The socket has not been bound or is already connected"; - case SOCKERR_OPNOTSUPP: - return "The socket does not support the operation"; - case SOCKERR_OPTUNSUPP: - return "The socket option is not supported"; - case SOCKERR_OPTARGSINVALID: - return "The socket option arguments are invalid"; - case SOCKERR_SOCKLEVELINVALID: - return "The socket level is invalid"; - case SOCKERR_TIMEOUTFAILURE: - return "The timeout operation failed"; - case SOCKERR_SOCKADDRALLOCFAIL: - return "Failed to allocate address structure"; - case SOCKERR_FDSET_SIZEBAD: - return "The calculated maximum size of the file descriptor set is bad"; - case SOCKERR_UNKNOWNFLAG: - return "The flag is unknown"; - case SOCKERR_MSGSIZE: - return "The datagram was too big to fit the specified buffer, so truncated"; - case SOCKERR_NORECOVERY: - return "The operation failed with no recovery possible"; - case SOCKERR_ARGSINVALID: - return "The arguments are invalid"; - case SOCKERR_BADDESC: - return "The socket argument is not a valid file descriptor"; - case SOCKERR_NOTSOCK: - return "The socket argument is not a socket"; - case SOCKERR_HOSTENTALLOCFAIL: - return "Unable to allocate the hostent structure"; - case SOCKERR_TIMEVALALLOCFAIL: - return "Unable to allocate the timeval structure"; - case SOCKERR_LINGERALLOCFAIL: - return "Unable to allocate the linger structure"; - case SOCKERR_IPMREQALLOCFAIL: - return "Unable to allocate the ipmreq structure"; - case SOCKERR_FDSETALLOCFAIL: - return "Unable to allocate the fdset structure"; - case SOCKERR_OPFAILED: - return "Operation failed"; - case SOCKERR_CONNECTION_REFUSED: - return "Connection refused"; - case SOCKERR_ENETUNREACH: - return "Network unreachable"; - case SOCKERR_EHOSTUNREACH: - return "No route to host"; - case SOCKERR_EPIPE: - return "Broken pipe"; - case SOCKERR_EACCES: - return "Permission denied (maybe missing INTERNET permission)"; - - default: -// LOGE("unknown socket error %d", anErrorNum); - return "unknown error"; - } -} - -static int convertError(int errorCode) { - switch (errorCode) { - case EBADF: - return SOCKERR_BADDESC; - case ENOBUFS: - return SOCKERR_NOBUFFERS; - case EOPNOTSUPP: - return SOCKERR_OPNOTSUPP; - case ENOPROTOOPT: - return SOCKERR_OPTUNSUPP; - case EINVAL: - return SOCKERR_SOCKLEVELINVALID; - case ENOTSOCK: - return SOCKERR_NOTSOCK; - case EINTR: - return SOCKERR_INTERRUPTED; - case ENOTCONN: - return SOCKERR_NOTCONNECTED; - case EAFNOSUPPORT: - return SOCKERR_BADAF; - /* note: CONNRESET not included because it has the same - * value as ECONNRESET and they both map to SOCKERR_CONNRESET */ - case ECONNRESET: - return SOCKERR_CONNRESET; - case EAGAIN: - return SOCKERR_WOULDBLOCK; - case EPROTONOSUPPORT: - return SOCKERR_BADPROTO; - case EFAULT: - return SOCKERR_ARGSINVALID; - case ETIMEDOUT: - return SOCKERR_TIMEOUT; - case ECONNREFUSED: - return SOCKERR_CONNECTION_REFUSED; - case ENETUNREACH: - return SOCKERR_ENETUNREACH; - case EACCES: - return SOCKERR_EACCES; - case EPIPE: - return SOCKERR_EPIPE; - case EHOSTUNREACH: - return SOCKERR_EHOSTUNREACH; - case EADDRINUSE: - return SOCKERR_ADDRINUSE; - case EADDRNOTAVAIL: - return SOCKERR_ADDRNOTAVAIL; - case EMSGSIZE: - return SOCKERR_MSGSIZE; - default: -// LOGE("unclassified errno %d (%s)", errorCode, strerror(errorCode)); - return SOCKERR_OPFAILED; - } -} - -static int sockSelect(int nfds, fd_set *readfds, fd_set *writefds, - fd_set *exceptfds, struct timeval *timeout) { - - int result = select(nfds, readfds, writefds, exceptfds, timeout); - - if (result < 0) { - if (errno == EINTR) { - result = SOCKERR_INTERRUPTED; - } else { - result = SOCKERR_OPFAILED; - } - } else if (result == 0) { - result = SOCKERR_TIMEOUT; - } - return result; -} - -#define SELECT_READ_TYPE 0 -#define SELECT_WRITE_TYPE 1 - -static int selectWait(int handle, int uSecTime, int type) { - fd_set fdset; - struct timeval time, *timePtr; - int result = 0; - int size = handle + 1; - - FD_ZERO(&fdset); - FD_SET(handle, &fdset); - - if (0 <= uSecTime) { - /* Use a timeout if uSecTime >= 0 */ - memset(&time, 0, sizeof(time)); - time.tv_usec = uSecTime; - timePtr = &time; - } else { - /* Infinite timeout if uSecTime < 0 */ - timePtr = NULL; - } - - if (type == SELECT_READ_TYPE) { - result = sockSelect(size, &fdset, NULL, NULL, timePtr); - } else { - result = sockSelect(size, NULL, &fdset, NULL, timePtr); - } - return result; -} - -static int pollSelectWait(JNIEnv *env, jobject fileDescriptor, int timeout, int type) { - /* now try reading the socket for the timespan timeout. - * if timeout is 0 try forever until the soclets gets ready or until an - * exception occurs. - */ - int pollTimeoutUSec = 100000, pollMsec = 100; - int finishTime = 0; - int timeLeft = timeout; - int hasTimeout = timeout > 0 ? 1 : 0; - int result = 0; - int handle; - - if (hasTimeout) { - finishTime = time_msec_clock() + timeout; - } - - int poll = 1; - - while (poll) { /* begin polling loop */ - - /* - * Fetch the handle every time in case the socket is closed. - */ - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_INTERRUPTED); - return -1; - } - - if (hasTimeout) { - - if (timeLeft - 10 < pollMsec) { - pollTimeoutUSec = timeLeft <= 0 ? 0 : (timeLeft * 1000); - } - - result = selectWait(handle, pollTimeoutUSec, type); - - /* - * because we are polling at a time smaller than timeout - * (presumably) lets treat an interrupt and timeout the same - go - * see if we're done timewise, and then just try again if not. - */ - if (SOCKERR_TIMEOUT == result || - SOCKERR_INTERRUPTED == result) { - - timeLeft = finishTime - time_msec_clock(); - - if (timeLeft <= 0) { - /* - * Always throw the "timeout" message because that is - * effectively what has happened, even if we happen to - * have been interrupted. - */ - jniThrowException(env, "java/net/SocketTimeoutException", - netLookupErrorString(SOCKERR_TIMEOUT)); - } else { - continue; // try again - } - - } else if (0 > result) { - log_socket_close(handle, result); - throwSocketException(env, result); - } - poll = 0; - - } else { /* polling with no timeout (why would you do this?)*/ - - result = selectWait(handle, pollTimeoutUSec, type); - - /* - * if interrupted (or a timeout) just retry - */ - if (SOCKERR_TIMEOUT == result || - SOCKERR_INTERRUPTED == result) { - - continue; // try again - } else if (0 > result) { - log_socket_close(handle, result); - throwSocketException(env, result); - } - poll = 0; - } - } /* end polling loop */ - - return result; -} - -/** - * A helper method, to set the connect context to a Long object. - * - * @param env pointer to the JNI library - * @param longclass Java Long Object - */ -void setConnectContext(JNIEnv *env,jobject longclass,jbyte * context) { - jclass descriptorCLS; - jfieldID descriptorFID; - descriptorCLS = env->FindClass("java/lang/Long"); - descriptorFID = env->GetFieldID(descriptorCLS, "value", "J"); - env->SetLongField(longclass, descriptorFID, (jlong)((jint)context)); -}; - -/** - * A helper method, to get the connect context. - * - * @param env pointer to the JNI library - * @param longclass Java Long Object - */ -jbyte *getConnectContext(JNIEnv *env, jobject longclass) { - jclass descriptorCLS; - jfieldID descriptorFID; - descriptorCLS = env->FindClass("java/lang/Long"); - descriptorFID = env->GetFieldID(descriptorCLS, "value", "J"); - return (jbyte*) ((jint)env->GetLongField(longclass, descriptorFID)); -}; - -// typical ip checksum -unsigned short ip_checksum(unsigned short* buffer, int size) { - register unsigned short * buf = buffer; - register int bufleft = size; - register unsigned long sum = 0; - - while (bufleft > 1) { - sum = sum + (*buf++); - bufleft = bufleft - sizeof(unsigned short ); - } - if (bufleft) { - sum = sum + (*(unsigned char*)buf); - } - sum = (sum >> 16) + (sum & 0xffff); - sum += (sum >> 16); - - return (unsigned short )(~sum); -} - -/** - * Establish a connection to a peer with a timeout. This function is called - * repeatedly in order to carry out the connect and to allow other tasks to - * proceed on certain platforms. The caller must first call with - * step = SOCKET_STEP_START, if the result is SOCKERR_NOTCONNECTED it will then - * call it with step = CHECK until either another error or 0 is returned to - * indicate the connect is complete. Each time the function should sleep for no - * more than timeout milliseconds. If the connect succeeds or an error occurs, - * the caller must always end the process by calling the function with - * step = SOCKET_STEP_DONE - * - * @param[in] portLibrary The port library. - * @param[in] sock pointer to the unconnected local socket. - * @param[in] addr pointer to the sockaddr, specifying remote host/port. - * @param[in] timeout the timeout in milliseconds. If timeout is negative, - * perform a block operation. - * @param[in,out] pointer to context pointer. Filled in on first call and then - * to be passed into each subsequent call. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -static int sockConnectWithTimeout(int handle, struct sockaddr_in addr, - unsigned int timeout, unsigned int step, jbyte *ctxt) { - int rc = 0; - struct timeval passedTimeout; - int errorVal; - socklen_t errorValLen = sizeof(int); - struct selectFDSet *context = NULL; - - if (SOCKET_STEP_START == step) { - - context = (struct selectFDSet *) ctxt; - - context->sock = handle; - context->nfds = handle + 1; - - if (useAdbNetworking && !isLocalhost(&addr)) { - - // LOGD("+connect to address 0x%08x (via adb)", - // addr.sin_addr.s_addr); - rc = adb_networking_connect_fd(handle, &addr); - // LOGD("-connect ret %d errno %d (via adb)", rc, errno); - - } else { - log_socket_connect(handle, ntohl(addr.sin_addr.s_addr), - ntohs(addr.sin_port)); - /* set the socket to non-blocking */ - int block = JNI_TRUE; - rc = ioctl(handle, FIONBIO, &block); - if (0 != rc) { - return convertError(rc); - } - - // LOGD("+connect to address 0x%08x (via normal) on handle %d", - // addr.sin_addr.s_addr, handle); - do { - rc = connect(handle, (struct sockaddr *) &addr, - sizeof(struct sockaddr)); - } while (rc < 0 && errno == EINTR); - // LOGD("-connect to address 0x%08x (via normal) returned %d", - // addr.sin_addr.s_addr, (int) rc); - - } - - if (rc == -1) { - rc = errno; - switch (rc) { - case EINTR: - return SOCKERR_ALREADYBOUND; - case EAGAIN: - case EINPROGRESS: - return SOCKERR_NOTCONNECTED; - default: - return convertError(rc); - } - } - - /* we connected right off the bat so just return */ - return rc; - - } else if (SOCKET_STEP_CHECK == step) { - /* now check if we have connected yet */ - - context = (struct selectFDSet *) ctxt; - - /* - * set the timeout value to be used. Because on some unix platforms we - * don't get notified when a socket is closed we only sleep for 100ms - * at a time - */ - passedTimeout.tv_sec = 0; - if (timeout > 100) { - passedTimeout.tv_usec = 100 * 1000; - } else if ((int)timeout >= 0) { - passedTimeout.tv_usec = timeout * 1000; - } - - /* initialize the FD sets for the select */ - FD_ZERO(&(context->exceptionSet)); - FD_ZERO(&(context->writeSet)); - FD_ZERO(&(context->readSet)); - FD_SET(context->sock, &(context->writeSet)); - FD_SET(context->sock, &(context->readSet)); - FD_SET(context->sock, &(context->exceptionSet)); - - rc = select(context->nfds, - &(context->readSet), - &(context->writeSet), - &(context->exceptionSet), - (int)timeout >= 0 ? &passedTimeout : NULL); - - /* if there is at least one descriptor ready to be checked */ - if (0 < rc) { - /* if the descriptor is in the write set we connected or failed */ - if (FD_ISSET(context->sock, &(context->writeSet))) { - - if (!FD_ISSET(context->sock, &(context->readSet))) { - /* ok we have connected ok */ - return 0; - } else { - /* ok we have more work to do to figure it out */ - if (getsockopt(context->sock, SOL_SOCKET, SO_ERROR, - &errorVal, &errorValLen) >= 0) { - return errorVal ? convertError(errorVal) : 0; - } else { - return convertError(errno); - } - } - } - - /* if the descriptor is in the exception set the connect failed */ - if (FD_ISSET(context->sock, &(context->exceptionSet))) { - if (getsockopt(context->sock, SOL_SOCKET, SO_ERROR, &errorVal, - &errorValLen) >= 0) { - return errorVal ? convertError(errorVal) : 0; - } - rc = errno; - return convertError(rc); - } - - } else if (rc < 0) { - /* something went wrong with the select call */ - rc = errno; - - /* if it was EINTR we can just try again. Return not connected */ - if (EINTR == rc) { - return SOCKERR_NOTCONNECTED; - } - - /* some other error occured so look it up and return */ - return convertError(rc); - } - - /* - * if we get here the timeout expired or the connect had not yet - * completed just indicate that the connect is not yet complete - */ - return SOCKERR_NOTCONNECTED; - } else if (SOCKET_STEP_DONE == step) { - /* we are done the connect or an error occured so clean up */ - if (handle != -1) { - int block = JNI_FALSE; - ioctl(handle, FIONBIO, &block); - } - return 0; - } - return SOCKERR_ARGSINVALID; -} - -/** - * Join/Leave the nominated multicast group on the specified socket. - * Implemented by setting the multicast 'add membership'/'drop membership' - * option at the HY_IPPROTO_IP level on the socket. - * - * Implementation note for multicast sockets in general: - * - * - This code is untested, because at the time of this writing multicast can't - * be properly tested on Android due to GSM routing restrictions. So it might - * or might not work. - * - * - The REUSEPORT socket option that Harmony employs is not supported on Linux - * and thus also not supported on Android. It's is not needed for multicast - * to work anyway (REUSEADDR should suffice). - * - * @param env pointer to the JNI library. - * @param socketP pointer to the hysocket to join/leave on. - * @param optVal pointer to the InetAddress, the multicast group to join/drop. - * - * @exception SocketException if an error occurs during the call - */ -static void mcastAddDropMembership (JNIEnv * env, int handle, jobject optVal, - int ignoreIF, int setSockOptVal) { - int result; - struct ip_mreq ipmreqP; - struct sockaddr_in sockaddrP; - int length = sizeof(struct ip_mreq); - socklen_t lengthIF = sizeof(struct sockaddr_in); - - /* - * JNI objects needed to access the information in the optVal oject - * passed in. The object passed in is a GenericIPMreq object - */ - jclass cls; - jfieldID multiaddrID; - jfieldID interfaceAddrID; - jobject multiaddr; - jobject interfaceAddr; - - /* - * check whether we are getting an InetAddress or an Generic IPMreq, for now - * we support both so that we will not break the tests - */ - if (env->IsInstanceOf (optVal, gCachedFields.iaddr_class)) { - - ipmreqP.imr_interface.s_addr = htonl(INADDR_ANY); - if (!ignoreIF) { - - result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, &sockaddrP, - &lengthIF); - - if (0 != result) { - throwSocketException (env, convertError(errno)); - return; - } - - memcpy(&(ipmreqP.imr_interface.s_addr), &(sockaddrP.sin_addr), 4); - } - - result = inetAddressToSocketAddress(env, optVal, 0, &sockaddrP); - - if (result < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - memcpy(&(ipmreqP.imr_multiaddr.s_addr), &(sockaddrP.sin_addr), 4); - - result = setsockopt(handle, IPPROTO_IP, setSockOptVal, &ipmreqP, length); - if (0 != result) { - throwSocketException (env, convertError(errno)); - return; - } - - } else { - - /* we need the multicast address regardless of the type of address */ - cls = env->GetObjectClass(optVal); - multiaddrID = env->GetFieldID(cls, "multiaddr", "Ljava/net/InetAddress;"); - multiaddr = env->GetObjectField(optVal, multiaddrID); - - result = inetAddressToSocketAddress(env, multiaddr, 0, &sockaddrP); - - if (result < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - memcpy(&(ipmreqP.imr_multiaddr.s_addr), &(sockaddrP.sin_addr), 4); - - /* we need to use an IP_MREQ as it is an IPV4 address */ - interfaceAddrID = env->GetFieldID(cls, "interfaceAddr", - "Ljava/net/InetAddress;"); - interfaceAddr = env->GetObjectField(optVal, interfaceAddrID); - - ipmreqP.imr_interface.s_addr = htonl(INADDR_ANY); - - /* - * if an interfaceAddr was passed then use that value, otherwise set the - * interface to all 0 to indicate the system should select the interface - * used - */ - if (!ignoreIF) { - if (NULL != interfaceAddr) { - - result = inetAddressToSocketAddress(env, interfaceAddr, 0, - &sockaddrP); - - if (result < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - memcpy(&(ipmreqP.imr_interface.s_addr), &(sockaddrP.sin_addr), 4); - - } - } - - /* join/drop the multicast address */ - result = setsockopt(handle, IPPROTO_IP, setSockOptVal, &ipmreqP, length); - if (0 != result) { - throwSocketException (env, convertError(errno)); - return; - } - } -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_oneTimeInitializationImpl(JNIEnv* env, jobject obj, - jboolean jcl_supports_ipv6) { - // LOGD("ENTER oneTimeInitializationImpl of OSNetworkSystem"); - - char useAdbNetworkingProperty[PROPERTY_VALUE_MAX]; - char adbConnectedProperty[PROPERTY_VALUE_MAX]; - - property_get("android.net.use-adb-networking", useAdbNetworkingProperty, ""); - property_get("adb.connected", adbConnectedProperty, ""); - - if (strlen((char *)useAdbNetworkingProperty) > 0 - && strlen((char *)adbConnectedProperty) > 0) { - useAdbNetworking = 1; - } - - memset(&gCachedFields, 0, sizeof(gCachedFields)); - - // initializing InetAddress - - jclass iaddrclass = env->FindClass("java/net/InetAddress"); - - if (iaddrclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.net.InetAddress"); - return; - } - - gCachedFields.iaddr_class = (jclass) env->NewGlobalRef(iaddrclass); - - jmethodID iaddrclassinit = env->GetMethodID(iaddrclass, "", "()V"); - - if (iaddrclassinit == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", "InetAddress.()"); - return; - } - - gCachedFields.iaddr_class_init = iaddrclassinit; - - jmethodID iaddrgetbyaddress = env->GetStaticMethodID(iaddrclass, - "getByAddress", "([B)Ljava/net/InetAddress;"); - - if (iaddrgetbyaddress == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", - "InetAddress.getByAddress(byte[] val)"); - return; - } - - gCachedFields.iaddr_getbyaddress = iaddrgetbyaddress; - - jfieldID iaddripaddress = env->GetFieldID(iaddrclass, "ipaddress", "[B"); - - if (iaddripaddress == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", - "Can't find field InetAddress.ipaddress"); - return; - } - - gCachedFields.iaddr_ipaddress = iaddripaddress; - - // get the GenericIPMreq class - - jclass genericipmreqclass = env->FindClass("org/apache/harmony/luni/net/GenericIPMreq"); - - if (genericipmreqclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "org.apache.harmony.luni.net.GenericIPMreq"); - return; - } - - gCachedFields.genericipmreq_class = (jclass) env->NewGlobalRef(genericipmreqclass); - - // initializing Integer - - jclass integerclass = env->FindClass("java/lang/Integer"); - - if (integerclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.lang.Integer"); - return; - } - - jmethodID integerclassinit = env->GetMethodID(integerclass, "", "(I)V"); - - if (integerclassinit == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", - "Integer.(int val)"); - return; - } - - jfieldID integerclassvalue = env->GetFieldID(integerclass, "value", "I"); - - if (integerclassvalue == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", "Integer.value"); - return; - } - - gCachedFields.integer_class = (jclass) env->NewGlobalRef(integerclass); - gCachedFields.integer_class_init = integerclassinit; - gCachedFields.integer_class_value = integerclassvalue; - - // initializing Boolean - - jclass booleanclass = env->FindClass("java/lang/Boolean"); - - if (booleanclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.lang.Boolean"); - return; - } - - jmethodID booleanclassinit = env->GetMethodID(booleanclass, "", "(Z)V"); - - if (booleanclassinit == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", - "Boolean.(boolean val)"); - return; - } - - jfieldID booleanclassvalue = env->GetFieldID(booleanclass, "value", "Z"); - - if (booleanclassvalue == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", "Boolean.value"); - return; - } - - gCachedFields.boolean_class = (jclass) env->NewGlobalRef(booleanclass); - gCachedFields.boolean_class_init = booleanclassinit; - gCachedFields.boolean_class_value = booleanclassvalue; - - // initializing Byte - - jclass byteclass = env->FindClass("java/lang/Byte"); - - if (byteclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.lang.Byte"); - return; - } - - jmethodID byteclassinit = env->GetMethodID(byteclass, "", "(B)V"); - - if (byteclassinit == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", - "Byte.(byte val)"); - return; - } - - jfieldID byteclassvalue = env->GetFieldID(byteclass, "value", "B"); - - if (byteclassvalue == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", "Byte.value"); - return; - } - - gCachedFields.byte_class = (jclass) env->NewGlobalRef(byteclass); - gCachedFields.byte_class_init = byteclassinit; - gCachedFields.byte_class_value = byteclassvalue; - - // initializing String - - jclass stringclass = env->FindClass("java/lang/String"); - - if (stringclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.lang.String"); - return; - } - - jmethodID stringclassinit = env->GetMethodID(stringclass, "", "([B)V"); - - if (stringclassinit == NULL) { - jniThrowException(env, "java/lang/NoSuchMethodError", - "String.(byte[] val)"); - return; - } - - gCachedFields.string_class = (jclass) env->NewGlobalRef(stringclass); - gCachedFields.string_class_init = stringclassinit; - - // initializing ScoketImpl - - jclass socketimplclass = env->FindClass("java/net/SocketImpl"); - - if (socketimplclass == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.net.SocketImpl"); - return; - } - - jfieldID socketimplport = env->GetFieldID(socketimplclass, "port", "I"); - - if (socketimplport == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", "SocketImpl.port"); - return; - } - - jfieldID socketimpladdress = env->GetFieldID(socketimplclass, "address", - "Ljava/net/InetAddress;"); - - if (socketimpladdress == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", - "SocketImpl.address"); - return; - } - - gCachedFields.socketimpl_address = socketimpladdress; - gCachedFields.socketimpl_port = socketimplport; - - gCachedFields.dpack_class = env->FindClass("java/net/DatagramPacket"); - if (gCachedFields.dpack_class == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.net.DatagramPacket"); - return; - } - - gCachedFields.dpack_address = env->GetFieldID(gCachedFields.dpack_class, - "address", "Ljava/net/InetAddress;"); - if (gCachedFields.dpack_address == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", - "DatagramPacket.address"); - return; - } - - gCachedFields.dpack_port = env->GetFieldID(gCachedFields.dpack_class, - "port", "I"); - if (gCachedFields.dpack_port == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", - "DatagramPacket.port"); - return; - } - - gCachedFields.dpack_length = env->GetFieldID(gCachedFields.dpack_class, - "length", "I"); - if (gCachedFields.dpack_length == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", - "DatagramPacket.length"); - return; - } - - gCachedFields.fd_class = env->FindClass("java/io/FileDescriptor"); - if (gCachedFields.fd_class == NULL) { - jniThrowException(env, "java/lang/ClassNotFoundException", - "java.io.FileDescriptor"); - return; - } - gCachedFields.descriptor = env->GetFieldID(gCachedFields.fd_class, "descriptor", "I"); - if (gCachedFields.descriptor == NULL) { - jniThrowException(env, "java/lang/NoSuchFieldError", - "FileDescriptor.descriptor"); - return; - } - -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jboolean preferIPv4Stack) { - // LOGD("ENTER createSocketImpl"); - - int ret = socket(PF_INET, SOCK_STREAM, 0); - - if (ret < 0) { - int err = convertError(errno); - throwSocketException(env, err); - return; - } - - jniSetFileDescriptorOfFD(env, fileDescriptor, ret); - - return; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createDatagramSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jboolean preferIPv4Stack) { - // LOGD("ENTER createDatagramSocketImpl"); - - int ret = socket(PF_INET, SOCK_DGRAM, 0); - - if (ret < 0) { - int err = convertError(errno); - throwSocketException(env, err); - return; - } - - jniSetFileDescriptorOfFD(env, fileDescriptor, ret); - - return; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_readSocketDirectImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint address, jint offset, jint count, - jint timeout) { - // LOGD("ENTER readSocketDirectImpl"); - - int handle; - jbyte *message = (jbyte *)address; - int result, ret, localCount; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - result = selectWait(handle, timeout, SELECT_READ_TYPE); - - if (0 > result) { - return 0; - } - - localCount = (count < 65536) ? count : 65536; - - do { - ret = recv(handle, (jbyte *) message, localCount, SOCKET_NOFLAGS); - } while (ret < 0 && errno == EINTR); - - if (0 == ret) { - return -1; - } else if (ret == -1) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - add_recv_stats(handle, ret); - return ret; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_readSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jbyteArray data, jint offset, jint count, - jint timeout) { - // LOGD("ENTER readSocketImpl"); - - jbyte *message; - int result, localCount; - - jbyte internalBuffer[BUFFERSIZE]; - - localCount = (count < 65536) ? count : 65536; - - if (localCount > BUFFERSIZE) { - message = (jbyte*)malloc(localCount * sizeof(jbyte)); - if (message == NULL) { - jniThrowException(env, "java/lang/OutOfMemoryError", - "couldn't allocate enough memory for readSocket"); - return 0; - } - } else { - message = (jbyte *)internalBuffer; - } - - result = Java_org_sipdroid_net_impl_OSNetworkSystem_readSocketDirectImpl(env, clazz, fileDescriptor, - (jint) message, offset, count, timeout); - - if (result > 0) { - env->SetByteArrayRegion(data, offset, result, (jbyte *)message); - } - - if (((jbyte *)message) != internalBuffer) { - free(( jbyte *)message); - } - - return result; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_writeSocketDirectImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint address, jint offset, jint count) { - // LOGD("ENTER writeSocketDirectImpl"); - - int handle; - jbyte *message = (jbyte *)address; - int result = 0, sent = 0; - - if (count <= 0) { - return 0; - } - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - result = send(handle, (jbyte *) message, (int) count, SOCKET_NOFLAGS); - if (result < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - - if (SOCKERR_WOULDBLOCK == err){ - jclass socketExClass,errorCodeExClass; - jmethodID errorCodeExConstructor, socketExConstructor,socketExCauseMethod; - jobject errorCodeEx, socketEx; - const char* errorMessage = netLookupErrorString(err); - jstring errorMessageString = env->NewStringUTF(errorMessage); - - errorCodeExClass = env->FindClass("org/apache/harmony/luni/util/ErrorCodeException"); - if (!errorCodeExClass){ - return 0; - } - errorCodeExConstructor = env->GetMethodID(errorCodeExClass,"","(I)V"); - if (!errorCodeExConstructor){ - return 0; - } - errorCodeEx = env->NewObject(errorCodeExClass,errorCodeExConstructor,err); - - socketExClass = env->FindClass("java/net/SocketException"); - if (!socketExClass) { - return 0; - } - socketExConstructor = env->GetMethodID(socketExClass,"","(Ljava/lang/String;)V"); - if (!socketExConstructor) { - return 0; - } - socketEx = env->NewObject(socketExClass, socketExConstructor, errorMessageString); - socketExCauseMethod = env->GetMethodID(socketExClass,"initCause","(Ljava/lang/Throwable;)Ljava/lang/Throwable;"); - env->CallObjectMethod(socketEx,socketExCauseMethod,errorCodeEx); - env->Throw((jthrowable)socketEx); - return 0; - } - throwSocketException(env, err); - return 0; - } - - add_send_stats(handle, result); - return result; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_writeSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jbyteArray data, jint offset, jint count) { - // LOGD("ENTER writeSocketImpl"); - - jbyte *message; - int sent = 0; - jint result = 0; - -/* TODO: ARRAY PINNING */ -#define INTERNAL_SEND_BUFFER_MAX 512 - jbyte internalBuffer[INTERNAL_SEND_BUFFER_MAX]; - - if (count > INTERNAL_SEND_BUFFER_MAX) { - message = (jbyte*)malloc(count * sizeof( jbyte)); - if (message == NULL) { - jniThrowException(env, "java/lang/OutOfMemoryError", - "couldn't allocate enough memory for writeSocket"); - return 0; - } - } else { - message = (jbyte *)internalBuffer; - } - - env->GetByteArrayRegion(data, offset, count, message); - - result = Java_org_sipdroid_net_impl_OSNetworkSystem_writeSocketDirectImpl(env, clazz, fileDescriptor, - (jint) message, offset, count); - - if (( jbyte *)message != internalBuffer) { - free(( jbyte *)message); - } -#undef INTERNAL_SEND_BUFFER_MAX - return result; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_setNonBlockingImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jboolean nonblocking) { - // LOGD("ENTER setNonBlockingImpl"); - - int handle; - int result; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - int block = nonblocking; - - result = ioctl(handle, FIONBIO, &block); - - if (result == -1) { - throwSocketException(env, convertError(errno)); - } -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint trafficClass, jobject inetAddr, jint port); - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_connectWithTimeoutSocketImpl(JNIEnv* env, - jclass clazz, jobject fileDescriptor, jint timeout, jint trafficClass, - jobject inetAddr, jint port, jint step, jbyteArray passContext) { - // LOGD("ENTER connectWithTimeoutSocketImpl"); - - int handle; - int result = 0; - struct sockaddr_in address; - jbyte *context = NULL; - - memset(&address, 0, sizeof(address)); - - address.sin_family = AF_INET; - - result = inetAddressToSocketAddress(env, inetAddr, port, - (struct sockaddr_in *) &address); - - if (result < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return result; - } - - // Check if we're using adb networking and redirect in case it is used. - if (useAdbNetworking && !isLocalhost(&address)) { - return Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(env, clazz, fileDescriptor, - trafficClass, inetAddr, port); - } - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return -1; - } - - address.sin_port = htons(port); - - context = (jbyte *)env->GetPrimitiveArrayCritical(passContext, NULL); - - switch (step) { - case SOCKET_CONNECT_STEP_START: - result = sockConnectWithTimeout(handle, address, 0, - SOCKET_STEP_START, context); - break; - case SOCKET_CONNECT_STEP_CHECK: - result = sockConnectWithTimeout(handle, address, timeout, - SOCKET_STEP_CHECK, context); - break; - } - - env->ReleasePrimitiveArrayCritical(passContext, context, JNI_ABORT); - - if (0 == result) { - /* connected , so stop here */ - sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, NULL); - } else if (result != SOCKERR_NOTCONNECTED) { - /* can not connect... */ - sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, NULL); - if (result == SOCKERR_EACCES) { - jniThrowException(env, "java/lang/SecurityException", - netLookupErrorString(result)); - } else { - jniThrowException(env, "java/net/ConnectException", - netLookupErrorString(result)); - } - } - - return result; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_connectStreamWithTimeoutSocketImpl(JNIEnv* env, - jclass clazz, jobject fileDescriptor, jint remotePort, jint timeout, - jint trafficClass, jobject inetAddr) { - // LOGD("ENTER connectStreamWithTimeoutSocketImpl"); - - int result = 0; - int handle; - struct sockaddr_in address; - jbyte *context = NULL; - int remainingTimeout = timeout; - int passedTimeout = 0; - int finishTime = 0; - int blocking = 0; - char hasTimeout = timeout > 0; - - /* if a timeout was specified calculate the finish time value */ - if (hasTimeout) { - finishTime = time_msec_clock() + (int) timeout; - } - - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } else { - result = inetAddressToSocketAddress(env, inetAddr, remotePort, - (struct sockaddr_in *) &address); - - if (result < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - // Check if we're using adb networking and redirect in case it is used. - if (useAdbNetworking && !isLocalhost(&address)) { - int retVal = Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(env, clazz, - fileDescriptor, trafficClass, inetAddr, remotePort); - if (retVal != 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - } - return; - } - - /* - * we will be looping checking for when we are connected so allocate - * the descriptor sets that we will use - */ - context =(jbyte *) malloc(sizeof(struct selectFDSet)); - - if (NULL == context) { - throwSocketException(env, SOCKERR_NOBUFFERS); - return; - } - - result = sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_START, context); - if (0 == result) { - /* ok we connected right away so we are done */ - sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, context); - goto bail; - } else if (result != SOCKERR_NOTCONNECTED) { - log_socket_close(handle, result); - sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, - context); - /* we got an error other than NOTCONNECTED so we cannot continue */ - if (SOCKERR_EACCES == result) { - jniThrowException(env, "java/lang/SecurityException", - netLookupErrorString(result)); - } else { - throwSocketException(env, result); - } - goto bail; - } - - while (SOCKERR_NOTCONNECTED == result) { - passedTimeout = remainingTimeout; - - /* - * ok now try and connect. Depending on the platform this may sleep - * for up to passedTimeout milliseconds - */ - result = sockConnectWithTimeout(handle, address, passedTimeout, - SOCKET_STEP_CHECK, context); - - /* - * now check if the socket is still connected. - * Do it here as some platforms seem to think they - * are connected if the socket is closed on them. - */ - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - sockConnectWithTimeout(handle, address, 0, - SOCKET_STEP_DONE, context); - throwSocketException(env, SOCKERR_BADSOCKET); - goto bail; - } - - /* - * check if we are now connected, - * if so we can finish the process and return - */ - if (0 == result) { - sockConnectWithTimeout(handle, address, 0, - SOCKET_STEP_DONE, context); - goto bail; - } - - /* - * if the error is SOCKERR_NOTCONNECTED then we have not yet - * connected and we may not be done yet - */ - if (SOCKERR_NOTCONNECTED == result) { - /* check if the timeout has expired */ - if (hasTimeout) { - remainingTimeout = finishTime - time_msec_clock(); - if (remainingTimeout <= 0) { - log_socket_close(handle, result); - sockConnectWithTimeout(handle, address, 0, - SOCKET_STEP_DONE, context); - jniThrowException(env, - "java/net/SocketTimeoutException", - netLookupErrorString(result)); - goto bail; - } - } else { - remainingTimeout = 100; - } - } else { - log_socket_close(handle, result); - sockConnectWithTimeout(handle, address, remainingTimeout, - SOCKET_STEP_DONE, context); - if ((SOCKERR_CONNRESET == result) || - (SOCKERR_CONNECTION_REFUSED == result) || - (SOCKERR_ADDRNOTAVAIL == result) || - (SOCKERR_ADDRINUSE == result) || - (SOCKERR_ENETUNREACH == result)) { - jniThrowException(env, "java/net/ConnectException", - netLookupErrorString(result)); - } else if (SOCKERR_EACCES == result) { - jniThrowException(env, "java/lang/SecurityException", - netLookupErrorString(result)); - } else { - throwSocketException(env, result); - } - goto bail; - } - } - } - -bail: - - /* free the memory for the FD set */ - if (context != NULL) { - free(context); - } -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint trafficClass, jobject inetAddr, jint port) { - //LOGD("ENTER direct-call connectSocketImpl\n"); - - struct sockaddr_in address; - int ret; - int handle; - jbyteArray java_in_addr; - - memset(&address, 0, sizeof(address)); - - address.sin_family = AF_INET; - - ret = inetAddressToSocketAddress(env, inetAddr, port, - (struct sockaddr_in *) &address); - - if (ret < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return ret; - } - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return -1; - } - - address.sin_port = htons(port); - - if (useAdbNetworking && !isLocalhost(&address)) { - - // LOGD("+connect to address 0x%08x port %d (via adb)", - // address.sin_addr.s_addr, (int) port); - ret = adb_networking_connect_fd(handle, &address); - // LOGD("-connect ret %d errno %d (via adb)", ret, errno); - - } else { - - // call this method with a timeout of zero - Java_org_sipdroid_net_impl_OSNetworkSystem_connectStreamWithTimeoutSocketImpl(env, clazz, - fileDescriptor, port, 0, trafficClass, inetAddr); - if (env->ExceptionOccurred() != 0) { - return -1; - } else { - return 0; - } - - } - - if (ret < 0) { - jniThrowException(env, "java/net/ConnectException", - netLookupErrorString(convertError(errno))); - return ret; - } - - return ret; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_socketBindImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint port, jobject inetAddress) { - // LOGD("ENTER socketBindImpl"); - - struct sockaddr_in sockaddress; - int ret; - int handle; - - ret = inetAddressToSocketAddress(env, inetAddress, port, - (struct sockaddr_in *) &sockaddress); - - if (ret < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - ret = bind(handle, (const sockaddr*)&sockaddress, sizeof(sockaddress)); - - if (ret < 0) { - jniThrowException(env, "java/net/BindException", - netLookupErrorString(convertError(errno))); - return; - } -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_listenStreamSocketImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint backlog) { - // LOGD("ENTER listenStreamSocketImpl"); - - int ret; - int handle; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - ret = listen(handle, backlog); - - if (ret < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return; - } -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_availableStreamImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor) { - // LOGD("ENTER availableStreamImpl"); - - int handle; - char message[BUFFERSIZE]; - - int result; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - do { - result = selectWait(handle, 1, SELECT_READ_TYPE); - - if (SOCKERR_TIMEOUT == result) { - // The read operation timed out, so answer 0 bytes available - return 0; - } else if (SOCKERR_INTERRUPTED == result) { - continue; - } else if (0 > result) { - log_socket_close(handle, result); - throwSocketException(env, result); - return 0; - } - } while (SOCKERR_INTERRUPTED == result); - - result = recv(handle, (jbyte *) message, BUFFERSIZE, MSG_PEEK); - - if (0 > result) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - add_recv_stats(handle, result); - return result; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_acceptSocketImpl(JNIEnv* env, jclass clazz, - jobject fdServer, jobject newSocket, jobject fdnewSocket, jint timeout) { - // LOGD("ENTER acceptSocketImpl"); - - union { - struct sockaddr address; - struct sockaddr_in in_address; - } sa; - - int ret; - int retFD; - int result; - int handle; - socklen_t addrlen; - - if (newSocket == NULL) { - throwNullPointerException(env); - return; - } - - result = pollSelectWait(env, fdServer, timeout, SELECT_READ_TYPE); - - if (0 > result) { - return; - } - - handle = jniGetFDFromFileDescriptor(env, fdServer); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - do { - addrlen = sizeof(sa); - ret = accept(handle, &(sa.address), &addrlen); - } while (ret < 0 && errno == EINTR); - - if (ret < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return; - } - - retFD = ret; - - /* For AF_INET / inetOrLocal == true only: put - * peer address and port in instance variables - * We don't bother for UNIX domain sockets, since most peers are - * anonymous anyway - */ - if (sa.address.sa_family == AF_INET) { - // inetOrLocal should also be true - - jobject inetAddress; - - inetAddress = structInToInetAddress(env, &(sa.in_address.sin_addr)); - - if (inetAddress == NULL) { - close(retFD); - newSocket = NULL; - return; - } - - env->SetObjectField(newSocket, - gCachedFields.socketimpl_address, inetAddress); - - env->SetIntField(newSocket, gCachedFields.socketimpl_port, - ntohs(sa.in_address.sin_port)); - } - - jniSetFileDescriptorOfFD(env, fdnewSocket, retFD); -} - -extern "C" jboolean Java_org_sipdroid_net_impl_OSNetworkSystem_supportsUrgentDataImpl(JNIEnv* env, - jclass clazz, jobject fileDescriptor) { - // LOGD("ENTER supportsUrgentDataImpl"); - - int handle; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - if (handle == 0 || handle == -1) { - return JNI_FALSE; - } - - return JNI_TRUE; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_sendUrgentDataImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jbyte value) { - // LOGD("ENTER sendUrgentDataImpl"); - - int handle; - int result; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - result = send(handle, (jbyte *) &value, 1, MSG_OOB); - if (result < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - } -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_connectDatagramImpl2(JNIEnv* env, jclass clazz, - jobject fd, jint port, jint trafficClass, jobject inetAddress) { - // LOGD("ENTER connectDatagramImpl2"); - - int handle = jniGetFDFromFileDescriptor(env, fd); - - struct sockaddr_in sockAddr; - int ret; - - ret = inetAddressToSocketAddress(env, inetAddress, port, &sockAddr); - - if (ret < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - log_socket_connect(handle, ntohl(sockAddr.sin_addr.s_addr), port); - int result = connect(handle, (struct sockaddr *)&sockAddr, sizeof(sockAddr)); - if (result < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - } -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_disconnectDatagramImpl(JNIEnv* env, jclass clazz, - jobject fd) { - // LOGD("ENTER disconnectDatagramImpl"); - - int handle = jniGetFDFromFileDescriptor(env, fd); - - struct sockaddr_in *sockAddr; - socklen_t sockAddrLen = sizeof(struct sockaddr_in); - sockAddr = (struct sockaddr_in *) malloc(sockAddrLen); - memset(sockAddr, 0, sockAddrLen); - - sockAddr->sin_family = AF_UNSPEC; - int result = connect(handle, (struct sockaddr *)sockAddr, sockAddrLen); - free(sockAddr); - - if (result < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - } -} - -extern "C" jboolean Java_org_sipdroid_net_impl_OSNetworkSystem_socketBindImpl2(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint port, jboolean bindToDevice, - jobject inetAddress) { - // LOGD("ENTER socketBindImpl2"); - - struct sockaddr_in sockaddress; - int ret; - int handle; - - ret = inetAddressToSocketAddress(env, inetAddress, port, - (struct sockaddr_in *) &sockaddress); - - if (ret < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - ret = bind(handle, (const sockaddr*)&sockaddress, sizeof(sockaddress)); - - if (ret < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - jniThrowException(env, "java/net/BindException", netLookupErrorString(err)); - return 0; - } - - return 0; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_peekDatagramImpl(JNIEnv* env, jclass clazz, - jobject fd, jobject sender, jint receiveTimeout) { - // LOGD("ENTER peekDatagramImpl"); - - int port = -1; - - int result = pollSelectWait (env, fd, receiveTimeout, SELECT_READ_TYPE); - if (0> result) { - return (jint) 0; - } - - int handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - struct sockaddr_in sockAddr; - socklen_t sockAddrLen = sizeof(sockAddr); - - int length = recvfrom(handle, NULL, 0, MSG_PEEK, - (struct sockaddr *)&sockAddr, &sockAddrLen); - - if (length < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - - if (socketAddressToInetAddress(env, &sockAddr, sender, &port) < 0) { - throwIOExceptionStr(env, "Address conversion failed"); - return -1; - } - add_recv_stats(handle, length); - return port; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_receiveDatagramDirectImpl(JNIEnv* env, jclass clazz, - jobject fd, jobject packet, jint address, jint offset, jint length, - jint receiveTimeout, jboolean peek) { - // LOGD("ENTER receiveDatagramDirectImpl"); - - int result = pollSelectWait (env, fd, receiveTimeout, SELECT_READ_TYPE); - if (0 > result) { - return (jint) 0; - } - - int handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - struct sockaddr_in sockAddr; - socklen_t sockAddrLen = sizeof(sockAddr); - - int mode = peek ? MSG_PEEK : 0; - - int actualLength = recvfrom(handle, (char*)(address + offset), length, mode, - (struct sockaddr *)&sockAddr, &sockAddrLen); - - if (actualLength < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - - if (packet != NULL) { - /* - int port = ntohs(sockAddr.sin_port); - jbyteArray addr = env->NewByteArray(sizeof(struct in_addr)); - if ((structInToJavaAddress(env, &sockAddr.sin_addr, addr)) < 0) { - jniThrowException(env, "java/net/SocketException", - "Could not set address of packet."); - return 0; - } - jobject sender = env->CallStaticObjectMethod( - gCachedFields.iaddr_class, gCachedFields.iaddr_getbyaddress, - addr); - env->SetObjectField(packet, gCachedFields.dpack_address, sender); - env->SetIntField(packet, gCachedFields.dpack_port, port); - */ - env->SetIntField(packet, gCachedFields.dpack_length, actualLength); - } - add_recv_stats(handle, actualLength); - return actualLength; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_receiveDatagramImpl(JNIEnv* env, jclass clazz, - jobject fd, jobject packet, jbyteArray data, jint offset, jint length, - jint receiveTimeout, jboolean peek) { - // LOGD("ENTER receiveDatagramImpl"); - - int localLength = (length < 65536) ? length : 65536; - jbyte *bytes = (jbyte*) malloc(localLength); - if (bytes == NULL) { - jniThrowException(env, "java/lang/OutOfMemoryError", - "couldn't allocate enough memory for receiveDatagram"); - return 0; - } - - int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_receiveDatagramDirectImpl(env, clazz, fd, - packet, (jint)bytes, offset, localLength, receiveTimeout, peek); - - if (actualLength > 0) { - env->SetByteArrayRegion(data, offset, actualLength, bytes); - } - free(bytes); - - return actualLength; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_recvConnectedDatagramDirectImpl(JNIEnv* env, - jclass clazz, jobject fd, jobject packet, jint address, jint offset, - jint length, jint receiveTimeout, jboolean peek) { - // LOGD("ENTER receiveConnectedDatagramDirectImpl"); - - int result = pollSelectWait (env, fd, receiveTimeout, SELECT_READ_TYPE); - - if (0 > result) { - return 0; - } - - int handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - int mode = peek ? MSG_PEEK : 0; - - int actualLength = recvfrom(handle, - (char*)(address + offset), length, mode, NULL, NULL); - - if (actualLength < 0) { - jniThrowException(env, "java/net/PortUnreachableException", ""); - return 0; - } - - if ( packet != NULL) { - env->SetIntField(packet, gCachedFields.dpack_length, actualLength); - } - add_recv_stats(handle, actualLength); - return actualLength; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_recvConnectedDatagramImpl(JNIEnv* env, jclass clazz, - jobject fd, jobject packet, jbyteArray data, jint offset, jint length, - jint receiveTimeout, jboolean peek) { - // LOGD("ENTER receiveConnectedDatagramImpl"); - - int localLength = (length < 65536) ? length : 65536; - jbyte *bytes = (jbyte*) malloc(localLength); - if (bytes == NULL) { - jniThrowException(env, "java/lang/OutOfMemoryError", - "couldn't allocate enough memory for recvConnectedDatagram"); - return 0; - } - - int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_recvConnectedDatagramDirectImpl(env, - clazz, fd, packet, (jint)bytes, offset, localLength, - receiveTimeout, peek); - - if (actualLength > 0) { - env->SetByteArrayRegion(data, offset, actualLength, bytes); - } - free(bytes); - - return actualLength; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramDirectImpl(JNIEnv* env, jclass clazz, - jobject fd, jint address, jint offset, jint length, jint port, - jboolean bindToDevice, jint trafficClass, jobject inetAddress) { - // LOGD("ENTER sendDatagramDirectImpl"); - - int result = 0; - - int handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - struct sockaddr_in receiver; - - if (inetAddressToSocketAddress(env, inetAddress, port, &receiver) < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - result = sendto(handle, (char*)(address + offset), length, SOCKET_NOFLAGS, - (struct sockaddr*)&receiver, sizeof(receiver)); - - if (result < 0) { - int err = convertError(errno); - if ((SOCKERR_CONNRESET == err) - || (SOCKERR_CONNECTION_REFUSED == err)) { - return 0; - } else { - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - } - add_send_stats(handle, result); - return result; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramImpl(JNIEnv* env, jclass clazz, - jobject fd, jbyteArray data, jint offset, jint length, jint port, - jboolean bindToDevice, jint trafficClass, jobject inetAddress) { - // LOGD("ENTER sendDatagramImpl"); - - jbyte *bytes = env->GetByteArrayElements(data, NULL); - int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramDirectImpl(env, clazz, fd, - (jint)bytes, offset, length, port, bindToDevice, trafficClass, - inetAddress); - env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); - - return actualLength; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendConnectedDatagramDirectImpl(JNIEnv* env, - jclass clazz, jobject fd, jint address, jint offset, jint length, - jboolean bindToDevice) { - // LOGD("ENTER sendConnectedDatagramDirectImpl"); - - int handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - int result = send(handle, (char*)(address + offset), length, 0); - - if (result < 0) { - int err = convertError(errno); - if ((SOCKERR_CONNRESET == err) || (SOCKERR_CONNECTION_REFUSED == err)) { - return 0; - } else { - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - } - add_send_stats(handle, length); - return result; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendConnectedDatagramImpl(JNIEnv* env, jclass clazz, - jobject fd, jbyteArray data, jint offset, jint length, - jboolean bindToDevice) { - // LOGD("ENTER sendConnectedDatagramImpl"); - - jbyte *bytes = env->GetByteArrayElements(data, NULL); - int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_sendConnectedDatagramDirectImpl(env, - clazz, fd, (jint)bytes, offset, length, bindToDevice); - env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); - - return actualLength; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createServerStreamSocketImpl(JNIEnv* env, - jclass clazz, jobject fileDescriptor, jboolean preferIPv4Stack) { - // LOGD("ENTER createServerStreamSocketImpl"); - - if (fileDescriptor == NULL) { - throwNullPointerException(env); - return; - } - - int handle = socket(PF_INET, SOCK_STREAM, 0); - - if (handle < 0) { - int err = convertError(errno); - throwSocketException(env, err); - return; - } - - jniSetFileDescriptorOfFD(env, fileDescriptor, handle); - - int value = 1; - - setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)); -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createMulticastSocketImpl(JNIEnv* env, - jclass clazz, jobject fileDescriptor, jboolean preferIPv4Stack) { - // LOGD("ENTER createMulticastSocketImpl"); - - int handle = socket(PF_INET, SOCK_DGRAM, 0); - - if (handle < 0) { - int err = convertError(errno); - throwSocketException(env, err); - return; - } - - jniSetFileDescriptorOfFD(env, fileDescriptor, handle); - - int value = 1; - - // setsockopt(handle, SOL_SOCKET, SO_REUSEPORT, &value, sizeof(jbyte)); - setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)); -} - -/* - * @param timeout in milliseconds. If zero, block until data received - */ -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_receiveStreamImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jbyteArray data, jint offset, jint count, - jint timeout) { - // LOGD("ENTER receiveStreamImpl"); - - int result; - int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - // Cap read length to available buf size - int spaceAvailable = env->GetArrayLength(data) - offset; - int localCount = count < spaceAvailable? count : spaceAvailable; - - jboolean isCopy; - jbyte *body = env->GetByteArrayElements(data, &isCopy); - - // set timeout - struct timeval tv; - tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; - setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, - sizeof(struct timeval)); - - do { - result = recv(handle, body + offset, localCount, SOCKET_NOFLAGS); - } while (result < 0 && errno == EINTR); - - env->ReleaseByteArrayElements(data, body, 0); - - /* - * If no bytes are read, return -1 to signal 'endOfFile' - * to the Java input stream - */ - if (0 < result) { - add_recv_stats(handle, result); - return result; - } else if (0 == result) { - return -1; - } else { - // If EAGAIN or EWOULDBLOCK, read timed out - if (errno == EAGAIN || errno == EWOULDBLOCK) { - jniThrowException(env, "java/net/SocketTimeoutException", - netLookupErrorString(SOCKERR_TIMEOUT)); - } else { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - } - return 0; - } -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendStreamImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jbyteArray data, jint offset, jint count) { - // LOGD("ENTER sendStreamImpl"); - - int handle = 0; - int result = 0, sent = 0; - - jboolean isCopy; - jbyte *message = env->GetByteArrayElements(data, &isCopy); - - // Cap write length to available buf size - int spaceAvailable = env->GetArrayLength(data) - offset; - if (count > spaceAvailable) count = spaceAvailable; - - while (sent < count) { - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - if (handle == 0 || handle == -1) { - throwSocketException(env, - sent == 0 ? SOCKERR_BADSOCKET : SOCKERR_INTERRUPTED); - env->ReleaseByteArrayElements(data, message, 0); - return 0; - } - - // LOGD("before select %d", count); - selectWait(handle, SEND_RETRY_TIME, SELECT_WRITE_TYPE); - result = send(handle, (jbyte *)message + offset + sent, - (int) count - sent, SOCKET_NOFLAGS); - - if (result < 0) { - result = errno; - if (result == EAGAIN ||result == EWOULDBLOCK) { - // LOGD("write blocked %d", sent); - continue; - } - env->ReleaseByteArrayElements(data, message, 0); - int err = convertError(result); - log_socket_close(handle, err); - throwSocketException(env, err); - return 0; - } - sent += result; - } - - env->ReleaseByteArrayElements(data, message, 0); - add_send_stats(handle, sent); - return sent; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_shutdownInputImpl(JNIEnv* env, jobject obj, - jobject fileDescriptor) { - // LOGD("ENTER shutdownInputImpl"); - - int ret; - int handle; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - ret = shutdown(handle, SHUT_RD); - - if (ret < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return; - } -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_shutdownOutputImpl(JNIEnv* env, jobject obj, - jobject fileDescriptor) { - // LOGD("ENTER shutdownOutputImpl"); - - int ret; - int handle; - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - return; - } - - ret = shutdown(handle, SHUT_WR); - - if (ret < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - return; - } -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramImpl2(JNIEnv* env, jclass clazz, - jobject fd, jbyteArray data, jint offset, jint length, jint port, - jobject inetAddress) { - // LOGD("ENTER sendDatagramImpl2"); - - jbyte *message; - jbyte nhostAddrBytes[4]; - unsigned short nPort; - int result = 0, sent = 0; - int handle = 0; - struct sockaddr_in sockaddrP; - - if (inetAddress != NULL) { - - result = inetAddressToSocketAddress(env, inetAddress, port, - (struct sockaddr_in *) &sockaddrP); - - if (result < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - - handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return 0; - } - } - - message = (jbyte*) malloc(length * sizeof(jbyte)); - if (message == NULL) { - jniThrowException(env, "java/lang/OutOfMemoryError", - "couldn't allocate enough memory for readSocket"); - return 0; - } - - env->GetByteArrayRegion(data, offset, length, message); - - while (sent < length) { - handle = jniGetFDFromFileDescriptor(env, fd); - - if (handle == 0 || handle == -1) { - throwSocketException(env, - sent == 0 ? SOCKERR_BADSOCKET : SOCKERR_INTERRUPTED); - free(message); - return 0; - } - - result = sendto(handle, (char *) (message + sent), - (int) (length - sent), SOCKET_NOFLAGS, - (struct sockaddr *) &sockaddrP, sizeof(sockaddrP)); - - if (result < 0) { - int err = convertError(errno); - log_socket_close(handle, err); - throwSocketException(env, err); - free(message); - return 0; - } - - sent += result; - } - - free(message); - add_send_stats(handle, sent); - return sent; -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_selectImpl(JNIEnv* env, jclass clazz, - jobjectArray readFDArray, jobjectArray writeFDArray, jint countReadC, - jint countWriteC, jintArray outFlags, jlong timeout) { - // LOGD("ENTER selectImpl"); - - struct timeval timeP; - int result = 0; - int size = 0; - jobject gotFD; - fd_set *fdset_read,*fdset_write; - int handle; - jboolean isCopy ; - jint *flagArray; - int val; - unsigned int time_sec = (unsigned int)timeout/1000; - unsigned int time_msec = (unsigned int)(timeout%1000); - - fdset_read = (fd_set *)malloc(sizeof(fd_set)); - fdset_write = (fd_set *)malloc(sizeof(fd_set)); - - FD_ZERO(fdset_read); - FD_ZERO(fdset_write); - - for (val = 0; valGetObjectArrayElement(readFDArray,val); - - handle = jniGetFDFromFileDescriptor(env, gotFD); - - FD_SET(handle, fdset_read); - - if (0 > (size - handle)) { - size = handle; - } - } - - for (val = 0; valGetObjectArrayElement(writeFDArray,val); - - handle = jniGetFDFromFileDescriptor(env, gotFD); - - FD_SET(handle, fdset_write); - - if (0 > (size - handle)) { - size = handle; - } - } - - /* the size is the max_fd + 1 */ - size =size + 1; - - if (0 > size) { - result = SOCKERR_FDSET_SIZEBAD; - } else { - /* only set when timeout >= 0 (non-block)*/ - if (0 <= timeout) { - - timeP.tv_sec = time_sec; - timeP.tv_usec = time_msec*1000; - - result = sockSelect(size, fdset_read, fdset_write, NULL, &timeP); - - } else { - result = sockSelect(size, fdset_read, fdset_write, NULL, NULL); - } - } - - if (0 < result) { - /*output the result to a int array*/ - flagArray = env->GetIntArrayElements(outFlags, &isCopy); - - for (val=0; valGetObjectArrayElement(readFDArray,val); - - handle = jniGetFDFromFileDescriptor(env, gotFD); - - if (FD_ISSET(handle,fdset_read)) { - flagArray[val] = SOCKET_OP_READ; - } else { - flagArray[val] = SOCKET_OP_NONE; - } - } - - for (val=0; valGetObjectArrayElement(writeFDArray,val); - - handle = jniGetFDFromFileDescriptor(env, gotFD); - - if (FD_ISSET(handle,fdset_write)) { - flagArray[val+countReadC] = SOCKET_OP_WRITE; - } else { - flagArray[val+countReadC] = SOCKET_OP_NONE; - } - } - - env->ReleaseIntArrayElements(outFlags, flagArray, 0); - } - - free(fdset_write); - free(fdset_read); - - /* return both correct and error result, let java handle the exception*/ - return result; -} - -extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketLocalAddressImpl(JNIEnv* env, - jclass clazz, jobject fileDescriptor, jboolean preferIPv6Addresses) { - // LOGD("ENTER getSocketLocalAddressImpl"); - - struct sockaddr_in addr; - socklen_t addrLen = sizeof(addr); - - memset(&addr, 0, addrLen); - - int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - int result; - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_UNKNOWNSOCKET); - return NULL; - } - - result = getsockname(handle, (struct sockaddr *)&addr, &addrLen); - - // Spec says ignore all errors - - return structInToInetAddress(env, &(addr.sin_addr)); - -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketLocalPortImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jboolean preferIPv6Addresses) { - // LOGD("ENTER getSocketLocalPortImpl"); - - struct sockaddr_in addr; - socklen_t addrLen = sizeof(addr); - - int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - int result; - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_UNKNOWNSOCKET); - return 0; - } - - result = getsockname(handle, (struct sockaddr *)&addr, &addrLen); - - if (0 != result) { - // The java spec does not indicate any exceptions on this call - return 0; - } else { - return ntohs(addr.sin_port); - } -} - -extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketOptionImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint anOption) { - // LOGD("ENTER getSocketOptionImpl"); - - int handle; - int intValue = 0; - socklen_t intSize = sizeof(int); - unsigned char byteValue = 0; - socklen_t byteSize = sizeof(unsigned char); - int result; - struct sockaddr_in sockVal; - socklen_t sockSize = sizeof(sockVal); - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return NULL; - } - - switch ((int) anOption & 0xffff) { - case JAVASOCKOPT_SO_LINGER: { - struct linger lingr; - socklen_t size = sizeof(struct linger); - result = getsockopt(handle, SOL_SOCKET, SO_LINGER, &lingr, &size); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - if (!lingr.l_onoff) { - intValue = -1; - } else { - intValue = lingr.l_linger; - } - return newJavaLangInteger(env, intValue); - } - case JAVASOCKOPT_TCP_NODELAY: { - if ((anOption >> 16) & BROKEN_TCP_NODELAY) { - return NULL; - } - result = getsockopt(handle, IPPROTO_TCP, TCP_NODELAY, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangBoolean(env, intValue); - } - case JAVASOCKOPT_MCAST_TTL: { - if ((anOption >> 16) & BROKEN_MULTICAST_TTL) { - return newJavaLangByte(env, 0); - } - result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_TTL, &byteValue, &byteSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangByte(env, (jbyte)(byteValue & 0xFF)); - } - case JAVASOCKOPT_MCAST_INTERFACE: { - if ((anOption >> 16) & BROKEN_MULTICAST_IF) { - return NULL; - } - result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, &sockVal, &sockSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return structInToInetAddress(env, &(sockVal.sin_addr)); - } - case JAVASOCKOPT_SO_SNDBUF: { - result = getsockopt(handle, SOL_SOCKET, SO_SNDBUF, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangInteger(env, intValue); - } - case JAVASOCKOPT_SO_RCVBUF: { - result = getsockopt(handle, SOL_SOCKET, SO_RCVBUF, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangInteger(env, intValue); - } - case JAVASOCKOPT_SO_BROADCAST: { - result = getsockopt(handle, SOL_SOCKET, SO_BROADCAST, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangBoolean(env, intValue); - } - case JAVASOCKOPT_SO_REUSEADDR: { - result = getsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangBoolean(env, intValue); - } - case JAVASOCKOPT_SO_KEEPALIVE: { - result = getsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangBoolean(env, intValue); - } - case JAVASOCKOPT_SO_OOBINLINE: { - result = getsockopt(handle, SOL_SOCKET, SO_OOBINLINE, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangBoolean(env, intValue); - } - case JAVASOCKOPT_IP_MULTICAST_LOOP: { - result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_LOOP, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangBoolean(env, intValue); - } - case JAVASOCKOPT_IP_TOS: { - result = getsockopt(handle, IPPROTO_IP, IP_TOS, &intValue, &intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangInteger(env, intValue); - } - case JAVASOCKOPT_SO_RCVTIMEOUT: { - struct timeval timeout; - socklen_t size = sizeof(timeout); - result = getsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, &timeout, &size); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return NULL; - } - return newJavaLangInteger(env, timeout.tv_sec * 1000 + timeout.tv_usec/1000); - } - default: { - throwSocketException(env, SOCKERR_OPTUNSUPP); - return NULL; - } - } - -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_setSocketOptionImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor, jint anOption, jobject optVal) { - // LOGD("ENTER setSocketOptionImpl"); - - int handle, result; - int intVal, intSize = sizeof(int); - unsigned char byteVal, byteSize = sizeof(unsigned char); - struct sockaddr_in sockVal; - int sockSize = sizeof(sockVal); - - if (env->IsInstanceOf(optVal, gCachedFields.integer_class)) { - intVal = (int) env->GetIntField(optVal, gCachedFields.integer_class_value); - } else if (env->IsInstanceOf(optVal, gCachedFields.boolean_class)) { - intVal = (int) env->GetBooleanField(optVal, gCachedFields.boolean_class_value); - } else if (env->IsInstanceOf(optVal, gCachedFields.byte_class)) { - byteVal = (int) env->GetByteField(optVal, gCachedFields.byte_class_value); - } else if (env->IsInstanceOf(optVal, gCachedFields.iaddr_class)) { - if (inetAddressToSocketAddress(env, optVal, 0, &sockVal) < 0) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - } else if (env->IsInstanceOf(optVal, gCachedFields.genericipmreq_class)) { - // we'll use optVal directly - } else { - throwSocketException(env, SOCKERR_OPTUNSUPP); - return; - } - - handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - switch ((int) anOption & 0xffff) { - case JAVASOCKOPT_SO_LINGER: { - struct linger lingr; - lingr.l_onoff = intVal > 0 ? 1 : 0; - lingr.l_linger = intVal; - result = setsockopt(handle, SOL_SOCKET, SO_LINGER, &lingr, - sizeof(struct linger)); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_TCP_NODELAY: { - if ((anOption >> 16) & BROKEN_TCP_NODELAY) { - return; - } - result = setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_MCAST_TTL: { - if ((anOption >> 16) & BROKEN_MULTICAST_TTL) { - return; - } - result = setsockopt(handle, IPPROTO_IP, IP_MULTICAST_TTL, &byteVal, byteSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_MCAST_ADD_MEMBERSHIP: { - mcastAddDropMembership(env, handle, optVal, - (anOption >> 16) & BROKEN_MULTICAST_IF, IP_ADD_MEMBERSHIP); - return; - } - - case JAVASOCKOPT_MCAST_DROP_MEMBERSHIP: { - mcastAddDropMembership(env, handle, optVal, - (anOption >> 16) & BROKEN_MULTICAST_IF, IP_DROP_MEMBERSHIP); - return; - } - - case JAVASOCKOPT_MCAST_INTERFACE: { - if ((anOption >> 16) & BROKEN_MULTICAST_IF) { - return; - } - result = setsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, &sockVal, sockSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_SO_SNDBUF: { - result = setsockopt(handle, SOL_SOCKET, SO_SNDBUF, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_SO_RCVBUF: { - result = setsockopt(handle, SOL_SOCKET, SO_RCVBUF, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_SO_BROADCAST: { - result = setsockopt(handle, SOL_SOCKET, SO_BROADCAST, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_SO_REUSEADDR: { - result = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - case JAVASOCKOPT_SO_KEEPALIVE: { - result = setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_SO_OOBINLINE: { - result = setsockopt(handle, SOL_SOCKET, SO_OOBINLINE, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_IP_MULTICAST_LOOP: { - result = setsockopt(handle, IPPROTO_IP, IP_MULTICAST_LOOP, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_IP_TOS: { - result = setsockopt(handle, IPPROTO_IP, IP_TOS, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_REUSEADDR_AND_REUSEPORT: { - // SO_REUSEPORT doesn't need to get set on this System - result = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &intVal, intSize); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - case JAVASOCKOPT_SO_RCVTIMEOUT: { - struct timeval timeout; - timeout.tv_sec = intVal / 1000; - timeout.tv_usec = (intVal % 1000) * 1000; - result = setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, &timeout, - sizeof(struct timeval)); - if (0 != result) { - throwSocketException(env, convertError(errno)); - return; - } - break; - } - - default: { - throwSocketException(env, SOCKERR_OPTUNSUPP); - } - } -} - -extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketFlagsImpl(JNIEnv* env, jclass clazz) { - // LOGD("ENTER getSocketFlagsImpl"); - - // Not implemented by harmony - return 0; -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_socketCloseImpl(JNIEnv* env, jclass clazz, - jobject fileDescriptor) { - // LOGD("ENTER socketCloseImpl"); - - int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); - - if (handle == 0 || handle == -1) { - throwSocketException(env, SOCKERR_BADSOCKET); - return; - } - - log_socket_close(handle, SOCKET_CLOSE_LOCAL); - - jniSetFileDescriptorOfFD(env, fileDescriptor, -1); - - close(handle); -} - -extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getHostByAddrImpl(JNIEnv* env, jclass clazz, - jbyteArray addrStr) { - // LOGD("ENTER getHostByAddrImpl"); - - if (addrStr == NULL) { - throwNullPointerException(env); - return JNI_FALSE; - } - - jstring address = (jstring)newJavaLangString(env, addrStr); - jstring result; - const char* addr = env->GetStringUTFChars(address, NULL); - - struct hostent* ent = gethostbyaddr(addr, strlen(addr), AF_INET); - - if (ent != NULL && ent->h_name != NULL) { - result = env->NewStringUTF(ent->h_name); - } else { - result = NULL; - } - - env->ReleaseStringUTFChars(address, addr); - - return result; -} - -extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getHostByNameImpl(JNIEnv* env, jclass clazz, - jstring nameStr, jboolean preferIPv6Addresses) { - // LOGD("ENTER getHostByNameImpl"); - - if (nameStr == NULL) { - throwNullPointerException(env); - return NULL; - } - - const char* name = env->GetStringUTFChars(nameStr, NULL); - - if (useAdbNetworking) { - - union { - struct in_addr a; - jbyte j[4]; - } outaddr; - - // LOGD("ADB networking: +gethostbyname '%s'", name); - int err; - err = adb_networking_gethostbyname(name, &(outaddr.a)); - - env->ReleaseStringUTFChars(nameStr, name); -#if 0 - LOGD("ADB networking: -gethostbyname err %d addr 0x%08x %u.%u.%u.%u", - err, (unsigned int)outaddr.a.s_addr, - outaddr.j[0],outaddr.j[1], - outaddr.j[2],outaddr.j[3]); -#endif - - if (err < 0) { - return NULL; - } else { - jbyteArray addr = env->NewByteArray(4); - env->SetByteArrayRegion(addr, 0, 4, outaddr.j); - return addr; - } - } else { - - // normal case...no adb networking - struct hostent* ent = gethostbyname(name); - - env->ReleaseStringUTFChars(nameStr, name); - - if (ent != NULL && ent->h_length > 0) { - jbyteArray addr = env->NewByteArray(4); - jbyte v[4]; - memcpy(v, ent->h_addr, 4); - env->SetByteArrayRegion(addr, 0, 4, v); - return addr; - } else { - return NULL; - } - } -} - -extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_setInetAddressImpl(JNIEnv* env, jobject obj, - jobject sender, jbyteArray address) { - // LOGD("ENTER setInetAddressImpl"); - - env->SetObjectField(sender, gCachedFields.iaddr_ipaddress, address); -} - -/* -extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_inheritedChannelImpl(JNIEnv* env, jobject obj) { - // LOGD("ENTER inheritedChannelImpl"); - - int socket = 0; - int opt; - socklen_t length = sizeof(opt); - int socket_type; - struct sockaddr_in local_addr; - struct sockaddr_in remote_addr; - jclass channel_class, socketaddr_class, serverSocket_class, socketImpl_class; - jobject channel_object = NULL, socketaddr_object, serverSocket_object; - jobject fd_object, addr_object, localAddr_object, socketImpl_object; - jfieldID port_field, socketaddr_field, bound_field, fd_field; - jfieldID serverSocket_field, socketImpl_field, addr_field, localAddr_field; - jmethodID channel_new; - jbyteArray addr_array; - struct sockaddr_in *sock; - jbyte * address; - jbyte * localAddr; - jboolean jtrue = JNI_TRUE; - - if (0 != getsockopt(socket, SOL_SOCKET, SO_TYPE, &opt, &length)) { - return NULL; - } - if (SOCK_STREAM !=opt && SOCK_DGRAM !=opt) { - return NULL; - } - socket_type = opt; - - length = sizeof(struct sockaddr); - if (0 != getsockname(socket, (struct sockaddr *)&local_addr, &length)) { - return NULL; - } else { - if (AF_INET != local_addr.sin_family || length != sizeof(struct sockaddr)) { - return NULL; - } - localAddr = (jbyte*) malloc(sizeof(jbyte)*4); - if (NULL == localAddr) { - return NULL; - } - memcpy (localAddr, &(local_addr.sin_addr.s_addr), 4); - } - if (0 != getpeername(socket, (struct sockaddr *)&remote_addr, &length)) { - remote_addr.sin_port = 0; - remote_addr.sin_addr.s_addr = 0; - address = (jbyte*) malloc(sizeof(jbyte)*4); - bzero(address, sizeof(jbyte)*4); - } else { - if (AF_INET != remote_addr.sin_family - || length != sizeof(struct sockaddr)) { - return NULL; - } - address = (jbyte*) malloc(sizeof(jbyte)*4); - memcpy (address, &(remote_addr.sin_addr.s_addr), 4); - } - - // analysis end, begin pack to java - if (SOCK_STREAM == opt) { - if (remote_addr.sin_port!=0) { - //socket - channel_class = env->FindClass( - "org/apache/harmony/nio/internal/SocketChannelImpl"); - if (NULL == channel_class) { - goto clean; - } - - channel_new = env->GetMethodID(channel_class, "", "()V"); - if (NULL == channel_new) { - goto clean; - } - channel_object = env->NewObject(channel_class, channel_new); - if (NULL == channel_object) { - goto clean; - } - // new and set FileDescript - - fd_field = env->GetFieldID(channel_class, "fd", - "java/io/FielDescriptor"); - fd_object = env->GetObjectField(channel_object, fd_field); - if (NULL == fd_object) { - goto clean; - } - - jniSetFileDescriptorOfFD(env, fd_object, socket); - - // local port - port_field = env->GetFieldID(channel_class, "localPort", "I"); - env->SetIntField(channel_object, port_field, - ntohs(local_addr.sin_port)); - - // new and set remote addr - addr_object = env->NewObject(gCachedFields.iaddr_class, - gCachedFields.iaddr_class_init); - if (NULL == addr_object) { - goto clean; - } - socketaddr_class = env->FindClass("java/net/InetSocketAddress"); - socketaddr_field = env->GetFieldID(channel_class, "connectAddress", - "Ljava/net/InetSocketAddress;"); - socketaddr_object = env->GetObjectField(channel_object, - socketaddr_field); - if (NULL == socketaddr_object) { - goto clean; - } - addr_field = env->GetFieldID(socketaddr_class, "addr", - "Ljava/net/InetAddress;"); - env->SetObjectField(socketaddr_object, addr_field, addr_object); - addr_array = env->NewByteArray((jsize)4); - env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, address); - env->SetObjectField(addr_object, gCachedFields.iaddr_ipaddress, - addr_array); - - // localAddr - socketaddr_class = env->FindClass("java/net/InetSocketAddress"); - socketaddr_field = env->GetFieldID(channel_class, "connectAddress", - "Ljava/net/InetSocketAddress;"); - socketaddr_object = env->GetObjectField(channel_object, - socketaddr_field); - - localAddr_field = env->GetFieldID(channel_class, "localAddress", - "Ljava/net/InetAddress;"); - localAddr_object = env->NewObject(gCachedFields.iaddr_class, - gCachedFields.iaddr_class_init); - jfieldID socketaddr_field = env->GetFieldID(channel_class, - "connectAddress", "Ljava/net/InetSocketAddress;"); - jobject socketaddr_object = env->GetObjectField(channel_object, - socketaddr_field); - env->SetObjectField(socketaddr_object, localAddr_field, - localAddr_object); - if (NULL == localAddr_object) { - goto clean; - } - addr_array = env->NewByteArray((jsize)4); - env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, localAddr); - env->SetObjectField(localAddr_object, gCachedFields.iaddr_ipaddress, - addr_array); - - - // set port - port_field = env->GetFieldID(socketaddr_class, "port", "I"); - env->SetIntField(socketaddr_object, port_field, - ntohs(remote_addr.sin_port)); - - // set bound - if (0 != local_addr.sin_port) { - bound_field = env->GetFieldID(channel_class, "isBound", "Z"); - env->SetBooleanField(channel_object, bound_field, jtrue); - } - - } else { - //serverSocket - channel_class = env->FindClass( - "org/apache/harmony/nio/internal/ServerSocketChannelImpl"); - if (NULL == channel_class) { - goto clean; - } - - channel_new = env->GetMethodID(channel_class, "", "()V"); - if (NULL == channel_new) { - goto clean; - } - channel_object = env->NewObject(channel_class, channel_new); - if (NULL == channel_object) { - goto clean; - } - - serverSocket_field = env->GetFieldID(channel_class, "socket", - "Ljava/net/ServerSocket;"); - serverSocket_class = env->FindClass("Ljava/net/ServerSocket;"); - serverSocket_object = env->GetObjectField(channel_object, - serverSocket_field); - // set bound - if (0 != local_addr.sin_port) { - bound_field = env->GetFieldID(channel_class, "isBound", "Z"); - env->SetBooleanField(channel_object, bound_field, jtrue); - bound_field = env->GetFieldID(serverSocket_class, "isBound", "Z"); - env->SetBooleanField(serverSocket_object, bound_field, jtrue); - } - // localAddr - socketImpl_class = env->FindClass("java/net/SocketImpl"); - socketImpl_field = env->GetFieldID(channel_class, "impl", - "Ljava/net/SocketImpl;"); - socketImpl_object = env->GetObjectField(channel_object, - socketImpl_field); - if (NULL == socketImpl_object) { - goto clean; - } - - localAddr_field = env->GetFieldID(channel_class, "localAddress", - "Ljava/net/InetAddress;"); - localAddr_object = env->NewObject(gCachedFields.iaddr_class, - gCachedFields.iaddr_class_init); - if (NULL == localAddr_object) { - goto clean; - } - env->SetObjectField(socketImpl_object, localAddr_field, - localAddr_object); - addr_array = env->NewByteArray((jsize)4); - env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, localAddr); - env->SetObjectField(localAddr_object, - gCachedFields.iaddr_ipaddress, addr_array); - - // set port - port_field = env->GetFieldID(socketImpl_class, "localport", "I"); - env->SetIntField(socketImpl_object, port_field, - ntohs(local_addr.sin_port)); - } - } else { - //Datagram Socket - // new DatagramChannel - channel_class = env->FindClass( - "org/apache/harmony/nio/internal/DatagramChannelImpl"); - if (NULL == channel_class) { - goto clean; - } - - channel_new = env->GetMethodID(channel_class, "", "()V"); - if (NULL == channel_new) { - goto clean; - } - channel_object = env->NewObject(channel_class, channel_new); - if (NULL == channel_object) { - goto clean; - } - - // new and set FileDescript - fd_field = env->GetFieldID(channel_class, "fd", "java/io/FileDescriptor"); - fd_object = env->GetObjectField(channel_object, fd_field); - if (NULL == fd_object) { - goto clean; - } - - jniSetFileDescriptorOfFD(env, fd_object, socket); - - port_field = env->GetFieldID(channel_class, "localPort", "I"); - env->SetIntField(channel_object, port_field, ntohs(local_addr.sin_port)); - - // new and set remote addr - addr_object = env->NewObject(gCachedFields.iaddr_class, - gCachedFields.iaddr_class_init); - if (NULL == addr_object) { - goto clean; - } - socketaddr_class = env->FindClass("java/net/InetSocketAddress"); - socketaddr_field = env->GetFieldID(channel_class, "connectAddress", - "Ljava/net/InetSocketAddress;"); - socketaddr_object = env->GetObjectField(channel_object, socketaddr_field); - if (NULL == socketaddr_object) { - goto clean; - } - addr_field = env->GetFieldID(socketaddr_class, "addr", - "Ljava/net/InetAddress;"); - env->SetObjectField(socketaddr_object, addr_field, addr_object); - addr_array = env->NewByteArray((jsize)4); - env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, address); - env->SetObjectField(addr_object, gCachedFields.iaddr_ipaddress, addr_array); - - // set bound - if (0 != local_addr.sin_port) { - bound_field = env->GetFieldID(channel_class, "isBound", "Z"); - env->SetBooleanField(channel_object, bound_field, jtrue); - } - } -clean: - free(address); - free(localAddr); - return channel_object; -} -*/ +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * Copyright (C) 2007 The Android Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define LOG_TAG "OSNetworkSystem" + +//#include "JNIHelp.h" +#include "jni.h" +#include "errno.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//#include +//#include +//#include +//#include "AndroidSystemNatives.h" + +/** + * @name Socket Errors + * Error codes for socket operations + * + * @internal SOCKERR* range from -200 to -299 avoid overlap + */ +#define SOCKERR_BADSOCKET -200 /* generic error */ +#define SOCKERR_NOTINITIALIZED -201 /* socket library uninitialized */ +#define SOCKERR_BADAF -202 /* bad address family */ +#define SOCKERR_BADPROTO -203 /* bad protocol */ +#define SOCKERR_BADTYPE -204 /* bad type */ +#define SOCKERR_SYSTEMBUSY -205 /* system busy handling requests */ +#define SOCKERR_SYSTEMFULL -206 /* too many sockets */ +#define SOCKERR_NOTCONNECTED -207 /* socket is not connected */ +#define SOCKERR_INTERRUPTED -208 /* the call was cancelled */ +#define SOCKERR_TIMEOUT -209 /* the operation timed out */ +#define SOCKERR_CONNRESET -210 /* the connection was reset */ +#define SOCKERR_WOULDBLOCK -211 /* the socket is marked as nonblocking operation would block */ +#define SOCKERR_ADDRNOTAVAIL -212 /* address not available */ +#define SOCKERR_ADDRINUSE -213 /* address already in use */ +#define SOCKERR_NOTBOUND -214 /* the socket is not bound */ +#define SOCKERR_UNKNOWNSOCKET -215 /* resolution of fileDescriptor to socket failed */ +#define SOCKERR_INVALIDTIMEOUT -216 /* the specified timeout is invalid */ +#define SOCKERR_FDSETFULL -217 /* Unable to create an FDSET */ +#define SOCKERR_TIMEVALFULL -218 /* Unable to create a TIMEVAL */ +#define SOCKERR_REMSOCKSHUTDOWN -219 /* The remote socket has shutdown gracefully */ +#define SOCKERR_NOTLISTENING -220 /* listen() was not invoked prior to accept() */ +#define SOCKERR_NOTSTREAMSOCK -221 /* The socket does not support connection-oriented service */ +#define SOCKERR_ALREADYBOUND -222 /* The socket is already bound to an address */ +#define SOCKERR_NBWITHLINGER -223 /* The socket is marked non-blocking & SO_LINGER is non-zero */ +#define SOCKERR_ISCONNECTED -224 /* The socket is already connected */ +#define SOCKERR_NOBUFFERS -225 /* No buffer space is available */ +#define SOCKERR_HOSTNOTFOUND -226 /* Authoritative Answer Host not found */ +#define SOCKERR_NODATA -227 /* Valid name, no data record of requested type */ +#define SOCKERR_BOUNDORCONN -228 /* The socket has not been bound or is already connected */ +#define SOCKERR_OPNOTSUPP -229 /* The socket does not support the operation */ +#define SOCKERR_OPTUNSUPP -230 /* The socket option is not supported */ +#define SOCKERR_OPTARGSINVALID -231 /* The socket option arguments are invalid */ +#define SOCKERR_SOCKLEVELINVALID -232 /* The socket level is invalid */ +#define SOCKERR_TIMEOUTFAILURE -233 +#define SOCKERR_SOCKADDRALLOCFAIL -234 /* Unable to allocate the sockaddr structure */ +#define SOCKERR_FDSET_SIZEBAD -235 /* The calculated maximum size of the file descriptor set is bad */ +#define SOCKERR_UNKNOWNFLAG -236 /* The flag is unknown */ +#define SOCKERR_MSGSIZE -237 /* The datagram was too big to fit the specified buffer & was truncated. */ +#define SOCKERR_NORECOVERY -238 /* The operation failed with no recovery possible */ +#define SOCKERR_ARGSINVALID -239 /* The arguments are invalid */ +#define SOCKERR_BADDESC -240 /* The socket argument is not a valid file descriptor */ +#define SOCKERR_NOTSOCK -241 /* The socket argument is not a socket */ +#define SOCKERR_HOSTENTALLOCFAIL -242 /* Unable to allocate the hostent structure */ +#define SOCKERR_TIMEVALALLOCFAIL -243 /* Unable to allocate the timeval structure */ +#define SOCKERR_LINGERALLOCFAIL -244 /* Unable to allocate the linger structure */ +#define SOCKERR_IPMREQALLOCFAIL -245 /* Unable to allocate the ipmreq structure */ +#define SOCKERR_FDSETALLOCFAIL -246 /* Unable to allocate the fdset structure */ +#define SOCKERR_OPFAILED -247 /* Operation failed */ +#define SOCKERR_VALUE_NULL -248 /* The value indexed was NULL */ +#define SOCKERR_CONNECTION_REFUSED -249 /* connection was refused */ +#define SOCKERR_ENETUNREACH -250 /* network is not reachable */ +#define SOCKERR_EACCES -251 /* permissions do not allow action on socket */ +#define SOCKERR_EHOSTUNREACH -252 /* no route to host */ +#define SOCKERR_EPIPE -253 /* broken pipe */ + +#define JAVASOCKOPT_TCP_NODELAY 1 +#define JAVASOCKOPT_IP_TOS 3 +#define JAVASOCKOPT_SO_REUSEADDR 4 +#define JAVASOCKOPT_SO_KEEPALIVE 8 +#define JAVASOCKOPT_MCAST_TIME_TO_LIVE 10 /* Currently unused */ +#define JAVASOCKOPT_SO_BINDADDR 15 +#define JAVASOCKOPT_MCAST_INTERFACE 16 +#define JAVASOCKOPT_MCAST_TTL 17 +#define JAVASOCKOPT_IP_MULTICAST_LOOP 18 +#define JAVASOCKOPT_MCAST_ADD_MEMBERSHIP 19 +#define JAVASOCKOPT_MCAST_DROP_MEMBERSHIP 20 +#define JAVASOCKOPT_IP_MULTICAST_IF2 31 +#define JAVASOCKOPT_SO_BROADCAST 32 +#define JAVASOCKOPT_SO_LINGER 128 +#define JAVASOCKOPT_REUSEADDR_AND_REUSEPORT 10001 +#define JAVASOCKOPT_SO_SNDBUF 4097 +#define JAVASOCKOPT_SO_RCVBUF 4098 +#define JAVASOCKOPT_SO_RCVTIMEOUT 4102 +#define JAVASOCKOPT_SO_OOBINLINE 4099 + +/* constants for calling multi-call functions */ +#define SOCKET_STEP_START 10 +#define SOCKET_STEP_CHECK 20 +#define SOCKET_STEP_DONE 30 + +#define BROKEN_MULTICAST_IF 1 +#define BROKEN_MULTICAST_TTL 2 +#define BROKEN_TCP_NODELAY 4 + +#define SOCKET_CONNECT_STEP_START 0 +#define SOCKET_CONNECT_STEP_CHECK 1 + +#define SOCKET_OP_NONE 0 +#define SOCKET_OP_READ 1 +#define SOCKET_OP_WRITE 2 +#define SOCKET_READ_WRITE 3 + +#define SOCKET_MSG_PEEK 1 +#define SOCKET_MSG_OOB 2 + +#define SOCKET_NOFLAGS 0 + +#undef BUFFERSIZE +#define BUFFERSIZE 2048 + +// wait for 500000 usec = 0.5 second +#define SEND_RETRY_TIME 500000 + + +struct CachedFields { + jfieldID fd_descriptor; + jclass iaddr_class; + jmethodID iaddr_class_init; + jmethodID iaddr_getbyaddress; + jfieldID iaddr_ipaddress; + jclass genericipmreq_class; + jclass integer_class; + jmethodID integer_class_init; + jfieldID integer_class_value; + jclass boolean_class; + jmethodID boolean_class_init; + jfieldID boolean_class_value; + jclass byte_class; + jmethodID byte_class_init; + jfieldID byte_class_value; + jclass string_class; + jmethodID string_class_init; + jfieldID socketimpl_address; + jfieldID socketimpl_port; + jclass dpack_class; + jfieldID dpack_address; + jfieldID dpack_port; + jfieldID dpack_length; + jclass fd_class; + jfieldID descriptor; +} gCachedFields; + +static int useAdbNetworking = 0; + +/* needed for connecting with timeout */ +typedef struct selectFDSet { + int nfds; + int sock; + fd_set writeSet; + fd_set readSet; + fd_set exceptionSet; +} selectFDSet; + +static const char * netLookupErrorString(int anErrorNum); + +#define log_socket_close(a,b) +#define log_socket_connect(a,b,c) +#define add_send_stats(a,b) +#define add_recv_stats(a,b) +#define adb_networking_connect_fd(a,b) 0 +#define adb_networking_gethostbyname(a,b) 0 +#define PROPERTY_VALUE_MAX 1 +#define property_get(a,b,c) +#define assert(a) +/* + * Throw an exception with the specified class and an optional message. + */ +int jniThrowException(JNIEnv* env, const char* className, const char* msg) +{ + jclass exceptionClass; + + exceptionClass = env->FindClass(className); + if (exceptionClass == NULL) { +// LOGE("Unable to find exception class %s\n", className); + assert(0); /* fatal during dev; should always be fatal? */ + return -1; + } + + if (env->ThrowNew(exceptionClass, msg) != JNI_OK) { +// LOGE("Failed throwing '%s' '%s'\n", className, msg); + assert(!"failed to throw"); + } + return 0; +} + +/* + * Internal helper function. + * + * Get the file descriptor. + */ +static inline int getFd(JNIEnv* env, jobject obj) +{ + return env->GetIntField(obj, gCachedFields.descriptor); +} + +/* + * Internal helper function. + * + * Set the file descriptor. + */ +static inline void setFd(JNIEnv* env, jobject obj, jint value) +{ + env->SetIntField(obj, gCachedFields.descriptor, value); +} + +/* + * For JNIHelp.c + * Get an int file descriptor from a java.io.FileDescriptor + */ + +static int jniGetFDFromFileDescriptor (JNIEnv* env, jobject fileDescriptor) { + + return getFd(env, fileDescriptor); +} + +/* + * For JNIHelp.c + * Set the descriptor of a java.io.FileDescriptor + */ + +static void jniSetFileDescriptorOfFD (JNIEnv* env, jobject fileDescriptor, int value) { + + setFd(env, fileDescriptor, value); +} + +/** + * Throws an SocketException with the message affiliated with the errorCode. + */ +static void throwSocketException(JNIEnv *env, int errorCode) { + jniThrowException(env, "java/net/SocketException", + netLookupErrorString(errorCode)); +} + +/** + * Throws an IOException with the given message. + */ +static void throwIOExceptionStr(JNIEnv *env, const char *message) { + jniThrowException(env, "java/io/IOException", message); +} + +/** + * Throws a NullPointerException. + */ +static void throwNullPointerException(JNIEnv *env) { + jniThrowException(env, "java/lang/NullPointerException", NULL); +} + +/** + * Converts a 4-byte array to a native address structure. Throws a + * NullPointerException or an IOException in case of error. This is + * signaled by a return value of -1. The normal return value is 0. + */ +static int javaAddressToStructIn( + JNIEnv *env, jbyteArray java_address, struct in_addr *address) { + + memset(address, 0, sizeof(address)); + + if (java_address == NULL) { + return -1; + } + + if (env->GetArrayLength(java_address) != sizeof(address->s_addr)) { + return -1; + } + + jbyte * java_address_bytes + = env->GetByteArrayElements(java_address, NULL); + + memcpy(&(address->s_addr), + java_address_bytes, + sizeof(address->s_addr)); + + env->ReleaseByteArrayElements(java_address, java_address_bytes, JNI_ABORT); + + return 0; +} + +/** + * Converts a native address structure to a 4-byte array. Throws a + * NullPointerException or an IOException in case of error. This is + * signaled by a return value of -1. The normal return value is 0. + */ +static int structInToJavaAddress( + JNIEnv *env, struct in_addr *address, jbyteArray java_address) { + + if (java_address == NULL) { + return -1; + } + + if (env->GetArrayLength(java_address) != sizeof(address->s_addr)) { + return -1; + } + + jbyte *java_address_bytes; + + java_address_bytes = env->GetByteArrayElements(java_address, NULL); + + memcpy(java_address_bytes, &(address->s_addr), sizeof(address->s_addr)); + + env->ReleaseByteArrayElements(java_address, java_address_bytes, 0); + + return 0; +} + +/** + * Converts a native address structure to an InetAddress object. + * Throws a NullPointerException or an IOException in case of + * error. This is signaled by a return value of -1. The normal + * return value is 0. + */ +static int socketAddressToInetAddress(JNIEnv *env, + struct sockaddr_in *sockaddress, jobject inetaddress, int *port) { + + jbyteArray ipaddress; + int result; + + ipaddress = (jbyteArray)env->GetObjectField(inetaddress, + gCachedFields.iaddr_ipaddress); + + if (structInToJavaAddress(env, &sockaddress->sin_addr, ipaddress) < 0) { + return -1; + } + + *port = ntohs(sockaddress->sin_port); + + return 0; +} + +/** + * Converts an InetAddress object to a native address structure. + * Throws a NullPointerException or an IOException in case of + * error. This is signaled by a return value of -1. The normal + * return value is 0. + */ +static int inetAddressToSocketAddress(JNIEnv *env, + jobject inetaddress, int port, struct sockaddr_in *sockaddress) { + + jbyteArray ipaddress; + int result; + + ipaddress = (jbyteArray)env->GetObjectField(inetaddress, + gCachedFields.iaddr_ipaddress); + + memset(sockaddress, 0, sizeof(sockaddress)); + + sockaddress->sin_family = AF_INET; + sockaddress->sin_port = htons(port); + + if (javaAddressToStructIn(env, ipaddress, &(sockaddress->sin_addr)) < 0) { + return -1; + } + + return 0; +} + +static jobject structInToInetAddress(JNIEnv *env, struct in_addr *address) { + jbyteArray bytes; + int success; + + bytes = env->NewByteArray(4); + + if (bytes == NULL) { + return NULL; + } + + if (structInToJavaAddress(env, address, bytes) < 0) { + return NULL; + } + + return env->CallStaticObjectMethod(gCachedFields.iaddr_class, + gCachedFields.iaddr_getbyaddress, bytes); +} + +/** + * Answer a new java.lang.Boolean object. + * + * @param env pointer to the JNI library + * @param anInt the Boolean constructor argument + * + * @return the new Boolean + */ + +static jobject newJavaLangBoolean(JNIEnv * env, jint anInt) { + jclass tempClass; + jmethodID tempMethod; + + tempClass = gCachedFields.boolean_class; + tempMethod = gCachedFields.boolean_class_init; + return env->NewObject(tempClass, tempMethod, (jboolean) (anInt != 0)); +} + +/** + * Answer a new java.lang.Byte object. + * + * @param env pointer to the JNI library + * @param anInt the Byte constructor argument + * + * @return the new Byte + */ + +static jobject newJavaLangByte(JNIEnv * env, jbyte val) { + jclass tempClass; + jmethodID tempMethod; + + tempClass = gCachedFields.byte_class; + tempMethod = gCachedFields.byte_class_init; + return env->NewObject(tempClass, tempMethod, val); +} + +/** + * Answer a new java.lang.Integer object. + * + * @param env pointer to the JNI library + * @param anInt the Integer constructor argument + * + * @return the new Integer + */ + +static jobject newJavaLangInteger(JNIEnv * env, jint anInt) { + jclass tempClass; + jmethodID tempMethod; + + tempClass = gCachedFields.integer_class; + tempMethod = gCachedFields.integer_class_init; + return env->NewObject(tempClass, tempMethod, anInt); +} + +/** + * Answer a new java.lang.String object. + * + * @param env pointer to the JNI library + * @param anInt the byte[] constructor argument + * + * @return the new String + */ + +static jobject newJavaLangString(JNIEnv * env, jbyteArray bytes) { + jclass tempClass; + jmethodID tempMethod; + + tempClass = gCachedFields.string_class; + tempMethod = gCachedFields.string_class_init; + return env->NewObject(tempClass, tempMethod, (jbyteArray) bytes); +} + +/** + * Query OS for timestamp. + * Retrieve the current value of system clock and convert to milliseconds. + * + * @param[in] portLibrary The port library. + * + * @return 0 on failure, time value in milliseconds on success. + * @deprecated Use @ref time_hires_clock and @ref time_hires_delta + * + * technically, this should return I_64 since both timeval.tv_sec and + * timeval.tv_usec are long + */ + +static int time_msec_clock() { + struct timeval tp; + struct timezone tzp; + + gettimeofday(&tp, &tzp); + return (tp.tv_sec * 1000) + (tp.tv_usec / 1000); +} + +/** + * check if the passed sockaddr_in struct contains a localhost address + * + * @param[in] address pointer to the address to check + * + * @return 0 if the passed address isn't a localhost address + */ +static int isLocalhost(struct sockaddr_in *address) { + // return address == 127.0.0.1 + return (unsigned int) address->sin_addr.s_addr == 16777343; +} + +/** + * Answer the errorString corresponding to the errorNumber, if available. + * This function will answer a default error string, if the errorNumber is not + * recognized. + * + * This function will have to be reworked to handle internationalization + * properly, removing the explicit strings. + * + * @param anErrorNum the error code to resolve to a human readable string + * + * @return a human readable error string + */ + +static const char * netLookupErrorString(int anErrorNum) { + switch (anErrorNum) { + case SOCKERR_BADSOCKET: + return "Bad socket"; + case SOCKERR_NOTINITIALIZED: + return "Socket library uninitialized"; + case SOCKERR_BADAF: + return "Bad address family"; + case SOCKERR_BADPROTO: + return "Bad protocol"; + case SOCKERR_BADTYPE: + return "Bad type"; + case SOCKERR_SYSTEMBUSY: + return "System busy handling requests"; + case SOCKERR_SYSTEMFULL: + return "Too many sockets allocated"; + case SOCKERR_NOTCONNECTED: + return "Socket is not connected"; + case SOCKERR_INTERRUPTED: + return "The system call was cancelled"; + case SOCKERR_TIMEOUT: + return "The operation timed out"; + case SOCKERR_CONNRESET: + return "The connection was reset"; + case SOCKERR_WOULDBLOCK: + return "The nonblocking operation would block"; + case SOCKERR_ADDRNOTAVAIL: + return "The address is not available"; + case SOCKERR_ADDRINUSE: + return "The address is already in use"; + case SOCKERR_NOTBOUND: + return "The socket is not bound"; + case SOCKERR_UNKNOWNSOCKET: + return "Resolution of the FileDescriptor to socket failed"; + case SOCKERR_INVALIDTIMEOUT: + return "The specified timeout is invalid"; + case SOCKERR_FDSETFULL: + return "Unable to create an FDSET"; + case SOCKERR_TIMEVALFULL: + return "Unable to create a TIMEVAL"; + case SOCKERR_REMSOCKSHUTDOWN: + return "The remote socket has shutdown gracefully"; + case SOCKERR_NOTLISTENING: + return "Listen() was not invoked prior to accept()"; + case SOCKERR_NOTSTREAMSOCK: + return "The socket does not support connection-oriented service"; + case SOCKERR_ALREADYBOUND: + return "The socket is already bound to an address"; + case SOCKERR_NBWITHLINGER: + return "The socket is marked non-blocking & SO_LINGER is non-zero"; + case SOCKERR_ISCONNECTED: + return "The socket is already connected"; + case SOCKERR_NOBUFFERS: + return "No buffer space is available"; + case SOCKERR_HOSTNOTFOUND: + return "Authoritative Answer Host not found"; + case SOCKERR_NODATA: + return "Valid name, no data record of requested type"; + case SOCKERR_BOUNDORCONN: + return "The socket has not been bound or is already connected"; + case SOCKERR_OPNOTSUPP: + return "The socket does not support the operation"; + case SOCKERR_OPTUNSUPP: + return "The socket option is not supported"; + case SOCKERR_OPTARGSINVALID: + return "The socket option arguments are invalid"; + case SOCKERR_SOCKLEVELINVALID: + return "The socket level is invalid"; + case SOCKERR_TIMEOUTFAILURE: + return "The timeout operation failed"; + case SOCKERR_SOCKADDRALLOCFAIL: + return "Failed to allocate address structure"; + case SOCKERR_FDSET_SIZEBAD: + return "The calculated maximum size of the file descriptor set is bad"; + case SOCKERR_UNKNOWNFLAG: + return "The flag is unknown"; + case SOCKERR_MSGSIZE: + return "The datagram was too big to fit the specified buffer, so truncated"; + case SOCKERR_NORECOVERY: + return "The operation failed with no recovery possible"; + case SOCKERR_ARGSINVALID: + return "The arguments are invalid"; + case SOCKERR_BADDESC: + return "The socket argument is not a valid file descriptor"; + case SOCKERR_NOTSOCK: + return "The socket argument is not a socket"; + case SOCKERR_HOSTENTALLOCFAIL: + return "Unable to allocate the hostent structure"; + case SOCKERR_TIMEVALALLOCFAIL: + return "Unable to allocate the timeval structure"; + case SOCKERR_LINGERALLOCFAIL: + return "Unable to allocate the linger structure"; + case SOCKERR_IPMREQALLOCFAIL: + return "Unable to allocate the ipmreq structure"; + case SOCKERR_FDSETALLOCFAIL: + return "Unable to allocate the fdset structure"; + case SOCKERR_OPFAILED: + return "Operation failed"; + case SOCKERR_CONNECTION_REFUSED: + return "Connection refused"; + case SOCKERR_ENETUNREACH: + return "Network unreachable"; + case SOCKERR_EHOSTUNREACH: + return "No route to host"; + case SOCKERR_EPIPE: + return "Broken pipe"; + case SOCKERR_EACCES: + return "Permission denied (maybe missing INTERNET permission)"; + + default: +// LOGE("unknown socket error %d", anErrorNum); + return "unknown error"; + } +} + +static int convertError(int errorCode) { + switch (errorCode) { + case EBADF: + return SOCKERR_BADDESC; + case ENOBUFS: + return SOCKERR_NOBUFFERS; + case EOPNOTSUPP: + return SOCKERR_OPNOTSUPP; + case ENOPROTOOPT: + return SOCKERR_OPTUNSUPP; + case EINVAL: + return SOCKERR_SOCKLEVELINVALID; + case ENOTSOCK: + return SOCKERR_NOTSOCK; + case EINTR: + return SOCKERR_INTERRUPTED; + case ENOTCONN: + return SOCKERR_NOTCONNECTED; + case EAFNOSUPPORT: + return SOCKERR_BADAF; + /* note: CONNRESET not included because it has the same + * value as ECONNRESET and they both map to SOCKERR_CONNRESET */ + case ECONNRESET: + return SOCKERR_CONNRESET; + case EAGAIN: + return SOCKERR_WOULDBLOCK; + case EPROTONOSUPPORT: + return SOCKERR_BADPROTO; + case EFAULT: + return SOCKERR_ARGSINVALID; + case ETIMEDOUT: + return SOCKERR_TIMEOUT; + case ECONNREFUSED: + return SOCKERR_CONNECTION_REFUSED; + case ENETUNREACH: + return SOCKERR_ENETUNREACH; + case EACCES: + return SOCKERR_EACCES; + case EPIPE: + return SOCKERR_EPIPE; + case EHOSTUNREACH: + return SOCKERR_EHOSTUNREACH; + case EADDRINUSE: + return SOCKERR_ADDRINUSE; + case EADDRNOTAVAIL: + return SOCKERR_ADDRNOTAVAIL; + case EMSGSIZE: + return SOCKERR_MSGSIZE; + default: +// LOGE("unclassified errno %d (%s)", errorCode, strerror(errorCode)); + return SOCKERR_OPFAILED; + } +} + +static int sockSelect(int nfds, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds, struct timeval *timeout) { + + int result = select(nfds, readfds, writefds, exceptfds, timeout); + + if (result < 0) { + if (errno == EINTR) { + result = SOCKERR_INTERRUPTED; + } else { + result = SOCKERR_OPFAILED; + } + } else if (result == 0) { + result = SOCKERR_TIMEOUT; + } + return result; +} + +#define SELECT_READ_TYPE 0 +#define SELECT_WRITE_TYPE 1 + +static int selectWait(int handle, int uSecTime, int type) { + fd_set fdset; + struct timeval time, *timePtr; + int result = 0; + int size = handle + 1; + + FD_ZERO(&fdset); + FD_SET(handle, &fdset); + + if (0 <= uSecTime) { + /* Use a timeout if uSecTime >= 0 */ + memset(&time, 0, sizeof(time)); + time.tv_usec = uSecTime; + timePtr = &time; + } else { + /* Infinite timeout if uSecTime < 0 */ + timePtr = NULL; + } + + if (type == SELECT_READ_TYPE) { + result = sockSelect(size, &fdset, NULL, NULL, timePtr); + } else { + result = sockSelect(size, NULL, &fdset, NULL, timePtr); + } + return result; +} + +static int pollSelectWait(JNIEnv *env, jobject fileDescriptor, int timeout, int type) { + /* now try reading the socket for the timespan timeout. + * if timeout is 0 try forever until the soclets gets ready or until an + * exception occurs. + */ + int pollTimeoutUSec = 100000, pollMsec = 100; + int finishTime = 0; + int timeLeft = timeout; + int hasTimeout = timeout > 0 ? 1 : 0; + int result = 0; + int handle; + + if (hasTimeout) { + finishTime = time_msec_clock() + timeout; + } + + int poll = 1; + + while (poll) { /* begin polling loop */ + + /* + * Fetch the handle every time in case the socket is closed. + */ + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_INTERRUPTED); + return -1; + } + + if (hasTimeout) { + + if (timeLeft - 10 < pollMsec) { + pollTimeoutUSec = timeLeft <= 0 ? 0 : (timeLeft * 1000); + } + + result = selectWait(handle, pollTimeoutUSec, type); + + /* + * because we are polling at a time smaller than timeout + * (presumably) lets treat an interrupt and timeout the same - go + * see if we're done timewise, and then just try again if not. + */ + if (SOCKERR_TIMEOUT == result || + SOCKERR_INTERRUPTED == result) { + + timeLeft = finishTime - time_msec_clock(); + + if (timeLeft <= 0) { + /* + * Always throw the "timeout" message because that is + * effectively what has happened, even if we happen to + * have been interrupted. + */ + jniThrowException(env, "java/net/SocketTimeoutException", + netLookupErrorString(SOCKERR_TIMEOUT)); + } else { + continue; // try again + } + + } else if (0 > result) { + log_socket_close(handle, result); + throwSocketException(env, result); + } + poll = 0; + + } else { /* polling with no timeout (why would you do this?)*/ + + result = selectWait(handle, pollTimeoutUSec, type); + + /* + * if interrupted (or a timeout) just retry + */ + if (SOCKERR_TIMEOUT == result || + SOCKERR_INTERRUPTED == result) { + + continue; // try again + } else if (0 > result) { + log_socket_close(handle, result); + throwSocketException(env, result); + } + poll = 0; + } + } /* end polling loop */ + + return result; +} + +/** + * A helper method, to set the connect context to a Long object. + * + * @param env pointer to the JNI library + * @param longclass Java Long Object + */ +void setConnectContext(JNIEnv *env,jobject longclass,jbyte * context) { + jclass descriptorCLS; + jfieldID descriptorFID; + descriptorCLS = env->FindClass("java/lang/Long"); + descriptorFID = env->GetFieldID(descriptorCLS, "value", "J"); + env->SetLongField(longclass, descriptorFID, (jlong)((jint)context)); +}; + +/** + * A helper method, to get the connect context. + * + * @param env pointer to the JNI library + * @param longclass Java Long Object + */ +jbyte *getConnectContext(JNIEnv *env, jobject longclass) { + jclass descriptorCLS; + jfieldID descriptorFID; + descriptorCLS = env->FindClass("java/lang/Long"); + descriptorFID = env->GetFieldID(descriptorCLS, "value", "J"); + return (jbyte*) ((jint)env->GetLongField(longclass, descriptorFID)); +}; + +// typical ip checksum +unsigned short ip_checksum(unsigned short* buffer, int size) { + register unsigned short * buf = buffer; + register int bufleft = size; + register unsigned long sum = 0; + + while (bufleft > 1) { + sum = sum + (*buf++); + bufleft = bufleft - sizeof(unsigned short ); + } + if (bufleft) { + sum = sum + (*(unsigned char*)buf); + } + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + + return (unsigned short )(~sum); +} + +/** + * Establish a connection to a peer with a timeout. This function is called + * repeatedly in order to carry out the connect and to allow other tasks to + * proceed on certain platforms. The caller must first call with + * step = SOCKET_STEP_START, if the result is SOCKERR_NOTCONNECTED it will then + * call it with step = CHECK until either another error or 0 is returned to + * indicate the connect is complete. Each time the function should sleep for no + * more than timeout milliseconds. If the connect succeeds or an error occurs, + * the caller must always end the process by calling the function with + * step = SOCKET_STEP_DONE + * + * @param[in] portLibrary The port library. + * @param[in] sock pointer to the unconnected local socket. + * @param[in] addr pointer to the sockaddr, specifying remote host/port. + * @param[in] timeout the timeout in milliseconds. If timeout is negative, + * perform a block operation. + * @param[in,out] pointer to context pointer. Filled in on first call and then + * to be passed into each subsequent call. + * + * @return 0, if no errors occurred, otherwise the (negative) error code. + */ +static int sockConnectWithTimeout(int handle, struct sockaddr_in addr, + unsigned int timeout, unsigned int step, jbyte *ctxt) { + int rc = 0; + struct timeval passedTimeout; + int errorVal; + socklen_t errorValLen = sizeof(int); + struct selectFDSet *context = NULL; + + if (SOCKET_STEP_START == step) { + + context = (struct selectFDSet *) ctxt; + + context->sock = handle; + context->nfds = handle + 1; + + if (useAdbNetworking && !isLocalhost(&addr)) { + + // LOGD("+connect to address 0x%08x (via adb)", + // addr.sin_addr.s_addr); + rc = adb_networking_connect_fd(handle, &addr); + // LOGD("-connect ret %d errno %d (via adb)", rc, errno); + + } else { + log_socket_connect(handle, ntohl(addr.sin_addr.s_addr), + ntohs(addr.sin_port)); + /* set the socket to non-blocking */ + int block = JNI_TRUE; + rc = ioctl(handle, FIONBIO, &block); + if (0 != rc) { + return convertError(rc); + } + + // LOGD("+connect to address 0x%08x (via normal) on handle %d", + // addr.sin_addr.s_addr, handle); + do { + rc = connect(handle, (struct sockaddr *) &addr, + sizeof(struct sockaddr)); + } while (rc < 0 && errno == EINTR); + // LOGD("-connect to address 0x%08x (via normal) returned %d", + // addr.sin_addr.s_addr, (int) rc); + + } + + if (rc == -1) { + rc = errno; + switch (rc) { + case EINTR: + return SOCKERR_ALREADYBOUND; + case EAGAIN: + case EINPROGRESS: + return SOCKERR_NOTCONNECTED; + default: + return convertError(rc); + } + } + + /* we connected right off the bat so just return */ + return rc; + + } else if (SOCKET_STEP_CHECK == step) { + /* now check if we have connected yet */ + + context = (struct selectFDSet *) ctxt; + + /* + * set the timeout value to be used. Because on some unix platforms we + * don't get notified when a socket is closed we only sleep for 100ms + * at a time + */ + passedTimeout.tv_sec = 0; + if (timeout > 100) { + passedTimeout.tv_usec = 100 * 1000; + } else if ((int)timeout >= 0) { + passedTimeout.tv_usec = timeout * 1000; + } + + /* initialize the FD sets for the select */ + FD_ZERO(&(context->exceptionSet)); + FD_ZERO(&(context->writeSet)); + FD_ZERO(&(context->readSet)); + FD_SET(context->sock, &(context->writeSet)); + FD_SET(context->sock, &(context->readSet)); + FD_SET(context->sock, &(context->exceptionSet)); + + rc = select(context->nfds, + &(context->readSet), + &(context->writeSet), + &(context->exceptionSet), + (int)timeout >= 0 ? &passedTimeout : NULL); + + /* if there is at least one descriptor ready to be checked */ + if (0 < rc) { + /* if the descriptor is in the write set we connected or failed */ + if (FD_ISSET(context->sock, &(context->writeSet))) { + + if (!FD_ISSET(context->sock, &(context->readSet))) { + /* ok we have connected ok */ + return 0; + } else { + /* ok we have more work to do to figure it out */ + if (getsockopt(context->sock, SOL_SOCKET, SO_ERROR, + &errorVal, &errorValLen) >= 0) { + return errorVal ? convertError(errorVal) : 0; + } else { + return convertError(errno); + } + } + } + + /* if the descriptor is in the exception set the connect failed */ + if (FD_ISSET(context->sock, &(context->exceptionSet))) { + if (getsockopt(context->sock, SOL_SOCKET, SO_ERROR, &errorVal, + &errorValLen) >= 0) { + return errorVal ? convertError(errorVal) : 0; + } + rc = errno; + return convertError(rc); + } + + } else if (rc < 0) { + /* something went wrong with the select call */ + rc = errno; + + /* if it was EINTR we can just try again. Return not connected */ + if (EINTR == rc) { + return SOCKERR_NOTCONNECTED; + } + + /* some other error occured so look it up and return */ + return convertError(rc); + } + + /* + * if we get here the timeout expired or the connect had not yet + * completed just indicate that the connect is not yet complete + */ + return SOCKERR_NOTCONNECTED; + } else if (SOCKET_STEP_DONE == step) { + /* we are done the connect or an error occured so clean up */ + if (handle != -1) { + int block = JNI_FALSE; + ioctl(handle, FIONBIO, &block); + } + return 0; + } + return SOCKERR_ARGSINVALID; +} + +/** + * Join/Leave the nominated multicast group on the specified socket. + * Implemented by setting the multicast 'add membership'/'drop membership' + * option at the HY_IPPROTO_IP level on the socket. + * + * Implementation note for multicast sockets in general: + * + * - This code is untested, because at the time of this writing multicast can't + * be properly tested on Android due to GSM routing restrictions. So it might + * or might not work. + * + * - The REUSEPORT socket option that Harmony employs is not supported on Linux + * and thus also not supported on Android. It's is not needed for multicast + * to work anyway (REUSEADDR should suffice). + * + * @param env pointer to the JNI library. + * @param socketP pointer to the hysocket to join/leave on. + * @param optVal pointer to the InetAddress, the multicast group to join/drop. + * + * @exception SocketException if an error occurs during the call + */ +static void mcastAddDropMembership (JNIEnv * env, int handle, jobject optVal, + int ignoreIF, int setSockOptVal) { + int result; + struct ip_mreq ipmreqP; + struct sockaddr_in sockaddrP; + int length = sizeof(struct ip_mreq); + socklen_t lengthIF = sizeof(struct sockaddr_in); + + /* + * JNI objects needed to access the information in the optVal oject + * passed in. The object passed in is a GenericIPMreq object + */ + jclass cls; + jfieldID multiaddrID; + jfieldID interfaceAddrID; + jobject multiaddr; + jobject interfaceAddr; + + /* + * check whether we are getting an InetAddress or an Generic IPMreq, for now + * we support both so that we will not break the tests + */ + if (env->IsInstanceOf (optVal, gCachedFields.iaddr_class)) { + + ipmreqP.imr_interface.s_addr = htonl(INADDR_ANY); + if (!ignoreIF) { + + result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, &sockaddrP, + &lengthIF); + + if (0 != result) { + throwSocketException (env, convertError(errno)); + return; + } + + memcpy(&(ipmreqP.imr_interface.s_addr), &(sockaddrP.sin_addr), 4); + } + + result = inetAddressToSocketAddress(env, optVal, 0, &sockaddrP); + + if (result < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + memcpy(&(ipmreqP.imr_multiaddr.s_addr), &(sockaddrP.sin_addr), 4); + + result = setsockopt(handle, IPPROTO_IP, setSockOptVal, &ipmreqP, length); + if (0 != result) { + throwSocketException (env, convertError(errno)); + return; + } + + } else { + + /* we need the multicast address regardless of the type of address */ + cls = env->GetObjectClass(optVal); + multiaddrID = env->GetFieldID(cls, "multiaddr", "Ljava/net/InetAddress;"); + multiaddr = env->GetObjectField(optVal, multiaddrID); + + result = inetAddressToSocketAddress(env, multiaddr, 0, &sockaddrP); + + if (result < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + memcpy(&(ipmreqP.imr_multiaddr.s_addr), &(sockaddrP.sin_addr), 4); + + /* we need to use an IP_MREQ as it is an IPV4 address */ + interfaceAddrID = env->GetFieldID(cls, "interfaceAddr", + "Ljava/net/InetAddress;"); + interfaceAddr = env->GetObjectField(optVal, interfaceAddrID); + + ipmreqP.imr_interface.s_addr = htonl(INADDR_ANY); + + /* + * if an interfaceAddr was passed then use that value, otherwise set the + * interface to all 0 to indicate the system should select the interface + * used + */ + if (!ignoreIF) { + if (NULL != interfaceAddr) { + + result = inetAddressToSocketAddress(env, interfaceAddr, 0, + &sockaddrP); + + if (result < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + memcpy(&(ipmreqP.imr_interface.s_addr), &(sockaddrP.sin_addr), 4); + + } + } + + /* join/drop the multicast address */ + result = setsockopt(handle, IPPROTO_IP, setSockOptVal, &ipmreqP, length); + if (0 != result) { + throwSocketException (env, convertError(errno)); + return; + } + } +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_oneTimeInitializationImpl(JNIEnv* env, jobject obj, + jboolean jcl_supports_ipv6) { + // LOGD("ENTER oneTimeInitializationImpl of OSNetworkSystem"); + + char useAdbNetworkingProperty[PROPERTY_VALUE_MAX]; + char adbConnectedProperty[PROPERTY_VALUE_MAX]; + + property_get("android.net.use-adb-networking", useAdbNetworkingProperty, ""); + property_get("adb.connected", adbConnectedProperty, ""); + + if (strlen((char *)useAdbNetworkingProperty) > 0 + && strlen((char *)adbConnectedProperty) > 0) { + useAdbNetworking = 1; + } + + memset(&gCachedFields, 0, sizeof(gCachedFields)); + + // initializing InetAddress + + jclass iaddrclass = env->FindClass("java/net/InetAddress"); + + if (iaddrclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.net.InetAddress"); + return; + } + + gCachedFields.iaddr_class = (jclass) env->NewGlobalRef(iaddrclass); + + jmethodID iaddrclassinit = env->GetMethodID(iaddrclass, "", "()V"); + + if (iaddrclassinit == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", "InetAddress.()"); + return; + } + + gCachedFields.iaddr_class_init = iaddrclassinit; + + jmethodID iaddrgetbyaddress = env->GetStaticMethodID(iaddrclass, + "getByAddress", "([B)Ljava/net/InetAddress;"); + + if (iaddrgetbyaddress == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", + "InetAddress.getByAddress(byte[] val)"); + return; + } + + gCachedFields.iaddr_getbyaddress = iaddrgetbyaddress; + + jfieldID iaddripaddress = env->GetFieldID(iaddrclass, "ipaddress", "[B"); + + if (iaddripaddress == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", + "Can't find field InetAddress.ipaddress"); + return; + } + + gCachedFields.iaddr_ipaddress = iaddripaddress; + + // get the GenericIPMreq class + + jclass genericipmreqclass = env->FindClass("org/apache/harmony/luni/net/GenericIPMreq"); + + if (genericipmreqclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "org.apache.harmony.luni.net.GenericIPMreq"); + return; + } + + gCachedFields.genericipmreq_class = (jclass) env->NewGlobalRef(genericipmreqclass); + + // initializing Integer + + jclass integerclass = env->FindClass("java/lang/Integer"); + + if (integerclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.lang.Integer"); + return; + } + + jmethodID integerclassinit = env->GetMethodID(integerclass, "", "(I)V"); + + if (integerclassinit == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", + "Integer.(int val)"); + return; + } + + jfieldID integerclassvalue = env->GetFieldID(integerclass, "value", "I"); + + if (integerclassvalue == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", "Integer.value"); + return; + } + + gCachedFields.integer_class = (jclass) env->NewGlobalRef(integerclass); + gCachedFields.integer_class_init = integerclassinit; + gCachedFields.integer_class_value = integerclassvalue; + + // initializing Boolean + + jclass booleanclass = env->FindClass("java/lang/Boolean"); + + if (booleanclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.lang.Boolean"); + return; + } + + jmethodID booleanclassinit = env->GetMethodID(booleanclass, "", "(Z)V"); + + if (booleanclassinit == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", + "Boolean.(boolean val)"); + return; + } + + jfieldID booleanclassvalue = env->GetFieldID(booleanclass, "value", "Z"); + + if (booleanclassvalue == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", "Boolean.value"); + return; + } + + gCachedFields.boolean_class = (jclass) env->NewGlobalRef(booleanclass); + gCachedFields.boolean_class_init = booleanclassinit; + gCachedFields.boolean_class_value = booleanclassvalue; + + // initializing Byte + + jclass byteclass = env->FindClass("java/lang/Byte"); + + if (byteclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.lang.Byte"); + return; + } + + jmethodID byteclassinit = env->GetMethodID(byteclass, "", "(B)V"); + + if (byteclassinit == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", + "Byte.(byte val)"); + return; + } + + jfieldID byteclassvalue = env->GetFieldID(byteclass, "value", "B"); + + if (byteclassvalue == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", "Byte.value"); + return; + } + + gCachedFields.byte_class = (jclass) env->NewGlobalRef(byteclass); + gCachedFields.byte_class_init = byteclassinit; + gCachedFields.byte_class_value = byteclassvalue; + + // initializing String + + jclass stringclass = env->FindClass("java/lang/String"); + + if (stringclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.lang.String"); + return; + } + + jmethodID stringclassinit = env->GetMethodID(stringclass, "", "([B)V"); + + if (stringclassinit == NULL) { + jniThrowException(env, "java/lang/NoSuchMethodError", + "String.(byte[] val)"); + return; + } + + gCachedFields.string_class = (jclass) env->NewGlobalRef(stringclass); + gCachedFields.string_class_init = stringclassinit; + + // initializing ScoketImpl + + jclass socketimplclass = env->FindClass("java/net/SocketImpl"); + + if (socketimplclass == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.net.SocketImpl"); + return; + } + + jfieldID socketimplport = env->GetFieldID(socketimplclass, "port", "I"); + + if (socketimplport == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", "SocketImpl.port"); + return; + } + + jfieldID socketimpladdress = env->GetFieldID(socketimplclass, "address", + "Ljava/net/InetAddress;"); + + if (socketimpladdress == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", + "SocketImpl.address"); + return; + } + + gCachedFields.socketimpl_address = socketimpladdress; + gCachedFields.socketimpl_port = socketimplport; + + gCachedFields.dpack_class = env->FindClass("java/net/DatagramPacket"); + if (gCachedFields.dpack_class == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.net.DatagramPacket"); + return; + } + + gCachedFields.dpack_address = env->GetFieldID(gCachedFields.dpack_class, + "address", "Ljava/net/InetAddress;"); + if (gCachedFields.dpack_address == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", + "DatagramPacket.address"); + return; + } + + gCachedFields.dpack_port = env->GetFieldID(gCachedFields.dpack_class, + "port", "I"); + if (gCachedFields.dpack_port == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", + "DatagramPacket.port"); + return; + } + + gCachedFields.dpack_length = env->GetFieldID(gCachedFields.dpack_class, + "length", "I"); + if (gCachedFields.dpack_length == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", + "DatagramPacket.length"); + return; + } + + gCachedFields.fd_class = env->FindClass("java/io/FileDescriptor"); + if (gCachedFields.fd_class == NULL) { + jniThrowException(env, "java/lang/ClassNotFoundException", + "java.io.FileDescriptor"); + return; + } + gCachedFields.descriptor = env->GetFieldID(gCachedFields.fd_class, "descriptor", "I"); + if (gCachedFields.descriptor == NULL) { + jniThrowException(env, "java/lang/NoSuchFieldError", + "FileDescriptor.descriptor"); + return; + } + +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jboolean preferIPv4Stack) { + // LOGD("ENTER createSocketImpl"); + + int ret = socket(PF_INET, SOCK_STREAM, 0); + + if (ret < 0) { + int err = convertError(errno); + throwSocketException(env, err); + return; + } + + jniSetFileDescriptorOfFD(env, fileDescriptor, ret); + + return; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createDatagramSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jboolean preferIPv4Stack) { + // LOGD("ENTER createDatagramSocketImpl"); + + int ret = socket(PF_INET, SOCK_DGRAM, 0); + + if (ret < 0) { + int err = convertError(errno); + throwSocketException(env, err); + return; + } + + jniSetFileDescriptorOfFD(env, fileDescriptor, ret); + + return; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_readSocketDirectImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint address, jint offset, jint count, + jint timeout) { + // LOGD("ENTER readSocketDirectImpl"); + + int handle; + jbyte *message = (jbyte *)address; + int result, ret, localCount; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + result = selectWait(handle, timeout, SELECT_READ_TYPE); + + if (0 > result) { + return 0; + } + + localCount = (count < 65536) ? count : 65536; + + do { + ret = recv(handle, (jbyte *) message, localCount, SOCKET_NOFLAGS); + } while (ret < 0 && errno == EINTR); + + if (0 == ret) { + return -1; + } else if (ret == -1) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + add_recv_stats(handle, ret); + return ret; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_readSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jbyteArray data, jint offset, jint count, + jint timeout) { + // LOGD("ENTER readSocketImpl"); + + jbyte *message; + int result, localCount; + + jbyte internalBuffer[BUFFERSIZE]; + + localCount = (count < 65536) ? count : 65536; + + if (localCount > BUFFERSIZE) { + message = (jbyte*)malloc(localCount * sizeof(jbyte)); + if (message == NULL) { + jniThrowException(env, "java/lang/OutOfMemoryError", + "couldn't allocate enough memory for readSocket"); + return 0; + } + } else { + message = (jbyte *)internalBuffer; + } + + result = Java_org_sipdroid_net_impl_OSNetworkSystem_readSocketDirectImpl(env, clazz, fileDescriptor, + (jint) message, offset, count, timeout); + + if (result > 0) { + env->SetByteArrayRegion(data, offset, result, (jbyte *)message); + } + + if (((jbyte *)message) != internalBuffer) { + free(( jbyte *)message); + } + + return result; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_writeSocketDirectImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint address, jint offset, jint count) { + // LOGD("ENTER writeSocketDirectImpl"); + + int handle; + jbyte *message = (jbyte *)address; + int result = 0, sent = 0; + + if (count <= 0) { + return 0; + } + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + result = send(handle, (jbyte *) message, (int) count, SOCKET_NOFLAGS); + if (result < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + + if (SOCKERR_WOULDBLOCK == err){ + jclass socketExClass,errorCodeExClass; + jmethodID errorCodeExConstructor, socketExConstructor,socketExCauseMethod; + jobject errorCodeEx, socketEx; + const char* errorMessage = netLookupErrorString(err); + jstring errorMessageString = env->NewStringUTF(errorMessage); + + errorCodeExClass = env->FindClass("org/apache/harmony/luni/util/ErrorCodeException"); + if (!errorCodeExClass){ + return 0; + } + errorCodeExConstructor = env->GetMethodID(errorCodeExClass,"","(I)V"); + if (!errorCodeExConstructor){ + return 0; + } + errorCodeEx = env->NewObject(errorCodeExClass,errorCodeExConstructor,err); + + socketExClass = env->FindClass("java/net/SocketException"); + if (!socketExClass) { + return 0; + } + socketExConstructor = env->GetMethodID(socketExClass,"","(Ljava/lang/String;)V"); + if (!socketExConstructor) { + return 0; + } + socketEx = env->NewObject(socketExClass, socketExConstructor, errorMessageString); + socketExCauseMethod = env->GetMethodID(socketExClass,"initCause","(Ljava/lang/Throwable;)Ljava/lang/Throwable;"); + env->CallObjectMethod(socketEx,socketExCauseMethod,errorCodeEx); + env->Throw((jthrowable)socketEx); + return 0; + } + throwSocketException(env, err); + return 0; + } + + add_send_stats(handle, result); + return result; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_writeSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jbyteArray data, jint offset, jint count) { + // LOGD("ENTER writeSocketImpl"); + + jbyte *message; + int sent = 0; + jint result = 0; + +/* TODO: ARRAY PINNING */ +#define INTERNAL_SEND_BUFFER_MAX 512 + jbyte internalBuffer[INTERNAL_SEND_BUFFER_MAX]; + + if (count > INTERNAL_SEND_BUFFER_MAX) { + message = (jbyte*)malloc(count * sizeof( jbyte)); + if (message == NULL) { + jniThrowException(env, "java/lang/OutOfMemoryError", + "couldn't allocate enough memory for writeSocket"); + return 0; + } + } else { + message = (jbyte *)internalBuffer; + } + + env->GetByteArrayRegion(data, offset, count, message); + + result = Java_org_sipdroid_net_impl_OSNetworkSystem_writeSocketDirectImpl(env, clazz, fileDescriptor, + (jint) message, offset, count); + + if (( jbyte *)message != internalBuffer) { + free(( jbyte *)message); + } +#undef INTERNAL_SEND_BUFFER_MAX + return result; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_setNonBlockingImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jboolean nonblocking) { + // LOGD("ENTER setNonBlockingImpl"); + + int handle; + int result; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + int block = nonblocking; + + result = ioctl(handle, FIONBIO, &block); + + if (result == -1) { + throwSocketException(env, convertError(errno)); + } +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint trafficClass, jobject inetAddr, jint port); + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_connectWithTimeoutSocketImpl(JNIEnv* env, + jclass clazz, jobject fileDescriptor, jint timeout, jint trafficClass, + jobject inetAddr, jint port, jint step, jbyteArray passContext) { + // LOGD("ENTER connectWithTimeoutSocketImpl"); + + int handle; + int result = 0; + struct sockaddr_in address; + jbyte *context = NULL; + + memset(&address, 0, sizeof(address)); + + address.sin_family = AF_INET; + + result = inetAddressToSocketAddress(env, inetAddr, port, + (struct sockaddr_in *) &address); + + if (result < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return result; + } + + // Check if we're using adb networking and redirect in case it is used. + if (useAdbNetworking && !isLocalhost(&address)) { + return Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(env, clazz, fileDescriptor, + trafficClass, inetAddr, port); + } + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return -1; + } + + address.sin_port = htons(port); + + context = (jbyte *)env->GetPrimitiveArrayCritical(passContext, NULL); + + switch (step) { + case SOCKET_CONNECT_STEP_START: + result = sockConnectWithTimeout(handle, address, 0, + SOCKET_STEP_START, context); + break; + case SOCKET_CONNECT_STEP_CHECK: + result = sockConnectWithTimeout(handle, address, timeout, + SOCKET_STEP_CHECK, context); + break; + } + + env->ReleasePrimitiveArrayCritical(passContext, context, JNI_ABORT); + + if (0 == result) { + /* connected , so stop here */ + sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, NULL); + } else if (result != SOCKERR_NOTCONNECTED) { + /* can not connect... */ + sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, NULL); + if (result == SOCKERR_EACCES) { + jniThrowException(env, "java/lang/SecurityException", + netLookupErrorString(result)); + } else { + jniThrowException(env, "java/net/ConnectException", + netLookupErrorString(result)); + } + } + + return result; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_connectStreamWithTimeoutSocketImpl(JNIEnv* env, + jclass clazz, jobject fileDescriptor, jint remotePort, jint timeout, + jint trafficClass, jobject inetAddr) { + // LOGD("ENTER connectStreamWithTimeoutSocketImpl"); + + int result = 0; + int handle; + struct sockaddr_in address; + jbyte *context = NULL; + int remainingTimeout = timeout; + int passedTimeout = 0; + int finishTime = 0; + int blocking = 0; + char hasTimeout = timeout > 0; + + /* if a timeout was specified calculate the finish time value */ + if (hasTimeout) { + finishTime = time_msec_clock() + (int) timeout; + } + + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } else { + result = inetAddressToSocketAddress(env, inetAddr, remotePort, + (struct sockaddr_in *) &address); + + if (result < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + // Check if we're using adb networking and redirect in case it is used. + if (useAdbNetworking && !isLocalhost(&address)) { + int retVal = Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(env, clazz, + fileDescriptor, trafficClass, inetAddr, remotePort); + if (retVal != 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + } + return; + } + + /* + * we will be looping checking for when we are connected so allocate + * the descriptor sets that we will use + */ + context =(jbyte *) malloc(sizeof(struct selectFDSet)); + + if (NULL == context) { + throwSocketException(env, SOCKERR_NOBUFFERS); + return; + } + + result = sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_START, context); + if (0 == result) { + /* ok we connected right away so we are done */ + sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, context); + goto bail; + } else if (result != SOCKERR_NOTCONNECTED) { + log_socket_close(handle, result); + sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, + context); + /* we got an error other than NOTCONNECTED so we cannot continue */ + if (SOCKERR_EACCES == result) { + jniThrowException(env, "java/lang/SecurityException", + netLookupErrorString(result)); + } else { + throwSocketException(env, result); + } + goto bail; + } + + while (SOCKERR_NOTCONNECTED == result) { + passedTimeout = remainingTimeout; + + /* + * ok now try and connect. Depending on the platform this may sleep + * for up to passedTimeout milliseconds + */ + result = sockConnectWithTimeout(handle, address, passedTimeout, + SOCKET_STEP_CHECK, context); + + /* + * now check if the socket is still connected. + * Do it here as some platforms seem to think they + * are connected if the socket is closed on them. + */ + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + sockConnectWithTimeout(handle, address, 0, + SOCKET_STEP_DONE, context); + throwSocketException(env, SOCKERR_BADSOCKET); + goto bail; + } + + /* + * check if we are now connected, + * if so we can finish the process and return + */ + if (0 == result) { + sockConnectWithTimeout(handle, address, 0, + SOCKET_STEP_DONE, context); + goto bail; + } + + /* + * if the error is SOCKERR_NOTCONNECTED then we have not yet + * connected and we may not be done yet + */ + if (SOCKERR_NOTCONNECTED == result) { + /* check if the timeout has expired */ + if (hasTimeout) { + remainingTimeout = finishTime - time_msec_clock(); + if (remainingTimeout <= 0) { + log_socket_close(handle, result); + sockConnectWithTimeout(handle, address, 0, + SOCKET_STEP_DONE, context); + jniThrowException(env, + "java/net/SocketTimeoutException", + netLookupErrorString(result)); + goto bail; + } + } else { + remainingTimeout = 100; + } + } else { + log_socket_close(handle, result); + sockConnectWithTimeout(handle, address, remainingTimeout, + SOCKET_STEP_DONE, context); + if ((SOCKERR_CONNRESET == result) || + (SOCKERR_CONNECTION_REFUSED == result) || + (SOCKERR_ADDRNOTAVAIL == result) || + (SOCKERR_ADDRINUSE == result) || + (SOCKERR_ENETUNREACH == result)) { + jniThrowException(env, "java/net/ConnectException", + netLookupErrorString(result)); + } else if (SOCKERR_EACCES == result) { + jniThrowException(env, "java/lang/SecurityException", + netLookupErrorString(result)); + } else { + throwSocketException(env, result); + } + goto bail; + } + } + } + +bail: + + /* free the memory for the FD set */ + if (context != NULL) { + free(context); + } +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_connectSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint trafficClass, jobject inetAddr, jint port) { + //LOGD("ENTER direct-call connectSocketImpl\n"); + + struct sockaddr_in address; + int ret; + int handle; + jbyteArray java_in_addr; + + memset(&address, 0, sizeof(address)); + + address.sin_family = AF_INET; + + ret = inetAddressToSocketAddress(env, inetAddr, port, + (struct sockaddr_in *) &address); + + if (ret < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return ret; + } + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return -1; + } + + address.sin_port = htons(port); + + if (useAdbNetworking && !isLocalhost(&address)) { + + // LOGD("+connect to address 0x%08x port %d (via adb)", + // address.sin_addr.s_addr, (int) port); + ret = adb_networking_connect_fd(handle, &address); + // LOGD("-connect ret %d errno %d (via adb)", ret, errno); + + } else { + + // call this method with a timeout of zero + Java_org_sipdroid_net_impl_OSNetworkSystem_connectStreamWithTimeoutSocketImpl(env, clazz, + fileDescriptor, port, 0, trafficClass, inetAddr); + if (env->ExceptionOccurred() != 0) { + return -1; + } else { + return 0; + } + + } + + if (ret < 0) { + jniThrowException(env, "java/net/ConnectException", + netLookupErrorString(convertError(errno))); + return ret; + } + + return ret; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_socketBindImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint port, jobject inetAddress) { + // LOGD("ENTER socketBindImpl"); + + struct sockaddr_in sockaddress; + int ret; + int handle; + + ret = inetAddressToSocketAddress(env, inetAddress, port, + (struct sockaddr_in *) &sockaddress); + + if (ret < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + ret = bind(handle, (const sockaddr*)&sockaddress, sizeof(sockaddress)); + + if (ret < 0) { + jniThrowException(env, "java/net/BindException", + netLookupErrorString(convertError(errno))); + return; + } +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_listenStreamSocketImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint backlog) { + // LOGD("ENTER listenStreamSocketImpl"); + + int ret; + int handle; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + ret = listen(handle, backlog); + + if (ret < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return; + } +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_availableStreamImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor) { + // LOGD("ENTER availableStreamImpl"); + + int handle; + char message[BUFFERSIZE]; + + int result; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + do { + result = selectWait(handle, 1, SELECT_READ_TYPE); + + if (SOCKERR_TIMEOUT == result) { + // The read operation timed out, so answer 0 bytes available + return 0; + } else if (SOCKERR_INTERRUPTED == result) { + continue; + } else if (0 > result) { + log_socket_close(handle, result); + throwSocketException(env, result); + return 0; + } + } while (SOCKERR_INTERRUPTED == result); + + result = recv(handle, (jbyte *) message, BUFFERSIZE, MSG_PEEK); + + if (0 > result) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + add_recv_stats(handle, result); + return result; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_acceptSocketImpl(JNIEnv* env, jclass clazz, + jobject fdServer, jobject newSocket, jobject fdnewSocket, jint timeout) { + // LOGD("ENTER acceptSocketImpl"); + + union { + struct sockaddr address; + struct sockaddr_in in_address; + } sa; + + int ret; + int retFD; + int result; + int handle; + socklen_t addrlen; + + if (newSocket == NULL) { + throwNullPointerException(env); + return; + } + + result = pollSelectWait(env, fdServer, timeout, SELECT_READ_TYPE); + + if (0 > result) { + return; + } + + handle = jniGetFDFromFileDescriptor(env, fdServer); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + do { + addrlen = sizeof(sa); + ret = accept(handle, &(sa.address), &addrlen); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return; + } + + retFD = ret; + + /* For AF_INET / inetOrLocal == true only: put + * peer address and port in instance variables + * We don't bother for UNIX domain sockets, since most peers are + * anonymous anyway + */ + if (sa.address.sa_family == AF_INET) { + // inetOrLocal should also be true + + jobject inetAddress; + + inetAddress = structInToInetAddress(env, &(sa.in_address.sin_addr)); + + if (inetAddress == NULL) { + close(retFD); + newSocket = NULL; + return; + } + + env->SetObjectField(newSocket, + gCachedFields.socketimpl_address, inetAddress); + + env->SetIntField(newSocket, gCachedFields.socketimpl_port, + ntohs(sa.in_address.sin_port)); + } + + jniSetFileDescriptorOfFD(env, fdnewSocket, retFD); +} + +extern "C" jboolean Java_org_sipdroid_net_impl_OSNetworkSystem_supportsUrgentDataImpl(JNIEnv* env, + jclass clazz, jobject fileDescriptor) { + // LOGD("ENTER supportsUrgentDataImpl"); + + int handle; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + if (handle == 0 || handle == -1) { + return JNI_FALSE; + } + + return JNI_TRUE; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_sendUrgentDataImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jbyte value) { + // LOGD("ENTER sendUrgentDataImpl"); + + int handle; + int result; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + result = send(handle, (jbyte *) &value, 1, MSG_OOB); + if (result < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + } +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_connectDatagramImpl2(JNIEnv* env, jclass clazz, + jobject fd, jint port, jint trafficClass, jobject inetAddress) { + // LOGD("ENTER connectDatagramImpl2"); + + int handle = jniGetFDFromFileDescriptor(env, fd); + + struct sockaddr_in sockAddr; + int ret; + + ret = inetAddressToSocketAddress(env, inetAddress, port, &sockAddr); + + if (ret < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + log_socket_connect(handle, ntohl(sockAddr.sin_addr.s_addr), port); + int result = connect(handle, (struct sockaddr *)&sockAddr, sizeof(sockAddr)); + if (result < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + } +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_disconnectDatagramImpl(JNIEnv* env, jclass clazz, + jobject fd) { + // LOGD("ENTER disconnectDatagramImpl"); + + int handle = jniGetFDFromFileDescriptor(env, fd); + + struct sockaddr_in *sockAddr; + socklen_t sockAddrLen = sizeof(struct sockaddr_in); + sockAddr = (struct sockaddr_in *) malloc(sockAddrLen); + memset(sockAddr, 0, sockAddrLen); + + sockAddr->sin_family = AF_UNSPEC; + int result = connect(handle, (struct sockaddr *)sockAddr, sockAddrLen); + free(sockAddr); + + if (result < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + } +} + +extern "C" jboolean Java_org_sipdroid_net_impl_OSNetworkSystem_socketBindImpl2(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint port, jboolean bindToDevice, + jobject inetAddress) { + // LOGD("ENTER socketBindImpl2"); + + struct sockaddr_in sockaddress; + int ret; + int handle; + + ret = inetAddressToSocketAddress(env, inetAddress, port, + (struct sockaddr_in *) &sockaddress); + + if (ret < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + ret = bind(handle, (const sockaddr*)&sockaddress, sizeof(sockaddress)); + + if (ret < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + jniThrowException(env, "java/net/BindException", netLookupErrorString(err)); + return 0; + } + + return 0; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_peekDatagramImpl(JNIEnv* env, jclass clazz, + jobject fd, jobject sender, jint receiveTimeout) { + // LOGD("ENTER peekDatagramImpl"); + + int port = -1; + + int result = pollSelectWait (env, fd, receiveTimeout, SELECT_READ_TYPE); + if (0> result) { + return (jint) 0; + } + + int handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + struct sockaddr_in sockAddr; + socklen_t sockAddrLen = sizeof(sockAddr); + + int length = recvfrom(handle, NULL, 0, MSG_PEEK, + (struct sockaddr *)&sockAddr, &sockAddrLen); + + if (length < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + + if (socketAddressToInetAddress(env, &sockAddr, sender, &port) < 0) { + throwIOExceptionStr(env, "Address conversion failed"); + return -1; + } + add_recv_stats(handle, length); + return port; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_receiveDatagramDirectImpl(JNIEnv* env, jclass clazz, + jobject fd, jobject packet, jint address, jint offset, jint length, + jint receiveTimeout, jboolean peek) { + // LOGD("ENTER receiveDatagramDirectImpl"); + + int result = pollSelectWait (env, fd, receiveTimeout, SELECT_READ_TYPE); + if (0 > result) { + return (jint) 0; + } + + int handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + struct sockaddr_in sockAddr; + socklen_t sockAddrLen = sizeof(sockAddr); + + int mode = peek ? MSG_PEEK : 0; + + int actualLength = recvfrom(handle, (char*)(address + offset), length, mode, + (struct sockaddr *)&sockAddr, &sockAddrLen); + + if (actualLength < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + + if (packet != NULL) { + /* + int port = ntohs(sockAddr.sin_port); + jbyteArray addr = env->NewByteArray(sizeof(struct in_addr)); + if ((structInToJavaAddress(env, &sockAddr.sin_addr, addr)) < 0) { + jniThrowException(env, "java/net/SocketException", + "Could not set address of packet."); + return 0; + } + jobject sender = env->CallStaticObjectMethod( + gCachedFields.iaddr_class, gCachedFields.iaddr_getbyaddress, + addr); + env->SetObjectField(packet, gCachedFields.dpack_address, sender); + env->SetIntField(packet, gCachedFields.dpack_port, port); + */ + env->SetIntField(packet, gCachedFields.dpack_length, actualLength); + } + add_recv_stats(handle, actualLength); + return actualLength; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_receiveDatagramImpl(JNIEnv* env, jclass clazz, + jobject fd, jobject packet, jbyteArray data, jint offset, jint length, + jint receiveTimeout, jboolean peek) { + // LOGD("ENTER receiveDatagramImpl"); + + int localLength = (length < 65536) ? length : 65536; + jbyte *bytes = (jbyte*) malloc(localLength); + if (bytes == NULL) { + jniThrowException(env, "java/lang/OutOfMemoryError", + "couldn't allocate enough memory for receiveDatagram"); + return 0; + } + + int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_receiveDatagramDirectImpl(env, clazz, fd, + packet, (jint)bytes, offset, localLength, receiveTimeout, peek); + + if (actualLength > 0) { + env->SetByteArrayRegion(data, offset, actualLength, bytes); + } + free(bytes); + + return actualLength; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_recvConnectedDatagramDirectImpl(JNIEnv* env, + jclass clazz, jobject fd, jobject packet, jint address, jint offset, + jint length, jint receiveTimeout, jboolean peek) { + // LOGD("ENTER receiveConnectedDatagramDirectImpl"); + + int result = pollSelectWait (env, fd, receiveTimeout, SELECT_READ_TYPE); + + if (0 > result) { + return 0; + } + + int handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + int mode = peek ? MSG_PEEK : 0; + + int actualLength = recvfrom(handle, + (char*)(address + offset), length, mode, NULL, NULL); + + if (actualLength < 0) { + jniThrowException(env, "java/net/PortUnreachableException", ""); + return 0; + } + + if ( packet != NULL) { + env->SetIntField(packet, gCachedFields.dpack_length, actualLength); + } + add_recv_stats(handle, actualLength); + return actualLength; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_recvConnectedDatagramImpl(JNIEnv* env, jclass clazz, + jobject fd, jobject packet, jbyteArray data, jint offset, jint length, + jint receiveTimeout, jboolean peek) { + // LOGD("ENTER receiveConnectedDatagramImpl"); + + int localLength = (length < 65536) ? length : 65536; + jbyte *bytes = (jbyte*) malloc(localLength); + if (bytes == NULL) { + jniThrowException(env, "java/lang/OutOfMemoryError", + "couldn't allocate enough memory for recvConnectedDatagram"); + return 0; + } + + int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_recvConnectedDatagramDirectImpl(env, + clazz, fd, packet, (jint)bytes, offset, localLength, + receiveTimeout, peek); + + if (actualLength > 0) { + env->SetByteArrayRegion(data, offset, actualLength, bytes); + } + free(bytes); + + return actualLength; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramDirectImpl(JNIEnv* env, jclass clazz, + jobject fd, jint address, jint offset, jint length, jint port, + jboolean bindToDevice, jint trafficClass, jobject inetAddress) { + // LOGD("ENTER sendDatagramDirectImpl"); + + int result = 0; + + int handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + struct sockaddr_in receiver; + + if (inetAddressToSocketAddress(env, inetAddress, port, &receiver) < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + result = sendto(handle, (char*)(address + offset), length, SOCKET_NOFLAGS, + (struct sockaddr*)&receiver, sizeof(receiver)); + + if (result < 0) { + int err = convertError(errno); + if ((SOCKERR_CONNRESET == err) + || (SOCKERR_CONNECTION_REFUSED == err)) { + return 0; + } else { + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + } + add_send_stats(handle, result); + return result; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramImpl(JNIEnv* env, jclass clazz, + jobject fd, jbyteArray data, jint offset, jint length, jint port, + jboolean bindToDevice, jint trafficClass, jobject inetAddress) { + // LOGD("ENTER sendDatagramImpl"); + + jbyte *bytes = env->GetByteArrayElements(data, NULL); + int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramDirectImpl(env, clazz, fd, + (jint)bytes, offset, length, port, bindToDevice, trafficClass, + inetAddress); + env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); + + return actualLength; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendConnectedDatagramDirectImpl(JNIEnv* env, + jclass clazz, jobject fd, jint address, jint offset, jint length, + jboolean bindToDevice) { + // LOGD("ENTER sendConnectedDatagramDirectImpl"); + + int handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + int result = send(handle, (char*)(address + offset), length, 0); + + if (result < 0) { + int err = convertError(errno); + if ((SOCKERR_CONNRESET == err) || (SOCKERR_CONNECTION_REFUSED == err)) { + return 0; + } else { + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + } + add_send_stats(handle, length); + return result; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendConnectedDatagramImpl(JNIEnv* env, jclass clazz, + jobject fd, jbyteArray data, jint offset, jint length, + jboolean bindToDevice) { + // LOGD("ENTER sendConnectedDatagramImpl"); + + jbyte *bytes = env->GetByteArrayElements(data, NULL); + int actualLength = Java_org_sipdroid_net_impl_OSNetworkSystem_sendConnectedDatagramDirectImpl(env, + clazz, fd, (jint)bytes, offset, length, bindToDevice); + env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); + + return actualLength; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createServerStreamSocketImpl(JNIEnv* env, + jclass clazz, jobject fileDescriptor, jboolean preferIPv4Stack) { + // LOGD("ENTER createServerStreamSocketImpl"); + + if (fileDescriptor == NULL) { + throwNullPointerException(env); + return; + } + + int handle = socket(PF_INET, SOCK_STREAM, 0); + + if (handle < 0) { + int err = convertError(errno); + throwSocketException(env, err); + return; + } + + jniSetFileDescriptorOfFD(env, fileDescriptor, handle); + + int value = 1; + + setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)); +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_createMulticastSocketImpl(JNIEnv* env, + jclass clazz, jobject fileDescriptor, jboolean preferIPv4Stack) { + // LOGD("ENTER createMulticastSocketImpl"); + + int handle = socket(PF_INET, SOCK_DGRAM, 0); + + if (handle < 0) { + int err = convertError(errno); + throwSocketException(env, err); + return; + } + + jniSetFileDescriptorOfFD(env, fileDescriptor, handle); + + int value = 1; + + // setsockopt(handle, SOL_SOCKET, SO_REUSEPORT, &value, sizeof(jbyte)); + setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)); +} + +/* + * @param timeout in milliseconds. If zero, block until data received + */ +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_receiveStreamImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jbyteArray data, jint offset, jint count, + jint timeout) { + // LOGD("ENTER receiveStreamImpl"); + + int result; + int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + // Cap read length to available buf size + int spaceAvailable = env->GetArrayLength(data) - offset; + int localCount = count < spaceAvailable? count : spaceAvailable; + + jboolean isCopy; + jbyte *body = env->GetByteArrayElements(data, &isCopy); + + // set timeout + struct timeval tv; + tv.tv_sec = timeout / 1000; + tv.tv_usec = (timeout % 1000) * 1000; + setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, + sizeof(struct timeval)); + + do { + result = recv(handle, body + offset, localCount, SOCKET_NOFLAGS); + } while (result < 0 && errno == EINTR); + + env->ReleaseByteArrayElements(data, body, 0); + + /* + * If no bytes are read, return -1 to signal 'endOfFile' + * to the Java input stream + */ + if (0 < result) { + add_recv_stats(handle, result); + return result; + } else if (0 == result) { + return -1; + } else { + // If EAGAIN or EWOULDBLOCK, read timed out + if (errno == EAGAIN || errno == EWOULDBLOCK) { + jniThrowException(env, "java/net/SocketTimeoutException", + netLookupErrorString(SOCKERR_TIMEOUT)); + } else { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + } + return 0; + } +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendStreamImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jbyteArray data, jint offset, jint count) { + // LOGD("ENTER sendStreamImpl"); + + int handle = 0; + int result = 0, sent = 0; + + jboolean isCopy; + jbyte *message = env->GetByteArrayElements(data, &isCopy); + + // Cap write length to available buf size + int spaceAvailable = env->GetArrayLength(data) - offset; + if (count > spaceAvailable) count = spaceAvailable; + + while (sent < count) { + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + if (handle == 0 || handle == -1) { + throwSocketException(env, + sent == 0 ? SOCKERR_BADSOCKET : SOCKERR_INTERRUPTED); + env->ReleaseByteArrayElements(data, message, 0); + return 0; + } + + // LOGD("before select %d", count); + selectWait(handle, SEND_RETRY_TIME, SELECT_WRITE_TYPE); + result = send(handle, (jbyte *)message + offset + sent, + (int) count - sent, SOCKET_NOFLAGS); + + if (result < 0) { + result = errno; + if (result == EAGAIN ||result == EWOULDBLOCK) { + // LOGD("write blocked %d", sent); + continue; + } + env->ReleaseByteArrayElements(data, message, 0); + int err = convertError(result); + log_socket_close(handle, err); + throwSocketException(env, err); + return 0; + } + sent += result; + } + + env->ReleaseByteArrayElements(data, message, 0); + add_send_stats(handle, sent); + return sent; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_shutdownInputImpl(JNIEnv* env, jobject obj, + jobject fileDescriptor) { + // LOGD("ENTER shutdownInputImpl"); + + int ret; + int handle; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + ret = shutdown(handle, SHUT_RD); + + if (ret < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return; + } +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_shutdownOutputImpl(JNIEnv* env, jobject obj, + jobject fileDescriptor) { + // LOGD("ENTER shutdownOutputImpl"); + + int ret; + int handle; + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + return; + } + + ret = shutdown(handle, SHUT_WR); + + if (ret < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + return; + } +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_sendDatagramImpl2(JNIEnv* env, jclass clazz, + jobject fd, jbyteArray data, jint offset, jint length, jint port, + jobject inetAddress) { + // LOGD("ENTER sendDatagramImpl2"); + + jbyte *message; + jbyte nhostAddrBytes[4]; + unsigned short nPort; + int result = 0, sent = 0; + int handle = 0; + struct sockaddr_in sockaddrP; + + if (inetAddress != NULL) { + + result = inetAddressToSocketAddress(env, inetAddress, port, + (struct sockaddr_in *) &sockaddrP); + + if (result < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + + handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return 0; + } + } + + message = (jbyte*) malloc(length * sizeof(jbyte)); + if (message == NULL) { + jniThrowException(env, "java/lang/OutOfMemoryError", + "couldn't allocate enough memory for readSocket"); + return 0; + } + + env->GetByteArrayRegion(data, offset, length, message); + + while (sent < length) { + handle = jniGetFDFromFileDescriptor(env, fd); + + if (handle == 0 || handle == -1) { + throwSocketException(env, + sent == 0 ? SOCKERR_BADSOCKET : SOCKERR_INTERRUPTED); + free(message); + return 0; + } + + result = sendto(handle, (char *) (message + sent), + (int) (length - sent), SOCKET_NOFLAGS, + (struct sockaddr *) &sockaddrP, sizeof(sockaddrP)); + + if (result < 0) { + int err = convertError(errno); + log_socket_close(handle, err); + throwSocketException(env, err); + free(message); + return 0; + } + + sent += result; + } + + free(message); + add_send_stats(handle, sent); + return sent; +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_selectImpl(JNIEnv* env, jclass clazz, + jobjectArray readFDArray, jobjectArray writeFDArray, jint countReadC, + jint countWriteC, jintArray outFlags, jlong timeout) { + // LOGD("ENTER selectImpl"); + + struct timeval timeP; + int result = 0; + int size = 0; + jobject gotFD; + fd_set *fdset_read,*fdset_write; + int handle; + jboolean isCopy ; + jint *flagArray; + int val; + unsigned int time_sec = (unsigned int)timeout/1000; + unsigned int time_msec = (unsigned int)(timeout%1000); + + fdset_read = (fd_set *)malloc(sizeof(fd_set)); + fdset_write = (fd_set *)malloc(sizeof(fd_set)); + + FD_ZERO(fdset_read); + FD_ZERO(fdset_write); + + for (val = 0; valGetObjectArrayElement(readFDArray,val); + + handle = jniGetFDFromFileDescriptor(env, gotFD); + + FD_SET(handle, fdset_read); + + if (0 > (size - handle)) { + size = handle; + } + } + + for (val = 0; valGetObjectArrayElement(writeFDArray,val); + + handle = jniGetFDFromFileDescriptor(env, gotFD); + + FD_SET(handle, fdset_write); + + if (0 > (size - handle)) { + size = handle; + } + } + + /* the size is the max_fd + 1 */ + size =size + 1; + + if (0 > size) { + result = SOCKERR_FDSET_SIZEBAD; + } else { + /* only set when timeout >= 0 (non-block)*/ + if (0 <= timeout) { + + timeP.tv_sec = time_sec; + timeP.tv_usec = time_msec*1000; + + result = sockSelect(size, fdset_read, fdset_write, NULL, &timeP); + + } else { + result = sockSelect(size, fdset_read, fdset_write, NULL, NULL); + } + } + + if (0 < result) { + /*output the result to a int array*/ + flagArray = env->GetIntArrayElements(outFlags, &isCopy); + + for (val=0; valGetObjectArrayElement(readFDArray,val); + + handle = jniGetFDFromFileDescriptor(env, gotFD); + + if (FD_ISSET(handle,fdset_read)) { + flagArray[val] = SOCKET_OP_READ; + } else { + flagArray[val] = SOCKET_OP_NONE; + } + } + + for (val=0; valGetObjectArrayElement(writeFDArray,val); + + handle = jniGetFDFromFileDescriptor(env, gotFD); + + if (FD_ISSET(handle,fdset_write)) { + flagArray[val+countReadC] = SOCKET_OP_WRITE; + } else { + flagArray[val+countReadC] = SOCKET_OP_NONE; + } + } + + env->ReleaseIntArrayElements(outFlags, flagArray, 0); + } + + free(fdset_write); + free(fdset_read); + + /* return both correct and error result, let java handle the exception*/ + return result; +} + +extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketLocalAddressImpl(JNIEnv* env, + jclass clazz, jobject fileDescriptor, jboolean preferIPv6Addresses) { + // LOGD("ENTER getSocketLocalAddressImpl"); + + struct sockaddr_in addr; + socklen_t addrLen = sizeof(addr); + + memset(&addr, 0, addrLen); + + int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + int result; + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_UNKNOWNSOCKET); + return NULL; + } + + result = getsockname(handle, (struct sockaddr *)&addr, &addrLen); + + // Spec says ignore all errors + + return structInToInetAddress(env, &(addr.sin_addr)); + +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketLocalPortImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jboolean preferIPv6Addresses) { + // LOGD("ENTER getSocketLocalPortImpl"); + + struct sockaddr_in addr; + socklen_t addrLen = sizeof(addr); + + int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + int result; + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_UNKNOWNSOCKET); + return 0; + } + + result = getsockname(handle, (struct sockaddr *)&addr, &addrLen); + + if (0 != result) { + // The java spec does not indicate any exceptions on this call + return 0; + } else { + return ntohs(addr.sin_port); + } +} + +extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketOptionImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint anOption) { + // LOGD("ENTER getSocketOptionImpl"); + + int handle; + int intValue = 0; + socklen_t intSize = sizeof(int); + unsigned char byteValue = 0; + socklen_t byteSize = sizeof(unsigned char); + int result; + struct sockaddr_in sockVal; + socklen_t sockSize = sizeof(sockVal); + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return NULL; + } + + switch ((int) anOption & 0xffff) { + case JAVASOCKOPT_SO_LINGER: { + struct linger lingr; + socklen_t size = sizeof(struct linger); + result = getsockopt(handle, SOL_SOCKET, SO_LINGER, &lingr, &size); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + if (!lingr.l_onoff) { + intValue = -1; + } else { + intValue = lingr.l_linger; + } + return newJavaLangInteger(env, intValue); + } + case JAVASOCKOPT_TCP_NODELAY: { + if ((anOption >> 16) & BROKEN_TCP_NODELAY) { + return NULL; + } + result = getsockopt(handle, IPPROTO_TCP, TCP_NODELAY, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangBoolean(env, intValue); + } + case JAVASOCKOPT_MCAST_TTL: { + if ((anOption >> 16) & BROKEN_MULTICAST_TTL) { + return newJavaLangByte(env, 0); + } + result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_TTL, &byteValue, &byteSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangByte(env, (jbyte)(byteValue & 0xFF)); + } + case JAVASOCKOPT_MCAST_INTERFACE: { + if ((anOption >> 16) & BROKEN_MULTICAST_IF) { + return NULL; + } + result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, &sockVal, &sockSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return structInToInetAddress(env, &(sockVal.sin_addr)); + } + case JAVASOCKOPT_SO_SNDBUF: { + result = getsockopt(handle, SOL_SOCKET, SO_SNDBUF, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangInteger(env, intValue); + } + case JAVASOCKOPT_SO_RCVBUF: { + result = getsockopt(handle, SOL_SOCKET, SO_RCVBUF, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangInteger(env, intValue); + } + case JAVASOCKOPT_SO_BROADCAST: { + result = getsockopt(handle, SOL_SOCKET, SO_BROADCAST, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangBoolean(env, intValue); + } + case JAVASOCKOPT_SO_REUSEADDR: { + result = getsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangBoolean(env, intValue); + } + case JAVASOCKOPT_SO_KEEPALIVE: { + result = getsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangBoolean(env, intValue); + } + case JAVASOCKOPT_SO_OOBINLINE: { + result = getsockopt(handle, SOL_SOCKET, SO_OOBINLINE, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangBoolean(env, intValue); + } + case JAVASOCKOPT_IP_MULTICAST_LOOP: { + result = getsockopt(handle, IPPROTO_IP, IP_MULTICAST_LOOP, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangBoolean(env, intValue); + } + case JAVASOCKOPT_IP_TOS: { + result = getsockopt(handle, IPPROTO_IP, IP_TOS, &intValue, &intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangInteger(env, intValue); + } + case JAVASOCKOPT_SO_RCVTIMEOUT: { + struct timeval timeout; + socklen_t size = sizeof(timeout); + result = getsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, &timeout, &size); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return NULL; + } + return newJavaLangInteger(env, timeout.tv_sec * 1000 + timeout.tv_usec/1000); + } + default: { + throwSocketException(env, SOCKERR_OPTUNSUPP); + return NULL; + } + } + +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_setSocketOptionImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor, jint anOption, jobject optVal) { + // LOGD("ENTER setSocketOptionImpl"); + + int handle, result; + int intVal, intSize = sizeof(int); + unsigned char byteVal, byteSize = sizeof(unsigned char); + struct sockaddr_in sockVal; + int sockSize = sizeof(sockVal); + + if (env->IsInstanceOf(optVal, gCachedFields.integer_class)) { + intVal = (int) env->GetIntField(optVal, gCachedFields.integer_class_value); + } else if (env->IsInstanceOf(optVal, gCachedFields.boolean_class)) { + intVal = (int) env->GetBooleanField(optVal, gCachedFields.boolean_class_value); + } else if (env->IsInstanceOf(optVal, gCachedFields.byte_class)) { + byteVal = (int) env->GetByteField(optVal, gCachedFields.byte_class_value); + } else if (env->IsInstanceOf(optVal, gCachedFields.iaddr_class)) { + if (inetAddressToSocketAddress(env, optVal, 0, &sockVal) < 0) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + } else if (env->IsInstanceOf(optVal, gCachedFields.genericipmreq_class)) { + // we'll use optVal directly + } else { + throwSocketException(env, SOCKERR_OPTUNSUPP); + return; + } + + handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + switch ((int) anOption & 0xffff) { + case JAVASOCKOPT_SO_LINGER: { + struct linger lingr; + lingr.l_onoff = intVal > 0 ? 1 : 0; + lingr.l_linger = intVal; + result = setsockopt(handle, SOL_SOCKET, SO_LINGER, &lingr, + sizeof(struct linger)); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_TCP_NODELAY: { + if ((anOption >> 16) & BROKEN_TCP_NODELAY) { + return; + } + result = setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_MCAST_TTL: { + if ((anOption >> 16) & BROKEN_MULTICAST_TTL) { + return; + } + result = setsockopt(handle, IPPROTO_IP, IP_MULTICAST_TTL, &byteVal, byteSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_MCAST_ADD_MEMBERSHIP: { + mcastAddDropMembership(env, handle, optVal, + (anOption >> 16) & BROKEN_MULTICAST_IF, IP_ADD_MEMBERSHIP); + return; + } + + case JAVASOCKOPT_MCAST_DROP_MEMBERSHIP: { + mcastAddDropMembership(env, handle, optVal, + (anOption >> 16) & BROKEN_MULTICAST_IF, IP_DROP_MEMBERSHIP); + return; + } + + case JAVASOCKOPT_MCAST_INTERFACE: { + if ((anOption >> 16) & BROKEN_MULTICAST_IF) { + return; + } + result = setsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, &sockVal, sockSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_SO_SNDBUF: { + result = setsockopt(handle, SOL_SOCKET, SO_SNDBUF, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_SO_RCVBUF: { + result = setsockopt(handle, SOL_SOCKET, SO_RCVBUF, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_SO_BROADCAST: { + result = setsockopt(handle, SOL_SOCKET, SO_BROADCAST, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_SO_REUSEADDR: { + result = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + case JAVASOCKOPT_SO_KEEPALIVE: { + result = setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_SO_OOBINLINE: { + result = setsockopt(handle, SOL_SOCKET, SO_OOBINLINE, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_IP_MULTICAST_LOOP: { + result = setsockopt(handle, IPPROTO_IP, IP_MULTICAST_LOOP, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_IP_TOS: { + result = setsockopt(handle, IPPROTO_IP, IP_TOS, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_REUSEADDR_AND_REUSEPORT: { + // SO_REUSEPORT doesn't need to get set on this System + result = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &intVal, intSize); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + case JAVASOCKOPT_SO_RCVTIMEOUT: { + struct timeval timeout; + timeout.tv_sec = intVal / 1000; + timeout.tv_usec = (intVal % 1000) * 1000; + result = setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(struct timeval)); + if (0 != result) { + throwSocketException(env, convertError(errno)); + return; + } + break; + } + + default: { + throwSocketException(env, SOCKERR_OPTUNSUPP); + } + } +} + +extern "C" jint Java_org_sipdroid_net_impl_OSNetworkSystem_getSocketFlagsImpl(JNIEnv* env, jclass clazz) { + // LOGD("ENTER getSocketFlagsImpl"); + + // Not implemented by harmony + return 0; +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_socketCloseImpl(JNIEnv* env, jclass clazz, + jobject fileDescriptor) { + // LOGD("ENTER socketCloseImpl"); + + int handle = jniGetFDFromFileDescriptor(env, fileDescriptor); + + if (handle == 0 || handle == -1) { + throwSocketException(env, SOCKERR_BADSOCKET); + return; + } + + log_socket_close(handle, SOCKET_CLOSE_LOCAL); + + jniSetFileDescriptorOfFD(env, fileDescriptor, -1); + + close(handle); +} + +extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getHostByAddrImpl(JNIEnv* env, jclass clazz, + jbyteArray addrStr) { + // LOGD("ENTER getHostByAddrImpl"); + + if (addrStr == NULL) { + throwNullPointerException(env); + return JNI_FALSE; + } + + jstring address = (jstring)newJavaLangString(env, addrStr); + jstring result; + const char* addr = env->GetStringUTFChars(address, NULL); + + struct hostent* ent = gethostbyaddr(addr, strlen(addr), AF_INET); + + if (ent != NULL && ent->h_name != NULL) { + result = env->NewStringUTF(ent->h_name); + } else { + result = NULL; + } + + env->ReleaseStringUTFChars(address, addr); + + return result; +} + +extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_getHostByNameImpl(JNIEnv* env, jclass clazz, + jstring nameStr, jboolean preferIPv6Addresses) { + // LOGD("ENTER getHostByNameImpl"); + + if (nameStr == NULL) { + throwNullPointerException(env); + return NULL; + } + + const char* name = env->GetStringUTFChars(nameStr, NULL); + + if (useAdbNetworking) { + + union { + struct in_addr a; + jbyte j[4]; + } outaddr; + + // LOGD("ADB networking: +gethostbyname '%s'", name); + int err; + err = adb_networking_gethostbyname(name, &(outaddr.a)); + + env->ReleaseStringUTFChars(nameStr, name); +#if 0 + LOGD("ADB networking: -gethostbyname err %d addr 0x%08x %u.%u.%u.%u", + err, (unsigned int)outaddr.a.s_addr, + outaddr.j[0],outaddr.j[1], + outaddr.j[2],outaddr.j[3]); +#endif + + if (err < 0) { + return NULL; + } else { + jbyteArray addr = env->NewByteArray(4); + env->SetByteArrayRegion(addr, 0, 4, outaddr.j); + return addr; + } + } else { + + // normal case...no adb networking + struct hostent* ent = gethostbyname(name); + + env->ReleaseStringUTFChars(nameStr, name); + + if (ent != NULL && ent->h_length > 0) { + jbyteArray addr = env->NewByteArray(4); + jbyte v[4]; + memcpy(v, ent->h_addr, 4); + env->SetByteArrayRegion(addr, 0, 4, v); + return addr; + } else { + return NULL; + } + } +} + +extern "C" void Java_org_sipdroid_net_impl_OSNetworkSystem_setInetAddressImpl(JNIEnv* env, jobject obj, + jobject sender, jbyteArray address) { + // LOGD("ENTER setInetAddressImpl"); + + env->SetObjectField(sender, gCachedFields.iaddr_ipaddress, address); +} + +/* +extern "C" jobject Java_org_sipdroid_net_impl_OSNetworkSystem_inheritedChannelImpl(JNIEnv* env, jobject obj) { + // LOGD("ENTER inheritedChannelImpl"); + + int socket = 0; + int opt; + socklen_t length = sizeof(opt); + int socket_type; + struct sockaddr_in local_addr; + struct sockaddr_in remote_addr; + jclass channel_class, socketaddr_class, serverSocket_class, socketImpl_class; + jobject channel_object = NULL, socketaddr_object, serverSocket_object; + jobject fd_object, addr_object, localAddr_object, socketImpl_object; + jfieldID port_field, socketaddr_field, bound_field, fd_field; + jfieldID serverSocket_field, socketImpl_field, addr_field, localAddr_field; + jmethodID channel_new; + jbyteArray addr_array; + struct sockaddr_in *sock; + jbyte * address; + jbyte * localAddr; + jboolean jtrue = JNI_TRUE; + + if (0 != getsockopt(socket, SOL_SOCKET, SO_TYPE, &opt, &length)) { + return NULL; + } + if (SOCK_STREAM !=opt && SOCK_DGRAM !=opt) { + return NULL; + } + socket_type = opt; + + length = sizeof(struct sockaddr); + if (0 != getsockname(socket, (struct sockaddr *)&local_addr, &length)) { + return NULL; + } else { + if (AF_INET != local_addr.sin_family || length != sizeof(struct sockaddr)) { + return NULL; + } + localAddr = (jbyte*) malloc(sizeof(jbyte)*4); + if (NULL == localAddr) { + return NULL; + } + memcpy (localAddr, &(local_addr.sin_addr.s_addr), 4); + } + if (0 != getpeername(socket, (struct sockaddr *)&remote_addr, &length)) { + remote_addr.sin_port = 0; + remote_addr.sin_addr.s_addr = 0; + address = (jbyte*) malloc(sizeof(jbyte)*4); + bzero(address, sizeof(jbyte)*4); + } else { + if (AF_INET != remote_addr.sin_family + || length != sizeof(struct sockaddr)) { + return NULL; + } + address = (jbyte*) malloc(sizeof(jbyte)*4); + memcpy (address, &(remote_addr.sin_addr.s_addr), 4); + } + + // analysis end, begin pack to java + if (SOCK_STREAM == opt) { + if (remote_addr.sin_port!=0) { + //socket + channel_class = env->FindClass( + "org/apache/harmony/nio/internal/SocketChannelImpl"); + if (NULL == channel_class) { + goto clean; + } + + channel_new = env->GetMethodID(channel_class, "", "()V"); + if (NULL == channel_new) { + goto clean; + } + channel_object = env->NewObject(channel_class, channel_new); + if (NULL == channel_object) { + goto clean; + } + // new and set FileDescript + + fd_field = env->GetFieldID(channel_class, "fd", + "java/io/FielDescriptor"); + fd_object = env->GetObjectField(channel_object, fd_field); + if (NULL == fd_object) { + goto clean; + } + + jniSetFileDescriptorOfFD(env, fd_object, socket); + + // local port + port_field = env->GetFieldID(channel_class, "localPort", "I"); + env->SetIntField(channel_object, port_field, + ntohs(local_addr.sin_port)); + + // new and set remote addr + addr_object = env->NewObject(gCachedFields.iaddr_class, + gCachedFields.iaddr_class_init); + if (NULL == addr_object) { + goto clean; + } + socketaddr_class = env->FindClass("java/net/InetSocketAddress"); + socketaddr_field = env->GetFieldID(channel_class, "connectAddress", + "Ljava/net/InetSocketAddress;"); + socketaddr_object = env->GetObjectField(channel_object, + socketaddr_field); + if (NULL == socketaddr_object) { + goto clean; + } + addr_field = env->GetFieldID(socketaddr_class, "addr", + "Ljava/net/InetAddress;"); + env->SetObjectField(socketaddr_object, addr_field, addr_object); + addr_array = env->NewByteArray((jsize)4); + env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, address); + env->SetObjectField(addr_object, gCachedFields.iaddr_ipaddress, + addr_array); + + // localAddr + socketaddr_class = env->FindClass("java/net/InetSocketAddress"); + socketaddr_field = env->GetFieldID(channel_class, "connectAddress", + "Ljava/net/InetSocketAddress;"); + socketaddr_object = env->GetObjectField(channel_object, + socketaddr_field); + + localAddr_field = env->GetFieldID(channel_class, "localAddress", + "Ljava/net/InetAddress;"); + localAddr_object = env->NewObject(gCachedFields.iaddr_class, + gCachedFields.iaddr_class_init); + jfieldID socketaddr_field = env->GetFieldID(channel_class, + "connectAddress", "Ljava/net/InetSocketAddress;"); + jobject socketaddr_object = env->GetObjectField(channel_object, + socketaddr_field); + env->SetObjectField(socketaddr_object, localAddr_field, + localAddr_object); + if (NULL == localAddr_object) { + goto clean; + } + addr_array = env->NewByteArray((jsize)4); + env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, localAddr); + env->SetObjectField(localAddr_object, gCachedFields.iaddr_ipaddress, + addr_array); + + + // set port + port_field = env->GetFieldID(socketaddr_class, "port", "I"); + env->SetIntField(socketaddr_object, port_field, + ntohs(remote_addr.sin_port)); + + // set bound + if (0 != local_addr.sin_port) { + bound_field = env->GetFieldID(channel_class, "isBound", "Z"); + env->SetBooleanField(channel_object, bound_field, jtrue); + } + + } else { + //serverSocket + channel_class = env->FindClass( + "org/apache/harmony/nio/internal/ServerSocketChannelImpl"); + if (NULL == channel_class) { + goto clean; + } + + channel_new = env->GetMethodID(channel_class, "", "()V"); + if (NULL == channel_new) { + goto clean; + } + channel_object = env->NewObject(channel_class, channel_new); + if (NULL == channel_object) { + goto clean; + } + + serverSocket_field = env->GetFieldID(channel_class, "socket", + "Ljava/net/ServerSocket;"); + serverSocket_class = env->FindClass("Ljava/net/ServerSocket;"); + serverSocket_object = env->GetObjectField(channel_object, + serverSocket_field); + // set bound + if (0 != local_addr.sin_port) { + bound_field = env->GetFieldID(channel_class, "isBound", "Z"); + env->SetBooleanField(channel_object, bound_field, jtrue); + bound_field = env->GetFieldID(serverSocket_class, "isBound", "Z"); + env->SetBooleanField(serverSocket_object, bound_field, jtrue); + } + // localAddr + socketImpl_class = env->FindClass("java/net/SocketImpl"); + socketImpl_field = env->GetFieldID(channel_class, "impl", + "Ljava/net/SocketImpl;"); + socketImpl_object = env->GetObjectField(channel_object, + socketImpl_field); + if (NULL == socketImpl_object) { + goto clean; + } + + localAddr_field = env->GetFieldID(channel_class, "localAddress", + "Ljava/net/InetAddress;"); + localAddr_object = env->NewObject(gCachedFields.iaddr_class, + gCachedFields.iaddr_class_init); + if (NULL == localAddr_object) { + goto clean; + } + env->SetObjectField(socketImpl_object, localAddr_field, + localAddr_object); + addr_array = env->NewByteArray((jsize)4); + env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, localAddr); + env->SetObjectField(localAddr_object, + gCachedFields.iaddr_ipaddress, addr_array); + + // set port + port_field = env->GetFieldID(socketImpl_class, "localport", "I"); + env->SetIntField(socketImpl_object, port_field, + ntohs(local_addr.sin_port)); + } + } else { + //Datagram Socket + // new DatagramChannel + channel_class = env->FindClass( + "org/apache/harmony/nio/internal/DatagramChannelImpl"); + if (NULL == channel_class) { + goto clean; + } + + channel_new = env->GetMethodID(channel_class, "", "()V"); + if (NULL == channel_new) { + goto clean; + } + channel_object = env->NewObject(channel_class, channel_new); + if (NULL == channel_object) { + goto clean; + } + + // new and set FileDescript + fd_field = env->GetFieldID(channel_class, "fd", "java/io/FileDescriptor"); + fd_object = env->GetObjectField(channel_object, fd_field); + if (NULL == fd_object) { + goto clean; + } + + jniSetFileDescriptorOfFD(env, fd_object, socket); + + port_field = env->GetFieldID(channel_class, "localPort", "I"); + env->SetIntField(channel_object, port_field, ntohs(local_addr.sin_port)); + + // new and set remote addr + addr_object = env->NewObject(gCachedFields.iaddr_class, + gCachedFields.iaddr_class_init); + if (NULL == addr_object) { + goto clean; + } + socketaddr_class = env->FindClass("java/net/InetSocketAddress"); + socketaddr_field = env->GetFieldID(channel_class, "connectAddress", + "Ljava/net/InetSocketAddress;"); + socketaddr_object = env->GetObjectField(channel_object, socketaddr_field); + if (NULL == socketaddr_object) { + goto clean; + } + addr_field = env->GetFieldID(socketaddr_class, "addr", + "Ljava/net/InetAddress;"); + env->SetObjectField(socketaddr_object, addr_field, addr_object); + addr_array = env->NewByteArray((jsize)4); + env->SetByteArrayRegion(addr_array, (jsize)0, (jsize)4, address); + env->SetObjectField(addr_object, gCachedFields.iaddr_ipaddress, addr_array); + + // set bound + if (0 != local_addr.sin_port) { + bound_field = env->GetFieldID(channel_class, "isBound", "Z"); + env->SetBooleanField(channel_object, bound_field, jtrue); + } + } +clean: + free(address); + free(localAddr); + return channel_object; +} +*/ diff --git a/jni/bv16_jni.cpp b/app/src/main/jni/bv16_jni.cpp similarity index 100% rename from jni/bv16_jni.cpp rename to app/src/main/jni/bv16_jni.cpp diff --git a/jni/bx16_fixedp/bv16/bitpack.c b/app/src/main/jni/bx16_fixedp/bv16/bitpack.c similarity index 97% rename from jni/bx16_fixedp/bv16/bitpack.c rename to app/src/main/jni/bx16_fixedp/bv16/bitpack.c index e1cccf4..fbc647a 100644 --- a/jni/bx16_fixedp/bv16/bitpack.c +++ b/app/src/main/jni/bx16_fixedp/bv16/bitpack.c @@ -1,215 +1,215 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - bitpack.c: BV16 bit packing routines - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16.h" - -/**************************************************************************** -* -* BV16_BITPACK - BroadVoice16 Encoded Bit Pack Function -* -* PURPOSE: -* -* This function take the encoded bit structure (15 words) and packed it -* into a bit stream (5 words) for sending it across the network. -* -* PROTOTYPE: -* BV16_BitPack ( UINT16 * PackedStream, sBV16bs * BitStruct ); -* -* PARAMETERS: -* PackedStream => pointer to the outgoing encoded stream -* BitStruct => pointer to the encoded bit structure -* -* RETURNS: -* Nothing -* -* NOTE: -* The following is the bit table within the bit structure for -* BroadVoice16 -* -* Word16 bit_table[] = { -* 7, 7, // LSP -* 7, // Pitch Lag -* 5, // Pitch Gain -* 4, // Excitation Vector Log-Gain -* 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 // Excitation Vector -* }; -* -****************************************************************************/ -void BV16_BitPack (UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ) -{ - UWord32 temppack; - - /* fill the first 16 bit word */ - temppack = ( ((UWord32)BitStruct->lspidx[0]) << 25 ); /* 32-7 */ - temppack |= ( ((UWord32)BitStruct->lspidx[1]) << 18 ); /* 25-7 */ - temppack |= ( ((UWord32)BitStruct->ppidx) << 11 ); /* 18-7 */ - /* total=21 */ - - /* store 1st byte in the payload */ - *PackedStream++ = (UWord8)(temppack >> 24); - - /* store 2nd byte in the payload */ - *PackedStream++ = (UWord8)((temppack << 8) >> 24); - - /* clear the upper 16 bits */ - temppack = temppack << 16; - - temppack |= ( ((UWord32)BitStruct->bqidx) << 22 ); /* 32-(21-16)-5 = 32-10 */ - temppack |= ( ((UWord32)BitStruct->gidx) << 18 ); /* 22-4 */ - temppack |= ( ((UWord32)BitStruct->qvidx[0]) << 13 ); /* 18-5 */ - /* total=19 */ - - /* store 3rd byte in the payload */ - *PackedStream++ = (UWord8)(temppack >> 24); - - /* store 4th byte in the payload */ - *PackedStream++ = (UWord8)((temppack << 8) >> 24); - - /* clear the upper 16 bits */ - temppack = temppack << 16; - - temppack |= ( ((UWord32)BitStruct->qvidx[1]) << 24 ); /* 32-(19-16)-5 = 32-8 */ - temppack |= ( ((UWord32)BitStruct->qvidx[2]) << 19 ); /* 24-5 */ - temppack |= ( ((UWord32)BitStruct->qvidx[3]) << 14 ); /* 19-5 */ - /* total=18 */ - - /* store 5th byte in the payload */ - *PackedStream++ = (UWord8)(temppack >> 24); - - /* store 6th byte in the payload */ - *PackedStream++ = (UWord8)((temppack << 8) >> 24); - - /* clear the upper 16 bits */ - temppack = temppack << 16; - - temppack |= ( ((UWord32)BitStruct->qvidx[4]) << 25 ); /* 32-(18-16)-5 = 32-7 */ - temppack |= ( ((UWord32)BitStruct->qvidx[5]) << 20 ); /* 25-5 */ - temppack |= ( ((UWord32)BitStruct->qvidx[6]) << 15 ); /* 20-5 */ - /* total=17 */ - - /* store 7th byte in the payload */ - *PackedStream++ = (UWord8)(temppack >> 24); - - /* store 8th byte in the payload */ - *PackedStream++ = (UWord8)((temppack << 8) >> 24); - - /* clear the upper 16 bits */ - temppack = temppack << 16; - - temppack |= ( ((UWord32)BitStruct->qvidx[7]) << 26 ); /* 32-(17-16)-5 = 32-6 */ - temppack |= ( ((UWord32)BitStruct->qvidx[8]) << 21 ); /* 25-5 */ - temppack |= ( ((UWord32)BitStruct->qvidx[9]) << 16 ); /* 20-5 */ - /* total=18 */ - - /* store 9th byte in the payload */ - *PackedStream++ = (UWord8)(temppack >> 24); - - /* store 10th byte in the payload */ - *PackedStream++ = (UWord8)((temppack << 8) >> 24); - - return; -} - - -/**************************************************************************** -* -* BV16_BITUNPACK - BroadVoice16 Encoded Bit Unpack Function -* -* PURPOSE: -* -* This function take the encoded bit stream (5 words) and unpack it -* into a bit structure (15 words) for BroadVoice16 decoder. -* -* PROTOTYPE: -* BV16_BitUnPack ( UINT16 * PackedStream, sBV16bs * BitStruct ); -* -* PARAMETERS: -* PackedStream => pointer to the incoming encoded bit stream -* BitStruct => pointer to the bit structure for decoder -* -* RETURNS: -* Nothing -* -* NOTE: -* The following is the bit table within the bit structure for -* BroadVoice16 -* -* Word16 bit_table[] = { -* 7, 7, // LSP -* 7, // Pitch Lag -* 5, // Pitch Gain -* 4, // Excitation Vector Log-Gain -* 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 // Excitation Vector -* }; -* -* -****************************************************************************/ -void BV16_BitUnPack (UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ) -{ - UWord32 bitword32; - - /* unpack bytes 1 and 2 of bit stream */ - bitword32 = (UWord32)*PackedStream++; - bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; - BitStruct->lspidx[0] = (short)( bitword32 >> 9 ); - BitStruct->lspidx[1] = (short)( ( bitword32 & 0x01FF )>> 2 ); - - /* unpack bytes 3 and 4 of bit stream */ - bitword32 = ((bitword32 & 0x0003) << 8) | (UWord32) *PackedStream++; - bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; - BitStruct->ppidx = (short)( bitword32 >> 11 ); - BitStruct->bqidx = (short)( ( bitword32 & 0x07FF ) >> 6 ); - BitStruct->gidx = (short)( ( bitword32 & 0x003F ) >> 2 ); - - /* unpack bytes 5 and 6 of bit stream */ - bitword32 = ((bitword32 & 0x0003) << 8) | (UWord32) *PackedStream++; - bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; - BitStruct->qvidx[0] = (short)( bitword32 >> 13 ); - BitStruct->qvidx[1] = (short)( ( bitword32 & 0x1FFF ) >> 8 ); - BitStruct->qvidx[2] = (short)( ( bitword32 & 0x00FF ) >> 3 ); - - /* unpack bytes 7 and 8 of bit stream */ - bitword32 = ((bitword32 & 0x0007) << 8) | (UWord32) *PackedStream++; - bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; - BitStruct->qvidx[3] = (short)( bitword32 >> 14 ); - BitStruct->qvidx[4] = (short)( ( bitword32 & 0x3FFF ) >> 9 ); - BitStruct->qvidx[5] = (short)( ( bitword32 & 0x01FF ) >> 4 ); - - /* unpack bytes 9 and 10 of bit stream */ - bitword32 = ((bitword32 & 0x000F) << 8) | (UWord32) *PackedStream++; - bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; - BitStruct->qvidx[6] = (short)( ( bitword32 >> 15 ) ); - BitStruct->qvidx[7] = (short)( ( bitword32 & 0x7FFF ) >> 10 ); - BitStruct->qvidx[8] = (short)( ( bitword32 & 0x03FF ) >> 5 ); - BitStruct->qvidx[9] = (short)( bitword32 & 0x001F ); - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + bitpack.c: BV16 bit packing routines + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16.h" + +/**************************************************************************** +* +* BV16_BITPACK - BroadVoice16 Encoded Bit Pack Function +* +* PURPOSE: +* +* This function take the encoded bit structure (15 words) and packed it +* into a bit stream (5 words) for sending it across the network. +* +* PROTOTYPE: +* BV16_BitPack ( UINT16 * PackedStream, sBV16bs * BitStruct ); +* +* PARAMETERS: +* PackedStream => pointer to the outgoing encoded stream +* BitStruct => pointer to the encoded bit structure +* +* RETURNS: +* Nothing +* +* NOTE: +* The following is the bit table within the bit structure for +* BroadVoice16 +* +* Word16 bit_table[] = { +* 7, 7, // LSP +* 7, // Pitch Lag +* 5, // Pitch Gain +* 4, // Excitation Vector Log-Gain +* 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 // Excitation Vector +* }; +* +****************************************************************************/ +void BV16_BitPack (UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ) +{ + UWord32 temppack; + + /* fill the first 16 bit word */ + temppack = ( ((UWord32)BitStruct->lspidx[0]) << 25 ); /* 32-7 */ + temppack |= ( ((UWord32)BitStruct->lspidx[1]) << 18 ); /* 25-7 */ + temppack |= ( ((UWord32)BitStruct->ppidx) << 11 ); /* 18-7 */ + /* total=21 */ + + /* store 1st byte in the payload */ + *PackedStream++ = (UWord8)(temppack >> 24); + + /* store 2nd byte in the payload */ + *PackedStream++ = (UWord8)((temppack << 8) >> 24); + + /* clear the upper 16 bits */ + temppack = temppack << 16; + + temppack |= ( ((UWord32)BitStruct->bqidx) << 22 ); /* 32-(21-16)-5 = 32-10 */ + temppack |= ( ((UWord32)BitStruct->gidx) << 18 ); /* 22-4 */ + temppack |= ( ((UWord32)BitStruct->qvidx[0]) << 13 ); /* 18-5 */ + /* total=19 */ + + /* store 3rd byte in the payload */ + *PackedStream++ = (UWord8)(temppack >> 24); + + /* store 4th byte in the payload */ + *PackedStream++ = (UWord8)((temppack << 8) >> 24); + + /* clear the upper 16 bits */ + temppack = temppack << 16; + + temppack |= ( ((UWord32)BitStruct->qvidx[1]) << 24 ); /* 32-(19-16)-5 = 32-8 */ + temppack |= ( ((UWord32)BitStruct->qvidx[2]) << 19 ); /* 24-5 */ + temppack |= ( ((UWord32)BitStruct->qvidx[3]) << 14 ); /* 19-5 */ + /* total=18 */ + + /* store 5th byte in the payload */ + *PackedStream++ = (UWord8)(temppack >> 24); + + /* store 6th byte in the payload */ + *PackedStream++ = (UWord8)((temppack << 8) >> 24); + + /* clear the upper 16 bits */ + temppack = temppack << 16; + + temppack |= ( ((UWord32)BitStruct->qvidx[4]) << 25 ); /* 32-(18-16)-5 = 32-7 */ + temppack |= ( ((UWord32)BitStruct->qvidx[5]) << 20 ); /* 25-5 */ + temppack |= ( ((UWord32)BitStruct->qvidx[6]) << 15 ); /* 20-5 */ + /* total=17 */ + + /* store 7th byte in the payload */ + *PackedStream++ = (UWord8)(temppack >> 24); + + /* store 8th byte in the payload */ + *PackedStream++ = (UWord8)((temppack << 8) >> 24); + + /* clear the upper 16 bits */ + temppack = temppack << 16; + + temppack |= ( ((UWord32)BitStruct->qvidx[7]) << 26 ); /* 32-(17-16)-5 = 32-6 */ + temppack |= ( ((UWord32)BitStruct->qvidx[8]) << 21 ); /* 25-5 */ + temppack |= ( ((UWord32)BitStruct->qvidx[9]) << 16 ); /* 20-5 */ + /* total=18 */ + + /* store 9th byte in the payload */ + *PackedStream++ = (UWord8)(temppack >> 24); + + /* store 10th byte in the payload */ + *PackedStream++ = (UWord8)((temppack << 8) >> 24); + + return; +} + + +/**************************************************************************** +* +* BV16_BITUNPACK - BroadVoice16 Encoded Bit Unpack Function +* +* PURPOSE: +* +* This function take the encoded bit stream (5 words) and unpack it +* into a bit structure (15 words) for BroadVoice16 decoder. +* +* PROTOTYPE: +* BV16_BitUnPack ( UINT16 * PackedStream, sBV16bs * BitStruct ); +* +* PARAMETERS: +* PackedStream => pointer to the incoming encoded bit stream +* BitStruct => pointer to the bit structure for decoder +* +* RETURNS: +* Nothing +* +* NOTE: +* The following is the bit table within the bit structure for +* BroadVoice16 +* +* Word16 bit_table[] = { +* 7, 7, // LSP +* 7, // Pitch Lag +* 5, // Pitch Gain +* 4, // Excitation Vector Log-Gain +* 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 // Excitation Vector +* }; +* +* +****************************************************************************/ +void BV16_BitUnPack (UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ) +{ + UWord32 bitword32; + + /* unpack bytes 1 and 2 of bit stream */ + bitword32 = (UWord32)*PackedStream++; + bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; + BitStruct->lspidx[0] = (short)( bitword32 >> 9 ); + BitStruct->lspidx[1] = (short)( ( bitword32 & 0x01FF )>> 2 ); + + /* unpack bytes 3 and 4 of bit stream */ + bitword32 = ((bitword32 & 0x0003) << 8) | (UWord32) *PackedStream++; + bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; + BitStruct->ppidx = (short)( bitword32 >> 11 ); + BitStruct->bqidx = (short)( ( bitword32 & 0x07FF ) >> 6 ); + BitStruct->gidx = (short)( ( bitword32 & 0x003F ) >> 2 ); + + /* unpack bytes 5 and 6 of bit stream */ + bitword32 = ((bitword32 & 0x0003) << 8) | (UWord32) *PackedStream++; + bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; + BitStruct->qvidx[0] = (short)( bitword32 >> 13 ); + BitStruct->qvidx[1] = (short)( ( bitword32 & 0x1FFF ) >> 8 ); + BitStruct->qvidx[2] = (short)( ( bitword32 & 0x00FF ) >> 3 ); + + /* unpack bytes 7 and 8 of bit stream */ + bitword32 = ((bitword32 & 0x0007) << 8) | (UWord32) *PackedStream++; + bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; + BitStruct->qvidx[3] = (short)( bitword32 >> 14 ); + BitStruct->qvidx[4] = (short)( ( bitword32 & 0x3FFF ) >> 9 ); + BitStruct->qvidx[5] = (short)( ( bitword32 & 0x01FF ) >> 4 ); + + /* unpack bytes 9 and 10 of bit stream */ + bitword32 = ((bitword32 & 0x000F) << 8) | (UWord32) *PackedStream++; + bitword32 = (bitword32 << 8) | (UWord32)*PackedStream++; + BitStruct->qvidx[6] = (short)( ( bitword32 >> 15 ) ); + BitStruct->qvidx[7] = (short)( ( bitword32 & 0x7FFF ) >> 10 ); + BitStruct->qvidx[8] = (short)( ( bitword32 & 0x03FF ) >> 5 ); + BitStruct->qvidx[9] = (short)( bitword32 & 0x001F ); + + return; +} diff --git a/jni/bx16_fixedp/bv16/bitpack.h b/app/src/main/jni/bx16_fixedp/bv16/bitpack.h similarity index 98% rename from jni/bx16_fixedp/bv16/bitpack.h rename to app/src/main/jni/bx16_fixedp/bv16/bitpack.h index 032cf42..0401c2c 100644 --- a/jni/bx16_fixedp/bv16/bitpack.h +++ b/app/src/main/jni/bx16_fixedp/bv16/bitpack.h @@ -1,35 +1,35 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - - -/***************************************************************************** - bitpack.h: BV16 bit packing routines - - $Log$ -******************************************************************************/ - -#ifndef BITPACK_H -#define BITPACK_H - -void BV16_BitPack(UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ); -void BV16_BitUnPack(UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ); - -#endif +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + + +/***************************************************************************** + bitpack.h: BV16 bit packing routines + + $Log$ +******************************************************************************/ + +#ifndef BITPACK_H +#define BITPACK_H + +void BV16_BitPack(UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ); +void BV16_BitUnPack(UWord8 * PackedStream, struct BV16_Bit_Stream * BitStruct ); + +#endif diff --git a/jni/bx16_fixedp/bv16/bv.c b/app/src/main/jni/bx16_fixedp/bv16/bv.c similarity index 97% rename from jni/bx16_fixedp/bv16/bv.c rename to app/src/main/jni/bx16_fixedp/bv16/bv.c index 1bc49f2..266c00b 100644 --- a/jni/bx16_fixedp/bv16/bv.c +++ b/app/src/main/jni/bx16_fixedp/bv16/bv.c @@ -1,231 +1,231 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - bv.c : BroadVoice16 Codec Entry Interface Program - - $Log$ -******************************************************************************/ - -#include -#include -#include -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16.h" -#include "utility.h" -#if G192BITSTREAM -#include "g192.h" -#else -#include "bitpack.h" -#endif -#include "memutil.h" - -int frame; -Word16 bfi=0; - -void usage(char *name) -{ - fprintf(stderr,"usage: %s enc|dec input output\n", name); - fprintf(stderr,"\nFormat for speech_file:\n Binary file of 8 kHz sampled 16-bit PCM data.\n"); -#if G192BITSTREAM - fprintf(stderr,"\nFormat for bitstream_file per frame: ITU-T G.192 format\n\ - One (2-byte) synchronization word [0x6B21],\n\ - One (2-byte) size word,\n\ - 160 words (2-byte) containing 160 bits.\n\n"); -#else - fprintf(stderr, "\nFormat for bitstream_file per frame: Packed Bits\n"); -#endif - exit(1); -} - -int main(int argc, char **argv) -{ - FILE *fi, *fo, *fbdi=NULL; - int enc=1, sizebitstream, sizestate; - int nread, frsz, i; - Word16 *x; - void *state, *bs; -#if !G192BITSTREAM - UWord8 PackedStream[10]; -#endif - - int next_bad_frame=-1; - - fprintf(stderr,"/***************************************************************************/\n"); - fprintf(stderr,"/* BroadVoice(R)16, Copyright (c) 2000-09, Broadcom Corporation. */\n"); - fprintf(stderr,"/* All Rights Reserved. */\n"); - fprintf(stderr,"/* */\n"); - fprintf(stderr,"/* This software is provided under the GNU Lesser General Public License, */\n"); - fprintf(stderr,"/* version 2.1, as published by the Free Software Foundation (\"LGPL\"). */\n"); - fprintf(stderr,"/* This program is distributed in the hope that it will be useful, but */\n"); - fprintf(stderr,"/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */\n"); - fprintf(stderr,"/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */\n"); - fprintf(stderr,"/* more details. A copy of the LGPL is available at */\n"); - fprintf(stderr,"/* http://www.broadcom.com/licenses/LGPLv2.1.php, */\n"); - fprintf(stderr,"/* or by writing to the Free Software Foundation, Inc., */\n"); - fprintf(stderr,"/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */\n"); - fprintf(stderr,"/***************************************************************************/\n"); - - if ((argc!=4)&&(argc!=5)) usage(argv[0]); - if (!strcmp(argv[1],"enc")) enc=1; - else if (!strcmp(argv[1],"dec")) enc=0; - else usage(argv[0]); - - if (!(fi=fopen(argv[2],"rb"))) - { - fprintf(stderr,"error: can't read %s\n", argv[2]); - exit(2); - } - if (!(fo=fopen(argv[3],"wb"))) - { - fprintf(stderr,"error: can't write to %s\n", argv[3]); - exit(3); - } - if (argc==5) - { - if (!(fbdi=fopen(argv[4],"rb"))) - { - fprintf(stderr,"error: can't read %s\n", argv[4]); - exit(3); - } - } - - frsz = FRSZ; - sizebitstream = sizeof(struct BV16_Bit_Stream); - if (!strcmp(argv[1],"enc")) - { - sizestate = sizeof(struct BV16_Encoder_State); - state = allocWord16(0,sizeof(struct BV16_Encoder_State)/2-1); - Reset_BV16_Encoder((struct BV16_Encoder_State*)state); - } - else - { - sizestate = sizeof(struct BV16_Decoder_State); - state = allocWord16(0,sizeof(struct BV16_Decoder_State)/2-1); - Reset_BV16_Decoder((struct BV16_Decoder_State*)state); - } - - bs = allocWord16(0,sizebitstream/2-1); - x = allocWord16(0, frsz-1); - - if (enc){ -#if G192BITSTREAM - fprintf(stderr," BroadVoice16 Fixed-Point Encoder V1.1 with ITU-T G.192\n"); -#else - fprintf(stderr," BroadVoice16 Fixed-Point Encoder V1.1 with packed bit-stream\n"); -#endif - fprintf(stderr," Input speech file : %s\n",argv[2]); - fprintf(stderr," Output bit-stream file: %s\n",argv[3]); - } - else{ -#if G192BITSTREAM - fprintf(stderr," BroadVoice16 Fixed-Point Decoder V1.1 with ITU-T G.192\n"); -#else - fprintf(stderr," BroadVoice16 Fixed-Point Decoder V1.1 with packed bit-stream\n"); -#endif - fprintf(stderr," Input bit-stream file : %s\n",argv[2]); - fprintf(stderr," Output speech file : %s\n",argv[3]); - } - - /* START THE MAIN FRAME LOOP */ - frame=0; - /* read for the 1st bad frame */ - if (fbdi!=NULL) - fscanf(fbdi,"%d", &next_bad_frame); - - while (1) - { - - /* FRAME COUNTER */ - frame++; - - /* READ IN ONE SPEECH FRAME */ - if (enc==1) - { - nread=fread(x, sizeof(Word16), frsz, fi); - if (nread<=0) goto End; - for (i=nread;i +#include +#include +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16.h" +#include "utility.h" +#if G192BITSTREAM +#include "g192.h" +#else +#include "bitpack.h" +#endif +#include "memutil.h" + +int frame; +Word16 bfi=0; + +void usage(char *name) +{ + fprintf(stderr,"usage: %s enc|dec input output\n", name); + fprintf(stderr,"\nFormat for speech_file:\n Binary file of 8 kHz sampled 16-bit PCM data.\n"); +#if G192BITSTREAM + fprintf(stderr,"\nFormat for bitstream_file per frame: ITU-T G.192 format\n\ + One (2-byte) synchronization word [0x6B21],\n\ + One (2-byte) size word,\n\ + 160 words (2-byte) containing 160 bits.\n\n"); +#else + fprintf(stderr, "\nFormat for bitstream_file per frame: Packed Bits\n"); +#endif + exit(1); +} + +int main(int argc, char **argv) +{ + FILE *fi, *fo, *fbdi=NULL; + int enc=1, sizebitstream, sizestate; + int nread, frsz, i; + Word16 *x; + void *state, *bs; +#if !G192BITSTREAM + UWord8 PackedStream[10]; +#endif + + int next_bad_frame=-1; + + fprintf(stderr,"/***************************************************************************/\n"); + fprintf(stderr,"/* BroadVoice(R)16, Copyright (c) 2000-09, Broadcom Corporation. */\n"); + fprintf(stderr,"/* All Rights Reserved. */\n"); + fprintf(stderr,"/* */\n"); + fprintf(stderr,"/* This software is provided under the GNU Lesser General Public License, */\n"); + fprintf(stderr,"/* version 2.1, as published by the Free Software Foundation (\"LGPL\"). */\n"); + fprintf(stderr,"/* This program is distributed in the hope that it will be useful, but */\n"); + fprintf(stderr,"/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */\n"); + fprintf(stderr,"/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */\n"); + fprintf(stderr,"/* more details. A copy of the LGPL is available at */\n"); + fprintf(stderr,"/* http://www.broadcom.com/licenses/LGPLv2.1.php, */\n"); + fprintf(stderr,"/* or by writing to the Free Software Foundation, Inc., */\n"); + fprintf(stderr,"/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */\n"); + fprintf(stderr,"/***************************************************************************/\n"); + + if ((argc!=4)&&(argc!=5)) usage(argv[0]); + if (!strcmp(argv[1],"enc")) enc=1; + else if (!strcmp(argv[1],"dec")) enc=0; + else usage(argv[0]); + + if (!(fi=fopen(argv[2],"rb"))) + { + fprintf(stderr,"error: can't read %s\n", argv[2]); + exit(2); + } + if (!(fo=fopen(argv[3],"wb"))) + { + fprintf(stderr,"error: can't write to %s\n", argv[3]); + exit(3); + } + if (argc==5) + { + if (!(fbdi=fopen(argv[4],"rb"))) + { + fprintf(stderr,"error: can't read %s\n", argv[4]); + exit(3); + } + } + + frsz = FRSZ; + sizebitstream = sizeof(struct BV16_Bit_Stream); + if (!strcmp(argv[1],"enc")) + { + sizestate = sizeof(struct BV16_Encoder_State); + state = allocWord16(0,sizeof(struct BV16_Encoder_State)/2-1); + Reset_BV16_Encoder((struct BV16_Encoder_State*)state); + } + else + { + sizestate = sizeof(struct BV16_Decoder_State); + state = allocWord16(0,sizeof(struct BV16_Decoder_State)/2-1); + Reset_BV16_Decoder((struct BV16_Decoder_State*)state); + } + + bs = allocWord16(0,sizebitstream/2-1); + x = allocWord16(0, frsz-1); + + if (enc){ +#if G192BITSTREAM + fprintf(stderr," BroadVoice16 Fixed-Point Encoder V1.1 with ITU-T G.192\n"); +#else + fprintf(stderr," BroadVoice16 Fixed-Point Encoder V1.1 with packed bit-stream\n"); +#endif + fprintf(stderr," Input speech file : %s\n",argv[2]); + fprintf(stderr," Output bit-stream file: %s\n",argv[3]); + } + else{ +#if G192BITSTREAM + fprintf(stderr," BroadVoice16 Fixed-Point Decoder V1.1 with ITU-T G.192\n"); +#else + fprintf(stderr," BroadVoice16 Fixed-Point Decoder V1.1 with packed bit-stream\n"); +#endif + fprintf(stderr," Input bit-stream file : %s\n",argv[2]); + fprintf(stderr," Output speech file : %s\n",argv[3]); + } + + /* START THE MAIN FRAME LOOP */ + frame=0; + /* read for the 1st bad frame */ + if (fbdi!=NULL) + fscanf(fbdi,"%d", &next_bad_frame); + + while (1) + { + + /* FRAME COUNTER */ + frame++; + + /* READ IN ONE SPEECH FRAME */ + if (enc==1) + { + nread=fread(x, sizeof(Word16), frsz, fi); + if (nread<=0) goto End; + for (i=nread;idfm_h[2*i+1]; - for (i=0;idfm_h[2*i]; - - lp0 = lxwd; - - for (i=0;idfm_h[2*i+1] = fp1_h[i]; - cstate->dfm_h[2*i] = fp1_l[i]; - } - - lp0 = lxwd; - new_exp = sub(norm_l(a1), 3); /* headroom to avoid overflow */ - exp = sub(cstate->xwd_exp,new_exp); /* increase in bit-resolution */ - - if (exp < 0) { /* Descending signal level */ - new_exp = cstate->xwd_exp; - exp = 0; - } - - for (i=0;ixwd[i], exp); - - /* fill-in new exponent */ - fp0 = xwd + XDOFF; - for (i=0;i exp0) - exp0 = exp1; - } - exp0 = sub(norm_s(exp0),3); /* extra exponent for next frame */ - - exp = sub(exp0, exp); - - if (exp >=0) - { - for (i=0;ixwd[i] = shl(cstate->xwd[i+FRSZD], exp); - } - else - { - exp = -exp; - if (exp >=15) - exp = 15; - for (i=0;ixwd[i] = shr(cstate->xwd[i+FRSZD], exp); - } - for (;ixwd[i] = shl(xwd[FRSZD+i],exp0); - - - cstate->xwd_exp = add(new_exp, exp0); - - /* Compute correlation & energy of prediction basis vector */ - - /* reset local buffers */ - for (i=0;i0) { - a0 = L_mult(energy_man[n-1],cor2[n]); - a1 = L_mult(energy_man[n], cor2[n-1]); - exp0 = shl(sub(cor2_exp[n], cor2_exp[n-1]),1); - exp0 = add(exp0, energy_exp[n-1]); - exp0 = sub(exp0, energy_exp[n]); - - if (exp0>=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - - a0 = L_mult(energy_man[n+1],cor2[n]); - a1 = L_mult(energy_man[n], cor2[n+1]); - exp0 = shl(sub(cor2_exp[n], cor2_exp[n+1]),1); - exp0 = add(exp0, energy_exp[n+1]); - exp0 = sub(exp0, energy_exp[n]); - - if (exp0>=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - idx[npeaks] = n; - npeaks++; - } - } - } - - n++; - - } - - /* Return early if there is no peak or only one peak */ - - if (npeaks == 0){ /* if there are no positive peak, */ - return MINPPD*DECF; /* return minimum pitch period in decimated domain */ - } - - if (npeaks == 1){ /* if there is exactly one peak, */ - return (idx[0]+1)*DECF; /* return the time lag for this single peak */ - } - - /* If program proceeds to here, there are 2 or more peaks */ - cor2max=(Word16) 0x8000; - cor2max_exp= (Word16) 0; - energymax_man=1; - energymax_exp=0; - - imax=0; - for (i=0; i < npeaks; i++) { - - /* Use quadratic interpolation to find the interpolated cor[] and - energy[] corresponding to interpolated peak of cor2[]/energy[] */ - /* first calculate coefficients of y(x)=ax^2+bx+c; */ - n=idx[i]; - a0=L_sub(L_shr(L_add(cor[n+1],cor[n-1]),1),cor[n]); - L_Extract(a0, &ah, &al); - a0=L_shr(L_sub(cor[n+1],cor[n-1]),1); - L_Extract(a0, &bh, &bl); - cc=cor[n]; - - /* Initialize variables before searching for interpolated peak */ - im=0; - cor2m_exp = cor2_exp[n]; - cor2m = cor2[n]; - energym_exp = energy_exp[n]; - energym_man = energy_man[n]; - eni=energy[n]; - - /* Determine which side the interpolated peak falls in, then - do the search in the appropriate range */ - - a0 = L_mult(energy_man[n-1],cor2[n+1]); - a1 = L_mult(energy_man[n+1], cor2[n-1]); - exp0 = shl(sub(cor2_exp[n+1], cor2_exp[n-1]),1); - exp0 = add(exp0, energy_exp[n-1]); - exp0 = sub(exp0, energy_exp[n+1]); - - if (exp0>=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { /* if right side */ - - deltae = L_shr(L_sub(energy[n+1], eni), 2); - - for (k = 0; k < HDECF; k++) { - a0=L_add(L_add(Mpy_32_16(ah,al,x2[k]),Mpy_32_16(bh,bl,x[k])),cc); - eni = L_add(eni, deltae); - a1 = eni; - exp0 = norm_l(a0); - s0 = extract_h(L_shl(a0, exp0)); - s0 = extract_h(L_mult(s0, s0)); - e2 = energym_exp; - t0 = energym_man; - a2 = L_mult(t0, s0); - e3 = norm_l(a1); - t1 = extract_h(L_shl(a1, e3)); - a3 = L_mult(t1, cor2m); - exp1 = shl(sub(exp0, cor2m_exp),1); - exp1 = add(exp1, e2); - exp1 = sub(exp1, e3); - - if (exp1>=0) - a2 = L_shr(a2, exp1); - else - a3 = L_shl(a3, exp1); - - if (a2 > a3) { - im = k+1; - cor2m = s0; - cor2m_exp = exp0; - energym_exp = e3; - energym_man = t1; - } - } - } else { /* if interpolated peak is on the left side */ - - deltae = L_shr(L_sub(energy[n-1], eni), 2); - for (k = 0; k < HDECF; k++) { - a0=L_add(L_sub(Mpy_32_16(ah,al,x2[k]),Mpy_32_16(bh,bl,x[k])),cc); - eni = L_add(eni, deltae); - a1=eni; - - exp0 = norm_l(a0); - s0 = extract_h(L_shl(a0, exp0)); - s0 = extract_h(L_mult(s0, s0)); - e2 = energym_exp; - t0 = energym_man; - a2 = L_mult(t0, s0); - e3 = norm_l(a1); - t1 = extract_h(L_shl(a1, e3)); - a3 = L_mult(t1, cor2m); - exp1 = shl(sub(exp0, cor2m_exp),1); - exp1 = add(exp1, e2); - exp1 = sub(exp1, e3); - - if (exp1>=0) - a2 = L_shr(a2, exp1); - else - a3 = L_shl(a3, exp1); - - if (a2 > a3) { - im = -k-1; - cor2m = s0; - cor2m_exp = exp0; - energym_exp = e3; - energym_man = t1; - } - } - } - - /* Search done; assign cor2[] and energy[] corresponding to - interpolated peak */ - plag[i]=add(shl(add(idx[i],1),2),im); /* lag of interp. peak */ - cor2i[i]=cor2m; - cor2i_exp[i]=cor2m_exp; - /* interpolated energy[] of i-th interpolated peak */ - energyi_exp[i] = energym_exp; - energyi_man[i] = energym_man; - - /* Search for global maximum of interpolated cor2[]/energy[] peak */ - a0 = L_mult(cor2m,energymax_man); - a1 = L_mult(cor2max, energyi_man[i]); - exp0 = shl(sub(cor2m_exp, cor2max_exp),1); - exp0 = add(exp0, energymax_exp); - exp0 = sub(exp0, energyi_exp[i]); - - if (exp0 >=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - imax=i; - cor2max=cor2m; - cor2max_exp=cor2m_exp; - energymax_exp = energyi_exp[i]; - energymax_man = energyi_man[i]; - } - } - cpp=plag[imax]; /* first candidate for coarse pitch period */ - mplth=plag[npeaks-1]; /* set mplth to the lag of last peak */ - - /* Find the largest peak (if there is any) around the last pitch */ - maxdev= shr(cstate->cpplast,2); /* maximum deviation from last pitch */ - im = -1; - cor2m=(Word16) 0x8000; - cor2m_exp= (Word16) 0; - energym_man = 1; - energym_exp = 0; - - for (i=0;icpplast)) <= maxdev) { - a0 = L_mult(cor2i[i],energym_man); - a1 = L_mult(cor2m, energyi_man[i]); - exp0 = shl(sub(cor2i_exp[i], cor2m_exp),1); - exp0 = add(exp0, energym_exp); - exp0 = sub(exp0, energyi_exp[i]); - - if (exp0 >=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - im=i; - cor2m=cor2i[i]; - cor2m_exp=cor2i_exp[i]; - energym_man = energyi_man[i]; - energym_exp = energyi_exp[i]; - } - } - } /* if there is no peaks around last pitch, then im is still -1 */ - - - /* Now see if we should pick any alternatice peak */ - /* first, search first half of pitch range, see if any qualified peak - has large enough peaks at every multiple of its lag */ - i=0; - - while (2*plag[i] < mplth) { - - /* Determine the appropriate threshold for this peak */ - - if (i != im) { /* if not around last pitch, */ - threshold = TH1; /* use a higher threshold */ - } else { /* if around last pitch */ - threshold = TH2; /* use a lower threshold */ - } - - /* If threshold exceeded, test peaks at multiples of this lag */ - a0 = L_mult(cor2i[i],energymax_man); - t1 = extract_h(L_mult(energyi_man[i], threshold)); - a1 = L_mult(cor2max, t1); - exp0 = shl(sub(cor2i_exp[i], cor2max_exp),1); - exp0 = add(exp0, energymax_exp); - exp0 = sub(exp0, energyi_exp[i]); - - if (exp0 >=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - flag=1; - j=i+1; - k=0; - s=shl(plag[i],1); /* initialize t to twice the current lag */ - - while (s<=mplth) { /* loop thru all multiple lag <= mplth */ - - mpflag=0; /* initialize multiple pitch flag to 0 */ - t0 = mult_r(s,MPDTH); - a=sub(s, t0); /* multiple pitch range lower bound */ - b=add(s, t0); /* multiple pitch range upper bound */ - while (j < npeaks) { /* loop thru peaks with larger lags */ - - if (plag[j] > b) { /* if range exceeded, */ - break; /* break the innermost while loop */ - } /* if didn't break, then plag[j] <= b */ - - if (plag[j] > a) { /* if current peak lag within range, */ - /* then check if peak value large enough */ - a0 = L_mult(cor2i[j],energymax_man); - if (k<4) - t1 = MPTH[k]; - else - t1 = MPTH4; - t1 = extract_h(L_mult(t1, energyi_man[j])); - a1 = L_mult(cor2max, t1); - exp0 = shl(sub(cor2i_exp[j], cor2max_exp),1); - exp0 = add(exp0, energymax_exp); - exp0 = sub(exp0, energyi_exp[j]); - - if (exp0 >=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - mpflag=1; /* if peak large enough, set mpflag, */ - break; /* and break the innermost while loop */ - } - } - j++; - } - /* if no qualified peak found at this multiple lag */ - - if (mpflag == 0) { - flag=0; /* disqualify the lag plag[i] */ - break; /* and break the while (s<=mplth) loop */ - } - k++; - s = add(s, plag[i]); /* update s to the next multiple pitch lag */ - - } - /* if there is a qualified peak at every multiple of plag[i], */ - - if (flag == 1) { - cpp = plag[i]; /* then accept this as final pitch */ - - return cpp; /* and return to calling function */ - } - } - i++; - - if (i == npeaks) - break; /* to avoid out of array bound error */ - } - - /* If program proceeds to here, none of the peaks with lags < 0.5*mplth - qualifies as the final pitch. in this case, check if - there is any peak large enough around last pitch. if so, use its - lag as the final pitch. */ - - if (im != -1) { /* if there is at least one peak around last pitch */ - - if (im == imax) { /* if this peak is also the global maximum, */ - return cpp; /* return first pitch candidate at global max */ - } - - if (im < imax) { /* if lag of this peak < lag of global max, */ - a0 = L_mult(cor2m,energymax_man); - t1 = extract_h(L_mult(energym_man, LPTH2)); - a1 = L_mult(cor2max, t1); - exp0 = shl(sub(cor2m_exp, cor2max_exp),1); - exp0 = add(exp0, energymax_exp); - exp0 = sub(exp0, energym_exp); - - if (exp0 >=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - - if (plag[im] > HMAXPPD*DECF) { - cpp=plag[im]; - - return cpp; - } - for (k=2; k<=5;k++) { /* check if current candidate pitch */ - s=mult(plag[imax],invk[k-2]); /* is a sub-multiple of */ - t0 = mult_r(s,SMDTH); - a=sub(s, t0); /* the time lag of */ - b=add(s, t0); /* the global maximum peak */ - - if (plag[im]>a && plag[im] lag of global max, */ - a0 = L_mult(cor2m,energymax_man); - t1 = extract_h(L_mult(energym_man, LPTH1)); - a1 = L_mult(cor2max, t1); - exp0 = shl(sub(cor2m_exp, cor2max_exp),1); - exp0 = add(exp0, energymax_exp); - exp0 = sub(exp0, energym_exp); - - if (exp0 >=0) - a0 = L_shr(a0, exp0); - else - a1 = L_shl(a1, exp0); - - if (a0 > a1) { - cpp = plag[im]; /* if this peak is large enough, */ - - return cpp; /* accept its lag */ - } - } - } - - /* If program proceeds to here, we have no choice but to accept the - lag of the global maximum */ - return cpp; - -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + coarptch.c: Coarse pitch search + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" + + +Word16 coarsepitch( + Word16 *xw, /* (i) (normalized) weighted signal */ + struct BV16_Encoder_State *cstate) /* (i/o) Coder State */ +{ + + Word16 s; /* Q2 */ + Word16 a, b; + Word16 im; + Word16 maxdev, flag, mpflag; + Word32 eni, deltae; + Word32 cc; + Word16 ah,al, bh, bl; + Word32 a0, a1, a2, a3; + Word32 *lp0; + Word16 exp, new_exp; + Word16 *fp0, *fp1, *fp2, *fp3, *sp; + Word16 *fp1_h, *fp1_l, *fp2_h, *fp2_l; + Word16 cor2max, cor2max_exp; + Word16 cor2m, cor2m_exp; + Word16 s0, t0, t1, exp0, exp1, e2, e3; + Word16 threshold; + Word16 mplth; /* Q2 */ + Word16 i, j, k, n, npeaks, imax, idx[MAXPPD-MINPPD+1]; + Word16 cpp; + Word16 plag[HMAXPPD], cor2[MAXPPD1], cor2_exp[MAXPPD1]; + Word16 cor2i[HMAXPPD], cor2i_exp[HMAXPPD], xwd[LXD]; + Word16 tmp_h[DFO+FRSZ], tmp_l[DFO+FRSZ]; /* DPF Q7 */ + Word32 cor[MAXPPD1], energy[MAXPPD1], lxwd[FRSZD]; + Word16 energy_man[MAXPPD1], energy_exp[MAXPPD1]; + Word16 energyi_man[HMAXPPD], energyi_exp[HMAXPPD]; + Word16 energym_man, energym_exp; + Word16 energymax_man, energymax_exp; + + /* Lowpass filter xw() to 800 hz; shift & output into xwd() */ + + /* AP and AZ filtering and decimation */ + + fp1_h = tmp_h + DFO; + fp1_l = tmp_l + DFO; + + sp = xw; + a1 = 1; + + + for (i=0;idfm_h[2*i+1]; + for (i=0;idfm_h[2*i]; + + lp0 = lxwd; + + for (i=0;idfm_h[2*i+1] = fp1_h[i]; + cstate->dfm_h[2*i] = fp1_l[i]; + } + + lp0 = lxwd; + new_exp = sub(norm_l(a1), 3); /* headroom to avoid overflow */ + exp = sub(cstate->xwd_exp,new_exp); /* increase in bit-resolution */ + + if (exp < 0) { /* Descending signal level */ + new_exp = cstate->xwd_exp; + exp = 0; + } + + for (i=0;ixwd[i], exp); + + /* fill-in new exponent */ + fp0 = xwd + XDOFF; + for (i=0;i exp0) + exp0 = exp1; + } + exp0 = sub(norm_s(exp0),3); /* extra exponent for next frame */ + + exp = sub(exp0, exp); + + if (exp >=0) + { + for (i=0;ixwd[i] = shl(cstate->xwd[i+FRSZD], exp); + } + else + { + exp = -exp; + if (exp >=15) + exp = 15; + for (i=0;ixwd[i] = shr(cstate->xwd[i+FRSZD], exp); + } + for (;ixwd[i] = shl(xwd[FRSZD+i],exp0); + + + cstate->xwd_exp = add(new_exp, exp0); + + /* Compute correlation & energy of prediction basis vector */ + + /* reset local buffers */ + for (i=0;i0) { + a0 = L_mult(energy_man[n-1],cor2[n]); + a1 = L_mult(energy_man[n], cor2[n-1]); + exp0 = shl(sub(cor2_exp[n], cor2_exp[n-1]),1); + exp0 = add(exp0, energy_exp[n-1]); + exp0 = sub(exp0, energy_exp[n]); + + if (exp0>=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + + a0 = L_mult(energy_man[n+1],cor2[n]); + a1 = L_mult(energy_man[n], cor2[n+1]); + exp0 = shl(sub(cor2_exp[n], cor2_exp[n+1]),1); + exp0 = add(exp0, energy_exp[n+1]); + exp0 = sub(exp0, energy_exp[n]); + + if (exp0>=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + idx[npeaks] = n; + npeaks++; + } + } + } + + n++; + + } + + /* Return early if there is no peak or only one peak */ + + if (npeaks == 0){ /* if there are no positive peak, */ + return MINPPD*DECF; /* return minimum pitch period in decimated domain */ + } + + if (npeaks == 1){ /* if there is exactly one peak, */ + return (idx[0]+1)*DECF; /* return the time lag for this single peak */ + } + + /* If program proceeds to here, there are 2 or more peaks */ + cor2max=(Word16) 0x8000; + cor2max_exp= (Word16) 0; + energymax_man=1; + energymax_exp=0; + + imax=0; + for (i=0; i < npeaks; i++) { + + /* Use quadratic interpolation to find the interpolated cor[] and + energy[] corresponding to interpolated peak of cor2[]/energy[] */ + /* first calculate coefficients of y(x)=ax^2+bx+c; */ + n=idx[i]; + a0=L_sub(L_shr(L_add(cor[n+1],cor[n-1]),1),cor[n]); + L_Extract(a0, &ah, &al); + a0=L_shr(L_sub(cor[n+1],cor[n-1]),1); + L_Extract(a0, &bh, &bl); + cc=cor[n]; + + /* Initialize variables before searching for interpolated peak */ + im=0; + cor2m_exp = cor2_exp[n]; + cor2m = cor2[n]; + energym_exp = energy_exp[n]; + energym_man = energy_man[n]; + eni=energy[n]; + + /* Determine which side the interpolated peak falls in, then + do the search in the appropriate range */ + + a0 = L_mult(energy_man[n-1],cor2[n+1]); + a1 = L_mult(energy_man[n+1], cor2[n-1]); + exp0 = shl(sub(cor2_exp[n+1], cor2_exp[n-1]),1); + exp0 = add(exp0, energy_exp[n-1]); + exp0 = sub(exp0, energy_exp[n+1]); + + if (exp0>=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { /* if right side */ + + deltae = L_shr(L_sub(energy[n+1], eni), 2); + + for (k = 0; k < HDECF; k++) { + a0=L_add(L_add(Mpy_32_16(ah,al,x2[k]),Mpy_32_16(bh,bl,x[k])),cc); + eni = L_add(eni, deltae); + a1 = eni; + exp0 = norm_l(a0); + s0 = extract_h(L_shl(a0, exp0)); + s0 = extract_h(L_mult(s0, s0)); + e2 = energym_exp; + t0 = energym_man; + a2 = L_mult(t0, s0); + e3 = norm_l(a1); + t1 = extract_h(L_shl(a1, e3)); + a3 = L_mult(t1, cor2m); + exp1 = shl(sub(exp0, cor2m_exp),1); + exp1 = add(exp1, e2); + exp1 = sub(exp1, e3); + + if (exp1>=0) + a2 = L_shr(a2, exp1); + else + a3 = L_shl(a3, exp1); + + if (a2 > a3) { + im = k+1; + cor2m = s0; + cor2m_exp = exp0; + energym_exp = e3; + energym_man = t1; + } + } + } else { /* if interpolated peak is on the left side */ + + deltae = L_shr(L_sub(energy[n-1], eni), 2); + for (k = 0; k < HDECF; k++) { + a0=L_add(L_sub(Mpy_32_16(ah,al,x2[k]),Mpy_32_16(bh,bl,x[k])),cc); + eni = L_add(eni, deltae); + a1=eni; + + exp0 = norm_l(a0); + s0 = extract_h(L_shl(a0, exp0)); + s0 = extract_h(L_mult(s0, s0)); + e2 = energym_exp; + t0 = energym_man; + a2 = L_mult(t0, s0); + e3 = norm_l(a1); + t1 = extract_h(L_shl(a1, e3)); + a3 = L_mult(t1, cor2m); + exp1 = shl(sub(exp0, cor2m_exp),1); + exp1 = add(exp1, e2); + exp1 = sub(exp1, e3); + + if (exp1>=0) + a2 = L_shr(a2, exp1); + else + a3 = L_shl(a3, exp1); + + if (a2 > a3) { + im = -k-1; + cor2m = s0; + cor2m_exp = exp0; + energym_exp = e3; + energym_man = t1; + } + } + } + + /* Search done; assign cor2[] and energy[] corresponding to + interpolated peak */ + plag[i]=add(shl(add(idx[i],1),2),im); /* lag of interp. peak */ + cor2i[i]=cor2m; + cor2i_exp[i]=cor2m_exp; + /* interpolated energy[] of i-th interpolated peak */ + energyi_exp[i] = energym_exp; + energyi_man[i] = energym_man; + + /* Search for global maximum of interpolated cor2[]/energy[] peak */ + a0 = L_mult(cor2m,energymax_man); + a1 = L_mult(cor2max, energyi_man[i]); + exp0 = shl(sub(cor2m_exp, cor2max_exp),1); + exp0 = add(exp0, energymax_exp); + exp0 = sub(exp0, energyi_exp[i]); + + if (exp0 >=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + imax=i; + cor2max=cor2m; + cor2max_exp=cor2m_exp; + energymax_exp = energyi_exp[i]; + energymax_man = energyi_man[i]; + } + } + cpp=plag[imax]; /* first candidate for coarse pitch period */ + mplth=plag[npeaks-1]; /* set mplth to the lag of last peak */ + + /* Find the largest peak (if there is any) around the last pitch */ + maxdev= shr(cstate->cpplast,2); /* maximum deviation from last pitch */ + im = -1; + cor2m=(Word16) 0x8000; + cor2m_exp= (Word16) 0; + energym_man = 1; + energym_exp = 0; + + for (i=0;icpplast)) <= maxdev) { + a0 = L_mult(cor2i[i],energym_man); + a1 = L_mult(cor2m, energyi_man[i]); + exp0 = shl(sub(cor2i_exp[i], cor2m_exp),1); + exp0 = add(exp0, energym_exp); + exp0 = sub(exp0, energyi_exp[i]); + + if (exp0 >=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + im=i; + cor2m=cor2i[i]; + cor2m_exp=cor2i_exp[i]; + energym_man = energyi_man[i]; + energym_exp = energyi_exp[i]; + } + } + } /* if there is no peaks around last pitch, then im is still -1 */ + + + /* Now see if we should pick any alternatice peak */ + /* first, search first half of pitch range, see if any qualified peak + has large enough peaks at every multiple of its lag */ + i=0; + + while (2*plag[i] < mplth) { + + /* Determine the appropriate threshold for this peak */ + + if (i != im) { /* if not around last pitch, */ + threshold = TH1; /* use a higher threshold */ + } else { /* if around last pitch */ + threshold = TH2; /* use a lower threshold */ + } + + /* If threshold exceeded, test peaks at multiples of this lag */ + a0 = L_mult(cor2i[i],energymax_man); + t1 = extract_h(L_mult(energyi_man[i], threshold)); + a1 = L_mult(cor2max, t1); + exp0 = shl(sub(cor2i_exp[i], cor2max_exp),1); + exp0 = add(exp0, energymax_exp); + exp0 = sub(exp0, energyi_exp[i]); + + if (exp0 >=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + flag=1; + j=i+1; + k=0; + s=shl(plag[i],1); /* initialize t to twice the current lag */ + + while (s<=mplth) { /* loop thru all multiple lag <= mplth */ + + mpflag=0; /* initialize multiple pitch flag to 0 */ + t0 = mult_r(s,MPDTH); + a=sub(s, t0); /* multiple pitch range lower bound */ + b=add(s, t0); /* multiple pitch range upper bound */ + while (j < npeaks) { /* loop thru peaks with larger lags */ + + if (plag[j] > b) { /* if range exceeded, */ + break; /* break the innermost while loop */ + } /* if didn't break, then plag[j] <= b */ + + if (plag[j] > a) { /* if current peak lag within range, */ + /* then check if peak value large enough */ + a0 = L_mult(cor2i[j],energymax_man); + if (k<4) + t1 = MPTH[k]; + else + t1 = MPTH4; + t1 = extract_h(L_mult(t1, energyi_man[j])); + a1 = L_mult(cor2max, t1); + exp0 = shl(sub(cor2i_exp[j], cor2max_exp),1); + exp0 = add(exp0, energymax_exp); + exp0 = sub(exp0, energyi_exp[j]); + + if (exp0 >=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + mpflag=1; /* if peak large enough, set mpflag, */ + break; /* and break the innermost while loop */ + } + } + j++; + } + /* if no qualified peak found at this multiple lag */ + + if (mpflag == 0) { + flag=0; /* disqualify the lag plag[i] */ + break; /* and break the while (s<=mplth) loop */ + } + k++; + s = add(s, plag[i]); /* update s to the next multiple pitch lag */ + + } + /* if there is a qualified peak at every multiple of plag[i], */ + + if (flag == 1) { + cpp = plag[i]; /* then accept this as final pitch */ + + return cpp; /* and return to calling function */ + } + } + i++; + + if (i == npeaks) + break; /* to avoid out of array bound error */ + } + + /* If program proceeds to here, none of the peaks with lags < 0.5*mplth + qualifies as the final pitch. in this case, check if + there is any peak large enough around last pitch. if so, use its + lag as the final pitch. */ + + if (im != -1) { /* if there is at least one peak around last pitch */ + + if (im == imax) { /* if this peak is also the global maximum, */ + return cpp; /* return first pitch candidate at global max */ + } + + if (im < imax) { /* if lag of this peak < lag of global max, */ + a0 = L_mult(cor2m,energymax_man); + t1 = extract_h(L_mult(energym_man, LPTH2)); + a1 = L_mult(cor2max, t1); + exp0 = shl(sub(cor2m_exp, cor2max_exp),1); + exp0 = add(exp0, energymax_exp); + exp0 = sub(exp0, energym_exp); + + if (exp0 >=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + + if (plag[im] > HMAXPPD*DECF) { + cpp=plag[im]; + + return cpp; + } + for (k=2; k<=5;k++) { /* check if current candidate pitch */ + s=mult(plag[imax],invk[k-2]); /* is a sub-multiple of */ + t0 = mult_r(s,SMDTH); + a=sub(s, t0); /* the time lag of */ + b=add(s, t0); /* the global maximum peak */ + + if (plag[im]>a && plag[im] lag of global max, */ + a0 = L_mult(cor2m,energymax_man); + t1 = extract_h(L_mult(energym_man, LPTH1)); + a1 = L_mult(cor2max, t1); + exp0 = shl(sub(cor2m_exp, cor2max_exp),1); + exp0 = add(exp0, energymax_exp); + exp0 = sub(exp0, energym_exp); + + if (exp0 >=0) + a0 = L_shr(a0, exp0); + else + a1 = L_shl(a1, exp0); + + if (a0 > a1) { + cpp = plag[im]; /* if this peak is large enough, */ + + return cpp; /* accept its lag */ + } + } + } + + /* If program proceeds to here, we have no choice but to accept the + lag of the global maximum */ + return cpp; + +} diff --git a/jni/bx16_fixedp/bv16/decoder.c b/app/src/main/jni/bx16_fixedp/bv16/decoder.c similarity index 97% rename from jni/bx16_fixedp/bv16/decoder.c rename to app/src/main/jni/bx16_fixedp/bv16/decoder.c index 7e73b4a..d254bd8 100644 --- a/jni/bx16_fixedp/bv16/decoder.c +++ b/app/src/main/jni/bx16_fixedp/bv16/decoder.c @@ -1,163 +1,163 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - decoder.c : BV16 Fixed-Point Decoder Main Subroutines - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16externs.h" -#include "basop32.h" -#include "utility.h" -#include "mathutil.h" -#include "postfilt.h" - -void Reset_BV16_Decoder(struct BV16_Decoder_State *c) -{ - int i; - W16zero((Word16 *) c, sizeof(struct BV16_Decoder_State)/sizeof(Word16)); - c->lsplast[0] = 3641; - c->lsplast[1] = 7282; - c->lsplast[2] = 10923; - c->lsplast[3] = 14564; - c->lsplast[4] = 18204; - c->lsplast[5] = 21845; - c->lsplast[6] = 25486; - c->lsplast[7] = 29127; - c->pp_last = 50; - c->lmax = MIN_32; - c->lmin = MAX_32; - c->lmean = 419430400; /* 12.5 Q25 */ - c->x1 = 570425344; /* 17.0 Q25 */ - c->level = 570425344; /* 17.0 Q25 */ - c->ngfae = LGPORDER+1; - c->nggalgc = Nfdm+1; - c->estl_alpha_min = estl_alpha; - c->idum =0; - c->per = 0; - for(i=0; iatplc[i+1] = 0; - c->ma_a = 0; - c->b_prv[0] = 8192; /* Q13 */ - c->b_prv[1] = 0; - c->pp_prv = 100; -} - -void BV16_Decode( - struct BV16_Bit_Stream *bs, - struct BV16_Decoder_State *ds, - Word16 *x) -{ - Word32 lgq, lg_el; - Word16 gainq; /* Q3 */ - Word16 pp; - Word32 a0; - Word16 gain_exp; - Word16 i; - Word16 a0hi, a0lo; - Word16 ltsym[LTMOFF+FRSZ]; - Word16 xq[LXQ]; - Word16 a[LPCO+1]; - Word16 lspq[LPCO]; /* Q15 */ - Word16 cbs[VDIM*CBSZ]; - Word16 bq[3]; /* Q15 */ - Word32 bss; - Word32 E; - - /* set frame erasure flags */ - if (ds->cfecount != 0) { - ds->ngfae = 1; - } else { - ds->ngfae++; - if (ds->ngfae>LGPORDER) ds->ngfae=LGPORDER+1; - } - - /* reset frame erasure counter */ - ds->cfecount = 0; - - /* decode pitch period */ - pp = (bs->ppidx + MINPP); - - /* decode spectral information */ - lspdec(lspq,bs->lspidx,ds->lsppm,ds->lsplast); - lsp2a(lspq,a); - W16copy(ds->lsplast, lspq, LPCO); - - /* decode pitch taps */ - pp3dec(bs->bqidx, bq); - - /* decode gain */ - a0 = gaindec(&lgq,bs->gidx,ds->lgpm,ds->prevlg,ds->level, - &ds->nggalgc,&lg_el); - - /* gain normalization */ - gain_exp = sub(norm_l(a0), 2); - /* scale down quantized gain by 1.5, 1/1.5=2/3 (21845 Q15) */ - L_Extract(a0, &a0hi, &a0lo); - a0 = Mpy_32_16(a0hi, a0lo, 21845); - gainq = intround(L_shl(a0, gain_exp)); - - - /* scale the scalar quantizer codebook to current signal level */ - for (i=0;i<(VDIM*CBSZ);i++) cbs[i] = mult_r(gainq, cccb[i]); - - /* copy state memory to buffer */ - W16copy(xq, ds->xq, XQOFF); - W16copy(ltsym, ds->ltsym, LTMOFF); - - /* decoding of the excitation signal with integrated long-term */ - /* and short-term synthesis */ - excdec_w_synth(xq+XQOFF,ltsym+LTMOFF,ds->stsym,bs->qvidx,bq,cbs,pp, - a,gain_exp,&E); - - ds->E = E; - - /* update the remaining state memory */ - W16copy(ds->ltsym, ltsym+FRSZ, LTMOFF); - W16copy(ds->xq, xq+FRSZ, XQOFF); - ds->pp_last = pp; - W16copy(ds->bq_last, bq, 3); - - /* level estimation */ - estlevel(lg_el,&ds->level,&ds->lmax,&ds->lmin,&ds->lmean,&ds->x1, - ds->ngfae, ds->nggalgc,&ds->estl_alpha_min); - - /* adaptive postfiltering */ - postfilter(xq, pp, &(ds->ma_a), ds->b_prv, &(ds->pp_prv), x); - - /* scale signal up by 1.5 */ - for(i=0; iatplc, a, LPCO+1); - bss = L_add(L_add(bq[0], bq[1]), bq[2]); - if (bss > 32768) - bss = 32768; - else if (bss < 0) - bss = 0; - ds->per = add(shr(ds->per, 1), (Word16)L_shr(bss, 1)); - -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + decoder.c : BV16 Fixed-Point Decoder Main Subroutines + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" +#include "utility.h" +#include "mathutil.h" +#include "postfilt.h" + +void Reset_BV16_Decoder(struct BV16_Decoder_State *c) +{ + int i; + W16zero((Word16 *) c, sizeof(struct BV16_Decoder_State)/sizeof(Word16)); + c->lsplast[0] = 3641; + c->lsplast[1] = 7282; + c->lsplast[2] = 10923; + c->lsplast[3] = 14564; + c->lsplast[4] = 18204; + c->lsplast[5] = 21845; + c->lsplast[6] = 25486; + c->lsplast[7] = 29127; + c->pp_last = 50; + c->lmax = MIN_32; + c->lmin = MAX_32; + c->lmean = 419430400; /* 12.5 Q25 */ + c->x1 = 570425344; /* 17.0 Q25 */ + c->level = 570425344; /* 17.0 Q25 */ + c->ngfae = LGPORDER+1; + c->nggalgc = Nfdm+1; + c->estl_alpha_min = estl_alpha; + c->idum =0; + c->per = 0; + for(i=0; iatplc[i+1] = 0; + c->ma_a = 0; + c->b_prv[0] = 8192; /* Q13 */ + c->b_prv[1] = 0; + c->pp_prv = 100; +} + +void BV16_Decode( + struct BV16_Bit_Stream *bs, + struct BV16_Decoder_State *ds, + Word16 *x) +{ + Word32 lgq, lg_el; + Word16 gainq; /* Q3 */ + Word16 pp; + Word32 a0; + Word16 gain_exp; + Word16 i; + Word16 a0hi, a0lo; + Word16 ltsym[LTMOFF+FRSZ]; + Word16 xq[LXQ]; + Word16 a[LPCO+1]; + Word16 lspq[LPCO]; /* Q15 */ + Word16 cbs[VDIM*CBSZ]; + Word16 bq[3]; /* Q15 */ + Word32 bss; + Word32 E; + + /* set frame erasure flags */ + if (ds->cfecount != 0) { + ds->ngfae = 1; + } else { + ds->ngfae++; + if (ds->ngfae>LGPORDER) ds->ngfae=LGPORDER+1; + } + + /* reset frame erasure counter */ + ds->cfecount = 0; + + /* decode pitch period */ + pp = (bs->ppidx + MINPP); + + /* decode spectral information */ + lspdec(lspq,bs->lspidx,ds->lsppm,ds->lsplast); + lsp2a(lspq,a); + W16copy(ds->lsplast, lspq, LPCO); + + /* decode pitch taps */ + pp3dec(bs->bqidx, bq); + + /* decode gain */ + a0 = gaindec(&lgq,bs->gidx,ds->lgpm,ds->prevlg,ds->level, + &ds->nggalgc,&lg_el); + + /* gain normalization */ + gain_exp = sub(norm_l(a0), 2); + /* scale down quantized gain by 1.5, 1/1.5=2/3 (21845 Q15) */ + L_Extract(a0, &a0hi, &a0lo); + a0 = Mpy_32_16(a0hi, a0lo, 21845); + gainq = intround(L_shl(a0, gain_exp)); + + + /* scale the scalar quantizer codebook to current signal level */ + for (i=0;i<(VDIM*CBSZ);i++) cbs[i] = mult_r(gainq, cccb[i]); + + /* copy state memory to buffer */ + W16copy(xq, ds->xq, XQOFF); + W16copy(ltsym, ds->ltsym, LTMOFF); + + /* decoding of the excitation signal with integrated long-term */ + /* and short-term synthesis */ + excdec_w_synth(xq+XQOFF,ltsym+LTMOFF,ds->stsym,bs->qvidx,bq,cbs,pp, + a,gain_exp,&E); + + ds->E = E; + + /* update the remaining state memory */ + W16copy(ds->ltsym, ltsym+FRSZ, LTMOFF); + W16copy(ds->xq, xq+FRSZ, XQOFF); + ds->pp_last = pp; + W16copy(ds->bq_last, bq, 3); + + /* level estimation */ + estlevel(lg_el,&ds->level,&ds->lmax,&ds->lmin,&ds->lmean,&ds->x1, + ds->ngfae, ds->nggalgc,&ds->estl_alpha_min); + + /* adaptive postfiltering */ + postfilter(xq, pp, &(ds->ma_a), ds->b_prv, &(ds->pp_prv), x); + + /* scale signal up by 1.5 */ + for(i=0; iatplc, a, LPCO+1); + bss = L_add(L_add(bq[0], bq[1]), bq[2]); + if (bss > 32768) + bss = 32768; + else if (bss < 0) + bss = 0; + ds->per = add(shr(ds->per, 1), (Word16)L_shr(bss, 1)); + +} diff --git a/jni/bx16_fixedp/bv16/encoder.c b/app/src/main/jni/bx16_fixedp/bv16/encoder.c similarity index 97% rename from jni/bx16_fixedp/bv16/encoder.c rename to app/src/main/jni/bx16_fixedp/bv16/encoder.c index fcd16e6..6456722 100644 --- a/jni/bx16_fixedp/bv16/encoder.c +++ b/app/src/main/jni/bx16_fixedp/bv16/encoder.c @@ -1,188 +1,188 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - encoder.c : BV16 Fixed-Point Encoder Main Subroutines - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16externs.h" -#include "basop32.h" -#include "utility.h" - -void Reset_BV16_Encoder(struct BV16_Encoder_State *c) -{ - W16zero((Word16 *) c, sizeof(struct BV16_Encoder_State)/sizeof(Word16)); - c->lsplast[0] = 3641; - c->lsplast[1] = 7282; - c->lsplast[2] = 10923; - c->lsplast[3] = 14564; - c->lsplast[4] = 18204; - c->lsplast[5] = 21845; - c->lsplast[6] = 25486; - c->lsplast[7] = 29127; - c->xwd_exp = 32; - c->cpplast = 12*DECF; - c->lmax = MIN_32; - c->lmin = MAX_32; - c->lmean = 419430400; /* 12.5 Q25 */ - c->x1 = 570425344; /* 17.0 Q25 */ - c->level = 570425344; /* 17.0 Q25 */ - c->old_A[0] = 4096; -} - -void BV16_Encode( - struct BV16_Bit_Stream *bs, - struct BV16_Encoder_State *cs, - Word16 *inx) -{ - Word32 r[NSTORDER+1]; /* DPF representation of autocorrelation lags */ - Word16 ltnfm[MAXPP1+FRSZ]; - Word16 a[LPCO+1]; /* LPC coefficients */ - Word16 aw[LPCO+1]; /* Weighted LPC coefficients */ - Word16 fsz[NSTORDER+1]; - Word16 fsp[NSTORDER+1]; - Word16 x[LX]; /* Signal buffer */ - Word16 dq[LX]; /* quantized short term pred error */ - Word16 sdq[LX]; - Word16 xw[FRSZ]; /* Q0 perceptually weighted version of x() */ - Word16 lsp[LPCO], lspq[LPCO]; /* Q15 */ - Word16 cbs[VDIM*CBSZ]; - Word16 bq[3]; /* Q15 */ - Word16 beta; /* Q13 */ - Word16 ppt; /* Q9 */ - Word16 gainq; /* Q2 */ - Word32 ee; /* Q1 */ - Word32 a0; - Word16 gain_exp; - Word16 pp, cpp, i; - Word16 eehi, eelo; - Word16 a0hi, a0lo; - Word16 dummy; - - /* copy state memory to local memory buffers */ - W16copy(x, cs->x, XOFF); - W16copy(ltnfm, cs->ltnfm, MAXPP1); - - /* 150 Hz HighPass filtering */ - preprocess(cs, x+XOFF, inx, FRSZ); - - /* update highpass filtered signal buffer */ - W16copy(cs->x,x+FRSZ,XOFF); - - /* perform lpc analysis with asymmetrical window */ - Autocorr(r, x+LX-WINSZ, winl, WINSZ, NSTORDER); - Spectral_Smoothing(NSTORDER, r, sstwinl_h, sstwinl_l); - Levinson(r, a, cs->old_A, LPCO); - - /* pole-zero noise feedback filter */ - - fsz[0] = 0; - for (i=1;i<=NSTORDER;i++) - fsz[i] = mult_r(a[i], gfsz[i]); - fsp[0] = 4096; - for (i=1;i<=NSTORDER;i++) - fsp[i] = mult_r(a[i], gfsp[i]); - - /* bandwidth expansion */ - for (i=1;i<=LPCO;i++) - a[i] = mult_r(bwel[i],a[i]); - - /* LPC -> LSP Conversion */ - a2lsp(a,lsp,cs->lsplast); - W16copy(cs->lsplast,lsp,LPCO); - - /* Spectral Quantization */ - lspquan(lspq,bs->lspidx,lsp,cs->lsppm); - lsp2a(lspq,a); - - /* calculate lpc prediction residual */ - W16copy(dq,cs->dq,XOFF); - azfilter(a,LPCO,x+XOFF,dq+XOFF,FRSZ); - - /* weighted version of lpc filter to generate weighted speech */ - - aw[0] = a[0]; - for (i=1;i<=LPCO; i++) aw[i] = mult_r(STWAL[i],a[i]); - - /* get perceptually weighted speech signal */ - apfilter(aw, LPCO, dq+XOFF, xw, FRSZ, cs->stwpm, 1); - - /* get the coarse version of pitch period using 4:1 decimation */ - cpp = coarsepitch(xw, cs); - cs->cpplast=cpp; - - /* refine the pitch period in the neighborhood of coarse pitch period - also calculate the pitch predictor tap for single-tap predictor */ - - for (i=0;ippidx = pp - MINPP; - - /* vector quantize 3 pitch predictor taps with minimum residual energy */ - bs->bqidx=pitchtapquan(dq, pp, bq, &ee); - - /* scale up pitch prediction residual energy by 1.5^2=2.25 (18432 Q13) */ - L_Extract(ee, &eehi, &eelo); - ee = L_shl(Mpy_32_16(eehi, eelo, 18432), 2); - - /* get coefficients of long-term noise feedback filter */ - if (ppt > 512) - beta = LTWF; - else if (ppt <= 0) - beta = 0; - else - beta = extract_h(L_shl(L_mult(LTWF, ppt),6)); - - /* gain quantization */ - bs->gidx = gainquan(&a0,&ee,cs->lgpm,cs->prevlg,cs->level); - - /* level estimation */ - dummy = estl_alpha; - estlevel(cs->prevlg[0],&cs->level,&cs->lmax,&cs->lmin, - &cs->lmean,&cs->x1,LGPORDER+1,Nfdm+1,&dummy); - - /* find appropriate gain block floating point exponent */ - gain_exp = sub(norm_l(a0), 2); - /* scale down quantized gain by 1.5, 1/1.5=2/3 (21845 Q15) */ - L_Extract(a0, &a0hi, &a0lo); - a0 = Mpy_32_16(a0hi, a0lo, 21845); - gainq = intround(L_shl(a0, gain_exp)); - - /* scale the scalar quantizer codebook */ - for (i=0;i<(VDIM*CBSZ);i++) - cbs[i] = mult_r(gainq, cccb[i]); - - /* perform noise feedback coding of the excitation signal */ - excquan(bs->qvidx,x+XOFF,a,fsz,fsp,bq,beta,cs->stsym, - dq,ltnfm,cs->stnfz,cs->stnfp,cbs,pp,gain_exp); - - /* update state memory */ - W16copy(cs->dq,&dq[FRSZ],XOFF); - W16copy(cs->ltnfm,<nfm[FRSZ],MAXPP1); - -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + encoder.c : BV16 Fixed-Point Encoder Main Subroutines + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" +#include "utility.h" + +void Reset_BV16_Encoder(struct BV16_Encoder_State *c) +{ + W16zero((Word16 *) c, sizeof(struct BV16_Encoder_State)/sizeof(Word16)); + c->lsplast[0] = 3641; + c->lsplast[1] = 7282; + c->lsplast[2] = 10923; + c->lsplast[3] = 14564; + c->lsplast[4] = 18204; + c->lsplast[5] = 21845; + c->lsplast[6] = 25486; + c->lsplast[7] = 29127; + c->xwd_exp = 32; + c->cpplast = 12*DECF; + c->lmax = MIN_32; + c->lmin = MAX_32; + c->lmean = 419430400; /* 12.5 Q25 */ + c->x1 = 570425344; /* 17.0 Q25 */ + c->level = 570425344; /* 17.0 Q25 */ + c->old_A[0] = 4096; +} + +void BV16_Encode( + struct BV16_Bit_Stream *bs, + struct BV16_Encoder_State *cs, + Word16 *inx) +{ + Word32 r[NSTORDER+1]; /* DPF representation of autocorrelation lags */ + Word16 ltnfm[MAXPP1+FRSZ]; + Word16 a[LPCO+1]; /* LPC coefficients */ + Word16 aw[LPCO+1]; /* Weighted LPC coefficients */ + Word16 fsz[NSTORDER+1]; + Word16 fsp[NSTORDER+1]; + Word16 x[LX]; /* Signal buffer */ + Word16 dq[LX]; /* quantized short term pred error */ + Word16 sdq[LX]; + Word16 xw[FRSZ]; /* Q0 perceptually weighted version of x() */ + Word16 lsp[LPCO], lspq[LPCO]; /* Q15 */ + Word16 cbs[VDIM*CBSZ]; + Word16 bq[3]; /* Q15 */ + Word16 beta; /* Q13 */ + Word16 ppt; /* Q9 */ + Word16 gainq; /* Q2 */ + Word32 ee; /* Q1 */ + Word32 a0; + Word16 gain_exp; + Word16 pp, cpp, i; + Word16 eehi, eelo; + Word16 a0hi, a0lo; + Word16 dummy; + + /* copy state memory to local memory buffers */ + W16copy(x, cs->x, XOFF); + W16copy(ltnfm, cs->ltnfm, MAXPP1); + + /* 150 Hz HighPass filtering */ + preprocess(cs, x+XOFF, inx, FRSZ); + + /* update highpass filtered signal buffer */ + W16copy(cs->x,x+FRSZ,XOFF); + + /* perform lpc analysis with asymmetrical window */ + Autocorr(r, x+LX-WINSZ, winl, WINSZ, NSTORDER); + Spectral_Smoothing(NSTORDER, r, sstwinl_h, sstwinl_l); + Levinson(r, a, cs->old_A, LPCO); + + /* pole-zero noise feedback filter */ + + fsz[0] = 0; + for (i=1;i<=NSTORDER;i++) + fsz[i] = mult_r(a[i], gfsz[i]); + fsp[0] = 4096; + for (i=1;i<=NSTORDER;i++) + fsp[i] = mult_r(a[i], gfsp[i]); + + /* bandwidth expansion */ + for (i=1;i<=LPCO;i++) + a[i] = mult_r(bwel[i],a[i]); + + /* LPC -> LSP Conversion */ + a2lsp(a,lsp,cs->lsplast); + W16copy(cs->lsplast,lsp,LPCO); + + /* Spectral Quantization */ + lspquan(lspq,bs->lspidx,lsp,cs->lsppm); + lsp2a(lspq,a); + + /* calculate lpc prediction residual */ + W16copy(dq,cs->dq,XOFF); + azfilter(a,LPCO,x+XOFF,dq+XOFF,FRSZ); + + /* weighted version of lpc filter to generate weighted speech */ + + aw[0] = a[0]; + for (i=1;i<=LPCO; i++) aw[i] = mult_r(STWAL[i],a[i]); + + /* get perceptually weighted speech signal */ + apfilter(aw, LPCO, dq+XOFF, xw, FRSZ, cs->stwpm, 1); + + /* get the coarse version of pitch period using 4:1 decimation */ + cpp = coarsepitch(xw, cs); + cs->cpplast=cpp; + + /* refine the pitch period in the neighborhood of coarse pitch period + also calculate the pitch predictor tap for single-tap predictor */ + + for (i=0;ippidx = pp - MINPP; + + /* vector quantize 3 pitch predictor taps with minimum residual energy */ + bs->bqidx=pitchtapquan(dq, pp, bq, &ee); + + /* scale up pitch prediction residual energy by 1.5^2=2.25 (18432 Q13) */ + L_Extract(ee, &eehi, &eelo); + ee = L_shl(Mpy_32_16(eehi, eelo, 18432), 2); + + /* get coefficients of long-term noise feedback filter */ + if (ppt > 512) + beta = LTWF; + else if (ppt <= 0) + beta = 0; + else + beta = extract_h(L_shl(L_mult(LTWF, ppt),6)); + + /* gain quantization */ + bs->gidx = gainquan(&a0,&ee,cs->lgpm,cs->prevlg,cs->level); + + /* level estimation */ + dummy = estl_alpha; + estlevel(cs->prevlg[0],&cs->level,&cs->lmax,&cs->lmin, + &cs->lmean,&cs->x1,LGPORDER+1,Nfdm+1,&dummy); + + /* find appropriate gain block floating point exponent */ + gain_exp = sub(norm_l(a0), 2); + /* scale down quantized gain by 1.5, 1/1.5=2/3 (21845 Q15) */ + L_Extract(a0, &a0hi, &a0lo); + a0 = Mpy_32_16(a0hi, a0lo, 21845); + gainq = intround(L_shl(a0, gain_exp)); + + /* scale the scalar quantizer codebook */ + for (i=0;i<(VDIM*CBSZ);i++) + cbs[i] = mult_r(gainq, cccb[i]); + + /* perform noise feedback coding of the excitation signal */ + excquan(bs->qvidx,x+XOFF,a,fsz,fsp,bq,beta,cs->stsym, + dq,ltnfm,cs->stnfz,cs->stnfp,cbs,pp,gain_exp); + + /* update state memory */ + W16copy(cs->dq,&dq[FRSZ],XOFF); + W16copy(cs->ltnfm,<nfm[FRSZ],MAXPP1); + +} diff --git a/jni/bx16_fixedp/bv16/excdec.c b/app/src/main/jni/bx16_fixedp/bv16/excdec.c similarity index 97% rename from jni/bx16_fixedp/bv16/excdec.c rename to app/src/main/jni/bx16_fixedp/bv16/excdec.c index 5c9400d..08a9e1a 100644 --- a/jni/bx16_fixedp/bv16/excdec.c +++ b/app/src/main/jni/bx16_fixedp/bv16/excdec.c @@ -1,130 +1,130 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - excdec.c :excitation signal decoding with integrated long-term and - short-term synthesis. - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "basop32.h" -#include "utility.h" - -void excdec_w_synth( - Word16 *xq, /* (o) Q0 quantized signal vector */ - Word16 *ltsym, /* (i/o) Q0 quantized excitation signal vector */ - Word16 *stsym, /* (i/o) Q0 short-term predictor memory */ - Word16 *idx, /* (o) quantizer codebook index for uq[] vector */ - Word16 *b, /* (i) Q15 coefficient of 3-tap pitch predictor */ - Word16 *cb, /* (i) Q0 codebook */ - Word16 pp, /* pitch period (# of 8 kHz samples) */ - Word16 *aq, /* (i) Q12 short-term predictor coefficients */ - Word16 gain_exp, /* gain_exp of current sub-frame */ - Word32 *EE - ) -{ - Word16 i, n, m, *ip, id; - Word16 *fp1, *fp2, *fp3; - Word32 a0; - Word16 tt; - Word32 E; - Word32 a1; - Word16 buf1[LPCO+FRSZ]; /* buffer for filter memory & signal */ - Word16 uq[VDIM]; /* selected codebook vector (incl. sign) */ - - W16copy(buf1, stsym, LPCO); /* buffer is used to avoid memory shifts */ - - ip=idx; - E = 0; - - /* Loop through every vector of the current subframe */ - for (m = 0; m < FRSZ; m += VDIM) { - - /********************************************************************************/ - /* Excitation vector */ - /********************************************************************************/ - - id = *ip++; /* get codebook index of current vector */ - fp1 = uq; - if (id < CBSZ){ - fp2 = &cb[id*VDIM]; - for (n=0;n 0; i--) - a1 = L_msu(a1, *fp1++, aq[i]); // Q13 - a1 = L_shl(a1, 3); // Q16 - a1 = L_add(a0, a1); - *fp1++ = intround(a1); // Q0 - - } - } - - /* Update noise feedback filter memory after filtering current subframe */ - W16copy(stsym, buf1+FRSZ, LPCO); - - /* copy to speech buffer */ - W16copy(xq, buf1+LPCO, FRSZ); - *EE = E; - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + excdec.c :excitation signal decoding with integrated long-term and + short-term synthesis. + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "basop32.h" +#include "utility.h" + +void excdec_w_synth( + Word16 *xq, /* (o) Q0 quantized signal vector */ + Word16 *ltsym, /* (i/o) Q0 quantized excitation signal vector */ + Word16 *stsym, /* (i/o) Q0 short-term predictor memory */ + Word16 *idx, /* (o) quantizer codebook index for uq[] vector */ + Word16 *b, /* (i) Q15 coefficient of 3-tap pitch predictor */ + Word16 *cb, /* (i) Q0 codebook */ + Word16 pp, /* pitch period (# of 8 kHz samples) */ + Word16 *aq, /* (i) Q12 short-term predictor coefficients */ + Word16 gain_exp, /* gain_exp of current sub-frame */ + Word32 *EE + ) +{ + Word16 i, n, m, *ip, id; + Word16 *fp1, *fp2, *fp3; + Word32 a0; + Word16 tt; + Word32 E; + Word32 a1; + Word16 buf1[LPCO+FRSZ]; /* buffer for filter memory & signal */ + Word16 uq[VDIM]; /* selected codebook vector (incl. sign) */ + + W16copy(buf1, stsym, LPCO); /* buffer is used to avoid memory shifts */ + + ip=idx; + E = 0; + + /* Loop through every vector of the current subframe */ + for (m = 0; m < FRSZ; m += VDIM) { + + /********************************************************************************/ + /* Excitation vector */ + /********************************************************************************/ + + id = *ip++; /* get codebook index of current vector */ + fp1 = uq; + if (id < CBSZ){ + fp2 = &cb[id*VDIM]; + for (n=0;n 0; i--) + a1 = L_msu(a1, *fp1++, aq[i]); // Q13 + a1 = L_shl(a1, 3); // Q16 + a1 = L_add(a0, a1); + *fp1++ = intround(a1); // Q0 + + } + } + + /* Update noise feedback filter memory after filtering current subframe */ + W16copy(stsym, buf1+FRSZ, LPCO); + + /* copy to speech buffer */ + W16copy(xq, buf1+LPCO, FRSZ); + *EE = E; + + return; +} diff --git a/jni/bx16_fixedp/bv16/excquan.c b/app/src/main/jni/bx16_fixedp/bv16/excquan.c similarity index 97% rename from jni/bx16_fixedp/bv16/excquan.c rename to app/src/main/jni/bx16_fixedp/bv16/excquan.c index a213868..5d19365 100644 --- a/jni/bx16_fixedp/bv16/excquan.c +++ b/app/src/main/jni/bx16_fixedp/bv16/excquan.c @@ -1,403 +1,403 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - excquan.c : Vector Quantizer for 2-Stage Noise Feedback Coding - with long-term predictive noise feedback coding embedded - inside the short-term predictive noise feedback coding loop. - - Note that the Noise Feedback Coding of the excitation signal is implemented - using the Zero-State Responsse and Zero-input Response decomposition as - described in: J.-H. Chen, "Novel Codec Structures for Noise Feedback - Coding of Speech," Proc. ICASSP, 2006. The principle of the Noise Feedback - Coding of the excitation signal is described in: "BV16 Speech Codec - Specification for Voice over IP Applications in Cable Telephony," American - National Standard, ANSI/SCTE 24-21 2006. - - Note that indicated Q-values may be relative to the over-all normalization - by gain_exp. - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "basop32.h" -#include "utility.h" - -void vq2snfc_zsr_codebook( - Word16 *qzsr, // normalized by gain_exp-1 - Word16 *cb, // normalized by gain_exp - Word16 *aq, // Q12 - Word16 *fsz, // Q12 - Word16 *fsp) // Q12 -{ - Word32 a0, a1; - Word16 buf1[VDIM], buf2[VDIM], buf3[VDIM]; - Word16 *fp1, *fp2, *fp3, *fpa, *fpb; - Word16 j, i, n; - - /* Q-values of signals are relative to the normalization by gain_exp */ - - /* Calculate negated Zero State Response */ - fp2 = cb; /* fp2 points to start of first codevector */ - fp3 = qzsr; /* fp3 points to start of first zero-state response vector */ - - /* For each codevector */ - for(j=0; j 0; n--) - a0 = L_msu(a0, *fp1++, aq[n]); // Q13 - a0 = L_shl(a0, 3); // Q16 - - /* Update memory of short-term prediction filter */ - a0 = L_add(a0, L_deposit_h(*fp2++)); - *fp1++ = intround(a0); - - /* noise feedback filter */ - a1 = 0; - fpa = buf2; // Q0 - fpb = buf3; // Q0 - for (n = i; n > 0; n--){ - a1 = L_mac(a1, *fpa++, fsz[n]); // Q13 - a1 = L_msu(a1, *fpb++, fsp[n]); // Q13 - } - a1 = L_shl(a1, 3); // Q16 - - /* Update memory of pole section of noise feedback filter */ - *fpb++ = intround(a1); // Q0 - - /* ZSR */ - a0 = L_add(a0, a1); // Q16 - - /* Update memory of zero section of noise feedback filter */ - *fpa++ = negate(intround(a0)); - - /* Get ZSR at correct normalization - gain_exp-1 */ - *fp3++ = intround(L_shr(a0,1)); - } - } - - return; -} - -/* COMPUTE PITCH-PREDICTED VECTOR, WHICH SHOULD BE INDEPENDENT OF THE -RESIDUAL VQ CODEVECTORS BEING TRIED IF vdim < MIN. PITCH PERIOD */ - -void vq2snfc_ppv( - Word32 *ltfv, // Q16 - Word32 *ppv, // Q16 - Word16 *ltsym, // Q0 - Word16 *ltnfm, // Q0 - Word16 *b, // Q15 - Word16 beta) // Q13 -{ - Word32 a0, a1; - Word16 n, *sp1; - for (n = 0; n < VDIM; n++) { - sp1 = <sym[n]; // Q0 - a0 = L_mult(*sp1--, b[0]); // Q16 - a0 = L_mac(a0, *sp1--, b[1]); // Q16 - a0 = L_mac(a0, *sp1--, b[2]); // Q16 - *ppv++ = a0; // Q16 - a1 = L_mult(ltnfm[n], beta); // Q14 - a1 = L_shl(a1, 2); // Q16 - *ltfv++ = L_add(a0, a1); // Q16 - } - - return; -} - -void vq2snfc_zir( - Word16 *qzir, - Word32 *ppv, - Word32 *ltfv, - Word16 *aq, - Word16 *buf1, - Word16 *buf2, - Word16 *buf3, - Word16 *fsz, - Word16 *fsp, - Word16 *s, - Word16 gexpm3) -{ - Word32 a0, a1, a2; - Word16 i, n; - Word16 *sp1, *spa, *spb; - - for (n = 0; n < VDIM; n++) { - - /* Perform multiply-adds along the delay line of filter */ - sp1 = &buf1[n]; /* Q16 */ - a0 = 0; /* Q13 */ - for (i = LPCO; i > 0; i--) a0 = L_msu(a0, *sp1++, aq[i]); - a0 = L_shl(a0, 3); /* Q16 */ - /* Perform multiply-adds along the noise feedback filter */ - spa = &buf2[n]; - spb = &buf3[n]; - a1 = 0; - for (i=NSTORDER; i > 0; i--) { - a1 = L_mac(a1, *spa++, fsz[i]); - a1 = L_msu(a1, *spb++, fsp[i]); - } - a1 = L_shl(a1, 3); /* Q16 */ - *spb = intround(a1); /* update output of the noise feedback filter */ - a2 = L_deposit_h(s[n]); - a2 = L_sub(a2, a0); - a2 = L_sub(a2, a1); /* Q16 */ - *qzir++ = intround(L_shl(L_sub(a2,*ltfv++),gexpm3)); - /* Update short-term noise feedback filter memory */ - a0 = L_add(a0, *ppv); /* a0 now conatins the qs[n] */ - *sp1 = intround(a0); - a2 = L_sub(a2, *ppv++); /* a2 now contains qszi[n] */ - *spa = intround(a2); /* update short-term noise feedback filter memory */ - - } - - return; -} - - -/* loop through every codevector of the residual vq codebook */ -/* and find the one that minimizes the energy of q[n] */ - -Word16 vq2snfc_vq( - Word16 *qzsr, // normalized by gain_exp - 1 - Word16 *qzir, // normalized by gain_exp - 3 - Word16 *rsign) -{ - Word32 Emin, E; - Word16 j, n, jmin, sign=1, e; - Word16 *fp4, *fp2; - - Emin = MAX_32; - jmin = 0; - fp4 = qzsr; - for (j = 0; j < CBSZ; j++) { - /* Try positive sign */ - fp2 = qzir; - E = 0; - for (n=0;n 0; i--) - a0 = L_msu(a0, *fp1++, aq[i]); // Q13 - a0 = L_shl(a0, 3); // Q16 - - /* Update memory of short-term synthesis filter */ - *fp1++ = intround(L_add(a0, vq)); // Q0 - - /* Short-term pole-zero noise feedback filter */ - fpa = stnfz+n; // Q0 - fpb = stnfp+n; // Q0 - a1 = 0; - for (i=NSTORDER; i > 0; i--) { - a1 = L_mac(a1, *fpa++, fsz[i]); // Q13 - a1 = L_msu(a1, *fpb++, fsp[i]); // Q13 - } - a1 = L_shl(a1, 3); // Q16 - - /* Update memory of pole section of noise feedback filter */ - *fpb++ = intround(a1); // Q0 - - v = L_sub(L_sub(L_deposit_h(*p_s++), a0), a1); - // Q16 - qs = L_sub(v, vq); // Q16 - - /* Update memory of zero section of noise feedback filter */ - *fpa++ = intround(qs); // Q0 - - /* Update memory of long-term noise feedback filter */ - *fp4++ = intround(L_sub(L_sub(v, *p_ltfv++), uq32)); - // Q0 - } - - return; -} - -void excquan( - Word16 *idx, /* quantizer codebook index for uq[] vector */ - Word16 *s, /* (i) Q0 input signal vector */ - Word16 *aq, /* (i) Q12 noise feedback filter coefficient array */ - Word16 *fsz, /* (i) Q12 short-term noise feedback filter - numerator */ - Word16 *fsp, /* (i) Q12 short-term noise feedback filter - denominator */ - Word16 *b, /* (i) Q15 coefficient of 3-tap pitch predictor */ - Word16 beta, /* (i) Q13 coefficient of pitch feedback filter */ - Word16 *stsym, /* (i/o) Q0 filter memory */ - Word16 *ltsym, /* (i/0) Q0 long-term synthesis filter memory */ - Word16 *ltnfm, /* (i/o) Q0 long-term noise feedback filter memory */ - Word16 *stnfz, /* (i/o) Q0 filter memory */ - Word16 *stnfp, /* (i/o) Q0 filter memory */ - Word16 *cb, /* (i) scalar quantizer codebook - normalized by gain_exp */ - Word16 pp, /* pitch period (# of 8 kHz samples) */ - Word16 gain_exp - ) -{ - Word16 qzir[VDIM]; // normalized by gain_exp-3 - Word16 uq[VDIM]; // normalized by gain_exp - Word16 buf1[LPCO+FRSZ]; // Q0 - Word16 buf2[NSTORDER+FRSZ]; // Q0 - Word16 buf3[NSTORDER+FRSZ]; // Q0 - Word32 ltfv[VDIM], ppv[VDIM]; // Q16 - Word16 qzsr[VDIM*CBSZ]; // normalized by gain_exp-1 - Word16 *sp3; - Word16 sign=1; - Word16 m, n, jmin, iv; - Word16 gexpm1, gexpm3; - - gexpm1 = sub(gain_exp, 1); - gexpm3 = sub(gain_exp, 3); - - /* COPY FILTER MEMORY TO BEGINNING PART OF TEMPORARY BUFFER */ - W16copy(buf1, stsym, LPCO); /* buffer is used to avoid memory shifts */ - - /* COPY NOISE FEEDBACK FILTER MEMORY */ - W16copy(buf2, stnfz, NSTORDER); - W16copy(buf3, stnfp, NSTORDER); - - /* -------------------------------------- */ - /* Z e r o - S t a t e R e s p o n s e */ - /* -------------------------------------- */ - - vq2snfc_zsr_codebook(qzsr,cb,aq,fsz,fsp); - - /* --------------------------------------------------- */ - /* LOOP THROUGH EVERY VECTOR OF THE CURRENT SUBFRAME */ - /* --------------------------------------------------- */ - - iv = 0; /* iv = index of the current vector */ - for (m = 0; m < FRSZ; m += VDIM) { - - /* --------------------------------------- */ - /* Z e r o - I n p u t R e s p o n s e */ - /* --------------------------------------- */ - - /* Compute pitch-predicted vector, which should be independent of the - residual vq codevectors being tried if vdim < min. pitch period */ - vq2snfc_ppv(ltfv,ppv,<sym[MAXPP1+m-pp+1],<nfm[MAXPP1+m-pp],b,beta); - - /* Compute zero-input response */ - vq2snfc_zir(qzir,ppv,ltfv,aq,&buf1[m],&buf2[m],&buf3[m],fsz,fsp,&s[m],gexpm3); - - /* --------------------------------------- */ - /* C O D E B O O K S E A R C H */ - /* --------------------------------------- */ - - jmin = vq2snfc_vq(qzsr,qzir,&sign); - - /* The best codevector has been found; assign vq codebook index */ - idx[iv++] = (sign==-1) ? (jmin+CBSZ) : jmin ; - - sp3 = &cb[jmin*VDIM]; /* sp3 points to start of best codevector */ - - for (n=0;n 0; n--) + a0 = L_msu(a0, *fp1++, aq[n]); // Q13 + a0 = L_shl(a0, 3); // Q16 + + /* Update memory of short-term prediction filter */ + a0 = L_add(a0, L_deposit_h(*fp2++)); + *fp1++ = intround(a0); + + /* noise feedback filter */ + a1 = 0; + fpa = buf2; // Q0 + fpb = buf3; // Q0 + for (n = i; n > 0; n--){ + a1 = L_mac(a1, *fpa++, fsz[n]); // Q13 + a1 = L_msu(a1, *fpb++, fsp[n]); // Q13 + } + a1 = L_shl(a1, 3); // Q16 + + /* Update memory of pole section of noise feedback filter */ + *fpb++ = intround(a1); // Q0 + + /* ZSR */ + a0 = L_add(a0, a1); // Q16 + + /* Update memory of zero section of noise feedback filter */ + *fpa++ = negate(intround(a0)); + + /* Get ZSR at correct normalization - gain_exp-1 */ + *fp3++ = intround(L_shr(a0,1)); + } + } + + return; +} + +/* COMPUTE PITCH-PREDICTED VECTOR, WHICH SHOULD BE INDEPENDENT OF THE +RESIDUAL VQ CODEVECTORS BEING TRIED IF vdim < MIN. PITCH PERIOD */ + +void vq2snfc_ppv( + Word32 *ltfv, // Q16 + Word32 *ppv, // Q16 + Word16 *ltsym, // Q0 + Word16 *ltnfm, // Q0 + Word16 *b, // Q15 + Word16 beta) // Q13 +{ + Word32 a0, a1; + Word16 n, *sp1; + for (n = 0; n < VDIM; n++) { + sp1 = <sym[n]; // Q0 + a0 = L_mult(*sp1--, b[0]); // Q16 + a0 = L_mac(a0, *sp1--, b[1]); // Q16 + a0 = L_mac(a0, *sp1--, b[2]); // Q16 + *ppv++ = a0; // Q16 + a1 = L_mult(ltnfm[n], beta); // Q14 + a1 = L_shl(a1, 2); // Q16 + *ltfv++ = L_add(a0, a1); // Q16 + } + + return; +} + +void vq2snfc_zir( + Word16 *qzir, + Word32 *ppv, + Word32 *ltfv, + Word16 *aq, + Word16 *buf1, + Word16 *buf2, + Word16 *buf3, + Word16 *fsz, + Word16 *fsp, + Word16 *s, + Word16 gexpm3) +{ + Word32 a0, a1, a2; + Word16 i, n; + Word16 *sp1, *spa, *spb; + + for (n = 0; n < VDIM; n++) { + + /* Perform multiply-adds along the delay line of filter */ + sp1 = &buf1[n]; /* Q16 */ + a0 = 0; /* Q13 */ + for (i = LPCO; i > 0; i--) a0 = L_msu(a0, *sp1++, aq[i]); + a0 = L_shl(a0, 3); /* Q16 */ + /* Perform multiply-adds along the noise feedback filter */ + spa = &buf2[n]; + spb = &buf3[n]; + a1 = 0; + for (i=NSTORDER; i > 0; i--) { + a1 = L_mac(a1, *spa++, fsz[i]); + a1 = L_msu(a1, *spb++, fsp[i]); + } + a1 = L_shl(a1, 3); /* Q16 */ + *spb = intround(a1); /* update output of the noise feedback filter */ + a2 = L_deposit_h(s[n]); + a2 = L_sub(a2, a0); + a2 = L_sub(a2, a1); /* Q16 */ + *qzir++ = intround(L_shl(L_sub(a2,*ltfv++),gexpm3)); + /* Update short-term noise feedback filter memory */ + a0 = L_add(a0, *ppv); /* a0 now conatins the qs[n] */ + *sp1 = intround(a0); + a2 = L_sub(a2, *ppv++); /* a2 now contains qszi[n] */ + *spa = intround(a2); /* update short-term noise feedback filter memory */ + + } + + return; +} + + +/* loop through every codevector of the residual vq codebook */ +/* and find the one that minimizes the energy of q[n] */ + +Word16 vq2snfc_vq( + Word16 *qzsr, // normalized by gain_exp - 1 + Word16 *qzir, // normalized by gain_exp - 3 + Word16 *rsign) +{ + Word32 Emin, E; + Word16 j, n, jmin, sign=1, e; + Word16 *fp4, *fp2; + + Emin = MAX_32; + jmin = 0; + fp4 = qzsr; + for (j = 0; j < CBSZ; j++) { + /* Try positive sign */ + fp2 = qzir; + E = 0; + for (n=0;n 0; i--) + a0 = L_msu(a0, *fp1++, aq[i]); // Q13 + a0 = L_shl(a0, 3); // Q16 + + /* Update memory of short-term synthesis filter */ + *fp1++ = intround(L_add(a0, vq)); // Q0 + + /* Short-term pole-zero noise feedback filter */ + fpa = stnfz+n; // Q0 + fpb = stnfp+n; // Q0 + a1 = 0; + for (i=NSTORDER; i > 0; i--) { + a1 = L_mac(a1, *fpa++, fsz[i]); // Q13 + a1 = L_msu(a1, *fpb++, fsp[i]); // Q13 + } + a1 = L_shl(a1, 3); // Q16 + + /* Update memory of pole section of noise feedback filter */ + *fpb++ = intround(a1); // Q0 + + v = L_sub(L_sub(L_deposit_h(*p_s++), a0), a1); + // Q16 + qs = L_sub(v, vq); // Q16 + + /* Update memory of zero section of noise feedback filter */ + *fpa++ = intround(qs); // Q0 + + /* Update memory of long-term noise feedback filter */ + *fp4++ = intround(L_sub(L_sub(v, *p_ltfv++), uq32)); + // Q0 + } + + return; +} + +void excquan( + Word16 *idx, /* quantizer codebook index for uq[] vector */ + Word16 *s, /* (i) Q0 input signal vector */ + Word16 *aq, /* (i) Q12 noise feedback filter coefficient array */ + Word16 *fsz, /* (i) Q12 short-term noise feedback filter - numerator */ + Word16 *fsp, /* (i) Q12 short-term noise feedback filter - denominator */ + Word16 *b, /* (i) Q15 coefficient of 3-tap pitch predictor */ + Word16 beta, /* (i) Q13 coefficient of pitch feedback filter */ + Word16 *stsym, /* (i/o) Q0 filter memory */ + Word16 *ltsym, /* (i/0) Q0 long-term synthesis filter memory */ + Word16 *ltnfm, /* (i/o) Q0 long-term noise feedback filter memory */ + Word16 *stnfz, /* (i/o) Q0 filter memory */ + Word16 *stnfp, /* (i/o) Q0 filter memory */ + Word16 *cb, /* (i) scalar quantizer codebook - normalized by gain_exp */ + Word16 pp, /* pitch period (# of 8 kHz samples) */ + Word16 gain_exp + ) +{ + Word16 qzir[VDIM]; // normalized by gain_exp-3 + Word16 uq[VDIM]; // normalized by gain_exp + Word16 buf1[LPCO+FRSZ]; // Q0 + Word16 buf2[NSTORDER+FRSZ]; // Q0 + Word16 buf3[NSTORDER+FRSZ]; // Q0 + Word32 ltfv[VDIM], ppv[VDIM]; // Q16 + Word16 qzsr[VDIM*CBSZ]; // normalized by gain_exp-1 + Word16 *sp3; + Word16 sign=1; + Word16 m, n, jmin, iv; + Word16 gexpm1, gexpm3; + + gexpm1 = sub(gain_exp, 1); + gexpm3 = sub(gain_exp, 3); + + /* COPY FILTER MEMORY TO BEGINNING PART OF TEMPORARY BUFFER */ + W16copy(buf1, stsym, LPCO); /* buffer is used to avoid memory shifts */ + + /* COPY NOISE FEEDBACK FILTER MEMORY */ + W16copy(buf2, stnfz, NSTORDER); + W16copy(buf3, stnfp, NSTORDER); + + /* -------------------------------------- */ + /* Z e r o - S t a t e R e s p o n s e */ + /* -------------------------------------- */ + + vq2snfc_zsr_codebook(qzsr,cb,aq,fsz,fsp); + + /* --------------------------------------------------- */ + /* LOOP THROUGH EVERY VECTOR OF THE CURRENT SUBFRAME */ + /* --------------------------------------------------- */ + + iv = 0; /* iv = index of the current vector */ + for (m = 0; m < FRSZ; m += VDIM) { + + /* --------------------------------------- */ + /* Z e r o - I n p u t R e s p o n s e */ + /* --------------------------------------- */ + + /* Compute pitch-predicted vector, which should be independent of the + residual vq codevectors being tried if vdim < min. pitch period */ + vq2snfc_ppv(ltfv,ppv,<sym[MAXPP1+m-pp+1],<nfm[MAXPP1+m-pp],b,beta); + + /* Compute zero-input response */ + vq2snfc_zir(qzir,ppv,ltfv,aq,&buf1[m],&buf2[m],&buf3[m],fsz,fsp,&s[m],gexpm3); + + /* --------------------------------------- */ + /* C O D E B O O K S E A R C H */ + /* --------------------------------------- */ + + jmin = vq2snfc_vq(qzsr,qzir,&sign); + + /* The best codevector has been found; assign vq codebook index */ + idx[iv++] = (sign==-1) ? (jmin+CBSZ) : jmin ; + + sp3 = &cb[jmin*VDIM]; /* sp3 points to start of best codevector */ + + for (n=0;n= MAXPP) cpp = MAXPP-1; - if (cpp < MINPP) cpp = MINPP; - lb = sub((Word16)cpp,DEV); - if (lb < MINPP) lb = MINPP; /* lower bound of pitch period search range */ - ub = add((Word16)cpp,DEV); - /* to avoid selecting HMAXPP as the refined pitch period */ - if (ub >= MAXPP) ub = MAXPP-1;/* lower bound of pitch period search range */ - - i = lb; /* start the search from lower bound */ - xt = x+XOFF; - sp0 = xt; - sp1 = xt-i; - cor = energy = 0; - for (j=0;j0) - { - a0 = L_mult0(cor2, energymax); - a1 = L_mult0(cor2max, ener); - s = add(cor2_exp, energymax_exp); - t = add(cor2max_exp, ener_exp); - if (s>=t) a0 = L_shr(a0, sub(s,t)); - else a1 = L_shr(a1, sub(t,s)); - if (a0 > a1) - { - pp = i; - cormax = cor; - enermax32 = energy; - cor2max = cor2; - cor2max_exp = cor2_exp; - energymax = ener; - energymax_exp = ener_exp; - } - } - } - - if ((enermax32 == 0) || (cormax<=0)) - *ppt = 0; - else - { - ub = sub(norm_l(cormax),1); - lb = norm_l(enermax32); - s = extract_h(L_shl(cormax,ub)); - t = extract_h(L_shl(enermax32,lb)); - s = div_s(s, t); - lb = sub(sub(lb,ub),6); - *ppt = shl(s, lb); - } - return pp; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + fineptch.c : Fine pitch search functions. + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "basop32.h" + +#define DEV (DECF-1) + +Word16 refinepitch( + Word16 *x, /* (i) Q1 */ + Word16 cpp, + Word16 *ppt) /* (o) Q9 */ +{ + Word32 a0, a1; + Word32 cor, energy, cormax, enermax32; /* Q3 */ + Word16 energymax, energymax_exp, ener, ener_exp; + Word16 cor2, cor2_exp, cor2max, cor2max_exp; + Word16 *sp0, *sp1, *sp2, *sp3; + Word16 *xt; + Word16 s, t; + Word16 lb, ub; + int pp, i, j; + + if (cpp >= MAXPP) cpp = MAXPP-1; + if (cpp < MINPP) cpp = MINPP; + lb = sub((Word16)cpp,DEV); + if (lb < MINPP) lb = MINPP; /* lower bound of pitch period search range */ + ub = add((Word16)cpp,DEV); + /* to avoid selecting HMAXPP as the refined pitch period */ + if (ub >= MAXPP) ub = MAXPP-1;/* lower bound of pitch period search range */ + + i = lb; /* start the search from lower bound */ + xt = x+XOFF; + sp0 = xt; + sp1 = xt-i; + cor = energy = 0; + for (j=0;j0) + { + a0 = L_mult0(cor2, energymax); + a1 = L_mult0(cor2max, ener); + s = add(cor2_exp, energymax_exp); + t = add(cor2max_exp, ener_exp); + if (s>=t) a0 = L_shr(a0, sub(s,t)); + else a1 = L_shr(a1, sub(t,s)); + if (a0 > a1) + { + pp = i; + cormax = cor; + enermax32 = energy; + cor2max = cor2; + cor2max_exp = cor2_exp; + energymax = ener; + energymax_exp = ener_exp; + } + } + } + + if ((enermax32 == 0) || (cormax<=0)) + *ppt = 0; + else + { + ub = sub(norm_l(cormax),1); + lb = norm_l(enermax32); + s = extract_h(L_shl(cormax,ub)); + t = extract_h(L_shl(enermax32,lb)); + s = div_s(s, t); + lb = sub(sub(lb,ub),6); + *ppt = shl(s, lb); + } + return pp; +} diff --git a/jni/bx16_fixedp/bv16/g192.c b/app/src/main/jni/bx16_fixedp/bv16/g192.c similarity index 96% rename from jni/bx16_fixedp/bv16/g192.c rename to app/src/main/jni/bx16_fixedp/bv16/g192.c index 85c5e77..dbf1f06 100644 --- a/jni/bx16_fixedp/bv16/g192.c +++ b/app/src/main/jni/bx16_fixedp/bv16/g192.c @@ -1,131 +1,131 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - g192.c : Implementation of optional G.192 bit-stream format - - $Log$ -******************************************************************************/ - -#include -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" - -#define NBIT 80 /* number of bits per frame */ -#define BIT_0 (short)0x007f -#define BIT_1 (short)0x0081 -#define SYNC_WORD (short)0x6b21 - -extern Word16 bfi; - -Word16 bit_table_16[] = { - 7, 7, /* LSP */ - 7, /* Pitch Lag */ - 5, /* Pitch Gain */ - 4, /* Excitation Vector Log-Gain */ - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 /* Excitation Vector */ -}; - -#define Nindices 15 /* number of Q indices per frame */ - -Word16 bin2int_16(Word16 no_of_bits, Word16 *bitstream) -{ - Word16 index, b_pos; - - index = 0; - for(b_pos=0; b_pos> 1; - } - - return; -} - -void bv16_fwrite_g192bitstrm(struct BV16_Bit_Stream *bs, FILE *fo) -{ - Word16 n, m; - Word16 bitstream[NBIT+2], *p_bitstream, *pbs; - - bitstream[0] = SYNC_WORD; - bitstream[1] = NBIT; - p_bitstream = bitstream + 2; - - pbs = (Word16 *) bs; - - for (n=0;n +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" + +#define NBIT 80 /* number of bits per frame */ +#define BIT_0 (short)0x007f +#define BIT_1 (short)0x0081 +#define SYNC_WORD (short)0x6b21 + +extern Word16 bfi; + +Word16 bit_table_16[] = { + 7, 7, /* LSP */ + 7, /* Pitch Lag */ + 5, /* Pitch Gain */ + 4, /* Excitation Vector Log-Gain */ + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 /* Excitation Vector */ +}; + +#define Nindices 15 /* number of Q indices per frame */ + +Word16 bin2int_16(Word16 no_of_bits, Word16 *bitstream) +{ + Word16 index, b_pos; + + index = 0; + for(b_pos=0; b_pos> 1; + } + + return; +} + +void bv16_fwrite_g192bitstrm(struct BV16_Bit_Stream *bs, FILE *fo) +{ + Word16 n, m; + Word16 bitstream[NBIT+2], *p_bitstream, *pbs; + + bitstream[0] = SYNC_WORD; + bitstream[1] = NBIT; + p_bitstream = bitstream + 2; + + pbs = (Word16 *) bs; + + for (n=0;n= NGB) { - i = NGB - 1; - } else if (i < 0) { - i = 0; - } - n = shr(sub(shr(extract_h(L_sub(prevlg[0],prevlg[1])),9),LGCLB),1); - /* get row index */ - if (n >= NGCB) { - n = NGCB - 1; - } else if (n < 0) { - n = 0; - } - - i = i * NGCB + n; - - /* Update log-gain predictor memory, - check whether decoded log-gain exceeds lgclimit */ - for (k = LGPORDER - 1; k > 0; k--) { - lgpm[k] = lgpm[k-1]; - } - lgc = extract_h(L_sub(*lgq, prevlg[0])); /* Q9 */ - if ((lgc>lgclimit[i]) && (gidx>0)) { /* if log-gain exceeds limit */ - *lgq = prevlg[0]; /* use the log-gain of previous frame */ - lgpm[0] = extract_h(L_shl(L_sub(*lgq, elg),2)); - *nggalgc = 0; - *lg_el = L_add(L_deposit_h(lgclimit[i]), prevlg[0]); - } else { - lgpm[0] = lgpecb[gidx]; - *nggalgc = add(*nggalgc, 1); - if (*nggalgc > Nfdm) *nggalgc = Nfdm+1; - *lg_el = *lgq; - } - - /* Update log-gain predictor memory */ - prevlg[1] = prevlg[0]; - prevlg[0] = *lgq; - - /* Convert quantized log-gain to linear domain */ - elg = L_shr(*lgq,10); /* Q25 -> Q26 (0.5F) --> Q16 */ - L_Extract(elg, &lg_exp, &lg_frac); - lg_exp = add(lg_exp, 18); /* output to be Q2 */ - return Pow2(lg_exp, lg_frac); -} - -void gainplc(Word32 E, Word16 *lgeqm, Word32 *lgqm) -{ - int k; - Word16 exponent, fraction, lge; - Word32 lg, mrlg, elg; - - exponent = 1; - fraction = 0; - if (E > TMinlg*FRSZ) - { - Log2( E, &exponent, &fraction); - lg = L_add(L_shl(L_deposit_h(exponent),9), - L_shr(L_deposit_h(fraction),6)); /* Q25 */ - lg = L_sub(lg, 178574274); /* 178574274 = log2(1/SFSZL) Q 25 */ - } - else - lg = 0; /* Minlg */ - - mrlg = L_shr(L_deposit_h(lgmean),2); /* Q25 */ - mrlg = L_sub(lg, mrlg); /* Q25 */ - - elg = 0; - for(k=0; k0; k--) - lgeqm[k] = lgeqm[k-1]; - lgeqm[0] = lge; - - /* update quantized log-gain memory */ - lgqm[1] = lgqm[0]; - lgqm[0] = lg; - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + gaindec.c : gain decoding functions + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" +#include "mathutil.h" + +Word32 gaindec( /* Q18 */ + Word32 *lgq, /* Q25 */ + Word16 gidx, + Word16 *lgpm, /* Q11 */ + Word32 *prevlg, /* Q25 */ + Word32 level, /* Q25 */ + Word16 *nggalgc, + Word32 *lg_el) +{ + Word32 elg; + Word16 lg_exp, lg_frac, lgc; + Word16 i, n, k; + + /* Calculate estimated log-gain */ + elg = L_shr(L_deposit_h(lgmean),1); /* Q26 */ + for (i = 0; i < LGPORDER; i++) { + elg = L_mac0(elg, lgp[i],lgpm[i]); /* Q26 */ + } + elg = L_shr(elg,1); + + /* Calculate decoded log-gain */ + *lgq = L_add(L_shr(L_deposit_h(lgpecb[gidx]), 2), elg); /* Q25 */ + + /* Look up from lgclimit() table the maximum log gain change allowed */ + i = shr(sub(shr(extract_h(L_sub(prevlg[0],level)),9),LGLB),1); + /* get column index */ + if (i >= NGB) { + i = NGB - 1; + } else if (i < 0) { + i = 0; + } + n = shr(sub(shr(extract_h(L_sub(prevlg[0],prevlg[1])),9),LGCLB),1); + /* get row index */ + if (n >= NGCB) { + n = NGCB - 1; + } else if (n < 0) { + n = 0; + } + + i = i * NGCB + n; + + /* Update log-gain predictor memory, + check whether decoded log-gain exceeds lgclimit */ + for (k = LGPORDER - 1; k > 0; k--) { + lgpm[k] = lgpm[k-1]; + } + lgc = extract_h(L_sub(*lgq, prevlg[0])); /* Q9 */ + if ((lgc>lgclimit[i]) && (gidx>0)) { /* if log-gain exceeds limit */ + *lgq = prevlg[0]; /* use the log-gain of previous frame */ + lgpm[0] = extract_h(L_shl(L_sub(*lgq, elg),2)); + *nggalgc = 0; + *lg_el = L_add(L_deposit_h(lgclimit[i]), prevlg[0]); + } else { + lgpm[0] = lgpecb[gidx]; + *nggalgc = add(*nggalgc, 1); + if (*nggalgc > Nfdm) *nggalgc = Nfdm+1; + *lg_el = *lgq; + } + + /* Update log-gain predictor memory */ + prevlg[1] = prevlg[0]; + prevlg[0] = *lgq; + + /* Convert quantized log-gain to linear domain */ + elg = L_shr(*lgq,10); /* Q25 -> Q26 (0.5F) --> Q16 */ + L_Extract(elg, &lg_exp, &lg_frac); + lg_exp = add(lg_exp, 18); /* output to be Q2 */ + return Pow2(lg_exp, lg_frac); +} + +void gainplc(Word32 E, Word16 *lgeqm, Word32 *lgqm) +{ + int k; + Word16 exponent, fraction, lge; + Word32 lg, mrlg, elg; + + exponent = 1; + fraction = 0; + if (E > TMinlg*FRSZ) + { + Log2( E, &exponent, &fraction); + lg = L_add(L_shl(L_deposit_h(exponent),9), + L_shr(L_deposit_h(fraction),6)); /* Q25 */ + lg = L_sub(lg, 178574274); /* 178574274 = log2(1/SFSZL) Q 25 */ + } + else + lg = 0; /* Minlg */ + + mrlg = L_shr(L_deposit_h(lgmean),2); /* Q25 */ + mrlg = L_sub(lg, mrlg); /* Q25 */ + + elg = 0; + for(k=0; k0; k--) + lgeqm[k] = lgeqm[k-1]; + lgeqm[0] = lge; + + /* update quantized log-gain memory */ + lgqm[1] = lgqm[0]; + lgqm[0] = lg; + + return; +} diff --git a/jni/bx16_fixedp/bv16/gainquan.c b/app/src/main/jni/bx16_fixedp/bv16/gainquan.c similarity index 97% rename from jni/bx16_fixedp/bv16/gainquan.c rename to app/src/main/jni/bx16_fixedp/bv16/gainquan.c index 986e5c7..532cabe 100644 --- a/jni/bx16_fixedp/bv16/gainquan.c +++ b/app/src/main/jni/bx16_fixedp/bv16/gainquan.c @@ -1,125 +1,125 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - gainquan.c : gain quantization based on inter-subframe - moving-average prediction of logarithmic gain. - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16externs.h" -#include "basop32.h" -#include "mathutil.h" - -Word16 gainquan( - Word32 *gainq, /* Q18 */ - Word32 *ee, /* Q1 */ - Word16 *lgpm, /* Q11 */ - Word32 *prevlg, - Word32 level) -{ - Word32 lg, elg, lgq, limit; - Word16 lg_exp, lg_frac, lgpe, d, dmin; - Word16 i, n, gidx=0, *p_gidx; - - /* Divide ee by hfrsz = 8*5 */ - if (*ee < 2*FRSZ) lg = 0; - else { - L_Extract(*ee, &lg_exp, &lg_frac); /* Q1 -> Q4 for divided by 8 */ - lg = Mpy_32_16(lg_exp, lg_frac, 6554); /* multiplied by 0.2 in Q15 */ - Log2(lg, &lg_exp, &lg_frac); /* Q4 treated as Q0 */ - lg_exp = sub(lg_exp, 4); /* compensated Q4 */ - lg = L_add(L_shl(L_deposit_h(lg_exp),9), - L_shr(L_deposit_h(lg_frac),6)); /* Q25 */ - } - - /* Calculate estimated log-gain */ - elg = L_shr(L_deposit_h(lgmean),1); /* Q26 */ - for (i = 0; i < LGPORDER; i++) { - elg = L_mac0(elg,lgp[i],lgpm[i]); /* Q26 */ - } - elg = L_shr(elg,1); /* Q25 */ - - /* Subtract log-gain mean & estimated log-gain to get prediction error */ - lgpe = intround(L_shl(L_sub(lg, elg),2)); /* Q11 */ - - /* Scalar quantization of log-gain prediction error */ - dmin = MAX_16; - p_gidx = idxord; - for (i = 0; i < LGPECBSZ; i++) { - d = abs_s(sub(lgpe, lgpecb[*p_gidx++])); - if (d < dmin) { - dmin = d; - gidx= i; - } - } - - /* Calculate quantized log-gain */ - lgq = L_add(L_shr(L_deposit_h(lgpecb[idxord[gidx]]), 2), elg); - /* Q25 */ - - /* Look up from lgclimit() table the maximum log gain change allowed */ - i = shr(sub(shr(extract_h(L_sub(prevlg[0],level)),9),LGLB),1); /* get column index */ - if (i >= NGB) { - i = NGB - 1; - } else if (i < 0) { - i = 0; - } - n = shr(sub(shr(extract_h(L_sub(prevlg[0],prevlg[1])),9),LGCLB),1); - /* get row index */ - if (n >= NGCB) { - n = NGCB - 1; - } else if (n < 0) { - n = 0; - } - i = i * NGCB + n; - - /* Check whether quantized log-gain cause a gain change > lgclimit */ - limit = L_add(prevlg[0],L_deposit_h(lgclimit[i])); /* limit log-gain */ - while ((lgq > limit) && (gidx > 0) ) { /* if q log-gain exceeds limit */ - gidx -= 1; /* decrement gain quantizer index by 1 */ - lgq = L_add(L_shr(L_deposit_h(lgpecb[idxord[gidx]]),2),elg); - } - - /* get true codebook index */ - gidx = idxord[gidx]; - - /* Update log-gain predictor memory */ - prevlg[1] = prevlg[0]; - prevlg[0] = lgq; - for (i = LGPORDER - 1; i > 0; i--) { - lgpm[i] = lgpm[i-1]; - } - lgpm[0] = lgpecb[gidx]; - - /* Convert quantized log-gain to linear domain */ - elg = L_shr(lgq,10); /* Q25 -> Q26 (0.5F) --> Q16 */ - L_Extract(elg, &lg_exp, &lg_frac); - lg_exp = add(lg_exp, 18); /* output to be Q2 */ - *gainq = Pow2(lg_exp, lg_frac); - - return gidx; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + gainquan.c : gain quantization based on inter-subframe + moving-average prediction of logarithmic gain. + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" +#include "mathutil.h" + +Word16 gainquan( + Word32 *gainq, /* Q18 */ + Word32 *ee, /* Q1 */ + Word16 *lgpm, /* Q11 */ + Word32 *prevlg, + Word32 level) +{ + Word32 lg, elg, lgq, limit; + Word16 lg_exp, lg_frac, lgpe, d, dmin; + Word16 i, n, gidx=0, *p_gidx; + + /* Divide ee by hfrsz = 8*5 */ + if (*ee < 2*FRSZ) lg = 0; + else { + L_Extract(*ee, &lg_exp, &lg_frac); /* Q1 -> Q4 for divided by 8 */ + lg = Mpy_32_16(lg_exp, lg_frac, 6554); /* multiplied by 0.2 in Q15 */ + Log2(lg, &lg_exp, &lg_frac); /* Q4 treated as Q0 */ + lg_exp = sub(lg_exp, 4); /* compensated Q4 */ + lg = L_add(L_shl(L_deposit_h(lg_exp),9), + L_shr(L_deposit_h(lg_frac),6)); /* Q25 */ + } + + /* Calculate estimated log-gain */ + elg = L_shr(L_deposit_h(lgmean),1); /* Q26 */ + for (i = 0; i < LGPORDER; i++) { + elg = L_mac0(elg,lgp[i],lgpm[i]); /* Q26 */ + } + elg = L_shr(elg,1); /* Q25 */ + + /* Subtract log-gain mean & estimated log-gain to get prediction error */ + lgpe = intround(L_shl(L_sub(lg, elg),2)); /* Q11 */ + + /* Scalar quantization of log-gain prediction error */ + dmin = MAX_16; + p_gidx = idxord; + for (i = 0; i < LGPECBSZ; i++) { + d = abs_s(sub(lgpe, lgpecb[*p_gidx++])); + if (d < dmin) { + dmin = d; + gidx= i; + } + } + + /* Calculate quantized log-gain */ + lgq = L_add(L_shr(L_deposit_h(lgpecb[idxord[gidx]]), 2), elg); + /* Q25 */ + + /* Look up from lgclimit() table the maximum log gain change allowed */ + i = shr(sub(shr(extract_h(L_sub(prevlg[0],level)),9),LGLB),1); /* get column index */ + if (i >= NGB) { + i = NGB - 1; + } else if (i < 0) { + i = 0; + } + n = shr(sub(shr(extract_h(L_sub(prevlg[0],prevlg[1])),9),LGCLB),1); + /* get row index */ + if (n >= NGCB) { + n = NGCB - 1; + } else if (n < 0) { + n = 0; + } + i = i * NGCB + n; + + /* Check whether quantized log-gain cause a gain change > lgclimit */ + limit = L_add(prevlg[0],L_deposit_h(lgclimit[i])); /* limit log-gain */ + while ((lgq > limit) && (gidx > 0) ) { /* if q log-gain exceeds limit */ + gidx -= 1; /* decrement gain quantizer index by 1 */ + lgq = L_add(L_shr(L_deposit_h(lgpecb[idxord[gidx]]),2),elg); + } + + /* get true codebook index */ + gidx = idxord[gidx]; + + /* Update log-gain predictor memory */ + prevlg[1] = prevlg[0]; + prevlg[0] = lgq; + for (i = LGPORDER - 1; i > 0; i--) { + lgpm[i] = lgpm[i-1]; + } + lgpm[0] = lgpecb[gidx]; + + /* Convert quantized log-gain to linear domain */ + elg = L_shr(lgq,10); /* Q25 -> Q26 (0.5F) --> Q16 */ + L_Extract(elg, &lg_exp, &lg_frac); + lg_exp = add(lg_exp, 18); /* output to be Q2 */ + *gainq = Pow2(lg_exp, lg_frac); + + return gidx; +} diff --git a/jni/bx16_fixedp/bv16/levelest.c b/app/src/main/jni/bx16_fixedp/bv16/levelest.c similarity index 97% rename from jni/bx16_fixedp/bv16/levelest.c rename to app/src/main/jni/bx16_fixedp/bv16/levelest.c index 1b0798a..e68ee14 100644 --- a/jni/bx16_fixedp/bv16/levelest.c +++ b/app/src/main/jni/bx16_fixedp/bv16/levelest.c @@ -1,116 +1,116 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - levelest.c : Input level estimation - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bv16cnst.h" -#include "basop32.h" - -Word32 estlevel( - Word32 lg, - Word32 *level, - Word32 *lmax, - Word32 *lmin, - Word32 *lmean, - Word32 *x1, - Word16 ngfae, - Word16 nggalgc, - Word16 *estl_alpha_min) -{ - Word32 lth; - Word32 a0; - Word16 s, t; - - /* Reset forgetting factor for Lmin to fast decay. This is to avoid Lmin - staying at an incorrect low level compensation for the possibility - it has caused incorrect bit-error declaration by making - the estimated level too low. */ - - if(nggalgc == 0) *estl_alpha_min = estl_alpha1; - - /* Reset forgetting factor for Lmin to regular decay if fast decay - has taken place for the past Nfdm frames. */ - - else if (nggalgc == Nfdm+1) *estl_alpha_min = estl_alpha; - - /* Update the new maximum, minimum, & mean of log-gain */ - if (lg > *lmax) *lmax=lg; /* use new log-gain as max if it is > max */ - else { /* o.w. attenuate toward lmean */ - - /* *lmax=*lmean+estl_alpha*(*lmax-*lmean); */ - a0 = L_sub(*lmax, *lmean); - L_Extract(a0, &s, &t); - a0 = Mpy_32_16(s, t, estl_alpha); - *lmax = L_add(a0, *lmean); - } - - if (lg < *lmin && ngfae == LGPORDER+1 && nggalgc > LGPORDER) { - *lmin = lg; /* use new log-gain as min if it is < min */ - *estl_alpha_min = estl_alpha; /* Reset forgetting factor for Lmin to - regular decay in case it has been on - fast decay since it has now found - a new minimum level. */ - } - else { /* o.w. attenuate toward lmean */ - - /* *lmin=*lmean+(*estl_alpha_min)*(*lmin-*lmean); */ - a0 = L_sub(*lmin, *lmean); - L_Extract(a0, &s, &t); - a0 = Mpy_32_16(s, t, (*estl_alpha_min)); - *lmin = L_add(a0, *lmean); - } - - /* *lmean=estl_beta*(*lmean)+estl_beta1*(0.5*(*lmax+*lmin)); */ - a0 = L_shr(L_add(*lmax, *lmin),1); - L_Extract(a0, &s, &t); - a0 = Mpy_32_16(s, t, estl_beta1); - L_Extract(*lmean, &s, &t); - *lmean = L_add(a0, Mpy_32_16(s, t, estl_beta)); - - /* update estimated input level, by calculating a running average - (using an exponential window) of log-gains exceeding lmean */ - /* lth=*lmean+estl_TH*(*lmax-*lmean); */ - - a0 = L_sub(*lmax, *lmean); - L_Extract(a0, &s, &t); - lth = L_add(*lmean, Mpy_32_16(s, t, estl_TH)); - - if (lg > lth) { - - /* *x1=estl_a*(*x1)+estl_a1*lg; */ - L_Extract(*x1, &s, &t); - a0 = Mpy_32_16(s, t, estl_a); - L_Extract(lg, &s, &t); - *x1 = L_add(a0, Mpy_32_16(s, t, estl_a1)); - - /* *level=estl_a*(*level)+estl_a1*(*x1); */ - L_Extract(*level, &s, &t); - a0 = Mpy_32_16(s, t, estl_a); - L_Extract(*x1, &s, &t); - *level = L_add(a0, Mpy_32_16(s, t, estl_a1)); - } - return lth; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + levelest.c : Input level estimation + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bv16cnst.h" +#include "basop32.h" + +Word32 estlevel( + Word32 lg, + Word32 *level, + Word32 *lmax, + Word32 *lmin, + Word32 *lmean, + Word32 *x1, + Word16 ngfae, + Word16 nggalgc, + Word16 *estl_alpha_min) +{ + Word32 lth; + Word32 a0; + Word16 s, t; + + /* Reset forgetting factor for Lmin to fast decay. This is to avoid Lmin + staying at an incorrect low level compensation for the possibility + it has caused incorrect bit-error declaration by making + the estimated level too low. */ + + if(nggalgc == 0) *estl_alpha_min = estl_alpha1; + + /* Reset forgetting factor for Lmin to regular decay if fast decay + has taken place for the past Nfdm frames. */ + + else if (nggalgc == Nfdm+1) *estl_alpha_min = estl_alpha; + + /* Update the new maximum, minimum, & mean of log-gain */ + if (lg > *lmax) *lmax=lg; /* use new log-gain as max if it is > max */ + else { /* o.w. attenuate toward lmean */ + + /* *lmax=*lmean+estl_alpha*(*lmax-*lmean); */ + a0 = L_sub(*lmax, *lmean); + L_Extract(a0, &s, &t); + a0 = Mpy_32_16(s, t, estl_alpha); + *lmax = L_add(a0, *lmean); + } + + if (lg < *lmin && ngfae == LGPORDER+1 && nggalgc > LGPORDER) { + *lmin = lg; /* use new log-gain as min if it is < min */ + *estl_alpha_min = estl_alpha; /* Reset forgetting factor for Lmin to + regular decay in case it has been on + fast decay since it has now found + a new minimum level. */ + } + else { /* o.w. attenuate toward lmean */ + + /* *lmin=*lmean+(*estl_alpha_min)*(*lmin-*lmean); */ + a0 = L_sub(*lmin, *lmean); + L_Extract(a0, &s, &t); + a0 = Mpy_32_16(s, t, (*estl_alpha_min)); + *lmin = L_add(a0, *lmean); + } + + /* *lmean=estl_beta*(*lmean)+estl_beta1*(0.5*(*lmax+*lmin)); */ + a0 = L_shr(L_add(*lmax, *lmin),1); + L_Extract(a0, &s, &t); + a0 = Mpy_32_16(s, t, estl_beta1); + L_Extract(*lmean, &s, &t); + *lmean = L_add(a0, Mpy_32_16(s, t, estl_beta)); + + /* update estimated input level, by calculating a running average + (using an exponential window) of log-gains exceeding lmean */ + /* lth=*lmean+estl_TH*(*lmax-*lmean); */ + + a0 = L_sub(*lmax, *lmean); + L_Extract(a0, &s, &t); + lth = L_add(*lmean, Mpy_32_16(s, t, estl_TH)); + + if (lg > lth) { + + /* *x1=estl_a*(*x1)+estl_a1*lg; */ + L_Extract(*x1, &s, &t); + a0 = Mpy_32_16(s, t, estl_a); + L_Extract(lg, &s, &t); + *x1 = L_add(a0, Mpy_32_16(s, t, estl_a1)); + + /* *level=estl_a*(*level)+estl_a1*(*x1); */ + L_Extract(*level, &s, &t); + a0 = Mpy_32_16(s, t, estl_a); + L_Extract(*x1, &s, &t); + *level = L_add(a0, Mpy_32_16(s, t, estl_a1)); + } + return lth; +} diff --git a/jni/bx16_fixedp/bv16/lspdec.c b/app/src/main/jni/bx16_fixedp/bv16/lspdec.c similarity index 97% rename from jni/bx16_fixedp/bv16/lspdec.c rename to app/src/main/jni/bx16_fixedp/bv16/lspdec.c index bf44a99..cdf43c6 100644 --- a/jni/bx16_fixedp/bv16/lspdec.c +++ b/app/src/main/jni/bx16_fixedp/bv16/lspdec.c @@ -1,153 +1,153 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - lspdec.c : LSP decoding function - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16externs.h" -#include "basop32.h" - -void lspdec( - Word16 *lspq, /* Q15 */ - Word16 *lspidx, - Word16 *lsppm, /* Q15 */ - Word16 *lspq_last) -{ - Word32 a0; - Word16 elsp[LPCO], lspe[LPCO]; - Word16 lspeq1[LPCO], lspeq2[LPCO]; - Word16 *fp1, *fp2; - Word16 i, k, sign, idx, stbl; - - /* Calculate estimated (ma-predicted) lsp vector */ - fp1 = lspp; /* Q14 */ - fp2 = lsppm; /* Q15 */ - for (i = 0; i < LPCO; i++) - { - a0 = 0; - for (k = 0; k < LSPPORDER; k++) - { - a0 = L_mac(a0, *fp1++, *fp2++); - } - elsp[i] = intround(L_shl(a0,1)); /* Q15 */ - } - - /* Perform first-stage vq codebook decode */ - vqdec(lspeq1,lspidx[0],lspecb1,LPCO); - - /* Perform second-stage vq codebook decode */ - idx = lspidx[1]; - if(lspidx[1] >= LSPECBSZ2) - { - sign = -1; - idx = sub((Word16)(2*LSPECBSZ2-1),idx); - } else sign = 1; - - vqdec(lspeq2,idx,lspecb2,LPCO); - - /* Get overall quantizer output vector of the two-stage vq */ - if (sign==1) - for (i = 0; i < LPCO; i++) - lspe[i] = shr(add(lspeq1[i],lspeq2[i]),2); - else - for (i = 0; i < LPCO; i++) - lspe[i] = shr(sub(lspeq1[i],lspeq2[i]),2); - - /* Calculate quantized lsp for stability check */ - for (i = 0; i < STBLDIM; i++) - { - lspq[i] = add(add(lspe[i],elsp[i]),lspmean[i]); - } - - /* detect bit-errors based on ordering property of lsp */ - stbl = stblchck(lspq, STBLDIM); - - /* replace LSP if bit-errors are detected */ - if(!stbl) { - for(i=0; i= 0; i--) { - for (k = LSPPORDER; k > 1; k--) { - *fp1-- = *fp2--; - } - *fp1-- = lspe[i]; - fp2--; - } - - /* Ensure correct ordering of lsp to guarantee lpc filter stability */ - stblz_lsp(lspq,LPCO); - -} - -void lspdecplc( - Word16 *lspq, /* Q15 */ - Word16 *lsppm) /* Q15 */ -{ - Word32 a0; - Word16 elsp[LPCO]; - Word16 *fp1, *fp2; - Word16 i, k; - - /* Calculate estimated (ma-predicted) lsp vector */ - fp1 = lspp; /* Q14 */ - fp2 = lsppm; /* Q15 */ - for (i = 0; i < LPCO; i++) { - a0 = 0; - for (k = 0; k < LSPPORDER; k++) { - a0 = L_mac(a0, *fp1++, *fp2++); - } - elsp[i] = intround(L_shl(a0,1)); /* Q15 */ - } - - /* Update lsp ma predictor memory */ - i = LPCO * LSPPORDER - 1; - fp1 = &lsppm[i]; - fp2 = &lsppm[i - 1]; - for (i = LPCO - 1; i >= 0; i--) { - for (k = LSPPORDER; k > 1; k--) { - *fp1-- = *fp2--; - } - *fp1-- = sub(sub(lspq[i],lspmean[i]),elsp[i]); - fp2--; - } - -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + lspdec.c : LSP decoding function + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" + +void lspdec( + Word16 *lspq, /* Q15 */ + Word16 *lspidx, + Word16 *lsppm, /* Q15 */ + Word16 *lspq_last) +{ + Word32 a0; + Word16 elsp[LPCO], lspe[LPCO]; + Word16 lspeq1[LPCO], lspeq2[LPCO]; + Word16 *fp1, *fp2; + Word16 i, k, sign, idx, stbl; + + /* Calculate estimated (ma-predicted) lsp vector */ + fp1 = lspp; /* Q14 */ + fp2 = lsppm; /* Q15 */ + for (i = 0; i < LPCO; i++) + { + a0 = 0; + for (k = 0; k < LSPPORDER; k++) + { + a0 = L_mac(a0, *fp1++, *fp2++); + } + elsp[i] = intround(L_shl(a0,1)); /* Q15 */ + } + + /* Perform first-stage vq codebook decode */ + vqdec(lspeq1,lspidx[0],lspecb1,LPCO); + + /* Perform second-stage vq codebook decode */ + idx = lspidx[1]; + if(lspidx[1] >= LSPECBSZ2) + { + sign = -1; + idx = sub((Word16)(2*LSPECBSZ2-1),idx); + } else sign = 1; + + vqdec(lspeq2,idx,lspecb2,LPCO); + + /* Get overall quantizer output vector of the two-stage vq */ + if (sign==1) + for (i = 0; i < LPCO; i++) + lspe[i] = shr(add(lspeq1[i],lspeq2[i]),2); + else + for (i = 0; i < LPCO; i++) + lspe[i] = shr(sub(lspeq1[i],lspeq2[i]),2); + + /* Calculate quantized lsp for stability check */ + for (i = 0; i < STBLDIM; i++) + { + lspq[i] = add(add(lspe[i],elsp[i]),lspmean[i]); + } + + /* detect bit-errors based on ordering property of lsp */ + stbl = stblchck(lspq, STBLDIM); + + /* replace LSP if bit-errors are detected */ + if(!stbl) { + for(i=0; i= 0; i--) { + for (k = LSPPORDER; k > 1; k--) { + *fp1-- = *fp2--; + } + *fp1-- = lspe[i]; + fp2--; + } + + /* Ensure correct ordering of lsp to guarantee lpc filter stability */ + stblz_lsp(lspq,LPCO); + +} + +void lspdecplc( + Word16 *lspq, /* Q15 */ + Word16 *lsppm) /* Q15 */ +{ + Word32 a0; + Word16 elsp[LPCO]; + Word16 *fp1, *fp2; + Word16 i, k; + + /* Calculate estimated (ma-predicted) lsp vector */ + fp1 = lspp; /* Q14 */ + fp2 = lsppm; /* Q15 */ + for (i = 0; i < LPCO; i++) { + a0 = 0; + for (k = 0; k < LSPPORDER; k++) { + a0 = L_mac(a0, *fp1++, *fp2++); + } + elsp[i] = intround(L_shl(a0,1)); /* Q15 */ + } + + /* Update lsp ma predictor memory */ + i = LPCO * LSPPORDER - 1; + fp1 = &lsppm[i]; + fp2 = &lsppm[i - 1]; + for (i = LPCO - 1; i >= 0; i--) { + for (k = LSPPORDER; k > 1; k--) { + *fp1-- = *fp2--; + } + *fp1-- = sub(sub(lspq[i],lspmean[i]),elsp[i]); + fp2--; + } + +} diff --git a/jni/bx16_fixedp/bv16/lspquan.c b/app/src/main/jni/bx16_fixedp/bv16/lspquan.c similarity index 96% rename from jni/bx16_fixedp/bv16/lspquan.c rename to app/src/main/jni/bx16_fixedp/bv16/lspquan.c index 82c716e..a44e1c4 100644 --- a/jni/bx16_fixedp/bv16/lspquan.c +++ b/app/src/main/jni/bx16_fixedp/bv16/lspquan.c @@ -1,272 +1,272 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - lspquan.c : Lsp quantization based on inter-frame moving-average - prediction and two-stage VQ. - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16externs.h" -#include "basop32.h" - -void vqmse( - Word16 *xq, - Word16 *idx, - Word16 *x, - Word16 *cb, - int vdim, - int cbsz); - -void svqwmse( - Word16 *xq, - Word16 *idx, - Word16 *x, - Word16 *xa, - Word16 *w, - Word16 *cb, - int vdim, - int cbsz); - -void lspquan( - Word16 *lspq, /* Q15 */ - Word16 *lspidx, - Word16 *lsp, /* Q15 */ - Word16 *lsppm) /* Q15 */ -{ - Word32 a0; - Word16 d[LPCO], w[LPCO]; - Word16 elsp[LPCO], lspe[LPCO], lspa[LPCO]; - Word16 lspeq1[LPCO], lspeq2[LPCO]; - Word16 *fp1, *fp2, min_d; - int i, k; - - /* Calculate the weights for weighted mean-square error distortion */ - min_d = MAX_16; - for (i = 0; i < LPCO - 1 ; i++) - { - d[i] = sub(lsp[i+1],lsp[i]); /* LSP difference vector */ - if (d[i] < min_d) - min_d = d[i]; - } - - w[0] = shr(div_s(min_d, d[0]),1); - for (i = 1; i < LPCO - 1 ; i++) - { - if (d[i] < d[i-1]) - w[i] = shr(div_s(min_d, d[i]),1); - else - w[i] = shr(div_s(min_d, d[i-1]),1); - } - w[LPCO-1] = shr(div_s(min_d, d[LPCO-2]),1); - - /* Calculate estimated (ma-predicted) lsp vector */ - fp1 = lspp; /* Q14 */ - fp2 = lsppm; /* Q15 */ - for (i = 0; i < LPCO; i++) - { - a0 = 0; - for (k = 0; k < LSPPORDER; k++) - { - a0 = L_mac(a0, *fp1++, *fp2++); - } - elsp[i] = intround(L_shl(a0,1)); /* Q15 */ - } - - /* Subtract lsp mean value & estimated lsp to get prediction error */ - for (i = 0; i < LPCO; i++) - { - lspe[i] = shl(sub(sub(lsp[i],lspmean[i]),elsp[i]),2); /* Q17 */ - } - - /* Perform first-stage vq codebook search */ - vqmse(lspeq1,&lspidx[0],lspe,lspecb1, LPCO,LSPECBSZ1); - - /* Calculate quantization error vector of first-stage vq */ - for (i = 0; i < LPCO; i++) { - d[i] = sub(lspe[i],lspeq1[i]); /* Q17 */ - } - - /* Perform second-stage vq codebook search */ - for (i = 0; i < LPCO; i++) - lspa[i] = add(add(lspmean[i],elsp[i]),shr(lspeq1[i],2)); /* Q15 */ - - svqwmse(lspeq2,&lspidx[1],d,lspa,w,lspecb2,LPCO,LSPECBSZ2); - - /* Get overall quantizer output vector of the two-stage vq */ - for (i = 0; i < LPCO; i++) { - lspe[i] = shr(add(lspeq1[i],lspeq2[i]),2); - } - - /* Update lsp ma predictor memory */ - i = LPCO * LSPPORDER - 1; - fp1 = &lsppm[i]; - fp2 = &lsppm[i - 1]; - for (i = LPCO - 1; i >= 0; i--) { - for (k = LSPPORDER; k > 1; k--) { - *fp1-- = *fp2--; - } - *fp1-- = lspe[i]; - fp2--; - } - - /* Calculate quantized lsp */ - for (i = 0; i < LPCO; i++) { - lspq[i] = add(add(lspmean[i],elsp[i]), lspe[i]); - } - - /* Ensure correct ordering of lsp to guarantee lpc filter stability */ - stblz_lsp(lspq,LPCO); -} - -/*==========================================================================*/ - -void vqmse( - Word16 *xq, /* Q17 VQ output vector (quantized version of input vector) */ - Word16 *idx, /* VQ codebook index for the nearest neighbor */ - Word16 *x, /* Q17 input vector */ - Word16 *cb, /* VQ codebook */ - int vdim, /* vector dimension */ - int cbsz) /* codebook size (number of codevectors) */ -{ - Word32 dmin, d; - Word16 *fp1; - Word16 j, k; - - Word16 e; - - fp1 = cb; - dmin = MAX_32; - for (j = 0; j < cbsz; j++) { - d = 0; - for (k = 0; k < vdim; k++) { - e = sub(x[k], *fp1++); // Q17 - d = L_mac0(d, e, e); // Q34 - } - if (L_sub(d, dmin) < 0) { - dmin = d; - *idx = j; - } - } - - j = *idx * vdim; - for (k = 0; k < vdim; k++) { - xq[k] = cb[j + k]; - } -} - - -/* Signed WMSE VQ */ -void svqwmse( - Word16 *xq, /* Q17 */ - Word16 *idx, - Word16 *x, /* Q17 */ - Word16 *xa, /* Q15 */ - Word16 *w, /* Q15 */ - Word16 *cb, /* Q17 */ - int vdim, - int cbsz) -{ - Word32 dmin, d; - Word16 *fp1, *fp2; - Word16 xqc[STBLDIM]; - Word16 j, k, stbl, sign=1; - Word16 e, we; - - fp1 = cb; - dmin = MAX_32; - *idx = -1; - - for (j = 0; j < cbsz; j++) { - - /* Try negative sign */ - d = 0; - fp2 = fp1; - - for(k=0; k 0){ - dmin = d; - *idx = j; - sign = -1; - } - } - - /* Try positive sign */ - fp1 -= vdim; - d = 0; - fp2 = fp1; - - for(k=0; k 0){ - dmin = d; - *idx = j; - sign = +1; - } - } - } - - if(*idx == -1){ - *idx = 0; - sign = 1; - } - - fp1 = cb + (*idx)*vdim; - - for (k = 0; k < vdim; k++) - xq[k] = *fp1++; - - if (sign==-1) for (k = 0; k < vdim; k++) xq[k] = negate(xq[k]); - - if(sign < 0) *idx = sub((Word16)(2*cbsz-1),(*idx)); -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + lspquan.c : Lsp quantization based on inter-frame moving-average + prediction and two-stage VQ. + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" + +void vqmse( + Word16 *xq, + Word16 *idx, + Word16 *x, + Word16 *cb, + int vdim, + int cbsz); + +void svqwmse( + Word16 *xq, + Word16 *idx, + Word16 *x, + Word16 *xa, + Word16 *w, + Word16 *cb, + int vdim, + int cbsz); + +void lspquan( + Word16 *lspq, /* Q15 */ + Word16 *lspidx, + Word16 *lsp, /* Q15 */ + Word16 *lsppm) /* Q15 */ +{ + Word32 a0; + Word16 d[LPCO], w[LPCO]; + Word16 elsp[LPCO], lspe[LPCO], lspa[LPCO]; + Word16 lspeq1[LPCO], lspeq2[LPCO]; + Word16 *fp1, *fp2, min_d; + int i, k; + + /* Calculate the weights for weighted mean-square error distortion */ + min_d = MAX_16; + for (i = 0; i < LPCO - 1 ; i++) + { + d[i] = sub(lsp[i+1],lsp[i]); /* LSP difference vector */ + if (d[i] < min_d) + min_d = d[i]; + } + + w[0] = shr(div_s(min_d, d[0]),1); + for (i = 1; i < LPCO - 1 ; i++) + { + if (d[i] < d[i-1]) + w[i] = shr(div_s(min_d, d[i]),1); + else + w[i] = shr(div_s(min_d, d[i-1]),1); + } + w[LPCO-1] = shr(div_s(min_d, d[LPCO-2]),1); + + /* Calculate estimated (ma-predicted) lsp vector */ + fp1 = lspp; /* Q14 */ + fp2 = lsppm; /* Q15 */ + for (i = 0; i < LPCO; i++) + { + a0 = 0; + for (k = 0; k < LSPPORDER; k++) + { + a0 = L_mac(a0, *fp1++, *fp2++); + } + elsp[i] = intround(L_shl(a0,1)); /* Q15 */ + } + + /* Subtract lsp mean value & estimated lsp to get prediction error */ + for (i = 0; i < LPCO; i++) + { + lspe[i] = shl(sub(sub(lsp[i],lspmean[i]),elsp[i]),2); /* Q17 */ + } + + /* Perform first-stage vq codebook search */ + vqmse(lspeq1,&lspidx[0],lspe,lspecb1, LPCO,LSPECBSZ1); + + /* Calculate quantization error vector of first-stage vq */ + for (i = 0; i < LPCO; i++) { + d[i] = sub(lspe[i],lspeq1[i]); /* Q17 */ + } + + /* Perform second-stage vq codebook search */ + for (i = 0; i < LPCO; i++) + lspa[i] = add(add(lspmean[i],elsp[i]),shr(lspeq1[i],2)); /* Q15 */ + + svqwmse(lspeq2,&lspidx[1],d,lspa,w,lspecb2,LPCO,LSPECBSZ2); + + /* Get overall quantizer output vector of the two-stage vq */ + for (i = 0; i < LPCO; i++) { + lspe[i] = shr(add(lspeq1[i],lspeq2[i]),2); + } + + /* Update lsp ma predictor memory */ + i = LPCO * LSPPORDER - 1; + fp1 = &lsppm[i]; + fp2 = &lsppm[i - 1]; + for (i = LPCO - 1; i >= 0; i--) { + for (k = LSPPORDER; k > 1; k--) { + *fp1-- = *fp2--; + } + *fp1-- = lspe[i]; + fp2--; + } + + /* Calculate quantized lsp */ + for (i = 0; i < LPCO; i++) { + lspq[i] = add(add(lspmean[i],elsp[i]), lspe[i]); + } + + /* Ensure correct ordering of lsp to guarantee lpc filter stability */ + stblz_lsp(lspq,LPCO); +} + +/*==========================================================================*/ + +void vqmse( + Word16 *xq, /* Q17 VQ output vector (quantized version of input vector) */ + Word16 *idx, /* VQ codebook index for the nearest neighbor */ + Word16 *x, /* Q17 input vector */ + Word16 *cb, /* VQ codebook */ + int vdim, /* vector dimension */ + int cbsz) /* codebook size (number of codevectors) */ +{ + Word32 dmin, d; + Word16 *fp1; + Word16 j, k; + + Word16 e; + + fp1 = cb; + dmin = MAX_32; + for (j = 0; j < cbsz; j++) { + d = 0; + for (k = 0; k < vdim; k++) { + e = sub(x[k], *fp1++); // Q17 + d = L_mac0(d, e, e); // Q34 + } + if (L_sub(d, dmin) < 0) { + dmin = d; + *idx = j; + } + } + + j = *idx * vdim; + for (k = 0; k < vdim; k++) { + xq[k] = cb[j + k]; + } +} + + +/* Signed WMSE VQ */ +void svqwmse( + Word16 *xq, /* Q17 */ + Word16 *idx, + Word16 *x, /* Q17 */ + Word16 *xa, /* Q15 */ + Word16 *w, /* Q15 */ + Word16 *cb, /* Q17 */ + int vdim, + int cbsz) +{ + Word32 dmin, d; + Word16 *fp1, *fp2; + Word16 xqc[STBLDIM]; + Word16 j, k, stbl, sign=1; + Word16 e, we; + + fp1 = cb; + dmin = MAX_32; + *idx = -1; + + for (j = 0; j < cbsz; j++) { + + /* Try negative sign */ + d = 0; + fp2 = fp1; + + for(k=0; k 0){ + dmin = d; + *idx = j; + sign = -1; + } + } + + /* Try positive sign */ + fp1 -= vdim; + d = 0; + fp2 = fp1; + + for(k=0; k 0){ + dmin = d; + *idx = j; + sign = +1; + } + } + } + + if(*idx == -1){ + *idx = 0; + sign = 1; + } + + fp1 = cb + (*idx)*vdim; + + for (k = 0; k < vdim; k++) + xq[k] = *fp1++; + + if (sign==-1) for (k = 0; k < vdim; k++) xq[k] = negate(xq[k]); + + if(sign < 0) *idx = sub((Word16)(2*cbsz-1),(*idx)); +} diff --git a/jni/bx16_fixedp/bv16/plc.c b/app/src/main/jni/bx16_fixedp/bv16/plc.c similarity index 97% rename from jni/bx16_fixedp/bv16/plc.c rename to app/src/main/jni/bx16_fixedp/bv16/plc.c index 74548d7..4cdf6c6 100644 --- a/jni/bx16_fixedp/bv16/plc.c +++ b/app/src/main/jni/bx16_fixedp/bv16/plc.c @@ -1,180 +1,180 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - plc.c : Packet Loss Concealment - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "utility.h" -#include "bv16strct.h" -#include "basop32.h" -#include "mathutil.h" -#include "bv16externs.h" -#include "postfilt.h" - - -void BV16_PLC( - struct BV16_Decoder_State *ds, - Word16 *out) -{ - int n; - Word16 *sq; - Word32 E; - Word16 tmp; - Word16 scplcg; /* Q14 */ - Word32 acc; - Word16 dsEw, dsEexp, Eexp, Ew; - Word16 gain, gainexp; - Word16 hi, lo; - Word16 xq[LXQ]; - Word16 d[LTMOFF+FRSZ]; - Word16 r[FRSZ]; - - /************************************************************/ - /* Copy decoder state memory */ - /************************************************************/ - W16copy(d, ds->ltsym, LTMOFF); - W16copy(xq, ds->xq, XQOFF); - - sq = xq+XQOFF; - /************************************************************/ - /* Update counter of consecutive list frames */ - /************************************************************/ - if(ds->cfecount < HoldPLCG+AttnPLCG-1) - ds->cfecount = add(ds->cfecount, 1); - ds->ngfae = 0; - - /************************************************************/ - /* Generate Unscaled Excitation */ - /************************************************************/ - - E = 0; - for(n=0; nidum = 1664525L*ds->idum + 1013904223L; - r[n] = extract_l(L_sub(L_shr(ds->idum, 16), 32768)); - tmp = shr(r[n], 3); - E = L_mac0(E, tmp, tmp); - } - - /************************************************************/ - /* Calculate Scaling */ - /************************************************************/ - scplcg = add(ScPLCG_a, mult(ScPLCG_b, ds->per)); - if(scplcg > ScPLCGmax) - scplcg = ScPLCGmax; - else if(scplcg < ScPLCGmin) - scplcg = ScPLCGmin; - scplcg = shl(scplcg, 1 ); /* Q14->Q15 */ - - - dsEexp = norm_l(ds->E); - dsEexp = sub(dsEexp, 1); - dsEw = extract_h(L_shl(ds->E, dsEexp)); - Eexp = norm_l(E); - Ew = extract_h(L_shl(E, Eexp)); - Eexp = sub(Eexp, 6); - gain = div_s(dsEw, Ew); - gainexp= add(sub(dsEexp, Eexp), 15); - if ((gainexp&1)==0) /* make sure it is odd before sqrt() */ - { - gain = shr(gain, 1); - gainexp = sub(gainexp, 1); - } - gain = sqrts(gain); - gainexp = add(shr(sub(gainexp,15),1), 15); - gain = mult(gain, scplcg); - - /************************************************************/ - /* Long-term synthesis filter */ - /************************************************************/ - gainexp = sub(gainexp, 15); - for(n=0; nbq_last[0], d[LTMOFF+n-ds->pp_last+1]); - acc = L_mac(acc, ds->bq_last[1], d[LTMOFF+n-ds->pp_last]); - acc = L_mac(acc, ds->bq_last[2], d[LTMOFF+n-ds->pp_last-1]); - d[LTMOFF+n] = intround(acc); - } - - /************************************************************/ - /* Short-term synthesis filter */ - /************************************************************/ - apfilter(ds->atplc, LPCO, d+LTMOFF,sq, FRSZ, ds->stsym, 1); - - /************************************************************/ - /* Save decoder state memory */ - /************************************************************/ - W16copy(ds->ltsym, d+FRSZ, LTMOFF); /* excitation */ - - /************************************************************/ - /* Update memory of predictive LSP quantizer */ - /************************************************************/ - lspdecplc(ds->lsplast,ds->lsppm); - - /************************************************************/ - /* Update memory of predictive gain quantizer */ - /************************************************************/ - E = L_shr(ds->E, 2); - E = L_add(E, L_shl(ds->E, 1)); /* E = 2.25*ds->E; */ - gainplc(E, ds->lgpm, ds->prevlg); - - /************************************************************/ - /* Signal level estimation */ - /************************************************************/ - estlevel(ds->prevlg[0],&ds->level,&ds->lmax,&ds->lmin,&ds->lmean,&ds->x1, - ds->ngfae, ds->nggalgc,&ds->estl_alpha_min); - - /************************************************************/ - /* Attenuation during long packet losses */ - /************************************************************/ - if(ds->cfecount >= HoldPLCG) - { - acc = L_mult0( -AttnFacPLCG, sub(ds->cfecount, HoldPLCG-1)); - acc = L_add(1l<<20, acc); - gain = intround(L_shl(acc, 11)); - ds->bq_last[0] = mult(gain, ds->bq_last[0]); - ds->bq_last[1] = mult(gain, ds->bq_last[1]); - ds->bq_last[2] = mult(gain, ds->bq_last[2]); - gain = mult(gain, gain); - L_Extract(ds->E, &hi, &lo); - ds->E = Mpy_32_16(hi, lo, gain); - } - - /************************************************************/ - /* Postfiltering */ - /************************************************************/ - postfilter(xq, ds->pp_last, &(ds->ma_a), ds->b_prv, &(ds->pp_prv), out); - /* scale signal up by 1.5 */ - - for(n=0; nxq, xq+FRSZ, XQOFF); - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + plc.c : Packet Loss Concealment + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "utility.h" +#include "bv16strct.h" +#include "basop32.h" +#include "mathutil.h" +#include "bv16externs.h" +#include "postfilt.h" + + +void BV16_PLC( + struct BV16_Decoder_State *ds, + Word16 *out) +{ + int n; + Word16 *sq; + Word32 E; + Word16 tmp; + Word16 scplcg; /* Q14 */ + Word32 acc; + Word16 dsEw, dsEexp, Eexp, Ew; + Word16 gain, gainexp; + Word16 hi, lo; + Word16 xq[LXQ]; + Word16 d[LTMOFF+FRSZ]; + Word16 r[FRSZ]; + + /************************************************************/ + /* Copy decoder state memory */ + /************************************************************/ + W16copy(d, ds->ltsym, LTMOFF); + W16copy(xq, ds->xq, XQOFF); + + sq = xq+XQOFF; + /************************************************************/ + /* Update counter of consecutive list frames */ + /************************************************************/ + if(ds->cfecount < HoldPLCG+AttnPLCG-1) + ds->cfecount = add(ds->cfecount, 1); + ds->ngfae = 0; + + /************************************************************/ + /* Generate Unscaled Excitation */ + /************************************************************/ + + E = 0; + for(n=0; nidum = 1664525L*ds->idum + 1013904223L; + r[n] = extract_l(L_sub(L_shr(ds->idum, 16), 32768)); + tmp = shr(r[n], 3); + E = L_mac0(E, tmp, tmp); + } + + /************************************************************/ + /* Calculate Scaling */ + /************************************************************/ + scplcg = add(ScPLCG_a, mult(ScPLCG_b, ds->per)); + if(scplcg > ScPLCGmax) + scplcg = ScPLCGmax; + else if(scplcg < ScPLCGmin) + scplcg = ScPLCGmin; + scplcg = shl(scplcg, 1 ); /* Q14->Q15 */ + + + dsEexp = norm_l(ds->E); + dsEexp = sub(dsEexp, 1); + dsEw = extract_h(L_shl(ds->E, dsEexp)); + Eexp = norm_l(E); + Ew = extract_h(L_shl(E, Eexp)); + Eexp = sub(Eexp, 6); + gain = div_s(dsEw, Ew); + gainexp= add(sub(dsEexp, Eexp), 15); + if ((gainexp&1)==0) /* make sure it is odd before sqrt() */ + { + gain = shr(gain, 1); + gainexp = sub(gainexp, 1); + } + gain = sqrts(gain); + gainexp = add(shr(sub(gainexp,15),1), 15); + gain = mult(gain, scplcg); + + /************************************************************/ + /* Long-term synthesis filter */ + /************************************************************/ + gainexp = sub(gainexp, 15); + for(n=0; nbq_last[0], d[LTMOFF+n-ds->pp_last+1]); + acc = L_mac(acc, ds->bq_last[1], d[LTMOFF+n-ds->pp_last]); + acc = L_mac(acc, ds->bq_last[2], d[LTMOFF+n-ds->pp_last-1]); + d[LTMOFF+n] = intround(acc); + } + + /************************************************************/ + /* Short-term synthesis filter */ + /************************************************************/ + apfilter(ds->atplc, LPCO, d+LTMOFF,sq, FRSZ, ds->stsym, 1); + + /************************************************************/ + /* Save decoder state memory */ + /************************************************************/ + W16copy(ds->ltsym, d+FRSZ, LTMOFF); /* excitation */ + + /************************************************************/ + /* Update memory of predictive LSP quantizer */ + /************************************************************/ + lspdecplc(ds->lsplast,ds->lsppm); + + /************************************************************/ + /* Update memory of predictive gain quantizer */ + /************************************************************/ + E = L_shr(ds->E, 2); + E = L_add(E, L_shl(ds->E, 1)); /* E = 2.25*ds->E; */ + gainplc(E, ds->lgpm, ds->prevlg); + + /************************************************************/ + /* Signal level estimation */ + /************************************************************/ + estlevel(ds->prevlg[0],&ds->level,&ds->lmax,&ds->lmin,&ds->lmean,&ds->x1, + ds->ngfae, ds->nggalgc,&ds->estl_alpha_min); + + /************************************************************/ + /* Attenuation during long packet losses */ + /************************************************************/ + if(ds->cfecount >= HoldPLCG) + { + acc = L_mult0( -AttnFacPLCG, sub(ds->cfecount, HoldPLCG-1)); + acc = L_add(1l<<20, acc); + gain = intround(L_shl(acc, 11)); + ds->bq_last[0] = mult(gain, ds->bq_last[0]); + ds->bq_last[1] = mult(gain, ds->bq_last[1]); + ds->bq_last[2] = mult(gain, ds->bq_last[2]); + gain = mult(gain, gain); + L_Extract(ds->E, &hi, &lo); + ds->E = Mpy_32_16(hi, lo, gain); + } + + /************************************************************/ + /* Postfiltering */ + /************************************************************/ + postfilter(xq, ds->pp_last, &(ds->ma_a), ds->b_prv, &(ds->pp_prv), out); + /* scale signal up by 1.5 */ + + for(n=0; nxq, xq+FRSZ, XQOFF); + + return; +} diff --git a/jni/bx16_fixedp/bv16/postfilt.c b/app/src/main/jni/bx16_fixedp/bv16/postfilt.c similarity index 96% rename from jni/bx16_fixedp/bv16/postfilt.c rename to app/src/main/jni/bx16_fixedp/bv16/postfilt.c index 6049039..2ebcbe5 100644 --- a/jni/bx16_fixedp/bv16/postfilt.c +++ b/app/src/main/jni/bx16_fixedp/bv16/postfilt.c @@ -1,247 +1,247 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - postfilt.c : Pitch postfilter. - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "basop32.h" -#include "mathutil.h" - -/* Standard Long-Term Postfilter */ -void postfilter( - Word16 *s, /* input : quantized speech signal */ - Word16 pp, /* input : pitch period */ - Word16 *ma_a, - Word16 *b_prv, - Word16 *pp_prv, - Word16 *e) /* output: enhanced speech signal */ -{ - int n; - Word16 len, t0, t1, t2, t3, shift, aa, R0norm, R0_exp; - Word32 a0, a1, R0, R1, R01, R01max, Rx; - Word16 *fp1; - Word16 ppt, pptmin, pptmax, ppnew; - Word16 bb[2]; - Word16 R1max_exp, R1max, R01Sqmax_exp, R01Sqmax, R01Sq_exp, R01Sq, R1_exp, R1n; - Word16 gainn, Rx_exp; - Word16 buf[MAXPP+FRSZ]; - Word16 *ps, ww1, ww2; - Word32 step, delta; - Word16 bi0, bi1c, bi1p; - - ps = s+XQOFF; - - /********************************************************************/ - /* pitch search around decoded pitch */ - /********************************************************************/ - pptmin = sub(pp, DPPQNS); - pptmax = add(pp, DPPQNS); - if (pptminMAXPP) - { - pptmax = MAXPP; - pptmin = sub(pptmax, 2*DPPQNS); - } - - fp1 = &s[XQOFF-pptmax]; - len = add(FRSZ, pptmax); - a0 = 0; - for (n=0;n 0) - { - ps = buf+pptmax; - fp1 = &s[XQOFF-pptmax]; - shift = shr(add(shift, 1), 1); - for (n=0;n=0) a0 = L_shr(a0, t2); - if (t2<0) a1 = L_shl(a1, t2); - - if (L_sub(a0, a1)>0) - { - R01Sqmax = R01Sq; - R01Sqmax_exp = R01Sq_exp; - R1max = R1n; R1max_exp = R1_exp; - ppnew = ppt; - R01max = R01; - } - } - - /******************************************************************/ - /* calculate all-zero pitch postfilter */ - /******************************************************************/ - if (R1max==0 || R0==0 || R01max <= 0) - { - aa = 0; - } - else - { - a0 = R1max_exp-16; - t1 = mult(R1max, R0norm); - a0 = a0+R0_exp-15; - sqrt_i(t1, (Word16)a0, &t1, &t2); - t0 = norm_l(R01max); - t3 = extract_h(L_shl(R01max, t0)); - t0 = t0-16; - aa = mult(t3, t1); - t0 = t0+t2-15; - t0 = t0-15; - if (t0<0) aa = shl(aa, sub(0,t0)); - else aa = shr(aa, t0); - } - a0 = L_mult(8192, aa); - a0 = L_mac(a0, 24576, *ma_a); - *ma_a = intround(a0); - if((*ma_a < ATHLD1) && (aa < (ATHLD2))) - aa = 0; - bb[1] = mult(ScLTPF, aa); - - /******************************************************************/ - /* calculate normalization energies */ - /******************************************************************/ - Rx = 0; - R0 = 0; - for(n=0; n= t1) - gainn = 32767; - else - { - t1 = div_s(t2, t1); - gainn = sqrts(t1); - } - } - - /******************************************************************/ - /* interpolate from the previous postfilter to the current */ - /******************************************************************/ - bb[0] = gainn; - bb[1] = mult(gainn, bb[1]); - step = (Word32)((1.0/(NINT+1))*(2147483648.0)); - delta = 0; - for(n=0; nMAXPP) + { + pptmax = MAXPP; + pptmin = sub(pptmax, 2*DPPQNS); + } + + fp1 = &s[XQOFF-pptmax]; + len = add(FRSZ, pptmax); + a0 = 0; + for (n=0;n 0) + { + ps = buf+pptmax; + fp1 = &s[XQOFF-pptmax]; + shift = shr(add(shift, 1), 1); + for (n=0;n=0) a0 = L_shr(a0, t2); + if (t2<0) a1 = L_shl(a1, t2); + + if (L_sub(a0, a1)>0) + { + R01Sqmax = R01Sq; + R01Sqmax_exp = R01Sq_exp; + R1max = R1n; R1max_exp = R1_exp; + ppnew = ppt; + R01max = R01; + } + } + + /******************************************************************/ + /* calculate all-zero pitch postfilter */ + /******************************************************************/ + if (R1max==0 || R0==0 || R01max <= 0) + { + aa = 0; + } + else + { + a0 = R1max_exp-16; + t1 = mult(R1max, R0norm); + a0 = a0+R0_exp-15; + sqrt_i(t1, (Word16)a0, &t1, &t2); + t0 = norm_l(R01max); + t3 = extract_h(L_shl(R01max, t0)); + t0 = t0-16; + aa = mult(t3, t1); + t0 = t0+t2-15; + t0 = t0-15; + if (t0<0) aa = shl(aa, sub(0,t0)); + else aa = shr(aa, t0); + } + a0 = L_mult(8192, aa); + a0 = L_mac(a0, 24576, *ma_a); + *ma_a = intround(a0); + if((*ma_a < ATHLD1) && (aa < (ATHLD2))) + aa = 0; + bb[1] = mult(ScLTPF, aa); + + /******************************************************************/ + /* calculate normalization energies */ + /******************************************************************/ + Rx = 0; + R0 = 0; + for(n=0; n= t1) + gainn = 32767; + else + { + t1 = div_s(t2, t1); + gainn = sqrts(t1); + } + } + + /******************************************************************/ + /* interpolate from the previous postfilter to the current */ + /******************************************************************/ + bb[0] = gainn; + bb[1] = mult(gainn, bb[1]); + step = (Word32)((1.0/(NINT+1))*(2147483648.0)); + delta = 0; + for(n=0; nhpfpm[0], cs->hpfpm[1], hpfa[1]); // Q14 - a0 = L_add(a0, Mpy_32_16(cs->hpfpm[2], cs->hpfpm[3], hpfa[2])); // Q14 - - /* zero section of filtering */ - a0 = L_mac0(a0, input[n], hpfb[0]); // Q14 - a0 = L_mac0(a0, cs->hpfzm[0], hpfb[1]); // Q14 - a0 = L_mac0(a0, cs->hpfzm[1], hpfb[2]); // Q14 - a0 = L_shl(a0, 1); // Q15 - - /* update pole section of memory */ - cs->hpfpm[2] = cs->hpfpm[0]; - cs->hpfpm[3] = cs->hpfpm[1]; - L_Extract(a0, cs->hpfpm, cs->hpfpm+1); - - /* get output in Q0 (less down-scaling by factor 1.5) */ - a0 = L_shl(a0, 1); // Q16 - output[n] = intround(a0); // Q0 - - /* update zero section of memory */ - cs->hpfzm[1] = cs->hpfzm[0]; // Q0 - cs->hpfzm[0] = input[n]; // Q0 - } -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + preproc.c : Preprocssing (input highpass filter) + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "bv16cnst.h" +#include "bv16strct.h" +#include "bv16externs.h" +#include "basop32.h" + +/************************************************************************/ +/* 2nd order pole-zero 150Hz high-pass filter designed with Matlab: */ +/* [b a]=ellip(2,.25,40,150/4000,'high'); */ +/* Note that down-scaling by a factor 1.5 is built into b-coefficients */ +/* */ +/* y[n]=b[0]*x[n]+b[1]*x[n-1]+b[2]*x[n-2]-a[1]*y[n-1]-a[2]*y[n-2] */ +/************************************************************************/ + +void preprocess( + struct BV16_Encoder_State *cs, + Word16 *output, /* (o) Q0 output signal, less factor 1.5 */ + Word16 *input, /* (i) Q0 input signal */ + Word16 N) /* length of signal */ +{ + Word16 n; + Word32 a0; + + for(n=0; nhpfpm[0], cs->hpfpm[1], hpfa[1]); // Q14 + a0 = L_add(a0, Mpy_32_16(cs->hpfpm[2], cs->hpfpm[3], hpfa[2])); // Q14 + + /* zero section of filtering */ + a0 = L_mac0(a0, input[n], hpfb[0]); // Q14 + a0 = L_mac0(a0, cs->hpfzm[0], hpfb[1]); // Q14 + a0 = L_mac0(a0, cs->hpfzm[1], hpfb[2]); // Q14 + a0 = L_shl(a0, 1); // Q15 + + /* update pole section of memory */ + cs->hpfpm[2] = cs->hpfpm[0]; + cs->hpfpm[3] = cs->hpfpm[1]; + L_Extract(a0, cs->hpfpm, cs->hpfpm+1); + + /* get output in Q0 (less down-scaling by factor 1.5) */ + a0 = L_shl(a0, 1); // Q16 + output[n] = intround(a0); // Q0 + + /* update zero section of memory */ + cs->hpfzm[1] = cs->hpfzm[0]; // Q0 + cs->hpfzm[0] = input[n]; // Q0 + } +} diff --git a/jni/bx16_fixedp/bv16/ptquan.c b/app/src/main/jni/bx16_fixedp/bv16/ptquan.c similarity index 96% rename from jni/bx16_fixedp/bv16/ptquan.c rename to app/src/main/jni/bx16_fixedp/bv16/ptquan.c index b3be749..ccfbf89 100644 --- a/jni/bx16_fixedp/bv16/ptquan.c +++ b/app/src/main/jni/bx16_fixedp/bv16/ptquan.c @@ -1,142 +1,142 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - ptquan.c : Quantization of the 3 pitch predictor taps - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "bv16cnst.h" -#include "bv16strct.h" -#include "bv16externs.h" -#include "basop32.h" - -Word16 pitchtapquan( - Word16 *x, - Word16 pp, - Word16 *b, /* Q15 */ - Word32 *re) /* Q3 */ -{ - Word32 cormax, cor; - Word16 s0, s1, s2; - Word32 t0, t1, t2; - Word16 *xt, *sp0, *sp1, *sp2; - int ppm2, qidx=0, i, j; - Word32 p[9]; - Word16 sp[9]; - - ppm2 = pp-2; - xt = x + XOFF; - - for (i=0;i<3;i++) - { - sp0 = xt; - sp1 = x+XOFF-ppm2-i-1; - t0 = 1; - for (j=0;j cormax) - { - cormax = cor; - qidx = i; - } - } - - sp2 = pp9cb + qidx*9; - for (i=0;i<3;i++) - b[i] = sp2[i]; /* multiplied by 0.5 : Q14 -> Q15 */ - - sp0 = x + XOFF; - sp1 = x + XOFF - ppm2 - 1; - t1 = 0; - for (i=0;i cormax) + { + cormax = cor; + qidx = i; + } + } + + sp2 = pp9cb + qidx*9; + for (i=0;i<3;i++) + b[i] = sp2[i]; /* multiplied by 0.5 : Q14 -> Q15 */ + + sp0 = x + XOFF; + sp1 = x + XOFF - ppm2 - 1; + t1 = 0; + for (i=0;i>1)+1 -#define NBIS 4 /* number of bisections */ - -Word16 FNevChebP(Word16 x, Word16 *t_man, Word16 *t_exp, Word16 nd2); - -void a2lsp( - Word16 pc[], /* (i) Q12: predictor coefficients */ - Word16 lsp[], /* (o) Q15: line spectral pairs */ - Word16 old_lsp[]) /* (i) Q15: old lsp */ -{ - Word16 i, j, exp; - Word16 fa_man[NAB], fa_exp[NAB], fb_man[NAB], fb_exp[NAB]; - Word16 ta_man[NAB], ta_exp[NAB], tb_man[NAB], tb_exp[NAB]; - Word16 *t_man, *t_exp; - Word32 a0; - Word16 nd2, nf, ngrd; - Word16 xroot, xlow, ylow, ind, xhigh, yhigh, xmid, ymid, dx, dy, dxdy, x, sign; - - - /* Find normalization for fa and fb */ - /* fb[0] = fa[0] = 1.0; */ - /* for (i = 1, j = LPCO; i <= (LPCO/2); i++, j--) { */ - /* fa[i] = pc[i] + pc[j] - fa[i-1]; */ - /* fb[i] = pc[i] - pc[j] + fb[i-1]; */ - /* } */ - fa_man[0] = 16384; - fa_exp[0] = 6; // fa_man[0] in high 16-bits >> fa_exp[0] = 1.0 in Q24 - fb_man[0] = 16384; - fb_exp[0] = 6; // fb_man[0] in high 16-bits >> fb_exp[0] = 1.0 in Q24 - for (i = 1, j = LPCO; i <= (LPCO/2); i++, j--) { - a0 = L_mult0(pc[i], 4096); // Q24 - a0 = L_mac0(a0, pc[j], 4096); // Q24 - a0 = L_sub(a0, L_shr(L_deposit_h(fa_man[i-1]),fa_exp[i-1])); // Q24 - fa_exp[i] = norm_l(a0); - fa_man[i] = intround(L_shl(a0, fa_exp[i])); // Q(8+fb_exp[i]) - - a0 = L_mult0(pc[i], 4096); // Q24 - a0 = L_msu0(a0, pc[j], 4096); // Q24 - a0 = L_add(a0, L_shr(L_deposit_h(fb_man[i-1]),fb_exp[i-1])); // Q24 - fb_exp[i] = norm_l(a0); - fb_man[i] = intround(L_shl(a0, fb_exp[i])); // Q(8+fb_exp[i]) - } - - nd2 = (LPCO)/2; - - /* ta[] and tb[] in Q(7+exp) */ - /* ta[0] = fa[nab-1]; ta[i] = 2.0 * fa[j]; */ - /* tb[0] = fb[nab-1]; tb[i] = 2.0 * fb[j]; */ - ta_man[0] = fa_man[NAB-1]; - ta_exp[0] = add(fa_exp[NAB-1], 1); - tb_man[0] = fb_man[NAB-1]; - tb_exp[0] = add(fb_exp[NAB-1], 1); - for (i = 1, j = NAB - 2; i < NAB; ++i, --j) { - ta_man[i] = fa_man[j]; - ta_exp[i] = fa_exp[j]; - tb_man[i] = fb_man[j]; - tb_exp[i] = fb_exp[j]; - } - - nf = 0; - t_man = ta_man; - t_exp = ta_exp; - xroot = 0x7fff; - ngrd = 0; - xlow = grid[0]; // Q15 - ylow = FNevChebP(xlow, t_man, t_exp, nd2); - ind = 0; - - /* Root search loop */ - while (ngrd<(Ngrd-1) && nf < LPCO) { - - ngrd++; - xhigh = xlow; - yhigh = ylow; - xlow = grid[ngrd]; - ylow = FNevChebP(xlow, t_man, t_exp, nd2); - - if ( L_mult(ylow ,yhigh) <= 0) { - - /* Bisections of the interval containing a sign change */ - - dx = xhigh - xlow; - for (i = 1; i <= NBIS; ++i) { - dx = shr(dx, 1); - xmid = add(xlow, dx); - ymid = FNevChebP(xmid, t_man, t_exp, nd2); - if (L_mult(ylow,ymid) <= 0) { - yhigh = ymid; - xhigh = xmid; - } else { - ylow = ymid; - xlow = xmid; - } - } - - /* - * Linear interpolation in the subinterval with a sign change - * (take care if yhigh=ylow=0) - */ - - dx = sub(xhigh, xlow); - dy = sub(ylow, yhigh); - if (dy != 0) { - sign = dy; - dy = abs_s(dy); - exp = norm_s(dy); - dy = shl(dy, exp); - /* The maximum grid distance is 1629 => */ - /* Maximum dx=1629/2^4=101.8125, i.e. 16384/101.8125=160.92~128 (7 bits) */ - /* However, due to the starting point for the search of a new root, */ - /* xlow = xroot, 1 more bit of headroom for the division is required. */ - dxdy = div_s(shl(dx,6), dy); - a0 = L_mult(dxdy, ylow); - a0 = L_shr(a0, sub(6, exp)); - x = intround(a0); - if(sign < 0) x = negate(x); - xmid = add(xlow, x); - } - else { - xmid = add(xlow, shr(dx,1)); - } - - /* acos mapping for New lsp component */ - while (( costable[ind] >= xmid ) && (ind < 63)) ind++; - ind--; - a0 = L_mult( sub(xmid, costable[ind]) , acosslope[ind] ); - x = intround(L_shl(a0, 4)); - lsp[nf] = add(x, shl(ind, 9)); - ++nf; - - /* Start the search for the roots of next polynomial at the estimated - * location of the root just found. We have to catch the case that the - * two polynomials have roots at the same place to avoid getting stuck at - * that root. - */ - - if (xmid >= xroot) xmid = xlow - dx; - xroot = xmid; - if (t_man == ta_man){ - t_man = tb_man; - t_exp = tb_exp; - } - else{ - t_man = ta_man; - t_exp = ta_exp; - } - xlow = xmid; - ylow = FNevChebP(xlow, t_man, t_exp, nd2); - - } - } - - /* Check if all LSPs are found */ - if( sub(nf, LPCO) < 0) - { - W16copy(lsp, old_lsp, LPCO); - } - - return; -} - -Word16 FNevChebP( - Word16 x, /* (i) Q15: value */ - Word16 *t_man, /* (i) Q7: mantissa of coefficients */ - Word16 *t_exp, /* (i): exponent fo cofficients */ - Word16 nd2) /* (i): order */ -{ - Word16 i; - Word16 x2; - Word16 b_man[NAB], b_exp[NAB]; - Word16 y; - Word32 a0; - - x2 = x; // 2x in Q14 - b_man[0] = t_man[nd2]; - b_exp[0] = t_exp[nd2]; // b[0] in Q(7+t_exp) - a0 = L_mult(x2, b_man[0]); - a0 = L_shr(a0, sub(b_exp[0], 1)); // t*b[0] in Q23 - a0 = L_add(a0, L_shr(L_deposit_h(t_man[nd2-1]), t_exp[nd2-1])); // c[nd2-1] + t*b[0] in Q23 - b_exp[1] = norm_l(a0); - b_man[1] = intround(L_shl(a0, b_exp[1])); // b[1] = c[nd2-1] + t * b[0] - - for (i=2;i>1)+1 +#define NBIS 4 /* number of bisections */ + +Word16 FNevChebP(Word16 x, Word16 *t_man, Word16 *t_exp, Word16 nd2); + +void a2lsp( + Word16 pc[], /* (i) Q12: predictor coefficients */ + Word16 lsp[], /* (o) Q15: line spectral pairs */ + Word16 old_lsp[]) /* (i) Q15: old lsp */ +{ + Word16 i, j, exp; + Word16 fa_man[NAB], fa_exp[NAB], fb_man[NAB], fb_exp[NAB]; + Word16 ta_man[NAB], ta_exp[NAB], tb_man[NAB], tb_exp[NAB]; + Word16 *t_man, *t_exp; + Word32 a0; + Word16 nd2, nf, ngrd; + Word16 xroot, xlow, ylow, ind, xhigh, yhigh, xmid, ymid, dx, dy, dxdy, x, sign; + + + /* Find normalization for fa and fb */ + /* fb[0] = fa[0] = 1.0; */ + /* for (i = 1, j = LPCO; i <= (LPCO/2); i++, j--) { */ + /* fa[i] = pc[i] + pc[j] - fa[i-1]; */ + /* fb[i] = pc[i] - pc[j] + fb[i-1]; */ + /* } */ + fa_man[0] = 16384; + fa_exp[0] = 6; // fa_man[0] in high 16-bits >> fa_exp[0] = 1.0 in Q24 + fb_man[0] = 16384; + fb_exp[0] = 6; // fb_man[0] in high 16-bits >> fb_exp[0] = 1.0 in Q24 + for (i = 1, j = LPCO; i <= (LPCO/2); i++, j--) { + a0 = L_mult0(pc[i], 4096); // Q24 + a0 = L_mac0(a0, pc[j], 4096); // Q24 + a0 = L_sub(a0, L_shr(L_deposit_h(fa_man[i-1]),fa_exp[i-1])); // Q24 + fa_exp[i] = norm_l(a0); + fa_man[i] = intround(L_shl(a0, fa_exp[i])); // Q(8+fb_exp[i]) + + a0 = L_mult0(pc[i], 4096); // Q24 + a0 = L_msu0(a0, pc[j], 4096); // Q24 + a0 = L_add(a0, L_shr(L_deposit_h(fb_man[i-1]),fb_exp[i-1])); // Q24 + fb_exp[i] = norm_l(a0); + fb_man[i] = intround(L_shl(a0, fb_exp[i])); // Q(8+fb_exp[i]) + } + + nd2 = (LPCO)/2; + + /* ta[] and tb[] in Q(7+exp) */ + /* ta[0] = fa[nab-1]; ta[i] = 2.0 * fa[j]; */ + /* tb[0] = fb[nab-1]; tb[i] = 2.0 * fb[j]; */ + ta_man[0] = fa_man[NAB-1]; + ta_exp[0] = add(fa_exp[NAB-1], 1); + tb_man[0] = fb_man[NAB-1]; + tb_exp[0] = add(fb_exp[NAB-1], 1); + for (i = 1, j = NAB - 2; i < NAB; ++i, --j) { + ta_man[i] = fa_man[j]; + ta_exp[i] = fa_exp[j]; + tb_man[i] = fb_man[j]; + tb_exp[i] = fb_exp[j]; + } + + nf = 0; + t_man = ta_man; + t_exp = ta_exp; + xroot = 0x7fff; + ngrd = 0; + xlow = grid[0]; // Q15 + ylow = FNevChebP(xlow, t_man, t_exp, nd2); + ind = 0; + + /* Root search loop */ + while (ngrd<(Ngrd-1) && nf < LPCO) { + + ngrd++; + xhigh = xlow; + yhigh = ylow; + xlow = grid[ngrd]; + ylow = FNevChebP(xlow, t_man, t_exp, nd2); + + if ( L_mult(ylow ,yhigh) <= 0) { + + /* Bisections of the interval containing a sign change */ + + dx = xhigh - xlow; + for (i = 1; i <= NBIS; ++i) { + dx = shr(dx, 1); + xmid = add(xlow, dx); + ymid = FNevChebP(xmid, t_man, t_exp, nd2); + if (L_mult(ylow,ymid) <= 0) { + yhigh = ymid; + xhigh = xmid; + } else { + ylow = ymid; + xlow = xmid; + } + } + + /* + * Linear interpolation in the subinterval with a sign change + * (take care if yhigh=ylow=0) + */ + + dx = sub(xhigh, xlow); + dy = sub(ylow, yhigh); + if (dy != 0) { + sign = dy; + dy = abs_s(dy); + exp = norm_s(dy); + dy = shl(dy, exp); + /* The maximum grid distance is 1629 => */ + /* Maximum dx=1629/2^4=101.8125, i.e. 16384/101.8125=160.92~128 (7 bits) */ + /* However, due to the starting point for the search of a new root, */ + /* xlow = xroot, 1 more bit of headroom for the division is required. */ + dxdy = div_s(shl(dx,6), dy); + a0 = L_mult(dxdy, ylow); + a0 = L_shr(a0, sub(6, exp)); + x = intround(a0); + if(sign < 0) x = negate(x); + xmid = add(xlow, x); + } + else { + xmid = add(xlow, shr(dx,1)); + } + + /* acos mapping for New lsp component */ + while (( costable[ind] >= xmid ) && (ind < 63)) ind++; + ind--; + a0 = L_mult( sub(xmid, costable[ind]) , acosslope[ind] ); + x = intround(L_shl(a0, 4)); + lsp[nf] = add(x, shl(ind, 9)); + ++nf; + + /* Start the search for the roots of next polynomial at the estimated + * location of the root just found. We have to catch the case that the + * two polynomials have roots at the same place to avoid getting stuck at + * that root. + */ + + if (xmid >= xroot) xmid = xlow - dx; + xroot = xmid; + if (t_man == ta_man){ + t_man = tb_man; + t_exp = tb_exp; + } + else{ + t_man = ta_man; + t_exp = ta_exp; + } + xlow = xmid; + ylow = FNevChebP(xlow, t_man, t_exp, nd2); + + } + } + + /* Check if all LSPs are found */ + if( sub(nf, LPCO) < 0) + { + W16copy(lsp, old_lsp, LPCO); + } + + return; +} + +Word16 FNevChebP( + Word16 x, /* (i) Q15: value */ + Word16 *t_man, /* (i) Q7: mantissa of coefficients */ + Word16 *t_exp, /* (i): exponent fo cofficients */ + Word16 nd2) /* (i): order */ +{ + Word16 i; + Word16 x2; + Word16 b_man[NAB], b_exp[NAB]; + Word16 y; + Word32 a0; + + x2 = x; // 2x in Q14 + b_man[0] = t_man[nd2]; + b_exp[0] = t_exp[nd2]; // b[0] in Q(7+t_exp) + a0 = L_mult(x2, b_man[0]); + a0 = L_shr(a0, sub(b_exp[0], 1)); // t*b[0] in Q23 + a0 = L_add(a0, L_shr(L_deposit_h(t_man[nd2-1]), t_exp[nd2-1])); // c[nd2-1] + t*b[0] in Q23 + b_exp[1] = norm_l(a0); + b_man[1] = intround(L_shl(a0, b_exp[1])); // b[1] = c[nd2-1] + t * b[0] + + for (i=2;i 0; i--) - a0 = L_msu0(a0, a[i], *fp1++); // Q12 - - /* update temporary buffer for filter memory */ - *fp1 = intround(L_shl(a0,4)); - } - - /* copy to output array */ - W16copy(y, buf+m, lg); - - /* get the filter memory after filtering the current vector */ - if(update) - W16copy(mem, buf+lg, m); - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + allpole.c : Common Fixed-Point Library: all-pole filter + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "basop32.h" +#include "utility.h" + +#define BUFFERSIZE (LPCO+160) + +void apfilter( + Word16 a[], /* (i) Q12 : prediction coefficients */ + Word16 m, /* (i) : LPC order */ + Word16 x[], /* (i) Q0 : input signal */ + Word16 y[], /* (o) Q0 : output signal */ + Word16 lg, /* (i) : size of filtering */ + Word16 mem[], /* (i/o) Q0: filter memory */ + Word16 update /* (i) : memory update flag */ + ) +{ + Word16 buf[BUFFERSIZE]; /* buffer for filter memory & signal */ + Word32 a0; + Word16 *fp1; + Word16 i, n; + + /* copy filter memory to beginning part of temporary buffer */ + W16copy(buf, mem, m); + + /* loop through every element of the current vector */ + for (n = 0; n < lg; n++) { + + /* perform multiply-adds along the delay line of filter */ + fp1 = &buf[n]; + a0 = L_mult0(4096, x[n]); // Q12 + for (i = m; i > 0; i--) + a0 = L_msu0(a0, a[i], *fp1++); // Q12 + + /* update temporary buffer for filter memory */ + *fp1 = intround(L_shl(a0,4)); + } + + /* copy to output array */ + W16copy(y, buf+m, lg); + + /* get the filter memory after filtering the current vector */ + if(update) + W16copy(mem, buf+lg, m); + + return; +} diff --git a/jni/bx16_fixedp/bvcommon/allzero.c b/app/src/main/jni/bx16_fixedp/bvcommon/allzero.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/allzero.c rename to app/src/main/jni/bx16_fixedp/bvcommon/allzero.c index f21e486..96dfe6b 100644 --- a/jni/bx16_fixedp/bvcommon/allzero.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/allzero.c @@ -1,57 +1,57 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - allzero.c : Common Fixed-Point Library: all-zero filter - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "basop32.h" - -void azfilter( - Word16 a[], /* (i) Q12 : prediction coefficients */ - Word16 m, /* (i) : LPC order */ - Word16 x[], /* (i) Q0 : input signal samples, incl. past */ - Word16 y[], /* (o) Q0 : filtered output signal */ - Word16 lg /* (i) : size of filtering */ - ) -{ - Word16 i, n; - Word32 a0; - Word16 *fp1; - - /* loop through every element of the current vector */ - for (n = 0; n < lg; n++) { - - /* perform multiply-adds along the delay line of filter */ - fp1 = x + n; - a0 = L_mult0(a[0], *fp1--); // Q12 - for (i = 1; i <= m; i++) - a0 = L_mac0(a0, a[i], *fp1--); // Q12 - - /* get the output with rounding */ - y[n] = intround(L_shl(a0, 4)); // Q0 - } - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + allzero.c : Common Fixed-Point Library: all-zero filter + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "basop32.h" + +void azfilter( + Word16 a[], /* (i) Q12 : prediction coefficients */ + Word16 m, /* (i) : LPC order */ + Word16 x[], /* (i) Q0 : input signal samples, incl. past */ + Word16 y[], /* (o) Q0 : filtered output signal */ + Word16 lg /* (i) : size of filtering */ + ) +{ + Word16 i, n; + Word32 a0; + Word16 *fp1; + + /* loop through every element of the current vector */ + for (n = 0; n < lg; n++) { + + /* perform multiply-adds along the delay line of filter */ + fp1 = x + n; + a0 = L_mult0(a[0], *fp1--); // Q12 + for (i = 1; i <= m; i++) + a0 = L_mac0(a0, a[i], *fp1--); // Q12 + + /* get the output with rounding */ + y[n] = intround(L_shl(a0, 4)); // Q0 + } + + return; +} diff --git a/jni/bx16_fixedp/bvcommon/autocor.c b/app/src/main/jni/bx16_fixedp/bvcommon/autocor.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/autocor.c rename to app/src/main/jni/bx16_fixedp/bvcommon/autocor.c index a9b841d..5491eaf 100644 --- a/jni/bx16_fixedp/bvcommon/autocor.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/autocor.c @@ -1,98 +1,98 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - autocor.c : Common Fixed-Point Library: window an input array and - compute autocorrelation coefficients - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "basop32.h" - -#define WINSZ 160 - -void Autocorr( - Word32 r[], /* (o) : Autocorrelations */ - Word16 x[], /* (i) : Input signal */ - Word16 window[],/* (i) : LPC Analysis window */ - Word16 l_window,/* (i) : window length */ - Word16 m) /* (i) : LPC order */ -{ - Word16 n, j, y_shift, shift; - Word16 buf[WINSZ]; - Word32 a0; - - /* Window signal */ - for(n=0; n LSP Conversion */ - -Word16 grid[] ={ - 32766, 32557, 32272, 31868, 31385, 30832, - 30197, 29478, 28684, 27813, 26864, 25846, - 24769, 23637, 22459, 21238, 19993, 18710, - 17370, 15999, 14574, 13086, 11571, 10021, - 8473, 6913, 5344, 3764, 2154, 529, - -1100, -2723, -4324, -5912, -7470, -9016, - -10566, -12102, -13618, -15087, -16496, -17847, - -19166, -20434, -21667, -22872, -24041, -25152, - -26211, -27204, -28129, -28975, -29744, -30437, - -31038, -31572, -32021, -32385, -32621, -32766 -}; +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + cmtables.c : Common Fixed-Point Library: tables + + $Log$ +******************************************************************************/ + +#include "typedef.h" + +/* LPC bandwidth expansion */ + +Word16 bwel[] = { + 32767, 31737, 30738, 29770, 28833, 27925, 27046, 26195, 25370 +}; + +/* LPC WEIGHTING filter */ +Word16 STWAL[] = { + 32767, 24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281 +}; + +/* coarse pitch search */ +Word16 invk[4] = { 16384, 10923, 8192, 6554 }; + +/* LPC -> LSP Conversion */ + +Word16 grid[] ={ + 32766, 32557, 32272, 31868, 31385, 30832, + 30197, 29478, 28684, 27813, 26864, 25846, + 24769, 23637, 22459, 21238, 19993, 18710, + 17370, 15999, 14574, 13086, 11571, 10021, + 8473, 6913, 5344, 3764, 2154, 529, + -1100, -2723, -4324, -5912, -7470, -9016, + -10566, -12102, -13618, -15087, -16496, -17847, + -19166, -20434, -21667, -22872, -24041, -25152, + -26211, -27204, -28129, -28975, -29744, -30437, + -31038, -31572, -32021, -32385, -32621, -32766 +}; diff --git a/jni/bx16_fixedp/bvcommon/levdur.c b/app/src/main/jni/bx16_fixedp/bvcommon/levdur.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/levdur.c rename to app/src/main/jni/bx16_fixedp/bvcommon/levdur.c index 5d8bea6..882edb2 100644 --- a/jni/bx16_fixedp/bvcommon/levdur.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/levdur.c @@ -1,145 +1,145 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - levdur.c : Common Fixed-Point Library: Levinson-Durbin - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "basop32.h" - -void Levinson( - Word32 r32[], /* (i) : r32[] double precision vector of autocorrelation coefficients */ - Word16 a[], /* (o) : a[] in Q12 - LPC coefficients */ - Word16 old_a[], /* (i/o): old_a[] in Q12 - previous LPC coefficients */ - Word16 m /* (i) : LPC order */ -) -{ - Word16 i, j, high, low, alpha_hi, alpha_lo, alpha_exp; - Word16 exp, r_hi[LPCO+1], r_lo[LPCO+1]; - Word16 a_hi[LPCO+1], a_lo[LPCO+1], anew_hi[LPCO+1], anew_lo[LPCO+1]; - Word16 rc_hi, rc_lo; - Word32 a0, a1, alpha_man; - - /* Normalization of autocorrelation coefficients */ - exp = norm_l(r32[0]); - for(i=0; i<=m; i++){ - r32[i] = L_shl(r32[i], exp); - L_Extract(r32[i], r_hi+i, r_lo+i); - } - - /* a[1] = rc = -r[1]/r[0] */ - a1 = L_abs(r32[1]); - a0 = Div_32(a1, r_hi[0], r_lo[0]); // rc in Q31 - if(r32[1] > 0) - a0 = L_negate(a0); - L_Extract(L_shr(a0,4), a_hi+1, a_lo+1); // Q27 - - /* alpha = r[0]*(1-rc*rc) */ - L_Extract(a0, &high, &low); - a0 = Mpy_32(high, low, high, low); // rc^2 in Q31 - a0 = L_abs(a0); // Lesson from G.729 - a0 = L_sub(0x40000000, L_shr(a0,1)); // 1-rc*rc in Q30 - L_Extract(a0, &high, &low); - a0 = Mpy_32(r_hi[0], r_lo[0], high, low); // alpha in Q30 - alpha_exp = norm_l(a0); - alpha_man = L_shl(a0, alpha_exp); - alpha_exp = sub(alpha_exp, 1); // alpha: Q(31+alpha_exp) - - /* Recursive solution of Yule-Walker equations */ - for(i= 2; i<=m; i++) - { - - /* s = r[i] + sum{r[j]*a[i-j], j=1,2,...,i-1} */ - a0 = 0; - for(j=1; j= 0){ - a1 = L_shr(a1,1); - exp = sub(exp,1); - } - L_Extract(alpha_man, &alpha_hi, &alpha_lo); - a1 = Div_32(a1, alpha_hi, alpha_lo); - if(a0 > 0) - a1 = L_negate(a1); // rc in Q(31+exp-alpha_exp) - a1 = L_shr(a1, sub(exp, alpha_exp)); // rc in Q31 - L_Extract(a1, &rc_hi, &rc_lo); // rc in Q31 - - /* Check for absolute value of reflection coefficient - stability */ - if (sub(abs_s(intround(a1)), 32750) > 0) - { - a[0] = 4096; - for(j=1; j<=m; j++) - a[j] = old_a[j]; - - return; - } - - /* anew[j]=a[j]+rc*a[i-j], j=1,2,...i-1 */ - /* anew[i]=rc */ - for(j=1; j 0) + a0 = L_negate(a0); + L_Extract(L_shr(a0,4), a_hi+1, a_lo+1); // Q27 + + /* alpha = r[0]*(1-rc*rc) */ + L_Extract(a0, &high, &low); + a0 = Mpy_32(high, low, high, low); // rc^2 in Q31 + a0 = L_abs(a0); // Lesson from G.729 + a0 = L_sub(0x40000000, L_shr(a0,1)); // 1-rc*rc in Q30 + L_Extract(a0, &high, &low); + a0 = Mpy_32(r_hi[0], r_lo[0], high, low); // alpha in Q30 + alpha_exp = norm_l(a0); + alpha_man = L_shl(a0, alpha_exp); + alpha_exp = sub(alpha_exp, 1); // alpha: Q(31+alpha_exp) + + /* Recursive solution of Yule-Walker equations */ + for(i= 2; i<=m; i++) + { + + /* s = r[i] + sum{r[j]*a[i-j], j=1,2,...,i-1} */ + a0 = 0; + for(j=1; j= 0){ + a1 = L_shr(a1,1); + exp = sub(exp,1); + } + L_Extract(alpha_man, &alpha_hi, &alpha_lo); + a1 = Div_32(a1, alpha_hi, alpha_lo); + if(a0 > 0) + a1 = L_negate(a1); // rc in Q(31+exp-alpha_exp) + a1 = L_shr(a1, sub(exp, alpha_exp)); // rc in Q31 + L_Extract(a1, &rc_hi, &rc_lo); // rc in Q31 + + /* Check for absolute value of reflection coefficient - stability */ + if (sub(abs_s(intround(a1)), 32750) > 0) + { + a[0] = 4096; + for(j=1; j<=m; j++) + a[j] = old_a[j]; + + return; + } + + /* anew[j]=a[j]+rc*a[i-j], j=1,2,...i-1 */ + /* anew[i]=rc */ + for(j=1; j>1); n++) { - - /* cosine mapping */ - index = shr(lsp[2*n-2],9); // Q6 - offset = lsp[2*n-2]&(Word16)0x01ff; // Q9 - a0 = L_mult(sub(costable[index+1], costable[index]), offset); // Q10 - coslsp = add(costable[index], intround(L_shl(a0, 6))); // Q15 cos((double)PI*lsp[2*n-2]) - - c = coslsp; // Q14 c = 2. * cos((double)PI*lsp[2*n-2]) - - for(i = 2*n; i >= 2; i--){ - L_Extract(f[i-1], &hi, &lo); - - f[i] = L_add(f[i], f[i-2]); // Q23 f[i] += f[i-2] - a0 = Mpy_32_16(hi, lo, c); // Q22 - f[i] = L_sub(f[i], L_shl(a0,1)); // Q23 f[i] += f[i-2] - c*f[i-1]; - } - f[1] = L_msu(f[1], c, 256); // Q23 f[1] -= c; - } - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + lsp2a.c : Common Fixed-Point Library: conversion from lsp's to a's + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" +#include "basop32.h" +#include "mathutil.h" + +void get_pq_polynomials( + Word32 *f, /* Q23 */ + Word16 *lsp); /* Q15 */ + +void lsp2a( + Word16 lsp[], /* (i) Q15 : line spectral pairs */ + Word16 a[]) /* (o) Q12 : predictor coefficients (order = 10) */ +{ + Word32 p[LPCO+1], q[LPCO+1]; // Q23 + Word32 a0; + Word16 i, n; + + get_pq_polynomials(p, lsp); + get_pq_polynomials(q, lsp+1); + + a[0] = 4096; // Q12 + a0 = L_add(p[1], q[1]); // Q23 + a[1] = intround(L_shl(a0,4)); // Q12 - includes 0.5 factor of a[1] = 0.5*(p[1]+q[1]) + for(i=1, n=2; i>1); n++) { + + /* cosine mapping */ + index = shr(lsp[2*n-2],9); // Q6 + offset = lsp[2*n-2]&(Word16)0x01ff; // Q9 + a0 = L_mult(sub(costable[index+1], costable[index]), offset); // Q10 + coslsp = add(costable[index], intround(L_shl(a0, 6))); // Q15 cos((double)PI*lsp[2*n-2]) + + c = coslsp; // Q14 c = 2. * cos((double)PI*lsp[2*n-2]) + + for(i = 2*n; i >= 2; i--){ + L_Extract(f[i-1], &hi, &lo); + + f[i] = L_add(f[i], f[i-2]); // Q23 f[i] += f[i-2] + a0 = Mpy_32_16(hi, lo, c); // Q22 + f[i] = L_sub(f[i], L_shl(a0,1)); // Q23 f[i] += f[i-2] - c*f[i-1]; + } + f[1] = L_msu(f[1], c, 256); // Q23 f[1] -= c; + } + + return; +} diff --git a/jni/bx16_fixedp/bvcommon/mathtables.c b/app/src/main/jni/bx16_fixedp/bvcommon/mathtables.c similarity index 98% rename from jni/bx16_fixedp/bvcommon/mathtables.c rename to app/src/main/jni/bx16_fixedp/bvcommon/mathtables.c index 0981f05..be2006c 100644 --- a/jni/bx16_fixedp/bvcommon/mathtables.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/mathtables.c @@ -1,115 +1,115 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - fixmath.c : Common Fixed-Point Library: math routine tables - - $Log$ -******************************************************************************/ - -#include "typedef.h" - -/* Table for 2^x, x=[-1; 0] in Q15 - generated in Matlab with: */ -/* N=65; */ -/* Q=15; */ -/* x=linspace(-1.0, 0.0, N); */ -/* pow2tab=max(min(intround(2^Q.*2.^x),32767),-32768); */ - -Word16 tabpow[65] = { - 16384, 16562, 16743, 16925, 17109, 17296, 17484, 17674, 17867, - 18061, 18258, 18457, 18658, 18861, 19066, 19274, 19484, 19696, - 19911, 20127, 20347, 20568, 20792, 21019, 21247, 21479, 21713, - 21949, 22188, 22430, 22674, 22921, 23170, 23423, 23678, 23936, - 24196, 24460, 24726, 24995, 25268, 25543, 25821, 26102, 26386, - 26674, 26964, 27258, 27554, 27855, 28158, 28464, 28774, 29088, - 29405, 29725, 30048, 30376, 30706, 31041, 31379, 31720, 32066, - 32415, 32767}; - -/* Table for log2(x), x=[1; 2] in Q15 - generated in Matlab with: */ -/* N=65; */ -/* Q=15; */ -/* x=linspace(1.0, 2.0, N); */ -/* log2tab=max(min(intround(2^Q.*log2(x)),32767),-32768); */ - -Word16 tablog[65] = { - 0, 733, 1455, 2166, 2866, 3556, 4236, 4907, 5568, - 6220, 6863, 7498, 8124, 8742, 9352, 9954, 10549, 11136, - 11716, 12289, 12855, 13415, 13968, 14514, 15055, 15589, 16117, - 16639, 17156, 17667, 18173, 18673, 19168, 19658, 20143, 20623, - 21098, 21568, 22034, 22495, 22952, 23404, 23852, 24296, 24736, - 25172, 25604, 26031, 26455, 26876, 27292, 27705, 28114, 28520, - 28922, 29321, 29717, 30109, 30498, 30884, 31267, 31647, 32024, - 32397, 32767}; - -/* cos(x), x=[0; pi] in Q15 - generated in Matlab with: */ -/* N=65; */ -/* Q=15; */ -/* x=linspace(0, pi, N); */ -/* cosx=min(max(intround((2^Q)*cos(x)),32767),-32768); */ - -Word16 costable[65] = { - 32767, 32729, 32610, 32413, 32138, 31786, - 31357, 30853, 30274, 29622, 28899, 28106, - 27246, 26320, 25330, 24279, 23170, 22006, - 20788, 19520, 18205, 16846, 15447, 14010, - 12540, 11039, 9512, 7962, 6393, 4808, - 3212, 1608, 0, -1608, -3212, -4808, - -6393, -7962, -9512, -11039, -12540, -14010, - -15447, -16846, -18205, -19520, -20788, -22006, - -23170, -24279, -25330, -26320, -27246, -28106, - -28899, -29622, -30274, -30853, -31357, -31786, - -32138, -32413, -32610, -32729, -32768}; - -/* slope of acos(x), Q12 - generated in Matlab with: */ -/* Qa=12; */ -/* z=intround((2^Qa)*(x(2:N)-x(1:N-1))./(cos(x(2:N))-cos(x(1:N-1)))/(2*pi)); */ -/* in addition to above Matlab commands to generate cosx. */ - -Word16 acosslope[64] = { - -26566, -8862, -5326, -3814, -2976, -2444, - -2078, -1812, -1609, -1450, -1323, -1219, - -1132, -1060, -998, -946, -900, -861, - -827, -797, -772, -749, -730, -713, - -699, -687, -676, -668, -662, -657, - -654, -652, -652, -654, -657, -662, - -668, -676, -687, -699, -713, -730, - -749, -772, -797, -827, -861, -900, - -946, -998, -1060, -1132, -1219, -1323, - -1450, -1609, -1812, -2078, -2444, -2976, - -3814, -5326, -8862, -26566}; - -/* sqrt(x), Q15 - generated in Matlab with : */ -/* N=65; */ -/* Q=15; */ -/* x=linspace(0, 1, N); */ -/* sqrttab=max(min(intround((2^Q)*sqrt(x)),32767),-32768); */ - -Word16 tabsqrt[65] = { - 0, 4096, 5793, 7094, 8192, 9159, 10033, - 10837, 11585, 12288, 12953, 13585, 14189, 14768, - 15326, 15864, 16384, 16888, 17378, 17854, 18318, - 18770, 19212, 19644, 20066, 20480, 20886, 21283, - 21674, 22058, 22435, 22806, 23170, 23530, 23884, - 24232, 24576, 24915, 25249, 25580, 25905, 26227, - 26545, 26859, 27170, 27477, 27780, 28081, 28378, - 28672, 28963, 29251, 29537, 29819, 30099, 30377, - 30652, 30924, 31194, 31462, 31727, 31991, 32252, - 32511, 32767}; +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + fixmath.c : Common Fixed-Point Library: math routine tables + + $Log$ +******************************************************************************/ + +#include "typedef.h" + +/* Table for 2^x, x=[-1; 0] in Q15 - generated in Matlab with: */ +/* N=65; */ +/* Q=15; */ +/* x=linspace(-1.0, 0.0, N); */ +/* pow2tab=max(min(intround(2^Q.*2.^x),32767),-32768); */ + +Word16 tabpow[65] = { + 16384, 16562, 16743, 16925, 17109, 17296, 17484, 17674, 17867, + 18061, 18258, 18457, 18658, 18861, 19066, 19274, 19484, 19696, + 19911, 20127, 20347, 20568, 20792, 21019, 21247, 21479, 21713, + 21949, 22188, 22430, 22674, 22921, 23170, 23423, 23678, 23936, + 24196, 24460, 24726, 24995, 25268, 25543, 25821, 26102, 26386, + 26674, 26964, 27258, 27554, 27855, 28158, 28464, 28774, 29088, + 29405, 29725, 30048, 30376, 30706, 31041, 31379, 31720, 32066, + 32415, 32767}; + +/* Table for log2(x), x=[1; 2] in Q15 - generated in Matlab with: */ +/* N=65; */ +/* Q=15; */ +/* x=linspace(1.0, 2.0, N); */ +/* log2tab=max(min(intround(2^Q.*log2(x)),32767),-32768); */ + +Word16 tablog[65] = { + 0, 733, 1455, 2166, 2866, 3556, 4236, 4907, 5568, + 6220, 6863, 7498, 8124, 8742, 9352, 9954, 10549, 11136, + 11716, 12289, 12855, 13415, 13968, 14514, 15055, 15589, 16117, + 16639, 17156, 17667, 18173, 18673, 19168, 19658, 20143, 20623, + 21098, 21568, 22034, 22495, 22952, 23404, 23852, 24296, 24736, + 25172, 25604, 26031, 26455, 26876, 27292, 27705, 28114, 28520, + 28922, 29321, 29717, 30109, 30498, 30884, 31267, 31647, 32024, + 32397, 32767}; + +/* cos(x), x=[0; pi] in Q15 - generated in Matlab with: */ +/* N=65; */ +/* Q=15; */ +/* x=linspace(0, pi, N); */ +/* cosx=min(max(intround((2^Q)*cos(x)),32767),-32768); */ + +Word16 costable[65] = { + 32767, 32729, 32610, 32413, 32138, 31786, + 31357, 30853, 30274, 29622, 28899, 28106, + 27246, 26320, 25330, 24279, 23170, 22006, + 20788, 19520, 18205, 16846, 15447, 14010, + 12540, 11039, 9512, 7962, 6393, 4808, + 3212, 1608, 0, -1608, -3212, -4808, + -6393, -7962, -9512, -11039, -12540, -14010, + -15447, -16846, -18205, -19520, -20788, -22006, + -23170, -24279, -25330, -26320, -27246, -28106, + -28899, -29622, -30274, -30853, -31357, -31786, + -32138, -32413, -32610, -32729, -32768}; + +/* slope of acos(x), Q12 - generated in Matlab with: */ +/* Qa=12; */ +/* z=intround((2^Qa)*(x(2:N)-x(1:N-1))./(cos(x(2:N))-cos(x(1:N-1)))/(2*pi)); */ +/* in addition to above Matlab commands to generate cosx. */ + +Word16 acosslope[64] = { + -26566, -8862, -5326, -3814, -2976, -2444, + -2078, -1812, -1609, -1450, -1323, -1219, + -1132, -1060, -998, -946, -900, -861, + -827, -797, -772, -749, -730, -713, + -699, -687, -676, -668, -662, -657, + -654, -652, -652, -654, -657, -662, + -668, -676, -687, -699, -713, -730, + -749, -772, -797, -827, -861, -900, + -946, -998, -1060, -1132, -1219, -1323, + -1450, -1609, -1812, -2078, -2444, -2976, + -3814, -5326, -8862, -26566}; + +/* sqrt(x), Q15 - generated in Matlab with : */ +/* N=65; */ +/* Q=15; */ +/* x=linspace(0, 1, N); */ +/* sqrttab=max(min(intround((2^Q)*sqrt(x)),32767),-32768); */ + +Word16 tabsqrt[65] = { + 0, 4096, 5793, 7094, 8192, 9159, 10033, + 10837, 11585, 12288, 12953, 13585, 14189, 14768, + 15326, 15864, 16384, 16888, 17378, 17854, 18318, + 18770, 19212, 19644, 20066, 20480, 20886, 21283, + 21674, 22058, 22435, 22806, 23170, 23530, 23884, + 24232, 24576, 24915, 25249, 25580, 25905, 26227, + 26545, 26859, 27170, 27477, 27780, 28081, 28378, + 28672, 28963, 29251, 29537, 29819, 30099, 30377, + 30652, 30924, 31194, 31462, 31727, 31991, 32252, + 32511, 32767}; diff --git a/jni/bx16_fixedp/bvcommon/mathutil.c b/app/src/main/jni/bx16_fixedp/bvcommon/mathutil.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/mathutil.c rename to app/src/main/jni/bx16_fixedp/bvcommon/mathutil.c index df83e1c..59f2fd6 100644 --- a/jni/bx16_fixedp/bvcommon/mathutil.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/mathutil.c @@ -1,191 +1,191 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - dspfunc.c : Common Fixed-Point Library: common signal processing functions - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "basop32.h" -#include "mathutil.h" - -/****************************************************/ -/* y = 2^x = 2^exp * 2^frac */ -/* = 2^(exp+1) * 2^(frac-1) */ -/* */ -/* 2^(exp+1) = 1<<(exp+1) */ -/* 2^(frac-1) = 2^z, z in [-1; 0[ use table look-up */ -/* and interpolation. */ -/****************************************************/ - -Word32 Pow2( /* Q0 output */ - Word16 int_comp, /* Q0 Integer part */ - Word16 frac_comp /* Q15 frac_compal part */ - ) -{ - Word32 a0; - Word16 idx_frac, frac, sub_frac, sub_tab; - - idx_frac = shr(frac_comp, 9); // for 65 entry table - sub_frac = frac_comp & 0x01FF; // sub-frac_comp in Q9 - sub_frac = shl(sub_frac, 6); // Q15 - frac = tabpow[idx_frac]; // Q15 table entry of 2^(frac_comp-1) - a0 = L_deposit_h(frac); // Q31 table entry of 2^(frac_comp-1) - sub_tab = sub(tabpow[idx_frac+1], frac); // Q15 difference to next table entry - a0 = L_mac(a0, sub_frac, sub_tab); // Q31 linear interpolation between table entries - - a0 = L_shr_r(a0, sub(30, int_comp)); // Q0 - note: sub(30, int_comp) = 31-(int_comp+1) - - return a0; -} - -/***********************************************************/ -/* y = log2(x) = log2(man*2^exp) */ -/* = log2(man) + log2(2^exp) */ -/* = log2(man) + exp */ -/* */ -/* exponent = 30-exp */ -/* fraction = table look-up and interpolation of log2(man) */ -/***********************************************************/ - -void Log2( - Word32 x, /* (i) input */ - Word16 *int_comp, /* Q0 integer part */ - Word16 *frac_comp /* Q15 fractional part */ - ) -{ - Word16 exp, idx_man, sub_man, sub_tab; - Word32 a0; - - if(x <= 0){ - *int_comp = 0; - *frac_comp = 0; - } - else{ - exp = norm_l(x); // normalization - a0 = L_shl(x, exp); // Q30 mantissa, i.e. 1.xxx Q30 - - /* use table look-up of man in [1.0, 2.0[ Q30 */ - a0 = L_shr(L_sub(a0, (Word32)0x40000000), 8); // Q16 index into table - note zero'ing of leading 1 - idx_man = extract_h(a0); // Q0 index into table - sub_man = extract_l(L_shr((a0 & 0xFFFF), 1)); // Q15 fractional sub_man - a0 = L_deposit_h(tablog[idx_man]); // Q31 - sub_tab = sub(tablog[idx_man+1], tablog[idx_man]); // Q15 - a0 = L_mac(a0, sub_man, sub_tab); // Q31 - - *frac_comp = intround(a0); // Q15 - *int_comp = sub(30, exp); // Q0 - } - - return; -} - -/*******************************************/ -/* y = sqrt(x) */ -/* table look-up with linear interpolation */ -/*******************************************/ - -Word16 sqrts(Word16 x) -{ - Word16 xb, y, exp, idx, sub_frac, sub_tab; - Word32 a0; - - if(x <= 0){ - y = 0; - } - else{ - exp = norm_s(x); - - /* use 65-entry table */ - xb = shl(x, exp); // normalization of x - idx = shr(xb, 9); // for 65 entry table - a0 = L_deposit_h(tabsqrt[idx]); // Q31 table look-up value - sub_frac = shl((Word16)(xb & 0x01FF), 6); // Q15 sub-fraction - sub_tab = sub(tabsqrt[idx+1], tabsqrt[idx]); // Q15 table interval for interpolation - a0 = L_mac(a0, sub_frac, sub_tab); // Q31 linear interpolation between table entries - if(exp & 0x0001){ - exp = shr(add(exp, 1), 1); // normalization of sqrt() - a0 = L_shr(a0, exp); - y = intround(a0); // Q15 - a0 = L_mac(a0, 13573, y); // Q31 incorporate the missing "/sqrt(2)" - } - else{ - exp = shr(exp, 1); // normalization of sqrt() - a0 = L_shr(a0, exp); // Q31 - } - y = intround(a0); // Q15 - } - - return y; -} - -/****************************************************/ -/* y = 1/sqrt(x) */ -/* table look-up for sqrt with linear interpolation */ -/* use div_s for inverse */ -/****************************************************/ - -void sqrt_i(Word16 x_man, Word16 x_exp, Word16 *y_man, Word16 *y_exp) -{ - Word16 x_manb, x_expb, y, exp, idx, sub_frac, sub_tab; - Word32 a0; - - if(x_man <= 0){ - *y_man = 0; - *y_exp = 0; - } - else{ - - exp = norm_s(x_man); - x_manb = shl(x_man, exp); // normalize to Q15 in [0; 1] for table look-up - x_expb = add(x_exp, exp); // update exponent for x (left-shifting by additionl exp) - x_expb = sub(x_expb, 15); // we need to take sqrt of 0-32767 number but table is for 0-1 - idx = shr(x_manb, 9); // for 65 entry table - a0 = L_deposit_h(tabsqrt[idx]); // Q31 table look-up value - sub_frac = shl((Word16)(x_manb & 0x01FF), 6);// Q15 sub-fraction - sub_tab = sub(tabsqrt[idx+1], tabsqrt[idx]); // Q15 table interval for interpolation - a0 = L_mac(a0, sub_frac, sub_tab); // Q31 linear interpolation between table entries - exp = norm_l(a0); // exponent of a0 - y = intround(L_shl(a0,exp)); // normalize sqrt-root and drop 16 LBSs - exp = add(15, exp); // exponent of a0 taking Q15 of y into account - - if(x_expb & 0x0001){ - if(y < 0x5A82){ // 1*sqrt(2) in Q14) - with div_s(y1, y2), y2 must be >= y1 - exp = add(exp, shr(add(x_expb,1), 1)); // normalization for sqrt() - *y_man = div_s(0x2D41, y); // 0x2D41 is 1/sqrt(2) in Q14 - } - else{ - exp = add(exp, shr(sub(x_expb,1), 1)); // normalization for sqrt() - *y_man = div_s(0x5A82, y); // 0x5A82 is 1*sqrt(2) in Q14 - } - *y_exp = sub(29, exp); // ...and div_s returns fraction divide in Q15 - } - else{ - exp = add(exp, shr(x_expb, 1)); // normalization for sqrt() - *y_man = div_s(0x4000, y); - *y_exp = sub(29, exp); // 0x4000 is 1 in Q14 and div_s returns fraction divide in Q15 - } - } - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + dspfunc.c : Common Fixed-Point Library: common signal processing functions + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "basop32.h" +#include "mathutil.h" + +/****************************************************/ +/* y = 2^x = 2^exp * 2^frac */ +/* = 2^(exp+1) * 2^(frac-1) */ +/* */ +/* 2^(exp+1) = 1<<(exp+1) */ +/* 2^(frac-1) = 2^z, z in [-1; 0[ use table look-up */ +/* and interpolation. */ +/****************************************************/ + +Word32 Pow2( /* Q0 output */ + Word16 int_comp, /* Q0 Integer part */ + Word16 frac_comp /* Q15 frac_compal part */ + ) +{ + Word32 a0; + Word16 idx_frac, frac, sub_frac, sub_tab; + + idx_frac = shr(frac_comp, 9); // for 65 entry table + sub_frac = frac_comp & 0x01FF; // sub-frac_comp in Q9 + sub_frac = shl(sub_frac, 6); // Q15 + frac = tabpow[idx_frac]; // Q15 table entry of 2^(frac_comp-1) + a0 = L_deposit_h(frac); // Q31 table entry of 2^(frac_comp-1) + sub_tab = sub(tabpow[idx_frac+1], frac); // Q15 difference to next table entry + a0 = L_mac(a0, sub_frac, sub_tab); // Q31 linear interpolation between table entries + + a0 = L_shr_r(a0, sub(30, int_comp)); // Q0 - note: sub(30, int_comp) = 31-(int_comp+1) + + return a0; +} + +/***********************************************************/ +/* y = log2(x) = log2(man*2^exp) */ +/* = log2(man) + log2(2^exp) */ +/* = log2(man) + exp */ +/* */ +/* exponent = 30-exp */ +/* fraction = table look-up and interpolation of log2(man) */ +/***********************************************************/ + +void Log2( + Word32 x, /* (i) input */ + Word16 *int_comp, /* Q0 integer part */ + Word16 *frac_comp /* Q15 fractional part */ + ) +{ + Word16 exp, idx_man, sub_man, sub_tab; + Word32 a0; + + if(x <= 0){ + *int_comp = 0; + *frac_comp = 0; + } + else{ + exp = norm_l(x); // normalization + a0 = L_shl(x, exp); // Q30 mantissa, i.e. 1.xxx Q30 + + /* use table look-up of man in [1.0, 2.0[ Q30 */ + a0 = L_shr(L_sub(a0, (Word32)0x40000000), 8); // Q16 index into table - note zero'ing of leading 1 + idx_man = extract_h(a0); // Q0 index into table + sub_man = extract_l(L_shr((a0 & 0xFFFF), 1)); // Q15 fractional sub_man + a0 = L_deposit_h(tablog[idx_man]); // Q31 + sub_tab = sub(tablog[idx_man+1], tablog[idx_man]); // Q15 + a0 = L_mac(a0, sub_man, sub_tab); // Q31 + + *frac_comp = intround(a0); // Q15 + *int_comp = sub(30, exp); // Q0 + } + + return; +} + +/*******************************************/ +/* y = sqrt(x) */ +/* table look-up with linear interpolation */ +/*******************************************/ + +Word16 sqrts(Word16 x) +{ + Word16 xb, y, exp, idx, sub_frac, sub_tab; + Word32 a0; + + if(x <= 0){ + y = 0; + } + else{ + exp = norm_s(x); + + /* use 65-entry table */ + xb = shl(x, exp); // normalization of x + idx = shr(xb, 9); // for 65 entry table + a0 = L_deposit_h(tabsqrt[idx]); // Q31 table look-up value + sub_frac = shl((Word16)(xb & 0x01FF), 6); // Q15 sub-fraction + sub_tab = sub(tabsqrt[idx+1], tabsqrt[idx]); // Q15 table interval for interpolation + a0 = L_mac(a0, sub_frac, sub_tab); // Q31 linear interpolation between table entries + if(exp & 0x0001){ + exp = shr(add(exp, 1), 1); // normalization of sqrt() + a0 = L_shr(a0, exp); + y = intround(a0); // Q15 + a0 = L_mac(a0, 13573, y); // Q31 incorporate the missing "/sqrt(2)" + } + else{ + exp = shr(exp, 1); // normalization of sqrt() + a0 = L_shr(a0, exp); // Q31 + } + y = intround(a0); // Q15 + } + + return y; +} + +/****************************************************/ +/* y = 1/sqrt(x) */ +/* table look-up for sqrt with linear interpolation */ +/* use div_s for inverse */ +/****************************************************/ + +void sqrt_i(Word16 x_man, Word16 x_exp, Word16 *y_man, Word16 *y_exp) +{ + Word16 x_manb, x_expb, y, exp, idx, sub_frac, sub_tab; + Word32 a0; + + if(x_man <= 0){ + *y_man = 0; + *y_exp = 0; + } + else{ + + exp = norm_s(x_man); + x_manb = shl(x_man, exp); // normalize to Q15 in [0; 1] for table look-up + x_expb = add(x_exp, exp); // update exponent for x (left-shifting by additionl exp) + x_expb = sub(x_expb, 15); // we need to take sqrt of 0-32767 number but table is for 0-1 + idx = shr(x_manb, 9); // for 65 entry table + a0 = L_deposit_h(tabsqrt[idx]); // Q31 table look-up value + sub_frac = shl((Word16)(x_manb & 0x01FF), 6);// Q15 sub-fraction + sub_tab = sub(tabsqrt[idx+1], tabsqrt[idx]); // Q15 table interval for interpolation + a0 = L_mac(a0, sub_frac, sub_tab); // Q31 linear interpolation between table entries + exp = norm_l(a0); // exponent of a0 + y = intround(L_shl(a0,exp)); // normalize sqrt-root and drop 16 LBSs + exp = add(15, exp); // exponent of a0 taking Q15 of y into account + + if(x_expb & 0x0001){ + if(y < 0x5A82){ // 1*sqrt(2) in Q14) - with div_s(y1, y2), y2 must be >= y1 + exp = add(exp, shr(add(x_expb,1), 1)); // normalization for sqrt() + *y_man = div_s(0x2D41, y); // 0x2D41 is 1/sqrt(2) in Q14 + } + else{ + exp = add(exp, shr(sub(x_expb,1), 1)); // normalization for sqrt() + *y_man = div_s(0x5A82, y); // 0x5A82 is 1*sqrt(2) in Q14 + } + *y_exp = sub(29, exp); // ...and div_s returns fraction divide in Q15 + } + else{ + exp = add(exp, shr(x_expb, 1)); // normalization for sqrt() + *y_man = div_s(0x4000, y); + *y_exp = sub(29, exp); // 0x4000 is 1 in Q14 and div_s returns fraction divide in Q15 + } + } + + return; +} diff --git a/jni/bx16_fixedp/bvcommon/mathutil.h b/app/src/main/jni/bx16_fixedp/bvcommon/mathutil.h similarity index 97% rename from jni/bx16_fixedp/bvcommon/mathutil.h rename to app/src/main/jni/bx16_fixedp/bvcommon/mathutil.h index 870974b..edd10ee 100644 --- a/jni/bx16_fixedp/bvcommon/mathutil.h +++ b/app/src/main/jni/bx16_fixedp/bvcommon/mathutil.h @@ -1,46 +1,46 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - fixmath.h : Common Fixed-Point Library: - - $Log$ -******************************************************************************/ - -Word32 Pow2( /* Q0 output */ - Word16 int_comp, /* Q0 Integer part */ - Word16 frac_comp /* Q15 frac_compal part */ - ); - -void Log2( - Word32 x, /* (i) input */ - Word16 *int_comp, /* Q0 integer part */ - Word16 *frac_comp /* Q15 fractional part */ - ); - -void sqrt_i(Word16 x_man, Word16 x_exp, Word16 *y_man, Word16 *y_exp); -Word16 sqrts(Word16 x); - -extern Word16 tabsqrt[]; -extern Word16 tablog[]; -extern Word16 tabpow[]; -extern Word16 costable[]; -extern Word16 acosslope[]; +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + fixmath.h : Common Fixed-Point Library: + + $Log$ +******************************************************************************/ + +Word32 Pow2( /* Q0 output */ + Word16 int_comp, /* Q0 Integer part */ + Word16 frac_comp /* Q15 frac_compal part */ + ); + +void Log2( + Word32 x, /* (i) input */ + Word16 *int_comp, /* Q0 integer part */ + Word16 *frac_comp /* Q15 fractional part */ + ); + +void sqrt_i(Word16 x_man, Word16 x_exp, Word16 *y_man, Word16 *y_exp); +Word16 sqrts(Word16 x); + +extern Word16 tabsqrt[]; +extern Word16 tablog[]; +extern Word16 tabpow[]; +extern Word16 costable[]; +extern Word16 acosslope[]; diff --git a/jni/bx16_fixedp/bvcommon/memutil.c b/app/src/main/jni/bx16_fixedp/bvcommon/memutil.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/memutil.c rename to app/src/main/jni/bx16_fixedp/bvcommon/memutil.c index 1abd96e..213e78d 100644 --- a/jni/bx16_fixedp/bvcommon/memutil.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/memutil.c @@ -1,80 +1,80 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - memutil.c : Common Fixed-Point Library: memory utilities - - $Log$ -******************************************************************************/ - -#include -#include -#include "typedef.h" - -/* allocate a Word16 vector with subscript range v[nl...nh] */ -/* from numerical recipes in C 2nd edition, page 943 */ -Word16 *allocWord16(long nl, long nh) -{ - Word16 *v; - - v = (Word16 *)malloc((size_t)((nh-nl+1)*sizeof(Word16))); - if (!v){ - printf("Memory allocation error in allocWord16()\n"); - exit(0); - } - - return v-nl; -} - -/* free a Word16 vector allocated by svector() */ -/* from numerical recipes in C 2nd edition, page 946 */ -void deallocWord16(Word16 *v, long nl, long nh) -{ - - free((char *)(v+nl)); - - return; -} - -/* allocate a Word32 vector with subscript range v[nl...nh] */ -/* from numerical recipes in C 2nd edition, page 943 */ -Word32 *allocWord32(long nl, long nh) -{ - Word32 *v; - - v = (Word32 *)malloc((size_t)((nh-nl+1)*sizeof(Word32))); - if (!v){ - printf("Memory allocation error in allocWord32()\n"); - exit(0); - } - - return v-nl; -} - -/* free a Word32 vector allocated by svector() */ -/* from numerical recipes in C 2nd edition, page 946 */ -void deallocWord32(Word32 *v, long nl, long nh) -{ - - free((char *)(v+nl)); - - return; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + memutil.c : Common Fixed-Point Library: memory utilities + + $Log$ +******************************************************************************/ + +#include +#include +#include "typedef.h" + +/* allocate a Word16 vector with subscript range v[nl...nh] */ +/* from numerical recipes in C 2nd edition, page 943 */ +Word16 *allocWord16(long nl, long nh) +{ + Word16 *v; + + v = (Word16 *)malloc((size_t)((nh-nl+1)*sizeof(Word16))); + if (!v){ + printf("Memory allocation error in allocWord16()\n"); + exit(0); + } + + return v-nl; +} + +/* free a Word16 vector allocated by svector() */ +/* from numerical recipes in C 2nd edition, page 946 */ +void deallocWord16(Word16 *v, long nl, long nh) +{ + + free((char *)(v+nl)); + + return; +} + +/* allocate a Word32 vector with subscript range v[nl...nh] */ +/* from numerical recipes in C 2nd edition, page 943 */ +Word32 *allocWord32(long nl, long nh) +{ + Word32 *v; + + v = (Word32 *)malloc((size_t)((nh-nl+1)*sizeof(Word32))); + if (!v){ + printf("Memory allocation error in allocWord32()\n"); + exit(0); + } + + return v-nl; +} + +/* free a Word32 vector allocated by svector() */ +/* from numerical recipes in C 2nd edition, page 946 */ +void deallocWord32(Word32 *v, long nl, long nh) +{ + + free((char *)(v+nl)); + + return; +} diff --git a/jni/bx16_fixedp/bvcommon/memutil.h b/app/src/main/jni/bx16_fixedp/bvcommon/memutil.h similarity index 98% rename from jni/bx16_fixedp/bvcommon/memutil.h rename to app/src/main/jni/bx16_fixedp/bvcommon/memutil.h index 4c67f5f..01868fc 100644 --- a/jni/bx16_fixedp/bvcommon/memutil.h +++ b/app/src/main/jni/bx16_fixedp/bvcommon/memutil.h @@ -1,31 +1,31 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - memutil.h : Common Fixed-Point Library: memory utilities - - $Log$ -******************************************************************************/ - -Word16 *allocWord16(long nl, long nh); -void deallocWord16(Word16 *v, long nl, long nh); -Word32 *allocWord32(long nl, long nh); -void deallocWord32(Word32 *v, long nl, long nh); +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + memutil.h : Common Fixed-Point Library: memory utilities + + $Log$ +******************************************************************************/ + +Word16 *allocWord16(long nl, long nh); +void deallocWord16(Word16 *v, long nl, long nh); +Word32 *allocWord32(long nl, long nh); +void deallocWord32(Word32 *v, long nl, long nh); diff --git a/jni/bx16_fixedp/bvcommon/ptdec.c b/app/src/main/jni/bx16_fixedp/bvcommon/ptdec.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/ptdec.c rename to app/src/main/jni/bx16_fixedp/bvcommon/ptdec.c index 90c7ae8..c1ba357 100644 --- a/jni/bx16_fixedp/bvcommon/ptdec.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/ptdec.c @@ -1,40 +1,40 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - ptdec.c : Common Fixed-Point Library: - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" - -void pp3dec( - Word16 idx, - Word16 *b) -{ - Word16 *fp; - Word16 i; - fp = pp9cb+idx*9; - for (i=0;i<3;i++) - b[i] = *fp++; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + ptdec.c : Common Fixed-Point Library: + + $Log$ +******************************************************************************/ + +#include "typedef.h" +#include "bvcommon.h" + +void pp3dec( + Word16 idx, + Word16 *b) +{ + Word16 *fp; + Word16 i; + fp = pp9cb+idx*9; + for (i=0;i<3;i++) + b[i] = *fp++; +} diff --git a/jni/bx16_fixedp/bvcommon/stblzlsp.c b/app/src/main/jni/bx16_fixedp/bvcommon/stblzlsp.c similarity index 97% rename from jni/bx16_fixedp/bvcommon/stblzlsp.c rename to app/src/main/jni/bx16_fixedp/bvcommon/stblzlsp.c index 588fa31..a96334e 100644 --- a/jni/bx16_fixedp/bvcommon/stblzlsp.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/stblzlsp.c @@ -1,98 +1,98 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - stblzlsp.c : Common Fixed-Point Library: stabilize the lsp's - - $Log$ -******************************************************************************/ - -#include "typedef.h" -#include "bvcommon.h" -#include "basop32.h" - -void stblz_lsp( - Word16 *lsp, /* Q15 */ - Word16 order) -{ - - /* This function orders the lsp to prevent */ - /* unstable synthesis filters and imposes basic */ - /* lsp properties in order to avoid marginal */ - /* stability of the synthesis filter. */ - - Word16 k, i; - Word16 mintmp, maxtmp, a0; - - /* order lsps as minimum stability requirement */ - do { - k = 0; /* use k as a flag for order reversal */ - for (i = 0; i < order - 1; i++) { - if (lsp[i] > lsp[i+1]) { /* if there is an order reversal */ - a0 = lsp[i+1]; - lsp[i+1] = lsp[i]; /* swap the two LSP elements */ - lsp[i] = a0; - k = 1; /* set the flag for order reversal */ - } - } - } while (k > 0); /* repeat order checking if there was order reversal */ - - - /* impose basic lsp properties */ - maxtmp=sub(LSPMAX,(Word16)((order-1)*DLSPMIN)); - - if(lsp[0] < LSPMIN) lsp[0] = LSPMIN; - else if(lsp[0] > maxtmp) lsp[0] = maxtmp; - - for(i=0; i maxtmp) lsp[i+1] = maxtmp; - - } - - return; -} - -Word16 stblchck( - Word16 *x, - Word16 vdim) -{ - Word16 k, stbl; - - if (x[0] < 0) stbl = 0; - else - { - stbl = 1; - for (k=1; k lsp[i+1]) { /* if there is an order reversal */ + a0 = lsp[i+1]; + lsp[i+1] = lsp[i]; /* swap the two LSP elements */ + lsp[i] = a0; + k = 1; /* set the flag for order reversal */ + } + } + } while (k > 0); /* repeat order checking if there was order reversal */ + + + /* impose basic lsp properties */ + maxtmp=sub(LSPMAX,(Word16)((order-1)*DLSPMIN)); + + if(lsp[0] < LSPMIN) lsp[0] = LSPMIN; + else if(lsp[0] > maxtmp) lsp[0] = maxtmp; + + for(i=0; i maxtmp) lsp[i+1] = maxtmp; + + } + + return; +} + +Word16 stblchck( + Word16 *x, + Word16 vdim) +{ + Word16 k, stbl; + + if (x[0] < 0) stbl = 0; + else + { + stbl = 1; + for (k=1; k0) *y++ = *x++; -} - -void W16copy(Word16 *y, Word16 *x, int size) -{ - while ((size--)>0) *y++ = *x++; -} - -void W16zero(Word16 *x, int size) -{ - while ((size--)>0) *x++ = 0; -} - -void W32copy(Word32 *y, Word32 *x, int size) -{ - while ((size--)>0) *y++ = *x++; -} - +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + utility.c : Common Fixed-Point Library: memory utilities + + $Log$ +******************************************************************************/ + +#include "typedef.h" + +void UW8copy(UWord8 *y, UWord8 *x, int size) +{ + while ((size--)>0) *y++ = *x++; +} + +void W16copy(Word16 *y, Word16 *x, int size) +{ + while ((size--)>0) *y++ = *x++; +} + +void W16zero(Word16 *x, int size) +{ + while ((size--)>0) *x++ = 0; +} + +void W32copy(Word32 *y, Word32 *x, int size) +{ + while ((size--)>0) *y++ = *x++; +} + diff --git a/jni/bx16_fixedp/bvcommon/utility.h b/app/src/main/jni/bx16_fixedp/bvcommon/utility.h similarity index 98% rename from jni/bx16_fixedp/bvcommon/utility.h rename to app/src/main/jni/bx16_fixedp/bvcommon/utility.h index be8dc71..31680b3 100644 --- a/jni/bx16_fixedp/bvcommon/utility.h +++ b/app/src/main/jni/bx16_fixedp/bvcommon/utility.h @@ -1,32 +1,32 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - utility.h : Common Fixed-Point Library: - - $Log$ -******************************************************************************/ - -void UW8copy(UWord8 *y, UWord8 *x, int size); -void W16copy(Word16 *y, Word16 *x, int size); -void W16zero(Word16 *x, int size); -void W32copy(Word32 *y, Word32 *x, int size); - +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + utility.h : Common Fixed-Point Library: + + $Log$ +******************************************************************************/ + +void UW8copy(UWord8 *y, UWord8 *x, int size); +void W16copy(Word16 *y, Word16 *x, int size); +void W16zero(Word16 *x, int size); +void W32copy(Word32 *y, Word32 *x, int size); + diff --git a/jni/bx16_fixedp/bvcommon/vqdecode.c b/app/src/main/jni/bx16_fixedp/bvcommon/vqdecode.c similarity index 98% rename from jni/bx16_fixedp/bvcommon/vqdecode.c rename to app/src/main/jni/bx16_fixedp/bvcommon/vqdecode.c index b036b20..815ff31 100644 --- a/jni/bx16_fixedp/bvcommon/vqdecode.c +++ b/app/src/main/jni/bx16_fixedp/bvcommon/vqdecode.c @@ -1,41 +1,41 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - - -/***************************************************************************** - vqdecode.c : Common Fixed-Point Library: - - $Log$ -******************************************************************************/ - -#include "typedef.h" - -void vqdec( - Word16 *xq, /* VQ output vector (quantized version of input vector) */ - Word16 idx, /* VQ codebook index for the nearest neighbor */ - Word16 *cb, /* VQ codebook */ - Word16 vdim) /* vector dimension */ -{ - - Word16 j, k; - j = idx * vdim; - for (k = 0; k < vdim; k++) - xq[k] = cb[j + k]; -} +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + + +/***************************************************************************** + vqdecode.c : Common Fixed-Point Library: + + $Log$ +******************************************************************************/ + +#include "typedef.h" + +void vqdec( + Word16 *xq, /* VQ output vector (quantized version of input vector) */ + Word16 idx, /* VQ codebook index for the nearest neighbor */ + Word16 *cb, /* VQ codebook */ + Word16 vdim) /* vector dimension */ +{ + + Word16 j, k; + j = idx * vdim; + for (k = 0; k < vdim; k++) + xq[k] = cb[j + k]; +} diff --git a/jni/bx16_fixedp/itug191lib/basop32.c b/app/src/main/jni/bx16_fixedp/itug191lib/basop32.c similarity index 98% rename from jni/bx16_fixedp/itug191lib/basop32.c rename to app/src/main/jni/bx16_fixedp/itug191lib/basop32.c index 1ec8a61..3187f66 100644 --- a/jni/bx16_fixedp/itug191lib/basop32.c +++ b/app/src/main/jni/bx16_fixedp/itug191lib/basop32.c @@ -1,2604 +1,2604 @@ -/* v.2.0 - 15.Nov.2004 - ============================================================================= - - U U GGG SSSS TTTTT - U U G S T - U U G GG SSSS T - U U G G S T - UUU GG SSS T - - ======================================== - ITU-T - USER'S GROUP ON SOFTWARE TOOLS - ======================================== - - ============================================================= - COPYRIGHT NOTE: This source code, and all of its derivations, - is subject to the "ITU-T General Public License". Please have - it read in the distribution disk, or in the ITU-T - Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO - CODING STANDARDS". - ============================================================= - -MODULE: BASOP32, BASIC OPERATORS - -ORIGINAL BY: - Incorporated from anonymous contributions for - ETSI Standards as well as G.723.1, G.729, and G.722.1 - -DESCRIPTION: - This file contains the definition of 16- and 32-bit basic - operators to be used in the implementation of signal - processing algorithms. The basic operators try to resemble - assembly language instructions that are commonly found in - digital signal processor (DSP) CPUs, thus allowing algorithm - C-code implementations more directly mapeable to DSP assembly - code. - - ********************************************************* - NOTE: so far, this module does not have a demo program! - ********************************************************* - -FUNCTIONS: - Defined in basop32.h. Self-documentation within each function. - -HISTORY: - 26.Jan.00 v1.0 Incorporated to the STL from updated G.723.1/G.729 - basic operator library (based on basicop2.c) and - G.723.1's basop.c [L_mls(), div_l(), i_mult()] - - 05.Jul.00 v1.1 Added 32-bit shiftless accumulation basic - operators (L_msu0, L_mac0, L_mult0). Improved - documentation for i_mult(). - - 03 Nov 04 v2.0 Incorporation of new 32-bit / 40-bit / control - operators for the ITU-T Standard Tool Library as - described in Geneva, 20-30 January 2004 WP 3/16 Q10/16 - TD 11 document and subsequent discussions on the - wp3audio@yahoogroups.com email reflector. - norm_s() weight reduced from 15 to 1. - norm_l() weight reduced from 30 to 1. - L_abs() weight reduced from 2 to 1. - L_add() weight reduced from 2 to 1. - L_negate() weight reduced from 2 to 1. - L_shl() weight reduced from 2 to 1. - L_shr() weight reduced from 2 to 1. - L_sub() weight reduced from 2 to 1. - mac_r() weight reduced from 2 to 1. - msu_r() weight reduced from 2 to 1. - mult_r() weight reduced from 2 to 1. - L_deposit_h() weight reduced from 2 to 1. - L_deposit_l() weight reduced from 2 to 1. - 15 Nov 04 v2.0 L_mls() weight of 5. - div_l() weight of 32. - i_mult() weight of 3. - - ============================================================================= -*/ - - -/*___________________________________________________________________________ - | | - | Basic arithmetic operators. | - | | - | $Id $ | - | | - | saturate() | - | add() | - | sub() | - | abs_s() | - | divide_s() | - | extract_h() | - | extract_l() | - | L_abs() | - | L_add() | - | L_deposit_h() | - | L_deposit_l() | - | L_mac() | - | L_msu() | - | L_mult() | - | L_negate() | - | L_shl() | - | L_shr() | - | L_sub() | - | mac_r() | - | msu_r() | - | mult() | - | mult_r() | - | negate() | - | norm_l() | - | norm_s() | - | round() | - | shl() | - | shr() | - |___________________________________________________________________________| -*/ - - -/*___________________________________________________________________________ - | | - | Include-Files | - |___________________________________________________________________________| -*/ -#include -#include -#include "stl.h" - - -#if (WMOPS) -extern BASIC_OP multiCounter[MAXCOUNTERS]; -extern int currCounter; -#endif - - -/*___________________________________________________________________________ - | | - | Local Functions | - |___________________________________________________________________________| -*/ -Word16 saturate (Word32 L_var1); - - -/*___________________________________________________________________________ - | | - | Constants and Globals | - |___________________________________________________________________________| -*/ -Flag Overflow = 0; -Flag Carry = 0; - - -/*___________________________________________________________________________ - | | - | Functions | - |___________________________________________________________________________| -*/ - -/*___________________________________________________________________________ - | | - | Function Name : saturate | - | | - | Purpose : | - | | - | Limit the 32 bit input to the range of a 16 bit word. | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 saturate (Word32 L_var1) -{ - Word16 var_out; - - if (L_var1 > 0X00007fffL) - { - Overflow = 1; - var_out = MAX_16; - } - else if (L_var1 < (Word32) 0xffff8000L) - { - Overflow = 1; - var_out = MIN_16; - } - else - { - var_out = extract_l (L_var1); - -#if (WMOPS) - multiCounter[currCounter].extract_l--; -#endif - } - - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : add | - | | - | Purpose : | - | | - | Performs the addition (var1+var2) with overflow control and saturation;| - | the 16 bit result is set at +32767 when overflow occurs or at -32768 | - | when underflow occurs. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 add (Word16 var1, Word16 var2) -{ - Word16 var_out; - Word32 L_sum; - - L_sum = (Word32) var1 + var2; - var_out = saturate (L_sum); - -#if (WMOPS) - multiCounter[currCounter].add++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : sub | - | | - | Purpose : | - | | - | Performs the subtraction (var1+var2) with overflow control and satu- | - | ration; the 16 bit result is set at +32767 when overflow occurs or at | - | -32768 when underflow occurs. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 sub (Word16 var1, Word16 var2) -{ - Word16 var_out; - Word32 L_diff; - - L_diff = (Word32) var1 - var2; - var_out = saturate (L_diff); - -#if (WMOPS) - multiCounter[currCounter].sub++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : abs_s | - | | - | Purpose : | - | | - | Absolute value of var1; abs_s(-32768) = 32767. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 0000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 abs_s (Word16 var1) -{ - Word16 var_out; - - if (var1 == (Word16) MIN_16) - { - var_out = MAX_16; - } - else - { - if (var1 < 0) - { - var_out = -var1; - } - else - { - var_out = var1; - } - } - -#if (WMOPS) - multiCounter[currCounter].abs_s++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : shl | - | | - | Purpose : | - | | - | Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill| - | the var2 LSB of the result. If var2 is negative, arithmetically shift | - | var1 right by -var2 with sign extension. Saturate the result in case of | - | underflows or overflows. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 shl (Word16 var1, Word16 var2) -{ - Word16 var_out; - Word32 result; - - if (var2 < 0) - { - if (var2 < -16) - var2 = -16; - var2 = -var2; - var_out = shr (var1, var2); - -#if (WMOPS) - multiCounter[currCounter].shr--; -#endif - } - else - { - result = (Word32) var1 *((Word32) 1 << var2); - - if ((var2 > 15 && var1 != 0) || (result != (Word32) ((Word16) result))) - { - Overflow = 1; - var_out = (var1 > 0) ? MAX_16 : MIN_16; - } - else - { - var_out = extract_l (result); - -#if (WMOPS) - multiCounter[currCounter].extract_l--; -#endif - } - } - -#if (WMOPS) - multiCounter[currCounter].shl++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : shr | - | | - | Purpose : | - | | - | Arithmetically shift the 16 bit input var1 right var2 positions with | - | sign extension. If var2 is negative, arithmetically shift var1 left by | - | -var2 with sign extension. Saturate the result in case of underflows or | - | overflows. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 shr (Word16 var1, Word16 var2) -{ - Word16 var_out; - - if (var2 < 0) - { - if (var2 < -16) - var2 = -16; - var2 = -var2; - var_out = shl (var1, var2); - -#if (WMOPS) - multiCounter[currCounter].shl--; -#endif - } - else - { - if (var2 >= 15) - { - var_out = (var1 < 0) ? -1 : 0; - } - else - { - if (var1 < 0) - { - var_out = ~((~var1) >> var2); - } - else - { - var_out = var1 >> var2; - } - } - } - -#if (WMOPS) - multiCounter[currCounter].shr++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : mult | - | | - | Purpose : | - | | - | Performs the multiplication of var1 by var2 and gives a 16 bit result | - | which is scaled i.e.: | - | mult(var1,var2) = extract_l(L_shr((var1 times var2),15)) and | - | mult(-32768,-32768) = 32767. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 mult (Word16 var1, Word16 var2) -{ - Word16 var_out; - Word32 L_product; - - L_product = (Word32) var1 *(Word32) var2; - - L_product = (L_product & (Word32) 0xffff8000L) >> 15; - - if (L_product & (Word32) 0x00010000L) - L_product = L_product | (Word32) 0xffff0000L; - - var_out = saturate (L_product); - -#if (WMOPS) - multiCounter[currCounter].mult++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_mult | - | | - | Purpose : | - | | - | L_mult is the 32 bit result of the multiplication of var1 times var2 | - | with one shift left i.e.: | - | L_mult(var1,var2) = L_shl((var1 times var2),1) and | - | L_mult(-32768,-32768) = 2147483647. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_mult (Word16 var1, Word16 var2) -{ - Word32 L_var_out; - - L_var_out = (Word32) var1 *(Word32) var2; - - if (L_var_out != (Word32) 0x40000000L) - { - L_var_out *= 2; - } - else - { - Overflow = 1; - L_var_out = MAX_32; - } - -#if (WMOPS) - multiCounter[currCounter].L_mult++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : negate | - | | - | Purpose : | - | | - | Negate var1 with saturation, saturate in the case where input is -32768:| - | negate(var1) = sub(0,var1). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 negate (Word16 var1) -{ - Word16 var_out; - - var_out = (var1 == MIN_16) ? MAX_16 : -var1; - -#if (WMOPS) - multiCounter[currCounter].negate++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : extract_h | - | | - | Purpose : | - | | - | Return the 16 MSB of L_var1. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32 ) whose value falls in the | - | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 extract_h (Word32 L_var1) -{ - Word16 var_out; - - var_out = (Word16) (L_var1 >> 16); - -#if (WMOPS) - multiCounter[currCounter].extract_h++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : extract_l | - | | - | Purpose : | - | | - | Return the 16 LSB of L_var1. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32 ) whose value falls in the | - | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 extract_l (Word32 L_var1) -{ - Word16 var_out; - - var_out = (Word16) L_var1; - -#if (WMOPS) - multiCounter[currCounter].extract_l++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : round | - | | - | Purpose : | - | | - | Round the lower 16 bits of the 32 bit input number into the MS 16 bits | - | with saturation. Shift the resulting bits right by 16 and return the 16 | - | bit number: | - | round(L_var1) = extract_h(L_add(L_var1,32768)) | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32 ) whose value falls in the | - | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 intround (Word32 L_var1) -{ - Word16 var_out; - Word32 L_rounded; - - L_rounded = L_add (L_var1, (Word32) 0x00008000L); - var_out = extract_h (L_rounded); - -#if (WMOPS) - multiCounter[currCounter].L_add--; - multiCounter[currCounter].extract_h--; - multiCounter[currCounter].round++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_mac | - | | - | Purpose : | - | | - | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit | - | result to L_var3 with saturation, return a 32 bit result: | - | L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var3 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_mac (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word32 L_var_out; - Word32 L_product; - - L_product = L_mult (var1, var2); - L_var_out = L_add (L_var3, L_product); - -#if (WMOPS) - multiCounter[currCounter].L_mult--; - multiCounter[currCounter].L_add--; - multiCounter[currCounter].L_mac++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_msu | - | | - | Purpose : | - | | - | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 | - | bit result from L_var3 with saturation, return a 32 bit result: | - | L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var3 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_msu (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word32 L_var_out; - Word32 L_product; - - L_product = L_mult (var1, var2); - L_var_out = L_sub (L_var3, L_product); - -#if (WMOPS) - multiCounter[currCounter].L_mult--; - multiCounter[currCounter].L_sub--; - multiCounter[currCounter].L_msu++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_macNs | - | | - | Purpose : | - | | - | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit | - | result to L_var3 without saturation, return a 32 bit result. Generate | - | carry and overflow values : | - | L_macNs(L_var3,var1,var2) = L_add_c(L_var3,L_mult(var1,var2)). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var3 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - | | - | Caution : | - | | - | In some cases the Carry flag has to be cleared or set before using | - | operators which take into account its value. | - |___________________________________________________________________________| -*/ -Word32 L_macNs (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word32 L_var_out; - - L_var_out = L_mult (var1, var2); - L_var_out = L_add_c (L_var3, L_var_out); - -#if (WMOPS) - multiCounter[currCounter].L_mult--; - multiCounter[currCounter].L_add_c--; - multiCounter[currCounter].L_macNs++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_msuNs | - | | - | Purpose : | - | | - | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 | - | bit result from L_var3 without saturation, return a 32 bit result. Ge- | - | nerate carry and overflow values : | - | L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,L_mult(var1,var2)). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var3 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - | | - | Caution : | - | | - | In some cases the Carry flag has to be cleared or set before using | - | operators which take into account its value. | - |___________________________________________________________________________| -*/ -Word32 L_msuNs (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word32 L_var_out; - - L_var_out = L_mult (var1, var2); - L_var_out = L_sub_c (L_var3, L_var_out); - -#if (WMOPS) - multiCounter[currCounter].L_mult--; - multiCounter[currCounter].L_sub_c--; - multiCounter[currCounter].L_msuNs++; - -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_add | - | | - | Purpose : | - | | - | 32 bits addition of the two 32 bits variables (L_var1+L_var2) with | - | overflow control and saturation; the result is set at +2147483647 when | - | overflow occurs or at -2147483648 when underflow occurs. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | L_var2 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_add (Word32 L_var1, Word32 L_var2) -{ - Word32 L_var_out; - - L_var_out = L_var1 + L_var2; - - if (((L_var1 ^ L_var2) & MIN_32) == 0) - { - if ((L_var_out ^ L_var1) & MIN_32) - { - L_var_out = (L_var1 < 0) ? MIN_32 : MAX_32; - Overflow = 1; - } - } - -#if (WMOPS) - multiCounter[currCounter].L_add++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_sub | - | | - | Purpose : | - | | - | 32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with | - | overflow control and saturation; the result is set at +2147483647 when | - | overflow occurs or at -2147483648 when underflow occurs. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | L_var2 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_sub (Word32 L_var1, Word32 L_var2) -{ - Word32 L_var_out; - - L_var_out = L_var1 - L_var2; - - if (((L_var1 ^ L_var2) & MIN_32) != 0) - { - if ((L_var_out ^ L_var1) & MIN_32) - { - L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32; - Overflow = 1; - } - } - -#if (WMOPS) - multiCounter[currCounter].L_sub++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_add_c | - | | - | Purpose : | - | | - | Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)| - | with carry. No saturation. Generate carry and Overflow values. The car- | - | ry and overflow values are binary variables which can be tested and as- | - | signed values. | - | | - | Complexity weight : 2 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | L_var2 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - | | - | Caution : | - | | - | In some cases the Carry flag has to be cleared or set before using | - | operators which take into account its value. | - |___________________________________________________________________________| -*/ -Word32 L_add_c (Word32 L_var1, Word32 L_var2) -{ - Word32 L_var_out; - Word32 L_test; - Flag carry_int = 0; - - L_var_out = L_var1 + L_var2 + Carry; - - L_test = L_var1 + L_var2; - - if ((L_var1 > 0) && (L_var2 > 0) && (L_test < 0)) - { - Overflow = 1; - carry_int = 0; - } - else - { - if ((L_var1 < 0) && (L_var2 < 0)) - { - if (L_test >= 0) - { - Overflow = 1; - carry_int = 1; - } - else - { - Overflow = 0; - carry_int = 1; - } - } - else - { - if (((L_var1 ^ L_var2) < 0) && (L_test >= 0)) - { - Overflow = 0; - carry_int = 1; - } - else - { - Overflow = 0; - carry_int = 0; - } - } - } - - if (Carry) - { - if (L_test == MAX_32) - { - Overflow = 1; - Carry = carry_int; - } - else - { - if (L_test == (Word32) 0xFFFFFFFFL) - { - Carry = 1; - } - else - { - Carry = carry_int; - } - } - } - else - { - Carry = carry_int; - } - -#if (WMOPS) - multiCounter[currCounter].L_add_c++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_sub_c | - | | - | Purpose : | - | | - | Performs 32 bits subtraction of the two 32 bits variables with carry | - | (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow | - | values. The carry and overflow values are binary variables which can | - | be tested and assigned values. | - | | - | Complexity weight : 2 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | L_var2 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - | | - | Caution : | - | | - | In some cases the Carry flag has to be cleared or set before using | - | operators which take into account its value. | - |___________________________________________________________________________| -*/ -Word32 L_sub_c (Word32 L_var1, Word32 L_var2) -{ - Word32 L_var_out; - Word32 L_test; - Flag carry_int = 0; - - if (Carry) - { - Carry = 0; - if (L_var2 != MIN_32) - { - L_var_out = L_add_c (L_var1, -L_var2); -#if (WMOPS) - multiCounter[currCounter].L_add_c--; -#endif - } - else - { - L_var_out = L_var1 - L_var2; - if (L_var1 > 0L) - { - Overflow = 1; - Carry = 0; - } - } - } - else - { - L_var_out = L_var1 - L_var2 - (Word32) 0X00000001L; - L_test = L_var1 - L_var2; - - if ((L_test < 0) && (L_var1 > 0) && (L_var2 < 0)) - { - Overflow = 1; - carry_int = 0; - } - else if ((L_test > 0) && (L_var1 < 0) && (L_var2 > 0)) - { - Overflow = 1; - carry_int = 1; - } - else if ((L_test > 0) && ((L_var1 ^ L_var2) > 0)) - { - Overflow = 0; - carry_int = 1; - } - if (L_test == MIN_32) - { - Overflow = 1; - Carry = carry_int; - } - else - { - Carry = carry_int; - } - } - -#if (WMOPS) - multiCounter[currCounter].L_sub_c++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_negate | - | | - | Purpose : | - | | - | Negate the 32 bit variable L_var1 with saturation; saturate in the case | - | where input is -2147483648 (0x8000 0000). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_negate (Word32 L_var1) -{ - Word32 L_var_out; - - L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1; - -#if (WMOPS) - multiCounter[currCounter].L_negate++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : mult_r | - | | - | Purpose : | - | | - | Same as mult with rounding, i.e.: | - | mult_r(var1,var2) = extract_l(L_shr(((var1 * var2) + 16384),15)) and | - | mult_r(-32768,-32768) = 32767. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x8000 <= var_out <= 0x7fff. | - |___________________________________________________________________________| -*/ -Word16 mult_r (Word16 var1, Word16 var2) -{ - Word16 var_out; - Word32 L_product_arr; - - L_product_arr = (Word32) var1 *(Word32) var2; /* product */ - L_product_arr += (Word32) 0x00004000L; /* round */ - L_product_arr &= (Word32) 0xffff8000L; - L_product_arr >>= 15; /* shift */ - - if (L_product_arr & (Word32) 0x00010000L) /* sign extend when necessary */ - { - L_product_arr |= (Word32) 0xffff0000L; - } - var_out = saturate (L_product_arr); - -#if (WMOPS) - multiCounter[currCounter].mult_r++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_shl | - | | - | Purpose : | - | | - | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero | - | fill the var2 LSB of the result. If var2 is negative, arithmetically | - | shift L_var1 right by -var2 with sign extension. Saturate the result in | - | case of underflows or overflows. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_shl (Word32 L_var1, Word16 var2) -{ - - Word32 L_var_out = 0L; - - if (var2 <= 0) - { - if (var2 < -32) - var2 = -32; - var2 = -var2; - L_var_out = L_shr (L_var1, var2); - #if (WMOPS) - multiCounter[currCounter].L_shr--; - #endif - } - else - { - for (; var2 > 0; var2--) - { - if (L_var1 > (Word32) 0X3fffffffL) - { - Overflow = 1; - L_var_out = MAX_32; - break; - } - else - { - if (L_var1 < (Word32) 0xc0000000L) - { - Overflow = 1; - L_var_out = MIN_32; - break; - } - } - L_var1 *= 2; - L_var_out = L_var1; - } - } - #if (WMOPS) - multiCounter[currCounter].L_shl++; - #endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_shr | - | | - | Purpose : | - | | - | Arithmetically shift the 32 bit input L_var1 right var2 positions with | - | sign extension. If var2 is negative, arithmetically shift L_var1 left | - | by -var2 and zero fill the -var2 LSB of the result. Saturate the result | - | in case of underflows or overflows. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_shr (Word32 L_var1, Word16 var2) -{ - Word32 L_var_out; - - if (var2 < 0) - { - if (var2 < -32) - var2 = -32; - var2 = -var2; - L_var_out = L_shl (L_var1, var2); - #if (WMOPS) - multiCounter[currCounter].L_shl--; - #endif - } - else - { - if (var2 >= 31) - { - L_var_out = (L_var1 < 0L) ? -1 : 0; - } - else - { - if (L_var1 < 0) - { - L_var_out = ~((~L_var1) >> var2); - } - else - { - L_var_out = L_var1 >> var2; - } - } - } - #if (WMOPS) - multiCounter[currCounter].L_shr++; - #endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : shr_r | - | | - | Purpose : | - | | - | Same as shr(var1,var2) but with rounding. Saturate the result in case of| - | underflows or overflows : | - | - If var2 is greater than zero : | - | if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1)))) | - | is equal to zero | - | then | - | shr_r(var1,var2) = shr(var1,var2) | - | else | - | shr_r(var1,var2) = add(shr(var1,var2),1) | - | - If var2 is less than or equal to zero : | - | shr_r(var1,var2) = shr(var1,var2). | - | | - | Complexity weight : 3 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 shr_r (Word16 var1, Word16 var2) -{ - Word16 var_out; - - if (var2 > 15) - { - var_out = 0; - } - else - { - var_out = shr (var1, var2); - -#if (WMOPS) - multiCounter[currCounter].shr--; -#endif - - if (var2 > 0) - { - if ((var1 & ((Word16) 1 << (var2 - 1))) != 0) - { - var_out++; - } - } - } - -#if (WMOPS) - multiCounter[currCounter].shr_r++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : mac_r | - | | - | Purpose : | - | | - | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit | - | result to L_var3 with saturation. Round the LS 16 bits of the result | - | into the MS 16 bits with saturation and shift the result right by 16. | - | Return a 16 bit result. | - | mac_r(L_var3,var1,var2) = round(L_mac(L_var3,var1,var2)) | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var3 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 mac_r (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word16 var_out; - - L_var3 = L_mac (L_var3, var1, var2); - L_var3 = L_add (L_var3, (Word32) 0x00008000L); - var_out = extract_h (L_var3); - -#if (WMOPS) - multiCounter[currCounter].L_mac--; - multiCounter[currCounter].L_add--; - multiCounter[currCounter].extract_h--; - multiCounter[currCounter].mac_r++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : msu_r | - | | - | Purpose : | - | | - | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 | - | bit result from L_var3 with saturation. Round the LS 16 bits of the res-| - | ult into the MS 16 bits with saturation and shift the result right by | - | 16. Return a 16 bit result. | - | msu_r(L_var3,var1,var2) = round(L_msu(L_var3,var1,var2)) | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var3 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word16 msu_r (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word16 var_out; - - L_var3 = L_msu (L_var3, var1, var2); - L_var3 = L_add (L_var3, (Word32) 0x00008000L); - var_out = extract_h (L_var3); - -#if (WMOPS) - multiCounter[currCounter].L_msu--; - multiCounter[currCounter].L_add--; - multiCounter[currCounter].extract_h--; - multiCounter[currCounter].msu_r++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_deposit_h | - | | - | Purpose : | - | | - | Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The | - | 16 LS bits of the output are zeroed. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var_out <= 0x7fff 0000. | - |___________________________________________________________________________| -*/ -Word32 L_deposit_h (Word16 var1) -{ - Word32 L_var_out; - - L_var_out = (Word32) var1 << 16; - -#if (WMOPS) - multiCounter[currCounter].L_deposit_h++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_deposit_l | - | | - | Purpose : | - | | - | Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The | - | 16 MS bits of the output are sign extended. | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0xFFFF 8000 <= var_out <= 0x0000 7fff. | - |___________________________________________________________________________| -*/ -Word32 L_deposit_l (Word16 var1) -{ - Word32 L_var_out; - - L_var_out = (Word32) var1; - -#if (WMOPS) - multiCounter[currCounter].L_deposit_l++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_shr_r | - | | - | Purpose : | - | | - | Same as L_shr(L_var1,var2) but with rounding. Saturate the result in | - | case of underflows or overflows : | - | - If var2 is greater than zero : | - | if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))| - | is equal to zero | - | then | - | L_shr_r(L_var1,var2) = L_shr(L_var1,var2) | - | else | - | L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1) | - | - If var2 is less than or equal to zero : | - | L_shr_r(L_var1,var2) = L_shr(L_var1,var2). | - | | - | Complexity weight : 3 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_shr_r (Word32 L_var1, Word16 var2) -{ - Word32 L_var_out; - - if (var2 > 31) - { - L_var_out = 0; - } - else - { - L_var_out = L_shr (L_var1, var2); - -#if (WMOPS) - multiCounter[currCounter].L_shr--; -#endif - if (var2 > 0) - { - if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0) - { - L_var_out++; - } - } - } - -#if (WMOPS) - multiCounter[currCounter].L_shr_r++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_abs | - | | - | Purpose : | - | | - | Absolute value of L_var1; Saturate in case where the input is | - | -214783648 | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x0000 0000 <= var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_abs (Word32 L_var1) -{ - Word32 L_var_out; - - if (L_var1 == MIN_32) - { - L_var_out = MAX_32; - } - else - { - if (L_var1 < 0) - { - L_var_out = -L_var1; - } - else - { - L_var_out = L_var1; - } - } - -#if (WMOPS) - multiCounter[currCounter].L_abs++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : L_sat | - | | - | Purpose : | - | | - | 32 bit L_var1 is set to 2147483647 if an overflow occured or to | - | -2147483648 if an underflow occured on the most recent L_add_c, | - | L_sub_c, L_macNs or L_msuNs operations. The carry and overflow values | - | are binary values which can be tested and assigned values. | - | | - | Complexity weight : 4 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | L_var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var_out <= 0x7fff ffff. | - |___________________________________________________________________________| -*/ -Word32 L_sat (Word32 L_var1) -{ - Word32 L_var_out; - - L_var_out = L_var1; - - if (Overflow) - { - - if (Carry) - { - L_var_out = MIN_32; - } - else - { - L_var_out = MAX_32; - } - - Carry = 0; - Overflow = 0; - } - -#if (WMOPS) - multiCounter[currCounter].L_sat++; -#endif - return (L_var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : norm_s | - | | - | Purpose : | - | | - | Produces the number of left shift needed to normalize the 16 bit varia- | - | ble var1 for positive values on the interval with minimum of 16384 and | - | maximum of 32767, and for negative values on the interval with minimum | - | of -32768 and maximum of -16384; in order to normalize the result, the | - | following operation must be done : | - | norm_var1 = shl(var1,norm_s(var1)). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 0000 <= var_out <= 0x0000 000f. | - |___________________________________________________________________________| -*/ -Word16 norm_s (Word16 var1) -{ - Word16 var_out; - - if (var1 == 0) - { - var_out = 0; - } - else - { - if (var1 == (Word16) 0xffff) - { - var_out = 15; - } - else - { - if (var1 < 0) - { - var1 = ~var1; - } - for (var_out = 0; var1 < 0x4000; var_out++) - { - var1 <<= 1; - } - } - } - -#if (WMOPS) - multiCounter[currCounter].norm_s++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : div_s | - | | - | Purpose : | - | | - | Produces a result which is the fractional integer division of var1 by | - | var2; var1 and var2 must be positive and var2 must be greater or equal | - | to var1; the result is positive (leading bit equal to 0) and truncated | - | to 16 bits. | - | If var1 = var2 then div(var1,var2) = 32767. | - | | - | Complexity weight : 18 | - | | - | Inputs : | - | | - | var1 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 0000 <= var1 <= var2 and var2 != 0. | - | | - | var2 | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : var1 <= var2 <= 0x0000 7fff and var2 != 0. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 0000 <= var_out <= 0x0000 7fff. | - | It's a Q15 value (point between b15 and b14). | - |___________________________________________________________________________| -*/ -Word16 div_s (Word16 var1, Word16 var2) -{ - Word16 var_out = 0; - Word16 iteration; - Word32 L_num; - Word32 L_denom; - - if ((var1 > var2) || (var1 < 0) || (var2 < 0)) - { - printf ("Division Error var1=%d var2=%d\n", var1, var2); - abort(); /* exit (0); */ - } - if (var2 == 0) - { - printf ("Division by 0, Fatal error \n"); - abort(); /* exit (0); */ - } - if (var1 == 0) - { - var_out = 0; - } - else - { - if (var1 == var2) - { - var_out = MAX_16; - } - else - { - L_num = L_deposit_l (var1); - L_denom = L_deposit_l (var2); - -#if (WMOPS) - multiCounter[currCounter].L_deposit_l--; - multiCounter[currCounter].L_deposit_l--; -#endif - - for (iteration = 0; iteration < 15; iteration++) - { - var_out <<= 1; - L_num <<= 1; - - if (L_num >= L_denom) - { - L_num = L_sub (L_num, L_denom); - var_out = add (var_out, 1); -#if (WMOPS) - multiCounter[currCounter].L_sub--; - multiCounter[currCounter].add--; -#endif - } - } - } - } - -#if (WMOPS) - multiCounter[currCounter].div_s++; -#endif - return (var_out); -} - - -/*___________________________________________________________________________ - | | - | Function Name : norm_l | - | | - | Purpose : | - | | - | Produces the number of left shifts needed to normalize the 32 bit varia-| - | ble L_var1 for positive values on the interval with minimum of | - | 1073741824 and maximum of 2147483647, and for negative values on the in-| - | terval with minimum of -2147483648 and maximum of -1073741824; in order | - | to normalize the result, the following operation must be done : | - | norm_L_var1 = L_shl(L_var1,norm_l(L_var1)). | - | | - | Complexity weight : 1 | - | | - | Inputs : | - | | - | L_var1 | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x0000 0000 <= var_out <= 0x0000 001f. | - |___________________________________________________________________________| -*/ -Word16 norm_l (Word32 L_var1) -{ - Word16 var_out; - - if (L_var1 == 0) - { - var_out = 0; - } - else - { - if (L_var1 == (Word32) 0xffffffffL) - { - var_out = 31; - } - else - { - if (L_var1 < 0) - { - L_var1 = ~L_var1; - } - for (var_out = 0; L_var1 < (Word32) 0x40000000L; var_out++) - { - L_var1 <<= 1; - } - } - } - -#if (WMOPS) - multiCounter[currCounter].norm_l++; -#endif - return (var_out); -} - -/* - ****************************************************************************** - * Additional operators extracted from the G.723.1 Library - * Adapted for WMOPS calculations - ****************************************************************************** -*/ - -/*___________________________________________________________________________ - | | - | Function Name : L_mls | - | | - | Purpose : | - | | - | Multiplies a 16 bit word v by a 32 bit word Lv and returns a 32 bit | - | word (multiplying 16 by 32 bit words gives 48 bit word; the function | - | extracts the 32 MSB and shift the result to the left by 1). | - | | - | A 32 bit word can be written as | - | Lv = a + b * 2^16 | - | where a= unsigned 16 LSBs and b= signed 16 MSBs. | - | The function returns v * Lv / 2^15 which is equivalent to | - | a*v / 2^15 + b*v*2 | - | | - | Complexity weight : 5 | - | | - | Inputs : | - | | - | Lv | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | - | v | - | 16 bit short signed integer (Word16) whose value falls in the | - | range : 0x8000 <= var1 <= 0x7fff. | - | | - | Outputs : | - | | - | none | - | | - | Return Value : | - | | - | var_out | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= var_out <= 0x7fff ffff. | - | | - |___________________________________________________________________________| -*/ -Word32 L_mls (Word32 Lv, Word16 v) -{ - Word32 Temp ; - - Temp = Lv & (Word32) 0x0000ffff ; - Temp = Temp * (Word32) v ; - Temp = L_shr( Temp, (Word16) 15 ) ; - Temp = L_mac( Temp, v, extract_h(Lv) ) ; - -#if (WMOPS) - multiCounter[currCounter].L_shr--; - multiCounter[currCounter].L_mac--; - multiCounter[currCounter].extract_h--; - multiCounter[currCounter].L_mls++; -#endif - - return Temp ; -} - - -/*__________________________________________________________________________ -| | -| Function Name : div_l | -| | -| Purpose : | -| | -| Produces a result which is the fractional integer division of L_var1 by | -| var2; L_var1 and var2 must be positive and var2 << 16 must be greater or| -| equal to L_var1; the result is positive (leading bit equal to 0) and | -| truncated to 16 bits. | -| If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767. | -| | -| Complexity weight : 32 | -| | -| Inputs : | -| | -| L_var1 | -| 32 bit long signed integer (Word32) whose value falls in the | -| range : 0x0000 0000 <= var1 <= (var2 << 16) and var2 != 0. | -| L_var1 must be considered as a Q.31 value | -| | -| var2 | -| 16 bit short signed integer (Word16) whose value falls in the | -| range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0. | -| var2 must be considered as a Q.15 value | -| | -| Outputs : | -| | -| none | -| | -| Return Value : | -| | -| var_out | -| 16 bit short signed integer (Word16) whose value falls in the | -| range : 0x0000 0000 <= var_out <= 0x0000 7fff. | -| It's a Q15 value (point between b15 and b14). | -|___________________________________________________________________________| -*/ -Word16 div_l (Word32 L_num, Word16 den) -{ - Word16 var_out = (Word16)0; - Word32 L_den; - Word16 iteration; - -#if (WMOPS) - multiCounter[currCounter].div_l++; -#endif - - if ( den == (Word16) 0 ) { - printf("Division by 0 in div_l, Fatal error \n"); - exit(0); - } - - if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) { - printf("Division Error in div_l, Fatal error \n"); - exit(0); - } - - L_den = L_deposit_h( den ) ; -#if (WMOPS) - multiCounter[currCounter].L_deposit_h--; -#endif - - if ( L_num >= L_den ){ - return MAX_16 ; - } - else { - L_num = L_shr(L_num, (Word16)1) ; - L_den = L_shr(L_den, (Word16)1); -#if (WMOPS) - multiCounter[currCounter].L_shr-=2; -#endif - for(iteration=(Word16)0; iteration< (Word16)15;iteration++) { - var_out = shl( var_out, (Word16)1); - L_num = L_shl( L_num, (Word16)1); -#if (WMOPS) - multiCounter[currCounter].shl--; - multiCounter[currCounter].L_shl--; -#endif - if (L_num >= L_den) { - L_num = L_sub(L_num,L_den); - var_out = add(var_out, (Word16)1); -#if (WMOPS) - multiCounter[currCounter].L_sub--; - multiCounter[currCounter].add--; -#endif - } - } - - return var_out; - } -} - - -/*__________________________________________________________________________ -| | -| Function Name : i_mult | -| | -| Purpose : | -| | -| Integer 16-bit multiplication. No overflow protection is performed if | -| ORIGINAL_G7231 is defined. | -| | -| Complexity weight : 3 | -| | -| Inputs : | -| | -| a | -| 16 bit short signed integer (Word16). | -| | -| b | -| 16 bit short signed integer (Word16). | -| | -| Outputs : | -| | -| none | -| | -| Return Value : | -| | -| 16 bit short signed integer (Word16). No overflow checks | -| are performed if ORIGINAL_G7231 is defined. | -|___________________________________________________________________________| -*/ -Word16 i_mult (Word16 a, Word16 b) -{ -#ifdef ORIGINAL_G7231 - return a*b ; -#else - Word32 register c=a*b; -#if (WMOPS) - multiCounter[currCounter].i_mult++; -#endif - return saturate(c) ; -#endif -} - - -/* - ****************************************************************************** - * The following three operators are not part of the original - * G.729/G.723.1 set of basic operators and implement shiftless - * accumulation operation. - ****************************************************************************** -*/ - -/*___________________________________________________________________________ - | - | Function Name : L_mult0 - | - | Purpose : - | - | L_mult0 is the 32 bit result of the multiplication of var1 times var2 - | without one left shift. - | - | Complexity weight : 1 - | - | Inputs : - | - | var1 16 bit short signed integer (Word16) whose value falls in the - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. - | - | var2 16 bit short signed integer (Word16) whose value falls in the - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. - | - | Return Value : - | - | L_var_out - | 32 bit long signed integer (Word32) whose value falls in the - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. - |___________________________________________________________________________ -*/ -Word32 L_mult0 (Word16 var1,Word16 var2) -{ - Word32 L_var_out; - - L_var_out = (Word32)var1 * (Word32)var2; - -#if (WMOPS) - multiCounter[currCounter].L_mult0++; -#endif - return(L_var_out); -} - - -/*___________________________________________________________________________ - | - | Function Name : L_mac0 - | - | Purpose : - | - | Multiply var1 by var2 (without left shift) and add the 32 bit result to - | L_var3 with saturation, return a 32 bit result: - | L_mac0(L_var3,var1,var2) = L_add(L_var3,(L_mult0(var1,var2)). - | - | Complexity weight : 1 - | - | Inputs : - | - | L_var3 32 bit long signed integer (Word32) whose value falls in the - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. - | - | var1 16 bit short signed integer (Word16) whose value falls in the - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. - | - | var2 16 bit short signed integer (Word16) whose value falls in the - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. - | - | Return Value : - | - | L_var_out - | 32 bit long signed integer (Word32) whose value falls in the - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. - |___________________________________________________________________________ -*/ -Word32 L_mac0 (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word32 L_var_out; - Word32 L_product; - - L_product = L_mult0(var1,var2); - L_var_out = L_add(L_var3,L_product); - -#if (WMOPS) - multiCounter[currCounter].L_mac0++; - multiCounter[currCounter].L_mult0--; - multiCounter[currCounter].L_add--; -#endif - return(L_var_out); -} - - -/*___________________________________________________________________________ - | - | Function Name : L_msu0 - | - | Purpose : - | - | Multiply var1 by var2 (without left shift) and subtract the 32 bit - | result to L_var3 with saturation, return a 32 bit result: - | L_msu0(L_var3,var1,var2) = L_sub(L_var3,(L_mult0(var1,var2)). - | - | Complexity weight : 1 - | - | Inputs : - | - | L_var3 32 bit long signed integer (Word32) whose value falls in the - | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. - | - | var1 16 bit short signed integer (Word16) whose value falls in the - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. - | - | var2 16 bit short signed integer (Word16) whose value falls in the - | range : 0xffff 8000 <= var1 <= 0x0000 7fff. - | - | Return Value : - | - | L_var_out - | 32 bit long signed integer (Word32) whose value falls in the - | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. - |___________________________________________________________________________ -*/ -Word32 L_msu0 (Word32 L_var3, Word16 var1, Word16 var2) -{ - Word32 L_var_out; - Word32 L_product; - - L_product = L_mult0(var1,var2); - L_var_out = L_sub(L_var3,L_product); - -#if (WMOPS) - multiCounter[currCounter].L_msu0++; - multiCounter[currCounter].L_mult0--; - multiCounter[currCounter].L_sub--; -#endif - return(L_var_out); -} - - -/* end of file */ +/* v.2.0 - 15.Nov.2004 + ============================================================================= + + U U GGG SSSS TTTTT + U U G S T + U U G GG SSSS T + U U G G S T + UUU GG SSS T + + ======================================== + ITU-T - USER'S GROUP ON SOFTWARE TOOLS + ======================================== + + ============================================================= + COPYRIGHT NOTE: This source code, and all of its derivations, + is subject to the "ITU-T General Public License". Please have + it read in the distribution disk, or in the ITU-T + Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO + CODING STANDARDS". + ============================================================= + +MODULE: BASOP32, BASIC OPERATORS + +ORIGINAL BY: + Incorporated from anonymous contributions for + ETSI Standards as well as G.723.1, G.729, and G.722.1 + +DESCRIPTION: + This file contains the definition of 16- and 32-bit basic + operators to be used in the implementation of signal + processing algorithms. The basic operators try to resemble + assembly language instructions that are commonly found in + digital signal processor (DSP) CPUs, thus allowing algorithm + C-code implementations more directly mapeable to DSP assembly + code. + + ********************************************************* + NOTE: so far, this module does not have a demo program! + ********************************************************* + +FUNCTIONS: + Defined in basop32.h. Self-documentation within each function. + +HISTORY: + 26.Jan.00 v1.0 Incorporated to the STL from updated G.723.1/G.729 + basic operator library (based on basicop2.c) and + G.723.1's basop.c [L_mls(), div_l(), i_mult()] + + 05.Jul.00 v1.1 Added 32-bit shiftless accumulation basic + operators (L_msu0, L_mac0, L_mult0). Improved + documentation for i_mult(). + + 03 Nov 04 v2.0 Incorporation of new 32-bit / 40-bit / control + operators for the ITU-T Standard Tool Library as + described in Geneva, 20-30 January 2004 WP 3/16 Q10/16 + TD 11 document and subsequent discussions on the + wp3audio@yahoogroups.com email reflector. + norm_s() weight reduced from 15 to 1. + norm_l() weight reduced from 30 to 1. + L_abs() weight reduced from 2 to 1. + L_add() weight reduced from 2 to 1. + L_negate() weight reduced from 2 to 1. + L_shl() weight reduced from 2 to 1. + L_shr() weight reduced from 2 to 1. + L_sub() weight reduced from 2 to 1. + mac_r() weight reduced from 2 to 1. + msu_r() weight reduced from 2 to 1. + mult_r() weight reduced from 2 to 1. + L_deposit_h() weight reduced from 2 to 1. + L_deposit_l() weight reduced from 2 to 1. + 15 Nov 04 v2.0 L_mls() weight of 5. + div_l() weight of 32. + i_mult() weight of 3. + + ============================================================================= +*/ + + +/*___________________________________________________________________________ + | | + | Basic arithmetic operators. | + | | + | $Id $ | + | | + | saturate() | + | add() | + | sub() | + | abs_s() | + | divide_s() | + | extract_h() | + | extract_l() | + | L_abs() | + | L_add() | + | L_deposit_h() | + | L_deposit_l() | + | L_mac() | + | L_msu() | + | L_mult() | + | L_negate() | + | L_shl() | + | L_shr() | + | L_sub() | + | mac_r() | + | msu_r() | + | mult() | + | mult_r() | + | negate() | + | norm_l() | + | norm_s() | + | round() | + | shl() | + | shr() | + |___________________________________________________________________________| +*/ + + +/*___________________________________________________________________________ + | | + | Include-Files | + |___________________________________________________________________________| +*/ +#include +#include +#include "stl.h" + + +#if (WMOPS) +extern BASIC_OP multiCounter[MAXCOUNTERS]; +extern int currCounter; +#endif + + +/*___________________________________________________________________________ + | | + | Local Functions | + |___________________________________________________________________________| +*/ +Word16 saturate (Word32 L_var1); + + +/*___________________________________________________________________________ + | | + | Constants and Globals | + |___________________________________________________________________________| +*/ +Flag Overflow = 0; +Flag Carry = 0; + + +/*___________________________________________________________________________ + | | + | Functions | + |___________________________________________________________________________| +*/ + +/*___________________________________________________________________________ + | | + | Function Name : saturate | + | | + | Purpose : | + | | + | Limit the 32 bit input to the range of a 16 bit word. | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 saturate (Word32 L_var1) +{ + Word16 var_out; + + if (L_var1 > 0X00007fffL) + { + Overflow = 1; + var_out = MAX_16; + } + else if (L_var1 < (Word32) 0xffff8000L) + { + Overflow = 1; + var_out = MIN_16; + } + else + { + var_out = extract_l (L_var1); + +#if (WMOPS) + multiCounter[currCounter].extract_l--; +#endif + } + + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : add | + | | + | Purpose : | + | | + | Performs the addition (var1+var2) with overflow control and saturation;| + | the 16 bit result is set at +32767 when overflow occurs or at -32768 | + | when underflow occurs. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 add (Word16 var1, Word16 var2) +{ + Word16 var_out; + Word32 L_sum; + + L_sum = (Word32) var1 + var2; + var_out = saturate (L_sum); + +#if (WMOPS) + multiCounter[currCounter].add++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : sub | + | | + | Purpose : | + | | + | Performs the subtraction (var1+var2) with overflow control and satu- | + | ration; the 16 bit result is set at +32767 when overflow occurs or at | + | -32768 when underflow occurs. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 sub (Word16 var1, Word16 var2) +{ + Word16 var_out; + Word32 L_diff; + + L_diff = (Word32) var1 - var2; + var_out = saturate (L_diff); + +#if (WMOPS) + multiCounter[currCounter].sub++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : abs_s | + | | + | Purpose : | + | | + | Absolute value of var1; abs_s(-32768) = 32767. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 0000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 abs_s (Word16 var1) +{ + Word16 var_out; + + if (var1 == (Word16) MIN_16) + { + var_out = MAX_16; + } + else + { + if (var1 < 0) + { + var_out = -var1; + } + else + { + var_out = var1; + } + } + +#if (WMOPS) + multiCounter[currCounter].abs_s++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : shl | + | | + | Purpose : | + | | + | Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill| + | the var2 LSB of the result. If var2 is negative, arithmetically shift | + | var1 right by -var2 with sign extension. Saturate the result in case of | + | underflows or overflows. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 shl (Word16 var1, Word16 var2) +{ + Word16 var_out; + Word32 result; + + if (var2 < 0) + { + if (var2 < -16) + var2 = -16; + var2 = -var2; + var_out = shr (var1, var2); + +#if (WMOPS) + multiCounter[currCounter].shr--; +#endif + } + else + { + result = (Word32) var1 *((Word32) 1 << var2); + + if ((var2 > 15 && var1 != 0) || (result != (Word32) ((Word16) result))) + { + Overflow = 1; + var_out = (var1 > 0) ? MAX_16 : MIN_16; + } + else + { + var_out = extract_l (result); + +#if (WMOPS) + multiCounter[currCounter].extract_l--; +#endif + } + } + +#if (WMOPS) + multiCounter[currCounter].shl++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : shr | + | | + | Purpose : | + | | + | Arithmetically shift the 16 bit input var1 right var2 positions with | + | sign extension. If var2 is negative, arithmetically shift var1 left by | + | -var2 with sign extension. Saturate the result in case of underflows or | + | overflows. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 shr (Word16 var1, Word16 var2) +{ + Word16 var_out; + + if (var2 < 0) + { + if (var2 < -16) + var2 = -16; + var2 = -var2; + var_out = shl (var1, var2); + +#if (WMOPS) + multiCounter[currCounter].shl--; +#endif + } + else + { + if (var2 >= 15) + { + var_out = (var1 < 0) ? -1 : 0; + } + else + { + if (var1 < 0) + { + var_out = ~((~var1) >> var2); + } + else + { + var_out = var1 >> var2; + } + } + } + +#if (WMOPS) + multiCounter[currCounter].shr++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : mult | + | | + | Purpose : | + | | + | Performs the multiplication of var1 by var2 and gives a 16 bit result | + | which is scaled i.e.: | + | mult(var1,var2) = extract_l(L_shr((var1 times var2),15)) and | + | mult(-32768,-32768) = 32767. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 mult (Word16 var1, Word16 var2) +{ + Word16 var_out; + Word32 L_product; + + L_product = (Word32) var1 *(Word32) var2; + + L_product = (L_product & (Word32) 0xffff8000L) >> 15; + + if (L_product & (Word32) 0x00010000L) + L_product = L_product | (Word32) 0xffff0000L; + + var_out = saturate (L_product); + +#if (WMOPS) + multiCounter[currCounter].mult++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_mult | + | | + | Purpose : | + | | + | L_mult is the 32 bit result of the multiplication of var1 times var2 | + | with one shift left i.e.: | + | L_mult(var1,var2) = L_shl((var1 times var2),1) and | + | L_mult(-32768,-32768) = 2147483647. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_mult (Word16 var1, Word16 var2) +{ + Word32 L_var_out; + + L_var_out = (Word32) var1 *(Word32) var2; + + if (L_var_out != (Word32) 0x40000000L) + { + L_var_out *= 2; + } + else + { + Overflow = 1; + L_var_out = MAX_32; + } + +#if (WMOPS) + multiCounter[currCounter].L_mult++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : negate | + | | + | Purpose : | + | | + | Negate var1 with saturation, saturate in the case where input is -32768:| + | negate(var1) = sub(0,var1). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 negate (Word16 var1) +{ + Word16 var_out; + + var_out = (var1 == MIN_16) ? MAX_16 : -var1; + +#if (WMOPS) + multiCounter[currCounter].negate++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : extract_h | + | | + | Purpose : | + | | + | Return the 16 MSB of L_var1. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32 ) whose value falls in the | + | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 extract_h (Word32 L_var1) +{ + Word16 var_out; + + var_out = (Word16) (L_var1 >> 16); + +#if (WMOPS) + multiCounter[currCounter].extract_h++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : extract_l | + | | + | Purpose : | + | | + | Return the 16 LSB of L_var1. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32 ) whose value falls in the | + | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 extract_l (Word32 L_var1) +{ + Word16 var_out; + + var_out = (Word16) L_var1; + +#if (WMOPS) + multiCounter[currCounter].extract_l++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : round | + | | + | Purpose : | + | | + | Round the lower 16 bits of the 32 bit input number into the MS 16 bits | + | with saturation. Shift the resulting bits right by 16 and return the 16 | + | bit number: | + | round(L_var1) = extract_h(L_add(L_var1,32768)) | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32 ) whose value falls in the | + | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 intround (Word32 L_var1) +{ + Word16 var_out; + Word32 L_rounded; + + L_rounded = L_add (L_var1, (Word32) 0x00008000L); + var_out = extract_h (L_rounded); + +#if (WMOPS) + multiCounter[currCounter].L_add--; + multiCounter[currCounter].extract_h--; + multiCounter[currCounter].round++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_mac | + | | + | Purpose : | + | | + | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit | + | result to L_var3 with saturation, return a 32 bit result: | + | L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var3 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_mac (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word32 L_var_out; + Word32 L_product; + + L_product = L_mult (var1, var2); + L_var_out = L_add (L_var3, L_product); + +#if (WMOPS) + multiCounter[currCounter].L_mult--; + multiCounter[currCounter].L_add--; + multiCounter[currCounter].L_mac++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_msu | + | | + | Purpose : | + | | + | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 | + | bit result from L_var3 with saturation, return a 32 bit result: | + | L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var3 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_msu (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word32 L_var_out; + Word32 L_product; + + L_product = L_mult (var1, var2); + L_var_out = L_sub (L_var3, L_product); + +#if (WMOPS) + multiCounter[currCounter].L_mult--; + multiCounter[currCounter].L_sub--; + multiCounter[currCounter].L_msu++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_macNs | + | | + | Purpose : | + | | + | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit | + | result to L_var3 without saturation, return a 32 bit result. Generate | + | carry and overflow values : | + | L_macNs(L_var3,var1,var2) = L_add_c(L_var3,L_mult(var1,var2)). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var3 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + | | + | Caution : | + | | + | In some cases the Carry flag has to be cleared or set before using | + | operators which take into account its value. | + |___________________________________________________________________________| +*/ +Word32 L_macNs (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word32 L_var_out; + + L_var_out = L_mult (var1, var2); + L_var_out = L_add_c (L_var3, L_var_out); + +#if (WMOPS) + multiCounter[currCounter].L_mult--; + multiCounter[currCounter].L_add_c--; + multiCounter[currCounter].L_macNs++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_msuNs | + | | + | Purpose : | + | | + | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 | + | bit result from L_var3 without saturation, return a 32 bit result. Ge- | + | nerate carry and overflow values : | + | L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,L_mult(var1,var2)). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var3 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + | | + | Caution : | + | | + | In some cases the Carry flag has to be cleared or set before using | + | operators which take into account its value. | + |___________________________________________________________________________| +*/ +Word32 L_msuNs (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word32 L_var_out; + + L_var_out = L_mult (var1, var2); + L_var_out = L_sub_c (L_var3, L_var_out); + +#if (WMOPS) + multiCounter[currCounter].L_mult--; + multiCounter[currCounter].L_sub_c--; + multiCounter[currCounter].L_msuNs++; + +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_add | + | | + | Purpose : | + | | + | 32 bits addition of the two 32 bits variables (L_var1+L_var2) with | + | overflow control and saturation; the result is set at +2147483647 when | + | overflow occurs or at -2147483648 when underflow occurs. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | L_var2 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_add (Word32 L_var1, Word32 L_var2) +{ + Word32 L_var_out; + + L_var_out = L_var1 + L_var2; + + if (((L_var1 ^ L_var2) & MIN_32) == 0) + { + if ((L_var_out ^ L_var1) & MIN_32) + { + L_var_out = (L_var1 < 0) ? MIN_32 : MAX_32; + Overflow = 1; + } + } + +#if (WMOPS) + multiCounter[currCounter].L_add++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_sub | + | | + | Purpose : | + | | + | 32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with | + | overflow control and saturation; the result is set at +2147483647 when | + | overflow occurs or at -2147483648 when underflow occurs. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | L_var2 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_sub (Word32 L_var1, Word32 L_var2) +{ + Word32 L_var_out; + + L_var_out = L_var1 - L_var2; + + if (((L_var1 ^ L_var2) & MIN_32) != 0) + { + if ((L_var_out ^ L_var1) & MIN_32) + { + L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32; + Overflow = 1; + } + } + +#if (WMOPS) + multiCounter[currCounter].L_sub++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_add_c | + | | + | Purpose : | + | | + | Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)| + | with carry. No saturation. Generate carry and Overflow values. The car- | + | ry and overflow values are binary variables which can be tested and as- | + | signed values. | + | | + | Complexity weight : 2 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | L_var2 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + | | + | Caution : | + | | + | In some cases the Carry flag has to be cleared or set before using | + | operators which take into account its value. | + |___________________________________________________________________________| +*/ +Word32 L_add_c (Word32 L_var1, Word32 L_var2) +{ + Word32 L_var_out; + Word32 L_test; + Flag carry_int = 0; + + L_var_out = L_var1 + L_var2 + Carry; + + L_test = L_var1 + L_var2; + + if ((L_var1 > 0) && (L_var2 > 0) && (L_test < 0)) + { + Overflow = 1; + carry_int = 0; + } + else + { + if ((L_var1 < 0) && (L_var2 < 0)) + { + if (L_test >= 0) + { + Overflow = 1; + carry_int = 1; + } + else + { + Overflow = 0; + carry_int = 1; + } + } + else + { + if (((L_var1 ^ L_var2) < 0) && (L_test >= 0)) + { + Overflow = 0; + carry_int = 1; + } + else + { + Overflow = 0; + carry_int = 0; + } + } + } + + if (Carry) + { + if (L_test == MAX_32) + { + Overflow = 1; + Carry = carry_int; + } + else + { + if (L_test == (Word32) 0xFFFFFFFFL) + { + Carry = 1; + } + else + { + Carry = carry_int; + } + } + } + else + { + Carry = carry_int; + } + +#if (WMOPS) + multiCounter[currCounter].L_add_c++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_sub_c | + | | + | Purpose : | + | | + | Performs 32 bits subtraction of the two 32 bits variables with carry | + | (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow | + | values. The carry and overflow values are binary variables which can | + | be tested and assigned values. | + | | + | Complexity weight : 2 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | L_var2 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + | | + | Caution : | + | | + | In some cases the Carry flag has to be cleared or set before using | + | operators which take into account its value. | + |___________________________________________________________________________| +*/ +Word32 L_sub_c (Word32 L_var1, Word32 L_var2) +{ + Word32 L_var_out; + Word32 L_test; + Flag carry_int = 0; + + if (Carry) + { + Carry = 0; + if (L_var2 != MIN_32) + { + L_var_out = L_add_c (L_var1, -L_var2); +#if (WMOPS) + multiCounter[currCounter].L_add_c--; +#endif + } + else + { + L_var_out = L_var1 - L_var2; + if (L_var1 > 0L) + { + Overflow = 1; + Carry = 0; + } + } + } + else + { + L_var_out = L_var1 - L_var2 - (Word32) 0X00000001L; + L_test = L_var1 - L_var2; + + if ((L_test < 0) && (L_var1 > 0) && (L_var2 < 0)) + { + Overflow = 1; + carry_int = 0; + } + else if ((L_test > 0) && (L_var1 < 0) && (L_var2 > 0)) + { + Overflow = 1; + carry_int = 1; + } + else if ((L_test > 0) && ((L_var1 ^ L_var2) > 0)) + { + Overflow = 0; + carry_int = 1; + } + if (L_test == MIN_32) + { + Overflow = 1; + Carry = carry_int; + } + else + { + Carry = carry_int; + } + } + +#if (WMOPS) + multiCounter[currCounter].L_sub_c++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_negate | + | | + | Purpose : | + | | + | Negate the 32 bit variable L_var1 with saturation; saturate in the case | + | where input is -2147483648 (0x8000 0000). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_negate (Word32 L_var1) +{ + Word32 L_var_out; + + L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1; + +#if (WMOPS) + multiCounter[currCounter].L_negate++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : mult_r | + | | + | Purpose : | + | | + | Same as mult with rounding, i.e.: | + | mult_r(var1,var2) = extract_l(L_shr(((var1 * var2) + 16384),15)) and | + | mult_r(-32768,-32768) = 32767. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x8000 <= var_out <= 0x7fff. | + |___________________________________________________________________________| +*/ +Word16 mult_r (Word16 var1, Word16 var2) +{ + Word16 var_out; + Word32 L_product_arr; + + L_product_arr = (Word32) var1 *(Word32) var2; /* product */ + L_product_arr += (Word32) 0x00004000L; /* round */ + L_product_arr &= (Word32) 0xffff8000L; + L_product_arr >>= 15; /* shift */ + + if (L_product_arr & (Word32) 0x00010000L) /* sign extend when necessary */ + { + L_product_arr |= (Word32) 0xffff0000L; + } + var_out = saturate (L_product_arr); + +#if (WMOPS) + multiCounter[currCounter].mult_r++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_shl | + | | + | Purpose : | + | | + | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero | + | fill the var2 LSB of the result. If var2 is negative, arithmetically | + | shift L_var1 right by -var2 with sign extension. Saturate the result in | + | case of underflows or overflows. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_shl (Word32 L_var1, Word16 var2) +{ + + Word32 L_var_out = 0L; + + if (var2 <= 0) + { + if (var2 < -32) + var2 = -32; + var2 = -var2; + L_var_out = L_shr (L_var1, var2); + #if (WMOPS) + multiCounter[currCounter].L_shr--; + #endif + } + else + { + for (; var2 > 0; var2--) + { + if (L_var1 > (Word32) 0X3fffffffL) + { + Overflow = 1; + L_var_out = MAX_32; + break; + } + else + { + if (L_var1 < (Word32) 0xc0000000L) + { + Overflow = 1; + L_var_out = MIN_32; + break; + } + } + L_var1 *= 2; + L_var_out = L_var1; + } + } + #if (WMOPS) + multiCounter[currCounter].L_shl++; + #endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_shr | + | | + | Purpose : | + | | + | Arithmetically shift the 32 bit input L_var1 right var2 positions with | + | sign extension. If var2 is negative, arithmetically shift L_var1 left | + | by -var2 and zero fill the -var2 LSB of the result. Saturate the result | + | in case of underflows or overflows. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_shr (Word32 L_var1, Word16 var2) +{ + Word32 L_var_out; + + if (var2 < 0) + { + if (var2 < -32) + var2 = -32; + var2 = -var2; + L_var_out = L_shl (L_var1, var2); + #if (WMOPS) + multiCounter[currCounter].L_shl--; + #endif + } + else + { + if (var2 >= 31) + { + L_var_out = (L_var1 < 0L) ? -1 : 0; + } + else + { + if (L_var1 < 0) + { + L_var_out = ~((~L_var1) >> var2); + } + else + { + L_var_out = L_var1 >> var2; + } + } + } + #if (WMOPS) + multiCounter[currCounter].L_shr++; + #endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : shr_r | + | | + | Purpose : | + | | + | Same as shr(var1,var2) but with rounding. Saturate the result in case of| + | underflows or overflows : | + | - If var2 is greater than zero : | + | if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1)))) | + | is equal to zero | + | then | + | shr_r(var1,var2) = shr(var1,var2) | + | else | + | shr_r(var1,var2) = add(shr(var1,var2),1) | + | - If var2 is less than or equal to zero : | + | shr_r(var1,var2) = shr(var1,var2). | + | | + | Complexity weight : 3 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 shr_r (Word16 var1, Word16 var2) +{ + Word16 var_out; + + if (var2 > 15) + { + var_out = 0; + } + else + { + var_out = shr (var1, var2); + +#if (WMOPS) + multiCounter[currCounter].shr--; +#endif + + if (var2 > 0) + { + if ((var1 & ((Word16) 1 << (var2 - 1))) != 0) + { + var_out++; + } + } + } + +#if (WMOPS) + multiCounter[currCounter].shr_r++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : mac_r | + | | + | Purpose : | + | | + | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit | + | result to L_var3 with saturation. Round the LS 16 bits of the result | + | into the MS 16 bits with saturation and shift the result right by 16. | + | Return a 16 bit result. | + | mac_r(L_var3,var1,var2) = round(L_mac(L_var3,var1,var2)) | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var3 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 mac_r (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word16 var_out; + + L_var3 = L_mac (L_var3, var1, var2); + L_var3 = L_add (L_var3, (Word32) 0x00008000L); + var_out = extract_h (L_var3); + +#if (WMOPS) + multiCounter[currCounter].L_mac--; + multiCounter[currCounter].L_add--; + multiCounter[currCounter].extract_h--; + multiCounter[currCounter].mac_r++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : msu_r | + | | + | Purpose : | + | | + | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 | + | bit result from L_var3 with saturation. Round the LS 16 bits of the res-| + | ult into the MS 16 bits with saturation and shift the result right by | + | 16. Return a 16 bit result. | + | msu_r(L_var3,var1,var2) = round(L_msu(L_var3,var1,var2)) | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var3 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word16 msu_r (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word16 var_out; + + L_var3 = L_msu (L_var3, var1, var2); + L_var3 = L_add (L_var3, (Word32) 0x00008000L); + var_out = extract_h (L_var3); + +#if (WMOPS) + multiCounter[currCounter].L_msu--; + multiCounter[currCounter].L_add--; + multiCounter[currCounter].extract_h--; + multiCounter[currCounter].msu_r++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_deposit_h | + | | + | Purpose : | + | | + | Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The | + | 16 LS bits of the output are zeroed. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var_out <= 0x7fff 0000. | + |___________________________________________________________________________| +*/ +Word32 L_deposit_h (Word16 var1) +{ + Word32 L_var_out; + + L_var_out = (Word32) var1 << 16; + +#if (WMOPS) + multiCounter[currCounter].L_deposit_h++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_deposit_l | + | | + | Purpose : | + | | + | Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The | + | 16 MS bits of the output are sign extended. | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0xFFFF 8000 <= var_out <= 0x0000 7fff. | + |___________________________________________________________________________| +*/ +Word32 L_deposit_l (Word16 var1) +{ + Word32 L_var_out; + + L_var_out = (Word32) var1; + +#if (WMOPS) + multiCounter[currCounter].L_deposit_l++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_shr_r | + | | + | Purpose : | + | | + | Same as L_shr(L_var1,var2) but with rounding. Saturate the result in | + | case of underflows or overflows : | + | - If var2 is greater than zero : | + | if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))| + | is equal to zero | + | then | + | L_shr_r(L_var1,var2) = L_shr(L_var1,var2) | + | else | + | L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1) | + | - If var2 is less than or equal to zero : | + | L_shr_r(L_var1,var2) = L_shr(L_var1,var2). | + | | + | Complexity weight : 3 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_shr_r (Word32 L_var1, Word16 var2) +{ + Word32 L_var_out; + + if (var2 > 31) + { + L_var_out = 0; + } + else + { + L_var_out = L_shr (L_var1, var2); + +#if (WMOPS) + multiCounter[currCounter].L_shr--; +#endif + if (var2 > 0) + { + if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0) + { + L_var_out++; + } + } + } + +#if (WMOPS) + multiCounter[currCounter].L_shr_r++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_abs | + | | + | Purpose : | + | | + | Absolute value of L_var1; Saturate in case where the input is | + | -214783648 | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x0000 0000 <= var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_abs (Word32 L_var1) +{ + Word32 L_var_out; + + if (L_var1 == MIN_32) + { + L_var_out = MAX_32; + } + else + { + if (L_var1 < 0) + { + L_var_out = -L_var1; + } + else + { + L_var_out = L_var1; + } + } + +#if (WMOPS) + multiCounter[currCounter].L_abs++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : L_sat | + | | + | Purpose : | + | | + | 32 bit L_var1 is set to 2147483647 if an overflow occured or to | + | -2147483648 if an underflow occured on the most recent L_add_c, | + | L_sub_c, L_macNs or L_msuNs operations. The carry and overflow values | + | are binary values which can be tested and assigned values. | + | | + | Complexity weight : 4 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | L_var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var_out <= 0x7fff ffff. | + |___________________________________________________________________________| +*/ +Word32 L_sat (Word32 L_var1) +{ + Word32 L_var_out; + + L_var_out = L_var1; + + if (Overflow) + { + + if (Carry) + { + L_var_out = MIN_32; + } + else + { + L_var_out = MAX_32; + } + + Carry = 0; + Overflow = 0; + } + +#if (WMOPS) + multiCounter[currCounter].L_sat++; +#endif + return (L_var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : norm_s | + | | + | Purpose : | + | | + | Produces the number of left shift needed to normalize the 16 bit varia- | + | ble var1 for positive values on the interval with minimum of 16384 and | + | maximum of 32767, and for negative values on the interval with minimum | + | of -32768 and maximum of -16384; in order to normalize the result, the | + | following operation must be done : | + | norm_var1 = shl(var1,norm_s(var1)). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 0000 <= var_out <= 0x0000 000f. | + |___________________________________________________________________________| +*/ +Word16 norm_s (Word16 var1) +{ + Word16 var_out; + + if (var1 == 0) + { + var_out = 0; + } + else + { + if (var1 == (Word16) 0xffff) + { + var_out = 15; + } + else + { + if (var1 < 0) + { + var1 = ~var1; + } + for (var_out = 0; var1 < 0x4000; var_out++) + { + var1 <<= 1; + } + } + } + +#if (WMOPS) + multiCounter[currCounter].norm_s++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : div_s | + | | + | Purpose : | + | | + | Produces a result which is the fractional integer division of var1 by | + | var2; var1 and var2 must be positive and var2 must be greater or equal | + | to var1; the result is positive (leading bit equal to 0) and truncated | + | to 16 bits. | + | If var1 = var2 then div(var1,var2) = 32767. | + | | + | Complexity weight : 18 | + | | + | Inputs : | + | | + | var1 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 0000 <= var1 <= var2 and var2 != 0. | + | | + | var2 | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : var1 <= var2 <= 0x0000 7fff and var2 != 0. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 0000 <= var_out <= 0x0000 7fff. | + | It's a Q15 value (point between b15 and b14). | + |___________________________________________________________________________| +*/ +Word16 div_s (Word16 var1, Word16 var2) +{ + Word16 var_out = 0; + Word16 iteration; + Word32 L_num; + Word32 L_denom; + + if ((var1 > var2) || (var1 < 0) || (var2 < 0)) + { + printf ("Division Error var1=%d var2=%d\n", var1, var2); + abort(); /* exit (0); */ + } + if (var2 == 0) + { + printf ("Division by 0, Fatal error \n"); + abort(); /* exit (0); */ + } + if (var1 == 0) + { + var_out = 0; + } + else + { + if (var1 == var2) + { + var_out = MAX_16; + } + else + { + L_num = L_deposit_l (var1); + L_denom = L_deposit_l (var2); + +#if (WMOPS) + multiCounter[currCounter].L_deposit_l--; + multiCounter[currCounter].L_deposit_l--; +#endif + + for (iteration = 0; iteration < 15; iteration++) + { + var_out <<= 1; + L_num <<= 1; + + if (L_num >= L_denom) + { + L_num = L_sub (L_num, L_denom); + var_out = add (var_out, 1); +#if (WMOPS) + multiCounter[currCounter].L_sub--; + multiCounter[currCounter].add--; +#endif + } + } + } + } + +#if (WMOPS) + multiCounter[currCounter].div_s++; +#endif + return (var_out); +} + + +/*___________________________________________________________________________ + | | + | Function Name : norm_l | + | | + | Purpose : | + | | + | Produces the number of left shifts needed to normalize the 32 bit varia-| + | ble L_var1 for positive values on the interval with minimum of | + | 1073741824 and maximum of 2147483647, and for negative values on the in-| + | terval with minimum of -2147483648 and maximum of -1073741824; in order | + | to normalize the result, the following operation must be done : | + | norm_L_var1 = L_shl(L_var1,norm_l(L_var1)). | + | | + | Complexity weight : 1 | + | | + | Inputs : | + | | + | L_var1 | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x0000 0000 <= var_out <= 0x0000 001f. | + |___________________________________________________________________________| +*/ +Word16 norm_l (Word32 L_var1) +{ + Word16 var_out; + + if (L_var1 == 0) + { + var_out = 0; + } + else + { + if (L_var1 == (Word32) 0xffffffffL) + { + var_out = 31; + } + else + { + if (L_var1 < 0) + { + L_var1 = ~L_var1; + } + for (var_out = 0; L_var1 < (Word32) 0x40000000L; var_out++) + { + L_var1 <<= 1; + } + } + } + +#if (WMOPS) + multiCounter[currCounter].norm_l++; +#endif + return (var_out); +} + +/* + ****************************************************************************** + * Additional operators extracted from the G.723.1 Library + * Adapted for WMOPS calculations + ****************************************************************************** +*/ + +/*___________________________________________________________________________ + | | + | Function Name : L_mls | + | | + | Purpose : | + | | + | Multiplies a 16 bit word v by a 32 bit word Lv and returns a 32 bit | + | word (multiplying 16 by 32 bit words gives 48 bit word; the function | + | extracts the 32 MSB and shift the result to the left by 1). | + | | + | A 32 bit word can be written as | + | Lv = a + b * 2^16 | + | where a= unsigned 16 LSBs and b= signed 16 MSBs. | + | The function returns v * Lv / 2^15 which is equivalent to | + | a*v / 2^15 + b*v*2 | + | | + | Complexity weight : 5 | + | | + | Inputs : | + | | + | Lv | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var1 <= 0x7fff ffff. | + | v | + | 16 bit short signed integer (Word16) whose value falls in the | + | range : 0x8000 <= var1 <= 0x7fff. | + | | + | Outputs : | + | | + | none | + | | + | Return Value : | + | | + | var_out | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= var_out <= 0x7fff ffff. | + | | + |___________________________________________________________________________| +*/ +Word32 L_mls (Word32 Lv, Word16 v) +{ + Word32 Temp ; + + Temp = Lv & (Word32) 0x0000ffff ; + Temp = Temp * (Word32) v ; + Temp = L_shr( Temp, (Word16) 15 ) ; + Temp = L_mac( Temp, v, extract_h(Lv) ) ; + +#if (WMOPS) + multiCounter[currCounter].L_shr--; + multiCounter[currCounter].L_mac--; + multiCounter[currCounter].extract_h--; + multiCounter[currCounter].L_mls++; +#endif + + return Temp ; +} + + +/*__________________________________________________________________________ +| | +| Function Name : div_l | +| | +| Purpose : | +| | +| Produces a result which is the fractional integer division of L_var1 by | +| var2; L_var1 and var2 must be positive and var2 << 16 must be greater or| +| equal to L_var1; the result is positive (leading bit equal to 0) and | +| truncated to 16 bits. | +| If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767. | +| | +| Complexity weight : 32 | +| | +| Inputs : | +| | +| L_var1 | +| 32 bit long signed integer (Word32) whose value falls in the | +| range : 0x0000 0000 <= var1 <= (var2 << 16) and var2 != 0. | +| L_var1 must be considered as a Q.31 value | +| | +| var2 | +| 16 bit short signed integer (Word16) whose value falls in the | +| range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0. | +| var2 must be considered as a Q.15 value | +| | +| Outputs : | +| | +| none | +| | +| Return Value : | +| | +| var_out | +| 16 bit short signed integer (Word16) whose value falls in the | +| range : 0x0000 0000 <= var_out <= 0x0000 7fff. | +| It's a Q15 value (point between b15 and b14). | +|___________________________________________________________________________| +*/ +Word16 div_l (Word32 L_num, Word16 den) +{ + Word16 var_out = (Word16)0; + Word32 L_den; + Word16 iteration; + +#if (WMOPS) + multiCounter[currCounter].div_l++; +#endif + + if ( den == (Word16) 0 ) { + printf("Division by 0 in div_l, Fatal error \n"); + exit(0); + } + + if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) { + printf("Division Error in div_l, Fatal error \n"); + exit(0); + } + + L_den = L_deposit_h( den ) ; +#if (WMOPS) + multiCounter[currCounter].L_deposit_h--; +#endif + + if ( L_num >= L_den ){ + return MAX_16 ; + } + else { + L_num = L_shr(L_num, (Word16)1) ; + L_den = L_shr(L_den, (Word16)1); +#if (WMOPS) + multiCounter[currCounter].L_shr-=2; +#endif + for(iteration=(Word16)0; iteration< (Word16)15;iteration++) { + var_out = shl( var_out, (Word16)1); + L_num = L_shl( L_num, (Word16)1); +#if (WMOPS) + multiCounter[currCounter].shl--; + multiCounter[currCounter].L_shl--; +#endif + if (L_num >= L_den) { + L_num = L_sub(L_num,L_den); + var_out = add(var_out, (Word16)1); +#if (WMOPS) + multiCounter[currCounter].L_sub--; + multiCounter[currCounter].add--; +#endif + } + } + + return var_out; + } +} + + +/*__________________________________________________________________________ +| | +| Function Name : i_mult | +| | +| Purpose : | +| | +| Integer 16-bit multiplication. No overflow protection is performed if | +| ORIGINAL_G7231 is defined. | +| | +| Complexity weight : 3 | +| | +| Inputs : | +| | +| a | +| 16 bit short signed integer (Word16). | +| | +| b | +| 16 bit short signed integer (Word16). | +| | +| Outputs : | +| | +| none | +| | +| Return Value : | +| | +| 16 bit short signed integer (Word16). No overflow checks | +| are performed if ORIGINAL_G7231 is defined. | +|___________________________________________________________________________| +*/ +Word16 i_mult (Word16 a, Word16 b) +{ +#ifdef ORIGINAL_G7231 + return a*b ; +#else + Word32 register c=a*b; +#if (WMOPS) + multiCounter[currCounter].i_mult++; +#endif + return saturate(c) ; +#endif +} + + +/* + ****************************************************************************** + * The following three operators are not part of the original + * G.729/G.723.1 set of basic operators and implement shiftless + * accumulation operation. + ****************************************************************************** +*/ + +/*___________________________________________________________________________ + | + | Function Name : L_mult0 + | + | Purpose : + | + | L_mult0 is the 32 bit result of the multiplication of var1 times var2 + | without one left shift. + | + | Complexity weight : 1 + | + | Inputs : + | + | var1 16 bit short signed integer (Word16) whose value falls in the + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. + | + | var2 16 bit short signed integer (Word16) whose value falls in the + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. + | + | Return Value : + | + | L_var_out + | 32 bit long signed integer (Word32) whose value falls in the + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. + |___________________________________________________________________________ +*/ +Word32 L_mult0 (Word16 var1,Word16 var2) +{ + Word32 L_var_out; + + L_var_out = (Word32)var1 * (Word32)var2; + +#if (WMOPS) + multiCounter[currCounter].L_mult0++; +#endif + return(L_var_out); +} + + +/*___________________________________________________________________________ + | + | Function Name : L_mac0 + | + | Purpose : + | + | Multiply var1 by var2 (without left shift) and add the 32 bit result to + | L_var3 with saturation, return a 32 bit result: + | L_mac0(L_var3,var1,var2) = L_add(L_var3,(L_mult0(var1,var2)). + | + | Complexity weight : 1 + | + | Inputs : + | + | L_var3 32 bit long signed integer (Word32) whose value falls in the + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. + | + | var1 16 bit short signed integer (Word16) whose value falls in the + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. + | + | var2 16 bit short signed integer (Word16) whose value falls in the + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. + | + | Return Value : + | + | L_var_out + | 32 bit long signed integer (Word32) whose value falls in the + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. + |___________________________________________________________________________ +*/ +Word32 L_mac0 (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word32 L_var_out; + Word32 L_product; + + L_product = L_mult0(var1,var2); + L_var_out = L_add(L_var3,L_product); + +#if (WMOPS) + multiCounter[currCounter].L_mac0++; + multiCounter[currCounter].L_mult0--; + multiCounter[currCounter].L_add--; +#endif + return(L_var_out); +} + + +/*___________________________________________________________________________ + | + | Function Name : L_msu0 + | + | Purpose : + | + | Multiply var1 by var2 (without left shift) and subtract the 32 bit + | result to L_var3 with saturation, return a 32 bit result: + | L_msu0(L_var3,var1,var2) = L_sub(L_var3,(L_mult0(var1,var2)). + | + | Complexity weight : 1 + | + | Inputs : + | + | L_var3 32 bit long signed integer (Word32) whose value falls in the + | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. + | + | var1 16 bit short signed integer (Word16) whose value falls in the + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. + | + | var2 16 bit short signed integer (Word16) whose value falls in the + | range : 0xffff 8000 <= var1 <= 0x0000 7fff. + | + | Return Value : + | + | L_var_out + | 32 bit long signed integer (Word32) whose value falls in the + | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. + |___________________________________________________________________________ +*/ +Word32 L_msu0 (Word32 L_var3, Word16 var1, Word16 var2) +{ + Word32 L_var_out; + Word32 L_product; + + L_product = L_mult0(var1,var2); + L_var_out = L_sub(L_var3,L_product); + +#if (WMOPS) + multiCounter[currCounter].L_msu0++; + multiCounter[currCounter].L_mult0--; + multiCounter[currCounter].L_sub--; +#endif + return(L_var_out); +} + + +/* end of file */ diff --git a/jni/bx16_fixedp/itug191lib/basop32.h b/app/src/main/jni/bx16_fixedp/itug191lib/basop32.h similarity index 98% rename from jni/bx16_fixedp/itug191lib/basop32.h rename to app/src/main/jni/bx16_fixedp/itug191lib/basop32.h index 7ce9b4b..a0b80a9 100644 --- a/jni/bx16_fixedp/itug191lib/basop32.h +++ b/app/src/main/jni/bx16_fixedp/itug191lib/basop32.h @@ -1,128 +1,128 @@ -/* - =========================================================================== - File: BASOP32.H v.2.0 - 15.Nov.2004 - =========================================================================== - - ITU-T STL BASIC OPERATORS - - GLOBAL FUNCTION PROTOTYPES - - History: - 26.Jan.00 v1.0 Incorporated to the STL from updated G.723.1/G.729 - basic operator library (based on basic_op.h) and - G.723.1's basop.h. - 05.Jul.00 v1.1 Added 32-bit shiftless mult/mac/msub operators - - 03 Nov 04 v2.0 Incorporation of new 32-bit / 40-bit / control - operators for the ITU-T Standard Tool Library as - described in Geneva, 20-30 January 2004 WP 3/16 Q10/16 - TD 11 document and subsequent discussions on the - wp3audio@yahoogroups.com email reflector. - norm_s() weight reduced from 15 to 1. - norm_l() weight reduced from 30 to 1. - L_abs() weight reduced from 2 to 1. - L_add() weight reduced from 2 to 1. - L_negate() weight reduced from 2 to 1. - L_shl() weight reduced from 2 to 1. - L_shr() weight reduced from 2 to 1. - L_sub() weight reduced from 2 to 1. - mac_r() weight reduced from 2 to 1. - msu_r() weight reduced from 2 to 1. - mult_r() weight reduced from 2 to 1. - L_deposit_h() weight reduced from 2 to 1. - L_deposit_l() weight reduced from 2 to 1. - 15 Nov 04 v2.0 L_mls() weight of 5. - div_l() weight of 32. - i_mult() weight of 3. - - ============================================================================ -*/ - - -#ifndef _BASIC_OP_H -#define _BASIC_OP_H - - -/*___________________________________________________________________________ - | | - | Constants and Globals | - | $Id $ - |___________________________________________________________________________| -*/ -extern Flag Overflow; -extern Flag Carry; - -#define MAX_32 (Word32)0x7fffffffL -#define MIN_32 (Word32)0x80000000L - -#define MAX_16 (Word16)0x7fff -#define MIN_16 (Word16)0x8000 - -/*___________________________________________________________________________ - | | - | Prototypes for basic arithmetic operators | - |___________________________________________________________________________| -*/ - -Word16 add (Word16 var1, Word16 var2); /* Short add, 1 */ -Word16 sub (Word16 var1, Word16 var2); /* Short sub, 1 */ -Word16 abs_s (Word16 var1); /* Short abs, 1 */ -Word16 shl (Word16 var1, Word16 var2); /* Short shift left, 1 */ -Word16 shr (Word16 var1, Word16 var2); /* Short shift right, 1 */ -Word16 mult (Word16 var1, Word16 var2); /* Short mult, 1 */ -Word32 L_mult (Word16 var1, Word16 var2); /* Long mult, 1 */ -Word16 negate (Word16 var1); /* Short negate, 1 */ -Word16 extract_h (Word32 L_var1); /* Extract high, 1 */ -Word16 extract_l (Word32 L_var1); /* Extract low, 1 */ -Word16 intround (Word32 L_var1); /* Round, 1 */ -Word32 L_mac (Word32 L_var3, Word16 var1, Word16 var2); /* Mac, 1 */ -Word32 L_msu (Word32 L_var3, Word16 var1, Word16 var2); /* Msu, 1 */ -Word32 L_macNs (Word32 L_var3, Word16 var1, Word16 var2); /* Mac without - sat, 1 */ -Word32 L_msuNs (Word32 L_var3, Word16 var1, Word16 var2); /* Msu without - sat, 1 */ -Word32 L_add (Word32 L_var1, Word32 L_var2); /* Long add, 1 */ -Word32 L_sub (Word32 L_var1, Word32 L_var2); /* Long sub, 1 */ -Word32 L_add_c (Word32 L_var1, Word32 L_var2); /* Long add with c, 2 */ -Word32 L_sub_c (Word32 L_var1, Word32 L_var2); /* Long sub with c, 2 */ -Word32 L_negate (Word32 L_var1); /* Long negate, 1 */ -Word16 mult_r (Word16 var1, Word16 var2); /* Mult with round, 1 */ -Word32 L_shl (Word32 L_var1, Word16 var2); /* Long shift left, 1 */ -Word32 L_shr (Word32 L_var1, Word16 var2); /* Long shift right, 1 */ -Word16 shr_r (Word16 var1, Word16 var2); /* Shift right with - round, 2 */ -Word16 mac_r (Word32 L_var3, Word16 var1, Word16 var2); /* Mac with - rounding, 1 */ -Word16 msu_r (Word32 L_var3, Word16 var1, Word16 var2); /* Msu with - rounding, 1 */ -Word32 L_deposit_h (Word16 var1); /* 16 bit var1 -> MSB, 1 */ -Word32 L_deposit_l (Word16 var1); /* 16 bit var1 -> LSB, 1 */ - -Word32 L_shr_r (Word32 L_var1, Word16 var2); /* Long shift right with - round, 3 */ -Word32 L_abs (Word32 L_var1); /* Long abs, 1 */ -Word32 L_sat (Word32 L_var1); /* Long saturation, 4 */ -Word16 norm_s (Word16 var1); /* Short norm, 1 */ -Word16 div_s (Word16 var1, Word16 var2); /* Short division, 18 */ -Word16 norm_l (Word32 L_var1); /* Long norm, 1 */ - - -/* - * Additional G.723.1 operators -*/ -Word32 L_mls( Word32, Word16 ) ; /* Weight FFS; currently assigned 5 */ -Word16 div_l( Word32, Word16 ) ; /* Weight FFS; currently assigned 32 */ -Word16 i_mult(Word16 a, Word16 b); /* Weight FFS; currently assigned 3 */ - -/* - * New shiftless operators, not used in G.729/G.723.1 -*/ -Word32 L_mult0(Word16 v1, Word16 v2); /* 32-bit Multiply w/o shift 1 */ -Word32 L_mac0(Word32 L_v3, Word16 v1, Word16 v2); /* 32-bit Mac w/o shift 1 */ -Word32 L_msu0(Word32 L_v3, Word16 v1, Word16 v2); /* 32-bit Msu w/o shift 1 */ - - -#endif /* ifndef _BASIC_OP_H */ - - -/* end of file */ +/* + =========================================================================== + File: BASOP32.H v.2.0 - 15.Nov.2004 + =========================================================================== + + ITU-T STL BASIC OPERATORS + + GLOBAL FUNCTION PROTOTYPES + + History: + 26.Jan.00 v1.0 Incorporated to the STL from updated G.723.1/G.729 + basic operator library (based on basic_op.h) and + G.723.1's basop.h. + 05.Jul.00 v1.1 Added 32-bit shiftless mult/mac/msub operators + + 03 Nov 04 v2.0 Incorporation of new 32-bit / 40-bit / control + operators for the ITU-T Standard Tool Library as + described in Geneva, 20-30 January 2004 WP 3/16 Q10/16 + TD 11 document and subsequent discussions on the + wp3audio@yahoogroups.com email reflector. + norm_s() weight reduced from 15 to 1. + norm_l() weight reduced from 30 to 1. + L_abs() weight reduced from 2 to 1. + L_add() weight reduced from 2 to 1. + L_negate() weight reduced from 2 to 1. + L_shl() weight reduced from 2 to 1. + L_shr() weight reduced from 2 to 1. + L_sub() weight reduced from 2 to 1. + mac_r() weight reduced from 2 to 1. + msu_r() weight reduced from 2 to 1. + mult_r() weight reduced from 2 to 1. + L_deposit_h() weight reduced from 2 to 1. + L_deposit_l() weight reduced from 2 to 1. + 15 Nov 04 v2.0 L_mls() weight of 5. + div_l() weight of 32. + i_mult() weight of 3. + + ============================================================================ +*/ + + +#ifndef _BASIC_OP_H +#define _BASIC_OP_H + + +/*___________________________________________________________________________ + | | + | Constants and Globals | + | $Id $ + |___________________________________________________________________________| +*/ +extern Flag Overflow; +extern Flag Carry; + +#define MAX_32 (Word32)0x7fffffffL +#define MIN_32 (Word32)0x80000000L + +#define MAX_16 (Word16)0x7fff +#define MIN_16 (Word16)0x8000 + +/*___________________________________________________________________________ + | | + | Prototypes for basic arithmetic operators | + |___________________________________________________________________________| +*/ + +Word16 add (Word16 var1, Word16 var2); /* Short add, 1 */ +Word16 sub (Word16 var1, Word16 var2); /* Short sub, 1 */ +Word16 abs_s (Word16 var1); /* Short abs, 1 */ +Word16 shl (Word16 var1, Word16 var2); /* Short shift left, 1 */ +Word16 shr (Word16 var1, Word16 var2); /* Short shift right, 1 */ +Word16 mult (Word16 var1, Word16 var2); /* Short mult, 1 */ +Word32 L_mult (Word16 var1, Word16 var2); /* Long mult, 1 */ +Word16 negate (Word16 var1); /* Short negate, 1 */ +Word16 extract_h (Word32 L_var1); /* Extract high, 1 */ +Word16 extract_l (Word32 L_var1); /* Extract low, 1 */ +Word16 intround (Word32 L_var1); /* Round, 1 */ +Word32 L_mac (Word32 L_var3, Word16 var1, Word16 var2); /* Mac, 1 */ +Word32 L_msu (Word32 L_var3, Word16 var1, Word16 var2); /* Msu, 1 */ +Word32 L_macNs (Word32 L_var3, Word16 var1, Word16 var2); /* Mac without + sat, 1 */ +Word32 L_msuNs (Word32 L_var3, Word16 var1, Word16 var2); /* Msu without + sat, 1 */ +Word32 L_add (Word32 L_var1, Word32 L_var2); /* Long add, 1 */ +Word32 L_sub (Word32 L_var1, Word32 L_var2); /* Long sub, 1 */ +Word32 L_add_c (Word32 L_var1, Word32 L_var2); /* Long add with c, 2 */ +Word32 L_sub_c (Word32 L_var1, Word32 L_var2); /* Long sub with c, 2 */ +Word32 L_negate (Word32 L_var1); /* Long negate, 1 */ +Word16 mult_r (Word16 var1, Word16 var2); /* Mult with round, 1 */ +Word32 L_shl (Word32 L_var1, Word16 var2); /* Long shift left, 1 */ +Word32 L_shr (Word32 L_var1, Word16 var2); /* Long shift right, 1 */ +Word16 shr_r (Word16 var1, Word16 var2); /* Shift right with + round, 2 */ +Word16 mac_r (Word32 L_var3, Word16 var1, Word16 var2); /* Mac with + rounding, 1 */ +Word16 msu_r (Word32 L_var3, Word16 var1, Word16 var2); /* Msu with + rounding, 1 */ +Word32 L_deposit_h (Word16 var1); /* 16 bit var1 -> MSB, 1 */ +Word32 L_deposit_l (Word16 var1); /* 16 bit var1 -> LSB, 1 */ + +Word32 L_shr_r (Word32 L_var1, Word16 var2); /* Long shift right with + round, 3 */ +Word32 L_abs (Word32 L_var1); /* Long abs, 1 */ +Word32 L_sat (Word32 L_var1); /* Long saturation, 4 */ +Word16 norm_s (Word16 var1); /* Short norm, 1 */ +Word16 div_s (Word16 var1, Word16 var2); /* Short division, 18 */ +Word16 norm_l (Word32 L_var1); /* Long norm, 1 */ + + +/* + * Additional G.723.1 operators +*/ +Word32 L_mls( Word32, Word16 ) ; /* Weight FFS; currently assigned 5 */ +Word16 div_l( Word32, Word16 ) ; /* Weight FFS; currently assigned 32 */ +Word16 i_mult(Word16 a, Word16 b); /* Weight FFS; currently assigned 3 */ + +/* + * New shiftless operators, not used in G.729/G.723.1 +*/ +Word32 L_mult0(Word16 v1, Word16 v2); /* 32-bit Multiply w/o shift 1 */ +Word32 L_mac0(Word32 L_v3, Word16 v1, Word16 v2); /* 32-bit Mac w/o shift 1 */ +Word32 L_msu0(Word32 L_v3, Word16 v1, Word16 v2); /* 32-bit Msu w/o shift 1 */ + + +#endif /* ifndef _BASIC_OP_H */ + + +/* end of file */ diff --git a/jni/bx16_fixedp/itug191lib/readme.txt b/app/src/main/jni/bx16_fixedp/itug191lib/readme.txt similarity index 98% rename from jni/bx16_fixedp/itug191lib/readme.txt rename to app/src/main/jni/bx16_fixedp/itug191lib/readme.txt index 73f86c1..245e303 100644 --- a/jni/bx16_fixedp/itug191lib/readme.txt +++ b/app/src/main/jni/bx16_fixedp/itug191lib/readme.txt @@ -1,11 +1,11 @@ -Copy: -1. basop32.c -2. basop32.h -from ITU-T G.191 (09/05) STL basic operator library and put the files in this directory. - -It can be downloaded from ITU-T at http://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-G.191-200509-I!!SOFT-ZST-E&type=items -The files are located in ./Software/stl2005/basop - -If you encounter any problem downloading the ITU-T files, please send an email to broadvoice@broadcom.com - +Copy: +1. basop32.c +2. basop32.h +from ITU-T G.191 (09/05) STL basic operator library and put the files in this directory. + +It can be downloaded from ITU-T at http://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-G.191-200509-I!!SOFT-ZST-E&type=items +The files are located in ./Software/stl2005/basop + +If you encounter any problem downloading the ITU-T files, please send an email to broadvoice@broadcom.com + If you have already downloaded these files for BroadVoice32 then they can simply be copied from there. \ No newline at end of file diff --git a/jni/bx16_fixedp/itug191lib/stl.h b/app/src/main/jni/bx16_fixedp/itug191lib/stl.h similarity index 98% rename from jni/bx16_fixedp/itug191lib/stl.h rename to app/src/main/jni/bx16_fixedp/itug191lib/stl.h index db95b4f..09dca3b 100644 --- a/jni/bx16_fixedp/itug191lib/stl.h +++ b/app/src/main/jni/bx16_fixedp/itug191lib/stl.h @@ -1,21 +1,21 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - -// empty file to prevent compilation error +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + +// empty file to prevent compilation error diff --git a/jni/bx16_fixedp/itug729ilib/basic_op.h b/app/src/main/jni/bx16_fixedp/itug729ilib/basic_op.h similarity index 98% rename from jni/bx16_fixedp/itug729ilib/basic_op.h rename to app/src/main/jni/bx16_fixedp/itug729ilib/basic_op.h index db95b4f..09dca3b 100644 --- a/jni/bx16_fixedp/itug729ilib/basic_op.h +++ b/app/src/main/jni/bx16_fixedp/itug729ilib/basic_op.h @@ -1,21 +1,21 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - -// empty file to prevent compilation error +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + +// empty file to prevent compilation error diff --git a/jni/bx16_fixedp/itug729ilib/oper_32b.c b/app/src/main/jni/bx16_fixedp/itug729ilib/oper_32b.c similarity index 98% rename from jni/bx16_fixedp/itug729ilib/oper_32b.c rename to app/src/main/jni/bx16_fixedp/itug729ilib/oper_32b.c index 512bd20..97ae80a 100644 --- a/jni/bx16_fixedp/itug729ilib/oper_32b.c +++ b/app/src/main/jni/bx16_fixedp/itug729ilib/oper_32b.c @@ -1,217 +1,217 @@ -/* ITU-T G.729 Software Package Release 2 (November 2006) */ -/* Version 3.3 Last modified: December 26, 1995 */ - -#include "typedef.h" -#include "basic_op.h" -#include "oper_32b.h" - -/*___________________________________________________________________________ - | | - | This file contains operations in double precision. | - | These operations are not standard double precision operations. | - | They are used where single precision is not enough but the full 32 bits | - | precision is not necessary. For example, the function Div_32() has a | - | 24 bits precision which is enough for our purposes. | - | | - | The double precision numbers use a special representation: | - | | - | L_32 = hi<<16 + lo<<1 | - | | - | L_32 is a 32 bit integer. | - | hi and lo are 16 bit signed integers. | - | As the low part also contains the sign, this allows fast multiplication. | - | | - | 0x8000 0000 <= L_32 <= 0x7fff fffe. | - | | - | We will use DPF (Double Precision Format )in this file to specify | - | this special format. | - |___________________________________________________________________________| -*/ - -/*___________________________________________________________________________ - | | - | Function L_Extract() | - | | - | Extract from a 32 bit integer two 16 bit DPF. | - | | - | Arguments: | - | | - | L_32 : 32 bit integer. | - | 0x8000 0000 <= L_32 <= 0x7fff ffff. | - | hi : b16 to b31 of L_32 | - | lo : (L_32 - hi<<16)>>1 | - |___________________________________________________________________________| -*/ - -void L_Extract(Word32 L_32, Word16 *hi, Word16 *lo) -{ - *hi = extract_h(L_32); - *lo = extract_l( L_msu( L_shr(L_32, 1) , *hi, 16384)); /* lo = L_32>>1 */ - return; -} - -/*___________________________________________________________________________ - | | - | Function L_Comp() | - | | - | Compose from two 16 bit DPF a 32 bit integer. | - | | - | L_32 = hi<<16 + lo<<1 | - | | - | Arguments: | - | | - | hi msb | - | lo lsf (with sign) | - | | - | Return Value : | - | | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x8000 0000 <= L_32 <= 0x7fff fff0. | - | | - |___________________________________________________________________________| -*/ - -Word32 L_Comp(Word16 hi, Word16 lo) -{ - Word32 L_32; - - L_32 = L_deposit_h(hi); - return( L_mac(L_32, lo, 1)); /* = hi<<16 + lo<<1 */ -} - -/*___________________________________________________________________________ - | Function Mpy_32() | - | | - | Multiply two 32 bit integers (DPF). The result is divided by 2**31 | - | | - | L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 | - | | - | This operation can also be viewed as the multiplication of two Q31 | - | number and the result is also in Q31. | - | | - | Arguments: | - | | - | hi1 hi part of first number | - | lo1 lo part of first number | - | hi2 hi part of second number | - | lo2 lo part of second number | - | | - |___________________________________________________________________________| -*/ - -Word32 Mpy_32(Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2) -{ - Word32 L_32; - - L_32 = L_mult(hi1, hi2); - L_32 = L_mac(L_32, mult(hi1, lo2) , 1); - L_32 = L_mac(L_32, mult(lo1, hi2) , 1); - - return( L_32 ); -} - -/*___________________________________________________________________________ - | Function Mpy_32_16() | - | | - | Multiply a 16 bit integer by a 32 bit (DPF). The result is divided | - | by 2**15 | - | | - | This operation can also be viewed as the multiplication of a Q31 | - | number by a Q15 number, the result is in Q31. | - | | - | L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 | - | | - | Arguments: | - | | - | hi hi part of 32 bit number. | - | lo lo part of 32 bit number. | - | n 16 bit number. | - | | - |___________________________________________________________________________| -*/ - -Word32 Mpy_32_16(Word16 hi, Word16 lo, Word16 n) -{ - Word32 L_32; - - L_32 = L_mult(hi, n); - L_32 = L_mac(L_32, mult(lo, n) , 1); - - return( L_32 ); -} - -/*___________________________________________________________________________ - | | - | Function Name : Div_32 | - | | - | Purpose : | - | Fractional integer division of two 32 bit numbers. | - | L_num / L_denom. | - | L_num and L_denom must be positive and L_num < L_denom. | - | L_denom = denom_hi<<16 + denom_lo<<1 | - | denom_hi is a normalize number. | - | The result is in Q30. | - | | - | Inputs : | - | | - | L_num | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x0000 0000 < L_num < L_denom | - | | - | L_denom = denom_hi<<16 + denom_lo<<1 (DPF) | - | | - | denom_hi | - | 16 bit positive normalized integer whose value falls in the | - | range : 0x4000 < hi < 0x7fff | - | denom_lo | - | 16 bit positive integer whose value falls in the | - | range : 0 < lo < 0x7fff | - | | - | Return Value : | - | | - | L_div | - | 32 bit long signed integer (Word32) whose value falls in the | - | range : 0x0000 0000 <= L_div <= 0x7fff ffff. | - | It's a Q31 value | - | | - | Algorithm: | - | | - | - find = 1/L_denom. | - | First approximation: approx = 1 / denom_hi | - | 1/L_denom = approx * (2.0 - L_denom * approx ) | - | | - | - result = L_num * (1/L_denom) | - |___________________________________________________________________________| -*/ - -Word32 Div_32(Word32 L_num, Word16 denom_hi, Word16 denom_lo) -{ - Word16 approx, hi, lo, n_hi, n_lo; - Word32 L_32; - - - /* First approximation: 1 / L_denom = 1/denom_hi */ - - approx = div_s( (Word16)0x3fff, denom_hi); /* result in Q14 */ - /* Note: 3fff = 0.5 in Q15 */ - - /* 1/L_denom = approx * (2.0 - L_denom * approx) */ - - L_32 = Mpy_32_16(denom_hi, denom_lo, approx); /* result in Q30 */ - - - L_32 = L_sub( (Word32)0x7fffffffL, L_32); /* result in Q30 */ - - L_Extract(L_32, &hi, &lo); - - L_32 = Mpy_32_16(hi, lo, approx); /* = 1/L_denom in Q29 */ - - /* L_num * (1/L_denom) */ - - L_Extract(L_32, &hi, &lo); - L_Extract(L_num, &n_hi, &n_lo); - L_32 = Mpy_32(n_hi, n_lo, hi, lo); /* result in Q29 */ - L_32 = L_shl(L_32, 2); /* From Q29 to Q31 */ - - return( L_32 ); -} +/* ITU-T G.729 Software Package Release 2 (November 2006) */ +/* Version 3.3 Last modified: December 26, 1995 */ + +#include "typedef.h" +#include "basic_op.h" +#include "oper_32b.h" + +/*___________________________________________________________________________ + | | + | This file contains operations in double precision. | + | These operations are not standard double precision operations. | + | They are used where single precision is not enough but the full 32 bits | + | precision is not necessary. For example, the function Div_32() has a | + | 24 bits precision which is enough for our purposes. | + | | + | The double precision numbers use a special representation: | + | | + | L_32 = hi<<16 + lo<<1 | + | | + | L_32 is a 32 bit integer. | + | hi and lo are 16 bit signed integers. | + | As the low part also contains the sign, this allows fast multiplication. | + | | + | 0x8000 0000 <= L_32 <= 0x7fff fffe. | + | | + | We will use DPF (Double Precision Format )in this file to specify | + | this special format. | + |___________________________________________________________________________| +*/ + +/*___________________________________________________________________________ + | | + | Function L_Extract() | + | | + | Extract from a 32 bit integer two 16 bit DPF. | + | | + | Arguments: | + | | + | L_32 : 32 bit integer. | + | 0x8000 0000 <= L_32 <= 0x7fff ffff. | + | hi : b16 to b31 of L_32 | + | lo : (L_32 - hi<<16)>>1 | + |___________________________________________________________________________| +*/ + +void L_Extract(Word32 L_32, Word16 *hi, Word16 *lo) +{ + *hi = extract_h(L_32); + *lo = extract_l( L_msu( L_shr(L_32, 1) , *hi, 16384)); /* lo = L_32>>1 */ + return; +} + +/*___________________________________________________________________________ + | | + | Function L_Comp() | + | | + | Compose from two 16 bit DPF a 32 bit integer. | + | | + | L_32 = hi<<16 + lo<<1 | + | | + | Arguments: | + | | + | hi msb | + | lo lsf (with sign) | + | | + | Return Value : | + | | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x8000 0000 <= L_32 <= 0x7fff fff0. | + | | + |___________________________________________________________________________| +*/ + +Word32 L_Comp(Word16 hi, Word16 lo) +{ + Word32 L_32; + + L_32 = L_deposit_h(hi); + return( L_mac(L_32, lo, 1)); /* = hi<<16 + lo<<1 */ +} + +/*___________________________________________________________________________ + | Function Mpy_32() | + | | + | Multiply two 32 bit integers (DPF). The result is divided by 2**31 | + | | + | L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 | + | | + | This operation can also be viewed as the multiplication of two Q31 | + | number and the result is also in Q31. | + | | + | Arguments: | + | | + | hi1 hi part of first number | + | lo1 lo part of first number | + | hi2 hi part of second number | + | lo2 lo part of second number | + | | + |___________________________________________________________________________| +*/ + +Word32 Mpy_32(Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2) +{ + Word32 L_32; + + L_32 = L_mult(hi1, hi2); + L_32 = L_mac(L_32, mult(hi1, lo2) , 1); + L_32 = L_mac(L_32, mult(lo1, hi2) , 1); + + return( L_32 ); +} + +/*___________________________________________________________________________ + | Function Mpy_32_16() | + | | + | Multiply a 16 bit integer by a 32 bit (DPF). The result is divided | + | by 2**15 | + | | + | This operation can also be viewed as the multiplication of a Q31 | + | number by a Q15 number, the result is in Q31. | + | | + | L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 | + | | + | Arguments: | + | | + | hi hi part of 32 bit number. | + | lo lo part of 32 bit number. | + | n 16 bit number. | + | | + |___________________________________________________________________________| +*/ + +Word32 Mpy_32_16(Word16 hi, Word16 lo, Word16 n) +{ + Word32 L_32; + + L_32 = L_mult(hi, n); + L_32 = L_mac(L_32, mult(lo, n) , 1); + + return( L_32 ); +} + +/*___________________________________________________________________________ + | | + | Function Name : Div_32 | + | | + | Purpose : | + | Fractional integer division of two 32 bit numbers. | + | L_num / L_denom. | + | L_num and L_denom must be positive and L_num < L_denom. | + | L_denom = denom_hi<<16 + denom_lo<<1 | + | denom_hi is a normalize number. | + | The result is in Q30. | + | | + | Inputs : | + | | + | L_num | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x0000 0000 < L_num < L_denom | + | | + | L_denom = denom_hi<<16 + denom_lo<<1 (DPF) | + | | + | denom_hi | + | 16 bit positive normalized integer whose value falls in the | + | range : 0x4000 < hi < 0x7fff | + | denom_lo | + | 16 bit positive integer whose value falls in the | + | range : 0 < lo < 0x7fff | + | | + | Return Value : | + | | + | L_div | + | 32 bit long signed integer (Word32) whose value falls in the | + | range : 0x0000 0000 <= L_div <= 0x7fff ffff. | + | It's a Q31 value | + | | + | Algorithm: | + | | + | - find = 1/L_denom. | + | First approximation: approx = 1 / denom_hi | + | 1/L_denom = approx * (2.0 - L_denom * approx ) | + | | + | - result = L_num * (1/L_denom) | + |___________________________________________________________________________| +*/ + +Word32 Div_32(Word32 L_num, Word16 denom_hi, Word16 denom_lo) +{ + Word16 approx, hi, lo, n_hi, n_lo; + Word32 L_32; + + + /* First approximation: 1 / L_denom = 1/denom_hi */ + + approx = div_s( (Word16)0x3fff, denom_hi); /* result in Q14 */ + /* Note: 3fff = 0.5 in Q15 */ + + /* 1/L_denom = approx * (2.0 - L_denom * approx) */ + + L_32 = Mpy_32_16(denom_hi, denom_lo, approx); /* result in Q30 */ + + + L_32 = L_sub( (Word32)0x7fffffffL, L_32); /* result in Q30 */ + + L_Extract(L_32, &hi, &lo); + + L_32 = Mpy_32_16(hi, lo, approx); /* = 1/L_denom in Q29 */ + + /* L_num * (1/L_denom) */ + + L_Extract(L_32, &hi, &lo); + L_Extract(L_num, &n_hi, &n_lo); + L_32 = Mpy_32(n_hi, n_lo, hi, lo); /* result in Q29 */ + L_32 = L_shl(L_32, 2); /* From Q29 to Q31 */ + + return( L_32 ); +} diff --git a/jni/bx16_fixedp/itug729ilib/oper_32b.h b/app/src/main/jni/bx16_fixedp/itug729ilib/oper_32b.h similarity index 97% rename from jni/bx16_fixedp/itug729ilib/oper_32b.h rename to app/src/main/jni/bx16_fixedp/itug729ilib/oper_32b.h index e59c55e..9ca24c0 100644 --- a/jni/bx16_fixedp/itug729ilib/oper_32b.h +++ b/app/src/main/jni/bx16_fixedp/itug729ilib/oper_32b.h @@ -1,10 +1,10 @@ -/* Version 3.3 Last modified: December 26, 1995 */ - -/* Double precision operations */ - -void L_Extract(Word32 L_32, Word16 *hi, Word16 *lo); -Word32 L_Comp(Word16 hi, Word16 lo); -Word32 Mpy_32(Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2); -Word32 Mpy_32_16(Word16 hi, Word16 lo, Word16 n); -Word32 Div_32(Word32 L_num, Word16 denom_hi, Word16 denom_lo); - +/* Version 3.3 Last modified: December 26, 1995 */ + +/* Double precision operations */ + +void L_Extract(Word32 L_32, Word16 *hi, Word16 *lo); +Word32 L_Comp(Word16 hi, Word16 lo); +Word32 Mpy_32(Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2); +Word32 Mpy_32_16(Word16 hi, Word16 lo, Word16 n); +Word32 Div_32(Word32 L_num, Word16 denom_hi, Word16 denom_lo); + diff --git a/jni/bx16_fixedp/itug729ilib/readme.txt b/app/src/main/jni/bx16_fixedp/itug729ilib/readme.txt similarity index 97% rename from jni/bx16_fixedp/itug729ilib/readme.txt rename to app/src/main/jni/bx16_fixedp/itug729ilib/readme.txt index 1c6a6c2..2ffeb1c 100644 --- a/jni/bx16_fixedp/itug729ilib/readme.txt +++ b/app/src/main/jni/bx16_fixedp/itug729ilib/readme.txt @@ -1,11 +1,11 @@ -Copy: -1. oper_32b.c -2. oper_32b.h -from ITU-T G.729I (01/07) and put the files in this directory. - -It can be downloaded from ITU-T at http://www.itu.int/rec/T-REC-G.729/recommendation.asp?lang=en&parent=T-REC-G.729-200701-I -The files are located in ./Soft/g729AnnexI/c_code - -If you encounter any problem downloading the ITU-T files, please send an email to broadvoice@broadcom.com - +Copy: +1. oper_32b.c +2. oper_32b.h +from ITU-T G.729I (01/07) and put the files in this directory. + +It can be downloaded from ITU-T at http://www.itu.int/rec/T-REC-G.729/recommendation.asp?lang=en&parent=T-REC-G.729-200701-I +The files are located in ./Soft/g729AnnexI/c_code + +If you encounter any problem downloading the ITU-T files, please send an email to broadvoice@broadcom.com + If you have already downloaded these files for BroadVoice32 then they can simply be copied from there. \ No newline at end of file diff --git a/jni/bx16_fixedp/itug729ilib/typedef.h b/app/src/main/jni/bx16_fixedp/itug729ilib/typedef.h similarity index 98% rename from jni/bx16_fixedp/itug729ilib/typedef.h rename to app/src/main/jni/bx16_fixedp/itug729ilib/typedef.h index db95b4f..09dca3b 100644 --- a/jni/bx16_fixedp/itug729ilib/typedef.h +++ b/app/src/main/jni/bx16_fixedp/itug729ilib/typedef.h @@ -1,21 +1,21 @@ -/*****************************************************************************/ -/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ -/* Revision Date: November 13, 2009 */ -/* Version 1.1 */ -/*****************************************************************************/ - -/*****************************************************************************/ -/* Copyright 2000-2009 Broadcom Corporation */ -/* */ -/* This software is provided under the GNU Lesser General Public License, */ -/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ -/* more details. A copy of the LGPL is available at */ -/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ -/* or by writing to the Free Software Foundation, Inc., */ -/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*****************************************************************************/ - -// empty file to prevent compilation error +/*****************************************************************************/ +/* BroadVoice(R)16 (BV16) Fixed-Point ANSI-C Source Code */ +/* Revision Date: November 13, 2009 */ +/* Version 1.1 */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Copyright 2000-2009 Broadcom Corporation */ +/* */ +/* This software is provided under the GNU Lesser General Public License, */ +/* version 2.1, as published by the Free Software Foundation ("LGPL"). */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY SUPPORT OR WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LGPL for */ +/* more details. A copy of the LGPL is available at */ +/* http://www.broadcom.com/licenses/LGPLv2.1.php, */ +/* or by writing to the Free Software Foundation, Inc., */ +/* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/*****************************************************************************/ + +// empty file to prevent compilation error diff --git a/jni/g722_jni.cpp b/app/src/main/jni/g722_jni.cpp similarity index 100% rename from jni/g722_jni.cpp rename to app/src/main/jni/g722_jni.cpp diff --git a/jni/gsm_jni.cpp b/app/src/main/jni/gsm_jni.cpp similarity index 100% rename from jni/gsm_jni.cpp rename to app/src/main/jni/gsm_jni.cpp diff --git a/jni/silk/interface/SKP_Silk_SDK_API.h b/app/src/main/jni/silk/interface/SKP_Silk_SDK_API.h similarity index 98% rename from jni/silk/interface/SKP_Silk_SDK_API.h rename to app/src/main/jni/silk/interface/SKP_Silk_SDK_API.h index cb31db2..7f85dac 100644 --- a/jni/silk/interface/SKP_Silk_SDK_API.h +++ b/app/src/main/jni/silk/interface/SKP_Silk_SDK_API.h @@ -1,154 +1,154 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_SDK_API_H -#define SKP_SILK_SDK_API_H - -#include "SKP_Silk_control.h" -#include "SKP_Silk_typedef.h" -#include "SKP_Silk_errors.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define SILK_MAX_FRAMES_PER_PACKET 5 - -/* Struct for TOC (Table Of Contents) */ -typedef struct { - SKP_int framesInPacket; /* Number of 20 ms frames in packet */ - SKP_int fs_kHz; /* Sampling frequency in packet */ - SKP_int inbandLBRR; /* Does packet contain LBRR information */ - SKP_int corrupt; /* Packet is corrupt */ - SKP_int vadFlags[ SILK_MAX_FRAMES_PER_PACKET ]; /* VAD flag for each frame in packet */ - SKP_int sigtypeFlags[ SILK_MAX_FRAMES_PER_PACKET ]; /* Signal type for each frame in packet */ -} SKP_Silk_TOC_struct; - -/****************************************/ -/* Encoder functions */ -/****************************************/ - -/***********************************************/ -/* Get size in bytes of the Silk encoder state */ -/***********************************************/ -SKP_int SKP_Silk_SDK_Get_Encoder_Size( - SKP_int32 *encSizeBytes /* O: Number of bytes in SILK encoder state */ -); - -/*************************/ -/* Init or reset encoder */ -/*************************/ -SKP_int SKP_Silk_SDK_InitEncoder( - void *encState, /* I/O: State */ - SKP_SILK_SDK_EncControlStruct *encStatus /* O: Encoder Status */ -); - -/***************************************/ -/* Read control structure from encoder */ -/***************************************/ -SKP_int SKP_Silk_SDK_QueryEncoder( - const void *encState, /* I: State */ - SKP_SILK_SDK_EncControlStruct *encStatus /* O: Encoder Status */ -); - -/**************************/ -/* Encode frame with Silk */ -/**************************/ -SKP_int SKP_Silk_SDK_Encode( - void *encState, /* I/O: State */ - const SKP_SILK_SDK_EncControlStruct *encControl, /* I: Control status */ - const SKP_int16 *samplesIn, /* I: Speech sample input vector */ - SKP_int nSamplesIn, /* I: Number of samples in input vector */ - SKP_uint8 *outData, /* O: Encoded output vector */ - SKP_int16 *nBytesOut /* I/O: Number of Bytes in outData (input: Max Bytes) */ -); - -/****************************************/ -/* Decoder functions */ -/****************************************/ - -/***********************************************/ -/* Get size in bytes of the Silk decoder state */ -/***********************************************/ -SKP_int SKP_Silk_SDK_Get_Decoder_Size( - SKP_int32 *decSizeBytes /* O: Number of bytes in SILK decoder state */ -); - -/*************************/ -/* Init or Reset decoder */ -/*************************/ -SKP_int SKP_Silk_SDK_InitDecoder( - void *decState /* I/O: State */ -); - -/******************/ -/* Decode a frame */ -/******************/ -SKP_int SKP_Silk_SDK_Decode( - void* decState, /* I/O: State */ - SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control Structure */ - SKP_int lostFlag, /* I: 0: no loss, 1 loss */ - const SKP_uint8 *inData, /* I: Encoded input vector */ - const SKP_int nBytesIn, /* I: Number of input Bytes */ - SKP_int16 *samplesOut, /* O: Decoded output speech vector */ - SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */ -); - -/***************************************************************/ -/* Find Low Bit Rate Redundancy (LBRR) information in a packet */ -/***************************************************************/ -void SKP_Silk_SDK_search_for_LBRR( - void *decState, /* I: Decoder state, to select bitstream version only */ - const SKP_uint8 *inData, /* I: Encoded input vector */ - const SKP_int16 nBytesIn, /* I: Number of input Bytes */ - SKP_int lost_offset, /* I: Offset from lost packet */ - SKP_uint8 *LBRRData, /* O: LBRR payload */ - SKP_int16 *nLBRRBytes /* O: Number of LBRR Bytes */ -); - -/************************************/ -/* Get type of content for a packet */ -/************************************/ -void SKP_Silk_SDK_get_TOC( - void *decState, /* I: Decoder state, to select bitstream version only */ - const SKP_uint8 *inData, /* I: Encoded input vector */ - const SKP_int16 nBytesIn, /* I: Number of input bytes */ - SKP_Silk_TOC_struct *Silk_TOC /* O: Type of content */ -); - -/**************************/ -/* Get the version number */ -/**************************/ -/* Return a pointer to string specifying the version */ -const char *SKP_Silk_SDK_get_version(); - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_SDK_API_H +#define SKP_SILK_SDK_API_H + +#include "SKP_Silk_control.h" +#include "SKP_Silk_typedef.h" +#include "SKP_Silk_errors.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define SILK_MAX_FRAMES_PER_PACKET 5 + +/* Struct for TOC (Table Of Contents) */ +typedef struct { + SKP_int framesInPacket; /* Number of 20 ms frames in packet */ + SKP_int fs_kHz; /* Sampling frequency in packet */ + SKP_int inbandLBRR; /* Does packet contain LBRR information */ + SKP_int corrupt; /* Packet is corrupt */ + SKP_int vadFlags[ SILK_MAX_FRAMES_PER_PACKET ]; /* VAD flag for each frame in packet */ + SKP_int sigtypeFlags[ SILK_MAX_FRAMES_PER_PACKET ]; /* Signal type for each frame in packet */ +} SKP_Silk_TOC_struct; + +/****************************************/ +/* Encoder functions */ +/****************************************/ + +/***********************************************/ +/* Get size in bytes of the Silk encoder state */ +/***********************************************/ +SKP_int SKP_Silk_SDK_Get_Encoder_Size( + SKP_int32 *encSizeBytes /* O: Number of bytes in SILK encoder state */ +); + +/*************************/ +/* Init or reset encoder */ +/*************************/ +SKP_int SKP_Silk_SDK_InitEncoder( + void *encState, /* I/O: State */ + SKP_SILK_SDK_EncControlStruct *encStatus /* O: Encoder Status */ +); + +/***************************************/ +/* Read control structure from encoder */ +/***************************************/ +SKP_int SKP_Silk_SDK_QueryEncoder( + const void *encState, /* I: State */ + SKP_SILK_SDK_EncControlStruct *encStatus /* O: Encoder Status */ +); + +/**************************/ +/* Encode frame with Silk */ +/**************************/ +SKP_int SKP_Silk_SDK_Encode( + void *encState, /* I/O: State */ + const SKP_SILK_SDK_EncControlStruct *encControl, /* I: Control status */ + const SKP_int16 *samplesIn, /* I: Speech sample input vector */ + SKP_int nSamplesIn, /* I: Number of samples in input vector */ + SKP_uint8 *outData, /* O: Encoded output vector */ + SKP_int16 *nBytesOut /* I/O: Number of Bytes in outData (input: Max Bytes) */ +); + +/****************************************/ +/* Decoder functions */ +/****************************************/ + +/***********************************************/ +/* Get size in bytes of the Silk decoder state */ +/***********************************************/ +SKP_int SKP_Silk_SDK_Get_Decoder_Size( + SKP_int32 *decSizeBytes /* O: Number of bytes in SILK decoder state */ +); + +/*************************/ +/* Init or Reset decoder */ +/*************************/ +SKP_int SKP_Silk_SDK_InitDecoder( + void *decState /* I/O: State */ +); + +/******************/ +/* Decode a frame */ +/******************/ +SKP_int SKP_Silk_SDK_Decode( + void* decState, /* I/O: State */ + SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control Structure */ + SKP_int lostFlag, /* I: 0: no loss, 1 loss */ + const SKP_uint8 *inData, /* I: Encoded input vector */ + const SKP_int nBytesIn, /* I: Number of input Bytes */ + SKP_int16 *samplesOut, /* O: Decoded output speech vector */ + SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */ +); + +/***************************************************************/ +/* Find Low Bit Rate Redundancy (LBRR) information in a packet */ +/***************************************************************/ +void SKP_Silk_SDK_search_for_LBRR( + void *decState, /* I: Decoder state, to select bitstream version only */ + const SKP_uint8 *inData, /* I: Encoded input vector */ + const SKP_int16 nBytesIn, /* I: Number of input Bytes */ + SKP_int lost_offset, /* I: Offset from lost packet */ + SKP_uint8 *LBRRData, /* O: LBRR payload */ + SKP_int16 *nLBRRBytes /* O: Number of LBRR Bytes */ +); + +/************************************/ +/* Get type of content for a packet */ +/************************************/ +void SKP_Silk_SDK_get_TOC( + void *decState, /* I: Decoder state, to select bitstream version only */ + const SKP_uint8 *inData, /* I: Encoded input vector */ + const SKP_int16 nBytesIn, /* I: Number of input bytes */ + SKP_Silk_TOC_struct *Silk_TOC /* O: Type of content */ +); + +/**************************/ +/* Get the version number */ +/**************************/ +/* Return a pointer to string specifying the version */ +const char *SKP_Silk_SDK_get_version(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/interface/SKP_Silk_control.h b/app/src/main/jni/silk/interface/SKP_Silk_control.h similarity index 97% rename from jni/silk/interface/SKP_Silk_control.h rename to app/src/main/jni/silk/interface/SKP_Silk_control.h index 2bd056f..1216d2e 100644 --- a/jni/silk/interface/SKP_Silk_control.h +++ b/app/src/main/jni/silk/interface/SKP_Silk_control.h @@ -1,88 +1,88 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_CONTROL_H -#define SKP_SILK_CONTROL_H - -#include "SKP_Silk_typedef.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -/***********************************************/ -/* Structure for controlling encoder operation */ -/***********************************************/ -typedef struct { - /* I: Sampling rate in Hertz; 8000/12000/16000/24000 */ - SKP_int32 sampleRate; - - /* I: Number of samples per packet; must be equivalent of 20, 40, 60, 80 or 100 ms */ - SKP_int packetSize; - - /* I: Bitrate during active speech in bits/second; internally limited */ - SKP_int32 bitRate; - - /* I: Uplink Packet loss in pct (0...100) */ - SKP_int packetLossPercentage; - - /* I: Complexity mode; 0 is lowest; 1 is medium and 2 is highest complexity */ - SKP_int complexity; - - /* I: Flag to enable in-band Forward Error Correction (FEC); 0/1 */ - SKP_int useInBandFEC; - - /* I: Flag to enable Discontinous Transmission; 0/1 */ - SKP_int useDTX; -} SKP_SILK_SDK_EncControlStruct; - -/**************************************************************************/ -/* Structure for controlling decoder operation and reading decoder status */ -/**************************************************************************/ -typedef struct { - /* I: Sampling rate in Hertz; 8000/12000/16000/24000 */ - SKP_int32 sampleRate; - - /* O: Number of samples per frame */ - SKP_int frameSize; - - /* O: Frames per packet 1, 2, 3, 4, 5 */ - SKP_int framesPerPacket; - - /* O: Flag to indicate that the decoder has remaining payloads internally */ - SKP_int moreInternalDecoderFrames; - - /* O: Distance between main payload and redundant payload in packets */ - SKP_int inBandFECOffset; -} SKP_SILK_SDK_DecControlStruct; - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_CONTROL_H +#define SKP_SILK_CONTROL_H + +#include "SKP_Silk_typedef.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/***********************************************/ +/* Structure for controlling encoder operation */ +/***********************************************/ +typedef struct { + /* I: Sampling rate in Hertz; 8000/12000/16000/24000 */ + SKP_int32 sampleRate; + + /* I: Number of samples per packet; must be equivalent of 20, 40, 60, 80 or 100 ms */ + SKP_int packetSize; + + /* I: Bitrate during active speech in bits/second; internally limited */ + SKP_int32 bitRate; + + /* I: Uplink Packet loss in pct (0...100) */ + SKP_int packetLossPercentage; + + /* I: Complexity mode; 0 is lowest; 1 is medium and 2 is highest complexity */ + SKP_int complexity; + + /* I: Flag to enable in-band Forward Error Correction (FEC); 0/1 */ + SKP_int useInBandFEC; + + /* I: Flag to enable Discontinous Transmission; 0/1 */ + SKP_int useDTX; +} SKP_SILK_SDK_EncControlStruct; + +/**************************************************************************/ +/* Structure for controlling decoder operation and reading decoder status */ +/**************************************************************************/ +typedef struct { + /* I: Sampling rate in Hertz; 8000/12000/16000/24000 */ + SKP_int32 sampleRate; + + /* O: Number of samples per frame */ + SKP_int frameSize; + + /* O: Frames per packet 1, 2, 3, 4, 5 */ + SKP_int framesPerPacket; + + /* O: Flag to indicate that the decoder has remaining payloads internally */ + SKP_int moreInternalDecoderFrames; + + /* O: Distance between main payload and redundant payload in packets */ + SKP_int inBandFECOffset; +} SKP_SILK_SDK_DecControlStruct; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/interface/SKP_Silk_errors.h b/app/src/main/jni/silk/interface/SKP_Silk_errors.h similarity index 97% rename from jni/silk/interface/SKP_Silk_errors.h rename to app/src/main/jni/silk/interface/SKP_Silk_errors.h index 99b837c..a14cd8d 100644 --- a/jni/silk/interface/SKP_Silk_errors.h +++ b/app/src/main/jni/silk/interface/SKP_Silk_errors.h @@ -1,93 +1,93 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_ERRORS_H -#define SKP_SILK_ERRORS_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -/******************/ -/* Error messages */ -/******************/ -#define SKP_SILK_NO_ERROR 0 - -/**************************/ -/* Encoder error messages */ -/**************************/ - -/* Input length is not a multiplum of 10 ms, - or length is longer than the packet length */ -#define SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES -1 - -/* Sampling frequency not 8000, 12000, 16000 - or 24000 Hertz */ -#define SKP_SILK_ENC_FS_NOT_SUPPORTED -2 - -/* Packet size not 20, 40, 60, 80 or 100 ms */ -#define SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED -3 - -/* Allocated payload buffer too short */ -#define SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT -4 - -/* Loss rate not between 0 and 100 percent */ -#define SKP_SILK_ENC_WRONG_LOSS_RATE -5 - -/* Complexity setting not valid, use 0, 1 or 2 */ -#define SKP_SILK_ENC_WRONG_COMPLEXITY_SETTING -6 - -/* Inband FEC setting not valid, use 0 or 1 */ -#define SKP_SILK_ENC_WRONG_INBAND_FEC_SETTING -7 - -/* DTX setting not valid, use 0 or 1 */ -#define SKP_SILK_ENC_WRONG_DTX_SETTING -8 - -/* Internal encoder error */ -#define SKP_SILK_ENC_INTERNAL_ERROR -9 - -/**************************/ -/* Decoder error messages */ -/**************************/ - -/* Output sampling frequency lower than internal - decoded sampling frequency */ -#define SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY -10 - -/* Payload size exceeded the maximum allowed 1024 bytes */ -#define SKP_SILK_DEC_PAYLOAD_TOO_LARGE -11 - -/* Payload has bit errors */ -#define SKP_SILK_DEC_PAYLOAD_ERROR -12 - - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_ERRORS_H +#define SKP_SILK_ERRORS_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************/ +/* Error messages */ +/******************/ +#define SKP_SILK_NO_ERROR 0 + +/**************************/ +/* Encoder error messages */ +/**************************/ + +/* Input length is not a multiplum of 10 ms, + or length is longer than the packet length */ +#define SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES -1 + +/* Sampling frequency not 8000, 12000, 16000 + or 24000 Hertz */ +#define SKP_SILK_ENC_FS_NOT_SUPPORTED -2 + +/* Packet size not 20, 40, 60, 80 or 100 ms */ +#define SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED -3 + +/* Allocated payload buffer too short */ +#define SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT -4 + +/* Loss rate not between 0 and 100 percent */ +#define SKP_SILK_ENC_WRONG_LOSS_RATE -5 + +/* Complexity setting not valid, use 0, 1 or 2 */ +#define SKP_SILK_ENC_WRONG_COMPLEXITY_SETTING -6 + +/* Inband FEC setting not valid, use 0 or 1 */ +#define SKP_SILK_ENC_WRONG_INBAND_FEC_SETTING -7 + +/* DTX setting not valid, use 0 or 1 */ +#define SKP_SILK_ENC_WRONG_DTX_SETTING -8 + +/* Internal encoder error */ +#define SKP_SILK_ENC_INTERNAL_ERROR -9 + +/**************************/ +/* Decoder error messages */ +/**************************/ + +/* Output sampling frequency lower than internal + decoded sampling frequency */ +#define SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY -10 + +/* Payload size exceeded the maximum allowed 1024 bytes */ +#define SKP_SILK_DEC_PAYLOAD_TOO_LARGE -11 + +/* Payload has bit errors */ +#define SKP_SILK_DEC_PAYLOAD_ERROR -12 + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/interface/SKP_Silk_typedef.h b/app/src/main/jni/silk/interface/SKP_Silk_typedef.h similarity index 97% rename from jni/silk/interface/SKP_Silk_typedef.h rename to app/src/main/jni/silk/interface/SKP_Silk_typedef.h index 7cb661a..8c46f7d 100644 --- a/jni/silk/interface/SKP_Silk_typedef.h +++ b/app/src/main/jni/silk/interface/SKP_Silk_typedef.h @@ -1,99 +1,99 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef _SKP_SILK_API_TYPDEF_H_ -#define _SKP_SILK_API_TYPDEF_H_ - -#ifndef SKP_USE_DOUBLE_PRECISION_FLOATS -#define SKP_USE_DOUBLE_PRECISION_FLOATS 0 -#endif - -#include -#if defined( __GNUC__ ) -#include -#endif - -#define SKP_int int /* used for counters etc; at least 16 bits */ -#define SKP_int64 long long -#define SKP_int32 int -#define SKP_int16 short -#define SKP_int8 signed char - -#define SKP_uint unsigned int /* used for counters etc; at least 16 bits */ -#define SKP_uint64 unsigned long long -#define SKP_uint32 unsigned int -#define SKP_uint16 unsigned short -#define SKP_uint8 unsigned char - -#define SKP_int_ptr_size intptr_t - -#if SKP_USE_DOUBLE_PRECISION_FLOATS -# define SKP_float double -# define SKP_float_MAX DBL_MAX -#else -# define SKP_float float -# define SKP_float_MAX FLT_MAX -#endif - -#define SKP_INLINE static __inline - -#ifdef _WIN32 -# define SKP_STR_CASEINSENSITIVE_COMPARE(x, y) _stricmp(x, y) -#else -# define SKP_STR_CASEINSENSITIVE_COMPARE(x, y) strcasecmp(x, y) -#endif - -#define SKP_int64_MAX ((SKP_int64)0x7FFFFFFFFFFFFFFFLL) // 2^63 - 1 -#define SKP_int64_MIN ((SKP_int64)0x8000000000000000LL) // -2^63 -#define SKP_int32_MAX 0x7FFFFFFF // 2^31 - 1 = 2147483647 -#define SKP_int32_MIN ((SKP_int32)0x80000000) // -2^31 = -2147483648 -#define SKP_int16_MAX 0x7FFF // 2^15 - 1 = 32767 -#define SKP_int16_MIN ((SKP_int16)0x8000) // -2^15 = -32768 -#define SKP_int8_MAX 0x7F // 2^7 - 1 = 127 -#define SKP_int8_MIN ((SKP_int8)0x80) // -2^7 = -128 - -#define SKP_uint32_MAX 0xFFFFFFFF // 2^32 - 1 = 4294967295 -#define SKP_uint32_MIN 0x00000000 -#define SKP_uint16_MAX 0xFFFF // 2^16 - 1 = 65535 -#define SKP_uint16_MIN 0x0000 -#define SKP_uint8_MAX 0xFF // 2^8 - 1 = 255 -#define SKP_uint8_MIN 0x00 - -#define SKP_TRUE 1 -#define SKP_FALSE 0 - -/* assertions */ -#if (defined _WIN32 && !defined _WINCE && !defined(__GNUC__) && !defined(NO_ASSERTS)) -# ifndef SKP_assert -# include /* ASSERTE() */ -# define SKP_assert(COND) _ASSERTE(COND) -# endif -#else -# define SKP_assert(COND) -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef _SKP_SILK_API_TYPDEF_H_ +#define _SKP_SILK_API_TYPDEF_H_ + +#ifndef SKP_USE_DOUBLE_PRECISION_FLOATS +#define SKP_USE_DOUBLE_PRECISION_FLOATS 0 +#endif + +#include +#if defined( __GNUC__ ) +#include +#endif + +#define SKP_int int /* used for counters etc; at least 16 bits */ +#define SKP_int64 long long +#define SKP_int32 int +#define SKP_int16 short +#define SKP_int8 signed char + +#define SKP_uint unsigned int /* used for counters etc; at least 16 bits */ +#define SKP_uint64 unsigned long long +#define SKP_uint32 unsigned int +#define SKP_uint16 unsigned short +#define SKP_uint8 unsigned char + +#define SKP_int_ptr_size intptr_t + +#if SKP_USE_DOUBLE_PRECISION_FLOATS +# define SKP_float double +# define SKP_float_MAX DBL_MAX +#else +# define SKP_float float +# define SKP_float_MAX FLT_MAX +#endif + +#define SKP_INLINE static __inline + +#ifdef _WIN32 +# define SKP_STR_CASEINSENSITIVE_COMPARE(x, y) _stricmp(x, y) +#else +# define SKP_STR_CASEINSENSITIVE_COMPARE(x, y) strcasecmp(x, y) +#endif + +#define SKP_int64_MAX ((SKP_int64)0x7FFFFFFFFFFFFFFFLL) // 2^63 - 1 +#define SKP_int64_MIN ((SKP_int64)0x8000000000000000LL) // -2^63 +#define SKP_int32_MAX 0x7FFFFFFF // 2^31 - 1 = 2147483647 +#define SKP_int32_MIN ((SKP_int32)0x80000000) // -2^31 = -2147483648 +#define SKP_int16_MAX 0x7FFF // 2^15 - 1 = 32767 +#define SKP_int16_MIN ((SKP_int16)0x8000) // -2^15 = -32768 +#define SKP_int8_MAX 0x7F // 2^7 - 1 = 127 +#define SKP_int8_MIN ((SKP_int8)0x80) // -2^7 = -128 + +#define SKP_uint32_MAX 0xFFFFFFFF // 2^32 - 1 = 4294967295 +#define SKP_uint32_MIN 0x00000000 +#define SKP_uint16_MAX 0xFFFF // 2^16 - 1 = 65535 +#define SKP_uint16_MIN 0x0000 +#define SKP_uint8_MAX 0xFF // 2^8 - 1 = 255 +#define SKP_uint8_MIN 0x00 + +#define SKP_TRUE 1 +#define SKP_FALSE 0 + +/* assertions */ +#if (defined _WIN32 && !defined _WINCE && !defined(__GNUC__) && !defined(NO_ASSERTS)) +# ifndef SKP_assert +# include /* ASSERTE() */ +# define SKP_assert(COND) _ASSERTE(COND) +# endif +#else +# define SKP_assert(COND) +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_A2NLSF.c b/app/src/main/jni/silk/src/SKP_Silk_A2NLSF.c similarity index 97% rename from jni/silk/src/SKP_Silk_A2NLSF.c rename to app/src/main/jni/silk/src/SKP_Silk_A2NLSF.c index f8e56ca..41e1213 100644 --- a/jni/silk/src/SKP_Silk_A2NLSF.c +++ b/app/src/main/jni/silk/src/SKP_Silk_A2NLSF.c @@ -1,280 +1,280 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* Conversion between prediction filter coefficients and NLSFs */ -/* Requires the order to be an even number */ -/* A piecewise linear approximation maps LSF <-> cos(LSF) */ -/* Therefore the result is not accurate NLSFs, but the two */ -/* function are accurate inverses of each other */ - -#include "SKP_Silk_SigProc_FIX.h" - -/* Number of binary divisions, when not in low complexity mode */ -#define BIN_DIV_STEPS_A2NLSF_FIX 2 /* must be no higher than 16 - log2( LSF_COS_TAB_SZ_FIX ) */ -#define QPoly 16 -#define MAX_ITERATIONS_A2NLSF_FIX 50 - -/* Flag for using 2x as many cosine sampling points, reduces the risk of missing a root */ -#define OVERSAMPLE_COSINE_TABLE 0 - -/* Helper function for A2NLSF(..) */ -/* Transforms polynomials from cos(n*f) to cos(f)^n */ -SKP_INLINE void SKP_Silk_A2NLSF_trans_poly( - SKP_int32 *p, /* I/O Polynomial */ - const SKP_int dd /* I Polynomial order (= filter order / 2 ) */ -) -{ - SKP_int k, n; - - for( k = 2; k <= dd; k++ ) { - for( n = dd; n > k; n-- ) { - p[ n - 2 ] -= p[ n ]; - } - p[ k - 2 ] -= SKP_LSHIFT( p[ k ], 1 ); - } -} - -/* Helper function for A2NLSF(..) */ -/* Polynomial evaluation */ -SKP_INLINE SKP_int32 SKP_Silk_A2NLSF_eval_poly( /* return the polynomial evaluation, in QPoly */ - SKP_int32 *p, /* I Polynomial, QPoly */ - const SKP_int32 x, /* I Evaluation point, Q12 */ - const SKP_int dd /* I Order */ -) -{ - SKP_int n; - SKP_int32 x_Q16, y32; - - y32 = p[ dd ]; /* QPoly */ - x_Q16 = SKP_LSHIFT( x, 4 ); - for( n = dd - 1; n >= 0; n-- ) { - y32 = SKP_SMLAWW( p[ n ], y32, x_Q16 ); /* QPoly */ - } - return y32; -} - -SKP_INLINE void SKP_Silk_A2NLSF_init( - const SKP_int32 *a_Q16, - SKP_int32 *P, - SKP_int32 *Q, - const SKP_int dd -) -{ - SKP_int k; - - /* Convert filter coefs to even and odd polynomials */ - P[dd] = SKP_LSHIFT( 1, QPoly ); - Q[dd] = SKP_LSHIFT( 1, QPoly ); - for( k = 0; k < dd; k++ ) { -#if( QPoly < 16 ) - P[ k ] = SKP_RSHIFT_ROUND( -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ], 16 - QPoly ); /* QPoly */ - Q[ k ] = SKP_RSHIFT_ROUND( -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ], 16 - QPoly ); /* QPoly */ -#elif( Qpoly == 16 ) - P[ k ] = -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ]; // QPoly - Q[ k ] = -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ]; // QPoly -#else - P[ k ] = SKP_LSHIFT( -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ], QPoly - 16 ); /* QPoly */ - Q[ k ] = SKP_LSHIFT( -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ], QPoly - 16 ); /* QPoly */ -#endif - } - - /* Divide out zeros as we have that for even filter orders, */ - /* z = 1 is always a root in Q, and */ - /* z = -1 is always a root in P */ - for( k = dd; k > 0; k-- ) { - P[ k - 1 ] -= P[ k ]; - Q[ k - 1 ] += Q[ k ]; - } - - /* Transform polynomials from cos(n*f) to cos(f)^n */ - SKP_Silk_A2NLSF_trans_poly( P, dd ); - SKP_Silk_A2NLSF_trans_poly( Q, dd ); -} - -/* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */ -/* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */ -void SKP_Silk_A2NLSF( - SKP_int *NLSF, /* O Normalized Line Spectral Frequencies, Q15 (0 - (2^15-1)), [d] */ - SKP_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */ - const SKP_int d /* I Filter order (must be even) */ -) -{ - SKP_int i, k, m, dd, root_ix, ffrac; - SKP_int32 xlo, xhi, xmid; - SKP_int32 ylo, yhi, ymid; - SKP_int32 nom, den; - SKP_int32 P[ SigProc_MAX_ORDER_LPC / 2 + 1 ]; - SKP_int32 Q[ SigProc_MAX_ORDER_LPC / 2 + 1 ]; - SKP_int32 *PQ[ 2 ]; - SKP_int32 *p; - - /* Store pointers to array */ - PQ[ 0 ] = P; - PQ[ 1 ] = Q; - - dd = SKP_RSHIFT( d, 1 ); - - SKP_Silk_A2NLSF_init( a_Q16, P, Q, dd ); - - /* Find roots, alternating between P and Q */ - p = P; /* Pointer to polynomial */ - - xlo = SKP_Silk_LSFCosTab_FIX_Q12[ 0 ]; // Q12 - ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); - - if( ylo < 0 ) { - /* Set the first NLSF to zero and move on to the next */ - NLSF[ 0 ] = 0; - p = Q; /* Pointer to polynomial */ - ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); - root_ix = 1; /* Index of current root */ - } else { - root_ix = 0; /* Index of current root */ - } - k = 1; /* Loop counter */ - i = 0; /* Counter for bandwidth expansions applied */ - while( 1 ) { - /* Evaluate polynomial */ -#if OVERSAMPLE_COSINE_TABLE - xhi = SKP_Silk_LSFCosTab_FIX_Q12[ k >> 1 ] + - ( ( SKP_Silk_LSFCosTab_FIX_Q12[ ( k + 1 ) >> 1 ] - - SKP_Silk_LSFCosTab_FIX_Q12[ k >> 1 ] ) >> 1 ); /* Q12 */ -#else - xhi = SKP_Silk_LSFCosTab_FIX_Q12[ k ]; /* Q12 */ -#endif - yhi = SKP_Silk_A2NLSF_eval_poly( p, xhi, dd ); - - /* Detect zero crossing */ - if( ( ylo <= 0 && yhi >= 0 ) || ( ylo >= 0 && yhi <= 0 ) ) { - /* Binary division */ -#if OVERSAMPLE_COSINE_TABLE - ffrac = -128; -#else - ffrac = -256; -#endif - for( m = 0; m < BIN_DIV_STEPS_A2NLSF_FIX; m++ ) { - /* Evaluate polynomial */ - xmid = SKP_RSHIFT_ROUND( xlo + xhi, 1 ); - ymid = SKP_Silk_A2NLSF_eval_poly( p, xmid, dd ); - - /* Detect zero crossing */ - if( ( ylo <= 0 && ymid >= 0 ) || ( ylo >= 0 && ymid <= 0 ) ) { - /* Reduce frequency */ - xhi = xmid; - yhi = ymid; - } else { - /* Increase frequency */ - xlo = xmid; - ylo = ymid; -#if OVERSAMPLE_COSINE_TABLE - ffrac = SKP_ADD_RSHIFT( ffrac, 64, m ); -#else - ffrac = SKP_ADD_RSHIFT( ffrac, 128, m ); -#endif - } - } - - /* Interpolate */ - if( SKP_abs( ylo ) < 65536 ) { - /* Avoid dividing by zero */ - den = ylo - yhi; - nom = SKP_LSHIFT( ylo, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) + SKP_RSHIFT( den, 1 ); - if( den != 0 ) { - ffrac += SKP_DIV32( nom, den ); - } - } else { - /* No risk of dividing by zero because abs(ylo - yhi) >= abs(ylo) >= 65536 */ - ffrac += SKP_DIV32( ylo, SKP_RSHIFT( ylo - yhi, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) ); - } -#if OVERSAMPLE_COSINE_TABLE - NLSF[ root_ix ] = (SKP_int)SKP_min_32( SKP_LSHIFT( (SKP_int32)k, 7 ) + ffrac, SKP_int16_MAX ); -#else - NLSF[ root_ix ] = (SKP_int)SKP_min_32( SKP_LSHIFT( (SKP_int32)k, 8 ) + ffrac, SKP_int16_MAX ); -#endif - - SKP_assert( NLSF[ root_ix ] >= 0 ); - SKP_assert( NLSF[ root_ix ] <= 32767 ); - - root_ix++; /* Next root */ - if( root_ix >= d ) { - /* Found all roots */ - break; - } - /* Alternate pointer to polynomial */ - p = PQ[ root_ix & 1 ]; - - /* Evaluate polynomial */ -#if OVERSAMPLE_COSINE_TABLE - xlo = SKP_Silk_LSFCosTab_FIX_Q12[ ( k - 1 ) >> 1 ] + - ( ( SKP_Silk_LSFCosTab_FIX_Q12[ k >> 1 ] - - SKP_Silk_LSFCosTab_FIX_Q12[ ( k - 1 ) >> 1 ] ) >> 1 ); // Q12 -#else - xlo = SKP_Silk_LSFCosTab_FIX_Q12[ k - 1 ]; // Q12 -#endif - ylo = SKP_LSHIFT( 1 - ( root_ix & 2 ), 12 ); - } else { - /* Increment loop counter */ - k++; - xlo = xhi; - ylo = yhi; - -#if OVERSAMPLE_COSINE_TABLE - if( k > 2 * LSF_COS_TAB_SZ_FIX ) { -#else - if( k > LSF_COS_TAB_SZ_FIX ) { -#endif - i++; - if( i > MAX_ITERATIONS_A2NLSF_FIX ) { - /* Set NLSFs to white spectrum and exit */ - NLSF[ 0 ] = SKP_DIV32_16( 1 << 15, d + 1 ); - for( k = 1; k < d; k++ ) { - NLSF[ k ] = SKP_SMULBB( k + 1, NLSF[ 0 ] ); - } - return; - } - - /* Error: Apply progressively more bandwidth expansion and run again */ - SKP_Silk_bwexpander_32( a_Q16, d, 65536 - SKP_SMULBB( 66, i ) ); // 66_Q16 = 0.001 - - SKP_Silk_A2NLSF_init( a_Q16, P, Q, dd ); - p = P; /* Pointer to polynomial */ - xlo = SKP_Silk_LSFCosTab_FIX_Q12[ 0 ]; // Q12 - ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); - if( ylo < 0 ) { - /* Set the first NLSF to zero and move on to the next */ - NLSF[ 0 ] = 0; - p = Q; /* Pointer to polynomial */ - ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); - root_ix = 1; /* Index of current root */ - } else { - root_ix = 0; /* Index of current root */ - } - k = 1; /* Reset loop counter */ - } - } - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* Conversion between prediction filter coefficients and NLSFs */ +/* Requires the order to be an even number */ +/* A piecewise linear approximation maps LSF <-> cos(LSF) */ +/* Therefore the result is not accurate NLSFs, but the two */ +/* function are accurate inverses of each other */ + +#include "SKP_Silk_SigProc_FIX.h" + +/* Number of binary divisions, when not in low complexity mode */ +#define BIN_DIV_STEPS_A2NLSF_FIX 2 /* must be no higher than 16 - log2( LSF_COS_TAB_SZ_FIX ) */ +#define QPoly 16 +#define MAX_ITERATIONS_A2NLSF_FIX 50 + +/* Flag for using 2x as many cosine sampling points, reduces the risk of missing a root */ +#define OVERSAMPLE_COSINE_TABLE 0 + +/* Helper function for A2NLSF(..) */ +/* Transforms polynomials from cos(n*f) to cos(f)^n */ +SKP_INLINE void SKP_Silk_A2NLSF_trans_poly( + SKP_int32 *p, /* I/O Polynomial */ + const SKP_int dd /* I Polynomial order (= filter order / 2 ) */ +) +{ + SKP_int k, n; + + for( k = 2; k <= dd; k++ ) { + for( n = dd; n > k; n-- ) { + p[ n - 2 ] -= p[ n ]; + } + p[ k - 2 ] -= SKP_LSHIFT( p[ k ], 1 ); + } +} + +/* Helper function for A2NLSF(..) */ +/* Polynomial evaluation */ +SKP_INLINE SKP_int32 SKP_Silk_A2NLSF_eval_poly( /* return the polynomial evaluation, in QPoly */ + SKP_int32 *p, /* I Polynomial, QPoly */ + const SKP_int32 x, /* I Evaluation point, Q12 */ + const SKP_int dd /* I Order */ +) +{ + SKP_int n; + SKP_int32 x_Q16, y32; + + y32 = p[ dd ]; /* QPoly */ + x_Q16 = SKP_LSHIFT( x, 4 ); + for( n = dd - 1; n >= 0; n-- ) { + y32 = SKP_SMLAWW( p[ n ], y32, x_Q16 ); /* QPoly */ + } + return y32; +} + +SKP_INLINE void SKP_Silk_A2NLSF_init( + const SKP_int32 *a_Q16, + SKP_int32 *P, + SKP_int32 *Q, + const SKP_int dd +) +{ + SKP_int k; + + /* Convert filter coefs to even and odd polynomials */ + P[dd] = SKP_LSHIFT( 1, QPoly ); + Q[dd] = SKP_LSHIFT( 1, QPoly ); + for( k = 0; k < dd; k++ ) { +#if( QPoly < 16 ) + P[ k ] = SKP_RSHIFT_ROUND( -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ], 16 - QPoly ); /* QPoly */ + Q[ k ] = SKP_RSHIFT_ROUND( -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ], 16 - QPoly ); /* QPoly */ +#elif( Qpoly == 16 ) + P[ k ] = -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ]; // QPoly + Q[ k ] = -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ]; // QPoly +#else + P[ k ] = SKP_LSHIFT( -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ], QPoly - 16 ); /* QPoly */ + Q[ k ] = SKP_LSHIFT( -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ], QPoly - 16 ); /* QPoly */ +#endif + } + + /* Divide out zeros as we have that for even filter orders, */ + /* z = 1 is always a root in Q, and */ + /* z = -1 is always a root in P */ + for( k = dd; k > 0; k-- ) { + P[ k - 1 ] -= P[ k ]; + Q[ k - 1 ] += Q[ k ]; + } + + /* Transform polynomials from cos(n*f) to cos(f)^n */ + SKP_Silk_A2NLSF_trans_poly( P, dd ); + SKP_Silk_A2NLSF_trans_poly( Q, dd ); +} + +/* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */ +/* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */ +void SKP_Silk_A2NLSF( + SKP_int *NLSF, /* O Normalized Line Spectral Frequencies, Q15 (0 - (2^15-1)), [d] */ + SKP_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */ + const SKP_int d /* I Filter order (must be even) */ +) +{ + SKP_int i, k, m, dd, root_ix, ffrac; + SKP_int32 xlo, xhi, xmid; + SKP_int32 ylo, yhi, ymid; + SKP_int32 nom, den; + SKP_int32 P[ SigProc_MAX_ORDER_LPC / 2 + 1 ]; + SKP_int32 Q[ SigProc_MAX_ORDER_LPC / 2 + 1 ]; + SKP_int32 *PQ[ 2 ]; + SKP_int32 *p; + + /* Store pointers to array */ + PQ[ 0 ] = P; + PQ[ 1 ] = Q; + + dd = SKP_RSHIFT( d, 1 ); + + SKP_Silk_A2NLSF_init( a_Q16, P, Q, dd ); + + /* Find roots, alternating between P and Q */ + p = P; /* Pointer to polynomial */ + + xlo = SKP_Silk_LSFCosTab_FIX_Q12[ 0 ]; // Q12 + ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); + + if( ylo < 0 ) { + /* Set the first NLSF to zero and move on to the next */ + NLSF[ 0 ] = 0; + p = Q; /* Pointer to polynomial */ + ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); + root_ix = 1; /* Index of current root */ + } else { + root_ix = 0; /* Index of current root */ + } + k = 1; /* Loop counter */ + i = 0; /* Counter for bandwidth expansions applied */ + while( 1 ) { + /* Evaluate polynomial */ +#if OVERSAMPLE_COSINE_TABLE + xhi = SKP_Silk_LSFCosTab_FIX_Q12[ k >> 1 ] + + ( ( SKP_Silk_LSFCosTab_FIX_Q12[ ( k + 1 ) >> 1 ] - + SKP_Silk_LSFCosTab_FIX_Q12[ k >> 1 ] ) >> 1 ); /* Q12 */ +#else + xhi = SKP_Silk_LSFCosTab_FIX_Q12[ k ]; /* Q12 */ +#endif + yhi = SKP_Silk_A2NLSF_eval_poly( p, xhi, dd ); + + /* Detect zero crossing */ + if( ( ylo <= 0 && yhi >= 0 ) || ( ylo >= 0 && yhi <= 0 ) ) { + /* Binary division */ +#if OVERSAMPLE_COSINE_TABLE + ffrac = -128; +#else + ffrac = -256; +#endif + for( m = 0; m < BIN_DIV_STEPS_A2NLSF_FIX; m++ ) { + /* Evaluate polynomial */ + xmid = SKP_RSHIFT_ROUND( xlo + xhi, 1 ); + ymid = SKP_Silk_A2NLSF_eval_poly( p, xmid, dd ); + + /* Detect zero crossing */ + if( ( ylo <= 0 && ymid >= 0 ) || ( ylo >= 0 && ymid <= 0 ) ) { + /* Reduce frequency */ + xhi = xmid; + yhi = ymid; + } else { + /* Increase frequency */ + xlo = xmid; + ylo = ymid; +#if OVERSAMPLE_COSINE_TABLE + ffrac = SKP_ADD_RSHIFT( ffrac, 64, m ); +#else + ffrac = SKP_ADD_RSHIFT( ffrac, 128, m ); +#endif + } + } + + /* Interpolate */ + if( SKP_abs( ylo ) < 65536 ) { + /* Avoid dividing by zero */ + den = ylo - yhi; + nom = SKP_LSHIFT( ylo, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) + SKP_RSHIFT( den, 1 ); + if( den != 0 ) { + ffrac += SKP_DIV32( nom, den ); + } + } else { + /* No risk of dividing by zero because abs(ylo - yhi) >= abs(ylo) >= 65536 */ + ffrac += SKP_DIV32( ylo, SKP_RSHIFT( ylo - yhi, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) ); + } +#if OVERSAMPLE_COSINE_TABLE + NLSF[ root_ix ] = (SKP_int)SKP_min_32( SKP_LSHIFT( (SKP_int32)k, 7 ) + ffrac, SKP_int16_MAX ); +#else + NLSF[ root_ix ] = (SKP_int)SKP_min_32( SKP_LSHIFT( (SKP_int32)k, 8 ) + ffrac, SKP_int16_MAX ); +#endif + + SKP_assert( NLSF[ root_ix ] >= 0 ); + SKP_assert( NLSF[ root_ix ] <= 32767 ); + + root_ix++; /* Next root */ + if( root_ix >= d ) { + /* Found all roots */ + break; + } + /* Alternate pointer to polynomial */ + p = PQ[ root_ix & 1 ]; + + /* Evaluate polynomial */ +#if OVERSAMPLE_COSINE_TABLE + xlo = SKP_Silk_LSFCosTab_FIX_Q12[ ( k - 1 ) >> 1 ] + + ( ( SKP_Silk_LSFCosTab_FIX_Q12[ k >> 1 ] - + SKP_Silk_LSFCosTab_FIX_Q12[ ( k - 1 ) >> 1 ] ) >> 1 ); // Q12 +#else + xlo = SKP_Silk_LSFCosTab_FIX_Q12[ k - 1 ]; // Q12 +#endif + ylo = SKP_LSHIFT( 1 - ( root_ix & 2 ), 12 ); + } else { + /* Increment loop counter */ + k++; + xlo = xhi; + ylo = yhi; + +#if OVERSAMPLE_COSINE_TABLE + if( k > 2 * LSF_COS_TAB_SZ_FIX ) { +#else + if( k > LSF_COS_TAB_SZ_FIX ) { +#endif + i++; + if( i > MAX_ITERATIONS_A2NLSF_FIX ) { + /* Set NLSFs to white spectrum and exit */ + NLSF[ 0 ] = SKP_DIV32_16( 1 << 15, d + 1 ); + for( k = 1; k < d; k++ ) { + NLSF[ k ] = SKP_SMULBB( k + 1, NLSF[ 0 ] ); + } + return; + } + + /* Error: Apply progressively more bandwidth expansion and run again */ + SKP_Silk_bwexpander_32( a_Q16, d, 65536 - SKP_SMULBB( 66, i ) ); // 66_Q16 = 0.001 + + SKP_Silk_A2NLSF_init( a_Q16, P, Q, dd ); + p = P; /* Pointer to polynomial */ + xlo = SKP_Silk_LSFCosTab_FIX_Q12[ 0 ]; // Q12 + ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); + if( ylo < 0 ) { + /* Set the first NLSF to zero and move on to the next */ + NLSF[ 0 ] = 0; + p = Q; /* Pointer to polynomial */ + ylo = SKP_Silk_A2NLSF_eval_poly( p, xlo, dd ); + root_ix = 1; /* Index of current root */ + } else { + root_ix = 0; /* Index of current root */ + } + k = 1; /* Reset loop counter */ + } + } + } +} diff --git a/jni/silk/src/SKP_Silk_CNG.c b/app/src/main/jni/silk/src/SKP_Silk_CNG.c similarity index 97% rename from jni/silk/src/SKP_Silk_CNG.c rename to app/src/main/jni/silk/src/SKP_Silk_CNG.c index 2958fad..31e4513 100644 --- a/jni/silk/src/SKP_Silk_CNG.c +++ b/app/src/main/jni/silk/src/SKP_Silk_CNG.c @@ -1,149 +1,149 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Generates excitation for CNG LPC synthesis */ -SKP_INLINE void SKP_Silk_CNG_exc( - SKP_int16 residual[], /* O CNG residual signal Q0 */ - SKP_int32 exc_buf_Q10[], /* I Random samples buffer Q10 */ - SKP_int32 Gain_Q16, /* I Gain to apply */ - SKP_int length, /* I Length */ - SKP_int32 *rand_seed /* I/O Seed to random index generator */ -) -{ - SKP_int32 seed; - SKP_int i, idx, exc_mask; - - exc_mask = CNG_BUF_MASK_MAX; - while( exc_mask > length ) { - exc_mask = SKP_RSHIFT( exc_mask, 1 ); - } - - seed = *rand_seed; - for( i = 0; i < length; i++ ) { - seed = SKP_RAND( seed ); - idx = ( SKP_int )( SKP_RSHIFT( seed, 24 ) & exc_mask ); - SKP_assert( idx >= 0 ); - SKP_assert( idx <= CNG_BUF_MASK_MAX ); - residual[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( exc_buf_Q10[ idx ], Gain_Q16 ), 10 ) ); - } - *rand_seed = seed; -} - -void SKP_Silk_CNG_Reset( - SKP_Silk_decoder_state *psDec /* I/O Decoder state */ -) -{ - SKP_int i, NLSF_step_Q15, NLSF_acc_Q15; - - NLSF_step_Q15 = SKP_DIV32_16( SKP_int16_MAX, psDec->LPC_order + 1 ); - NLSF_acc_Q15 = 0; - for( i = 0; i < psDec->LPC_order; i++ ) { - NLSF_acc_Q15 += NLSF_step_Q15; - psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15; - } - psDec->sCNG.CNG_smth_Gain_Q16 = 0; - psDec->sCNG.rand_seed = 3176576; -} - -/* Updates CNG estimate, and applies the CNG when packet was lost */ -void SKP_Silk_CNG( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* I/O Signal */ - SKP_int length /* I Length of residual */ -) -{ - SKP_int i, subfr; - SKP_int32 tmp_32, Gain_Q26, max_Gain_Q16; - SKP_int16 LPC_buf[ MAX_LPC_ORDER ]; - SKP_int16 CNG_sig[ MAX_FRAME_LENGTH ]; - SKP_Silk_CNG_struct *psCNG; - psCNG = &psDec->sCNG; - - if( psDec->fs_kHz != psCNG->fs_kHz ) { - /* Reset state */ - SKP_Silk_CNG_Reset( psDec ); - - psCNG->fs_kHz = psDec->fs_kHz; - } - if( psDec->lossCnt == 0 && psDec->vadFlag == NO_VOICE_ACTIVITY ) { - /* Update CNG parameters */ - - /* Smoothing of LSF's */ - for( i = 0; i < psDec->LPC_order; i++ ) { - psCNG->CNG_smth_NLSF_Q15[ i ] += SKP_SMULWB( psDec->prevNLSF_Q15[ i ] - psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 ); - } - /* Find the subframe with the highest gain */ - max_Gain_Q16 = 0; - subfr = 0; - for( i = 0; i < NB_SUBFR; i++ ) { - if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) { - max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ]; - subfr = i; - } - } - /* Update CNG excitation buffer with excitation from this subframe */ - SKP_memmove( &psCNG->CNG_exc_buf_Q10[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q10, ( NB_SUBFR - 1 ) * psDec->subfr_length * sizeof( SKP_int32 ) ); - SKP_memcpy( psCNG->CNG_exc_buf_Q10, &psDec->exc_Q10[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( SKP_int32 ) ); - - /* Smooth gains */ - for( i = 0; i < NB_SUBFR; i++ ) { - psCNG->CNG_smth_Gain_Q16 += SKP_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 ); - } - } - - /* Add CNG when packet is lost and / or when low speech activity */ - if( psDec->lossCnt ) {//|| psDec->vadFlag == NO_VOICE_ACTIVITY ) { - - /* Generate CNG excitation */ - SKP_Silk_CNG_exc( CNG_sig, psCNG->CNG_exc_buf_Q10, - psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); - - /* Convert CNG NLSF to filter representation */ - SKP_Silk_NLSF2A_stable( LPC_buf, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order ); - - Gain_Q26 = ( SKP_int32 )1 << 26; /* 1.0 */ - - /* Generate CNG signal, by synthesis filtering */ - if( psDec->LPC_order == 16 ) { - SKP_Silk_LPC_synthesis_order16( CNG_sig, LPC_buf, - Gain_Q26, psCNG->CNG_synth_state, CNG_sig, length ); - } else { - SKP_Silk_LPC_synthesis_filter( CNG_sig, LPC_buf, - Gain_Q26, psCNG->CNG_synth_state, CNG_sig, length, psDec->LPC_order ); - } - /* Mix with signal */ - for( i = 0; i < length; i++ ) { - tmp_32 = signal[ i ] + CNG_sig[ i ]; - signal[ i ] = SKP_SAT16( tmp_32 ); - } - } else { - SKP_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( SKP_int32 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Generates excitation for CNG LPC synthesis */ +SKP_INLINE void SKP_Silk_CNG_exc( + SKP_int16 residual[], /* O CNG residual signal Q0 */ + SKP_int32 exc_buf_Q10[], /* I Random samples buffer Q10 */ + SKP_int32 Gain_Q16, /* I Gain to apply */ + SKP_int length, /* I Length */ + SKP_int32 *rand_seed /* I/O Seed to random index generator */ +) +{ + SKP_int32 seed; + SKP_int i, idx, exc_mask; + + exc_mask = CNG_BUF_MASK_MAX; + while( exc_mask > length ) { + exc_mask = SKP_RSHIFT( exc_mask, 1 ); + } + + seed = *rand_seed; + for( i = 0; i < length; i++ ) { + seed = SKP_RAND( seed ); + idx = ( SKP_int )( SKP_RSHIFT( seed, 24 ) & exc_mask ); + SKP_assert( idx >= 0 ); + SKP_assert( idx <= CNG_BUF_MASK_MAX ); + residual[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( exc_buf_Q10[ idx ], Gain_Q16 ), 10 ) ); + } + *rand_seed = seed; +} + +void SKP_Silk_CNG_Reset( + SKP_Silk_decoder_state *psDec /* I/O Decoder state */ +) +{ + SKP_int i, NLSF_step_Q15, NLSF_acc_Q15; + + NLSF_step_Q15 = SKP_DIV32_16( SKP_int16_MAX, psDec->LPC_order + 1 ); + NLSF_acc_Q15 = 0; + for( i = 0; i < psDec->LPC_order; i++ ) { + NLSF_acc_Q15 += NLSF_step_Q15; + psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15; + } + psDec->sCNG.CNG_smth_Gain_Q16 = 0; + psDec->sCNG.rand_seed = 3176576; +} + +/* Updates CNG estimate, and applies the CNG when packet was lost */ +void SKP_Silk_CNG( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* I/O Signal */ + SKP_int length /* I Length of residual */ +) +{ + SKP_int i, subfr; + SKP_int32 tmp_32, Gain_Q26, max_Gain_Q16; + SKP_int16 LPC_buf[ MAX_LPC_ORDER ]; + SKP_int16 CNG_sig[ MAX_FRAME_LENGTH ]; + SKP_Silk_CNG_struct *psCNG; + psCNG = &psDec->sCNG; + + if( psDec->fs_kHz != psCNG->fs_kHz ) { + /* Reset state */ + SKP_Silk_CNG_Reset( psDec ); + + psCNG->fs_kHz = psDec->fs_kHz; + } + if( psDec->lossCnt == 0 && psDec->vadFlag == NO_VOICE_ACTIVITY ) { + /* Update CNG parameters */ + + /* Smoothing of LSF's */ + for( i = 0; i < psDec->LPC_order; i++ ) { + psCNG->CNG_smth_NLSF_Q15[ i ] += SKP_SMULWB( psDec->prevNLSF_Q15[ i ] - psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 ); + } + /* Find the subframe with the highest gain */ + max_Gain_Q16 = 0; + subfr = 0; + for( i = 0; i < NB_SUBFR; i++ ) { + if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) { + max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ]; + subfr = i; + } + } + /* Update CNG excitation buffer with excitation from this subframe */ + SKP_memmove( &psCNG->CNG_exc_buf_Q10[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q10, ( NB_SUBFR - 1 ) * psDec->subfr_length * sizeof( SKP_int32 ) ); + SKP_memcpy( psCNG->CNG_exc_buf_Q10, &psDec->exc_Q10[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( SKP_int32 ) ); + + /* Smooth gains */ + for( i = 0; i < NB_SUBFR; i++ ) { + psCNG->CNG_smth_Gain_Q16 += SKP_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 ); + } + } + + /* Add CNG when packet is lost and / or when low speech activity */ + if( psDec->lossCnt ) {//|| psDec->vadFlag == NO_VOICE_ACTIVITY ) { + + /* Generate CNG excitation */ + SKP_Silk_CNG_exc( CNG_sig, psCNG->CNG_exc_buf_Q10, + psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); + + /* Convert CNG NLSF to filter representation */ + SKP_Silk_NLSF2A_stable( LPC_buf, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order ); + + Gain_Q26 = ( SKP_int32 )1 << 26; /* 1.0 */ + + /* Generate CNG signal, by synthesis filtering */ + if( psDec->LPC_order == 16 ) { + SKP_Silk_LPC_synthesis_order16( CNG_sig, LPC_buf, + Gain_Q26, psCNG->CNG_synth_state, CNG_sig, length ); + } else { + SKP_Silk_LPC_synthesis_filter( CNG_sig, LPC_buf, + Gain_Q26, psCNG->CNG_synth_state, CNG_sig, length, psDec->LPC_order ); + } + /* Mix with signal */ + for( i = 0; i < length; i++ ) { + tmp_32 = signal[ i ] + CNG_sig[ i ]; + signal[ i ] = SKP_SAT16( tmp_32 ); + } + } else { + SKP_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( SKP_int32 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_HP_variable_cutoff_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_HP_variable_cutoff_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_HP_variable_cutoff_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_HP_variable_cutoff_FIX.c index 68356fa..1d74184 100644 --- a/jni/silk/src/SKP_Silk_HP_variable_cutoff_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_HP_variable_cutoff_FIX.c @@ -1,118 +1,118 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -#if HIGH_PASS_INPUT - -#define SKP_RADIANS_CONSTANT_Q19 1482 // 0.45f * 2.0f * 3.14159265359 / 1000 -#define SKP_LOG2_VARIABLE_HP_MIN_FREQ_Q7 809 // log(80) in Q7 - -/* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */ -void SKP_Silk_HP_variable_cutoff_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */ - SKP_int16 *out, /* O high-pass filtered output signal */ - const SKP_int16 *in /* I input signal */ -) -{ - SKP_int quality_Q15; - SKP_int32 B_Q28[ 3 ], A_Q28[ 2 ]; - SKP_int32 Fc_Q19, r_Q28, r_Q22; - SKP_int32 pitch_freq_Hz_Q16, pitch_freq_log_Q7, delta_freq_Q7; - - /*********************************************/ - /* Estimate Low End of Pitch Frequency Range */ - /*********************************************/ - if( psEnc->sCmn.prev_sigtype == SIG_TYPE_VOICED ) { - /* difference, in log domain */ - pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEnc->sCmn.fs_kHz, 1000 ), 16 ), psEnc->sCmn.prevLag ); - pitch_freq_log_Q7 = SKP_Silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 ); //0x70 - - /* adjustment based on quality */ - quality_Q15 = psEncCtrl->input_quality_bands_Q15[ 0 ]; - pitch_freq_log_Q7 = SKP_SUB32( pitch_freq_log_Q7, SKP_SMULWB( SKP_SMULWB( SKP_LSHIFT( quality_Q15, 2 ), quality_Q15 ), - pitch_freq_log_Q7 - SKP_LOG2_VARIABLE_HP_MIN_FREQ_Q7 ) ); - pitch_freq_log_Q7 = SKP_ADD32( pitch_freq_log_Q7, SKP_RSHIFT( 19661 - quality_Q15, 9 ) ); // 19661_Q15 = 0.6_Q0 - - //delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; - delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEnc->variable_HP_smth1_Q15, 8 ); - if( delta_freq_Q7 < 0 ) { - /* less smoothing for decreasing pitch frequency, to track something close to the minimum */ - delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 ); - } - - /* limit delta, to reduce impact of outliers */ - delta_freq_Q7 = SKP_LIMIT( delta_freq_Q7, -VARIABLE_HP_MAX_DELTA_FREQ_Q7, VARIABLE_HP_MAX_DELTA_FREQ_Q7 ); - - /* update smoother */ - psEnc->variable_HP_smth1_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth1_Q15, - SKP_MUL( SKP_LSHIFT( psEnc->speech_activity_Q8, 1 ), delta_freq_Q7 ), VARIABLE_HP_SMTH_COEF1_Q16 ); - } - /* second smoother */ - psEnc->variable_HP_smth2_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth2_Q15, - psEnc->variable_HP_smth1_Q15 - psEnc->variable_HP_smth2_Q15, VARIABLE_HP_SMTH_COEF2_Q16 ); - - /* convert from log scale to Hertz */ - psEncCtrl->pitch_freq_low_Hz = SKP_Silk_log2lin( SKP_RSHIFT( psEnc->variable_HP_smth2_Q15, 8 ) ); //pow( 2.0, psEnc->variable_HP_smth2 ); - - /* limit frequency range */ - psEncCtrl->pitch_freq_low_Hz = SKP_LIMIT( psEncCtrl->pitch_freq_low_Hz, VARIABLE_HP_MIN_FREQ_Q0, VARIABLE_HP_MAX_FREQ_Q0 ); - - /********************************/ - /* Compute Filter Coefficients */ - /********************************/ - /* compute cut-off frequency, in radians */ - //Fc_num = (SKP_float)( 0.45f * 2.0f * 3.14159265359 * psEncCtrl->pitch_freq_low_Hz ); - //Fc_denom = (SKP_float)( 1e3f * psEnc->sCmn.fs_kHz ); - SKP_assert( psEncCtrl->pitch_freq_low_Hz <= SKP_int32_MAX / SKP_RADIANS_CONSTANT_Q19 ); - Fc_Q19 = SKP_DIV32_16( SKP_SMULBB( SKP_RADIANS_CONSTANT_Q19, psEncCtrl->pitch_freq_low_Hz ), psEnc->sCmn.fs_kHz ); // range: 3704 - 27787, 11-15 bits - SKP_assert( Fc_Q19 >= 3704 ); - SKP_assert( Fc_Q19 <= 27787 ); - - r_Q28 = ( 1 << 28 ) - SKP_MUL( 471, Fc_Q19 ); // 471_Q9 = 0.92_Q0, range: 255347779 to 266690872, 27-28 bits - SKP_assert( r_Q28 >= 255347779 ); - SKP_assert( r_Q28 <= 266690872 ); - - /* b = r * [ 1; -2; 1 ]; */ - /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ - B_Q28[ 0 ] = r_Q28; - B_Q28[ 1 ] = SKP_LSHIFT( -r_Q28, 1 ); - B_Q28[ 2 ] = r_Q28; - - // -r * ( 2 - Fc * Fc ); - r_Q22 = SKP_RSHIFT( r_Q28, 6 ); - A_Q28[ 0 ] = SKP_SMULWW( r_Q22, SKP_SMULWW( Fc_Q19, Fc_Q19 ) - ( 2 << 22 ) ); - A_Q28[ 1 ] = SKP_SMULWW( r_Q22, r_Q22 ); - - /********************************/ - /* High-Pass Filter */ - /********************************/ - SKP_Silk_biquad_alt( in, B_Q28, A_Q28, psEnc->sCmn.In_HP_State, out, psEnc->sCmn.frame_length ); -} - -#endif // HIGH_PASS_INPUT +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +#if HIGH_PASS_INPUT + +#define SKP_RADIANS_CONSTANT_Q19 1482 // 0.45f * 2.0f * 3.14159265359 / 1000 +#define SKP_LOG2_VARIABLE_HP_MIN_FREQ_Q7 809 // log(80) in Q7 + +/* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */ +void SKP_Silk_HP_variable_cutoff_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */ + SKP_int16 *out, /* O high-pass filtered output signal */ + const SKP_int16 *in /* I input signal */ +) +{ + SKP_int quality_Q15; + SKP_int32 B_Q28[ 3 ], A_Q28[ 2 ]; + SKP_int32 Fc_Q19, r_Q28, r_Q22; + SKP_int32 pitch_freq_Hz_Q16, pitch_freq_log_Q7, delta_freq_Q7; + + /*********************************************/ + /* Estimate Low End of Pitch Frequency Range */ + /*********************************************/ + if( psEnc->sCmn.prev_sigtype == SIG_TYPE_VOICED ) { + /* difference, in log domain */ + pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEnc->sCmn.fs_kHz, 1000 ), 16 ), psEnc->sCmn.prevLag ); + pitch_freq_log_Q7 = SKP_Silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 ); //0x70 + + /* adjustment based on quality */ + quality_Q15 = psEncCtrl->input_quality_bands_Q15[ 0 ]; + pitch_freq_log_Q7 = SKP_SUB32( pitch_freq_log_Q7, SKP_SMULWB( SKP_SMULWB( SKP_LSHIFT( quality_Q15, 2 ), quality_Q15 ), + pitch_freq_log_Q7 - SKP_LOG2_VARIABLE_HP_MIN_FREQ_Q7 ) ); + pitch_freq_log_Q7 = SKP_ADD32( pitch_freq_log_Q7, SKP_RSHIFT( 19661 - quality_Q15, 9 ) ); // 19661_Q15 = 0.6_Q0 + + //delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; + delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEnc->variable_HP_smth1_Q15, 8 ); + if( delta_freq_Q7 < 0 ) { + /* less smoothing for decreasing pitch frequency, to track something close to the minimum */ + delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 ); + } + + /* limit delta, to reduce impact of outliers */ + delta_freq_Q7 = SKP_LIMIT( delta_freq_Q7, -VARIABLE_HP_MAX_DELTA_FREQ_Q7, VARIABLE_HP_MAX_DELTA_FREQ_Q7 ); + + /* update smoother */ + psEnc->variable_HP_smth1_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth1_Q15, + SKP_MUL( SKP_LSHIFT( psEnc->speech_activity_Q8, 1 ), delta_freq_Q7 ), VARIABLE_HP_SMTH_COEF1_Q16 ); + } + /* second smoother */ + psEnc->variable_HP_smth2_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth2_Q15, + psEnc->variable_HP_smth1_Q15 - psEnc->variable_HP_smth2_Q15, VARIABLE_HP_SMTH_COEF2_Q16 ); + + /* convert from log scale to Hertz */ + psEncCtrl->pitch_freq_low_Hz = SKP_Silk_log2lin( SKP_RSHIFT( psEnc->variable_HP_smth2_Q15, 8 ) ); //pow( 2.0, psEnc->variable_HP_smth2 ); + + /* limit frequency range */ + psEncCtrl->pitch_freq_low_Hz = SKP_LIMIT( psEncCtrl->pitch_freq_low_Hz, VARIABLE_HP_MIN_FREQ_Q0, VARIABLE_HP_MAX_FREQ_Q0 ); + + /********************************/ + /* Compute Filter Coefficients */ + /********************************/ + /* compute cut-off frequency, in radians */ + //Fc_num = (SKP_float)( 0.45f * 2.0f * 3.14159265359 * psEncCtrl->pitch_freq_low_Hz ); + //Fc_denom = (SKP_float)( 1e3f * psEnc->sCmn.fs_kHz ); + SKP_assert( psEncCtrl->pitch_freq_low_Hz <= SKP_int32_MAX / SKP_RADIANS_CONSTANT_Q19 ); + Fc_Q19 = SKP_DIV32_16( SKP_SMULBB( SKP_RADIANS_CONSTANT_Q19, psEncCtrl->pitch_freq_low_Hz ), psEnc->sCmn.fs_kHz ); // range: 3704 - 27787, 11-15 bits + SKP_assert( Fc_Q19 >= 3704 ); + SKP_assert( Fc_Q19 <= 27787 ); + + r_Q28 = ( 1 << 28 ) - SKP_MUL( 471, Fc_Q19 ); // 471_Q9 = 0.92_Q0, range: 255347779 to 266690872, 27-28 bits + SKP_assert( r_Q28 >= 255347779 ); + SKP_assert( r_Q28 <= 266690872 ); + + /* b = r * [ 1; -2; 1 ]; */ + /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ + B_Q28[ 0 ] = r_Q28; + B_Q28[ 1 ] = SKP_LSHIFT( -r_Q28, 1 ); + B_Q28[ 2 ] = r_Q28; + + // -r * ( 2 - Fc * Fc ); + r_Q22 = SKP_RSHIFT( r_Q28, 6 ); + A_Q28[ 0 ] = SKP_SMULWW( r_Q22, SKP_SMULWW( Fc_Q19, Fc_Q19 ) - ( 2 << 22 ) ); + A_Q28[ 1 ] = SKP_SMULWW( r_Q22, r_Q22 ); + + /********************************/ + /* High-Pass Filter */ + /********************************/ + SKP_Silk_biquad_alt( in, B_Q28, A_Q28, psEnc->sCmn.In_HP_State, out, psEnc->sCmn.frame_length ); +} + +#endif // HIGH_PASS_INPUT diff --git a/jni/silk/src/SKP_Silk_Inlines.h b/app/src/main/jni/silk/src/SKP_Silk_Inlines.h similarity index 97% rename from jni/silk/src/SKP_Silk_Inlines.h rename to app/src/main/jni/silk/src/SKP_Silk_Inlines.h index 66a09bd..e4c0b86 100644 --- a/jni/silk/src/SKP_Silk_Inlines.h +++ b/app/src/main/jni/silk/src/SKP_Silk_Inlines.h @@ -1,280 +1,280 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/*! \file SKP_Silk_Inlines.h - * \brief SigProcFix_Inlines.h defines inline signal processing functions. - */ - -#ifndef _SKP_SILK_FIX_INLINES_H_ -#define _SKP_SILK_FIX_INLINES_H_ - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* count leading zeros of SKP_int64 */ -SKP_INLINE SKP_int32 SKP_Silk_CLZ64(SKP_int64 in) -{ - SKP_int32 in_upper; - - in_upper = (SKP_int32)SKP_RSHIFT64(in, 32); - if (in_upper == 0) { - /* Search in the lower 32 bits */ - return 32 + SKP_Silk_CLZ32( (SKP_int32) in ); - } else { - /* Search in the upper 32 bits */ - return SKP_Silk_CLZ32( in_upper ); - } -} - -/* get number of leading zeros and fractional part (the bits right after the leading one */ -SKP_INLINE void SKP_Silk_CLZ_FRAC(SKP_int32 in, /* I: input */ - SKP_int32 *lz, /* O: number of leading zeros */ - SKP_int32 *frac_Q7) /* O: the 7 bits right after the leading one */ -{ - SKP_int32 leadingZeros; - - leadingZeros = SKP_Silk_CLZ32(in); - *lz = leadingZeros; - if( leadingZeros < 24 ) { - *frac_Q7 = SKP_RSHIFT(in, 24 - leadingZeros) & 0x7F; - } else { - *frac_Q7 = SKP_LSHIFT(in, leadingZeros - 24) & 0x7F; - } -} - -/* Approximation of square root */ -/* Accuracy: < +/- 10% for output values > 15 */ -/* < +/- 2.5% for output values > 120 */ -SKP_INLINE SKP_int32 SKP_Silk_SQRT_APPROX(SKP_int32 x) -{ - SKP_int32 y, lz, frac_Q7; - - if( x <= 0 ) { - return 0; - } - - SKP_Silk_CLZ_FRAC(x, &lz, &frac_Q7); - - if( lz & 1 ) { - y = 32768; - } else { - y = 46214; /* 46214 = sqrt(2) * 32768 */ - } - - /* get scaling right */ - y >>= SKP_RSHIFT(lz, 1); - - /* increment using fractional part of input */ - y = SKP_SMLAWB(y, y, SKP_SMULBB(213, frac_Q7)); - - return y; -} - -/* returns the number of left shifts before overflow for a 16 bit number (ITU definition with norm(0)=0) */ -SKP_INLINE SKP_int32 SKP_Silk_norm16(SKP_int16 a) { - - SKP_int32 a32; - - /* if ((a == 0) || (a == SKP_int16_MIN)) return(0); */ - if ((a << 1) == 0) return(0); - - a32 = a; - /* if (a32 < 0) a32 = -a32 - 1; */ - a32 ^= SKP_RSHIFT(a32, 31); - - return SKP_Silk_CLZ32(a32) - 17; -} - -/* returns the number of left shifts before overflow for a 32 bit number (ITU definition with norm(0)=0) */ -SKP_INLINE SKP_int32 SKP_Silk_norm32(SKP_int32 a) { - - /* if ((a == 0) || (a == SKP_int32_MIN)) return(0); */ - if ((a << 1) == 0) return(0); - - /* if (a < 0) a = -a - 1; */ - a ^= SKP_RSHIFT(a, 31); - - return SKP_Silk_CLZ32(a) - 1; -} - -/* Divide two int32 values and return result as int32 in a given Q-domain */ -SKP_INLINE SKP_int32 SKP_DIV32_varQ( /* O returns a good approximation of "(a32 << Qres) / b32" */ - const SKP_int32 a32, /* I numerator (Q0) */ - const SKP_int32 b32, /* I denominator (Q0) */ - const SKP_int Qres /* I Q-domain of result (>= 0) */ -) -{ - SKP_int a_headrm, b_headrm, lshift; - SKP_int32 b32_inv, a32_nrm, b32_nrm, result; - - SKP_assert( b32 != 0 ); - SKP_assert( Qres >= 0 ); - - /* Compute number of bits head room and normalize inputs */ - a_headrm = SKP_Silk_CLZ32( SKP_abs(a32) ) - 1; - a32_nrm = SKP_LSHIFT(a32, a_headrm); /* Q: a_headrm */ - b_headrm = SKP_Silk_CLZ32( SKP_abs(b32) ) - 1; - b32_nrm = SKP_LSHIFT(b32, b_headrm); /* Q: b_headrm */ - - /* Inverse of b32, with 14 bits of precision */ - b32_inv = SKP_DIV32_16( SKP_int32_MAX >> 2, SKP_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ - - /* First approximation */ - result = SKP_SMULWB(a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ - - /* Compute residual by subtracting product of denominator and first approximation */ - a32_nrm -= SKP_LSHIFT_ovflw( SKP_SMMUL(b32_nrm, result), 3 ); /* Q: a_headrm */ - - /* Refinement */ - result = SKP_SMLAWB(result, a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ - - /* Convert to Qres domain */ - lshift = 29 + a_headrm - b_headrm - Qres; - if( lshift <= 0 ) { - return SKP_LSHIFT_SAT32(result, -lshift); - } else { - if( lshift < 32){ - return SKP_RSHIFT(result, lshift); - } else { - /* Avoid undefined result */ - return 0; - } - } -} - -/* Invert int32 value and return result as int32 in a given Q-domain */ -SKP_INLINE SKP_int32 SKP_INVERSE32_varQ( /* O returns a good approximation of "(1 << Qres) / b32" */ - const SKP_int32 b32, /* I denominator (Q0) */ - const SKP_int Qres /* I Q-domain of result (> 0) */ -) -{ - SKP_int b_headrm, lshift; - SKP_int32 b32_inv, b32_nrm, err_Q32, result; - - SKP_assert( b32 != 0 ); - SKP_assert( Qres > 0 ); - - /* Compute number of bits head room and normalize input */ - b_headrm = SKP_Silk_CLZ32( SKP_abs(b32) ) - 1; - b32_nrm = SKP_LSHIFT(b32, b_headrm); /* Q: b_headrm */ - - /* Inverse of b32, with 14 bits of precision */ - b32_inv = SKP_DIV32_16( SKP_int32_MAX >> 2, SKP_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ - - /* First approximation */ - result = SKP_LSHIFT(b32_inv, 16); /* Q: 61 - b_headrm */ - - /* Compute residual by subtracting product of denominator and first approximation from one */ - err_Q32 = SKP_LSHIFT_ovflw( -SKP_SMULWB(b32_nrm, b32_inv), 3 ); /* Q32 */ - - /* Refinement */ - result = SKP_SMLAWW(result, err_Q32, b32_inv); /* Q: 61 - b_headrm */ - - /* Convert to Qres domain */ - lshift = 61 - b_headrm - Qres; - if( lshift <= 0 ) { - return SKP_LSHIFT_SAT32(result, -lshift); - } else { - if( lshift < 32){ - return SKP_RSHIFT(result, lshift); - }else{ - /* Avoid undefined result */ - return 0; - } - } -} - -#define SKP_SIN_APPROX_CONST0 (1073735400) -#define SKP_SIN_APPROX_CONST1 (-82778932) -#define SKP_SIN_APPROX_CONST2 (1059577) -#define SKP_SIN_APPROX_CONST3 (-5013) - -/* Sine approximation; an input of 65536 corresponds to 2 * pi */ -/* Uses polynomial expansion of the input to the power 0, 2, 4 and 6 */ -/* The relative error is below 1e-5 */ -SKP_INLINE SKP_int32 SKP_Silk_SIN_APPROX_Q24( /* O returns approximately 2^24 * sin(x * 2 * pi / 65536) */ - SKP_int32 x -) -{ - SKP_int y_Q30; - - /* Keep only bottom 16 bits (the function repeats itself with period 65536) */ - x &= 65535; - - /* Split range in four quadrants */ - if( x <= 32768 ) { - if( x < 16384 ) { - /* Return cos(pi/2 - x) */ - x = 16384 - x; - } else { - /* Return cos(x - pi/2) */ - x -= 16384; - } - if( x < 1100 ) { - /* Special case: high accuracy */ - return SKP_SMLAWB( 1 << 24, SKP_MUL( x, x ), -5053 ); - } - x = SKP_SMULWB( SKP_LSHIFT( x, 8 ), x ); /* contains x^2 in Q20 */ - y_Q30 = SKP_SMLAWB( SKP_SIN_APPROX_CONST2, x, SKP_SIN_APPROX_CONST3 ); - y_Q30 = SKP_SMLAWW( SKP_SIN_APPROX_CONST1, x, y_Q30 ); - y_Q30 = SKP_SMLAWW( SKP_SIN_APPROX_CONST0 + 66, x, y_Q30 ); - } else { - if( x < 49152 ) { - /* Return -cos(3*pi/2 - x) */ - x = 49152 - x; - } else { - /* Return -cos(x - 3*pi/2) */ - x -= 49152; - } - if( x < 1100 ) { - /* Special case: high accuracy */ - return SKP_SMLAWB( -1 << 24, SKP_MUL( x, x ), 5053 ); - } - x = SKP_SMULWB( SKP_LSHIFT( x, 8 ), x ); /* contains x^2 in Q20 */ - y_Q30 = SKP_SMLAWB( -SKP_SIN_APPROX_CONST2, x, -SKP_SIN_APPROX_CONST3 ); - y_Q30 = SKP_SMLAWW( -SKP_SIN_APPROX_CONST1, x, y_Q30 ); - y_Q30 = SKP_SMLAWW( -SKP_SIN_APPROX_CONST0, x, y_Q30 ); - } - return SKP_RSHIFT_ROUND( y_Q30, 6 ); -} - -/* Cosine approximation; an input of 65536 corresponds to 2 * pi */ -/* The relative error is below 1e-5 */ -SKP_INLINE SKP_int32 SKP_Silk_COS_APPROX_Q24( /* O returns approximately 2^24 * cos(x * 2 * pi / 65536) */ - SKP_int32 x -) -{ - return SKP_Silk_SIN_APPROX_Q24( x + 16384 ); -} - -#ifdef __cplusplus -} -#endif - -#endif //_SKP_SILK_FIX_INLINES_H_ +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/*! \file SKP_Silk_Inlines.h + * \brief SigProcFix_Inlines.h defines inline signal processing functions. + */ + +#ifndef _SKP_SILK_FIX_INLINES_H_ +#define _SKP_SILK_FIX_INLINES_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* count leading zeros of SKP_int64 */ +SKP_INLINE SKP_int32 SKP_Silk_CLZ64(SKP_int64 in) +{ + SKP_int32 in_upper; + + in_upper = (SKP_int32)SKP_RSHIFT64(in, 32); + if (in_upper == 0) { + /* Search in the lower 32 bits */ + return 32 + SKP_Silk_CLZ32( (SKP_int32) in ); + } else { + /* Search in the upper 32 bits */ + return SKP_Silk_CLZ32( in_upper ); + } +} + +/* get number of leading zeros and fractional part (the bits right after the leading one */ +SKP_INLINE void SKP_Silk_CLZ_FRAC(SKP_int32 in, /* I: input */ + SKP_int32 *lz, /* O: number of leading zeros */ + SKP_int32 *frac_Q7) /* O: the 7 bits right after the leading one */ +{ + SKP_int32 leadingZeros; + + leadingZeros = SKP_Silk_CLZ32(in); + *lz = leadingZeros; + if( leadingZeros < 24 ) { + *frac_Q7 = SKP_RSHIFT(in, 24 - leadingZeros) & 0x7F; + } else { + *frac_Q7 = SKP_LSHIFT(in, leadingZeros - 24) & 0x7F; + } +} + +/* Approximation of square root */ +/* Accuracy: < +/- 10% for output values > 15 */ +/* < +/- 2.5% for output values > 120 */ +SKP_INLINE SKP_int32 SKP_Silk_SQRT_APPROX(SKP_int32 x) +{ + SKP_int32 y, lz, frac_Q7; + + if( x <= 0 ) { + return 0; + } + + SKP_Silk_CLZ_FRAC(x, &lz, &frac_Q7); + + if( lz & 1 ) { + y = 32768; + } else { + y = 46214; /* 46214 = sqrt(2) * 32768 */ + } + + /* get scaling right */ + y >>= SKP_RSHIFT(lz, 1); + + /* increment using fractional part of input */ + y = SKP_SMLAWB(y, y, SKP_SMULBB(213, frac_Q7)); + + return y; +} + +/* returns the number of left shifts before overflow for a 16 bit number (ITU definition with norm(0)=0) */ +SKP_INLINE SKP_int32 SKP_Silk_norm16(SKP_int16 a) { + + SKP_int32 a32; + + /* if ((a == 0) || (a == SKP_int16_MIN)) return(0); */ + if ((a << 1) == 0) return(0); + + a32 = a; + /* if (a32 < 0) a32 = -a32 - 1; */ + a32 ^= SKP_RSHIFT(a32, 31); + + return SKP_Silk_CLZ32(a32) - 17; +} + +/* returns the number of left shifts before overflow for a 32 bit number (ITU definition with norm(0)=0) */ +SKP_INLINE SKP_int32 SKP_Silk_norm32(SKP_int32 a) { + + /* if ((a == 0) || (a == SKP_int32_MIN)) return(0); */ + if ((a << 1) == 0) return(0); + + /* if (a < 0) a = -a - 1; */ + a ^= SKP_RSHIFT(a, 31); + + return SKP_Silk_CLZ32(a) - 1; +} + +/* Divide two int32 values and return result as int32 in a given Q-domain */ +SKP_INLINE SKP_int32 SKP_DIV32_varQ( /* O returns a good approximation of "(a32 << Qres) / b32" */ + const SKP_int32 a32, /* I numerator (Q0) */ + const SKP_int32 b32, /* I denominator (Q0) */ + const SKP_int Qres /* I Q-domain of result (>= 0) */ +) +{ + SKP_int a_headrm, b_headrm, lshift; + SKP_int32 b32_inv, a32_nrm, b32_nrm, result; + + SKP_assert( b32 != 0 ); + SKP_assert( Qres >= 0 ); + + /* Compute number of bits head room and normalize inputs */ + a_headrm = SKP_Silk_CLZ32( SKP_abs(a32) ) - 1; + a32_nrm = SKP_LSHIFT(a32, a_headrm); /* Q: a_headrm */ + b_headrm = SKP_Silk_CLZ32( SKP_abs(b32) ) - 1; + b32_nrm = SKP_LSHIFT(b32, b_headrm); /* Q: b_headrm */ + + /* Inverse of b32, with 14 bits of precision */ + b32_inv = SKP_DIV32_16( SKP_int32_MAX >> 2, SKP_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ + + /* First approximation */ + result = SKP_SMULWB(a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ + + /* Compute residual by subtracting product of denominator and first approximation */ + a32_nrm -= SKP_LSHIFT_ovflw( SKP_SMMUL(b32_nrm, result), 3 ); /* Q: a_headrm */ + + /* Refinement */ + result = SKP_SMLAWB(result, a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ + + /* Convert to Qres domain */ + lshift = 29 + a_headrm - b_headrm - Qres; + if( lshift <= 0 ) { + return SKP_LSHIFT_SAT32(result, -lshift); + } else { + if( lshift < 32){ + return SKP_RSHIFT(result, lshift); + } else { + /* Avoid undefined result */ + return 0; + } + } +} + +/* Invert int32 value and return result as int32 in a given Q-domain */ +SKP_INLINE SKP_int32 SKP_INVERSE32_varQ( /* O returns a good approximation of "(1 << Qres) / b32" */ + const SKP_int32 b32, /* I denominator (Q0) */ + const SKP_int Qres /* I Q-domain of result (> 0) */ +) +{ + SKP_int b_headrm, lshift; + SKP_int32 b32_inv, b32_nrm, err_Q32, result; + + SKP_assert( b32 != 0 ); + SKP_assert( Qres > 0 ); + + /* Compute number of bits head room and normalize input */ + b_headrm = SKP_Silk_CLZ32( SKP_abs(b32) ) - 1; + b32_nrm = SKP_LSHIFT(b32, b_headrm); /* Q: b_headrm */ + + /* Inverse of b32, with 14 bits of precision */ + b32_inv = SKP_DIV32_16( SKP_int32_MAX >> 2, SKP_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ + + /* First approximation */ + result = SKP_LSHIFT(b32_inv, 16); /* Q: 61 - b_headrm */ + + /* Compute residual by subtracting product of denominator and first approximation from one */ + err_Q32 = SKP_LSHIFT_ovflw( -SKP_SMULWB(b32_nrm, b32_inv), 3 ); /* Q32 */ + + /* Refinement */ + result = SKP_SMLAWW(result, err_Q32, b32_inv); /* Q: 61 - b_headrm */ + + /* Convert to Qres domain */ + lshift = 61 - b_headrm - Qres; + if( lshift <= 0 ) { + return SKP_LSHIFT_SAT32(result, -lshift); + } else { + if( lshift < 32){ + return SKP_RSHIFT(result, lshift); + }else{ + /* Avoid undefined result */ + return 0; + } + } +} + +#define SKP_SIN_APPROX_CONST0 (1073735400) +#define SKP_SIN_APPROX_CONST1 (-82778932) +#define SKP_SIN_APPROX_CONST2 (1059577) +#define SKP_SIN_APPROX_CONST3 (-5013) + +/* Sine approximation; an input of 65536 corresponds to 2 * pi */ +/* Uses polynomial expansion of the input to the power 0, 2, 4 and 6 */ +/* The relative error is below 1e-5 */ +SKP_INLINE SKP_int32 SKP_Silk_SIN_APPROX_Q24( /* O returns approximately 2^24 * sin(x * 2 * pi / 65536) */ + SKP_int32 x +) +{ + SKP_int y_Q30; + + /* Keep only bottom 16 bits (the function repeats itself with period 65536) */ + x &= 65535; + + /* Split range in four quadrants */ + if( x <= 32768 ) { + if( x < 16384 ) { + /* Return cos(pi/2 - x) */ + x = 16384 - x; + } else { + /* Return cos(x - pi/2) */ + x -= 16384; + } + if( x < 1100 ) { + /* Special case: high accuracy */ + return SKP_SMLAWB( 1 << 24, SKP_MUL( x, x ), -5053 ); + } + x = SKP_SMULWB( SKP_LSHIFT( x, 8 ), x ); /* contains x^2 in Q20 */ + y_Q30 = SKP_SMLAWB( SKP_SIN_APPROX_CONST2, x, SKP_SIN_APPROX_CONST3 ); + y_Q30 = SKP_SMLAWW( SKP_SIN_APPROX_CONST1, x, y_Q30 ); + y_Q30 = SKP_SMLAWW( SKP_SIN_APPROX_CONST0 + 66, x, y_Q30 ); + } else { + if( x < 49152 ) { + /* Return -cos(3*pi/2 - x) */ + x = 49152 - x; + } else { + /* Return -cos(x - 3*pi/2) */ + x -= 49152; + } + if( x < 1100 ) { + /* Special case: high accuracy */ + return SKP_SMLAWB( -1 << 24, SKP_MUL( x, x ), 5053 ); + } + x = SKP_SMULWB( SKP_LSHIFT( x, 8 ), x ); /* contains x^2 in Q20 */ + y_Q30 = SKP_SMLAWB( -SKP_SIN_APPROX_CONST2, x, -SKP_SIN_APPROX_CONST3 ); + y_Q30 = SKP_SMLAWW( -SKP_SIN_APPROX_CONST1, x, y_Q30 ); + y_Q30 = SKP_SMLAWW( -SKP_SIN_APPROX_CONST0, x, y_Q30 ); + } + return SKP_RSHIFT_ROUND( y_Q30, 6 ); +} + +/* Cosine approximation; an input of 65536 corresponds to 2 * pi */ +/* The relative error is below 1e-5 */ +SKP_INLINE SKP_int32 SKP_Silk_COS_APPROX_Q24( /* O returns approximately 2^24 * cos(x * 2 * pi / 65536) */ + SKP_int32 x +) +{ + return SKP_Silk_SIN_APPROX_Q24( x + 16384 ); +} + +#ifdef __cplusplus +} +#endif + +#endif //_SKP_SILK_FIX_INLINES_H_ diff --git a/jni/silk/src/SKP_Silk_LBRR_reset.c b/app/src/main/jni/silk/src/SKP_Silk_LBRR_reset.c similarity index 98% rename from jni/silk/src/SKP_Silk_LBRR_reset.c rename to app/src/main/jni/silk/src/SKP_Silk_LBRR_reset.c index 22e6830..a6436c1 100644 --- a/jni/silk/src/SKP_Silk_LBRR_reset.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LBRR_reset.c @@ -1,40 +1,40 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Resets LBRR buffer, used if packet size changes */ -void SKP_Silk_LBRR_reset( - SKP_Silk_encoder_state *psEncC /* I/O state */ -) -{ - SKP_int i; - - for( i = 0; i < MAX_LBRR_DELAY; i++ ) { - psEncC->LBRR_buffer[ i ].usage = SKP_SILK_NO_LBRR; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Resets LBRR buffer, used if packet size changes */ +void SKP_Silk_LBRR_reset( + SKP_Silk_encoder_state *psEncC /* I/O state */ +) +{ + SKP_int i; + + for( i = 0; i < MAX_LBRR_DELAY; i++ ) { + psEncC->LBRR_buffer[ i ].usage = SKP_SILK_NO_LBRR; + } +} diff --git a/jni/silk/src/SKP_Silk_LPC_inv_pred_gain.c b/app/src/main/jni/silk/src/SKP_Silk_LPC_inv_pred_gain.c similarity index 97% rename from jni/silk/src/SKP_Silk_LPC_inv_pred_gain.c rename to app/src/main/jni/silk/src/SKP_Silk_LPC_inv_pred_gain.c index a350a6f..407c6d8 100644 --- a/jni/silk/src/SKP_Silk_LPC_inv_pred_gain.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LPC_inv_pred_gain.c @@ -1,189 +1,189 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_LPC_inverse_pred_gain.c * - * * - * Compute inverse of LPC prediction gain, and * - * test if LPC coefficients are stable (all poles within unit circle) * - * * - * Copyright 2008 (c), Skype Limited * - * */ -#include "SKP_Silk_SigProc_FIX.h" -#define QA 16 -#define A_LIMIT 65520 - -/* Compute inverse of LPC prediction gain, and */ -/* test if LPC coefficients are stable (all poles within unit circle) */ -SKP_int SKP_Silk_LPC_inverse_pred_gain( /* O: Returns 1 if unstable, otherwise 0 */ - SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ - const SKP_int16 *A_Q12, /* I: Prediction coefficients, Q12 [order] */ - const SKP_int order /* I: Prediction order */ -) -{ - SKP_int k, n, headrm; - SKP_int32 rc_Q31, rc_mult1_Q30, rc_mult2_Q16; - SKP_int32 Atmp_QA[ 2 ][ SigProc_MAX_ORDER_LPC ], tmp_QA; - SKP_int32 *Aold_QA, *Anew_QA; - - Anew_QA = Atmp_QA[ order & 1 ]; - /* Increase Q domain of the AR coefficients */ - for( k = 0; k < order; k++ ) { - Anew_QA[ k ] = SKP_LSHIFT( (SKP_int32)A_Q12[ k ], QA - 12 ); - } - - *invGain_Q30 = ( 1 << 30 ); - for( k = order - 1; k > 0; k-- ) { - /* Check for stability */ - if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) { - return 1; - } - - /* Set RC equal to negated AR coef */ - rc_Q31 = -SKP_LSHIFT( Anew_QA[ k ], 31 - QA ); - - /* rc_mult1_Q30 range: [ 1 : 2^30-1 ] */ - rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); - SKP_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */ - SKP_assert( rc_mult1_Q30 < ( 1 << 30 ) ); - - /* rc_mult2_Q16 range: [ 2^16 : SKP_int32_MAX ] */ - rc_mult2_Q16 = SKP_INVERSE32_varQ( rc_mult1_Q30, 46 ); /* 16 = 46 - 30 */ - - /* Update inverse gain */ - /* invGain_Q30 range: [ 0 : 2^30 ] */ - *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); - SKP_assert( *invGain_Q30 >= 0 ); - SKP_assert( *invGain_Q30 <= ( 1 << 30 ) ); - - /* Swap pointers */ - Aold_QA = Anew_QA; - Anew_QA = Atmp_QA[ k & 1 ]; - - /* Update AR coefficient */ - headrm = SKP_Silk_CLZ32( rc_mult2_Q16 ) - 1; - rc_mult2_Q16 = SKP_LSHIFT( rc_mult2_Q16, headrm ); /* Q: 16 + headrm */ - for( n = 0; n < k; n++ ) { - tmp_QA = Aold_QA[ n ] - SKP_LSHIFT( SKP_SMMUL( Aold_QA[ k - n - 1 ], rc_Q31 ), 1 ); - Anew_QA[ n ] = SKP_LSHIFT( SKP_SMMUL( tmp_QA, rc_mult2_Q16 ), 16 - headrm ); - } - } - - /* Check for stability */ - if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) { - return 1; - } - - /* Set RC equal to negated AR coef */ - rc_Q31 = -SKP_LSHIFT( Anew_QA[ 0 ], 31 - QA ); - - /* Range: [ 1 : 2^30 ] */ - rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); - - /* Update inverse gain */ - /* Range: [ 0 : 2^30 ] */ - *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); - SKP_assert( *invGain_Q30 >= 0 ); - SKP_assert( *invGain_Q30 <= 1<<30 ); - - return 0; -} - -/* For input in Q13 domain */ -SKP_int SKP_Silk_LPC_inverse_pred_gain_Q13( /* O: Returns 1 if unstable, otherwise 0 */ - SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ - const SKP_int16 *A_Q13, /* I: Prediction coefficients, Q13 [order] */ - const SKP_int order /* I: Prediction order */ -) -{ - SKP_int k, n, headrm; - SKP_int32 rc_Q31, rc_mult1_Q30, rc_mult2_Q16; - SKP_int32 Atmp_QA[ 2 ][ SigProc_MAX_ORDER_LPC ], tmp_QA; - SKP_int32 *Aold_QA, *Anew_QA; - - Anew_QA = Atmp_QA[ order & 1 ]; - /* Increase Q domain of the AR coefficients */ - for( k = 0; k < order; k++ ) { - Anew_QA[ k ] = SKP_LSHIFT( (SKP_int32)A_Q13[ k ], QA - 13 ); - } - - *invGain_Q30 = ( 1 << 30 ); - for( k = order - 1; k > 0; k-- ) { - /* Check for stability */ - if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) { - return 1; - } - - /* Set RC equal to negated AR coef */ - rc_Q31 = -SKP_LSHIFT( Anew_QA[ k ], 31 - QA ); - - /* rc_mult1_Q30 range: [ 1 : 2^30-1 ] */ - rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); - SKP_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */ - SKP_assert( rc_mult1_Q30 < ( 1 << 30 ) ); - - /* rc_mult2_Q16 range: [ 2^16 : SKP_int32_MAX ] */ - rc_mult2_Q16 = SKP_INVERSE32_varQ( rc_mult1_Q30, 46 ); /* 16 = 46 - 30 */ - - /* Update inverse gain */ - /* invGain_Q30 range: [ 0 : 2^30 ] */ - *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); - SKP_assert( *invGain_Q30 >= 0 ); - SKP_assert( *invGain_Q30 <= 1<<30 ); - - /* Swap pointers */ - Aold_QA = Anew_QA; - Anew_QA = Atmp_QA[ k & 1 ]; - - /* Update AR coefficient */ - headrm = SKP_Silk_CLZ32( rc_mult2_Q16 ) - 1; - rc_mult2_Q16 = SKP_LSHIFT( rc_mult2_Q16, headrm ); /* Q: 16 + headrm */ - for( n = 0; n < k; n++ ) { - tmp_QA = Aold_QA[ n ] - SKP_LSHIFT( SKP_SMMUL( Aold_QA[ k - n - 1 ], rc_Q31 ), 1 ); - Anew_QA[ n ] = SKP_LSHIFT( SKP_SMMUL( tmp_QA, rc_mult2_Q16 ), 16 - headrm ); - } - } - - /* Check for stability */ - if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) { - return 1; - } - - /* Set RC equal to negated AR coef */ - rc_Q31 = -SKP_LSHIFT( Anew_QA[ 0 ], 31 - QA ); - - /* Range: [ 1 : 2^30 ] */ - rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); - - /* Update inverse gain */ - /* Range: [ 0 : 2^30 ] */ - *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); - SKP_assert( *invGain_Q30 >= 0 ); - SKP_assert( *invGain_Q30 <= 1<<30 ); - - return 0; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_LPC_inverse_pred_gain.c * + * * + * Compute inverse of LPC prediction gain, and * + * test if LPC coefficients are stable (all poles within unit circle) * + * * + * Copyright 2008 (c), Skype Limited * + * */ +#include "SKP_Silk_SigProc_FIX.h" +#define QA 16 +#define A_LIMIT 65520 + +/* Compute inverse of LPC prediction gain, and */ +/* test if LPC coefficients are stable (all poles within unit circle) */ +SKP_int SKP_Silk_LPC_inverse_pred_gain( /* O: Returns 1 if unstable, otherwise 0 */ + SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ + const SKP_int16 *A_Q12, /* I: Prediction coefficients, Q12 [order] */ + const SKP_int order /* I: Prediction order */ +) +{ + SKP_int k, n, headrm; + SKP_int32 rc_Q31, rc_mult1_Q30, rc_mult2_Q16; + SKP_int32 Atmp_QA[ 2 ][ SigProc_MAX_ORDER_LPC ], tmp_QA; + SKP_int32 *Aold_QA, *Anew_QA; + + Anew_QA = Atmp_QA[ order & 1 ]; + /* Increase Q domain of the AR coefficients */ + for( k = 0; k < order; k++ ) { + Anew_QA[ k ] = SKP_LSHIFT( (SKP_int32)A_Q12[ k ], QA - 12 ); + } + + *invGain_Q30 = ( 1 << 30 ); + for( k = order - 1; k > 0; k-- ) { + /* Check for stability */ + if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) { + return 1; + } + + /* Set RC equal to negated AR coef */ + rc_Q31 = -SKP_LSHIFT( Anew_QA[ k ], 31 - QA ); + + /* rc_mult1_Q30 range: [ 1 : 2^30-1 ] */ + rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); + SKP_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */ + SKP_assert( rc_mult1_Q30 < ( 1 << 30 ) ); + + /* rc_mult2_Q16 range: [ 2^16 : SKP_int32_MAX ] */ + rc_mult2_Q16 = SKP_INVERSE32_varQ( rc_mult1_Q30, 46 ); /* 16 = 46 - 30 */ + + /* Update inverse gain */ + /* invGain_Q30 range: [ 0 : 2^30 ] */ + *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); + SKP_assert( *invGain_Q30 >= 0 ); + SKP_assert( *invGain_Q30 <= ( 1 << 30 ) ); + + /* Swap pointers */ + Aold_QA = Anew_QA; + Anew_QA = Atmp_QA[ k & 1 ]; + + /* Update AR coefficient */ + headrm = SKP_Silk_CLZ32( rc_mult2_Q16 ) - 1; + rc_mult2_Q16 = SKP_LSHIFT( rc_mult2_Q16, headrm ); /* Q: 16 + headrm */ + for( n = 0; n < k; n++ ) { + tmp_QA = Aold_QA[ n ] - SKP_LSHIFT( SKP_SMMUL( Aold_QA[ k - n - 1 ], rc_Q31 ), 1 ); + Anew_QA[ n ] = SKP_LSHIFT( SKP_SMMUL( tmp_QA, rc_mult2_Q16 ), 16 - headrm ); + } + } + + /* Check for stability */ + if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) { + return 1; + } + + /* Set RC equal to negated AR coef */ + rc_Q31 = -SKP_LSHIFT( Anew_QA[ 0 ], 31 - QA ); + + /* Range: [ 1 : 2^30 ] */ + rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); + + /* Update inverse gain */ + /* Range: [ 0 : 2^30 ] */ + *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); + SKP_assert( *invGain_Q30 >= 0 ); + SKP_assert( *invGain_Q30 <= 1<<30 ); + + return 0; +} + +/* For input in Q13 domain */ +SKP_int SKP_Silk_LPC_inverse_pred_gain_Q13( /* O: Returns 1 if unstable, otherwise 0 */ + SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ + const SKP_int16 *A_Q13, /* I: Prediction coefficients, Q13 [order] */ + const SKP_int order /* I: Prediction order */ +) +{ + SKP_int k, n, headrm; + SKP_int32 rc_Q31, rc_mult1_Q30, rc_mult2_Q16; + SKP_int32 Atmp_QA[ 2 ][ SigProc_MAX_ORDER_LPC ], tmp_QA; + SKP_int32 *Aold_QA, *Anew_QA; + + Anew_QA = Atmp_QA[ order & 1 ]; + /* Increase Q domain of the AR coefficients */ + for( k = 0; k < order; k++ ) { + Anew_QA[ k ] = SKP_LSHIFT( (SKP_int32)A_Q13[ k ], QA - 13 ); + } + + *invGain_Q30 = ( 1 << 30 ); + for( k = order - 1; k > 0; k-- ) { + /* Check for stability */ + if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) { + return 1; + } + + /* Set RC equal to negated AR coef */ + rc_Q31 = -SKP_LSHIFT( Anew_QA[ k ], 31 - QA ); + + /* rc_mult1_Q30 range: [ 1 : 2^30-1 ] */ + rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); + SKP_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */ + SKP_assert( rc_mult1_Q30 < ( 1 << 30 ) ); + + /* rc_mult2_Q16 range: [ 2^16 : SKP_int32_MAX ] */ + rc_mult2_Q16 = SKP_INVERSE32_varQ( rc_mult1_Q30, 46 ); /* 16 = 46 - 30 */ + + /* Update inverse gain */ + /* invGain_Q30 range: [ 0 : 2^30 ] */ + *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); + SKP_assert( *invGain_Q30 >= 0 ); + SKP_assert( *invGain_Q30 <= 1<<30 ); + + /* Swap pointers */ + Aold_QA = Anew_QA; + Anew_QA = Atmp_QA[ k & 1 ]; + + /* Update AR coefficient */ + headrm = SKP_Silk_CLZ32( rc_mult2_Q16 ) - 1; + rc_mult2_Q16 = SKP_LSHIFT( rc_mult2_Q16, headrm ); /* Q: 16 + headrm */ + for( n = 0; n < k; n++ ) { + tmp_QA = Aold_QA[ n ] - SKP_LSHIFT( SKP_SMMUL( Aold_QA[ k - n - 1 ], rc_Q31 ), 1 ); + Anew_QA[ n ] = SKP_LSHIFT( SKP_SMMUL( tmp_QA, rc_mult2_Q16 ), 16 - headrm ); + } + } + + /* Check for stability */ + if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) { + return 1; + } + + /* Set RC equal to negated AR coef */ + rc_Q31 = -SKP_LSHIFT( Anew_QA[ 0 ], 31 - QA ); + + /* Range: [ 1 : 2^30 ] */ + rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 ); + + /* Update inverse gain */ + /* Range: [ 0 : 2^30 ] */ + *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 ); + SKP_assert( *invGain_Q30 >= 0 ); + SKP_assert( *invGain_Q30 <= 1<<30 ); + + return 0; +} diff --git a/jni/silk/src/SKP_Silk_LPC_stabilize.c b/app/src/main/jni/silk/src/SKP_Silk_LPC_stabilize.c similarity index 97% rename from jni/silk/src/SKP_Silk_LPC_stabilize.c rename to app/src/main/jni/silk/src/SKP_Silk_LPC_stabilize.c index bd58d10..0fb6d76 100644 --- a/jni/silk/src/SKP_Silk_LPC_stabilize.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LPC_stabilize.c @@ -1,132 +1,132 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_typedef.h" -#include "SKP_Silk_SigProc_FIX.h" - -#define LPC_STABILIZE_LPC_MAX_ABS_VALUE_Q16 ( ( (SKP_int32)SKP_int16_MAX ) << 4 ) - -/* LPC stabilizer, for a single input data vector */ -void SKP_Silk_LPC_stabilize( - SKP_int16 *a_Q12, /* O stabilized LPC vector [L] */ - SKP_int32 *a_Q16, /* I LPC vector [L] */ - const SKP_int32 bwe_Q16, /* I Bandwidth expansion factor */ - const SKP_int L /* I Number of LPC parameters in the input vector */ -) -{ - SKP_int32 maxabs, absval, sc_Q16; - SKP_int i, idx = 0; - SKP_int32 invGain_Q30; - - SKP_Silk_bwexpander_32( a_Q16, L, bwe_Q16 ); - - /***************************/ - /* Limit range of the LPCs */ - /***************************/ - /* Limit the maximum absolute value of the prediction coefficients */ - while( SKP_TRUE ) { - /* Find maximum absolute value and its index */ - maxabs = SKP_int32_MIN; - for( i = 0; i < L; i++ ) { - absval = SKP_abs( a_Q16[ i ] ); - if( absval > maxabs ) { - maxabs = absval; - idx = i; - } - } - - if( maxabs >= LPC_STABILIZE_LPC_MAX_ABS_VALUE_Q16 ) { - /* Reduce magnitude of prediction coefficients */ - sc_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( maxabs, 4 ) ); - sc_Q16 = 65536 - sc_Q16; - sc_Q16 = SKP_DIV32( sc_Q16, idx + 1 ); - sc_Q16 = 65536 - sc_Q16; - sc_Q16 = SKP_LSHIFT( SKP_SMULWB( sc_Q16, 32604 ), 1 ); // 0.995 in Q16 - SKP_Silk_bwexpander_32( a_Q16, L, sc_Q16 ); - } else { - break; - } - } - - /* Convert to 16 bit Q12 */ - for( i = 0; i < L; i++ ) { - a_Q12[ i ] = (SKP_int16)SKP_RSHIFT_ROUND( a_Q16[ i ], 4 ); - } - - /**********************/ - /* Ensure stable LPCs */ - /**********************/ - while( SKP_Silk_LPC_inverse_pred_gain( &invGain_Q30, a_Q12, L ) == 1 ) { - SKP_Silk_bwexpander( a_Q12, L, 65339 ); // 0.997 in Q16 - } -} - -void SKP_Silk_LPC_fit( - SKP_int16 *a_QQ, /* O Stabilized LPC vector, Q(24-rshift) [L] */ - SKP_int32 *a_Q24, /* I LPC vector [L] */ - const SKP_int QQ, /* I Q domain of output LPC vector */ - const SKP_int L /* I Number of LPC parameters in the input vector */ -) -{ - SKP_int i, rshift, idx = 0; - SKP_int32 maxabs, absval, sc_Q16; - - rshift = 24 - QQ; - - /***************************/ - /* Limit range of the LPCs */ - /***************************/ - /* Limit the maximum absolute value of the prediction coefficients */ - while( SKP_TRUE ) { - /* Find maximum absolute value and its index */ - maxabs = SKP_int32_MIN; - for( i = 0; i < L; i++ ) { - absval = SKP_abs( a_Q24[ i ] ); - if( absval > maxabs ) { - maxabs = absval; - idx = i; - } - } - - maxabs = SKP_RSHIFT( maxabs, rshift ); - if( maxabs >= SKP_int16_MAX ) { - /* Reduce magnitude of prediction coefficients */ - sc_Q16 = 65470 - SKP_DIV32( SKP_MUL( 65470 >> 2, maxabs - SKP_int16_MAX ), - SKP_RSHIFT32( SKP_MUL( maxabs, idx + 1), 2 ) ); - SKP_Silk_bwexpander_32( a_Q24, L, sc_Q16 ); - } else { - break; - } - } - - /* Convert to 16 bit Q(24-rshift) */ - SKP_assert( rshift > 0 ); - SKP_assert( rshift < 31 ); - for( i = 0; i < L; i++ ) { - a_QQ[ i ] = (SKP_int16)SKP_RSHIFT_ROUND( a_Q24[ i ], rshift ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_typedef.h" +#include "SKP_Silk_SigProc_FIX.h" + +#define LPC_STABILIZE_LPC_MAX_ABS_VALUE_Q16 ( ( (SKP_int32)SKP_int16_MAX ) << 4 ) + +/* LPC stabilizer, for a single input data vector */ +void SKP_Silk_LPC_stabilize( + SKP_int16 *a_Q12, /* O stabilized LPC vector [L] */ + SKP_int32 *a_Q16, /* I LPC vector [L] */ + const SKP_int32 bwe_Q16, /* I Bandwidth expansion factor */ + const SKP_int L /* I Number of LPC parameters in the input vector */ +) +{ + SKP_int32 maxabs, absval, sc_Q16; + SKP_int i, idx = 0; + SKP_int32 invGain_Q30; + + SKP_Silk_bwexpander_32( a_Q16, L, bwe_Q16 ); + + /***************************/ + /* Limit range of the LPCs */ + /***************************/ + /* Limit the maximum absolute value of the prediction coefficients */ + while( SKP_TRUE ) { + /* Find maximum absolute value and its index */ + maxabs = SKP_int32_MIN; + for( i = 0; i < L; i++ ) { + absval = SKP_abs( a_Q16[ i ] ); + if( absval > maxabs ) { + maxabs = absval; + idx = i; + } + } + + if( maxabs >= LPC_STABILIZE_LPC_MAX_ABS_VALUE_Q16 ) { + /* Reduce magnitude of prediction coefficients */ + sc_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( maxabs, 4 ) ); + sc_Q16 = 65536 - sc_Q16; + sc_Q16 = SKP_DIV32( sc_Q16, idx + 1 ); + sc_Q16 = 65536 - sc_Q16; + sc_Q16 = SKP_LSHIFT( SKP_SMULWB( sc_Q16, 32604 ), 1 ); // 0.995 in Q16 + SKP_Silk_bwexpander_32( a_Q16, L, sc_Q16 ); + } else { + break; + } + } + + /* Convert to 16 bit Q12 */ + for( i = 0; i < L; i++ ) { + a_Q12[ i ] = (SKP_int16)SKP_RSHIFT_ROUND( a_Q16[ i ], 4 ); + } + + /**********************/ + /* Ensure stable LPCs */ + /**********************/ + while( SKP_Silk_LPC_inverse_pred_gain( &invGain_Q30, a_Q12, L ) == 1 ) { + SKP_Silk_bwexpander( a_Q12, L, 65339 ); // 0.997 in Q16 + } +} + +void SKP_Silk_LPC_fit( + SKP_int16 *a_QQ, /* O Stabilized LPC vector, Q(24-rshift) [L] */ + SKP_int32 *a_Q24, /* I LPC vector [L] */ + const SKP_int QQ, /* I Q domain of output LPC vector */ + const SKP_int L /* I Number of LPC parameters in the input vector */ +) +{ + SKP_int i, rshift, idx = 0; + SKP_int32 maxabs, absval, sc_Q16; + + rshift = 24 - QQ; + + /***************************/ + /* Limit range of the LPCs */ + /***************************/ + /* Limit the maximum absolute value of the prediction coefficients */ + while( SKP_TRUE ) { + /* Find maximum absolute value and its index */ + maxabs = SKP_int32_MIN; + for( i = 0; i < L; i++ ) { + absval = SKP_abs( a_Q24[ i ] ); + if( absval > maxabs ) { + maxabs = absval; + idx = i; + } + } + + maxabs = SKP_RSHIFT( maxabs, rshift ); + if( maxabs >= SKP_int16_MAX ) { + /* Reduce magnitude of prediction coefficients */ + sc_Q16 = 65470 - SKP_DIV32( SKP_MUL( 65470 >> 2, maxabs - SKP_int16_MAX ), + SKP_RSHIFT32( SKP_MUL( maxabs, idx + 1), 2 ) ); + SKP_Silk_bwexpander_32( a_Q24, L, sc_Q16 ); + } else { + break; + } + } + + /* Convert to 16 bit Q(24-rshift) */ + SKP_assert( rshift > 0 ); + SKP_assert( rshift < 31 ); + for( i = 0; i < L; i++ ) { + a_QQ[ i ] = (SKP_int16)SKP_RSHIFT_ROUND( a_Q24[ i ], rshift ); + } +} diff --git a/jni/silk/src/SKP_Silk_LPC_synthesis_filter.c b/app/src/main/jni/silk/src/SKP_Silk_LPC_synthesis_filter.c similarity index 98% rename from jni/silk/src/SKP_Silk_LPC_synthesis_filter.c rename to app/src/main/jni/silk/src/SKP_Silk_LPC_synthesis_filter.c index fff5e70..1ee31d7 100644 --- a/jni/silk/src/SKP_Silk_LPC_synthesis_filter.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LPC_synthesis_filter.c @@ -1,98 +1,98 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_LPC_synthesis_filter.c * - * Coefficients are in Q12 * - * * - * even order AR filter * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* even order AR filter */ -void SKP_Silk_LPC_synthesis_filter( - const SKP_int16 *in, /* I: excitation signal */ - const SKP_int16 *A_Q12, /* I: AR coefficients [Order], between -8_Q0 and 8_Q0 */ - const SKP_int32 Gain_Q26, /* I: gain */ - SKP_int32 *S, /* I/O: state vector [Order] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len, /* I: signal length */ - const SKP_int Order /* I: filter order, must be even */ -) -{ - SKP_int k, j, idx, Order_half = SKP_RSHIFT( Order, 1 ); - SKP_int32 SA, SB, Atmp, A_align_Q12[SigProc_MAX_ORDER_LPC >> 1], out32_Q10, out32; - - /* Order must be even */ - SKP_assert( 2*Order_half == Order ); - - /* combine two A_Q12 values and ensure 32-bit alignment */ - for( k = 0; k < Order_half; k++ ) { - idx = SKP_SMULBB( 2, k ); - A_align_Q12[k] = (((SKP_int32)A_Q12[idx]) & 0x0000ffff) | SKP_LSHIFT( (SKP_int32)A_Q12[idx+1], 16 ); - } - - /* S[] values are in Q14 */ - for( k = 0; k < len; k++ ) { - SA = S[Order-1]; - out32_Q10 = 0; - for( j=0;j<(Order_half-1); j++ ) { - idx = SKP_SMULBB( 2, j ) + 1; - /* multiply-add two prediction coefficients for each loop */ - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLAWB and SMLAWT instructions should solve the problem. */ - Atmp = A_align_Q12[j]; - SB = S[Order - 1 - idx]; - S[Order - 1 - idx] = SA; - out32_Q10 = SKP_SMLAWB( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT( out32_Q10, SB, Atmp ); - SA = S[Order - 2 - idx]; - S[Order - 2 - idx] = SB; - } - - /* unrolled loop: epilog */ - Atmp = A_align_Q12[Order_half-1]; - SB = S[0]; - S[0] = SA; - out32_Q10 = SKP_SMLAWB( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT( out32_Q10, SB, Atmp ); - - /* apply gain to excitation signal and add to prediction */ - out32_Q10 = SKP_ADD_SAT32( out32_Q10, SKP_SMULWB( Gain_Q26, in[k] ) ); - - /* scale to Q0 */ - out32 = SKP_RSHIFT_ROUND( out32_Q10, 10 ); - - /* saturate output */ - out[k] = (SKP_int16)SKP_SAT16( out32 ); - - /* move result into delay line */ - S[Order - 1] = SKP_LSHIFT_SAT32( out32_Q10, 4 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_LPC_synthesis_filter.c * + * Coefficients are in Q12 * + * * + * even order AR filter * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* even order AR filter */ +void SKP_Silk_LPC_synthesis_filter( + const SKP_int16 *in, /* I: excitation signal */ + const SKP_int16 *A_Q12, /* I: AR coefficients [Order], between -8_Q0 and 8_Q0 */ + const SKP_int32 Gain_Q26, /* I: gain */ + SKP_int32 *S, /* I/O: state vector [Order] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len, /* I: signal length */ + const SKP_int Order /* I: filter order, must be even */ +) +{ + SKP_int k, j, idx, Order_half = SKP_RSHIFT( Order, 1 ); + SKP_int32 SA, SB, Atmp, A_align_Q12[SigProc_MAX_ORDER_LPC >> 1], out32_Q10, out32; + + /* Order must be even */ + SKP_assert( 2*Order_half == Order ); + + /* combine two A_Q12 values and ensure 32-bit alignment */ + for( k = 0; k < Order_half; k++ ) { + idx = SKP_SMULBB( 2, k ); + A_align_Q12[k] = (((SKP_int32)A_Q12[idx]) & 0x0000ffff) | SKP_LSHIFT( (SKP_int32)A_Q12[idx+1], 16 ); + } + + /* S[] values are in Q14 */ + for( k = 0; k < len; k++ ) { + SA = S[Order-1]; + out32_Q10 = 0; + for( j=0;j<(Order_half-1); j++ ) { + idx = SKP_SMULBB( 2, j ) + 1; + /* multiply-add two prediction coefficients for each loop */ + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLAWB and SMLAWT instructions should solve the problem. */ + Atmp = A_align_Q12[j]; + SB = S[Order - 1 - idx]; + S[Order - 1 - idx] = SA; + out32_Q10 = SKP_SMLAWB( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT( out32_Q10, SB, Atmp ); + SA = S[Order - 2 - idx]; + S[Order - 2 - idx] = SB; + } + + /* unrolled loop: epilog */ + Atmp = A_align_Q12[Order_half-1]; + SB = S[0]; + S[0] = SA; + out32_Q10 = SKP_SMLAWB( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT( out32_Q10, SB, Atmp ); + + /* apply gain to excitation signal and add to prediction */ + out32_Q10 = SKP_ADD_SAT32( out32_Q10, SKP_SMULWB( Gain_Q26, in[k] ) ); + + /* scale to Q0 */ + out32 = SKP_RSHIFT_ROUND( out32_Q10, 10 ); + + /* saturate output */ + out[k] = (SKP_int16)SKP_SAT16( out32 ); + + /* move result into delay line */ + S[Order - 1] = SKP_LSHIFT_SAT32( out32_Q10, 4 ); + } +} diff --git a/jni/silk/src/SKP_Silk_LPC_synthesis_order16.c b/app/src/main/jni/silk/src/SKP_Silk_LPC_synthesis_order16.c similarity index 97% rename from jni/silk/src/SKP_Silk_LPC_synthesis_order16.c rename to app/src/main/jni/silk/src/SKP_Silk_LPC_synthesis_order16.c index ec2d358..ee52c7f 100644 --- a/jni/silk/src/SKP_Silk_LPC_synthesis_order16.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LPC_synthesis_order16.c @@ -1,141 +1,141 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_LPC_synthesis_order16.c * - * Coefficients are in Q12 * - * * - * 16th order AR filter * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* 16th order AR filter */ -void SKP_Silk_LPC_synthesis_order16(const SKP_int16 *in, /* I: excitation signal */ - const SKP_int16 *A_Q12, /* I: AR coefficients [16], between -8_Q0 and 8_Q0 */ - const SKP_int32 Gain_Q26, /* I: gain */ - SKP_int32 *S, /* I/O: state vector [16] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len /* I: signal length, must be multiple of 16 */ -) -{ - SKP_int k; - SKP_int32 SA, SB, Atmp, A_align_Q12[8], out32_Q10, out32; - - /* combine two A_Q12 values and ensure 32-bit alignment */ - for( k = 0; k < 8; k++ ) { - A_align_Q12[k] = (((SKP_int32)A_Q12[ 2*k ]) & 0x0000ffff) | SKP_LSHIFT( (SKP_int32)A_Q12[ 2*k + 1 ], 16 ); - } - - /* S[] values are in Q14 */ - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLAWB and SMLAWT instructions should solve the problem. */ - for( k = 0; k < len; k++ ) { - /* unrolled loop: prolog */ - /* multiply-add two prediction coefficients per iteration */ - SA = S[15]; - Atmp = A_align_Q12[0]; - SB = S[14]; - S[14] = SA; - out32_Q10 = SKP_SMULWB( SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[13]; - S[13] = SB; - - /* unrolled loop: main loop */ - Atmp = A_align_Q12[1]; - SB = S[12]; - S[12] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[11]; - S[11] = SB; - - Atmp = A_align_Q12[2]; - SB = S[10]; - S[10] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[9]; - S[9] = SB; - - Atmp = A_align_Q12[3]; - SB = S[8]; - S[8] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[7]; - S[7] = SB; - - Atmp = A_align_Q12[4]; - SB = S[6]; - S[6] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[5]; - S[5] = SB; - - Atmp = A_align_Q12[5]; - SB = S[4]; - S[4] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[3]; - S[3] = SB; - - Atmp = A_align_Q12[6]; - SB = S[2]; - S[2] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - SA = S[1]; - S[1] = SB; - - /* unrolled loop: epilog */ - Atmp = A_align_Q12[7]; - SB = S[0]; - S[0] = SA; - out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); - out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); - - /* unrolled loop: end */ - /* apply gain to excitation signal and add to prediction */ - out32_Q10 = SKP_ADD_SAT32( out32_Q10, SKP_SMULWB( Gain_Q26, in[k] ) ); - - /* scale to Q0 */ - out32 = SKP_RSHIFT_ROUND( out32_Q10, 10 ); - - /* saturate output */ - out[k] = (SKP_int16)SKP_SAT16( out32 ); - - /* move result into delay line */ - S[15] = SKP_LSHIFT_SAT32( out32_Q10, 4 ); - } -} - - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_LPC_synthesis_order16.c * + * Coefficients are in Q12 * + * * + * 16th order AR filter * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* 16th order AR filter */ +void SKP_Silk_LPC_synthesis_order16(const SKP_int16 *in, /* I: excitation signal */ + const SKP_int16 *A_Q12, /* I: AR coefficients [16], between -8_Q0 and 8_Q0 */ + const SKP_int32 Gain_Q26, /* I: gain */ + SKP_int32 *S, /* I/O: state vector [16] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len /* I: signal length, must be multiple of 16 */ +) +{ + SKP_int k; + SKP_int32 SA, SB, Atmp, A_align_Q12[8], out32_Q10, out32; + + /* combine two A_Q12 values and ensure 32-bit alignment */ + for( k = 0; k < 8; k++ ) { + A_align_Q12[k] = (((SKP_int32)A_Q12[ 2*k ]) & 0x0000ffff) | SKP_LSHIFT( (SKP_int32)A_Q12[ 2*k + 1 ], 16 ); + } + + /* S[] values are in Q14 */ + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLAWB and SMLAWT instructions should solve the problem. */ + for( k = 0; k < len; k++ ) { + /* unrolled loop: prolog */ + /* multiply-add two prediction coefficients per iteration */ + SA = S[15]; + Atmp = A_align_Q12[0]; + SB = S[14]; + S[14] = SA; + out32_Q10 = SKP_SMULWB( SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[13]; + S[13] = SB; + + /* unrolled loop: main loop */ + Atmp = A_align_Q12[1]; + SB = S[12]; + S[12] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[11]; + S[11] = SB; + + Atmp = A_align_Q12[2]; + SB = S[10]; + S[10] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[9]; + S[9] = SB; + + Atmp = A_align_Q12[3]; + SB = S[8]; + S[8] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[7]; + S[7] = SB; + + Atmp = A_align_Q12[4]; + SB = S[6]; + S[6] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[5]; + S[5] = SB; + + Atmp = A_align_Q12[5]; + SB = S[4]; + S[4] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[3]; + S[3] = SB; + + Atmp = A_align_Q12[6]; + SB = S[2]; + S[2] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + SA = S[1]; + S[1] = SB; + + /* unrolled loop: epilog */ + Atmp = A_align_Q12[7]; + SB = S[0]; + S[0] = SA; + out32_Q10 = SKP_SMLAWB_ovflw( out32_Q10, SA, Atmp ); + out32_Q10 = SKP_SMLAWT_ovflw( out32_Q10, SB, Atmp ); + + /* unrolled loop: end */ + /* apply gain to excitation signal and add to prediction */ + out32_Q10 = SKP_ADD_SAT32( out32_Q10, SKP_SMULWB( Gain_Q26, in[k] ) ); + + /* scale to Q0 */ + out32 = SKP_RSHIFT_ROUND( out32_Q10, 10 ); + + /* saturate output */ + out[k] = (SKP_int16)SKP_SAT16( out32 ); + + /* move result into delay line */ + S[15] = SKP_LSHIFT_SAT32( out32_Q10, 4 ); + } +} + + diff --git a/jni/silk/src/SKP_Silk_LP_variable_cutoff.c b/app/src/main/jni/silk/src/SKP_Silk_LP_variable_cutoff.c similarity index 98% rename from jni/silk/src/SKP_Silk_LP_variable_cutoff.c rename to app/src/main/jni/silk/src/SKP_Silk_LP_variable_cutoff.c index 87feef5..162135f 100644 --- a/jni/silk/src/SKP_Silk_LP_variable_cutoff.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LP_variable_cutoff.c @@ -1,191 +1,191 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* - - Elliptic/Cauer filters designed with 0.1 dB passband ripple, - 80 dB minimum stopband attenuation, and - [0.95 : 0.15 : 0.35] normalized cut off frequencies. - -*/ -#include "SKP_Silk_main.h" - -#if SWITCH_TRANSITION_FILTERING - -/* Helper function, that interpolates the filter taps */ -SKP_INLINE void SKP_Silk_LP_interpolate_filter_taps( - SKP_int32 B_Q28[ TRANSITION_NB ], - SKP_int32 A_Q28[ TRANSITION_NA ], - const SKP_int ind, - const SKP_int32 fac_Q16 -) -{ - SKP_int nb, na; - - if( ind < TRANSITION_INT_NUM - 1 ) { - if( fac_Q16 > 0 ) { - if( fac_Q16 == SKP_SAT16( fac_Q16 ) ) { /* fac_Q16 is in range of a 16-bit int */ - /* Piece-wise linear interpolation of B and A */ - for( nb = 0; nb < TRANSITION_NB; nb++ ) { - B_Q28[ nb ] = SKP_SMLAWB( - SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ], - SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] - - SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ], - fac_Q16 ); - } - for( na = 0; na < TRANSITION_NA; na++ ) { - A_Q28[ na ] = SKP_SMLAWB( - SKP_Silk_Transition_LP_A_Q28[ ind ][ na ], - SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ] - - SKP_Silk_Transition_LP_A_Q28[ ind ][ na ], - fac_Q16 ); - } - } else if( fac_Q16 == ( 1 << 15 ) ) { /* Neither fac_Q16 nor ( ( 1 << 16 ) - fac_Q16 ) is in range of a 16-bit int */ - - /* Piece-wise linear interpolation of B and A */ - for( nb = 0; nb < TRANSITION_NB; nb++ ) { - B_Q28[ nb ] = SKP_RSHIFT( - SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ] + - SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], - 1 ); - } - for( na = 0; na < TRANSITION_NA; na++ ) { - A_Q28[ na ] = SKP_RSHIFT( - SKP_Silk_Transition_LP_A_Q28[ ind ][ na ] + - SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ], - 1 ); - } - } else { /* ( ( 1 << 16 ) - fac_Q16 ) is in range of a 16-bit int */ - - SKP_assert( ( ( 1 << 16 ) - fac_Q16 ) == SKP_SAT16( ( ( 1 << 16 ) - fac_Q16) ) ); - /* Piece-wise linear interpolation of B and A */ - for( nb = 0; nb < TRANSITION_NB; nb++ ) { - B_Q28[ nb ] = SKP_SMLAWB( - SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], - SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ] - - SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], - ( 1 << 16 ) - fac_Q16 ); - } - for( na = 0; na < TRANSITION_NA; na++ ) { - A_Q28[ na ] = SKP_SMLAWB( - SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ], - SKP_Silk_Transition_LP_A_Q28[ ind ][ na ] - - SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ], - ( 1 << 16 ) - fac_Q16 ); - } - } - } else { - SKP_memcpy( B_Q28, SKP_Silk_Transition_LP_B_Q28[ ind ], TRANSITION_NB * sizeof( SKP_int32 ) ); - SKP_memcpy( A_Q28, SKP_Silk_Transition_LP_A_Q28[ ind ], TRANSITION_NA * sizeof( SKP_int32 ) ); - } - } else { - SKP_memcpy( B_Q28, SKP_Silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NB * sizeof( SKP_int32 ) ); - SKP_memcpy( A_Q28, SKP_Silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NA * sizeof( SKP_int32 ) ); - } -} - -/* Low-pass filter with variable cutoff frequency based on */ -/* piece-wise linear interpolation between elliptic filters */ -/* Start by setting psEncC->transition_frame_no = 1; */ -/* Deactivate by setting psEncC->transition_frame_no = 0; */ -void SKP_Silk_LP_variable_cutoff( - SKP_Silk_LP_state *psLP, /* I/O LP filter state */ - SKP_int16 *out, /* O Low-pass filtered output signal */ - const SKP_int16 *in, /* I Input signal */ - const SKP_int frame_length /* I Frame length */ -) -{ - SKP_int32 B_Q28[ TRANSITION_NB ], A_Q28[ TRANSITION_NA ]; - SKP_int fac_Q16 = 0, ind = 0; - - SKP_assert( psLP->transition_frame_no >= 0 ); - SKP_assert( ( ( ( psLP->transition_frame_no <= TRANSITION_FRAMES_DOWN ) && ( psLP->mode == 0 ) ) || - ( ( psLP->transition_frame_no <= TRANSITION_FRAMES_UP ) && ( psLP->mode == 1 ) ) ) ); - - /* Interpolate filter coefficients if needed */ - if( psLP->transition_frame_no > 0 ) { - if( psLP->mode == 0 ) { - if( psLP->transition_frame_no < TRANSITION_FRAMES_DOWN ) { - /* Calculate index and interpolation factor for interpolation */ -#if( TRANSITION_INT_STEPS_DOWN == 32 ) - fac_Q16 = SKP_LSHIFT( psLP->transition_frame_no, 16 - 5 ); -#else - fac_Q16 = SKP_DIV32_16( SKP_LSHIFT( psLP->transition_frame_no, 16 ), TRANSITION_INT_STEPS_DOWN ); -#endif - ind = SKP_RSHIFT( fac_Q16, 16 ); - fac_Q16 -= SKP_LSHIFT( ind, 16 ); - - SKP_assert( ind >= 0 ); - SKP_assert( ind < TRANSITION_INT_NUM ); - - /* Interpolate filter coefficients */ - SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 ); - - /* Increment transition frame number for next frame */ - psLP->transition_frame_no++; - - } else if( psLP->transition_frame_no == TRANSITION_FRAMES_DOWN ) { - /* End of transition phase */ - SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, TRANSITION_INT_NUM - 1, 0 ); - } - } else if( psLP->mode == 1 ) { - if( psLP->transition_frame_no < TRANSITION_FRAMES_UP ) { - /* Calculate index and interpolation factor for interpolation */ -#if( TRANSITION_INT_STEPS_UP == 64 ) - fac_Q16 = SKP_LSHIFT( TRANSITION_FRAMES_UP - psLP->transition_frame_no, 16 - 6 ); -#else - fac_Q16 = SKP_DIV32_16( SKP_LSHIFT( TRANSITION_FRAMES_UP - psLP->transition_frame_no, 16 ), TRANSITION_INT_STEPS_UP ); -#endif - ind = SKP_RSHIFT( fac_Q16, 16 ); - fac_Q16 -= SKP_LSHIFT( ind, 16 ); - - SKP_assert( ind >= 0 ); - SKP_assert( ind < TRANSITION_INT_NUM ); - - /* Interpolate filter coefficients */ - SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 ); - - /* Increment transition frame number for next frame */ - psLP->transition_frame_no++; - - } else if( psLP->transition_frame_no == TRANSITION_FRAMES_UP ) { - /* End of transition phase */ - SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, 0, 0 ); - } - } - } - - if( psLP->transition_frame_no > 0 ) { - /* ARMA low-pass filtering */ - SKP_assert( TRANSITION_NB == 3 && TRANSITION_NA == 2 ); - SKP_Silk_biquad_alt( in, B_Q28, A_Q28, psLP->In_LP_State, out, frame_length ); - } else { - /* Instead of using the filter, copy input directly to output */ - SKP_memcpy( out, in, frame_length * sizeof( SKP_int16 ) ); - } -} -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* + + Elliptic/Cauer filters designed with 0.1 dB passband ripple, + 80 dB minimum stopband attenuation, and + [0.95 : 0.15 : 0.35] normalized cut off frequencies. + +*/ +#include "SKP_Silk_main.h" + +#if SWITCH_TRANSITION_FILTERING + +/* Helper function, that interpolates the filter taps */ +SKP_INLINE void SKP_Silk_LP_interpolate_filter_taps( + SKP_int32 B_Q28[ TRANSITION_NB ], + SKP_int32 A_Q28[ TRANSITION_NA ], + const SKP_int ind, + const SKP_int32 fac_Q16 +) +{ + SKP_int nb, na; + + if( ind < TRANSITION_INT_NUM - 1 ) { + if( fac_Q16 > 0 ) { + if( fac_Q16 == SKP_SAT16( fac_Q16 ) ) { /* fac_Q16 is in range of a 16-bit int */ + /* Piece-wise linear interpolation of B and A */ + for( nb = 0; nb < TRANSITION_NB; nb++ ) { + B_Q28[ nb ] = SKP_SMLAWB( + SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ], + SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] - + SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ], + fac_Q16 ); + } + for( na = 0; na < TRANSITION_NA; na++ ) { + A_Q28[ na ] = SKP_SMLAWB( + SKP_Silk_Transition_LP_A_Q28[ ind ][ na ], + SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ] - + SKP_Silk_Transition_LP_A_Q28[ ind ][ na ], + fac_Q16 ); + } + } else if( fac_Q16 == ( 1 << 15 ) ) { /* Neither fac_Q16 nor ( ( 1 << 16 ) - fac_Q16 ) is in range of a 16-bit int */ + + /* Piece-wise linear interpolation of B and A */ + for( nb = 0; nb < TRANSITION_NB; nb++ ) { + B_Q28[ nb ] = SKP_RSHIFT( + SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ] + + SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], + 1 ); + } + for( na = 0; na < TRANSITION_NA; na++ ) { + A_Q28[ na ] = SKP_RSHIFT( + SKP_Silk_Transition_LP_A_Q28[ ind ][ na ] + + SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ], + 1 ); + } + } else { /* ( ( 1 << 16 ) - fac_Q16 ) is in range of a 16-bit int */ + + SKP_assert( ( ( 1 << 16 ) - fac_Q16 ) == SKP_SAT16( ( ( 1 << 16 ) - fac_Q16) ) ); + /* Piece-wise linear interpolation of B and A */ + for( nb = 0; nb < TRANSITION_NB; nb++ ) { + B_Q28[ nb ] = SKP_SMLAWB( + SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], + SKP_Silk_Transition_LP_B_Q28[ ind ][ nb ] - + SKP_Silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], + ( 1 << 16 ) - fac_Q16 ); + } + for( na = 0; na < TRANSITION_NA; na++ ) { + A_Q28[ na ] = SKP_SMLAWB( + SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ], + SKP_Silk_Transition_LP_A_Q28[ ind ][ na ] - + SKP_Silk_Transition_LP_A_Q28[ ind + 1 ][ na ], + ( 1 << 16 ) - fac_Q16 ); + } + } + } else { + SKP_memcpy( B_Q28, SKP_Silk_Transition_LP_B_Q28[ ind ], TRANSITION_NB * sizeof( SKP_int32 ) ); + SKP_memcpy( A_Q28, SKP_Silk_Transition_LP_A_Q28[ ind ], TRANSITION_NA * sizeof( SKP_int32 ) ); + } + } else { + SKP_memcpy( B_Q28, SKP_Silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NB * sizeof( SKP_int32 ) ); + SKP_memcpy( A_Q28, SKP_Silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NA * sizeof( SKP_int32 ) ); + } +} + +/* Low-pass filter with variable cutoff frequency based on */ +/* piece-wise linear interpolation between elliptic filters */ +/* Start by setting psEncC->transition_frame_no = 1; */ +/* Deactivate by setting psEncC->transition_frame_no = 0; */ +void SKP_Silk_LP_variable_cutoff( + SKP_Silk_LP_state *psLP, /* I/O LP filter state */ + SKP_int16 *out, /* O Low-pass filtered output signal */ + const SKP_int16 *in, /* I Input signal */ + const SKP_int frame_length /* I Frame length */ +) +{ + SKP_int32 B_Q28[ TRANSITION_NB ], A_Q28[ TRANSITION_NA ]; + SKP_int fac_Q16 = 0, ind = 0; + + SKP_assert( psLP->transition_frame_no >= 0 ); + SKP_assert( ( ( ( psLP->transition_frame_no <= TRANSITION_FRAMES_DOWN ) && ( psLP->mode == 0 ) ) || + ( ( psLP->transition_frame_no <= TRANSITION_FRAMES_UP ) && ( psLP->mode == 1 ) ) ) ); + + /* Interpolate filter coefficients if needed */ + if( psLP->transition_frame_no > 0 ) { + if( psLP->mode == 0 ) { + if( psLP->transition_frame_no < TRANSITION_FRAMES_DOWN ) { + /* Calculate index and interpolation factor for interpolation */ +#if( TRANSITION_INT_STEPS_DOWN == 32 ) + fac_Q16 = SKP_LSHIFT( psLP->transition_frame_no, 16 - 5 ); +#else + fac_Q16 = SKP_DIV32_16( SKP_LSHIFT( psLP->transition_frame_no, 16 ), TRANSITION_INT_STEPS_DOWN ); +#endif + ind = SKP_RSHIFT( fac_Q16, 16 ); + fac_Q16 -= SKP_LSHIFT( ind, 16 ); + + SKP_assert( ind >= 0 ); + SKP_assert( ind < TRANSITION_INT_NUM ); + + /* Interpolate filter coefficients */ + SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 ); + + /* Increment transition frame number for next frame */ + psLP->transition_frame_no++; + + } else if( psLP->transition_frame_no == TRANSITION_FRAMES_DOWN ) { + /* End of transition phase */ + SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, TRANSITION_INT_NUM - 1, 0 ); + } + } else if( psLP->mode == 1 ) { + if( psLP->transition_frame_no < TRANSITION_FRAMES_UP ) { + /* Calculate index and interpolation factor for interpolation */ +#if( TRANSITION_INT_STEPS_UP == 64 ) + fac_Q16 = SKP_LSHIFT( TRANSITION_FRAMES_UP - psLP->transition_frame_no, 16 - 6 ); +#else + fac_Q16 = SKP_DIV32_16( SKP_LSHIFT( TRANSITION_FRAMES_UP - psLP->transition_frame_no, 16 ), TRANSITION_INT_STEPS_UP ); +#endif + ind = SKP_RSHIFT( fac_Q16, 16 ); + fac_Q16 -= SKP_LSHIFT( ind, 16 ); + + SKP_assert( ind >= 0 ); + SKP_assert( ind < TRANSITION_INT_NUM ); + + /* Interpolate filter coefficients */ + SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 ); + + /* Increment transition frame number for next frame */ + psLP->transition_frame_no++; + + } else if( psLP->transition_frame_no == TRANSITION_FRAMES_UP ) { + /* End of transition phase */ + SKP_Silk_LP_interpolate_filter_taps( B_Q28, A_Q28, 0, 0 ); + } + } + } + + if( psLP->transition_frame_no > 0 ) { + /* ARMA low-pass filtering */ + SKP_assert( TRANSITION_NB == 3 && TRANSITION_NA == 2 ); + SKP_Silk_biquad_alt( in, B_Q28, A_Q28, psLP->In_LP_State, out, frame_length ); + } else { + /* Instead of using the filter, copy input directly to output */ + SKP_memcpy( out, in, frame_length * sizeof( SKP_int16 ) ); + } +} +#endif diff --git a/jni/silk/src/SKP_Silk_LSF_cos_table.c b/app/src/main/jni/silk/src/SKP_Silk_LSF_cos_table.c similarity index 98% rename from jni/silk/src/SKP_Silk_LSF_cos_table.c rename to app/src/main/jni/silk/src/SKP_Silk_LSF_cos_table.c index 099324f..e16865e 100644 --- a/jni/silk/src/SKP_Silk_LSF_cos_table.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LSF_cos_table.c @@ -1,65 +1,65 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -// Q12 values (even) -const SKP_int SKP_Silk_LSFCosTab_FIX_Q12[LSF_COS_TAB_SZ_FIX + 1] = { - 8192, 8190, 8182, 8170, - 8152, 8130, 8104, 8072, - 8034, 7994, 7946, 7896, - 7840, 7778, 7714, 7644, - 7568, 7490, 7406, 7318, - 7226, 7128, 7026, 6922, - 6812, 6698, 6580, 6458, - 6332, 6204, 6070, 5934, - 5792, 5648, 5502, 5352, - 5198, 5040, 4880, 4718, - 4552, 4382, 4212, 4038, - 3862, 3684, 3502, 3320, - 3136, 2948, 2760, 2570, - 2378, 2186, 1990, 1794, - 1598, 1400, 1202, 1002, - 802, 602, 402, 202, - 0, -202, -402, -602, - -802, -1002, -1202, -1400, - -1598, -1794, -1990, -2186, - -2378, -2570, -2760, -2948, - -3136, -3320, -3502, -3684, - -3862, -4038, -4212, -4382, - -4552, -4718, -4880, -5040, - -5198, -5352, -5502, -5648, - -5792, -5934, -6070, -6204, - -6332, -6458, -6580, -6698, - -6812, -6922, -7026, -7128, - -7226, -7318, -7406, -7490, - -7568, -7644, -7714, -7778, - -7840, -7896, -7946, -7994, - -8034, -8072, -8104, -8130, - -8152, -8170, -8182, -8190, - -8192 -}; +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +// Q12 values (even) +const SKP_int SKP_Silk_LSFCosTab_FIX_Q12[LSF_COS_TAB_SZ_FIX + 1] = { + 8192, 8190, 8182, 8170, + 8152, 8130, 8104, 8072, + 8034, 7994, 7946, 7896, + 7840, 7778, 7714, 7644, + 7568, 7490, 7406, 7318, + 7226, 7128, 7026, 6922, + 6812, 6698, 6580, 6458, + 6332, 6204, 6070, 5934, + 5792, 5648, 5502, 5352, + 5198, 5040, 4880, 4718, + 4552, 4382, 4212, 4038, + 3862, 3684, 3502, 3320, + 3136, 2948, 2760, 2570, + 2378, 2186, 1990, 1794, + 1598, 1400, 1202, 1002, + 802, 602, 402, 202, + 0, -202, -402, -602, + -802, -1002, -1202, -1400, + -1598, -1794, -1990, -2186, + -2378, -2570, -2760, -2948, + -3136, -3320, -3502, -3684, + -3862, -4038, -4212, -4382, + -4552, -4718, -4880, -5040, + -5198, -5352, -5502, -5648, + -5792, -5934, -6070, -6204, + -6332, -6458, -6580, -6698, + -6812, -6922, -7026, -7128, + -7226, -7318, -7406, -7490, + -7568, -7644, -7714, -7778, + -7840, -7896, -7946, -7994, + -8034, -8072, -8104, -8130, + -8152, -8170, -8182, -8190, + -8192 +}; diff --git a/jni/silk/src/SKP_Silk_LTP_analysis_filter_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_LTP_analysis_filter_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_LTP_analysis_filter_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_LTP_analysis_filter_FIX.c index af851c7..8e579a7 100644 --- a/jni/silk/src/SKP_Silk_LTP_analysis_filter_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LTP_analysis_filter_FIX.c @@ -1,85 +1,85 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -void SKP_Silk_LTP_analysis_filter_FIX( - SKP_int16 *LTP_res, /* O: LTP residual signal of length NB_SUBFR * ( pre_length + subfr_length ) */ - const SKP_int16 *x, /* I: Pointer to input signal with at least max( pitchL ) preceeding samples */ - const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ],/* I: LTP_ORDER LTP coefficients for each NB_SUBFR subframe */ - const SKP_int pitchL[ NB_SUBFR ], /* I: Pitch lag, one for each subframe */ - const SKP_int32 invGains_Qxx[ NB_SUBFR ], /* I: Inverse quantization gains, one for each subframe */ - const SKP_int Qxx, /* I: Inverse quantization gains Q domain */ - const SKP_int subfr_length, /* I: Length of each subframe */ - const SKP_int pre_length /* I: Length of the preceeding samples starting at &x[0] for each subframe */ -) -{ - const SKP_int16 *x_ptr, *x_lag_ptr; - SKP_int16 Btmp_Q14[ LTP_ORDER ]; - SKP_int16 *LTP_res_ptr; - SKP_int k, i, j; - SKP_int32 LTP_est; - - x_ptr = x; - LTP_res_ptr = LTP_res; - for( k = 0; k < NB_SUBFR; k++ ) { - - x_lag_ptr = x_ptr - pitchL[ k ]; - for( i = 0; i < LTP_ORDER; i++ ) { - Btmp_Q14[ i ] = LTPCoef_Q14[ k * LTP_ORDER + i ]; - } - - /* LTP analysis FIR filter */ - for( i = 0; i < subfr_length + pre_length; i++ ) { - LTP_res_ptr[ i ] = x_ptr[ i ]; - - /* Long-term prediction */ - LTP_est = SKP_SMULBB( x_lag_ptr[ LTP_ORDER / 2 ], Btmp_Q14[ 0 ] ); - for( j = 1; j < LTP_ORDER; j++ ) { - LTP_est = SKP_SMLABB_ovflw( LTP_est, x_lag_ptr[ LTP_ORDER / 2 - j ], Btmp_Q14[ j ] ); - } - LTP_est = SKP_RSHIFT_ROUND( LTP_est, 14 ); // round and -> Q0 - - /* Subtract long-term prediction */ - LTP_res_ptr[ i ] = ( SKP_int16 )SKP_SAT16( ( SKP_int32 )x_ptr[ i ] - LTP_est ); - - /* Scale residual */ - if( Qxx == 16 ) { - LTP_res_ptr[ i ] = SKP_SMULWB( invGains_Qxx[ k ], LTP_res_ptr[ i ] ); - } else { - LTP_res_ptr[ i ] = ( SKP_int16 )SKP_CHECK_FIT16( SKP_RSHIFT64( SKP_SMULL( invGains_Qxx[ k ], LTP_res_ptr[ i ] ), Qxx ) ); - } - - x_lag_ptr++; - } - - /* Update pointers */ - LTP_res_ptr += subfr_length + pre_length; - x_ptr += subfr_length; - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +void SKP_Silk_LTP_analysis_filter_FIX( + SKP_int16 *LTP_res, /* O: LTP residual signal of length NB_SUBFR * ( pre_length + subfr_length ) */ + const SKP_int16 *x, /* I: Pointer to input signal with at least max( pitchL ) preceeding samples */ + const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ],/* I: LTP_ORDER LTP coefficients for each NB_SUBFR subframe */ + const SKP_int pitchL[ NB_SUBFR ], /* I: Pitch lag, one for each subframe */ + const SKP_int32 invGains_Qxx[ NB_SUBFR ], /* I: Inverse quantization gains, one for each subframe */ + const SKP_int Qxx, /* I: Inverse quantization gains Q domain */ + const SKP_int subfr_length, /* I: Length of each subframe */ + const SKP_int pre_length /* I: Length of the preceeding samples starting at &x[0] for each subframe */ +) +{ + const SKP_int16 *x_ptr, *x_lag_ptr; + SKP_int16 Btmp_Q14[ LTP_ORDER ]; + SKP_int16 *LTP_res_ptr; + SKP_int k, i, j; + SKP_int32 LTP_est; + + x_ptr = x; + LTP_res_ptr = LTP_res; + for( k = 0; k < NB_SUBFR; k++ ) { + + x_lag_ptr = x_ptr - pitchL[ k ]; + for( i = 0; i < LTP_ORDER; i++ ) { + Btmp_Q14[ i ] = LTPCoef_Q14[ k * LTP_ORDER + i ]; + } + + /* LTP analysis FIR filter */ + for( i = 0; i < subfr_length + pre_length; i++ ) { + LTP_res_ptr[ i ] = x_ptr[ i ]; + + /* Long-term prediction */ + LTP_est = SKP_SMULBB( x_lag_ptr[ LTP_ORDER / 2 ], Btmp_Q14[ 0 ] ); + for( j = 1; j < LTP_ORDER; j++ ) { + LTP_est = SKP_SMLABB_ovflw( LTP_est, x_lag_ptr[ LTP_ORDER / 2 - j ], Btmp_Q14[ j ] ); + } + LTP_est = SKP_RSHIFT_ROUND( LTP_est, 14 ); // round and -> Q0 + + /* Subtract long-term prediction */ + LTP_res_ptr[ i ] = ( SKP_int16 )SKP_SAT16( ( SKP_int32 )x_ptr[ i ] - LTP_est ); + + /* Scale residual */ + if( Qxx == 16 ) { + LTP_res_ptr[ i ] = SKP_SMULWB( invGains_Qxx[ k ], LTP_res_ptr[ i ] ); + } else { + LTP_res_ptr[ i ] = ( SKP_int16 )SKP_CHECK_FIT16( SKP_RSHIFT64( SKP_SMULL( invGains_Qxx[ k ], LTP_res_ptr[ i ] ), Qxx ) ); + } + + x_lag_ptr++; + } + + /* Update pointers */ + LTP_res_ptr += subfr_length + pre_length; + x_ptr += subfr_length; + } +} + diff --git a/jni/silk/src/SKP_Silk_LTP_scale_ctrl_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_LTP_scale_ctrl_FIX.c similarity index 97% rename from jni/silk/src/SKP_Silk_LTP_scale_ctrl_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_LTP_scale_ctrl_FIX.c index 4d69a8a..145f5c7 100644 --- a/jni/silk/src/SKP_Silk_LTP_scale_ctrl_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_LTP_scale_ctrl_FIX.c @@ -1,81 +1,81 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -#define NB_THRESHOLDS 11 - -/* Table containing trained thresholds for LTP scaling */ -static const SKP_int16 LTPScaleThresholds_Q15[ NB_THRESHOLDS ] = -{ - 31129, 26214, 16384, 13107, 9830, 6554, - 4915, 3276, 2621, 2458, 0 -}; - -void SKP_Silk_LTP_scale_ctrl_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state FIX */ - SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control FIX */ -) -{ - SKP_int round_loss, frames_per_packet; - SKP_int g_out_Q5, g_limit_Q15, thrld1_Q15, thrld2_Q15; - - /* 1st order high-pass filter */ - psEnc->HPLTPredCodGain_Q7 = SKP_max_int( psEncCtrl->LTPredCodGain_Q7 - psEnc->prevLTPredCodGain_Q7, 0 ) - + SKP_RSHIFT_ROUND( psEnc->HPLTPredCodGain_Q7, 1 ); - - psEnc->prevLTPredCodGain_Q7 = psEncCtrl->LTPredCodGain_Q7; - - /* combine input and filtered input */ - g_out_Q5 = SKP_RSHIFT_ROUND( SKP_RSHIFT( psEncCtrl->LTPredCodGain_Q7, 1 ) + SKP_RSHIFT( psEnc->HPLTPredCodGain_Q7, 1 ), 3 ); - g_limit_Q15 = SKP_Silk_sigm_Q15( g_out_Q5 - ( 3 << 5 ) ); /* mulitplid with 0.5 */ - - /* Default is minimum scaling */ - psEncCtrl->sCmn.LTP_scaleIndex = 0; - - /* Round the loss measure to whole pct */ - round_loss = ( SKP_int )psEnc->sCmn.PacketLoss_perc; - - /* Only scale if first frame in packet 0% */ - if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { - - frames_per_packet = SKP_DIV32_16( psEnc->sCmn.PacketSize_ms, FRAME_LENGTH_MS ); - - round_loss += frames_per_packet - 1; - thrld1_Q15 = LTPScaleThresholds_Q15[ SKP_min_int( round_loss, NB_THRESHOLDS - 1 ) ]; - thrld2_Q15 = LTPScaleThresholds_Q15[ SKP_min_int( round_loss + 1, NB_THRESHOLDS - 1 ) ]; - - if( g_limit_Q15 > thrld1_Q15 ) { - /* Maximum scaling */ - psEncCtrl->sCmn.LTP_scaleIndex = 2; - } else if( g_limit_Q15 > thrld2_Q15 ) { - /* Medium scaling */ - psEncCtrl->sCmn.LTP_scaleIndex = 1; - } - } - psEncCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ psEncCtrl->sCmn.LTP_scaleIndex ]; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +#define NB_THRESHOLDS 11 + +/* Table containing trained thresholds for LTP scaling */ +static const SKP_int16 LTPScaleThresholds_Q15[ NB_THRESHOLDS ] = +{ + 31129, 26214, 16384, 13107, 9830, 6554, + 4915, 3276, 2621, 2458, 0 +}; + +void SKP_Silk_LTP_scale_ctrl_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state FIX */ + SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control FIX */ +) +{ + SKP_int round_loss, frames_per_packet; + SKP_int g_out_Q5, g_limit_Q15, thrld1_Q15, thrld2_Q15; + + /* 1st order high-pass filter */ + psEnc->HPLTPredCodGain_Q7 = SKP_max_int( psEncCtrl->LTPredCodGain_Q7 - psEnc->prevLTPredCodGain_Q7, 0 ) + + SKP_RSHIFT_ROUND( psEnc->HPLTPredCodGain_Q7, 1 ); + + psEnc->prevLTPredCodGain_Q7 = psEncCtrl->LTPredCodGain_Q7; + + /* combine input and filtered input */ + g_out_Q5 = SKP_RSHIFT_ROUND( SKP_RSHIFT( psEncCtrl->LTPredCodGain_Q7, 1 ) + SKP_RSHIFT( psEnc->HPLTPredCodGain_Q7, 1 ), 3 ); + g_limit_Q15 = SKP_Silk_sigm_Q15( g_out_Q5 - ( 3 << 5 ) ); /* mulitplid with 0.5 */ + + /* Default is minimum scaling */ + psEncCtrl->sCmn.LTP_scaleIndex = 0; + + /* Round the loss measure to whole pct */ + round_loss = ( SKP_int )psEnc->sCmn.PacketLoss_perc; + + /* Only scale if first frame in packet 0% */ + if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { + + frames_per_packet = SKP_DIV32_16( psEnc->sCmn.PacketSize_ms, FRAME_LENGTH_MS ); + + round_loss += frames_per_packet - 1; + thrld1_Q15 = LTPScaleThresholds_Q15[ SKP_min_int( round_loss, NB_THRESHOLDS - 1 ) ]; + thrld2_Q15 = LTPScaleThresholds_Q15[ SKP_min_int( round_loss + 1, NB_THRESHOLDS - 1 ) ]; + + if( g_limit_Q15 > thrld1_Q15 ) { + /* Maximum scaling */ + psEncCtrl->sCmn.LTP_scaleIndex = 2; + } else if( g_limit_Q15 > thrld2_Q15 ) { + /* Medium scaling */ + psEncCtrl->sCmn.LTP_scaleIndex = 1; + } + } + psEncCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ psEncCtrl->sCmn.LTP_scaleIndex ]; +} diff --git a/jni/silk/src/SKP_Silk_MA.c b/app/src/main/jni/silk/src/SKP_Silk_MA.c similarity index 97% rename from jni/silk/src/SKP_Silk_MA.c rename to app/src/main/jni/silk/src/SKP_Silk_MA.c index cdfaca7..196c34e 100644 --- a/jni/silk/src/SKP_Silk_MA.c +++ b/app/src/main/jni/silk/src/SKP_Silk_MA.c @@ -1,232 +1,232 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_MA.c * - * * - * Variable order MA filter * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Variable order MA filter */ -void SKP_Silk_MA( - const SKP_int16 *in, /* I: input signal */ - const SKP_int16 *B, /* I: MA coefficients, Q13 [order+1] */ - SKP_int32 *S, /* I/O: state vector [order] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len, /* I: signal length */ - const SKP_int32 order /* I: filter order */ -) -{ - SKP_int k, d, in16; - SKP_int32 out32; - - for( k = 0; k < len; k++ ) { - in16 = in[ k ]; - out32 = SKP_SMLABB( S[ 0 ], in16, B[ 0 ] ); - out32 = SKP_RSHIFT_ROUND( out32, 13 ); - - for( d = 1; d < order; d++ ) { - S[ d - 1 ] = SKP_SMLABB( S[ d ], in16, B[ d ] ); - } - S[ order - 1 ] = SKP_SMULBB( in16, B[ order ] ); - - /* Limit */ - out[ k ] = (SKP_int16)SKP_SAT16( out32 ); - } -} -/* Variable order MA prediction error filter */ -void SKP_Silk_MA_Prediction( - const SKP_int16 *in, /* I: Input signal */ - const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ - SKP_int32 *S, /* I/O: State vector [order] */ - SKP_int16 *out, /* O: Output signal */ - const SKP_int32 len, /* I: Signal length */ - const SKP_int32 order /* I: Filter order */ -) -{ - SKP_int k, d, in16; - SKP_int32 out32; - SKP_int32 B32; - - if( ( order & 1 ) == 0 && (SKP_int32)( (SKP_int_ptr_size)B & 3 ) == 0 ) { - /* Even order and 4-byte aligned coefficient array */ - - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLABB and SMLABT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLABB and SMLABT instructions should solve the problem. */ - for( k = 0; k < len; k++ ) { - in16 = in[ k ]; - out32 = SKP_LSHIFT( in16, 12 ) - S[ 0 ]; - out32 = SKP_RSHIFT_ROUND( out32, 12 ); - - for( d = 0; d < order - 2; d += 2 ) { - B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ - S[ d ] = SKP_SMLABB_ovflw( S[ d + 1 ], in16, B32 ); - S[ d + 1 ] = SKP_SMLABT_ovflw( S[ d + 2 ], in16, B32 ); - } - B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ - S[ order - 2 ] = SKP_SMLABB_ovflw( S[ order - 1 ], in16, B32 ); - S[ order - 1 ] = SKP_SMULBT( in16, B32 ); - - /* Limit */ - out[ k ] = (SKP_int16)SKP_SAT16( out32 ); - } - } else { - /* Odd order or not 4-byte aligned coefficient array */ - for( k = 0; k < len; k++ ) { - in16 = in[ k ]; - out32 = SKP_LSHIFT( in16, 12 ) - S[ 0 ]; - out32 = SKP_RSHIFT_ROUND( out32, 12 ); - - for( d = 0; d < order - 1; d++ ) { - S[ d ] = SKP_SMLABB_ovflw( S[ d + 1 ], in16, B[ d ] ); - } - S[ order - 1 ] = SKP_SMULBB( in16, B[ order - 1 ] ); - - /* Limit */ - out[ k ] = (SKP_int16)SKP_SAT16( out32 ); - } - } -} - -void SKP_Silk_MA_Prediction_Q13( - const SKP_int16 *in, /* I: input signal */ - const SKP_int16 *B, /* I: MA prediction coefficients, Q13 [order] */ - SKP_int32 *S, /* I/O: state vector [order] */ - SKP_int16 *out, /* O: output signal */ - SKP_int32 len, /* I: signal length */ - SKP_int32 order /* I: filter order */ -) -{ - SKP_int k, d, in16; - SKP_int32 out32, B32; - - if( ( order & 1 ) == 0 && (SKP_int32)( (SKP_int_ptr_size)B & 3 ) == 0 ) { - /* Even order and 4-byte aligned coefficient array */ - - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLABB and SMLABT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLABB and SMLABT instructions should solve the problem. */ - for( k = 0; k < len; k++ ) { - in16 = in[ k ]; - out32 = SKP_LSHIFT( in16, 13 ) - S[ 0 ]; - out32 = SKP_RSHIFT_ROUND( out32, 13 ); - - for( d = 0; d < order - 2; d += 2 ) { - B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ - S[ d ] = SKP_SMLABB( S[ d + 1 ], in16, B32 ); - S[ d + 1 ] = SKP_SMLABT( S[ d + 2 ], in16, B32 ); - } - B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ - S[ order - 2 ] = SKP_SMLABB( S[ order - 1 ], in16, B32 ); - S[ order - 1 ] = SKP_SMULBT( in16, B32 ); - - /* Limit */ - out[ k ] = (SKP_int16)SKP_SAT16( out32 ); - } - } else { - /* Odd order or not 4-byte aligned coefficient array */ - for( k = 0; k < len; k++ ) { - in16 = in[ k ]; - out32 = SKP_LSHIFT( in16, 13 ) - S[ 0 ]; - out32 = SKP_RSHIFT_ROUND( out32, 13 ); - - for( d = 0; d < order - 1; d++ ) { - S[ d ] = SKP_SMLABB( S[ d + 1 ], in16, B[ d ] ); - } - S[ order - 1 ] = SKP_SMULBB( in16, B[ order - 1 ] ); - - /* Limit */ - out[ k ] = (SKP_int16)SKP_SAT16( out32 ); - } - } -} -/* Variable order MA prediction error filter. */ -/* Inverse filter of SKP_Silk_LPC_synthesis_filter */ -void SKP_Silk_LPC_analysis_filter( - const SKP_int16 *in, /* I: Input signal */ - const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ - SKP_int16 *S, /* I/O: State vector [order] */ - SKP_int16 *out, /* O: Output signal */ - const SKP_int32 len, /* I: Signal length */ - const SKP_int32 Order /* I: Filter order */ -) -{ - SKP_int k, j, idx, Order_half = SKP_RSHIFT( Order, 1 ); - SKP_int32 Btmp, B_align_Q12[ SigProc_MAX_ORDER_LPC >> 1 ], out32_Q12, out32; - SKP_int16 SA, SB; - /* Order must be even */ - SKP_assert( 2 * Order_half == Order ); - - /* Combine two A_Q12 values and ensure 32-bit alignment */ - for( k = 0; k < Order_half; k++ ) { - idx = SKP_SMULBB( 2, k ); - B_align_Q12[ k ] = ( ( (SKP_int32)B[ idx ] ) & 0x0000ffff ) | SKP_LSHIFT( (SKP_int32)B[ idx + 1 ], 16 ); - } - - /* S[] values are in Q0 */ - for( k = 0; k < len; k++ ) { - SA = S[ 0 ]; - out32_Q12 = 0; - for( j = 0; j < ( Order_half - 1 ); j++ ) { - idx = SKP_SMULBB( 2, j ) + 1; - /* Multiply-add two prediction coefficients for each loop */ - Btmp = B_align_Q12[ j ]; - SB = S[ idx ]; - S[ idx ] = SA; - out32_Q12 = SKP_SMLABB( out32_Q12, SA, Btmp ); - out32_Q12 = SKP_SMLABT( out32_Q12, SB, Btmp ); - SA = S[ idx + 1 ]; - S[ idx + 1 ] = SB; - } - - /* Unrolled loop: epilog */ - Btmp = B_align_Q12[ Order_half - 1 ]; - SB = S[ Order - 1 ]; - S[ Order - 1 ] = SA; - out32_Q12 = SKP_SMLABB( out32_Q12, SA, Btmp ); - out32_Q12 = SKP_SMLABT( out32_Q12, SB, Btmp ); - - /* Subtract prediction */ - out32_Q12 = SKP_SUB_SAT32( SKP_LSHIFT( (SKP_int32)in[ k ], 12 ), out32_Q12 ); - - /* Scale to Q0 */ - out32 = SKP_RSHIFT_ROUND( out32_Q12, 12 ); - - /* Saturate output */ - out[ k ] = (SKP_int16)SKP_SAT16( out32 ); - - /* Move input line */ - S[ 0 ] = in[ k ]; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_MA.c * + * * + * Variable order MA filter * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Variable order MA filter */ +void SKP_Silk_MA( + const SKP_int16 *in, /* I: input signal */ + const SKP_int16 *B, /* I: MA coefficients, Q13 [order+1] */ + SKP_int32 *S, /* I/O: state vector [order] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len, /* I: signal length */ + const SKP_int32 order /* I: filter order */ +) +{ + SKP_int k, d, in16; + SKP_int32 out32; + + for( k = 0; k < len; k++ ) { + in16 = in[ k ]; + out32 = SKP_SMLABB( S[ 0 ], in16, B[ 0 ] ); + out32 = SKP_RSHIFT_ROUND( out32, 13 ); + + for( d = 1; d < order; d++ ) { + S[ d - 1 ] = SKP_SMLABB( S[ d ], in16, B[ d ] ); + } + S[ order - 1 ] = SKP_SMULBB( in16, B[ order ] ); + + /* Limit */ + out[ k ] = (SKP_int16)SKP_SAT16( out32 ); + } +} +/* Variable order MA prediction error filter */ +void SKP_Silk_MA_Prediction( + const SKP_int16 *in, /* I: Input signal */ + const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ + SKP_int32 *S, /* I/O: State vector [order] */ + SKP_int16 *out, /* O: Output signal */ + const SKP_int32 len, /* I: Signal length */ + const SKP_int32 order /* I: Filter order */ +) +{ + SKP_int k, d, in16; + SKP_int32 out32; + SKP_int32 B32; + + if( ( order & 1 ) == 0 && (SKP_int32)( (SKP_int_ptr_size)B & 3 ) == 0 ) { + /* Even order and 4-byte aligned coefficient array */ + + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLABB and SMLABT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLABB and SMLABT instructions should solve the problem. */ + for( k = 0; k < len; k++ ) { + in16 = in[ k ]; + out32 = SKP_LSHIFT( in16, 12 ) - S[ 0 ]; + out32 = SKP_RSHIFT_ROUND( out32, 12 ); + + for( d = 0; d < order - 2; d += 2 ) { + B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ + S[ d ] = SKP_SMLABB_ovflw( S[ d + 1 ], in16, B32 ); + S[ d + 1 ] = SKP_SMLABT_ovflw( S[ d + 2 ], in16, B32 ); + } + B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ + S[ order - 2 ] = SKP_SMLABB_ovflw( S[ order - 1 ], in16, B32 ); + S[ order - 1 ] = SKP_SMULBT( in16, B32 ); + + /* Limit */ + out[ k ] = (SKP_int16)SKP_SAT16( out32 ); + } + } else { + /* Odd order or not 4-byte aligned coefficient array */ + for( k = 0; k < len; k++ ) { + in16 = in[ k ]; + out32 = SKP_LSHIFT( in16, 12 ) - S[ 0 ]; + out32 = SKP_RSHIFT_ROUND( out32, 12 ); + + for( d = 0; d < order - 1; d++ ) { + S[ d ] = SKP_SMLABB_ovflw( S[ d + 1 ], in16, B[ d ] ); + } + S[ order - 1 ] = SKP_SMULBB( in16, B[ order - 1 ] ); + + /* Limit */ + out[ k ] = (SKP_int16)SKP_SAT16( out32 ); + } + } +} + +void SKP_Silk_MA_Prediction_Q13( + const SKP_int16 *in, /* I: input signal */ + const SKP_int16 *B, /* I: MA prediction coefficients, Q13 [order] */ + SKP_int32 *S, /* I/O: state vector [order] */ + SKP_int16 *out, /* O: output signal */ + SKP_int32 len, /* I: signal length */ + SKP_int32 order /* I: filter order */ +) +{ + SKP_int k, d, in16; + SKP_int32 out32, B32; + + if( ( order & 1 ) == 0 && (SKP_int32)( (SKP_int_ptr_size)B & 3 ) == 0 ) { + /* Even order and 4-byte aligned coefficient array */ + + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLABB and SMLABT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLABB and SMLABT instructions should solve the problem. */ + for( k = 0; k < len; k++ ) { + in16 = in[ k ]; + out32 = SKP_LSHIFT( in16, 13 ) - S[ 0 ]; + out32 = SKP_RSHIFT_ROUND( out32, 13 ); + + for( d = 0; d < order - 2; d += 2 ) { + B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ + S[ d ] = SKP_SMLABB( S[ d + 1 ], in16, B32 ); + S[ d + 1 ] = SKP_SMLABT( S[ d + 2 ], in16, B32 ); + } + B32 = *( (SKP_int32*)&B[ d ] ); /* read two coefficients at once */ + S[ order - 2 ] = SKP_SMLABB( S[ order - 1 ], in16, B32 ); + S[ order - 1 ] = SKP_SMULBT( in16, B32 ); + + /* Limit */ + out[ k ] = (SKP_int16)SKP_SAT16( out32 ); + } + } else { + /* Odd order or not 4-byte aligned coefficient array */ + for( k = 0; k < len; k++ ) { + in16 = in[ k ]; + out32 = SKP_LSHIFT( in16, 13 ) - S[ 0 ]; + out32 = SKP_RSHIFT_ROUND( out32, 13 ); + + for( d = 0; d < order - 1; d++ ) { + S[ d ] = SKP_SMLABB( S[ d + 1 ], in16, B[ d ] ); + } + S[ order - 1 ] = SKP_SMULBB( in16, B[ order - 1 ] ); + + /* Limit */ + out[ k ] = (SKP_int16)SKP_SAT16( out32 ); + } + } +} +/* Variable order MA prediction error filter. */ +/* Inverse filter of SKP_Silk_LPC_synthesis_filter */ +void SKP_Silk_LPC_analysis_filter( + const SKP_int16 *in, /* I: Input signal */ + const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ + SKP_int16 *S, /* I/O: State vector [order] */ + SKP_int16 *out, /* O: Output signal */ + const SKP_int32 len, /* I: Signal length */ + const SKP_int32 Order /* I: Filter order */ +) +{ + SKP_int k, j, idx, Order_half = SKP_RSHIFT( Order, 1 ); + SKP_int32 Btmp, B_align_Q12[ SigProc_MAX_ORDER_LPC >> 1 ], out32_Q12, out32; + SKP_int16 SA, SB; + /* Order must be even */ + SKP_assert( 2 * Order_half == Order ); + + /* Combine two A_Q12 values and ensure 32-bit alignment */ + for( k = 0; k < Order_half; k++ ) { + idx = SKP_SMULBB( 2, k ); + B_align_Q12[ k ] = ( ( (SKP_int32)B[ idx ] ) & 0x0000ffff ) | SKP_LSHIFT( (SKP_int32)B[ idx + 1 ], 16 ); + } + + /* S[] values are in Q0 */ + for( k = 0; k < len; k++ ) { + SA = S[ 0 ]; + out32_Q12 = 0; + for( j = 0; j < ( Order_half - 1 ); j++ ) { + idx = SKP_SMULBB( 2, j ) + 1; + /* Multiply-add two prediction coefficients for each loop */ + Btmp = B_align_Q12[ j ]; + SB = S[ idx ]; + S[ idx ] = SA; + out32_Q12 = SKP_SMLABB( out32_Q12, SA, Btmp ); + out32_Q12 = SKP_SMLABT( out32_Q12, SB, Btmp ); + SA = S[ idx + 1 ]; + S[ idx + 1 ] = SB; + } + + /* Unrolled loop: epilog */ + Btmp = B_align_Q12[ Order_half - 1 ]; + SB = S[ Order - 1 ]; + S[ Order - 1 ] = SA; + out32_Q12 = SKP_SMLABB( out32_Q12, SA, Btmp ); + out32_Q12 = SKP_SMLABT( out32_Q12, SB, Btmp ); + + /* Subtract prediction */ + out32_Q12 = SKP_SUB_SAT32( SKP_LSHIFT( (SKP_int32)in[ k ], 12 ), out32_Q12 ); + + /* Scale to Q0 */ + out32 = SKP_RSHIFT_ROUND( out32_Q12, 12 ); + + /* Saturate output */ + out[ k ] = (SKP_int16)SKP_SAT16( out32 ); + + /* Move input line */ + S[ 0 ] = in[ k ]; + } +} diff --git a/jni/silk/src/SKP_Silk_NLSF2A.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF2A.c similarity index 97% rename from jni/silk/src/SKP_Silk_NLSF2A.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF2A.c index 262b654..9ae243e 100644 --- a/jni/silk/src/SKP_Silk_NLSF2A.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF2A.c @@ -1,150 +1,150 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* conversion between prediction filter coefficients and LSFs */ -/* order should be even */ -/* a piecewise linear approximation maps LSF <-> cos(LSF) */ -/* therefore the result is not accurate LSFs, but the two */ -/* function are accurate inverses of each other */ - -#include "SKP_Silk_SigProc_FIX.h" - -/* helper function for NLSF2A(..) */ -SKP_INLINE void SKP_Silk_NLSF2A_find_poly( - SKP_int32 *out, /* o intermediate polynomial, Q20 */ - const SKP_int32 *cLSF, /* i vector of interleaved 2*cos(LSFs), Q20 */ - SKP_int dd /* i polynomial order (= 1/2 * filter order) */ -) -{ - SKP_int k, n; - SKP_int32 ftmp; - - out[0] = SKP_LSHIFT( 1, 20 ); - out[1] = -cLSF[0]; - for( k = 1; k < dd; k++ ) { - ftmp = cLSF[2*k]; // Q20 - out[k+1] = SKP_LSHIFT( out[k-1], 1 ) - (SKP_int32)SKP_RSHIFT_ROUND64( SKP_SMULL( ftmp, out[k] ), 20 ); - for( n = k; n > 1; n-- ) { - out[n] += out[n-2] - (SKP_int32)SKP_RSHIFT_ROUND64( SKP_SMULL( ftmp, out[n-1] ), 20 ); - } - out[1] -= ftmp; - } -} - -/* compute whitening filter coefficients from normalized line spectral frequencies */ -void SKP_Silk_NLSF2A( - SKP_int16 *a, /* o monic whitening filter coefficients in Q12, [d] */ - const SKP_int *NLSF, /* i normalized line spectral frequencies in Q15, [d] */ - const SKP_int d /* i filter order (should be even) */ -) -{ - SKP_int k, i, dd; - SKP_int32 cos_LSF_Q20[SigProc_MAX_ORDER_LPC]; - SKP_int32 P[SigProc_MAX_ORDER_LPC/2+1], Q[SigProc_MAX_ORDER_LPC/2+1]; - SKP_int32 Ptmp, Qtmp; - SKP_int32 f_int; - SKP_int32 f_frac; - SKP_int32 cos_val, delta; - SKP_int32 a_int32[SigProc_MAX_ORDER_LPC]; - SKP_int32 maxabs, absval, idx=0, sc_Q16; - - SKP_assert(LSF_COS_TAB_SZ_FIX == 128); - - /* convert LSFs to 2*cos(LSF(i)), using piecewise linear curve from table */ - for( k = 0; k < d; k++ ) { - SKP_assert(NLSF[k] >= 0 ); - SKP_assert(NLSF[k] <= 32767 ); - - /* f_int on a scale 0-127 (rounded down) */ - f_int = SKP_RSHIFT( NLSF[k], 15 - 7 ); - - /* f_frac, range: 0..255 */ - f_frac = NLSF[k] - SKP_LSHIFT( f_int, 15 - 7 ); - - SKP_assert(f_int >= 0); - SKP_assert(f_int < LSF_COS_TAB_SZ_FIX ); - - /* Read start and end value from table */ - cos_val = SKP_Silk_LSFCosTab_FIX_Q12[ f_int ]; /* Q12 */ - delta = SKP_Silk_LSFCosTab_FIX_Q12[ f_int + 1 ] - cos_val; /* Q12, with a range of 0..200 */ - - /* Linear interpolation */ - cos_LSF_Q20[k] = SKP_LSHIFT( cos_val, 8 ) + SKP_MUL( delta, f_frac ); /* Q20 */ - } - - dd = SKP_RSHIFT( d, 1 ); - - /* generate even and odd polynomials using convolution */ - SKP_Silk_NLSF2A_find_poly( P, &cos_LSF_Q20[0], dd ); - SKP_Silk_NLSF2A_find_poly( Q, &cos_LSF_Q20[1], dd ); - - /* convert even and odd polynomials to SKP_int32 Q12 filter coefs */ - for( k = 0; k < dd; k++ ) { - Ptmp = P[k+1] + P[k]; - Qtmp = Q[k+1] - Q[k]; - - /* the Ptmp and Qtmp values at this stage need to fit in int32 */ - - a_int32[k] = -SKP_RSHIFT_ROUND( Ptmp + Qtmp, 9 ); /* Q20 -> Q12 */ - a_int32[d-k-1] = SKP_RSHIFT_ROUND( Qtmp - Ptmp, 9 ); /* Q20 -> Q12 */ - } - - /* Limit the maximum absolute value of the prediction coefficients */ - for( i = 0; i < 10; i++ ) { - /* Find maximum absolute value and its index */ - maxabs = 0; - for( k = 0; k < d; k++ ) { - absval = SKP_abs( a_int32[k] ); - if( absval > maxabs ) { - maxabs = absval; - idx = k; - } - } - - if( maxabs > SKP_int16_MAX ) { - /* Reduce magnitude of prediction coefficients */ - sc_Q16 = 65470 - SKP_DIV32( SKP_MUL( 65470 >> 2, maxabs - SKP_int16_MAX ), - SKP_RSHIFT32( SKP_MUL( maxabs, idx + 1), 2 ) ); - SKP_Silk_bwexpander_32( a_int32, d, sc_Q16 ); - } else { - break; - } - } - - /* Reached the last iteration */ - if( i == 10 ) { - SKP_assert(0); - for( k = 0; k < d; k++ ) { - a_int32[k] = SKP_SAT16( a_int32[k] ); - } - } - - /* Return as SKP_int16 Q12 coefficients */ - for( k = 0; k < d; k++ ) { - a[k] = (SKP_int16)a_int32[k]; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* conversion between prediction filter coefficients and LSFs */ +/* order should be even */ +/* a piecewise linear approximation maps LSF <-> cos(LSF) */ +/* therefore the result is not accurate LSFs, but the two */ +/* function are accurate inverses of each other */ + +#include "SKP_Silk_SigProc_FIX.h" + +/* helper function for NLSF2A(..) */ +SKP_INLINE void SKP_Silk_NLSF2A_find_poly( + SKP_int32 *out, /* o intermediate polynomial, Q20 */ + const SKP_int32 *cLSF, /* i vector of interleaved 2*cos(LSFs), Q20 */ + SKP_int dd /* i polynomial order (= 1/2 * filter order) */ +) +{ + SKP_int k, n; + SKP_int32 ftmp; + + out[0] = SKP_LSHIFT( 1, 20 ); + out[1] = -cLSF[0]; + for( k = 1; k < dd; k++ ) { + ftmp = cLSF[2*k]; // Q20 + out[k+1] = SKP_LSHIFT( out[k-1], 1 ) - (SKP_int32)SKP_RSHIFT_ROUND64( SKP_SMULL( ftmp, out[k] ), 20 ); + for( n = k; n > 1; n-- ) { + out[n] += out[n-2] - (SKP_int32)SKP_RSHIFT_ROUND64( SKP_SMULL( ftmp, out[n-1] ), 20 ); + } + out[1] -= ftmp; + } +} + +/* compute whitening filter coefficients from normalized line spectral frequencies */ +void SKP_Silk_NLSF2A( + SKP_int16 *a, /* o monic whitening filter coefficients in Q12, [d] */ + const SKP_int *NLSF, /* i normalized line spectral frequencies in Q15, [d] */ + const SKP_int d /* i filter order (should be even) */ +) +{ + SKP_int k, i, dd; + SKP_int32 cos_LSF_Q20[SigProc_MAX_ORDER_LPC]; + SKP_int32 P[SigProc_MAX_ORDER_LPC/2+1], Q[SigProc_MAX_ORDER_LPC/2+1]; + SKP_int32 Ptmp, Qtmp; + SKP_int32 f_int; + SKP_int32 f_frac; + SKP_int32 cos_val, delta; + SKP_int32 a_int32[SigProc_MAX_ORDER_LPC]; + SKP_int32 maxabs, absval, idx=0, sc_Q16; + + SKP_assert(LSF_COS_TAB_SZ_FIX == 128); + + /* convert LSFs to 2*cos(LSF(i)), using piecewise linear curve from table */ + for( k = 0; k < d; k++ ) { + SKP_assert(NLSF[k] >= 0 ); + SKP_assert(NLSF[k] <= 32767 ); + + /* f_int on a scale 0-127 (rounded down) */ + f_int = SKP_RSHIFT( NLSF[k], 15 - 7 ); + + /* f_frac, range: 0..255 */ + f_frac = NLSF[k] - SKP_LSHIFT( f_int, 15 - 7 ); + + SKP_assert(f_int >= 0); + SKP_assert(f_int < LSF_COS_TAB_SZ_FIX ); + + /* Read start and end value from table */ + cos_val = SKP_Silk_LSFCosTab_FIX_Q12[ f_int ]; /* Q12 */ + delta = SKP_Silk_LSFCosTab_FIX_Q12[ f_int + 1 ] - cos_val; /* Q12, with a range of 0..200 */ + + /* Linear interpolation */ + cos_LSF_Q20[k] = SKP_LSHIFT( cos_val, 8 ) + SKP_MUL( delta, f_frac ); /* Q20 */ + } + + dd = SKP_RSHIFT( d, 1 ); + + /* generate even and odd polynomials using convolution */ + SKP_Silk_NLSF2A_find_poly( P, &cos_LSF_Q20[0], dd ); + SKP_Silk_NLSF2A_find_poly( Q, &cos_LSF_Q20[1], dd ); + + /* convert even and odd polynomials to SKP_int32 Q12 filter coefs */ + for( k = 0; k < dd; k++ ) { + Ptmp = P[k+1] + P[k]; + Qtmp = Q[k+1] - Q[k]; + + /* the Ptmp and Qtmp values at this stage need to fit in int32 */ + + a_int32[k] = -SKP_RSHIFT_ROUND( Ptmp + Qtmp, 9 ); /* Q20 -> Q12 */ + a_int32[d-k-1] = SKP_RSHIFT_ROUND( Qtmp - Ptmp, 9 ); /* Q20 -> Q12 */ + } + + /* Limit the maximum absolute value of the prediction coefficients */ + for( i = 0; i < 10; i++ ) { + /* Find maximum absolute value and its index */ + maxabs = 0; + for( k = 0; k < d; k++ ) { + absval = SKP_abs( a_int32[k] ); + if( absval > maxabs ) { + maxabs = absval; + idx = k; + } + } + + if( maxabs > SKP_int16_MAX ) { + /* Reduce magnitude of prediction coefficients */ + sc_Q16 = 65470 - SKP_DIV32( SKP_MUL( 65470 >> 2, maxabs - SKP_int16_MAX ), + SKP_RSHIFT32( SKP_MUL( maxabs, idx + 1), 2 ) ); + SKP_Silk_bwexpander_32( a_int32, d, sc_Q16 ); + } else { + break; + } + } + + /* Reached the last iteration */ + if( i == 10 ) { + SKP_assert(0); + for( k = 0; k < d; k++ ) { + a_int32[k] = SKP_SAT16( a_int32[k] ); + } + } + + /* Return as SKP_int16 Q12 coefficients */ + for( k = 0; k < d; k++ ) { + a[k] = (SKP_int16)a_int32[k]; + } +} diff --git a/jni/silk/src/SKP_Silk_NLSF2A_stable.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF2A_stable.c similarity index 98% rename from jni/silk/src/SKP_Silk_NLSF2A_stable.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF2A_stable.c index 05be911..f16e038 100644 --- a/jni/silk/src/SKP_Silk_NLSF2A_stable.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF2A_stable.c @@ -1,57 +1,57 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Convert NLSF parameters to stable AR prediction filter coefficients */ -void SKP_Silk_NLSF2A_stable( - SKP_int16 pAR_Q12[ MAX_LPC_ORDER ], /* O Stabilized AR coefs [LPC_order] */ - const SKP_int pNLSF[ MAX_LPC_ORDER ], /* I NLSF vector [LPC_order] */ - const SKP_int LPC_order /* I LPC/LSF order */ -) -{ - SKP_int i; - SKP_int32 invGain_Q30; - - SKP_Silk_NLSF2A( pAR_Q12, pNLSF, LPC_order ); - - /* Ensure stable LPCs */ - for( i = 0; i < MAX_LPC_STABILIZE_ITERATIONS; i++ ) { - if( SKP_Silk_LPC_inverse_pred_gain( &invGain_Q30, pAR_Q12, LPC_order ) == 1 ) { - SKP_Silk_bwexpander( pAR_Q12, LPC_order, 65536 - SKP_SMULBB( 66, i ) ); /* 66_Q16 = 0.001 */ - } else { - break; - } - } - - /* Reached the last iteration */ - if( i == MAX_LPC_STABILIZE_ITERATIONS ) { - for( i = 0; i < LPC_order; i++ ) { - pAR_Q12[ i ] = 0; - } - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Convert NLSF parameters to stable AR prediction filter coefficients */ +void SKP_Silk_NLSF2A_stable( + SKP_int16 pAR_Q12[ MAX_LPC_ORDER ], /* O Stabilized AR coefs [LPC_order] */ + const SKP_int pNLSF[ MAX_LPC_ORDER ], /* I NLSF vector [LPC_order] */ + const SKP_int LPC_order /* I LPC/LSF order */ +) +{ + SKP_int i; + SKP_int32 invGain_Q30; + + SKP_Silk_NLSF2A( pAR_Q12, pNLSF, LPC_order ); + + /* Ensure stable LPCs */ + for( i = 0; i < MAX_LPC_STABILIZE_ITERATIONS; i++ ) { + if( SKP_Silk_LPC_inverse_pred_gain( &invGain_Q30, pAR_Q12, LPC_order ) == 1 ) { + SKP_Silk_bwexpander( pAR_Q12, LPC_order, 65536 - SKP_SMULBB( 66, i ) ); /* 66_Q16 = 0.001 */ + } else { + break; + } + } + + /* Reached the last iteration */ + if( i == MAX_LPC_STABILIZE_ITERATIONS ) { + for( i = 0; i < LPC_order; i++ ) { + pAR_Q12[ i ] = 0; + } + } +} diff --git a/jni/silk/src/SKP_Silk_NLSF_MSVQ_decode.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF_MSVQ_decode.c similarity index 98% rename from jni/silk/src/SKP_Silk_NLSF_MSVQ_decode.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF_MSVQ_decode.c index d5be46e..0573d24 100644 --- a/jni/silk/src/SKP_Silk_NLSF_MSVQ_decode.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF_MSVQ_decode.c @@ -1,91 +1,91 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* NLSF vector decoder */ -void SKP_Silk_NLSF_MSVQ_decode( - SKP_int *pNLSF_Q15, /* O Pointer to decoded output vector [LPC_ORDER x 1] */ - const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Pointer to NLSF codebook struct */ - const SKP_int *NLSFIndices, /* I Pointer to NLSF indices [nStages x 1] */ - const SKP_int LPC_order /* I LPC order used */ -) -{ - const SKP_int16 *pCB_element; - SKP_int s; - SKP_int i; - - /* Check that each index is within valid range */ - SKP_assert( 0 <= NLSFIndices[ 0 ] && NLSFIndices[ 0 ] < psNLSF_CB->CBStages[ 0 ].nVectors ); - - /* Point to the first vector element */ - pCB_element = &psNLSF_CB->CBStages[ 0 ].CB_NLSF_Q15[ SKP_MUL( NLSFIndices[ 0 ], LPC_order ) ]; - - /* Initialize with the codebook vector from stage 0 */ - for( i = 0; i < LPC_order; i++ ) { - pNLSF_Q15[ i ] = ( SKP_int )pCB_element[ i ]; - } - - for( s = 1; s < psNLSF_CB->nStages; s++ ) { - /* Check that each index is within valid range */ - SKP_assert( 0 <= NLSFIndices[ s ] && NLSFIndices[ s ] < psNLSF_CB->CBStages[ s ].nVectors ); - - if( LPC_order == 16 ) { - /* Point to the first vector element */ - pCB_element = &psNLSF_CB->CBStages[ s ].CB_NLSF_Q15[ SKP_LSHIFT( NLSFIndices[ s ], 4 ) ]; - - /* Add the codebook vector from the current stage */ - pNLSF_Q15[ 0 ] += pCB_element[ 0 ]; - pNLSF_Q15[ 1 ] += pCB_element[ 1 ]; - pNLSF_Q15[ 2 ] += pCB_element[ 2 ]; - pNLSF_Q15[ 3 ] += pCB_element[ 3 ]; - pNLSF_Q15[ 4 ] += pCB_element[ 4 ]; - pNLSF_Q15[ 5 ] += pCB_element[ 5 ]; - pNLSF_Q15[ 6 ] += pCB_element[ 6 ]; - pNLSF_Q15[ 7 ] += pCB_element[ 7 ]; - pNLSF_Q15[ 8 ] += pCB_element[ 8 ]; - pNLSF_Q15[ 9 ] += pCB_element[ 9 ]; - pNLSF_Q15[ 10 ] += pCB_element[ 10 ]; - pNLSF_Q15[ 11 ] += pCB_element[ 11 ]; - pNLSF_Q15[ 12 ] += pCB_element[ 12 ]; - pNLSF_Q15[ 13 ] += pCB_element[ 13 ]; - pNLSF_Q15[ 14 ] += pCB_element[ 14 ]; - pNLSF_Q15[ 15 ] += pCB_element[ 15 ]; - } else { - /* Point to the first vector element */ - pCB_element = &psNLSF_CB->CBStages[ s ].CB_NLSF_Q15[ SKP_SMULBB( NLSFIndices[ s ], LPC_order ) ]; - - /* Add the codebook vector from the current stage */ - for( i = 0; i < LPC_order; i++ ) { - pNLSF_Q15[ i ] += pCB_element[ i ]; - } - } - } - - /* NLSF stabilization */ - SKP_Silk_NLSF_stabilize( pNLSF_Q15, psNLSF_CB->NDeltaMin_Q15, LPC_order ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* NLSF vector decoder */ +void SKP_Silk_NLSF_MSVQ_decode( + SKP_int *pNLSF_Q15, /* O Pointer to decoded output vector [LPC_ORDER x 1] */ + const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Pointer to NLSF codebook struct */ + const SKP_int *NLSFIndices, /* I Pointer to NLSF indices [nStages x 1] */ + const SKP_int LPC_order /* I LPC order used */ +) +{ + const SKP_int16 *pCB_element; + SKP_int s; + SKP_int i; + + /* Check that each index is within valid range */ + SKP_assert( 0 <= NLSFIndices[ 0 ] && NLSFIndices[ 0 ] < psNLSF_CB->CBStages[ 0 ].nVectors ); + + /* Point to the first vector element */ + pCB_element = &psNLSF_CB->CBStages[ 0 ].CB_NLSF_Q15[ SKP_MUL( NLSFIndices[ 0 ], LPC_order ) ]; + + /* Initialize with the codebook vector from stage 0 */ + for( i = 0; i < LPC_order; i++ ) { + pNLSF_Q15[ i ] = ( SKP_int )pCB_element[ i ]; + } + + for( s = 1; s < psNLSF_CB->nStages; s++ ) { + /* Check that each index is within valid range */ + SKP_assert( 0 <= NLSFIndices[ s ] && NLSFIndices[ s ] < psNLSF_CB->CBStages[ s ].nVectors ); + + if( LPC_order == 16 ) { + /* Point to the first vector element */ + pCB_element = &psNLSF_CB->CBStages[ s ].CB_NLSF_Q15[ SKP_LSHIFT( NLSFIndices[ s ], 4 ) ]; + + /* Add the codebook vector from the current stage */ + pNLSF_Q15[ 0 ] += pCB_element[ 0 ]; + pNLSF_Q15[ 1 ] += pCB_element[ 1 ]; + pNLSF_Q15[ 2 ] += pCB_element[ 2 ]; + pNLSF_Q15[ 3 ] += pCB_element[ 3 ]; + pNLSF_Q15[ 4 ] += pCB_element[ 4 ]; + pNLSF_Q15[ 5 ] += pCB_element[ 5 ]; + pNLSF_Q15[ 6 ] += pCB_element[ 6 ]; + pNLSF_Q15[ 7 ] += pCB_element[ 7 ]; + pNLSF_Q15[ 8 ] += pCB_element[ 8 ]; + pNLSF_Q15[ 9 ] += pCB_element[ 9 ]; + pNLSF_Q15[ 10 ] += pCB_element[ 10 ]; + pNLSF_Q15[ 11 ] += pCB_element[ 11 ]; + pNLSF_Q15[ 12 ] += pCB_element[ 12 ]; + pNLSF_Q15[ 13 ] += pCB_element[ 13 ]; + pNLSF_Q15[ 14 ] += pCB_element[ 14 ]; + pNLSF_Q15[ 15 ] += pCB_element[ 15 ]; + } else { + /* Point to the first vector element */ + pCB_element = &psNLSF_CB->CBStages[ s ].CB_NLSF_Q15[ SKP_SMULBB( NLSFIndices[ s ], LPC_order ) ]; + + /* Add the codebook vector from the current stage */ + for( i = 0; i < LPC_order; i++ ) { + pNLSF_Q15[ i ] += pCB_element[ i ]; + } + } + } + + /* NLSF stabilization */ + SKP_Silk_NLSF_stabilize( pNLSF_Q15, psNLSF_CB->NDeltaMin_Q15, LPC_order ); +} diff --git a/jni/silk/src/SKP_Silk_NLSF_MSVQ_encode_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF_MSVQ_encode_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_NLSF_MSVQ_encode_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF_MSVQ_encode_FIX.c index ba92556..5d48e62 100644 --- a/jni/silk/src/SKP_Silk_NLSF_MSVQ_encode_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF_MSVQ_encode_FIX.c @@ -1,231 +1,231 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/***********************/ -/* NLSF vector encoder */ -/***********************/ -void SKP_Silk_NLSF_MSVQ_encode_FIX( - SKP_int *NLSFIndices, /* O Codebook path vector [ CB_STAGES ] */ - SKP_int *pNLSF_Q15, /* I/O Quantized NLSF vector [ LPC_ORDER ] */ - const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Codebook object */ - const SKP_int *pNLSF_q_Q15_prev, /* I Prev. quantized NLSF vector [LPC_ORDER] */ - const SKP_int *pW_Q6, /* I NLSF weight vector [ LPC_ORDER ] */ - const SKP_int NLSF_mu_Q15, /* I Rate weight for the RD optimization */ - const SKP_int NLSF_mu_fluc_red_Q16, /* I Fluctuation reduction error weight */ - const SKP_int NLSF_MSVQ_Survivors, /* I Max survivors from each stage */ - const SKP_int LPC_order, /* I LPC order */ - const SKP_int deactivate_fluc_red /* I Deactivate fluctuation reduction */ -) -{ - SKP_int i, s, k, cur_survivors = 0, prev_survivors, input_index, cb_index, bestIndex; - SKP_int32 rateDistThreshold_Q18; - SKP_int pNLSF_in_Q15[ MAX_LPC_ORDER ]; -#if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 1 ) - SKP_int32 se_Q15, wsse_Q20, bestRateDist_Q20; -#endif - -#if( LOW_COMPLEXITY_ONLY == 1 ) - SKP_int32 pRateDist_Q18[ NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE ]; - SKP_int32 pRate_Q5[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ]; - SKP_int32 pRate_new_Q5[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ]; - SKP_int pTempIndices[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ]; - SKP_int pPath[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_CB_STAGES ]; - SKP_int pPath_new[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_CB_STAGES ]; - SKP_int pRes_Q15[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * MAX_LPC_ORDER ]; - SKP_int pRes_new_Q15[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * MAX_LPC_ORDER ]; -#else - SKP_int32 pRateDist_Q18[ NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED ]; - SKP_int32 pRate_Q5[ MAX_NLSF_MSVQ_SURVIVORS ]; - SKP_int32 pRate_new_Q5[ MAX_NLSF_MSVQ_SURVIVORS ]; - SKP_int pTempIndices[ MAX_NLSF_MSVQ_SURVIVORS ]; - SKP_int pPath[ MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_CB_STAGES ]; - SKP_int pPath_new[ MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_CB_STAGES ]; - SKP_int pRes_Q15[ MAX_NLSF_MSVQ_SURVIVORS * MAX_LPC_ORDER ]; - SKP_int pRes_new_Q15[ MAX_NLSF_MSVQ_SURVIVORS * MAX_LPC_ORDER ]; -#endif - - const SKP_int *pConstInt; - SKP_int *pInt; - const SKP_int16 *pCB_element; - const SKP_Silk_NLSF_CBS *pCurrentCBStage; - - SKP_assert( NLSF_MSVQ_Survivors <= MAX_NLSF_MSVQ_SURVIVORS ); - SKP_assert( ( LOW_COMPLEXITY_ONLY == 0 ) || ( NLSF_MSVQ_Survivors <= MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ) ); - - - - /* Copy the input vector */ - SKP_memcpy( pNLSF_in_Q15, pNLSF_Q15, LPC_order * sizeof( SKP_int ) ); - - /****************************************************/ - /* Tree search for the multi-stage vector quantizer */ - /****************************************************/ - - /* Clear accumulated rates */ - SKP_memset( pRate_Q5, 0, NLSF_MSVQ_Survivors * sizeof( SKP_int32 ) ); - - /* Copy NLSFs into residual signal vector */ - for( i = 0; i < LPC_order; i++ ) { - pRes_Q15[ i ] = pNLSF_Q15[ i ]; - } - - /* Set first stage values */ - prev_survivors = 1; - - /* Loop over all stages */ - for( s = 0; s < psNLSF_CB->nStages; s++ ) { - - /* Set a pointer to the current stage codebook */ - pCurrentCBStage = &psNLSF_CB->CBStages[ s ]; - - /* Calculate the number of survivors in the current stage */ - cur_survivors = SKP_min_32( NLSF_MSVQ_Survivors, SKP_SMULBB( prev_survivors, pCurrentCBStage->nVectors ) ); - -#if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 0 ) - /* Find a single best survivor in the last stage, if we */ - /* do not need candidates for fluctuation reduction */ - if( s == psNLSF_CB->nStages - 1 ) { - cur_survivors = 1; - } -#endif - - /* Nearest neighbor clustering for multiple input data vectors */ - SKP_Silk_NLSF_VQ_rate_distortion_FIX( pRateDist_Q18, pCurrentCBStage, pRes_Q15, pW_Q6, - pRate_Q5, NLSF_mu_Q15, prev_survivors, LPC_order ); - - /* Sort the rate-distortion errors */ - SKP_Silk_insertion_sort_increasing( pRateDist_Q18, pTempIndices, - prev_survivors * pCurrentCBStage->nVectors, cur_survivors ); - - /* Discard survivors with rate-distortion values too far above the best one */ - if( pRateDist_Q18[ 0 ] < SKP_int32_MAX / NLSF_MSVQ_SURV_MAX_REL_RD ) { - rateDistThreshold_Q18 = SKP_MUL( NLSF_MSVQ_SURV_MAX_REL_RD, pRateDist_Q18[ 0 ] ); - while( pRateDist_Q18[ cur_survivors - 1 ] > rateDistThreshold_Q18 && cur_survivors > 1 ) { - cur_survivors--; - } - } - /* Update accumulated codebook contributions for the 'cur_survivors' best codebook indices */ - for( k = 0; k < cur_survivors; k++ ) { - if( s > 0 ) { - /* Find the indices of the input and the codebook vector */ - if( pCurrentCBStage->nVectors == 8 ) { - input_index = SKP_RSHIFT( pTempIndices[ k ], 3 ); - cb_index = pTempIndices[ k ] & 7; - } else { - input_index = SKP_DIV32_16( pTempIndices[ k ], pCurrentCBStage->nVectors ); - cb_index = pTempIndices[ k ] - SKP_SMULBB( input_index, pCurrentCBStage->nVectors ); - } - } else { - /* Find the indices of the input and the codebook vector */ - input_index = 0; - cb_index = pTempIndices[ k ]; - } - - /* Subtract new contribution from the previous residual vector for each of 'cur_survivors' */ - pConstInt = &pRes_Q15[ SKP_SMULBB( input_index, LPC_order ) ]; - pCB_element = &pCurrentCBStage->CB_NLSF_Q15[ SKP_SMULBB( cb_index, LPC_order ) ]; - pInt = &pRes_new_Q15[ SKP_SMULBB( k, LPC_order ) ]; - for( i = 0; i < LPC_order; i++ ) { - pInt[ i ] = pConstInt[ i ] - ( SKP_int )pCB_element[ i ]; - } - - /* Update accumulated rate for stage 1 to the current */ - pRate_new_Q5[ k ] = pRate_Q5[ input_index ] + pCurrentCBStage->Rates_Q5[ cb_index ]; - - /* Copy paths from previous matrix, starting with the best path */ - pConstInt = &pPath[ SKP_SMULBB( input_index, psNLSF_CB->nStages ) ]; - pInt = &pPath_new[ SKP_SMULBB( k, psNLSF_CB->nStages ) ]; - for( i = 0; i < s; i++ ) { - pInt[ i ] = pConstInt[ i ]; - } - /* Write the current stage indices for the 'cur_survivors' to the best path matrix */ - pInt[ s ] = cb_index; - } - - if( s < psNLSF_CB->nStages - 1 ) { - /* Copy NLSF residual matrix for next stage */ - SKP_memcpy( pRes_Q15, pRes_new_Q15, SKP_SMULBB( cur_survivors, LPC_order ) * sizeof( SKP_int ) ); - - /* Copy rate vector for next stage */ - SKP_memcpy( pRate_Q5, pRate_new_Q5, cur_survivors * sizeof( SKP_int32 ) ); - - /* Copy best path matrix for next stage */ - SKP_memcpy( pPath, pPath_new, SKP_SMULBB( cur_survivors, psNLSF_CB->nStages ) * sizeof( SKP_int ) ); - } - - prev_survivors = cur_survivors; - } - - /* (Preliminary) index of the best survivor, later to be decoded */ - bestIndex = 0; - -#if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 1 ) - /******************************/ - /* NLSF fluctuation reduction */ - /******************************/ - if( deactivate_fluc_red != 1 ) { - - /* Search among all survivors, now taking also weighted fluctuation errors into account */ - bestRateDist_Q20 = SKP_int32_MAX; - for( s = 0; s < cur_survivors; s++ ) { - /* Decode survivor to compare with previous quantized NLSF vector */ - SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, &pPath_new[ SKP_SMULBB( s, psNLSF_CB->nStages ) ], LPC_order ); - - /* Compare decoded NLSF vector with the previously quantized vector */ - wsse_Q20 = 0; - for( i = 0; i < LPC_order; i += 2 ) { - /* Compute weighted squared quantization error for index i */ - se_Q15 = pNLSF_Q15[ i ] - pNLSF_q_Q15_prev[ i ]; // range: [ -32767 : 32767 ] - wsse_Q20 = SKP_SMLAWB( wsse_Q20, SKP_SMULBB( se_Q15, se_Q15 ), pW_Q6[ i ] ); - - /* Compute weighted squared quantization error for index i + 1 */ - se_Q15 = pNLSF_Q15[ i + 1 ] - pNLSF_q_Q15_prev[ i + 1 ]; // range: [ -32767 : 32767 ] - wsse_Q20 = SKP_SMLAWB( wsse_Q20, SKP_SMULBB( se_Q15, se_Q15 ), pW_Q6[ i + 1 ] ); - } - SKP_assert( wsse_Q20 >= 0 ); - - /* Add the fluctuation reduction penalty to the rate distortion error */ - wsse_Q20 = SKP_ADD_POS_SAT32( pRateDist_Q18[ s ], SKP_SMULWB( wsse_Q20, NLSF_mu_fluc_red_Q16 ) ); - - /* Keep index of best survivor */ - if( wsse_Q20 < bestRateDist_Q20 ) { - bestRateDist_Q20 = wsse_Q20; - bestIndex = s; - } - } - } -#endif - - /* Copy best path to output argument */ - SKP_memcpy( NLSFIndices, &pPath_new[ SKP_SMULBB( bestIndex, psNLSF_CB->nStages ) ], psNLSF_CB->nStages * sizeof( SKP_int ) ); - - /* Decode and stabilize the best survivor */ - SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, NLSFIndices, LPC_order ); - -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/***********************/ +/* NLSF vector encoder */ +/***********************/ +void SKP_Silk_NLSF_MSVQ_encode_FIX( + SKP_int *NLSFIndices, /* O Codebook path vector [ CB_STAGES ] */ + SKP_int *pNLSF_Q15, /* I/O Quantized NLSF vector [ LPC_ORDER ] */ + const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Codebook object */ + const SKP_int *pNLSF_q_Q15_prev, /* I Prev. quantized NLSF vector [LPC_ORDER] */ + const SKP_int *pW_Q6, /* I NLSF weight vector [ LPC_ORDER ] */ + const SKP_int NLSF_mu_Q15, /* I Rate weight for the RD optimization */ + const SKP_int NLSF_mu_fluc_red_Q16, /* I Fluctuation reduction error weight */ + const SKP_int NLSF_MSVQ_Survivors, /* I Max survivors from each stage */ + const SKP_int LPC_order, /* I LPC order */ + const SKP_int deactivate_fluc_red /* I Deactivate fluctuation reduction */ +) +{ + SKP_int i, s, k, cur_survivors = 0, prev_survivors, input_index, cb_index, bestIndex; + SKP_int32 rateDistThreshold_Q18; + SKP_int pNLSF_in_Q15[ MAX_LPC_ORDER ]; +#if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 1 ) + SKP_int32 se_Q15, wsse_Q20, bestRateDist_Q20; +#endif + +#if( LOW_COMPLEXITY_ONLY == 1 ) + SKP_int32 pRateDist_Q18[ NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE ]; + SKP_int32 pRate_Q5[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ]; + SKP_int32 pRate_new_Q5[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ]; + SKP_int pTempIndices[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ]; + SKP_int pPath[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_CB_STAGES ]; + SKP_int pPath_new[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_CB_STAGES ]; + SKP_int pRes_Q15[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * MAX_LPC_ORDER ]; + SKP_int pRes_new_Q15[ MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * MAX_LPC_ORDER ]; +#else + SKP_int32 pRateDist_Q18[ NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED ]; + SKP_int32 pRate_Q5[ MAX_NLSF_MSVQ_SURVIVORS ]; + SKP_int32 pRate_new_Q5[ MAX_NLSF_MSVQ_SURVIVORS ]; + SKP_int pTempIndices[ MAX_NLSF_MSVQ_SURVIVORS ]; + SKP_int pPath[ MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_CB_STAGES ]; + SKP_int pPath_new[ MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_CB_STAGES ]; + SKP_int pRes_Q15[ MAX_NLSF_MSVQ_SURVIVORS * MAX_LPC_ORDER ]; + SKP_int pRes_new_Q15[ MAX_NLSF_MSVQ_SURVIVORS * MAX_LPC_ORDER ]; +#endif + + const SKP_int *pConstInt; + SKP_int *pInt; + const SKP_int16 *pCB_element; + const SKP_Silk_NLSF_CBS *pCurrentCBStage; + + SKP_assert( NLSF_MSVQ_Survivors <= MAX_NLSF_MSVQ_SURVIVORS ); + SKP_assert( ( LOW_COMPLEXITY_ONLY == 0 ) || ( NLSF_MSVQ_Survivors <= MAX_NLSF_MSVQ_SURVIVORS_LC_MODE ) ); + + + + /* Copy the input vector */ + SKP_memcpy( pNLSF_in_Q15, pNLSF_Q15, LPC_order * sizeof( SKP_int ) ); + + /****************************************************/ + /* Tree search for the multi-stage vector quantizer */ + /****************************************************/ + + /* Clear accumulated rates */ + SKP_memset( pRate_Q5, 0, NLSF_MSVQ_Survivors * sizeof( SKP_int32 ) ); + + /* Copy NLSFs into residual signal vector */ + for( i = 0; i < LPC_order; i++ ) { + pRes_Q15[ i ] = pNLSF_Q15[ i ]; + } + + /* Set first stage values */ + prev_survivors = 1; + + /* Loop over all stages */ + for( s = 0; s < psNLSF_CB->nStages; s++ ) { + + /* Set a pointer to the current stage codebook */ + pCurrentCBStage = &psNLSF_CB->CBStages[ s ]; + + /* Calculate the number of survivors in the current stage */ + cur_survivors = SKP_min_32( NLSF_MSVQ_Survivors, SKP_SMULBB( prev_survivors, pCurrentCBStage->nVectors ) ); + +#if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 0 ) + /* Find a single best survivor in the last stage, if we */ + /* do not need candidates for fluctuation reduction */ + if( s == psNLSF_CB->nStages - 1 ) { + cur_survivors = 1; + } +#endif + + /* Nearest neighbor clustering for multiple input data vectors */ + SKP_Silk_NLSF_VQ_rate_distortion_FIX( pRateDist_Q18, pCurrentCBStage, pRes_Q15, pW_Q6, + pRate_Q5, NLSF_mu_Q15, prev_survivors, LPC_order ); + + /* Sort the rate-distortion errors */ + SKP_Silk_insertion_sort_increasing( pRateDist_Q18, pTempIndices, + prev_survivors * pCurrentCBStage->nVectors, cur_survivors ); + + /* Discard survivors with rate-distortion values too far above the best one */ + if( pRateDist_Q18[ 0 ] < SKP_int32_MAX / NLSF_MSVQ_SURV_MAX_REL_RD ) { + rateDistThreshold_Q18 = SKP_MUL( NLSF_MSVQ_SURV_MAX_REL_RD, pRateDist_Q18[ 0 ] ); + while( pRateDist_Q18[ cur_survivors - 1 ] > rateDistThreshold_Q18 && cur_survivors > 1 ) { + cur_survivors--; + } + } + /* Update accumulated codebook contributions for the 'cur_survivors' best codebook indices */ + for( k = 0; k < cur_survivors; k++ ) { + if( s > 0 ) { + /* Find the indices of the input and the codebook vector */ + if( pCurrentCBStage->nVectors == 8 ) { + input_index = SKP_RSHIFT( pTempIndices[ k ], 3 ); + cb_index = pTempIndices[ k ] & 7; + } else { + input_index = SKP_DIV32_16( pTempIndices[ k ], pCurrentCBStage->nVectors ); + cb_index = pTempIndices[ k ] - SKP_SMULBB( input_index, pCurrentCBStage->nVectors ); + } + } else { + /* Find the indices of the input and the codebook vector */ + input_index = 0; + cb_index = pTempIndices[ k ]; + } + + /* Subtract new contribution from the previous residual vector for each of 'cur_survivors' */ + pConstInt = &pRes_Q15[ SKP_SMULBB( input_index, LPC_order ) ]; + pCB_element = &pCurrentCBStage->CB_NLSF_Q15[ SKP_SMULBB( cb_index, LPC_order ) ]; + pInt = &pRes_new_Q15[ SKP_SMULBB( k, LPC_order ) ]; + for( i = 0; i < LPC_order; i++ ) { + pInt[ i ] = pConstInt[ i ] - ( SKP_int )pCB_element[ i ]; + } + + /* Update accumulated rate for stage 1 to the current */ + pRate_new_Q5[ k ] = pRate_Q5[ input_index ] + pCurrentCBStage->Rates_Q5[ cb_index ]; + + /* Copy paths from previous matrix, starting with the best path */ + pConstInt = &pPath[ SKP_SMULBB( input_index, psNLSF_CB->nStages ) ]; + pInt = &pPath_new[ SKP_SMULBB( k, psNLSF_CB->nStages ) ]; + for( i = 0; i < s; i++ ) { + pInt[ i ] = pConstInt[ i ]; + } + /* Write the current stage indices for the 'cur_survivors' to the best path matrix */ + pInt[ s ] = cb_index; + } + + if( s < psNLSF_CB->nStages - 1 ) { + /* Copy NLSF residual matrix for next stage */ + SKP_memcpy( pRes_Q15, pRes_new_Q15, SKP_SMULBB( cur_survivors, LPC_order ) * sizeof( SKP_int ) ); + + /* Copy rate vector for next stage */ + SKP_memcpy( pRate_Q5, pRate_new_Q5, cur_survivors * sizeof( SKP_int32 ) ); + + /* Copy best path matrix for next stage */ + SKP_memcpy( pPath, pPath_new, SKP_SMULBB( cur_survivors, psNLSF_CB->nStages ) * sizeof( SKP_int ) ); + } + + prev_survivors = cur_survivors; + } + + /* (Preliminary) index of the best survivor, later to be decoded */ + bestIndex = 0; + +#if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 1 ) + /******************************/ + /* NLSF fluctuation reduction */ + /******************************/ + if( deactivate_fluc_red != 1 ) { + + /* Search among all survivors, now taking also weighted fluctuation errors into account */ + bestRateDist_Q20 = SKP_int32_MAX; + for( s = 0; s < cur_survivors; s++ ) { + /* Decode survivor to compare with previous quantized NLSF vector */ + SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, &pPath_new[ SKP_SMULBB( s, psNLSF_CB->nStages ) ], LPC_order ); + + /* Compare decoded NLSF vector with the previously quantized vector */ + wsse_Q20 = 0; + for( i = 0; i < LPC_order; i += 2 ) { + /* Compute weighted squared quantization error for index i */ + se_Q15 = pNLSF_Q15[ i ] - pNLSF_q_Q15_prev[ i ]; // range: [ -32767 : 32767 ] + wsse_Q20 = SKP_SMLAWB( wsse_Q20, SKP_SMULBB( se_Q15, se_Q15 ), pW_Q6[ i ] ); + + /* Compute weighted squared quantization error for index i + 1 */ + se_Q15 = pNLSF_Q15[ i + 1 ] - pNLSF_q_Q15_prev[ i + 1 ]; // range: [ -32767 : 32767 ] + wsse_Q20 = SKP_SMLAWB( wsse_Q20, SKP_SMULBB( se_Q15, se_Q15 ), pW_Q6[ i + 1 ] ); + } + SKP_assert( wsse_Q20 >= 0 ); + + /* Add the fluctuation reduction penalty to the rate distortion error */ + wsse_Q20 = SKP_ADD_POS_SAT32( pRateDist_Q18[ s ], SKP_SMULWB( wsse_Q20, NLSF_mu_fluc_red_Q16 ) ); + + /* Keep index of best survivor */ + if( wsse_Q20 < bestRateDist_Q20 ) { + bestRateDist_Q20 = wsse_Q20; + bestIndex = s; + } + } + } +#endif + + /* Copy best path to output argument */ + SKP_memcpy( NLSFIndices, &pPath_new[ SKP_SMULBB( bestIndex, psNLSF_CB->nStages ) ], psNLSF_CB->nStages * sizeof( SKP_int ) ); + + /* Decode and stabilize the best survivor */ + SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, NLSFIndices, LPC_order ); + +} diff --git a/jni/silk/src/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c index 09e2bc7..d18bb06 100644 --- a/jni/silk/src/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c @@ -1,61 +1,61 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Rate-Distortion calculations for multiple input data vectors */ -void SKP_Silk_NLSF_VQ_rate_distortion_FIX( - SKP_int32 *pRD_Q20, /* O Rate-distortion values [psNLSF_CBS->nVectors*N] */ - const SKP_Silk_NLSF_CBS *psNLSF_CBS, /* I NLSF codebook stage struct */ - const SKP_int *in_Q15, /* I Input vectors to be quantized */ - const SKP_int *w_Q6, /* I Weight vector */ - const SKP_int32 *rate_acc_Q5, /* I Accumulated rates from previous stage */ - const SKP_int mu_Q15, /* I Weight between weighted error and rate */ - const SKP_int N, /* I Number of input vectors to be quantized */ - const SKP_int LPC_order /* I LPC order */ -) -{ - SKP_int i, n; - SKP_int32 *pRD_vec_Q20; - - /* Compute weighted quantization errors for all input vectors over one codebook stage */ - SKP_Silk_NLSF_VQ_sum_error_FIX( pRD_Q20, in_Q15, w_Q6, psNLSF_CBS->CB_NLSF_Q15, - N, psNLSF_CBS->nVectors, LPC_order ); - - /* Loop over input vectors */ - pRD_vec_Q20 = pRD_Q20; - for( n = 0; n < N; n++ ) { - /* Add rate cost to error for each codebook vector */ - for( i = 0; i < psNLSF_CBS->nVectors; i++ ) { - SKP_assert( rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ] >= 0 ); - SKP_assert( rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ] <= SKP_int16_MAX ); - pRD_vec_Q20[ i ] = SKP_SMLABB( pRD_vec_Q20[ i ], rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ], mu_Q15 ); - SKP_assert( pRD_vec_Q20[ i ] >= 0 ); - } - pRD_vec_Q20 += psNLSF_CBS->nVectors; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Rate-Distortion calculations for multiple input data vectors */ +void SKP_Silk_NLSF_VQ_rate_distortion_FIX( + SKP_int32 *pRD_Q20, /* O Rate-distortion values [psNLSF_CBS->nVectors*N] */ + const SKP_Silk_NLSF_CBS *psNLSF_CBS, /* I NLSF codebook stage struct */ + const SKP_int *in_Q15, /* I Input vectors to be quantized */ + const SKP_int *w_Q6, /* I Weight vector */ + const SKP_int32 *rate_acc_Q5, /* I Accumulated rates from previous stage */ + const SKP_int mu_Q15, /* I Weight between weighted error and rate */ + const SKP_int N, /* I Number of input vectors to be quantized */ + const SKP_int LPC_order /* I LPC order */ +) +{ + SKP_int i, n; + SKP_int32 *pRD_vec_Q20; + + /* Compute weighted quantization errors for all input vectors over one codebook stage */ + SKP_Silk_NLSF_VQ_sum_error_FIX( pRD_Q20, in_Q15, w_Q6, psNLSF_CBS->CB_NLSF_Q15, + N, psNLSF_CBS->nVectors, LPC_order ); + + /* Loop over input vectors */ + pRD_vec_Q20 = pRD_Q20; + for( n = 0; n < N; n++ ) { + /* Add rate cost to error for each codebook vector */ + for( i = 0; i < psNLSF_CBS->nVectors; i++ ) { + SKP_assert( rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ] >= 0 ); + SKP_assert( rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ] <= SKP_int16_MAX ); + pRD_vec_Q20[ i ] = SKP_SMLABB( pRD_vec_Q20[ i ], rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ], mu_Q15 ); + SKP_assert( pRD_vec_Q20[ i ] >= 0 ); + } + pRD_vec_Q20 += psNLSF_CBS->nVectors; + } +} diff --git a/jni/silk/src/SKP_Silk_NLSF_VQ_sum_error_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_sum_error_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_NLSF_VQ_sum_error_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_sum_error_FIX.c index 7a2171c..9e583f7 100644 --- a/jni/silk/src/SKP_Silk_NLSF_VQ_sum_error_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_sum_error_FIX.c @@ -1,79 +1,79 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Compute weighted quantization errors for an LPC_order element input vector, over one codebook stage */ -void SKP_Silk_NLSF_VQ_sum_error_FIX( - SKP_int32 *err_Q20, /* O Weighted quantization errors [N*K] */ - const SKP_int *in_Q15, /* I Input vectors to be quantized [N*LPC_order] */ - const SKP_int *w_Q6, /* I Weighting vectors [N*LPC_order] */ - const SKP_int16 *pCB_Q15, /* I Codebook vectors [K*LPC_order] */ - const SKP_int N, /* I Number of input vectors */ - const SKP_int K, /* I Number of codebook vectors */ - const SKP_int LPC_order /* I Number of LPCs */ -) -{ - SKP_int i, n, m; - SKP_int32 diff_Q15, sum_error, Wtmp_Q6; - SKP_int32 Wcpy_Q6[ MAX_LPC_ORDER / 2 ]; - const SKP_int16 *cb_vec_Q15; - - SKP_assert( LPC_order <= 16 ); - SKP_assert( ( LPC_order & 1 ) == 0 ); - - /* Copy to local stack and pack two weights per int32 */ - for( m = 0; m < SKP_RSHIFT( LPC_order, 1 ); m++ ) { - Wcpy_Q6[ m ] = w_Q6[ 2 * m ] | SKP_LSHIFT( ( SKP_int32 )w_Q6[ 2 * m + 1 ], 16 ); - } - - /* Loop over input vectors */ - for( n = 0; n < N; n++ ) { - /* Loop over codebook */ - cb_vec_Q15 = pCB_Q15; - for( i = 0; i < K; i++ ) { - sum_error = 0; - for( m = 0; m < LPC_order; m += 2 ) { - /* Get two weights packed in an int32 */ - Wtmp_Q6 = Wcpy_Q6[ SKP_RSHIFT( m, 1 ) ]; - - /* Compute weighted squared quantization error for index m */ - diff_Q15 = in_Q15[ m ] - *cb_vec_Q15++; // range: [ -32767 : 32767 ] - sum_error = SKP_SMLAWB( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 ); - - /* Compute weighted squared quantization error for index m + 1 */ - diff_Q15 = in_Q15[m + 1] - *cb_vec_Q15++; // range: [ -32767 : 32767 ] - sum_error = SKP_SMLAWT( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 ); - } - SKP_assert( sum_error >= 0 ); - err_Q20[ i ] = sum_error; - } - err_Q20 += K; - in_Q15 += LPC_order; - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Compute weighted quantization errors for an LPC_order element input vector, over one codebook stage */ +void SKP_Silk_NLSF_VQ_sum_error_FIX( + SKP_int32 *err_Q20, /* O Weighted quantization errors [N*K] */ + const SKP_int *in_Q15, /* I Input vectors to be quantized [N*LPC_order] */ + const SKP_int *w_Q6, /* I Weighting vectors [N*LPC_order] */ + const SKP_int16 *pCB_Q15, /* I Codebook vectors [K*LPC_order] */ + const SKP_int N, /* I Number of input vectors */ + const SKP_int K, /* I Number of codebook vectors */ + const SKP_int LPC_order /* I Number of LPCs */ +) +{ + SKP_int i, n, m; + SKP_int32 diff_Q15, sum_error, Wtmp_Q6; + SKP_int32 Wcpy_Q6[ MAX_LPC_ORDER / 2 ]; + const SKP_int16 *cb_vec_Q15; + + SKP_assert( LPC_order <= 16 ); + SKP_assert( ( LPC_order & 1 ) == 0 ); + + /* Copy to local stack and pack two weights per int32 */ + for( m = 0; m < SKP_RSHIFT( LPC_order, 1 ); m++ ) { + Wcpy_Q6[ m ] = w_Q6[ 2 * m ] | SKP_LSHIFT( ( SKP_int32 )w_Q6[ 2 * m + 1 ], 16 ); + } + + /* Loop over input vectors */ + for( n = 0; n < N; n++ ) { + /* Loop over codebook */ + cb_vec_Q15 = pCB_Q15; + for( i = 0; i < K; i++ ) { + sum_error = 0; + for( m = 0; m < LPC_order; m += 2 ) { + /* Get two weights packed in an int32 */ + Wtmp_Q6 = Wcpy_Q6[ SKP_RSHIFT( m, 1 ) ]; + + /* Compute weighted squared quantization error for index m */ + diff_Q15 = in_Q15[ m ] - *cb_vec_Q15++; // range: [ -32767 : 32767 ] + sum_error = SKP_SMLAWB( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 ); + + /* Compute weighted squared quantization error for index m + 1 */ + diff_Q15 = in_Q15[m + 1] - *cb_vec_Q15++; // range: [ -32767 : 32767 ] + sum_error = SKP_SMLAWT( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 ); + } + SKP_assert( sum_error >= 0 ); + err_Q20[ i ] = sum_error; + } + err_Q20 += K; + in_Q15 += LPC_order; + } +} + diff --git a/jni/silk/src/SKP_Silk_NLSF_VQ_weights_laroia.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_weights_laroia.c similarity index 98% rename from jni/silk/src/SKP_Silk_NLSF_VQ_weights_laroia.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_weights_laroia.c index e7f4618..5590869 100644 --- a/jni/silk/src/SKP_Silk_NLSF_VQ_weights_laroia.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF_VQ_weights_laroia.c @@ -1,78 +1,78 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -/* -R. Laroia, N. Phamdo and N. Farvardin, "Robust and Efficient Quantization of Speech LSP -Parameters Using Structured Vector Quantization", Proc. IEEE Int. Conf. Acoust., Speech, -Signal Processing, pp. 641-644, 1991. -*/ - -#define Q_OUT 6 - -/* Laroia low complexity NLSF weights */ -void SKP_Silk_NLSF_VQ_weights_laroia( - SKP_int *pNLSFW_Q6, /* O: Pointer to input vector weights [D x 1] */ - const SKP_int *pNLSF_Q15, /* I: Pointer to input vector [D x 1] */ - const SKP_int D /* I: Input vector dimension (even) */ -) -{ - SKP_int k; - SKP_int32 tmp1_int, tmp2_int; - - /* Check that we are guaranteed to end up within the required range */ - SKP_assert( D > 0 ); - SKP_assert( ( D & 1 ) == 0 ); - - /* First value */ - tmp1_int = SKP_max_int( pNLSF_Q15[ 0 ], 1 ); - tmp1_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp1_int ); - tmp2_int = SKP_max_int( pNLSF_Q15[ 1 ] - pNLSF_Q15[ 0 ], 1 ); - tmp2_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp2_int ); - pNLSFW_Q6[ 0 ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); - SKP_assert( pNLSFW_Q6[ 0 ] > 0 ); - - /* Main loop */ - for( k = 1; k < D - 1; k += 2 ) { - tmp1_int = SKP_max_int( pNLSF_Q15[ k + 1 ] - pNLSF_Q15[ k ], 1 ); - tmp1_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp1_int ); - pNLSFW_Q6[ k ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); - SKP_assert( pNLSFW_Q6[ k ] > 0 ); - - tmp2_int = SKP_max_int( pNLSF_Q15[ k + 2 ] - pNLSF_Q15[ k + 1 ], 1 ); - tmp2_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp2_int ); - pNLSFW_Q6[ k + 1 ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); - SKP_assert( pNLSFW_Q6[ k + 1 ] > 0 ); - } - - /* Last value */ - tmp1_int = SKP_max_int( ( 1 << 15 ) - pNLSF_Q15[ D - 1 ], 1 ); - tmp1_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp1_int ); - pNLSFW_Q6[ D - 1 ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); - SKP_assert( pNLSFW_Q6[ D - 1 ] > 0 ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +/* +R. Laroia, N. Phamdo and N. Farvardin, "Robust and Efficient Quantization of Speech LSP +Parameters Using Structured Vector Quantization", Proc. IEEE Int. Conf. Acoust., Speech, +Signal Processing, pp. 641-644, 1991. +*/ + +#define Q_OUT 6 + +/* Laroia low complexity NLSF weights */ +void SKP_Silk_NLSF_VQ_weights_laroia( + SKP_int *pNLSFW_Q6, /* O: Pointer to input vector weights [D x 1] */ + const SKP_int *pNLSF_Q15, /* I: Pointer to input vector [D x 1] */ + const SKP_int D /* I: Input vector dimension (even) */ +) +{ + SKP_int k; + SKP_int32 tmp1_int, tmp2_int; + + /* Check that we are guaranteed to end up within the required range */ + SKP_assert( D > 0 ); + SKP_assert( ( D & 1 ) == 0 ); + + /* First value */ + tmp1_int = SKP_max_int( pNLSF_Q15[ 0 ], 1 ); + tmp1_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp1_int ); + tmp2_int = SKP_max_int( pNLSF_Q15[ 1 ] - pNLSF_Q15[ 0 ], 1 ); + tmp2_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp2_int ); + pNLSFW_Q6[ 0 ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); + SKP_assert( pNLSFW_Q6[ 0 ] > 0 ); + + /* Main loop */ + for( k = 1; k < D - 1; k += 2 ) { + tmp1_int = SKP_max_int( pNLSF_Q15[ k + 1 ] - pNLSF_Q15[ k ], 1 ); + tmp1_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp1_int ); + pNLSFW_Q6[ k ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); + SKP_assert( pNLSFW_Q6[ k ] > 0 ); + + tmp2_int = SKP_max_int( pNLSF_Q15[ k + 2 ] - pNLSF_Q15[ k + 1 ], 1 ); + tmp2_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp2_int ); + pNLSFW_Q6[ k + 1 ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); + SKP_assert( pNLSFW_Q6[ k + 1 ] > 0 ); + } + + /* Last value */ + tmp1_int = SKP_max_int( ( 1 << 15 ) - pNLSF_Q15[ D - 1 ], 1 ); + tmp1_int = SKP_DIV32_16( 1 << ( 15 + Q_OUT ), tmp1_int ); + pNLSFW_Q6[ D - 1 ] = (SKP_int)SKP_min_int( tmp1_int + tmp2_int, SKP_int16_MAX ); + SKP_assert( pNLSFW_Q6[ D - 1 ] > 0 ); +} diff --git a/jni/silk/src/SKP_Silk_NLSF_stabilize.c b/app/src/main/jni/silk/src/SKP_Silk_NLSF_stabilize.c similarity index 97% rename from jni/silk/src/SKP_Silk_NLSF_stabilize.c rename to app/src/main/jni/silk/src/SKP_Silk_NLSF_stabilize.c index 7932586..f651c3c 100644 --- a/jni/silk/src/SKP_Silk_NLSF_stabilize.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NLSF_stabilize.c @@ -1,154 +1,154 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* NLSF stabilizer: */ -/* */ -/* - Moves NLSFs futher apart if they are too close */ -/* - Moves NLSFs away from borders if they are too close */ -/* - High effort to achieve a modification with minimum */ -/* Euclidean distance to input vector */ -/* - Output are sorted NLSF coefficients */ -/* */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Constant Definitions */ -#define MAX_LOOPS 20 - -/* NLSF stabilizer, for a single input data vector */ -void SKP_Silk_NLSF_stabilize( - SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vector in Q15 [L] */ - const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ - const SKP_int L /* I: Number of NLSF parameters in the input vector */ -) -{ - SKP_int center_freq_Q15, diff_Q15, min_center_Q15, max_center_Q15; - SKP_int32 min_diff_Q15; - SKP_int loops; - SKP_int i, I=0, k; - - /* This is necessary to ensure an output within range of a SKP_int16 */ - SKP_assert( NDeltaMin_Q15[L] >= 1 ); - - for( loops = 0; loops < MAX_LOOPS; loops++ ) { - /**************************/ - /* Find smallest distance */ - /**************************/ - /* First element */ - min_diff_Q15 = NLSF_Q15[0] - NDeltaMin_Q15[0]; - I = 0; - /* Middle elements */ - for( i = 1; i <= L-1; i++ ) { - diff_Q15 = NLSF_Q15[i] - ( NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); - if( diff_Q15 < min_diff_Q15 ) { - min_diff_Q15 = diff_Q15; - I = i; - } - } - /* Last element */ - diff_Q15 = (1<<15) - ( NLSF_Q15[L-1] + NDeltaMin_Q15[L] ); - if( diff_Q15 < min_diff_Q15 ) { - min_diff_Q15 = diff_Q15; - I = L; - } - - /***************************************************/ - /* Now check if the smallest distance non-negative */ - /***************************************************/ - if (min_diff_Q15 >= 0) { - return; - } - - if( I == 0 ) { - /* Move away from lower limit */ - NLSF_Q15[0] = NDeltaMin_Q15[0]; - - } else if( I == L) { - /* Move away from higher limit */ - NLSF_Q15[L-1] = (1<<15) - NDeltaMin_Q15[L]; - - } else { - /* Find the lower extreme for the location of the current center frequency */ - min_center_Q15 = 0; - for( k = 0; k < I; k++ ) { - min_center_Q15 += NDeltaMin_Q15[k]; - } - min_center_Q15 += SKP_RSHIFT( NDeltaMin_Q15[I], 1 ); - - /* Find the upper extreme for the location of the current center frequency */ - max_center_Q15 = (1<<15); - for( k = L; k > I; k-- ) { - max_center_Q15 -= NDeltaMin_Q15[k]; - } - max_center_Q15 -= ( NDeltaMin_Q15[I] - SKP_RSHIFT( NDeltaMin_Q15[I], 1 ) ); - - /* Move apart, sorted by value, keeping the same center frequency */ - center_freq_Q15 = SKP_LIMIT( SKP_RSHIFT_ROUND( (SKP_int32)NLSF_Q15[I-1] + (SKP_int32)NLSF_Q15[I], 1 ), - min_center_Q15, max_center_Q15 ); - NLSF_Q15[I-1] = center_freq_Q15 - SKP_RSHIFT( NDeltaMin_Q15[I], 1 ); - NLSF_Q15[I] = NLSF_Q15[I-1] + NDeltaMin_Q15[I]; - } - } - - /* Safe and simple fall back method, which is less ideal than the above */ - if( loops == MAX_LOOPS ) - { - /* Insertion sort (fast for already almost sorted arrays): */ - /* Best case: O(n) for an already sorted array */ - /* Worst case: O(n^2) for an inversely sorted array */ - SKP_Silk_insertion_sort_increasing_all_values(&NLSF_Q15[0], L); - - /* First NLSF should be no less than NDeltaMin[0] */ - NLSF_Q15[0] = SKP_max_int( NLSF_Q15[0], NDeltaMin_Q15[0] ); - - /* Keep delta_min distance between the NLSFs */ - for( i = 1; i < L; i++ ) - NLSF_Q15[i] = SKP_max_int( NLSF_Q15[i], NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); - - /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ - NLSF_Q15[L-1] = SKP_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] ); - - /* Keep NDeltaMin distance between the NLSFs */ - for( i = L-2; i >= 0; i-- ) - NLSF_Q15[i] = SKP_min_int( NLSF_Q15[i], NLSF_Q15[i+1] - NDeltaMin_Q15[i+1] ); - } -} - -/* NLSF stabilizer, over multiple input column data vectors */ -void SKP_Silk_NLSF_stabilize_multi( - SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vectors in Q15 [LxN] */ - const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ - const SKP_int N, /* I: Number of input vectors to be stabilized */ - const SKP_int L /* I: NLSF vector dimension */ -) -{ - SKP_int n; - - /* loop over input data */ - for( n = 0; n < N; n++ ) { - SKP_Silk_NLSF_stabilize( &NLSF_Q15[n * L], NDeltaMin_Q15, L ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* NLSF stabilizer: */ +/* */ +/* - Moves NLSFs futher apart if they are too close */ +/* - Moves NLSFs away from borders if they are too close */ +/* - High effort to achieve a modification with minimum */ +/* Euclidean distance to input vector */ +/* - Output are sorted NLSF coefficients */ +/* */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Constant Definitions */ +#define MAX_LOOPS 20 + +/* NLSF stabilizer, for a single input data vector */ +void SKP_Silk_NLSF_stabilize( + SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vector in Q15 [L] */ + const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ + const SKP_int L /* I: Number of NLSF parameters in the input vector */ +) +{ + SKP_int center_freq_Q15, diff_Q15, min_center_Q15, max_center_Q15; + SKP_int32 min_diff_Q15; + SKP_int loops; + SKP_int i, I=0, k; + + /* This is necessary to ensure an output within range of a SKP_int16 */ + SKP_assert( NDeltaMin_Q15[L] >= 1 ); + + for( loops = 0; loops < MAX_LOOPS; loops++ ) { + /**************************/ + /* Find smallest distance */ + /**************************/ + /* First element */ + min_diff_Q15 = NLSF_Q15[0] - NDeltaMin_Q15[0]; + I = 0; + /* Middle elements */ + for( i = 1; i <= L-1; i++ ) { + diff_Q15 = NLSF_Q15[i] - ( NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); + if( diff_Q15 < min_diff_Q15 ) { + min_diff_Q15 = diff_Q15; + I = i; + } + } + /* Last element */ + diff_Q15 = (1<<15) - ( NLSF_Q15[L-1] + NDeltaMin_Q15[L] ); + if( diff_Q15 < min_diff_Q15 ) { + min_diff_Q15 = diff_Q15; + I = L; + } + + /***************************************************/ + /* Now check if the smallest distance non-negative */ + /***************************************************/ + if (min_diff_Q15 >= 0) { + return; + } + + if( I == 0 ) { + /* Move away from lower limit */ + NLSF_Q15[0] = NDeltaMin_Q15[0]; + + } else if( I == L) { + /* Move away from higher limit */ + NLSF_Q15[L-1] = (1<<15) - NDeltaMin_Q15[L]; + + } else { + /* Find the lower extreme for the location of the current center frequency */ + min_center_Q15 = 0; + for( k = 0; k < I; k++ ) { + min_center_Q15 += NDeltaMin_Q15[k]; + } + min_center_Q15 += SKP_RSHIFT( NDeltaMin_Q15[I], 1 ); + + /* Find the upper extreme for the location of the current center frequency */ + max_center_Q15 = (1<<15); + for( k = L; k > I; k-- ) { + max_center_Q15 -= NDeltaMin_Q15[k]; + } + max_center_Q15 -= ( NDeltaMin_Q15[I] - SKP_RSHIFT( NDeltaMin_Q15[I], 1 ) ); + + /* Move apart, sorted by value, keeping the same center frequency */ + center_freq_Q15 = SKP_LIMIT( SKP_RSHIFT_ROUND( (SKP_int32)NLSF_Q15[I-1] + (SKP_int32)NLSF_Q15[I], 1 ), + min_center_Q15, max_center_Q15 ); + NLSF_Q15[I-1] = center_freq_Q15 - SKP_RSHIFT( NDeltaMin_Q15[I], 1 ); + NLSF_Q15[I] = NLSF_Q15[I-1] + NDeltaMin_Q15[I]; + } + } + + /* Safe and simple fall back method, which is less ideal than the above */ + if( loops == MAX_LOOPS ) + { + /* Insertion sort (fast for already almost sorted arrays): */ + /* Best case: O(n) for an already sorted array */ + /* Worst case: O(n^2) for an inversely sorted array */ + SKP_Silk_insertion_sort_increasing_all_values(&NLSF_Q15[0], L); + + /* First NLSF should be no less than NDeltaMin[0] */ + NLSF_Q15[0] = SKP_max_int( NLSF_Q15[0], NDeltaMin_Q15[0] ); + + /* Keep delta_min distance between the NLSFs */ + for( i = 1; i < L; i++ ) + NLSF_Q15[i] = SKP_max_int( NLSF_Q15[i], NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); + + /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ + NLSF_Q15[L-1] = SKP_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] ); + + /* Keep NDeltaMin distance between the NLSFs */ + for( i = L-2; i >= 0; i-- ) + NLSF_Q15[i] = SKP_min_int( NLSF_Q15[i], NLSF_Q15[i+1] - NDeltaMin_Q15[i+1] ); + } +} + +/* NLSF stabilizer, over multiple input column data vectors */ +void SKP_Silk_NLSF_stabilize_multi( + SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vectors in Q15 [LxN] */ + const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ + const SKP_int N, /* I: Number of input vectors to be stabilized */ + const SKP_int L /* I: NLSF vector dimension */ +) +{ + SKP_int n; + + /* loop over input data */ + for( n = 0; n < N; n++ ) { + SKP_Silk_NLSF_stabilize( &NLSF_Q15[n * L], NDeltaMin_Q15, L ); + } +} diff --git a/jni/silk/src/SKP_Silk_NSQ.c b/app/src/main/jni/silk/src/SKP_Silk_NSQ.c similarity index 98% rename from jni/silk/src/SKP_Silk_NSQ.c rename to app/src/main/jni/silk/src/SKP_Silk_NSQ.c index 19cb8ea..1060666 100644 --- a/jni/silk/src/SKP_Silk_NSQ.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NSQ.c @@ -1,446 +1,446 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -SKP_INLINE void SKP_Silk_nsq_scale_states( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - const SKP_int16 x[], /* I input in Q0 */ - SKP_int32 x_sc_Q10[], /* O input scaled with 1/Gain */ - SKP_int length, /* I length of input */ - SKP_int16 sLTP[], /* I re-whitened LTP state in Q0 */ - SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ - SKP_int subfr, /* I subframe number */ - const SKP_int LTP_scale_Q14, /* I */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int pitchL[ NB_SUBFR ] /* I */ -); - -SKP_INLINE void SKP_Silk_noise_shape_quantizer( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - SKP_int sigtype, /* I Signal type */ - const SKP_int32 x_sc_Q10[], /* I */ - SKP_int q[], /* O */ - SKP_int16 xq[], /* O */ - SKP_int32 sLTP_Q16[], /* I/O LTP state */ - const SKP_int16 a_Q12[], /* I Short term prediction coefs */ - const SKP_int16 b_Q14[], /* I Long term prediction coefs */ - const SKP_int16 AR_shp_Q13[], /* I Noise shaping AR coefs */ - SKP_int lag, /* I Pitch lag */ - SKP_int32 HarmShapeFIRPacked_Q14, /* I */ - SKP_int Tilt_Q14, /* I Spectral tilt */ - SKP_int32 LF_shp_Q14, /* I */ - SKP_int32 Gain_Q16, /* I */ - SKP_int Lambda_Q10, /* I */ - SKP_int offset_Q10, /* I */ - SKP_int length, /* I Input length */ - SKP_int shapingLPCOrder, /* I Noise shaping AR filter order */ - SKP_int predictLPCOrder /* I Prediction filter order */ -); - -void SKP_Silk_NSQ( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ - SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - const SKP_int16 x[], /* I prefiltered input signal */ - SKP_int q[], /* O quantized qulse signal */ - const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ - const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Short term prediction coefficients */ - const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I Long term prediction coefficients */ - const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ - const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ - const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ - const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int Lambda_Q10, /* I */ - const SKP_int LTP_scale_Q14 /* I LTP state scaling */ -) -{ - SKP_int k, lag, start_idx, subfr_length, LSF_interpolation_flag; - const SKP_int16 *A_Q12, *B_Q14, *AR_shp_Q13; - SKP_int16 *pxq; - SKP_int32 sLTP_Q16[ 2 * MAX_FRAME_LENGTH ]; - SKP_int16 sLTP[ 2 * MAX_FRAME_LENGTH ]; - SKP_int32 HarmShapeFIRPacked_Q14; - SKP_int offset_Q10; - SKP_int32 FiltState[ MAX_LPC_ORDER ]; - SKP_int32 x_sc_Q10[ MAX_FRAME_LENGTH / NB_SUBFR ]; - - subfr_length = psEncC->frame_length / NB_SUBFR; - - NSQ->rand_seed = psEncCtrlC->Seed; - /* Set unvoiced lag to the previous one, overwrite later for voiced */ - lag = NSQ->lagPrev; - - SKP_assert( NSQ->prev_inv_gain_Q16 != 0 ); - - offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psEncCtrlC->sigtype ][ psEncCtrlC->QuantOffsetType ]; - - if( LSFInterpFactor_Q2 == ( 1 << 2 ) ) { - LSF_interpolation_flag = 0; - } else { - LSF_interpolation_flag = 1; - } - - /* Setup pointers to start of sub frame */ - NSQ->sLTP_shp_buf_idx = psEncC->frame_length; - NSQ->sLTP_buf_idx = psEncC->frame_length; - pxq = &NSQ->xq[ psEncC->frame_length ]; - for( k = 0; k < NB_SUBFR; k++ ) { - A_Q12 = &PredCoef_Q12[ (( k >> 1 ) | ( 1 - LSF_interpolation_flag )) * MAX_LPC_ORDER ]; - B_Q14 = <PCoef_Q14[ k * LTP_ORDER ]; - AR_shp_Q13 = &AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ]; - - /* Noise shape parameters */ - SKP_assert( HarmShapeGain_Q14[ k ] >= 0 ); - HarmShapeFIRPacked_Q14 = SKP_RSHIFT( HarmShapeGain_Q14[ k ], 2 ); - HarmShapeFIRPacked_Q14 |= SKP_LSHIFT( ( SKP_int32 )SKP_RSHIFT( HarmShapeGain_Q14[ k ], 1 ), 16 ); - - if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { - /* Voiced */ - lag = psEncCtrlC->pitchL[ k ]; - - NSQ->rewhite_flag = 0; - /* Re-whitening */ - if( ( k & ( 3 - SKP_LSHIFT( LSF_interpolation_flag, 1 ) ) ) == 0 ) { - /* Rewhiten with new A coefs */ - - start_idx = psEncC->frame_length - lag - psEncC->predictLPCOrder - LTP_ORDER / 2; - start_idx = SKP_LIMIT( start_idx, 0, psEncC->frame_length - psEncC->predictLPCOrder ); /* Limit */ - - SKP_memset( FiltState, 0, psEncC->predictLPCOrder * sizeof( SKP_int32 ) ); - SKP_Silk_MA_Prediction( &NSQ->xq[ start_idx + k * ( psEncC->frame_length >> 2 ) ], - A_Q12, FiltState, sLTP + start_idx, psEncC->frame_length - start_idx, psEncC->predictLPCOrder ); - - NSQ->rewhite_flag = 1; - NSQ->sLTP_buf_idx = psEncC->frame_length; - } - } - - SKP_Silk_nsq_scale_states( NSQ, x, x_sc_Q10, psEncC->subfr_length, sLTP, - sLTP_Q16, k, LTP_scale_Q14, Gains_Q16, psEncCtrlC->pitchL ); - - SKP_Silk_noise_shape_quantizer( NSQ, psEncCtrlC->sigtype, x_sc_Q10, q, pxq, sLTP_Q16, A_Q12, B_Q14, - AR_shp_Q13, lag, HarmShapeFIRPacked_Q14, Tilt_Q14[ k ], LF_shp_Q14[ k ], Gains_Q16[ k ], Lambda_Q10, - offset_Q10, psEncC->subfr_length, psEncC->shapingLPCOrder, psEncC->predictLPCOrder - ); - - x += psEncC->subfr_length; - q += psEncC->subfr_length; - pxq += psEncC->subfr_length; - } - - /* Save scalars for this layer */ - NSQ->sLF_AR_shp_Q12 = NSQ->sLF_AR_shp_Q12; - NSQ->prev_inv_gain_Q16 = NSQ->prev_inv_gain_Q16; - NSQ->lagPrev = psEncCtrlC->pitchL[ NB_SUBFR - 1 ]; - /* Save quantized speech and noise shaping signals */ - SKP_memcpy( NSQ->xq, &NSQ->xq[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int16 ) ); - SKP_memcpy( NSQ->sLTP_shp_Q10, &NSQ->sLTP_shp_Q10[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int32 ) ); - -} - -/***********************************/ -/* SKP_Silk_noise_shape_quantizer */ -/***********************************/ -SKP_INLINE void SKP_Silk_noise_shape_quantizer( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - SKP_int sigtype, /* I Signal type */ - const SKP_int32 x_sc_Q10[], /* I */ - SKP_int q[], /* O */ - SKP_int16 xq[], /* O */ - SKP_int32 sLTP_Q16[], /* I/O LTP state */ - const SKP_int16 a_Q12[], /* I Short term prediction coefs */ - const SKP_int16 b_Q14[], /* I Long term prediction coefs */ - const SKP_int16 AR_shp_Q13[], /* I Noise shaping AR coefs */ - SKP_int lag, /* I Pitch lag */ - SKP_int32 HarmShapeFIRPacked_Q14, /* I */ - SKP_int Tilt_Q14, /* I Spectral tilt */ - SKP_int32 LF_shp_Q14, /* I */ - SKP_int32 Gain_Q16, /* I */ - SKP_int Lambda_Q10, /* I */ - SKP_int offset_Q10, /* I */ - SKP_int length, /* I Input length */ - SKP_int shapingLPCOrder, /* I Noise shaping AR filter order */ - SKP_int predictLPCOrder /* I Prediction filter order */ -) -{ - SKP_int i, j; - SKP_int32 LTP_pred_Q14, LPC_pred_Q10, n_AR_Q10, n_LTP_Q14; - SKP_int32 n_LF_Q10, r_Q10, q_Q0, q_Q10; - SKP_int32 thr1_Q10, thr2_Q10, thr3_Q10; - SKP_int32 Atmp, dither; - SKP_int32 exc_Q10, LPC_exc_Q10, xq_Q10; - SKP_int32 tmp, sLF_AR_shp_Q10; - SKP_int32 *psLPC_Q14; - SKP_int32 *shp_lag_ptr, *pred_lag_ptr; - SKP_int32 a_Q12_tmp[ MAX_LPC_ORDER / 2 ], AR_shp_Q13_tmp[ MAX_LPC_ORDER / 2 ]; - - shp_lag_ptr = &NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; - pred_lag_ptr = &sLTP_Q16[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; - - /* Setup short term AR state */ - psLPC_Q14 = &NSQ->sLPC_Q14[ MAX_LPC_ORDER - 1 ]; - - /* Quantization thresholds */ - thr1_Q10 = SKP_SUB_RSHIFT32( -1536, Lambda_Q10, 1); - thr2_Q10 = SKP_SUB_RSHIFT32( -512, Lambda_Q10, 1); - thr2_Q10 = SKP_ADD_RSHIFT32( thr2_Q10, SKP_SMULBB( offset_Q10, Lambda_Q10 ), 10 ); - thr3_Q10 = SKP_ADD_RSHIFT32( 512, Lambda_Q10, 1); - - /* Preload LPC coeficients to array on stack. Gives small performance gain */ - SKP_memcpy( a_Q12_tmp, a_Q12, predictLPCOrder * sizeof( SKP_int16 ) ); - SKP_memcpy( AR_shp_Q13_tmp, AR_shp_Q13, shapingLPCOrder * sizeof( SKP_int16 ) ); - - for( i = 0; i < length; i++ ) { - /* Generate dither */ - NSQ->rand_seed = SKP_RAND( NSQ->rand_seed ); - - /* dither = rand_seed < 0 ? 0xFFFFFFFF : 0; */ - dither = SKP_RSHIFT( NSQ->rand_seed, 31 ); - - /* Short-term prediction */ - SKP_assert( ( predictLPCOrder & 1 ) == 0 ); /* check that order is even */ - SKP_assert( ( (SKP_int64)a_Q12 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ - SKP_assert( predictLPCOrder >= 10 ); /* check that unrolling works */ - - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLAWB and SMLAWT instructions should solve the problem. */ - /* Partially unrolled */ - Atmp = a_Q12_tmp[ 0 ]; /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -1 ], Atmp ); - Atmp = a_Q12_tmp[ 1 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -2 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -3 ], Atmp ); - Atmp = a_Q12_tmp[ 2 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -4 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -5 ], Atmp ); - Atmp = a_Q12_tmp[ 3 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -6 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -7 ], Atmp ); - Atmp = a_Q12_tmp[ 4 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -8 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -9 ], Atmp ); - for( j = 10; j < predictLPCOrder; j += 2 ) { - Atmp = a_Q12_tmp[ j >> 1 ]; /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -j ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -j - 1 ], Atmp ); - } - - /* Long-term prediction */ - if( sigtype == SIG_TYPE_VOICED ) { - /* Unrolled loop */ - LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], b_Q14[ 0 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], b_Q14[ 1 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], b_Q14[ 2 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], b_Q14[ 3 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], b_Q14[ 4 ] ); - pred_lag_ptr++; - } else { - LTP_pred_Q14 = 0; - } - - /* Noise shape feedback */ - SKP_assert( ( shapingLPCOrder & 1 ) == 0 ); /* check that order is even */ - SKP_assert( ( (SKP_int64)AR_shp_Q13 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ - SKP_assert( shapingLPCOrder >= 12 ); /* check that unrolling works */ - - /* Partially unrolled */ - Atmp = AR_shp_Q13_tmp[ 0 ]; /* read two coefficients at once */ - n_AR_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -1 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 1 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -2 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -3 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 2 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -4 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -5 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 3 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -6 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -7 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 4 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -8 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -9 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 5 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -10 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -11 ], Atmp ); - for( j = 12; j < shapingLPCOrder; j += 2 ) { - Atmp = AR_shp_Q13_tmp[ j >> 1 ]; /* read two coefficients at once */ - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -j ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -j - 1 ], Atmp ); - } - n_AR_Q10 = SKP_RSHIFT( n_AR_Q10, 1 ); /* Q11 -> Q10 */ - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, NSQ->sLF_AR_shp_Q12, Tilt_Q14 ); - - n_LF_Q10 = SKP_LSHIFT( SKP_SMULWB( NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - 1 ], LF_shp_Q14 ), 2 ); - n_LF_Q10 = SKP_SMLAWT( n_LF_Q10, NSQ->sLF_AR_shp_Q12, LF_shp_Q14 ); - - SKP_assert( lag > 0 || sigtype == SIG_TYPE_UNVOICED); - - /* Long-term shaping */ - if( lag > 0 ) { - /* Symmetric, packed FIR coefficients */ - n_LTP_Q14 = SKP_SMULWB( SKP_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); - n_LTP_Q14 = SKP_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); - shp_lag_ptr++; - n_LTP_Q14 = SKP_LSHIFT( n_LTP_Q14, 6 ); - } else { - n_LTP_Q14 = 0; - } - - /* Input minus prediction plus noise feedback */ - //r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP; - tmp = SKP_SUB32( LTP_pred_Q14, n_LTP_Q14 ); /* Add Q14 stuff */ - tmp = SKP_RSHIFT_ROUND( tmp, 4 ); /* round to Q10 */ - tmp = SKP_ADD32( tmp, LPC_pred_Q10 ); /* add Q10 stuff */ - tmp = SKP_SUB32( tmp, n_AR_Q10 ); /* subtract Q10 stuff */ - tmp = SKP_SUB32( tmp, n_LF_Q10 ); /* subtract Q10 stuff */ - r_Q10 = SKP_SUB32( x_sc_Q10[ i ], tmp ); - - - /* Flip sign depending on dither */ - r_Q10 = ( r_Q10 ^ dither ) - dither; - r_Q10 = SKP_SUB32( r_Q10, offset_Q10 ); - r_Q10 = SKP_LIMIT( r_Q10, -64 << 10, 64 << 10 ); - - /* Quantize */ - if( r_Q10 < thr1_Q10 ) { - q_Q0 = SKP_RSHIFT_ROUND( SKP_ADD_RSHIFT32( r_Q10, Lambda_Q10, 1 ), 10 ); - q_Q10 = SKP_LSHIFT( q_Q0, 10 ); - } else if( r_Q10 < thr2_Q10 ) { - q_Q0 = -1; - q_Q10 = -1024; - } else if( r_Q10 > thr3_Q10 ) { - q_Q0 = SKP_RSHIFT_ROUND( SKP_SUB_RSHIFT32( r_Q10, Lambda_Q10, 1 ), 10 ); - q_Q10 = SKP_LSHIFT( q_Q0, 10 ); - } else { - q_Q0 = 0; - q_Q10 = 0; - } - q[ i ] = q_Q0; - - /* Excitation */ - exc_Q10 = SKP_ADD32( q_Q10, offset_Q10 ); - exc_Q10 = ( exc_Q10 ^ dither ) - dither; - - /* Add predictions */ - LPC_exc_Q10 = SKP_ADD32( exc_Q10, SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ) ); - xq_Q10 = SKP_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); - - /* Scale XQ back to normal level before saving */ - xq[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( xq_Q10, Gain_Q16 ), 10 ) ); - - - /* Update states */ - psLPC_Q14++; - *psLPC_Q14 = SKP_LSHIFT( xq_Q10, 4 ); - sLF_AR_shp_Q10 = SKP_SUB32( xq_Q10, n_AR_Q10 ); - NSQ->sLF_AR_shp_Q12 = SKP_LSHIFT( sLF_AR_shp_Q10, 2 ); - - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx ] = SKP_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); - sLTP_Q16[NSQ->sLTP_buf_idx] = SKP_LSHIFT( LPC_exc_Q10, 6 ); - NSQ->sLTP_shp_buf_idx++; - NSQ->sLTP_buf_idx++; - - /* Make dither dependent on quantized signal */ - NSQ->rand_seed += q[ i ]; - } - /* Update LPC synth buffer */ - SKP_memcpy( NSQ->sLPC_Q14, &NSQ->sLPC_Q14[ length ], MAX_LPC_ORDER * sizeof( SKP_int32 ) ); -} - -SKP_INLINE void SKP_Silk_nsq_scale_states( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - const SKP_int16 x[], /* I input in Q0 */ - SKP_int32 x_sc_Q10[], /* O input scaled with 1/Gain */ - SKP_int length, /* I length of input */ - SKP_int16 sLTP[], /* I re-whitened LTP state in Q0 */ - SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ - SKP_int subfr, /* I subframe number */ - const SKP_int LTP_scale_Q14, /* I */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int pitchL[ NB_SUBFR ] /* I */ -) -{ - SKP_int i, scale_length, lag; - SKP_int32 inv_gain_Q16, gain_adj_Q16, inv_gain_Q32; - - inv_gain_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( Gains_Q16[ subfr ], 1) ); - inv_gain_Q16 = SKP_min( inv_gain_Q16, SKP_int16_MAX ); - lag = pitchL[ subfr ]; - - /* After rewhitening the LTP state is un-scaled */ - if( NSQ->rewhite_flag ) { - inv_gain_Q32 = SKP_LSHIFT( inv_gain_Q16, 16 ); - if( subfr == 0 ) { - /* Do LTP downscaling */ - inv_gain_Q32 = SKP_LSHIFT( SKP_SMULWB( inv_gain_Q32, LTP_scale_Q14 ), 2 ); - } - for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { - sLTP_Q16[ i ] = SKP_SMULWB( inv_gain_Q32, sLTP[ i ] ); - } - } - - /* Prepare for Worst case. Next frame starts with max lag voiced */ - scale_length = length * NB_SUBFR; /* approx max lag */ - scale_length = scale_length - SKP_SMULBB( NB_SUBFR - (subfr + 1), length ); /* subtract samples that will be too old in next frame */ - scale_length = SKP_max_int( scale_length, lag + LTP_ORDER ); /* make sure to scale whole pitch period if voiced */ - - /* Adjust for changing gain */ - if( inv_gain_Q16 != NSQ->prev_inv_gain_Q16 ) { - gain_adj_Q16 = SKP_DIV32_varQ( inv_gain_Q16, NSQ->prev_inv_gain_Q16, 16 ); - - for( i = NSQ->sLTP_shp_buf_idx - scale_length; i < NSQ->sLTP_shp_buf_idx; i++ ) { - NSQ->sLTP_shp_Q10[ i ] = SKP_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q10[ i ] ); - } - - /* Scale LTP predict state */ - if( NSQ->rewhite_flag == 0 ) { - for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { - sLTP_Q16[ i ] = SKP_SMULWW( gain_adj_Q16, sLTP_Q16[ i ] ); - } - } - NSQ->sLF_AR_shp_Q12 = SKP_SMULWW( gain_adj_Q16, NSQ->sLF_AR_shp_Q12 ); - - /* scale short term state */ - for( i = 0; i < MAX_LPC_ORDER; i++ ) { - NSQ->sLPC_Q14[ i ] = SKP_SMULWW( gain_adj_Q16, NSQ->sLPC_Q14[ i ] ); - } - } - - /* Scale input */ - for( i = 0; i < length; i++ ) { - x_sc_Q10[ i ] = SKP_RSHIFT( SKP_SMULBB( x[ i ], ( SKP_int16 )inv_gain_Q16 ), 6 ); - } - - /* save inv_gain */ - SKP_assert( inv_gain_Q16 != 0 ); - NSQ->prev_inv_gain_Q16 = inv_gain_Q16; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +SKP_INLINE void SKP_Silk_nsq_scale_states( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + const SKP_int16 x[], /* I input in Q0 */ + SKP_int32 x_sc_Q10[], /* O input scaled with 1/Gain */ + SKP_int length, /* I length of input */ + SKP_int16 sLTP[], /* I re-whitened LTP state in Q0 */ + SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ + SKP_int subfr, /* I subframe number */ + const SKP_int LTP_scale_Q14, /* I */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int pitchL[ NB_SUBFR ] /* I */ +); + +SKP_INLINE void SKP_Silk_noise_shape_quantizer( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + SKP_int sigtype, /* I Signal type */ + const SKP_int32 x_sc_Q10[], /* I */ + SKP_int q[], /* O */ + SKP_int16 xq[], /* O */ + SKP_int32 sLTP_Q16[], /* I/O LTP state */ + const SKP_int16 a_Q12[], /* I Short term prediction coefs */ + const SKP_int16 b_Q14[], /* I Long term prediction coefs */ + const SKP_int16 AR_shp_Q13[], /* I Noise shaping AR coefs */ + SKP_int lag, /* I Pitch lag */ + SKP_int32 HarmShapeFIRPacked_Q14, /* I */ + SKP_int Tilt_Q14, /* I Spectral tilt */ + SKP_int32 LF_shp_Q14, /* I */ + SKP_int32 Gain_Q16, /* I */ + SKP_int Lambda_Q10, /* I */ + SKP_int offset_Q10, /* I */ + SKP_int length, /* I Input length */ + SKP_int shapingLPCOrder, /* I Noise shaping AR filter order */ + SKP_int predictLPCOrder /* I Prediction filter order */ +); + +void SKP_Silk_NSQ( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ + SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + const SKP_int16 x[], /* I prefiltered input signal */ + SKP_int q[], /* O quantized qulse signal */ + const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ + const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Short term prediction coefficients */ + const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I Long term prediction coefficients */ + const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ + const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ + const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ + const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int Lambda_Q10, /* I */ + const SKP_int LTP_scale_Q14 /* I LTP state scaling */ +) +{ + SKP_int k, lag, start_idx, subfr_length, LSF_interpolation_flag; + const SKP_int16 *A_Q12, *B_Q14, *AR_shp_Q13; + SKP_int16 *pxq; + SKP_int32 sLTP_Q16[ 2 * MAX_FRAME_LENGTH ]; + SKP_int16 sLTP[ 2 * MAX_FRAME_LENGTH ]; + SKP_int32 HarmShapeFIRPacked_Q14; + SKP_int offset_Q10; + SKP_int32 FiltState[ MAX_LPC_ORDER ]; + SKP_int32 x_sc_Q10[ MAX_FRAME_LENGTH / NB_SUBFR ]; + + subfr_length = psEncC->frame_length / NB_SUBFR; + + NSQ->rand_seed = psEncCtrlC->Seed; + /* Set unvoiced lag to the previous one, overwrite later for voiced */ + lag = NSQ->lagPrev; + + SKP_assert( NSQ->prev_inv_gain_Q16 != 0 ); + + offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psEncCtrlC->sigtype ][ psEncCtrlC->QuantOffsetType ]; + + if( LSFInterpFactor_Q2 == ( 1 << 2 ) ) { + LSF_interpolation_flag = 0; + } else { + LSF_interpolation_flag = 1; + } + + /* Setup pointers to start of sub frame */ + NSQ->sLTP_shp_buf_idx = psEncC->frame_length; + NSQ->sLTP_buf_idx = psEncC->frame_length; + pxq = &NSQ->xq[ psEncC->frame_length ]; + for( k = 0; k < NB_SUBFR; k++ ) { + A_Q12 = &PredCoef_Q12[ (( k >> 1 ) | ( 1 - LSF_interpolation_flag )) * MAX_LPC_ORDER ]; + B_Q14 = <PCoef_Q14[ k * LTP_ORDER ]; + AR_shp_Q13 = &AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ]; + + /* Noise shape parameters */ + SKP_assert( HarmShapeGain_Q14[ k ] >= 0 ); + HarmShapeFIRPacked_Q14 = SKP_RSHIFT( HarmShapeGain_Q14[ k ], 2 ); + HarmShapeFIRPacked_Q14 |= SKP_LSHIFT( ( SKP_int32 )SKP_RSHIFT( HarmShapeGain_Q14[ k ], 1 ), 16 ); + + if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { + /* Voiced */ + lag = psEncCtrlC->pitchL[ k ]; + + NSQ->rewhite_flag = 0; + /* Re-whitening */ + if( ( k & ( 3 - SKP_LSHIFT( LSF_interpolation_flag, 1 ) ) ) == 0 ) { + /* Rewhiten with new A coefs */ + + start_idx = psEncC->frame_length - lag - psEncC->predictLPCOrder - LTP_ORDER / 2; + start_idx = SKP_LIMIT( start_idx, 0, psEncC->frame_length - psEncC->predictLPCOrder ); /* Limit */ + + SKP_memset( FiltState, 0, psEncC->predictLPCOrder * sizeof( SKP_int32 ) ); + SKP_Silk_MA_Prediction( &NSQ->xq[ start_idx + k * ( psEncC->frame_length >> 2 ) ], + A_Q12, FiltState, sLTP + start_idx, psEncC->frame_length - start_idx, psEncC->predictLPCOrder ); + + NSQ->rewhite_flag = 1; + NSQ->sLTP_buf_idx = psEncC->frame_length; + } + } + + SKP_Silk_nsq_scale_states( NSQ, x, x_sc_Q10, psEncC->subfr_length, sLTP, + sLTP_Q16, k, LTP_scale_Q14, Gains_Q16, psEncCtrlC->pitchL ); + + SKP_Silk_noise_shape_quantizer( NSQ, psEncCtrlC->sigtype, x_sc_Q10, q, pxq, sLTP_Q16, A_Q12, B_Q14, + AR_shp_Q13, lag, HarmShapeFIRPacked_Q14, Tilt_Q14[ k ], LF_shp_Q14[ k ], Gains_Q16[ k ], Lambda_Q10, + offset_Q10, psEncC->subfr_length, psEncC->shapingLPCOrder, psEncC->predictLPCOrder + ); + + x += psEncC->subfr_length; + q += psEncC->subfr_length; + pxq += psEncC->subfr_length; + } + + /* Save scalars for this layer */ + NSQ->sLF_AR_shp_Q12 = NSQ->sLF_AR_shp_Q12; + NSQ->prev_inv_gain_Q16 = NSQ->prev_inv_gain_Q16; + NSQ->lagPrev = psEncCtrlC->pitchL[ NB_SUBFR - 1 ]; + /* Save quantized speech and noise shaping signals */ + SKP_memcpy( NSQ->xq, &NSQ->xq[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int16 ) ); + SKP_memcpy( NSQ->sLTP_shp_Q10, &NSQ->sLTP_shp_Q10[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int32 ) ); + +} + +/***********************************/ +/* SKP_Silk_noise_shape_quantizer */ +/***********************************/ +SKP_INLINE void SKP_Silk_noise_shape_quantizer( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + SKP_int sigtype, /* I Signal type */ + const SKP_int32 x_sc_Q10[], /* I */ + SKP_int q[], /* O */ + SKP_int16 xq[], /* O */ + SKP_int32 sLTP_Q16[], /* I/O LTP state */ + const SKP_int16 a_Q12[], /* I Short term prediction coefs */ + const SKP_int16 b_Q14[], /* I Long term prediction coefs */ + const SKP_int16 AR_shp_Q13[], /* I Noise shaping AR coefs */ + SKP_int lag, /* I Pitch lag */ + SKP_int32 HarmShapeFIRPacked_Q14, /* I */ + SKP_int Tilt_Q14, /* I Spectral tilt */ + SKP_int32 LF_shp_Q14, /* I */ + SKP_int32 Gain_Q16, /* I */ + SKP_int Lambda_Q10, /* I */ + SKP_int offset_Q10, /* I */ + SKP_int length, /* I Input length */ + SKP_int shapingLPCOrder, /* I Noise shaping AR filter order */ + SKP_int predictLPCOrder /* I Prediction filter order */ +) +{ + SKP_int i, j; + SKP_int32 LTP_pred_Q14, LPC_pred_Q10, n_AR_Q10, n_LTP_Q14; + SKP_int32 n_LF_Q10, r_Q10, q_Q0, q_Q10; + SKP_int32 thr1_Q10, thr2_Q10, thr3_Q10; + SKP_int32 Atmp, dither; + SKP_int32 exc_Q10, LPC_exc_Q10, xq_Q10; + SKP_int32 tmp, sLF_AR_shp_Q10; + SKP_int32 *psLPC_Q14; + SKP_int32 *shp_lag_ptr, *pred_lag_ptr; + SKP_int32 a_Q12_tmp[ MAX_LPC_ORDER / 2 ], AR_shp_Q13_tmp[ MAX_LPC_ORDER / 2 ]; + + shp_lag_ptr = &NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; + pred_lag_ptr = &sLTP_Q16[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; + + /* Setup short term AR state */ + psLPC_Q14 = &NSQ->sLPC_Q14[ MAX_LPC_ORDER - 1 ]; + + /* Quantization thresholds */ + thr1_Q10 = SKP_SUB_RSHIFT32( -1536, Lambda_Q10, 1); + thr2_Q10 = SKP_SUB_RSHIFT32( -512, Lambda_Q10, 1); + thr2_Q10 = SKP_ADD_RSHIFT32( thr2_Q10, SKP_SMULBB( offset_Q10, Lambda_Q10 ), 10 ); + thr3_Q10 = SKP_ADD_RSHIFT32( 512, Lambda_Q10, 1); + + /* Preload LPC coeficients to array on stack. Gives small performance gain */ + SKP_memcpy( a_Q12_tmp, a_Q12, predictLPCOrder * sizeof( SKP_int16 ) ); + SKP_memcpy( AR_shp_Q13_tmp, AR_shp_Q13, shapingLPCOrder * sizeof( SKP_int16 ) ); + + for( i = 0; i < length; i++ ) { + /* Generate dither */ + NSQ->rand_seed = SKP_RAND( NSQ->rand_seed ); + + /* dither = rand_seed < 0 ? 0xFFFFFFFF : 0; */ + dither = SKP_RSHIFT( NSQ->rand_seed, 31 ); + + /* Short-term prediction */ + SKP_assert( ( predictLPCOrder & 1 ) == 0 ); /* check that order is even */ + SKP_assert( ( (SKP_int64)a_Q12 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ + SKP_assert( predictLPCOrder >= 10 ); /* check that unrolling works */ + + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLAWB and SMLAWT instructions should solve the problem. */ + /* Partially unrolled */ + Atmp = a_Q12_tmp[ 0 ]; /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -1 ], Atmp ); + Atmp = a_Q12_tmp[ 1 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -2 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -3 ], Atmp ); + Atmp = a_Q12_tmp[ 2 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -4 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -5 ], Atmp ); + Atmp = a_Q12_tmp[ 3 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -6 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -7 ], Atmp ); + Atmp = a_Q12_tmp[ 4 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -8 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -9 ], Atmp ); + for( j = 10; j < predictLPCOrder; j += 2 ) { + Atmp = a_Q12_tmp[ j >> 1 ]; /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -j ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -j - 1 ], Atmp ); + } + + /* Long-term prediction */ + if( sigtype == SIG_TYPE_VOICED ) { + /* Unrolled loop */ + LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], b_Q14[ 0 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], b_Q14[ 1 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], b_Q14[ 2 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], b_Q14[ 3 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], b_Q14[ 4 ] ); + pred_lag_ptr++; + } else { + LTP_pred_Q14 = 0; + } + + /* Noise shape feedback */ + SKP_assert( ( shapingLPCOrder & 1 ) == 0 ); /* check that order is even */ + SKP_assert( ( (SKP_int64)AR_shp_Q13 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ + SKP_assert( shapingLPCOrder >= 12 ); /* check that unrolling works */ + + /* Partially unrolled */ + Atmp = AR_shp_Q13_tmp[ 0 ]; /* read two coefficients at once */ + n_AR_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -1 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 1 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -2 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -3 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 2 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -4 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -5 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 3 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -6 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -7 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 4 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -8 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -9 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 5 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -10 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -11 ], Atmp ); + for( j = 12; j < shapingLPCOrder; j += 2 ) { + Atmp = AR_shp_Q13_tmp[ j >> 1 ]; /* read two coefficients at once */ + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -j ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -j - 1 ], Atmp ); + } + n_AR_Q10 = SKP_RSHIFT( n_AR_Q10, 1 ); /* Q11 -> Q10 */ + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, NSQ->sLF_AR_shp_Q12, Tilt_Q14 ); + + n_LF_Q10 = SKP_LSHIFT( SKP_SMULWB( NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - 1 ], LF_shp_Q14 ), 2 ); + n_LF_Q10 = SKP_SMLAWT( n_LF_Q10, NSQ->sLF_AR_shp_Q12, LF_shp_Q14 ); + + SKP_assert( lag > 0 || sigtype == SIG_TYPE_UNVOICED); + + /* Long-term shaping */ + if( lag > 0 ) { + /* Symmetric, packed FIR coefficients */ + n_LTP_Q14 = SKP_SMULWB( SKP_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); + n_LTP_Q14 = SKP_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); + shp_lag_ptr++; + n_LTP_Q14 = SKP_LSHIFT( n_LTP_Q14, 6 ); + } else { + n_LTP_Q14 = 0; + } + + /* Input minus prediction plus noise feedback */ + //r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP; + tmp = SKP_SUB32( LTP_pred_Q14, n_LTP_Q14 ); /* Add Q14 stuff */ + tmp = SKP_RSHIFT_ROUND( tmp, 4 ); /* round to Q10 */ + tmp = SKP_ADD32( tmp, LPC_pred_Q10 ); /* add Q10 stuff */ + tmp = SKP_SUB32( tmp, n_AR_Q10 ); /* subtract Q10 stuff */ + tmp = SKP_SUB32( tmp, n_LF_Q10 ); /* subtract Q10 stuff */ + r_Q10 = SKP_SUB32( x_sc_Q10[ i ], tmp ); + + + /* Flip sign depending on dither */ + r_Q10 = ( r_Q10 ^ dither ) - dither; + r_Q10 = SKP_SUB32( r_Q10, offset_Q10 ); + r_Q10 = SKP_LIMIT( r_Q10, -64 << 10, 64 << 10 ); + + /* Quantize */ + if( r_Q10 < thr1_Q10 ) { + q_Q0 = SKP_RSHIFT_ROUND( SKP_ADD_RSHIFT32( r_Q10, Lambda_Q10, 1 ), 10 ); + q_Q10 = SKP_LSHIFT( q_Q0, 10 ); + } else if( r_Q10 < thr2_Q10 ) { + q_Q0 = -1; + q_Q10 = -1024; + } else if( r_Q10 > thr3_Q10 ) { + q_Q0 = SKP_RSHIFT_ROUND( SKP_SUB_RSHIFT32( r_Q10, Lambda_Q10, 1 ), 10 ); + q_Q10 = SKP_LSHIFT( q_Q0, 10 ); + } else { + q_Q0 = 0; + q_Q10 = 0; + } + q[ i ] = q_Q0; + + /* Excitation */ + exc_Q10 = SKP_ADD32( q_Q10, offset_Q10 ); + exc_Q10 = ( exc_Q10 ^ dither ) - dither; + + /* Add predictions */ + LPC_exc_Q10 = SKP_ADD32( exc_Q10, SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ) ); + xq_Q10 = SKP_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); + + /* Scale XQ back to normal level before saving */ + xq[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( xq_Q10, Gain_Q16 ), 10 ) ); + + + /* Update states */ + psLPC_Q14++; + *psLPC_Q14 = SKP_LSHIFT( xq_Q10, 4 ); + sLF_AR_shp_Q10 = SKP_SUB32( xq_Q10, n_AR_Q10 ); + NSQ->sLF_AR_shp_Q12 = SKP_LSHIFT( sLF_AR_shp_Q10, 2 ); + + NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx ] = SKP_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); + sLTP_Q16[NSQ->sLTP_buf_idx] = SKP_LSHIFT( LPC_exc_Q10, 6 ); + NSQ->sLTP_shp_buf_idx++; + NSQ->sLTP_buf_idx++; + + /* Make dither dependent on quantized signal */ + NSQ->rand_seed += q[ i ]; + } + /* Update LPC synth buffer */ + SKP_memcpy( NSQ->sLPC_Q14, &NSQ->sLPC_Q14[ length ], MAX_LPC_ORDER * sizeof( SKP_int32 ) ); +} + +SKP_INLINE void SKP_Silk_nsq_scale_states( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + const SKP_int16 x[], /* I input in Q0 */ + SKP_int32 x_sc_Q10[], /* O input scaled with 1/Gain */ + SKP_int length, /* I length of input */ + SKP_int16 sLTP[], /* I re-whitened LTP state in Q0 */ + SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ + SKP_int subfr, /* I subframe number */ + const SKP_int LTP_scale_Q14, /* I */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int pitchL[ NB_SUBFR ] /* I */ +) +{ + SKP_int i, scale_length, lag; + SKP_int32 inv_gain_Q16, gain_adj_Q16, inv_gain_Q32; + + inv_gain_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( Gains_Q16[ subfr ], 1) ); + inv_gain_Q16 = SKP_min( inv_gain_Q16, SKP_int16_MAX ); + lag = pitchL[ subfr ]; + + /* After rewhitening the LTP state is un-scaled */ + if( NSQ->rewhite_flag ) { + inv_gain_Q32 = SKP_LSHIFT( inv_gain_Q16, 16 ); + if( subfr == 0 ) { + /* Do LTP downscaling */ + inv_gain_Q32 = SKP_LSHIFT( SKP_SMULWB( inv_gain_Q32, LTP_scale_Q14 ), 2 ); + } + for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { + sLTP_Q16[ i ] = SKP_SMULWB( inv_gain_Q32, sLTP[ i ] ); + } + } + + /* Prepare for Worst case. Next frame starts with max lag voiced */ + scale_length = length * NB_SUBFR; /* approx max lag */ + scale_length = scale_length - SKP_SMULBB( NB_SUBFR - (subfr + 1), length ); /* subtract samples that will be too old in next frame */ + scale_length = SKP_max_int( scale_length, lag + LTP_ORDER ); /* make sure to scale whole pitch period if voiced */ + + /* Adjust for changing gain */ + if( inv_gain_Q16 != NSQ->prev_inv_gain_Q16 ) { + gain_adj_Q16 = SKP_DIV32_varQ( inv_gain_Q16, NSQ->prev_inv_gain_Q16, 16 ); + + for( i = NSQ->sLTP_shp_buf_idx - scale_length; i < NSQ->sLTP_shp_buf_idx; i++ ) { + NSQ->sLTP_shp_Q10[ i ] = SKP_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q10[ i ] ); + } + + /* Scale LTP predict state */ + if( NSQ->rewhite_flag == 0 ) { + for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { + sLTP_Q16[ i ] = SKP_SMULWW( gain_adj_Q16, sLTP_Q16[ i ] ); + } + } + NSQ->sLF_AR_shp_Q12 = SKP_SMULWW( gain_adj_Q16, NSQ->sLF_AR_shp_Q12 ); + + /* scale short term state */ + for( i = 0; i < MAX_LPC_ORDER; i++ ) { + NSQ->sLPC_Q14[ i ] = SKP_SMULWW( gain_adj_Q16, NSQ->sLPC_Q14[ i ] ); + } + } + + /* Scale input */ + for( i = 0; i < length; i++ ) { + x_sc_Q10[ i ] = SKP_RSHIFT( SKP_SMULBB( x[ i ], ( SKP_int16 )inv_gain_Q16 ), 6 ); + } + + /* save inv_gain */ + SKP_assert( inv_gain_Q16 != 0 ); + NSQ->prev_inv_gain_Q16 = inv_gain_Q16; +} diff --git a/jni/silk/src/SKP_Silk_NSQ_del_dec.c b/app/src/main/jni/silk/src/SKP_Silk_NSQ_del_dec.c similarity index 98% rename from jni/silk/src/SKP_Silk_NSQ_del_dec.c rename to app/src/main/jni/silk/src/SKP_Silk_NSQ_del_dec.c index 3601263..15fa3c7 100644 --- a/jni/silk/src/SKP_Silk_NSQ_del_dec.c +++ b/app/src/main/jni/silk/src/SKP_Silk_NSQ_del_dec.c @@ -1,722 +1,722 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -typedef struct { - SKP_int RandState[ DECISION_DELAY ]; - SKP_int32 Q_Q10[ DECISION_DELAY ]; - SKP_int32 Xq_Q10[ DECISION_DELAY ]; - SKP_int32 Pred_Q16[ DECISION_DELAY ]; - SKP_int32 Shape_Q10[ DECISION_DELAY ]; - SKP_int32 Gain_Q16[ DECISION_DELAY ]; - SKP_int32 sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + NSQ_LPC_BUF_LENGTH ]; - SKP_int32 LF_AR_Q12; - SKP_int32 Seed; - SKP_int32 SeedInit; - SKP_int32 RD_Q10; -} NSQ_del_dec_struct; - -typedef struct { - SKP_int32 Q_Q10; - SKP_int32 RD_Q10; - SKP_int32 xq_Q14; - SKP_int32 LF_AR_Q12; - SKP_int32 sLTP_shp_Q10; - SKP_int32 LPC_exc_Q16; -} NSQ_sample_struct; - -SKP_INLINE void SKP_Silk_copy_del_dec_state( - NSQ_del_dec_struct *DD_dst, /* I Dst del dec state */ - NSQ_del_dec_struct *DD_src, /* I Src del dec state */ - SKP_int LPC_state_idx /* I Index to LPC buffer */ -); - -SKP_INLINE void SKP_Silk_nsq_del_dec_scale_states( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ - const SKP_int16 x[], /* I Input in Q0 */ - SKP_int32 x_sc_Q10[], /* O Input scaled with 1/Gain in Q10 */ - SKP_int length, /* I Length of input */ - SKP_int16 sLTP[], /* I Re-whitened LTP state in Q0 */ - SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ - SKP_int subfr, /* I Subframe number */ - SKP_int nStatesDelayedDecision, /* I Number of del dec states */ - SKP_int smpl_buf_idx, /* I Index to newest samples in buffers */ - const SKP_int LTP_scale_Q14, /* I LTP state scaling */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int pitchL[ NB_SUBFR ] /* I Pitch lag */ -); - -/******************************************/ -/* Noise shape quantizer for one subframe */ -/******************************************/ -SKP_INLINE void SKP_Silk_noise_shape_quantizer_del_dec( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ - SKP_int sigtype, /* I Signal type */ - const SKP_int32 x_Q10[], /* I */ - SKP_int q[], /* O */ - SKP_int16 xq[], /* O */ - SKP_int32 sLTP_Q16[], /* I/O LTP filter state */ - const SKP_int16 a_Q12[], /* I Short term prediction coefs */ - const SKP_int16 b_Q14[], /* I Long term prediction coefs */ - const SKP_int16 AR_shp_Q13[], /* I Noise shaping coefs */ - SKP_int lag, /* I Pitch lag */ - SKP_int32 HarmShapeFIRPacked_Q14, /* I */ - SKP_int Tilt_Q14, /* I Spectral tilt */ - SKP_int32 LF_shp_Q14, /* I */ - SKP_int32 Gain_Q16, /* I */ - SKP_int Lambda_Q10, /* I */ - SKP_int offset_Q10, /* I */ - SKP_int length, /* I Input length */ - SKP_int subfr, /* I Subframe number */ - SKP_int shapingLPCOrder, /* I Shaping LPC filter order */ - SKP_int predictLPCOrder, /* I Prediction LPC filter order */ - SKP_int nStatesDelayedDecision, /* I Number of states in decision tree */ - SKP_int *smpl_buf_idx, /* I Index to newest samples in buffers */ - SKP_int decisionDelay /* I */ -); - -void SKP_Silk_NSQ_del_dec( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ - SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - const SKP_int16 x[], /* I Prefiltered input signal */ - SKP_int q[], /* O Quantized pulse signal */ - const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ - const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Prediction coefs */ - const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I LT prediction coefs */ - const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ - const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ - const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ - const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int Lambda_Q10, /* I */ - const SKP_int LTP_scale_Q14 /* I LTP state scaling */ -) -{ - SKP_int i, k, lag, start_idx, LSF_interpolation_flag, Winner_ind, subfr; - SKP_int last_smple_idx, smpl_buf_idx, decisionDelay, subfr_length; - const SKP_int16 *A_Q12, *B_Q14, *AR_shp_Q13; - SKP_int16 *pxq; - SKP_int32 sLTP_Q16[ 2 * MAX_FRAME_LENGTH ]; - SKP_int16 sLTP[ 2 * MAX_FRAME_LENGTH ]; - SKP_int32 HarmShapeFIRPacked_Q14; - SKP_int offset_Q10; - SKP_int32 FiltState[ MAX_LPC_ORDER ], RDmin_Q10; - SKP_int32 x_sc_Q10[ MAX_FRAME_LENGTH / NB_SUBFR ]; - NSQ_del_dec_struct psDelDec[ DEL_DEC_STATES_MAX ]; - NSQ_del_dec_struct *psDD; - - subfr_length = psEncC->frame_length / NB_SUBFR; - - /* Set unvoiced lag to the previous one, overwrite later for voiced */ - lag = NSQ->lagPrev; - - SKP_assert( NSQ->prev_inv_gain_Q16 != 0 ); - - /* Initialize delayed decision states */ - SKP_memset( psDelDec, 0, psEncC->nStatesDelayedDecision * sizeof( NSQ_del_dec_struct ) ); - for( k = 0; k < psEncC->nStatesDelayedDecision; k++ ) { - psDD = &psDelDec[ k ]; - psDD->Seed = ( k + psEncCtrlC->Seed ) & 3; - psDD->SeedInit = psDD->Seed; - psDD->RD_Q10 = 0; - psDD->LF_AR_Q12 = NSQ->sLF_AR_shp_Q12; - psDD->Shape_Q10[ 0 ] = NSQ->sLTP_shp_Q10[ psEncC->frame_length - 1 ]; - SKP_memcpy( psDD->sLPC_Q14, NSQ->sLPC_Q14, NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); - } - - offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psEncCtrlC->sigtype ][ psEncCtrlC->QuantOffsetType ]; - smpl_buf_idx = 0; /* index of oldest samples */ - - decisionDelay = SKP_min_int( DECISION_DELAY, subfr_length ); - /* For voiced frames limit the decision delay to lower than the pitch lag */ - if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { - for( k = 0; k < NB_SUBFR; k++ ) { - decisionDelay = SKP_min_int( decisionDelay, psEncCtrlC->pitchL[ k ] - LTP_ORDER / 2 - 1 ); - } - } - - if( LSFInterpFactor_Q2 == ( 1 << 2 ) ) { - LSF_interpolation_flag = 0; - } else { - LSF_interpolation_flag = 1; - } - - /* Setup pointers to start of sub frame */ - pxq = &NSQ->xq[ psEncC->frame_length ]; - NSQ->sLTP_shp_buf_idx = psEncC->frame_length; - NSQ->sLTP_buf_idx = psEncC->frame_length; - subfr = 0; - for( k = 0; k < NB_SUBFR; k++ ) { - A_Q12 = &PredCoef_Q12[ ( ( k >> 1 ) | ( 1 - LSF_interpolation_flag ) ) * MAX_LPC_ORDER ]; - B_Q14 = <PCoef_Q14[ k * LTP_ORDER ]; - AR_shp_Q13 = &AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ]; - - NSQ->rewhite_flag = 0; - if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { - /* Voiced */ - lag = psEncCtrlC->pitchL[ k ]; - - /* Re-whitening */ - if( ( k & ( 3 - SKP_LSHIFT( LSF_interpolation_flag, 1 ) ) ) == 0 ) { - if( k == 2 ) { - /* RESET DELAYED DECISIONS */ - /* Find winner */ - RDmin_Q10 = psDelDec[ 0 ].RD_Q10; - Winner_ind = 0; - for( i = 1; i < psEncC->nStatesDelayedDecision; i++ ) { - if( psDelDec[ i ].RD_Q10 < RDmin_Q10 ) { - RDmin_Q10 = psDelDec[ i ].RD_Q10; - Winner_ind = i; - } - } - for( i = 0; i < psEncC->nStatesDelayedDecision; i++ ) { - if( i != Winner_ind ) { - psDelDec[ i ].RD_Q10 += ( SKP_int32_MAX >> 4 ); - SKP_assert( psDelDec[ i ].RD_Q10 >= 0 ); - } - } - - /* Copy final part of signals from winner state to output and long-term filter states */ - psDD = &psDelDec[ Winner_ind ]; - last_smple_idx = smpl_buf_idx + decisionDelay; - for( i = 0; i < decisionDelay; i++ ) { - last_smple_idx = ( last_smple_idx - 1 ) & DECISION_DELAY_MASK; - q[ i - decisionDelay ] = ( SKP_int )SKP_RSHIFT( psDD->Q_Q10[ last_smple_idx ], 10 ); - pxq[ i - decisionDelay ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( - SKP_SMULWW( psDD->Xq_Q10[ last_smple_idx ], - psDD->Gain_Q16[ last_smple_idx ] ), 10 ) ); - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q10[ last_smple_idx ]; - } - - subfr = 0; - } - - /* Rewhiten with new A coefs */ - start_idx = psEncC->frame_length - lag - psEncC->predictLPCOrder - LTP_ORDER / 2; - start_idx = SKP_LIMIT( start_idx, 0, psEncC->frame_length - psEncC->predictLPCOrder ); - - SKP_memset( FiltState, 0, psEncC->predictLPCOrder * sizeof( SKP_int32 ) ); - SKP_Silk_MA_Prediction( &NSQ->xq[ start_idx + k * psEncC->subfr_length ], - A_Q12, FiltState, sLTP + start_idx, psEncC->frame_length - start_idx, psEncC->predictLPCOrder ); - - NSQ->sLTP_buf_idx = psEncC->frame_length; - NSQ->rewhite_flag = 1; - } - } - - /* Noise shape parameters */ - SKP_assert( HarmShapeGain_Q14[ k ] >= 0 ); - HarmShapeFIRPacked_Q14 = SKP_RSHIFT( HarmShapeGain_Q14[ k ], 2 ); - HarmShapeFIRPacked_Q14 |= SKP_LSHIFT( ( SKP_int32 )SKP_RSHIFT( HarmShapeGain_Q14[ k ], 1 ), 16 ); - - SKP_Silk_nsq_del_dec_scale_states( NSQ, psDelDec, x, x_sc_Q10, - subfr_length, sLTP, sLTP_Q16, k, psEncC->nStatesDelayedDecision, smpl_buf_idx, - LTP_scale_Q14, Gains_Q16, psEncCtrlC->pitchL ); - - SKP_Silk_noise_shape_quantizer_del_dec( NSQ, psDelDec, psEncCtrlC->sigtype, x_sc_Q10, q, pxq, sLTP_Q16, - A_Q12, B_Q14, AR_shp_Q13, lag, HarmShapeFIRPacked_Q14, Tilt_Q14[ k ], LF_shp_Q14[ k ], Gains_Q16[ k ], - Lambda_Q10, offset_Q10, psEncC->subfr_length, subfr++, psEncC->shapingLPCOrder, psEncC->predictLPCOrder, - psEncC->nStatesDelayedDecision, &smpl_buf_idx, decisionDelay - ); - - x += psEncC->subfr_length; - q += psEncC->subfr_length; - pxq += psEncC->subfr_length; - } - - /* Find winner */ - RDmin_Q10 = psDelDec[ 0 ].RD_Q10; - Winner_ind = 0; - for( k = 1; k < psEncC->nStatesDelayedDecision; k++ ) { - if( psDelDec[ k ].RD_Q10 < RDmin_Q10 ) { - RDmin_Q10 = psDelDec[ k ].RD_Q10; - Winner_ind = k; - } - } - - /* Copy final part of signals from winner state to output and long-term filter states */ - psDD = &psDelDec[ Winner_ind ]; - psEncCtrlC->Seed = psDD->SeedInit; - last_smple_idx = smpl_buf_idx + decisionDelay; - for( i = 0; i < decisionDelay; i++ ) { - last_smple_idx = ( last_smple_idx - 1 ) & DECISION_DELAY_MASK; - q[ i - decisionDelay ] = ( SKP_int )SKP_RSHIFT( psDD->Q_Q10[ last_smple_idx ], 10 ); - pxq[ i - decisionDelay ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( - SKP_SMULWW( psDD->Xq_Q10[ last_smple_idx ], psDD->Gain_Q16[ last_smple_idx ] ), 10 ) ); - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q10[ last_smple_idx ]; - sLTP_Q16[ NSQ->sLTP_buf_idx - decisionDelay + i ] = psDD->Pred_Q16[ last_smple_idx ]; - - } - SKP_memcpy( NSQ->sLPC_Q14, &psDD->sLPC_Q14[ psEncC->subfr_length ], NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); - - /* Update states */ - NSQ->sLF_AR_shp_Q12 = psDD->LF_AR_Q12; - NSQ->prev_inv_gain_Q16 = NSQ->prev_inv_gain_Q16; - NSQ->lagPrev = psEncCtrlC->pitchL[ NB_SUBFR - 1 ]; - - /* Save quantized speech and noise shaping signals */ - SKP_memcpy( NSQ->xq, &NSQ->xq[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int16 ) ); - SKP_memcpy( NSQ->sLTP_shp_Q10, &NSQ->sLTP_shp_Q10[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int32 ) ); - -} - -/******************************************/ -/* Noise shape quantizer for one subframe */ -/******************************************/ -SKP_INLINE void SKP_Silk_noise_shape_quantizer_del_dec( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ - SKP_int sigtype, /* I Signal type */ - const SKP_int32 x_Q10[], /* I */ - SKP_int q[], /* O */ - SKP_int16 xq[], /* O */ - SKP_int32 sLTP_Q16[], /* I/O LTP filter state */ - const SKP_int16 a_Q12[], /* I Short term prediction coefs */ - const SKP_int16 b_Q14[], /* I Long term prediction coefs */ - const SKP_int16 AR_shp_Q13[], /* I Noise shaping coefs */ - SKP_int lag, /* I Pitch lag */ - SKP_int32 HarmShapeFIRPacked_Q14, /* I */ - SKP_int Tilt_Q14, /* I Spectral tilt */ - SKP_int32 LF_shp_Q14, /* I */ - SKP_int32 Gain_Q16, /* I */ - SKP_int Lambda_Q10, /* I */ - SKP_int offset_Q10, /* I */ - SKP_int length, /* I Input length */ - SKP_int subfr, /* I Subframe number */ - SKP_int shapingLPCOrder, /* I Shaping LPC filter order */ - SKP_int predictLPCOrder, /* I Prediction LPC filter order */ - SKP_int nStatesDelayedDecision, /* I Number of states in decision tree */ - SKP_int *smpl_buf_idx, /* I Index to newest samples in buffers */ - SKP_int decisionDelay /* I */ -) -{ - SKP_int i, j, k, Winner_ind, RDmin_ind, RDmax_ind, last_smple_idx; - SKP_int32 Winner_rand_state; - SKP_int32 LTP_pred_Q14, LPC_pred_Q10, n_AR_Q10, n_LTP_Q14; - SKP_int32 n_LF_Q10; - SKP_int32 r_Q10, rr_Q20, rd1_Q10, rd2_Q10, RDmin_Q10, RDmax_Q10; - SKP_int32 q1_Q10, q2_Q10; - SKP_int32 Atmp, dither; - SKP_int32 exc_Q10, LPC_exc_Q10, xq_Q10; - SKP_int32 tmp, sLF_AR_shp_Q10; - SKP_int32 *pred_lag_ptr, *shp_lag_ptr; - SKP_int32 *psLPC_Q14; - SKP_int32 a_Q12_tmp[ MAX_LPC_ORDER / 2 ], AR_shp_Q13_tmp[ MAX_LPC_ORDER / 2 ]; - NSQ_sample_struct psSampleState[ DEL_DEC_STATES_MAX ][ 2 ]; - NSQ_del_dec_struct *psDD; - NSQ_sample_struct *psSS; - - shp_lag_ptr = &NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; - pred_lag_ptr = &sLTP_Q16[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; - - /* Preload LPC coeficients to array on stack. Gives small performance gain */ - SKP_memcpy( a_Q12_tmp, a_Q12, predictLPCOrder * sizeof( SKP_int16 ) ); - SKP_memcpy( AR_shp_Q13_tmp, AR_shp_Q13, shapingLPCOrder * sizeof( SKP_int16 ) ); - - for( i = 0; i < length; i++ ) { - /* Perform common calculations used in all states */ - - /* Long-term prediction */ - if( sigtype == SIG_TYPE_VOICED ) { - /* Unrolled loop */ - LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], b_Q14[ 0 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], b_Q14[ 1 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], b_Q14[ 2 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], b_Q14[ 3 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], b_Q14[ 4 ] ); - pred_lag_ptr++; - } else { - LTP_pred_Q14 = 0; - } - - /* Long-term shaping */ - if( lag > 0 ) { - /* Symmetric, packed FIR coefficients */ - n_LTP_Q14 = SKP_SMULWB( SKP_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); - n_LTP_Q14 = SKP_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); - n_LTP_Q14 = SKP_LSHIFT( n_LTP_Q14, 6 ); - shp_lag_ptr++; - } else { - n_LTP_Q14 = 0; - } - - for( k = 0; k < nStatesDelayedDecision; k++ ) { - /* Delayed decision state */ - psDD = &psDelDec[ k ]; - - /* Sample state */ - psSS = psSampleState[ k ]; - - /* Generate dither */ - psDD->Seed = SKP_RAND( psDD->Seed ); - - /* dither = rand_seed < 0 ? 0xFFFFFFFF : 0; */ - dither = SKP_RSHIFT( psDD->Seed, 31 ); - - /* Pointer used in short term prediction and shaping */ - psLPC_Q14 = &psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - 1 + i ]; - /* Short-term prediction */ - SKP_assert( ( predictLPCOrder & 1 ) == 0 ); /* check that order is even */ - SKP_assert( ( (SKP_int64)a_Q12 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ - SKP_assert( predictLPCOrder >= 10 ); /* check that unrolling works */ - - /* Partially unrolled */ - Atmp = a_Q12_tmp[ 0 ]; /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -1 ], Atmp ); - Atmp = a_Q12_tmp[ 1 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -2 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -3 ], Atmp ); - Atmp = a_Q12_tmp[ 2 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -4 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -5 ], Atmp ); - Atmp = a_Q12_tmp[ 3 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -6 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -7 ], Atmp ); - Atmp = a_Q12_tmp[ 4 ]; - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -8 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -9 ], Atmp ); - for( j = 10; j < predictLPCOrder; j += 2 ) { - Atmp = a_Q12_tmp[ j >> 1 ]; /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -j ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -j - 1 ], Atmp ); - } - - /* Noise shape feedback */ - SKP_assert( ( shapingLPCOrder & 1 ) == 0 ); /* check that order is even */ - SKP_assert( ( (SKP_int64)AR_shp_Q13 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ - SKP_assert( shapingLPCOrder >= 12 ); /* check that unrolling works */ - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLAWB and SMLAWT instructions should solve the problem. */ - - /* Partially unrolled */ - Atmp = AR_shp_Q13_tmp[ 0 ]; /* read two coefficients at once */ - n_AR_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -1 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 1 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -2 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -3 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 2 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -4 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -5 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 3 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -6 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -7 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 4 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -8 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -9 ], Atmp ); - Atmp = AR_shp_Q13_tmp[ 5 ]; - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -10 ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -11 ], Atmp ); - for( j = 12; j < shapingLPCOrder; j += 2 ) { - Atmp = AR_shp_Q13_tmp[ j >> 1 ]; /* read two coefficients at once */ - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -j ], Atmp ); - n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -j - 1 ], Atmp ); - } - n_AR_Q10 = SKP_RSHIFT( n_AR_Q10, 1 ); /* Q11 -> Q10 */ - n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psDD->LF_AR_Q12, Tilt_Q14 ); - - n_LF_Q10 = SKP_LSHIFT( SKP_SMULWB( psDD->Shape_Q10[ *smpl_buf_idx ], LF_shp_Q14 ), 2 ); - n_LF_Q10 = SKP_SMLAWT( n_LF_Q10, psDD->LF_AR_Q12, LF_shp_Q14 ); - - /* Input minus prediction plus noise feedback */ - /* r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP */ - tmp = SKP_SUB32( LTP_pred_Q14, n_LTP_Q14 ); /* Add Q14 stuff */ - tmp = SKP_RSHIFT_ROUND( tmp, 4 ); /* round to Q10 */ - tmp = SKP_ADD32( tmp, LPC_pred_Q10 ); /* add Q10 stuff */ - tmp = SKP_SUB32( tmp, n_AR_Q10 ); /* subtract Q10 stuff */ - tmp = SKP_SUB32( tmp, n_LF_Q10 ); /* subtract Q10 stuff */ - r_Q10 = SKP_SUB32( x_Q10[ i ], tmp ); /* residual error Q10 */ - - - /* Flip sign depending on dither */ - r_Q10 = ( r_Q10 ^ dither ) - dither; - r_Q10 = SKP_SUB32( r_Q10, offset_Q10 ); - r_Q10 = SKP_LIMIT( r_Q10, -64 << 10, 64 << 10 ); - - /* Find two quantization level candidates and measure their rate-distortion */ - if( r_Q10 < -1536 ) { - q1_Q10 = SKP_LSHIFT( SKP_RSHIFT_ROUND( r_Q10, 10 ), 10 ); - r_Q10 = SKP_SUB32( r_Q10, q1_Q10 ); - rd1_Q10 = SKP_RSHIFT( SKP_SMLABB( SKP_MUL( -SKP_ADD32( q1_Q10, offset_Q10 ), Lambda_Q10 ), r_Q10, r_Q10 ), 10 ); - rd2_Q10 = SKP_ADD32( rd1_Q10, 1024 ); - rd2_Q10 = SKP_SUB32( rd2_Q10, SKP_ADD_LSHIFT32( Lambda_Q10, r_Q10, 1 ) ); - q2_Q10 = SKP_ADD32( q1_Q10, 1024 ); - } else if( r_Q10 > 512 ) { - q1_Q10 = SKP_LSHIFT( SKP_RSHIFT_ROUND( r_Q10, 10 ), 10 ); - r_Q10 = SKP_SUB32( r_Q10, q1_Q10 ); - rd1_Q10 = SKP_RSHIFT( SKP_SMLABB( SKP_MUL( SKP_ADD32( q1_Q10, offset_Q10 ), Lambda_Q10 ), r_Q10, r_Q10 ), 10 ); - rd2_Q10 = SKP_ADD32( rd1_Q10, 1024 ); - rd2_Q10 = SKP_SUB32( rd2_Q10, SKP_SUB_LSHIFT32( Lambda_Q10, r_Q10, 1 ) ); - q2_Q10 = SKP_SUB32( q1_Q10, 1024 ); - } else { /* r_Q10 >= -1536 && q1_Q10 <= 512 */ - rr_Q20 = SKP_SMULBB( offset_Q10, Lambda_Q10 ); - rd2_Q10 = SKP_RSHIFT( SKP_SMLABB( rr_Q20, r_Q10, r_Q10 ), 10 ); - rd1_Q10 = SKP_ADD32( rd2_Q10, 1024 ); - rd1_Q10 = SKP_ADD32( rd1_Q10, SKP_SUB_RSHIFT32( SKP_ADD_LSHIFT32( Lambda_Q10, r_Q10, 1 ), rr_Q20, 9 ) ); - q1_Q10 = -1024; - q2_Q10 = 0; - } - - if( rd1_Q10 < rd2_Q10 ) { - psSS[ 0 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd1_Q10 ); - psSS[ 1 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd2_Q10 ); - psSS[ 0 ].Q_Q10 = q1_Q10; - psSS[ 1 ].Q_Q10 = q2_Q10; - } else { - psSS[ 0 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd2_Q10 ); - psSS[ 1 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd1_Q10 ); - psSS[ 0 ].Q_Q10 = q2_Q10; - psSS[ 1 ].Q_Q10 = q1_Q10; - } - - /* Update states for best quantization */ - - /* Quantized excitation */ - exc_Q10 = SKP_ADD32( offset_Q10, psSS[ 0 ].Q_Q10 ); - exc_Q10 = ( exc_Q10 ^ dither ) - dither; - - /* Add predictions */ - LPC_exc_Q10 = exc_Q10 + SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ); - xq_Q10 = SKP_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); - - /* Update states */ - sLF_AR_shp_Q10 = SKP_SUB32( xq_Q10, n_AR_Q10 ); - psSS[ 0 ].sLTP_shp_Q10 = SKP_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); - psSS[ 0 ].LF_AR_Q12 = SKP_LSHIFT( sLF_AR_shp_Q10, 2 ); - psSS[ 0 ].xq_Q14 = SKP_LSHIFT( xq_Q10, 4 ); - psSS[ 0 ].LPC_exc_Q16 = SKP_LSHIFT( LPC_exc_Q10, 6 ); - - /* Update states for second best quantization */ - - /* Quantized excitation */ - exc_Q10 = SKP_ADD32( offset_Q10, psSS[ 1 ].Q_Q10 ); - exc_Q10 = ( exc_Q10 ^ dither ) - dither; - - /* Add predictions */ - LPC_exc_Q10 = exc_Q10 + SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ); - xq_Q10 = SKP_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); - - /* Update states */ - sLF_AR_shp_Q10 = SKP_SUB32( xq_Q10, n_AR_Q10 ); - psSS[ 1 ].sLTP_shp_Q10 = SKP_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); - psSS[ 1 ].LF_AR_Q12 = SKP_LSHIFT( sLF_AR_shp_Q10, 2 ); - psSS[ 1 ].xq_Q14 = SKP_LSHIFT( xq_Q10, 4 ); - psSS[ 1 ].LPC_exc_Q16 = SKP_LSHIFT( LPC_exc_Q10, 6 ); - } - - *smpl_buf_idx = ( *smpl_buf_idx - 1 ) & DECISION_DELAY_MASK; /* Index to newest samples */ - last_smple_idx = ( *smpl_buf_idx + decisionDelay ) & DECISION_DELAY_MASK; /* Index to decisionDelay old samples */ - - /* Find winner */ - RDmin_Q10 = psSampleState[ 0 ][ 0 ].RD_Q10; - Winner_ind = 0; - for( k = 1; k < nStatesDelayedDecision; k++ ) { - if( psSampleState[ k ][ 0 ].RD_Q10 < RDmin_Q10 ) { - RDmin_Q10 = psSampleState[ k ][ 0 ].RD_Q10; - Winner_ind = k; - } - } - - /* Increase RD values of expired states */ - Winner_rand_state = psDelDec[ Winner_ind ].RandState[ last_smple_idx ]; - for( k = 0; k < nStatesDelayedDecision; k++ ) { - if( psDelDec[ k ].RandState[ last_smple_idx ] != Winner_rand_state ) { - psSampleState[ k ][ 0 ].RD_Q10 = SKP_ADD32( psSampleState[ k ][ 0 ].RD_Q10, ( SKP_int32_MAX >> 4 ) ); - psSampleState[ k ][ 1 ].RD_Q10 = SKP_ADD32( psSampleState[ k ][ 1 ].RD_Q10, ( SKP_int32_MAX >> 4 ) ); - SKP_assert( psSampleState[ k ][ 0 ].RD_Q10 >= 0 ); - } - } - - /* Find worst in first set and best in second set */ - RDmax_Q10 = psSampleState[ 0 ][ 0 ].RD_Q10; - RDmin_Q10 = psSampleState[ 0 ][ 1 ].RD_Q10; - RDmax_ind = 0; - RDmin_ind = 0; - for( k = 1; k < nStatesDelayedDecision; k++ ) { - /* find worst in first set */ - if( psSampleState[ k ][ 0 ].RD_Q10 > RDmax_Q10 ) { - RDmax_Q10 = psSampleState[ k ][ 0 ].RD_Q10; - RDmax_ind = k; - } - /* find best in second set */ - if( psSampleState[ k ][ 1 ].RD_Q10 < RDmin_Q10 ) { - RDmin_Q10 = psSampleState[ k ][ 1 ].RD_Q10; - RDmin_ind = k; - } - } - - /* Replace a state if best from second set outperforms worst in first set */ - if( RDmin_Q10 < RDmax_Q10 ) { - SKP_Silk_copy_del_dec_state( &psDelDec[ RDmax_ind ], &psDelDec[ RDmin_ind ], i ); - SKP_memcpy( &psSampleState[ RDmax_ind ][ 0 ], &psSampleState[ RDmin_ind ][ 1 ], sizeof( NSQ_sample_struct ) ); - } - - /* Write samples from winner to output and long-term filter states */ - psDD = &psDelDec[ Winner_ind ]; - if( subfr > 0 || i >= decisionDelay ) { - q[ i - decisionDelay ] = ( SKP_int )SKP_RSHIFT( psDD->Q_Q10[ last_smple_idx ], 10 ); - xq[ i - decisionDelay ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( - SKP_SMULWW( psDD->Xq_Q10[ last_smple_idx ], psDD->Gain_Q16[ last_smple_idx ] ), 10 ) ); - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay ] = psDD->Shape_Q10[ last_smple_idx ]; - sLTP_Q16[ NSQ->sLTP_buf_idx - decisionDelay ] = psDD->Pred_Q16[ last_smple_idx ]; - } - NSQ->sLTP_shp_buf_idx++; - NSQ->sLTP_buf_idx++; - - /* Update states */ - for( k = 0; k < nStatesDelayedDecision; k++ ) { - psDD = &psDelDec[ k ]; - psSS = &psSampleState[ k ][ 0 ]; - psDD->LF_AR_Q12 = psSS->LF_AR_Q12; - psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH + i ] = psSS->xq_Q14; - psDD->Xq_Q10[ *smpl_buf_idx ] = SKP_RSHIFT( psSS->xq_Q14, 4 ); - psDD->Q_Q10[ *smpl_buf_idx ] = psSS->Q_Q10; - psDD->Pred_Q16[ *smpl_buf_idx ] = psSS->LPC_exc_Q16; - psDD->Shape_Q10[ *smpl_buf_idx ] = psSS->sLTP_shp_Q10; - psDD->Seed = SKP_ADD_RSHIFT32( psDD->Seed, psSS->Q_Q10, 10 ); - psDD->RandState[ *smpl_buf_idx ] = psDD->Seed; - psDD->RD_Q10 = psSS->RD_Q10; - psDD->Gain_Q16[ *smpl_buf_idx ] = Gain_Q16; - } - } - /* Update LPC states */ - for( k = 0; k < nStatesDelayedDecision; k++ ) { - psDD = &psDelDec[ k ]; - SKP_memcpy( psDD->sLPC_Q14, &psDD->sLPC_Q14[ length ], NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); - } -} - -SKP_INLINE void SKP_Silk_nsq_del_dec_scale_states( - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ - const SKP_int16 x[], /* I Input in Q0 */ - SKP_int32 x_sc_Q10[], /* O Input scaled with 1/Gain in Q10 */ - SKP_int length, /* I Length of input */ - SKP_int16 sLTP[], /* I Re-whitened LTP state in Q0 */ - SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ - SKP_int subfr, /* I Subframe number */ - SKP_int nStatesDelayedDecision, /* I Number of del dec states */ - SKP_int smpl_buf_idx, /* I Index to newest samples in buffers */ - const SKP_int LTP_scale_Q14, /* I LTP state scaling */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int pitchL[ NB_SUBFR ] /* I Pitch lag */ -) -{ - SKP_int i, k, scale_length, lag; - SKP_int32 inv_gain_Q16, gain_adj_Q16, inv_gain_Q32; - NSQ_del_dec_struct *psDD; - - inv_gain_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( Gains_Q16[ subfr ], 1 ) ); - inv_gain_Q16 = SKP_min( inv_gain_Q16, SKP_int16_MAX ); - lag = pitchL[ subfr ]; - /* After rewhitening the LTP state is un-scaled. So scale with inv_gain_Q16 */ - if( NSQ->rewhite_flag ) { - inv_gain_Q32 = SKP_LSHIFT( inv_gain_Q16, 16 ); - if( subfr == 0 ) { - /* Do LTP downscaling */ - inv_gain_Q32 = SKP_LSHIFT( SKP_SMULWB( inv_gain_Q32, LTP_scale_Q14 ), 2 ); - } - for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { - SKP_assert( i < MAX_FRAME_LENGTH ); - sLTP_Q16[ i ] = SKP_SMULWB( inv_gain_Q32, sLTP[ i ] ); - } - } - - /* Adjust for changing gain */ - if( inv_gain_Q16 != NSQ->prev_inv_gain_Q16 ) { - gain_adj_Q16 = SKP_DIV32_varQ( inv_gain_Q16, NSQ->prev_inv_gain_Q16, 16 ); - - for( k = 0; k < nStatesDelayedDecision; k++ ) { - psDD = &psDelDec[ k ]; - - /* Scale scalar states */ - psDD->LF_AR_Q12 = SKP_SMULWW( gain_adj_Q16, psDD->LF_AR_Q12 ); - - /* scale short term state */ - for( i = 0; i < NSQ_LPC_BUF_LENGTH; i++ ) { - psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - i - 1 ] = SKP_SMULWW( gain_adj_Q16, psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - i - 1 ] ); - } - for( i = 0; i < DECISION_DELAY; i++ ) { - psDD->Pred_Q16[ i ] = SKP_SMULWW( gain_adj_Q16, psDD->Pred_Q16[ i ] ); - psDD->Shape_Q10[ i ] = SKP_SMULWW( gain_adj_Q16, psDD->Shape_Q10[ i ] ); - } - } - - /* Scale long term shaping state */ - - /* Calculate length to be scaled, Worst case: Next frame is voiced with max lag */ - scale_length = length * NB_SUBFR; /* aprox max lag */ - scale_length = scale_length - SKP_SMULBB( NB_SUBFR - ( subfr + 1 ), length ); /* subtract samples that will be too old in next frame */ - scale_length = SKP_max_int( scale_length, lag + LTP_ORDER ); /* make sure to scale whole pitch period if voiced */ - - for( i = NSQ->sLTP_shp_buf_idx - scale_length; i < NSQ->sLTP_shp_buf_idx; i++ ) { - NSQ->sLTP_shp_Q10[ i ] = SKP_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q10[ i ] ); - } - - /* Scale LTP predict state */ - if( NSQ->rewhite_flag == 0 ) { - for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { - sLTP_Q16[ i ] = SKP_SMULWW( gain_adj_Q16, sLTP_Q16[ i ] ); - } - } - } - - /* Scale input */ - for( i = 0; i < length; i++ ) { - x_sc_Q10[ i ] = SKP_RSHIFT( SKP_SMULBB( x[ i ], ( SKP_int16 )inv_gain_Q16 ), 6 ); - } - - /* save inv_gain */ - SKP_assert( inv_gain_Q16 != 0 ); - NSQ->prev_inv_gain_Q16 = inv_gain_Q16; -} - -SKP_INLINE void SKP_Silk_copy_del_dec_state( - NSQ_del_dec_struct *DD_dst, /* I Dst del dec state */ - NSQ_del_dec_struct *DD_src, /* I Src del dec state */ - SKP_int LPC_state_idx /* I Index to LPC buffer */ -) -{ - SKP_memcpy( DD_dst->RandState, DD_src->RandState, DECISION_DELAY * sizeof( SKP_int ) ); - SKP_memcpy( DD_dst->Q_Q10, DD_src->Q_Q10, DECISION_DELAY * sizeof( SKP_int32 ) ); - SKP_memcpy( DD_dst->Pred_Q16, DD_src->Pred_Q16, DECISION_DELAY * sizeof( SKP_int32 ) ); - SKP_memcpy( DD_dst->Shape_Q10, DD_src->Shape_Q10, DECISION_DELAY * sizeof( SKP_int32 ) ); - SKP_memcpy( DD_dst->Xq_Q10, DD_src->Xq_Q10, DECISION_DELAY * sizeof( SKP_int32 ) ); - - SKP_memcpy( &DD_dst->sLPC_Q14[ LPC_state_idx ], &DD_src->sLPC_Q14[ LPC_state_idx ], NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); - DD_dst->LF_AR_Q12 = DD_src->LF_AR_Q12; - DD_dst->Seed = DD_src->Seed; - DD_dst->SeedInit = DD_src->SeedInit; - DD_dst->RD_Q10 = DD_src->RD_Q10; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +typedef struct { + SKP_int RandState[ DECISION_DELAY ]; + SKP_int32 Q_Q10[ DECISION_DELAY ]; + SKP_int32 Xq_Q10[ DECISION_DELAY ]; + SKP_int32 Pred_Q16[ DECISION_DELAY ]; + SKP_int32 Shape_Q10[ DECISION_DELAY ]; + SKP_int32 Gain_Q16[ DECISION_DELAY ]; + SKP_int32 sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + NSQ_LPC_BUF_LENGTH ]; + SKP_int32 LF_AR_Q12; + SKP_int32 Seed; + SKP_int32 SeedInit; + SKP_int32 RD_Q10; +} NSQ_del_dec_struct; + +typedef struct { + SKP_int32 Q_Q10; + SKP_int32 RD_Q10; + SKP_int32 xq_Q14; + SKP_int32 LF_AR_Q12; + SKP_int32 sLTP_shp_Q10; + SKP_int32 LPC_exc_Q16; +} NSQ_sample_struct; + +SKP_INLINE void SKP_Silk_copy_del_dec_state( + NSQ_del_dec_struct *DD_dst, /* I Dst del dec state */ + NSQ_del_dec_struct *DD_src, /* I Src del dec state */ + SKP_int LPC_state_idx /* I Index to LPC buffer */ +); + +SKP_INLINE void SKP_Silk_nsq_del_dec_scale_states( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ + const SKP_int16 x[], /* I Input in Q0 */ + SKP_int32 x_sc_Q10[], /* O Input scaled with 1/Gain in Q10 */ + SKP_int length, /* I Length of input */ + SKP_int16 sLTP[], /* I Re-whitened LTP state in Q0 */ + SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ + SKP_int subfr, /* I Subframe number */ + SKP_int nStatesDelayedDecision, /* I Number of del dec states */ + SKP_int smpl_buf_idx, /* I Index to newest samples in buffers */ + const SKP_int LTP_scale_Q14, /* I LTP state scaling */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int pitchL[ NB_SUBFR ] /* I Pitch lag */ +); + +/******************************************/ +/* Noise shape quantizer for one subframe */ +/******************************************/ +SKP_INLINE void SKP_Silk_noise_shape_quantizer_del_dec( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ + SKP_int sigtype, /* I Signal type */ + const SKP_int32 x_Q10[], /* I */ + SKP_int q[], /* O */ + SKP_int16 xq[], /* O */ + SKP_int32 sLTP_Q16[], /* I/O LTP filter state */ + const SKP_int16 a_Q12[], /* I Short term prediction coefs */ + const SKP_int16 b_Q14[], /* I Long term prediction coefs */ + const SKP_int16 AR_shp_Q13[], /* I Noise shaping coefs */ + SKP_int lag, /* I Pitch lag */ + SKP_int32 HarmShapeFIRPacked_Q14, /* I */ + SKP_int Tilt_Q14, /* I Spectral tilt */ + SKP_int32 LF_shp_Q14, /* I */ + SKP_int32 Gain_Q16, /* I */ + SKP_int Lambda_Q10, /* I */ + SKP_int offset_Q10, /* I */ + SKP_int length, /* I Input length */ + SKP_int subfr, /* I Subframe number */ + SKP_int shapingLPCOrder, /* I Shaping LPC filter order */ + SKP_int predictLPCOrder, /* I Prediction LPC filter order */ + SKP_int nStatesDelayedDecision, /* I Number of states in decision tree */ + SKP_int *smpl_buf_idx, /* I Index to newest samples in buffers */ + SKP_int decisionDelay /* I */ +); + +void SKP_Silk_NSQ_del_dec( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ + SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + const SKP_int16 x[], /* I Prefiltered input signal */ + SKP_int q[], /* O Quantized pulse signal */ + const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ + const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Prediction coefs */ + const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I LT prediction coefs */ + const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ + const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ + const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ + const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int Lambda_Q10, /* I */ + const SKP_int LTP_scale_Q14 /* I LTP state scaling */ +) +{ + SKP_int i, k, lag, start_idx, LSF_interpolation_flag, Winner_ind, subfr; + SKP_int last_smple_idx, smpl_buf_idx, decisionDelay, subfr_length; + const SKP_int16 *A_Q12, *B_Q14, *AR_shp_Q13; + SKP_int16 *pxq; + SKP_int32 sLTP_Q16[ 2 * MAX_FRAME_LENGTH ]; + SKP_int16 sLTP[ 2 * MAX_FRAME_LENGTH ]; + SKP_int32 HarmShapeFIRPacked_Q14; + SKP_int offset_Q10; + SKP_int32 FiltState[ MAX_LPC_ORDER ], RDmin_Q10; + SKP_int32 x_sc_Q10[ MAX_FRAME_LENGTH / NB_SUBFR ]; + NSQ_del_dec_struct psDelDec[ DEL_DEC_STATES_MAX ]; + NSQ_del_dec_struct *psDD; + + subfr_length = psEncC->frame_length / NB_SUBFR; + + /* Set unvoiced lag to the previous one, overwrite later for voiced */ + lag = NSQ->lagPrev; + + SKP_assert( NSQ->prev_inv_gain_Q16 != 0 ); + + /* Initialize delayed decision states */ + SKP_memset( psDelDec, 0, psEncC->nStatesDelayedDecision * sizeof( NSQ_del_dec_struct ) ); + for( k = 0; k < psEncC->nStatesDelayedDecision; k++ ) { + psDD = &psDelDec[ k ]; + psDD->Seed = ( k + psEncCtrlC->Seed ) & 3; + psDD->SeedInit = psDD->Seed; + psDD->RD_Q10 = 0; + psDD->LF_AR_Q12 = NSQ->sLF_AR_shp_Q12; + psDD->Shape_Q10[ 0 ] = NSQ->sLTP_shp_Q10[ psEncC->frame_length - 1 ]; + SKP_memcpy( psDD->sLPC_Q14, NSQ->sLPC_Q14, NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); + } + + offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psEncCtrlC->sigtype ][ psEncCtrlC->QuantOffsetType ]; + smpl_buf_idx = 0; /* index of oldest samples */ + + decisionDelay = SKP_min_int( DECISION_DELAY, subfr_length ); + /* For voiced frames limit the decision delay to lower than the pitch lag */ + if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { + for( k = 0; k < NB_SUBFR; k++ ) { + decisionDelay = SKP_min_int( decisionDelay, psEncCtrlC->pitchL[ k ] - LTP_ORDER / 2 - 1 ); + } + } + + if( LSFInterpFactor_Q2 == ( 1 << 2 ) ) { + LSF_interpolation_flag = 0; + } else { + LSF_interpolation_flag = 1; + } + + /* Setup pointers to start of sub frame */ + pxq = &NSQ->xq[ psEncC->frame_length ]; + NSQ->sLTP_shp_buf_idx = psEncC->frame_length; + NSQ->sLTP_buf_idx = psEncC->frame_length; + subfr = 0; + for( k = 0; k < NB_SUBFR; k++ ) { + A_Q12 = &PredCoef_Q12[ ( ( k >> 1 ) | ( 1 - LSF_interpolation_flag ) ) * MAX_LPC_ORDER ]; + B_Q14 = <PCoef_Q14[ k * LTP_ORDER ]; + AR_shp_Q13 = &AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ]; + + NSQ->rewhite_flag = 0; + if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { + /* Voiced */ + lag = psEncCtrlC->pitchL[ k ]; + + /* Re-whitening */ + if( ( k & ( 3 - SKP_LSHIFT( LSF_interpolation_flag, 1 ) ) ) == 0 ) { + if( k == 2 ) { + /* RESET DELAYED DECISIONS */ + /* Find winner */ + RDmin_Q10 = psDelDec[ 0 ].RD_Q10; + Winner_ind = 0; + for( i = 1; i < psEncC->nStatesDelayedDecision; i++ ) { + if( psDelDec[ i ].RD_Q10 < RDmin_Q10 ) { + RDmin_Q10 = psDelDec[ i ].RD_Q10; + Winner_ind = i; + } + } + for( i = 0; i < psEncC->nStatesDelayedDecision; i++ ) { + if( i != Winner_ind ) { + psDelDec[ i ].RD_Q10 += ( SKP_int32_MAX >> 4 ); + SKP_assert( psDelDec[ i ].RD_Q10 >= 0 ); + } + } + + /* Copy final part of signals from winner state to output and long-term filter states */ + psDD = &psDelDec[ Winner_ind ]; + last_smple_idx = smpl_buf_idx + decisionDelay; + for( i = 0; i < decisionDelay; i++ ) { + last_smple_idx = ( last_smple_idx - 1 ) & DECISION_DELAY_MASK; + q[ i - decisionDelay ] = ( SKP_int )SKP_RSHIFT( psDD->Q_Q10[ last_smple_idx ], 10 ); + pxq[ i - decisionDelay ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( + SKP_SMULWW( psDD->Xq_Q10[ last_smple_idx ], + psDD->Gain_Q16[ last_smple_idx ] ), 10 ) ); + NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q10[ last_smple_idx ]; + } + + subfr = 0; + } + + /* Rewhiten with new A coefs */ + start_idx = psEncC->frame_length - lag - psEncC->predictLPCOrder - LTP_ORDER / 2; + start_idx = SKP_LIMIT( start_idx, 0, psEncC->frame_length - psEncC->predictLPCOrder ); + + SKP_memset( FiltState, 0, psEncC->predictLPCOrder * sizeof( SKP_int32 ) ); + SKP_Silk_MA_Prediction( &NSQ->xq[ start_idx + k * psEncC->subfr_length ], + A_Q12, FiltState, sLTP + start_idx, psEncC->frame_length - start_idx, psEncC->predictLPCOrder ); + + NSQ->sLTP_buf_idx = psEncC->frame_length; + NSQ->rewhite_flag = 1; + } + } + + /* Noise shape parameters */ + SKP_assert( HarmShapeGain_Q14[ k ] >= 0 ); + HarmShapeFIRPacked_Q14 = SKP_RSHIFT( HarmShapeGain_Q14[ k ], 2 ); + HarmShapeFIRPacked_Q14 |= SKP_LSHIFT( ( SKP_int32 )SKP_RSHIFT( HarmShapeGain_Q14[ k ], 1 ), 16 ); + + SKP_Silk_nsq_del_dec_scale_states( NSQ, psDelDec, x, x_sc_Q10, + subfr_length, sLTP, sLTP_Q16, k, psEncC->nStatesDelayedDecision, smpl_buf_idx, + LTP_scale_Q14, Gains_Q16, psEncCtrlC->pitchL ); + + SKP_Silk_noise_shape_quantizer_del_dec( NSQ, psDelDec, psEncCtrlC->sigtype, x_sc_Q10, q, pxq, sLTP_Q16, + A_Q12, B_Q14, AR_shp_Q13, lag, HarmShapeFIRPacked_Q14, Tilt_Q14[ k ], LF_shp_Q14[ k ], Gains_Q16[ k ], + Lambda_Q10, offset_Q10, psEncC->subfr_length, subfr++, psEncC->shapingLPCOrder, psEncC->predictLPCOrder, + psEncC->nStatesDelayedDecision, &smpl_buf_idx, decisionDelay + ); + + x += psEncC->subfr_length; + q += psEncC->subfr_length; + pxq += psEncC->subfr_length; + } + + /* Find winner */ + RDmin_Q10 = psDelDec[ 0 ].RD_Q10; + Winner_ind = 0; + for( k = 1; k < psEncC->nStatesDelayedDecision; k++ ) { + if( psDelDec[ k ].RD_Q10 < RDmin_Q10 ) { + RDmin_Q10 = psDelDec[ k ].RD_Q10; + Winner_ind = k; + } + } + + /* Copy final part of signals from winner state to output and long-term filter states */ + psDD = &psDelDec[ Winner_ind ]; + psEncCtrlC->Seed = psDD->SeedInit; + last_smple_idx = smpl_buf_idx + decisionDelay; + for( i = 0; i < decisionDelay; i++ ) { + last_smple_idx = ( last_smple_idx - 1 ) & DECISION_DELAY_MASK; + q[ i - decisionDelay ] = ( SKP_int )SKP_RSHIFT( psDD->Q_Q10[ last_smple_idx ], 10 ); + pxq[ i - decisionDelay ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( + SKP_SMULWW( psDD->Xq_Q10[ last_smple_idx ], psDD->Gain_Q16[ last_smple_idx ] ), 10 ) ); + NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q10[ last_smple_idx ]; + sLTP_Q16[ NSQ->sLTP_buf_idx - decisionDelay + i ] = psDD->Pred_Q16[ last_smple_idx ]; + + } + SKP_memcpy( NSQ->sLPC_Q14, &psDD->sLPC_Q14[ psEncC->subfr_length ], NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); + + /* Update states */ + NSQ->sLF_AR_shp_Q12 = psDD->LF_AR_Q12; + NSQ->prev_inv_gain_Q16 = NSQ->prev_inv_gain_Q16; + NSQ->lagPrev = psEncCtrlC->pitchL[ NB_SUBFR - 1 ]; + + /* Save quantized speech and noise shaping signals */ + SKP_memcpy( NSQ->xq, &NSQ->xq[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int16 ) ); + SKP_memcpy( NSQ->sLTP_shp_Q10, &NSQ->sLTP_shp_Q10[ psEncC->frame_length ], psEncC->frame_length * sizeof( SKP_int32 ) ); + +} + +/******************************************/ +/* Noise shape quantizer for one subframe */ +/******************************************/ +SKP_INLINE void SKP_Silk_noise_shape_quantizer_del_dec( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ + SKP_int sigtype, /* I Signal type */ + const SKP_int32 x_Q10[], /* I */ + SKP_int q[], /* O */ + SKP_int16 xq[], /* O */ + SKP_int32 sLTP_Q16[], /* I/O LTP filter state */ + const SKP_int16 a_Q12[], /* I Short term prediction coefs */ + const SKP_int16 b_Q14[], /* I Long term prediction coefs */ + const SKP_int16 AR_shp_Q13[], /* I Noise shaping coefs */ + SKP_int lag, /* I Pitch lag */ + SKP_int32 HarmShapeFIRPacked_Q14, /* I */ + SKP_int Tilt_Q14, /* I Spectral tilt */ + SKP_int32 LF_shp_Q14, /* I */ + SKP_int32 Gain_Q16, /* I */ + SKP_int Lambda_Q10, /* I */ + SKP_int offset_Q10, /* I */ + SKP_int length, /* I Input length */ + SKP_int subfr, /* I Subframe number */ + SKP_int shapingLPCOrder, /* I Shaping LPC filter order */ + SKP_int predictLPCOrder, /* I Prediction LPC filter order */ + SKP_int nStatesDelayedDecision, /* I Number of states in decision tree */ + SKP_int *smpl_buf_idx, /* I Index to newest samples in buffers */ + SKP_int decisionDelay /* I */ +) +{ + SKP_int i, j, k, Winner_ind, RDmin_ind, RDmax_ind, last_smple_idx; + SKP_int32 Winner_rand_state; + SKP_int32 LTP_pred_Q14, LPC_pred_Q10, n_AR_Q10, n_LTP_Q14; + SKP_int32 n_LF_Q10; + SKP_int32 r_Q10, rr_Q20, rd1_Q10, rd2_Q10, RDmin_Q10, RDmax_Q10; + SKP_int32 q1_Q10, q2_Q10; + SKP_int32 Atmp, dither; + SKP_int32 exc_Q10, LPC_exc_Q10, xq_Q10; + SKP_int32 tmp, sLF_AR_shp_Q10; + SKP_int32 *pred_lag_ptr, *shp_lag_ptr; + SKP_int32 *psLPC_Q14; + SKP_int32 a_Q12_tmp[ MAX_LPC_ORDER / 2 ], AR_shp_Q13_tmp[ MAX_LPC_ORDER / 2 ]; + NSQ_sample_struct psSampleState[ DEL_DEC_STATES_MAX ][ 2 ]; + NSQ_del_dec_struct *psDD; + NSQ_sample_struct *psSS; + + shp_lag_ptr = &NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; + pred_lag_ptr = &sLTP_Q16[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; + + /* Preload LPC coeficients to array on stack. Gives small performance gain */ + SKP_memcpy( a_Q12_tmp, a_Q12, predictLPCOrder * sizeof( SKP_int16 ) ); + SKP_memcpy( AR_shp_Q13_tmp, AR_shp_Q13, shapingLPCOrder * sizeof( SKP_int16 ) ); + + for( i = 0; i < length; i++ ) { + /* Perform common calculations used in all states */ + + /* Long-term prediction */ + if( sigtype == SIG_TYPE_VOICED ) { + /* Unrolled loop */ + LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], b_Q14[ 0 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], b_Q14[ 1 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], b_Q14[ 2 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], b_Q14[ 3 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], b_Q14[ 4 ] ); + pred_lag_ptr++; + } else { + LTP_pred_Q14 = 0; + } + + /* Long-term shaping */ + if( lag > 0 ) { + /* Symmetric, packed FIR coefficients */ + n_LTP_Q14 = SKP_SMULWB( SKP_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); + n_LTP_Q14 = SKP_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); + n_LTP_Q14 = SKP_LSHIFT( n_LTP_Q14, 6 ); + shp_lag_ptr++; + } else { + n_LTP_Q14 = 0; + } + + for( k = 0; k < nStatesDelayedDecision; k++ ) { + /* Delayed decision state */ + psDD = &psDelDec[ k ]; + + /* Sample state */ + psSS = psSampleState[ k ]; + + /* Generate dither */ + psDD->Seed = SKP_RAND( psDD->Seed ); + + /* dither = rand_seed < 0 ? 0xFFFFFFFF : 0; */ + dither = SKP_RSHIFT( psDD->Seed, 31 ); + + /* Pointer used in short term prediction and shaping */ + psLPC_Q14 = &psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - 1 + i ]; + /* Short-term prediction */ + SKP_assert( ( predictLPCOrder & 1 ) == 0 ); /* check that order is even */ + SKP_assert( ( (SKP_int64)a_Q12 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ + SKP_assert( predictLPCOrder >= 10 ); /* check that unrolling works */ + + /* Partially unrolled */ + Atmp = a_Q12_tmp[ 0 ]; /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -1 ], Atmp ); + Atmp = a_Q12_tmp[ 1 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -2 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -3 ], Atmp ); + Atmp = a_Q12_tmp[ 2 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -4 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -5 ], Atmp ); + Atmp = a_Q12_tmp[ 3 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -6 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -7 ], Atmp ); + Atmp = a_Q12_tmp[ 4 ]; + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -8 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -9 ], Atmp ); + for( j = 10; j < predictLPCOrder; j += 2 ) { + Atmp = a_Q12_tmp[ j >> 1 ]; /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -j ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psLPC_Q14[ -j - 1 ], Atmp ); + } + + /* Noise shape feedback */ + SKP_assert( ( shapingLPCOrder & 1 ) == 0 ); /* check that order is even */ + SKP_assert( ( (SKP_int64)AR_shp_Q13 & 3 ) == 0 ); /* check that array starts at 4-byte aligned address */ + SKP_assert( shapingLPCOrder >= 12 ); /* check that unrolling works */ + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLAWB and SMLAWT instructions should solve the problem. */ + + /* Partially unrolled */ + Atmp = AR_shp_Q13_tmp[ 0 ]; /* read two coefficients at once */ + n_AR_Q10 = SKP_SMULWB( psLPC_Q14[ 0 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -1 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 1 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -2 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -3 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 2 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -4 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -5 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 3 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -6 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -7 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 4 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -8 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -9 ], Atmp ); + Atmp = AR_shp_Q13_tmp[ 5 ]; + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -10 ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -11 ], Atmp ); + for( j = 12; j < shapingLPCOrder; j += 2 ) { + Atmp = AR_shp_Q13_tmp[ j >> 1 ]; /* read two coefficients at once */ + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psLPC_Q14[ -j ], Atmp ); + n_AR_Q10 = SKP_SMLAWT( n_AR_Q10, psLPC_Q14[ -j - 1 ], Atmp ); + } + n_AR_Q10 = SKP_RSHIFT( n_AR_Q10, 1 ); /* Q11 -> Q10 */ + n_AR_Q10 = SKP_SMLAWB( n_AR_Q10, psDD->LF_AR_Q12, Tilt_Q14 ); + + n_LF_Q10 = SKP_LSHIFT( SKP_SMULWB( psDD->Shape_Q10[ *smpl_buf_idx ], LF_shp_Q14 ), 2 ); + n_LF_Q10 = SKP_SMLAWT( n_LF_Q10, psDD->LF_AR_Q12, LF_shp_Q14 ); + + /* Input minus prediction plus noise feedback */ + /* r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP */ + tmp = SKP_SUB32( LTP_pred_Q14, n_LTP_Q14 ); /* Add Q14 stuff */ + tmp = SKP_RSHIFT_ROUND( tmp, 4 ); /* round to Q10 */ + tmp = SKP_ADD32( tmp, LPC_pred_Q10 ); /* add Q10 stuff */ + tmp = SKP_SUB32( tmp, n_AR_Q10 ); /* subtract Q10 stuff */ + tmp = SKP_SUB32( tmp, n_LF_Q10 ); /* subtract Q10 stuff */ + r_Q10 = SKP_SUB32( x_Q10[ i ], tmp ); /* residual error Q10 */ + + + /* Flip sign depending on dither */ + r_Q10 = ( r_Q10 ^ dither ) - dither; + r_Q10 = SKP_SUB32( r_Q10, offset_Q10 ); + r_Q10 = SKP_LIMIT( r_Q10, -64 << 10, 64 << 10 ); + + /* Find two quantization level candidates and measure their rate-distortion */ + if( r_Q10 < -1536 ) { + q1_Q10 = SKP_LSHIFT( SKP_RSHIFT_ROUND( r_Q10, 10 ), 10 ); + r_Q10 = SKP_SUB32( r_Q10, q1_Q10 ); + rd1_Q10 = SKP_RSHIFT( SKP_SMLABB( SKP_MUL( -SKP_ADD32( q1_Q10, offset_Q10 ), Lambda_Q10 ), r_Q10, r_Q10 ), 10 ); + rd2_Q10 = SKP_ADD32( rd1_Q10, 1024 ); + rd2_Q10 = SKP_SUB32( rd2_Q10, SKP_ADD_LSHIFT32( Lambda_Q10, r_Q10, 1 ) ); + q2_Q10 = SKP_ADD32( q1_Q10, 1024 ); + } else if( r_Q10 > 512 ) { + q1_Q10 = SKP_LSHIFT( SKP_RSHIFT_ROUND( r_Q10, 10 ), 10 ); + r_Q10 = SKP_SUB32( r_Q10, q1_Q10 ); + rd1_Q10 = SKP_RSHIFT( SKP_SMLABB( SKP_MUL( SKP_ADD32( q1_Q10, offset_Q10 ), Lambda_Q10 ), r_Q10, r_Q10 ), 10 ); + rd2_Q10 = SKP_ADD32( rd1_Q10, 1024 ); + rd2_Q10 = SKP_SUB32( rd2_Q10, SKP_SUB_LSHIFT32( Lambda_Q10, r_Q10, 1 ) ); + q2_Q10 = SKP_SUB32( q1_Q10, 1024 ); + } else { /* r_Q10 >= -1536 && q1_Q10 <= 512 */ + rr_Q20 = SKP_SMULBB( offset_Q10, Lambda_Q10 ); + rd2_Q10 = SKP_RSHIFT( SKP_SMLABB( rr_Q20, r_Q10, r_Q10 ), 10 ); + rd1_Q10 = SKP_ADD32( rd2_Q10, 1024 ); + rd1_Q10 = SKP_ADD32( rd1_Q10, SKP_SUB_RSHIFT32( SKP_ADD_LSHIFT32( Lambda_Q10, r_Q10, 1 ), rr_Q20, 9 ) ); + q1_Q10 = -1024; + q2_Q10 = 0; + } + + if( rd1_Q10 < rd2_Q10 ) { + psSS[ 0 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd1_Q10 ); + psSS[ 1 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd2_Q10 ); + psSS[ 0 ].Q_Q10 = q1_Q10; + psSS[ 1 ].Q_Q10 = q2_Q10; + } else { + psSS[ 0 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd2_Q10 ); + psSS[ 1 ].RD_Q10 = SKP_ADD32( psDD->RD_Q10, rd1_Q10 ); + psSS[ 0 ].Q_Q10 = q2_Q10; + psSS[ 1 ].Q_Q10 = q1_Q10; + } + + /* Update states for best quantization */ + + /* Quantized excitation */ + exc_Q10 = SKP_ADD32( offset_Q10, psSS[ 0 ].Q_Q10 ); + exc_Q10 = ( exc_Q10 ^ dither ) - dither; + + /* Add predictions */ + LPC_exc_Q10 = exc_Q10 + SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ); + xq_Q10 = SKP_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); + + /* Update states */ + sLF_AR_shp_Q10 = SKP_SUB32( xq_Q10, n_AR_Q10 ); + psSS[ 0 ].sLTP_shp_Q10 = SKP_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); + psSS[ 0 ].LF_AR_Q12 = SKP_LSHIFT( sLF_AR_shp_Q10, 2 ); + psSS[ 0 ].xq_Q14 = SKP_LSHIFT( xq_Q10, 4 ); + psSS[ 0 ].LPC_exc_Q16 = SKP_LSHIFT( LPC_exc_Q10, 6 ); + + /* Update states for second best quantization */ + + /* Quantized excitation */ + exc_Q10 = SKP_ADD32( offset_Q10, psSS[ 1 ].Q_Q10 ); + exc_Q10 = ( exc_Q10 ^ dither ) - dither; + + /* Add predictions */ + LPC_exc_Q10 = exc_Q10 + SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ); + xq_Q10 = SKP_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); + + /* Update states */ + sLF_AR_shp_Q10 = SKP_SUB32( xq_Q10, n_AR_Q10 ); + psSS[ 1 ].sLTP_shp_Q10 = SKP_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); + psSS[ 1 ].LF_AR_Q12 = SKP_LSHIFT( sLF_AR_shp_Q10, 2 ); + psSS[ 1 ].xq_Q14 = SKP_LSHIFT( xq_Q10, 4 ); + psSS[ 1 ].LPC_exc_Q16 = SKP_LSHIFT( LPC_exc_Q10, 6 ); + } + + *smpl_buf_idx = ( *smpl_buf_idx - 1 ) & DECISION_DELAY_MASK; /* Index to newest samples */ + last_smple_idx = ( *smpl_buf_idx + decisionDelay ) & DECISION_DELAY_MASK; /* Index to decisionDelay old samples */ + + /* Find winner */ + RDmin_Q10 = psSampleState[ 0 ][ 0 ].RD_Q10; + Winner_ind = 0; + for( k = 1; k < nStatesDelayedDecision; k++ ) { + if( psSampleState[ k ][ 0 ].RD_Q10 < RDmin_Q10 ) { + RDmin_Q10 = psSampleState[ k ][ 0 ].RD_Q10; + Winner_ind = k; + } + } + + /* Increase RD values of expired states */ + Winner_rand_state = psDelDec[ Winner_ind ].RandState[ last_smple_idx ]; + for( k = 0; k < nStatesDelayedDecision; k++ ) { + if( psDelDec[ k ].RandState[ last_smple_idx ] != Winner_rand_state ) { + psSampleState[ k ][ 0 ].RD_Q10 = SKP_ADD32( psSampleState[ k ][ 0 ].RD_Q10, ( SKP_int32_MAX >> 4 ) ); + psSampleState[ k ][ 1 ].RD_Q10 = SKP_ADD32( psSampleState[ k ][ 1 ].RD_Q10, ( SKP_int32_MAX >> 4 ) ); + SKP_assert( psSampleState[ k ][ 0 ].RD_Q10 >= 0 ); + } + } + + /* Find worst in first set and best in second set */ + RDmax_Q10 = psSampleState[ 0 ][ 0 ].RD_Q10; + RDmin_Q10 = psSampleState[ 0 ][ 1 ].RD_Q10; + RDmax_ind = 0; + RDmin_ind = 0; + for( k = 1; k < nStatesDelayedDecision; k++ ) { + /* find worst in first set */ + if( psSampleState[ k ][ 0 ].RD_Q10 > RDmax_Q10 ) { + RDmax_Q10 = psSampleState[ k ][ 0 ].RD_Q10; + RDmax_ind = k; + } + /* find best in second set */ + if( psSampleState[ k ][ 1 ].RD_Q10 < RDmin_Q10 ) { + RDmin_Q10 = psSampleState[ k ][ 1 ].RD_Q10; + RDmin_ind = k; + } + } + + /* Replace a state if best from second set outperforms worst in first set */ + if( RDmin_Q10 < RDmax_Q10 ) { + SKP_Silk_copy_del_dec_state( &psDelDec[ RDmax_ind ], &psDelDec[ RDmin_ind ], i ); + SKP_memcpy( &psSampleState[ RDmax_ind ][ 0 ], &psSampleState[ RDmin_ind ][ 1 ], sizeof( NSQ_sample_struct ) ); + } + + /* Write samples from winner to output and long-term filter states */ + psDD = &psDelDec[ Winner_ind ]; + if( subfr > 0 || i >= decisionDelay ) { + q[ i - decisionDelay ] = ( SKP_int )SKP_RSHIFT( psDD->Q_Q10[ last_smple_idx ], 10 ); + xq[ i - decisionDelay ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( + SKP_SMULWW( psDD->Xq_Q10[ last_smple_idx ], psDD->Gain_Q16[ last_smple_idx ] ), 10 ) ); + NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay ] = psDD->Shape_Q10[ last_smple_idx ]; + sLTP_Q16[ NSQ->sLTP_buf_idx - decisionDelay ] = psDD->Pred_Q16[ last_smple_idx ]; + } + NSQ->sLTP_shp_buf_idx++; + NSQ->sLTP_buf_idx++; + + /* Update states */ + for( k = 0; k < nStatesDelayedDecision; k++ ) { + psDD = &psDelDec[ k ]; + psSS = &psSampleState[ k ][ 0 ]; + psDD->LF_AR_Q12 = psSS->LF_AR_Q12; + psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH + i ] = psSS->xq_Q14; + psDD->Xq_Q10[ *smpl_buf_idx ] = SKP_RSHIFT( psSS->xq_Q14, 4 ); + psDD->Q_Q10[ *smpl_buf_idx ] = psSS->Q_Q10; + psDD->Pred_Q16[ *smpl_buf_idx ] = psSS->LPC_exc_Q16; + psDD->Shape_Q10[ *smpl_buf_idx ] = psSS->sLTP_shp_Q10; + psDD->Seed = SKP_ADD_RSHIFT32( psDD->Seed, psSS->Q_Q10, 10 ); + psDD->RandState[ *smpl_buf_idx ] = psDD->Seed; + psDD->RD_Q10 = psSS->RD_Q10; + psDD->Gain_Q16[ *smpl_buf_idx ] = Gain_Q16; + } + } + /* Update LPC states */ + for( k = 0; k < nStatesDelayedDecision; k++ ) { + psDD = &psDelDec[ k ]; + SKP_memcpy( psDD->sLPC_Q14, &psDD->sLPC_Q14[ length ], NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); + } +} + +SKP_INLINE void SKP_Silk_nsq_del_dec_scale_states( + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ + const SKP_int16 x[], /* I Input in Q0 */ + SKP_int32 x_sc_Q10[], /* O Input scaled with 1/Gain in Q10 */ + SKP_int length, /* I Length of input */ + SKP_int16 sLTP[], /* I Re-whitened LTP state in Q0 */ + SKP_int32 sLTP_Q16[], /* O LTP state matching scaled input */ + SKP_int subfr, /* I Subframe number */ + SKP_int nStatesDelayedDecision, /* I Number of del dec states */ + SKP_int smpl_buf_idx, /* I Index to newest samples in buffers */ + const SKP_int LTP_scale_Q14, /* I LTP state scaling */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int pitchL[ NB_SUBFR ] /* I Pitch lag */ +) +{ + SKP_int i, k, scale_length, lag; + SKP_int32 inv_gain_Q16, gain_adj_Q16, inv_gain_Q32; + NSQ_del_dec_struct *psDD; + + inv_gain_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( Gains_Q16[ subfr ], 1 ) ); + inv_gain_Q16 = SKP_min( inv_gain_Q16, SKP_int16_MAX ); + lag = pitchL[ subfr ]; + /* After rewhitening the LTP state is un-scaled. So scale with inv_gain_Q16 */ + if( NSQ->rewhite_flag ) { + inv_gain_Q32 = SKP_LSHIFT( inv_gain_Q16, 16 ); + if( subfr == 0 ) { + /* Do LTP downscaling */ + inv_gain_Q32 = SKP_LSHIFT( SKP_SMULWB( inv_gain_Q32, LTP_scale_Q14 ), 2 ); + } + for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { + SKP_assert( i < MAX_FRAME_LENGTH ); + sLTP_Q16[ i ] = SKP_SMULWB( inv_gain_Q32, sLTP[ i ] ); + } + } + + /* Adjust for changing gain */ + if( inv_gain_Q16 != NSQ->prev_inv_gain_Q16 ) { + gain_adj_Q16 = SKP_DIV32_varQ( inv_gain_Q16, NSQ->prev_inv_gain_Q16, 16 ); + + for( k = 0; k < nStatesDelayedDecision; k++ ) { + psDD = &psDelDec[ k ]; + + /* Scale scalar states */ + psDD->LF_AR_Q12 = SKP_SMULWW( gain_adj_Q16, psDD->LF_AR_Q12 ); + + /* scale short term state */ + for( i = 0; i < NSQ_LPC_BUF_LENGTH; i++ ) { + psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - i - 1 ] = SKP_SMULWW( gain_adj_Q16, psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - i - 1 ] ); + } + for( i = 0; i < DECISION_DELAY; i++ ) { + psDD->Pred_Q16[ i ] = SKP_SMULWW( gain_adj_Q16, psDD->Pred_Q16[ i ] ); + psDD->Shape_Q10[ i ] = SKP_SMULWW( gain_adj_Q16, psDD->Shape_Q10[ i ] ); + } + } + + /* Scale long term shaping state */ + + /* Calculate length to be scaled, Worst case: Next frame is voiced with max lag */ + scale_length = length * NB_SUBFR; /* aprox max lag */ + scale_length = scale_length - SKP_SMULBB( NB_SUBFR - ( subfr + 1 ), length ); /* subtract samples that will be too old in next frame */ + scale_length = SKP_max_int( scale_length, lag + LTP_ORDER ); /* make sure to scale whole pitch period if voiced */ + + for( i = NSQ->sLTP_shp_buf_idx - scale_length; i < NSQ->sLTP_shp_buf_idx; i++ ) { + NSQ->sLTP_shp_Q10[ i ] = SKP_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q10[ i ] ); + } + + /* Scale LTP predict state */ + if( NSQ->rewhite_flag == 0 ) { + for( i = NSQ->sLTP_buf_idx - lag - LTP_ORDER / 2; i < NSQ->sLTP_buf_idx; i++ ) { + sLTP_Q16[ i ] = SKP_SMULWW( gain_adj_Q16, sLTP_Q16[ i ] ); + } + } + } + + /* Scale input */ + for( i = 0; i < length; i++ ) { + x_sc_Q10[ i ] = SKP_RSHIFT( SKP_SMULBB( x[ i ], ( SKP_int16 )inv_gain_Q16 ), 6 ); + } + + /* save inv_gain */ + SKP_assert( inv_gain_Q16 != 0 ); + NSQ->prev_inv_gain_Q16 = inv_gain_Q16; +} + +SKP_INLINE void SKP_Silk_copy_del_dec_state( + NSQ_del_dec_struct *DD_dst, /* I Dst del dec state */ + NSQ_del_dec_struct *DD_src, /* I Src del dec state */ + SKP_int LPC_state_idx /* I Index to LPC buffer */ +) +{ + SKP_memcpy( DD_dst->RandState, DD_src->RandState, DECISION_DELAY * sizeof( SKP_int ) ); + SKP_memcpy( DD_dst->Q_Q10, DD_src->Q_Q10, DECISION_DELAY * sizeof( SKP_int32 ) ); + SKP_memcpy( DD_dst->Pred_Q16, DD_src->Pred_Q16, DECISION_DELAY * sizeof( SKP_int32 ) ); + SKP_memcpy( DD_dst->Shape_Q10, DD_src->Shape_Q10, DECISION_DELAY * sizeof( SKP_int32 ) ); + SKP_memcpy( DD_dst->Xq_Q10, DD_src->Xq_Q10, DECISION_DELAY * sizeof( SKP_int32 ) ); + + SKP_memcpy( &DD_dst->sLPC_Q14[ LPC_state_idx ], &DD_src->sLPC_Q14[ LPC_state_idx ], NSQ_LPC_BUF_LENGTH * sizeof( SKP_int32 ) ); + DD_dst->LF_AR_Q12 = DD_src->LF_AR_Q12; + DD_dst->Seed = DD_src->Seed; + DD_dst->SeedInit = DD_src->SeedInit; + DD_dst->RD_Q10 = DD_src->RD_Q10; +} diff --git a/jni/silk/src/SKP_Silk_PLC.c b/app/src/main/jni/silk/src/SKP_Silk_PLC.c similarity index 97% rename from jni/silk/src/SKP_Silk_PLC.c rename to app/src/main/jni/silk/src/SKP_Silk_PLC.c index 746ae10..4ec3626 100644 --- a/jni/silk/src/SKP_Silk_PLC.c +++ b/app/src/main/jni/silk/src/SKP_Silk_PLC.c @@ -1,389 +1,389 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" -#include "SKP_Silk_PLC.h" - -#define NB_ATT 2 -static const SKP_int16 HARM_ATT_Q15[NB_ATT] = { 32440, 31130 }; /* 0.99, 0.95 */ -static const SKP_int16 PLC_RAND_ATTENUATE_V_Q15[NB_ATT] = { 31130, 26214 }; /* 0.95, 0.8 */ -static const SKP_int16 PLC_RAND_ATTENUATE_UV_Q15[NB_ATT] = { 32440, 29491 }; /* 0.99, 0.9 */ - -void SKP_Silk_PLC_Reset( - SKP_Silk_decoder_state *psDec /* I/O Decoder state */ -) -{ - psDec->sPLC.pitchL_Q8 = SKP_RSHIFT( psDec->frame_length, 1 ); -} - -void SKP_Silk_PLC( - SKP_Silk_decoder_state *psDec, /* I Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I Decoder control */ - SKP_int16 signal[], /* O Concealed signal */ - SKP_int length, /* I length of residual */ - SKP_int lost /* I Loss flag */ -) -{ - /* PLC control function */ - if( psDec->fs_kHz != psDec->sPLC.fs_kHz ) { - SKP_Silk_PLC_Reset( psDec ); - psDec->sPLC.fs_kHz = psDec->fs_kHz; - } - - if( lost ) { - /****************************/ - /* Generate Signal */ - /****************************/ - SKP_Silk_PLC_conceal( psDec, psDecCtrl, signal, length ); - } else { - /****************************/ - /* Update state */ - /****************************/ - SKP_Silk_PLC_update( psDec, psDecCtrl, signal, length ); - } -} - -/**************************************************/ -/* Update state of PLC */ -/**************************************************/ -void SKP_Silk_PLC_update( - SKP_Silk_decoder_state *psDec, /* (I/O) Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* (I/O) Decoder control */ - SKP_int16 signal[], - SKP_int length -) -{ - SKP_int32 LTP_Gain_Q14, temp_LTP_Gain_Q14; - SKP_int i, j; - SKP_Silk_PLC_struct *psPLC; - - psPLC = &psDec->sPLC; - - /* Update parameters used in case of packet loss */ - psDec->prev_sigtype = psDecCtrl->sigtype; - LTP_Gain_Q14 = 0; - if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) { - /* Find the parameters for the last subframe which contains a pitch pulse */ - for( j = 0; j * psDec->subfr_length < psDecCtrl->pitchL[ NB_SUBFR - 1 ]; j++ ) { - temp_LTP_Gain_Q14 = 0; - for( i = 0; i < LTP_ORDER; i++ ) { - temp_LTP_Gain_Q14 += psDecCtrl->LTPCoef_Q14[ ( NB_SUBFR - 1 - j ) * LTP_ORDER + i ]; - } - if( temp_LTP_Gain_Q14 > LTP_Gain_Q14 ) { - LTP_Gain_Q14 = temp_LTP_Gain_Q14; - SKP_memcpy( psPLC->LTPCoef_Q14, - &psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( NB_SUBFR - 1 - j, LTP_ORDER ) ], - LTP_ORDER * sizeof( SKP_int16 ) ); - - psPLC->pitchL_Q8 = SKP_LSHIFT( psDecCtrl->pitchL[ NB_SUBFR - 1 - j ], 8 ); - } - } - -#if USE_SINGLE_TAP - SKP_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( SKP_int16 ) ); - psPLC->LTPCoef_Q14[ LTP_ORDER / 2 ] = LTP_Gain_Q14; -#endif - - /* Limit LT coefs */ - if( LTP_Gain_Q14 < V_PITCH_GAIN_START_MIN_Q14 ) { - SKP_int scale_Q10; - SKP_int32 tmp; - - tmp = SKP_LSHIFT( V_PITCH_GAIN_START_MIN_Q14, 10 ); - scale_Q10 = SKP_DIV32( tmp, SKP_max( LTP_Gain_Q14, 1 ) ); - for( i = 0; i < LTP_ORDER; i++ ) { - psPLC->LTPCoef_Q14[ i ] = SKP_RSHIFT( SKP_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q10 ), 10 ); - } - } else if( LTP_Gain_Q14 > V_PITCH_GAIN_START_MAX_Q14 ) { - SKP_int scale_Q14; - SKP_int32 tmp; - - tmp = SKP_LSHIFT( V_PITCH_GAIN_START_MAX_Q14, 14 ); - scale_Q14 = SKP_DIV32( tmp, SKP_max( LTP_Gain_Q14, 1 ) ); - for( i = 0; i < LTP_ORDER; i++ ) { - psPLC->LTPCoef_Q14[ i ] = SKP_RSHIFT( SKP_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q14 ), 14 ); - } - } - } else { - psPLC->pitchL_Q8 = SKP_LSHIFT( SKP_SMULBB( psDec->fs_kHz, 18 ), 8 ); - SKP_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( SKP_int16 )); - } - - /* Save LPC coeficients */ - SKP_memcpy( psPLC->prevLPC_Q12, psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order * sizeof( SKP_int16 ) ); - psPLC->prevLTP_scale_Q14 = psDecCtrl->LTP_scale_Q14; - - /* Save Gains */ - SKP_memcpy( psPLC->prevGain_Q16, psDecCtrl->Gains_Q16, NB_SUBFR * sizeof( SKP_int32 ) ); -} - -void SKP_Silk_PLC_conceal( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* O concealed signal */ - SKP_int length /* I length of residual */ -) -{ - SKP_int i, j, k; - SKP_int16 *B_Q14, exc_buf[ MAX_FRAME_LENGTH ], *exc_buf_ptr; - SKP_int16 rand_scale_Q14, A_Q12_tmp[ MAX_LPC_ORDER ]; - SKP_int32 rand_seed, harm_Gain_Q15, rand_Gain_Q15; - SKP_int lag, idx, shift1, shift2; - SKP_int32 energy1, energy2, *rand_ptr, *pred_lag_ptr, Atmp; - SKP_int32 sig_Q10[ MAX_FRAME_LENGTH ], *sig_Q10_ptr, LPC_exc_Q10, LPC_pred_Q10, LTP_pred_Q14; - SKP_Silk_PLC_struct *psPLC; - - psPLC = &psDec->sPLC; - - /* Update LTP buffer */ - SKP_memcpy( psDec->sLTP_Q16, &psDec->sLTP_Q16[ psDec->frame_length ], psDec->frame_length * sizeof( SKP_int32 ) ); - - /* LPC concealment. Apply BWE to previous LPC */ - SKP_Silk_bwexpander( psPLC->prevLPC_Q12, psDec->LPC_order, BWE_COEF_Q16 ); - - /* Find random noise component */ - /* Scale previous excitation signal */ - exc_buf_ptr = exc_buf; - for( k = ( NB_SUBFR >> 1 ); k < NB_SUBFR; k++ ) { - for( i = 0; i < psDec->subfr_length; i++ ) { - exc_buf_ptr[ i ] = ( SKP_int16 )SKP_RSHIFT( - SKP_SMULWW( psDec->exc_Q10[ i + k * psDec->subfr_length ], psPLC->prevGain_Q16[ k ] ), 10 ); - } - exc_buf_ptr += psDec->subfr_length; - } - /* Find the subframe with lowest energy of the last two and use that as random noise generator */ - SKP_Silk_sum_sqr_shift( &energy1, &shift1, exc_buf, psDec->subfr_length ); - SKP_Silk_sum_sqr_shift( &energy2, &shift2, &exc_buf[ psDec->subfr_length ], psDec->subfr_length ); - - if( SKP_RSHIFT( energy1, shift2 ) < SKP_RSHIFT( energy1, shift2 ) ) { - /* First sub-frame has lowest energy */ - rand_ptr = &psDec->exc_Q10[ SKP_max_int( 0, 3 * psDec->subfr_length - RAND_BUF_SIZE ) ]; - } else { - /* Second sub-frame has lowest energy */ - rand_ptr = &psDec->exc_Q10[ SKP_max_int( 0, psDec->frame_length - RAND_BUF_SIZE ) ]; - } - - /* Setup Gain to random noise component */ - B_Q14 = psPLC->LTPCoef_Q14; - rand_scale_Q14 = psPLC->randScale_Q14; - - /* Setup attenuation gains */ - harm_Gain_Q15 = HARM_ATT_Q15[ SKP_min_int( NB_ATT - 1, psDec->lossCnt ) ]; - if( psDec->prev_sigtype == SIG_TYPE_VOICED ) { - rand_Gain_Q15 = PLC_RAND_ATTENUATE_V_Q15[ SKP_min_int( NB_ATT - 1, psDec->lossCnt ) ]; - } else { - rand_Gain_Q15 = PLC_RAND_ATTENUATE_UV_Q15[ SKP_min_int( NB_ATT - 1, psDec->lossCnt ) ]; - } - - /* First Lost frame */ - if( psDec->lossCnt == 0 ) { - rand_scale_Q14 = (1 << 14 ); - - /* Reduce random noise Gain for voiced frames */ - if( psDec->prev_sigtype == SIG_TYPE_VOICED ) { - for( i = 0; i < LTP_ORDER; i++ ) { - rand_scale_Q14 -= B_Q14[ i ]; - } - rand_scale_Q14 = SKP_max_16( 3277, rand_scale_Q14 ); /* 0.2 */ - rand_scale_Q14 = ( SKP_int16 )SKP_RSHIFT( SKP_SMULBB( rand_scale_Q14, psPLC->prevLTP_scale_Q14 ), 14 ); - } - - /* Reduce random noise for unvoiced frames with high LPC gain */ - if( psDec->prev_sigtype == SIG_TYPE_UNVOICED ) { - SKP_int32 invGain_Q30, down_scale_Q30; - - SKP_Silk_LPC_inverse_pred_gain( &invGain_Q30, psPLC->prevLPC_Q12, psDec->LPC_order ); - - down_scale_Q30 = SKP_min_32( SKP_RSHIFT( ( 1 << 30 ), LOG2_INV_LPC_GAIN_HIGH_THRES ), invGain_Q30 ); - down_scale_Q30 = SKP_max_32( SKP_RSHIFT( ( 1 << 30 ), LOG2_INV_LPC_GAIN_LOW_THRES ), down_scale_Q30 ); - down_scale_Q30 = SKP_LSHIFT( down_scale_Q30, LOG2_INV_LPC_GAIN_HIGH_THRES ); - - rand_Gain_Q15 = SKP_RSHIFT( SKP_SMULWB( down_scale_Q30, rand_Gain_Q15 ), 14 ); - } - } - - rand_seed = psPLC->rand_seed; - lag = SKP_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 ); - psDec->sLTP_buf_idx = psDec->frame_length; - - /***************************/ - /* LTP synthesis filtering */ - /***************************/ - sig_Q10_ptr = sig_Q10; - for( k = 0; k < NB_SUBFR; k++ ) { - /* Setup pointer */ - pred_lag_ptr = &psDec->sLTP_Q16[ psDec->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; - for( i = 0; i < psDec->subfr_length; i++ ) { - rand_seed = SKP_RAND( rand_seed ); - idx = SKP_RSHIFT( rand_seed, 25 ) & RAND_BUF_MASK; - - /* Unrolled loop */ - LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], B_Q14[ 0 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], B_Q14[ 1 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], B_Q14[ 2 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], B_Q14[ 3 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], B_Q14[ 4 ] ); - pred_lag_ptr++; - - /* Generate LPC residual */ - LPC_exc_Q10 = SKP_LSHIFT( SKP_SMULWB( rand_ptr[ idx ], rand_scale_Q14 ), 2 ); /* Random noise part */ - LPC_exc_Q10 = SKP_ADD32( LPC_exc_Q10, SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ) ); /* Harmonic part */ - - /* Update states */ - psDec->sLTP_Q16[ psDec->sLTP_buf_idx ] = SKP_LSHIFT( LPC_exc_Q10, 6 ); - psDec->sLTP_buf_idx++; - - /* Save LPC residual */ - sig_Q10_ptr[ i ] = LPC_exc_Q10; - } - sig_Q10_ptr += psDec->subfr_length; - /* Gradually reduce LTP gain */ - for( j = 0; j < LTP_ORDER; j++ ) { - B_Q14[ j ] = SKP_RSHIFT( SKP_SMULBB( harm_Gain_Q15, B_Q14[ j ] ), 15 ); - } - /* Gradually reduce excitation gain */ - rand_scale_Q14 = SKP_RSHIFT( SKP_SMULBB( rand_scale_Q14, rand_Gain_Q15 ), 15 ); - - /* Slowly increase pitch lag */ - psPLC->pitchL_Q8 += SKP_SMULWB( psPLC->pitchL_Q8, PITCH_DRIFT_FAC_Q16 ); - psPLC->pitchL_Q8 = SKP_min_32( psPLC->pitchL_Q8, SKP_LSHIFT( SKP_SMULBB( MAX_PITCH_LAG_MS, psDec->fs_kHz ), 8 ) ); - lag = SKP_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 ); - } - - /***************************/ - /* LPC synthesis filtering */ - /***************************/ - sig_Q10_ptr = sig_Q10; - /* Preload LPC coeficients to array on stack. Gives small performance gain */ - SKP_memcpy( A_Q12_tmp, psPLC->prevLPC_Q12, psDec->LPC_order * sizeof( SKP_int16 ) ); - SKP_assert( psDec->LPC_order >= 10 ); /* check that unrolling works */ - for( k = 0; k < NB_SUBFR; k++ ) { - for( i = 0; i < psDec->subfr_length; i++ ){ - /* unrolled */ - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 0 ] ); /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMULWB( psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 2 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 4 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 6 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 8 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], Atmp ); - for( j = 10 ; j < psDec->LPC_order ; j+=2 ) { - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ j ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 - j ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 - j ], Atmp ); - } - - /* Add prediction to LPC residual */ - sig_Q10_ptr[ i ] = SKP_ADD32( sig_Q10_ptr[ i ], LPC_pred_Q10 ); - - /* Update states */ - psDec->sLPC_Q14[ MAX_LPC_ORDER + i ] = SKP_LSHIFT( sig_Q10_ptr[ i ], 4 ); - } - sig_Q10_ptr += psDec->subfr_length; - /* Update LPC filter state */ - SKP_memcpy( psDec->sLPC_Q14, &psDec->sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( SKP_int32 ) ); - } - - /* Scale with Gain */ - for( i = 0; i < psDec->frame_length; i++ ) { - signal[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( sig_Q10[ i ], psPLC->prevGain_Q16[ NB_SUBFR - 1 ] ), 10 ) ); - } - - /**************************************/ - /* Update states */ - /**************************************/ - psPLC->rand_seed = rand_seed; - psPLC->randScale_Q14 = rand_scale_Q14; - for( i = 0; i < NB_SUBFR; i++ ) { - psDecCtrl->pitchL[ i ] = lag; - } -} - -/* Glues concealed frames with new good recieved frames */ -void SKP_Silk_PLC_glue_frames( - SKP_Silk_decoder_state *psDec, /* I/O decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* I/O signal */ - SKP_int length /* I length of residual */ -) -{ - SKP_int i, energy_shift; - SKP_int32 energy; - SKP_Silk_PLC_struct *psPLC; - psPLC = &psDec->sPLC; - - if( psDec->lossCnt ) { - /* Calculate energy in concealed residual */ - SKP_Silk_sum_sqr_shift( &psPLC->conc_energy, &psPLC->conc_energy_shift, signal, length ); - - psPLC->last_frame_lost = 1; - } else { - if( psDec->sPLC.last_frame_lost ) { - /* Calculate residual in decoded signal if last frame was lost */ - SKP_Silk_sum_sqr_shift( &energy, &energy_shift, signal, length ); - - /* Normalize energies */ - if( energy_shift > psPLC->conc_energy_shift ) { - psPLC->conc_energy = SKP_RSHIFT( psPLC->conc_energy, energy_shift - psPLC->conc_energy_shift ); - } else if( energy_shift < psPLC->conc_energy_shift ) { - energy = SKP_RSHIFT( energy, psPLC->conc_energy_shift - energy_shift ); - } - - /* Fade in the energy difference */ - if( energy > psPLC->conc_energy ) { - SKP_int32 frac_Q24, LZ; - SKP_int32 gain_Q12, slope_Q12; - - LZ = SKP_Silk_CLZ32( psPLC->conc_energy ); - LZ = LZ - 1; - psPLC->conc_energy = SKP_LSHIFT( psPLC->conc_energy, LZ ); - energy = SKP_RSHIFT( energy, SKP_max_32( 24 - LZ, 0 ) ); - - frac_Q24 = SKP_DIV32( psPLC->conc_energy, SKP_max( energy, 1 ) ); - - gain_Q12 = SKP_Silk_SQRT_APPROX( frac_Q24 ); - slope_Q12 = SKP_DIV32_16( ( 1 << 12 ) - gain_Q12, length ); - - for( i = 0; i < length; i++ ) { - signal[ i ] = SKP_RSHIFT( SKP_MUL( gain_Q12, signal[ i ] ), 12 ); - gain_Q12 += slope_Q12; - gain_Q12 = SKP_min( gain_Q12, ( 1 << 12 ) ); - } - } - } - psPLC->last_frame_lost = 0; - - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" +#include "SKP_Silk_PLC.h" + +#define NB_ATT 2 +static const SKP_int16 HARM_ATT_Q15[NB_ATT] = { 32440, 31130 }; /* 0.99, 0.95 */ +static const SKP_int16 PLC_RAND_ATTENUATE_V_Q15[NB_ATT] = { 31130, 26214 }; /* 0.95, 0.8 */ +static const SKP_int16 PLC_RAND_ATTENUATE_UV_Q15[NB_ATT] = { 32440, 29491 }; /* 0.99, 0.9 */ + +void SKP_Silk_PLC_Reset( + SKP_Silk_decoder_state *psDec /* I/O Decoder state */ +) +{ + psDec->sPLC.pitchL_Q8 = SKP_RSHIFT( psDec->frame_length, 1 ); +} + +void SKP_Silk_PLC( + SKP_Silk_decoder_state *psDec, /* I Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I Decoder control */ + SKP_int16 signal[], /* O Concealed signal */ + SKP_int length, /* I length of residual */ + SKP_int lost /* I Loss flag */ +) +{ + /* PLC control function */ + if( psDec->fs_kHz != psDec->sPLC.fs_kHz ) { + SKP_Silk_PLC_Reset( psDec ); + psDec->sPLC.fs_kHz = psDec->fs_kHz; + } + + if( lost ) { + /****************************/ + /* Generate Signal */ + /****************************/ + SKP_Silk_PLC_conceal( psDec, psDecCtrl, signal, length ); + } else { + /****************************/ + /* Update state */ + /****************************/ + SKP_Silk_PLC_update( psDec, psDecCtrl, signal, length ); + } +} + +/**************************************************/ +/* Update state of PLC */ +/**************************************************/ +void SKP_Silk_PLC_update( + SKP_Silk_decoder_state *psDec, /* (I/O) Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* (I/O) Decoder control */ + SKP_int16 signal[], + SKP_int length +) +{ + SKP_int32 LTP_Gain_Q14, temp_LTP_Gain_Q14; + SKP_int i, j; + SKP_Silk_PLC_struct *psPLC; + + psPLC = &psDec->sPLC; + + /* Update parameters used in case of packet loss */ + psDec->prev_sigtype = psDecCtrl->sigtype; + LTP_Gain_Q14 = 0; + if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) { + /* Find the parameters for the last subframe which contains a pitch pulse */ + for( j = 0; j * psDec->subfr_length < psDecCtrl->pitchL[ NB_SUBFR - 1 ]; j++ ) { + temp_LTP_Gain_Q14 = 0; + for( i = 0; i < LTP_ORDER; i++ ) { + temp_LTP_Gain_Q14 += psDecCtrl->LTPCoef_Q14[ ( NB_SUBFR - 1 - j ) * LTP_ORDER + i ]; + } + if( temp_LTP_Gain_Q14 > LTP_Gain_Q14 ) { + LTP_Gain_Q14 = temp_LTP_Gain_Q14; + SKP_memcpy( psPLC->LTPCoef_Q14, + &psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( NB_SUBFR - 1 - j, LTP_ORDER ) ], + LTP_ORDER * sizeof( SKP_int16 ) ); + + psPLC->pitchL_Q8 = SKP_LSHIFT( psDecCtrl->pitchL[ NB_SUBFR - 1 - j ], 8 ); + } + } + +#if USE_SINGLE_TAP + SKP_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( SKP_int16 ) ); + psPLC->LTPCoef_Q14[ LTP_ORDER / 2 ] = LTP_Gain_Q14; +#endif + + /* Limit LT coefs */ + if( LTP_Gain_Q14 < V_PITCH_GAIN_START_MIN_Q14 ) { + SKP_int scale_Q10; + SKP_int32 tmp; + + tmp = SKP_LSHIFT( V_PITCH_GAIN_START_MIN_Q14, 10 ); + scale_Q10 = SKP_DIV32( tmp, SKP_max( LTP_Gain_Q14, 1 ) ); + for( i = 0; i < LTP_ORDER; i++ ) { + psPLC->LTPCoef_Q14[ i ] = SKP_RSHIFT( SKP_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q10 ), 10 ); + } + } else if( LTP_Gain_Q14 > V_PITCH_GAIN_START_MAX_Q14 ) { + SKP_int scale_Q14; + SKP_int32 tmp; + + tmp = SKP_LSHIFT( V_PITCH_GAIN_START_MAX_Q14, 14 ); + scale_Q14 = SKP_DIV32( tmp, SKP_max( LTP_Gain_Q14, 1 ) ); + for( i = 0; i < LTP_ORDER; i++ ) { + psPLC->LTPCoef_Q14[ i ] = SKP_RSHIFT( SKP_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q14 ), 14 ); + } + } + } else { + psPLC->pitchL_Q8 = SKP_LSHIFT( SKP_SMULBB( psDec->fs_kHz, 18 ), 8 ); + SKP_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( SKP_int16 )); + } + + /* Save LPC coeficients */ + SKP_memcpy( psPLC->prevLPC_Q12, psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order * sizeof( SKP_int16 ) ); + psPLC->prevLTP_scale_Q14 = psDecCtrl->LTP_scale_Q14; + + /* Save Gains */ + SKP_memcpy( psPLC->prevGain_Q16, psDecCtrl->Gains_Q16, NB_SUBFR * sizeof( SKP_int32 ) ); +} + +void SKP_Silk_PLC_conceal( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* O concealed signal */ + SKP_int length /* I length of residual */ +) +{ + SKP_int i, j, k; + SKP_int16 *B_Q14, exc_buf[ MAX_FRAME_LENGTH ], *exc_buf_ptr; + SKP_int16 rand_scale_Q14, A_Q12_tmp[ MAX_LPC_ORDER ]; + SKP_int32 rand_seed, harm_Gain_Q15, rand_Gain_Q15; + SKP_int lag, idx, shift1, shift2; + SKP_int32 energy1, energy2, *rand_ptr, *pred_lag_ptr, Atmp; + SKP_int32 sig_Q10[ MAX_FRAME_LENGTH ], *sig_Q10_ptr, LPC_exc_Q10, LPC_pred_Q10, LTP_pred_Q14; + SKP_Silk_PLC_struct *psPLC; + + psPLC = &psDec->sPLC; + + /* Update LTP buffer */ + SKP_memcpy( psDec->sLTP_Q16, &psDec->sLTP_Q16[ psDec->frame_length ], psDec->frame_length * sizeof( SKP_int32 ) ); + + /* LPC concealment. Apply BWE to previous LPC */ + SKP_Silk_bwexpander( psPLC->prevLPC_Q12, psDec->LPC_order, BWE_COEF_Q16 ); + + /* Find random noise component */ + /* Scale previous excitation signal */ + exc_buf_ptr = exc_buf; + for( k = ( NB_SUBFR >> 1 ); k < NB_SUBFR; k++ ) { + for( i = 0; i < psDec->subfr_length; i++ ) { + exc_buf_ptr[ i ] = ( SKP_int16 )SKP_RSHIFT( + SKP_SMULWW( psDec->exc_Q10[ i + k * psDec->subfr_length ], psPLC->prevGain_Q16[ k ] ), 10 ); + } + exc_buf_ptr += psDec->subfr_length; + } + /* Find the subframe with lowest energy of the last two and use that as random noise generator */ + SKP_Silk_sum_sqr_shift( &energy1, &shift1, exc_buf, psDec->subfr_length ); + SKP_Silk_sum_sqr_shift( &energy2, &shift2, &exc_buf[ psDec->subfr_length ], psDec->subfr_length ); + + if( SKP_RSHIFT( energy1, shift2 ) < SKP_RSHIFT( energy1, shift2 ) ) { + /* First sub-frame has lowest energy */ + rand_ptr = &psDec->exc_Q10[ SKP_max_int( 0, 3 * psDec->subfr_length - RAND_BUF_SIZE ) ]; + } else { + /* Second sub-frame has lowest energy */ + rand_ptr = &psDec->exc_Q10[ SKP_max_int( 0, psDec->frame_length - RAND_BUF_SIZE ) ]; + } + + /* Setup Gain to random noise component */ + B_Q14 = psPLC->LTPCoef_Q14; + rand_scale_Q14 = psPLC->randScale_Q14; + + /* Setup attenuation gains */ + harm_Gain_Q15 = HARM_ATT_Q15[ SKP_min_int( NB_ATT - 1, psDec->lossCnt ) ]; + if( psDec->prev_sigtype == SIG_TYPE_VOICED ) { + rand_Gain_Q15 = PLC_RAND_ATTENUATE_V_Q15[ SKP_min_int( NB_ATT - 1, psDec->lossCnt ) ]; + } else { + rand_Gain_Q15 = PLC_RAND_ATTENUATE_UV_Q15[ SKP_min_int( NB_ATT - 1, psDec->lossCnt ) ]; + } + + /* First Lost frame */ + if( psDec->lossCnt == 0 ) { + rand_scale_Q14 = (1 << 14 ); + + /* Reduce random noise Gain for voiced frames */ + if( psDec->prev_sigtype == SIG_TYPE_VOICED ) { + for( i = 0; i < LTP_ORDER; i++ ) { + rand_scale_Q14 -= B_Q14[ i ]; + } + rand_scale_Q14 = SKP_max_16( 3277, rand_scale_Q14 ); /* 0.2 */ + rand_scale_Q14 = ( SKP_int16 )SKP_RSHIFT( SKP_SMULBB( rand_scale_Q14, psPLC->prevLTP_scale_Q14 ), 14 ); + } + + /* Reduce random noise for unvoiced frames with high LPC gain */ + if( psDec->prev_sigtype == SIG_TYPE_UNVOICED ) { + SKP_int32 invGain_Q30, down_scale_Q30; + + SKP_Silk_LPC_inverse_pred_gain( &invGain_Q30, psPLC->prevLPC_Q12, psDec->LPC_order ); + + down_scale_Q30 = SKP_min_32( SKP_RSHIFT( ( 1 << 30 ), LOG2_INV_LPC_GAIN_HIGH_THRES ), invGain_Q30 ); + down_scale_Q30 = SKP_max_32( SKP_RSHIFT( ( 1 << 30 ), LOG2_INV_LPC_GAIN_LOW_THRES ), down_scale_Q30 ); + down_scale_Q30 = SKP_LSHIFT( down_scale_Q30, LOG2_INV_LPC_GAIN_HIGH_THRES ); + + rand_Gain_Q15 = SKP_RSHIFT( SKP_SMULWB( down_scale_Q30, rand_Gain_Q15 ), 14 ); + } + } + + rand_seed = psPLC->rand_seed; + lag = SKP_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 ); + psDec->sLTP_buf_idx = psDec->frame_length; + + /***************************/ + /* LTP synthesis filtering */ + /***************************/ + sig_Q10_ptr = sig_Q10; + for( k = 0; k < NB_SUBFR; k++ ) { + /* Setup pointer */ + pred_lag_ptr = &psDec->sLTP_Q16[ psDec->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; + for( i = 0; i < psDec->subfr_length; i++ ) { + rand_seed = SKP_RAND( rand_seed ); + idx = SKP_RSHIFT( rand_seed, 25 ) & RAND_BUF_MASK; + + /* Unrolled loop */ + LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], B_Q14[ 0 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], B_Q14[ 1 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], B_Q14[ 2 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], B_Q14[ 3 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], B_Q14[ 4 ] ); + pred_lag_ptr++; + + /* Generate LPC residual */ + LPC_exc_Q10 = SKP_LSHIFT( SKP_SMULWB( rand_ptr[ idx ], rand_scale_Q14 ), 2 ); /* Random noise part */ + LPC_exc_Q10 = SKP_ADD32( LPC_exc_Q10, SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ) ); /* Harmonic part */ + + /* Update states */ + psDec->sLTP_Q16[ psDec->sLTP_buf_idx ] = SKP_LSHIFT( LPC_exc_Q10, 6 ); + psDec->sLTP_buf_idx++; + + /* Save LPC residual */ + sig_Q10_ptr[ i ] = LPC_exc_Q10; + } + sig_Q10_ptr += psDec->subfr_length; + /* Gradually reduce LTP gain */ + for( j = 0; j < LTP_ORDER; j++ ) { + B_Q14[ j ] = SKP_RSHIFT( SKP_SMULBB( harm_Gain_Q15, B_Q14[ j ] ), 15 ); + } + /* Gradually reduce excitation gain */ + rand_scale_Q14 = SKP_RSHIFT( SKP_SMULBB( rand_scale_Q14, rand_Gain_Q15 ), 15 ); + + /* Slowly increase pitch lag */ + psPLC->pitchL_Q8 += SKP_SMULWB( psPLC->pitchL_Q8, PITCH_DRIFT_FAC_Q16 ); + psPLC->pitchL_Q8 = SKP_min_32( psPLC->pitchL_Q8, SKP_LSHIFT( SKP_SMULBB( MAX_PITCH_LAG_MS, psDec->fs_kHz ), 8 ) ); + lag = SKP_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 ); + } + + /***************************/ + /* LPC synthesis filtering */ + /***************************/ + sig_Q10_ptr = sig_Q10; + /* Preload LPC coeficients to array on stack. Gives small performance gain */ + SKP_memcpy( A_Q12_tmp, psPLC->prevLPC_Q12, psDec->LPC_order * sizeof( SKP_int16 ) ); + SKP_assert( psDec->LPC_order >= 10 ); /* check that unrolling works */ + for( k = 0; k < NB_SUBFR; k++ ) { + for( i = 0; i < psDec->subfr_length; i++ ){ + /* unrolled */ + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 0 ] ); /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMULWB( psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 2 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 4 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 6 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 8 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], Atmp ); + for( j = 10 ; j < psDec->LPC_order ; j+=2 ) { + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ j ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 - j ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 - j ], Atmp ); + } + + /* Add prediction to LPC residual */ + sig_Q10_ptr[ i ] = SKP_ADD32( sig_Q10_ptr[ i ], LPC_pred_Q10 ); + + /* Update states */ + psDec->sLPC_Q14[ MAX_LPC_ORDER + i ] = SKP_LSHIFT( sig_Q10_ptr[ i ], 4 ); + } + sig_Q10_ptr += psDec->subfr_length; + /* Update LPC filter state */ + SKP_memcpy( psDec->sLPC_Q14, &psDec->sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( SKP_int32 ) ); + } + + /* Scale with Gain */ + for( i = 0; i < psDec->frame_length; i++ ) { + signal[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( sig_Q10[ i ], psPLC->prevGain_Q16[ NB_SUBFR - 1 ] ), 10 ) ); + } + + /**************************************/ + /* Update states */ + /**************************************/ + psPLC->rand_seed = rand_seed; + psPLC->randScale_Q14 = rand_scale_Q14; + for( i = 0; i < NB_SUBFR; i++ ) { + psDecCtrl->pitchL[ i ] = lag; + } +} + +/* Glues concealed frames with new good recieved frames */ +void SKP_Silk_PLC_glue_frames( + SKP_Silk_decoder_state *psDec, /* I/O decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* I/O signal */ + SKP_int length /* I length of residual */ +) +{ + SKP_int i, energy_shift; + SKP_int32 energy; + SKP_Silk_PLC_struct *psPLC; + psPLC = &psDec->sPLC; + + if( psDec->lossCnt ) { + /* Calculate energy in concealed residual */ + SKP_Silk_sum_sqr_shift( &psPLC->conc_energy, &psPLC->conc_energy_shift, signal, length ); + + psPLC->last_frame_lost = 1; + } else { + if( psDec->sPLC.last_frame_lost ) { + /* Calculate residual in decoded signal if last frame was lost */ + SKP_Silk_sum_sqr_shift( &energy, &energy_shift, signal, length ); + + /* Normalize energies */ + if( energy_shift > psPLC->conc_energy_shift ) { + psPLC->conc_energy = SKP_RSHIFT( psPLC->conc_energy, energy_shift - psPLC->conc_energy_shift ); + } else if( energy_shift < psPLC->conc_energy_shift ) { + energy = SKP_RSHIFT( energy, psPLC->conc_energy_shift - energy_shift ); + } + + /* Fade in the energy difference */ + if( energy > psPLC->conc_energy ) { + SKP_int32 frac_Q24, LZ; + SKP_int32 gain_Q12, slope_Q12; + + LZ = SKP_Silk_CLZ32( psPLC->conc_energy ); + LZ = LZ - 1; + psPLC->conc_energy = SKP_LSHIFT( psPLC->conc_energy, LZ ); + energy = SKP_RSHIFT( energy, SKP_max_32( 24 - LZ, 0 ) ); + + frac_Q24 = SKP_DIV32( psPLC->conc_energy, SKP_max( energy, 1 ) ); + + gain_Q12 = SKP_Silk_SQRT_APPROX( frac_Q24 ); + slope_Q12 = SKP_DIV32_16( ( 1 << 12 ) - gain_Q12, length ); + + for( i = 0; i < length; i++ ) { + signal[ i ] = SKP_RSHIFT( SKP_MUL( gain_Q12, signal[ i ] ), 12 ); + gain_Q12 += slope_Q12; + gain_Q12 = SKP_min( gain_Q12, ( 1 << 12 ) ); + } + } + } + psPLC->last_frame_lost = 0; + + } +} diff --git a/jni/silk/src/SKP_Silk_PLC.h b/app/src/main/jni/silk/src/SKP_Silk_PLC.h similarity index 98% rename from jni/silk/src/SKP_Silk_PLC.h rename to app/src/main/jni/silk/src/SKP_Silk_PLC.h index 0409a24..0a8336c 100644 --- a/jni/silk/src/SKP_Silk_PLC.h +++ b/app/src/main/jni/silk/src/SKP_Silk_PLC.h @@ -1,79 +1,79 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_PLC_FIX_H -#define SKP_SILK_PLC_FIX_H - -#include "SKP_Silk_main_FIX.h" - -#define BWE_COEF_Q16 64880 /* 0.99 in Q16 */ -#define V_PITCH_GAIN_START_MIN_Q14 11469 /* 0.7 in Q14 */ -#define V_PITCH_GAIN_START_MAX_Q14 15565 /* 0.95 in Q14 */ -#define MAX_PITCH_LAG_MS 18 -#define SA_THRES_Q8 50 -#define USE_SINGLE_TAP 1 -#define RAND_BUF_SIZE 128 -#define RAND_BUF_MASK (RAND_BUF_SIZE - 1) -#define LOG2_INV_LPC_GAIN_HIGH_THRES 4 /* 2^4 = 12 dB LPC gain */ -#define LOG2_INV_LPC_GAIN_LOW_THRES 8 /* 2^8 = 24 dB LPC gain */ -#define PITCH_DRIFT_FAC_Q16 655 /* 0.01 in Q16 */ - -void SKP_Silk_PLC_Reset( - SKP_Silk_decoder_state *psDec /* I/O Decoder state */ -); - -void SKP_Silk_PLC( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* I/O signal */ - SKP_int length, /* I length of residual */ - SKP_int lost /* I Loss flag */ -); - -void SKP_Silk_PLC_update( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], - SKP_int length -); - -void SKP_Silk_PLC_conceal( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* O LPC residual signal */ - SKP_int length /* I length of signal */ -); - -void SKP_Silk_PLC_glue_frames( - SKP_Silk_decoder_state *psDec, /* I/O decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* I/O signal */ - SKP_int length /* I length of signal */ -); - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_PLC_FIX_H +#define SKP_SILK_PLC_FIX_H + +#include "SKP_Silk_main_FIX.h" + +#define BWE_COEF_Q16 64880 /* 0.99 in Q16 */ +#define V_PITCH_GAIN_START_MIN_Q14 11469 /* 0.7 in Q14 */ +#define V_PITCH_GAIN_START_MAX_Q14 15565 /* 0.95 in Q14 */ +#define MAX_PITCH_LAG_MS 18 +#define SA_THRES_Q8 50 +#define USE_SINGLE_TAP 1 +#define RAND_BUF_SIZE 128 +#define RAND_BUF_MASK (RAND_BUF_SIZE - 1) +#define LOG2_INV_LPC_GAIN_HIGH_THRES 4 /* 2^4 = 12 dB LPC gain */ +#define LOG2_INV_LPC_GAIN_LOW_THRES 8 /* 2^8 = 24 dB LPC gain */ +#define PITCH_DRIFT_FAC_Q16 655 /* 0.01 in Q16 */ + +void SKP_Silk_PLC_Reset( + SKP_Silk_decoder_state *psDec /* I/O Decoder state */ +); + +void SKP_Silk_PLC( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* I/O signal */ + SKP_int length, /* I length of residual */ + SKP_int lost /* I Loss flag */ +); + +void SKP_Silk_PLC_update( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], + SKP_int length +); + +void SKP_Silk_PLC_conceal( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* O LPC residual signal */ + SKP_int length /* I length of signal */ +); + +void SKP_Silk_PLC_glue_frames( + SKP_Silk_decoder_state *psDec, /* I/O decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* I/O signal */ + SKP_int length /* I length of signal */ +); + +#endif + diff --git a/jni/silk/src/SKP_Silk_SigProc_FIX.h b/app/src/main/jni/silk/src/SKP_Silk_SigProc_FIX.h similarity index 98% rename from jni/silk/src/SKP_Silk_SigProc_FIX.h rename to app/src/main/jni/silk/src/SKP_Silk_SigProc_FIX.h index 41d95f5..d845d47 100644 --- a/jni/silk/src/SKP_Silk_SigProc_FIX.h +++ b/app/src/main/jni/silk/src/SKP_Silk_SigProc_FIX.h @@ -1,796 +1,796 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef _SKP_SILK_SIGPROC_FIX_H_ -#define _SKP_SILK_SIGPROC_FIX_H_ - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define SigProc_MAX_ORDER_LPC 16 /* max order of the LPC analysis in schur() and k2a() */ -#define SigProc_MAX_CORRELATION_LENGTH 640 /* max input length to the correlation */ -#include "SKP_Silk_typedef.h" -#include -#include /* for abs() */ -#include "SKP_Silk_macros.h" -#include "SKP_Silk_resample_rom.h" - - - -/********************************************************************/ -/* SIGNAL PROCESSING FUNCTIONS */ -/********************************************************************/ - -/* downsample by a factor 2 */ -void SKP_Silk_resample_1_2( - const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ - SKP_int32 *S, /* I/O: State vector [6] */ - SKP_int16 *out, /* O: 8 kHz signal [len] */ - SKP_int32 *scratch, /* I: Scratch memory [4*len] */ - const SKP_int32 len /* I: Number of OUTPUT samples */ -); - -/*! - * downsample by a factor 2, coarser (good for resampling audio) - */ -void SKP_Silk_resample_1_2_coarse( - const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ - SKP_int32 *S, /* I/O: state vector [4] */ - SKP_int16 *out, /* O: 8 kHz signal [len] */ - SKP_int32 *scratch, /* I: scratch memory [3*len] */ - const SKP_int32 len /* I: number of OUTPUT samples */ -); - -/*! - * downsample by a factor 2, coarsest (good for signals that are already oversampled, or for analysis purposes) - */ -void SKP_Silk_resample_1_2_coarsest( - const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ - SKP_int32 *S, /* I/O: State vector [2] */ - SKP_int16 *out, /* O: 8 kHz signal [len] */ - SKP_int32 *scratch, /* I: Scratch memory [3*len] */ - const SKP_int32 len /* I: Number of OUTPUT samples */ -); - -/*! - * upsample by a factor 2, coarser (good for resampling audio) - */ -void SKP_Silk_resample_2_1_coarse( - const SKP_int16 *in, /* I: 8 kHz signal [len] */ - SKP_int32 *S, /* I/O: State vector [4] */ - SKP_int16 *out, /* O: 16 kHz signal [2*len] */ - SKP_int32 *scratch, /* I: Scratch memory [3*len] */ - const SKP_int32 len /* I: Number of INPUT samples */ -); - -/*! - * Resamples by a factor 1/3 - */ -void SKP_Silk_resample_1_3( - SKP_int16 *out, /* O: Fs_low signal [inLen/3] */ - SKP_int32 *S, /* I/O: State vector [7] */ - const SKP_int16 *in, /* I: Fs_high signal [inLen] */ - const SKP_int32 inLen /* I: Input length, must be a multiple of 3 */ -); - -/*! - * Resamples by a factor 3/1 - */ -void SKP_Silk_resample_3_1( - SKP_int16 *out, /* O: Fs_high signal [inLen*3] */ - SKP_int32 *S, /* I/O: State vector [7] */ - const SKP_int16 *in, /* I: Fs_low signal [inLen] */ - const SKP_int32 inLen /* I: Input length */ -); - -/*! - * Resamples by a factor 2/3 - */ -void SKP_Silk_resample_2_3( - SKP_int16 *out, /* O: Fs_low signal [inLen * 2/3] */ - SKP_int32 *S, /* I/O: State vector [7+4] */ - const SKP_int16 *in, /* I: Fs_high signal [inLen] */ - const SKP_int inLen /* I: Input length, must be a multiple of 3 */ -); - -/*! - * Resamples by a factor 3/2 - */ -void SKP_Silk_resample_3_2( - SKP_int16 *out, /* O: Fs_high signal [inLen*3/2] */ - SKP_int32 *S, /* I/O: State vector [7+4] */ - const SKP_int16 *in, /* I: Fs_low signal [inLen] */ - SKP_int inLen /* I: Input length, must be a multiple of 2 */ -); - -/*! - * Resamples by a factor 4/3 - */ -void SKP_Silk_resample_4_3( - SKP_int16 *out, /* O: Fs_low signal [inLen * 4/3] */ - SKP_int32 *S, /* I/O: State vector [7+4+4] */ - const SKP_int16 *in, /* I: Fs_high signal [inLen] */ - const SKP_int inLen /* I: input length, must be a multiple of 3 */ -); - -/*! - * Resamples by a factor 3/4 - */ - void SKP_Silk_resample_3_4( - SKP_int16 *out, /* O: Fs_high signal [inLen*3/4] */ - SKP_int32 *S, /* I/O: State vector [7+2+6] */ - const SKP_int16 *in, /* I: Fs_low signal [inLen] */ - SKP_int inLen /* I: Input length, must be a multiple of 4 */ -); - -/*! - * resample with a factor 2/3 coarse - */ -void SKP_Silk_resample_2_3_coarse( - SKP_int16 *out, /* O: Output signal */ - SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ - const SKP_int16 *in, /* I: Input signal */ - const SKP_int frameLenIn, /* I: Number of input samples */ - SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ -); - -/*! - * resample with a factor 2/3 coarsest - */ -void SKP_Silk_resample_2_3_coarsest( - SKP_int16 *out, /* O: Output signal */ - SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ - const SKP_int16 *in, /* I: Input signal */ - const SKP_int frameLenIn, /* I: Number of input samples */ - SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ -); - -/*! - * First order low-pass filter, with input as SKP_int16, running at 48 kHz - */ -void SKP_Silk_lowpass_short( - const SKP_int16 *in, /* I: Q15 48 kHz signal; [len] */ - SKP_int32 *S, /* I/O: Q25 state; length = 1 */ - SKP_int32 *out, /* O: Q25 48 kHz signal; [len] */ - const SKP_int32 len /* O: Signal length */ -); - -/*! - * First order low-pass filter, with input as SKP_int32, running at 48 kHz - */ -void SKP_Silk_lowpass_int( - const SKP_int32 *in, /* I: Q25 48 kHz signal; length = len */ - SKP_int32 *S, /* I/O: Q25 state; length = 1 */ - SKP_int32 *out, /* O: Q25 48 kHz signal; length = len */ - const SKP_int32 len /* I: Number of samples */ -); - -/*! - * First-order allpass filter - */ -void SKP_Silk_allpass_int( - const SKP_int32 *in, /* I: Q25 input signal [len] */ - SKP_int32 *S, /* I/O: Q25 state [1] */ - SKP_int A, /* I: Q15 coefficient (0 <= A < 32768) */ - SKP_int32 *out, /* O: Q25 output signal [len] */ - const SKP_int32 len /* I: Number of samples */ -); - -/*! - * second order ARMA filter - * can handle (slowly) varying coefficients - */ -void SKP_Silk_biquad( - const SKP_int16 *in, /* I: input signal */ - const SKP_int16 *B, /* I: MA coefficients, Q13 [3] */ - const SKP_int16 *A, /* I: AR coefficients, Q13 [2] */ - SKP_int32 *S, /* I/O: state vector [2] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len /* I: signal length */ -); -/*! - * second order ARMA filter; - * slower than biquad() but uses more precise coefficients - * can handle (slowly) varying coefficients - */ -void SKP_Silk_biquad_alt( - const SKP_int16 *in, /* I: input signal */ - const SKP_int32 *B_Q28, /* I: MA coefficients [3] */ - const SKP_int32 *A_Q28, /* I: AR coefficients [2] */ - SKP_int32 *S, /* I/O: state vector [2] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len /* I: signal length (must be even) */ -); - -/*! - * variable order MA filter. Prediction error filter implementation. Coeficients negated and starting with coef to x[n - 1] - */ -void SKP_Silk_MA_Prediction( - const SKP_int16 *in, /* I: Input signal */ - const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ - SKP_int32 *S, /* I/O: State vector [order] */ - SKP_int16 *out, /* O: Output signal */ - const SKP_int32 len, /* I: Signal length */ - const SKP_int32 order /* I: Filter order */ -); - -void SKP_Silk_MA_Prediction_Q13( - const SKP_int16 *in, /* I: input signal */ - const SKP_int16 *B, /* I: MA prediction coefficients, Q13 [order] */ - SKP_int32 *S, /* I/O: state vector [order] */ - SKP_int16 *out, /* O: output signal */ - SKP_int32 len, /* I: signal length */ - SKP_int32 order /* I: filter order */ -); - -/*! - * 16th order AR filter for LPC synthesis, coefficients are in Q12 - */ -void SKP_Silk_LPC_synthesis_order16( - const SKP_int16 *in, /* I: excitation signal */ - const SKP_int16 *A_Q12, /* I: AR coefficients [16], between -8_Q0 and 8_Q0 */ - const SKP_int32 Gain_Q26, /* I: gain */ - SKP_int32 *S, /* I/O: state vector [16] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len /* I: signal length, must be multiple of 16 */ -); - -/* variable order MA prediction error filter. */ -/* Inverse filter of SKP_Silk_LPC_synthesis_filter */ -void SKP_Silk_LPC_analysis_filter( - const SKP_int16 *in, /* I: Input signal */ - const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ - SKP_int16 *S, /* I/O: State vector [order] */ - SKP_int16 *out, /* O: Output signal */ - const SKP_int32 len, /* I: Signal length */ - const SKP_int32 Order /* I: Filter order */ -); - -/* even order AR filter */ -void SKP_Silk_LPC_synthesis_filter( - const SKP_int16 *in, /* I: excitation signal */ - const SKP_int16 *A_Q12, /* I: AR coefficients [Order], between -8_Q0 and 8_Q0 */ - const SKP_int32 Gain_Q26, /* I: gain */ - SKP_int32 *S, /* I/O: state vector [Order] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len, /* I: signal length */ - const SKP_int Order /* I: filter order, must be even */ -); - -/* Chirp (bandwidth expand) LP AR filter */ -void SKP_Silk_bwexpander( - SKP_int16 *ar, /* I/O AR filter to be expanded (without leading 1) */ - const SKP_int d, /* I Length of ar */ - SKP_int32 chirp_Q16 /* I Chirp factor (typically in the range 0 to 1) */ -); - -/* Chirp (bandwidth expand) LP AR filter */ -void SKP_Silk_bwexpander_32( - SKP_int32 *ar, /* I/O AR filter to be expanded (without leading 1) */ - const SKP_int d, /* I Length of ar */ - SKP_int32 chirp_Q16 /* I Chirp factor in Q16 */ -); - -/* Compute inverse of LPC prediction gain, and */ -/* test if LPC coefficients are stable (all poles within unit circle) */ -SKP_int SKP_Silk_LPC_inverse_pred_gain( /* O: Returns 1 if unstable, otherwise 0 */ - SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ - const SKP_int16 *A_Q12, /* I: Prediction coefficients, Q12 [order] */ - const SKP_int order /* I: Prediction order */ -); - -SKP_int SKP_Silk_LPC_inverse_pred_gain_Q13( /* O: returns 1 if unstable, otherwise 0 */ - SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ - const SKP_int16 *A_Q13, /* I: Prediction coefficients, Q13 [order] */ - const SKP_int order /* I: Prediction order */ -); - -/* split signal in two decimated bands using first-order allpass filters */ -void SKP_Silk_ana_filt_bank_1( - const SKP_int16 *in, /* I: Input signal [N] */ - SKP_int32 *S, /* I/O: State vector [2] */ - SKP_int16 *outL, /* O: Low band [N/2] */ - SKP_int16 *outH, /* O: High band [N/2] */ - SKP_int32 *scratch, /* I: Scratch memory [3*N/2] */ - const SKP_int32 N /* I: Number of input samples */ -); - -/********************************************************************/ -/* SCALAR FUNCTIONS */ -/********************************************************************/ - -/* approximation of 128 * log2() (exact inverse of approx 2^() below) */ -/* convert input to a log scale */ -SKP_int32 SKP_Silk_lin2log(const SKP_int32 inLin); /* I: input in linear scale */ - -/* Approximation of a sigmoid function */ -SKP_int SKP_Silk_sigm_Q15(SKP_int in_Q5); - -/* approximation of 2^() (exact inverse of approx log2() above) */ -/* convert input to a linear scale */ -SKP_int32 SKP_Silk_log2lin(const SKP_int32 inLog_Q7); /* I: input on log scale */ - -/* Function that returns the maximum absolut value of the input vector */ -SKP_int16 SKP_Silk_int16_array_maxabs( /* O Maximum absolute value, max: 2^15-1 */ - const SKP_int16 *vec, /* I Input vector [len] */ - const SKP_int32 len /* I Length of input vector */ -); - -/* Compute number of bits to right shift the sum of squares of a vector */ -/* of int16s to make it fit in an int32 */ -void SKP_Silk_sum_sqr_shift( - SKP_int32 *energy, /* O Energy of x, after shifting to the right */ - SKP_int *shift, /* O Number of bits right shift applied to energy */ - const SKP_int16 *x, /* I Input vector */ - SKP_int len /* I Length of input vector */ -); - -/* Calculates the reflection coefficients from the correlation sequence */ -/* Faster than schur64(), but much less accurate. */ -/* Uses SMLAWB(), requiring armv5E and higher. */ -void SKP_Silk_schur( - SKP_int16 *rc_Q15, /* O: reflection coefficients [order] Q15 */ - const SKP_int32 *c, /* I: correlations [order+1] */ - const SKP_int32 order /* I: prediction order */ -); - -/* Calculates the reflection coefficients from the correlation sequence */ -/* Slower than schur(), but more accurate. */ -/* Uses SMULL(), available on armv4 */ -SKP_int32 SKP_Silk_schur64( /* O: returns residual energy */ - SKP_int32 rc_Q16[], /* O: Reflection coefficients [order] Q16 */ - const SKP_int32 c[], /* I: Correlations [order+1] */ - SKP_int32 order /* I: Prediction order */ -); - -/* Step up function, converts reflection coefficients to prediction coefficients */ -void SKP_Silk_k2a( - SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ - const SKP_int16 *rc_Q15, /* I: Reflection coefficients [order] Q15 */ - const SKP_int32 order /* I: Prediction order */ -); - -/* Step up function, converts reflection coefficients to prediction coefficients */ -void SKP_Silk_k2a_Q16( - SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ - const SKP_int32 *rc_Q16, /* I: Reflection coefficients [order] Q16 */ - const SKP_int32 order /* I: Prediction order */ -); - -/* Apply sine window to signal vector. */ -/* Window types: */ -/* 0 -> sine window from 0 to pi */ -/* 1 -> sine window from 0 to pi/2 */ -/* 2 -> sine window from pi/2 to pi */ -/* every other sample of window is linearly interpolated, for speed */ -void SKP_Silk_apply_sine_window( - SKP_int16 px_win[], /* O Pointer to windowed signal */ - const SKP_int16 px[], /* I Pointer to input signal */ - const SKP_int win_type, /* I Selects a window type */ - const SKP_int length /* I Window length, multiple of 4 */ -); - -/* Compute autocorrelation */ -void SKP_Silk_autocorr( - SKP_int32 *results, /* O Result (length correlationCount) */ - SKP_int32 *scale, /* O Scaling of the correlation vector */ - const SKP_int16 *inputData, /* I Input data to correlate */ - const SKP_int inputDataSize, /* I Length of input */ - const SKP_int correlationCount /* I Number of correlation taps to compute */ -); - -/* Pitch estimator */ -#define SigProc_PITCH_EST_MIN_COMPLEX 0 -#define SigProc_PITCH_EST_MID_COMPLEX 1 -#define SigProc_PITCH_EST_MAX_COMPLEX 2 - -void SKP_Silk_decode_pitch( - SKP_int lagIndex, /* I */ - SKP_int contourIndex, /* O */ - SKP_int pitch_lags[], /* O 4 pitch values */ - SKP_int Fs_kHz /* I sampling frequency (kHz) */ -); - -SKP_int SKP_Silk_pitch_analysis_core( /* O Voicing estimate: 0 voiced, 1 unvoiced */ - const SKP_int16 *signal, /* I Signal of length PITCH_EST_FRAME_LENGTH_MS*Fs_kHz */ - SKP_int *pitch_out, /* O 4 pitch lag values */ - SKP_int *lagIndex, /* O Lag Index */ - SKP_int *contourIndex, /* O Pitch contour Index */ - SKP_int *LTPCorr_Q15, /* I/O Normalized correlation; input: value from previous frame */ - SKP_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */ - const SKP_int32 search_thres1_Q16, /* I First stage threshold for lag candidates 0 - 1 */ - const SKP_int search_thres2_Q15, /* I Final threshold for lag candidates 0 - 1 */ - const SKP_int Fs_kHz, /* I Sample frequency (kHz) */ - const SKP_int complexity /* I Complexity setting, 0-2, where 2 is highest */ -); - -/* parameter defining the size and accuracy of the piecewise linear */ -/* cosine approximatin table. */ - -#define LSF_COS_TAB_SZ_FIX 128 -/* rom table with cosine values */ -extern const SKP_int SKP_Silk_LSFCosTab_FIX_Q12[ LSF_COS_TAB_SZ_FIX + 1 ]; - -void SKP_Silk_LPC_fit( - SKP_int16 *a_QQ, /* O stabilized LPC vector, Q(24-rshift) [L] */ - SKP_int32 *a_Q24, /* I LPC vector [L] */ - const SKP_int QQ, /* I Q domain of output LPC vector */ - const SKP_int L /* I Number of LPC parameters in the input vector */ -); - -/* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */ -/* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */ -void SKP_Silk_A2NLSF( - SKP_int *NLSF, /* O Normalized Line Spectral Frequencies, Q15 (0 - (2^15-1)), [d] */ - SKP_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */ - const SKP_int d /* I Filter order (must be even) */ -); - -/* compute whitening filter coefficients from normalized line spectral frequencies */ -void SKP_Silk_NLSF2A( - SKP_int16 *a, /* o monic whitening filter coefficients in Q12, [d] */ - const SKP_int *NLSF, /* i normalized line spectral frequencies in Q15, [d] */ - const SKP_int d /* i filter order (should be even) */ -); - -void SKP_Silk_insertion_sort_increasing( - SKP_int32 *a, /* I/O Unsorted / Sorted vector */ - SKP_int *index, /* O: Index vector for the sorted elements */ - const SKP_int L, /* I: Vector length */ - const SKP_int K /* I: Number of correctly sorted positions */ -); - -void SKP_Silk_insertion_sort_decreasing( - SKP_int *a, /* I/O: Unsorted / Sorted vector */ - SKP_int *index, /* O: Index vector for the sorted elements */ - const SKP_int L, /* I: Vector length */ - const SKP_int K /* I: Number of correctly sorted positions */ -); - -void SKP_Silk_insertion_sort_decreasing_int16( - SKP_int16 *a, /* I/O: Unsorted / Sorted vector */ - SKP_int *index, /* O: Index vector for the sorted elements */ - const SKP_int L, /* I: Vector length */ - const SKP_int K /* I: Number of correctly sorted positions */ -); - -void SKP_Silk_insertion_sort_increasing_all_values( - SKP_int *a, /* I/O: Unsorted / Sorted vector */ - const SKP_int L /* I: Vector length */ -); - -/* NLSF stabilizer, for a single input data vector */ -void SKP_Silk_NLSF_stabilize( - SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vector in Q15 [L] */ - const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ - const SKP_int L /* I: Number of NLSF parameters in the input vector */ -); - -/* NLSF stabilizer, over multiple input column data vectors */ -void SKP_Silk_NLSF_stabilize_multi( - SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vectors in Q15 [LxN] */ - const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ - const SKP_int N, /* I: Number of input vectors to be stabilized */ - const SKP_int L /* I: NLSF vector dimension */ -); - -/* Laroia low complexity NLSF weights */ -void SKP_Silk_NLSF_VQ_weights_laroia( - SKP_int *pNLSFW_Q6, /* O: Pointer to input vector weights [D x 1] */ - const SKP_int *pNLSF_Q15, /* I: Pointer to input vector [D x 1] */ - const SKP_int D /* I: Input vector dimension (even) */ -); - -/* Compute reflection coefficients from input signal */ -void SKP_Silk_burg_modified( - SKP_int32 *res_nrg, /* O residual energy */ - SKP_int *res_nrgQ, /* O residual energy Q value */ - SKP_int32 A_Q16[], /* O prediction coefficients (length order) */ - const SKP_int16 x[], /* I input signal, length: nb_subfr * ( D + subfr_length ) */ - const SKP_int subfr_length, /* I input signal subframe length (including D preceeding samples) */ - const SKP_int nb_subfr, /* I number of subframes stacked in x */ - const SKP_int32 WhiteNoiseFrac_Q32, /* I fraction added to zero-lag autocorrelation */ - const SKP_int D /* I order */ -); - -/* Multiply a vector by a constant */ -void SKP_Silk_scale_vector16_Q14( - SKP_int16 *data1, - SKP_int gain_Q14, /* Gain in Q14 */ - SKP_int dataSize -); - -/* Copy and multiply a vector by a constant */ -void SKP_Silk_scale_copy_vector16( - SKP_int16 *data_out, - const SKP_int16 *data_in, - SKP_int32 gain_Q16, /* I: gain in Q16 */ - const SKP_int dataSize /* I: length */ -); - -void SKP_Silk_scale_vector32_16_Q14( - SKP_int32 *data1, /* I/O: Q0/Q0 */ - SKP_int gain_Q14, /* I: Q14 */ - SKP_int dataSize /* I: length */ -); - -/* Multiply a vector by a constant, does not saturate output data */ -void SKP_Silk_scale_vector32_Q16( - SKP_int32 *data1, /* I/O: Q0/Q0 */ - SKP_int32 gain_Q16, /* I: gain in Q16 ( SKP_int16_MIN <= gain_Q16 <= SKP_int16_MAX + 65536 ) */ - const SKP_int dataSize /* I: length */ -); - -/* Some for the LTP related function requires Q26 to work.*/ -void SKP_Silk_scale_vector32_Q26_lshift_18( - SKP_int32 *data1, /* I/O: Q0/Q18 */ - SKP_int32 gain_Q26, /* I: Q26 */ - SKP_int dataSize /* I: length */ -); - -/********************************************************************/ -/* INLINE ARM MATH */ -/********************************************************************/ - -/* return sum(inVec1[i]*inVec2[i]) */ -/* inVec1 and inVec2 should be increasing ordered, and starting address should be 4 byte aligned. (a factor of 4)*/ -SKP_int32 SKP_Silk_inner_prod_aligned( - const SKP_int16* const inVec1, /* I input vector 1 */ - const SKP_int16* const inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -); - -SKP_int32 SKP_Silk_inner_prod16_aligned_sat( - const SKP_int16* const inVec1, /* I input vector 1 */ - const SKP_int16* const inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -); - -SKP_int64 SKP_Silk_inner_prod_aligned_64( - const SKP_int32 *inVec1, /* I input vector 1 */ - const SKP_int32 *inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -); - -SKP_int64 SKP_Silk_inner_prod16_aligned_64( - const SKP_int16 *inVec1, /* I input vector 1 */ - const SKP_int16 *inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -); -/********************************************************************/ -/* MACROS */ -/********************************************************************/ - -/* Define 4-byte aligned array of SKP_int16 */ -#define SKP_array_of_int16_4_byte_aligned( arrayName, nElements ) \ - SKP_int32 dummy_int32 ## arrayName; \ - SKP_int16 arrayName[ (nElements) ] - -/* Useful Macros that can be adjusted to other platforms */ -#define SKP_memcpy(a, b, c) memcpy((a), (b), (c)) /* Dest, Src, ByteCount */ -#define SKP_memset(a, b, c) memset((a), (b), (c)) /* Dest, value, ByteCount */ -#define SKP_memmove(a, b, c) memmove((a), (b), (c)) /* Dest, Src, ByteCount */ -/* fixed point macros */ - -// (a32 * b32) output have to be 32bit int -#define SKP_MUL(a32, b32) ((a32) * (b32)) - -// (a32 * b32) output have to be 32bit uint -#define SKP_MUL_uint(a32, b32) SKP_MUL(a32, b32) - -// a32 + (b32 * c32) output have to be 32bit int -#define SKP_MLA(a32, b32, c32) SKP_ADD32((a32),((b32) * (c32))) - -// a32 + (b32 * c32) output have to be 32bit uint -#define SKP_MLA_uint(a32, b32, c32) SKP_MLA(a32, b32, c32) - -// ((a32 >> 16) * (b32 >> 16)) output have to be 32bit int -#define SKP_SMULTT(a32, b32) (((a32) >> 16) * ((b32) >> 16)) - -// a32 + ((a32 >> 16) * (b32 >> 16)) output have to be 32bit int -#define SKP_SMLATT(a32, b32, c32) SKP_ADD32((a32),((b32) >> 16) * ((c32) >> 16)) - -#define SKP_SMLALBB(a64, b16, c16) SKP_ADD64((a64),(SKP_int64)((SKP_int32)(b16) * (SKP_int32)(c16))) - -// (a32 * b32) -#define SKP_SMULL(a32, b32) ((SKP_int64)(a32) * /*(SKP_int64)*/(b32)) - -// multiply-accumulate macros that allow overflow in the addition (ie, no asserts in debug mode) -#define SKP_MLA_ovflw(a32, b32, c32) SKP_MLA(a32, b32, c32) -#ifndef SKP_SMLABB_ovflw -# define SKP_SMLABB_ovflw(a32, b32, c32) SKP_SMLABB(a32, b32, c32) -#endif -#define SKP_SMLABT_ovflw(a32, b32, c32) SKP_SMLABT(a32, b32, c32) -#define SKP_SMLATT_ovflw(a32, b32, c32) SKP_SMLATT(a32, b32, c32) -#define SKP_SMLAWB_ovflw(a32, b32, c32) SKP_SMLAWB(a32, b32, c32) -#define SKP_SMLAWT_ovflw(a32, b32, c32) SKP_SMLAWT(a32, b32, c32) - -#define SKP_DIV64_32(a64, b32) ((a64)/(b32)) /* TODO: rewrite it as a set of SKP_DIV32.*/ - -#define SKP_DIV32_16(a32, b16) ((SKP_int32)((a32) / (b16))) -#define SKP_DIV32(a32, b32) ((SKP_int32)((a32) / (b32))) - -// These macros enables checking for overflow in SKP_Silk_API_Debug.h -#define SKP_ADD16(a, b) ((a) + (b)) -#define SKP_ADD32(a, b) ((a) + (b)) -#define SKP_ADD64(a, b) ((a) + (b)) - -#define SKP_SUB16(a, b) ((a) - (b)) -#define SKP_SUB32(a, b) ((a) - (b)) -#define SKP_SUB64(a, b) ((a) - (b)) - -#define SKP_SAT8(a) ((a) > SKP_int8_MAX ? SKP_int8_MAX : \ - ((a) < SKP_int8_MIN ? SKP_int8_MIN : (a))) -#define SKP_SAT16(a) ((a) > SKP_int16_MAX ? SKP_int16_MAX : \ - ((a) < SKP_int16_MIN ? SKP_int16_MIN : (a))) -#define SKP_SAT32(a) ((a) > SKP_int32_MAX ? SKP_int32_MAX : \ - ((a) < SKP_int32_MIN ? SKP_int32_MIN : (a))) - -#define SKP_CHECK_FIT8(a) (a) -#define SKP_CHECK_FIT16(a) (a) -#define SKP_CHECK_FIT32(a) (a) - -#define SKP_ADD_SAT16(a, b) (SKP_int16)SKP_SAT16( SKP_ADD32( (SKP_int32)(a), (b) ) ) -#define SKP_ADD_SAT64(a, b) ((((a) + (b)) & 0x8000000000000000LL) == 0 ? \ - ((((a) & (b)) & 0x8000000000000000LL) != 0 ? SKP_int64_MIN : (a)+(b)) : \ - ((((a) | (b)) & 0x8000000000000000LL) == 0 ? SKP_int64_MAX : (a)+(b)) ) - -#define SKP_SUB_SAT16(a, b) (SKP_int16)SKP_SAT16( SKP_SUB32( (SKP_int32)(a), (b) ) ) -#define SKP_SUB_SAT64(a, b) ((((a)-(b)) & 0x8000000000000000LL) == 0 ? \ - (( (a) & ((b)^0x8000000000000000LL) & 0x8000000000000000LL) ? SKP_int64_MIN : (a)-(b)) : \ - ((((a)^0x8000000000000000LL) & (b) & 0x8000000000000000LL) ? SKP_int64_MAX : (a)-(b)) ) - -/* Saturation for positive input values */ -#define SKP_POS_SAT32(a) ((a) > SKP_int32_MAX ? SKP_int32_MAX : (a)) - -/* Add with saturation for positive input values */ -#define SKP_ADD_POS_SAT8(a, b) ((((a)+(b)) & 0x80) ? SKP_int8_MAX : ((a)+(b))) -#define SKP_ADD_POS_SAT16(a, b) ((((a)+(b)) & 0x8000) ? SKP_int16_MAX : ((a)+(b))) -#define SKP_ADD_POS_SAT32(a, b) ((((a)+(b)) & 0x80000000) ? SKP_int32_MAX : ((a)+(b))) -#define SKP_ADD_POS_SAT64(a, b) ((((a)+(b)) & 0x8000000000000000LL) ? SKP_int64_MAX : ((a)+(b))) - -#define SKP_LSHIFT8(a, shift) ((a)<<(shift)) // shift >= 0, shift < 8 -#define SKP_LSHIFT16(a, shift) ((a)<<(shift)) // shift >= 0, shift < 16 -#define SKP_LSHIFT32(a, shift) ((a)<<(shift)) // shift >= 0, shift < 32 -#define SKP_LSHIFT64(a, shift) ((a)<<(shift)) // shift >= 0, shift < 64 -#define SKP_LSHIFT(a, shift) SKP_LSHIFT32(a, shift) // shift >= 0, shift < 32 - -#define SKP_RSHIFT8(a, shift) ((a)>>(shift)) // shift >= 0, shift < 8 -#define SKP_RSHIFT16(a, shift) ((a)>>(shift)) // shift >= 0, shift < 16 -#define SKP_RSHIFT32(a, shift) ((a)>>(shift)) // shift >= 0, shift < 32 -#define SKP_RSHIFT64(a, shift) ((a)>>(shift)) // shift >= 0, shift < 64 -#define SKP_RSHIFT(a, shift) SKP_RSHIFT32(a, shift) // shift >= 0, shift < 32 - -/* saturates before shifting */ -#define SKP_LSHIFT_SAT16(a, shift) (SKP_LSHIFT16( SKP_LIMIT( (a), SKP_RSHIFT16( SKP_int16_MIN, (shift) ), \ - SKP_RSHIFT16( SKP_int16_MAX, (shift) ) ), (shift) )) -#define SKP_LSHIFT_SAT32(a, shift) (SKP_LSHIFT32( SKP_LIMIT( (a), SKP_RSHIFT32( SKP_int32_MIN, (shift) ), \ - SKP_RSHIFT32( SKP_int32_MAX, (shift) ) ), (shift) )) - -#define SKP_LSHIFT_ovflw(a, shift) ((a)<<(shift)) // shift >= 0, allowed to overflow -#define SKP_LSHIFT_uint(a, shift) ((a)<<(shift)) // shift >= 0 -#define SKP_RSHIFT_uint(a, shift) ((a)>>(shift)) // shift >= 0 - -#define SKP_ADD_LSHIFT(a, b, shift) ((a) + SKP_LSHIFT((b), (shift))) // shift >= 0 -#define SKP_ADD_LSHIFT32(a, b, shift) SKP_ADD32((a), SKP_LSHIFT32((b), (shift))) // shift >= 0 -#define SKP_ADD_LSHIFT_uint(a, b, shift) ((a) + SKP_LSHIFT_uint((b), (shift))) // shift >= 0 -#define SKP_ADD_RSHIFT(a, b, shift) ((a) + SKP_RSHIFT((b), (shift))) // shift >= 0 -#define SKP_ADD_RSHIFT32(a, b, shift) SKP_ADD32((a), SKP_RSHIFT32((b), (shift))) // shift >= 0 -#define SKP_ADD_RSHIFT_uint(a, b, shift) ((a) + SKP_RSHIFT_uint((b), (shift))) // shift >= 0 -#define SKP_SUB_LSHIFT32(a, b, shift) SKP_SUB32((a), SKP_LSHIFT32((b), (shift))) // shift >= 0 -#define SKP_SUB_RSHIFT32(a, b, shift) SKP_SUB32((a), SKP_RSHIFT32((b), (shift))) // shift >= 0 - -/* Requires that shift > 0 */ -#define SKP_RSHIFT_ROUND(a, shift) ((shift) == 1 ? ((a) >> 1) + ((a) & 1) : (((a) >> ((shift) - 1)) + 1) >> 1) -#define SKP_RSHIFT_ROUND64(a, shift) ((shift) == 1 ? ((a) >> 1) + ((a) & 1) : (((a) >> ((shift) - 1)) + 1) >> 1) - -/* Number of rightshift required to fit the multiplication */ -#define SKP_NSHIFT_MUL_32_32(a, b) ( -(31- (32-SKP_Silk_CLZ32(SKP_abs(a)) + (32-SKP_Silk_CLZ32(SKP_abs(b))))) ) -#define SKP_NSHIFT_MUL_16_16(a, b) ( -(15- (16-SKP_Silk_CLZ16(SKP_abs(a)) + (16-SKP_Silk_CLZ16(SKP_abs(b))))) ) - - -#define SKP_min(a, b) (((a) < (b)) ? (a) : (b)) -#define SKP_max(a, b) (((a) > (b)) ? (a) : (b)) - -/* Macro to convert floating-point constants to fixed-point */ -#define SKP_FIX_CONST( C, Q ) ((SKP_int32)((C) * (1 << (Q)) + 0.5)) - -/* SKP_min() versions with typecast in the function call */ -SKP_INLINE SKP_int SKP_min_int(SKP_int a, SKP_int b) -{ - return (((a) < (b)) ? (a) : (b)); -} -SKP_INLINE SKP_int16 SKP_min_16(SKP_int16 a, SKP_int16 b) -{ - return (((a) < (b)) ? (a) : (b)); -} -SKP_INLINE SKP_int32 SKP_min_32(SKP_int32 a, SKP_int32 b) -{ - return (((a) < (b)) ? (a) : (b)); -} -SKP_INLINE SKP_int64 SKP_min_64(SKP_int64 a, SKP_int64 b) -{ - return (((a) < (b)) ? (a) : (b)); -} - -/* SKP_min() versions with typecast in the function call */ -SKP_INLINE SKP_int SKP_max_int(SKP_int a, SKP_int b) -{ - return (((a) > (b)) ? (a) : (b)); -} -SKP_INLINE SKP_int16 SKP_max_16(SKP_int16 a, SKP_int16 b) -{ - return (((a) > (b)) ? (a) : (b)); -} -SKP_INLINE SKP_int32 SKP_max_32(SKP_int32 a, SKP_int32 b) -{ - return (((a) > (b)) ? (a) : (b)); -} -SKP_INLINE SKP_int64 SKP_max_64(SKP_int64 a, SKP_int64 b) -{ - return (((a) > (b)) ? (a) : (b)); -} - -#define SKP_LIMIT( a, limit1, limit2) ((limit1) > (limit2) ? ((a) > (limit1) ? (limit1) : ((a) < (limit2) ? (limit2) : (a))) \ - : ((a) > (limit2) ? (limit2) : ((a) < (limit1) ? (limit1) : (a)))) - -//#define SKP_non_neg(a) ((a) & ((-(a)) >> (8 * sizeof(a) - 1))) /* doesn't seem faster than SKP_max(0, a); - -#define SKP_abs(a) (((a) > 0) ? (a) : -(a)) // Be careful, SKP_abs returns wrong when input equals to SKP_intXX_MIN -#define SKP_abs_int(a) (((a) ^ ((a) >> (8 * sizeof(a) - 1))) - ((a) >> (8 * sizeof(a) - 1))) -#define SKP_abs_int32(a) (((a) ^ ((a) >> 31)) - ((a) >> 31)) -#define SKP_abs_int64(a) (((a) > 0) ? (a) : -(a)) - -#define SKP_sign(a) ((a) > 0 ? 1 : ( (a) < 0 ? -1 : 0 )) - -#define SKP_sqrt(a) (sqrt(a)) - -/* PSEUDO-RANDOM GENERATOR */ -/* Make sure to store the result as the seed for the next call (also in between */ -/* frames), otherwise result won't be random at all. When only using some of the */ -/* bits, take the most significant bits by right-shifting. Do not just mask off */ -/* the lowest bits. */ -#define SKP_RAND(seed) (SKP_MLA_ovflw(907633515, (seed), 196314165)) - -// Add some multiplication functions that can be easily mapped to ARM. - -// SKP_SMMUL: Signed top word multiply. -// ARMv6 2 instruction cycles. -// ARMv3M+ 3 instruction cycles. use SMULL and ignore LSB registers.(except xM) -//#define SKP_SMMUL(a32, b32) (SKP_int32)SKP_RSHIFT(SKP_SMLAL(SKP_SMULWB((a32), (b32)), (a32), SKP_RSHIFT_ROUND((b32), 16)), 16) -// the following seems faster on x86 -#define SKP_SMMUL(a32, b32) (SKP_int32)SKP_RSHIFT64(SKP_SMULL((a32), (b32)), 32) - -#include "SKP_Silk_Inlines.h" - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef _SKP_SILK_SIGPROC_FIX_H_ +#define _SKP_SILK_SIGPROC_FIX_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define SigProc_MAX_ORDER_LPC 16 /* max order of the LPC analysis in schur() and k2a() */ +#define SigProc_MAX_CORRELATION_LENGTH 640 /* max input length to the correlation */ +#include "SKP_Silk_typedef.h" +#include +#include /* for abs() */ +#include "SKP_Silk_macros.h" +#include "SKP_Silk_resample_rom.h" + + + +/********************************************************************/ +/* SIGNAL PROCESSING FUNCTIONS */ +/********************************************************************/ + +/* downsample by a factor 2 */ +void SKP_Silk_resample_1_2( + const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ + SKP_int32 *S, /* I/O: State vector [6] */ + SKP_int16 *out, /* O: 8 kHz signal [len] */ + SKP_int32 *scratch, /* I: Scratch memory [4*len] */ + const SKP_int32 len /* I: Number of OUTPUT samples */ +); + +/*! + * downsample by a factor 2, coarser (good for resampling audio) + */ +void SKP_Silk_resample_1_2_coarse( + const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ + SKP_int32 *S, /* I/O: state vector [4] */ + SKP_int16 *out, /* O: 8 kHz signal [len] */ + SKP_int32 *scratch, /* I: scratch memory [3*len] */ + const SKP_int32 len /* I: number of OUTPUT samples */ +); + +/*! + * downsample by a factor 2, coarsest (good for signals that are already oversampled, or for analysis purposes) + */ +void SKP_Silk_resample_1_2_coarsest( + const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ + SKP_int32 *S, /* I/O: State vector [2] */ + SKP_int16 *out, /* O: 8 kHz signal [len] */ + SKP_int32 *scratch, /* I: Scratch memory [3*len] */ + const SKP_int32 len /* I: Number of OUTPUT samples */ +); + +/*! + * upsample by a factor 2, coarser (good for resampling audio) + */ +void SKP_Silk_resample_2_1_coarse( + const SKP_int16 *in, /* I: 8 kHz signal [len] */ + SKP_int32 *S, /* I/O: State vector [4] */ + SKP_int16 *out, /* O: 16 kHz signal [2*len] */ + SKP_int32 *scratch, /* I: Scratch memory [3*len] */ + const SKP_int32 len /* I: Number of INPUT samples */ +); + +/*! + * Resamples by a factor 1/3 + */ +void SKP_Silk_resample_1_3( + SKP_int16 *out, /* O: Fs_low signal [inLen/3] */ + SKP_int32 *S, /* I/O: State vector [7] */ + const SKP_int16 *in, /* I: Fs_high signal [inLen] */ + const SKP_int32 inLen /* I: Input length, must be a multiple of 3 */ +); + +/*! + * Resamples by a factor 3/1 + */ +void SKP_Silk_resample_3_1( + SKP_int16 *out, /* O: Fs_high signal [inLen*3] */ + SKP_int32 *S, /* I/O: State vector [7] */ + const SKP_int16 *in, /* I: Fs_low signal [inLen] */ + const SKP_int32 inLen /* I: Input length */ +); + +/*! + * Resamples by a factor 2/3 + */ +void SKP_Silk_resample_2_3( + SKP_int16 *out, /* O: Fs_low signal [inLen * 2/3] */ + SKP_int32 *S, /* I/O: State vector [7+4] */ + const SKP_int16 *in, /* I: Fs_high signal [inLen] */ + const SKP_int inLen /* I: Input length, must be a multiple of 3 */ +); + +/*! + * Resamples by a factor 3/2 + */ +void SKP_Silk_resample_3_2( + SKP_int16 *out, /* O: Fs_high signal [inLen*3/2] */ + SKP_int32 *S, /* I/O: State vector [7+4] */ + const SKP_int16 *in, /* I: Fs_low signal [inLen] */ + SKP_int inLen /* I: Input length, must be a multiple of 2 */ +); + +/*! + * Resamples by a factor 4/3 + */ +void SKP_Silk_resample_4_3( + SKP_int16 *out, /* O: Fs_low signal [inLen * 4/3] */ + SKP_int32 *S, /* I/O: State vector [7+4+4] */ + const SKP_int16 *in, /* I: Fs_high signal [inLen] */ + const SKP_int inLen /* I: input length, must be a multiple of 3 */ +); + +/*! + * Resamples by a factor 3/4 + */ + void SKP_Silk_resample_3_4( + SKP_int16 *out, /* O: Fs_high signal [inLen*3/4] */ + SKP_int32 *S, /* I/O: State vector [7+2+6] */ + const SKP_int16 *in, /* I: Fs_low signal [inLen] */ + SKP_int inLen /* I: Input length, must be a multiple of 4 */ +); + +/*! + * resample with a factor 2/3 coarse + */ +void SKP_Silk_resample_2_3_coarse( + SKP_int16 *out, /* O: Output signal */ + SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ + const SKP_int16 *in, /* I: Input signal */ + const SKP_int frameLenIn, /* I: Number of input samples */ + SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ +); + +/*! + * resample with a factor 2/3 coarsest + */ +void SKP_Silk_resample_2_3_coarsest( + SKP_int16 *out, /* O: Output signal */ + SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ + const SKP_int16 *in, /* I: Input signal */ + const SKP_int frameLenIn, /* I: Number of input samples */ + SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ +); + +/*! + * First order low-pass filter, with input as SKP_int16, running at 48 kHz + */ +void SKP_Silk_lowpass_short( + const SKP_int16 *in, /* I: Q15 48 kHz signal; [len] */ + SKP_int32 *S, /* I/O: Q25 state; length = 1 */ + SKP_int32 *out, /* O: Q25 48 kHz signal; [len] */ + const SKP_int32 len /* O: Signal length */ +); + +/*! + * First order low-pass filter, with input as SKP_int32, running at 48 kHz + */ +void SKP_Silk_lowpass_int( + const SKP_int32 *in, /* I: Q25 48 kHz signal; length = len */ + SKP_int32 *S, /* I/O: Q25 state; length = 1 */ + SKP_int32 *out, /* O: Q25 48 kHz signal; length = len */ + const SKP_int32 len /* I: Number of samples */ +); + +/*! + * First-order allpass filter + */ +void SKP_Silk_allpass_int( + const SKP_int32 *in, /* I: Q25 input signal [len] */ + SKP_int32 *S, /* I/O: Q25 state [1] */ + SKP_int A, /* I: Q15 coefficient (0 <= A < 32768) */ + SKP_int32 *out, /* O: Q25 output signal [len] */ + const SKP_int32 len /* I: Number of samples */ +); + +/*! + * second order ARMA filter + * can handle (slowly) varying coefficients + */ +void SKP_Silk_biquad( + const SKP_int16 *in, /* I: input signal */ + const SKP_int16 *B, /* I: MA coefficients, Q13 [3] */ + const SKP_int16 *A, /* I: AR coefficients, Q13 [2] */ + SKP_int32 *S, /* I/O: state vector [2] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len /* I: signal length */ +); +/*! + * second order ARMA filter; + * slower than biquad() but uses more precise coefficients + * can handle (slowly) varying coefficients + */ +void SKP_Silk_biquad_alt( + const SKP_int16 *in, /* I: input signal */ + const SKP_int32 *B_Q28, /* I: MA coefficients [3] */ + const SKP_int32 *A_Q28, /* I: AR coefficients [2] */ + SKP_int32 *S, /* I/O: state vector [2] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len /* I: signal length (must be even) */ +); + +/*! + * variable order MA filter. Prediction error filter implementation. Coeficients negated and starting with coef to x[n - 1] + */ +void SKP_Silk_MA_Prediction( + const SKP_int16 *in, /* I: Input signal */ + const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ + SKP_int32 *S, /* I/O: State vector [order] */ + SKP_int16 *out, /* O: Output signal */ + const SKP_int32 len, /* I: Signal length */ + const SKP_int32 order /* I: Filter order */ +); + +void SKP_Silk_MA_Prediction_Q13( + const SKP_int16 *in, /* I: input signal */ + const SKP_int16 *B, /* I: MA prediction coefficients, Q13 [order] */ + SKP_int32 *S, /* I/O: state vector [order] */ + SKP_int16 *out, /* O: output signal */ + SKP_int32 len, /* I: signal length */ + SKP_int32 order /* I: filter order */ +); + +/*! + * 16th order AR filter for LPC synthesis, coefficients are in Q12 + */ +void SKP_Silk_LPC_synthesis_order16( + const SKP_int16 *in, /* I: excitation signal */ + const SKP_int16 *A_Q12, /* I: AR coefficients [16], between -8_Q0 and 8_Q0 */ + const SKP_int32 Gain_Q26, /* I: gain */ + SKP_int32 *S, /* I/O: state vector [16] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len /* I: signal length, must be multiple of 16 */ +); + +/* variable order MA prediction error filter. */ +/* Inverse filter of SKP_Silk_LPC_synthesis_filter */ +void SKP_Silk_LPC_analysis_filter( + const SKP_int16 *in, /* I: Input signal */ + const SKP_int16 *B, /* I: MA prediction coefficients, Q12 [order] */ + SKP_int16 *S, /* I/O: State vector [order] */ + SKP_int16 *out, /* O: Output signal */ + const SKP_int32 len, /* I: Signal length */ + const SKP_int32 Order /* I: Filter order */ +); + +/* even order AR filter */ +void SKP_Silk_LPC_synthesis_filter( + const SKP_int16 *in, /* I: excitation signal */ + const SKP_int16 *A_Q12, /* I: AR coefficients [Order], between -8_Q0 and 8_Q0 */ + const SKP_int32 Gain_Q26, /* I: gain */ + SKP_int32 *S, /* I/O: state vector [Order] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len, /* I: signal length */ + const SKP_int Order /* I: filter order, must be even */ +); + +/* Chirp (bandwidth expand) LP AR filter */ +void SKP_Silk_bwexpander( + SKP_int16 *ar, /* I/O AR filter to be expanded (without leading 1) */ + const SKP_int d, /* I Length of ar */ + SKP_int32 chirp_Q16 /* I Chirp factor (typically in the range 0 to 1) */ +); + +/* Chirp (bandwidth expand) LP AR filter */ +void SKP_Silk_bwexpander_32( + SKP_int32 *ar, /* I/O AR filter to be expanded (without leading 1) */ + const SKP_int d, /* I Length of ar */ + SKP_int32 chirp_Q16 /* I Chirp factor in Q16 */ +); + +/* Compute inverse of LPC prediction gain, and */ +/* test if LPC coefficients are stable (all poles within unit circle) */ +SKP_int SKP_Silk_LPC_inverse_pred_gain( /* O: Returns 1 if unstable, otherwise 0 */ + SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ + const SKP_int16 *A_Q12, /* I: Prediction coefficients, Q12 [order] */ + const SKP_int order /* I: Prediction order */ +); + +SKP_int SKP_Silk_LPC_inverse_pred_gain_Q13( /* O: returns 1 if unstable, otherwise 0 */ + SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */ + const SKP_int16 *A_Q13, /* I: Prediction coefficients, Q13 [order] */ + const SKP_int order /* I: Prediction order */ +); + +/* split signal in two decimated bands using first-order allpass filters */ +void SKP_Silk_ana_filt_bank_1( + const SKP_int16 *in, /* I: Input signal [N] */ + SKP_int32 *S, /* I/O: State vector [2] */ + SKP_int16 *outL, /* O: Low band [N/2] */ + SKP_int16 *outH, /* O: High band [N/2] */ + SKP_int32 *scratch, /* I: Scratch memory [3*N/2] */ + const SKP_int32 N /* I: Number of input samples */ +); + +/********************************************************************/ +/* SCALAR FUNCTIONS */ +/********************************************************************/ + +/* approximation of 128 * log2() (exact inverse of approx 2^() below) */ +/* convert input to a log scale */ +SKP_int32 SKP_Silk_lin2log(const SKP_int32 inLin); /* I: input in linear scale */ + +/* Approximation of a sigmoid function */ +SKP_int SKP_Silk_sigm_Q15(SKP_int in_Q5); + +/* approximation of 2^() (exact inverse of approx log2() above) */ +/* convert input to a linear scale */ +SKP_int32 SKP_Silk_log2lin(const SKP_int32 inLog_Q7); /* I: input on log scale */ + +/* Function that returns the maximum absolut value of the input vector */ +SKP_int16 SKP_Silk_int16_array_maxabs( /* O Maximum absolute value, max: 2^15-1 */ + const SKP_int16 *vec, /* I Input vector [len] */ + const SKP_int32 len /* I Length of input vector */ +); + +/* Compute number of bits to right shift the sum of squares of a vector */ +/* of int16s to make it fit in an int32 */ +void SKP_Silk_sum_sqr_shift( + SKP_int32 *energy, /* O Energy of x, after shifting to the right */ + SKP_int *shift, /* O Number of bits right shift applied to energy */ + const SKP_int16 *x, /* I Input vector */ + SKP_int len /* I Length of input vector */ +); + +/* Calculates the reflection coefficients from the correlation sequence */ +/* Faster than schur64(), but much less accurate. */ +/* Uses SMLAWB(), requiring armv5E and higher. */ +void SKP_Silk_schur( + SKP_int16 *rc_Q15, /* O: reflection coefficients [order] Q15 */ + const SKP_int32 *c, /* I: correlations [order+1] */ + const SKP_int32 order /* I: prediction order */ +); + +/* Calculates the reflection coefficients from the correlation sequence */ +/* Slower than schur(), but more accurate. */ +/* Uses SMULL(), available on armv4 */ +SKP_int32 SKP_Silk_schur64( /* O: returns residual energy */ + SKP_int32 rc_Q16[], /* O: Reflection coefficients [order] Q16 */ + const SKP_int32 c[], /* I: Correlations [order+1] */ + SKP_int32 order /* I: Prediction order */ +); + +/* Step up function, converts reflection coefficients to prediction coefficients */ +void SKP_Silk_k2a( + SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ + const SKP_int16 *rc_Q15, /* I: Reflection coefficients [order] Q15 */ + const SKP_int32 order /* I: Prediction order */ +); + +/* Step up function, converts reflection coefficients to prediction coefficients */ +void SKP_Silk_k2a_Q16( + SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ + const SKP_int32 *rc_Q16, /* I: Reflection coefficients [order] Q16 */ + const SKP_int32 order /* I: Prediction order */ +); + +/* Apply sine window to signal vector. */ +/* Window types: */ +/* 0 -> sine window from 0 to pi */ +/* 1 -> sine window from 0 to pi/2 */ +/* 2 -> sine window from pi/2 to pi */ +/* every other sample of window is linearly interpolated, for speed */ +void SKP_Silk_apply_sine_window( + SKP_int16 px_win[], /* O Pointer to windowed signal */ + const SKP_int16 px[], /* I Pointer to input signal */ + const SKP_int win_type, /* I Selects a window type */ + const SKP_int length /* I Window length, multiple of 4 */ +); + +/* Compute autocorrelation */ +void SKP_Silk_autocorr( + SKP_int32 *results, /* O Result (length correlationCount) */ + SKP_int32 *scale, /* O Scaling of the correlation vector */ + const SKP_int16 *inputData, /* I Input data to correlate */ + const SKP_int inputDataSize, /* I Length of input */ + const SKP_int correlationCount /* I Number of correlation taps to compute */ +); + +/* Pitch estimator */ +#define SigProc_PITCH_EST_MIN_COMPLEX 0 +#define SigProc_PITCH_EST_MID_COMPLEX 1 +#define SigProc_PITCH_EST_MAX_COMPLEX 2 + +void SKP_Silk_decode_pitch( + SKP_int lagIndex, /* I */ + SKP_int contourIndex, /* O */ + SKP_int pitch_lags[], /* O 4 pitch values */ + SKP_int Fs_kHz /* I sampling frequency (kHz) */ +); + +SKP_int SKP_Silk_pitch_analysis_core( /* O Voicing estimate: 0 voiced, 1 unvoiced */ + const SKP_int16 *signal, /* I Signal of length PITCH_EST_FRAME_LENGTH_MS*Fs_kHz */ + SKP_int *pitch_out, /* O 4 pitch lag values */ + SKP_int *lagIndex, /* O Lag Index */ + SKP_int *contourIndex, /* O Pitch contour Index */ + SKP_int *LTPCorr_Q15, /* I/O Normalized correlation; input: value from previous frame */ + SKP_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */ + const SKP_int32 search_thres1_Q16, /* I First stage threshold for lag candidates 0 - 1 */ + const SKP_int search_thres2_Q15, /* I Final threshold for lag candidates 0 - 1 */ + const SKP_int Fs_kHz, /* I Sample frequency (kHz) */ + const SKP_int complexity /* I Complexity setting, 0-2, where 2 is highest */ +); + +/* parameter defining the size and accuracy of the piecewise linear */ +/* cosine approximatin table. */ + +#define LSF_COS_TAB_SZ_FIX 128 +/* rom table with cosine values */ +extern const SKP_int SKP_Silk_LSFCosTab_FIX_Q12[ LSF_COS_TAB_SZ_FIX + 1 ]; + +void SKP_Silk_LPC_fit( + SKP_int16 *a_QQ, /* O stabilized LPC vector, Q(24-rshift) [L] */ + SKP_int32 *a_Q24, /* I LPC vector [L] */ + const SKP_int QQ, /* I Q domain of output LPC vector */ + const SKP_int L /* I Number of LPC parameters in the input vector */ +); + +/* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */ +/* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */ +void SKP_Silk_A2NLSF( + SKP_int *NLSF, /* O Normalized Line Spectral Frequencies, Q15 (0 - (2^15-1)), [d] */ + SKP_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */ + const SKP_int d /* I Filter order (must be even) */ +); + +/* compute whitening filter coefficients from normalized line spectral frequencies */ +void SKP_Silk_NLSF2A( + SKP_int16 *a, /* o monic whitening filter coefficients in Q12, [d] */ + const SKP_int *NLSF, /* i normalized line spectral frequencies in Q15, [d] */ + const SKP_int d /* i filter order (should be even) */ +); + +void SKP_Silk_insertion_sort_increasing( + SKP_int32 *a, /* I/O Unsorted / Sorted vector */ + SKP_int *index, /* O: Index vector for the sorted elements */ + const SKP_int L, /* I: Vector length */ + const SKP_int K /* I: Number of correctly sorted positions */ +); + +void SKP_Silk_insertion_sort_decreasing( + SKP_int *a, /* I/O: Unsorted / Sorted vector */ + SKP_int *index, /* O: Index vector for the sorted elements */ + const SKP_int L, /* I: Vector length */ + const SKP_int K /* I: Number of correctly sorted positions */ +); + +void SKP_Silk_insertion_sort_decreasing_int16( + SKP_int16 *a, /* I/O: Unsorted / Sorted vector */ + SKP_int *index, /* O: Index vector for the sorted elements */ + const SKP_int L, /* I: Vector length */ + const SKP_int K /* I: Number of correctly sorted positions */ +); + +void SKP_Silk_insertion_sort_increasing_all_values( + SKP_int *a, /* I/O: Unsorted / Sorted vector */ + const SKP_int L /* I: Vector length */ +); + +/* NLSF stabilizer, for a single input data vector */ +void SKP_Silk_NLSF_stabilize( + SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vector in Q15 [L] */ + const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ + const SKP_int L /* I: Number of NLSF parameters in the input vector */ +); + +/* NLSF stabilizer, over multiple input column data vectors */ +void SKP_Silk_NLSF_stabilize_multi( + SKP_int *NLSF_Q15, /* I/O: Unstable/stabilized normalized LSF vectors in Q15 [LxN] */ + const SKP_int *NDeltaMin_Q15, /* I: Normalized delta min vector in Q15, NDeltaMin_Q15[L] must be >= 1 [L+1] */ + const SKP_int N, /* I: Number of input vectors to be stabilized */ + const SKP_int L /* I: NLSF vector dimension */ +); + +/* Laroia low complexity NLSF weights */ +void SKP_Silk_NLSF_VQ_weights_laroia( + SKP_int *pNLSFW_Q6, /* O: Pointer to input vector weights [D x 1] */ + const SKP_int *pNLSF_Q15, /* I: Pointer to input vector [D x 1] */ + const SKP_int D /* I: Input vector dimension (even) */ +); + +/* Compute reflection coefficients from input signal */ +void SKP_Silk_burg_modified( + SKP_int32 *res_nrg, /* O residual energy */ + SKP_int *res_nrgQ, /* O residual energy Q value */ + SKP_int32 A_Q16[], /* O prediction coefficients (length order) */ + const SKP_int16 x[], /* I input signal, length: nb_subfr * ( D + subfr_length ) */ + const SKP_int subfr_length, /* I input signal subframe length (including D preceeding samples) */ + const SKP_int nb_subfr, /* I number of subframes stacked in x */ + const SKP_int32 WhiteNoiseFrac_Q32, /* I fraction added to zero-lag autocorrelation */ + const SKP_int D /* I order */ +); + +/* Multiply a vector by a constant */ +void SKP_Silk_scale_vector16_Q14( + SKP_int16 *data1, + SKP_int gain_Q14, /* Gain in Q14 */ + SKP_int dataSize +); + +/* Copy and multiply a vector by a constant */ +void SKP_Silk_scale_copy_vector16( + SKP_int16 *data_out, + const SKP_int16 *data_in, + SKP_int32 gain_Q16, /* I: gain in Q16 */ + const SKP_int dataSize /* I: length */ +); + +void SKP_Silk_scale_vector32_16_Q14( + SKP_int32 *data1, /* I/O: Q0/Q0 */ + SKP_int gain_Q14, /* I: Q14 */ + SKP_int dataSize /* I: length */ +); + +/* Multiply a vector by a constant, does not saturate output data */ +void SKP_Silk_scale_vector32_Q16( + SKP_int32 *data1, /* I/O: Q0/Q0 */ + SKP_int32 gain_Q16, /* I: gain in Q16 ( SKP_int16_MIN <= gain_Q16 <= SKP_int16_MAX + 65536 ) */ + const SKP_int dataSize /* I: length */ +); + +/* Some for the LTP related function requires Q26 to work.*/ +void SKP_Silk_scale_vector32_Q26_lshift_18( + SKP_int32 *data1, /* I/O: Q0/Q18 */ + SKP_int32 gain_Q26, /* I: Q26 */ + SKP_int dataSize /* I: length */ +); + +/********************************************************************/ +/* INLINE ARM MATH */ +/********************************************************************/ + +/* return sum(inVec1[i]*inVec2[i]) */ +/* inVec1 and inVec2 should be increasing ordered, and starting address should be 4 byte aligned. (a factor of 4)*/ +SKP_int32 SKP_Silk_inner_prod_aligned( + const SKP_int16* const inVec1, /* I input vector 1 */ + const SKP_int16* const inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +); + +SKP_int32 SKP_Silk_inner_prod16_aligned_sat( + const SKP_int16* const inVec1, /* I input vector 1 */ + const SKP_int16* const inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +); + +SKP_int64 SKP_Silk_inner_prod_aligned_64( + const SKP_int32 *inVec1, /* I input vector 1 */ + const SKP_int32 *inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +); + +SKP_int64 SKP_Silk_inner_prod16_aligned_64( + const SKP_int16 *inVec1, /* I input vector 1 */ + const SKP_int16 *inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +); +/********************************************************************/ +/* MACROS */ +/********************************************************************/ + +/* Define 4-byte aligned array of SKP_int16 */ +#define SKP_array_of_int16_4_byte_aligned( arrayName, nElements ) \ + SKP_int32 dummy_int32 ## arrayName; \ + SKP_int16 arrayName[ (nElements) ] + +/* Useful Macros that can be adjusted to other platforms */ +#define SKP_memcpy(a, b, c) memcpy((a), (b), (c)) /* Dest, Src, ByteCount */ +#define SKP_memset(a, b, c) memset((a), (b), (c)) /* Dest, value, ByteCount */ +#define SKP_memmove(a, b, c) memmove((a), (b), (c)) /* Dest, Src, ByteCount */ +/* fixed point macros */ + +// (a32 * b32) output have to be 32bit int +#define SKP_MUL(a32, b32) ((a32) * (b32)) + +// (a32 * b32) output have to be 32bit uint +#define SKP_MUL_uint(a32, b32) SKP_MUL(a32, b32) + +// a32 + (b32 * c32) output have to be 32bit int +#define SKP_MLA(a32, b32, c32) SKP_ADD32((a32),((b32) * (c32))) + +// a32 + (b32 * c32) output have to be 32bit uint +#define SKP_MLA_uint(a32, b32, c32) SKP_MLA(a32, b32, c32) + +// ((a32 >> 16) * (b32 >> 16)) output have to be 32bit int +#define SKP_SMULTT(a32, b32) (((a32) >> 16) * ((b32) >> 16)) + +// a32 + ((a32 >> 16) * (b32 >> 16)) output have to be 32bit int +#define SKP_SMLATT(a32, b32, c32) SKP_ADD32((a32),((b32) >> 16) * ((c32) >> 16)) + +#define SKP_SMLALBB(a64, b16, c16) SKP_ADD64((a64),(SKP_int64)((SKP_int32)(b16) * (SKP_int32)(c16))) + +// (a32 * b32) +#define SKP_SMULL(a32, b32) ((SKP_int64)(a32) * /*(SKP_int64)*/(b32)) + +// multiply-accumulate macros that allow overflow in the addition (ie, no asserts in debug mode) +#define SKP_MLA_ovflw(a32, b32, c32) SKP_MLA(a32, b32, c32) +#ifndef SKP_SMLABB_ovflw +# define SKP_SMLABB_ovflw(a32, b32, c32) SKP_SMLABB(a32, b32, c32) +#endif +#define SKP_SMLABT_ovflw(a32, b32, c32) SKP_SMLABT(a32, b32, c32) +#define SKP_SMLATT_ovflw(a32, b32, c32) SKP_SMLATT(a32, b32, c32) +#define SKP_SMLAWB_ovflw(a32, b32, c32) SKP_SMLAWB(a32, b32, c32) +#define SKP_SMLAWT_ovflw(a32, b32, c32) SKP_SMLAWT(a32, b32, c32) + +#define SKP_DIV64_32(a64, b32) ((a64)/(b32)) /* TODO: rewrite it as a set of SKP_DIV32.*/ + +#define SKP_DIV32_16(a32, b16) ((SKP_int32)((a32) / (b16))) +#define SKP_DIV32(a32, b32) ((SKP_int32)((a32) / (b32))) + +// These macros enables checking for overflow in SKP_Silk_API_Debug.h +#define SKP_ADD16(a, b) ((a) + (b)) +#define SKP_ADD32(a, b) ((a) + (b)) +#define SKP_ADD64(a, b) ((a) + (b)) + +#define SKP_SUB16(a, b) ((a) - (b)) +#define SKP_SUB32(a, b) ((a) - (b)) +#define SKP_SUB64(a, b) ((a) - (b)) + +#define SKP_SAT8(a) ((a) > SKP_int8_MAX ? SKP_int8_MAX : \ + ((a) < SKP_int8_MIN ? SKP_int8_MIN : (a))) +#define SKP_SAT16(a) ((a) > SKP_int16_MAX ? SKP_int16_MAX : \ + ((a) < SKP_int16_MIN ? SKP_int16_MIN : (a))) +#define SKP_SAT32(a) ((a) > SKP_int32_MAX ? SKP_int32_MAX : \ + ((a) < SKP_int32_MIN ? SKP_int32_MIN : (a))) + +#define SKP_CHECK_FIT8(a) (a) +#define SKP_CHECK_FIT16(a) (a) +#define SKP_CHECK_FIT32(a) (a) + +#define SKP_ADD_SAT16(a, b) (SKP_int16)SKP_SAT16( SKP_ADD32( (SKP_int32)(a), (b) ) ) +#define SKP_ADD_SAT64(a, b) ((((a) + (b)) & 0x8000000000000000LL) == 0 ? \ + ((((a) & (b)) & 0x8000000000000000LL) != 0 ? SKP_int64_MIN : (a)+(b)) : \ + ((((a) | (b)) & 0x8000000000000000LL) == 0 ? SKP_int64_MAX : (a)+(b)) ) + +#define SKP_SUB_SAT16(a, b) (SKP_int16)SKP_SAT16( SKP_SUB32( (SKP_int32)(a), (b) ) ) +#define SKP_SUB_SAT64(a, b) ((((a)-(b)) & 0x8000000000000000LL) == 0 ? \ + (( (a) & ((b)^0x8000000000000000LL) & 0x8000000000000000LL) ? SKP_int64_MIN : (a)-(b)) : \ + ((((a)^0x8000000000000000LL) & (b) & 0x8000000000000000LL) ? SKP_int64_MAX : (a)-(b)) ) + +/* Saturation for positive input values */ +#define SKP_POS_SAT32(a) ((a) > SKP_int32_MAX ? SKP_int32_MAX : (a)) + +/* Add with saturation for positive input values */ +#define SKP_ADD_POS_SAT8(a, b) ((((a)+(b)) & 0x80) ? SKP_int8_MAX : ((a)+(b))) +#define SKP_ADD_POS_SAT16(a, b) ((((a)+(b)) & 0x8000) ? SKP_int16_MAX : ((a)+(b))) +#define SKP_ADD_POS_SAT32(a, b) ((((a)+(b)) & 0x80000000) ? SKP_int32_MAX : ((a)+(b))) +#define SKP_ADD_POS_SAT64(a, b) ((((a)+(b)) & 0x8000000000000000LL) ? SKP_int64_MAX : ((a)+(b))) + +#define SKP_LSHIFT8(a, shift) ((a)<<(shift)) // shift >= 0, shift < 8 +#define SKP_LSHIFT16(a, shift) ((a)<<(shift)) // shift >= 0, shift < 16 +#define SKP_LSHIFT32(a, shift) ((a)<<(shift)) // shift >= 0, shift < 32 +#define SKP_LSHIFT64(a, shift) ((a)<<(shift)) // shift >= 0, shift < 64 +#define SKP_LSHIFT(a, shift) SKP_LSHIFT32(a, shift) // shift >= 0, shift < 32 + +#define SKP_RSHIFT8(a, shift) ((a)>>(shift)) // shift >= 0, shift < 8 +#define SKP_RSHIFT16(a, shift) ((a)>>(shift)) // shift >= 0, shift < 16 +#define SKP_RSHIFT32(a, shift) ((a)>>(shift)) // shift >= 0, shift < 32 +#define SKP_RSHIFT64(a, shift) ((a)>>(shift)) // shift >= 0, shift < 64 +#define SKP_RSHIFT(a, shift) SKP_RSHIFT32(a, shift) // shift >= 0, shift < 32 + +/* saturates before shifting */ +#define SKP_LSHIFT_SAT16(a, shift) (SKP_LSHIFT16( SKP_LIMIT( (a), SKP_RSHIFT16( SKP_int16_MIN, (shift) ), \ + SKP_RSHIFT16( SKP_int16_MAX, (shift) ) ), (shift) )) +#define SKP_LSHIFT_SAT32(a, shift) (SKP_LSHIFT32( SKP_LIMIT( (a), SKP_RSHIFT32( SKP_int32_MIN, (shift) ), \ + SKP_RSHIFT32( SKP_int32_MAX, (shift) ) ), (shift) )) + +#define SKP_LSHIFT_ovflw(a, shift) ((a)<<(shift)) // shift >= 0, allowed to overflow +#define SKP_LSHIFT_uint(a, shift) ((a)<<(shift)) // shift >= 0 +#define SKP_RSHIFT_uint(a, shift) ((a)>>(shift)) // shift >= 0 + +#define SKP_ADD_LSHIFT(a, b, shift) ((a) + SKP_LSHIFT((b), (shift))) // shift >= 0 +#define SKP_ADD_LSHIFT32(a, b, shift) SKP_ADD32((a), SKP_LSHIFT32((b), (shift))) // shift >= 0 +#define SKP_ADD_LSHIFT_uint(a, b, shift) ((a) + SKP_LSHIFT_uint((b), (shift))) // shift >= 0 +#define SKP_ADD_RSHIFT(a, b, shift) ((a) + SKP_RSHIFT((b), (shift))) // shift >= 0 +#define SKP_ADD_RSHIFT32(a, b, shift) SKP_ADD32((a), SKP_RSHIFT32((b), (shift))) // shift >= 0 +#define SKP_ADD_RSHIFT_uint(a, b, shift) ((a) + SKP_RSHIFT_uint((b), (shift))) // shift >= 0 +#define SKP_SUB_LSHIFT32(a, b, shift) SKP_SUB32((a), SKP_LSHIFT32((b), (shift))) // shift >= 0 +#define SKP_SUB_RSHIFT32(a, b, shift) SKP_SUB32((a), SKP_RSHIFT32((b), (shift))) // shift >= 0 + +/* Requires that shift > 0 */ +#define SKP_RSHIFT_ROUND(a, shift) ((shift) == 1 ? ((a) >> 1) + ((a) & 1) : (((a) >> ((shift) - 1)) + 1) >> 1) +#define SKP_RSHIFT_ROUND64(a, shift) ((shift) == 1 ? ((a) >> 1) + ((a) & 1) : (((a) >> ((shift) - 1)) + 1) >> 1) + +/* Number of rightshift required to fit the multiplication */ +#define SKP_NSHIFT_MUL_32_32(a, b) ( -(31- (32-SKP_Silk_CLZ32(SKP_abs(a)) + (32-SKP_Silk_CLZ32(SKP_abs(b))))) ) +#define SKP_NSHIFT_MUL_16_16(a, b) ( -(15- (16-SKP_Silk_CLZ16(SKP_abs(a)) + (16-SKP_Silk_CLZ16(SKP_abs(b))))) ) + + +#define SKP_min(a, b) (((a) < (b)) ? (a) : (b)) +#define SKP_max(a, b) (((a) > (b)) ? (a) : (b)) + +/* Macro to convert floating-point constants to fixed-point */ +#define SKP_FIX_CONST( C, Q ) ((SKP_int32)((C) * (1 << (Q)) + 0.5)) + +/* SKP_min() versions with typecast in the function call */ +SKP_INLINE SKP_int SKP_min_int(SKP_int a, SKP_int b) +{ + return (((a) < (b)) ? (a) : (b)); +} +SKP_INLINE SKP_int16 SKP_min_16(SKP_int16 a, SKP_int16 b) +{ + return (((a) < (b)) ? (a) : (b)); +} +SKP_INLINE SKP_int32 SKP_min_32(SKP_int32 a, SKP_int32 b) +{ + return (((a) < (b)) ? (a) : (b)); +} +SKP_INLINE SKP_int64 SKP_min_64(SKP_int64 a, SKP_int64 b) +{ + return (((a) < (b)) ? (a) : (b)); +} + +/* SKP_min() versions with typecast in the function call */ +SKP_INLINE SKP_int SKP_max_int(SKP_int a, SKP_int b) +{ + return (((a) > (b)) ? (a) : (b)); +} +SKP_INLINE SKP_int16 SKP_max_16(SKP_int16 a, SKP_int16 b) +{ + return (((a) > (b)) ? (a) : (b)); +} +SKP_INLINE SKP_int32 SKP_max_32(SKP_int32 a, SKP_int32 b) +{ + return (((a) > (b)) ? (a) : (b)); +} +SKP_INLINE SKP_int64 SKP_max_64(SKP_int64 a, SKP_int64 b) +{ + return (((a) > (b)) ? (a) : (b)); +} + +#define SKP_LIMIT( a, limit1, limit2) ((limit1) > (limit2) ? ((a) > (limit1) ? (limit1) : ((a) < (limit2) ? (limit2) : (a))) \ + : ((a) > (limit2) ? (limit2) : ((a) < (limit1) ? (limit1) : (a)))) + +//#define SKP_non_neg(a) ((a) & ((-(a)) >> (8 * sizeof(a) - 1))) /* doesn't seem faster than SKP_max(0, a); + +#define SKP_abs(a) (((a) > 0) ? (a) : -(a)) // Be careful, SKP_abs returns wrong when input equals to SKP_intXX_MIN +#define SKP_abs_int(a) (((a) ^ ((a) >> (8 * sizeof(a) - 1))) - ((a) >> (8 * sizeof(a) - 1))) +#define SKP_abs_int32(a) (((a) ^ ((a) >> 31)) - ((a) >> 31)) +#define SKP_abs_int64(a) (((a) > 0) ? (a) : -(a)) + +#define SKP_sign(a) ((a) > 0 ? 1 : ( (a) < 0 ? -1 : 0 )) + +#define SKP_sqrt(a) (sqrt(a)) + +/* PSEUDO-RANDOM GENERATOR */ +/* Make sure to store the result as the seed for the next call (also in between */ +/* frames), otherwise result won't be random at all. When only using some of the */ +/* bits, take the most significant bits by right-shifting. Do not just mask off */ +/* the lowest bits. */ +#define SKP_RAND(seed) (SKP_MLA_ovflw(907633515, (seed), 196314165)) + +// Add some multiplication functions that can be easily mapped to ARM. + +// SKP_SMMUL: Signed top word multiply. +// ARMv6 2 instruction cycles. +// ARMv3M+ 3 instruction cycles. use SMULL and ignore LSB registers.(except xM) +//#define SKP_SMMUL(a32, b32) (SKP_int32)SKP_RSHIFT(SKP_SMLAL(SKP_SMULWB((a32), (b32)), (a32), SKP_RSHIFT_ROUND((b32), 16)), 16) +// the following seems faster on x86 +#define SKP_SMMUL(a32, b32) (SKP_int32)SKP_RSHIFT64(SKP_SMULL((a32), (b32)), 32) + +#include "SKP_Silk_Inlines.h" + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_VAD.c b/app/src/main/jni/silk/src/SKP_Silk_VAD.c similarity index 97% rename from jni/silk/src/SKP_Silk_VAD.c rename to app/src/main/jni/silk/src/SKP_Silk_VAD.c index 30a5aa6..f527360 100644 --- a/jni/silk/src/SKP_Silk_VAD.c +++ b/app/src/main/jni/silk/src/SKP_Silk_VAD.c @@ -1,320 +1,320 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* - * File Name: SKP_Silk_VAD.c - * Description: Silk VAD. - */ - -#include -#include "SKP_Silk_main.h" - -/**********************************/ -/* Initialization of the Silk VAD */ -/**********************************/ -SKP_int SKP_Silk_VAD_Init( /* O Return value, 0 if success */ - SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ -) -{ - SKP_int b, ret = 0; - - /* reset state memory */ - SKP_memset( psSilk_VAD, 0, sizeof( SKP_Silk_VAD_state ) ); - - /* init noise levels */ - /* Initialize array with approx pink noise levels (psd proportional to inverse of frequency) */ - for( b = 0; b < VAD_N_BANDS; b++ ) { - psSilk_VAD->NoiseLevelBias[ b ] = SKP_max_32( SKP_DIV32_16( VAD_NOISE_LEVELS_BIAS, b + 1 ), 1 ); - } - - /* Initialize state */ - for( b = 0; b < VAD_N_BANDS; b++ ) { - psSilk_VAD->NL[ b ] = SKP_MUL( 100, psSilk_VAD->NoiseLevelBias[ b ] ); - psSilk_VAD->inv_NL[ b ] = SKP_DIV32( SKP_int32_MAX, psSilk_VAD->NL[ b ] ); - } - psSilk_VAD->counter = 15; - - /* init smoothed energy-to-noise ratio*/ - for( b = 0; b < VAD_N_BANDS; b++ ) { - psSilk_VAD->NrgRatioSmth_Q8[ b ] = 100 * 256; /* 100 * 256 --> 20 dB SNR */ - } - - return( ret ); -} - -/* Weighting factors for tilt measure */ -const static SKP_int32 tiltWeights[ VAD_N_BANDS ] = { 30000, 6000, -12000, -12000 }; - -/***************************************/ -/* Get the speech activity level in Q8 */ -/***************************************/ -SKP_int SKP_Silk_VAD_GetSA_Q8( /* O Return value, 0 if success */ - SKP_Silk_VAD_state *psSilk_VAD, /* I/O Silk VAD state */ - SKP_int *pSA_Q8, /* O Speech activity level in Q8 */ - SKP_int *pSNR_dB_Q7, /* O SNR for current frame in Q7 */ - SKP_int pQuality_Q15[ VAD_N_BANDS ], /* O Smoothed SNR for each band */ - SKP_int *pTilt_Q15, /* O current frame's frequency tilt */ - const SKP_int16 pIn[], /* I PCM input [framelength] */ - const SKP_int framelength /* I Input frame length */ -) -{ - SKP_int SA_Q15, input_tilt; - SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH / 2 ]; - SKP_int decimated_framelength, dec_subframe_length, dec_subframe_offset, SNR_Q7, i, b, s; - SKP_int32 sumSquared, smooth_coef_Q16; - SKP_int16 HPstateTmp; - - SKP_int16 X[ VAD_N_BANDS ][ MAX_FRAME_LENGTH / 2 ]; - SKP_int32 Xnrg[ VAD_N_BANDS ]; - SKP_int32 NrgToNoiseRatio_Q8[ VAD_N_BANDS ]; - SKP_int32 speech_nrg, x_tmp; - SKP_int ret = 0; - - /* Safety checks */ - SKP_assert( VAD_N_BANDS == 4 ); - SKP_assert( MAX_FRAME_LENGTH >= framelength ); - SKP_assert( framelength <= 512 ); - - /***********************/ - /* Filter and Decimate */ - /***********************/ - /* 0-8 kHz to 0-4 kHz and 4-8 kHz */ - SKP_Silk_ana_filt_bank_1( pIn, &psSilk_VAD->AnaState[ 0 ], &X[ 0 ][ 0 ], &X[ 3 ][ 0 ], &scratch[ 0 ], framelength ); - - /* 0-4 kHz to 0-2 kHz and 2-4 kHz */ - SKP_Silk_ana_filt_bank_1( &X[ 0 ][ 0 ], &psSilk_VAD->AnaState1[ 0 ], &X[ 0 ][ 0 ], &X[ 2 ][ 0 ], &scratch[ 0 ], SKP_RSHIFT( framelength, 1 ) ); - - /* 0-2 kHz to 0-1 kHz and 1-2 kHz */ - SKP_Silk_ana_filt_bank_1( &X[ 0 ][ 0 ], &psSilk_VAD->AnaState2[ 0 ], &X[ 0 ][ 0 ], &X[ 1 ][ 0 ], &scratch[ 0 ], SKP_RSHIFT( framelength, 2 ) ); - - /*********************************************/ - /* HP filter on lowest band (differentiator) */ - /*********************************************/ - decimated_framelength = SKP_RSHIFT( framelength, 3 ); - X[ 0 ][ decimated_framelength - 1 ] = SKP_RSHIFT( X[ 0 ][ decimated_framelength - 1 ], 1 ); - HPstateTmp = X[ 0 ][ decimated_framelength - 1 ]; - for( i = decimated_framelength - 1; i > 0; i-- ) { - X[ 0 ][ i - 1 ] = SKP_RSHIFT( X[ 0 ][ i - 1 ], 1 ); - X[ 0 ][ i ] -= X[ 0 ][ i - 1 ]; - } - X[ 0 ][ 0 ] -= psSilk_VAD->HPstate; - psSilk_VAD->HPstate = HPstateTmp; - - /*************************************/ - /* Calculate the energy in each band */ - /*************************************/ - for( b = 0; b < VAD_N_BANDS; b++ ) { - /* Find the decimated framelength in the non-uniformly divided bands */ - decimated_framelength = SKP_RSHIFT( framelength, SKP_min_int( VAD_N_BANDS - b, VAD_N_BANDS - 1 ) ); - - /* Split length into subframe lengths */ - dec_subframe_length = SKP_RSHIFT( decimated_framelength, VAD_INTERNAL_SUBFRAMES_LOG2 ); - dec_subframe_offset = 0; - - /* Compute energy per sub-frame */ - /* initialize with summed energy of last subframe */ - Xnrg[ b ] = psSilk_VAD->XnrgSubfr[ b ]; - for( s = 0; s < VAD_INTERNAL_SUBFRAMES; s++ ) { - sumSquared = 0; - for( i = 0; i < dec_subframe_length; i++ ) { - /* The energy will be less than dec_subframe_length * ( SKP_int16_MIN / 8 )^2. */ - /* Therefore we can accumulate with no risk of overflow (unless dec_subframe_length > 128) */ - x_tmp = SKP_RSHIFT( X[ b ][ i + dec_subframe_offset ], 3 ); - sumSquared = SKP_SMLABB( sumSquared, x_tmp, x_tmp ); - - /* Safety check */ - SKP_assert( sumSquared >= 0 ); - } - - /* add/saturate summed energy of current subframe */ - if( s < VAD_INTERNAL_SUBFRAMES - 1 ) { - Xnrg[ b ] = SKP_ADD_POS_SAT32( Xnrg[ b ], sumSquared ); - } else { - /* look-ahead subframe */ - Xnrg[ b ] = SKP_ADD_POS_SAT32( Xnrg[ b ], SKP_RSHIFT( sumSquared, 1 ) ); - } - - dec_subframe_offset += dec_subframe_length; - } - psSilk_VAD->XnrgSubfr[ b ] = sumSquared; - } - - /********************/ - /* Noise estimation */ - /********************/ - SKP_Silk_VAD_GetNoiseLevels( &Xnrg[ 0 ], psSilk_VAD ); - - /***********************************************/ - /* Signal-plus-noise to noise ratio estimation */ - /***********************************************/ - sumSquared = 0; - input_tilt = 0; - for( b = 0; b < VAD_N_BANDS; b++ ) { - speech_nrg = Xnrg[ b ] - psSilk_VAD->NL[ b ]; - if( speech_nrg > 0 ) { - /* Divide, with sufficient resolution */ - if( ( Xnrg[ b ] & 0xFF800000 ) == 0 ) { - NrgToNoiseRatio_Q8[ b ] = SKP_DIV32( SKP_LSHIFT( Xnrg[ b ], 8 ), psSilk_VAD->NL[ b ] + 1 ); - } else { - NrgToNoiseRatio_Q8[ b ] = SKP_DIV32( Xnrg[ b ], SKP_RSHIFT( psSilk_VAD->NL[ b ], 8 ) + 1 ); - } - - /* Convert to log domain */ - SNR_Q7 = SKP_Silk_lin2log( NrgToNoiseRatio_Q8[ b ] ) - 8 * 128; - - /* Sum-of-squares */ - sumSquared = SKP_SMLABB( sumSquared, SNR_Q7, SNR_Q7 ); /* Q14 */ - - /* Tilt measure */ - if( speech_nrg < ( 1 << 20 ) ) { - /* Scale down SNR value for small subband speech energies */ - SNR_Q7 = SKP_SMULWB( SKP_LSHIFT( SKP_Silk_SQRT_APPROX( speech_nrg ), 6 ), SNR_Q7 ); - } - input_tilt = SKP_SMLAWB( input_tilt, tiltWeights[ b ], SNR_Q7 ); - } else { - NrgToNoiseRatio_Q8[ b ] = 256; - } - } - - /* Mean-of-squares */ - sumSquared = SKP_DIV32_16( sumSquared, VAD_N_BANDS ); /* Q14 */ - - /* Root-mean-square approximation, scale to dBs, and write to output pointer */ - *pSNR_dB_Q7 = ( SKP_int16 )( 3 * SKP_Silk_SQRT_APPROX( sumSquared ) ); /* Q7 */ - - /*********************************/ - /* Speech Probability Estimation */ - /*********************************/ - SA_Q15 = SKP_Silk_sigm_Q15( SKP_SMULWB( VAD_SNR_FACTOR_Q16, *pSNR_dB_Q7 ) - VAD_NEGATIVE_OFFSET_Q5 ); - - /**************************/ - /* Frequency Tilt Measure */ - /**************************/ - *pTilt_Q15 = SKP_LSHIFT( SKP_Silk_sigm_Q15( input_tilt ) - 16384, 1 ); - - /**************************************************/ - /* Scale the sigmoid output based on power levels */ - /**************************************************/ - speech_nrg = 0; - for( b = 0; b < VAD_N_BANDS; b++ ) { - /* Accumulate signal-without-noise energies, higher frequency bands have more weight */ - speech_nrg += ( b + 1 ) * SKP_RSHIFT( Xnrg[ b ] - psSilk_VAD->NL[ b ], 4 ); - } - - /* Power scaling */ - if( speech_nrg <= 0 ) { - SA_Q15 = SKP_RSHIFT( SA_Q15, 1 ); - } else if( speech_nrg < 32768 ) { - /* square-root */ - speech_nrg = SKP_Silk_SQRT_APPROX( SKP_LSHIFT( speech_nrg, 15 ) ); - SA_Q15 = SKP_SMULWB( 32768 + speech_nrg, SA_Q15 ); - } - - /* Copy the resulting speech activity in Q8 to *pSA_Q8 */ - *pSA_Q8 = SKP_min_int( SKP_RSHIFT( SA_Q15, 7 ), SKP_uint8_MAX ); - - /***********************************/ - /* Energy Level and SNR estimation */ - /***********************************/ - /* smoothing coefficient */ - smooth_coef_Q16 = SKP_SMULWB( VAD_SNR_SMOOTH_COEF_Q18, SKP_SMULWB( SA_Q15, SA_Q15 ) ); - for( b = 0; b < VAD_N_BANDS; b++ ) { - /* compute smoothed energy-to-noise ratio per band */ - psSilk_VAD->NrgRatioSmth_Q8[ b ] = SKP_SMLAWB( psSilk_VAD->NrgRatioSmth_Q8[ b ], - NrgToNoiseRatio_Q8[ b ] - psSilk_VAD->NrgRatioSmth_Q8[ b ], smooth_coef_Q16 ); - - /* signal to noise ratio in dB per band */ - SNR_Q7 = 3 * ( SKP_Silk_lin2log( psSilk_VAD->NrgRatioSmth_Q8[b] ) - 8 * 128 ); - /* quality = sigmoid( 0.25 * ( SNR_dB - 16 ) ); */ - pQuality_Q15[ b ] = SKP_Silk_sigm_Q15( SKP_RSHIFT( SNR_Q7 - 16 * 128, 4 ) ); - } - - return( ret ); -} - -/**************************/ -/* Noise level estimation */ -/**************************/ -void SKP_Silk_VAD_GetNoiseLevels( - const SKP_int32 pX[ VAD_N_BANDS ], /* I subband energies */ - SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ -) -{ - SKP_int k; - SKP_int32 nl, nrg, inv_nrg; - SKP_int coef, min_coef; - - /* Initially faster smoothing */ - if( psSilk_VAD->counter < 1000 ) { /* 1000 = 20 sec */ - min_coef = SKP_DIV32_16( SKP_int16_MAX, SKP_RSHIFT( psSilk_VAD->counter, 4 ) + 1 ); - } else { - min_coef = 0; - } - - for( k = 0; k < VAD_N_BANDS; k++ ) { - /* Get old noise level estimate for current band */ - nl = psSilk_VAD->NL[ k ]; - SKP_assert( nl >= 0 ); - - /* Add bias */ - nrg = SKP_ADD_POS_SAT32( pX[ k ], psSilk_VAD->NoiseLevelBias[ k ] ); - SKP_assert( nrg > 0 ); - - /* Invert energies */ - inv_nrg = SKP_DIV32( SKP_int32_MAX, nrg ); - SKP_assert( inv_nrg >= 0 ); - - /* Less update when subband energy is high */ - if( nrg > SKP_LSHIFT( nl, 3 ) ) { - coef = VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 >> 3; - } else if( nrg < nl ) { - coef = VAD_NOISE_LEVEL_SMOOTH_COEF_Q16; - } else { - coef = SKP_SMULWB( SKP_SMULWW( inv_nrg, nl ), VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 << 1 ); - } - - /* Initially faster smoothing */ - coef = SKP_max_int( coef, min_coef ); - - /* Smooth inverse energies */ - psSilk_VAD->inv_NL[ k ] = SKP_SMLAWB( psSilk_VAD->inv_NL[ k ], inv_nrg - psSilk_VAD->inv_NL[ k ], coef ); - SKP_assert( psSilk_VAD->inv_NL[ k ] >= 0 ); - - /* Compute noise level by inverting again */ - nl = SKP_DIV32( SKP_int32_MAX, psSilk_VAD->inv_NL[ k ] ); - SKP_assert( nl >= 0 ); - - /* Limit noise levels (guarantee 7 bits of head room) */ - nl = SKP_min( nl, 0x00FFFFFF ); - - /* Store as part of state */ - psSilk_VAD->NL[ k ] = nl; - } - - /* Increment frame counter */ - psSilk_VAD->counter++; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* + * File Name: SKP_Silk_VAD.c + * Description: Silk VAD. + */ + +#include +#include "SKP_Silk_main.h" + +/**********************************/ +/* Initialization of the Silk VAD */ +/**********************************/ +SKP_int SKP_Silk_VAD_Init( /* O Return value, 0 if success */ + SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ +) +{ + SKP_int b, ret = 0; + + /* reset state memory */ + SKP_memset( psSilk_VAD, 0, sizeof( SKP_Silk_VAD_state ) ); + + /* init noise levels */ + /* Initialize array with approx pink noise levels (psd proportional to inverse of frequency) */ + for( b = 0; b < VAD_N_BANDS; b++ ) { + psSilk_VAD->NoiseLevelBias[ b ] = SKP_max_32( SKP_DIV32_16( VAD_NOISE_LEVELS_BIAS, b + 1 ), 1 ); + } + + /* Initialize state */ + for( b = 0; b < VAD_N_BANDS; b++ ) { + psSilk_VAD->NL[ b ] = SKP_MUL( 100, psSilk_VAD->NoiseLevelBias[ b ] ); + psSilk_VAD->inv_NL[ b ] = SKP_DIV32( SKP_int32_MAX, psSilk_VAD->NL[ b ] ); + } + psSilk_VAD->counter = 15; + + /* init smoothed energy-to-noise ratio*/ + for( b = 0; b < VAD_N_BANDS; b++ ) { + psSilk_VAD->NrgRatioSmth_Q8[ b ] = 100 * 256; /* 100 * 256 --> 20 dB SNR */ + } + + return( ret ); +} + +/* Weighting factors for tilt measure */ +const static SKP_int32 tiltWeights[ VAD_N_BANDS ] = { 30000, 6000, -12000, -12000 }; + +/***************************************/ +/* Get the speech activity level in Q8 */ +/***************************************/ +SKP_int SKP_Silk_VAD_GetSA_Q8( /* O Return value, 0 if success */ + SKP_Silk_VAD_state *psSilk_VAD, /* I/O Silk VAD state */ + SKP_int *pSA_Q8, /* O Speech activity level in Q8 */ + SKP_int *pSNR_dB_Q7, /* O SNR for current frame in Q7 */ + SKP_int pQuality_Q15[ VAD_N_BANDS ], /* O Smoothed SNR for each band */ + SKP_int *pTilt_Q15, /* O current frame's frequency tilt */ + const SKP_int16 pIn[], /* I PCM input [framelength] */ + const SKP_int framelength /* I Input frame length */ +) +{ + SKP_int SA_Q15, input_tilt; + SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH / 2 ]; + SKP_int decimated_framelength, dec_subframe_length, dec_subframe_offset, SNR_Q7, i, b, s; + SKP_int32 sumSquared, smooth_coef_Q16; + SKP_int16 HPstateTmp; + + SKP_int16 X[ VAD_N_BANDS ][ MAX_FRAME_LENGTH / 2 ]; + SKP_int32 Xnrg[ VAD_N_BANDS ]; + SKP_int32 NrgToNoiseRatio_Q8[ VAD_N_BANDS ]; + SKP_int32 speech_nrg, x_tmp; + SKP_int ret = 0; + + /* Safety checks */ + SKP_assert( VAD_N_BANDS == 4 ); + SKP_assert( MAX_FRAME_LENGTH >= framelength ); + SKP_assert( framelength <= 512 ); + + /***********************/ + /* Filter and Decimate */ + /***********************/ + /* 0-8 kHz to 0-4 kHz and 4-8 kHz */ + SKP_Silk_ana_filt_bank_1( pIn, &psSilk_VAD->AnaState[ 0 ], &X[ 0 ][ 0 ], &X[ 3 ][ 0 ], &scratch[ 0 ], framelength ); + + /* 0-4 kHz to 0-2 kHz and 2-4 kHz */ + SKP_Silk_ana_filt_bank_1( &X[ 0 ][ 0 ], &psSilk_VAD->AnaState1[ 0 ], &X[ 0 ][ 0 ], &X[ 2 ][ 0 ], &scratch[ 0 ], SKP_RSHIFT( framelength, 1 ) ); + + /* 0-2 kHz to 0-1 kHz and 1-2 kHz */ + SKP_Silk_ana_filt_bank_1( &X[ 0 ][ 0 ], &psSilk_VAD->AnaState2[ 0 ], &X[ 0 ][ 0 ], &X[ 1 ][ 0 ], &scratch[ 0 ], SKP_RSHIFT( framelength, 2 ) ); + + /*********************************************/ + /* HP filter on lowest band (differentiator) */ + /*********************************************/ + decimated_framelength = SKP_RSHIFT( framelength, 3 ); + X[ 0 ][ decimated_framelength - 1 ] = SKP_RSHIFT( X[ 0 ][ decimated_framelength - 1 ], 1 ); + HPstateTmp = X[ 0 ][ decimated_framelength - 1 ]; + for( i = decimated_framelength - 1; i > 0; i-- ) { + X[ 0 ][ i - 1 ] = SKP_RSHIFT( X[ 0 ][ i - 1 ], 1 ); + X[ 0 ][ i ] -= X[ 0 ][ i - 1 ]; + } + X[ 0 ][ 0 ] -= psSilk_VAD->HPstate; + psSilk_VAD->HPstate = HPstateTmp; + + /*************************************/ + /* Calculate the energy in each band */ + /*************************************/ + for( b = 0; b < VAD_N_BANDS; b++ ) { + /* Find the decimated framelength in the non-uniformly divided bands */ + decimated_framelength = SKP_RSHIFT( framelength, SKP_min_int( VAD_N_BANDS - b, VAD_N_BANDS - 1 ) ); + + /* Split length into subframe lengths */ + dec_subframe_length = SKP_RSHIFT( decimated_framelength, VAD_INTERNAL_SUBFRAMES_LOG2 ); + dec_subframe_offset = 0; + + /* Compute energy per sub-frame */ + /* initialize with summed energy of last subframe */ + Xnrg[ b ] = psSilk_VAD->XnrgSubfr[ b ]; + for( s = 0; s < VAD_INTERNAL_SUBFRAMES; s++ ) { + sumSquared = 0; + for( i = 0; i < dec_subframe_length; i++ ) { + /* The energy will be less than dec_subframe_length * ( SKP_int16_MIN / 8 )^2. */ + /* Therefore we can accumulate with no risk of overflow (unless dec_subframe_length > 128) */ + x_tmp = SKP_RSHIFT( X[ b ][ i + dec_subframe_offset ], 3 ); + sumSquared = SKP_SMLABB( sumSquared, x_tmp, x_tmp ); + + /* Safety check */ + SKP_assert( sumSquared >= 0 ); + } + + /* add/saturate summed energy of current subframe */ + if( s < VAD_INTERNAL_SUBFRAMES - 1 ) { + Xnrg[ b ] = SKP_ADD_POS_SAT32( Xnrg[ b ], sumSquared ); + } else { + /* look-ahead subframe */ + Xnrg[ b ] = SKP_ADD_POS_SAT32( Xnrg[ b ], SKP_RSHIFT( sumSquared, 1 ) ); + } + + dec_subframe_offset += dec_subframe_length; + } + psSilk_VAD->XnrgSubfr[ b ] = sumSquared; + } + + /********************/ + /* Noise estimation */ + /********************/ + SKP_Silk_VAD_GetNoiseLevels( &Xnrg[ 0 ], psSilk_VAD ); + + /***********************************************/ + /* Signal-plus-noise to noise ratio estimation */ + /***********************************************/ + sumSquared = 0; + input_tilt = 0; + for( b = 0; b < VAD_N_BANDS; b++ ) { + speech_nrg = Xnrg[ b ] - psSilk_VAD->NL[ b ]; + if( speech_nrg > 0 ) { + /* Divide, with sufficient resolution */ + if( ( Xnrg[ b ] & 0xFF800000 ) == 0 ) { + NrgToNoiseRatio_Q8[ b ] = SKP_DIV32( SKP_LSHIFT( Xnrg[ b ], 8 ), psSilk_VAD->NL[ b ] + 1 ); + } else { + NrgToNoiseRatio_Q8[ b ] = SKP_DIV32( Xnrg[ b ], SKP_RSHIFT( psSilk_VAD->NL[ b ], 8 ) + 1 ); + } + + /* Convert to log domain */ + SNR_Q7 = SKP_Silk_lin2log( NrgToNoiseRatio_Q8[ b ] ) - 8 * 128; + + /* Sum-of-squares */ + sumSquared = SKP_SMLABB( sumSquared, SNR_Q7, SNR_Q7 ); /* Q14 */ + + /* Tilt measure */ + if( speech_nrg < ( 1 << 20 ) ) { + /* Scale down SNR value for small subband speech energies */ + SNR_Q7 = SKP_SMULWB( SKP_LSHIFT( SKP_Silk_SQRT_APPROX( speech_nrg ), 6 ), SNR_Q7 ); + } + input_tilt = SKP_SMLAWB( input_tilt, tiltWeights[ b ], SNR_Q7 ); + } else { + NrgToNoiseRatio_Q8[ b ] = 256; + } + } + + /* Mean-of-squares */ + sumSquared = SKP_DIV32_16( sumSquared, VAD_N_BANDS ); /* Q14 */ + + /* Root-mean-square approximation, scale to dBs, and write to output pointer */ + *pSNR_dB_Q7 = ( SKP_int16 )( 3 * SKP_Silk_SQRT_APPROX( sumSquared ) ); /* Q7 */ + + /*********************************/ + /* Speech Probability Estimation */ + /*********************************/ + SA_Q15 = SKP_Silk_sigm_Q15( SKP_SMULWB( VAD_SNR_FACTOR_Q16, *pSNR_dB_Q7 ) - VAD_NEGATIVE_OFFSET_Q5 ); + + /**************************/ + /* Frequency Tilt Measure */ + /**************************/ + *pTilt_Q15 = SKP_LSHIFT( SKP_Silk_sigm_Q15( input_tilt ) - 16384, 1 ); + + /**************************************************/ + /* Scale the sigmoid output based on power levels */ + /**************************************************/ + speech_nrg = 0; + for( b = 0; b < VAD_N_BANDS; b++ ) { + /* Accumulate signal-without-noise energies, higher frequency bands have more weight */ + speech_nrg += ( b + 1 ) * SKP_RSHIFT( Xnrg[ b ] - psSilk_VAD->NL[ b ], 4 ); + } + + /* Power scaling */ + if( speech_nrg <= 0 ) { + SA_Q15 = SKP_RSHIFT( SA_Q15, 1 ); + } else if( speech_nrg < 32768 ) { + /* square-root */ + speech_nrg = SKP_Silk_SQRT_APPROX( SKP_LSHIFT( speech_nrg, 15 ) ); + SA_Q15 = SKP_SMULWB( 32768 + speech_nrg, SA_Q15 ); + } + + /* Copy the resulting speech activity in Q8 to *pSA_Q8 */ + *pSA_Q8 = SKP_min_int( SKP_RSHIFT( SA_Q15, 7 ), SKP_uint8_MAX ); + + /***********************************/ + /* Energy Level and SNR estimation */ + /***********************************/ + /* smoothing coefficient */ + smooth_coef_Q16 = SKP_SMULWB( VAD_SNR_SMOOTH_COEF_Q18, SKP_SMULWB( SA_Q15, SA_Q15 ) ); + for( b = 0; b < VAD_N_BANDS; b++ ) { + /* compute smoothed energy-to-noise ratio per band */ + psSilk_VAD->NrgRatioSmth_Q8[ b ] = SKP_SMLAWB( psSilk_VAD->NrgRatioSmth_Q8[ b ], + NrgToNoiseRatio_Q8[ b ] - psSilk_VAD->NrgRatioSmth_Q8[ b ], smooth_coef_Q16 ); + + /* signal to noise ratio in dB per band */ + SNR_Q7 = 3 * ( SKP_Silk_lin2log( psSilk_VAD->NrgRatioSmth_Q8[b] ) - 8 * 128 ); + /* quality = sigmoid( 0.25 * ( SNR_dB - 16 ) ); */ + pQuality_Q15[ b ] = SKP_Silk_sigm_Q15( SKP_RSHIFT( SNR_Q7 - 16 * 128, 4 ) ); + } + + return( ret ); +} + +/**************************/ +/* Noise level estimation */ +/**************************/ +void SKP_Silk_VAD_GetNoiseLevels( + const SKP_int32 pX[ VAD_N_BANDS ], /* I subband energies */ + SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ +) +{ + SKP_int k; + SKP_int32 nl, nrg, inv_nrg; + SKP_int coef, min_coef; + + /* Initially faster smoothing */ + if( psSilk_VAD->counter < 1000 ) { /* 1000 = 20 sec */ + min_coef = SKP_DIV32_16( SKP_int16_MAX, SKP_RSHIFT( psSilk_VAD->counter, 4 ) + 1 ); + } else { + min_coef = 0; + } + + for( k = 0; k < VAD_N_BANDS; k++ ) { + /* Get old noise level estimate for current band */ + nl = psSilk_VAD->NL[ k ]; + SKP_assert( nl >= 0 ); + + /* Add bias */ + nrg = SKP_ADD_POS_SAT32( pX[ k ], psSilk_VAD->NoiseLevelBias[ k ] ); + SKP_assert( nrg > 0 ); + + /* Invert energies */ + inv_nrg = SKP_DIV32( SKP_int32_MAX, nrg ); + SKP_assert( inv_nrg >= 0 ); + + /* Less update when subband energy is high */ + if( nrg > SKP_LSHIFT( nl, 3 ) ) { + coef = VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 >> 3; + } else if( nrg < nl ) { + coef = VAD_NOISE_LEVEL_SMOOTH_COEF_Q16; + } else { + coef = SKP_SMULWB( SKP_SMULWW( inv_nrg, nl ), VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 << 1 ); + } + + /* Initially faster smoothing */ + coef = SKP_max_int( coef, min_coef ); + + /* Smooth inverse energies */ + psSilk_VAD->inv_NL[ k ] = SKP_SMLAWB( psSilk_VAD->inv_NL[ k ], inv_nrg - psSilk_VAD->inv_NL[ k ], coef ); + SKP_assert( psSilk_VAD->inv_NL[ k ] >= 0 ); + + /* Compute noise level by inverting again */ + nl = SKP_DIV32( SKP_int32_MAX, psSilk_VAD->inv_NL[ k ] ); + SKP_assert( nl >= 0 ); + + /* Limit noise levels (guarantee 7 bits of head room) */ + nl = SKP_min( nl, 0x00FFFFFF ); + + /* Store as part of state */ + psSilk_VAD->NL[ k ] = nl; + } + + /* Increment frame counter */ + psSilk_VAD->counter++; +} diff --git a/jni/silk/src/SKP_Silk_VQ_nearest_neighbor_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_VQ_nearest_neighbor_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_VQ_nearest_neighbor_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_VQ_nearest_neighbor_FIX.c index 82727d5..6c4999b 100644 --- a/jni/silk/src/SKP_Silk_VQ_nearest_neighbor_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_VQ_nearest_neighbor_FIX.c @@ -1,110 +1,110 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Entropy constrained MATRIX-weighted VQ, hard-coded to 5-element vectors, for a single input data vector */ -void SKP_Silk_VQ_WMat_EC_FIX( - SKP_int *ind, /* O index of best codebook vector */ - SKP_int32 *rate_dist_Q14, /* O best weighted quantization error + mu * rate*/ - const SKP_int16 *in_Q14, /* I input vector to be quantized */ - const SKP_int32 *W_Q18, /* I weighting matrix */ - const SKP_int16 *cb_Q14, /* I codebook */ - const SKP_int16 *cl_Q6, /* I code length for each codebook vector */ - const SKP_int mu_Q8, /* I tradeoff between weighted error and rate */ - SKP_int L /* I number of vectors in codebook */ -) -{ - SKP_int k; - const SKP_int16 *cb_row_Q14; - SKP_int32 sum1_Q14, sum2_Q16, diff_Q14_01, diff_Q14_23, diff_Q14_4; - - /* Loop over codebook */ - *rate_dist_Q14 = SKP_int32_MAX; - cb_row_Q14 = cb_Q14; - for( k = 0; k < L; k++ ) { - /* Pack pairs of int16 values per int32 */ - diff_Q14_01 = (SKP_uint16)( in_Q14[ 0 ] - cb_row_Q14[ 0 ] ) | SKP_LSHIFT( ( SKP_int32 )in_Q14[ 1 ] - cb_row_Q14[ 1 ], 16 ); - diff_Q14_23 = (SKP_uint16)( in_Q14[ 2 ] - cb_row_Q14[ 2 ] ) | SKP_LSHIFT( ( SKP_int32 )in_Q14[ 3 ] - cb_row_Q14[ 3 ], 16 ); - diff_Q14_4 = in_Q14[ 4 ] - cb_row_Q14[ 4 ]; - - /* Weighted rate */ - sum1_Q14 = SKP_SMULBB( mu_Q8, cl_Q6[ k ] ); - - SKP_assert( sum1_Q14 >= 0 ); - - /* Add weighted quantization error, assuming W_Q18 is symmetric */ - /* NOTE: the code below loads two int16 values as one int32, and multiplies each using the */ - /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLAWB and SMLAWT instructions should solve the problem. */ - /* first row of W_Q18 */ - sum2_Q16 = SKP_SMULWT( W_Q18[ 1 ], diff_Q14_01 ); - sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 2 ], diff_Q14_23 ); - sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 3 ], diff_Q14_23 ); - sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 4 ], diff_Q14_4 ); - sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); - sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 0 ], diff_Q14_01 ); - sum1_Q14 = SKP_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14_01 ); - - /* second row of W_Q18 */ - sum2_Q16 = SKP_SMULWB( W_Q18[ 7 ], diff_Q14_23 ); - sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 8 ], diff_Q14_23 ); - sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 9 ], diff_Q14_4 ); - sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); - sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 6 ], diff_Q14_01 ); - sum1_Q14 = SKP_SMLAWT( sum1_Q14, sum2_Q16, diff_Q14_01 ); - - /* third row of W_Q18 */ - sum2_Q16 = SKP_SMULWT( W_Q18[ 13 ], diff_Q14_23 ); - sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 14 ], diff_Q14_4 ); - sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); - sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 12 ], diff_Q14_23 ); - sum1_Q14 = SKP_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14_23 ); - - /* fourth row of W_Q18 */ - sum2_Q16 = SKP_SMULWB( W_Q18[ 19 ], diff_Q14_4 ); - sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); - sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 18 ], diff_Q14_23 ); - sum1_Q14 = SKP_SMLAWT( sum1_Q14, sum2_Q16, diff_Q14_23 ); - - /* last row of W_Q18 */ - sum2_Q16 = SKP_SMULWB( W_Q18[ 24 ], diff_Q14_4 ); - sum1_Q14 = SKP_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14_4 ); - - SKP_assert( sum1_Q14 >= 0 ); - - /* find best */ - if( sum1_Q14 < *rate_dist_Q14 ) { - *rate_dist_Q14 = sum1_Q14; - *ind = k; - } - - /* Go to next cbk vector */ - cb_row_Q14 += LTP_ORDER; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Entropy constrained MATRIX-weighted VQ, hard-coded to 5-element vectors, for a single input data vector */ +void SKP_Silk_VQ_WMat_EC_FIX( + SKP_int *ind, /* O index of best codebook vector */ + SKP_int32 *rate_dist_Q14, /* O best weighted quantization error + mu * rate*/ + const SKP_int16 *in_Q14, /* I input vector to be quantized */ + const SKP_int32 *W_Q18, /* I weighting matrix */ + const SKP_int16 *cb_Q14, /* I codebook */ + const SKP_int16 *cl_Q6, /* I code length for each codebook vector */ + const SKP_int mu_Q8, /* I tradeoff between weighted error and rate */ + SKP_int L /* I number of vectors in codebook */ +) +{ + SKP_int k; + const SKP_int16 *cb_row_Q14; + SKP_int32 sum1_Q14, sum2_Q16, diff_Q14_01, diff_Q14_23, diff_Q14_4; + + /* Loop over codebook */ + *rate_dist_Q14 = SKP_int32_MAX; + cb_row_Q14 = cb_Q14; + for( k = 0; k < L; k++ ) { + /* Pack pairs of int16 values per int32 */ + diff_Q14_01 = (SKP_uint16)( in_Q14[ 0 ] - cb_row_Q14[ 0 ] ) | SKP_LSHIFT( ( SKP_int32 )in_Q14[ 1 ] - cb_row_Q14[ 1 ], 16 ); + diff_Q14_23 = (SKP_uint16)( in_Q14[ 2 ] - cb_row_Q14[ 2 ] ) | SKP_LSHIFT( ( SKP_int32 )in_Q14[ 3 ] - cb_row_Q14[ 3 ], 16 ); + diff_Q14_4 = in_Q14[ 4 ] - cb_row_Q14[ 4 ]; + + /* Weighted rate */ + sum1_Q14 = SKP_SMULBB( mu_Q8, cl_Q6[ k ] ); + + SKP_assert( sum1_Q14 >= 0 ); + + /* Add weighted quantization error, assuming W_Q18 is symmetric */ + /* NOTE: the code below loads two int16 values as one int32, and multiplies each using the */ + /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLAWB and SMLAWT instructions should solve the problem. */ + /* first row of W_Q18 */ + sum2_Q16 = SKP_SMULWT( W_Q18[ 1 ], diff_Q14_01 ); + sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 2 ], diff_Q14_23 ); + sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 3 ], diff_Q14_23 ); + sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 4 ], diff_Q14_4 ); + sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); + sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 0 ], diff_Q14_01 ); + sum1_Q14 = SKP_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14_01 ); + + /* second row of W_Q18 */ + sum2_Q16 = SKP_SMULWB( W_Q18[ 7 ], diff_Q14_23 ); + sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 8 ], diff_Q14_23 ); + sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 9 ], diff_Q14_4 ); + sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); + sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 6 ], diff_Q14_01 ); + sum1_Q14 = SKP_SMLAWT( sum1_Q14, sum2_Q16, diff_Q14_01 ); + + /* third row of W_Q18 */ + sum2_Q16 = SKP_SMULWT( W_Q18[ 13 ], diff_Q14_23 ); + sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 14 ], diff_Q14_4 ); + sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); + sum2_Q16 = SKP_SMLAWB( sum2_Q16, W_Q18[ 12 ], diff_Q14_23 ); + sum1_Q14 = SKP_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14_23 ); + + /* fourth row of W_Q18 */ + sum2_Q16 = SKP_SMULWB( W_Q18[ 19 ], diff_Q14_4 ); + sum2_Q16 = SKP_LSHIFT( sum2_Q16, 1 ); + sum2_Q16 = SKP_SMLAWT( sum2_Q16, W_Q18[ 18 ], diff_Q14_23 ); + sum1_Q14 = SKP_SMLAWT( sum1_Q14, sum2_Q16, diff_Q14_23 ); + + /* last row of W_Q18 */ + sum2_Q16 = SKP_SMULWB( W_Q18[ 24 ], diff_Q14_4 ); + sum1_Q14 = SKP_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14_4 ); + + SKP_assert( sum1_Q14 >= 0 ); + + /* find best */ + if( sum1_Q14 < *rate_dist_Q14 ) { + *rate_dist_Q14 = sum1_Q14; + *ind = k; + } + + /* Go to next cbk vector */ + cb_row_Q14 += LTP_ORDER; + } +} diff --git a/jni/silk/src/SKP_Silk_allpass_int.c b/app/src/main/jni/silk/src/SKP_Silk_allpass_int.c similarity index 98% rename from jni/silk/src/SKP_Silk_allpass_int.c rename to app/src/main/jni/silk/src/SKP_Silk_allpass_int.c index 20adfd5..4843d6f 100644 --- a/jni/silk/src/SKP_Silk_allpass_int.c +++ b/app/src/main/jni/silk/src/SKP_Silk_allpass_int.c @@ -1,69 +1,69 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_allpass_int.c * - * * - * First-order allpass filter with * - * transfer function: * - * * - * A + Z^(-1) * - * H(z) = ------------ * - * 1 + A*Z^(-1) * - * * - * Implemented using minimum multiplier filter design. * - * * - * Reference: http://www.univ.trieste.it/~ramponi/teaching/ * - * DSP/materiale/Ch6(2).pdf * - * * - * Copyright 2007 (c), Skype Limited * - * Date: 070525 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - - -/* First-order allpass filter */ -void SKP_Silk_allpass_int( - const SKP_int32 *in, /* I: Q25 input signal [len] */ - SKP_int32 *S, /* I/O: Q25 state [1] */ - SKP_int A, /* I: Q15 coefficient (0 <= A < 32768) */ - SKP_int32 *out, /* O: Q25 output signal [len] */ - const SKP_int32 len /* I: Number of samples */ -) -{ - SKP_int32 Y2, X2, S0; - SKP_int k; - - S0 = S[ 0 ]; - for( k = len - 1; k >= 0; k-- ) { - Y2 = *in - S0; - X2 = ( Y2 >> 15 ) * A + ( ( ( Y2 & 0x00007FFF ) * A ) >> 15 ); - ( *out++ ) = S0 + X2; - S0 = ( *in++ ) + X2; - } - S[ 0 ] = S0; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_allpass_int.c * + * * + * First-order allpass filter with * + * transfer function: * + * * + * A + Z^(-1) * + * H(z) = ------------ * + * 1 + A*Z^(-1) * + * * + * Implemented using minimum multiplier filter design. * + * * + * Reference: http://www.univ.trieste.it/~ramponi/teaching/ * + * DSP/materiale/Ch6(2).pdf * + * * + * Copyright 2007 (c), Skype Limited * + * Date: 070525 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + + +/* First-order allpass filter */ +void SKP_Silk_allpass_int( + const SKP_int32 *in, /* I: Q25 input signal [len] */ + SKP_int32 *S, /* I/O: Q25 state [1] */ + SKP_int A, /* I: Q15 coefficient (0 <= A < 32768) */ + SKP_int32 *out, /* O: Q25 output signal [len] */ + const SKP_int32 len /* I: Number of samples */ +) +{ + SKP_int32 Y2, X2, S0; + SKP_int k; + + S0 = S[ 0 ]; + for( k = len - 1; k >= 0; k-- ) { + Y2 = *in - S0; + X2 = ( Y2 >> 15 ) * A + ( ( ( Y2 & 0x00007FFF ) * A ) >> 15 ); + ( *out++ ) = S0 + X2; + S0 = ( *in++ ) + X2; + } + S[ 0 ] = S0; +} diff --git a/jni/silk/src/SKP_Silk_ana_filt_bank_1.c b/app/src/main/jni/silk/src/SKP_Silk_ana_filt_bank_1.c similarity index 98% rename from jni/silk/src/SKP_Silk_ana_filt_bank_1.c rename to app/src/main/jni/silk/src/SKP_Silk_ana_filt_bank_1.c index 3dd3b1b..0a7ac44 100644 --- a/jni/silk/src/SKP_Silk_ana_filt_bank_1.c +++ b/app/src/main/jni/silk/src/SKP_Silk_ana_filt_bank_1.c @@ -1,73 +1,73 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_ana_filt_bank_1.c * - * * - * Split signal into two decimated bands using first-order allpass filters * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Coefficients for 2-band filter bank based on first-order allpass filters */ -static SKP_int16 A_fb1_20[ 1 ] = { 5394 }; -static SKP_int16 A_fb1_21[ 1 ] = { 20623 }; - -/* Split signal into two decimated bands using first-order allpass filters */ -void SKP_Silk_ana_filt_bank_1( - const SKP_int16 *in, /* I: Input signal [N] */ - SKP_int32 *S, /* I/O: State vector [2] */ - SKP_int16 *outL, /* O: Low band [N/2] */ - SKP_int16 *outH, /* O: High band [N/2] */ - SKP_int32 *scratch, /* I: Scratch memory [3*N/2] */ - const SKP_int32 N /* I: Number of input samples */ -) -{ - SKP_int k, N2 = SKP_RSHIFT( N, 1 ); - SKP_int32 out_tmp; - - /* De-interleave three allpass inputs, and convert Q15 -> Q25 */ - for( k = 0; k < N2; k++ ) { - scratch[ k + N ] = SKP_LSHIFT( (SKP_int32)in[ 2 * k ], 10 ); - scratch[ k + N2 ] = SKP_LSHIFT( (SKP_int32)in[ 2 * k + 1 ], 10 ); - } - - /* Allpass filters */ - SKP_Silk_allpass_int( scratch + N2, S+0, A_fb1_20[ 0 ], scratch, N2 ); - SKP_Silk_allpass_int( scratch + N, S+1, A_fb1_21[ 0 ], scratch + N2, N2 ); - - /* Add and subtract two allpass outputs to create bands */ - for( k = 0; k < N2; k++ ) { - out_tmp = scratch[ k ] + scratch[ k + N2 ]; - outL[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( out_tmp, 11 ) ); - - out_tmp = scratch[ k ] - scratch[ k + N2 ]; - outH[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( out_tmp, 11 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_ana_filt_bank_1.c * + * * + * Split signal into two decimated bands using first-order allpass filters * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Coefficients for 2-band filter bank based on first-order allpass filters */ +static SKP_int16 A_fb1_20[ 1 ] = { 5394 }; +static SKP_int16 A_fb1_21[ 1 ] = { 20623 }; + +/* Split signal into two decimated bands using first-order allpass filters */ +void SKP_Silk_ana_filt_bank_1( + const SKP_int16 *in, /* I: Input signal [N] */ + SKP_int32 *S, /* I/O: State vector [2] */ + SKP_int16 *outL, /* O: Low band [N/2] */ + SKP_int16 *outH, /* O: High band [N/2] */ + SKP_int32 *scratch, /* I: Scratch memory [3*N/2] */ + const SKP_int32 N /* I: Number of input samples */ +) +{ + SKP_int k, N2 = SKP_RSHIFT( N, 1 ); + SKP_int32 out_tmp; + + /* De-interleave three allpass inputs, and convert Q15 -> Q25 */ + for( k = 0; k < N2; k++ ) { + scratch[ k + N ] = SKP_LSHIFT( (SKP_int32)in[ 2 * k ], 10 ); + scratch[ k + N2 ] = SKP_LSHIFT( (SKP_int32)in[ 2 * k + 1 ], 10 ); + } + + /* Allpass filters */ + SKP_Silk_allpass_int( scratch + N2, S+0, A_fb1_20[ 0 ], scratch, N2 ); + SKP_Silk_allpass_int( scratch + N, S+1, A_fb1_21[ 0 ], scratch + N2, N2 ); + + /* Add and subtract two allpass outputs to create bands */ + for( k = 0; k < N2; k++ ) { + out_tmp = scratch[ k ] + scratch[ k + N2 ]; + outL[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( out_tmp, 11 ) ); + + out_tmp = scratch[ k ] - scratch[ k + N2 ]; + outH[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( out_tmp, 11 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_apply_sine_window.c b/app/src/main/jni/silk/src/SKP_Silk_apply_sine_window.c similarity index 98% rename from jni/silk/src/SKP_Silk_apply_sine_window.c rename to app/src/main/jni/silk/src/SKP_Silk_apply_sine_window.c index 5d2b569..20b9059 100644 --- a/jni/silk/src/SKP_Silk_apply_sine_window.c +++ b/app/src/main/jni/silk/src/SKP_Silk_apply_sine_window.c @@ -1,92 +1,92 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -/* Apply sine window to signal vector. */ -/* Window types: */ -/* 0 -> sine window from 0 to pi */ -/* 1 -> sine window from 0 to pi/2 */ -/* 2 -> sine window from pi/2 to pi */ -/* every other sample of window is linearly interpolated, for speed */ -void SKP_Silk_apply_sine_window( - SKP_int16 px_win[], /* O Pointer to windowed signal */ - const SKP_int16 px[], /* I Pointer to input signal */ - const SKP_int win_type, /* I Selects a window type */ - const SKP_int length /* I Window length, multiple of 4 */ -) -{ - SKP_int k; - SKP_int32 px32, f_Q16, c_Q20, S0_Q16, S1_Q16; - - /* Length must be multiple of 4 */ - SKP_assert( ( length & 3 ) == 0 ); - - /* Input pointer must be 4-byte aligned */ - SKP_assert( ( (SKP_int64)px & 3 ) == 0 ); - - if( win_type == 0 ) { - f_Q16 = SKP_DIV32_16( 411775, length + 1 ); // 411775 = 2 * 65536 * pi - } else { - f_Q16 = SKP_DIV32_16( 205887, length + 1 ); // 205887 = 65536 * pi - } - - /* factor used for cosine approximation */ - c_Q20 = -SKP_RSHIFT( SKP_MUL( f_Q16, f_Q16 ), 12 ); - - /* c_Q20 becomes too large if length is too small */ - SKP_assert( c_Q20 >= -32768 ); - - /* initialize state */ - if( win_type < 2 ) { - /* start from 0 */ - S0_Q16 = 0; - /* approximation of sin(f) */ - S1_Q16 = f_Q16; - } else { - /* start from 1 */ - S0_Q16 = ( 1 << 16 ); - /* approximation of cos(f) */ - S1_Q16 = ( 1 << 16 ) + SKP_RSHIFT( c_Q20, 5 ); - } - - /* Uses the recursive equation: sin(n*f) = 2 * cos(f) * sin((n-1)*f) - sin((n-2)*f) */ - /* 4 samples at a time */ - for( k = 0; k < length; k += 4 ) { - px32 = *( (SKP_int32 *)&px[ k ] ); /* load two values at once */ - px_win[ k ] = (SKP_int16)SKP_SMULWB( SKP_RSHIFT( S0_Q16 + S1_Q16, 1 ), px32 ); - px_win[ k + 1 ] = (SKP_int16)SKP_SMULWT( S1_Q16, px32 ); - S0_Q16 = SKP_RSHIFT( SKP_MUL( c_Q20, S1_Q16 ), 20 ) + SKP_LSHIFT( S1_Q16, 1 ) - S0_Q16 + 1; - S0_Q16 = SKP_min( S0_Q16, ( 1 << 16 ) ); - - px32 = *( (SKP_int32 *)&px[k + 2] ); /* load two values at once */ - px_win[ k + 2 ] = (SKP_int16)SKP_SMULWB( SKP_RSHIFT( S0_Q16 + S1_Q16, 1 ), px32 ); - px_win[ k + 3 ] = (SKP_int16)SKP_SMULWT( S0_Q16, px32 ); - S1_Q16 = SKP_RSHIFT( SKP_MUL( c_Q20, S0_Q16 ), 20 ) + SKP_LSHIFT( S0_Q16, 1 ) - S1_Q16; - S1_Q16 = SKP_min( S1_Q16, ( 1 << 16 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +/* Apply sine window to signal vector. */ +/* Window types: */ +/* 0 -> sine window from 0 to pi */ +/* 1 -> sine window from 0 to pi/2 */ +/* 2 -> sine window from pi/2 to pi */ +/* every other sample of window is linearly interpolated, for speed */ +void SKP_Silk_apply_sine_window( + SKP_int16 px_win[], /* O Pointer to windowed signal */ + const SKP_int16 px[], /* I Pointer to input signal */ + const SKP_int win_type, /* I Selects a window type */ + const SKP_int length /* I Window length, multiple of 4 */ +) +{ + SKP_int k; + SKP_int32 px32, f_Q16, c_Q20, S0_Q16, S1_Q16; + + /* Length must be multiple of 4 */ + SKP_assert( ( length & 3 ) == 0 ); + + /* Input pointer must be 4-byte aligned */ + SKP_assert( ( (SKP_int64)px & 3 ) == 0 ); + + if( win_type == 0 ) { + f_Q16 = SKP_DIV32_16( 411775, length + 1 ); // 411775 = 2 * 65536 * pi + } else { + f_Q16 = SKP_DIV32_16( 205887, length + 1 ); // 205887 = 65536 * pi + } + + /* factor used for cosine approximation */ + c_Q20 = -SKP_RSHIFT( SKP_MUL( f_Q16, f_Q16 ), 12 ); + + /* c_Q20 becomes too large if length is too small */ + SKP_assert( c_Q20 >= -32768 ); + + /* initialize state */ + if( win_type < 2 ) { + /* start from 0 */ + S0_Q16 = 0; + /* approximation of sin(f) */ + S1_Q16 = f_Q16; + } else { + /* start from 1 */ + S0_Q16 = ( 1 << 16 ); + /* approximation of cos(f) */ + S1_Q16 = ( 1 << 16 ) + SKP_RSHIFT( c_Q20, 5 ); + } + + /* Uses the recursive equation: sin(n*f) = 2 * cos(f) * sin((n-1)*f) - sin((n-2)*f) */ + /* 4 samples at a time */ + for( k = 0; k < length; k += 4 ) { + px32 = *( (SKP_int32 *)&px[ k ] ); /* load two values at once */ + px_win[ k ] = (SKP_int16)SKP_SMULWB( SKP_RSHIFT( S0_Q16 + S1_Q16, 1 ), px32 ); + px_win[ k + 1 ] = (SKP_int16)SKP_SMULWT( S1_Q16, px32 ); + S0_Q16 = SKP_RSHIFT( SKP_MUL( c_Q20, S1_Q16 ), 20 ) + SKP_LSHIFT( S1_Q16, 1 ) - S0_Q16 + 1; + S0_Q16 = SKP_min( S0_Q16, ( 1 << 16 ) ); + + px32 = *( (SKP_int32 *)&px[k + 2] ); /* load two values at once */ + px_win[ k + 2 ] = (SKP_int16)SKP_SMULWB( SKP_RSHIFT( S0_Q16 + S1_Q16, 1 ), px32 ); + px_win[ k + 3 ] = (SKP_int16)SKP_SMULWT( S0_Q16, px32 ); + S1_Q16 = SKP_RSHIFT( SKP_MUL( c_Q20, S0_Q16 ), 20 ) + SKP_LSHIFT( S0_Q16, 1 ) - S1_Q16; + S1_Q16 = SKP_min( S1_Q16, ( 1 << 16 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_array_maxabs.c b/app/src/main/jni/silk/src/SKP_Silk_array_maxabs.c similarity index 98% rename from jni/silk/src/SKP_Silk_array_maxabs.c rename to app/src/main/jni/silk/src/SKP_Silk_array_maxabs.c index 915af9f..840ca0a 100644 --- a/jni/silk/src/SKP_Silk_array_maxabs.c +++ b/app/src/main/jni/silk/src/SKP_Silk_array_maxabs.c @@ -1,64 +1,64 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_int16_array_maxabs.c * - * * - * Function that returns the maximum absolut value of * - * the input vector * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Function that returns the maximum absolut value of the input vector */ -SKP_int16 SKP_Silk_int16_array_maxabs( /* O Maximum absolute value, max: 2^15-1 */ - const SKP_int16 *vec, /* I Input vector [len] */ - const SKP_int32 len /* I Length of input vector */ -) -{ - SKP_int32 max = 0, i, lvl = 0, ind; - - ind = len - 1; - max = SKP_SMULBB( vec[ ind ], vec[ ind ] ); - for( i = len - 2; i >= 0; i-- ) { - lvl = SKP_SMULBB( vec[ i ], vec[ i ] ); - if( lvl > max ) { - max = lvl; - ind = i; - } - } - - /* Do not return 32768, as it will not fit in an int16 so may lead to problems later on */ - lvl = SKP_abs( vec[ ind ] ); - if( lvl > SKP_int16_MAX ) { - return( SKP_int16_MAX ); - } else { - return( (SKP_int16)lvl ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_int16_array_maxabs.c * + * * + * Function that returns the maximum absolut value of * + * the input vector * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Function that returns the maximum absolut value of the input vector */ +SKP_int16 SKP_Silk_int16_array_maxabs( /* O Maximum absolute value, max: 2^15-1 */ + const SKP_int16 *vec, /* I Input vector [len] */ + const SKP_int32 len /* I Length of input vector */ +) +{ + SKP_int32 max = 0, i, lvl = 0, ind; + + ind = len - 1; + max = SKP_SMULBB( vec[ ind ], vec[ ind ] ); + for( i = len - 2; i >= 0; i-- ) { + lvl = SKP_SMULBB( vec[ i ], vec[ i ] ); + if( lvl > max ) { + max = lvl; + ind = i; + } + } + + /* Do not return 32768, as it will not fit in an int16 so may lead to problems later on */ + lvl = SKP_abs( vec[ ind ] ); + if( lvl > SKP_int16_MAX ) { + return( SKP_int16_MAX ); + } else { + return( (SKP_int16)lvl ); + } +} diff --git a/jni/silk/src/SKP_Silk_autocorr.c b/app/src/main/jni/silk/src/SKP_Silk_autocorr.c similarity index 98% rename from jni/silk/src/SKP_Silk_autocorr.c rename to app/src/main/jni/silk/src/SKP_Silk_autocorr.c index 0d3652d..c9dbdec 100644 --- a/jni/silk/src/SKP_Silk_autocorr.c +++ b/app/src/main/jni/silk/src/SKP_Silk_autocorr.c @@ -1,81 +1,81 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_autocorr.c * - * * - * Calculates the autocorrelation * - * The result has 29 non-zero bits for the first correlation, to leave * - * some room for adding white noise fractions etc. * - * * - * Copyright 2008 (c), Skype Limited * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Compute autocorrelation */ -void SKP_Silk_autocorr( - SKP_int32 *results, /* O Result (length correlationCount) */ - SKP_int32 *scale, /* O Scaling of the correlation vector */ - const SKP_int16 *inputData, /* I Input data to correlate */ - const SKP_int inputDataSize, /* I Length of input */ - const SKP_int correlationCount /* I Number of correlation taps to compute */ -) -{ - SKP_int i, lz, nRightShifts, corrCount; - SKP_int64 corr64; - - corrCount = SKP_min_int( inputDataSize, correlationCount ); - - /* compute energy (zero-lag correlation) */ - corr64 = SKP_Silk_inner_prod16_aligned_64( inputData, inputData, inputDataSize ); - - /* deal with all-zero input data */ - corr64 += 1; - - /* number of leading zeros */ - lz = SKP_Silk_CLZ64( corr64 ); - - /* scaling: number of right shifts applied to correlations */ - nRightShifts = 35 - lz; - *scale = nRightShifts; - - if( nRightShifts <= 0 ) { - results[ 0 ] = SKP_LSHIFT( (SKP_int32)SKP_CHECK_FIT32( corr64 ), -nRightShifts ); - - /* compute remaining correlations based on int32 inner product */ - for( i = 1; i < corrCount; i++ ) { - results[ i ] = SKP_LSHIFT( SKP_Silk_inner_prod_aligned( inputData, inputData + i, inputDataSize - i ), -nRightShifts ); - } - } else { - results[ 0 ] = (SKP_int32)SKP_CHECK_FIT32( SKP_RSHIFT64( corr64, nRightShifts ) ); - - /* compute remaining correlations based on int64 inner product */ - for( i = 1; i < corrCount; i++ ) { - results[ i ] = (SKP_int32)SKP_CHECK_FIT32( SKP_RSHIFT64( SKP_Silk_inner_prod16_aligned_64( inputData, inputData + i, inputDataSize - i ), nRightShifts ) ); - } - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_autocorr.c * + * * + * Calculates the autocorrelation * + * The result has 29 non-zero bits for the first correlation, to leave * + * some room for adding white noise fractions etc. * + * * + * Copyright 2008 (c), Skype Limited * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Compute autocorrelation */ +void SKP_Silk_autocorr( + SKP_int32 *results, /* O Result (length correlationCount) */ + SKP_int32 *scale, /* O Scaling of the correlation vector */ + const SKP_int16 *inputData, /* I Input data to correlate */ + const SKP_int inputDataSize, /* I Length of input */ + const SKP_int correlationCount /* I Number of correlation taps to compute */ +) +{ + SKP_int i, lz, nRightShifts, corrCount; + SKP_int64 corr64; + + corrCount = SKP_min_int( inputDataSize, correlationCount ); + + /* compute energy (zero-lag correlation) */ + corr64 = SKP_Silk_inner_prod16_aligned_64( inputData, inputData, inputDataSize ); + + /* deal with all-zero input data */ + corr64 += 1; + + /* number of leading zeros */ + lz = SKP_Silk_CLZ64( corr64 ); + + /* scaling: number of right shifts applied to correlations */ + nRightShifts = 35 - lz; + *scale = nRightShifts; + + if( nRightShifts <= 0 ) { + results[ 0 ] = SKP_LSHIFT( (SKP_int32)SKP_CHECK_FIT32( corr64 ), -nRightShifts ); + + /* compute remaining correlations based on int32 inner product */ + for( i = 1; i < corrCount; i++ ) { + results[ i ] = SKP_LSHIFT( SKP_Silk_inner_prod_aligned( inputData, inputData + i, inputDataSize - i ), -nRightShifts ); + } + } else { + results[ 0 ] = (SKP_int32)SKP_CHECK_FIT32( SKP_RSHIFT64( corr64, nRightShifts ) ); + + /* compute remaining correlations based on int64 inner product */ + for( i = 1; i < corrCount; i++ ) { + results[ i ] = (SKP_int32)SKP_CHECK_FIT32( SKP_RSHIFT64( SKP_Silk_inner_prod16_aligned_64( inputData, inputData + i, inputDataSize - i ), nRightShifts ) ); + } + } +} diff --git a/jni/silk/src/SKP_Silk_biquad.c b/app/src/main/jni/silk/src/SKP_Silk_biquad.c similarity index 98% rename from jni/silk/src/SKP_Silk_biquad.c rename to app/src/main/jni/silk/src/SKP_Silk_biquad.c index 81b76f1..0eea1f2 100644 --- a/jni/silk/src/SKP_Silk_biquad.c +++ b/app/src/main/jni/silk/src/SKP_Silk_biquad.c @@ -1,72 +1,72 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_biquad.c * - * * - * Second order ARMA filter * - * Can handle slowly varying filter coefficients * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Second order ARMA filter */ -/* Can handle slowly varying filter coefficients */ -void SKP_Silk_biquad( - const SKP_int16 *in, /* I: input signal */ - const SKP_int16 *B, /* I: MA coefficients, Q13 [3] */ - const SKP_int16 *A, /* I: AR coefficients, Q13 [2] */ - SKP_int32 *S, /* I/O: state vector [2] */ - SKP_int16 *out, /* O: output signal */ - const SKP_int32 len /* I: signal length */ -) -{ - SKP_int k, in16; - SKP_int32 A0_neg, A1_neg, S0, S1, out32, tmp32; - - S0 = S[ 0 ]; - S1 = S[ 1 ]; - A0_neg = -A[ 0 ]; - A1_neg = -A[ 1 ]; - for( k = 0; k < len; k++ ) { - /* S[ 0 ], S[ 1 ]: Q13 */ - in16 = in[ k ]; - out32 = SKP_SMLABB( S0, in16, B[ 0 ] ); - - S0 = SKP_SMLABB( S1, in16, B[ 1 ] ); - S0 += SKP_LSHIFT( SKP_SMULWB( out32, A0_neg ), 3 ); - - S1 = SKP_LSHIFT( SKP_SMULWB( out32, A1_neg ), 3 ); - S1 = SKP_SMLABB( S1, in16, B[ 2 ] ); - tmp32 = SKP_RSHIFT_ROUND( out32, 13 ) + 1; - out[ k ] = (SKP_int16)SKP_SAT16( tmp32 ); - } - S[ 0 ] = S0; - S[ 1 ] = S1; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_biquad.c * + * * + * Second order ARMA filter * + * Can handle slowly varying filter coefficients * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Second order ARMA filter */ +/* Can handle slowly varying filter coefficients */ +void SKP_Silk_biquad( + const SKP_int16 *in, /* I: input signal */ + const SKP_int16 *B, /* I: MA coefficients, Q13 [3] */ + const SKP_int16 *A, /* I: AR coefficients, Q13 [2] */ + SKP_int32 *S, /* I/O: state vector [2] */ + SKP_int16 *out, /* O: output signal */ + const SKP_int32 len /* I: signal length */ +) +{ + SKP_int k, in16; + SKP_int32 A0_neg, A1_neg, S0, S1, out32, tmp32; + + S0 = S[ 0 ]; + S1 = S[ 1 ]; + A0_neg = -A[ 0 ]; + A1_neg = -A[ 1 ]; + for( k = 0; k < len; k++ ) { + /* S[ 0 ], S[ 1 ]: Q13 */ + in16 = in[ k ]; + out32 = SKP_SMLABB( S0, in16, B[ 0 ] ); + + S0 = SKP_SMLABB( S1, in16, B[ 1 ] ); + S0 += SKP_LSHIFT( SKP_SMULWB( out32, A0_neg ), 3 ); + + S1 = SKP_LSHIFT( SKP_SMULWB( out32, A1_neg ), 3 ); + S1 = SKP_SMLABB( S1, in16, B[ 2 ] ); + tmp32 = SKP_RSHIFT_ROUND( out32, 13 ) + 1; + out[ k ] = (SKP_int16)SKP_SAT16( tmp32 ); + } + S[ 0 ] = S0; + S[ 1 ] = S1; +} diff --git a/jni/silk/src/SKP_Silk_biquad_alt.c b/app/src/main/jni/silk/src/SKP_Silk_biquad_alt.c similarity index 98% rename from jni/silk/src/SKP_Silk_biquad_alt.c rename to app/src/main/jni/silk/src/SKP_Silk_biquad_alt.c index 658d4d6..0770e24 100644 --- a/jni/silk/src/SKP_Silk_biquad_alt.c +++ b/app/src/main/jni/silk/src/SKP_Silk_biquad_alt.c @@ -1,73 +1,73 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_biquad_alt.c * - * * - * Second order ARMA filter * - * Can handle slowly varying filter coefficients * - * */ -#include "SKP_Silk_SigProc_FIX.h" - - -/* Second order ARMA filter, alternative implementation */ -void SKP_Silk_biquad_alt( - const SKP_int16 *in, /* I: Input signal */ - const SKP_int32 *B_Q28, /* I: MA coefficients [3] */ - const SKP_int32 *A_Q28, /* I: AR coefficients [2] */ - SKP_int32 *S, /* I/O: State vector [2] */ - SKP_int16 *out, /* O: Output signal */ - const SKP_int32 len /* I: Signal length (must be even) */ -) -{ - /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ - SKP_int k; - SKP_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14; - - /* Negate A_Q28 values and split in two parts */ - A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF; /* lower part */ - A0_U_Q28 = SKP_RSHIFT( -A_Q28[ 0 ], 14 ); /* upper part */ - A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF; /* lower part */ - A1_U_Q28 = SKP_RSHIFT( -A_Q28[ 1 ], 14 ); /* upper part */ - - for( k = 0; k < len; k++ ) { - /* S[ 0 ], S[ 1 ]: Q12 */ - inval = in[ k ]; - out32_Q14 = SKP_LSHIFT( SKP_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 ); - - S[ 0 ] = S[1] + SKP_RSHIFT( SKP_SMULWB( out32_Q14, A0_L_Q28 ), 14 ); - S[ 0 ] = SKP_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 ); - S[ 0 ] = SKP_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval); - - S[ 1 ] = SKP_RSHIFT( SKP_SMULWB( out32_Q14, A1_L_Q28 ), 14 ); - S[ 1 ] = SKP_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 ); - S[ 1 ] = SKP_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval ); - - /* Scale back to Q0 and saturate */ - out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT( out32_Q14, 14 ) + 2 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_biquad_alt.c * + * * + * Second order ARMA filter * + * Can handle slowly varying filter coefficients * + * */ +#include "SKP_Silk_SigProc_FIX.h" + + +/* Second order ARMA filter, alternative implementation */ +void SKP_Silk_biquad_alt( + const SKP_int16 *in, /* I: Input signal */ + const SKP_int32 *B_Q28, /* I: MA coefficients [3] */ + const SKP_int32 *A_Q28, /* I: AR coefficients [2] */ + SKP_int32 *S, /* I/O: State vector [2] */ + SKP_int16 *out, /* O: Output signal */ + const SKP_int32 len /* I: Signal length (must be even) */ +) +{ + /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ + SKP_int k; + SKP_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14; + + /* Negate A_Q28 values and split in two parts */ + A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF; /* lower part */ + A0_U_Q28 = SKP_RSHIFT( -A_Q28[ 0 ], 14 ); /* upper part */ + A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF; /* lower part */ + A1_U_Q28 = SKP_RSHIFT( -A_Q28[ 1 ], 14 ); /* upper part */ + + for( k = 0; k < len; k++ ) { + /* S[ 0 ], S[ 1 ]: Q12 */ + inval = in[ k ]; + out32_Q14 = SKP_LSHIFT( SKP_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 ); + + S[ 0 ] = S[1] + SKP_RSHIFT( SKP_SMULWB( out32_Q14, A0_L_Q28 ), 14 ); + S[ 0 ] = SKP_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 ); + S[ 0 ] = SKP_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval); + + S[ 1 ] = SKP_RSHIFT( SKP_SMULWB( out32_Q14, A1_L_Q28 ), 14 ); + S[ 1 ] = SKP_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 ); + S[ 1 ] = SKP_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval ); + + /* Scale back to Q0 and saturate */ + out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT( out32_Q14, 14 ) + 2 ); + } +} diff --git a/jni/silk/src/SKP_Silk_burg_modified.c b/app/src/main/jni/silk/src/SKP_Silk_burg_modified.c similarity index 98% rename from jni/silk/src/SKP_Silk_burg_modified.c rename to app/src/main/jni/silk/src/SKP_Silk_burg_modified.c index 1ac47dc..37427af 100644 --- a/jni/silk/src/SKP_Silk_burg_modified.c +++ b/app/src/main/jni/silk/src/SKP_Silk_burg_modified.c @@ -1,228 +1,228 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_burg_modified.c * - * * - * Calculates the reflection coefficients from the input vector * - * Input vector contains nb_subfr sub vectors of length L_sub + D * - * * - * Copyright 2009 (c), Skype Limited * - * Date: 100105 * - */ - -#include "SKP_Silk_SigProc_FIX.h" - -#define MAX_FRAME_SIZE 544 // subfr_length * nb_subfr = ( 0.005 * 24000 + 16 ) * 4 = 544 -#define MAX_NB_SUBFR 4 - -#define QA 24 -#define N_BITS_HEAD_ROOM 2 -#define MIN_RSHIFTS -16 -#define MAX_RSHIFTS (32 - QA) - -/* Compute reflection coefficients from input signal */ -void SKP_Silk_burg_modified( - SKP_int32 *res_nrg, /* O residual energy */ - SKP_int *res_nrg_Q, /* O residual energy Q value */ - SKP_int32 A_Q16[], /* O prediction coefficients (length order) */ - const SKP_int16 x[], /* I input signal, length: nb_subfr * ( D + subfr_length ) */ - const SKP_int subfr_length, /* I input signal subframe length (including D preceeding samples) */ - const SKP_int nb_subfr, /* I number of subframes stacked in x */ - const SKP_int32 WhiteNoiseFrac_Q32, /* I fraction added to zero-lag autocorrelation */ - const SKP_int D /* I order */ -) -{ - SKP_int k, n, s, lz, rshifts, rshifts_extra; - SKP_int32 C0, num, nrg, rc_Q31, Atmp_QA, Atmp1, tmp1, tmp2, x1, x2; - const SKP_int16 *x_ptr; - - SKP_int32 C_first_row[ SigProc_MAX_ORDER_LPC ]; - SKP_int32 C_last_row[ SigProc_MAX_ORDER_LPC ]; - SKP_int32 Af_QA[ SigProc_MAX_ORDER_LPC ]; - - SKP_int32 CAf[ SigProc_MAX_ORDER_LPC + 1 ]; - SKP_int32 CAb[ SigProc_MAX_ORDER_LPC + 1 ]; - - SKP_assert( subfr_length * nb_subfr <= MAX_FRAME_SIZE ); - SKP_assert( nb_subfr <= MAX_NB_SUBFR ); - - - /* Compute autocorrelations, added over subframes */ - SKP_Silk_sum_sqr_shift( &C0, &rshifts, x, nb_subfr * subfr_length ); - if( rshifts > MAX_RSHIFTS ) { - C0 = SKP_LSHIFT32( C0, rshifts - MAX_RSHIFTS ); - SKP_assert( C0 > 0 ); - rshifts = MAX_RSHIFTS; - } else { - lz = SKP_Silk_CLZ32( C0 ) - 1; - rshifts_extra = N_BITS_HEAD_ROOM - lz; - if( rshifts_extra > 0 ) { - rshifts_extra = SKP_min( rshifts_extra, MAX_RSHIFTS - rshifts ); - C0 = SKP_RSHIFT32( C0, rshifts_extra ); - } else { - rshifts_extra = SKP_max( rshifts_extra, MIN_RSHIFTS - rshifts ); - C0 = SKP_LSHIFT32( C0, -rshifts_extra ); - } - rshifts += rshifts_extra; - } - SKP_memset( C_first_row, 0, SigProc_MAX_ORDER_LPC * sizeof( SKP_int32 ) ); - if( rshifts > 0 ) { - for( s = 0; s < nb_subfr; s++ ) { - x_ptr = x + s * subfr_length; - for( n = 1; n < D + 1; n++ ) { - C_first_row[ n - 1 ] += (SKP_int32)SKP_RSHIFT64( - SKP_Silk_inner_prod16_aligned_64( x_ptr, x_ptr + n, subfr_length - n ), rshifts ); - } - } - } else { - for( s = 0; s < nb_subfr; s++ ) { - x_ptr = x + s * subfr_length; - for( n = 1; n < D + 1; n++ ) { - C_first_row[ n - 1 ] += SKP_LSHIFT32( - SKP_Silk_inner_prod_aligned( x_ptr, x_ptr + n, subfr_length - n ), -rshifts ); - } - } - } - SKP_memcpy( C_last_row, C_first_row, SigProc_MAX_ORDER_LPC * sizeof( SKP_int32 ) ); - - /* Initialize */ - CAb[ 0 ] = CAf[ 0 ] = C0 + SKP_SMMUL( WhiteNoiseFrac_Q32, C0 ) + 1; // Q(-rshifts) - - for( n = 0; n < D; n++ ) { - /* Update first row of correlation matrix (without first element) */ - /* Update last row of correlation matrix (without last element, stored in reversed order) */ - /* Update C * Af */ - /* Update C * flipud(Af) (stored in reversed order) */ - if( rshifts > -2 ) { - for( s = 0; s < nb_subfr; s++ ) { - x_ptr = x + s * subfr_length; - x1 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], 16 - rshifts ); // Q(16-rshifts) - x2 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], 16 - rshifts ); // Q(16-rshifts) - tmp1 = SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], QA - 16 ); // Q(QA-16) - tmp2 = SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], QA - 16 ); // Q(QA-16) - for( k = 0; k < n; k++ ) { - C_first_row[ k ] = SKP_SMLAWB( C_first_row[ k ], x1, x_ptr[ n - k - 1 ] ); // Q( -rshifts ) - C_last_row[ k ] = SKP_SMLAWB( C_last_row[ k ], x2, x_ptr[ subfr_length - n + k ] ); // Q( -rshifts ) - Atmp_QA = Af_QA[ k ]; - tmp1 = SKP_SMLAWB( tmp1, Atmp_QA, x_ptr[ n - k - 1 ] ); // Q(QA-16) - tmp2 = SKP_SMLAWB( tmp2, Atmp_QA, x_ptr[ subfr_length - n + k ] ); // Q(QA-16) - } - tmp1 = SKP_LSHIFT32( -tmp1, 32 - QA - rshifts ); // Q(16-rshifts) - tmp2 = SKP_LSHIFT32( -tmp2, 32 - QA - rshifts ); // Q(16-rshifts) - for( k = 0; k <= n; k++ ) { - CAf[ k ] = SKP_SMLAWB( CAf[ k ], tmp1, x_ptr[ n - k ] ); // Q( -rshift ) - CAb[ k ] = SKP_SMLAWB( CAb[ k ], tmp2, x_ptr[ subfr_length - n + k - 1 ] ); // Q( -rshift ) - } - } - } else { - for( s = 0; s < nb_subfr; s++ ) { - x_ptr = x + s * subfr_length; - x1 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], -rshifts ); // Q( -rshifts ) - x2 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], -rshifts ); // Q( -rshifts ) - tmp1 = SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], 17 ); // Q17 - tmp2 = SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], 17 ); // Q17 - for( k = 0; k < n; k++ ) { - C_first_row[ k ] = SKP_MLA( C_first_row[ k ], x1, x_ptr[ n - k - 1 ] ); // Q( -rshifts ) - C_last_row[ k ] = SKP_MLA( C_last_row[ k ], x2, x_ptr[ subfr_length - n + k ] ); // Q( -rshifts ) - Atmp1 = SKP_RSHIFT_ROUND( Af_QA[ k ], QA - 17 ); // Q17 - tmp1 = SKP_MLA( tmp1, x_ptr[ n - k - 1 ], Atmp1 ); // Q17 - tmp2 = SKP_MLA( tmp2, x_ptr[ subfr_length - n + k ], Atmp1 ); // Q17 - } - tmp1 = -tmp1; // Q17 - tmp2 = -tmp2; // Q17 - for( k = 0; k <= n; k++ ) { - CAf[ k ] = SKP_SMLAWW( CAf[ k ], tmp1, - SKP_LSHIFT32( (SKP_int32)x_ptr[ n - k ], -rshifts - 1 ) ); // Q( -rshift ) - CAb[ k ] = SKP_SMLAWW( CAb[ k ], tmp2, - SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n + k - 1 ], -rshifts - 1 ) );// Q( -rshift ) - } - } - } - - /* Calculate nominator and denominator for the next order reflection (parcor) coefficient */ - tmp1 = C_first_row[ n ]; // Q( -rshifts ) - tmp2 = C_last_row[ n ]; // Q( -rshifts ) - num = 0; // Q( -rshifts ) - nrg = SKP_ADD32( CAb[ 0 ], CAf[ 0 ] ); // Q( 1-rshifts ) - for( k = 0; k < n; k++ ) { - Atmp_QA = Af_QA[ k ]; - lz = SKP_Silk_CLZ32( SKP_abs( Atmp_QA ) ) - 1; - lz = SKP_min( 32 - QA, lz ); - Atmp1 = SKP_LSHIFT32( Atmp_QA, lz ); // Q( QA + lz ) - - tmp1 = SKP_ADD_LSHIFT32( tmp1, SKP_SMMUL( C_last_row[ n - k - 1 ], Atmp1 ), 32 - QA - lz ); // Q( -rshifts ) - tmp2 = SKP_ADD_LSHIFT32( tmp2, SKP_SMMUL( C_first_row[ n - k - 1 ], Atmp1 ), 32 - QA - lz ); // Q( -rshifts ) - num = SKP_ADD_LSHIFT32( num, SKP_SMMUL( CAb[ n - k ], Atmp1 ), 32 - QA - lz ); // Q( -rshifts ) - nrg = SKP_ADD_LSHIFT32( nrg, SKP_SMMUL( SKP_ADD32( CAb[ k + 1 ], CAf[ k + 1 ] ), - Atmp1 ), 32 - QA - lz ); // Q( 1-rshifts ) - } - CAf[ n + 1 ] = tmp1; // Q( -rshifts ) - CAb[ n + 1 ] = tmp2; // Q( -rshifts ) - num = SKP_ADD32( num, tmp2 ); // Q( -rshifts ) - num = SKP_LSHIFT32( -num, 1 ); // Q( 1-rshifts ) - - /* Calculate the next order reflection (parcor) coefficient */ - if( SKP_abs( num ) < nrg ) { - rc_Q31 = SKP_DIV32_varQ( num, nrg, 31 ); - } else { - /* Negative energy or ratio too high; set remaining coefficients to zero and exit loop */ - SKP_memset( &Af_QA[ n ], 0, ( D - n ) * sizeof( SKP_int32 ) ); - SKP_assert( 0 ); - break; - } - - /* Update the AR coefficients */ - for( k = 0; k < (n + 1) >> 1; k++ ) { - tmp1 = Af_QA[ k ]; // QA - tmp2 = Af_QA[ n - k - 1 ]; // QA - Af_QA[ k ] = SKP_ADD_LSHIFT32( tmp1, SKP_SMMUL( tmp2, rc_Q31 ), 1 ); // QA - Af_QA[ n - k - 1 ] = SKP_ADD_LSHIFT32( tmp2, SKP_SMMUL( tmp1, rc_Q31 ), 1 ); // QA - } - Af_QA[ n ] = SKP_RSHIFT32( rc_Q31, 31 - QA ); // QA - - /* Update C * Af and C * Ab */ - for( k = 0; k <= n + 1; k++ ) { - tmp1 = CAf[ k ]; // Q( -rshifts ) - tmp2 = CAb[ n - k + 1 ]; // Q( -rshifts ) - CAf[ k ] = SKP_ADD_LSHIFT32( tmp1, SKP_SMMUL( tmp2, rc_Q31 ), 1 ); // Q( -rshifts ) - CAb[ n - k + 1 ] = SKP_ADD_LSHIFT32( tmp2, SKP_SMMUL( tmp1, rc_Q31 ), 1 ); // Q( -rshifts ) - } - } - - /* Return residual energy */ - nrg = CAf[ 0 ]; // Q( -rshifts ) - tmp1 = 1 << 16; // Q16 - for( k = 0; k < D; k++ ) { - Atmp1 = SKP_RSHIFT_ROUND( Af_QA[ k ], QA - 16 ); // Q16 - nrg = SKP_SMLAWW( nrg, CAf[ k + 1 ], Atmp1 ); // Q( -rshifts ) - tmp1 = SKP_SMLAWW( tmp1, Atmp1, Atmp1 ); // Q16 - A_Q16[ k ] = -Atmp1; - } - *res_nrg = SKP_SMLAWW( nrg, SKP_SMMUL( WhiteNoiseFrac_Q32, C0 ), -tmp1 ); // Q( -rshifts ) - *res_nrg_Q = -rshifts; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_burg_modified.c * + * * + * Calculates the reflection coefficients from the input vector * + * Input vector contains nb_subfr sub vectors of length L_sub + D * + * * + * Copyright 2009 (c), Skype Limited * + * Date: 100105 * + */ + +#include "SKP_Silk_SigProc_FIX.h" + +#define MAX_FRAME_SIZE 544 // subfr_length * nb_subfr = ( 0.005 * 24000 + 16 ) * 4 = 544 +#define MAX_NB_SUBFR 4 + +#define QA 24 +#define N_BITS_HEAD_ROOM 2 +#define MIN_RSHIFTS -16 +#define MAX_RSHIFTS (32 - QA) + +/* Compute reflection coefficients from input signal */ +void SKP_Silk_burg_modified( + SKP_int32 *res_nrg, /* O residual energy */ + SKP_int *res_nrg_Q, /* O residual energy Q value */ + SKP_int32 A_Q16[], /* O prediction coefficients (length order) */ + const SKP_int16 x[], /* I input signal, length: nb_subfr * ( D + subfr_length ) */ + const SKP_int subfr_length, /* I input signal subframe length (including D preceeding samples) */ + const SKP_int nb_subfr, /* I number of subframes stacked in x */ + const SKP_int32 WhiteNoiseFrac_Q32, /* I fraction added to zero-lag autocorrelation */ + const SKP_int D /* I order */ +) +{ + SKP_int k, n, s, lz, rshifts, rshifts_extra; + SKP_int32 C0, num, nrg, rc_Q31, Atmp_QA, Atmp1, tmp1, tmp2, x1, x2; + const SKP_int16 *x_ptr; + + SKP_int32 C_first_row[ SigProc_MAX_ORDER_LPC ]; + SKP_int32 C_last_row[ SigProc_MAX_ORDER_LPC ]; + SKP_int32 Af_QA[ SigProc_MAX_ORDER_LPC ]; + + SKP_int32 CAf[ SigProc_MAX_ORDER_LPC + 1 ]; + SKP_int32 CAb[ SigProc_MAX_ORDER_LPC + 1 ]; + + SKP_assert( subfr_length * nb_subfr <= MAX_FRAME_SIZE ); + SKP_assert( nb_subfr <= MAX_NB_SUBFR ); + + + /* Compute autocorrelations, added over subframes */ + SKP_Silk_sum_sqr_shift( &C0, &rshifts, x, nb_subfr * subfr_length ); + if( rshifts > MAX_RSHIFTS ) { + C0 = SKP_LSHIFT32( C0, rshifts - MAX_RSHIFTS ); + SKP_assert( C0 > 0 ); + rshifts = MAX_RSHIFTS; + } else { + lz = SKP_Silk_CLZ32( C0 ) - 1; + rshifts_extra = N_BITS_HEAD_ROOM - lz; + if( rshifts_extra > 0 ) { + rshifts_extra = SKP_min( rshifts_extra, MAX_RSHIFTS - rshifts ); + C0 = SKP_RSHIFT32( C0, rshifts_extra ); + } else { + rshifts_extra = SKP_max( rshifts_extra, MIN_RSHIFTS - rshifts ); + C0 = SKP_LSHIFT32( C0, -rshifts_extra ); + } + rshifts += rshifts_extra; + } + SKP_memset( C_first_row, 0, SigProc_MAX_ORDER_LPC * sizeof( SKP_int32 ) ); + if( rshifts > 0 ) { + for( s = 0; s < nb_subfr; s++ ) { + x_ptr = x + s * subfr_length; + for( n = 1; n < D + 1; n++ ) { + C_first_row[ n - 1 ] += (SKP_int32)SKP_RSHIFT64( + SKP_Silk_inner_prod16_aligned_64( x_ptr, x_ptr + n, subfr_length - n ), rshifts ); + } + } + } else { + for( s = 0; s < nb_subfr; s++ ) { + x_ptr = x + s * subfr_length; + for( n = 1; n < D + 1; n++ ) { + C_first_row[ n - 1 ] += SKP_LSHIFT32( + SKP_Silk_inner_prod_aligned( x_ptr, x_ptr + n, subfr_length - n ), -rshifts ); + } + } + } + SKP_memcpy( C_last_row, C_first_row, SigProc_MAX_ORDER_LPC * sizeof( SKP_int32 ) ); + + /* Initialize */ + CAb[ 0 ] = CAf[ 0 ] = C0 + SKP_SMMUL( WhiteNoiseFrac_Q32, C0 ) + 1; // Q(-rshifts) + + for( n = 0; n < D; n++ ) { + /* Update first row of correlation matrix (without first element) */ + /* Update last row of correlation matrix (without last element, stored in reversed order) */ + /* Update C * Af */ + /* Update C * flipud(Af) (stored in reversed order) */ + if( rshifts > -2 ) { + for( s = 0; s < nb_subfr; s++ ) { + x_ptr = x + s * subfr_length; + x1 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], 16 - rshifts ); // Q(16-rshifts) + x2 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], 16 - rshifts ); // Q(16-rshifts) + tmp1 = SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], QA - 16 ); // Q(QA-16) + tmp2 = SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], QA - 16 ); // Q(QA-16) + for( k = 0; k < n; k++ ) { + C_first_row[ k ] = SKP_SMLAWB( C_first_row[ k ], x1, x_ptr[ n - k - 1 ] ); // Q( -rshifts ) + C_last_row[ k ] = SKP_SMLAWB( C_last_row[ k ], x2, x_ptr[ subfr_length - n + k ] ); // Q( -rshifts ) + Atmp_QA = Af_QA[ k ]; + tmp1 = SKP_SMLAWB( tmp1, Atmp_QA, x_ptr[ n - k - 1 ] ); // Q(QA-16) + tmp2 = SKP_SMLAWB( tmp2, Atmp_QA, x_ptr[ subfr_length - n + k ] ); // Q(QA-16) + } + tmp1 = SKP_LSHIFT32( -tmp1, 32 - QA - rshifts ); // Q(16-rshifts) + tmp2 = SKP_LSHIFT32( -tmp2, 32 - QA - rshifts ); // Q(16-rshifts) + for( k = 0; k <= n; k++ ) { + CAf[ k ] = SKP_SMLAWB( CAf[ k ], tmp1, x_ptr[ n - k ] ); // Q( -rshift ) + CAb[ k ] = SKP_SMLAWB( CAb[ k ], tmp2, x_ptr[ subfr_length - n + k - 1 ] ); // Q( -rshift ) + } + } + } else { + for( s = 0; s < nb_subfr; s++ ) { + x_ptr = x + s * subfr_length; + x1 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], -rshifts ); // Q( -rshifts ) + x2 = -SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], -rshifts ); // Q( -rshifts ) + tmp1 = SKP_LSHIFT32( (SKP_int32)x_ptr[ n ], 17 ); // Q17 + tmp2 = SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n - 1 ], 17 ); // Q17 + for( k = 0; k < n; k++ ) { + C_first_row[ k ] = SKP_MLA( C_first_row[ k ], x1, x_ptr[ n - k - 1 ] ); // Q( -rshifts ) + C_last_row[ k ] = SKP_MLA( C_last_row[ k ], x2, x_ptr[ subfr_length - n + k ] ); // Q( -rshifts ) + Atmp1 = SKP_RSHIFT_ROUND( Af_QA[ k ], QA - 17 ); // Q17 + tmp1 = SKP_MLA( tmp1, x_ptr[ n - k - 1 ], Atmp1 ); // Q17 + tmp2 = SKP_MLA( tmp2, x_ptr[ subfr_length - n + k ], Atmp1 ); // Q17 + } + tmp1 = -tmp1; // Q17 + tmp2 = -tmp2; // Q17 + for( k = 0; k <= n; k++ ) { + CAf[ k ] = SKP_SMLAWW( CAf[ k ], tmp1, + SKP_LSHIFT32( (SKP_int32)x_ptr[ n - k ], -rshifts - 1 ) ); // Q( -rshift ) + CAb[ k ] = SKP_SMLAWW( CAb[ k ], tmp2, + SKP_LSHIFT32( (SKP_int32)x_ptr[ subfr_length - n + k - 1 ], -rshifts - 1 ) );// Q( -rshift ) + } + } + } + + /* Calculate nominator and denominator for the next order reflection (parcor) coefficient */ + tmp1 = C_first_row[ n ]; // Q( -rshifts ) + tmp2 = C_last_row[ n ]; // Q( -rshifts ) + num = 0; // Q( -rshifts ) + nrg = SKP_ADD32( CAb[ 0 ], CAf[ 0 ] ); // Q( 1-rshifts ) + for( k = 0; k < n; k++ ) { + Atmp_QA = Af_QA[ k ]; + lz = SKP_Silk_CLZ32( SKP_abs( Atmp_QA ) ) - 1; + lz = SKP_min( 32 - QA, lz ); + Atmp1 = SKP_LSHIFT32( Atmp_QA, lz ); // Q( QA + lz ) + + tmp1 = SKP_ADD_LSHIFT32( tmp1, SKP_SMMUL( C_last_row[ n - k - 1 ], Atmp1 ), 32 - QA - lz ); // Q( -rshifts ) + tmp2 = SKP_ADD_LSHIFT32( tmp2, SKP_SMMUL( C_first_row[ n - k - 1 ], Atmp1 ), 32 - QA - lz ); // Q( -rshifts ) + num = SKP_ADD_LSHIFT32( num, SKP_SMMUL( CAb[ n - k ], Atmp1 ), 32 - QA - lz ); // Q( -rshifts ) + nrg = SKP_ADD_LSHIFT32( nrg, SKP_SMMUL( SKP_ADD32( CAb[ k + 1 ], CAf[ k + 1 ] ), + Atmp1 ), 32 - QA - lz ); // Q( 1-rshifts ) + } + CAf[ n + 1 ] = tmp1; // Q( -rshifts ) + CAb[ n + 1 ] = tmp2; // Q( -rshifts ) + num = SKP_ADD32( num, tmp2 ); // Q( -rshifts ) + num = SKP_LSHIFT32( -num, 1 ); // Q( 1-rshifts ) + + /* Calculate the next order reflection (parcor) coefficient */ + if( SKP_abs( num ) < nrg ) { + rc_Q31 = SKP_DIV32_varQ( num, nrg, 31 ); + } else { + /* Negative energy or ratio too high; set remaining coefficients to zero and exit loop */ + SKP_memset( &Af_QA[ n ], 0, ( D - n ) * sizeof( SKP_int32 ) ); + SKP_assert( 0 ); + break; + } + + /* Update the AR coefficients */ + for( k = 0; k < (n + 1) >> 1; k++ ) { + tmp1 = Af_QA[ k ]; // QA + tmp2 = Af_QA[ n - k - 1 ]; // QA + Af_QA[ k ] = SKP_ADD_LSHIFT32( tmp1, SKP_SMMUL( tmp2, rc_Q31 ), 1 ); // QA + Af_QA[ n - k - 1 ] = SKP_ADD_LSHIFT32( tmp2, SKP_SMMUL( tmp1, rc_Q31 ), 1 ); // QA + } + Af_QA[ n ] = SKP_RSHIFT32( rc_Q31, 31 - QA ); // QA + + /* Update C * Af and C * Ab */ + for( k = 0; k <= n + 1; k++ ) { + tmp1 = CAf[ k ]; // Q( -rshifts ) + tmp2 = CAb[ n - k + 1 ]; // Q( -rshifts ) + CAf[ k ] = SKP_ADD_LSHIFT32( tmp1, SKP_SMMUL( tmp2, rc_Q31 ), 1 ); // Q( -rshifts ) + CAb[ n - k + 1 ] = SKP_ADD_LSHIFT32( tmp2, SKP_SMMUL( tmp1, rc_Q31 ), 1 ); // Q( -rshifts ) + } + } + + /* Return residual energy */ + nrg = CAf[ 0 ]; // Q( -rshifts ) + tmp1 = 1 << 16; // Q16 + for( k = 0; k < D; k++ ) { + Atmp1 = SKP_RSHIFT_ROUND( Af_QA[ k ], QA - 16 ); // Q16 + nrg = SKP_SMLAWW( nrg, CAf[ k + 1 ], Atmp1 ); // Q( -rshifts ) + tmp1 = SKP_SMLAWW( tmp1, Atmp1, Atmp1 ); // Q16 + A_Q16[ k ] = -Atmp1; + } + *res_nrg = SKP_SMLAWW( nrg, SKP_SMMUL( WhiteNoiseFrac_Q32, C0 ), -tmp1 ); // Q( -rshifts ) + *res_nrg_Q = -rshifts; +} diff --git a/jni/silk/src/SKP_Silk_bwexpander.c b/app/src/main/jni/silk/src/SKP_Silk_bwexpander.c similarity index 98% rename from jni/silk/src/SKP_Silk_bwexpander.c rename to app/src/main/jni/silk/src/SKP_Silk_bwexpander.c index d2f5aa6..47a806a 100644 --- a/jni/silk/src/SKP_Silk_bwexpander.c +++ b/app/src/main/jni/silk/src/SKP_Silk_bwexpander.c @@ -1,49 +1,49 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -/* Chirp (bandwidth expand) LP AR filter */ -void SKP_Silk_bwexpander( - SKP_int16 *ar, /* I/O AR filter to be expanded (without leading 1) */ - const SKP_int d, /* I Length of ar */ - SKP_int32 chirp_Q16 /* I Chirp factor (typically in the range 0 to 1) */ -) -{ - SKP_int i; - SKP_int32 chirp_minus_one_Q16; - - chirp_minus_one_Q16 = chirp_Q16 - 65536; - - /* NB: Dont use SKP_SMULWB, instead of SKP_RSHIFT_ROUND( SKP_MUL() , 16 ), below. */ - /* Bias in SKP_SMULWB can lead to unstable filters */ - for( i = 0; i < d - 1; i++ ) { - ar[ i ] = (SKP_int16)SKP_RSHIFT_ROUND( SKP_MUL( chirp_Q16, ar[ i ] ), 16 ); - chirp_Q16 += SKP_RSHIFT_ROUND( SKP_MUL( chirp_Q16, chirp_minus_one_Q16 ), 16 ); - } - ar[ d - 1 ] = (SKP_int16)SKP_RSHIFT_ROUND( SKP_MUL( chirp_Q16, ar[ d - 1 ] ), 16 ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +/* Chirp (bandwidth expand) LP AR filter */ +void SKP_Silk_bwexpander( + SKP_int16 *ar, /* I/O AR filter to be expanded (without leading 1) */ + const SKP_int d, /* I Length of ar */ + SKP_int32 chirp_Q16 /* I Chirp factor (typically in the range 0 to 1) */ +) +{ + SKP_int i; + SKP_int32 chirp_minus_one_Q16; + + chirp_minus_one_Q16 = chirp_Q16 - 65536; + + /* NB: Dont use SKP_SMULWB, instead of SKP_RSHIFT_ROUND( SKP_MUL() , 16 ), below. */ + /* Bias in SKP_SMULWB can lead to unstable filters */ + for( i = 0; i < d - 1; i++ ) { + ar[ i ] = (SKP_int16)SKP_RSHIFT_ROUND( SKP_MUL( chirp_Q16, ar[ i ] ), 16 ); + chirp_Q16 += SKP_RSHIFT_ROUND( SKP_MUL( chirp_Q16, chirp_minus_one_Q16 ), 16 ); + } + ar[ d - 1 ] = (SKP_int16)SKP_RSHIFT_ROUND( SKP_MUL( chirp_Q16, ar[ d - 1 ] ), 16 ); +} diff --git a/jni/silk/src/SKP_Silk_bwexpander_32.c b/app/src/main/jni/silk/src/SKP_Silk_bwexpander_32.c similarity index 98% rename from jni/silk/src/SKP_Silk_bwexpander_32.c rename to app/src/main/jni/silk/src/SKP_Silk_bwexpander_32.c index e4a4e98..5b5653d 100644 --- a/jni/silk/src/SKP_Silk_bwexpander_32.c +++ b/app/src/main/jni/silk/src/SKP_Silk_bwexpander_32.c @@ -1,46 +1,46 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -/* Chirp (bandwidth expand) LP AR filter */ -void SKP_Silk_bwexpander_32( - SKP_int32 *ar, /* I/O AR filter to be expanded (without leading 1) */ - const SKP_int d, /* I Length of ar */ - SKP_int32 chirp_Q16 /* I Chirp factor in Q16 */ -) -{ - SKP_int i; - SKP_int32 tmp_chirp_Q16; - - tmp_chirp_Q16 = chirp_Q16; - for( i = 0; i < d - 1; i++ ) { - ar[ i ] = SKP_SMULWW( ar[ i ], tmp_chirp_Q16 ); - tmp_chirp_Q16 = SKP_SMULWW( chirp_Q16, tmp_chirp_Q16 ); - } - ar[ d - 1 ] = SKP_SMULWW( ar[ d - 1 ], tmp_chirp_Q16 ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +/* Chirp (bandwidth expand) LP AR filter */ +void SKP_Silk_bwexpander_32( + SKP_int32 *ar, /* I/O AR filter to be expanded (without leading 1) */ + const SKP_int d, /* I Length of ar */ + SKP_int32 chirp_Q16 /* I Chirp factor in Q16 */ +) +{ + SKP_int i; + SKP_int32 tmp_chirp_Q16; + + tmp_chirp_Q16 = chirp_Q16; + for( i = 0; i < d - 1; i++ ) { + ar[ i ] = SKP_SMULWW( ar[ i ], tmp_chirp_Q16 ); + tmp_chirp_Q16 = SKP_SMULWW( chirp_Q16, tmp_chirp_Q16 ); + } + ar[ d - 1 ] = SKP_SMULWW( ar[ d - 1 ], tmp_chirp_Q16 ); +} diff --git a/jni/silk/src/SKP_Silk_code_signs.c b/app/src/main/jni/silk/src/SKP_Silk_code_signs.c similarity index 98% rename from jni/silk/src/SKP_Silk_code_signs.c rename to app/src/main/jni/silk/src/SKP_Silk_code_signs.c index 007a6bf..4ab9cac 100644 --- a/jni/silk/src/SKP_Silk_code_signs.c +++ b/app/src/main/jni/silk/src/SKP_Silk_code_signs.c @@ -1,87 +1,87 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -//#define SKP_enc_map(a) ((a) > 0 ? 1 : 0) -//#define SKP_dec_map(a) ((a) > 0 ? 1 : -1) -/* shifting avoids if-statement */ -#define SKP_enc_map(a) ( SKP_RSHIFT( (a), 15 ) + 1 ) -#define SKP_dec_map(a) ( SKP_LSHIFT( (a), 1 ) - 1 ) - -/* Encodes signs of excitation */ -void SKP_Silk_encode_signs( - SKP_Silk_range_coder_state *sRC, /* I/O Range coder state */ - const SKP_int q[], /* I Pulse signal */ - const SKP_int length, /* I Length of input */ - const SKP_int sigtype, /* I Signal type */ - const SKP_int QuantOffsetType, /* I Quantization offset type */ - const SKP_int RateLevelIndex /* I Rate level index */ -) -{ - SKP_int i; - SKP_int inData; - const SKP_uint16 *cdf; - - i = SKP_SMULBB( N_RATE_LEVELS - 1, SKP_LSHIFT( sigtype, 1 ) + QuantOffsetType ) + RateLevelIndex; - cdf = SKP_Silk_sign_CDF[ i ]; - - for( i = 0; i < length; i++ ) { - if( q[ i ] != 0 ) { - inData = SKP_enc_map( q[ i ] ); /* - = 0, + = 1 */ - SKP_Silk_range_encoder( sRC, inData, cdf ); - } - } -} - -/* Decodes signs of excitation */ -void SKP_Silk_decode_signs( - SKP_Silk_range_coder_state *sRC, /* I/O Range coder state */ - SKP_int q[], /* I/O pulse signal */ - const SKP_int length, /* I length of output */ - const SKP_int sigtype, /* I Signal type */ - const SKP_int QuantOffsetType, /* I Quantization offset type */ - const SKP_int RateLevelIndex /* I Rate Level Index */ -) -{ - SKP_int i; - SKP_int data; - const SKP_uint16 *cdf; - - i = SKP_SMULBB( N_RATE_LEVELS - 1, SKP_LSHIFT( sigtype, 1 ) + QuantOffsetType ) + RateLevelIndex; - cdf = SKP_Silk_sign_CDF[ i ]; - - for( i = 0; i < length; i++ ) { - if( q[ i ] > 0 ) { - SKP_Silk_range_decoder( &data, sRC, cdf, 1 ); - /* attach sign */ - /* implementation with shift, subtraction, multiplication */ - q[ i ] *= SKP_dec_map( data ); - } - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +//#define SKP_enc_map(a) ((a) > 0 ? 1 : 0) +//#define SKP_dec_map(a) ((a) > 0 ? 1 : -1) +/* shifting avoids if-statement */ +#define SKP_enc_map(a) ( SKP_RSHIFT( (a), 15 ) + 1 ) +#define SKP_dec_map(a) ( SKP_LSHIFT( (a), 1 ) - 1 ) + +/* Encodes signs of excitation */ +void SKP_Silk_encode_signs( + SKP_Silk_range_coder_state *sRC, /* I/O Range coder state */ + const SKP_int q[], /* I Pulse signal */ + const SKP_int length, /* I Length of input */ + const SKP_int sigtype, /* I Signal type */ + const SKP_int QuantOffsetType, /* I Quantization offset type */ + const SKP_int RateLevelIndex /* I Rate level index */ +) +{ + SKP_int i; + SKP_int inData; + const SKP_uint16 *cdf; + + i = SKP_SMULBB( N_RATE_LEVELS - 1, SKP_LSHIFT( sigtype, 1 ) + QuantOffsetType ) + RateLevelIndex; + cdf = SKP_Silk_sign_CDF[ i ]; + + for( i = 0; i < length; i++ ) { + if( q[ i ] != 0 ) { + inData = SKP_enc_map( q[ i ] ); /* - = 0, + = 1 */ + SKP_Silk_range_encoder( sRC, inData, cdf ); + } + } +} + +/* Decodes signs of excitation */ +void SKP_Silk_decode_signs( + SKP_Silk_range_coder_state *sRC, /* I/O Range coder state */ + SKP_int q[], /* I/O pulse signal */ + const SKP_int length, /* I length of output */ + const SKP_int sigtype, /* I Signal type */ + const SKP_int QuantOffsetType, /* I Quantization offset type */ + const SKP_int RateLevelIndex /* I Rate Level Index */ +) +{ + SKP_int i; + SKP_int data; + const SKP_uint16 *cdf; + + i = SKP_SMULBB( N_RATE_LEVELS - 1, SKP_LSHIFT( sigtype, 1 ) + QuantOffsetType ) + RateLevelIndex; + cdf = SKP_Silk_sign_CDF[ i ]; + + for( i = 0; i < length; i++ ) { + if( q[ i ] > 0 ) { + SKP_Silk_range_decoder( &data, sRC, cdf, 1 ); + /* attach sign */ + /* implementation with shift, subtraction, multiplication */ + q[ i ] *= SKP_dec_map( data ); + } + } +} + diff --git a/jni/silk/src/SKP_Silk_common_pitch_est_defines.h b/app/src/main/jni/silk/src/SKP_Silk_common_pitch_est_defines.h similarity index 98% rename from jni/silk/src/SKP_Silk_common_pitch_est_defines.h rename to app/src/main/jni/silk/src/SKP_Silk_common_pitch_est_defines.h index 2e001f4..03e6e07 100644 --- a/jni/silk/src/SKP_Silk_common_pitch_est_defines.h +++ b/app/src/main/jni/silk/src/SKP_Silk_common_pitch_est_defines.h @@ -1,76 +1,76 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SIGPROC_COMMON_PITCH_EST_DEFINES_H -#define SIGPROC_COMMON_PITCH_EST_DEFINES_H - -#include "SKP_Silk_SigProc_FIX.h" - -/************************************************************/ -/* Definitions For Fix pitch estimator */ -/************************************************************/ - -#define PITCH_EST_MAX_FS_KHZ 24 /* Maximum sampling frequency used */ - -#define PITCH_EST_FRAME_LENGTH_MS 40 /* 40 ms */ - -#define PITCH_EST_MAX_FRAME_LENGTH (PITCH_EST_FRAME_LENGTH_MS * PITCH_EST_MAX_FS_KHZ) -#define PITCH_EST_MAX_FRAME_LENGTH_ST_1 (PITCH_EST_MAX_FRAME_LENGTH >> 2) -#define PITCH_EST_MAX_FRAME_LENGTH_ST_2 (PITCH_EST_MAX_FRAME_LENGTH >> 1) -#define PITCH_EST_MAX_SF_FRAME_LENGTH (PITCH_EST_SUB_FRAME * PITCH_EST_MAX_FS_KHZ) - -#define PITCH_EST_MAX_LAG_MS 18 /* 18 ms -> 56 Hz */ -#define PITCH_EST_MIN_LAG_MS 2 /* 2 ms -> 500 Hz */ -#define PITCH_EST_MAX_LAG (PITCH_EST_MAX_LAG_MS * PITCH_EST_MAX_FS_KHZ) -#define PITCH_EST_MIN_LAG (PITCH_EST_MIN_LAG_MS * PITCH_EST_MAX_FS_KHZ) - -#define PITCH_EST_NB_SUBFR 4 - -#define PITCH_EST_D_SRCH_LENGTH 24 - -#define PITCH_EST_MAX_DECIMATE_STATE_LENGTH 7 - -#define PITCH_EST_NB_STAGE3_LAGS 5 - -#define PITCH_EST_NB_CBKS_STAGE2 3 -#define PITCH_EST_NB_CBKS_STAGE2_EXT 11 - -#define PITCH_EST_CB_mn2 1 -#define PITCH_EST_CB_mx2 2 - -#define PITCH_EST_NB_CBKS_STAGE3_MAX 34 -#define PITCH_EST_NB_CBKS_STAGE3_MID 24 -#define PITCH_EST_NB_CBKS_STAGE3_MIN 16 - -extern const SKP_int16 SKP_Silk_CB_lags_stage2[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE2_EXT]; -extern const SKP_int16 SKP_Silk_CB_lags_stage3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX]; -extern const SKP_int16 SKP_Silk_Lag_range_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ] [ PITCH_EST_NB_SUBFR ][ 2 ]; -extern const SKP_int16 SKP_Silk_cbk_sizes_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ]; -extern const SKP_int16 SKP_Silk_cbk_offsets_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ]; - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SIGPROC_COMMON_PITCH_EST_DEFINES_H +#define SIGPROC_COMMON_PITCH_EST_DEFINES_H + +#include "SKP_Silk_SigProc_FIX.h" + +/************************************************************/ +/* Definitions For Fix pitch estimator */ +/************************************************************/ + +#define PITCH_EST_MAX_FS_KHZ 24 /* Maximum sampling frequency used */ + +#define PITCH_EST_FRAME_LENGTH_MS 40 /* 40 ms */ + +#define PITCH_EST_MAX_FRAME_LENGTH (PITCH_EST_FRAME_LENGTH_MS * PITCH_EST_MAX_FS_KHZ) +#define PITCH_EST_MAX_FRAME_LENGTH_ST_1 (PITCH_EST_MAX_FRAME_LENGTH >> 2) +#define PITCH_EST_MAX_FRAME_LENGTH_ST_2 (PITCH_EST_MAX_FRAME_LENGTH >> 1) +#define PITCH_EST_MAX_SF_FRAME_LENGTH (PITCH_EST_SUB_FRAME * PITCH_EST_MAX_FS_KHZ) + +#define PITCH_EST_MAX_LAG_MS 18 /* 18 ms -> 56 Hz */ +#define PITCH_EST_MIN_LAG_MS 2 /* 2 ms -> 500 Hz */ +#define PITCH_EST_MAX_LAG (PITCH_EST_MAX_LAG_MS * PITCH_EST_MAX_FS_KHZ) +#define PITCH_EST_MIN_LAG (PITCH_EST_MIN_LAG_MS * PITCH_EST_MAX_FS_KHZ) + +#define PITCH_EST_NB_SUBFR 4 + +#define PITCH_EST_D_SRCH_LENGTH 24 + +#define PITCH_EST_MAX_DECIMATE_STATE_LENGTH 7 + +#define PITCH_EST_NB_STAGE3_LAGS 5 + +#define PITCH_EST_NB_CBKS_STAGE2 3 +#define PITCH_EST_NB_CBKS_STAGE2_EXT 11 + +#define PITCH_EST_CB_mn2 1 +#define PITCH_EST_CB_mx2 2 + +#define PITCH_EST_NB_CBKS_STAGE3_MAX 34 +#define PITCH_EST_NB_CBKS_STAGE3_MID 24 +#define PITCH_EST_NB_CBKS_STAGE3_MIN 16 + +extern const SKP_int16 SKP_Silk_CB_lags_stage2[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE2_EXT]; +extern const SKP_int16 SKP_Silk_CB_lags_stage3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX]; +extern const SKP_int16 SKP_Silk_Lag_range_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ] [ PITCH_EST_NB_SUBFR ][ 2 ]; +extern const SKP_int16 SKP_Silk_cbk_sizes_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ]; +extern const SKP_int16 SKP_Silk_cbk_offsets_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ]; + +#endif + diff --git a/jni/silk/src/SKP_Silk_control_codec_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_control_codec_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_control_codec_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_control_codec_FIX.c index e4f6442..f56cebc 100644 --- a/jni/silk/src/SKP_Silk_control_codec_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_control_codec_FIX.c @@ -1,656 +1,656 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Control encoder SNR */ -SKP_int SKP_Silk_control_encoder_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk encoder state */ - const SKP_int API_fs_kHz, /* I External (API) sampling rate (kHz) */ - const SKP_int PacketSize_ms, /* I Packet length (ms) */ - SKP_int32 TargetRate_bps, /* I Target max bitrate (bps) (used if SNR_dB == 0) */ - const SKP_int PacketLoss_perc, /* I Packet loss rate (in percent) */ - const SKP_int INBandFec_enabled, /* I Enable (1) / disable (0) inband FEC */ - const SKP_int DTX_enabled, /* I Enable / disable DTX */ - const SKP_int InputFramesize_ms, /* I Inputframe in ms */ - const SKP_int Complexity /* I Complexity (0->low; 1->medium; 2->high) */ -) -{ - SKP_int32 LBRRRate_thres_bps; - SKP_int k, fs_kHz, ret = 0; - SKP_int32 frac_Q6; - const SKP_int32 *rateTable; - - /* State machine for the SWB/WB switching */ - fs_kHz = psEnc->sCmn.fs_kHz; - - /* Only switch during low speech activity, when no frames are sitting in the payload buffer */ - if( API_fs_kHz == 8 || fs_kHz == 0 || API_fs_kHz < fs_kHz ) { - // Switching is not possible, encoder just initialized, or internal mode higher than external - fs_kHz = API_fs_kHz; - } else { - - /* Resample all valid data in x_buf. Resampling the last part gets rid of a click, 5ms after switching */ - /* this is because the same state is used when downsampling in API.c and is then up to date */ - /* the click immidiatly after switching is most of the time still there */ - - if( psEnc->sCmn.fs_kHz == 24 ) { - /* Accumulate the difference between the target rate and limit */ - if( psEnc->sCmn.fs_kHz_changed == 0 ) { - psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - SWB2WB_BITRATE_BPS_INITIAL ); - } else { - psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - SWB2WB_BITRATE_BPS ); - } - psEnc->sCmn.bitrateDiff = SKP_min( psEnc->sCmn.bitrateDiff, 0 ); - - /* Check if we should switch from 24 to 16 kHz */ -#if SWITCH_TRANSITION_FILTERING - if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* Transition phase not active */ - ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD || psEnc->sCmn.sSWBdetect.WB_detected == 1 ) && - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - psEnc->sCmn.sLP.transition_frame_no = 1; /* Begin transition phase */ - psEnc->sCmn.sLP.mode = 0; /* Switch down */ - } - - if( ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) && ( psEnc->sCmn.sLP.mode == 0 ) && /* Transition phase complete, ready to switch */ -#else - if( ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD || psEnc->sCmn.sSWBdetect.WB_detected == 1 ) && -#endif - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - SKP_int16 x_bufout[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - - psEnc->sCmn.bitrateDiff = 0; - fs_kHz = 16; - - SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - SKP_memset( psEnc->sCmn.resample24To16state, 0, sizeof( psEnc->sCmn.resample24To16state ) ); - -#if LOW_COMPLEXITY_ONLY - { - SKP_int16 scratch[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; - SKP_Silk_resample_2_3_coarse( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, (SKP_int16*)scratch ); - } -#else - SKP_Silk_resample_2_3( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); -#endif - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_bufout, 0, 320 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - -#if SWITCH_TRANSITION_FILTERING - psEnc->sCmn.sLP.transition_frame_no = 0; /* Transition phase complete */ -#endif - } - } else if( psEnc->sCmn.fs_kHz == 16 ) { - - /* Check if we should switch from 16 to 24 kHz */ -#if SWITCH_TRANSITION_FILTERING - if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* No transition phase running, ready to switch */ -#else - if( -#endif - ( API_fs_kHz > psEnc->sCmn.fs_kHz && TargetRate_bps >= WB2SWB_BITRATE_BPS && psEnc->sCmn.sSWBdetect.WB_detected == 0 ) && - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - SKP_int16 x_bufout[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; - SKP_int32 resample16To24state[ 11 ]; - - psEnc->sCmn.bitrateDiff = 0; - fs_kHz = 24; - - SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - SKP_memset( resample16To24state, 0, sizeof(resample16To24state) ); - - SKP_Silk_resample_3_2( &x_bufout[ 0 ], resample16To24state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_bufout, 0, 480 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); -#if SWITCH_TRANSITION_FILTERING - psEnc->sCmn.sLP.mode = 1; /* Switch up */ -#endif - } else { - /* accumulate the difference between the target rate and limit */ - psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - WB2MB_BITRATE_BPS ); - psEnc->sCmn.bitrateDiff = SKP_min( psEnc->sCmn.bitrateDiff, 0 ); - - /* Check if we should switch from 16 to 12 kHz */ -#if SWITCH_TRANSITION_FILTERING - if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* Transition phase not active */ - ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - psEnc->sCmn.sLP.transition_frame_no = 1; /* Begin transition phase */ - psEnc->sCmn.sLP.mode = 0; /* Switch down */ - } - - if( ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) && ( psEnc->sCmn.sLP.mode == 0 ) && /* Transition phase complete, ready to switch */ -#else - if( ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && -#endif - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - - SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - psEnc->sCmn.bitrateDiff = 0; - fs_kHz = 12; - - if( API_fs_kHz == 24 ) { - - /* Intermediate upsampling of x_bufFIX from 16 to 24 kHz */ - SKP_int16 x_buf24[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; - SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 resample16To24state[ 11 ]; - - SKP_memset( resample16To24state, 0, sizeof( resample16To24state ) ); - SKP_Silk_resample_3_2( &x_buf24[ 0 ], resample16To24state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - /* Update the state of the resampler used in API.c, from 24 to 12 kHz */ - SKP_memset( psEnc->sCmn.resample24To12state, 0, sizeof( psEnc->sCmn.resample24To12state ) ); - SKP_Silk_resample_1_2_coarse( &x_buf24[ 0 ], psEnc->sCmn.resample24To12state, &x_buf[ 0 ], scratch, SKP_RSHIFT( SKP_SMULBB( 3, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ), 2 ) ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_buf, 0, 240 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - } else if( API_fs_kHz == 16 ) { - SKP_int16 x_bufout[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 4 ]; - SKP_memset( psEnc->sCmn.resample16To12state, 0, sizeof( psEnc->sCmn.resample16To12state ) ); - - SKP_Silk_resample_3_4( &x_bufout[ 0 ], psEnc->sCmn.resample16To12state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_bufout, 0, 240 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - } -#if SWITCH_TRANSITION_FILTERING - psEnc->sCmn.sLP.transition_frame_no = 0; /* Transition phase complete */ -#endif - } - } - } else if( psEnc->sCmn.fs_kHz == 12 ) { - - /* Check if we should switch from 12 to 16 kHz */ -#if SWITCH_TRANSITION_FILTERING - if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* No transition phase running, ready to switch */ -#else - if( -#endif - ( API_fs_kHz > psEnc->sCmn.fs_kHz && TargetRate_bps >= MB2WB_BITRATE_BPS ) && - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - - SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - psEnc->sCmn.bitrateDiff = 0; - fs_kHz = 16; - - /* Reset state of the resampler to be used */ - if( API_fs_kHz == 24 ) { - - SKP_int16 x_bufout[ 2 * 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 3 ]; - - /* Intermediate upsampling of x_bufFIX from 12 to 24 kHz */ - SKP_int16 x_buf24[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 resample12To24state[6]; - - SKP_memset( resample12To24state, 0, sizeof( resample12To24state ) ); - SKP_Silk_resample_2_1_coarse( &x_buf[ 0 ], resample12To24state, &x_buf24[ 0 ], scratch, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - SKP_memset( psEnc->sCmn.resample24To16state, 0, sizeof( psEnc->sCmn.resample24To16state ) ); - -#if LOW_COMPLEXITY_ONLY - SKP_assert( sizeof( SKP_int16 ) * ( 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) <= sizeof( scratch ) ); - SKP_Silk_resample_2_3_coarse( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf24[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ), (SKP_int16*)scratch ); -#else - SKP_Silk_resample_2_3( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf24[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ) ); -#endif - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_bufout, 0, 320 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - } -#if SWITCH_TRANSITION_FILTERING - psEnc->sCmn.sLP.mode = 1; /* Switch up */ -#endif - } else { - /* accumulate the difference between the target rate and limit */ - psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - MB2NB_BITRATE_BPS ); - psEnc->sCmn.bitrateDiff = SKP_min( psEnc->sCmn.bitrateDiff, 0 ); - - /* Check if we should switch from 12 to 8 kHz */ -#if SWITCH_TRANSITION_FILTERING - if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* Transition phase not active */ - ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - psEnc->sCmn.sLP.transition_frame_no = 1; /* Begin transition phase */ - psEnc->sCmn.sLP.mode = 0; /* Switch down */ - } - - if( ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) && ( psEnc->sCmn.sLP.mode == 0 ) && -#else - if( ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && -#endif - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - - SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - psEnc->sCmn.bitrateDiff = 0; - fs_kHz = 8; - - if( API_fs_kHz == 24 ) { - - SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - /* Intermediate upsampling of x_buf from 12 to 24 kHz */ - SKP_int16 x_buf24[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 resample12To24state[ 6 ]; - - SKP_memset( resample12To24state, 0, sizeof( resample12To24state ) ); - SKP_Silk_resample_2_1_coarse( &x_buf[ 0 ], resample12To24state, &x_buf24[ 0 ], scratch, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - /* Update the state of the resampler used in API.c, from 24 to 8 kHz */ - SKP_memset( psEnc->sCmn.resample24To8state, 0, sizeof( psEnc->sCmn.resample24To8state ) ); - SKP_Silk_resample_1_3( &x_buf[ 0 ], psEnc->sCmn.resample24To8state, &x_buf24[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ) ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_buf, 0, 160 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - } else if( API_fs_kHz == 16 ) { - /* Intermediate upsampling of x_bufFIX from 12 to 16 kHz */ - SKP_int16 x_buf16[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; - SKP_int32 resample12To16state[11]; - - SKP_memset( resample12To16state, 0, sizeof( resample12To16state ) ); - SKP_Silk_resample_3_2( &x_buf16[ 0 ], resample12To16state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_buf, 0, 160 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - } else if( API_fs_kHz == 12 ) { - SKP_int16 x_bufout[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 3 ]; - SKP_memset( psEnc->sCmn.resample12To8state, 0, sizeof( psEnc->sCmn.resample12To8state ) ); -#if LOW_COMPLEXITY_ONLY - { - SKP_int16 scratch[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; - SKP_Silk_resample_2_3_coarse( &x_bufout[ 0 ], psEnc->sCmn.resample12To8state, &x_buf[ 0 ], - SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, scratch ); - } -#else - SKP_Silk_resample_2_3( &x_bufout[ 0 ], psEnc->sCmn.resample12To8state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); -#endif - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_bufout, 0, 160 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - } -#if SWITCH_TRANSITION_FILTERING - psEnc->sCmn.sLP.transition_frame_no = 0; /* Transition phase complete */ -#endif - } - } - } else if( psEnc->sCmn.fs_kHz == 8 ) { - - /* Check if we should switch from 8 to 12 kHz */ -#if SWITCH_TRANSITION_FILTERING - if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* No transition phase running, ready to switch */ -#else - if( -#endif - ( API_fs_kHz > psEnc->sCmn.fs_kHz && TargetRate_bps >= NB2MB_BITRATE_BPS ) && - ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - - SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - psEnc->sCmn.bitrateDiff = 0; - fs_kHz = 12; - - /* Reset state of the resampler to be used */ - if( API_fs_kHz == 24 ) { - SKP_int16 x_buf24[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 scratch[ 3 * 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; - SKP_int32 resample8To24state[ 7 ]; - - /* Intermediate upsampling of x_bufFIX from 8 to 24 kHz */ - SKP_memset( resample8To24state, 0, sizeof( resample8To24state ) ); - SKP_Silk_resample_3_1( &x_buf24[ 0 ], resample8To24state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - SKP_memset( psEnc->sCmn.resample24To12state, 0, sizeof( psEnc->sCmn.resample24To12state ) ); - - SKP_Silk_resample_1_2_coarse( &x_buf24[ 0 ], psEnc->sCmn.resample24To12state, &x_buf[ 0 ], scratch, SKP_RSHIFT( SKP_SMULBB( 3, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ), 1 ) ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_buf, 0, 240 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - - } else if( API_fs_kHz == 16 ) { - SKP_int16 x_buf16[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; - SKP_int32 resample8To16state[ 6 ]; - - /* Intermediate upsampling of x_bufFIX from 8 to 16 kHz */ - SKP_memset( resample8To16state, 0, sizeof( resample8To16state ) ); - SKP_Silk_resample_2_1_coarse( &x_buf[ 0 ], resample8To16state, &x_buf16[ 0 ], scratch, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); - - SKP_memset( psEnc->sCmn.resample16To12state, 0, sizeof( psEnc->sCmn.resample16To12state ) ); - - SKP_Silk_resample_3_4( &x_buf[ 0 ], psEnc->sCmn.resample16To12state, &x_buf16[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ) ); - - /* set the first frame to zero, no performance difference was noticed though */ - SKP_memset( x_buf, 0, 240 * sizeof( SKP_int16 ) ); - SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); - } -#if SWITCH_TRANSITION_FILTERING - psEnc->sCmn.sLP.mode = 1; /* Switch up */ -#endif - } - } else { - // Internal sample frequency not supported! - SKP_assert( 0 ); - } - } - -#if SWITCH_TRANSITION_FILTERING - /* After switching up, stop transition filter during speech inactivity */ - if( ( psEnc->sCmn.sLP.mode == 1 ) && - ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_UP ) && - ( psEnc->speech_activity_Q8 < 128 ) && - ( psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { - - psEnc->sCmn.sLP.transition_frame_no = 0; - - /* Reset transition filter state */ - SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) ); - } -#endif - - - - /* Set internal sampling frequency */ - if( psEnc->sCmn.fs_kHz != fs_kHz ) { - /* reset part of the state */ - SKP_memset( &psEnc->sShape, 0, sizeof( SKP_Silk_shape_state_FIX ) ); - SKP_memset( &psEnc->sPrefilt, 0, sizeof( SKP_Silk_prefilter_state_FIX ) ); - SKP_memset( &psEnc->sNSQ, 0, sizeof( SKP_Silk_nsq_state ) ); - SKP_memset( &psEnc->sPred, 0, sizeof( SKP_Silk_predict_state_FIX ) ); - SKP_memset( psEnc->sNSQ.xq, 0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) ); - SKP_memset( psEnc->sNSQ_LBRR.xq, 0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) ); - SKP_memset( psEnc->sCmn.LBRR_buffer, 0, MAX_LBRR_DELAY * sizeof( SKP_SILK_LBRR_struct ) ); -#if SWITCH_TRANSITION_FILTERING - SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) ); - if( psEnc->sCmn.sLP.mode == 1 ) { - /* Begin transition phase */ - psEnc->sCmn.sLP.transition_frame_no = 1; - } else { - /* End transition phase */ - psEnc->sCmn.sLP.transition_frame_no = 0; - } -#endif - psEnc->sCmn.inputBufIx = 0; - psEnc->sCmn.nFramesInPayloadBuf = 0; - psEnc->sCmn.nBytesInPayloadBuf = 0; - psEnc->sCmn.oldest_LBRR_idx = 0; - psEnc->sCmn.TargetRate_bps = 0; /* ensures that psEnc->SNR_dB is recomputed */ - - SKP_memset( psEnc->sPred.prev_NLSFq_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) ); - - /* Initialize non-zero parameters */ - psEnc->sCmn.prevLag = 100; - psEnc->sCmn.prev_sigtype = SIG_TYPE_UNVOICED; - psEnc->sCmn.first_frame_after_reset = 1; - psEnc->sPrefilt.lagPrev = 100; - psEnc->sShape.LastGainIndex = 1; - psEnc->sNSQ.lagPrev = 100; - psEnc->sNSQ.prev_inv_gain_Q16 = 65536; - psEnc->sNSQ_LBRR.prev_inv_gain_Q16 = 65536; - psEnc->sCmn.fs_kHz = fs_kHz; - if( psEnc->sCmn.fs_kHz == 8 ) { - psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER; - psEnc->sCmn.psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_10; - psEnc->sCmn.psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_10; - } else { - psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER; - psEnc->sCmn.psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_16; - psEnc->sCmn.psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_16; - } - psEnc->sCmn.frame_length = SKP_SMULBB( FRAME_LENGTH_MS, fs_kHz ); - psEnc->sCmn.subfr_length = SKP_DIV32_16( psEnc->sCmn.frame_length, NB_SUBFR ); - psEnc->sCmn.la_pitch = SKP_SMULBB( LA_PITCH_MS, fs_kHz ); - psEnc->sCmn.la_shape = SKP_SMULBB( LA_SHAPE_MS, fs_kHz ); - psEnc->sPred.min_pitch_lag = SKP_SMULBB( 3, fs_kHz ); - psEnc->sPred.max_pitch_lag = SKP_SMULBB( 18, fs_kHz ); - psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz ); - if( psEnc->sCmn.fs_kHz == 24 ) { - psEnc->mu_LTP_Q8 = MU_LTP_QUANT_SWB_Q8; - } else if( psEnc->sCmn.fs_kHz == 16 ) { - psEnc->mu_LTP_Q8 = MU_LTP_QUANT_WB_Q8; - } else if( psEnc->sCmn.fs_kHz == 12 ) { - psEnc->mu_LTP_Q8 = MU_LTP_QUANT_MB_Q8; - } else { - psEnc->mu_LTP_Q8 = MU_LTP_QUANT_NB_Q8; - } - psEnc->sCmn.fs_kHz_changed = 1; - - /* Check that settings are valid */ - SKP_assert( ( psEnc->sCmn.subfr_length * NB_SUBFR ) == psEnc->sCmn.frame_length ); - } - - /* Set encoding complexity */ - if( Complexity == 0 || LOW_COMPLEXITY_ONLY ) { - /* Low complexity */ - psEnc->sCmn.Complexity = 0; - psEnc->sCmn.pitchEstimationComplexity = PITCH_EST_COMPLEXITY_LC_MODE; - psEnc->pitchEstimationThreshold_Q16 = FIND_PITCH_CORRELATION_THRESHOLD_Q16_LC_MODE; - psEnc->sCmn.pitchEstimationLPCOrder = 8; - psEnc->sCmn.shapingLPCOrder = 12; - psEnc->sCmn.nStatesDelayedDecision = 1; - psEnc->NoiseShapingQuantizer = SKP_Silk_NSQ; - psEnc->sCmn.useInterpolatedNLSFs = 0; - psEnc->sCmn.LTPQuantLowComplexity = 1; - psEnc->sCmn.NLSF_MSVQ_Survivors = MAX_NLSF_MSVQ_SURVIVORS_LC_MODE; - } else if( Complexity == 1 ) { - /* Medium complexity */ - psEnc->sCmn.Complexity = 1; - psEnc->sCmn.pitchEstimationComplexity = PITCH_EST_COMPLEXITY_MC_MODE; - psEnc->pitchEstimationThreshold_Q16 = FIND_PITCH_CORRELATION_THRESHOLD_Q16_MC_MODE; - psEnc->sCmn.pitchEstimationLPCOrder = 12; - psEnc->sCmn.shapingLPCOrder = 16; - psEnc->sCmn.nStatesDelayedDecision = 2; - psEnc->NoiseShapingQuantizer = SKP_Silk_NSQ_del_dec; - psEnc->sCmn.useInterpolatedNLSFs = 0; - psEnc->sCmn.LTPQuantLowComplexity = 0; - psEnc->sCmn.NLSF_MSVQ_Survivors = MAX_NLSF_MSVQ_SURVIVORS_MC_MODE; - } else if( Complexity == 2 ) { - /* High complexity */ - psEnc->sCmn.Complexity = 2; - psEnc->sCmn.pitchEstimationComplexity = PITCH_EST_COMPLEXITY_HC_MODE; - psEnc->pitchEstimationThreshold_Q16 = FIND_PITCH_CORRELATION_THRESHOLD_Q16_HC_MODE; - psEnc->sCmn.pitchEstimationLPCOrder = 16; - psEnc->sCmn.shapingLPCOrder = 16; - psEnc->sCmn.nStatesDelayedDecision = 4; - psEnc->NoiseShapingQuantizer = SKP_Silk_NSQ_del_dec; - psEnc->sCmn.useInterpolatedNLSFs = 1; - psEnc->sCmn.LTPQuantLowComplexity = 0; - psEnc->sCmn.NLSF_MSVQ_Survivors = MAX_NLSF_MSVQ_SURVIVORS; - } else { - ret = SKP_SILK_ENC_WRONG_COMPLEXITY_SETTING; - } - - /* Dont have higher Pitch estimation LPC order than predict LPC order */ - psEnc->sCmn.pitchEstimationLPCOrder = SKP_min_int( psEnc->sCmn.pitchEstimationLPCOrder, psEnc->sCmn.predictLPCOrder ); - - SKP_assert( psEnc->sCmn.pitchEstimationLPCOrder <= FIND_PITCH_LPC_ORDER_MAX ); - SKP_assert( psEnc->sCmn.shapingLPCOrder <= SHAPE_LPC_ORDER_MAX ); - SKP_assert( psEnc->sCmn.nStatesDelayedDecision <= DEL_DEC_STATES_MAX ); - - /* Set bitrate/coding quality */ - TargetRate_bps = SKP_min( TargetRate_bps, 100000 ); - if( psEnc->sCmn.fs_kHz == 8 ) { - TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_NB_BPS ); - } else if( psEnc->sCmn.fs_kHz == 12 ) { - TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_MB_BPS ); - } else if( psEnc->sCmn.fs_kHz == 16 ) { - TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_WB_BPS ); - } else { - TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_SWB_BPS ); - } - if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) { - psEnc->sCmn.TargetRate_bps = TargetRate_bps; - - /* if new TargetRate_bps, translate to SNR_dB value */ - if( psEnc->sCmn.fs_kHz == 8 ) { - rateTable = TargetRate_table_NB; - } else if( psEnc->sCmn.fs_kHz == 12 ) { - rateTable = TargetRate_table_MB; - } else if( psEnc->sCmn.fs_kHz == 16 ) { - rateTable = TargetRate_table_WB; - } else { - rateTable = TargetRate_table_SWB; - } - for( k = 1; k < TARGET_RATE_TAB_SZ; k++ ) { - /* find bitrate interval in table and interpolate */ - if( TargetRate_bps < rateTable[ k ] ) { - frac_Q6 = SKP_DIV32( SKP_LSHIFT( TargetRate_bps - rateTable[ k - 1 ], 6 ), rateTable[ k ] - rateTable[ k - 1 ] ); - psEnc->SNR_dB_Q7 = SKP_LSHIFT( SNR_table_Q1[ k - 1 ], 6 ) + SKP_MUL( frac_Q6, SNR_table_Q1[ k ] - SNR_table_Q1[ k - 1 ] ); - break; - } - } - } - - /* Set packet size */ - if( ( PacketSize_ms != 20 ) && - ( PacketSize_ms != 40 ) && - ( PacketSize_ms != 60 ) && - ( PacketSize_ms != 80 ) && - ( PacketSize_ms != 100 ) ) { - ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED; - } else { - if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) { - psEnc->sCmn.PacketSize_ms = PacketSize_ms; - - /* Packet length changes. Reset LBRR buffer */ - SKP_Silk_LBRR_reset( &psEnc->sCmn ); - } - } - - /* Set packet loss rate measured by farend */ - if( ( PacketLoss_perc < 0 ) || ( PacketLoss_perc > 100 ) ) { - ret = SKP_SILK_ENC_WRONG_LOSS_RATE; - } - psEnc->sCmn.PacketLoss_perc = PacketLoss_perc; - -#if USE_LBRR - if( INBandFec_enabled < 0 || INBandFec_enabled > 1 ) { - ret = SKP_SILK_ENC_WRONG_INBAND_FEC_SETTING; - } - - /* Only change settings if first frame in packet */ - if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { - - psEnc->sCmn.LBRR_enabled = INBandFec_enabled; - if( psEnc->sCmn.fs_kHz == 8 ) { - LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 9000; - } else if( psEnc->sCmn.fs_kHz == 12 ) { - LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 6000;; - } else if( psEnc->sCmn.fs_kHz == 16 ) { - LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 3000; - } else { - LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS; - } - - if( psEnc->sCmn.TargetRate_bps >= LBRRRate_thres_bps ) { - /* Set gain increase / rate reduction for LBRR usage */ - /* Coarse tuned with pesq for now. */ - /* Linear regression coefs G = 8 - 0.5 * loss */ - /* Meaning that at 16% loss main rate and redundant rate is the same, -> G = 0 */ - psEnc->sCmn.LBRR_GainIncreases = SKP_max_int( 8 - SKP_RSHIFT( psEnc->sCmn.PacketLoss_perc, 1 ), 0 ); - - /* Set main stream rate compensation */ - if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) { - /* Tuned to give aprox same mean / weighted bitrate as no inband FEC */ - psEnc->inBandFEC_SNR_comp_Q8 = ( 6 << 8 ) - SKP_LSHIFT( psEnc->sCmn.LBRR_GainIncreases, 7 ); - } else { - psEnc->inBandFEC_SNR_comp_Q8 = 0; - psEnc->sCmn.LBRR_enabled = 0; - } - } else { - psEnc->inBandFEC_SNR_comp_Q8 = 0; - psEnc->sCmn.LBRR_enabled = 0; - } - } -#else - psEnc->sCmn.LBRR_enabled = 0; -#endif - - /* Set DTX mode */ - if( DTX_enabled < 0 || DTX_enabled > 1 ) { - ret = SKP_SILK_ENC_WRONG_DTX_SETTING; - } - psEnc->sCmn.useDTX = DTX_enabled; - - return ret; -} - -/* Control low bitrate redundancy usage */ -void SKP_Silk_LBRR_ctrl_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control */ -) -{ - SKP_int LBRR_usage; - - if( psEnc->sCmn.LBRR_enabled ) { - /* Control LBRR */ - - /* Usage Control based on sensitivity and packet loss caracteristics */ - /* For now only enable adding to next for active frames. Make more complex later */ - LBRR_usage = SKP_SILK_NO_LBRR; - if( psEnc->speech_activity_Q8 > LBRR_SPEECH_ACTIVITY_THRES_Q8 && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) { // nb! maybe multiply loss prob and speech activity - //if( psEnc->PacketLoss_burst > BURST_THRES ) - // psEncCtrl->LBRR_usage = SKP_SILK_ADD_LBRR_TO_PLUS2; - //} else { - LBRR_usage = SKP_SILK_ADD_LBRR_TO_PLUS1;//SKP_SILK_NO_LBRR - //} - } - psEncCtrl->sCmn.LBRR_usage = LBRR_usage; - } else { - psEncCtrl->sCmn.LBRR_usage = SKP_SILK_NO_LBRR; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Control encoder SNR */ +SKP_int SKP_Silk_control_encoder_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk encoder state */ + const SKP_int API_fs_kHz, /* I External (API) sampling rate (kHz) */ + const SKP_int PacketSize_ms, /* I Packet length (ms) */ + SKP_int32 TargetRate_bps, /* I Target max bitrate (bps) (used if SNR_dB == 0) */ + const SKP_int PacketLoss_perc, /* I Packet loss rate (in percent) */ + const SKP_int INBandFec_enabled, /* I Enable (1) / disable (0) inband FEC */ + const SKP_int DTX_enabled, /* I Enable / disable DTX */ + const SKP_int InputFramesize_ms, /* I Inputframe in ms */ + const SKP_int Complexity /* I Complexity (0->low; 1->medium; 2->high) */ +) +{ + SKP_int32 LBRRRate_thres_bps; + SKP_int k, fs_kHz, ret = 0; + SKP_int32 frac_Q6; + const SKP_int32 *rateTable; + + /* State machine for the SWB/WB switching */ + fs_kHz = psEnc->sCmn.fs_kHz; + + /* Only switch during low speech activity, when no frames are sitting in the payload buffer */ + if( API_fs_kHz == 8 || fs_kHz == 0 || API_fs_kHz < fs_kHz ) { + // Switching is not possible, encoder just initialized, or internal mode higher than external + fs_kHz = API_fs_kHz; + } else { + + /* Resample all valid data in x_buf. Resampling the last part gets rid of a click, 5ms after switching */ + /* this is because the same state is used when downsampling in API.c and is then up to date */ + /* the click immidiatly after switching is most of the time still there */ + + if( psEnc->sCmn.fs_kHz == 24 ) { + /* Accumulate the difference between the target rate and limit */ + if( psEnc->sCmn.fs_kHz_changed == 0 ) { + psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - SWB2WB_BITRATE_BPS_INITIAL ); + } else { + psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - SWB2WB_BITRATE_BPS ); + } + psEnc->sCmn.bitrateDiff = SKP_min( psEnc->sCmn.bitrateDiff, 0 ); + + /* Check if we should switch from 24 to 16 kHz */ +#if SWITCH_TRANSITION_FILTERING + if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* Transition phase not active */ + ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD || psEnc->sCmn.sSWBdetect.WB_detected == 1 ) && + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + psEnc->sCmn.sLP.transition_frame_no = 1; /* Begin transition phase */ + psEnc->sCmn.sLP.mode = 0; /* Switch down */ + } + + if( ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) && ( psEnc->sCmn.sLP.mode == 0 ) && /* Transition phase complete, ready to switch */ +#else + if( ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD || psEnc->sCmn.sSWBdetect.WB_detected == 1 ) && +#endif + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + SKP_int16 x_bufout[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + + psEnc->sCmn.bitrateDiff = 0; + fs_kHz = 16; + + SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + SKP_memset( psEnc->sCmn.resample24To16state, 0, sizeof( psEnc->sCmn.resample24To16state ) ); + +#if LOW_COMPLEXITY_ONLY + { + SKP_int16 scratch[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; + SKP_Silk_resample_2_3_coarse( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, (SKP_int16*)scratch ); + } +#else + SKP_Silk_resample_2_3( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); +#endif + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_bufout, 0, 320 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + +#if SWITCH_TRANSITION_FILTERING + psEnc->sCmn.sLP.transition_frame_no = 0; /* Transition phase complete */ +#endif + } + } else if( psEnc->sCmn.fs_kHz == 16 ) { + + /* Check if we should switch from 16 to 24 kHz */ +#if SWITCH_TRANSITION_FILTERING + if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* No transition phase running, ready to switch */ +#else + if( +#endif + ( API_fs_kHz > psEnc->sCmn.fs_kHz && TargetRate_bps >= WB2SWB_BITRATE_BPS && psEnc->sCmn.sSWBdetect.WB_detected == 0 ) && + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + SKP_int16 x_bufout[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; + SKP_int32 resample16To24state[ 11 ]; + + psEnc->sCmn.bitrateDiff = 0; + fs_kHz = 24; + + SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + SKP_memset( resample16To24state, 0, sizeof(resample16To24state) ); + + SKP_Silk_resample_3_2( &x_bufout[ 0 ], resample16To24state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_bufout, 0, 480 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); +#if SWITCH_TRANSITION_FILTERING + psEnc->sCmn.sLP.mode = 1; /* Switch up */ +#endif + } else { + /* accumulate the difference between the target rate and limit */ + psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - WB2MB_BITRATE_BPS ); + psEnc->sCmn.bitrateDiff = SKP_min( psEnc->sCmn.bitrateDiff, 0 ); + + /* Check if we should switch from 16 to 12 kHz */ +#if SWITCH_TRANSITION_FILTERING + if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* Transition phase not active */ + ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + psEnc->sCmn.sLP.transition_frame_no = 1; /* Begin transition phase */ + psEnc->sCmn.sLP.mode = 0; /* Switch down */ + } + + if( ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) && ( psEnc->sCmn.sLP.mode == 0 ) && /* Transition phase complete, ready to switch */ +#else + if( ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && +#endif + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + + SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + psEnc->sCmn.bitrateDiff = 0; + fs_kHz = 12; + + if( API_fs_kHz == 24 ) { + + /* Intermediate upsampling of x_bufFIX from 16 to 24 kHz */ + SKP_int16 x_buf24[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; + SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 resample16To24state[ 11 ]; + + SKP_memset( resample16To24state, 0, sizeof( resample16To24state ) ); + SKP_Silk_resample_3_2( &x_buf24[ 0 ], resample16To24state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + /* Update the state of the resampler used in API.c, from 24 to 12 kHz */ + SKP_memset( psEnc->sCmn.resample24To12state, 0, sizeof( psEnc->sCmn.resample24To12state ) ); + SKP_Silk_resample_1_2_coarse( &x_buf24[ 0 ], psEnc->sCmn.resample24To12state, &x_buf[ 0 ], scratch, SKP_RSHIFT( SKP_SMULBB( 3, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ), 2 ) ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_buf, 0, 240 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + } else if( API_fs_kHz == 16 ) { + SKP_int16 x_bufout[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 4 ]; + SKP_memset( psEnc->sCmn.resample16To12state, 0, sizeof( psEnc->sCmn.resample16To12state ) ); + + SKP_Silk_resample_3_4( &x_bufout[ 0 ], psEnc->sCmn.resample16To12state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_bufout, 0, 240 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + } +#if SWITCH_TRANSITION_FILTERING + psEnc->sCmn.sLP.transition_frame_no = 0; /* Transition phase complete */ +#endif + } + } + } else if( psEnc->sCmn.fs_kHz == 12 ) { + + /* Check if we should switch from 12 to 16 kHz */ +#if SWITCH_TRANSITION_FILTERING + if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* No transition phase running, ready to switch */ +#else + if( +#endif + ( API_fs_kHz > psEnc->sCmn.fs_kHz && TargetRate_bps >= MB2WB_BITRATE_BPS ) && + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + + SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + psEnc->sCmn.bitrateDiff = 0; + fs_kHz = 16; + + /* Reset state of the resampler to be used */ + if( API_fs_kHz == 24 ) { + + SKP_int16 x_bufout[ 2 * 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 3 ]; + + /* Intermediate upsampling of x_bufFIX from 12 to 24 kHz */ + SKP_int16 x_buf24[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 resample12To24state[6]; + + SKP_memset( resample12To24state, 0, sizeof( resample12To24state ) ); + SKP_Silk_resample_2_1_coarse( &x_buf[ 0 ], resample12To24state, &x_buf24[ 0 ], scratch, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + SKP_memset( psEnc->sCmn.resample24To16state, 0, sizeof( psEnc->sCmn.resample24To16state ) ); + +#if LOW_COMPLEXITY_ONLY + SKP_assert( sizeof( SKP_int16 ) * ( 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) <= sizeof( scratch ) ); + SKP_Silk_resample_2_3_coarse( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf24[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ), (SKP_int16*)scratch ); +#else + SKP_Silk_resample_2_3( &x_bufout[ 0 ], psEnc->sCmn.resample24To16state, &x_buf24[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ) ); +#endif + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_bufout, 0, 320 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + } +#if SWITCH_TRANSITION_FILTERING + psEnc->sCmn.sLP.mode = 1; /* Switch up */ +#endif + } else { + /* accumulate the difference between the target rate and limit */ + psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - MB2NB_BITRATE_BPS ); + psEnc->sCmn.bitrateDiff = SKP_min( psEnc->sCmn.bitrateDiff, 0 ); + + /* Check if we should switch from 12 to 8 kHz */ +#if SWITCH_TRANSITION_FILTERING + if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* Transition phase not active */ + ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + psEnc->sCmn.sLP.transition_frame_no = 1; /* Begin transition phase */ + psEnc->sCmn.sLP.mode = 0; /* Switch down */ + } + + if( ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) && ( psEnc->sCmn.sLP.mode == 0 ) && +#else + if( ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) && +#endif + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + + SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + psEnc->sCmn.bitrateDiff = 0; + fs_kHz = 8; + + if( API_fs_kHz == 24 ) { + + SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + /* Intermediate upsampling of x_buf from 12 to 24 kHz */ + SKP_int16 x_buf24[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 resample12To24state[ 6 ]; + + SKP_memset( resample12To24state, 0, sizeof( resample12To24state ) ); + SKP_Silk_resample_2_1_coarse( &x_buf[ 0 ], resample12To24state, &x_buf24[ 0 ], scratch, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + /* Update the state of the resampler used in API.c, from 24 to 8 kHz */ + SKP_memset( psEnc->sCmn.resample24To8state, 0, sizeof( psEnc->sCmn.resample24To8state ) ); + SKP_Silk_resample_1_3( &x_buf[ 0 ], psEnc->sCmn.resample24To8state, &x_buf24[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ) ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_buf, 0, 160 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + } else if( API_fs_kHz == 16 ) { + /* Intermediate upsampling of x_bufFIX from 12 to 16 kHz */ + SKP_int16 x_buf16[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; + SKP_int32 resample12To16state[11]; + + SKP_memset( resample12To16state, 0, sizeof( resample12To16state ) ); + SKP_Silk_resample_3_2( &x_buf16[ 0 ], resample12To16state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_buf, 0, 160 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + } else if( API_fs_kHz == 12 ) { + SKP_int16 x_bufout[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 3 ]; + SKP_memset( psEnc->sCmn.resample12To8state, 0, sizeof( psEnc->sCmn.resample12To8state ) ); +#if LOW_COMPLEXITY_ONLY + { + SKP_int16 scratch[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; + SKP_Silk_resample_2_3_coarse( &x_bufout[ 0 ], psEnc->sCmn.resample12To8state, &x_buf[ 0 ], + SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, scratch ); + } +#else + SKP_Silk_resample_2_3( &x_bufout[ 0 ], psEnc->sCmn.resample12To8state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); +#endif + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_bufout, 0, 160 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_bufout, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + } +#if SWITCH_TRANSITION_FILTERING + psEnc->sCmn.sLP.transition_frame_no = 0; /* Transition phase complete */ +#endif + } + } + } else if( psEnc->sCmn.fs_kHz == 8 ) { + + /* Check if we should switch from 8 to 12 kHz */ +#if SWITCH_TRANSITION_FILTERING + if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) && /* No transition phase running, ready to switch */ +#else + if( +#endif + ( API_fs_kHz > psEnc->sCmn.fs_kHz && TargetRate_bps >= NB2MB_BITRATE_BPS ) && + ( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; + + SKP_memcpy( x_buf, psEnc->x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + psEnc->sCmn.bitrateDiff = 0; + fs_kHz = 12; + + /* Reset state of the resampler to be used */ + if( API_fs_kHz == 24 ) { + SKP_int16 x_buf24[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 scratch[ 3 * 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) / 2 ]; + SKP_int32 resample8To24state[ 7 ]; + + /* Intermediate upsampling of x_bufFIX from 8 to 24 kHz */ + SKP_memset( resample8To24state, 0, sizeof( resample8To24state ) ); + SKP_Silk_resample_3_1( &x_buf24[ 0 ], resample8To24state, &x_buf[ 0 ], SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + SKP_memset( psEnc->sCmn.resample24To12state, 0, sizeof( psEnc->sCmn.resample24To12state ) ); + + SKP_Silk_resample_1_2_coarse( &x_buf24[ 0 ], psEnc->sCmn.resample24To12state, &x_buf[ 0 ], scratch, SKP_RSHIFT( SKP_SMULBB( 3, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ), 1 ) ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_buf, 0, 240 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + + } else if( API_fs_kHz == 16 ) { + SKP_int16 x_buf16[ 2 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 scratch[ 3 * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ]; + SKP_int32 resample8To16state[ 6 ]; + + /* Intermediate upsampling of x_bufFIX from 8 to 16 kHz */ + SKP_memset( resample8To16state, 0, sizeof( resample8To16state ) ); + SKP_Silk_resample_2_1_coarse( &x_buf[ 0 ], resample8To16state, &x_buf16[ 0 ], scratch, SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape ); + + SKP_memset( psEnc->sCmn.resample16To12state, 0, sizeof( psEnc->sCmn.resample16To12state ) ); + + SKP_Silk_resample_3_4( &x_buf[ 0 ], psEnc->sCmn.resample16To12state, &x_buf16[ 0 ], SKP_LSHIFT( SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape, 1 ) ); + + /* set the first frame to zero, no performance difference was noticed though */ + SKP_memset( x_buf, 0, 240 * sizeof( SKP_int16 ) ); + SKP_memcpy( psEnc->x_buf, x_buf, ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * sizeof( SKP_int16 ) ); + } +#if SWITCH_TRANSITION_FILTERING + psEnc->sCmn.sLP.mode = 1; /* Switch up */ +#endif + } + } else { + // Internal sample frequency not supported! + SKP_assert( 0 ); + } + } + +#if SWITCH_TRANSITION_FILTERING + /* After switching up, stop transition filter during speech inactivity */ + if( ( psEnc->sCmn.sLP.mode == 1 ) && + ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_UP ) && + ( psEnc->speech_activity_Q8 < 128 ) && + ( psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) { + + psEnc->sCmn.sLP.transition_frame_no = 0; + + /* Reset transition filter state */ + SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) ); + } +#endif + + + + /* Set internal sampling frequency */ + if( psEnc->sCmn.fs_kHz != fs_kHz ) { + /* reset part of the state */ + SKP_memset( &psEnc->sShape, 0, sizeof( SKP_Silk_shape_state_FIX ) ); + SKP_memset( &psEnc->sPrefilt, 0, sizeof( SKP_Silk_prefilter_state_FIX ) ); + SKP_memset( &psEnc->sNSQ, 0, sizeof( SKP_Silk_nsq_state ) ); + SKP_memset( &psEnc->sPred, 0, sizeof( SKP_Silk_predict_state_FIX ) ); + SKP_memset( psEnc->sNSQ.xq, 0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) ); + SKP_memset( psEnc->sNSQ_LBRR.xq, 0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) ); + SKP_memset( psEnc->sCmn.LBRR_buffer, 0, MAX_LBRR_DELAY * sizeof( SKP_SILK_LBRR_struct ) ); +#if SWITCH_TRANSITION_FILTERING + SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) ); + if( psEnc->sCmn.sLP.mode == 1 ) { + /* Begin transition phase */ + psEnc->sCmn.sLP.transition_frame_no = 1; + } else { + /* End transition phase */ + psEnc->sCmn.sLP.transition_frame_no = 0; + } +#endif + psEnc->sCmn.inputBufIx = 0; + psEnc->sCmn.nFramesInPayloadBuf = 0; + psEnc->sCmn.nBytesInPayloadBuf = 0; + psEnc->sCmn.oldest_LBRR_idx = 0; + psEnc->sCmn.TargetRate_bps = 0; /* ensures that psEnc->SNR_dB is recomputed */ + + SKP_memset( psEnc->sPred.prev_NLSFq_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) ); + + /* Initialize non-zero parameters */ + psEnc->sCmn.prevLag = 100; + psEnc->sCmn.prev_sigtype = SIG_TYPE_UNVOICED; + psEnc->sCmn.first_frame_after_reset = 1; + psEnc->sPrefilt.lagPrev = 100; + psEnc->sShape.LastGainIndex = 1; + psEnc->sNSQ.lagPrev = 100; + psEnc->sNSQ.prev_inv_gain_Q16 = 65536; + psEnc->sNSQ_LBRR.prev_inv_gain_Q16 = 65536; + psEnc->sCmn.fs_kHz = fs_kHz; + if( psEnc->sCmn.fs_kHz == 8 ) { + psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER; + psEnc->sCmn.psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_10; + psEnc->sCmn.psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_10; + } else { + psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER; + psEnc->sCmn.psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_16; + psEnc->sCmn.psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_16; + } + psEnc->sCmn.frame_length = SKP_SMULBB( FRAME_LENGTH_MS, fs_kHz ); + psEnc->sCmn.subfr_length = SKP_DIV32_16( psEnc->sCmn.frame_length, NB_SUBFR ); + psEnc->sCmn.la_pitch = SKP_SMULBB( LA_PITCH_MS, fs_kHz ); + psEnc->sCmn.la_shape = SKP_SMULBB( LA_SHAPE_MS, fs_kHz ); + psEnc->sPred.min_pitch_lag = SKP_SMULBB( 3, fs_kHz ); + psEnc->sPred.max_pitch_lag = SKP_SMULBB( 18, fs_kHz ); + psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz ); + if( psEnc->sCmn.fs_kHz == 24 ) { + psEnc->mu_LTP_Q8 = MU_LTP_QUANT_SWB_Q8; + } else if( psEnc->sCmn.fs_kHz == 16 ) { + psEnc->mu_LTP_Q8 = MU_LTP_QUANT_WB_Q8; + } else if( psEnc->sCmn.fs_kHz == 12 ) { + psEnc->mu_LTP_Q8 = MU_LTP_QUANT_MB_Q8; + } else { + psEnc->mu_LTP_Q8 = MU_LTP_QUANT_NB_Q8; + } + psEnc->sCmn.fs_kHz_changed = 1; + + /* Check that settings are valid */ + SKP_assert( ( psEnc->sCmn.subfr_length * NB_SUBFR ) == psEnc->sCmn.frame_length ); + } + + /* Set encoding complexity */ + if( Complexity == 0 || LOW_COMPLEXITY_ONLY ) { + /* Low complexity */ + psEnc->sCmn.Complexity = 0; + psEnc->sCmn.pitchEstimationComplexity = PITCH_EST_COMPLEXITY_LC_MODE; + psEnc->pitchEstimationThreshold_Q16 = FIND_PITCH_CORRELATION_THRESHOLD_Q16_LC_MODE; + psEnc->sCmn.pitchEstimationLPCOrder = 8; + psEnc->sCmn.shapingLPCOrder = 12; + psEnc->sCmn.nStatesDelayedDecision = 1; + psEnc->NoiseShapingQuantizer = SKP_Silk_NSQ; + psEnc->sCmn.useInterpolatedNLSFs = 0; + psEnc->sCmn.LTPQuantLowComplexity = 1; + psEnc->sCmn.NLSF_MSVQ_Survivors = MAX_NLSF_MSVQ_SURVIVORS_LC_MODE; + } else if( Complexity == 1 ) { + /* Medium complexity */ + psEnc->sCmn.Complexity = 1; + psEnc->sCmn.pitchEstimationComplexity = PITCH_EST_COMPLEXITY_MC_MODE; + psEnc->pitchEstimationThreshold_Q16 = FIND_PITCH_CORRELATION_THRESHOLD_Q16_MC_MODE; + psEnc->sCmn.pitchEstimationLPCOrder = 12; + psEnc->sCmn.shapingLPCOrder = 16; + psEnc->sCmn.nStatesDelayedDecision = 2; + psEnc->NoiseShapingQuantizer = SKP_Silk_NSQ_del_dec; + psEnc->sCmn.useInterpolatedNLSFs = 0; + psEnc->sCmn.LTPQuantLowComplexity = 0; + psEnc->sCmn.NLSF_MSVQ_Survivors = MAX_NLSF_MSVQ_SURVIVORS_MC_MODE; + } else if( Complexity == 2 ) { + /* High complexity */ + psEnc->sCmn.Complexity = 2; + psEnc->sCmn.pitchEstimationComplexity = PITCH_EST_COMPLEXITY_HC_MODE; + psEnc->pitchEstimationThreshold_Q16 = FIND_PITCH_CORRELATION_THRESHOLD_Q16_HC_MODE; + psEnc->sCmn.pitchEstimationLPCOrder = 16; + psEnc->sCmn.shapingLPCOrder = 16; + psEnc->sCmn.nStatesDelayedDecision = 4; + psEnc->NoiseShapingQuantizer = SKP_Silk_NSQ_del_dec; + psEnc->sCmn.useInterpolatedNLSFs = 1; + psEnc->sCmn.LTPQuantLowComplexity = 0; + psEnc->sCmn.NLSF_MSVQ_Survivors = MAX_NLSF_MSVQ_SURVIVORS; + } else { + ret = SKP_SILK_ENC_WRONG_COMPLEXITY_SETTING; + } + + /* Dont have higher Pitch estimation LPC order than predict LPC order */ + psEnc->sCmn.pitchEstimationLPCOrder = SKP_min_int( psEnc->sCmn.pitchEstimationLPCOrder, psEnc->sCmn.predictLPCOrder ); + + SKP_assert( psEnc->sCmn.pitchEstimationLPCOrder <= FIND_PITCH_LPC_ORDER_MAX ); + SKP_assert( psEnc->sCmn.shapingLPCOrder <= SHAPE_LPC_ORDER_MAX ); + SKP_assert( psEnc->sCmn.nStatesDelayedDecision <= DEL_DEC_STATES_MAX ); + + /* Set bitrate/coding quality */ + TargetRate_bps = SKP_min( TargetRate_bps, 100000 ); + if( psEnc->sCmn.fs_kHz == 8 ) { + TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_NB_BPS ); + } else if( psEnc->sCmn.fs_kHz == 12 ) { + TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_MB_BPS ); + } else if( psEnc->sCmn.fs_kHz == 16 ) { + TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_WB_BPS ); + } else { + TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_SWB_BPS ); + } + if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) { + psEnc->sCmn.TargetRate_bps = TargetRate_bps; + + /* if new TargetRate_bps, translate to SNR_dB value */ + if( psEnc->sCmn.fs_kHz == 8 ) { + rateTable = TargetRate_table_NB; + } else if( psEnc->sCmn.fs_kHz == 12 ) { + rateTable = TargetRate_table_MB; + } else if( psEnc->sCmn.fs_kHz == 16 ) { + rateTable = TargetRate_table_WB; + } else { + rateTable = TargetRate_table_SWB; + } + for( k = 1; k < TARGET_RATE_TAB_SZ; k++ ) { + /* find bitrate interval in table and interpolate */ + if( TargetRate_bps < rateTable[ k ] ) { + frac_Q6 = SKP_DIV32( SKP_LSHIFT( TargetRate_bps - rateTable[ k - 1 ], 6 ), rateTable[ k ] - rateTable[ k - 1 ] ); + psEnc->SNR_dB_Q7 = SKP_LSHIFT( SNR_table_Q1[ k - 1 ], 6 ) + SKP_MUL( frac_Q6, SNR_table_Q1[ k ] - SNR_table_Q1[ k - 1 ] ); + break; + } + } + } + + /* Set packet size */ + if( ( PacketSize_ms != 20 ) && + ( PacketSize_ms != 40 ) && + ( PacketSize_ms != 60 ) && + ( PacketSize_ms != 80 ) && + ( PacketSize_ms != 100 ) ) { + ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED; + } else { + if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) { + psEnc->sCmn.PacketSize_ms = PacketSize_ms; + + /* Packet length changes. Reset LBRR buffer */ + SKP_Silk_LBRR_reset( &psEnc->sCmn ); + } + } + + /* Set packet loss rate measured by farend */ + if( ( PacketLoss_perc < 0 ) || ( PacketLoss_perc > 100 ) ) { + ret = SKP_SILK_ENC_WRONG_LOSS_RATE; + } + psEnc->sCmn.PacketLoss_perc = PacketLoss_perc; + +#if USE_LBRR + if( INBandFec_enabled < 0 || INBandFec_enabled > 1 ) { + ret = SKP_SILK_ENC_WRONG_INBAND_FEC_SETTING; + } + + /* Only change settings if first frame in packet */ + if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { + + psEnc->sCmn.LBRR_enabled = INBandFec_enabled; + if( psEnc->sCmn.fs_kHz == 8 ) { + LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 9000; + } else if( psEnc->sCmn.fs_kHz == 12 ) { + LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 6000;; + } else if( psEnc->sCmn.fs_kHz == 16 ) { + LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 3000; + } else { + LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS; + } + + if( psEnc->sCmn.TargetRate_bps >= LBRRRate_thres_bps ) { + /* Set gain increase / rate reduction for LBRR usage */ + /* Coarse tuned with pesq for now. */ + /* Linear regression coefs G = 8 - 0.5 * loss */ + /* Meaning that at 16% loss main rate and redundant rate is the same, -> G = 0 */ + psEnc->sCmn.LBRR_GainIncreases = SKP_max_int( 8 - SKP_RSHIFT( psEnc->sCmn.PacketLoss_perc, 1 ), 0 ); + + /* Set main stream rate compensation */ + if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) { + /* Tuned to give aprox same mean / weighted bitrate as no inband FEC */ + psEnc->inBandFEC_SNR_comp_Q8 = ( 6 << 8 ) - SKP_LSHIFT( psEnc->sCmn.LBRR_GainIncreases, 7 ); + } else { + psEnc->inBandFEC_SNR_comp_Q8 = 0; + psEnc->sCmn.LBRR_enabled = 0; + } + } else { + psEnc->inBandFEC_SNR_comp_Q8 = 0; + psEnc->sCmn.LBRR_enabled = 0; + } + } +#else + psEnc->sCmn.LBRR_enabled = 0; +#endif + + /* Set DTX mode */ + if( DTX_enabled < 0 || DTX_enabled > 1 ) { + ret = SKP_SILK_ENC_WRONG_DTX_SETTING; + } + psEnc->sCmn.useDTX = DTX_enabled; + + return ret; +} + +/* Control low bitrate redundancy usage */ +void SKP_Silk_LBRR_ctrl_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control */ +) +{ + SKP_int LBRR_usage; + + if( psEnc->sCmn.LBRR_enabled ) { + /* Control LBRR */ + + /* Usage Control based on sensitivity and packet loss caracteristics */ + /* For now only enable adding to next for active frames. Make more complex later */ + LBRR_usage = SKP_SILK_NO_LBRR; + if( psEnc->speech_activity_Q8 > LBRR_SPEECH_ACTIVITY_THRES_Q8 && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) { // nb! maybe multiply loss prob and speech activity + //if( psEnc->PacketLoss_burst > BURST_THRES ) + // psEncCtrl->LBRR_usage = SKP_SILK_ADD_LBRR_TO_PLUS2; + //} else { + LBRR_usage = SKP_SILK_ADD_LBRR_TO_PLUS1;//SKP_SILK_NO_LBRR + //} + } + psEncCtrl->sCmn.LBRR_usage = LBRR_usage; + } else { + psEncCtrl->sCmn.LBRR_usage = SKP_SILK_NO_LBRR; + } +} diff --git a/jni/silk/src/SKP_Silk_corrMatrix_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_corrMatrix_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_corrMatrix_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_corrMatrix_FIX.c index 8d41874..9dfe635 100644 --- a/jni/silk/src/SKP_Silk_corrMatrix_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_corrMatrix_FIX.c @@ -1,152 +1,152 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/********************************************************************** - * Correlation Matrix Computations for LS estimate. - **********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Calculates correlation vector X'*t */ -void SKP_Silk_corrVector_FIX( - const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ - const SKP_int16 *t, /* I target vector [L] */ - const SKP_int L, /* I Length of vectors */ - const SKP_int order, /* I Max lag for correlation */ - SKP_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */ - const SKP_int rshifts /* I Right shifts of correlations */ -) -{ - SKP_int lag, i; - const SKP_int16 *ptr1, *ptr2; - SKP_int32 inner_prod; - - ptr1 = &x[ order - 1 ]; /* Points to first sample of column 0 of X: X[:,0] */ - ptr2 = t; - /* Calculate X'*t */ - if( rshifts > 0 ) { - /* Right shifting used */ - for( lag = 0; lag < order; lag++ ) { - inner_prod = 0; - for( i = 0; i < L; i++ ) { - inner_prod += SKP_RSHIFT32( SKP_SMULBB( ptr1[ i ], ptr2[i] ), rshifts ); - } - Xt[ lag ] = inner_prod; /* X[:,lag]'*t */ - ptr1--; /* Go to next column of X */ - } - } else { - SKP_assert( rshifts == 0 ); - for( lag = 0; lag < order; lag++ ) { - Xt[ lag ] = SKP_Silk_inner_prod_aligned( ptr1, ptr2, L ); /* X[:,lag]'*t */ - ptr1--; /* Go to next column of X */ - } - } -} - -/* Calculates correlation matrix X'*X */ -void SKP_Silk_corrMatrix_FIX( - const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ - const SKP_int L, /* I Length of vectors */ - const SKP_int order, /* I Max lag for correlation */ - SKP_int32 *XX, /* O Pointer to X'*X correlation matrix [ order x order ]*/ - SKP_int *rshifts /* I/O Right shifts of correlations */ -) -{ - SKP_int i, j, lag, rshifts_local, head_room_rshifts; - SKP_int32 energy; - const SKP_int16 *ptr1, *ptr2; - - /* Calculate energy to find shift used to fit in 32 bits */ - SKP_Silk_sum_sqr_shift( &energy, &rshifts_local, x, L + order - 1 ); - /* Add shifts to get the wanted head room */ - - head_room_rshifts = SKP_max( LTP_CORRS_HEAD_ROOM - SKP_Silk_CLZ32( energy ), 0 ); - - energy = SKP_RSHIFT32( energy, head_room_rshifts ); - rshifts_local += head_room_rshifts; - - /* Calculate energy of first column (0) of X: X[:,0]'*X[:,0] */ - /* Remove contribution of first order - 1 samples */ - for( i = 0; i < order - 1; i++ ) { - energy -= SKP_RSHIFT32( SKP_SMULBB( x[ i ], x[ i ] ), rshifts_local ); - } - if( rshifts_local < *rshifts ) { - /* Adjust energy */ - energy = SKP_RSHIFT32( energy, *rshifts - rshifts_local ); - rshifts_local = *rshifts; - } - - /* Calculate energy of remaining columns of X: X[:,j]'*X[:,j] */ - /* Fill out the diagonal of the correlation matrix */ - matrix_ptr( XX, 0, 0, order ) = energy; - ptr1 = &x[ order - 1 ]; /* First sample of column 0 of X */ - for( j = 1; j < order; j++ ) { - energy = SKP_SUB32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ L - j ], ptr1[ L - j ] ), rshifts_local ) ); - energy = SKP_ADD32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ -j ], ptr1[ -j ] ), rshifts_local ) ); - matrix_ptr( XX, j, j, order ) = energy; - } - - ptr2 = &x[ order - 2 ]; /* First sample of column 1 of X */ - /* Calculate the remaining elements of the correlation matrix */ - if( rshifts_local > 0 ) { - /* Right shifting used */ - for( lag = 1; lag < order; lag++ ) { - /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */ - energy = 0; - for( i = 0; i < L; i++ ) { - energy += SKP_RSHIFT32( SKP_SMULBB( ptr1[ i ], ptr2[i] ), rshifts_local ); - } - /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */ - matrix_ptr( XX, lag, 0, order ) = energy; - matrix_ptr( XX, 0, lag, order ) = energy; - for( j = 1; j < ( order - lag ); j++ ) { - energy = SKP_SUB32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ), rshifts_local ) ); - energy = SKP_ADD32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ -j ], ptr2[ -j ] ), rshifts_local ) ); - matrix_ptr( XX, lag + j, j, order ) = energy; - matrix_ptr( XX, j, lag + j, order ) = energy; - } - ptr2--; /* Update pointer to first sample of next column (lag) in X */ - } - } else { - for( lag = 1; lag < order; lag++ ) { - /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */ - energy = SKP_Silk_inner_prod_aligned( ptr1, ptr2, L ); - matrix_ptr( XX, lag, 0, order ) = energy; - matrix_ptr( XX, 0, lag, order ) = energy; - /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */ - for( j = 1; j < ( order - lag ); j++ ) { - energy = SKP_SUB32( energy, SKP_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ) ); - energy = SKP_SMLABB( energy, ptr1[ -j ], ptr2[ -j ] ); - matrix_ptr( XX, lag + j, j, order ) = energy; - matrix_ptr( XX, j, lag + j, order ) = energy; - } - ptr2--;/* Update pointer to first sample of next column (lag) in X */ - } - } - *rshifts = rshifts_local; -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/********************************************************************** + * Correlation Matrix Computations for LS estimate. + **********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Calculates correlation vector X'*t */ +void SKP_Silk_corrVector_FIX( + const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ + const SKP_int16 *t, /* I target vector [L] */ + const SKP_int L, /* I Length of vectors */ + const SKP_int order, /* I Max lag for correlation */ + SKP_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */ + const SKP_int rshifts /* I Right shifts of correlations */ +) +{ + SKP_int lag, i; + const SKP_int16 *ptr1, *ptr2; + SKP_int32 inner_prod; + + ptr1 = &x[ order - 1 ]; /* Points to first sample of column 0 of X: X[:,0] */ + ptr2 = t; + /* Calculate X'*t */ + if( rshifts > 0 ) { + /* Right shifting used */ + for( lag = 0; lag < order; lag++ ) { + inner_prod = 0; + for( i = 0; i < L; i++ ) { + inner_prod += SKP_RSHIFT32( SKP_SMULBB( ptr1[ i ], ptr2[i] ), rshifts ); + } + Xt[ lag ] = inner_prod; /* X[:,lag]'*t */ + ptr1--; /* Go to next column of X */ + } + } else { + SKP_assert( rshifts == 0 ); + for( lag = 0; lag < order; lag++ ) { + Xt[ lag ] = SKP_Silk_inner_prod_aligned( ptr1, ptr2, L ); /* X[:,lag]'*t */ + ptr1--; /* Go to next column of X */ + } + } +} + +/* Calculates correlation matrix X'*X */ +void SKP_Silk_corrMatrix_FIX( + const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ + const SKP_int L, /* I Length of vectors */ + const SKP_int order, /* I Max lag for correlation */ + SKP_int32 *XX, /* O Pointer to X'*X correlation matrix [ order x order ]*/ + SKP_int *rshifts /* I/O Right shifts of correlations */ +) +{ + SKP_int i, j, lag, rshifts_local, head_room_rshifts; + SKP_int32 energy; + const SKP_int16 *ptr1, *ptr2; + + /* Calculate energy to find shift used to fit in 32 bits */ + SKP_Silk_sum_sqr_shift( &energy, &rshifts_local, x, L + order - 1 ); + /* Add shifts to get the wanted head room */ + + head_room_rshifts = SKP_max( LTP_CORRS_HEAD_ROOM - SKP_Silk_CLZ32( energy ), 0 ); + + energy = SKP_RSHIFT32( energy, head_room_rshifts ); + rshifts_local += head_room_rshifts; + + /* Calculate energy of first column (0) of X: X[:,0]'*X[:,0] */ + /* Remove contribution of first order - 1 samples */ + for( i = 0; i < order - 1; i++ ) { + energy -= SKP_RSHIFT32( SKP_SMULBB( x[ i ], x[ i ] ), rshifts_local ); + } + if( rshifts_local < *rshifts ) { + /* Adjust energy */ + energy = SKP_RSHIFT32( energy, *rshifts - rshifts_local ); + rshifts_local = *rshifts; + } + + /* Calculate energy of remaining columns of X: X[:,j]'*X[:,j] */ + /* Fill out the diagonal of the correlation matrix */ + matrix_ptr( XX, 0, 0, order ) = energy; + ptr1 = &x[ order - 1 ]; /* First sample of column 0 of X */ + for( j = 1; j < order; j++ ) { + energy = SKP_SUB32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ L - j ], ptr1[ L - j ] ), rshifts_local ) ); + energy = SKP_ADD32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ -j ], ptr1[ -j ] ), rshifts_local ) ); + matrix_ptr( XX, j, j, order ) = energy; + } + + ptr2 = &x[ order - 2 ]; /* First sample of column 1 of X */ + /* Calculate the remaining elements of the correlation matrix */ + if( rshifts_local > 0 ) { + /* Right shifting used */ + for( lag = 1; lag < order; lag++ ) { + /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */ + energy = 0; + for( i = 0; i < L; i++ ) { + energy += SKP_RSHIFT32( SKP_SMULBB( ptr1[ i ], ptr2[i] ), rshifts_local ); + } + /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */ + matrix_ptr( XX, lag, 0, order ) = energy; + matrix_ptr( XX, 0, lag, order ) = energy; + for( j = 1; j < ( order - lag ); j++ ) { + energy = SKP_SUB32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ), rshifts_local ) ); + energy = SKP_ADD32( energy, SKP_RSHIFT32( SKP_SMULBB( ptr1[ -j ], ptr2[ -j ] ), rshifts_local ) ); + matrix_ptr( XX, lag + j, j, order ) = energy; + matrix_ptr( XX, j, lag + j, order ) = energy; + } + ptr2--; /* Update pointer to first sample of next column (lag) in X */ + } + } else { + for( lag = 1; lag < order; lag++ ) { + /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */ + energy = SKP_Silk_inner_prod_aligned( ptr1, ptr2, L ); + matrix_ptr( XX, lag, 0, order ) = energy; + matrix_ptr( XX, 0, lag, order ) = energy; + /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */ + for( j = 1; j < ( order - lag ); j++ ) { + energy = SKP_SUB32( energy, SKP_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ) ); + energy = SKP_SMLABB( energy, ptr1[ -j ], ptr2[ -j ] ); + matrix_ptr( XX, lag + j, j, order ) = energy; + matrix_ptr( XX, j, lag + j, order ) = energy; + } + ptr2--;/* Update pointer to first sample of next column (lag) in X */ + } + } + *rshifts = rshifts_local; +} + diff --git a/jni/silk/src/SKP_Silk_create_init_destroy.c b/app/src/main/jni/silk/src/SKP_Silk_create_init_destroy.c similarity index 97% rename from jni/silk/src/SKP_Silk_create_init_destroy.c rename to app/src/main/jni/silk/src/SKP_Silk_create_init_destroy.c index 9669cf8..028aecf 100644 --- a/jni/silk/src/SKP_Silk_create_init_destroy.c +++ b/app/src/main/jni/silk/src/SKP_Silk_create_init_destroy.c @@ -1,55 +1,55 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - - -/************************/ -/* Init Decoder State */ -/************************/ -SKP_int SKP_Silk_init_decoder( - SKP_Silk_decoder_state *psDec /* I/O Decoder state pointer */ -) -{ - SKP_memset( psDec, 0, sizeof( SKP_Silk_decoder_state ) ); - /* Set sampling rate to 24 kHz, and init non-zero values */ - SKP_Silk_decoder_set_fs( psDec, 24 ); - - /* Used to deactivate e.g. LSF interpolation and fluctuation reduction */ - psDec->first_frame_after_reset = 1; - psDec->prev_inv_gain_Q16 = 65536; - - /* Reset CNG state */ - SKP_Silk_CNG_Reset( psDec ); - - SKP_Silk_PLC_Reset( psDec ); - - psDec->bitstream_v = USE_BIT_STREAM_V; - - return(0); -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + + +/************************/ +/* Init Decoder State */ +/************************/ +SKP_int SKP_Silk_init_decoder( + SKP_Silk_decoder_state *psDec /* I/O Decoder state pointer */ +) +{ + SKP_memset( psDec, 0, sizeof( SKP_Silk_decoder_state ) ); + /* Set sampling rate to 24 kHz, and init non-zero values */ + SKP_Silk_decoder_set_fs( psDec, 24 ); + + /* Used to deactivate e.g. LSF interpolation and fluctuation reduction */ + psDec->first_frame_after_reset = 1; + psDec->prev_inv_gain_Q16 = 65536; + + /* Reset CNG state */ + SKP_Silk_CNG_Reset( psDec ); + + SKP_Silk_PLC_Reset( psDec ); + + psDec->bitstream_v = USE_BIT_STREAM_V; + + return(0); +} + diff --git a/jni/silk/src/SKP_Silk_dec_API.c b/app/src/main/jni/silk/src/SKP_Silk_dec_API.c similarity index 97% rename from jni/silk/src/SKP_Silk_dec_API.c rename to app/src/main/jni/silk/src/SKP_Silk_dec_API.c index 70f7d31..c39d64d 100644 --- a/jni/silk/src/SKP_Silk_dec_API.c +++ b/app/src/main/jni/silk/src/SKP_Silk_dec_API.c @@ -1,343 +1,343 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SDK_API.h" -#include "SKP_Silk_main_FIX.h" - -/*********************/ -/* Decoder functions */ -/*********************/ - -SKP_int SKP_Silk_SDK_Get_Decoder_Size( SKP_int32 *decSizeBytes ) -{ - SKP_int ret = 0; - - *decSizeBytes = sizeof( SKP_Silk_decoder_state ); - - return ret; -} - -/* Reset decoder state */ -SKP_int SKP_Silk_SDK_InitDecoder( - void* decState /* I/O: State */ -) -{ - SKP_int ret = 0; - SKP_Silk_decoder_state *struc; - - struc = (SKP_Silk_decoder_state *)decState; - - ret = SKP_Silk_init_decoder( struc ); - - return ret; -} - -/* Decode a frame */ -SKP_int SKP_Silk_SDK_Decode( - void* decState, /* I/O: State */ - SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control structure */ - SKP_int lostFlag, /* I: 0: no loss, 1 loss */ - const SKP_uint8 *inData, /* I: Encoded input vector */ - const SKP_int nBytesIn, /* I: Number of input Bytes */ - SKP_int16 *samplesOut, /* O: Decoded output speech vector */ - SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */ -) -{ - SKP_int ret = 0, used_bytes, prev_fs_kHz; - SKP_Silk_decoder_state *psDec; - - psDec = (SKP_Silk_decoder_state *)decState; - - /**********************************/ - /* Test if first frame in payload */ - /**********************************/ - if( psDec->moreInternalDecoderFrames == 0 ) { - /* First Frame in Payload */ - psDec->nFramesDecoded = 0; /* Used to count frames in packet */ - } - - if( psDec->moreInternalDecoderFrames == 0 && /* First frame in packet */ - lostFlag == 0 && /* Not packet loss */ - nBytesIn > MAX_ARITHM_BYTES ) { /* Too long payload */ - /* Avoid trying to decode a too large packet */ - lostFlag = 1; - ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE; - } - - /* Save previous sample frequency */ - prev_fs_kHz = psDec->fs_kHz; - - /* Call decoder for one frame */ - ret += SKP_Silk_decode_frame( psDec, samplesOut, nSamplesOut, inData, nBytesIn, - lostFlag, &used_bytes ); - - if( used_bytes ) { /* Only Call if not a packet loss */ - if( psDec->nBytesLeft > 0 && psDec->FrameTermination == SKP_SILK_MORE_FRAMES && psDec->nFramesDecoded < 5 ) { - /* We have more frames in the Payload */ - psDec->moreInternalDecoderFrames = 1; - } else { - /* Last frame in Payload */ - psDec->moreInternalDecoderFrames = 0; - psDec->nFramesInPacket = psDec->nFramesDecoded; - - /* Track inband FEC usage */ - if( psDec->vadFlag == VOICE_ACTIVITY ) { - if( psDec->FrameTermination == SKP_SILK_LAST_FRAME ) { - psDec->no_FEC_counter++; - if( psDec->no_FEC_counter > NO_LBRR_THRES ) { - psDec->inband_FEC_offset = 0; - } - } else if( psDec->FrameTermination == SKP_SILK_LBRR_VER1 ) { - psDec->inband_FEC_offset = 1; /* FEC info with 1 packet delay */ - psDec->no_FEC_counter = 0; - } else if( psDec->FrameTermination == SKP_SILK_LBRR_VER2 ) { - psDec->inband_FEC_offset = 2; /* FEC info with 2 packets delay */ - psDec->no_FEC_counter = 0; - } - } - } - } - - if( psDec->fs_kHz * 1000 > decControl->sampleRate ) { - ret = SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY; - } - - /* Do any resampling if needed */ - if( psDec->fs_kHz * 1000 != decControl->sampleRate ) { - SKP_int16 samplesOut_tmp[ 2 * MAX_FRAME_LENGTH ]; - SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ]; - - /* Copy to a tmpbuffer as the resampling writes to samplesOut */ - memcpy( samplesOut_tmp, samplesOut, *nSamplesOut * sizeof( SKP_int16 ) ); - - /* Clear resampler state when switching internal sampling frequency */ - if( prev_fs_kHz != psDec->fs_kHz ) { - SKP_memset( psDec->resampleState, 0, sizeof( psDec->resampleState ) ); - } - - if( psDec->fs_kHz == 16 && decControl->sampleRate == 24000 ) { - /* Resample from 16 kHz to 24 kHz */ - SKP_Silk_resample_3_2( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); - } else if( psDec->fs_kHz == 12 && decControl->sampleRate == 24000 ) { - /* Resample from 12 kHz to 24 kHz */ - SKP_Silk_resample_2_1_coarse( samplesOut_tmp, psDec->resampleState, samplesOut, scratch, *nSamplesOut ); - } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 24000 ) { - /* Resample from 8 kHz to 24 kHz */ - SKP_Silk_resample_3_1( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); - } else if( psDec->fs_kHz == 12 && decControl->sampleRate == 16000 ) { - /* Resample from 12 kHz to 16 kHz */ - SKP_Silk_resample_4_3( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); - } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 16000 ) { - /* Resample from 8 kHz to 16 kHz */ - SKP_Silk_resample_2_1_coarse( samplesOut_tmp, psDec->resampleState, samplesOut, scratch, *nSamplesOut ); - } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 12000 ) { - /* Resample from 8 kHz to 12 kHz */ - SKP_Silk_resample_3_2( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); - } - - *nSamplesOut = SKP_DIV32( ( SKP_int32 )*nSamplesOut * decControl->sampleRate, psDec->fs_kHz * 1000 ); - } - - /* Copy all parameters that are needed out of internal structure to the control stucture */ - decControl->frameSize = ( SKP_int )psDec->frame_length; - decControl->framesPerPacket = ( SKP_int )psDec->nFramesInPacket; - decControl->inBandFECOffset = ( SKP_int )psDec->inband_FEC_offset; - decControl->moreInternalDecoderFrames = ( SKP_int )psDec->moreInternalDecoderFrames; - - return ret; -} - -/* Function to find LBRR information in a packet */ -void SKP_Silk_SDK_search_for_LBRR( - void *decState, /* I: Decoder state, to select bitstream version only */ - const SKP_uint8 *inData, /* I: Encoded input vector */ - const SKP_int16 nBytesIn, /* I: Number of input Bytes */ - SKP_int lost_offset, /* I: Offset from lost packet */ - SKP_uint8 *LBRRData, /* O: LBRR payload */ - SKP_int16 *nLBRRBytes /* O: Number of LBRR Bytes */ -) -{ - SKP_Silk_decoder_state *psDec; - SKP_Silk_decoder_state sDec; // Local decoder state to avoid interfering with running decoder */ - SKP_Silk_decoder_control sDecCtrl; - SKP_int i, TempQ[ MAX_FRAME_LENGTH ]; - - psDec = ( SKP_Silk_decoder_state * )decState; - - if( lost_offset < 1 || lost_offset > MAX_LBRR_DELAY ) { - /* No useful FEC in this packet */ - *nLBRRBytes = 0; - return; - } - - sDec.nFramesDecoded = 0; - sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */ - SKP_memset( sDec.prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) ); - SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn ); - - if( psDec->bitstream_v == BIT_STREAM_V4 ) { /* Silk_v4 payload */ - /* Decode all parameter indices for the whole packet*/ - SKP_Silk_decode_indices_v4( &sDec ); - - /* Is there usable LBRR in this packet */ - *nLBRRBytes = 0; - if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) { - /* The wanted FEC is present in the packet */ - for( i = 0; i < sDec.nFramesInPacket; i++ ) { - SKP_Silk_decode_parameters_v4( &sDec, &sDecCtrl, TempQ, 0 ); - - if( sDec.nBytesLeft <= 0 || sDec.sRC.error ) { - /* Corrupt stream */ - LBRRData = NULL; - *nLBRRBytes = 0; - break; - } else { - sDec.nFramesDecoded++; - } - } - - if( LBRRData != NULL ) { - /* The wanted FEC is present in the packet */ - *nLBRRBytes = sDec.nBytesLeft; - SKP_memcpy( LBRRData, &inData[ nBytesIn - sDec.nBytesLeft ], sDec.nBytesLeft * sizeof( SKP_uint8 ) ); - } - } - } else { /* Silk_v3 payload */ - while(1) { - SKP_Silk_decode_parameters( &sDec, &sDecCtrl, TempQ, 0 ); - - if( sDec.sRC.error ) { - /* Corrupt stream */ - *nLBRRBytes = 0; - return; - }; - - if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) { - /* The wanted FEC is present in the packet */ - *nLBRRBytes = sDec.nBytesLeft; - SKP_memcpy( LBRRData, &inData[ nBytesIn - sDec.nBytesLeft ], sDec.nBytesLeft * sizeof( SKP_uint8 ) ); - break; - } - if( sDec.nBytesLeft > 0 && sDec.FrameTermination == SKP_SILK_MORE_FRAMES ) { - sDec.nFramesDecoded++; - } else { - LBRRData = NULL; - *nLBRRBytes = 0; - break; - } - } - } -} - -/* Getting type of content for a packet */ -void SKP_Silk_SDK_get_TOC( - void *decState, /* I/O: Decoder state, to select bitstream version only */ - const SKP_uint8 *inData, /* I: Encoded input vector */ - const SKP_int16 nBytesIn, /* I: Number of input bytes */ - SKP_Silk_TOC_struct *Silk_TOC /* O: Type of content */ -) -{ - SKP_Silk_decoder_state *psDec; - SKP_Silk_decoder_state sDec; // Local Decoder state to avoid interfering with running decoder */ - SKP_Silk_decoder_control sDecCtrl; - SKP_int i, TempQ[ MAX_FRAME_LENGTH ]; - - psDec = (SKP_Silk_decoder_state *)decState; - - sDec.nFramesDecoded = 0; - sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */ - SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn ); - - if( psDec->bitstream_v == BIT_STREAM_V4 ) { /* Silk_v4 payload */ - /* Decode all parameter indices for the whole packet*/ - SKP_Silk_decode_indices_v4( &sDec ); - - if( sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET || sDec.sRC.error ) { - /* Corrupt packet */ - SKP_memset( Silk_TOC, 0, sizeof( SKP_Silk_TOC_struct ) ); - Silk_TOC->corrupt = 1; - } else { - Silk_TOC->corrupt = 0; - Silk_TOC->framesInPacket = sDec.nFramesInPacket; - Silk_TOC->fs_kHz = sDec.fs_kHz; - if( sDec.FrameTermination == SKP_SILK_LAST_FRAME ) { - Silk_TOC->inbandLBRR = sDec.FrameTermination; - } else { - Silk_TOC->inbandLBRR = sDec.FrameTermination - 1; - } - /* Copy data */ - for( i = 0; i < sDec.nFramesInPacket; i++ ) { - Silk_TOC->vadFlags[ i ] = sDec.vadFlagBuf[ i ]; - Silk_TOC->sigtypeFlags[ i ] = sDec.sigtype[ i ]; - } - } - } else { /* Silk_v3 payload */ - Silk_TOC->corrupt = 0; - while( 1 ) { - SKP_Silk_decode_parameters( &sDec, &sDecCtrl, TempQ, 0 ); - - Silk_TOC->vadFlags[ sDec.nFramesDecoded ] = sDec.vadFlag; - Silk_TOC->sigtypeFlags[ sDec.nFramesDecoded ] = sDecCtrl.sigtype; - - if( sDec.sRC.error ) { - /* Corrupt stream */ - Silk_TOC->corrupt = 1; - break; - }; - - if( sDec.nBytesLeft > 0 && sDec.FrameTermination == SKP_SILK_MORE_FRAMES ) { - sDec.nFramesDecoded++; - } else { - break; - } - } - if( Silk_TOC->corrupt || sDec.FrameTermination == SKP_SILK_MORE_FRAMES || - sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET ) { - /* Corrupt packet */ - SKP_memset( Silk_TOC, 0, sizeof( SKP_Silk_TOC_struct ) ); - Silk_TOC->corrupt = 1; - } else { - Silk_TOC->framesInPacket = sDec.nFramesDecoded; - Silk_TOC->fs_kHz = sDec.fs_kHz; - if( sDec.FrameTermination == SKP_SILK_LAST_FRAME ) { - Silk_TOC->inbandLBRR = sDec.FrameTermination; - } else { - Silk_TOC->inbandLBRR = sDec.FrameTermination - 1; - } - } - } -} - -/**************************/ -/* Get the version number */ -/**************************/ -/* Return a pointer to string specifying the version */ -const char *SKP_Silk_SDK_get_version() -{ - static const char version[] = "1.0.2"; - return version; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SDK_API.h" +#include "SKP_Silk_main_FIX.h" + +/*********************/ +/* Decoder functions */ +/*********************/ + +SKP_int SKP_Silk_SDK_Get_Decoder_Size( SKP_int32 *decSizeBytes ) +{ + SKP_int ret = 0; + + *decSizeBytes = sizeof( SKP_Silk_decoder_state ); + + return ret; +} + +/* Reset decoder state */ +SKP_int SKP_Silk_SDK_InitDecoder( + void* decState /* I/O: State */ +) +{ + SKP_int ret = 0; + SKP_Silk_decoder_state *struc; + + struc = (SKP_Silk_decoder_state *)decState; + + ret = SKP_Silk_init_decoder( struc ); + + return ret; +} + +/* Decode a frame */ +SKP_int SKP_Silk_SDK_Decode( + void* decState, /* I/O: State */ + SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control structure */ + SKP_int lostFlag, /* I: 0: no loss, 1 loss */ + const SKP_uint8 *inData, /* I: Encoded input vector */ + const SKP_int nBytesIn, /* I: Number of input Bytes */ + SKP_int16 *samplesOut, /* O: Decoded output speech vector */ + SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */ +) +{ + SKP_int ret = 0, used_bytes, prev_fs_kHz; + SKP_Silk_decoder_state *psDec; + + psDec = (SKP_Silk_decoder_state *)decState; + + /**********************************/ + /* Test if first frame in payload */ + /**********************************/ + if( psDec->moreInternalDecoderFrames == 0 ) { + /* First Frame in Payload */ + psDec->nFramesDecoded = 0; /* Used to count frames in packet */ + } + + if( psDec->moreInternalDecoderFrames == 0 && /* First frame in packet */ + lostFlag == 0 && /* Not packet loss */ + nBytesIn > MAX_ARITHM_BYTES ) { /* Too long payload */ + /* Avoid trying to decode a too large packet */ + lostFlag = 1; + ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE; + } + + /* Save previous sample frequency */ + prev_fs_kHz = psDec->fs_kHz; + + /* Call decoder for one frame */ + ret += SKP_Silk_decode_frame( psDec, samplesOut, nSamplesOut, inData, nBytesIn, + lostFlag, &used_bytes ); + + if( used_bytes ) { /* Only Call if not a packet loss */ + if( psDec->nBytesLeft > 0 && psDec->FrameTermination == SKP_SILK_MORE_FRAMES && psDec->nFramesDecoded < 5 ) { + /* We have more frames in the Payload */ + psDec->moreInternalDecoderFrames = 1; + } else { + /* Last frame in Payload */ + psDec->moreInternalDecoderFrames = 0; + psDec->nFramesInPacket = psDec->nFramesDecoded; + + /* Track inband FEC usage */ + if( psDec->vadFlag == VOICE_ACTIVITY ) { + if( psDec->FrameTermination == SKP_SILK_LAST_FRAME ) { + psDec->no_FEC_counter++; + if( psDec->no_FEC_counter > NO_LBRR_THRES ) { + psDec->inband_FEC_offset = 0; + } + } else if( psDec->FrameTermination == SKP_SILK_LBRR_VER1 ) { + psDec->inband_FEC_offset = 1; /* FEC info with 1 packet delay */ + psDec->no_FEC_counter = 0; + } else if( psDec->FrameTermination == SKP_SILK_LBRR_VER2 ) { + psDec->inband_FEC_offset = 2; /* FEC info with 2 packets delay */ + psDec->no_FEC_counter = 0; + } + } + } + } + + if( psDec->fs_kHz * 1000 > decControl->sampleRate ) { + ret = SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY; + } + + /* Do any resampling if needed */ + if( psDec->fs_kHz * 1000 != decControl->sampleRate ) { + SKP_int16 samplesOut_tmp[ 2 * MAX_FRAME_LENGTH ]; + SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ]; + + /* Copy to a tmpbuffer as the resampling writes to samplesOut */ + memcpy( samplesOut_tmp, samplesOut, *nSamplesOut * sizeof( SKP_int16 ) ); + + /* Clear resampler state when switching internal sampling frequency */ + if( prev_fs_kHz != psDec->fs_kHz ) { + SKP_memset( psDec->resampleState, 0, sizeof( psDec->resampleState ) ); + } + + if( psDec->fs_kHz == 16 && decControl->sampleRate == 24000 ) { + /* Resample from 16 kHz to 24 kHz */ + SKP_Silk_resample_3_2( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); + } else if( psDec->fs_kHz == 12 && decControl->sampleRate == 24000 ) { + /* Resample from 12 kHz to 24 kHz */ + SKP_Silk_resample_2_1_coarse( samplesOut_tmp, psDec->resampleState, samplesOut, scratch, *nSamplesOut ); + } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 24000 ) { + /* Resample from 8 kHz to 24 kHz */ + SKP_Silk_resample_3_1( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); + } else if( psDec->fs_kHz == 12 && decControl->sampleRate == 16000 ) { + /* Resample from 12 kHz to 16 kHz */ + SKP_Silk_resample_4_3( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); + } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 16000 ) { + /* Resample from 8 kHz to 16 kHz */ + SKP_Silk_resample_2_1_coarse( samplesOut_tmp, psDec->resampleState, samplesOut, scratch, *nSamplesOut ); + } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 12000 ) { + /* Resample from 8 kHz to 12 kHz */ + SKP_Silk_resample_3_2( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut ); + } + + *nSamplesOut = SKP_DIV32( ( SKP_int32 )*nSamplesOut * decControl->sampleRate, psDec->fs_kHz * 1000 ); + } + + /* Copy all parameters that are needed out of internal structure to the control stucture */ + decControl->frameSize = ( SKP_int )psDec->frame_length; + decControl->framesPerPacket = ( SKP_int )psDec->nFramesInPacket; + decControl->inBandFECOffset = ( SKP_int )psDec->inband_FEC_offset; + decControl->moreInternalDecoderFrames = ( SKP_int )psDec->moreInternalDecoderFrames; + + return ret; +} + +/* Function to find LBRR information in a packet */ +void SKP_Silk_SDK_search_for_LBRR( + void *decState, /* I: Decoder state, to select bitstream version only */ + const SKP_uint8 *inData, /* I: Encoded input vector */ + const SKP_int16 nBytesIn, /* I: Number of input Bytes */ + SKP_int lost_offset, /* I: Offset from lost packet */ + SKP_uint8 *LBRRData, /* O: LBRR payload */ + SKP_int16 *nLBRRBytes /* O: Number of LBRR Bytes */ +) +{ + SKP_Silk_decoder_state *psDec; + SKP_Silk_decoder_state sDec; // Local decoder state to avoid interfering with running decoder */ + SKP_Silk_decoder_control sDecCtrl; + SKP_int i, TempQ[ MAX_FRAME_LENGTH ]; + + psDec = ( SKP_Silk_decoder_state * )decState; + + if( lost_offset < 1 || lost_offset > MAX_LBRR_DELAY ) { + /* No useful FEC in this packet */ + *nLBRRBytes = 0; + return; + } + + sDec.nFramesDecoded = 0; + sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */ + SKP_memset( sDec.prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) ); + SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn ); + + if( psDec->bitstream_v == BIT_STREAM_V4 ) { /* Silk_v4 payload */ + /* Decode all parameter indices for the whole packet*/ + SKP_Silk_decode_indices_v4( &sDec ); + + /* Is there usable LBRR in this packet */ + *nLBRRBytes = 0; + if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) { + /* The wanted FEC is present in the packet */ + for( i = 0; i < sDec.nFramesInPacket; i++ ) { + SKP_Silk_decode_parameters_v4( &sDec, &sDecCtrl, TempQ, 0 ); + + if( sDec.nBytesLeft <= 0 || sDec.sRC.error ) { + /* Corrupt stream */ + LBRRData = NULL; + *nLBRRBytes = 0; + break; + } else { + sDec.nFramesDecoded++; + } + } + + if( LBRRData != NULL ) { + /* The wanted FEC is present in the packet */ + *nLBRRBytes = sDec.nBytesLeft; + SKP_memcpy( LBRRData, &inData[ nBytesIn - sDec.nBytesLeft ], sDec.nBytesLeft * sizeof( SKP_uint8 ) ); + } + } + } else { /* Silk_v3 payload */ + while(1) { + SKP_Silk_decode_parameters( &sDec, &sDecCtrl, TempQ, 0 ); + + if( sDec.sRC.error ) { + /* Corrupt stream */ + *nLBRRBytes = 0; + return; + }; + + if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) { + /* The wanted FEC is present in the packet */ + *nLBRRBytes = sDec.nBytesLeft; + SKP_memcpy( LBRRData, &inData[ nBytesIn - sDec.nBytesLeft ], sDec.nBytesLeft * sizeof( SKP_uint8 ) ); + break; + } + if( sDec.nBytesLeft > 0 && sDec.FrameTermination == SKP_SILK_MORE_FRAMES ) { + sDec.nFramesDecoded++; + } else { + LBRRData = NULL; + *nLBRRBytes = 0; + break; + } + } + } +} + +/* Getting type of content for a packet */ +void SKP_Silk_SDK_get_TOC( + void *decState, /* I/O: Decoder state, to select bitstream version only */ + const SKP_uint8 *inData, /* I: Encoded input vector */ + const SKP_int16 nBytesIn, /* I: Number of input bytes */ + SKP_Silk_TOC_struct *Silk_TOC /* O: Type of content */ +) +{ + SKP_Silk_decoder_state *psDec; + SKP_Silk_decoder_state sDec; // Local Decoder state to avoid interfering with running decoder */ + SKP_Silk_decoder_control sDecCtrl; + SKP_int i, TempQ[ MAX_FRAME_LENGTH ]; + + psDec = (SKP_Silk_decoder_state *)decState; + + sDec.nFramesDecoded = 0; + sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */ + SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn ); + + if( psDec->bitstream_v == BIT_STREAM_V4 ) { /* Silk_v4 payload */ + /* Decode all parameter indices for the whole packet*/ + SKP_Silk_decode_indices_v4( &sDec ); + + if( sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET || sDec.sRC.error ) { + /* Corrupt packet */ + SKP_memset( Silk_TOC, 0, sizeof( SKP_Silk_TOC_struct ) ); + Silk_TOC->corrupt = 1; + } else { + Silk_TOC->corrupt = 0; + Silk_TOC->framesInPacket = sDec.nFramesInPacket; + Silk_TOC->fs_kHz = sDec.fs_kHz; + if( sDec.FrameTermination == SKP_SILK_LAST_FRAME ) { + Silk_TOC->inbandLBRR = sDec.FrameTermination; + } else { + Silk_TOC->inbandLBRR = sDec.FrameTermination - 1; + } + /* Copy data */ + for( i = 0; i < sDec.nFramesInPacket; i++ ) { + Silk_TOC->vadFlags[ i ] = sDec.vadFlagBuf[ i ]; + Silk_TOC->sigtypeFlags[ i ] = sDec.sigtype[ i ]; + } + } + } else { /* Silk_v3 payload */ + Silk_TOC->corrupt = 0; + while( 1 ) { + SKP_Silk_decode_parameters( &sDec, &sDecCtrl, TempQ, 0 ); + + Silk_TOC->vadFlags[ sDec.nFramesDecoded ] = sDec.vadFlag; + Silk_TOC->sigtypeFlags[ sDec.nFramesDecoded ] = sDecCtrl.sigtype; + + if( sDec.sRC.error ) { + /* Corrupt stream */ + Silk_TOC->corrupt = 1; + break; + }; + + if( sDec.nBytesLeft > 0 && sDec.FrameTermination == SKP_SILK_MORE_FRAMES ) { + sDec.nFramesDecoded++; + } else { + break; + } + } + if( Silk_TOC->corrupt || sDec.FrameTermination == SKP_SILK_MORE_FRAMES || + sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET ) { + /* Corrupt packet */ + SKP_memset( Silk_TOC, 0, sizeof( SKP_Silk_TOC_struct ) ); + Silk_TOC->corrupt = 1; + } else { + Silk_TOC->framesInPacket = sDec.nFramesDecoded; + Silk_TOC->fs_kHz = sDec.fs_kHz; + if( sDec.FrameTermination == SKP_SILK_LAST_FRAME ) { + Silk_TOC->inbandLBRR = sDec.FrameTermination; + } else { + Silk_TOC->inbandLBRR = sDec.FrameTermination - 1; + } + } + } +} + +/**************************/ +/* Get the version number */ +/**************************/ +/* Return a pointer to string specifying the version */ +const char *SKP_Silk_SDK_get_version() +{ + static const char version[] = "1.0.2"; + return version; +} diff --git a/jni/silk/src/SKP_Silk_decode_core.c b/app/src/main/jni/silk/src/SKP_Silk_decode_core.c similarity index 98% rename from jni/silk/src/SKP_Silk_decode_core.c rename to app/src/main/jni/silk/src/SKP_Silk_decode_core.c index fec15a2..50640d3 100644 --- a/jni/silk/src/SKP_Silk_decode_core.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decode_core.c @@ -1,256 +1,256 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/**********************************************************/ -/* Core decoder. Performs inverse NSQ operation LTP + LPC */ -/**********************************************************/ -void SKP_Silk_decode_core( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I Decoder control */ - SKP_int16 xq[], /* O Decoded speech */ - const SKP_int q[ MAX_FRAME_LENGTH ] /* I Pulse signal */ -) -{ - SKP_int i, k, lag = 0, start_idx, NLSF_interpolation_flag, sigtype, LTP_scale_Q14; - SKP_int16 *A_Q12, *B_Q14, *pxq, A_Q12_tmp[ MAX_LPC_ORDER ]; - SKP_int16 sLTP[ MAX_FRAME_LENGTH ]; - SKP_int32 Gain_Q16, *pred_lag_ptr, *pexc_Q10, *pres_Q10, LTP_pred_Q14, LPC_pred_Q10; - SKP_int32 rand_seed, offset_Q10, dither; - SKP_int32 vec_Q10[ MAX_FRAME_LENGTH / NB_SUBFR ], Atmp; - SKP_int32 inv_gain_Q16, inv_gain_Q32, gain_adj_Q16, FiltState[ MAX_LPC_ORDER ]; - SKP_assert( psDec->prev_inv_gain_Q16 != 0 ); - - offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psDecCtrl->sigtype ][ psDecCtrl->QuantOffsetType ]; - - if( psDecCtrl->NLSFInterpCoef_Q2 < ( 1 << 2 ) ) { - NLSF_interpolation_flag = 1; - } else { - NLSF_interpolation_flag = 0; - } - - - /* Decode excitation */ - rand_seed = psDecCtrl->Seed; - for( i = 0; i < psDec->frame_length; i++ ) { - rand_seed = SKP_RAND( rand_seed ); - /* dither = rand_seed < 0 ? 0xFFFFFFFF : 0; */ - dither = SKP_RSHIFT( rand_seed, 31 ); - - psDec->exc_Q10[ i ] = SKP_LSHIFT( ( SKP_int32 )q[ i ], 10 ) + offset_Q10; - psDec->exc_Q10[ i ] = ( psDec->exc_Q10[ i ] ^ dither ) - dither; - - rand_seed += q[ i ]; - } - - - pexc_Q10 = psDec->exc_Q10; - pres_Q10 = psDec->res_Q10; - pxq = &psDec->outBuf[ psDec->frame_length ]; - psDec->sLTP_buf_idx = psDec->frame_length; - /* Loop over subframes */ - for( k = 0; k < NB_SUBFR; k++ ) { - A_Q12 = psDecCtrl->PredCoef_Q12[ k >> 1 ]; - - /* Preload LPC coeficients to array on stack. Gives small performance gain */ - SKP_memcpy( A_Q12_tmp, A_Q12, psDec->LPC_order * sizeof( SKP_int16 ) ); - B_Q14 = &psDecCtrl->LTPCoef_Q14[ k * LTP_ORDER ]; - Gain_Q16 = psDecCtrl->Gains_Q16[ k ]; - LTP_scale_Q14 = psDecCtrl->LTP_scale_Q14; - sigtype = psDecCtrl->sigtype; - - inv_gain_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( Gain_Q16, 1 ) ); - inv_gain_Q16 = SKP_min( inv_gain_Q16, SKP_int16_MAX ); - - /* Calculate Gain adjustment factor */ - gain_adj_Q16 = ( SKP_int32 )1 << 16; - if( inv_gain_Q16 != psDec->prev_inv_gain_Q16 ) { - gain_adj_Q16 = SKP_DIV32_varQ( inv_gain_Q16, psDec->prev_inv_gain_Q16, 16 ); - } - - /* Avoid abrupt transition from voiced PLC to unvoiced normal decoding */ - if( psDec->lossCnt && psDec->prev_sigtype == SIG_TYPE_VOICED && - psDecCtrl->sigtype == SIG_TYPE_UNVOICED && k < ( NB_SUBFR >> 1 ) ) { - - SKP_memset( B_Q14, 0, LTP_ORDER * sizeof( SKP_int16 ) ); - B_Q14[ LTP_ORDER/2 ] = ( SKP_int16 )1 << 12; /* 0.25 */ - - sigtype = SIG_TYPE_VOICED; - psDecCtrl->pitchL[ k ] = psDec->lagPrev; - LTP_scale_Q14 = ( SKP_int )1 << 14; - } - if( sigtype == SIG_TYPE_VOICED ) { - /* Voiced */ - - lag = psDecCtrl->pitchL[ k ]; - /* Re-whitening */ - if( ( k & ( 3 - SKP_LSHIFT( NLSF_interpolation_flag, 1 ) ) ) == 0 ) { - /* Rewhiten with new A coefs */ - start_idx = psDec->frame_length - lag - psDec->LPC_order - LTP_ORDER / 2; - start_idx = SKP_LIMIT( start_idx, 0, psDec->frame_length - psDec->LPC_order ); - - SKP_Silk_MA_Prediction( &psDec->outBuf[ start_idx + k * ( psDec->frame_length >> 2 ) ], - A_Q12, FiltState, sLTP + start_idx, psDec->frame_length - start_idx, psDec->LPC_order ); - - /* After rewhitening the LTP state is unscaled */ - inv_gain_Q32 = SKP_LSHIFT( inv_gain_Q16, 16 ); - if( k == 0 ) { - /* Do LTP downscaling */ - inv_gain_Q32 = SKP_LSHIFT( SKP_SMULWB( inv_gain_Q32, psDecCtrl->LTP_scale_Q14 ), 2 ); - } - for( i = 0; i < (lag + LTP_ORDER/2); i++ ) { - psDec->sLTP_Q16[ psDec->sLTP_buf_idx - i - 1 ] = SKP_SMULWB( inv_gain_Q32, sLTP[ psDec->frame_length - i - 1 ] ); - } - } else { - /* Update LTP state when Gain changes */ - if( gain_adj_Q16 != ( SKP_int32 )1 << 16 ) { - for( i = 0; i < ( lag + LTP_ORDER / 2 ); i++ ) { - psDec->sLTP_Q16[ psDec->sLTP_buf_idx - i - 1 ] = SKP_SMULWW( gain_adj_Q16, psDec->sLTP_Q16[ psDec->sLTP_buf_idx - i - 1 ] ); - } - } - } - } - - /* Scale short term state */ - for( i = 0; i < MAX_LPC_ORDER; i++ ) { - psDec->sLPC_Q14[ i ] = SKP_SMULWW( gain_adj_Q16, psDec->sLPC_Q14[ i ] ); - } - - /* Save inv_gain */ - SKP_assert( inv_gain_Q16 != 0 ); - psDec->prev_inv_gain_Q16 = inv_gain_Q16; - - - /* Long-term prediction */ - if( sigtype == SIG_TYPE_VOICED ) { - /* Setup pointer */ - pred_lag_ptr = &psDec->sLTP_Q16[ psDec->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; - for( i = 0; i < psDec->subfr_length; i++ ) { - /* Unrolled loop */ - LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], B_Q14[ 0 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], B_Q14[ 1 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], B_Q14[ 2 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], B_Q14[ 3 ] ); - LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], B_Q14[ 4 ] ); - pred_lag_ptr++; - - /* Generate LPC residual */ - pres_Q10[ i ] = SKP_ADD32( pexc_Q10[ i ], SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ) ); - - /* Update states */ - psDec->sLTP_Q16[ psDec->sLTP_buf_idx ] = SKP_LSHIFT( pres_Q10[ i ], 6 ); - psDec->sLTP_buf_idx++; - } - } else { - SKP_memcpy( pres_Q10, pexc_Q10, psDec->subfr_length * sizeof( SKP_int32 ) ); - } - - - /* Short term prediction */ - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLAWB and SMLAWT instructions should solve the problem. */ - if( psDec->LPC_order == 16 ) { - for( i = 0; i < psDec->subfr_length; i++ ) { - /* unrolled */ - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 0 ] ); /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMULWB( psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 2 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 4 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 6 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 8 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 10 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 11 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 12 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 12 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 13 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 14 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 14 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 15 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 16 ], Atmp ); - - /* Add prediction to LPC residual */ - vec_Q10[ i ] = SKP_ADD32( pres_Q10[ i ], LPC_pred_Q10 ); - - /* Update states */ - psDec->sLPC_Q14[ MAX_LPC_ORDER + i ] = SKP_LSHIFT( vec_Q10[ i ], 4 ); - } - } else { - SKP_assert( psDec->LPC_order == 10 ); - for( i = 0; i < psDec->subfr_length; i++ ) { - /* unrolled */ - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 0 ] ); /* read two coefficients at once */ - LPC_pred_Q10 = SKP_SMULWB( psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 2 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 4 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 6 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], Atmp ); - Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 8 ] ); - LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], Atmp ); - LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], Atmp ); - - /* Add prediction to LPC residual */ - vec_Q10[ i ] = SKP_ADD32( pres_Q10[ i ], LPC_pred_Q10 ); - - /* Update states */ - psDec->sLPC_Q14[ MAX_LPC_ORDER + i ] = SKP_LSHIFT( vec_Q10[ i ], 4 ); - } - } - - /* Scale with Gain */ - for( i = 0; i < psDec->subfr_length; i++ ) { - pxq[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( vec_Q10[ i ], Gain_Q16 ), 10 ) ); - } - - /* Update LPC filter state */ - SKP_memcpy( psDec->sLPC_Q14, &psDec->sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( SKP_int32 ) ); - pexc_Q10 += psDec->subfr_length; - pres_Q10 += psDec->subfr_length; - pxq += psDec->subfr_length; - } - - /* Copy to output */ - SKP_memcpy( xq, &psDec->outBuf[ psDec->frame_length ], psDec->frame_length * sizeof( SKP_int16 ) ); - -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/**********************************************************/ +/* Core decoder. Performs inverse NSQ operation LTP + LPC */ +/**********************************************************/ +void SKP_Silk_decode_core( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I Decoder control */ + SKP_int16 xq[], /* O Decoded speech */ + const SKP_int q[ MAX_FRAME_LENGTH ] /* I Pulse signal */ +) +{ + SKP_int i, k, lag = 0, start_idx, NLSF_interpolation_flag, sigtype, LTP_scale_Q14; + SKP_int16 *A_Q12, *B_Q14, *pxq, A_Q12_tmp[ MAX_LPC_ORDER ]; + SKP_int16 sLTP[ MAX_FRAME_LENGTH ]; + SKP_int32 Gain_Q16, *pred_lag_ptr, *pexc_Q10, *pres_Q10, LTP_pred_Q14, LPC_pred_Q10; + SKP_int32 rand_seed, offset_Q10, dither; + SKP_int32 vec_Q10[ MAX_FRAME_LENGTH / NB_SUBFR ], Atmp; + SKP_int32 inv_gain_Q16, inv_gain_Q32, gain_adj_Q16, FiltState[ MAX_LPC_ORDER ]; + SKP_assert( psDec->prev_inv_gain_Q16 != 0 ); + + offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psDecCtrl->sigtype ][ psDecCtrl->QuantOffsetType ]; + + if( psDecCtrl->NLSFInterpCoef_Q2 < ( 1 << 2 ) ) { + NLSF_interpolation_flag = 1; + } else { + NLSF_interpolation_flag = 0; + } + + + /* Decode excitation */ + rand_seed = psDecCtrl->Seed; + for( i = 0; i < psDec->frame_length; i++ ) { + rand_seed = SKP_RAND( rand_seed ); + /* dither = rand_seed < 0 ? 0xFFFFFFFF : 0; */ + dither = SKP_RSHIFT( rand_seed, 31 ); + + psDec->exc_Q10[ i ] = SKP_LSHIFT( ( SKP_int32 )q[ i ], 10 ) + offset_Q10; + psDec->exc_Q10[ i ] = ( psDec->exc_Q10[ i ] ^ dither ) - dither; + + rand_seed += q[ i ]; + } + + + pexc_Q10 = psDec->exc_Q10; + pres_Q10 = psDec->res_Q10; + pxq = &psDec->outBuf[ psDec->frame_length ]; + psDec->sLTP_buf_idx = psDec->frame_length; + /* Loop over subframes */ + for( k = 0; k < NB_SUBFR; k++ ) { + A_Q12 = psDecCtrl->PredCoef_Q12[ k >> 1 ]; + + /* Preload LPC coeficients to array on stack. Gives small performance gain */ + SKP_memcpy( A_Q12_tmp, A_Q12, psDec->LPC_order * sizeof( SKP_int16 ) ); + B_Q14 = &psDecCtrl->LTPCoef_Q14[ k * LTP_ORDER ]; + Gain_Q16 = psDecCtrl->Gains_Q16[ k ]; + LTP_scale_Q14 = psDecCtrl->LTP_scale_Q14; + sigtype = psDecCtrl->sigtype; + + inv_gain_Q16 = SKP_DIV32( SKP_int32_MAX, SKP_RSHIFT( Gain_Q16, 1 ) ); + inv_gain_Q16 = SKP_min( inv_gain_Q16, SKP_int16_MAX ); + + /* Calculate Gain adjustment factor */ + gain_adj_Q16 = ( SKP_int32 )1 << 16; + if( inv_gain_Q16 != psDec->prev_inv_gain_Q16 ) { + gain_adj_Q16 = SKP_DIV32_varQ( inv_gain_Q16, psDec->prev_inv_gain_Q16, 16 ); + } + + /* Avoid abrupt transition from voiced PLC to unvoiced normal decoding */ + if( psDec->lossCnt && psDec->prev_sigtype == SIG_TYPE_VOICED && + psDecCtrl->sigtype == SIG_TYPE_UNVOICED && k < ( NB_SUBFR >> 1 ) ) { + + SKP_memset( B_Q14, 0, LTP_ORDER * sizeof( SKP_int16 ) ); + B_Q14[ LTP_ORDER/2 ] = ( SKP_int16 )1 << 12; /* 0.25 */ + + sigtype = SIG_TYPE_VOICED; + psDecCtrl->pitchL[ k ] = psDec->lagPrev; + LTP_scale_Q14 = ( SKP_int )1 << 14; + } + if( sigtype == SIG_TYPE_VOICED ) { + /* Voiced */ + + lag = psDecCtrl->pitchL[ k ]; + /* Re-whitening */ + if( ( k & ( 3 - SKP_LSHIFT( NLSF_interpolation_flag, 1 ) ) ) == 0 ) { + /* Rewhiten with new A coefs */ + start_idx = psDec->frame_length - lag - psDec->LPC_order - LTP_ORDER / 2; + start_idx = SKP_LIMIT( start_idx, 0, psDec->frame_length - psDec->LPC_order ); + + SKP_Silk_MA_Prediction( &psDec->outBuf[ start_idx + k * ( psDec->frame_length >> 2 ) ], + A_Q12, FiltState, sLTP + start_idx, psDec->frame_length - start_idx, psDec->LPC_order ); + + /* After rewhitening the LTP state is unscaled */ + inv_gain_Q32 = SKP_LSHIFT( inv_gain_Q16, 16 ); + if( k == 0 ) { + /* Do LTP downscaling */ + inv_gain_Q32 = SKP_LSHIFT( SKP_SMULWB( inv_gain_Q32, psDecCtrl->LTP_scale_Q14 ), 2 ); + } + for( i = 0; i < (lag + LTP_ORDER/2); i++ ) { + psDec->sLTP_Q16[ psDec->sLTP_buf_idx - i - 1 ] = SKP_SMULWB( inv_gain_Q32, sLTP[ psDec->frame_length - i - 1 ] ); + } + } else { + /* Update LTP state when Gain changes */ + if( gain_adj_Q16 != ( SKP_int32 )1 << 16 ) { + for( i = 0; i < ( lag + LTP_ORDER / 2 ); i++ ) { + psDec->sLTP_Q16[ psDec->sLTP_buf_idx - i - 1 ] = SKP_SMULWW( gain_adj_Q16, psDec->sLTP_Q16[ psDec->sLTP_buf_idx - i - 1 ] ); + } + } + } + } + + /* Scale short term state */ + for( i = 0; i < MAX_LPC_ORDER; i++ ) { + psDec->sLPC_Q14[ i ] = SKP_SMULWW( gain_adj_Q16, psDec->sLPC_Q14[ i ] ); + } + + /* Save inv_gain */ + SKP_assert( inv_gain_Q16 != 0 ); + psDec->prev_inv_gain_Q16 = inv_gain_Q16; + + + /* Long-term prediction */ + if( sigtype == SIG_TYPE_VOICED ) { + /* Setup pointer */ + pred_lag_ptr = &psDec->sLTP_Q16[ psDec->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; + for( i = 0; i < psDec->subfr_length; i++ ) { + /* Unrolled loop */ + LTP_pred_Q14 = SKP_SMULWB( pred_lag_ptr[ 0 ], B_Q14[ 0 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], B_Q14[ 1 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], B_Q14[ 2 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], B_Q14[ 3 ] ); + LTP_pred_Q14 = SKP_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], B_Q14[ 4 ] ); + pred_lag_ptr++; + + /* Generate LPC residual */ + pres_Q10[ i ] = SKP_ADD32( pexc_Q10[ i ], SKP_RSHIFT_ROUND( LTP_pred_Q14, 4 ) ); + + /* Update states */ + psDec->sLTP_Q16[ psDec->sLTP_buf_idx ] = SKP_LSHIFT( pres_Q10[ i ], 6 ); + psDec->sLTP_buf_idx++; + } + } else { + SKP_memcpy( pres_Q10, pexc_Q10, psDec->subfr_length * sizeof( SKP_int32 ) ); + } + + + /* Short term prediction */ + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLAWB and SMLAWT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLAWB and SMLAWT instructions should solve the problem. */ + if( psDec->LPC_order == 16 ) { + for( i = 0; i < psDec->subfr_length; i++ ) { + /* unrolled */ + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 0 ] ); /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMULWB( psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 2 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 4 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 6 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 8 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 10 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 11 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 12 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 12 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 13 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 14 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 14 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 15 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 16 ], Atmp ); + + /* Add prediction to LPC residual */ + vec_Q10[ i ] = SKP_ADD32( pres_Q10[ i ], LPC_pred_Q10 ); + + /* Update states */ + psDec->sLPC_Q14[ MAX_LPC_ORDER + i ] = SKP_LSHIFT( vec_Q10[ i ], 4 ); + } + } else { + SKP_assert( psDec->LPC_order == 10 ); + for( i = 0; i < psDec->subfr_length; i++ ) { + /* unrolled */ + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 0 ] ); /* read two coefficients at once */ + LPC_pred_Q10 = SKP_SMULWB( psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 2 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 4 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 6 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], Atmp ); + Atmp = *( ( SKP_int32* )&A_Q12_tmp[ 8 ] ); + LPC_pred_Q10 = SKP_SMLAWB( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], Atmp ); + LPC_pred_Q10 = SKP_SMLAWT( LPC_pred_Q10, psDec->sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], Atmp ); + + /* Add prediction to LPC residual */ + vec_Q10[ i ] = SKP_ADD32( pres_Q10[ i ], LPC_pred_Q10 ); + + /* Update states */ + psDec->sLPC_Q14[ MAX_LPC_ORDER + i ] = SKP_LSHIFT( vec_Q10[ i ], 4 ); + } + } + + /* Scale with Gain */ + for( i = 0; i < psDec->subfr_length; i++ ) { + pxq[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SMULWW( vec_Q10[ i ], Gain_Q16 ), 10 ) ); + } + + /* Update LPC filter state */ + SKP_memcpy( psDec->sLPC_Q14, &psDec->sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( SKP_int32 ) ); + pexc_Q10 += psDec->subfr_length; + pres_Q10 += psDec->subfr_length; + pxq += psDec->subfr_length; + } + + /* Copy to output */ + SKP_memcpy( xq, &psDec->outBuf[ psDec->frame_length ], psDec->frame_length * sizeof( SKP_int16 ) ); + +} diff --git a/jni/silk/src/SKP_Silk_decode_frame.c b/app/src/main/jni/silk/src/SKP_Silk_decode_frame.c similarity index 97% rename from jni/silk/src/SKP_Silk_decode_frame.c rename to app/src/main/jni/silk/src/SKP_Silk_decode_frame.c index 279179b..6e9c4cf 100644 --- a/jni/silk/src/SKP_Silk_decode_frame.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decode_frame.c @@ -1,167 +1,167 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - - -#include "SKP_Silk_main.h" -#include "SKP_Silk_PLC.h" - -/****************/ -/* Decode frame */ -/****************/ -SKP_int SKP_Silk_decode_frame( - SKP_Silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */ - SKP_int16 pOut[], /* O Pointer to output speech frame */ - SKP_int16 *pN, /* O Pointer to size of output frame */ - const SKP_uint8 pCode[], /* I Pointer to payload */ - const SKP_int nBytes, /* I Payload length */ - SKP_int action, /* I Action from Jitter Buffer */ - SKP_int *decBytes /* O Used bytes to decode this frame */ -) -{ - SKP_Silk_decoder_control sDecCtrl; - SKP_int L, fs_Khz_old, LPC_order_old, ret = 0; - SKP_int Pulses[ MAX_FRAME_LENGTH ]; - - - L = psDec->frame_length; - sDecCtrl.LTP_scale_Q14 = 0; - - /* Safety checks */ - SKP_assert( L > 0 && L <= MAX_FRAME_LENGTH ); - - /********************************************/ - /* Decode Frame if packet is not lost */ - /********************************************/ - *decBytes = 0; - if( action == 0 ) { - /********************************************/ - /* Initialize arithmetic coder */ - /********************************************/ - fs_Khz_old = psDec->fs_kHz; - LPC_order_old = psDec->LPC_order; - if( psDec->nFramesDecoded == 0 ) { - /* Initialize range decoder state */ - SKP_Silk_range_dec_init( &psDec->sRC, pCode, nBytes ); - - if( psDec->bitstream_v == BIT_STREAM_V4 ) { - SKP_Silk_decode_indices_v4( psDec ); - } - } - - /********************************************/ - /* Decode parameters and pulse signal */ - /********************************************/ - if( psDec->bitstream_v == BIT_STREAM_V4 ) { - SKP_Silk_decode_parameters_v4( psDec, &sDecCtrl, Pulses, 1 ); - } else { - SKP_Silk_decode_parameters( psDec, &sDecCtrl, Pulses, 1 ); - } - - - if( psDec->sRC.error ) { - psDec->nBytesLeft = 0; - - action = 1; /* PLC operation */ - psDec->fs_kHz = fs_Khz_old; /* revert fs if changed in decode_parameters */ - psDec->LPC_order = LPC_order_old; /* revert lpc_order if changed in decode_parameters */ - psDec->frame_length = fs_Khz_old * FRAME_LENGTH_MS; - psDec->subfr_length = fs_Khz_old * FRAME_LENGTH_MS / NB_SUBFR; - - /* Avoid crashing */ - *decBytes = psDec->sRC.bufferLength; - - if( psDec->sRC.error == RANGE_CODER_DEC_PAYLOAD_TOO_LONG ) { - ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE; - } else { - ret = SKP_SILK_DEC_PAYLOAD_ERROR; - } - } else { - *decBytes = psDec->sRC.bufferLength - psDec->nBytesLeft; - psDec->nFramesDecoded++; - - /* Update lengths. Sampling frequency could have changed */ - L = psDec->frame_length; - - /********************************************************/ - /* Run inverse NSQ */ - /********************************************************/ - SKP_Silk_decode_core( psDec, &sDecCtrl, pOut, Pulses ); - - /********************************************************/ - /* Update PLC state */ - /********************************************************/ - SKP_Silk_PLC( psDec, &sDecCtrl, pOut, L, action ); - - psDec->lossCnt = 0; - psDec->prev_sigtype = sDecCtrl.sigtype; - - /* A frame has been decoded without errors */ - psDec->first_frame_after_reset = 0; - } - } - /*************************************************************/ - /* Generate Concealment Frame if packet is lost, or corrupt */ - /*************************************************************/ - if( action == 1 ) { - /* Handle packet loss by extrapolation */ - SKP_Silk_PLC( psDec, &sDecCtrl, pOut, L, action ); - psDec->lossCnt++; - - } - - /*************************/ - /* Update output buffer. */ - /*************************/ - SKP_memcpy( psDec->outBuf, pOut, L * sizeof( SKP_int16 ) ); - - /****************************************************************/ - /* Ensure smooth connection of extrapolated and good frames */ - /****************************************************************/ - SKP_Silk_PLC_glue_frames( psDec, &sDecCtrl, pOut, L ); - - /************************************************/ - /* Comfort noise generation / estimation */ - /************************************************/ - SKP_Silk_CNG( psDec, &sDecCtrl, pOut , L ); - - /********************************************/ - /* HP filter output */ - /********************************************/ - SKP_assert( ( ( psDec->fs_kHz == 12 ) && ( L % 3 ) == 0 ) || - ( ( psDec->fs_kHz != 12 ) && ( L % 2 ) == 0 ) ); - SKP_Silk_biquad( pOut, psDec->HP_B, psDec->HP_A, psDec->HPState, pOut, L ); - - /********************************************/ - /* set output frame length */ - /********************************************/ - *pN = ( SKP_int16 )L; - - /* Update some decoder state variables */ - psDec->lagPrev = sDecCtrl.pitchL[ NB_SUBFR - 1 ]; - - return ret; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + + +#include "SKP_Silk_main.h" +#include "SKP_Silk_PLC.h" + +/****************/ +/* Decode frame */ +/****************/ +SKP_int SKP_Silk_decode_frame( + SKP_Silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */ + SKP_int16 pOut[], /* O Pointer to output speech frame */ + SKP_int16 *pN, /* O Pointer to size of output frame */ + const SKP_uint8 pCode[], /* I Pointer to payload */ + const SKP_int nBytes, /* I Payload length */ + SKP_int action, /* I Action from Jitter Buffer */ + SKP_int *decBytes /* O Used bytes to decode this frame */ +) +{ + SKP_Silk_decoder_control sDecCtrl; + SKP_int L, fs_Khz_old, LPC_order_old, ret = 0; + SKP_int Pulses[ MAX_FRAME_LENGTH ]; + + + L = psDec->frame_length; + sDecCtrl.LTP_scale_Q14 = 0; + + /* Safety checks */ + SKP_assert( L > 0 && L <= MAX_FRAME_LENGTH ); + + /********************************************/ + /* Decode Frame if packet is not lost */ + /********************************************/ + *decBytes = 0; + if( action == 0 ) { + /********************************************/ + /* Initialize arithmetic coder */ + /********************************************/ + fs_Khz_old = psDec->fs_kHz; + LPC_order_old = psDec->LPC_order; + if( psDec->nFramesDecoded == 0 ) { + /* Initialize range decoder state */ + SKP_Silk_range_dec_init( &psDec->sRC, pCode, nBytes ); + + if( psDec->bitstream_v == BIT_STREAM_V4 ) { + SKP_Silk_decode_indices_v4( psDec ); + } + } + + /********************************************/ + /* Decode parameters and pulse signal */ + /********************************************/ + if( psDec->bitstream_v == BIT_STREAM_V4 ) { + SKP_Silk_decode_parameters_v4( psDec, &sDecCtrl, Pulses, 1 ); + } else { + SKP_Silk_decode_parameters( psDec, &sDecCtrl, Pulses, 1 ); + } + + + if( psDec->sRC.error ) { + psDec->nBytesLeft = 0; + + action = 1; /* PLC operation */ + psDec->fs_kHz = fs_Khz_old; /* revert fs if changed in decode_parameters */ + psDec->LPC_order = LPC_order_old; /* revert lpc_order if changed in decode_parameters */ + psDec->frame_length = fs_Khz_old * FRAME_LENGTH_MS; + psDec->subfr_length = fs_Khz_old * FRAME_LENGTH_MS / NB_SUBFR; + + /* Avoid crashing */ + *decBytes = psDec->sRC.bufferLength; + + if( psDec->sRC.error == RANGE_CODER_DEC_PAYLOAD_TOO_LONG ) { + ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE; + } else { + ret = SKP_SILK_DEC_PAYLOAD_ERROR; + } + } else { + *decBytes = psDec->sRC.bufferLength - psDec->nBytesLeft; + psDec->nFramesDecoded++; + + /* Update lengths. Sampling frequency could have changed */ + L = psDec->frame_length; + + /********************************************************/ + /* Run inverse NSQ */ + /********************************************************/ + SKP_Silk_decode_core( psDec, &sDecCtrl, pOut, Pulses ); + + /********************************************************/ + /* Update PLC state */ + /********************************************************/ + SKP_Silk_PLC( psDec, &sDecCtrl, pOut, L, action ); + + psDec->lossCnt = 0; + psDec->prev_sigtype = sDecCtrl.sigtype; + + /* A frame has been decoded without errors */ + psDec->first_frame_after_reset = 0; + } + } + /*************************************************************/ + /* Generate Concealment Frame if packet is lost, or corrupt */ + /*************************************************************/ + if( action == 1 ) { + /* Handle packet loss by extrapolation */ + SKP_Silk_PLC( psDec, &sDecCtrl, pOut, L, action ); + psDec->lossCnt++; + + } + + /*************************/ + /* Update output buffer. */ + /*************************/ + SKP_memcpy( psDec->outBuf, pOut, L * sizeof( SKP_int16 ) ); + + /****************************************************************/ + /* Ensure smooth connection of extrapolated and good frames */ + /****************************************************************/ + SKP_Silk_PLC_glue_frames( psDec, &sDecCtrl, pOut, L ); + + /************************************************/ + /* Comfort noise generation / estimation */ + /************************************************/ + SKP_Silk_CNG( psDec, &sDecCtrl, pOut , L ); + + /********************************************/ + /* HP filter output */ + /********************************************/ + SKP_assert( ( ( psDec->fs_kHz == 12 ) && ( L % 3 ) == 0 ) || + ( ( psDec->fs_kHz != 12 ) && ( L % 2 ) == 0 ) ); + SKP_Silk_biquad( pOut, psDec->HP_B, psDec->HP_A, psDec->HPState, pOut, L ); + + /********************************************/ + /* set output frame length */ + /********************************************/ + *pN = ( SKP_int16 )L; + + /* Update some decoder state variables */ + psDec->lagPrev = sDecCtrl.pitchL[ NB_SUBFR - 1 ]; + + return ret; +} diff --git a/jni/silk/src/SKP_Silk_decode_indices_v4.c b/app/src/main/jni/silk/src/SKP_Silk_decode_indices_v4.c similarity index 98% rename from jni/silk/src/SKP_Silk_decode_indices_v4.c rename to app/src/main/jni/silk/src/SKP_Silk_decode_indices_v4.c index b86d1f5..3d5e094 100644 --- a/jni/silk/src/SKP_Silk_decode_indices_v4.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decode_indices_v4.c @@ -1,195 +1,195 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Decode indices from payload */ -void SKP_Silk_decode_indices_v4( - SKP_Silk_decoder_state *psDec /* I/O State */ -) -{ - SKP_int i, k, Ix, fs_kHz_dec, FrameIndex = 0, FrameTermination; - SKP_int sigtype, QuantOffsetType, seed_int, nBytesUsed; - SKP_int decode_absolute_lagIndex, delta_lagIndex, prev_lagIndex = 0; - const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL; - SKP_Silk_range_coder_state *psRC = &psDec->sRC; - /************************/ - /* Decode sampling rate */ - /************************/ - /* only done for first frame of packet */ - if( psDec->nFramesDecoded == 0 ) { - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset ); - - /* check that sampling rate is supported */ - if( Ix < 0 || Ix > 3 ) { - psRC->error = RANGE_CODER_ILLEGAL_SAMPLING_RATE; - return; - } - fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ]; - SKP_Silk_decoder_set_fs( psDec, fs_kHz_dec ); - - FrameIndex = 0; - FrameTermination = SKP_SILK_MORE_FRAMES; - } - - while( FrameTermination == SKP_SILK_MORE_FRAMES ) { - /*******************/ - /* Decode VAD flag */ - /*******************/ - SKP_Silk_range_decoder( &psDec->vadFlagBuf[ FrameIndex ], psRC, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset ); - - /*******************************************/ - /* Decode signal type and quantizer offset */ - /*******************************************/ - if( FrameIndex == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset ); - } else { - /* condidtional coding */ - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ], - SKP_Silk_type_offset_CDF_offset ); - } - sigtype = SKP_RSHIFT( Ix, 1 ); - QuantOffsetType = Ix & 1; - psDec->typeOffsetPrev = Ix; - - /****************/ - /* Decode gains */ - /****************/ - /* first subframe */ - if( FrameIndex == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_gain_CDF[ sigtype ], SKP_Silk_gain_CDF_offset ); - } else { - /* condidtional coding */ - SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); - } - - /* remaining subframes */ - for( i = 1; i < NB_SUBFR; i++ ) { - SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ i ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); - } - - /**********************/ - /* Decode LSF Indices */ - /**********************/ - - /* Set pointer to LSF VQ CB for the current signal type */ - psNLSF_CB = psDec->psNLSF_CB[ sigtype ]; - - /* Arithmetically decode NLSF path */ - SKP_Silk_range_decoder_multi( psDec->NLSFIndices[ FrameIndex ], psRC, psNLSF_CB->StartPtr, psNLSF_CB->MiddleIx, psNLSF_CB->nStages ); - - /***********************************/ - /* Decode LSF interpolation factor */ - /***********************************/ - SKP_Silk_range_decoder( &psDec->NLSFInterpCoef_Q2[ FrameIndex ], psRC, SKP_Silk_NLSF_interpolation_factor_CDF, - SKP_Silk_NLSF_interpolation_factor_offset ); - - if( sigtype == SIG_TYPE_VOICED ) { - /*********************/ - /* Decode pitch lags */ - /*********************/ - /* Get lag index */ - decode_absolute_lagIndex = 1; - if( FrameIndex > 0 && psDec->sigtype[ FrameIndex - 1 ] == SIG_TYPE_VOICED ) { - /* Decode Delta index */ - SKP_Silk_range_decoder( &delta_lagIndex,psRC, SKP_Silk_pitch_delta_CDF, SKP_Silk_pitch_delta_CDF_offset ); - if( delta_lagIndex < ( MAX_DELTA_LAG << 1 ) + 1 ) { - delta_lagIndex = delta_lagIndex - MAX_DELTA_LAG; - psDec->lagIndex[ FrameIndex ] = prev_lagIndex + delta_lagIndex; - decode_absolute_lagIndex = 0; - } - } - if( decode_absolute_lagIndex ) { - /* Absolute decoding */ - if( psDec->fs_kHz == 8 ) { - SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_NB_CDF, SKP_Silk_pitch_lag_NB_CDF_offset ); - } else if( psDec->fs_kHz == 12 ) { - SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_MB_CDF, SKP_Silk_pitch_lag_MB_CDF_offset ); - } else if( psDec->fs_kHz == 16 ) { - SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_WB_CDF, SKP_Silk_pitch_lag_WB_CDF_offset ); - } else { - SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset ); - } - } - prev_lagIndex = psDec->lagIndex[ FrameIndex ]; - - /* Get countour index */ - if( psDec->fs_kHz == 8 ) { - /* Less codevectors used in 8 khz mode */ - SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset ); - } else { - /* Joint for 12, 16, and 24 khz */ - SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset ); - } - - /********************/ - /* Decode LTP gains */ - /********************/ - /* Decode PERIndex value */ - SKP_Silk_range_decoder( &psDec->PERIndex[ FrameIndex ], psRC, SKP_Silk_LTP_per_index_CDF, SKP_Silk_LTP_per_index_CDF_offset ); - - for( k = 0; k < NB_SUBFR; k++ ) { - SKP_Silk_range_decoder( &psDec->LTPIndex[ FrameIndex ][ k ], psRC, SKP_Silk_LTP_gain_CDF_ptrs[ psDec->PERIndex[ FrameIndex ] ], - SKP_Silk_LTP_gain_CDF_offsets[ psDec->PERIndex[ FrameIndex ] ] ); - } - - /**********************/ - /* Decode LTP scaling */ - /**********************/ - SKP_Silk_range_decoder( &psDec->LTP_scaleIndex[ FrameIndex ], psRC, SKP_Silk_LTPscale_CDF, SKP_Silk_LTPscale_offset ); - } - - /***************/ - /* Decode seed */ - /***************/ - SKP_Silk_range_decoder( &seed_int, psRC, SKP_Silk_Seed_CDF, SKP_Silk_Seed_offset ); - psDec->Seed[ FrameIndex ] = ( SKP_int32 )seed_int; - /**************************************/ - /* Decode Frame termination indicator */ - /**************************************/ - SKP_Silk_range_decoder( &FrameTermination, psRC, SKP_Silk_FrameTermination_v4_CDF, SKP_Silk_FrameTermination_v4_offset ); - - psDec->sigtype[ FrameIndex ] = sigtype; - psDec->QuantOffsetType[ FrameIndex ] = QuantOffsetType; - - FrameIndex++; - } - - /****************************************/ - /* get number of bytes used so far */ - /****************************************/ - SKP_Silk_range_coder_get_length( psRC, &nBytesUsed ); - psDec->nBytesLeft = psRC->bufferLength - nBytesUsed; - if( psDec->nBytesLeft < 0 ) { - psRC->error = RANGE_CODER_READ_BEYOND_BUFFER; - } - - psDec->nFramesInPacket = FrameIndex; - psDec->FrameTermination = FrameTermination; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Decode indices from payload */ +void SKP_Silk_decode_indices_v4( + SKP_Silk_decoder_state *psDec /* I/O State */ +) +{ + SKP_int i, k, Ix, fs_kHz_dec, FrameIndex = 0, FrameTermination; + SKP_int sigtype, QuantOffsetType, seed_int, nBytesUsed; + SKP_int decode_absolute_lagIndex, delta_lagIndex, prev_lagIndex = 0; + const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL; + SKP_Silk_range_coder_state *psRC = &psDec->sRC; + /************************/ + /* Decode sampling rate */ + /************************/ + /* only done for first frame of packet */ + if( psDec->nFramesDecoded == 0 ) { + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset ); + + /* check that sampling rate is supported */ + if( Ix < 0 || Ix > 3 ) { + psRC->error = RANGE_CODER_ILLEGAL_SAMPLING_RATE; + return; + } + fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ]; + SKP_Silk_decoder_set_fs( psDec, fs_kHz_dec ); + + FrameIndex = 0; + FrameTermination = SKP_SILK_MORE_FRAMES; + } + + while( FrameTermination == SKP_SILK_MORE_FRAMES ) { + /*******************/ + /* Decode VAD flag */ + /*******************/ + SKP_Silk_range_decoder( &psDec->vadFlagBuf[ FrameIndex ], psRC, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset ); + + /*******************************************/ + /* Decode signal type and quantizer offset */ + /*******************************************/ + if( FrameIndex == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset ); + } else { + /* condidtional coding */ + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ], + SKP_Silk_type_offset_CDF_offset ); + } + sigtype = SKP_RSHIFT( Ix, 1 ); + QuantOffsetType = Ix & 1; + psDec->typeOffsetPrev = Ix; + + /****************/ + /* Decode gains */ + /****************/ + /* first subframe */ + if( FrameIndex == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_gain_CDF[ sigtype ], SKP_Silk_gain_CDF_offset ); + } else { + /* condidtional coding */ + SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); + } + + /* remaining subframes */ + for( i = 1; i < NB_SUBFR; i++ ) { + SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ i ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); + } + + /**********************/ + /* Decode LSF Indices */ + /**********************/ + + /* Set pointer to LSF VQ CB for the current signal type */ + psNLSF_CB = psDec->psNLSF_CB[ sigtype ]; + + /* Arithmetically decode NLSF path */ + SKP_Silk_range_decoder_multi( psDec->NLSFIndices[ FrameIndex ], psRC, psNLSF_CB->StartPtr, psNLSF_CB->MiddleIx, psNLSF_CB->nStages ); + + /***********************************/ + /* Decode LSF interpolation factor */ + /***********************************/ + SKP_Silk_range_decoder( &psDec->NLSFInterpCoef_Q2[ FrameIndex ], psRC, SKP_Silk_NLSF_interpolation_factor_CDF, + SKP_Silk_NLSF_interpolation_factor_offset ); + + if( sigtype == SIG_TYPE_VOICED ) { + /*********************/ + /* Decode pitch lags */ + /*********************/ + /* Get lag index */ + decode_absolute_lagIndex = 1; + if( FrameIndex > 0 && psDec->sigtype[ FrameIndex - 1 ] == SIG_TYPE_VOICED ) { + /* Decode Delta index */ + SKP_Silk_range_decoder( &delta_lagIndex,psRC, SKP_Silk_pitch_delta_CDF, SKP_Silk_pitch_delta_CDF_offset ); + if( delta_lagIndex < ( MAX_DELTA_LAG << 1 ) + 1 ) { + delta_lagIndex = delta_lagIndex - MAX_DELTA_LAG; + psDec->lagIndex[ FrameIndex ] = prev_lagIndex + delta_lagIndex; + decode_absolute_lagIndex = 0; + } + } + if( decode_absolute_lagIndex ) { + /* Absolute decoding */ + if( psDec->fs_kHz == 8 ) { + SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_NB_CDF, SKP_Silk_pitch_lag_NB_CDF_offset ); + } else if( psDec->fs_kHz == 12 ) { + SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_MB_CDF, SKP_Silk_pitch_lag_MB_CDF_offset ); + } else if( psDec->fs_kHz == 16 ) { + SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_WB_CDF, SKP_Silk_pitch_lag_WB_CDF_offset ); + } else { + SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset ); + } + } + prev_lagIndex = psDec->lagIndex[ FrameIndex ]; + + /* Get countour index */ + if( psDec->fs_kHz == 8 ) { + /* Less codevectors used in 8 khz mode */ + SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset ); + } else { + /* Joint for 12, 16, and 24 khz */ + SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset ); + } + + /********************/ + /* Decode LTP gains */ + /********************/ + /* Decode PERIndex value */ + SKP_Silk_range_decoder( &psDec->PERIndex[ FrameIndex ], psRC, SKP_Silk_LTP_per_index_CDF, SKP_Silk_LTP_per_index_CDF_offset ); + + for( k = 0; k < NB_SUBFR; k++ ) { + SKP_Silk_range_decoder( &psDec->LTPIndex[ FrameIndex ][ k ], psRC, SKP_Silk_LTP_gain_CDF_ptrs[ psDec->PERIndex[ FrameIndex ] ], + SKP_Silk_LTP_gain_CDF_offsets[ psDec->PERIndex[ FrameIndex ] ] ); + } + + /**********************/ + /* Decode LTP scaling */ + /**********************/ + SKP_Silk_range_decoder( &psDec->LTP_scaleIndex[ FrameIndex ], psRC, SKP_Silk_LTPscale_CDF, SKP_Silk_LTPscale_offset ); + } + + /***************/ + /* Decode seed */ + /***************/ + SKP_Silk_range_decoder( &seed_int, psRC, SKP_Silk_Seed_CDF, SKP_Silk_Seed_offset ); + psDec->Seed[ FrameIndex ] = ( SKP_int32 )seed_int; + /**************************************/ + /* Decode Frame termination indicator */ + /**************************************/ + SKP_Silk_range_decoder( &FrameTermination, psRC, SKP_Silk_FrameTermination_v4_CDF, SKP_Silk_FrameTermination_v4_offset ); + + psDec->sigtype[ FrameIndex ] = sigtype; + psDec->QuantOffsetType[ FrameIndex ] = QuantOffsetType; + + FrameIndex++; + } + + /****************************************/ + /* get number of bytes used so far */ + /****************************************/ + SKP_Silk_range_coder_get_length( psRC, &nBytesUsed ); + psDec->nBytesLeft = psRC->bufferLength - nBytesUsed; + if( psDec->nBytesLeft < 0 ) { + psRC->error = RANGE_CODER_READ_BEYOND_BUFFER; + } + + psDec->nFramesInPacket = FrameIndex; + psDec->FrameTermination = FrameTermination; +} diff --git a/jni/silk/src/SKP_Silk_decode_parameters.c b/app/src/main/jni/silk/src/SKP_Silk_decode_parameters.c similarity index 97% rename from jni/silk/src/SKP_Silk_decode_parameters.c rename to app/src/main/jni/silk/src/SKP_Silk_decode_parameters.c index d585395..0d78048 100644 --- a/jni/silk/src/SKP_Silk_decode_parameters.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decode_parameters.c @@ -1,243 +1,243 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Decode parameters from payload */ -void SKP_Silk_decode_parameters( - SKP_Silk_decoder_state *psDec, /* I/O State */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int q[], /* O Excitation signal */ - const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ -) -{ - SKP_int i, k, Ix, fs_kHz_dec, nBytesUsed; - SKP_int Ixs[ NB_SUBFR ]; - SKP_int GainsIndices[ NB_SUBFR ]; - SKP_int NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ]; - SKP_int pNLSF_Q15[ MAX_LPC_ORDER ], pNLSF0_Q15[ MAX_LPC_ORDER ]; - const SKP_int16 *cbk_ptr_Q14; - const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL; - SKP_Silk_range_coder_state *psRC = &psDec->sRC; - - /************************/ - /* Decode sampling rate */ - /************************/ - /* only done for first frame of packet */ - if( psDec->nFramesDecoded == 0 ) { - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset ); - - /* check that sampling rate is supported */ - if( Ix < 0 || Ix > 3 ) { - psRC->error = RANGE_CODER_ILLEGAL_SAMPLING_RATE; - return; - } - fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ]; - SKP_Silk_decoder_set_fs( psDec, fs_kHz_dec ); - } - - /*******************************************/ - /* Decode signal type and quantizer offset */ - /*******************************************/ - if( psDec->nFramesDecoded == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset ); - } else { - /* condidtional coding */ - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ], - SKP_Silk_type_offset_CDF_offset ); - } - psDecCtrl->sigtype = SKP_RSHIFT( Ix, 1 ); - psDecCtrl->QuantOffsetType = Ix & 1; - psDec->typeOffsetPrev = Ix; - - /****************/ - /* Decode gains */ - /****************/ - /* first subframe */ - if( psDec->nFramesDecoded == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_decoder( &GainsIndices[ 0 ], psRC, SKP_Silk_gain_CDF[ psDecCtrl->sigtype ], SKP_Silk_gain_CDF_offset ); - } else { - /* condidtional coding */ - SKP_Silk_range_decoder( &GainsIndices[ 0 ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); - } - - /* remaining subframes */ - for( i = 1; i < NB_SUBFR; i++ ) { - SKP_Silk_range_decoder( &GainsIndices[ i ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); - } - - /* Dequant Gains */ - SKP_Silk_gains_dequant( psDecCtrl->Gains_Q16, GainsIndices, &psDec->LastGainIndex, psDec->nFramesDecoded ); - /****************/ - /* Decode NLSFs */ - /****************/ - /* Set pointer to NLSF VQ CB for the current signal type */ - psNLSF_CB = psDec->psNLSF_CB[ psDecCtrl->sigtype ]; - - /* Arithmetically decode NLSF path */ - SKP_Silk_range_decoder_multi( NLSFIndices, psRC, psNLSF_CB->StartPtr, psNLSF_CB->MiddleIx, psNLSF_CB->nStages ); - - /* From the NLSF path, decode an NLSF vector */ - SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, NLSFIndices, psDec->LPC_order ); - - /************************************/ - /* Decode NLSF interpolation factor */ - /************************************/ - SKP_Silk_range_decoder( &psDecCtrl->NLSFInterpCoef_Q2, psRC, SKP_Silk_NLSF_interpolation_factor_CDF, - SKP_Silk_NLSF_interpolation_factor_offset ); - - /* If just reset, e.g., because internal Fs changed, do not allow interpolation */ - /* improves the case of packet loss in the first frame after a switch */ - if( psDec->first_frame_after_reset == 1 ) { - psDecCtrl->NLSFInterpCoef_Q2 = 4; - } - - if( fullDecoding ) { - /* Convert NLSF parameters to AR prediction filter coefficients */ - SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order ); - - if( psDecCtrl->NLSFInterpCoef_Q2 < 4 ) { - /* Calculation of the interpolated NLSF0 vector from the interpolation factor, */ - /* the previous NLSF1, and the current NLSF1 */ - for( i = 0; i < psDec->LPC_order; i++ ) { - pNLSF0_Q15[ i ] = psDec->prevNLSF_Q15[ i ] + SKP_RSHIFT( SKP_MUL( psDecCtrl->NLSFInterpCoef_Q2, - ( pNLSF_Q15[ i ] - psDec->prevNLSF_Q15[ i ] ) ), 2 ); - } - - /* Convert NLSF parameters to AR prediction filter coefficients */ - SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 0 ], pNLSF0_Q15, psDec->LPC_order ); - } else { - /* Copy LPC coefficients for first half from second half */ - SKP_memcpy( psDecCtrl->PredCoef_Q12[ 0 ], psDecCtrl->PredCoef_Q12[ 1 ], - psDec->LPC_order * sizeof( SKP_int16 ) ); - } - } - - SKP_memcpy( psDec->prevNLSF_Q15, pNLSF_Q15, psDec->LPC_order * sizeof( SKP_int ) ); - - /* After a packet loss do BWE of LPC coefs */ - if( psDec->lossCnt ) { - SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 0 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); - SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); - } - - if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) { - /*********************/ - /* Decode pitch lags */ - /*********************/ - /* Get lag index */ - if( psDec->fs_kHz == 8 ) { - SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_NB_CDF, SKP_Silk_pitch_lag_NB_CDF_offset ); - } else if( psDec->fs_kHz == 12 ) { - SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_MB_CDF, SKP_Silk_pitch_lag_MB_CDF_offset ); - } else if( psDec->fs_kHz == 16 ) { - SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_WB_CDF, SKP_Silk_pitch_lag_WB_CDF_offset ); - } else { - SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset ); - } - - /* Get countour index */ - if( psDec->fs_kHz == 8 ) { - /* Less codevectors used in 8 khz mode */ - SKP_Silk_range_decoder( &Ixs[ 1 ], psRC, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset ); - } else { - /* Joint for 12, 16, and 24 khz */ - SKP_Silk_range_decoder( &Ixs[ 1 ], psRC, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset ); - } - - /* Decode pitch values */ - SKP_Silk_decode_pitch( Ixs[ 0 ], Ixs[ 1 ], psDecCtrl->pitchL, psDec->fs_kHz ); - - /********************/ - /* Decode LTP gains */ - /********************/ - /* Decode PERIndex value */ - SKP_Silk_range_decoder( &psDecCtrl->PERIndex, psRC, SKP_Silk_LTP_per_index_CDF, - SKP_Silk_LTP_per_index_CDF_offset ); - - /* Decode Codebook Index */ - cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ psDecCtrl->PERIndex ]; // set pointer to start of codebook - - for( k = 0; k < NB_SUBFR; k++ ) { - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_LTP_gain_CDF_ptrs[ psDecCtrl->PERIndex ], - SKP_Silk_LTP_gain_CDF_offsets[ psDecCtrl->PERIndex ] ); - - for( i = 0; i < LTP_ORDER; i++ ) { - psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( k, LTP_ORDER ) + i ] = cbk_ptr_Q14[ SKP_SMULBB( Ix, LTP_ORDER ) + i ]; - } - } - - /**********************/ - /* Decode LTP scaling */ - /**********************/ - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_LTPscale_CDF, SKP_Silk_LTPscale_offset ); - psDecCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ Ix ]; - } else { - SKP_memset( psDecCtrl->pitchL, 0, NB_SUBFR * sizeof( SKP_int ) ); - SKP_memset( psDecCtrl->LTPCoef_Q14, 0, NB_SUBFR * LTP_ORDER * sizeof( SKP_int16 ) ); - psDecCtrl->PERIndex = 0; - psDecCtrl->LTP_scale_Q14 = 0; - } - - /***************/ - /* Decode seed */ - /***************/ - SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_Seed_CDF, SKP_Silk_Seed_offset ); - psDecCtrl->Seed = ( SKP_int32 )Ix; - /*********************************************/ - /* Decode quantization indices of excitation */ - /*********************************************/ - SKP_Silk_decode_pulses( psRC, psDecCtrl, q, psDec->frame_length ); - - /*********************************************/ - /* Decode VAD flag */ - /*********************************************/ - SKP_Silk_range_decoder( &psDec->vadFlag, psRC, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset ); - - /**************************************/ - /* Decode Frame termination indicator */ - /**************************************/ - SKP_Silk_range_decoder( &psDec->FrameTermination, psRC, SKP_Silk_FrameTermination_CDF, SKP_Silk_FrameTermination_offset ); - - /****************************************/ - /* get number of bytes used so far */ - /****************************************/ - SKP_Silk_range_coder_get_length( psRC, &nBytesUsed ); - psDec->nBytesLeft = psRC->bufferLength - nBytesUsed; - if( psDec->nBytesLeft < 0 ) { - psRC->error = RANGE_CODER_READ_BEYOND_BUFFER; - } - - /****************************************/ - /* check remaining bits in last byte */ - /****************************************/ - if( psDec->nBytesLeft == 0 ) { - SKP_Silk_range_coder_check_after_decoding( psRC ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Decode parameters from payload */ +void SKP_Silk_decode_parameters( + SKP_Silk_decoder_state *psDec, /* I/O State */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int q[], /* O Excitation signal */ + const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ +) +{ + SKP_int i, k, Ix, fs_kHz_dec, nBytesUsed; + SKP_int Ixs[ NB_SUBFR ]; + SKP_int GainsIndices[ NB_SUBFR ]; + SKP_int NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ]; + SKP_int pNLSF_Q15[ MAX_LPC_ORDER ], pNLSF0_Q15[ MAX_LPC_ORDER ]; + const SKP_int16 *cbk_ptr_Q14; + const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL; + SKP_Silk_range_coder_state *psRC = &psDec->sRC; + + /************************/ + /* Decode sampling rate */ + /************************/ + /* only done for first frame of packet */ + if( psDec->nFramesDecoded == 0 ) { + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset ); + + /* check that sampling rate is supported */ + if( Ix < 0 || Ix > 3 ) { + psRC->error = RANGE_CODER_ILLEGAL_SAMPLING_RATE; + return; + } + fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ]; + SKP_Silk_decoder_set_fs( psDec, fs_kHz_dec ); + } + + /*******************************************/ + /* Decode signal type and quantizer offset */ + /*******************************************/ + if( psDec->nFramesDecoded == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset ); + } else { + /* condidtional coding */ + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ], + SKP_Silk_type_offset_CDF_offset ); + } + psDecCtrl->sigtype = SKP_RSHIFT( Ix, 1 ); + psDecCtrl->QuantOffsetType = Ix & 1; + psDec->typeOffsetPrev = Ix; + + /****************/ + /* Decode gains */ + /****************/ + /* first subframe */ + if( psDec->nFramesDecoded == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_decoder( &GainsIndices[ 0 ], psRC, SKP_Silk_gain_CDF[ psDecCtrl->sigtype ], SKP_Silk_gain_CDF_offset ); + } else { + /* condidtional coding */ + SKP_Silk_range_decoder( &GainsIndices[ 0 ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); + } + + /* remaining subframes */ + for( i = 1; i < NB_SUBFR; i++ ) { + SKP_Silk_range_decoder( &GainsIndices[ i ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset ); + } + + /* Dequant Gains */ + SKP_Silk_gains_dequant( psDecCtrl->Gains_Q16, GainsIndices, &psDec->LastGainIndex, psDec->nFramesDecoded ); + /****************/ + /* Decode NLSFs */ + /****************/ + /* Set pointer to NLSF VQ CB for the current signal type */ + psNLSF_CB = psDec->psNLSF_CB[ psDecCtrl->sigtype ]; + + /* Arithmetically decode NLSF path */ + SKP_Silk_range_decoder_multi( NLSFIndices, psRC, psNLSF_CB->StartPtr, psNLSF_CB->MiddleIx, psNLSF_CB->nStages ); + + /* From the NLSF path, decode an NLSF vector */ + SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, NLSFIndices, psDec->LPC_order ); + + /************************************/ + /* Decode NLSF interpolation factor */ + /************************************/ + SKP_Silk_range_decoder( &psDecCtrl->NLSFInterpCoef_Q2, psRC, SKP_Silk_NLSF_interpolation_factor_CDF, + SKP_Silk_NLSF_interpolation_factor_offset ); + + /* If just reset, e.g., because internal Fs changed, do not allow interpolation */ + /* improves the case of packet loss in the first frame after a switch */ + if( psDec->first_frame_after_reset == 1 ) { + psDecCtrl->NLSFInterpCoef_Q2 = 4; + } + + if( fullDecoding ) { + /* Convert NLSF parameters to AR prediction filter coefficients */ + SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order ); + + if( psDecCtrl->NLSFInterpCoef_Q2 < 4 ) { + /* Calculation of the interpolated NLSF0 vector from the interpolation factor, */ + /* the previous NLSF1, and the current NLSF1 */ + for( i = 0; i < psDec->LPC_order; i++ ) { + pNLSF0_Q15[ i ] = psDec->prevNLSF_Q15[ i ] + SKP_RSHIFT( SKP_MUL( psDecCtrl->NLSFInterpCoef_Q2, + ( pNLSF_Q15[ i ] - psDec->prevNLSF_Q15[ i ] ) ), 2 ); + } + + /* Convert NLSF parameters to AR prediction filter coefficients */ + SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 0 ], pNLSF0_Q15, psDec->LPC_order ); + } else { + /* Copy LPC coefficients for first half from second half */ + SKP_memcpy( psDecCtrl->PredCoef_Q12[ 0 ], psDecCtrl->PredCoef_Q12[ 1 ], + psDec->LPC_order * sizeof( SKP_int16 ) ); + } + } + + SKP_memcpy( psDec->prevNLSF_Q15, pNLSF_Q15, psDec->LPC_order * sizeof( SKP_int ) ); + + /* After a packet loss do BWE of LPC coefs */ + if( psDec->lossCnt ) { + SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 0 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); + SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); + } + + if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) { + /*********************/ + /* Decode pitch lags */ + /*********************/ + /* Get lag index */ + if( psDec->fs_kHz == 8 ) { + SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_NB_CDF, SKP_Silk_pitch_lag_NB_CDF_offset ); + } else if( psDec->fs_kHz == 12 ) { + SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_MB_CDF, SKP_Silk_pitch_lag_MB_CDF_offset ); + } else if( psDec->fs_kHz == 16 ) { + SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_WB_CDF, SKP_Silk_pitch_lag_WB_CDF_offset ); + } else { + SKP_Silk_range_decoder( &Ixs[ 0 ], psRC, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset ); + } + + /* Get countour index */ + if( psDec->fs_kHz == 8 ) { + /* Less codevectors used in 8 khz mode */ + SKP_Silk_range_decoder( &Ixs[ 1 ], psRC, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset ); + } else { + /* Joint for 12, 16, and 24 khz */ + SKP_Silk_range_decoder( &Ixs[ 1 ], psRC, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset ); + } + + /* Decode pitch values */ + SKP_Silk_decode_pitch( Ixs[ 0 ], Ixs[ 1 ], psDecCtrl->pitchL, psDec->fs_kHz ); + + /********************/ + /* Decode LTP gains */ + /********************/ + /* Decode PERIndex value */ + SKP_Silk_range_decoder( &psDecCtrl->PERIndex, psRC, SKP_Silk_LTP_per_index_CDF, + SKP_Silk_LTP_per_index_CDF_offset ); + + /* Decode Codebook Index */ + cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ psDecCtrl->PERIndex ]; // set pointer to start of codebook + + for( k = 0; k < NB_SUBFR; k++ ) { + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_LTP_gain_CDF_ptrs[ psDecCtrl->PERIndex ], + SKP_Silk_LTP_gain_CDF_offsets[ psDecCtrl->PERIndex ] ); + + for( i = 0; i < LTP_ORDER; i++ ) { + psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( k, LTP_ORDER ) + i ] = cbk_ptr_Q14[ SKP_SMULBB( Ix, LTP_ORDER ) + i ]; + } + } + + /**********************/ + /* Decode LTP scaling */ + /**********************/ + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_LTPscale_CDF, SKP_Silk_LTPscale_offset ); + psDecCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ Ix ]; + } else { + SKP_memset( psDecCtrl->pitchL, 0, NB_SUBFR * sizeof( SKP_int ) ); + SKP_memset( psDecCtrl->LTPCoef_Q14, 0, NB_SUBFR * LTP_ORDER * sizeof( SKP_int16 ) ); + psDecCtrl->PERIndex = 0; + psDecCtrl->LTP_scale_Q14 = 0; + } + + /***************/ + /* Decode seed */ + /***************/ + SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_Seed_CDF, SKP_Silk_Seed_offset ); + psDecCtrl->Seed = ( SKP_int32 )Ix; + /*********************************************/ + /* Decode quantization indices of excitation */ + /*********************************************/ + SKP_Silk_decode_pulses( psRC, psDecCtrl, q, psDec->frame_length ); + + /*********************************************/ + /* Decode VAD flag */ + /*********************************************/ + SKP_Silk_range_decoder( &psDec->vadFlag, psRC, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset ); + + /**************************************/ + /* Decode Frame termination indicator */ + /**************************************/ + SKP_Silk_range_decoder( &psDec->FrameTermination, psRC, SKP_Silk_FrameTermination_CDF, SKP_Silk_FrameTermination_offset ); + + /****************************************/ + /* get number of bytes used so far */ + /****************************************/ + SKP_Silk_range_coder_get_length( psRC, &nBytesUsed ); + psDec->nBytesLeft = psRC->bufferLength - nBytesUsed; + if( psDec->nBytesLeft < 0 ) { + psRC->error = RANGE_CODER_READ_BEYOND_BUFFER; + } + + /****************************************/ + /* check remaining bits in last byte */ + /****************************************/ + if( psDec->nBytesLeft == 0 ) { + SKP_Silk_range_coder_check_after_decoding( psRC ); + } +} diff --git a/jni/silk/src/SKP_Silk_decode_parameters_v4.c b/app/src/main/jni/silk/src/SKP_Silk_decode_parameters_v4.c similarity index 98% rename from jni/silk/src/SKP_Silk_decode_parameters_v4.c rename to app/src/main/jni/silk/src/SKP_Silk_decode_parameters_v4.c index 2770b1b..0a3e810 100644 --- a/jni/silk/src/SKP_Silk_decode_parameters_v4.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decode_parameters_v4.c @@ -1,157 +1,157 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Decode parameters from payload */ -void SKP_Silk_decode_parameters_v4( - SKP_Silk_decoder_state *psDec, /* I/O State */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int q[ MAX_FRAME_LENGTH ], /* O Excitation signal */ - const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ -) -{ - SKP_int i, k, Ix, nBytesUsed; - SKP_int pNLSF_Q15[ MAX_LPC_ORDER ], pNLSF0_Q15[ MAX_LPC_ORDER ]; - const SKP_int16 *cbk_ptr_Q14; - const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL; - SKP_Silk_range_coder_state *psRC = &psDec->sRC; - - psDec->FrameTermination = SKP_SILK_MORE_FRAMES; - psDecCtrl->sigtype = psDec->sigtype[ psDec->nFramesDecoded ]; - psDecCtrl->QuantOffsetType = psDec->QuantOffsetType[ psDec->nFramesDecoded ]; - psDec->vadFlag = psDec->vadFlagBuf[ psDec->nFramesDecoded ]; - psDecCtrl->NLSFInterpCoef_Q2 = psDec->NLSFInterpCoef_Q2[ psDec->nFramesDecoded ]; - psDecCtrl->Seed = psDec->Seed[ psDec->nFramesDecoded ]; - - /* Dequant Gains */ - SKP_Silk_gains_dequant( psDecCtrl->Gains_Q16, psDec->GainsIndices[ psDec->nFramesDecoded ], &psDec->LastGainIndex, psDec->nFramesDecoded ); - /****************/ - /* Decode NLSFs */ - /****************/ - - /* Set pointer to NLSF VQ CB for the current signal type */ - psNLSF_CB = psDec->psNLSF_CB[ psDecCtrl->sigtype ]; - - /* From the NLSF path, decode an NLSF vector */ - SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, psDec->NLSFIndices[ psDec->nFramesDecoded ], psDec->LPC_order ); - - /* Convert NLSF parameters to AR prediction filter coefficients */ - SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order ); - - /* If just reset, e.g., because internal Fs changed, do not allow interpolation */ - /* improves the case of packet loss in the first frame after a switch */ - if( psDec->first_frame_after_reset == 1 ) { - psDecCtrl->NLSFInterpCoef_Q2 = 4; - } - - if( psDecCtrl->NLSFInterpCoef_Q2 < 4 ) { - /* Calculation of the interpolated NLSF0 vector from the interpolation factor, */ - /* the previous NLSF1, and the current NLSF1 */ - for( i = 0; i < psDec->LPC_order; i++ ) { - pNLSF0_Q15[ i ] = psDec->prevNLSF_Q15[ i ] + SKP_RSHIFT( SKP_MUL( psDecCtrl->NLSFInterpCoef_Q2, - ( pNLSF_Q15[ i ] - psDec->prevNLSF_Q15[ i ] ) ), 2 ); - } - - /* Convert NLSF parameters to AR prediction filter coefficients */ - SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 0 ], pNLSF0_Q15, psDec->LPC_order ); - } else { - /* Copy LPC coefficients for first half from second half */ - SKP_memcpy( psDecCtrl->PredCoef_Q12[ 0 ], psDecCtrl->PredCoef_Q12[ 1 ], - psDec->LPC_order * sizeof( SKP_int16 ) ); - } - - SKP_memcpy( psDec->prevNLSF_Q15, pNLSF_Q15, psDec->LPC_order * sizeof( SKP_int ) ); - - /* After a packet loss do BWE of LPC coefs */ - if( psDec->lossCnt ) { - SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 0 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); - SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); - } - - if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) { - /*********************/ - /* Decode pitch lags */ - /*********************/ - - /* Decode pitch values */ - SKP_Silk_decode_pitch( psDec->lagIndex[ psDec->nFramesDecoded ], - psDec->contourIndex[ psDec->nFramesDecoded ], psDecCtrl->pitchL, psDec->fs_kHz ); - - /********************/ - /* Decode LTP gains */ - /********************/ - psDecCtrl->PERIndex = psDec->PERIndex[ psDec->nFramesDecoded ]; - - /* Decode Codebook Index */ - cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ psDecCtrl->PERIndex ]; /* set pointer to start of codebook */ - - for( k = 0; k < NB_SUBFR; k++ ) { - Ix = psDec->LTPIndex[ psDec->nFramesDecoded ][ k ]; - for( i = 0; i < LTP_ORDER; i++ ) { - psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( k, LTP_ORDER ) + i ] = cbk_ptr_Q14[ SKP_SMULBB( Ix, LTP_ORDER ) + i ]; - } - } - - /**********************/ - /* Decode LTP scaling */ - /**********************/ - Ix = psDec->LTP_scaleIndex[ psDec->nFramesDecoded ]; - psDecCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ Ix ]; - } else { - SKP_memset( psDecCtrl->pitchL, 0, NB_SUBFR * sizeof( SKP_int ) ); - SKP_memset( psDecCtrl->LTPCoef_Q14, 0, NB_SUBFR * LTP_ORDER * sizeof( SKP_int16 ) ); - psDecCtrl->PERIndex = 0; - psDecCtrl->LTP_scale_Q14 = 0; - } - - /*********************************************/ - /* Decode quantization indices of excitation */ - /*********************************************/ - SKP_Silk_decode_pulses( psRC, psDecCtrl, q, psDec->frame_length ); - - /****************************************/ - /* get number of bytes used so far */ - /****************************************/ - SKP_Silk_range_coder_get_length( psRC, &nBytesUsed ); - psDec->nBytesLeft = psRC->bufferLength - nBytesUsed; - if( psDec->nBytesLeft < 0 ) { - psRC->error = RANGE_CODER_READ_BEYOND_BUFFER; - } - - /****************************************/ - /* check remaining bits in last byte */ - /****************************************/ - if( psDec->nBytesLeft == 0 ) { - SKP_Silk_range_coder_check_after_decoding( psRC ); - } - - if( psDec->nFramesInPacket == (psDec->nFramesDecoded + 1)) { - /* To indicate the packet has been fully decoded */ - psDec->FrameTermination = SKP_SILK_LAST_FRAME; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Decode parameters from payload */ +void SKP_Silk_decode_parameters_v4( + SKP_Silk_decoder_state *psDec, /* I/O State */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int q[ MAX_FRAME_LENGTH ], /* O Excitation signal */ + const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ +) +{ + SKP_int i, k, Ix, nBytesUsed; + SKP_int pNLSF_Q15[ MAX_LPC_ORDER ], pNLSF0_Q15[ MAX_LPC_ORDER ]; + const SKP_int16 *cbk_ptr_Q14; + const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL; + SKP_Silk_range_coder_state *psRC = &psDec->sRC; + + psDec->FrameTermination = SKP_SILK_MORE_FRAMES; + psDecCtrl->sigtype = psDec->sigtype[ psDec->nFramesDecoded ]; + psDecCtrl->QuantOffsetType = psDec->QuantOffsetType[ psDec->nFramesDecoded ]; + psDec->vadFlag = psDec->vadFlagBuf[ psDec->nFramesDecoded ]; + psDecCtrl->NLSFInterpCoef_Q2 = psDec->NLSFInterpCoef_Q2[ psDec->nFramesDecoded ]; + psDecCtrl->Seed = psDec->Seed[ psDec->nFramesDecoded ]; + + /* Dequant Gains */ + SKP_Silk_gains_dequant( psDecCtrl->Gains_Q16, psDec->GainsIndices[ psDec->nFramesDecoded ], &psDec->LastGainIndex, psDec->nFramesDecoded ); + /****************/ + /* Decode NLSFs */ + /****************/ + + /* Set pointer to NLSF VQ CB for the current signal type */ + psNLSF_CB = psDec->psNLSF_CB[ psDecCtrl->sigtype ]; + + /* From the NLSF path, decode an NLSF vector */ + SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, psDec->NLSFIndices[ psDec->nFramesDecoded ], psDec->LPC_order ); + + /* Convert NLSF parameters to AR prediction filter coefficients */ + SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order ); + + /* If just reset, e.g., because internal Fs changed, do not allow interpolation */ + /* improves the case of packet loss in the first frame after a switch */ + if( psDec->first_frame_after_reset == 1 ) { + psDecCtrl->NLSFInterpCoef_Q2 = 4; + } + + if( psDecCtrl->NLSFInterpCoef_Q2 < 4 ) { + /* Calculation of the interpolated NLSF0 vector from the interpolation factor, */ + /* the previous NLSF1, and the current NLSF1 */ + for( i = 0; i < psDec->LPC_order; i++ ) { + pNLSF0_Q15[ i ] = psDec->prevNLSF_Q15[ i ] + SKP_RSHIFT( SKP_MUL( psDecCtrl->NLSFInterpCoef_Q2, + ( pNLSF_Q15[ i ] - psDec->prevNLSF_Q15[ i ] ) ), 2 ); + } + + /* Convert NLSF parameters to AR prediction filter coefficients */ + SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 0 ], pNLSF0_Q15, psDec->LPC_order ); + } else { + /* Copy LPC coefficients for first half from second half */ + SKP_memcpy( psDecCtrl->PredCoef_Q12[ 0 ], psDecCtrl->PredCoef_Q12[ 1 ], + psDec->LPC_order * sizeof( SKP_int16 ) ); + } + + SKP_memcpy( psDec->prevNLSF_Q15, pNLSF_Q15, psDec->LPC_order * sizeof( SKP_int ) ); + + /* After a packet loss do BWE of LPC coefs */ + if( psDec->lossCnt ) { + SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 0 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); + SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 ); + } + + if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) { + /*********************/ + /* Decode pitch lags */ + /*********************/ + + /* Decode pitch values */ + SKP_Silk_decode_pitch( psDec->lagIndex[ psDec->nFramesDecoded ], + psDec->contourIndex[ psDec->nFramesDecoded ], psDecCtrl->pitchL, psDec->fs_kHz ); + + /********************/ + /* Decode LTP gains */ + /********************/ + psDecCtrl->PERIndex = psDec->PERIndex[ psDec->nFramesDecoded ]; + + /* Decode Codebook Index */ + cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ psDecCtrl->PERIndex ]; /* set pointer to start of codebook */ + + for( k = 0; k < NB_SUBFR; k++ ) { + Ix = psDec->LTPIndex[ psDec->nFramesDecoded ][ k ]; + for( i = 0; i < LTP_ORDER; i++ ) { + psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( k, LTP_ORDER ) + i ] = cbk_ptr_Q14[ SKP_SMULBB( Ix, LTP_ORDER ) + i ]; + } + } + + /**********************/ + /* Decode LTP scaling */ + /**********************/ + Ix = psDec->LTP_scaleIndex[ psDec->nFramesDecoded ]; + psDecCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ Ix ]; + } else { + SKP_memset( psDecCtrl->pitchL, 0, NB_SUBFR * sizeof( SKP_int ) ); + SKP_memset( psDecCtrl->LTPCoef_Q14, 0, NB_SUBFR * LTP_ORDER * sizeof( SKP_int16 ) ); + psDecCtrl->PERIndex = 0; + psDecCtrl->LTP_scale_Q14 = 0; + } + + /*********************************************/ + /* Decode quantization indices of excitation */ + /*********************************************/ + SKP_Silk_decode_pulses( psRC, psDecCtrl, q, psDec->frame_length ); + + /****************************************/ + /* get number of bytes used so far */ + /****************************************/ + SKP_Silk_range_coder_get_length( psRC, &nBytesUsed ); + psDec->nBytesLeft = psRC->bufferLength - nBytesUsed; + if( psDec->nBytesLeft < 0 ) { + psRC->error = RANGE_CODER_READ_BEYOND_BUFFER; + } + + /****************************************/ + /* check remaining bits in last byte */ + /****************************************/ + if( psDec->nBytesLeft == 0 ) { + SKP_Silk_range_coder_check_after_decoding( psRC ); + } + + if( psDec->nFramesInPacket == (psDec->nFramesDecoded + 1)) { + /* To indicate the packet has been fully decoded */ + psDec->FrameTermination = SKP_SILK_LAST_FRAME; + } +} diff --git a/jni/silk/src/SKP_Silk_decode_pulses.c b/app/src/main/jni/silk/src/SKP_Silk_decode_pulses.c similarity index 98% rename from jni/silk/src/SKP_Silk_decode_pulses.c rename to app/src/main/jni/silk/src/SKP_Silk_decode_pulses.c index 402228a..653c899 100644 --- a/jni/silk/src/SKP_Silk_decode_pulses.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decode_pulses.c @@ -1,105 +1,105 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/*********************************************/ -/* Decode quantization indices of excitation */ -/*********************************************/ -void SKP_Silk_decode_pulses( - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int q[], /* O Excitation signal */ - const SKP_int frame_length /* I Frame length (preliminary) */ -) -{ - SKP_int i, j, k, iter, abs_q, nLS, bit; - SKP_int sum_pulses[ MAX_NB_SHELL_BLOCKS ], nLshifts[ MAX_NB_SHELL_BLOCKS ]; - SKP_int *pulses_ptr; - const SKP_uint16 *cdf_ptr; - - /*********************/ - /* Decode rate level */ - /*********************/ - SKP_Silk_range_decoder( &psDecCtrl->RateLevelIndex, psRC, - SKP_Silk_rate_levels_CDF[ psDecCtrl->sigtype ], SKP_Silk_rate_levels_CDF_offset ); - - /* Calculate number of shell blocks */ - iter = frame_length / SHELL_CODEC_FRAME_LENGTH; - - /***************************************************/ - /* Sum-Weighted-Pulses Decoding */ - /***************************************************/ - cdf_ptr = SKP_Silk_pulses_per_block_CDF[ psDecCtrl->RateLevelIndex ]; - for( i = 0; i < iter; i++ ) { - nLshifts[ i ] = 0; - SKP_Silk_range_decoder( &sum_pulses[ i ], psRC, cdf_ptr, SKP_Silk_pulses_per_block_CDF_offset ); - - /* LSB indication */ - while( sum_pulses[ i ] == ( MAX_PULSES + 1 ) ) { - nLshifts[ i ]++; - SKP_Silk_range_decoder( &sum_pulses[ i ], psRC, - SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ], SKP_Silk_pulses_per_block_CDF_offset ); - } - } - - /***************************************************/ - /* Shell decoding */ - /***************************************************/ - for( i = 0; i < iter; i++ ) { - if( sum_pulses[ i ] > 0 ) { - SKP_Silk_shell_decoder( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], psRC, sum_pulses[ i ] ); - } else { - SKP_memset( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof( SKP_int ) ); - } - } - - /***************************************************/ - /* LSB Decoding */ - /***************************************************/ - for( i = 0; i < iter; i++ ) { - if( nLshifts[ i ] > 0 ) { - nLS = nLshifts[ i ]; - pulses_ptr = &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ]; - for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { - abs_q = pulses_ptr[ k ]; - for( j = 0; j < nLS; j++ ) { - abs_q = SKP_LSHIFT( abs_q, 1 ); - SKP_Silk_range_decoder( &bit, psRC, SKP_Silk_lsb_CDF, 1 ); - abs_q += bit; - } - pulses_ptr[ k ] = abs_q; - } - } - } - - /****************************************/ - /* Decode and add signs to pulse signal */ - /****************************************/ - SKP_Silk_decode_signs( psRC, q, frame_length, psDecCtrl->sigtype, - psDecCtrl->QuantOffsetType, psDecCtrl->RateLevelIndex); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/*********************************************/ +/* Decode quantization indices of excitation */ +/*********************************************/ +void SKP_Silk_decode_pulses( + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int q[], /* O Excitation signal */ + const SKP_int frame_length /* I Frame length (preliminary) */ +) +{ + SKP_int i, j, k, iter, abs_q, nLS, bit; + SKP_int sum_pulses[ MAX_NB_SHELL_BLOCKS ], nLshifts[ MAX_NB_SHELL_BLOCKS ]; + SKP_int *pulses_ptr; + const SKP_uint16 *cdf_ptr; + + /*********************/ + /* Decode rate level */ + /*********************/ + SKP_Silk_range_decoder( &psDecCtrl->RateLevelIndex, psRC, + SKP_Silk_rate_levels_CDF[ psDecCtrl->sigtype ], SKP_Silk_rate_levels_CDF_offset ); + + /* Calculate number of shell blocks */ + iter = frame_length / SHELL_CODEC_FRAME_LENGTH; + + /***************************************************/ + /* Sum-Weighted-Pulses Decoding */ + /***************************************************/ + cdf_ptr = SKP_Silk_pulses_per_block_CDF[ psDecCtrl->RateLevelIndex ]; + for( i = 0; i < iter; i++ ) { + nLshifts[ i ] = 0; + SKP_Silk_range_decoder( &sum_pulses[ i ], psRC, cdf_ptr, SKP_Silk_pulses_per_block_CDF_offset ); + + /* LSB indication */ + while( sum_pulses[ i ] == ( MAX_PULSES + 1 ) ) { + nLshifts[ i ]++; + SKP_Silk_range_decoder( &sum_pulses[ i ], psRC, + SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ], SKP_Silk_pulses_per_block_CDF_offset ); + } + } + + /***************************************************/ + /* Shell decoding */ + /***************************************************/ + for( i = 0; i < iter; i++ ) { + if( sum_pulses[ i ] > 0 ) { + SKP_Silk_shell_decoder( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], psRC, sum_pulses[ i ] ); + } else { + SKP_memset( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof( SKP_int ) ); + } + } + + /***************************************************/ + /* LSB Decoding */ + /***************************************************/ + for( i = 0; i < iter; i++ ) { + if( nLshifts[ i ] > 0 ) { + nLS = nLshifts[ i ]; + pulses_ptr = &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ]; + for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { + abs_q = pulses_ptr[ k ]; + for( j = 0; j < nLS; j++ ) { + abs_q = SKP_LSHIFT( abs_q, 1 ); + SKP_Silk_range_decoder( &bit, psRC, SKP_Silk_lsb_CDF, 1 ); + abs_q += bit; + } + pulses_ptr[ k ] = abs_q; + } + } + } + + /****************************************/ + /* Decode and add signs to pulse signal */ + /****************************************/ + SKP_Silk_decode_signs( psRC, q, frame_length, psDecCtrl->sigtype, + psDecCtrl->QuantOffsetType, psDecCtrl->RateLevelIndex); +} diff --git a/jni/silk/src/SKP_Silk_decoder_set_fs.c b/app/src/main/jni/silk/src/SKP_Silk_decoder_set_fs.c similarity index 98% rename from jni/silk/src/SKP_Silk_decoder_set_fs.c rename to app/src/main/jni/silk/src/SKP_Silk_decoder_set_fs.c index 824a84b..274ed93 100644 --- a/jni/silk/src/SKP_Silk_decoder_set_fs.c +++ b/app/src/main/jni/silk/src/SKP_Silk_decoder_set_fs.c @@ -1,81 +1,81 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Set decoder sampling rate */ -void SKP_Silk_decoder_set_fs( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state pointer */ - SKP_int fs_kHz /* I Sampling frequency (kHz) */ -) -{ - if( psDec->fs_kHz != fs_kHz ) { - psDec->fs_kHz = fs_kHz; - psDec->frame_length = SKP_SMULBB( FRAME_LENGTH_MS, fs_kHz ); - psDec->subfr_length = SKP_SMULBB( FRAME_LENGTH_MS / NB_SUBFR, fs_kHz ); - if( psDec->fs_kHz == 8 ) { - psDec->LPC_order = MIN_LPC_ORDER; - psDec->psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_10; - psDec->psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_10; - } else { - psDec->LPC_order = MAX_LPC_ORDER; - psDec->psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_16; - psDec->psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_16; - } - /* Reset part of the decoder state */ - SKP_memset( psDec->sLPC_Q14, 0, MAX_LPC_ORDER * sizeof( SKP_int32 ) ); - SKP_memset( psDec->outBuf, 0, MAX_FRAME_LENGTH * sizeof( SKP_int16 ) ); - SKP_memset( psDec->prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) ); - - psDec->sLTP_buf_idx = 0; - psDec->lagPrev = 100; - psDec->LastGainIndex = 1; - psDec->prev_sigtype = 0; - psDec->first_frame_after_reset = 1; - - if( fs_kHz == 24 ) { - psDec->HP_A = SKP_Silk_Dec_A_HP_24; - psDec->HP_B = SKP_Silk_Dec_B_HP_24; - } else if( fs_kHz == 16 ) { - psDec->HP_A = SKP_Silk_Dec_A_HP_16; - psDec->HP_B = SKP_Silk_Dec_B_HP_16; - } else if( fs_kHz == 12 ) { - psDec->HP_A = SKP_Silk_Dec_A_HP_12; - psDec->HP_B = SKP_Silk_Dec_B_HP_12; - } else if( fs_kHz == 8 ) { - psDec->HP_A = SKP_Silk_Dec_A_HP_8; - psDec->HP_B = SKP_Silk_Dec_B_HP_8; - } else { - /* unsupported sampling rate */ - SKP_assert( 0 ); - } - } - - /* Check that settings are valid */ - SKP_assert( psDec->frame_length > 0 && psDec->frame_length <= MAX_FRAME_LENGTH ); -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Set decoder sampling rate */ +void SKP_Silk_decoder_set_fs( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state pointer */ + SKP_int fs_kHz /* I Sampling frequency (kHz) */ +) +{ + if( psDec->fs_kHz != fs_kHz ) { + psDec->fs_kHz = fs_kHz; + psDec->frame_length = SKP_SMULBB( FRAME_LENGTH_MS, fs_kHz ); + psDec->subfr_length = SKP_SMULBB( FRAME_LENGTH_MS / NB_SUBFR, fs_kHz ); + if( psDec->fs_kHz == 8 ) { + psDec->LPC_order = MIN_LPC_ORDER; + psDec->psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_10; + psDec->psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_10; + } else { + psDec->LPC_order = MAX_LPC_ORDER; + psDec->psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_16; + psDec->psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_16; + } + /* Reset part of the decoder state */ + SKP_memset( psDec->sLPC_Q14, 0, MAX_LPC_ORDER * sizeof( SKP_int32 ) ); + SKP_memset( psDec->outBuf, 0, MAX_FRAME_LENGTH * sizeof( SKP_int16 ) ); + SKP_memset( psDec->prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) ); + + psDec->sLTP_buf_idx = 0; + psDec->lagPrev = 100; + psDec->LastGainIndex = 1; + psDec->prev_sigtype = 0; + psDec->first_frame_after_reset = 1; + + if( fs_kHz == 24 ) { + psDec->HP_A = SKP_Silk_Dec_A_HP_24; + psDec->HP_B = SKP_Silk_Dec_B_HP_24; + } else if( fs_kHz == 16 ) { + psDec->HP_A = SKP_Silk_Dec_A_HP_16; + psDec->HP_B = SKP_Silk_Dec_B_HP_16; + } else if( fs_kHz == 12 ) { + psDec->HP_A = SKP_Silk_Dec_A_HP_12; + psDec->HP_B = SKP_Silk_Dec_B_HP_12; + } else if( fs_kHz == 8 ) { + psDec->HP_A = SKP_Silk_Dec_A_HP_8; + psDec->HP_B = SKP_Silk_Dec_B_HP_8; + } else { + /* unsupported sampling rate */ + SKP_assert( 0 ); + } + } + + /* Check that settings are valid */ + SKP_assert( psDec->frame_length > 0 && psDec->frame_length <= MAX_FRAME_LENGTH ); +} + diff --git a/jni/silk/src/SKP_Silk_define.h b/app/src/main/jni/silk/src/SKP_Silk_define.h similarity index 97% rename from jni/silk/src/SKP_Silk_define.h rename to app/src/main/jni/silk/src/SKP_Silk_define.h index 27d20ca..576b940 100644 --- a/jni/silk/src/SKP_Silk_define.h +++ b/app/src/main/jni/silk/src/SKP_Silk_define.h @@ -1,330 +1,330 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_DEFINE_H -#define SKP_SILK_DEFINE_H - -#include "SKP_Silk_errors.h" -#include "SKP_Silk_typedef.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - - -#define MAX_FRAMES_PER_PACKET 5 -#define BIT_STREAM_V3 3 -#define BIT_STREAM_V4 4 -#define USE_BIT_STREAM_V BIT_STREAM_V3 // Should be moved to a API call - - -/* MAX DELTA LAG used for multiframe packets */ -#define MAX_DELTA_LAG 10 - -/* Lower limit on bitrate for each mode */ -#define MIN_TARGET_RATE_NB_BPS 5000 -#define MIN_TARGET_RATE_MB_BPS 7000 -#define MIN_TARGET_RATE_WB_BPS 8000 -#define MIN_TARGET_RATE_SWB_BPS 20000 - -/* Transition bitrates between modes */ -#define SWB2WB_BITRATE_BPS 30000 -#define SWB2WB_BITRATE_BPS_INITIAL 25000 -#define WB2SWB_BITRATE_BPS 35000 -#define WB2MB_BITRATE_BPS 15000 -#define MB2WB_BITRATE_BPS 20000 -#define MB2NB_BITRATE_BPS 10000 -#define NB2MB_BITRATE_BPS 14000 - -/* Integration/hysteresis threshold for lowering internal sample frequency */ -/* 30000000 -> 6 sec if bitrate is 5000 bps below limit; 3 sec if bitrate is 10000 bps below limit */ -#define ACCUM_BITS_DIFF_THRESHOLD 30000000 -#define TARGET_RATE_TAB_SZ 8 - -/* DTX settings */ -#define NO_SPEECH_FRAMES_BEFORE_DTX 5 /* eq 100 ms */ -#define MAX_CONSECUTIVE_DTX 20 /* eq 400 ms */ - -#define USE_LBRR 1 - -/* Amount of concecutive no FEC packets before telling JB */ -#define NO_LBRR_THRES 10 - -/* Maximum delay between real packet and LBRR packet */ -#define MAX_LBRR_DELAY 2 -#define LBRR_IDX_MASK 1 - -#define INBAND_FEC_MIN_RATE_BPS 18000 /* Dont use inband FEC below this total target rate */ -#define LBRR_LOSS_THRES 2 /* Start adding LBRR at this loss rate (needs tuning) */ - -/* LBRR usage defines */ -#define SKP_SILK_NO_LBRR 0 /* No LBRR information for this packet */ -#define SKP_SILK_ADD_LBRR_TO_PLUS1 1 /* Add LBRR for this packet to packet n + 1 */ -#define SKP_SILK_ADD_LBRR_TO_PLUS2 2 /* Add LBRR for this packet to packet n + 2 */ - -/* Frame termination indicator defines */ -#define SKP_SILK_LAST_FRAME 0 /* Last frames in packet */ -#define SKP_SILK_MORE_FRAMES 1 /* More frames to follow this one */ -#define SKP_SILK_LBRR_VER1 2 /* LBRR information from packet n - 1 */ -#define SKP_SILK_LBRR_VER2 3 /* LBRR information from packet n - 2 */ -#define SKP_SILK_EXT_LAYER 4 /* Extension layers added */ - -/* Number of Second order Sections for SWB detection HP filter */ -#define NB_SOS 3 -#define HP_8_KHZ_THRES 10 /* average energy per sample, above 8 kHz */ -#define CONCEC_SWB_SMPLS_THRES 480 * 15 /* 300 ms */ -#define WB_DETECT_ACTIVE_SPEECH_MS_THRES 15000 /* ms of active speech needed for WB detection */ - -/* Low complexity setting */ -#ifdef EMBEDDED_OPT -# define LOW_COMPLEXITY_ONLY 1 -#else -# define LOW_COMPLEXITY_ONLY 0 -#endif - -/* Activate bandwidth transition filtering for mode switching */ -#ifdef EMBEDDED_OPT -# define SWITCH_TRANSITION_FILTERING 0 -#else -#ifndef FORCE_FS_KHZ -# define SWITCH_TRANSITION_FILTERING 1 -#else -# define SWITCH_TRANSITION_FILTERING 0 -#endif -#endif - -/* Decoder Parameters */ -#define DEC_HP_ORDER 2 - -/* Maximum sampling frequency, should be 16 for embedded */ -#define MAX_FS_KHZ 24 - -/* Signal Types used by silk */ -#define SIG_TYPE_VOICED 0 -#define SIG_TYPE_UNVOICED 1 - -/* VAD Types used by silk */ -#define NO_VOICE_ACTIVITY 0 -#define VOICE_ACTIVITY 1 - -/* number of samples per frame */ -#define FRAME_LENGTH_MS 20 /* 20 ms */ -#define MAX_FRAME_LENGTH (FRAME_LENGTH_MS * MAX_FS_KHZ) - -/* number of lookahead samples for pitch analysis */ -#define LA_PITCH_MS 3 -#define LA_PITCH_MAX (LA_PITCH_MS * MAX_FS_KHZ) - -/* number of lookahead samples for noise shape analysis */ -#define LA_SHAPE_MS 5 -#define LA_SHAPE_MAX (LA_SHAPE_MS * MAX_FS_KHZ) - -/* Order of LPC used in find pitch */ -#define FIND_PITCH_LPC_ORDER_MAX 16 - -/* Length of LPC window used in find pitch */ -#define FIND_PITCH_LPC_WIN_MS (30 + (LA_PITCH_MS << 1)) -#define FIND_PITCH_LPC_WIN_MAX (FIND_PITCH_LPC_WIN_MS * MAX_FS_KHZ) - -#define PITCH_EST_COMPLEXITY_HC_MODE SigProc_PITCH_EST_MAX_COMPLEX -#define PITCH_EST_COMPLEXITY_MC_MODE SigProc_PITCH_EST_MID_COMPLEX -#define PITCH_EST_COMPLEXITY_LC_MODE SigProc_PITCH_EST_MIN_COMPLEX - - -/* Max number of bytes in payload output buffer (may contain multiple frames) */ -#define MAX_ARITHM_BYTES 1024 - -#define RANGE_CODER_WRITE_BEYOND_BUFFER -1 -#define RANGE_CODER_CDF_OUT_OF_RANGE -2 -#define RANGE_CODER_NORMALIZATION_FAILED -3 -#define RANGE_CODER_ZERO_INTERVAL_WIDTH -4 -#define RANGE_CODER_DECODER_CHECK_FAILED -5 -#define RANGE_CODER_READ_BEYOND_BUFFER -6 -#define RANGE_CODER_ILLEGAL_SAMPLING_RATE -7 -#define RANGE_CODER_DEC_PAYLOAD_TOO_LONG -8 - -/* dB level of lowest gain quantization level */ -#define MIN_QGAIN_DB 6 -/* dB level of highest gain quantization level */ -#define MAX_QGAIN_DB 86 -/* Number of gain quantization levels */ -#define N_LEVELS_QGAIN 64 -/* Max increase in gain quantization index */ -#define MAX_DELTA_GAIN_QUANT 40 -/* Max decrease in gain quantization index */ -#define MIN_DELTA_GAIN_QUANT -4 - -/* Quantization offsets (multiples of 4) */ -#define OFFSET_VL_Q10 32 -#define OFFSET_VH_Q10 100 -#define OFFSET_UVL_Q10 100 -#define OFFSET_UVH_Q10 256 - -/* Maximum numbers of iterations used to stabilize a LPC vector */ -#define MAX_LPC_STABILIZE_ITERATIONS 20 - -#define MAX_LPC_ORDER 16 -#define MIN_LPC_ORDER 10 - -/* Find Pred Coef defines */ -#define LTP_ORDER 5 - -/* LTP quantization settings */ -#define NB_LTP_CBKS 3 - -/* Number of subframes */ -#define NB_SUBFR 4 - -/* Flag to use harmonic noise shaping */ -#define USE_HARM_SHAPING 1 - -/* Max LPC order of noise shaping filters */ -#define SHAPE_LPC_ORDER_MAX 16 - -#define HARM_SHAPE_FIR_TAPS 3 - -/* Length of LPC window used in noise shape analysis */ -#define SHAPE_LPC_WIN_MS 15 -#define SHAPE_LPC_WIN_16_KHZ (SHAPE_LPC_WIN_MS * 16) -#define SHAPE_LPC_WIN_24_KHZ (SHAPE_LPC_WIN_MS * 24) -#define SHAPE_LPC_WIN_MAX (SHAPE_LPC_WIN_MS * MAX_FS_KHZ) - -/* Maximum number of delayed decision states */ -#define DEL_DEC_STATES_MAX 4 - -#define LTP_BUF_LENGTH 512 -#define LTP_MASK (LTP_BUF_LENGTH - 1) - -#define DECISION_DELAY 32 -#define DECISION_DELAY_MASK (DECISION_DELAY - 1) - -/* number of subframes for excitation entropy coding */ -#define SHELL_CODEC_FRAME_LENGTH 16 -#define MAX_NB_SHELL_BLOCKS (MAX_FRAME_LENGTH / SHELL_CODEC_FRAME_LENGTH) - -/* number of rate levels, for entropy coding of excitation */ -#define N_RATE_LEVELS 10 - -/* maximum sum of pulses per shell coding frame */ -#define MAX_PULSES 18 - -#define MAX_MATRIX_SIZE MAX_LPC_ORDER /* Max of LPC Order and LTP order */ - -#if( MAX_LPC_ORDER > DECISION_DELAY ) -# define NSQ_LPC_BUF_LENGTH MAX_LPC_ORDER -#else -# define NSQ_LPC_BUF_LENGTH DECISION_DELAY -#endif - -/***********************/ -/* High pass filtering */ -/***********************/ -#define HIGH_PASS_INPUT 1 - -/***************************/ -/* Voice activity detector */ -/***************************/ -#define VAD_N_BANDS 4 /* 0-1, 1-2, 2-4, and 4-8 kHz */ - -#define VAD_INTERNAL_SUBFRAMES_LOG2 2 -#define VAD_INTERNAL_SUBFRAMES (1 << VAD_INTERNAL_SUBFRAMES_LOG2) - -#define VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 1024 /* Must be < 4096 */ -#define VAD_NOISE_LEVELS_BIAS 50 - -/* Sigmoid settings */ -#define VAD_NEGATIVE_OFFSET_Q5 128 /* sigmoid is 0 at -128 */ -#define VAD_SNR_FACTOR_Q16 45000 - -/* smoothing for SNR measurement */ -#define VAD_SNR_SMOOTH_COEF_Q18 4096 - -/******************/ -/* NLSF quantizer */ -/******************/ -#ifdef NLSF_TRAINING -# define NLSF_MSVQ_MAX_CB_STAGES 30 -# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE 256 -# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END 128 -#else -# define NLSF_MSVQ_MAX_CB_STAGES 10 /* Update manually when changing codebooks */ -# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE 128 /* Update manually when changing codebooks */ -# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END 16 /* Update manually when changing codebooks */ -#endif - -#define NLSF_MSVQ_FLUCTUATION_REDUCTION 1 -#define MAX_NLSF_MSVQ_SURVIVORS 16 -#define MAX_NLSF_MSVQ_SURVIVORS_LC_MODE 2 -#define MAX_NLSF_MSVQ_SURVIVORS_MC_MODE 4 - -/* Based on above defines, calculate how much memory is necessary to allocate */ -#if( NLSF_MSVQ_MAX_VECTORS_IN_STAGE > ( MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END ) ) -# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE NLSF_MSVQ_MAX_VECTORS_IN_STAGE -#else -# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END -#endif - -#if( NLSF_MSVQ_MAX_VECTORS_IN_STAGE > ( MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END ) ) -# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED NLSF_MSVQ_MAX_VECTORS_IN_STAGE -#else -# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END -#endif - -#define NLSF_MSVQ_SURV_MAX_REL_RD 4 - -/* Transition filtering for mode switching */ -#if SWITCH_TRANSITION_FILTERING -# define TRANSITION_TIME_UP_MS 5120 // 5120 = 64 * FRAME_LENGTH_MS * ( TRANSITION_INT_NUM - 1 ) = 64*(20*4) -# define TRANSITION_TIME_DOWN_MS 2560 // 2560 = 32 * FRAME_LENGTH_MS * ( TRANSITION_INT_NUM - 1 ) = 32*(20*4) -# define TRANSITION_NB 3 /* Hardcoded in tables */ -# define TRANSITION_NA 2 /* Hardcoded in tables */ -# define TRANSITION_INT_NUM 5 /* Hardcoded in tables */ -# define TRANSITION_FRAMES_UP ( TRANSITION_TIME_UP_MS / FRAME_LENGTH_MS ) -# define TRANSITION_FRAMES_DOWN ( TRANSITION_TIME_DOWN_MS / FRAME_LENGTH_MS ) -# define TRANSITION_INT_STEPS_UP ( TRANSITION_FRAMES_UP / ( TRANSITION_INT_NUM - 1 ) ) -# define TRANSITION_INT_STEPS_DOWN ( TRANSITION_FRAMES_DOWN / ( TRANSITION_INT_NUM - 1 ) ) -#endif - -/* Row based */ -#define matrix_ptr(Matrix_base_adr, row, column, N) *(Matrix_base_adr + ((row)*(N)+(column))) -#define matrix_adr(Matrix_base_adr, row, column, N) (Matrix_base_adr + ((row)*(N)+(column))) - -/* Column based */ -#ifndef matrix_c_ptr -# define matrix_c_ptr(Matrix_base_adr, row, column, M) *(Matrix_base_adr + ((row)+(M)*(column))) -#endif -#define matrix_c_adr(Matrix_base_adr, row, column, M) (Matrix_base_adr + ((row)+(M)*(column))) - -/* BWE factors to apply after packet loss */ -#define BWE_AFTER_LOSS_Q16 63570 - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_DEFINE_H +#define SKP_SILK_DEFINE_H + +#include "SKP_Silk_errors.h" +#include "SKP_Silk_typedef.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +#define MAX_FRAMES_PER_PACKET 5 +#define BIT_STREAM_V3 3 +#define BIT_STREAM_V4 4 +#define USE_BIT_STREAM_V BIT_STREAM_V3 // Should be moved to a API call + + +/* MAX DELTA LAG used for multiframe packets */ +#define MAX_DELTA_LAG 10 + +/* Lower limit on bitrate for each mode */ +#define MIN_TARGET_RATE_NB_BPS 5000 +#define MIN_TARGET_RATE_MB_BPS 7000 +#define MIN_TARGET_RATE_WB_BPS 8000 +#define MIN_TARGET_RATE_SWB_BPS 20000 + +/* Transition bitrates between modes */ +#define SWB2WB_BITRATE_BPS 30000 +#define SWB2WB_BITRATE_BPS_INITIAL 25000 +#define WB2SWB_BITRATE_BPS 35000 +#define WB2MB_BITRATE_BPS 15000 +#define MB2WB_BITRATE_BPS 20000 +#define MB2NB_BITRATE_BPS 10000 +#define NB2MB_BITRATE_BPS 14000 + +/* Integration/hysteresis threshold for lowering internal sample frequency */ +/* 30000000 -> 6 sec if bitrate is 5000 bps below limit; 3 sec if bitrate is 10000 bps below limit */ +#define ACCUM_BITS_DIFF_THRESHOLD 30000000 +#define TARGET_RATE_TAB_SZ 8 + +/* DTX settings */ +#define NO_SPEECH_FRAMES_BEFORE_DTX 5 /* eq 100 ms */ +#define MAX_CONSECUTIVE_DTX 20 /* eq 400 ms */ + +#define USE_LBRR 1 + +/* Amount of concecutive no FEC packets before telling JB */ +#define NO_LBRR_THRES 10 + +/* Maximum delay between real packet and LBRR packet */ +#define MAX_LBRR_DELAY 2 +#define LBRR_IDX_MASK 1 + +#define INBAND_FEC_MIN_RATE_BPS 18000 /* Dont use inband FEC below this total target rate */ +#define LBRR_LOSS_THRES 2 /* Start adding LBRR at this loss rate (needs tuning) */ + +/* LBRR usage defines */ +#define SKP_SILK_NO_LBRR 0 /* No LBRR information for this packet */ +#define SKP_SILK_ADD_LBRR_TO_PLUS1 1 /* Add LBRR for this packet to packet n + 1 */ +#define SKP_SILK_ADD_LBRR_TO_PLUS2 2 /* Add LBRR for this packet to packet n + 2 */ + +/* Frame termination indicator defines */ +#define SKP_SILK_LAST_FRAME 0 /* Last frames in packet */ +#define SKP_SILK_MORE_FRAMES 1 /* More frames to follow this one */ +#define SKP_SILK_LBRR_VER1 2 /* LBRR information from packet n - 1 */ +#define SKP_SILK_LBRR_VER2 3 /* LBRR information from packet n - 2 */ +#define SKP_SILK_EXT_LAYER 4 /* Extension layers added */ + +/* Number of Second order Sections for SWB detection HP filter */ +#define NB_SOS 3 +#define HP_8_KHZ_THRES 10 /* average energy per sample, above 8 kHz */ +#define CONCEC_SWB_SMPLS_THRES 480 * 15 /* 300 ms */ +#define WB_DETECT_ACTIVE_SPEECH_MS_THRES 15000 /* ms of active speech needed for WB detection */ + +/* Low complexity setting */ +#ifdef EMBEDDED_OPT +# define LOW_COMPLEXITY_ONLY 1 +#else +# define LOW_COMPLEXITY_ONLY 0 +#endif + +/* Activate bandwidth transition filtering for mode switching */ +#ifdef EMBEDDED_OPT +# define SWITCH_TRANSITION_FILTERING 0 +#else +#ifndef FORCE_FS_KHZ +# define SWITCH_TRANSITION_FILTERING 1 +#else +# define SWITCH_TRANSITION_FILTERING 0 +#endif +#endif + +/* Decoder Parameters */ +#define DEC_HP_ORDER 2 + +/* Maximum sampling frequency, should be 16 for embedded */ +#define MAX_FS_KHZ 24 + +/* Signal Types used by silk */ +#define SIG_TYPE_VOICED 0 +#define SIG_TYPE_UNVOICED 1 + +/* VAD Types used by silk */ +#define NO_VOICE_ACTIVITY 0 +#define VOICE_ACTIVITY 1 + +/* number of samples per frame */ +#define FRAME_LENGTH_MS 20 /* 20 ms */ +#define MAX_FRAME_LENGTH (FRAME_LENGTH_MS * MAX_FS_KHZ) + +/* number of lookahead samples for pitch analysis */ +#define LA_PITCH_MS 3 +#define LA_PITCH_MAX (LA_PITCH_MS * MAX_FS_KHZ) + +/* number of lookahead samples for noise shape analysis */ +#define LA_SHAPE_MS 5 +#define LA_SHAPE_MAX (LA_SHAPE_MS * MAX_FS_KHZ) + +/* Order of LPC used in find pitch */ +#define FIND_PITCH_LPC_ORDER_MAX 16 + +/* Length of LPC window used in find pitch */ +#define FIND_PITCH_LPC_WIN_MS (30 + (LA_PITCH_MS << 1)) +#define FIND_PITCH_LPC_WIN_MAX (FIND_PITCH_LPC_WIN_MS * MAX_FS_KHZ) + +#define PITCH_EST_COMPLEXITY_HC_MODE SigProc_PITCH_EST_MAX_COMPLEX +#define PITCH_EST_COMPLEXITY_MC_MODE SigProc_PITCH_EST_MID_COMPLEX +#define PITCH_EST_COMPLEXITY_LC_MODE SigProc_PITCH_EST_MIN_COMPLEX + + +/* Max number of bytes in payload output buffer (may contain multiple frames) */ +#define MAX_ARITHM_BYTES 1024 + +#define RANGE_CODER_WRITE_BEYOND_BUFFER -1 +#define RANGE_CODER_CDF_OUT_OF_RANGE -2 +#define RANGE_CODER_NORMALIZATION_FAILED -3 +#define RANGE_CODER_ZERO_INTERVAL_WIDTH -4 +#define RANGE_CODER_DECODER_CHECK_FAILED -5 +#define RANGE_CODER_READ_BEYOND_BUFFER -6 +#define RANGE_CODER_ILLEGAL_SAMPLING_RATE -7 +#define RANGE_CODER_DEC_PAYLOAD_TOO_LONG -8 + +/* dB level of lowest gain quantization level */ +#define MIN_QGAIN_DB 6 +/* dB level of highest gain quantization level */ +#define MAX_QGAIN_DB 86 +/* Number of gain quantization levels */ +#define N_LEVELS_QGAIN 64 +/* Max increase in gain quantization index */ +#define MAX_DELTA_GAIN_QUANT 40 +/* Max decrease in gain quantization index */ +#define MIN_DELTA_GAIN_QUANT -4 + +/* Quantization offsets (multiples of 4) */ +#define OFFSET_VL_Q10 32 +#define OFFSET_VH_Q10 100 +#define OFFSET_UVL_Q10 100 +#define OFFSET_UVH_Q10 256 + +/* Maximum numbers of iterations used to stabilize a LPC vector */ +#define MAX_LPC_STABILIZE_ITERATIONS 20 + +#define MAX_LPC_ORDER 16 +#define MIN_LPC_ORDER 10 + +/* Find Pred Coef defines */ +#define LTP_ORDER 5 + +/* LTP quantization settings */ +#define NB_LTP_CBKS 3 + +/* Number of subframes */ +#define NB_SUBFR 4 + +/* Flag to use harmonic noise shaping */ +#define USE_HARM_SHAPING 1 + +/* Max LPC order of noise shaping filters */ +#define SHAPE_LPC_ORDER_MAX 16 + +#define HARM_SHAPE_FIR_TAPS 3 + +/* Length of LPC window used in noise shape analysis */ +#define SHAPE_LPC_WIN_MS 15 +#define SHAPE_LPC_WIN_16_KHZ (SHAPE_LPC_WIN_MS * 16) +#define SHAPE_LPC_WIN_24_KHZ (SHAPE_LPC_WIN_MS * 24) +#define SHAPE_LPC_WIN_MAX (SHAPE_LPC_WIN_MS * MAX_FS_KHZ) + +/* Maximum number of delayed decision states */ +#define DEL_DEC_STATES_MAX 4 + +#define LTP_BUF_LENGTH 512 +#define LTP_MASK (LTP_BUF_LENGTH - 1) + +#define DECISION_DELAY 32 +#define DECISION_DELAY_MASK (DECISION_DELAY - 1) + +/* number of subframes for excitation entropy coding */ +#define SHELL_CODEC_FRAME_LENGTH 16 +#define MAX_NB_SHELL_BLOCKS (MAX_FRAME_LENGTH / SHELL_CODEC_FRAME_LENGTH) + +/* number of rate levels, for entropy coding of excitation */ +#define N_RATE_LEVELS 10 + +/* maximum sum of pulses per shell coding frame */ +#define MAX_PULSES 18 + +#define MAX_MATRIX_SIZE MAX_LPC_ORDER /* Max of LPC Order and LTP order */ + +#if( MAX_LPC_ORDER > DECISION_DELAY ) +# define NSQ_LPC_BUF_LENGTH MAX_LPC_ORDER +#else +# define NSQ_LPC_BUF_LENGTH DECISION_DELAY +#endif + +/***********************/ +/* High pass filtering */ +/***********************/ +#define HIGH_PASS_INPUT 1 + +/***************************/ +/* Voice activity detector */ +/***************************/ +#define VAD_N_BANDS 4 /* 0-1, 1-2, 2-4, and 4-8 kHz */ + +#define VAD_INTERNAL_SUBFRAMES_LOG2 2 +#define VAD_INTERNAL_SUBFRAMES (1 << VAD_INTERNAL_SUBFRAMES_LOG2) + +#define VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 1024 /* Must be < 4096 */ +#define VAD_NOISE_LEVELS_BIAS 50 + +/* Sigmoid settings */ +#define VAD_NEGATIVE_OFFSET_Q5 128 /* sigmoid is 0 at -128 */ +#define VAD_SNR_FACTOR_Q16 45000 + +/* smoothing for SNR measurement */ +#define VAD_SNR_SMOOTH_COEF_Q18 4096 + +/******************/ +/* NLSF quantizer */ +/******************/ +#ifdef NLSF_TRAINING +# define NLSF_MSVQ_MAX_CB_STAGES 30 +# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE 256 +# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END 128 +#else +# define NLSF_MSVQ_MAX_CB_STAGES 10 /* Update manually when changing codebooks */ +# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE 128 /* Update manually when changing codebooks */ +# define NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END 16 /* Update manually when changing codebooks */ +#endif + +#define NLSF_MSVQ_FLUCTUATION_REDUCTION 1 +#define MAX_NLSF_MSVQ_SURVIVORS 16 +#define MAX_NLSF_MSVQ_SURVIVORS_LC_MODE 2 +#define MAX_NLSF_MSVQ_SURVIVORS_MC_MODE 4 + +/* Based on above defines, calculate how much memory is necessary to allocate */ +#if( NLSF_MSVQ_MAX_VECTORS_IN_STAGE > ( MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END ) ) +# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE NLSF_MSVQ_MAX_VECTORS_IN_STAGE +#else +# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE MAX_NLSF_MSVQ_SURVIVORS_LC_MODE * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END +#endif + +#if( NLSF_MSVQ_MAX_VECTORS_IN_STAGE > ( MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END ) ) +# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED NLSF_MSVQ_MAX_VECTORS_IN_STAGE +#else +# define NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED MAX_NLSF_MSVQ_SURVIVORS * NLSF_MSVQ_MAX_VECTORS_IN_STAGE_TWO_TO_END +#endif + +#define NLSF_MSVQ_SURV_MAX_REL_RD 4 + +/* Transition filtering for mode switching */ +#if SWITCH_TRANSITION_FILTERING +# define TRANSITION_TIME_UP_MS 5120 // 5120 = 64 * FRAME_LENGTH_MS * ( TRANSITION_INT_NUM - 1 ) = 64*(20*4) +# define TRANSITION_TIME_DOWN_MS 2560 // 2560 = 32 * FRAME_LENGTH_MS * ( TRANSITION_INT_NUM - 1 ) = 32*(20*4) +# define TRANSITION_NB 3 /* Hardcoded in tables */ +# define TRANSITION_NA 2 /* Hardcoded in tables */ +# define TRANSITION_INT_NUM 5 /* Hardcoded in tables */ +# define TRANSITION_FRAMES_UP ( TRANSITION_TIME_UP_MS / FRAME_LENGTH_MS ) +# define TRANSITION_FRAMES_DOWN ( TRANSITION_TIME_DOWN_MS / FRAME_LENGTH_MS ) +# define TRANSITION_INT_STEPS_UP ( TRANSITION_FRAMES_UP / ( TRANSITION_INT_NUM - 1 ) ) +# define TRANSITION_INT_STEPS_DOWN ( TRANSITION_FRAMES_DOWN / ( TRANSITION_INT_NUM - 1 ) ) +#endif + +/* Row based */ +#define matrix_ptr(Matrix_base_adr, row, column, N) *(Matrix_base_adr + ((row)*(N)+(column))) +#define matrix_adr(Matrix_base_adr, row, column, N) (Matrix_base_adr + ((row)*(N)+(column))) + +/* Column based */ +#ifndef matrix_c_ptr +# define matrix_c_ptr(Matrix_base_adr, row, column, M) *(Matrix_base_adr + ((row)+(M)*(column))) +#endif +#define matrix_c_adr(Matrix_base_adr, row, column, M) (Matrix_base_adr + ((row)+(M)*(column))) + +/* BWE factors to apply after packet loss */ +#define BWE_AFTER_LOSS_Q16 63570 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_define_FIX.h b/app/src/main/jni/silk/src/SKP_Silk_define_FIX.h similarity index 98% rename from jni/silk/src/SKP_Silk_define_FIX.h rename to app/src/main/jni/silk/src/SKP_Silk_define_FIX.h index ff42ace..f6969fd 100644 --- a/jni/silk/src/SKP_Silk_define_FIX.h +++ b/app/src/main/jni/silk/src/SKP_Silk_define_FIX.h @@ -1,97 +1,97 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_DEFINE_FIX_H -#define SKP_SILK_DEFINE_FIX_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* Head room for correlations */ -#define LTP_CORRS_HEAD_ROOM 2 -#define LPC_CORRS_HEAD_ROOM 10 - -#define WB_DETECT_ACTIVE_SPEECH_LEVEL_THRES_Q8 179 // 179.2_Q8 = 0.7f required speech activity for counting frame as active - -/* DTX settings */ -#define SPEECH_ACTIVITY_DTX_THRES_Q8 26 // 25.60_Q8 = 0.1f - -#define LBRR_SPEECH_ACTIVITY_THRES_Q8 128 - -/* level of noise floor for whitening filter LPC analysis in pitch analysis */ -#define FIND_PITCH_WHITE_NOISE_FRACTION_Q16 66 - -/* bandwdith expansion for whitening filter in pitch analysis */ -#define FIND_PITCH_BANDWITH_EXPANSION_Q16 64881 - -/* Threshold used by pitch estimator for early escape */ -#define FIND_PITCH_CORRELATION_THRESHOLD_Q16_HC_MODE 45875 // 0.7 -#define FIND_PITCH_CORRELATION_THRESHOLD_Q16_MC_MODE 49152 // 0.75 -#define FIND_PITCH_CORRELATION_THRESHOLD_Q16_LC_MODE 52429 // 0.8 - -/* Regualarization factor for correlation matrix. Equivalent to adding noise at -50 dB */ -#define FIND_LTP_COND_FAC_Q31 21475 -#define FIND_LPC_COND_FAC_Q32 257698 // 6e-5 - -/* Find Pred Coef defines */ -#define INACTIVE_BWExp_Q16 64225 // 0.98 -#define ACTIVE_BWExp_Q16 65470 // 0.999 -#define LTP_DAMPING_Q16 66 -#define LTP_SMOOTHING_Q26 6710886 - -/* LTP quantization settings */ -#define MU_LTP_QUANT_NB_Q8 8 -#define MU_LTP_QUANT_MB_Q8 6 -#define MU_LTP_QUANT_WB_Q8 5 -#define MU_LTP_QUANT_SWB_Q8 4 - -/***********************/ -/* High pass filtering */ -/***********************/ -/* Smoothing parameters for low end of pitch frequency range estimation */ -#define VARIABLE_HP_SMTH_COEF1_Q16 6554 // 0.1 -#define VARIABLE_HP_SMTH_COEF2_Q16 983 // 0.015 - -/* Min and max values for low end of pitch frequency range estimation */ -#define VARIABLE_HP_MIN_FREQ_Q0 80 -#define VARIABLE_HP_MAX_FREQ_Q0 150 - -/* Max absolute difference between log2 of pitch frequency and smoother state, to enter the smoother */ -#define VARIABLE_HP_MAX_DELTA_FREQ_Q7 51 // 0.4 in Q7 - -/* Defines for CN generation */ -#define CNG_BUF_MASK_MAX 255 /* 2^floor(log2(MAX_FRAME_LENGTH)) */ -#define CNG_GAIN_SMTH_Q16 4634 /* 0.25^(1/4) */ -#define CNG_NLSF_SMTH_Q16 16348 /* 0.25 */ - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_DEFINE_FIX_H +#define SKP_SILK_DEFINE_FIX_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* Head room for correlations */ +#define LTP_CORRS_HEAD_ROOM 2 +#define LPC_CORRS_HEAD_ROOM 10 + +#define WB_DETECT_ACTIVE_SPEECH_LEVEL_THRES_Q8 179 // 179.2_Q8 = 0.7f required speech activity for counting frame as active + +/* DTX settings */ +#define SPEECH_ACTIVITY_DTX_THRES_Q8 26 // 25.60_Q8 = 0.1f + +#define LBRR_SPEECH_ACTIVITY_THRES_Q8 128 + +/* level of noise floor for whitening filter LPC analysis in pitch analysis */ +#define FIND_PITCH_WHITE_NOISE_FRACTION_Q16 66 + +/* bandwdith expansion for whitening filter in pitch analysis */ +#define FIND_PITCH_BANDWITH_EXPANSION_Q16 64881 + +/* Threshold used by pitch estimator for early escape */ +#define FIND_PITCH_CORRELATION_THRESHOLD_Q16_HC_MODE 45875 // 0.7 +#define FIND_PITCH_CORRELATION_THRESHOLD_Q16_MC_MODE 49152 // 0.75 +#define FIND_PITCH_CORRELATION_THRESHOLD_Q16_LC_MODE 52429 // 0.8 + +/* Regualarization factor for correlation matrix. Equivalent to adding noise at -50 dB */ +#define FIND_LTP_COND_FAC_Q31 21475 +#define FIND_LPC_COND_FAC_Q32 257698 // 6e-5 + +/* Find Pred Coef defines */ +#define INACTIVE_BWExp_Q16 64225 // 0.98 +#define ACTIVE_BWExp_Q16 65470 // 0.999 +#define LTP_DAMPING_Q16 66 +#define LTP_SMOOTHING_Q26 6710886 + +/* LTP quantization settings */ +#define MU_LTP_QUANT_NB_Q8 8 +#define MU_LTP_QUANT_MB_Q8 6 +#define MU_LTP_QUANT_WB_Q8 5 +#define MU_LTP_QUANT_SWB_Q8 4 + +/***********************/ +/* High pass filtering */ +/***********************/ +/* Smoothing parameters for low end of pitch frequency range estimation */ +#define VARIABLE_HP_SMTH_COEF1_Q16 6554 // 0.1 +#define VARIABLE_HP_SMTH_COEF2_Q16 983 // 0.015 + +/* Min and max values for low end of pitch frequency range estimation */ +#define VARIABLE_HP_MIN_FREQ_Q0 80 +#define VARIABLE_HP_MAX_FREQ_Q0 150 + +/* Max absolute difference between log2 of pitch frequency and smoother state, to enter the smoother */ +#define VARIABLE_HP_MAX_DELTA_FREQ_Q7 51 // 0.4 in Q7 + +/* Defines for CN generation */ +#define CNG_BUF_MASK_MAX 255 /* 2^floor(log2(MAX_FRAME_LENGTH)) */ +#define CNG_GAIN_SMTH_Q16 4634 /* 0.25^(1/4) */ +#define CNG_NLSF_SMTH_Q16 16348 /* 0.25 */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_detect_SWB_input.c b/app/src/main/jni/silk/src/SKP_Silk_detect_SWB_input.c similarity index 98% rename from jni/silk/src/SKP_Silk_detect_SWB_input.c rename to app/src/main/jni/silk/src/SKP_Silk_detect_SWB_input.c index c251cea..fbce7ef 100644 --- a/jni/silk/src/SKP_Silk_detect_SWB_input.c +++ b/app/src/main/jni/silk/src/SKP_Silk_detect_SWB_input.c @@ -1,76 +1,76 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* - * Detect SWB input by measuring energy above 8 kHz. - */ - -#include "SKP_Silk_main.h" - -void SKP_Silk_detect_SWB_input( - SKP_Silk_detect_SWB_state *psSWBdetect, /* (I/O) encoder state */ - const SKP_int16 samplesIn[], /* (I) input to encoder */ - SKP_int nSamplesIn /* (I) length of input */ -) -{ - SKP_int HP_8_kHz_len, i; - SKP_int16 in_HP_8_kHz[ MAX_FRAME_LENGTH ]; - SKP_int32 energy_32, shift; - - /* High pass filter with cutoff at 8 khz */ - HP_8_kHz_len = SKP_min_int( nSamplesIn, MAX_FRAME_LENGTH ); - HP_8_kHz_len = SKP_max_int( HP_8_kHz_len, 0 ); - - /* Cutoff around 9 khz */ - /* A = conv(conv([8192,14613, 6868], [8192,12883, 7337]), [8192,11586, 7911]); */ - /* B = conv(conv([575, -948, 575], [575, -221, 575]), [575, 104, 575]); */ - SKP_Silk_biquad( samplesIn, SKP_Silk_SWB_detect_B_HP_Q13[ 0 ], SKP_Silk_SWB_detect_A_HP_Q13[ 0 ], - psSWBdetect->S_HP_8_kHz[ 0 ], in_HP_8_kHz, HP_8_kHz_len ); - for( i = 1; i < NB_SOS; i++ ) { - SKP_Silk_biquad( in_HP_8_kHz, SKP_Silk_SWB_detect_B_HP_Q13[ i ], SKP_Silk_SWB_detect_A_HP_Q13[ i ], - psSWBdetect->S_HP_8_kHz[ i ], in_HP_8_kHz, HP_8_kHz_len ); - } - - /* Calculate energy in HP signal */ - SKP_Silk_sum_sqr_shift( &energy_32, &shift, in_HP_8_kHz, HP_8_kHz_len ); - - /* Count concecutive samples above threshold, after adjusting threshold for number of input samples and shift */ - if( energy_32 > SKP_RSHIFT( SKP_SMULBB( HP_8_KHZ_THRES, HP_8_kHz_len ), shift ) ) { - psSWBdetect->ConsecSmplsAboveThres += nSamplesIn; - if( psSWBdetect->ConsecSmplsAboveThres > CONCEC_SWB_SMPLS_THRES ) { - psSWBdetect->SWB_detected = 1; - } - } else { - psSWBdetect->ConsecSmplsAboveThres -= nSamplesIn; - psSWBdetect->ConsecSmplsAboveThres = SKP_max( psSWBdetect->ConsecSmplsAboveThres, 0 ); - } - - /* If sufficient speech activity and no SWB detected, we detect the signal as being WB */ - if( ( psSWBdetect->ActiveSpeech_ms > WB_DETECT_ACTIVE_SPEECH_MS_THRES ) && ( psSWBdetect->SWB_detected == 0 ) ) { - psSWBdetect->WB_detected = 1; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* + * Detect SWB input by measuring energy above 8 kHz. + */ + +#include "SKP_Silk_main.h" + +void SKP_Silk_detect_SWB_input( + SKP_Silk_detect_SWB_state *psSWBdetect, /* (I/O) encoder state */ + const SKP_int16 samplesIn[], /* (I) input to encoder */ + SKP_int nSamplesIn /* (I) length of input */ +) +{ + SKP_int HP_8_kHz_len, i; + SKP_int16 in_HP_8_kHz[ MAX_FRAME_LENGTH ]; + SKP_int32 energy_32, shift; + + /* High pass filter with cutoff at 8 khz */ + HP_8_kHz_len = SKP_min_int( nSamplesIn, MAX_FRAME_LENGTH ); + HP_8_kHz_len = SKP_max_int( HP_8_kHz_len, 0 ); + + /* Cutoff around 9 khz */ + /* A = conv(conv([8192,14613, 6868], [8192,12883, 7337]), [8192,11586, 7911]); */ + /* B = conv(conv([575, -948, 575], [575, -221, 575]), [575, 104, 575]); */ + SKP_Silk_biquad( samplesIn, SKP_Silk_SWB_detect_B_HP_Q13[ 0 ], SKP_Silk_SWB_detect_A_HP_Q13[ 0 ], + psSWBdetect->S_HP_8_kHz[ 0 ], in_HP_8_kHz, HP_8_kHz_len ); + for( i = 1; i < NB_SOS; i++ ) { + SKP_Silk_biquad( in_HP_8_kHz, SKP_Silk_SWB_detect_B_HP_Q13[ i ], SKP_Silk_SWB_detect_A_HP_Q13[ i ], + psSWBdetect->S_HP_8_kHz[ i ], in_HP_8_kHz, HP_8_kHz_len ); + } + + /* Calculate energy in HP signal */ + SKP_Silk_sum_sqr_shift( &energy_32, &shift, in_HP_8_kHz, HP_8_kHz_len ); + + /* Count concecutive samples above threshold, after adjusting threshold for number of input samples and shift */ + if( energy_32 > SKP_RSHIFT( SKP_SMULBB( HP_8_KHZ_THRES, HP_8_kHz_len ), shift ) ) { + psSWBdetect->ConsecSmplsAboveThres += nSamplesIn; + if( psSWBdetect->ConsecSmplsAboveThres > CONCEC_SWB_SMPLS_THRES ) { + psSWBdetect->SWB_detected = 1; + } + } else { + psSWBdetect->ConsecSmplsAboveThres -= nSamplesIn; + psSWBdetect->ConsecSmplsAboveThres = SKP_max( psSWBdetect->ConsecSmplsAboveThres, 0 ); + } + + /* If sufficient speech activity and no SWB detected, we detect the signal as being WB */ + if( ( psSWBdetect->ActiveSpeech_ms > WB_DETECT_ACTIVE_SPEECH_MS_THRES ) && ( psSWBdetect->SWB_detected == 0 ) ) { + psSWBdetect->WB_detected = 1; + } +} diff --git a/jni/silk/src/SKP_Silk_enc_API.c b/app/src/main/jni/silk/src/SKP_Silk_enc_API.c similarity index 97% rename from jni/silk/src/SKP_Silk_enc_API.c rename to app/src/main/jni/silk/src/SKP_Silk_enc_API.c index 0bf6038..abb2eee 100644 --- a/jni/silk/src/SKP_Silk_enc_API.c +++ b/app/src/main/jni/silk/src/SKP_Silk_enc_API.c @@ -1,268 +1,268 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - - -#include "SKP_Silk_define.h" -#include "SKP_Silk_main_FIX.h" -#include "SKP_Silk_SDK_API.h" -#include "SKP_Silk_control.h" -#include "SKP_Silk_typedef.h" -#include "SKP_Silk_structs.h" -#define SKP_Silk_EncodeControlStruct SKP_SILK_SDK_EncControlStruct - -/****************************************/ -/* Encoder functions */ -/****************************************/ - -SKP_int SKP_Silk_SDK_Get_Encoder_Size( SKP_int *encSizeBytes ) -{ - SKP_int ret = 0; - - *encSizeBytes = sizeof( SKP_Silk_encoder_state_FIX ); - - return ret; -} - - -/***************************************/ -/* Read control structure from encoder */ -/***************************************/ -SKP_int SKP_Silk_SDK_QueryEncoder( - const void *encState, /* I: State Vector */ - SKP_Silk_EncodeControlStruct *encStatus /* O: Control Structure */ -) -{ - SKP_Silk_encoder_state_FIX *psEnc; - SKP_int ret = 0; - - psEnc = ( SKP_Silk_encoder_state_FIX* )encState; - - encStatus->sampleRate = ( unsigned short )SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ); /* convert kHz -> Hz */ - encStatus->packetSize = ( unsigned short )SKP_SMULBB( psEnc->sCmn.fs_kHz, psEnc->sCmn.PacketSize_ms ); /* convert samples -> ms */ - encStatus->bitRate = ( unsigned short )psEnc->sCmn.TargetRate_bps; - encStatus->packetLossPercentage = psEnc->sCmn.PacketLoss_perc; - encStatus->complexity = psEnc->sCmn.Complexity; - - return ret; -} - -/*************************/ -/* Init or Reset encoder */ -/*************************/ -SKP_int SKP_Silk_SDK_InitEncoder( - void *encState, /* I/O: State */ - SKP_Silk_EncodeControlStruct *encStatus /* O: Control structure */ -) -{ - SKP_Silk_encoder_state_FIX *psEnc; - SKP_int ret = 0; - - - psEnc = ( SKP_Silk_encoder_state_FIX* )encState; - - /* Reset Encoder */ - if( ret += SKP_Silk_init_encoder_FIX( psEnc ) ) { - SKP_assert( 0 ); - } - - /* Read Control structure */ - if( ret += SKP_Silk_SDK_QueryEncoder( encState, encStatus ) ) { - SKP_assert( 0 ); - } - - - return ret; -} - -/**************************/ -/* Encode frame with Silk */ -/**************************/ -SKP_int SKP_Silk_SDK_Encode( - void *encState, /* I/O: State */ - const SKP_Silk_EncodeControlStruct *encControl, /* I: Control structure */ - const SKP_int16 *samplesIn, /* I: Speech sample input vector */ - SKP_int nSamplesIn, /* I: Number of samples in input vector */ - SKP_uint8 *outData, /* O: Encoded output vector */ - SKP_int16 *nBytesOut /* I/O: Number of bytes in outData (input: Max bytes) */ -) -{ - SKP_int API_fs_kHz, PacketSize_ms, PacketLoss_perc, UseInBandFec, UseDTX, ret = 0; - SKP_int nSamplesToBuffer, Complexity, input_ms, nSamplesFromInput = 0; - SKP_int32 TargetRate_bps; - SKP_int16 MaxBytesOut; - SKP_Silk_encoder_state_FIX *psEnc = ( SKP_Silk_encoder_state_FIX* )encState; - - - SKP_assert( encControl != NULL ); - - /* Check sampling frequency first, to avoid divide by zero later */ - if( ( encControl->sampleRate != 8000 ) && ( encControl->sampleRate != 12000 ) && - ( encControl->sampleRate != 16000 ) && ( encControl->sampleRate != 24000 ) ) { - ret = SKP_SILK_ENC_FS_NOT_SUPPORTED; - SKP_assert( 0 ); - return( ret ); - } - - /* Set Encoder parameters from Control structure */ - API_fs_kHz = SKP_DIV32_16( ( SKP_int )encControl->sampleRate, 1000 ); /* convert Hz -> kHz */ - PacketSize_ms = SKP_DIV32_16( ( SKP_int )encControl->packetSize, API_fs_kHz ); /* convert samples -> ms */ - TargetRate_bps = ( SKP_int32 )encControl->bitRate; - PacketLoss_perc = ( SKP_int )encControl->packetLossPercentage; - UseInBandFec = ( SKP_int )encControl->useInBandFEC; - Complexity = ( SKP_int )encControl->complexity; - UseDTX = ( SKP_int )encControl->useDTX; - - /* Only accept input lengths that are multiplum of 10 ms */ - input_ms = SKP_DIV32_16( nSamplesIn, API_fs_kHz ); - if( ( input_ms % 10) != 0 || nSamplesIn < 0 ) { - ret = SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; - SKP_assert( 0 ); - return( ret ); - } - - /* Make sure no more than one packet can be produced */ - if( nSamplesIn > SKP_SMULBB( psEnc->sCmn.PacketSize_ms, API_fs_kHz ) ) { - ret = SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; - SKP_assert( 0 ); - return( ret ); - } - - if( ( ret = SKP_Silk_control_encoder_FIX( psEnc, API_fs_kHz, PacketSize_ms, TargetRate_bps, - PacketLoss_perc, UseInBandFec, UseDTX, input_ms, Complexity ) ) != 0 ) { - SKP_assert( 0 ); - return( ret ); - } - - /* Detect energy above 8 kHz */ - if( encControl->sampleRate == 24000 && psEnc->sCmn.sSWBdetect.SWB_detected == 0 && psEnc->sCmn.sSWBdetect.WB_detected == 0 ) { - SKP_Silk_detect_SWB_input( &psEnc->sCmn.sSWBdetect, samplesIn, ( SKP_int )nSamplesIn ); - } - - /* Input buffering/resampling and encoding */ - MaxBytesOut = 0; /* return 0 output bytes if no encoder called */ - while( 1 ) { - /* Resample/buffer */ - nSamplesToBuffer = psEnc->sCmn.frame_length - psEnc->sCmn.inputBufIx; - if( encControl->sampleRate == SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) ) { - /* Same sample frequency - copy the data */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, nSamplesIn ); - nSamplesFromInput = nSamplesToBuffer; - SKP_memcpy( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], samplesIn, SKP_SMULBB( nSamplesToBuffer, sizeof( SKP_int16 ) ) ); - } else if( encControl->sampleRate == 24000 && psEnc->sCmn.fs_kHz == 16 ) { - /* Resample the data from 24 kHz to 16 kHz */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_SMULWB( SKP_LSHIFT( nSamplesIn, 1 ), 21846 ) ); // 21846 = ceil(2/3)*2^15 - nSamplesFromInput = SKP_RSHIFT( SKP_SMULBB( nSamplesToBuffer, 3 ), 1 ); -#if LOW_COMPLEXITY_ONLY - { - SKP_int16 scratch[ MAX_FRAME_LENGTH + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; - SKP_assert( nSamplesFromInput <= MAX_FRAME_LENGTH ); - SKP_Silk_resample_2_3_coarse( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample24To16state, - samplesIn, nSamplesFromInput, scratch ); - } -#else - SKP_Silk_resample_2_3( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample24To16state, - samplesIn, nSamplesFromInput ); -#endif - } else if( encControl->sampleRate == 24000 && psEnc->sCmn.fs_kHz == 12 ) { - SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ]; - /* Resample the data from 24 kHz to 12 kHz */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_RSHIFT( nSamplesIn, 1 ) ); - nSamplesFromInput = SKP_LSHIFT16( nSamplesToBuffer, 1 ); - SKP_Silk_resample_1_2_coarse( samplesIn, psEnc->sCmn.resample24To12state, - &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], scratch, nSamplesToBuffer ); - } else if( encControl->sampleRate == 24000 && psEnc->sCmn.fs_kHz == 8 ) { - /* Resample the data from 24 kHz to 8 kHz */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_DIV32_16( nSamplesIn, 3 ) ); - nSamplesFromInput = SKP_SMULBB( nSamplesToBuffer, 3 ); - SKP_Silk_resample_1_3( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample24To8state, - samplesIn, nSamplesFromInput); - } else if( encControl->sampleRate == 16000 && psEnc->sCmn.fs_kHz == 12 ) { - /* Resample the data from 16 kHz to 12 kHz */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_RSHIFT( SKP_SMULBB( nSamplesIn, 3 ), 2 ) ); - nSamplesFromInput = SKP_SMULWB( SKP_LSHIFT16( nSamplesToBuffer, 2 ), 21846 ); // 21846 = ceil((1/3)*2^16) - SKP_Silk_resample_3_4( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample16To12state, - samplesIn, nSamplesFromInput ); - } else if( encControl->sampleRate == 16000 && psEnc->sCmn.fs_kHz == 8 ) { - SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ]; - /* Resample the data from 16 kHz to 8 kHz */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_RSHIFT( nSamplesIn, 1 ) ); - nSamplesFromInput = SKP_LSHIFT16( nSamplesToBuffer, 1 ); - SKP_Silk_resample_1_2_coarse( samplesIn, psEnc->sCmn.resample16To8state, - &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], scratch, nSamplesToBuffer ); - } else if( encControl->sampleRate == 12000 && psEnc->sCmn.fs_kHz == 8 ) { - /* Resample the data from 12 kHz to 8 kHz */ - nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_SMULWB( SKP_LSHIFT( nSamplesIn, 1 ), 21846 ) ); - nSamplesFromInput = SKP_RSHIFT( SKP_SMULBB( nSamplesToBuffer, 3 ), 1 ); -#if LOW_COMPLEXITY_ONLY - { - SKP_int16 scratch[ MAX_FRAME_LENGTH + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; - SKP_assert( nSamplesFromInput <= MAX_FRAME_LENGTH ); - SKP_Silk_resample_2_3_coarse( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample12To8state, - samplesIn, nSamplesFromInput, scratch ); - } -#else - SKP_Silk_resample_2_3( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample12To8state, - samplesIn, nSamplesFromInput ); -#endif - } - samplesIn += nSamplesFromInput; - nSamplesIn -= nSamplesFromInput; - psEnc->sCmn.inputBufIx += nSamplesToBuffer; - - /* Silk encoder */ - if( psEnc->sCmn.inputBufIx >= psEnc->sCmn.frame_length ) { - /* Enough data in input buffer, so encode */ - if( MaxBytesOut == 0 ) { - /* No payload obtained so far */ - MaxBytesOut = *nBytesOut; - if( ( ret = SKP_Silk_encode_frame_FIX( psEnc, outData, &MaxBytesOut, psEnc->sCmn.inputBuf ) ) != 0 ) { - SKP_assert( 0 ); - } - } else { - /* outData already contains a payload */ - if( ( ret = SKP_Silk_encode_frame_FIX( psEnc, outData, nBytesOut, psEnc->sCmn.inputBuf ) ) != 0 ) { - SKP_assert( 0 ); - } - /* Check that no second payload was created */ - SKP_assert( *nBytesOut == 0 ); - } - psEnc->sCmn.inputBufIx = 0; - } else { - break; - } - } - - *nBytesOut = MaxBytesOut; - if( psEnc->sCmn.useDTX && psEnc->sCmn.inDTX ) { - /* Dtx simulation */ - *nBytesOut = 0; - } - - - return ret; -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + + +#include "SKP_Silk_define.h" +#include "SKP_Silk_main_FIX.h" +#include "SKP_Silk_SDK_API.h" +#include "SKP_Silk_control.h" +#include "SKP_Silk_typedef.h" +#include "SKP_Silk_structs.h" +#define SKP_Silk_EncodeControlStruct SKP_SILK_SDK_EncControlStruct + +/****************************************/ +/* Encoder functions */ +/****************************************/ + +SKP_int SKP_Silk_SDK_Get_Encoder_Size( SKP_int *encSizeBytes ) +{ + SKP_int ret = 0; + + *encSizeBytes = sizeof( SKP_Silk_encoder_state_FIX ); + + return ret; +} + + +/***************************************/ +/* Read control structure from encoder */ +/***************************************/ +SKP_int SKP_Silk_SDK_QueryEncoder( + const void *encState, /* I: State Vector */ + SKP_Silk_EncodeControlStruct *encStatus /* O: Control Structure */ +) +{ + SKP_Silk_encoder_state_FIX *psEnc; + SKP_int ret = 0; + + psEnc = ( SKP_Silk_encoder_state_FIX* )encState; + + encStatus->sampleRate = ( unsigned short )SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ); /* convert kHz -> Hz */ + encStatus->packetSize = ( unsigned short )SKP_SMULBB( psEnc->sCmn.fs_kHz, psEnc->sCmn.PacketSize_ms ); /* convert samples -> ms */ + encStatus->bitRate = ( unsigned short )psEnc->sCmn.TargetRate_bps; + encStatus->packetLossPercentage = psEnc->sCmn.PacketLoss_perc; + encStatus->complexity = psEnc->sCmn.Complexity; + + return ret; +} + +/*************************/ +/* Init or Reset encoder */ +/*************************/ +SKP_int SKP_Silk_SDK_InitEncoder( + void *encState, /* I/O: State */ + SKP_Silk_EncodeControlStruct *encStatus /* O: Control structure */ +) +{ + SKP_Silk_encoder_state_FIX *psEnc; + SKP_int ret = 0; + + + psEnc = ( SKP_Silk_encoder_state_FIX* )encState; + + /* Reset Encoder */ + if( ret += SKP_Silk_init_encoder_FIX( psEnc ) ) { + SKP_assert( 0 ); + } + + /* Read Control structure */ + if( ret += SKP_Silk_SDK_QueryEncoder( encState, encStatus ) ) { + SKP_assert( 0 ); + } + + + return ret; +} + +/**************************/ +/* Encode frame with Silk */ +/**************************/ +SKP_int SKP_Silk_SDK_Encode( + void *encState, /* I/O: State */ + const SKP_Silk_EncodeControlStruct *encControl, /* I: Control structure */ + const SKP_int16 *samplesIn, /* I: Speech sample input vector */ + SKP_int nSamplesIn, /* I: Number of samples in input vector */ + SKP_uint8 *outData, /* O: Encoded output vector */ + SKP_int16 *nBytesOut /* I/O: Number of bytes in outData (input: Max bytes) */ +) +{ + SKP_int API_fs_kHz, PacketSize_ms, PacketLoss_perc, UseInBandFec, UseDTX, ret = 0; + SKP_int nSamplesToBuffer, Complexity, input_ms, nSamplesFromInput = 0; + SKP_int32 TargetRate_bps; + SKP_int16 MaxBytesOut; + SKP_Silk_encoder_state_FIX *psEnc = ( SKP_Silk_encoder_state_FIX* )encState; + + + SKP_assert( encControl != NULL ); + + /* Check sampling frequency first, to avoid divide by zero later */ + if( ( encControl->sampleRate != 8000 ) && ( encControl->sampleRate != 12000 ) && + ( encControl->sampleRate != 16000 ) && ( encControl->sampleRate != 24000 ) ) { + ret = SKP_SILK_ENC_FS_NOT_SUPPORTED; + SKP_assert( 0 ); + return( ret ); + } + + /* Set Encoder parameters from Control structure */ + API_fs_kHz = SKP_DIV32_16( ( SKP_int )encControl->sampleRate, 1000 ); /* convert Hz -> kHz */ + PacketSize_ms = SKP_DIV32_16( ( SKP_int )encControl->packetSize, API_fs_kHz ); /* convert samples -> ms */ + TargetRate_bps = ( SKP_int32 )encControl->bitRate; + PacketLoss_perc = ( SKP_int )encControl->packetLossPercentage; + UseInBandFec = ( SKP_int )encControl->useInBandFEC; + Complexity = ( SKP_int )encControl->complexity; + UseDTX = ( SKP_int )encControl->useDTX; + + /* Only accept input lengths that are multiplum of 10 ms */ + input_ms = SKP_DIV32_16( nSamplesIn, API_fs_kHz ); + if( ( input_ms % 10) != 0 || nSamplesIn < 0 ) { + ret = SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; + SKP_assert( 0 ); + return( ret ); + } + + /* Make sure no more than one packet can be produced */ + if( nSamplesIn > SKP_SMULBB( psEnc->sCmn.PacketSize_ms, API_fs_kHz ) ) { + ret = SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; + SKP_assert( 0 ); + return( ret ); + } + + if( ( ret = SKP_Silk_control_encoder_FIX( psEnc, API_fs_kHz, PacketSize_ms, TargetRate_bps, + PacketLoss_perc, UseInBandFec, UseDTX, input_ms, Complexity ) ) != 0 ) { + SKP_assert( 0 ); + return( ret ); + } + + /* Detect energy above 8 kHz */ + if( encControl->sampleRate == 24000 && psEnc->sCmn.sSWBdetect.SWB_detected == 0 && psEnc->sCmn.sSWBdetect.WB_detected == 0 ) { + SKP_Silk_detect_SWB_input( &psEnc->sCmn.sSWBdetect, samplesIn, ( SKP_int )nSamplesIn ); + } + + /* Input buffering/resampling and encoding */ + MaxBytesOut = 0; /* return 0 output bytes if no encoder called */ + while( 1 ) { + /* Resample/buffer */ + nSamplesToBuffer = psEnc->sCmn.frame_length - psEnc->sCmn.inputBufIx; + if( encControl->sampleRate == SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) ) { + /* Same sample frequency - copy the data */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, nSamplesIn ); + nSamplesFromInput = nSamplesToBuffer; + SKP_memcpy( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], samplesIn, SKP_SMULBB( nSamplesToBuffer, sizeof( SKP_int16 ) ) ); + } else if( encControl->sampleRate == 24000 && psEnc->sCmn.fs_kHz == 16 ) { + /* Resample the data from 24 kHz to 16 kHz */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_SMULWB( SKP_LSHIFT( nSamplesIn, 1 ), 21846 ) ); // 21846 = ceil(2/3)*2^15 + nSamplesFromInput = SKP_RSHIFT( SKP_SMULBB( nSamplesToBuffer, 3 ), 1 ); +#if LOW_COMPLEXITY_ONLY + { + SKP_int16 scratch[ MAX_FRAME_LENGTH + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; + SKP_assert( nSamplesFromInput <= MAX_FRAME_LENGTH ); + SKP_Silk_resample_2_3_coarse( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample24To16state, + samplesIn, nSamplesFromInput, scratch ); + } +#else + SKP_Silk_resample_2_3( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample24To16state, + samplesIn, nSamplesFromInput ); +#endif + } else if( encControl->sampleRate == 24000 && psEnc->sCmn.fs_kHz == 12 ) { + SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ]; + /* Resample the data from 24 kHz to 12 kHz */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_RSHIFT( nSamplesIn, 1 ) ); + nSamplesFromInput = SKP_LSHIFT16( nSamplesToBuffer, 1 ); + SKP_Silk_resample_1_2_coarse( samplesIn, psEnc->sCmn.resample24To12state, + &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], scratch, nSamplesToBuffer ); + } else if( encControl->sampleRate == 24000 && psEnc->sCmn.fs_kHz == 8 ) { + /* Resample the data from 24 kHz to 8 kHz */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_DIV32_16( nSamplesIn, 3 ) ); + nSamplesFromInput = SKP_SMULBB( nSamplesToBuffer, 3 ); + SKP_Silk_resample_1_3( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample24To8state, + samplesIn, nSamplesFromInput); + } else if( encControl->sampleRate == 16000 && psEnc->sCmn.fs_kHz == 12 ) { + /* Resample the data from 16 kHz to 12 kHz */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_RSHIFT( SKP_SMULBB( nSamplesIn, 3 ), 2 ) ); + nSamplesFromInput = SKP_SMULWB( SKP_LSHIFT16( nSamplesToBuffer, 2 ), 21846 ); // 21846 = ceil((1/3)*2^16) + SKP_Silk_resample_3_4( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample16To12state, + samplesIn, nSamplesFromInput ); + } else if( encControl->sampleRate == 16000 && psEnc->sCmn.fs_kHz == 8 ) { + SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ]; + /* Resample the data from 16 kHz to 8 kHz */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_RSHIFT( nSamplesIn, 1 ) ); + nSamplesFromInput = SKP_LSHIFT16( nSamplesToBuffer, 1 ); + SKP_Silk_resample_1_2_coarse( samplesIn, psEnc->sCmn.resample16To8state, + &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], scratch, nSamplesToBuffer ); + } else if( encControl->sampleRate == 12000 && psEnc->sCmn.fs_kHz == 8 ) { + /* Resample the data from 12 kHz to 8 kHz */ + nSamplesToBuffer = SKP_min_int( nSamplesToBuffer, SKP_SMULWB( SKP_LSHIFT( nSamplesIn, 1 ), 21846 ) ); + nSamplesFromInput = SKP_RSHIFT( SKP_SMULBB( nSamplesToBuffer, 3 ), 1 ); +#if LOW_COMPLEXITY_ONLY + { + SKP_int16 scratch[ MAX_FRAME_LENGTH + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; + SKP_assert( nSamplesFromInput <= MAX_FRAME_LENGTH ); + SKP_Silk_resample_2_3_coarse( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample12To8state, + samplesIn, nSamplesFromInput, scratch ); + } +#else + SKP_Silk_resample_2_3( &psEnc->sCmn.inputBuf[ psEnc->sCmn.inputBufIx ], psEnc->sCmn.resample12To8state, + samplesIn, nSamplesFromInput ); +#endif + } + samplesIn += nSamplesFromInput; + nSamplesIn -= nSamplesFromInput; + psEnc->sCmn.inputBufIx += nSamplesToBuffer; + + /* Silk encoder */ + if( psEnc->sCmn.inputBufIx >= psEnc->sCmn.frame_length ) { + /* Enough data in input buffer, so encode */ + if( MaxBytesOut == 0 ) { + /* No payload obtained so far */ + MaxBytesOut = *nBytesOut; + if( ( ret = SKP_Silk_encode_frame_FIX( psEnc, outData, &MaxBytesOut, psEnc->sCmn.inputBuf ) ) != 0 ) { + SKP_assert( 0 ); + } + } else { + /* outData already contains a payload */ + if( ( ret = SKP_Silk_encode_frame_FIX( psEnc, outData, nBytesOut, psEnc->sCmn.inputBuf ) ) != 0 ) { + SKP_assert( 0 ); + } + /* Check that no second payload was created */ + SKP_assert( *nBytesOut == 0 ); + } + psEnc->sCmn.inputBufIx = 0; + } else { + break; + } + } + + *nBytesOut = MaxBytesOut; + if( psEnc->sCmn.useDTX && psEnc->sCmn.inDTX ) { + /* Dtx simulation */ + *nBytesOut = 0; + } + + + return ret; +} + diff --git a/jni/silk/src/SKP_Silk_encode_frame_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_encode_frame_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_encode_frame_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_encode_frame_FIX.c index 4f97ec5..aa1501a 100644 --- a/jni/silk/src/SKP_Silk_encode_frame_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_encode_frame_FIX.c @@ -1,432 +1,432 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" -/****************/ -/* Encode frame */ -/****************/ -SKP_int SKP_Silk_encode_frame_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ - SKP_uint8 *pCode, /* O Pointer to payload */ - SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes */ - /* input: max length; output: used */ - const SKP_int16 *pIn /* I Pointer to input speech frame */ -) -{ - SKP_Silk_encoder_control_FIX sEncCtrl; - SKP_int i, nBytes, ret = 0; - SKP_int16 *x_frame, *res_pitch_frame; - SKP_int16 xfw[ MAX_FRAME_LENGTH ]; - SKP_int16 pIn_HP[ MAX_FRAME_LENGTH ]; - SKP_int16 res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ]; - SKP_int LBRR_idx, frame_terminator, SNR_dB_Q7; - const SKP_uint16 *FrameTermination_CDF; - - /* Low bitrate redundancy parameters */ - SKP_uint8 LBRRpayload[ MAX_ARITHM_BYTES ]; - SKP_int16 nBytesLBRR; - - //SKP_int32 Seed[ MAX_LAYERS ]; - sEncCtrl.sCmn.Seed = psEnc->sCmn.frameCounter++ & 3; - - - /**************************************************************/ - /* Setup Input Pointers, and insert frame in input buffer */ - /*************************************************************/ - x_frame = psEnc->x_buf + psEnc->sCmn.frame_length; /* start of frame to encode */ - res_pitch_frame = res_pitch + psEnc->sCmn.frame_length; /* start of pitch LPC residual frame */ - - /****************************/ - /* Voice Activity Detection */ - /****************************/ - ret = SKP_Silk_VAD_GetSA_Q8( &psEnc->sCmn.sVAD, &psEnc->speech_activity_Q8, &SNR_dB_Q7, - sEncCtrl.input_quality_bands_Q15, &sEncCtrl.input_tilt_Q15, - pIn,psEnc->sCmn.frame_length ); - - /*******************************************/ - /* High-pass filtering of the input signal */ - /*******************************************/ -#if HIGH_PASS_INPUT - /* Variable high-pass filter */ - SKP_Silk_HP_variable_cutoff_FIX( psEnc, &sEncCtrl, pIn_HP, pIn ); -#else - SKP_memcpy( pIn_HP, pIn,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) ); -#endif - -#if SWITCH_TRANSITION_FILTERING - /* Ensure smooth bandwidth transitions */ - SKP_Silk_LP_variable_cutoff( &psEnc->sCmn.sLP, x_frame + psEnc->sCmn.la_shape, pIn_HP, psEnc->sCmn.frame_length ); -#else - SKP_memcpy( x_frame + psEnc->sCmn.la_shape, pIn_HP,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) ); -#endif - - /*****************************************/ - /* Find pitch lags, initial LPC analysis */ - /*****************************************/ - SKP_Silk_find_pitch_lags_FIX( psEnc, &sEncCtrl, res_pitch, x_frame ); - - /************************/ - /* Noise shape analysis */ - /************************/ - SKP_Silk_noise_shape_analysis_FIX( psEnc, &sEncCtrl, res_pitch_frame, x_frame ); - - /*****************************************/ - /* Prefiltering for noise shaper */ - /*****************************************/ - SKP_Silk_prefilter_FIX( psEnc, &sEncCtrl, xfw, x_frame ); - - - /***************************************************/ - /* Find linear prediction coefficients (LPC + LTP) */ - /***************************************************/ - SKP_Silk_find_pred_coefs_FIX( psEnc, &sEncCtrl, res_pitch ); - - /****************************************/ - /* Process gains */ - /****************************************/ - SKP_Silk_process_gains_FIX( psEnc, &sEncCtrl ); - - psEnc->sCmn.sigtype[ psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.sigtype; - psEnc->sCmn.QuantOffsetType[ psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.QuantOffsetType; - - /****************************************/ - /* Low Bitrate Redundant Encoding */ - /****************************************/ - nBytesLBRR = MAX_ARITHM_BYTES; - SKP_Silk_LBRR_encode_FIX( psEnc, &sEncCtrl, LBRRpayload, &nBytesLBRR, xfw ); - - /*****************************************/ - /* Noise shaping quantization */ - /*****************************************/ - psEnc->NoiseShapingQuantizer( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sNSQ, xfw, - &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], sEncCtrl.sCmn.NLSFInterpCoef_Q2, - sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR2_Q13, sEncCtrl.HarmShapeGain_Q14, - sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.Lambda_Q10, - sEncCtrl.LTP_scale_Q14 ); - - /**************************************************/ - /* Convert speech activity into VAD and DTX flags */ - /**************************************************/ - if( psEnc->speech_activity_Q8 < SPEECH_ACTIVITY_DTX_THRES_Q8 ) { - psEnc->sCmn.vadFlag = NO_VOICE_ACTIVITY; - psEnc->sCmn.noSpeechCounter++; - if( psEnc->sCmn.noSpeechCounter > NO_SPEECH_FRAMES_BEFORE_DTX ) { - psEnc->sCmn.inDTX = 1; - } - if( psEnc->sCmn.noSpeechCounter > MAX_CONSECUTIVE_DTX ) { - psEnc->sCmn.noSpeechCounter = 0; - psEnc->sCmn.inDTX = 0; - } - } else { - psEnc->sCmn.noSpeechCounter = 0; - psEnc->sCmn.inDTX = 0; - psEnc->sCmn.vadFlag = VOICE_ACTIVITY; - } - - /****************************************/ - /* Initialize arithmetic coder */ - /****************************************/ - if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { - SKP_Silk_range_enc_init( &psEnc->sCmn.sRC ); - psEnc->sCmn.nBytesInPayloadBuf = 0; - } - - /****************************************/ - /* Encode Parameters */ - /****************************************/ - if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { - SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC ); - FrameTermination_CDF = SKP_Silk_FrameTermination_v4_CDF; - } else { - SKP_Silk_encode_parameters( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC, - &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ] ); - FrameTermination_CDF = SKP_Silk_FrameTermination_CDF; - } - - /****************************************/ - /* Update Buffers and State */ - /****************************************/ - /* Update Input buffer */ - SKP_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], ( psEnc->sCmn.frame_length + psEnc->sCmn.la_shape ) * sizeof( SKP_int16 ) ); - - /* parameters needed for next frame */ - psEnc->sCmn.prev_sigtype = sEncCtrl.sCmn.sigtype; - psEnc->sCmn.prevLag = sEncCtrl.sCmn.pitchL[ NB_SUBFR - 1]; - psEnc->sCmn.first_frame_after_reset = 0; - - if( psEnc->sCmn.sRC.error ) { - /* encoder returned error: clear payload buffer */ - psEnc->sCmn.nFramesInPayloadBuf = 0; - } else { - psEnc->sCmn.nFramesInPayloadBuf++; - } - - /****************************************/ - /* finalize payload and copy to output */ - /****************************************/ - if( psEnc->sCmn.nFramesInPayloadBuf * FRAME_LENGTH_MS >= psEnc->sCmn.PacketSize_ms ) { - - LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK; - - /* Check if FEC information should be added */ - frame_terminator = SKP_SILK_LAST_FRAME; - if( psEnc->sCmn.LBRR_buffer[ LBRR_idx ].usage == SKP_SILK_ADD_LBRR_TO_PLUS1 ) { - frame_terminator = SKP_SILK_LBRR_VER1; - } - if( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage == SKP_SILK_ADD_LBRR_TO_PLUS2 ) { - frame_terminator = SKP_SILK_LBRR_VER2; - LBRR_idx = psEnc->sCmn.oldest_LBRR_idx; - } - /* Add the frame termination info to stream */ - SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF ); - - if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { - /* Code excitation signal */ - for( i = 0; i sCmn.nFramesInPayloadBuf; i++ ) { - SKP_Silk_encode_pulses( &psEnc->sCmn.sRC, psEnc->sCmn.sigtype[ i ],psEnc->sCmn.QuantOffsetType[ i ], - &psEnc->sCmn.q[ i * psEnc->sCmn.frame_length],psEnc->sCmn.frame_length ); - } - } - /* payload length so far */ - SKP_Silk_range_coder_get_length( &psEnc->sCmn.sRC, &nBytes ); - - /* check that there is enough space in external output buffer, and move data */ - if( *pnBytesOut >= nBytes ) { - SKP_Silk_range_enc_wrap_up( &psEnc->sCmn.sRC ); - SKP_memcpy( pCode, psEnc->sCmn.sRC.buffer, nBytes * sizeof( SKP_uint8 ) ); - - if( frame_terminator > SKP_SILK_MORE_FRAMES && - *pnBytesOut >= nBytes + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes ) { - /* Get old packet and add to payload. */ - SKP_memcpy( &pCode[ nBytes ], - psEnc->sCmn.LBRR_buffer[ LBRR_idx ].payload, - psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes * sizeof( SKP_uint8 ) ); - nBytes += psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes; - } - - *pnBytesOut = nBytes; - - /* Update FEC buffer */ - SKP_memcpy( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].payload, LBRRpayload, - nBytesLBRR * sizeof( SKP_uint8 ) ); - psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].nBytes = nBytesLBRR; - /* This line tells describes how FEC should be used */ - psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage = sEncCtrl.sCmn.LBRR_usage; - psEnc->sCmn.oldest_LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK; - - /* Reset number of frames in payload buffer */ - psEnc->sCmn.nFramesInPayloadBuf = 0; - } else { - /* Not enough space: Payload will be discarded */ - *pnBytesOut = 0; - nBytes = 0; - psEnc->sCmn.nFramesInPayloadBuf = 0; - ret = SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT; - } - } else { - /* no payload for you this time */ - *pnBytesOut = 0; - - /* Encode that more frames follows */ - frame_terminator = SKP_SILK_MORE_FRAMES; - SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF ); - - /* payload length so far */ - SKP_Silk_range_coder_get_length( &psEnc->sCmn.sRC, &nBytes ); - - if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { - /* Take into account the q signal that isnt in the bitstream yet */ - nBytes += SKP_Silk_pulses_to_bytes( &psEnc->sCmn, - &psEnc->sCmn.q[ (psEnc->sCmn.nFramesInPayloadBuf - 1) * psEnc->sCmn.frame_length ] ); - } - } - - /* Check for arithmetic coder errors */ - if( psEnc->sCmn.sRC.error ) { - ret = SKP_SILK_ENC_INTERNAL_ERROR; - } - - /* simulate number of ms buffered in channel because of exceeding TargetRate */ - SKP_assert( ( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) == - SKP_SAT32( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) ); - SKP_assert( psEnc->sCmn.TargetRate_bps > 0 ); - psEnc->BufferedInChannel_ms += SKP_DIV32( 8 * 1000 * ( nBytes -psEnc->sCmn.nBytesInPayloadBuf ),psEnc->sCmn.TargetRate_bps ); - psEnc->BufferedInChannel_ms -= FRAME_LENGTH_MS; - psEnc->BufferedInChannel_ms = SKP_LIMIT( psEnc->BufferedInChannel_ms, 0, 100 ); - psEnc->sCmn.nBytesInPayloadBuf = nBytes; - - if( psEnc->speech_activity_Q8 > WB_DETECT_ACTIVE_SPEECH_LEVEL_THRES_Q8 ) { - psEnc->sCmn.sSWBdetect.ActiveSpeech_ms = SKP_ADD_POS_SAT32( psEnc->sCmn.sSWBdetect.ActiveSpeech_ms, FRAME_LENGTH_MS ); - } - - - return( ret ); -} - -/* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode residual with lower bitrate */ -void SKP_Silk_LBRR_encode_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Pointer to Silk encoder control struct */ - SKP_uint8 *pCode, /* O Pointer to payload */ - SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes */ - SKP_int16 xfw[] /* I Input signal */ -) -{ - SKP_int i, TempGainsIndices[ NB_SUBFR ], frame_terminator; - SKP_int nBytes, nFramesInPayloadBuf; - SKP_int32 TempGains_Q16[ NB_SUBFR ]; - SKP_int typeOffset, LTP_scaleIndex, Rate_only_parameters = 0; - /*******************************************/ - /* Control use of inband LBRR */ - /*******************************************/ - SKP_Silk_LBRR_ctrl_FIX( psEnc, psEncCtrl ); - - if( psEnc->sCmn.LBRR_enabled ) { - /* Save original Gains */ - SKP_memcpy( TempGainsIndices, psEncCtrl->sCmn.GainsIndices, NB_SUBFR * sizeof( SKP_int ) ); - SKP_memcpy( TempGains_Q16, psEncCtrl->Gains_Q16, NB_SUBFR * sizeof( SKP_int32 ) ); - - typeOffset = psEnc->sCmn.typeOffsetPrev; // Temp save as cannot be overwritten - LTP_scaleIndex = psEncCtrl->sCmn.LTP_scaleIndex; - - /* Set max rate where quant signal is encoded */ - if( psEnc->sCmn.fs_kHz == 8 ) { - Rate_only_parameters = 13500; - } else if( psEnc->sCmn.fs_kHz == 12 ) { - Rate_only_parameters = 15500; - } else if( psEnc->sCmn.fs_kHz == 16 ) { - Rate_only_parameters = 17500; - } else if( psEnc->sCmn.fs_kHz == 24 ) { - Rate_only_parameters = 19500; - } else { - SKP_assert( 0 ); - } - - if( psEnc->sCmn.Complexity > 0 && psEnc->sCmn.TargetRate_bps > Rate_only_parameters ) { - if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { - /* First frame in packet copy Everything */ - SKP_memcpy( &psEnc->sNSQ_LBRR, &psEnc->sNSQ, sizeof( SKP_Silk_nsq_state ) ); - - psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex; - /* Increase Gains to get target LBRR rate */ - psEncCtrl->sCmn.GainsIndices[ 0 ] = psEncCtrl->sCmn.GainsIndices[ 0 ] + psEnc->sCmn.LBRR_GainIncreases; - psEncCtrl->sCmn.GainsIndices[ 0 ] = SKP_LIMIT( psEncCtrl->sCmn.GainsIndices[ 0 ], 0, N_LEVELS_QGAIN - 1 ); - } - /* Decode to get Gains in sync with decoder */ - /* Overwrite unquantized gains with quantized gains */ - SKP_Silk_gains_dequant( psEncCtrl->Gains_Q16, psEncCtrl->sCmn.GainsIndices, - &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesInPayloadBuf ); - /*****************************************/ - /* Noise shaping quantization */ - /*****************************************/ - psEnc->NoiseShapingQuantizer( &psEnc->sCmn, &psEncCtrl->sCmn, - &psEnc->sNSQ_LBRR, xfw, &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ], - psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, - psEncCtrl->AR2_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, - psEncCtrl->Gains_Q16, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14 ); - } else { - SKP_memset( &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], 0, - psEnc->sCmn.frame_length * sizeof( SKP_int ) ); - psEncCtrl->sCmn.LTP_scaleIndex = 0; - } - /****************************************/ - /* Initialize arithmetic coder */ - /****************************************/ - if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { - SKP_Silk_range_enc_init( &psEnc->sCmn.sRC_LBRR ); - psEnc->sCmn.nBytesInPayloadBuf = 0; - } - - /****************************************/ - /* Encode Parameters */ - /****************************************/ - if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { - SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR ); - } else { - SKP_Silk_encode_parameters( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR, - &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length] ); - } - - if( psEnc->sCmn.sRC_LBRR.error ) { - /* encoder returned error: clear payload buffer */ - nFramesInPayloadBuf = 0; - } else { - nFramesInPayloadBuf = psEnc->sCmn.nFramesInPayloadBuf + 1; - } - - /****************************************/ - /* finalize payload and copy to output */ - /****************************************/ - if( SKP_SMULBB( nFramesInPayloadBuf, FRAME_LENGTH_MS ) >= psEnc->sCmn.PacketSize_ms ) { - - /* Check if FEC information should be added */ - frame_terminator = SKP_SILK_LAST_FRAME; - - /* Add the frame termination info to stream */ - SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_CDF ); - - if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { - /*********************************************/ - /* Encode quantization indices of excitation */ - /*********************************************/ - for( i = 0; i < nFramesInPayloadBuf; i++ ) { - SKP_Silk_encode_pulses( &psEnc->sCmn.sRC_LBRR, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ], - &psEnc->sCmn.q_LBRR[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length ); - } - } - /* payload length so far */ - SKP_Silk_range_coder_get_length( &psEnc->sCmn.sRC_LBRR, &nBytes ); - - /* check that there is enough space in external output buffer, and move data */ - if( *pnBytesOut >= nBytes ) { - SKP_Silk_range_enc_wrap_up( &psEnc->sCmn.sRC_LBRR ); - SKP_memcpy( pCode, psEnc->sCmn.sRC_LBRR.buffer, nBytes * sizeof( SKP_uint8 ) ); - - *pnBytesOut = nBytes; - } else { - /* not enough space: payload will be discarded */ - *pnBytesOut = 0; - SKP_assert( 0 ); - } - } else { - /* no payload for you this time */ - *pnBytesOut = 0; - - /* Encode that more frames follows */ - frame_terminator = SKP_SILK_MORE_FRAMES; - SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_CDF ); - } - - /* Restore original Gains */ - SKP_memcpy( psEncCtrl->sCmn.GainsIndices, TempGainsIndices, NB_SUBFR * sizeof( SKP_int ) ); - SKP_memcpy( psEncCtrl->Gains_Q16, TempGains_Q16, NB_SUBFR * sizeof( SKP_int32 ) ); - - /* Restore LTP scale index and typeoffset */ - psEncCtrl->sCmn.LTP_scaleIndex = LTP_scaleIndex; - psEnc->sCmn.typeOffsetPrev = typeOffset; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" +/****************/ +/* Encode frame */ +/****************/ +SKP_int SKP_Silk_encode_frame_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ + SKP_uint8 *pCode, /* O Pointer to payload */ + SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes */ + /* input: max length; output: used */ + const SKP_int16 *pIn /* I Pointer to input speech frame */ +) +{ + SKP_Silk_encoder_control_FIX sEncCtrl; + SKP_int i, nBytes, ret = 0; + SKP_int16 *x_frame, *res_pitch_frame; + SKP_int16 xfw[ MAX_FRAME_LENGTH ]; + SKP_int16 pIn_HP[ MAX_FRAME_LENGTH ]; + SKP_int16 res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ]; + SKP_int LBRR_idx, frame_terminator, SNR_dB_Q7; + const SKP_uint16 *FrameTermination_CDF; + + /* Low bitrate redundancy parameters */ + SKP_uint8 LBRRpayload[ MAX_ARITHM_BYTES ]; + SKP_int16 nBytesLBRR; + + //SKP_int32 Seed[ MAX_LAYERS ]; + sEncCtrl.sCmn.Seed = psEnc->sCmn.frameCounter++ & 3; + + + /**************************************************************/ + /* Setup Input Pointers, and insert frame in input buffer */ + /*************************************************************/ + x_frame = psEnc->x_buf + psEnc->sCmn.frame_length; /* start of frame to encode */ + res_pitch_frame = res_pitch + psEnc->sCmn.frame_length; /* start of pitch LPC residual frame */ + + /****************************/ + /* Voice Activity Detection */ + /****************************/ + ret = SKP_Silk_VAD_GetSA_Q8( &psEnc->sCmn.sVAD, &psEnc->speech_activity_Q8, &SNR_dB_Q7, + sEncCtrl.input_quality_bands_Q15, &sEncCtrl.input_tilt_Q15, + pIn,psEnc->sCmn.frame_length ); + + /*******************************************/ + /* High-pass filtering of the input signal */ + /*******************************************/ +#if HIGH_PASS_INPUT + /* Variable high-pass filter */ + SKP_Silk_HP_variable_cutoff_FIX( psEnc, &sEncCtrl, pIn_HP, pIn ); +#else + SKP_memcpy( pIn_HP, pIn,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) ); +#endif + +#if SWITCH_TRANSITION_FILTERING + /* Ensure smooth bandwidth transitions */ + SKP_Silk_LP_variable_cutoff( &psEnc->sCmn.sLP, x_frame + psEnc->sCmn.la_shape, pIn_HP, psEnc->sCmn.frame_length ); +#else + SKP_memcpy( x_frame + psEnc->sCmn.la_shape, pIn_HP,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) ); +#endif + + /*****************************************/ + /* Find pitch lags, initial LPC analysis */ + /*****************************************/ + SKP_Silk_find_pitch_lags_FIX( psEnc, &sEncCtrl, res_pitch, x_frame ); + + /************************/ + /* Noise shape analysis */ + /************************/ + SKP_Silk_noise_shape_analysis_FIX( psEnc, &sEncCtrl, res_pitch_frame, x_frame ); + + /*****************************************/ + /* Prefiltering for noise shaper */ + /*****************************************/ + SKP_Silk_prefilter_FIX( psEnc, &sEncCtrl, xfw, x_frame ); + + + /***************************************************/ + /* Find linear prediction coefficients (LPC + LTP) */ + /***************************************************/ + SKP_Silk_find_pred_coefs_FIX( psEnc, &sEncCtrl, res_pitch ); + + /****************************************/ + /* Process gains */ + /****************************************/ + SKP_Silk_process_gains_FIX( psEnc, &sEncCtrl ); + + psEnc->sCmn.sigtype[ psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.sigtype; + psEnc->sCmn.QuantOffsetType[ psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.QuantOffsetType; + + /****************************************/ + /* Low Bitrate Redundant Encoding */ + /****************************************/ + nBytesLBRR = MAX_ARITHM_BYTES; + SKP_Silk_LBRR_encode_FIX( psEnc, &sEncCtrl, LBRRpayload, &nBytesLBRR, xfw ); + + /*****************************************/ + /* Noise shaping quantization */ + /*****************************************/ + psEnc->NoiseShapingQuantizer( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sNSQ, xfw, + &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], sEncCtrl.sCmn.NLSFInterpCoef_Q2, + sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR2_Q13, sEncCtrl.HarmShapeGain_Q14, + sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.Lambda_Q10, + sEncCtrl.LTP_scale_Q14 ); + + /**************************************************/ + /* Convert speech activity into VAD and DTX flags */ + /**************************************************/ + if( psEnc->speech_activity_Q8 < SPEECH_ACTIVITY_DTX_THRES_Q8 ) { + psEnc->sCmn.vadFlag = NO_VOICE_ACTIVITY; + psEnc->sCmn.noSpeechCounter++; + if( psEnc->sCmn.noSpeechCounter > NO_SPEECH_FRAMES_BEFORE_DTX ) { + psEnc->sCmn.inDTX = 1; + } + if( psEnc->sCmn.noSpeechCounter > MAX_CONSECUTIVE_DTX ) { + psEnc->sCmn.noSpeechCounter = 0; + psEnc->sCmn.inDTX = 0; + } + } else { + psEnc->sCmn.noSpeechCounter = 0; + psEnc->sCmn.inDTX = 0; + psEnc->sCmn.vadFlag = VOICE_ACTIVITY; + } + + /****************************************/ + /* Initialize arithmetic coder */ + /****************************************/ + if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { + SKP_Silk_range_enc_init( &psEnc->sCmn.sRC ); + psEnc->sCmn.nBytesInPayloadBuf = 0; + } + + /****************************************/ + /* Encode Parameters */ + /****************************************/ + if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { + SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC ); + FrameTermination_CDF = SKP_Silk_FrameTermination_v4_CDF; + } else { + SKP_Silk_encode_parameters( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC, + &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ] ); + FrameTermination_CDF = SKP_Silk_FrameTermination_CDF; + } + + /****************************************/ + /* Update Buffers and State */ + /****************************************/ + /* Update Input buffer */ + SKP_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], ( psEnc->sCmn.frame_length + psEnc->sCmn.la_shape ) * sizeof( SKP_int16 ) ); + + /* parameters needed for next frame */ + psEnc->sCmn.prev_sigtype = sEncCtrl.sCmn.sigtype; + psEnc->sCmn.prevLag = sEncCtrl.sCmn.pitchL[ NB_SUBFR - 1]; + psEnc->sCmn.first_frame_after_reset = 0; + + if( psEnc->sCmn.sRC.error ) { + /* encoder returned error: clear payload buffer */ + psEnc->sCmn.nFramesInPayloadBuf = 0; + } else { + psEnc->sCmn.nFramesInPayloadBuf++; + } + + /****************************************/ + /* finalize payload and copy to output */ + /****************************************/ + if( psEnc->sCmn.nFramesInPayloadBuf * FRAME_LENGTH_MS >= psEnc->sCmn.PacketSize_ms ) { + + LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK; + + /* Check if FEC information should be added */ + frame_terminator = SKP_SILK_LAST_FRAME; + if( psEnc->sCmn.LBRR_buffer[ LBRR_idx ].usage == SKP_SILK_ADD_LBRR_TO_PLUS1 ) { + frame_terminator = SKP_SILK_LBRR_VER1; + } + if( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage == SKP_SILK_ADD_LBRR_TO_PLUS2 ) { + frame_terminator = SKP_SILK_LBRR_VER2; + LBRR_idx = psEnc->sCmn.oldest_LBRR_idx; + } + /* Add the frame termination info to stream */ + SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF ); + + if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { + /* Code excitation signal */ + for( i = 0; i sCmn.nFramesInPayloadBuf; i++ ) { + SKP_Silk_encode_pulses( &psEnc->sCmn.sRC, psEnc->sCmn.sigtype[ i ],psEnc->sCmn.QuantOffsetType[ i ], + &psEnc->sCmn.q[ i * psEnc->sCmn.frame_length],psEnc->sCmn.frame_length ); + } + } + /* payload length so far */ + SKP_Silk_range_coder_get_length( &psEnc->sCmn.sRC, &nBytes ); + + /* check that there is enough space in external output buffer, and move data */ + if( *pnBytesOut >= nBytes ) { + SKP_Silk_range_enc_wrap_up( &psEnc->sCmn.sRC ); + SKP_memcpy( pCode, psEnc->sCmn.sRC.buffer, nBytes * sizeof( SKP_uint8 ) ); + + if( frame_terminator > SKP_SILK_MORE_FRAMES && + *pnBytesOut >= nBytes + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes ) { + /* Get old packet and add to payload. */ + SKP_memcpy( &pCode[ nBytes ], + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].payload, + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes * sizeof( SKP_uint8 ) ); + nBytes += psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes; + } + + *pnBytesOut = nBytes; + + /* Update FEC buffer */ + SKP_memcpy( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].payload, LBRRpayload, + nBytesLBRR * sizeof( SKP_uint8 ) ); + psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].nBytes = nBytesLBRR; + /* This line tells describes how FEC should be used */ + psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage = sEncCtrl.sCmn.LBRR_usage; + psEnc->sCmn.oldest_LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK; + + /* Reset number of frames in payload buffer */ + psEnc->sCmn.nFramesInPayloadBuf = 0; + } else { + /* Not enough space: Payload will be discarded */ + *pnBytesOut = 0; + nBytes = 0; + psEnc->sCmn.nFramesInPayloadBuf = 0; + ret = SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT; + } + } else { + /* no payload for you this time */ + *pnBytesOut = 0; + + /* Encode that more frames follows */ + frame_terminator = SKP_SILK_MORE_FRAMES; + SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF ); + + /* payload length so far */ + SKP_Silk_range_coder_get_length( &psEnc->sCmn.sRC, &nBytes ); + + if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { + /* Take into account the q signal that isnt in the bitstream yet */ + nBytes += SKP_Silk_pulses_to_bytes( &psEnc->sCmn, + &psEnc->sCmn.q[ (psEnc->sCmn.nFramesInPayloadBuf - 1) * psEnc->sCmn.frame_length ] ); + } + } + + /* Check for arithmetic coder errors */ + if( psEnc->sCmn.sRC.error ) { + ret = SKP_SILK_ENC_INTERNAL_ERROR; + } + + /* simulate number of ms buffered in channel because of exceeding TargetRate */ + SKP_assert( ( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) == + SKP_SAT32( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) ); + SKP_assert( psEnc->sCmn.TargetRate_bps > 0 ); + psEnc->BufferedInChannel_ms += SKP_DIV32( 8 * 1000 * ( nBytes -psEnc->sCmn.nBytesInPayloadBuf ),psEnc->sCmn.TargetRate_bps ); + psEnc->BufferedInChannel_ms -= FRAME_LENGTH_MS; + psEnc->BufferedInChannel_ms = SKP_LIMIT( psEnc->BufferedInChannel_ms, 0, 100 ); + psEnc->sCmn.nBytesInPayloadBuf = nBytes; + + if( psEnc->speech_activity_Q8 > WB_DETECT_ACTIVE_SPEECH_LEVEL_THRES_Q8 ) { + psEnc->sCmn.sSWBdetect.ActiveSpeech_ms = SKP_ADD_POS_SAT32( psEnc->sCmn.sSWBdetect.ActiveSpeech_ms, FRAME_LENGTH_MS ); + } + + + return( ret ); +} + +/* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode residual with lower bitrate */ +void SKP_Silk_LBRR_encode_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Pointer to Silk encoder control struct */ + SKP_uint8 *pCode, /* O Pointer to payload */ + SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes */ + SKP_int16 xfw[] /* I Input signal */ +) +{ + SKP_int i, TempGainsIndices[ NB_SUBFR ], frame_terminator; + SKP_int nBytes, nFramesInPayloadBuf; + SKP_int32 TempGains_Q16[ NB_SUBFR ]; + SKP_int typeOffset, LTP_scaleIndex, Rate_only_parameters = 0; + /*******************************************/ + /* Control use of inband LBRR */ + /*******************************************/ + SKP_Silk_LBRR_ctrl_FIX( psEnc, psEncCtrl ); + + if( psEnc->sCmn.LBRR_enabled ) { + /* Save original Gains */ + SKP_memcpy( TempGainsIndices, psEncCtrl->sCmn.GainsIndices, NB_SUBFR * sizeof( SKP_int ) ); + SKP_memcpy( TempGains_Q16, psEncCtrl->Gains_Q16, NB_SUBFR * sizeof( SKP_int32 ) ); + + typeOffset = psEnc->sCmn.typeOffsetPrev; // Temp save as cannot be overwritten + LTP_scaleIndex = psEncCtrl->sCmn.LTP_scaleIndex; + + /* Set max rate where quant signal is encoded */ + if( psEnc->sCmn.fs_kHz == 8 ) { + Rate_only_parameters = 13500; + } else if( psEnc->sCmn.fs_kHz == 12 ) { + Rate_only_parameters = 15500; + } else if( psEnc->sCmn.fs_kHz == 16 ) { + Rate_only_parameters = 17500; + } else if( psEnc->sCmn.fs_kHz == 24 ) { + Rate_only_parameters = 19500; + } else { + SKP_assert( 0 ); + } + + if( psEnc->sCmn.Complexity > 0 && psEnc->sCmn.TargetRate_bps > Rate_only_parameters ) { + if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { + /* First frame in packet copy Everything */ + SKP_memcpy( &psEnc->sNSQ_LBRR, &psEnc->sNSQ, sizeof( SKP_Silk_nsq_state ) ); + + psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex; + /* Increase Gains to get target LBRR rate */ + psEncCtrl->sCmn.GainsIndices[ 0 ] = psEncCtrl->sCmn.GainsIndices[ 0 ] + psEnc->sCmn.LBRR_GainIncreases; + psEncCtrl->sCmn.GainsIndices[ 0 ] = SKP_LIMIT( psEncCtrl->sCmn.GainsIndices[ 0 ], 0, N_LEVELS_QGAIN - 1 ); + } + /* Decode to get Gains in sync with decoder */ + /* Overwrite unquantized gains with quantized gains */ + SKP_Silk_gains_dequant( psEncCtrl->Gains_Q16, psEncCtrl->sCmn.GainsIndices, + &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesInPayloadBuf ); + /*****************************************/ + /* Noise shaping quantization */ + /*****************************************/ + psEnc->NoiseShapingQuantizer( &psEnc->sCmn, &psEncCtrl->sCmn, + &psEnc->sNSQ_LBRR, xfw, &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ], + psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, + psEncCtrl->AR2_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, + psEncCtrl->Gains_Q16, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14 ); + } else { + SKP_memset( &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], 0, + psEnc->sCmn.frame_length * sizeof( SKP_int ) ); + psEncCtrl->sCmn.LTP_scaleIndex = 0; + } + /****************************************/ + /* Initialize arithmetic coder */ + /****************************************/ + if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) { + SKP_Silk_range_enc_init( &psEnc->sCmn.sRC_LBRR ); + psEnc->sCmn.nBytesInPayloadBuf = 0; + } + + /****************************************/ + /* Encode Parameters */ + /****************************************/ + if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { + SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR ); + } else { + SKP_Silk_encode_parameters( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR, + &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length] ); + } + + if( psEnc->sCmn.sRC_LBRR.error ) { + /* encoder returned error: clear payload buffer */ + nFramesInPayloadBuf = 0; + } else { + nFramesInPayloadBuf = psEnc->sCmn.nFramesInPayloadBuf + 1; + } + + /****************************************/ + /* finalize payload and copy to output */ + /****************************************/ + if( SKP_SMULBB( nFramesInPayloadBuf, FRAME_LENGTH_MS ) >= psEnc->sCmn.PacketSize_ms ) { + + /* Check if FEC information should be added */ + frame_terminator = SKP_SILK_LAST_FRAME; + + /* Add the frame termination info to stream */ + SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_CDF ); + + if( psEnc->sCmn.bitstream_v == BIT_STREAM_V4 ) { + /*********************************************/ + /* Encode quantization indices of excitation */ + /*********************************************/ + for( i = 0; i < nFramesInPayloadBuf; i++ ) { + SKP_Silk_encode_pulses( &psEnc->sCmn.sRC_LBRR, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ], + &psEnc->sCmn.q_LBRR[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length ); + } + } + /* payload length so far */ + SKP_Silk_range_coder_get_length( &psEnc->sCmn.sRC_LBRR, &nBytes ); + + /* check that there is enough space in external output buffer, and move data */ + if( *pnBytesOut >= nBytes ) { + SKP_Silk_range_enc_wrap_up( &psEnc->sCmn.sRC_LBRR ); + SKP_memcpy( pCode, psEnc->sCmn.sRC_LBRR.buffer, nBytes * sizeof( SKP_uint8 ) ); + + *pnBytesOut = nBytes; + } else { + /* not enough space: payload will be discarded */ + *pnBytesOut = 0; + SKP_assert( 0 ); + } + } else { + /* no payload for you this time */ + *pnBytesOut = 0; + + /* Encode that more frames follows */ + frame_terminator = SKP_SILK_MORE_FRAMES; + SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_CDF ); + } + + /* Restore original Gains */ + SKP_memcpy( psEncCtrl->sCmn.GainsIndices, TempGainsIndices, NB_SUBFR * sizeof( SKP_int ) ); + SKP_memcpy( psEncCtrl->Gains_Q16, TempGains_Q16, NB_SUBFR * sizeof( SKP_int32 ) ); + + /* Restore LTP scale index and typeoffset */ + psEncCtrl->sCmn.LTP_scaleIndex = LTP_scaleIndex; + psEnc->sCmn.typeOffsetPrev = typeOffset; + } +} diff --git a/jni/silk/src/SKP_Silk_encode_parameters.c b/app/src/main/jni/silk/src/SKP_Silk_encode_parameters.c similarity index 97% rename from jni/silk/src/SKP_Silk_encode_parameters.c rename to app/src/main/jni/silk/src/SKP_Silk_encode_parameters.c index 4af3551..0fd3b7e 100644 --- a/jni/silk/src/SKP_Silk_encode_parameters.c +++ b/app/src/main/jni/silk/src/SKP_Silk_encode_parameters.c @@ -1,167 +1,167 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/*******************************************/ -/* Encode parameters to create the payload */ -/*******************************************/ -void SKP_Silk_encode_parameters( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ - SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ - SKP_Silk_range_coder_state *psRC, /* I/O Range encoder state */ - const SKP_int *q /* I Quantization indices */ -) -{ - SKP_int i, k, typeOffset; - const SKP_Silk_NLSF_CB_struct *psNLSF_CB; - - - /************************/ - /* Encode sampling rate */ - /************************/ - /* only done for first frame in packet */ - if( psEncC->nFramesInPayloadBuf == 0 ) { - - /* Initialize arithmetic coder */ - SKP_Silk_range_enc_init( &psEncC->sRC ); - psEncC->nBytesInPayloadBuf = 0; - - /* get sampling rate index */ - for( i = 0; i < 3; i++ ) { - if( SKP_Silk_SamplingRates_table[ i ] == psEncC->fs_kHz ) { - break; - } - } - SKP_Silk_range_encoder( psRC, i, SKP_Silk_SamplingRates_CDF ); - } - - /*******************************************/ - /* Encode signal type and quantizer offset */ - /*******************************************/ - typeOffset = 2 * psEncCtrlC->sigtype + psEncCtrlC->QuantOffsetType; - if( psEncC->nFramesInPayloadBuf == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_CDF ); - } else { - /* condidtional coding */ - SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ] ); - } - psEncC->typeOffsetPrev = typeOffset; - - /****************/ - /* Encode gains */ - /****************/ - /* first subframe */ - if( psEncC->nFramesInPayloadBuf == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ] ); - } else { - /* condidtional coding */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_delta_gain_CDF ); - } - - /* remaining subframes */ - for( i = 1; i < NB_SUBFR; i++ ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ i ], SKP_Silk_delta_gain_CDF ); - } - - - /****************/ - /* Encode NLSFs */ - /****************/ - /* Range encoding of the NLSF path */ - psNLSF_CB = psEncC->psNLSF_CB[ psEncCtrlC->sigtype ]; - SKP_Silk_range_encoder_multi( psRC, psEncCtrlC->NLSFIndices, psNLSF_CB->StartPtr, psNLSF_CB->nStages ); - - /* Encode NLSF interpolation factor */ - SKP_assert( psEncC->useInterpolatedNLSFs == 1 || psEncCtrlC->NLSFInterpCoef_Q2 == ( 1 << 2 ) ); - SKP_Silk_range_encoder( psRC, psEncCtrlC->NLSFInterpCoef_Q2, SKP_Silk_NLSF_interpolation_factor_CDF ); - - - if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { - /*********************/ - /* Encode pitch lags */ - /*********************/ - - - /* lag index */ - if( psEncC->fs_kHz == 8 ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_NB_CDF ); - } else if( psEncC->fs_kHz == 12 ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_MB_CDF ); - } else if( psEncC->fs_kHz == 16 ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_WB_CDF ); - } else { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_SWB_CDF ); - } - - - /* countour index */ - if( psEncC->fs_kHz == 8 ) { - /* Less codevectors used in 8 khz mode */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_NB_CDF ); - } else { - /* Joint for 12, 16, 24 khz */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_CDF ); - } - - /********************/ - /* Encode LTP gains */ - /********************/ - - /* PERIndex value */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->PERIndex, SKP_Silk_LTP_per_index_CDF ); - - /* Codebook Indices */ - for( k = 0; k < NB_SUBFR; k++ ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->LTPIndex[ k ], SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ] ); - } - - /**********************/ - /* Encode LTP scaling */ - /**********************/ - SKP_Silk_range_encoder( psRC, psEncCtrlC->LTP_scaleIndex, SKP_Silk_LTPscale_CDF ); - } - - - /***************/ - /* Encode seed */ - /***************/ - SKP_Silk_range_encoder( psRC, psEncCtrlC->Seed, SKP_Silk_Seed_CDF ); - - /*********************************************/ - /* Encode quantization indices of excitation */ - /*********************************************/ - SKP_Silk_encode_pulses( psRC, psEncCtrlC->sigtype, psEncCtrlC->QuantOffsetType, q, psEncC->frame_length ); - - - /*********************************************/ - /* Encode VAD flag */ - /*********************************************/ - SKP_Silk_range_encoder( psRC, psEncC->vadFlag, SKP_Silk_vadflag_CDF ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/*******************************************/ +/* Encode parameters to create the payload */ +/*******************************************/ +void SKP_Silk_encode_parameters( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ + SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ + SKP_Silk_range_coder_state *psRC, /* I/O Range encoder state */ + const SKP_int *q /* I Quantization indices */ +) +{ + SKP_int i, k, typeOffset; + const SKP_Silk_NLSF_CB_struct *psNLSF_CB; + + + /************************/ + /* Encode sampling rate */ + /************************/ + /* only done for first frame in packet */ + if( psEncC->nFramesInPayloadBuf == 0 ) { + + /* Initialize arithmetic coder */ + SKP_Silk_range_enc_init( &psEncC->sRC ); + psEncC->nBytesInPayloadBuf = 0; + + /* get sampling rate index */ + for( i = 0; i < 3; i++ ) { + if( SKP_Silk_SamplingRates_table[ i ] == psEncC->fs_kHz ) { + break; + } + } + SKP_Silk_range_encoder( psRC, i, SKP_Silk_SamplingRates_CDF ); + } + + /*******************************************/ + /* Encode signal type and quantizer offset */ + /*******************************************/ + typeOffset = 2 * psEncCtrlC->sigtype + psEncCtrlC->QuantOffsetType; + if( psEncC->nFramesInPayloadBuf == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_CDF ); + } else { + /* condidtional coding */ + SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ] ); + } + psEncC->typeOffsetPrev = typeOffset; + + /****************/ + /* Encode gains */ + /****************/ + /* first subframe */ + if( psEncC->nFramesInPayloadBuf == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ] ); + } else { + /* condidtional coding */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_delta_gain_CDF ); + } + + /* remaining subframes */ + for( i = 1; i < NB_SUBFR; i++ ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ i ], SKP_Silk_delta_gain_CDF ); + } + + + /****************/ + /* Encode NLSFs */ + /****************/ + /* Range encoding of the NLSF path */ + psNLSF_CB = psEncC->psNLSF_CB[ psEncCtrlC->sigtype ]; + SKP_Silk_range_encoder_multi( psRC, psEncCtrlC->NLSFIndices, psNLSF_CB->StartPtr, psNLSF_CB->nStages ); + + /* Encode NLSF interpolation factor */ + SKP_assert( psEncC->useInterpolatedNLSFs == 1 || psEncCtrlC->NLSFInterpCoef_Q2 == ( 1 << 2 ) ); + SKP_Silk_range_encoder( psRC, psEncCtrlC->NLSFInterpCoef_Q2, SKP_Silk_NLSF_interpolation_factor_CDF ); + + + if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { + /*********************/ + /* Encode pitch lags */ + /*********************/ + + + /* lag index */ + if( psEncC->fs_kHz == 8 ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_NB_CDF ); + } else if( psEncC->fs_kHz == 12 ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_MB_CDF ); + } else if( psEncC->fs_kHz == 16 ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_WB_CDF ); + } else { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_SWB_CDF ); + } + + + /* countour index */ + if( psEncC->fs_kHz == 8 ) { + /* Less codevectors used in 8 khz mode */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_NB_CDF ); + } else { + /* Joint for 12, 16, 24 khz */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_CDF ); + } + + /********************/ + /* Encode LTP gains */ + /********************/ + + /* PERIndex value */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->PERIndex, SKP_Silk_LTP_per_index_CDF ); + + /* Codebook Indices */ + for( k = 0; k < NB_SUBFR; k++ ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->LTPIndex[ k ], SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ] ); + } + + /**********************/ + /* Encode LTP scaling */ + /**********************/ + SKP_Silk_range_encoder( psRC, psEncCtrlC->LTP_scaleIndex, SKP_Silk_LTPscale_CDF ); + } + + + /***************/ + /* Encode seed */ + /***************/ + SKP_Silk_range_encoder( psRC, psEncCtrlC->Seed, SKP_Silk_Seed_CDF ); + + /*********************************************/ + /* Encode quantization indices of excitation */ + /*********************************************/ + SKP_Silk_encode_pulses( psRC, psEncCtrlC->sigtype, psEncCtrlC->QuantOffsetType, q, psEncC->frame_length ); + + + /*********************************************/ + /* Encode VAD flag */ + /*********************************************/ + SKP_Silk_range_encoder( psRC, psEncC->vadFlag, SKP_Silk_vadflag_CDF ); +} diff --git a/jni/silk/src/SKP_Silk_encode_parameters_v4.c b/app/src/main/jni/silk/src/SKP_Silk_encode_parameters_v4.c similarity index 97% rename from jni/silk/src/SKP_Silk_encode_parameters_v4.c rename to app/src/main/jni/silk/src/SKP_Silk_encode_parameters_v4.c index c6786b8..4694c65 100644 --- a/jni/silk/src/SKP_Silk_encode_parameters_v4.c +++ b/app/src/main/jni/silk/src/SKP_Silk_encode_parameters_v4.c @@ -1,179 +1,179 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/*******************************************/ -/* Encode parameters to create the payload */ -/*******************************************/ -void SKP_Silk_encode_parameters_v4( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ - SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ - SKP_Silk_range_coder_state *psRC /* I/O Range encoder state */ -) -{ - SKP_int i, k, typeOffset; - SKP_int encode_absolute_lagIndex, delta_lagIndex; - const SKP_Silk_NLSF_CB_struct *psNLSF_CB; - - - /************************/ - /* Encode sampling rate */ - /************************/ - /* only done for first frame in packet */ - if( psEncC->nFramesInPayloadBuf == 0 ) { - - /* Initialize arithmetic coder */ - SKP_Silk_range_enc_init( &psEncC->sRC ); - psEncC->nBytesInPayloadBuf = 0; - - /* get sampling rate index */ - for( i = 0; i < 3; i++ ) { - if( SKP_Silk_SamplingRates_table[ i ] == psEncC->fs_kHz ) { - break; - } - } - SKP_Silk_range_encoder( psRC, i, SKP_Silk_SamplingRates_CDF ); - } - - /*********************************************/ - /* Encode VAD flag */ - /*********************************************/ - SKP_Silk_range_encoder( psRC, psEncC->vadFlag, SKP_Silk_vadflag_CDF ); - - /*******************************************/ - /* Encode signal type and quantizer offset */ - /*******************************************/ - typeOffset = 2 * psEncCtrlC->sigtype + psEncCtrlC->QuantOffsetType; - if( psEncC->nFramesInPayloadBuf == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_CDF ); - } else { - /* condidtional coding */ - SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ] ); - } - psEncC->typeOffsetPrev = typeOffset; - - /****************/ - /* Encode gains */ - /****************/ - /* first subframe */ - if( psEncC->nFramesInPayloadBuf == 0 ) { - /* first frame in packet: independent coding */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ] ); - } else { - /* condidtional coding */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_delta_gain_CDF ); - } - - /* remaining subframes */ - for( i = 1; i < NB_SUBFR; i++ ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ i ], SKP_Silk_delta_gain_CDF ); - } - - - /****************/ - /* Encode NLSFs */ - /****************/ - /* Range encoding of the NLSF path */ - psNLSF_CB = psEncC->psNLSF_CB[ psEncCtrlC->sigtype ]; - SKP_Silk_range_encoder_multi( psRC, psEncCtrlC->NLSFIndices, psNLSF_CB->StartPtr, psNLSF_CB->nStages ); - - /* Encode NLSF interpolation factor */ - SKP_assert( psEncC->useInterpolatedNLSFs == 1 || psEncCtrlC->NLSFInterpCoef_Q2 == ( 1 << 2 ) ); - SKP_Silk_range_encoder( psRC, psEncCtrlC->NLSFInterpCoef_Q2, SKP_Silk_NLSF_interpolation_factor_CDF ); - - - if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { - /*********************/ - /* Encode pitch lags */ - /*********************/ - - - /* lag index */ - encode_absolute_lagIndex = 1; - if( psEncC->nFramesInPayloadBuf > 0 && psEncC->prev_sigtype == SIG_TYPE_VOICED ) { - /* Delta Encoding */ - delta_lagIndex = psEncCtrlC->lagIndex - psEncC->prev_lagIndex; - if( delta_lagIndex > MAX_DELTA_LAG ) { - delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1; - } else if ( delta_lagIndex < -MAX_DELTA_LAG ) { - delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1; - } else { - delta_lagIndex = delta_lagIndex + MAX_DELTA_LAG; - encode_absolute_lagIndex = 0; /* Only use delta */ - } - SKP_Silk_range_encoder( psRC, delta_lagIndex, SKP_Silk_pitch_delta_CDF ); - } - if( encode_absolute_lagIndex ) { - /* Absolute encoding */ - if( psEncC->fs_kHz == 8 ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_NB_CDF ); - } else if( psEncC->fs_kHz == 12 ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_MB_CDF ); - } else if( psEncC->fs_kHz == 16 ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_WB_CDF ); - } else { - SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_SWB_CDF ); - } - } - psEncC->prev_lagIndex = psEncCtrlC->lagIndex; - - - /* countour index */ - if( psEncC->fs_kHz == 8 ) { - /* Less codevectors used in 8 khz mode */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_NB_CDF ); - } else { - /* Joint for 12, 16, 24 khz */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_CDF ); - } - - /********************/ - /* Encode LTP gains */ - /********************/ - - /* PERIndex value */ - SKP_Silk_range_encoder( psRC, psEncCtrlC->PERIndex, SKP_Silk_LTP_per_index_CDF ); - - /* Codebook Indices */ - for( k = 0; k < NB_SUBFR; k++ ) { - SKP_Silk_range_encoder( psRC, psEncCtrlC->LTPIndex[ k ], SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ] ); - } - - /**********************/ - /* Encode LTP scaling */ - /**********************/ - SKP_Silk_range_encoder( psRC, psEncCtrlC->LTP_scaleIndex, SKP_Silk_LTPscale_CDF ); - } - - - /***************/ - /* Encode seed */ - /***************/ - SKP_Silk_range_encoder( psRC, psEncCtrlC->Seed, SKP_Silk_Seed_CDF ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/*******************************************/ +/* Encode parameters to create the payload */ +/*******************************************/ +void SKP_Silk_encode_parameters_v4( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ + SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ + SKP_Silk_range_coder_state *psRC /* I/O Range encoder state */ +) +{ + SKP_int i, k, typeOffset; + SKP_int encode_absolute_lagIndex, delta_lagIndex; + const SKP_Silk_NLSF_CB_struct *psNLSF_CB; + + + /************************/ + /* Encode sampling rate */ + /************************/ + /* only done for first frame in packet */ + if( psEncC->nFramesInPayloadBuf == 0 ) { + + /* Initialize arithmetic coder */ + SKP_Silk_range_enc_init( &psEncC->sRC ); + psEncC->nBytesInPayloadBuf = 0; + + /* get sampling rate index */ + for( i = 0; i < 3; i++ ) { + if( SKP_Silk_SamplingRates_table[ i ] == psEncC->fs_kHz ) { + break; + } + } + SKP_Silk_range_encoder( psRC, i, SKP_Silk_SamplingRates_CDF ); + } + + /*********************************************/ + /* Encode VAD flag */ + /*********************************************/ + SKP_Silk_range_encoder( psRC, psEncC->vadFlag, SKP_Silk_vadflag_CDF ); + + /*******************************************/ + /* Encode signal type and quantizer offset */ + /*******************************************/ + typeOffset = 2 * psEncCtrlC->sigtype + psEncCtrlC->QuantOffsetType; + if( psEncC->nFramesInPayloadBuf == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_CDF ); + } else { + /* condidtional coding */ + SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ] ); + } + psEncC->typeOffsetPrev = typeOffset; + + /****************/ + /* Encode gains */ + /****************/ + /* first subframe */ + if( psEncC->nFramesInPayloadBuf == 0 ) { + /* first frame in packet: independent coding */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ] ); + } else { + /* condidtional coding */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_delta_gain_CDF ); + } + + /* remaining subframes */ + for( i = 1; i < NB_SUBFR; i++ ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ i ], SKP_Silk_delta_gain_CDF ); + } + + + /****************/ + /* Encode NLSFs */ + /****************/ + /* Range encoding of the NLSF path */ + psNLSF_CB = psEncC->psNLSF_CB[ psEncCtrlC->sigtype ]; + SKP_Silk_range_encoder_multi( psRC, psEncCtrlC->NLSFIndices, psNLSF_CB->StartPtr, psNLSF_CB->nStages ); + + /* Encode NLSF interpolation factor */ + SKP_assert( psEncC->useInterpolatedNLSFs == 1 || psEncCtrlC->NLSFInterpCoef_Q2 == ( 1 << 2 ) ); + SKP_Silk_range_encoder( psRC, psEncCtrlC->NLSFInterpCoef_Q2, SKP_Silk_NLSF_interpolation_factor_CDF ); + + + if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) { + /*********************/ + /* Encode pitch lags */ + /*********************/ + + + /* lag index */ + encode_absolute_lagIndex = 1; + if( psEncC->nFramesInPayloadBuf > 0 && psEncC->prev_sigtype == SIG_TYPE_VOICED ) { + /* Delta Encoding */ + delta_lagIndex = psEncCtrlC->lagIndex - psEncC->prev_lagIndex; + if( delta_lagIndex > MAX_DELTA_LAG ) { + delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1; + } else if ( delta_lagIndex < -MAX_DELTA_LAG ) { + delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1; + } else { + delta_lagIndex = delta_lagIndex + MAX_DELTA_LAG; + encode_absolute_lagIndex = 0; /* Only use delta */ + } + SKP_Silk_range_encoder( psRC, delta_lagIndex, SKP_Silk_pitch_delta_CDF ); + } + if( encode_absolute_lagIndex ) { + /* Absolute encoding */ + if( psEncC->fs_kHz == 8 ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_NB_CDF ); + } else if( psEncC->fs_kHz == 12 ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_MB_CDF ); + } else if( psEncC->fs_kHz == 16 ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_WB_CDF ); + } else { + SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_SWB_CDF ); + } + } + psEncC->prev_lagIndex = psEncCtrlC->lagIndex; + + + /* countour index */ + if( psEncC->fs_kHz == 8 ) { + /* Less codevectors used in 8 khz mode */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_NB_CDF ); + } else { + /* Joint for 12, 16, 24 khz */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_CDF ); + } + + /********************/ + /* Encode LTP gains */ + /********************/ + + /* PERIndex value */ + SKP_Silk_range_encoder( psRC, psEncCtrlC->PERIndex, SKP_Silk_LTP_per_index_CDF ); + + /* Codebook Indices */ + for( k = 0; k < NB_SUBFR; k++ ) { + SKP_Silk_range_encoder( psRC, psEncCtrlC->LTPIndex[ k ], SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ] ); + } + + /**********************/ + /* Encode LTP scaling */ + /**********************/ + SKP_Silk_range_encoder( psRC, psEncCtrlC->LTP_scaleIndex, SKP_Silk_LTPscale_CDF ); + } + + + /***************/ + /* Encode seed */ + /***************/ + SKP_Silk_range_encoder( psRC, psEncCtrlC->Seed, SKP_Silk_Seed_CDF ); +} diff --git a/jni/silk/src/SKP_Silk_encode_pulses.c b/app/src/main/jni/silk/src/SKP_Silk_encode_pulses.c similarity index 97% rename from jni/silk/src/SKP_Silk_encode_pulses.c rename to app/src/main/jni/silk/src/SKP_Silk_encode_pulses.c index d649003..7b89c1e 100644 --- a/jni/silk/src/SKP_Silk_encode_pulses.c +++ b/app/src/main/jni/silk/src/SKP_Silk_encode_pulses.c @@ -1,195 +1,195 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/*********************************************/ -/* Encode quantization indices of excitation */ -/*********************************************/ - -SKP_INLINE SKP_int combine_and_check( /* return ok */ - SKP_int *pulses_comb, /* O */ - const SKP_int *pulses_in, /* I */ - SKP_int max_pulses, /* I max value for sum of pulses */ - SKP_int len /* I number of output values */ -) -{ - SKP_int k, sum; - - for( k = 0; k < len; k++ ) { - sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ]; - if( sum > max_pulses ) { - return 1; - } - pulses_comb[ k ] = sum; - } - - return 0; -} - -/* Encode quantization indices of excitation */ -void SKP_Silk_encode_pulses( - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - const SKP_int sigtype, /* I Sigtype */ - const SKP_int QuantOffsetType,/* I QuantOffsetType */ - const SKP_int q[], /* I quantization indices */ - const SKP_int frame_length /* I Frame length */ -) -{ - SKP_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0; - SKP_int32 abs_q, minSumBits_Q6, sumBits_Q6; - SKP_int abs_pulses[ MAX_FRAME_LENGTH ]; - SKP_int sum_pulses[ MAX_NB_SHELL_BLOCKS ]; - SKP_int nRshifts[ MAX_NB_SHELL_BLOCKS ]; - SKP_int pulses_comb[ 8 ]; - SKP_int *abs_pulses_ptr; - const SKP_int *pulses_ptr; - const SKP_uint16 *cdf_ptr; - const SKP_int16 *nBits_ptr; - - SKP_memset( pulses_comb, 0, 8 * sizeof( SKP_int ) ); // Fixing Valgrind reported problem - - /****************************/ - /* Prepare for shell coding */ - /****************************/ - /* Calculate number of shell blocks */ - iter = frame_length / SHELL_CODEC_FRAME_LENGTH; - - /* Take the absolute value of the pulses */ - for( i = 0; i < frame_length; i+=4 ) { - abs_pulses[i+0] = ( SKP_int )SKP_abs( q[ i + 0 ] ); - abs_pulses[i+1] = ( SKP_int )SKP_abs( q[ i + 1 ] ); - abs_pulses[i+2] = ( SKP_int )SKP_abs( q[ i + 2 ] ); - abs_pulses[i+3] = ( SKP_int )SKP_abs( q[ i + 3 ] ); - } - - /* Calc sum pulses per shell code frame */ - abs_pulses_ptr = abs_pulses; - for( i = 0; i < iter; i++ ) { - nRshifts[ i ] = 0; - - while( 1 ) { - /* 1+1 -> 2 */ - scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, SKP_Silk_max_pulses_table[ 0 ], 8 ); - - /* 2+2 -> 4 */ - scale_down += combine_and_check( pulses_comb, pulses_comb, SKP_Silk_max_pulses_table[ 1 ], 4 ); - - /* 4+4 -> 8 */ - scale_down += combine_and_check( pulses_comb, pulses_comb, SKP_Silk_max_pulses_table[ 2 ], 2 ); - - /* 8+8 -> 16 */ - sum_pulses[ i ] = pulses_comb[ 0 ] + pulses_comb[ 1 ]; - if( sum_pulses[ i ] > SKP_Silk_max_pulses_table[ 3 ] ) { - scale_down++; - } - - if( scale_down ) { - /* We need to down scale the quantization signal */ - nRshifts[ i ]++; - for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { - abs_pulses_ptr[ k ] = SKP_RSHIFT( abs_pulses_ptr[ k ], 1 ); - } - } else { - /* Jump out of while(1) loop and go to next shell coding frame */ - break; - } - } - abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH; - } - - /**************/ - /* Rate level */ - /**************/ - /* find rate level that leads to fewest bits for coding of pulses per block info */ - minSumBits_Q6 = SKP_int32_MAX; - for( k = 0; k < N_RATE_LEVELS - 1; k++ ) { - nBits_ptr = SKP_Silk_pulses_per_block_BITS_Q6[ k ]; - sumBits_Q6 = SKP_Silk_rate_levels_BITS_Q6[sigtype][ k ]; - for( i = 0; i < iter; i++ ) { - if( nRshifts[ i ] > 0 ) { - sumBits_Q6 += nBits_ptr[ MAX_PULSES + 1 ]; - } else { - sumBits_Q6 += nBits_ptr[ sum_pulses[ i ] ]; - } - } - if( sumBits_Q6 < minSumBits_Q6 ) { - minSumBits_Q6 = sumBits_Q6; - RateLevelIndex = k; - } - } - SKP_Silk_range_encoder( psRC, RateLevelIndex, SKP_Silk_rate_levels_CDF[ sigtype ] ); - - /***************************************************/ - /* Sum-Weighted-Pulses Encoding */ - /***************************************************/ - cdf_ptr = SKP_Silk_pulses_per_block_CDF[ RateLevelIndex ]; - for( i = 0; i < iter; i++ ) { - if( nRshifts[ i ] == 0 ) { - SKP_Silk_range_encoder( psRC, sum_pulses[ i ], cdf_ptr ); - } else { - SKP_Silk_range_encoder( psRC, MAX_PULSES + 1, cdf_ptr ); - for( k = 0; k < nRshifts[ i ] - 1; k++ ) { - SKP_Silk_range_encoder( psRC, MAX_PULSES + 1, SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ] ); - } - SKP_Silk_range_encoder( psRC, sum_pulses[ i ], SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ] ); - } - } - - /******************/ - /* Shell Encoding */ - /******************/ - for( i = 0; i < iter; i++ ) { - if( sum_pulses[ i ] > 0 ) { - SKP_Silk_shell_encoder( psRC, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] ); - } - } - - /****************/ - /* LSB Encoding */ - /****************/ - for( i = 0; i < iter; i++ ) { - if( nRshifts[ i ] > 0 ) { - pulses_ptr = &q[ i * SHELL_CODEC_FRAME_LENGTH ]; - nLS = nRshifts[ i ] - 1; - for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { - abs_q = SKP_abs( pulses_ptr[ k ] ); - for( j = nLS; j > 0; j-- ) { - bit = SKP_RSHIFT( abs_q, j ) & 1; - SKP_Silk_range_encoder( psRC, bit, SKP_Silk_lsb_CDF ); - } - bit = abs_q & 1; - SKP_Silk_range_encoder( psRC, bit, SKP_Silk_lsb_CDF ); - } - } - } - - /****************/ - /* Encode signs */ - /****************/ - SKP_Silk_encode_signs( psRC, q, frame_length, sigtype, QuantOffsetType, RateLevelIndex ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/*********************************************/ +/* Encode quantization indices of excitation */ +/*********************************************/ + +SKP_INLINE SKP_int combine_and_check( /* return ok */ + SKP_int *pulses_comb, /* O */ + const SKP_int *pulses_in, /* I */ + SKP_int max_pulses, /* I max value for sum of pulses */ + SKP_int len /* I number of output values */ +) +{ + SKP_int k, sum; + + for( k = 0; k < len; k++ ) { + sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ]; + if( sum > max_pulses ) { + return 1; + } + pulses_comb[ k ] = sum; + } + + return 0; +} + +/* Encode quantization indices of excitation */ +void SKP_Silk_encode_pulses( + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + const SKP_int sigtype, /* I Sigtype */ + const SKP_int QuantOffsetType,/* I QuantOffsetType */ + const SKP_int q[], /* I quantization indices */ + const SKP_int frame_length /* I Frame length */ +) +{ + SKP_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0; + SKP_int32 abs_q, minSumBits_Q6, sumBits_Q6; + SKP_int abs_pulses[ MAX_FRAME_LENGTH ]; + SKP_int sum_pulses[ MAX_NB_SHELL_BLOCKS ]; + SKP_int nRshifts[ MAX_NB_SHELL_BLOCKS ]; + SKP_int pulses_comb[ 8 ]; + SKP_int *abs_pulses_ptr; + const SKP_int *pulses_ptr; + const SKP_uint16 *cdf_ptr; + const SKP_int16 *nBits_ptr; + + SKP_memset( pulses_comb, 0, 8 * sizeof( SKP_int ) ); // Fixing Valgrind reported problem + + /****************************/ + /* Prepare for shell coding */ + /****************************/ + /* Calculate number of shell blocks */ + iter = frame_length / SHELL_CODEC_FRAME_LENGTH; + + /* Take the absolute value of the pulses */ + for( i = 0; i < frame_length; i+=4 ) { + abs_pulses[i+0] = ( SKP_int )SKP_abs( q[ i + 0 ] ); + abs_pulses[i+1] = ( SKP_int )SKP_abs( q[ i + 1 ] ); + abs_pulses[i+2] = ( SKP_int )SKP_abs( q[ i + 2 ] ); + abs_pulses[i+3] = ( SKP_int )SKP_abs( q[ i + 3 ] ); + } + + /* Calc sum pulses per shell code frame */ + abs_pulses_ptr = abs_pulses; + for( i = 0; i < iter; i++ ) { + nRshifts[ i ] = 0; + + while( 1 ) { + /* 1+1 -> 2 */ + scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, SKP_Silk_max_pulses_table[ 0 ], 8 ); + + /* 2+2 -> 4 */ + scale_down += combine_and_check( pulses_comb, pulses_comb, SKP_Silk_max_pulses_table[ 1 ], 4 ); + + /* 4+4 -> 8 */ + scale_down += combine_and_check( pulses_comb, pulses_comb, SKP_Silk_max_pulses_table[ 2 ], 2 ); + + /* 8+8 -> 16 */ + sum_pulses[ i ] = pulses_comb[ 0 ] + pulses_comb[ 1 ]; + if( sum_pulses[ i ] > SKP_Silk_max_pulses_table[ 3 ] ) { + scale_down++; + } + + if( scale_down ) { + /* We need to down scale the quantization signal */ + nRshifts[ i ]++; + for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { + abs_pulses_ptr[ k ] = SKP_RSHIFT( abs_pulses_ptr[ k ], 1 ); + } + } else { + /* Jump out of while(1) loop and go to next shell coding frame */ + break; + } + } + abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH; + } + + /**************/ + /* Rate level */ + /**************/ + /* find rate level that leads to fewest bits for coding of pulses per block info */ + minSumBits_Q6 = SKP_int32_MAX; + for( k = 0; k < N_RATE_LEVELS - 1; k++ ) { + nBits_ptr = SKP_Silk_pulses_per_block_BITS_Q6[ k ]; + sumBits_Q6 = SKP_Silk_rate_levels_BITS_Q6[sigtype][ k ]; + for( i = 0; i < iter; i++ ) { + if( nRshifts[ i ] > 0 ) { + sumBits_Q6 += nBits_ptr[ MAX_PULSES + 1 ]; + } else { + sumBits_Q6 += nBits_ptr[ sum_pulses[ i ] ]; + } + } + if( sumBits_Q6 < minSumBits_Q6 ) { + minSumBits_Q6 = sumBits_Q6; + RateLevelIndex = k; + } + } + SKP_Silk_range_encoder( psRC, RateLevelIndex, SKP_Silk_rate_levels_CDF[ sigtype ] ); + + /***************************************************/ + /* Sum-Weighted-Pulses Encoding */ + /***************************************************/ + cdf_ptr = SKP_Silk_pulses_per_block_CDF[ RateLevelIndex ]; + for( i = 0; i < iter; i++ ) { + if( nRshifts[ i ] == 0 ) { + SKP_Silk_range_encoder( psRC, sum_pulses[ i ], cdf_ptr ); + } else { + SKP_Silk_range_encoder( psRC, MAX_PULSES + 1, cdf_ptr ); + for( k = 0; k < nRshifts[ i ] - 1; k++ ) { + SKP_Silk_range_encoder( psRC, MAX_PULSES + 1, SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ] ); + } + SKP_Silk_range_encoder( psRC, sum_pulses[ i ], SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ] ); + } + } + + /******************/ + /* Shell Encoding */ + /******************/ + for( i = 0; i < iter; i++ ) { + if( sum_pulses[ i ] > 0 ) { + SKP_Silk_shell_encoder( psRC, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] ); + } + } + + /****************/ + /* LSB Encoding */ + /****************/ + for( i = 0; i < iter; i++ ) { + if( nRshifts[ i ] > 0 ) { + pulses_ptr = &q[ i * SHELL_CODEC_FRAME_LENGTH ]; + nLS = nRshifts[ i ] - 1; + for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { + abs_q = SKP_abs( pulses_ptr[ k ] ); + for( j = nLS; j > 0; j-- ) { + bit = SKP_RSHIFT( abs_q, j ) & 1; + SKP_Silk_range_encoder( psRC, bit, SKP_Silk_lsb_CDF ); + } + bit = abs_q & 1; + SKP_Silk_range_encoder( psRC, bit, SKP_Silk_lsb_CDF ); + } + } + } + + /****************/ + /* Encode signs */ + /****************/ + SKP_Silk_encode_signs( psRC, q, frame_length, sigtype, QuantOffsetType, RateLevelIndex ); +} diff --git a/jni/silk/src/SKP_Silk_find_LPC_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_find_LPC_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_find_LPC_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_find_LPC_FIX.c index d45506c..82bf8c1 100644 --- a/jni/silk/src/SKP_Silk_find_LPC_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_find_LPC_FIX.c @@ -1,147 +1,147 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Finds LPC vector from correlations, and converts to NLSF */ -void SKP_Silk_find_LPC_FIX( - SKP_int NLSF_Q15[], /* O NLSFs */ - SKP_int *interpIndex, /* O NLSF interpolation index, only used for NLSF interpolation */ - const SKP_int prev_NLSFq_Q15[], /* I previous NLSFs, only used for NLSF interpolation */ - const SKP_int useInterpolatedNLSFs, /* I Flag */ - const SKP_int LPC_order, /* I LPC order */ - const SKP_int16 x[], /* I Input signal */ - const SKP_int subfr_length /* I Input signal subframe length including preceeding samples */ -) -{ - SKP_int k; - SKP_int32 a_Q16[ MAX_LPC_ORDER ]; - - SKP_int isInterpLower, shift; - SKP_int16 S[ MAX_LPC_ORDER ]; - SKP_int32 res_nrg0, res_nrg1; - SKP_int rshift0, rshift1; - - /* Used only for LSF interpolation */ - SKP_int32 a_tmp_Q16[ MAX_LPC_ORDER ], res_nrg_interp, res_nrg, res_tmp_nrg, res_nrg_2nd; - SKP_int res_nrg_interp_Q, res_nrg_Q, res_tmp_nrg_Q, res_nrg_2nd_Q; - SKP_int16 a_tmp_Q12[ MAX_LPC_ORDER ]; - SKP_int NLSF0_Q15[ MAX_LPC_ORDER ]; - SKP_int16 LPC_res[ ( MAX_FRAME_LENGTH + NB_SUBFR * MAX_LPC_ORDER ) / 2 ]; - - /* Default: no interpolation */ - *interpIndex = 4; - - /* Burg AR analysis for the full frame */ - SKP_Silk_burg_modified( &res_nrg, &res_nrg_Q, a_Q16, x, subfr_length, NB_SUBFR, FIND_LPC_COND_FAC_Q32, LPC_order ); - - if( useInterpolatedNLSFs == 1 ) { - - /* Optimal solution for last 10 ms */ - SKP_Silk_burg_modified( &res_tmp_nrg, &res_tmp_nrg_Q, a_tmp_Q16, x + ( NB_SUBFR >> 1 ) * subfr_length, - subfr_length, ( NB_SUBFR >> 1 ), FIND_LPC_COND_FAC_Q32, LPC_order ); - - /* subtract residual energy here, as that's easier than adding it to the */ - /* residual energy of the first 10 ms in each iteration of the search below */ - shift = res_tmp_nrg_Q - res_nrg_Q; - if( shift >= 0 ) { - if( shift < 32 ) { - res_nrg = res_nrg - SKP_RSHIFT( res_tmp_nrg, shift ); - } - } else { - SKP_assert( shift > -32 ); - res_nrg = SKP_RSHIFT( res_nrg, -shift ) - res_tmp_nrg; - res_nrg_Q = res_tmp_nrg_Q; - } - - /* Convert to NLSFs */ - SKP_Silk_A2NLSF( NLSF_Q15, a_tmp_Q16, LPC_order ); - - /* Search over interpolation indices to find the one with lowest residual energy */ - res_nrg_2nd = SKP_int32_MAX; - for( k = 3; k >= 0; k-- ) { - /* Interpolate NLSFs for first half */ - SKP_Silk_interpolate( NLSF0_Q15, prev_NLSFq_Q15, NLSF_Q15, k, LPC_order ); - - /* Convert to LPC for residual energy evaluation */ - SKP_Silk_NLSF2A_stable( a_tmp_Q12, NLSF0_Q15, LPC_order ); - - /* Calculate residual energy with NLSF interpolation */ - SKP_memset( S, 0, LPC_order * sizeof( SKP_int16 ) ); - SKP_Silk_LPC_analysis_filter( x, a_tmp_Q12, S, LPC_res, 2 * subfr_length, LPC_order ); - - SKP_Silk_sum_sqr_shift( &res_nrg0, &rshift0, LPC_res + LPC_order, subfr_length - LPC_order ); - SKP_Silk_sum_sqr_shift( &res_nrg1, &rshift1, LPC_res + LPC_order + subfr_length, subfr_length - LPC_order ); - - /* Add subframe energies from first half frame */ - shift = rshift0 - rshift1; - if( shift >= 0 ) { - res_nrg1 = SKP_RSHIFT( res_nrg1, shift ); - res_nrg_interp_Q = -rshift0; - } else { - res_nrg0 = SKP_RSHIFT( res_nrg0, -shift ); - res_nrg_interp_Q = -rshift1; - } - res_nrg_interp = SKP_ADD32( res_nrg0, res_nrg1 ); - - /* Compare with first half energy without NLSF interpolation, or best interpolated value so far */ - shift = res_nrg_interp_Q - res_nrg_Q; - if( shift >= 0 ) { - if( SKP_RSHIFT( res_nrg_interp, shift ) < res_nrg ) { - isInterpLower = SKP_TRUE; - } else { - isInterpLower = SKP_FALSE; - } - } else { - if( -shift < 32 ) { - if( res_nrg_interp < SKP_RSHIFT( res_nrg, -shift ) ) { - isInterpLower = SKP_TRUE; - } else { - isInterpLower = SKP_FALSE; - } - } else { - isInterpLower = SKP_FALSE; - } - } - - /* Determine whether current interpolated NLSFs are best so far */ - if( isInterpLower == SKP_TRUE ) { - /* Interpolation has lower residual energy */ - res_nrg = res_nrg_interp; - res_nrg_Q = res_nrg_interp_Q; - *interpIndex = k; - } - res_nrg_2nd = res_nrg_interp; - res_nrg_2nd_Q = res_nrg_interp_Q; - } - } - - if( *interpIndex == 4 ) { - /* NLSF interpolation is currently inactive, calculate NLSFs from full frame AR coefficients */ - SKP_Silk_A2NLSF( NLSF_Q15, a_Q16, LPC_order ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Finds LPC vector from correlations, and converts to NLSF */ +void SKP_Silk_find_LPC_FIX( + SKP_int NLSF_Q15[], /* O NLSFs */ + SKP_int *interpIndex, /* O NLSF interpolation index, only used for NLSF interpolation */ + const SKP_int prev_NLSFq_Q15[], /* I previous NLSFs, only used for NLSF interpolation */ + const SKP_int useInterpolatedNLSFs, /* I Flag */ + const SKP_int LPC_order, /* I LPC order */ + const SKP_int16 x[], /* I Input signal */ + const SKP_int subfr_length /* I Input signal subframe length including preceeding samples */ +) +{ + SKP_int k; + SKP_int32 a_Q16[ MAX_LPC_ORDER ]; + + SKP_int isInterpLower, shift; + SKP_int16 S[ MAX_LPC_ORDER ]; + SKP_int32 res_nrg0, res_nrg1; + SKP_int rshift0, rshift1; + + /* Used only for LSF interpolation */ + SKP_int32 a_tmp_Q16[ MAX_LPC_ORDER ], res_nrg_interp, res_nrg, res_tmp_nrg, res_nrg_2nd; + SKP_int res_nrg_interp_Q, res_nrg_Q, res_tmp_nrg_Q, res_nrg_2nd_Q; + SKP_int16 a_tmp_Q12[ MAX_LPC_ORDER ]; + SKP_int NLSF0_Q15[ MAX_LPC_ORDER ]; + SKP_int16 LPC_res[ ( MAX_FRAME_LENGTH + NB_SUBFR * MAX_LPC_ORDER ) / 2 ]; + + /* Default: no interpolation */ + *interpIndex = 4; + + /* Burg AR analysis for the full frame */ + SKP_Silk_burg_modified( &res_nrg, &res_nrg_Q, a_Q16, x, subfr_length, NB_SUBFR, FIND_LPC_COND_FAC_Q32, LPC_order ); + + if( useInterpolatedNLSFs == 1 ) { + + /* Optimal solution for last 10 ms */ + SKP_Silk_burg_modified( &res_tmp_nrg, &res_tmp_nrg_Q, a_tmp_Q16, x + ( NB_SUBFR >> 1 ) * subfr_length, + subfr_length, ( NB_SUBFR >> 1 ), FIND_LPC_COND_FAC_Q32, LPC_order ); + + /* subtract residual energy here, as that's easier than adding it to the */ + /* residual energy of the first 10 ms in each iteration of the search below */ + shift = res_tmp_nrg_Q - res_nrg_Q; + if( shift >= 0 ) { + if( shift < 32 ) { + res_nrg = res_nrg - SKP_RSHIFT( res_tmp_nrg, shift ); + } + } else { + SKP_assert( shift > -32 ); + res_nrg = SKP_RSHIFT( res_nrg, -shift ) - res_tmp_nrg; + res_nrg_Q = res_tmp_nrg_Q; + } + + /* Convert to NLSFs */ + SKP_Silk_A2NLSF( NLSF_Q15, a_tmp_Q16, LPC_order ); + + /* Search over interpolation indices to find the one with lowest residual energy */ + res_nrg_2nd = SKP_int32_MAX; + for( k = 3; k >= 0; k-- ) { + /* Interpolate NLSFs for first half */ + SKP_Silk_interpolate( NLSF0_Q15, prev_NLSFq_Q15, NLSF_Q15, k, LPC_order ); + + /* Convert to LPC for residual energy evaluation */ + SKP_Silk_NLSF2A_stable( a_tmp_Q12, NLSF0_Q15, LPC_order ); + + /* Calculate residual energy with NLSF interpolation */ + SKP_memset( S, 0, LPC_order * sizeof( SKP_int16 ) ); + SKP_Silk_LPC_analysis_filter( x, a_tmp_Q12, S, LPC_res, 2 * subfr_length, LPC_order ); + + SKP_Silk_sum_sqr_shift( &res_nrg0, &rshift0, LPC_res + LPC_order, subfr_length - LPC_order ); + SKP_Silk_sum_sqr_shift( &res_nrg1, &rshift1, LPC_res + LPC_order + subfr_length, subfr_length - LPC_order ); + + /* Add subframe energies from first half frame */ + shift = rshift0 - rshift1; + if( shift >= 0 ) { + res_nrg1 = SKP_RSHIFT( res_nrg1, shift ); + res_nrg_interp_Q = -rshift0; + } else { + res_nrg0 = SKP_RSHIFT( res_nrg0, -shift ); + res_nrg_interp_Q = -rshift1; + } + res_nrg_interp = SKP_ADD32( res_nrg0, res_nrg1 ); + + /* Compare with first half energy without NLSF interpolation, or best interpolated value so far */ + shift = res_nrg_interp_Q - res_nrg_Q; + if( shift >= 0 ) { + if( SKP_RSHIFT( res_nrg_interp, shift ) < res_nrg ) { + isInterpLower = SKP_TRUE; + } else { + isInterpLower = SKP_FALSE; + } + } else { + if( -shift < 32 ) { + if( res_nrg_interp < SKP_RSHIFT( res_nrg, -shift ) ) { + isInterpLower = SKP_TRUE; + } else { + isInterpLower = SKP_FALSE; + } + } else { + isInterpLower = SKP_FALSE; + } + } + + /* Determine whether current interpolated NLSFs are best so far */ + if( isInterpLower == SKP_TRUE ) { + /* Interpolation has lower residual energy */ + res_nrg = res_nrg_interp; + res_nrg_Q = res_nrg_interp_Q; + *interpIndex = k; + } + res_nrg_2nd = res_nrg_interp; + res_nrg_2nd_Q = res_nrg_interp_Q; + } + } + + if( *interpIndex == 4 ) { + /* NLSF interpolation is currently inactive, calculate NLSFs from full frame AR coefficients */ + SKP_Silk_A2NLSF( NLSF_Q15, a_Q16, LPC_order ); + } +} diff --git a/jni/silk/src/SKP_Silk_find_LTP_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_find_LTP_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_find_LTP_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_find_LTP_FIX.c index c8ac22d..89cdeaf 100644 --- a/jni/silk/src/SKP_Silk_find_LTP_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_find_LTP_FIX.c @@ -1,234 +1,234 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -void SKP_Silk_fit_LTP( - SKP_int32 LTP_coefs_Q16[ LTP_ORDER ], - SKP_int16 LTP_coefs_Q14[ LTP_ORDER ] -); - -void SKP_Silk_find_LTP_FIX( - SKP_int16 b_Q14[ NB_SUBFR * LTP_ORDER ], /* O LTP coefs */ - SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ - SKP_int *LTPredCodGain_Q7, /* O LTP coding gain */ - const SKP_int16 r_first[], /* I residual signal after LPC signal + state for first 10 ms */ - const SKP_int16 r_last[], /* I residual signal after LPC signal + state for last 10 ms */ - const SKP_int lag[ NB_SUBFR ], /* I LTP lags */ - const SKP_int32 Wght_Q15[ NB_SUBFR ], /* I weights */ - const SKP_int subfr_length, /* I subframe length */ - const SKP_int mem_offset, /* I number of samples in LTP memory */ - SKP_int corr_rshifts[ NB_SUBFR ] /* O right shifts applied to correlations */ -) -{ - SKP_int i, k, lshift; - const SKP_int16 *r_ptr, *lag_ptr; - SKP_int16 *b_Q14_ptr; - - SKP_int32 regu; - SKP_int32 *WLTP_ptr; - SKP_int32 b_Q16[ LTP_ORDER ], delta_b_Q14[ LTP_ORDER ], d_Q14[ NB_SUBFR ], nrg[ NB_SUBFR ], g_Q26; - SKP_int32 w[ NB_SUBFR ], WLTP_max, max_abs_d_Q14, max_w_bits; - - SKP_int32 temp32, denom32; - SKP_int extra_shifts; - SKP_int rr_shifts, maxRshifts, maxRshifts_wxtra, LZs; - SKP_int32 LPC_res_nrg, LPC_LTP_res_nrg, div_Q16; - SKP_int32 Rr[ LTP_ORDER ], rr[ NB_SUBFR ]; - SKP_int32 wd, m_Q12; - - b_Q14_ptr = b_Q14; - WLTP_ptr = WLTP; - r_ptr = &r_first[ mem_offset ]; - for( k = 0; k < NB_SUBFR; k++ ) { - if( k == ( NB_SUBFR >> 1 ) ) { /* shift residual for last 10 ms */ - r_ptr = &r_last[ mem_offset ]; - } - lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 ); - - SKP_Silk_sum_sqr_shift( &rr[ k ], &rr_shifts, r_ptr, subfr_length ); /* rr[ k ] in Q( -rr_shifts ) */ - /* Assure headroom */ - LZs = SKP_Silk_CLZ32( rr[k] ); - if( LZs < LTP_CORRS_HEAD_ROOM ) { - rr[ k ] = SKP_RSHIFT_ROUND( rr[ k ], LTP_CORRS_HEAD_ROOM - LZs ); - rr_shifts += (LTP_CORRS_HEAD_ROOM - LZs); - } - corr_rshifts[ k ] = rr_shifts; - SKP_Silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, WLTP_ptr, &corr_rshifts[ k ] ); /* WLTP_fix_ptr in Q( -corr_rshifts[ k ] ) */ - /* The correlation vector always have lower max abs value than rr and/or RR so head room is assured */ - SKP_Silk_corrVector_FIX( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr, corr_rshifts[ k ] ); /* Rr_fix_ptr in Q( -corr_rshifts[ k ] ) */ - if( corr_rshifts[ k ] > rr_shifts ) { - rr[ k ] = SKP_RSHIFT( rr[ k ], corr_rshifts[ k ] - rr_shifts ); /* rr[ k ] in Q( -corr_rshifts[ k ] ) */ - } - SKP_assert( rr[ k ] >= 0 ); - - regu = SKP_SMULWB( rr[ k ] + 1, LTP_DAMPING_Q16 ); - SKP_Silk_regularize_correlations_FIX( WLTP_ptr, &rr[k], regu, LTP_ORDER ); - - SKP_Silk_solve_LDL_FIX( WLTP_ptr, LTP_ORDER, Rr, b_Q16 ); /* WLTP_fix_ptr and Rr_fix_ptr both in Q(-corr_rshifts[k]) */ - - /* Limit and store in Q14 */ - SKP_Silk_fit_LTP( b_Q16, b_Q14_ptr ); - - /* Calculate residual energy */ - nrg[ k ] = SKP_Silk_residual_energy16_covar_FIX( b_Q14_ptr, WLTP_ptr, Rr, rr[ k ], LTP_ORDER, 14 ); /* nrg_fix in Q( -corr_rshifts[ k ] ) */ - - /* temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); */ - extra_shifts = SKP_min_int( corr_rshifts[ k ], LTP_CORRS_HEAD_ROOM ); - denom32 = SKP_LSHIFT_SAT32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 + extra_shifts ) + /* Q( -corr_rshifts[ k ] + extra_shifts ) */ - SKP_RSHIFT( SKP_SMULWB( subfr_length, 655 ), corr_rshifts[ k ] - extra_shifts ); /* Q( -corr_rshifts[ k ] + extra_shifts ) */ - denom32 = SKP_max( denom32, 1 ); - SKP_assert( ((SKP_int64)Wght_Q15[ k ] << 16 ) < SKP_int32_MAX ); /* Wght always < 0.5 in Q0 */ - temp32 = SKP_DIV32( SKP_LSHIFT( ( SKP_int32 )Wght_Q15[ k ], 16 ), denom32 ); /* Q( 15 + 16 + corr_rshifts[k] - extra_shifts ) */ - temp32 = SKP_RSHIFT( temp32, 31 + corr_rshifts[ k ] - extra_shifts - 26 ); /* Q26 */ - - /* Limit temp such that the below scaling never wraps around */ - WLTP_max = 0; - for( i = 0; i < LTP_ORDER * LTP_ORDER; i++ ) { - WLTP_max = SKP_max( WLTP_ptr[ i ], WLTP_max ); - } - lshift = SKP_Silk_CLZ32( WLTP_max ) - 1 - 3; /* keep 3 bits free for vq_nearest_neighbor_fix */ - SKP_assert( 26 - 18 + lshift >= 0 ); - if( 26 - 18 + lshift < 31 ) { - temp32 = SKP_min_32( temp32, SKP_LSHIFT( ( SKP_int32 )1, 26 - 18 + lshift ) ); - } - - SKP_Silk_scale_vector32_Q26_lshift_18( WLTP_ptr, temp32, LTP_ORDER * LTP_ORDER ); /* WLTP_ptr in Q( 18 - corr_rshifts[ k ] ) */ - - w[ k ] = matrix_ptr( WLTP_ptr, ( LTP_ORDER >> 1 ), ( LTP_ORDER >> 1 ), LTP_ORDER ); /* w in Q( 18 - corr_rshifts[ k ] ) */ - SKP_assert( w[k] >= 0 ); - - r_ptr += subfr_length; - b_Q14_ptr += LTP_ORDER; - WLTP_ptr += LTP_ORDER * LTP_ORDER; - } - - maxRshifts = 0; - for( k = 0; k < NB_SUBFR; k++ ) { - maxRshifts = SKP_max_int( corr_rshifts[ k ], maxRshifts ); - } - - /* compute LTP coding gain */ - if( LTPredCodGain_Q7 != NULL ) { - LPC_LTP_res_nrg = 0; - LPC_res_nrg = 0; - SKP_assert( LTP_CORRS_HEAD_ROOM >= 2 ); /* Check that no overflow will happen when adding */ - for( k = 0; k < NB_SUBFR; k++ ) { - LPC_res_nrg = SKP_ADD32( LPC_res_nrg, SKP_RSHIFT( SKP_ADD32( SKP_SMULWB( rr[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */ - LPC_LTP_res_nrg = SKP_ADD32( LPC_LTP_res_nrg, SKP_RSHIFT( SKP_ADD32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */ - } - LPC_LTP_res_nrg = SKP_max( LPC_LTP_res_nrg, 1 ); /* avoid division by zero */ - - div_Q16 = SKP_DIV32_varQ( LPC_res_nrg, LPC_LTP_res_nrg, 16 ); - *LTPredCodGain_Q7 = ( SKP_int )SKP_SMULBB( 3, SKP_Silk_lin2log( div_Q16 ) - ( 16 << 7 ) ); - - SKP_assert( *LTPredCodGain_Q7 == ( SKP_int )SKP_SAT16( SKP_MUL( 3, SKP_Silk_lin2log( div_Q16 ) - ( 16 << 7 ) ) ) ); - } - - /* smoothing */ - /* d = sum( B, 1 ); */ - b_Q14_ptr = b_Q14; - for( k = 0; k < NB_SUBFR; k++ ) { - d_Q14[ k ] = 0; - for( i = 0; i < LTP_ORDER; i++ ) { - d_Q14[ k ] += b_Q14_ptr[ i ]; - } - b_Q14_ptr += LTP_ORDER; - } - - /* m = ( w * d' ) / ( sum( w ) + 1e-3 ); */ - - /* Find maximum absolute value of d_Q14 and the bits used by w in Q0 */ - max_abs_d_Q14 = 0; - max_w_bits = 0; - for( k = 0; k < NB_SUBFR; k++ ) { - max_abs_d_Q14 = SKP_max_32( max_abs_d_Q14, SKP_abs( d_Q14[ k ] ) ); - /* w[ k ] is in Q( 18 - corr_rshifts[ k ] ) */ - /* Find bits needed in Q( 18 - maxRshifts ) */ - max_w_bits = SKP_max_32( max_w_bits, 32 - SKP_Silk_CLZ32( w[ k ] ) + corr_rshifts[ k ] - maxRshifts ); - } - - /* max_abs_d_Q14 = (5 << 15); worst case, i.e. LTP_ORDER * -SKP_int16_MIN */ - SKP_assert( max_abs_d_Q14 <= ( 5 << 15 ) ); - - /* How many bits is needed for w*d' in Q( 18 - maxRshifts ) in the worst case, of all d_Q14's being equal to max_abs_d_Q14 */ - extra_shifts = max_w_bits + 32 - SKP_Silk_CLZ32( max_abs_d_Q14 ) - 14; - - /* Subtract what we got available; bits in output var plus maxRshifts */ - extra_shifts -= ( 32 - 1 - 2 + maxRshifts ); /* Keep sign bit free as well as 2 bits for accumulation */ - extra_shifts = SKP_max_int( extra_shifts, 0 ); - - maxRshifts_wxtra = maxRshifts + extra_shifts; - - temp32 = SKP_RSHIFT( 262, maxRshifts + extra_shifts ) + 1; /* 1e-3f in Q( 18 - (maxRshifts + extra_shifts) ) */ - wd = 0; - for( k = 0; k < NB_SUBFR; k++ ) { - /* w has at least 2 bits of headroom so no overflow should happen */ - temp32 = SKP_ADD32( temp32, SKP_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ) ); /* Q( 18 - maxRshifts_wxtra ) */ - wd = SKP_ADD32( wd, SKP_LSHIFT( SKP_SMULWW( SKP_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ), d_Q14[ k ] ), 2 ) ); /* Q( 18 - maxRshifts_wxtra ) */ - } - m_Q12 = SKP_DIV32_varQ( wd, temp32, 12 ); - - b_Q14_ptr = b_Q14; - for( k = 0; k < NB_SUBFR; k++ ) { - /* w_fix[ k ] from Q( 18 - corr_rshifts[ k ] ) to Q( 16 ) */ - if( 2 - corr_rshifts[k] > 0 ) { - temp32 = SKP_RSHIFT( w[ k ], 2 - corr_rshifts[ k ] ); - } else { - temp32 = SKP_LSHIFT_SAT32( w[ k ], corr_rshifts[ k ] - 2 ); - } - - g_Q26 = SKP_MUL( - SKP_DIV32( - LTP_SMOOTHING_Q26, - SKP_RSHIFT( LTP_SMOOTHING_Q26, 10 ) + temp32 ), /* Q10 */ - SKP_LSHIFT_SAT32( SKP_SUB_SAT32( ( SKP_int32 )m_Q12, SKP_RSHIFT( d_Q14[ k ], 2 ) ), 4 ) ); /* Q16 */ - - temp32 = 0; - for( i = 0; i < LTP_ORDER; i++ ) { - delta_b_Q14[ i ] = SKP_max_16( b_Q14_ptr[ i ], 1638 ); /* 1638_Q14 = 0.1_Q0 */ - temp32 += delta_b_Q14[ i ]; /* Q14 */ - } - temp32 = SKP_DIV32( g_Q26, temp32 ); /* Q14->Q12 */ - for( i = 0; i < LTP_ORDER; i++ ) { - b_Q14_ptr[ i ] = SKP_LIMIT( ( SKP_int32 )b_Q14_ptr[ i ] + SKP_SMULWB( SKP_LSHIFT_SAT32( temp32, 4 ), delta_b_Q14[ i ] ), -16000, 28000 ); - } - b_Q14_ptr += LTP_ORDER; - } -} - -void SKP_Silk_fit_LTP( - SKP_int32 LTP_coefs_Q16[ LTP_ORDER ], - SKP_int16 LTP_coefs_Q14[ LTP_ORDER ] -) -{ - SKP_int i; - - for( i = 0; i < LTP_ORDER; i++ ) { - LTP_coefs_Q14[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( LTP_coefs_Q16[ i ], 2 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +void SKP_Silk_fit_LTP( + SKP_int32 LTP_coefs_Q16[ LTP_ORDER ], + SKP_int16 LTP_coefs_Q14[ LTP_ORDER ] +); + +void SKP_Silk_find_LTP_FIX( + SKP_int16 b_Q14[ NB_SUBFR * LTP_ORDER ], /* O LTP coefs */ + SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ + SKP_int *LTPredCodGain_Q7, /* O LTP coding gain */ + const SKP_int16 r_first[], /* I residual signal after LPC signal + state for first 10 ms */ + const SKP_int16 r_last[], /* I residual signal after LPC signal + state for last 10 ms */ + const SKP_int lag[ NB_SUBFR ], /* I LTP lags */ + const SKP_int32 Wght_Q15[ NB_SUBFR ], /* I weights */ + const SKP_int subfr_length, /* I subframe length */ + const SKP_int mem_offset, /* I number of samples in LTP memory */ + SKP_int corr_rshifts[ NB_SUBFR ] /* O right shifts applied to correlations */ +) +{ + SKP_int i, k, lshift; + const SKP_int16 *r_ptr, *lag_ptr; + SKP_int16 *b_Q14_ptr; + + SKP_int32 regu; + SKP_int32 *WLTP_ptr; + SKP_int32 b_Q16[ LTP_ORDER ], delta_b_Q14[ LTP_ORDER ], d_Q14[ NB_SUBFR ], nrg[ NB_SUBFR ], g_Q26; + SKP_int32 w[ NB_SUBFR ], WLTP_max, max_abs_d_Q14, max_w_bits; + + SKP_int32 temp32, denom32; + SKP_int extra_shifts; + SKP_int rr_shifts, maxRshifts, maxRshifts_wxtra, LZs; + SKP_int32 LPC_res_nrg, LPC_LTP_res_nrg, div_Q16; + SKP_int32 Rr[ LTP_ORDER ], rr[ NB_SUBFR ]; + SKP_int32 wd, m_Q12; + + b_Q14_ptr = b_Q14; + WLTP_ptr = WLTP; + r_ptr = &r_first[ mem_offset ]; + for( k = 0; k < NB_SUBFR; k++ ) { + if( k == ( NB_SUBFR >> 1 ) ) { /* shift residual for last 10 ms */ + r_ptr = &r_last[ mem_offset ]; + } + lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 ); + + SKP_Silk_sum_sqr_shift( &rr[ k ], &rr_shifts, r_ptr, subfr_length ); /* rr[ k ] in Q( -rr_shifts ) */ + /* Assure headroom */ + LZs = SKP_Silk_CLZ32( rr[k] ); + if( LZs < LTP_CORRS_HEAD_ROOM ) { + rr[ k ] = SKP_RSHIFT_ROUND( rr[ k ], LTP_CORRS_HEAD_ROOM - LZs ); + rr_shifts += (LTP_CORRS_HEAD_ROOM - LZs); + } + corr_rshifts[ k ] = rr_shifts; + SKP_Silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, WLTP_ptr, &corr_rshifts[ k ] ); /* WLTP_fix_ptr in Q( -corr_rshifts[ k ] ) */ + /* The correlation vector always have lower max abs value than rr and/or RR so head room is assured */ + SKP_Silk_corrVector_FIX( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr, corr_rshifts[ k ] ); /* Rr_fix_ptr in Q( -corr_rshifts[ k ] ) */ + if( corr_rshifts[ k ] > rr_shifts ) { + rr[ k ] = SKP_RSHIFT( rr[ k ], corr_rshifts[ k ] - rr_shifts ); /* rr[ k ] in Q( -corr_rshifts[ k ] ) */ + } + SKP_assert( rr[ k ] >= 0 ); + + regu = SKP_SMULWB( rr[ k ] + 1, LTP_DAMPING_Q16 ); + SKP_Silk_regularize_correlations_FIX( WLTP_ptr, &rr[k], regu, LTP_ORDER ); + + SKP_Silk_solve_LDL_FIX( WLTP_ptr, LTP_ORDER, Rr, b_Q16 ); /* WLTP_fix_ptr and Rr_fix_ptr both in Q(-corr_rshifts[k]) */ + + /* Limit and store in Q14 */ + SKP_Silk_fit_LTP( b_Q16, b_Q14_ptr ); + + /* Calculate residual energy */ + nrg[ k ] = SKP_Silk_residual_energy16_covar_FIX( b_Q14_ptr, WLTP_ptr, Rr, rr[ k ], LTP_ORDER, 14 ); /* nrg_fix in Q( -corr_rshifts[ k ] ) */ + + /* temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); */ + extra_shifts = SKP_min_int( corr_rshifts[ k ], LTP_CORRS_HEAD_ROOM ); + denom32 = SKP_LSHIFT_SAT32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 + extra_shifts ) + /* Q( -corr_rshifts[ k ] + extra_shifts ) */ + SKP_RSHIFT( SKP_SMULWB( subfr_length, 655 ), corr_rshifts[ k ] - extra_shifts ); /* Q( -corr_rshifts[ k ] + extra_shifts ) */ + denom32 = SKP_max( denom32, 1 ); + SKP_assert( ((SKP_int64)Wght_Q15[ k ] << 16 ) < SKP_int32_MAX ); /* Wght always < 0.5 in Q0 */ + temp32 = SKP_DIV32( SKP_LSHIFT( ( SKP_int32 )Wght_Q15[ k ], 16 ), denom32 ); /* Q( 15 + 16 + corr_rshifts[k] - extra_shifts ) */ + temp32 = SKP_RSHIFT( temp32, 31 + corr_rshifts[ k ] - extra_shifts - 26 ); /* Q26 */ + + /* Limit temp such that the below scaling never wraps around */ + WLTP_max = 0; + for( i = 0; i < LTP_ORDER * LTP_ORDER; i++ ) { + WLTP_max = SKP_max( WLTP_ptr[ i ], WLTP_max ); + } + lshift = SKP_Silk_CLZ32( WLTP_max ) - 1 - 3; /* keep 3 bits free for vq_nearest_neighbor_fix */ + SKP_assert( 26 - 18 + lshift >= 0 ); + if( 26 - 18 + lshift < 31 ) { + temp32 = SKP_min_32( temp32, SKP_LSHIFT( ( SKP_int32 )1, 26 - 18 + lshift ) ); + } + + SKP_Silk_scale_vector32_Q26_lshift_18( WLTP_ptr, temp32, LTP_ORDER * LTP_ORDER ); /* WLTP_ptr in Q( 18 - corr_rshifts[ k ] ) */ + + w[ k ] = matrix_ptr( WLTP_ptr, ( LTP_ORDER >> 1 ), ( LTP_ORDER >> 1 ), LTP_ORDER ); /* w in Q( 18 - corr_rshifts[ k ] ) */ + SKP_assert( w[k] >= 0 ); + + r_ptr += subfr_length; + b_Q14_ptr += LTP_ORDER; + WLTP_ptr += LTP_ORDER * LTP_ORDER; + } + + maxRshifts = 0; + for( k = 0; k < NB_SUBFR; k++ ) { + maxRshifts = SKP_max_int( corr_rshifts[ k ], maxRshifts ); + } + + /* compute LTP coding gain */ + if( LTPredCodGain_Q7 != NULL ) { + LPC_LTP_res_nrg = 0; + LPC_res_nrg = 0; + SKP_assert( LTP_CORRS_HEAD_ROOM >= 2 ); /* Check that no overflow will happen when adding */ + for( k = 0; k < NB_SUBFR; k++ ) { + LPC_res_nrg = SKP_ADD32( LPC_res_nrg, SKP_RSHIFT( SKP_ADD32( SKP_SMULWB( rr[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */ + LPC_LTP_res_nrg = SKP_ADD32( LPC_LTP_res_nrg, SKP_RSHIFT( SKP_ADD32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */ + } + LPC_LTP_res_nrg = SKP_max( LPC_LTP_res_nrg, 1 ); /* avoid division by zero */ + + div_Q16 = SKP_DIV32_varQ( LPC_res_nrg, LPC_LTP_res_nrg, 16 ); + *LTPredCodGain_Q7 = ( SKP_int )SKP_SMULBB( 3, SKP_Silk_lin2log( div_Q16 ) - ( 16 << 7 ) ); + + SKP_assert( *LTPredCodGain_Q7 == ( SKP_int )SKP_SAT16( SKP_MUL( 3, SKP_Silk_lin2log( div_Q16 ) - ( 16 << 7 ) ) ) ); + } + + /* smoothing */ + /* d = sum( B, 1 ); */ + b_Q14_ptr = b_Q14; + for( k = 0; k < NB_SUBFR; k++ ) { + d_Q14[ k ] = 0; + for( i = 0; i < LTP_ORDER; i++ ) { + d_Q14[ k ] += b_Q14_ptr[ i ]; + } + b_Q14_ptr += LTP_ORDER; + } + + /* m = ( w * d' ) / ( sum( w ) + 1e-3 ); */ + + /* Find maximum absolute value of d_Q14 and the bits used by w in Q0 */ + max_abs_d_Q14 = 0; + max_w_bits = 0; + for( k = 0; k < NB_SUBFR; k++ ) { + max_abs_d_Q14 = SKP_max_32( max_abs_d_Q14, SKP_abs( d_Q14[ k ] ) ); + /* w[ k ] is in Q( 18 - corr_rshifts[ k ] ) */ + /* Find bits needed in Q( 18 - maxRshifts ) */ + max_w_bits = SKP_max_32( max_w_bits, 32 - SKP_Silk_CLZ32( w[ k ] ) + corr_rshifts[ k ] - maxRshifts ); + } + + /* max_abs_d_Q14 = (5 << 15); worst case, i.e. LTP_ORDER * -SKP_int16_MIN */ + SKP_assert( max_abs_d_Q14 <= ( 5 << 15 ) ); + + /* How many bits is needed for w*d' in Q( 18 - maxRshifts ) in the worst case, of all d_Q14's being equal to max_abs_d_Q14 */ + extra_shifts = max_w_bits + 32 - SKP_Silk_CLZ32( max_abs_d_Q14 ) - 14; + + /* Subtract what we got available; bits in output var plus maxRshifts */ + extra_shifts -= ( 32 - 1 - 2 + maxRshifts ); /* Keep sign bit free as well as 2 bits for accumulation */ + extra_shifts = SKP_max_int( extra_shifts, 0 ); + + maxRshifts_wxtra = maxRshifts + extra_shifts; + + temp32 = SKP_RSHIFT( 262, maxRshifts + extra_shifts ) + 1; /* 1e-3f in Q( 18 - (maxRshifts + extra_shifts) ) */ + wd = 0; + for( k = 0; k < NB_SUBFR; k++ ) { + /* w has at least 2 bits of headroom so no overflow should happen */ + temp32 = SKP_ADD32( temp32, SKP_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ) ); /* Q( 18 - maxRshifts_wxtra ) */ + wd = SKP_ADD32( wd, SKP_LSHIFT( SKP_SMULWW( SKP_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ), d_Q14[ k ] ), 2 ) ); /* Q( 18 - maxRshifts_wxtra ) */ + } + m_Q12 = SKP_DIV32_varQ( wd, temp32, 12 ); + + b_Q14_ptr = b_Q14; + for( k = 0; k < NB_SUBFR; k++ ) { + /* w_fix[ k ] from Q( 18 - corr_rshifts[ k ] ) to Q( 16 ) */ + if( 2 - corr_rshifts[k] > 0 ) { + temp32 = SKP_RSHIFT( w[ k ], 2 - corr_rshifts[ k ] ); + } else { + temp32 = SKP_LSHIFT_SAT32( w[ k ], corr_rshifts[ k ] - 2 ); + } + + g_Q26 = SKP_MUL( + SKP_DIV32( + LTP_SMOOTHING_Q26, + SKP_RSHIFT( LTP_SMOOTHING_Q26, 10 ) + temp32 ), /* Q10 */ + SKP_LSHIFT_SAT32( SKP_SUB_SAT32( ( SKP_int32 )m_Q12, SKP_RSHIFT( d_Q14[ k ], 2 ) ), 4 ) ); /* Q16 */ + + temp32 = 0; + for( i = 0; i < LTP_ORDER; i++ ) { + delta_b_Q14[ i ] = SKP_max_16( b_Q14_ptr[ i ], 1638 ); /* 1638_Q14 = 0.1_Q0 */ + temp32 += delta_b_Q14[ i ]; /* Q14 */ + } + temp32 = SKP_DIV32( g_Q26, temp32 ); /* Q14->Q12 */ + for( i = 0; i < LTP_ORDER; i++ ) { + b_Q14_ptr[ i ] = SKP_LIMIT( ( SKP_int32 )b_Q14_ptr[ i ] + SKP_SMULWB( SKP_LSHIFT_SAT32( temp32, 4 ), delta_b_Q14[ i ] ), -16000, 28000 ); + } + b_Q14_ptr += LTP_ORDER; + } +} + +void SKP_Silk_fit_LTP( + SKP_int32 LTP_coefs_Q16[ LTP_ORDER ], + SKP_int16 LTP_coefs_Q14[ LTP_ORDER ] +) +{ + SKP_int i; + + for( i = 0; i < LTP_ORDER; i++ ) { + LTP_coefs_Q14[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( LTP_coefs_Q16[ i ], 2 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_find_pitch_lags_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_find_pitch_lags_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_find_pitch_lags_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_find_pitch_lags_FIX.c index dc7b2d3..3e41df2 100644 --- a/jni/silk/src/SKP_Silk_find_pitch_lags_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_find_pitch_lags_FIX.c @@ -1,122 +1,122 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Find pitch lags */ -void SKP_Silk_find_pitch_lags_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ - SKP_int16 res[], /* O residual */ - const SKP_int16 x[] /* I Speech signal */ -) -{ - SKP_Silk_predict_state_FIX *psPredSt = &psEnc->sPred; - SKP_int buf_len, i; - SKP_int32 scale; - SKP_int32 thrhld_Q15; - const SKP_int16 *x_buf, *x_buf_ptr; - SKP_int16 Wsig[ FIND_PITCH_LPC_WIN_MAX ], *Wsig_ptr; - SKP_int32 auto_corr[ FIND_PITCH_LPC_ORDER_MAX + 1 ]; - SKP_int16 rc_Q15[ FIND_PITCH_LPC_ORDER_MAX ]; - SKP_int32 A_Q24[ FIND_PITCH_LPC_ORDER_MAX ]; - SKP_int32 FiltState[ FIND_PITCH_LPC_ORDER_MAX ]; - SKP_int16 A_Q12[ FIND_PITCH_LPC_ORDER_MAX ]; - - /******************************************/ - /* Setup buffer lengths etc based of Fs. */ - /******************************************/ - buf_len = SKP_ADD_LSHIFT( psEnc->sCmn.la_pitch, psEnc->sCmn.frame_length, 1 ); - - /* Safty check */ - SKP_assert( buf_len >= psPredSt->pitch_LPC_win_length ); - - x_buf = x - psEnc->sCmn.frame_length; - - /*************************************/ - /* Estimate LPC AR coeficients */ - /*************************************/ - - /* Calculate windowed signal */ - - /* First LA_LTP samples */ - x_buf_ptr = x_buf + buf_len - psPredSt->pitch_LPC_win_length; - Wsig_ptr = Wsig; - SKP_Silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch ); - - /* Middle un - windowed samples */ - Wsig_ptr += psEnc->sCmn.la_pitch; - x_buf_ptr += psEnc->sCmn.la_pitch; - SKP_memcpy( Wsig_ptr, x_buf_ptr, ( psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ) ) * sizeof( SKP_int16 ) ); - - /* Last LA_LTP samples */ - Wsig_ptr += psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ); - x_buf_ptr += psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ); - SKP_Silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch ); - - /* Calculate autocorrelation sequence */ - SKP_Silk_autocorr( auto_corr, &scale, Wsig, psPredSt->pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 ); - - /* add white noise, as fraction of energy */ - auto_corr[ 0 ] = SKP_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], FIND_PITCH_WHITE_NOISE_FRACTION_Q16 ); - - /* calculate the reflection coefficients using schur */ - SKP_Silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder ); - - /* convert reflection coefficients to prediction coefficients */ - SKP_Silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder ); - - /* Convert From 32 bit Q24 to 16 bit Q12 coefs */ - for( i = 0; i < psEnc->sCmn.pitchEstimationLPCOrder; i++ ) { - A_Q12[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT( A_Q24[ i ], 12 ) ); - } - - /* Do BWE */ - SKP_Silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, FIND_PITCH_BANDWITH_EXPANSION_Q16 ); - - /*****************************************/ - /* LPC analysis filtering */ - /*****************************************/ - SKP_memset( FiltState, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) ); - SKP_Silk_MA_Prediction( x_buf, A_Q12, FiltState, res, buf_len, psEnc->sCmn.pitchEstimationLPCOrder ); - SKP_memset( res, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) ); - - /* Threshold for pitch estimator */ - thrhld_Q15 = ( 1 << 14 ); // 0.5f in Q15 - thrhld_Q15 = SKP_SMLABB( thrhld_Q15, -131, psEnc->sCmn.pitchEstimationLPCOrder ); - thrhld_Q15 = SKP_SMLABB( thrhld_Q15, -13, ( SKP_int16 )SKP_Silk_SQRT_APPROX( SKP_LSHIFT( ( SKP_int32 )psEnc->speech_activity_Q8, 8 ) ) ); - thrhld_Q15 = SKP_SMLABB( thrhld_Q15, 4587, psEnc->sCmn.prev_sigtype ); - thrhld_Q15 = SKP_MLA( thrhld_Q15, -31, SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) ); - thrhld_Q15 = SKP_SAT16( thrhld_Q15 ); - - /*****************************************/ - /* Call Pitch estimator */ - /*****************************************/ - psEncCtrl->sCmn.sigtype = SKP_Silk_pitch_analysis_core( res, psEncCtrl->sCmn.pitchL, &psEncCtrl->sCmn.lagIndex, - &psEncCtrl->sCmn.contourIndex, &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->pitchEstimationThreshold_Q16, - ( SKP_int16 )thrhld_Q15, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Find pitch lags */ +void SKP_Silk_find_pitch_lags_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ + SKP_int16 res[], /* O residual */ + const SKP_int16 x[] /* I Speech signal */ +) +{ + SKP_Silk_predict_state_FIX *psPredSt = &psEnc->sPred; + SKP_int buf_len, i; + SKP_int32 scale; + SKP_int32 thrhld_Q15; + const SKP_int16 *x_buf, *x_buf_ptr; + SKP_int16 Wsig[ FIND_PITCH_LPC_WIN_MAX ], *Wsig_ptr; + SKP_int32 auto_corr[ FIND_PITCH_LPC_ORDER_MAX + 1 ]; + SKP_int16 rc_Q15[ FIND_PITCH_LPC_ORDER_MAX ]; + SKP_int32 A_Q24[ FIND_PITCH_LPC_ORDER_MAX ]; + SKP_int32 FiltState[ FIND_PITCH_LPC_ORDER_MAX ]; + SKP_int16 A_Q12[ FIND_PITCH_LPC_ORDER_MAX ]; + + /******************************************/ + /* Setup buffer lengths etc based of Fs. */ + /******************************************/ + buf_len = SKP_ADD_LSHIFT( psEnc->sCmn.la_pitch, psEnc->sCmn.frame_length, 1 ); + + /* Safty check */ + SKP_assert( buf_len >= psPredSt->pitch_LPC_win_length ); + + x_buf = x - psEnc->sCmn.frame_length; + + /*************************************/ + /* Estimate LPC AR coeficients */ + /*************************************/ + + /* Calculate windowed signal */ + + /* First LA_LTP samples */ + x_buf_ptr = x_buf + buf_len - psPredSt->pitch_LPC_win_length; + Wsig_ptr = Wsig; + SKP_Silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch ); + + /* Middle un - windowed samples */ + Wsig_ptr += psEnc->sCmn.la_pitch; + x_buf_ptr += psEnc->sCmn.la_pitch; + SKP_memcpy( Wsig_ptr, x_buf_ptr, ( psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ) ) * sizeof( SKP_int16 ) ); + + /* Last LA_LTP samples */ + Wsig_ptr += psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ); + x_buf_ptr += psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ); + SKP_Silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch ); + + /* Calculate autocorrelation sequence */ + SKP_Silk_autocorr( auto_corr, &scale, Wsig, psPredSt->pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 ); + + /* add white noise, as fraction of energy */ + auto_corr[ 0 ] = SKP_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], FIND_PITCH_WHITE_NOISE_FRACTION_Q16 ); + + /* calculate the reflection coefficients using schur */ + SKP_Silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder ); + + /* convert reflection coefficients to prediction coefficients */ + SKP_Silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder ); + + /* Convert From 32 bit Q24 to 16 bit Q12 coefs */ + for( i = 0; i < psEnc->sCmn.pitchEstimationLPCOrder; i++ ) { + A_Q12[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT( A_Q24[ i ], 12 ) ); + } + + /* Do BWE */ + SKP_Silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, FIND_PITCH_BANDWITH_EXPANSION_Q16 ); + + /*****************************************/ + /* LPC analysis filtering */ + /*****************************************/ + SKP_memset( FiltState, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) ); + SKP_Silk_MA_Prediction( x_buf, A_Q12, FiltState, res, buf_len, psEnc->sCmn.pitchEstimationLPCOrder ); + SKP_memset( res, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) ); + + /* Threshold for pitch estimator */ + thrhld_Q15 = ( 1 << 14 ); // 0.5f in Q15 + thrhld_Q15 = SKP_SMLABB( thrhld_Q15, -131, psEnc->sCmn.pitchEstimationLPCOrder ); + thrhld_Q15 = SKP_SMLABB( thrhld_Q15, -13, ( SKP_int16 )SKP_Silk_SQRT_APPROX( SKP_LSHIFT( ( SKP_int32 )psEnc->speech_activity_Q8, 8 ) ) ); + thrhld_Q15 = SKP_SMLABB( thrhld_Q15, 4587, psEnc->sCmn.prev_sigtype ); + thrhld_Q15 = SKP_MLA( thrhld_Q15, -31, SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) ); + thrhld_Q15 = SKP_SAT16( thrhld_Q15 ); + + /*****************************************/ + /* Call Pitch estimator */ + /*****************************************/ + psEncCtrl->sCmn.sigtype = SKP_Silk_pitch_analysis_core( res, psEncCtrl->sCmn.pitchL, &psEncCtrl->sCmn.lagIndex, + &psEncCtrl->sCmn.contourIndex, &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->pitchEstimationThreshold_Q16, + ( SKP_int16 )thrhld_Q15, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity ); +} diff --git a/jni/silk/src/SKP_Silk_find_pred_coefs_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_find_pred_coefs_FIX.c similarity index 97% rename from jni/silk/src/SKP_Silk_find_pred_coefs_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_find_pred_coefs_FIX.c index 57b091e..0347af7 100644 --- a/jni/silk/src/SKP_Silk_find_pred_coefs_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_find_pred_coefs_FIX.c @@ -1,148 +1,148 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - - -#define VARQ 1 // EXPERIMENTAL -#define Qx 0 // EXPERIMENTAL - -void SKP_Silk_find_pred_coefs_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ - const SKP_int16 res_pitch[] /* I Residual from pitch analysis */ -) -{ - SKP_int i; - SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ]; - SKP_int32 invGains_Q16[ NB_SUBFR ], local_gains_Qx[ NB_SUBFR ], Wght_Q15[ NB_SUBFR ]; - SKP_int NLSF_Q15[ MAX_LPC_ORDER ]; - const SKP_int16 *x_ptr; - SKP_int16 *x_pre_ptr, LPC_in_pre[ NB_SUBFR * MAX_LPC_ORDER + MAX_FRAME_LENGTH ]; - - SKP_int32 tmp, min_gain_Q16; -#if !VARQ - SKP_int LZ; -#endif - SKP_int LTP_corrs_rshift[ NB_SUBFR ]; - - - /* weighting for weighted least squares */ - min_gain_Q16 = SKP_int32_MAX >> 6; - for( i = 0; i < NB_SUBFR; i++ ) { - min_gain_Q16 = SKP_min( min_gain_Q16, psEncCtrl->Gains_Q16[ i ] ); - } -#if !VARQ - LZ = SKP_Silk_CLZ32( min_gain_Q16 ) - 1; - LZ = SKP_LIMIT( LZ, 0, 16 ); - min_gain_Q16 = SKP_RSHIFT( min_gain_Q16, 2 ); /* Ensure that maximum invGains_Q16 is within range of a 16 bit int */ -#endif - for( i = 0; i < NB_SUBFR; i++ ) { - /* Divide to Q16 */ - SKP_assert( psEncCtrl->Gains_Q16[ i ] > 0 ); -#if VARQ - /* Invert and normalize gains, and ensure that maximum invGains_Q16 is within range of a 16 bit int */ - invGains_Q16[ i ] = SKP_DIV32_varQ( min_gain_Q16, psEncCtrl->Gains_Q16[ i ], 16 - 2 ); -#else - invGains_Q16[ i ] = SKP_DIV32( SKP_LSHIFT( min_gain_Q16, LZ ), SKP_RSHIFT( psEncCtrl->Gains_Q16[ i ], 16 - LZ ) ); -#endif - - /* Ensure Wght_Q15 a minimum value 1 */ - invGains_Q16[ i ] = SKP_max( invGains_Q16[ i ], 363 ); - - /* Square the inverted gains */ - SKP_assert( invGains_Q16[ i ] == SKP_SAT16( invGains_Q16[ i ] ) ); - tmp = SKP_SMULWB( invGains_Q16[ i ], invGains_Q16[ i ] ); - Wght_Q15[ i ] = SKP_RSHIFT( tmp, 1 ); - - /* Invert the inverted and normalized gains */ - local_gains_Qx[ i ] = SKP_DIV32( ( 1 << ( 16 + Qx ) ), invGains_Q16[ i ] ); - } - - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /**********/ - /* VOICED */ - /**********/ - SKP_assert( psEnc->sCmn.frame_length - psEnc->sCmn.predictLPCOrder >= psEncCtrl->sCmn.pitchL[ 0 ] + LTP_ORDER / 2 ); - - /* LTP analysis */ - SKP_Silk_find_LTP_FIX( psEncCtrl->LTPCoef_Q14, WLTP, &psEncCtrl->LTPredCodGain_Q7, res_pitch, - res_pitch + SKP_RSHIFT( psEnc->sCmn.frame_length, 1 ), psEncCtrl->sCmn.pitchL, Wght_Q15, - psEnc->sCmn.subfr_length, psEnc->sCmn.frame_length, LTP_corrs_rshift ); - - - /* Quantize LTP gain parameters */ - SKP_Silk_quant_LTP_gains_FIX( psEncCtrl->LTPCoef_Q14, psEncCtrl->sCmn.LTPIndex, &psEncCtrl->sCmn.PERIndex, - WLTP, psEnc->mu_LTP_Q8, psEnc->sCmn.LTPQuantLowComplexity ); - - /* Control LTP scaling */ - SKP_Silk_LTP_scale_ctrl_FIX( psEnc, psEncCtrl ); - - /* Create LTP residual */ - SKP_Silk_LTP_analysis_filter_FIX( LPC_in_pre, psEnc->x_buf + psEnc->sCmn.frame_length - psEnc->sCmn.predictLPCOrder, - psEncCtrl->LTPCoef_Q14, psEncCtrl->sCmn.pitchL, invGains_Q16, 16, psEnc->sCmn.subfr_length, psEnc->sCmn.predictLPCOrder ); - - } else { - /************/ - /* UNVOICED */ - /************/ - /* Create signal with prepended subframes, scaled by inverse gains */ - x_ptr = psEnc->x_buf + psEnc->sCmn.frame_length - psEnc->sCmn.predictLPCOrder; - x_pre_ptr = LPC_in_pre; - for( i = 0; i < NB_SUBFR; i++ ) { - SKP_Silk_scale_copy_vector16( x_pre_ptr, x_ptr, invGains_Q16[ i ], - psEnc->sCmn.subfr_length + psEnc->sCmn.predictLPCOrder ); - x_pre_ptr += psEnc->sCmn.subfr_length + psEnc->sCmn.predictLPCOrder; - x_ptr += psEnc->sCmn.subfr_length; - } - - SKP_memset( psEncCtrl->LTPCoef_Q14, 0, NB_SUBFR * LTP_ORDER * sizeof( SKP_int16 ) ); - psEncCtrl->LTPredCodGain_Q7 = 0; - } - - /* LPC_in_pre contains the LTP-filtered input for voiced, and the unfiltered input for unvoiced */ - TIC(FIND_LPC) - SKP_Silk_find_LPC_FIX( NLSF_Q15, &psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEnc->sPred.prev_NLSFq_Q15, - psEnc->sCmn.useInterpolatedNLSFs * ( 1 - psEnc->sCmn.first_frame_after_reset ), psEnc->sCmn.predictLPCOrder, - LPC_in_pre, psEnc->sCmn.subfr_length + psEnc->sCmn.predictLPCOrder ); - TOC(FIND_LPC) - - - /* Quantize LSFs */ - TIC(PROCESS_LSFS) - SKP_Silk_process_NLSFs_FIX( psEnc, psEncCtrl, NLSF_Q15 ); - TOC(PROCESS_LSFS) - - /* Calculate residual energy using quantized LPC coefficients */ - SKP_Silk_residual_energy_FIX( psEncCtrl->ResNrg, psEncCtrl->ResNrgQ, LPC_in_pre, psEncCtrl->PredCoef_Q12, local_gains_Qx, Qx, - psEnc->sCmn.subfr_length, psEnc->sCmn.predictLPCOrder ); - - /* Copy to prediction struct for use in next frame for fluctuation reduction */ - SKP_memcpy( psEnc->sPred.prev_NLSFq_Q15, NLSF_Q15, psEnc->sCmn.predictLPCOrder * sizeof( SKP_int ) ); - -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + + +#define VARQ 1 // EXPERIMENTAL +#define Qx 0 // EXPERIMENTAL + +void SKP_Silk_find_pred_coefs_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ + const SKP_int16 res_pitch[] /* I Residual from pitch analysis */ +) +{ + SKP_int i; + SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ]; + SKP_int32 invGains_Q16[ NB_SUBFR ], local_gains_Qx[ NB_SUBFR ], Wght_Q15[ NB_SUBFR ]; + SKP_int NLSF_Q15[ MAX_LPC_ORDER ]; + const SKP_int16 *x_ptr; + SKP_int16 *x_pre_ptr, LPC_in_pre[ NB_SUBFR * MAX_LPC_ORDER + MAX_FRAME_LENGTH ]; + + SKP_int32 tmp, min_gain_Q16; +#if !VARQ + SKP_int LZ; +#endif + SKP_int LTP_corrs_rshift[ NB_SUBFR ]; + + + /* weighting for weighted least squares */ + min_gain_Q16 = SKP_int32_MAX >> 6; + for( i = 0; i < NB_SUBFR; i++ ) { + min_gain_Q16 = SKP_min( min_gain_Q16, psEncCtrl->Gains_Q16[ i ] ); + } +#if !VARQ + LZ = SKP_Silk_CLZ32( min_gain_Q16 ) - 1; + LZ = SKP_LIMIT( LZ, 0, 16 ); + min_gain_Q16 = SKP_RSHIFT( min_gain_Q16, 2 ); /* Ensure that maximum invGains_Q16 is within range of a 16 bit int */ +#endif + for( i = 0; i < NB_SUBFR; i++ ) { + /* Divide to Q16 */ + SKP_assert( psEncCtrl->Gains_Q16[ i ] > 0 ); +#if VARQ + /* Invert and normalize gains, and ensure that maximum invGains_Q16 is within range of a 16 bit int */ + invGains_Q16[ i ] = SKP_DIV32_varQ( min_gain_Q16, psEncCtrl->Gains_Q16[ i ], 16 - 2 ); +#else + invGains_Q16[ i ] = SKP_DIV32( SKP_LSHIFT( min_gain_Q16, LZ ), SKP_RSHIFT( psEncCtrl->Gains_Q16[ i ], 16 - LZ ) ); +#endif + + /* Ensure Wght_Q15 a minimum value 1 */ + invGains_Q16[ i ] = SKP_max( invGains_Q16[ i ], 363 ); + + /* Square the inverted gains */ + SKP_assert( invGains_Q16[ i ] == SKP_SAT16( invGains_Q16[ i ] ) ); + tmp = SKP_SMULWB( invGains_Q16[ i ], invGains_Q16[ i ] ); + Wght_Q15[ i ] = SKP_RSHIFT( tmp, 1 ); + + /* Invert the inverted and normalized gains */ + local_gains_Qx[ i ] = SKP_DIV32( ( 1 << ( 16 + Qx ) ), invGains_Q16[ i ] ); + } + + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /**********/ + /* VOICED */ + /**********/ + SKP_assert( psEnc->sCmn.frame_length - psEnc->sCmn.predictLPCOrder >= psEncCtrl->sCmn.pitchL[ 0 ] + LTP_ORDER / 2 ); + + /* LTP analysis */ + SKP_Silk_find_LTP_FIX( psEncCtrl->LTPCoef_Q14, WLTP, &psEncCtrl->LTPredCodGain_Q7, res_pitch, + res_pitch + SKP_RSHIFT( psEnc->sCmn.frame_length, 1 ), psEncCtrl->sCmn.pitchL, Wght_Q15, + psEnc->sCmn.subfr_length, psEnc->sCmn.frame_length, LTP_corrs_rshift ); + + + /* Quantize LTP gain parameters */ + SKP_Silk_quant_LTP_gains_FIX( psEncCtrl->LTPCoef_Q14, psEncCtrl->sCmn.LTPIndex, &psEncCtrl->sCmn.PERIndex, + WLTP, psEnc->mu_LTP_Q8, psEnc->sCmn.LTPQuantLowComplexity ); + + /* Control LTP scaling */ + SKP_Silk_LTP_scale_ctrl_FIX( psEnc, psEncCtrl ); + + /* Create LTP residual */ + SKP_Silk_LTP_analysis_filter_FIX( LPC_in_pre, psEnc->x_buf + psEnc->sCmn.frame_length - psEnc->sCmn.predictLPCOrder, + psEncCtrl->LTPCoef_Q14, psEncCtrl->sCmn.pitchL, invGains_Q16, 16, psEnc->sCmn.subfr_length, psEnc->sCmn.predictLPCOrder ); + + } else { + /************/ + /* UNVOICED */ + /************/ + /* Create signal with prepended subframes, scaled by inverse gains */ + x_ptr = psEnc->x_buf + psEnc->sCmn.frame_length - psEnc->sCmn.predictLPCOrder; + x_pre_ptr = LPC_in_pre; + for( i = 0; i < NB_SUBFR; i++ ) { + SKP_Silk_scale_copy_vector16( x_pre_ptr, x_ptr, invGains_Q16[ i ], + psEnc->sCmn.subfr_length + psEnc->sCmn.predictLPCOrder ); + x_pre_ptr += psEnc->sCmn.subfr_length + psEnc->sCmn.predictLPCOrder; + x_ptr += psEnc->sCmn.subfr_length; + } + + SKP_memset( psEncCtrl->LTPCoef_Q14, 0, NB_SUBFR * LTP_ORDER * sizeof( SKP_int16 ) ); + psEncCtrl->LTPredCodGain_Q7 = 0; + } + + /* LPC_in_pre contains the LTP-filtered input for voiced, and the unfiltered input for unvoiced */ + TIC(FIND_LPC) + SKP_Silk_find_LPC_FIX( NLSF_Q15, &psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEnc->sPred.prev_NLSFq_Q15, + psEnc->sCmn.useInterpolatedNLSFs * ( 1 - psEnc->sCmn.first_frame_after_reset ), psEnc->sCmn.predictLPCOrder, + LPC_in_pre, psEnc->sCmn.subfr_length + psEnc->sCmn.predictLPCOrder ); + TOC(FIND_LPC) + + + /* Quantize LSFs */ + TIC(PROCESS_LSFS) + SKP_Silk_process_NLSFs_FIX( psEnc, psEncCtrl, NLSF_Q15 ); + TOC(PROCESS_LSFS) + + /* Calculate residual energy using quantized LPC coefficients */ + SKP_Silk_residual_energy_FIX( psEncCtrl->ResNrg, psEncCtrl->ResNrgQ, LPC_in_pre, psEncCtrl->PredCoef_Q12, local_gains_Qx, Qx, + psEnc->sCmn.subfr_length, psEnc->sCmn.predictLPCOrder ); + + /* Copy to prediction struct for use in next frame for fluctuation reduction */ + SKP_memcpy( psEnc->sPred.prev_NLSFq_Q15, NLSF_Q15, psEnc->sCmn.predictLPCOrder * sizeof( SKP_int ) ); + +} + diff --git a/jni/silk/src/SKP_Silk_gain_quant.c b/app/src/main/jni/silk/src/SKP_Silk_gain_quant.c similarity index 98% rename from jni/silk/src/SKP_Silk_gain_quant.c rename to app/src/main/jni/silk/src/SKP_Silk_gain_quant.c index 6e1fb4b..ee909ed 100644 --- a/jni/silk/src/SKP_Silk_gain_quant.c +++ b/app/src/main/jni/silk/src/SKP_Silk_gain_quant.c @@ -1,94 +1,94 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -#define OFFSET ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 ) -#define SCALE_Q16 ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) -#define INV_SCALE_Q16 ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) ) - -/* Gain scalar quantization with hysteresis, uniform on log scale */ -void SKP_Silk_gains_quant( - SKP_int ind[ NB_SUBFR ], /* O gain indices */ - SKP_int32 gain_Q16[ NB_SUBFR ], /* I/O gains (quantized out) */ - SKP_int *prev_ind, /* I/O last index in previous frame */ - const SKP_int conditional /* I first gain is delta coded if 1 */ -) -{ - SKP_int k; - - for( k = 0; k < NB_SUBFR; k++ ) { - /* Add half of previous quantization error, convert to log scale, scale, floor() */ - ind[ k ] = SKP_SMULWB( SCALE_Q16, SKP_Silk_lin2log( gain_Q16[ k ] ) - OFFSET ); - - /* Round towards previous quantized gain (hysteresis) */ - if( ind[ k ] < *prev_ind ) { - ind[ k ]++; - } - - /* Compute delta indices and limit */ - if( k == 0 && conditional == 0 ) { - /* Full index */ - ind[ k ] = SKP_LIMIT( ind[ k ], 0, N_LEVELS_QGAIN - 1 ); - ind[ k ] = SKP_max_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT ); - *prev_ind = ind[ k ]; - } else { - /* Delta index */ - ind[ k ] = SKP_LIMIT( ind[ k ] - *prev_ind, MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT ); - /* Accumulate deltas */ - *prev_ind += ind[ k ]; - /* Shift to make non-negative */ - ind[ k ] -= MIN_DELTA_GAIN_QUANT; - } - - /* Convert to linear scale and scale */ - gain_Q16[ k ] = SKP_Silk_log2lin( SKP_min_32( SKP_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ - } -} - -/* Gains scalar dequantization, uniform on log scale */ -void SKP_Silk_gains_dequant( - SKP_int32 gain_Q16[ NB_SUBFR ], /* O quantized gains */ - const SKP_int ind[ NB_SUBFR ], /* I gain indices */ - SKP_int *prev_ind, /* I/O last index in previous frame */ - const SKP_int conditional /* I first gain is delta coded if 1 */ -) -{ - SKP_int k; - - for( k = 0; k < NB_SUBFR; k++ ) { - if( k == 0 && conditional == 0 ) { - *prev_ind = ind[ k ]; - } else { - /* Delta index */ - *prev_ind += ind[ k ] + MIN_DELTA_GAIN_QUANT; - } - - /* Convert to linear scale and scale */ - gain_Q16[ k ] = SKP_Silk_log2lin( SKP_min_32( SKP_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +#define OFFSET ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 ) +#define SCALE_Q16 ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) +#define INV_SCALE_Q16 ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) ) + +/* Gain scalar quantization with hysteresis, uniform on log scale */ +void SKP_Silk_gains_quant( + SKP_int ind[ NB_SUBFR ], /* O gain indices */ + SKP_int32 gain_Q16[ NB_SUBFR ], /* I/O gains (quantized out) */ + SKP_int *prev_ind, /* I/O last index in previous frame */ + const SKP_int conditional /* I first gain is delta coded if 1 */ +) +{ + SKP_int k; + + for( k = 0; k < NB_SUBFR; k++ ) { + /* Add half of previous quantization error, convert to log scale, scale, floor() */ + ind[ k ] = SKP_SMULWB( SCALE_Q16, SKP_Silk_lin2log( gain_Q16[ k ] ) - OFFSET ); + + /* Round towards previous quantized gain (hysteresis) */ + if( ind[ k ] < *prev_ind ) { + ind[ k ]++; + } + + /* Compute delta indices and limit */ + if( k == 0 && conditional == 0 ) { + /* Full index */ + ind[ k ] = SKP_LIMIT( ind[ k ], 0, N_LEVELS_QGAIN - 1 ); + ind[ k ] = SKP_max_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT ); + *prev_ind = ind[ k ]; + } else { + /* Delta index */ + ind[ k ] = SKP_LIMIT( ind[ k ] - *prev_ind, MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT ); + /* Accumulate deltas */ + *prev_ind += ind[ k ]; + /* Shift to make non-negative */ + ind[ k ] -= MIN_DELTA_GAIN_QUANT; + } + + /* Convert to linear scale and scale */ + gain_Q16[ k ] = SKP_Silk_log2lin( SKP_min_32( SKP_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ + } +} + +/* Gains scalar dequantization, uniform on log scale */ +void SKP_Silk_gains_dequant( + SKP_int32 gain_Q16[ NB_SUBFR ], /* O quantized gains */ + const SKP_int ind[ NB_SUBFR ], /* I gain indices */ + SKP_int *prev_ind, /* I/O last index in previous frame */ + const SKP_int conditional /* I first gain is delta coded if 1 */ +) +{ + SKP_int k; + + for( k = 0; k < NB_SUBFR; k++ ) { + if( k == 0 && conditional == 0 ) { + *prev_ind = ind[ k ]; + } else { + /* Delta index */ + *prev_ind += ind[ k ] + MIN_DELTA_GAIN_QUANT; + } + + /* Convert to linear scale and scale */ + gain_Q16[ k ] = SKP_Silk_log2lin( SKP_min_32( SKP_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ + } +} diff --git a/jni/silk/src/SKP_Silk_init_encoder_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_init_encoder_FIX.c similarity index 97% rename from jni/silk/src/SKP_Silk_init_encoder_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_init_encoder_FIX.c index 457c37b..3604904 100644 --- a/jni/silk/src/SKP_Silk_init_encoder_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_init_encoder_FIX.c @@ -1,64 +1,64 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/*********************************/ -/* Initialize Silk Encoder state */ -/*********************************/ -SKP_int SKP_Silk_init_encoder_FIX( - SKP_Silk_encoder_state_FIX *psEnc /* I/O Pointer to Silk encoder state */ -) -{ - SKP_int ret = 0; - /* Clear the entire encoder state */ - SKP_memset( psEnc, 0, sizeof( SKP_Silk_encoder_state_FIX ) ); - - /* Initialize to 24 kHz sampling, 20 ms packets, 25 kbps, 0% packet loss, and init non-zero values */ - ret = SKP_Silk_control_encoder_FIX( psEnc, 24, 20, 25, 0, 0, 0, 10, 1 ); - -#if HIGH_PASS_INPUT - psEnc->variable_HP_smth1_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */ - psEnc->variable_HP_smth2_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */ -#endif - - /* Used to deactivate e.g. LSF interpolation and fluctuation reduction */ - psEnc->sCmn.first_frame_after_reset = 1; - psEnc->sCmn.fs_kHz_changed = 0; - psEnc->sCmn.LBRR_enabled = 0; - - /* Initialize Silk VAD */ - ret += SKP_Silk_VAD_Init( &psEnc->sCmn.sVAD ); - - /* Initialize NSQ */ - psEnc->sNSQ.prev_inv_gain_Q16 = 65536; /* 1.0 in Q16 */ - psEnc->sNSQ_LBRR.prev_inv_gain_Q16 = 65536; /* 1.0 in Q16 */ - - psEnc->sCmn.bitstream_v = USE_BIT_STREAM_V; - - return( ret ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/*********************************/ +/* Initialize Silk Encoder state */ +/*********************************/ +SKP_int SKP_Silk_init_encoder_FIX( + SKP_Silk_encoder_state_FIX *psEnc /* I/O Pointer to Silk encoder state */ +) +{ + SKP_int ret = 0; + /* Clear the entire encoder state */ + SKP_memset( psEnc, 0, sizeof( SKP_Silk_encoder_state_FIX ) ); + + /* Initialize to 24 kHz sampling, 20 ms packets, 25 kbps, 0% packet loss, and init non-zero values */ + ret = SKP_Silk_control_encoder_FIX( psEnc, 24, 20, 25, 0, 0, 0, 10, 1 ); + +#if HIGH_PASS_INPUT + psEnc->variable_HP_smth1_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */ + psEnc->variable_HP_smth2_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */ +#endif + + /* Used to deactivate e.g. LSF interpolation and fluctuation reduction */ + psEnc->sCmn.first_frame_after_reset = 1; + psEnc->sCmn.fs_kHz_changed = 0; + psEnc->sCmn.LBRR_enabled = 0; + + /* Initialize Silk VAD */ + ret += SKP_Silk_VAD_Init( &psEnc->sCmn.sVAD ); + + /* Initialize NSQ */ + psEnc->sNSQ.prev_inv_gain_Q16 = 65536; /* 1.0 in Q16 */ + psEnc->sNSQ_LBRR.prev_inv_gain_Q16 = 65536; /* 1.0 in Q16 */ + + psEnc->sCmn.bitstream_v = USE_BIT_STREAM_V; + + return( ret ); +} diff --git a/jni/silk/src/SKP_Silk_inner_prod_aligned.c b/app/src/main/jni/silk/src/SKP_Silk_inner_prod_aligned.c similarity index 97% rename from jni/silk/src/SKP_Silk_inner_prod_aligned.c rename to app/src/main/jni/silk/src/SKP_Silk_inner_prod_aligned.c index 3ade172..b1476ec 100644 --- a/jni/silk/src/SKP_Silk_inner_prod_aligned.c +++ b/app/src/main/jni/silk/src/SKP_Silk_inner_prod_aligned.c @@ -1,96 +1,96 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_inner_prod_aligned.c * - * * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 080601 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* sum= for(i=0;i6, memory access can be reduced by half. */ - -SKP_int32 SKP_Silk_inner_prod_aligned( - const SKP_int16* const inVec1, /* I input vector 1 */ - const SKP_int16* const inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -) -{ - SKP_int i; - SKP_int32 sum = 0; - for( i = 0; i < len; i++ ) { - sum = SKP_SMLABB( sum, inVec1[ i ], inVec2[ i ] ); - } - return sum; -} - -SKP_int64 SKP_Silk_inner_prod_aligned_64( - const SKP_int32 *inVec1, /* I input vector 1 */ - const SKP_int32 *inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -) -{ - SKP_int i; - SKP_int64 sum = 0; - for( i = 0; i < len; i++ ) { - sum = SKP_SMLAL( sum, inVec1[ i ], inVec2[ i ] ); - } - return sum; -} -SKP_int64 SKP_Silk_inner_prod16_aligned_64( - const SKP_int16 *inVec1, /* I input vector 1 */ - const SKP_int16 *inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -) -{ - SKP_int i; - SKP_int64 sum = 0; - for( i = 0; i < len; i++ ) { - sum = SKP_SMLALBB( sum, inVec1[ i ], inVec2[ i ] ); - } - return sum; -} - -SKP_int32 SKP_Silk_inner_prod16_aligned_sat( - const SKP_int16* const inVec1, /* I input vector 1 */ - const SKP_int16* const inVec2, /* I input vector 2 */ - const SKP_int len /* I vector lengths */ -) -{ - SKP_int i; - SKP_int32 sum = 0; - for( i = 0; i < len; i++ ) { - sum = SKP_ADD_SAT32( sum, SKP_SMULBB( inVec1[ i ], inVec2[ i ] ) ); - } - return sum; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_inner_prod_aligned.c * + * * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 080601 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* sum= for(i=0;i6, memory access can be reduced by half. */ + +SKP_int32 SKP_Silk_inner_prod_aligned( + const SKP_int16* const inVec1, /* I input vector 1 */ + const SKP_int16* const inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +) +{ + SKP_int i; + SKP_int32 sum = 0; + for( i = 0; i < len; i++ ) { + sum = SKP_SMLABB( sum, inVec1[ i ], inVec2[ i ] ); + } + return sum; +} + +SKP_int64 SKP_Silk_inner_prod_aligned_64( + const SKP_int32 *inVec1, /* I input vector 1 */ + const SKP_int32 *inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +) +{ + SKP_int i; + SKP_int64 sum = 0; + for( i = 0; i < len; i++ ) { + sum = SKP_SMLAL( sum, inVec1[ i ], inVec2[ i ] ); + } + return sum; +} +SKP_int64 SKP_Silk_inner_prod16_aligned_64( + const SKP_int16 *inVec1, /* I input vector 1 */ + const SKP_int16 *inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +) +{ + SKP_int i; + SKP_int64 sum = 0; + for( i = 0; i < len; i++ ) { + sum = SKP_SMLALBB( sum, inVec1[ i ], inVec2[ i ] ); + } + return sum; +} + +SKP_int32 SKP_Silk_inner_prod16_aligned_sat( + const SKP_int16* const inVec1, /* I input vector 1 */ + const SKP_int16* const inVec2, /* I input vector 2 */ + const SKP_int len /* I vector lengths */ +) +{ + SKP_int i; + SKP_int32 sum = 0; + for( i = 0; i < len; i++ ) { + sum = SKP_ADD_SAT32( sum, SKP_SMULBB( inVec1[ i ], inVec2[ i ] ) ); + } + return sum; +} diff --git a/jni/silk/src/SKP_Silk_interpolate.c b/app/src/main/jni/silk/src/SKP_Silk_interpolate.c similarity index 98% rename from jni/silk/src/SKP_Silk_interpolate.c rename to app/src/main/jni/silk/src/SKP_Silk_interpolate.c index b12778f..96c127e 100644 --- a/jni/silk/src/SKP_Silk_interpolate.c +++ b/app/src/main/jni/silk/src/SKP_Silk_interpolate.c @@ -1,47 +1,47 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Interpolate two vectors */ -void SKP_Silk_interpolate( - SKP_int xi[ MAX_LPC_ORDER ], /* O interpolated vector */ - const SKP_int x0[ MAX_LPC_ORDER ], /* I first vector */ - const SKP_int x1[ MAX_LPC_ORDER ], /* I second vector */ - const SKP_int ifact_Q2, /* I interp. factor, weight on 2nd vector */ - const SKP_int d /* I number of parameters */ -) -{ - SKP_int i; - - SKP_assert( ifact_Q2 >= 0 ); - SKP_assert( ifact_Q2 <= ( 1 << 2 ) ); - - for( i = 0; i < d; i++ ) { - xi[ i ] = ( SKP_int )( ( SKP_int32 )x0[ i ] + SKP_RSHIFT( SKP_MUL( ( SKP_int32 )x1[ i ] - ( SKP_int32 )x0[ i ], ifact_Q2 ), 2 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Interpolate two vectors */ +void SKP_Silk_interpolate( + SKP_int xi[ MAX_LPC_ORDER ], /* O interpolated vector */ + const SKP_int x0[ MAX_LPC_ORDER ], /* I first vector */ + const SKP_int x1[ MAX_LPC_ORDER ], /* I second vector */ + const SKP_int ifact_Q2, /* I interp. factor, weight on 2nd vector */ + const SKP_int d /* I number of parameters */ +) +{ + SKP_int i; + + SKP_assert( ifact_Q2 >= 0 ); + SKP_assert( ifact_Q2 <= ( 1 << 2 ) ); + + for( i = 0; i < d; i++ ) { + xi[ i ] = ( SKP_int )( ( SKP_int32 )x0[ i ] + SKP_RSHIFT( SKP_MUL( ( SKP_int32 )x1[ i ] - ( SKP_int32 )x0[ i ], ifact_Q2 ), 2 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_k2a.c b/app/src/main/jni/silk/src/SKP_Silk_k2a.c similarity index 98% rename from jni/silk/src/SKP_Silk_k2a.c rename to app/src/main/jni/silk/src/SKP_Silk_k2a.c index 0f9c341..7d844fc 100644 --- a/jni/silk/src/SKP_Silk_k2a.c +++ b/app/src/main/jni/silk/src/SKP_Silk_k2a.c @@ -1,58 +1,58 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_k2a.c * - * * - * Step up function, converts reflection coefficients to prediction * - * coefficients * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 080103 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Step up function, converts reflection coefficients to prediction coefficients */ -void SKP_Silk_k2a( - SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ - const SKP_int16 *rc_Q15, /* I: Reflection coefficients [order] Q15 */ - const SKP_int32 order /* I: Prediction order */ -) -{ - SKP_int k, n; - SKP_int32 Atmp[ SigProc_MAX_ORDER_LPC ]; - - for( k = 0; k < order; k++ ) { - for( n = 0; n < k; n++ ) { - Atmp[ n ] = A_Q24[ n ]; - } - for( n = 0; n < k; n++ ) { - A_Q24[ n ] = SKP_SMLAWB( A_Q24[ n ], SKP_LSHIFT( Atmp[ k - n - 1 ], 1 ), rc_Q15[ k ] ); - } - A_Q24[ k ] = -SKP_LSHIFT( (SKP_int32)rc_Q15[ k ], 9 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_k2a.c * + * * + * Step up function, converts reflection coefficients to prediction * + * coefficients * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 080103 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Step up function, converts reflection coefficients to prediction coefficients */ +void SKP_Silk_k2a( + SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ + const SKP_int16 *rc_Q15, /* I: Reflection coefficients [order] Q15 */ + const SKP_int32 order /* I: Prediction order */ +) +{ + SKP_int k, n; + SKP_int32 Atmp[ SigProc_MAX_ORDER_LPC ]; + + for( k = 0; k < order; k++ ) { + for( n = 0; n < k; n++ ) { + Atmp[ n ] = A_Q24[ n ]; + } + for( n = 0; n < k; n++ ) { + A_Q24[ n ] = SKP_SMLAWB( A_Q24[ n ], SKP_LSHIFT( Atmp[ k - n - 1 ], 1 ), rc_Q15[ k ] ); + } + A_Q24[ k ] = -SKP_LSHIFT( (SKP_int32)rc_Q15[ k ], 9 ); + } +} diff --git a/jni/silk/src/SKP_Silk_k2a_Q16.c b/app/src/main/jni/silk/src/SKP_Silk_k2a_Q16.c similarity index 98% rename from jni/silk/src/SKP_Silk_k2a_Q16.c rename to app/src/main/jni/silk/src/SKP_Silk_k2a_Q16.c index bc8e78c..10d0f69 100644 --- a/jni/silk/src/SKP_Silk_k2a_Q16.c +++ b/app/src/main/jni/silk/src/SKP_Silk_k2a_Q16.c @@ -1,58 +1,58 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_k2a.c * - * * - * Step up function, converts reflection coefficients to prediction * - * coefficients * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 080103 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Step up function, converts reflection coefficients to prediction coefficients */ -void SKP_Silk_k2a_Q16( - SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ - const SKP_int32 *rc_Q16, /* I: Reflection coefficients [order] Q16 */ - const SKP_int32 order /* I: Prediction order */ -) -{ - SKP_int k, n; - SKP_int32 Atmp[ SigProc_MAX_ORDER_LPC ]; - - for( k = 0; k < order; k++ ) { - for( n = 0; n < k; n++ ) { - Atmp[ n ] = A_Q24[ n ]; - } - for( n = 0; n < k; n++ ) { - A_Q24[ n ] = SKP_SMLAWW( A_Q24[ n ], Atmp[ k - n - 1 ], rc_Q16[ k ] ); - } - A_Q24[ k ] = -SKP_LSHIFT( rc_Q16[ k ], 8 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_k2a.c * + * * + * Step up function, converts reflection coefficients to prediction * + * coefficients * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 080103 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Step up function, converts reflection coefficients to prediction coefficients */ +void SKP_Silk_k2a_Q16( + SKP_int32 *A_Q24, /* O: Prediction coefficients [order] Q24 */ + const SKP_int32 *rc_Q16, /* I: Reflection coefficients [order] Q16 */ + const SKP_int32 order /* I: Prediction order */ +) +{ + SKP_int k, n; + SKP_int32 Atmp[ SigProc_MAX_ORDER_LPC ]; + + for( k = 0; k < order; k++ ) { + for( n = 0; n < k; n++ ) { + Atmp[ n ] = A_Q24[ n ]; + } + for( n = 0; n < k; n++ ) { + A_Q24[ n ] = SKP_SMLAWW( A_Q24[ n ], Atmp[ k - n - 1 ], rc_Q16[ k ] ); + } + A_Q24[ k ] = -SKP_LSHIFT( rc_Q16[ k ], 8 ); + } +} diff --git a/jni/silk/src/SKP_Silk_lin2log.c b/app/src/main/jni/silk/src/SKP_Silk_lin2log.c similarity index 98% rename from jni/silk/src/SKP_Silk_lin2log.c rename to app/src/main/jni/silk/src/SKP_Silk_lin2log.c index 6b3e7ed..1f36bd7 100644 --- a/jni/silk/src/SKP_Silk_lin2log.c +++ b/app/src/main/jni/silk/src/SKP_Silk_lin2log.c @@ -1,49 +1,49 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_lin2log.c * - * * - * Convert input to a log scale * - * Approximation of 128 * log2() * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" -/* Approximation of 128 * log2() (very close inverse of approx 2^() below) */ -/* Convert input to a log scale */ -SKP_int32 SKP_Silk_lin2log( const SKP_int32 inLin ) /* I: Input in linear scale */ -{ - SKP_int32 lz, frac_Q7; - - SKP_Silk_CLZ_FRAC( inLin, &lz, &frac_Q7 ); - - /* Piece-wise parabolic approximation */ - return( SKP_LSHIFT( 31 - lz, 7 ) + SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), 179 ) ); -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_lin2log.c * + * * + * Convert input to a log scale * + * Approximation of 128 * log2() * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" +/* Approximation of 128 * log2() (very close inverse of approx 2^() below) */ +/* Convert input to a log scale */ +SKP_int32 SKP_Silk_lin2log( const SKP_int32 inLin ) /* I: Input in linear scale */ +{ + SKP_int32 lz, frac_Q7; + + SKP_Silk_CLZ_FRAC( inLin, &lz, &frac_Q7 ); + + /* Piece-wise parabolic approximation */ + return( SKP_LSHIFT( 31 - lz, 7 ) + SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), 179 ) ); +} + diff --git a/jni/silk/src/SKP_Silk_log2lin.c b/app/src/main/jni/silk/src/SKP_Silk_log2lin.c similarity index 98% rename from jni/silk/src/SKP_Silk_log2lin.c rename to app/src/main/jni/silk/src/SKP_Silk_log2lin.c index b06ce30..4e23a31 100644 --- a/jni/silk/src/SKP_Silk_log2lin.c +++ b/app/src/main/jni/silk/src/SKP_Silk_log2lin.c @@ -1,58 +1,58 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_log2lin.c * - * * - * Convert input to a linear scale * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Approximation of 2^() (very close inverse of SKP_Silk_lin2log()) */ -/* Convert input to a linear scale */ -SKP_int32 SKP_Silk_log2lin( const SKP_int32 inLog_Q7 ) /* I: Input on log scale */ -{ - SKP_int32 out, frac_Q7; - - if( inLog_Q7 < 0 ) { - return 0; - } - - out = SKP_LSHIFT( 1, SKP_RSHIFT( inLog_Q7, 7 ) ); - frac_Q7 = inLog_Q7 & 0x7F; - if( inLog_Q7 < 2048 ) { - /* Piece-wise parabolic approximation */ - out = SKP_ADD_RSHIFT( out, SKP_MUL( out, SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), -174 ) ), 7 ); - } else { - /* Piece-wise parabolic approximation */ - out = SKP_MLA( out, SKP_RSHIFT( out, 7 ), SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), -174 ) ); - } - return out; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_log2lin.c * + * * + * Convert input to a linear scale * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Approximation of 2^() (very close inverse of SKP_Silk_lin2log()) */ +/* Convert input to a linear scale */ +SKP_int32 SKP_Silk_log2lin( const SKP_int32 inLog_Q7 ) /* I: Input on log scale */ +{ + SKP_int32 out, frac_Q7; + + if( inLog_Q7 < 0 ) { + return 0; + } + + out = SKP_LSHIFT( 1, SKP_RSHIFT( inLog_Q7, 7 ) ); + frac_Q7 = inLog_Q7 & 0x7F; + if( inLog_Q7 < 2048 ) { + /* Piece-wise parabolic approximation */ + out = SKP_ADD_RSHIFT( out, SKP_MUL( out, SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), -174 ) ), 7 ); + } else { + /* Piece-wise parabolic approximation */ + out = SKP_MLA( out, SKP_RSHIFT( out, 7 ), SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), -174 ) ); + } + return out; +} diff --git a/jni/silk/src/SKP_Silk_lowpass_int.c b/app/src/main/jni/silk/src/SKP_Silk_lowpass_int.c similarity index 98% rename from jni/silk/src/SKP_Silk_lowpass_int.c rename to app/src/main/jni/silk/src/SKP_Silk_lowpass_int.c index fbe9bda..08544df 100644 --- a/jni/silk/src/SKP_Silk_lowpass_int.c +++ b/app/src/main/jni/silk/src/SKP_Silk_lowpass_int.c @@ -1,61 +1,61 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_lowpass_int.c * - * * - * First order low-pass filter, with input as SKP_int32, running at * - * 48 kHz * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* First order low-pass filter, with input as SKP_int32, running at 48 kHz */ -void SKP_Silk_lowpass_int( - const SKP_int32 *in, /* I: Q25 48 kHz signal; length = len */ - SKP_int32 *S, /* I/O: Q25 state; length = 1 */ - SKP_int32 *out, /* O: Q25 48 kHz signal; length = len */ - const SKP_int32 len /* I: Number of samples */ -) -{ - SKP_int k; - SKP_int32 in_tmp, out_tmp, state; - - state = S[ 0 ]; - for( k = len; k > 0; k-- ) { - in_tmp = *in++; - in_tmp -= SKP_RSHIFT( in_tmp, 2 ); /* multiply by 0.75 */ - out_tmp = state + in_tmp; /* zero at nyquist */ - state = in_tmp - SKP_RSHIFT( out_tmp, 1 ); /* pole */ - *out++ = out_tmp; - } - S[ 0 ] = state; -} - - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_lowpass_int.c * + * * + * First order low-pass filter, with input as SKP_int32, running at * + * 48 kHz * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* First order low-pass filter, with input as SKP_int32, running at 48 kHz */ +void SKP_Silk_lowpass_int( + const SKP_int32 *in, /* I: Q25 48 kHz signal; length = len */ + SKP_int32 *S, /* I/O: Q25 state; length = 1 */ + SKP_int32 *out, /* O: Q25 48 kHz signal; length = len */ + const SKP_int32 len /* I: Number of samples */ +) +{ + SKP_int k; + SKP_int32 in_tmp, out_tmp, state; + + state = S[ 0 ]; + for( k = len; k > 0; k-- ) { + in_tmp = *in++; + in_tmp -= SKP_RSHIFT( in_tmp, 2 ); /* multiply by 0.75 */ + out_tmp = state + in_tmp; /* zero at nyquist */ + state = in_tmp - SKP_RSHIFT( out_tmp, 1 ); /* pole */ + *out++ = out_tmp; + } + S[ 0 ] = state; +} + + diff --git a/jni/silk/src/SKP_Silk_lowpass_short.c b/app/src/main/jni/silk/src/SKP_Silk_lowpass_short.c similarity index 98% rename from jni/silk/src/SKP_Silk_lowpass_short.c rename to app/src/main/jni/silk/src/SKP_Silk_lowpass_short.c index f8bfcf6..efb7447 100644 --- a/jni/silk/src/SKP_Silk_lowpass_short.c +++ b/app/src/main/jni/silk/src/SKP_Silk_lowpass_short.c @@ -1,61 +1,61 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_lowpass_short.c * - * * - * First order low-pass filter, with input as SKP_int16, running at * - * 48 kHz * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - - -/* First order low-pass filter, with input as SKP_int16, running at 48 kHz */ -void SKP_Silk_lowpass_short( - const SKP_int16 *in, /* I: Q15 48 kHz signal; [len] */ - SKP_int32 *S, /* I/O: Q25 state; length = 1 */ - SKP_int32 *out, /* O: Q25 48 kHz signal; [len] */ - const SKP_int32 len /* O: Signal length */ -) -{ - SKP_int k; - SKP_int32 in_tmp, out_tmp, state; - - state = S[ 0 ]; - for( k = 0; k < len; k++ ) { - in_tmp = SKP_MUL( 768, (SKP_int32)in[k] ); /* multiply by 0.75, going from Q15 to Q25 */ - out_tmp = state + in_tmp; /* zero at nyquist */ - state = in_tmp - SKP_RSHIFT( out_tmp, 1 ); /* pole */ - out[ k ] = out_tmp; - } - S[ 0 ] = state; -} - - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_lowpass_short.c * + * * + * First order low-pass filter, with input as SKP_int16, running at * + * 48 kHz * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + + +/* First order low-pass filter, with input as SKP_int16, running at 48 kHz */ +void SKP_Silk_lowpass_short( + const SKP_int16 *in, /* I: Q15 48 kHz signal; [len] */ + SKP_int32 *S, /* I/O: Q25 state; length = 1 */ + SKP_int32 *out, /* O: Q25 48 kHz signal; [len] */ + const SKP_int32 len /* O: Signal length */ +) +{ + SKP_int k; + SKP_int32 in_tmp, out_tmp, state; + + state = S[ 0 ]; + for( k = 0; k < len; k++ ) { + in_tmp = SKP_MUL( 768, (SKP_int32)in[k] ); /* multiply by 0.75, going from Q15 to Q25 */ + out_tmp = state + in_tmp; /* zero at nyquist */ + state = in_tmp - SKP_RSHIFT( out_tmp, 1 ); /* pole */ + out[ k ] = out_tmp; + } + S[ 0 ] = state; +} + + diff --git a/jni/silk/src/SKP_Silk_macros.h b/app/src/main/jni/silk/src/SKP_Silk_macros.h similarity index 97% rename from jni/silk/src/SKP_Silk_macros.h rename to app/src/main/jni/silk/src/SKP_Silk_macros.h index c42aa76..499adfa 100644 --- a/jni/silk/src/SKP_Silk_macros.h +++ b/app/src/main/jni/silk/src/SKP_Silk_macros.h @@ -1,122 +1,122 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef _SKP_SILK_API_C_H_ -#define _SKP_SILK_API_C_H_ - -// This is an inline header file for general platform. - -// (a32 * (SKP_int32)((SKP_int16)(b32))) >> 16 output have to be 32bit int -#define SKP_SMULWB(a32, b32) ((((a32) >> 16) * (SKP_int32)((SKP_int16)(b32))) + ((((a32) & 0x0000FFFF) * (SKP_int32)((SKP_int16)(b32))) >> 16)) - -// a32 + (b32 * (SKP_int32)((SKP_int16)(c32))) >> 16 output have to be 32bit int -#define SKP_SMLAWB(a32, b32, c32) ((a32) + ((((b32) >> 16) * (SKP_int32)((SKP_int16)(c32))) + ((((b32) & 0x0000FFFF) * (SKP_int32)((SKP_int16)(c32))) >> 16))) - -// (a32 * (b32 >> 16)) >> 16 -#define SKP_SMULWT(a32, b32) (((a32) >> 16) * ((b32) >> 16) + ((((a32) & 0x0000FFFF) * ((b32) >> 16)) >> 16)) - -// a32 + (b32 * (c32 >> 16)) >> 16 -#define SKP_SMLAWT(a32, b32, c32) ((a32) + (((b32) >> 16) * ((c32) >> 16)) + ((((b32) & 0x0000FFFF) * ((c32) >> 16)) >> 16)) - -// (SKP_int32)((SKP_int16)(a3))) * (SKP_int32)((SKP_int16)(b32)) output have to be 32bit int -#define SKP_SMULBB(a32, b32) ((SKP_int32)((SKP_int16)(a32)) * (SKP_int32)((SKP_int16)(b32))) - -// a32 + (SKP_int32)((SKP_int16)(b32)) * (SKP_int32)((SKP_int16)(c32)) output have to be 32bit int -#define SKP_SMLABB(a32, b32, c32) ((a32) + ((SKP_int32)((SKP_int16)(b32))) * (SKP_int32)((SKP_int16)(c32))) - -// (SKP_int32)((SKP_int16)(a32)) * (b32 >> 16) -#define SKP_SMULBT(a32, b32) ((SKP_int32)((SKP_int16)(a32)) * ((b32) >> 16)) - -// a32 + (SKP_int32)((SKP_int16)(b32)) * (c32 >> 16) -#define SKP_SMLABT(a32, b32, c32) ((a32) + ((SKP_int32)((SKP_int16)(b32))) * ((c32) >> 16)) - -// a64 + (b32 * c32) -#define SKP_SMLAL(a64, b32, c32) (SKP_ADD64((a64), ((SKP_int64)(b32) * (SKP_int64)(c32)))) - -// (a32 * b32) >> 16 -#define SKP_SMULWW(a32, b32) SKP_MLA(SKP_SMULWB((a32), (b32)), (a32), SKP_RSHIFT_ROUND((b32), 16)) - -// a32 + ((b32 * c32) >> 16) -#define SKP_SMLAWW(a32, b32, c32) SKP_MLA(SKP_SMLAWB((a32), (b32), (c32)), (b32), SKP_RSHIFT_ROUND((c32), 16)) - -/* add/subtract with output saturated */ -#define SKP_ADD_SAT32(a, b) ((((a) + (b)) & 0x80000000) == 0 ? \ - ((((a) & (b)) & 0x80000000) != 0 ? SKP_int32_MIN : (a)+(b)) : \ - ((((a) | (b)) & 0x80000000) == 0 ? SKP_int32_MAX : (a)+(b)) ) - -#define SKP_SUB_SAT32(a, b) ((((a)-(b)) & 0x80000000) == 0 ? \ - (( (a) & ((b)^0x80000000) & 0x80000000) ? SKP_int32_MIN : (a)-(b)) : \ - ((((a)^0x80000000) & (b) & 0x80000000) ? SKP_int32_MAX : (a)-(b)) ) - -SKP_INLINE SKP_int32 SKP_Silk_CLZ16(SKP_int16 in16) -{ - SKP_int32 out32 = 0; - if( in16 == 0 ) { - return 16; - } - /* test nibbles */ - if( in16 & 0xFF00 ) { - if( in16 & 0xF000 ) { - in16 >>= 12; - } else { - out32 += 4; - in16 >>= 8; - } - } else { - if( in16 & 0xFFF0 ) { - out32 += 8; - in16 >>= 4; - } else { - out32 += 12; - } - } - /* test bits and return */ - if( in16 & 0xC ) { - if( in16 & 0x8 ) - return out32 + 0; - else - return out32 + 1; - } else { - if( in16 & 0xE ) - return out32 + 2; - else - return out32 + 3; - } -} - -SKP_INLINE SKP_int32 SKP_Silk_CLZ32(SKP_int32 in32) -{ - /* test highest 16 bits and convert to SKP_int16 */ - if( in32 & 0xFFFF0000 ) { - return SKP_Silk_CLZ16((SKP_int16)(in32 >> 16)); - } else { - return SKP_Silk_CLZ16((SKP_int16)in32) + 16; - } -} - -#endif //_SKP_SILK_API_C_H_ - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef _SKP_SILK_API_C_H_ +#define _SKP_SILK_API_C_H_ + +// This is an inline header file for general platform. + +// (a32 * (SKP_int32)((SKP_int16)(b32))) >> 16 output have to be 32bit int +#define SKP_SMULWB(a32, b32) ((((a32) >> 16) * (SKP_int32)((SKP_int16)(b32))) + ((((a32) & 0x0000FFFF) * (SKP_int32)((SKP_int16)(b32))) >> 16)) + +// a32 + (b32 * (SKP_int32)((SKP_int16)(c32))) >> 16 output have to be 32bit int +#define SKP_SMLAWB(a32, b32, c32) ((a32) + ((((b32) >> 16) * (SKP_int32)((SKP_int16)(c32))) + ((((b32) & 0x0000FFFF) * (SKP_int32)((SKP_int16)(c32))) >> 16))) + +// (a32 * (b32 >> 16)) >> 16 +#define SKP_SMULWT(a32, b32) (((a32) >> 16) * ((b32) >> 16) + ((((a32) & 0x0000FFFF) * ((b32) >> 16)) >> 16)) + +// a32 + (b32 * (c32 >> 16)) >> 16 +#define SKP_SMLAWT(a32, b32, c32) ((a32) + (((b32) >> 16) * ((c32) >> 16)) + ((((b32) & 0x0000FFFF) * ((c32) >> 16)) >> 16)) + +// (SKP_int32)((SKP_int16)(a3))) * (SKP_int32)((SKP_int16)(b32)) output have to be 32bit int +#define SKP_SMULBB(a32, b32) ((SKP_int32)((SKP_int16)(a32)) * (SKP_int32)((SKP_int16)(b32))) + +// a32 + (SKP_int32)((SKP_int16)(b32)) * (SKP_int32)((SKP_int16)(c32)) output have to be 32bit int +#define SKP_SMLABB(a32, b32, c32) ((a32) + ((SKP_int32)((SKP_int16)(b32))) * (SKP_int32)((SKP_int16)(c32))) + +// (SKP_int32)((SKP_int16)(a32)) * (b32 >> 16) +#define SKP_SMULBT(a32, b32) ((SKP_int32)((SKP_int16)(a32)) * ((b32) >> 16)) + +// a32 + (SKP_int32)((SKP_int16)(b32)) * (c32 >> 16) +#define SKP_SMLABT(a32, b32, c32) ((a32) + ((SKP_int32)((SKP_int16)(b32))) * ((c32) >> 16)) + +// a64 + (b32 * c32) +#define SKP_SMLAL(a64, b32, c32) (SKP_ADD64((a64), ((SKP_int64)(b32) * (SKP_int64)(c32)))) + +// (a32 * b32) >> 16 +#define SKP_SMULWW(a32, b32) SKP_MLA(SKP_SMULWB((a32), (b32)), (a32), SKP_RSHIFT_ROUND((b32), 16)) + +// a32 + ((b32 * c32) >> 16) +#define SKP_SMLAWW(a32, b32, c32) SKP_MLA(SKP_SMLAWB((a32), (b32), (c32)), (b32), SKP_RSHIFT_ROUND((c32), 16)) + +/* add/subtract with output saturated */ +#define SKP_ADD_SAT32(a, b) ((((a) + (b)) & 0x80000000) == 0 ? \ + ((((a) & (b)) & 0x80000000) != 0 ? SKP_int32_MIN : (a)+(b)) : \ + ((((a) | (b)) & 0x80000000) == 0 ? SKP_int32_MAX : (a)+(b)) ) + +#define SKP_SUB_SAT32(a, b) ((((a)-(b)) & 0x80000000) == 0 ? \ + (( (a) & ((b)^0x80000000) & 0x80000000) ? SKP_int32_MIN : (a)-(b)) : \ + ((((a)^0x80000000) & (b) & 0x80000000) ? SKP_int32_MAX : (a)-(b)) ) + +SKP_INLINE SKP_int32 SKP_Silk_CLZ16(SKP_int16 in16) +{ + SKP_int32 out32 = 0; + if( in16 == 0 ) { + return 16; + } + /* test nibbles */ + if( in16 & 0xFF00 ) { + if( in16 & 0xF000 ) { + in16 >>= 12; + } else { + out32 += 4; + in16 >>= 8; + } + } else { + if( in16 & 0xFFF0 ) { + out32 += 8; + in16 >>= 4; + } else { + out32 += 12; + } + } + /* test bits and return */ + if( in16 & 0xC ) { + if( in16 & 0x8 ) + return out32 + 0; + else + return out32 + 1; + } else { + if( in16 & 0xE ) + return out32 + 2; + else + return out32 + 3; + } +} + +SKP_INLINE SKP_int32 SKP_Silk_CLZ32(SKP_int32 in32) +{ + /* test highest 16 bits and convert to SKP_int16 */ + if( in32 & 0xFFFF0000 ) { + return SKP_Silk_CLZ16((SKP_int16)(in32 >> 16)); + } else { + return SKP_Silk_CLZ16((SKP_int16)in32) + 16; + } +} + +#endif //_SKP_SILK_API_C_H_ + diff --git a/jni/silk/src/SKP_Silk_main.h b/app/src/main/jni/silk/src/SKP_Silk_main.h similarity index 98% rename from jni/silk/src/SKP_Silk_main.h rename to app/src/main/jni/silk/src/SKP_Silk_main.h index d5b0a73..f6f1c54 100644 --- a/jni/silk/src/SKP_Silk_main.h +++ b/app/src/main/jni/silk/src/SKP_Silk_main.h @@ -1,405 +1,405 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_MAIN_H -#define SKP_SILK_MAIN_H - -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_define.h" -#include "SKP_Silk_structs.h" -#include "SKP_Silk_tables.h" -#include "SKP_Silk_PLC.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* Encodes signs of excitation */ -void SKP_Silk_encode_signs( - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - const SKP_int q[], /* I pulse signal */ - const SKP_int length, /* I length of input */ - const SKP_int sigtype, /* I Signal type */ - const SKP_int QuantOffsetType, /* I Quantization offset type */ - const SKP_int RateLevelIndex /* I Rate Level Index */ -); - -/* Decodes signs of excitation */ -void SKP_Silk_decode_signs( - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - SKP_int q[], /* I/O pulse signal */ - const SKP_int length, /* I length of output */ - const SKP_int sigtype, /* I Signal type */ - const SKP_int QuantOffsetType, /* I Quantization offset type */ - const SKP_int RateLevelIndex /* I Rate Level Index */ -); - -/***************/ -/* Shell coder */ -/***************/ - -/* Encode quantization indices of excitation */ -void SKP_Silk_encode_pulses( - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - const SKP_int sigtype, /* I Sigtype */ - const SKP_int QuantOffsetType, /* I QuantOffsetType */ - const SKP_int q[], /* I quantization indices */ - const SKP_int frame_length /* I Frame length */ -); - -/* Shell encoder, operates on one shell code frame of 16 pulses */ -void SKP_Silk_shell_encoder( - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_int *pulses0 /* I data: nonnegative pulse amplitudes */ -); - -/* Shell decoder, operates on one shell code frame of 16 pulses */ -void SKP_Silk_shell_decoder( - SKP_int *pulses0, /* O data: nonnegative pulse amplitudes */ - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_int pulses4 /* I number of pulses per pulse-subframe */ -); - -/***************/ -/* Range coder */ -/***************/ -/* Range encoder for one symbol */ -void SKP_Silk_range_encoder( - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_int data, /* I uncompressed data */ - const SKP_uint16 prob[] /* I cumulative density functions */ -); - -/* Range encoder for multiple symbols */ -void SKP_Silk_range_encoder_multi( - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_int data[], /* I uncompressed data [nSymbols] */ - const SKP_uint16 * const prob[], /* I cumulative density functions */ - const SKP_int nSymbols /* I number of data symbols */ -); - -/* Range decoder for one symbol */ -void SKP_Silk_range_decoder( - SKP_int data[], /* O uncompressed data */ - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_uint16 prob[], /* I cumulative density function */ - SKP_int probIx /* I initial (middle) entry of cdf */ -); - -/* Range decoder for multiple symbols */ -void SKP_Silk_range_decoder_multi( - SKP_int data[], /* O uncompressed data [nSymbols] */ - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_uint16 * const prob[], /* I cumulative density functions */ - const SKP_int probStartIx[], /* I initial (middle) entries of cdfs [nSymbols] */ - const SKP_int nSymbols /* I number of data symbols */ -); - -/* Initialize range coder structure for encoder */ -void SKP_Silk_range_enc_init( - SKP_Silk_range_coder_state *psRC /* O compressor data structure */ -); - -/* Initialize range coder structure for decoder */ -void SKP_Silk_range_dec_init( - SKP_Silk_range_coder_state *psRC, /* O compressor data structure */ - const SKP_uint8 buffer[], /* I buffer for compressed data [bufferLength] */ - const SKP_int32 bufferLength /* I buffer length (in bytes) */ -); - -/* Determine length of bitstream */ -SKP_int SKP_Silk_range_coder_get_length( /* O returns number of BITS in stream */ - const SKP_Silk_range_coder_state *psRC, /* I compressed data structure */ - SKP_int *nBytes /* O number of BYTES in stream */ -); - -/* Write decodable stream to buffer, and determine its length */ -void SKP_Silk_range_enc_wrap_up( - SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ -); - -/* Check that any remaining bits in the last byte are set to 1 */ -void SKP_Silk_range_coder_check_after_decoding( - SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ -); - -/* Gain scalar quantization with hysteresis, uniform on log scale */ -void SKP_Silk_gains_quant( - SKP_int ind[ NB_SUBFR ], /* O gain indices */ - SKP_int32 gain_Q16[ NB_SUBFR ], /* I/O gains (quantized out) */ - SKP_int *prev_ind, /* I/O last index in previous frame */ - const SKP_int conditional /* I first gain is delta coded if 1 */ -); - -/* Gains scalar dequantization, uniform on log scale */ -void SKP_Silk_gains_dequant( - SKP_int32 gain_Q16[ NB_SUBFR ], /* O quantized gains */ - const SKP_int ind[ NB_SUBFR ], /* I gain indices */ - SKP_int *prev_ind, /* I/O last index in previous frame */ - const SKP_int conditional /* I first gain is delta coded if 1 */ -); - -/* Convert NLSF parameters to stable AR prediction filter coefficients */ -void SKP_Silk_NLSF2A_stable( - SKP_int16 pAR_Q12[ MAX_LPC_ORDER ], /* O Stabilized AR coefs [LPC_order] */ - const SKP_int pNLSF[ MAX_LPC_ORDER ], /* I NLSF vector [LPC_order] */ - const SKP_int LPC_order /* I LPC/LSF order */ -); - -/* Interpolate two vectors */ -void SKP_Silk_interpolate( - SKP_int xi[ MAX_LPC_ORDER ], /* O interpolated vector */ - const SKP_int x0[ MAX_LPC_ORDER ], /* I first vector */ - const SKP_int x1[ MAX_LPC_ORDER ], /* I second vector */ - const SKP_int ifact_Q2, /* I interp. factor, weight on 2nd vector */ - const SKP_int d /* I number of parameters */ -); - -/***********************************/ -/* Noise shaping quantization (NSQ)*/ -/***********************************/ -void SKP_Silk_NSQ( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ - SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - const SKP_int16 x[], /* I prefiltered input signal */ - SKP_int q[], /* O quantized qulse signal */ - const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ - const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Short term prediction coefficients */ - const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I Long term prediction coefficients */ - const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ - const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ - const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ - const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int Lambda_Q10, /* I */ - const SKP_int LTP_scale_Q14 /* I LTP state scaling */ -); - -/* Noise shaping using delayed decision */ -void SKP_Silk_NSQ_del_dec( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ - SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ - SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ - const SKP_int16 x[], /* I Prefiltered input signal */ - SKP_int q[], /* O Quantized pulse signal */ - const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ - const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Prediction coefs */ - const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I LT prediction coefs */ - const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ - const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ - const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ - const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ - const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ - const SKP_int Lambda_Q10, /* I */ - const SKP_int LTP_scale_Q14 /* I LTP state scaling */ -); - -/************/ -/* Silk VAD */ -/************/ -/* Initialize the Silk VAD */ -SKP_int SKP_Silk_VAD_Init( /* O Return value, 0 if success */ - SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ -); - -/* Silk VAD noise level estimation */ -void SKP_Silk_VAD_GetNoiseLevels( - const SKP_int32 pX[ VAD_N_BANDS ], /* I subband energies */ - SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ -); - -/* Get speech activity level in Q8 */ -SKP_int SKP_Silk_VAD_GetSA_Q8( /* O Return value, 0 if success */ - SKP_Silk_VAD_state *psSilk_VAD, /* I/O Silk VAD state */ - SKP_int *pSA_Q8, /* O Speech activity level in Q8 */ - SKP_int *pSNR_dB_Q7, /* O SNR for current frame in Q7 */ - SKP_int pQuality_Q15[ VAD_N_BANDS ], /* O Smoothed SNR for each band */ - SKP_int *pTilt_Q15, /* O current frame's frequency tilt */ - const SKP_int16 pIn[], /* I PCM input [framelength] */ - const SKP_int framelength /* I Input frame length */ -); - -/* Detect signal in 8 - 12 khz range */ -void SKP_Silk_detect_SWB_input( - SKP_Silk_detect_SWB_state *psSWBdetect, /* I/O Encoder state */ - const SKP_int16 samplesIn[], /* I Input to encoder */ - SKP_int nSamplesIn /* I Length of input */ -); - -#if SWITCH_TRANSITION_FILTERING -/* Low-pass filter with variable cutoff frequency based on */ -/* piece-wise linear interpolation between elliptic filters */ -/* Start by setting transition_frame_no = 1; */ -void SKP_Silk_LP_variable_cutoff( - SKP_Silk_LP_state *psLP, /* I/O LP filter state */ - SKP_int16 *out, /* O Low-pass filtered output signal */ - const SKP_int16 *in, /* I Input signal */ - const SKP_int frame_length /* I Frame length */ -); -#endif - -/****************************************************/ -/* Decoder Functions */ -/****************************************************/ -SKP_int SKP_Silk_create_decoder( - SKP_Silk_decoder_state **ppsDec /* I/O Decoder state pointer pointer */ -); - -SKP_int SKP_Silk_free_decoder( - SKP_Silk_decoder_state *psDec /* I/O Decoder state pointer */ -); - -SKP_int SKP_Silk_init_decoder( - SKP_Silk_decoder_state *psDec /* I/O Decoder state pointer */ -); - -/* Set decoder sampling rate */ -void SKP_Silk_decoder_set_fs( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state pointer */ - SKP_int fs_kHz /* I Sampling frequency (kHz) */ -); - -/****************/ -/* Decode frame */ -/****************/ -SKP_int SKP_Silk_decode_frame( - SKP_Silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */ - SKP_int16 pOut[], /* O Pointer to output speech frame */ - SKP_int16 *pN, /* O Pointer to size of output frame */ - const SKP_uint8 pCode[], /* I Pointer to payload */ - const SKP_int nBytes, /* I Payload length */ - SKP_int action, /* I Action from Jitter Buffer */ - SKP_int *decBytes /* O Used bytes to decode this frame */ -); - -/* Decode parameters from payload */ -void SKP_Silk_decode_parameters( - SKP_Silk_decoder_state *psDec, /* I/O State */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int q[], /* O Excitation signal */ - const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ -); - -/* Decode indices from payload v4 Bitstream */ -void SKP_Silk_decode_indices_v4( - SKP_Silk_decoder_state *psDec /* I/O State */ -); - -/* Decode parameters from payload v4 Bitstream */ -void SKP_Silk_decode_parameters_v4( - SKP_Silk_decoder_state *psDec, /* I/O State */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int q[ MAX_FRAME_LENGTH ], /* O Excitation signal */ - const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ -); - -/* Core decoder. Performs inverse NSQ operation LTP + LPC */ -void SKP_Silk_decode_core( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I Decoder control */ - SKP_int16 xq[], /* O Decoded speech */ - const SKP_int q[ MAX_FRAME_LENGTH ] /* I Pulse signal */ -); - -/* NLSF vector decoder */ -void SKP_Silk_NLSF_MSVQ_decode( - SKP_int *pNLSF_Q15, /* O Pointer to decoded output [LPC_ORDER x 1] */ - const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Pointer to NLSF codebook struct */ - const SKP_int *NLSFIndices, /* I Pointer to NLSF indices [nStages x 1] */ - const SKP_int LPC_order /* I LPC order */ -); - -/**********************/ -/* Arithmetic coding */ -/*********************/ - -/* Decode quantization indices of excitation (Shell coding) */ -void SKP_Silk_decode_pulses( - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int q[], /* O Excitation signal */ - const SKP_int frame_length /* I Frame length (preliminary) */ -); - -/******************/ -/* CNG */ -/******************/ - -/* Reset CNG */ -void SKP_Silk_CNG_Reset( - SKP_Silk_decoder_state *psDec /* I/O Decoder state */ -); - -/* Updates CNG estimate, and applies the CNG when packet was lost */ -void SKP_Silk_CNG( - SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ - SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ - SKP_int16 signal[], /* I/O Signal */ - SKP_int length /* I Length of residual */ -); - -/* Encoding of various parameters */ -void SKP_Silk_encode_parameters( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ - SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ - SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ - const SKP_int *q /* I Quantization indices */ -); - -/* Encoding of various parameters */ -void SKP_Silk_encode_parameters_v4( - SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ - SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ - SKP_Silk_range_coder_state *psRC /* I/O Range encoder state */ -); - -/* Extract lowest layer encoding */ -void SKP_Silk_get_low_layer_internal( - const SKP_uint8 *indata, /* I: Encoded input vector */ - const SKP_int16 nBytesIn, /* I: Number of input Bytes */ - SKP_uint8 *Layer0data, /* O: Layer0 payload */ - SKP_int16 *nLayer0Bytes /* O: Number of FEC Bytes */ -); - -/* Resets LBRR buffer, used if packet size changes */ -void SKP_Silk_LBRR_reset( - SKP_Silk_encoder_state *psEncC /* I/O Pointer to Silk encoder state */ -); - -/* Predict number of bytes used to encode q */ -SKP_int SKP_Silk_pulses_to_bytes( /* O Return value, predicted number of bytes used to encode q */ - SKP_Silk_encoder_state *psEncC, /* I/O Encoder State*/ - SKP_int q[] /* I Pulse signal */ -); - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_MAIN_H +#define SKP_SILK_MAIN_H + +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_define.h" +#include "SKP_Silk_structs.h" +#include "SKP_Silk_tables.h" +#include "SKP_Silk_PLC.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* Encodes signs of excitation */ +void SKP_Silk_encode_signs( + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + const SKP_int q[], /* I pulse signal */ + const SKP_int length, /* I length of input */ + const SKP_int sigtype, /* I Signal type */ + const SKP_int QuantOffsetType, /* I Quantization offset type */ + const SKP_int RateLevelIndex /* I Rate Level Index */ +); + +/* Decodes signs of excitation */ +void SKP_Silk_decode_signs( + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + SKP_int q[], /* I/O pulse signal */ + const SKP_int length, /* I length of output */ + const SKP_int sigtype, /* I Signal type */ + const SKP_int QuantOffsetType, /* I Quantization offset type */ + const SKP_int RateLevelIndex /* I Rate Level Index */ +); + +/***************/ +/* Shell coder */ +/***************/ + +/* Encode quantization indices of excitation */ +void SKP_Silk_encode_pulses( + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + const SKP_int sigtype, /* I Sigtype */ + const SKP_int QuantOffsetType, /* I QuantOffsetType */ + const SKP_int q[], /* I quantization indices */ + const SKP_int frame_length /* I Frame length */ +); + +/* Shell encoder, operates on one shell code frame of 16 pulses */ +void SKP_Silk_shell_encoder( + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_int *pulses0 /* I data: nonnegative pulse amplitudes */ +); + +/* Shell decoder, operates on one shell code frame of 16 pulses */ +void SKP_Silk_shell_decoder( + SKP_int *pulses0, /* O data: nonnegative pulse amplitudes */ + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_int pulses4 /* I number of pulses per pulse-subframe */ +); + +/***************/ +/* Range coder */ +/***************/ +/* Range encoder for one symbol */ +void SKP_Silk_range_encoder( + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_int data, /* I uncompressed data */ + const SKP_uint16 prob[] /* I cumulative density functions */ +); + +/* Range encoder for multiple symbols */ +void SKP_Silk_range_encoder_multi( + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_int data[], /* I uncompressed data [nSymbols] */ + const SKP_uint16 * const prob[], /* I cumulative density functions */ + const SKP_int nSymbols /* I number of data symbols */ +); + +/* Range decoder for one symbol */ +void SKP_Silk_range_decoder( + SKP_int data[], /* O uncompressed data */ + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_uint16 prob[], /* I cumulative density function */ + SKP_int probIx /* I initial (middle) entry of cdf */ +); + +/* Range decoder for multiple symbols */ +void SKP_Silk_range_decoder_multi( + SKP_int data[], /* O uncompressed data [nSymbols] */ + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_uint16 * const prob[], /* I cumulative density functions */ + const SKP_int probStartIx[], /* I initial (middle) entries of cdfs [nSymbols] */ + const SKP_int nSymbols /* I number of data symbols */ +); + +/* Initialize range coder structure for encoder */ +void SKP_Silk_range_enc_init( + SKP_Silk_range_coder_state *psRC /* O compressor data structure */ +); + +/* Initialize range coder structure for decoder */ +void SKP_Silk_range_dec_init( + SKP_Silk_range_coder_state *psRC, /* O compressor data structure */ + const SKP_uint8 buffer[], /* I buffer for compressed data [bufferLength] */ + const SKP_int32 bufferLength /* I buffer length (in bytes) */ +); + +/* Determine length of bitstream */ +SKP_int SKP_Silk_range_coder_get_length( /* O returns number of BITS in stream */ + const SKP_Silk_range_coder_state *psRC, /* I compressed data structure */ + SKP_int *nBytes /* O number of BYTES in stream */ +); + +/* Write decodable stream to buffer, and determine its length */ +void SKP_Silk_range_enc_wrap_up( + SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ +); + +/* Check that any remaining bits in the last byte are set to 1 */ +void SKP_Silk_range_coder_check_after_decoding( + SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ +); + +/* Gain scalar quantization with hysteresis, uniform on log scale */ +void SKP_Silk_gains_quant( + SKP_int ind[ NB_SUBFR ], /* O gain indices */ + SKP_int32 gain_Q16[ NB_SUBFR ], /* I/O gains (quantized out) */ + SKP_int *prev_ind, /* I/O last index in previous frame */ + const SKP_int conditional /* I first gain is delta coded if 1 */ +); + +/* Gains scalar dequantization, uniform on log scale */ +void SKP_Silk_gains_dequant( + SKP_int32 gain_Q16[ NB_SUBFR ], /* O quantized gains */ + const SKP_int ind[ NB_SUBFR ], /* I gain indices */ + SKP_int *prev_ind, /* I/O last index in previous frame */ + const SKP_int conditional /* I first gain is delta coded if 1 */ +); + +/* Convert NLSF parameters to stable AR prediction filter coefficients */ +void SKP_Silk_NLSF2A_stable( + SKP_int16 pAR_Q12[ MAX_LPC_ORDER ], /* O Stabilized AR coefs [LPC_order] */ + const SKP_int pNLSF[ MAX_LPC_ORDER ], /* I NLSF vector [LPC_order] */ + const SKP_int LPC_order /* I LPC/LSF order */ +); + +/* Interpolate two vectors */ +void SKP_Silk_interpolate( + SKP_int xi[ MAX_LPC_ORDER ], /* O interpolated vector */ + const SKP_int x0[ MAX_LPC_ORDER ], /* I first vector */ + const SKP_int x1[ MAX_LPC_ORDER ], /* I second vector */ + const SKP_int ifact_Q2, /* I interp. factor, weight on 2nd vector */ + const SKP_int d /* I number of parameters */ +); + +/***********************************/ +/* Noise shaping quantization (NSQ)*/ +/***********************************/ +void SKP_Silk_NSQ( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ + SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + const SKP_int16 x[], /* I prefiltered input signal */ + SKP_int q[], /* O quantized qulse signal */ + const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ + const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Short term prediction coefficients */ + const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I Long term prediction coefficients */ + const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ + const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ + const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ + const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int Lambda_Q10, /* I */ + const SKP_int LTP_scale_Q14 /* I LTP state scaling */ +); + +/* Noise shaping using delayed decision */ +void SKP_Silk_NSQ_del_dec( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ + SKP_Silk_encoder_control *psEncCtrlC, /* I Encoder Control */ + SKP_Silk_nsq_state *NSQ, /* I/O NSQ state */ + const SKP_int16 x[], /* I Prefiltered input signal */ + SKP_int q[], /* O Quantized pulse signal */ + const SKP_int LSFInterpFactor_Q2, /* I LSF interpolation factor in Q2 */ + const SKP_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Prediction coefs */ + const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ], /* I LT prediction coefs */ + const SKP_int16 AR2_Q13[ NB_SUBFR * SHAPE_LPC_ORDER_MAX ], /* I */ + const SKP_int HarmShapeGain_Q14[ NB_SUBFR ], /* I */ + const SKP_int Tilt_Q14[ NB_SUBFR ], /* I Spectral tilt */ + const SKP_int32 LF_shp_Q14[ NB_SUBFR ], /* I */ + const SKP_int32 Gains_Q16[ NB_SUBFR ], /* I */ + const SKP_int Lambda_Q10, /* I */ + const SKP_int LTP_scale_Q14 /* I LTP state scaling */ +); + +/************/ +/* Silk VAD */ +/************/ +/* Initialize the Silk VAD */ +SKP_int SKP_Silk_VAD_Init( /* O Return value, 0 if success */ + SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ +); + +/* Silk VAD noise level estimation */ +void SKP_Silk_VAD_GetNoiseLevels( + const SKP_int32 pX[ VAD_N_BANDS ], /* I subband energies */ + SKP_Silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ +); + +/* Get speech activity level in Q8 */ +SKP_int SKP_Silk_VAD_GetSA_Q8( /* O Return value, 0 if success */ + SKP_Silk_VAD_state *psSilk_VAD, /* I/O Silk VAD state */ + SKP_int *pSA_Q8, /* O Speech activity level in Q8 */ + SKP_int *pSNR_dB_Q7, /* O SNR for current frame in Q7 */ + SKP_int pQuality_Q15[ VAD_N_BANDS ], /* O Smoothed SNR for each band */ + SKP_int *pTilt_Q15, /* O current frame's frequency tilt */ + const SKP_int16 pIn[], /* I PCM input [framelength] */ + const SKP_int framelength /* I Input frame length */ +); + +/* Detect signal in 8 - 12 khz range */ +void SKP_Silk_detect_SWB_input( + SKP_Silk_detect_SWB_state *psSWBdetect, /* I/O Encoder state */ + const SKP_int16 samplesIn[], /* I Input to encoder */ + SKP_int nSamplesIn /* I Length of input */ +); + +#if SWITCH_TRANSITION_FILTERING +/* Low-pass filter with variable cutoff frequency based on */ +/* piece-wise linear interpolation between elliptic filters */ +/* Start by setting transition_frame_no = 1; */ +void SKP_Silk_LP_variable_cutoff( + SKP_Silk_LP_state *psLP, /* I/O LP filter state */ + SKP_int16 *out, /* O Low-pass filtered output signal */ + const SKP_int16 *in, /* I Input signal */ + const SKP_int frame_length /* I Frame length */ +); +#endif + +/****************************************************/ +/* Decoder Functions */ +/****************************************************/ +SKP_int SKP_Silk_create_decoder( + SKP_Silk_decoder_state **ppsDec /* I/O Decoder state pointer pointer */ +); + +SKP_int SKP_Silk_free_decoder( + SKP_Silk_decoder_state *psDec /* I/O Decoder state pointer */ +); + +SKP_int SKP_Silk_init_decoder( + SKP_Silk_decoder_state *psDec /* I/O Decoder state pointer */ +); + +/* Set decoder sampling rate */ +void SKP_Silk_decoder_set_fs( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state pointer */ + SKP_int fs_kHz /* I Sampling frequency (kHz) */ +); + +/****************/ +/* Decode frame */ +/****************/ +SKP_int SKP_Silk_decode_frame( + SKP_Silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */ + SKP_int16 pOut[], /* O Pointer to output speech frame */ + SKP_int16 *pN, /* O Pointer to size of output frame */ + const SKP_uint8 pCode[], /* I Pointer to payload */ + const SKP_int nBytes, /* I Payload length */ + SKP_int action, /* I Action from Jitter Buffer */ + SKP_int *decBytes /* O Used bytes to decode this frame */ +); + +/* Decode parameters from payload */ +void SKP_Silk_decode_parameters( + SKP_Silk_decoder_state *psDec, /* I/O State */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int q[], /* O Excitation signal */ + const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ +); + +/* Decode indices from payload v4 Bitstream */ +void SKP_Silk_decode_indices_v4( + SKP_Silk_decoder_state *psDec /* I/O State */ +); + +/* Decode parameters from payload v4 Bitstream */ +void SKP_Silk_decode_parameters_v4( + SKP_Silk_decoder_state *psDec, /* I/O State */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int q[ MAX_FRAME_LENGTH ], /* O Excitation signal */ + const SKP_int fullDecoding /* I Flag to tell if only arithmetic decoding */ +); + +/* Core decoder. Performs inverse NSQ operation LTP + LPC */ +void SKP_Silk_decode_core( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I Decoder control */ + SKP_int16 xq[], /* O Decoded speech */ + const SKP_int q[ MAX_FRAME_LENGTH ] /* I Pulse signal */ +); + +/* NLSF vector decoder */ +void SKP_Silk_NLSF_MSVQ_decode( + SKP_int *pNLSF_Q15, /* O Pointer to decoded output [LPC_ORDER x 1] */ + const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Pointer to NLSF codebook struct */ + const SKP_int *NLSFIndices, /* I Pointer to NLSF indices [nStages x 1] */ + const SKP_int LPC_order /* I LPC order */ +); + +/**********************/ +/* Arithmetic coding */ +/*********************/ + +/* Decode quantization indices of excitation (Shell coding) */ +void SKP_Silk_decode_pulses( + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int q[], /* O Excitation signal */ + const SKP_int frame_length /* I Frame length (preliminary) */ +); + +/******************/ +/* CNG */ +/******************/ + +/* Reset CNG */ +void SKP_Silk_CNG_Reset( + SKP_Silk_decoder_state *psDec /* I/O Decoder state */ +); + +/* Updates CNG estimate, and applies the CNG when packet was lost */ +void SKP_Silk_CNG( + SKP_Silk_decoder_state *psDec, /* I/O Decoder state */ + SKP_Silk_decoder_control *psDecCtrl, /* I/O Decoder control */ + SKP_int16 signal[], /* I/O Signal */ + SKP_int length /* I Length of residual */ +); + +/* Encoding of various parameters */ +void SKP_Silk_encode_parameters( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ + SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ + SKP_Silk_range_coder_state *psRC, /* I/O Range coder state */ + const SKP_int *q /* I Quantization indices */ +); + +/* Encoding of various parameters */ +void SKP_Silk_encode_parameters_v4( + SKP_Silk_encoder_state *psEncC, /* I/O Encoder state */ + SKP_Silk_encoder_control *psEncCtrlC, /* I/O Encoder control */ + SKP_Silk_range_coder_state *psRC /* I/O Range encoder state */ +); + +/* Extract lowest layer encoding */ +void SKP_Silk_get_low_layer_internal( + const SKP_uint8 *indata, /* I: Encoded input vector */ + const SKP_int16 nBytesIn, /* I: Number of input Bytes */ + SKP_uint8 *Layer0data, /* O: Layer0 payload */ + SKP_int16 *nLayer0Bytes /* O: Number of FEC Bytes */ +); + +/* Resets LBRR buffer, used if packet size changes */ +void SKP_Silk_LBRR_reset( + SKP_Silk_encoder_state *psEncC /* I/O Pointer to Silk encoder state */ +); + +/* Predict number of bytes used to encode q */ +SKP_int SKP_Silk_pulses_to_bytes( /* O Return value, predicted number of bytes used to encode q */ + SKP_Silk_encoder_state *psEncC, /* I/O Encoder State*/ + SKP_int q[] /* I Pulse signal */ +); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_main_FIX.h b/app/src/main/jni/silk/src/SKP_Silk_main_FIX.h similarity index 98% rename from jni/silk/src/SKP_Silk_main_FIX.h rename to app/src/main/jni/silk/src/SKP_Silk_main_FIX.h index ff28f9a..3514855 100644 --- a/jni/silk/src/SKP_Silk_main_FIX.h +++ b/app/src/main/jni/silk/src/SKP_Silk_main_FIX.h @@ -1,324 +1,324 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_MAIN_FIX_H -#define SKP_SILK_MAIN_FIX_H - -#include -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_structs_FIX.h" -#include "SKP_Silk_main.h" -#include "SKP_Silk_define_FIX.h" -#include "SKP_Silk_PLC.h" -#define TIC(TAG_NAME) -#define TOC(TAG_NAME) - -#ifndef FORCE_CPP_BUILD -#ifdef __cplusplus -extern "C" -{ -#endif -#endif - -/*********************/ -/* Encoder Functions */ -/*********************/ - -/* Initializes the Silk encoder state */ -SKP_int SKP_Silk_init_encoder_FIX( - SKP_Silk_encoder_state_FIX *psEnc /* I/O Pointer to Silk FIX encoder state */ -); - -/* Control the Silk encoder */ -SKP_int SKP_Silk_control_encoder_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ - const SKP_int API_fs_kHz, /* I External (API) sampling rate (kHz) */ - const SKP_int PacketSize_ms, /* I Packet length (ms) */ - SKP_int32 TargetRate_bps, /* I Target max bitrate (bps) (used if SNR_dB == 0) */ - const SKP_int PacketLoss_perc, /* I Packet loss rate (in percent) */ - const SKP_int INBandFec_enabled, /* I Enable (1) / disable (0) inband FEC */ - const SKP_int DTX_enabled, /* I Enable / disable DTX */ - const SKP_int InputFramesize_ms, /* I Inputframe in ms */ - const SKP_int Complexity /* I Complexity (0->low; 1->medium; 2->high) */ -); - -/* Encoder main function */ -SKP_int SKP_Silk_encode_frame_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ - SKP_uint8 *pCode, /* O Pointer to payload */ - SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes; */ - /* input: max length; output: used */ - const SKP_int16 *pIn /* I Pointer to input speech frame */ -); - -/* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode with lower bitrate */ -void SKP_Silk_LBRR_encode_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Pointer to Silk FIX encoder control struct */ - SKP_uint8 *pCode, /* O Pointer to payload */ - SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes */ - SKP_int16 xfw[] /* I Input signal */ -); - -/* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */ -void SKP_Silk_HP_variable_cutoff_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control */ - SKP_int16 *out, /* O high-pass filtered output signal */ - const SKP_int16 *in /* I input signal */ -); - -/****************/ -/* Prefiltering */ -/****************/ -void SKP_Silk_prefilter_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ - const SKP_Silk_encoder_control_FIX *psEncCtrl, /* I Encoder control */ - SKP_int16 xw[], /* O Weighted signal */ - const SKP_int16 x[] /* I Speech signal */ -); - -/**************************************************************/ -/* Compute noise shaping coefficients and initial gain values */ -/**************************************************************/ -void SKP_Silk_noise_shape_analysis_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control */ - const SKP_int16 *pitch_res, /* I LPC residual from pitch analysis */ - const SKP_int16 *x /* I Input signal [ 2 * frame_length + la_shape ]*/ -); - -/* Processing of gains */ -void SKP_Silk_process_gains_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O Encoder control */ -); - - -/* Control low bitrate redundancy usage */ -void SKP_Silk_LBRR_ctrl_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control */ -); - -/* Calculation of LTP state scaling */ -void SKP_Silk_LTP_scale_ctrl_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control */ -); - -/**********************************************/ -/* Prediction Analysis */ -/**********************************************/ - -/* Find pitch lags */ -void SKP_Silk_find_pitch_lags_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ - SKP_int16 res[], /* O residual */ - const SKP_int16 x[] /* I Speech signal */ -); - -void SKP_Silk_find_pred_coefs_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ - const SKP_int16 res_pitch[] /* I Residual from pitch analysis */ -); - -void SKP_Silk_find_LPC_FIX( - SKP_int NLSF_Q15[], /* O LSFs */ - SKP_int *interpIndex, /* O LSF interpolation index, only used for LSF interpolation */ - const SKP_int prev_NLSFq_Q15[], /* I previous LSFs, only used for LSF interpolation */ - const SKP_int useInterpolatedLSFs, /* I Flag */ - const SKP_int LPC_order, /* I LPC order */ - const SKP_int16 x[], /* I Input signal */ - const SKP_int subfr_length /* I Input signal subframe length including preceeding samples */ -); - -void SKP_Silk_LTP_analysis_filter_FIX( - SKP_int16 *LTP_res, /* O: LTP residual signal of length NB_SUBFR * ( pre_length + subfr_length ) */ - const SKP_int16 *x, /* I: Pointer to input signal with at least max( pitchL ) preceeding samples */ - const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ],/* I: LTP_ORDER LTP coefficients for each NB_SUBFR subframe */ - const SKP_int pitchL[ NB_SUBFR ], /* I: Pitch lag, one for each subframe */ - const SKP_int32 invGains_Qxx[ NB_SUBFR ], /* I: Inverse quantization gains, one for each subframe */ - const SKP_int Qxx, /* I: Inverse quantization gains Q domain */ - const SKP_int subfr_length, /* I: Length of each subframe */ - const SKP_int pre_length /* I: Length of the preceeding samples starting at &x[0] for each subframe */ -); - -/* Finds LTP vector from correlations */ -void SKP_Silk_find_LTP_FIX( - SKP_int16 b_Q14[ NB_SUBFR * LTP_ORDER ], /* O LTP coefs */ - SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ - SKP_int *LTPredCodGain_Q7, /* O LTP coding gain */ - const SKP_int16 r_first[], /* I residual signal after LPC signal + state for first 10 ms */ - const SKP_int16 r_last[], /* I residual signal after LPC signal + state for last 10 ms */ - const SKP_int lag[ NB_SUBFR ], /* I LTP lags */ - const SKP_int32 Wght_Q15[ NB_SUBFR ], /* I weights */ - const SKP_int subfr_length, /* I subframe length */ - const SKP_int mem_offset, /* I number of samples in LTP memory */ - SKP_int corr_rshifts[ NB_SUBFR ] /* O right shifts applied to correlations */ -); - -/* LTP tap quantizer */ -void SKP_Silk_quant_LTP_gains_FIX( - SKP_int16 B_Q14[], /* I/O (un)quantized LTP gains */ - SKP_int cbk_index[], /* O Codebook Index */ - SKP_int *periodicity_index, /* O Periodicity Index */ - const SKP_int32 W_Q18[], /* I Error Weights in Q18 */ - SKP_int mu_Q8, /* I Mu value (R/D tradeoff) */ - SKP_int lowComplexity /* I Flag for low complexity */ -); - -/******************/ -/* NLSF Quantizer */ -/******************/ - -/* Limit, stabilize, convert and quantize NLSFs. */ -void SKP_Silk_process_NLSFs_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ - SKP_int *pNLSF_Q15 /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ -); - -/* LSF vector encoder */ -void SKP_Silk_NLSF_MSVQ_encode_FIX( - SKP_int *NLSFIndices, /* O Codebook path vector [ CB_STAGES ] */ - SKP_int *pNLSF_Q15, /* I/O Quantized NLSF vector [ LPC_ORDER ] */ - const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Codebook object */ - const SKP_int *pNLSF_q_Q15_prev, /* I Prev. quantized NLSF vector [LPC_ORDER] */ - const SKP_int *pW_Q6, /* I NLSF weight vector [ LPC_ORDER ] */ - const SKP_int NLSF_mu_Q15, /* I Rate weight for the RD optimization */ - const SKP_int NLSF_mu_fluc_red_Q16, /* I Fluctuation reduction error weight */ - const SKP_int NLSF_MSVQ_Survivors, /* I Max survivors from each stage */ - const SKP_int LPC_order, /* I LPC order */ - const SKP_int deactivate_fluc_red /* I Deactivate fluctuation reduction */ -); - -/* Rate-Distortion calculations for multiple input data vectors */ -void SKP_Silk_NLSF_VQ_rate_distortion_FIX( - SKP_int32 *pRD_Q20, /* O Rate-distortion values [psNLSF_CBS->nVectors*N] */ - const SKP_Silk_NLSF_CBS *psNLSF_CBS, /* I NLSF codebook stage struct */ - const SKP_int *in_Q15, /* I Input vectors to be quantized */ - const SKP_int *w_Q6, /* I Weight vector */ - const SKP_int32 *rate_acc_Q5, /* I Accumulated rates from previous stage */ - const SKP_int mu_Q15, /* I Weight between weighted error and rate */ - const SKP_int N, /* I Number of input vectors to be quantized */ - const SKP_int LPC_order /* I LPC order */ -); - -/* Compute weighted quantization errors for an LPC_order element input vector, over one codebook stage */ -void SKP_Silk_NLSF_VQ_sum_error_FIX( - SKP_int32 *err_Q20, /* O Weighted quantization errors [N*K] */ - const SKP_int *in_Q15, /* I Input vectors to be quantized [N*LPC_order] */ - const SKP_int *w_Q6, /* I Weighting vectors [N*LPC_order] */ - const SKP_int16 *pCB_Q15, /* I Codebook vectors [K*LPC_order] */ - const SKP_int N, /* I Number of input vectors */ - const SKP_int K, /* I Number of codebook vectors */ - const SKP_int LPC_order /* I Number of LPCs */ -); - -/* Entropy constrained MATRIX-weighted VQ, for a single input data vector */ -void SKP_Silk_VQ_WMat_EC_FIX( - SKP_int *ind, /* O index of best codebook vector */ - SKP_int32 *rate_dist_Q14, /* O best weighted quantization error + mu * rate*/ - const SKP_int16 *in_Q14, /* I input vector to be quantized */ - const SKP_int32 *W_Q18, /* I weighting matrix */ - const SKP_int16 *cb_Q14, /* I codebook */ - const SKP_int16 *cl_Q6, /* I code length for each codebook vector */ - const SKP_int mu_Q8, /* I tradeoff between weighted error and rate */ - SKP_int L /* I number of vectors in codebook */ -); - -/******************/ -/* Linear Algebra */ -/******************/ - -/* Calculates correlation matrix X'*X */ -void SKP_Silk_corrMatrix_FIX( - const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ - const SKP_int L, /* I Length of vectors */ - const SKP_int order, /* I Max lag for correlation */ - SKP_int32 *XX, /* O Pointer to X'*X correlation matrix [ order x order ]*/ - SKP_int *rshifts /* I/O Right shifts of correlations */ -); - -/* Calculates correlation vector X'*t */ -void SKP_Silk_corrVector_FIX( - const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ - const SKP_int16 *t, /* I target vector [L] */ - const SKP_int L, /* I Length of vectors */ - const SKP_int order, /* I Max lag for correlation */ - SKP_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */ - const SKP_int rshifts /* I Right shifts of correlations */ -); - -/* Add noise to matrix diagonal */ -void SKP_Silk_regularize_correlations_FIX( - SKP_int32 *XX, /* I/O Correlation matrices */ - SKP_int32 *xx, /* I/O Correlation values */ - SKP_int32 noise, /* I Noise to add */ - SKP_int D /* I Dimension of XX */ -); - -/* Solves Ax = b, assuming A is symmetric */ -void SKP_Silk_solve_LDL_FIX( - SKP_int32 *A, /* I Pointer to symetric square matrix A */ - SKP_int M, /* I Size of matrix */ - const SKP_int32 *b, /* I Pointer to b vector */ - SKP_int32 *x_Q16 /* O Pointer to x solution vector */ -); - -/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ -SKP_int32 SKP_Silk_residual_energy16_covar_FIX( - const SKP_int16 *c, /* I Prediction vector */ - const SKP_int32 *wXX, /* I Correlation matrix */ - const SKP_int32 *wXx, /* I Correlation vector */ - SKP_int32 wxx, /* I Signal energy */ - SKP_int D, /* I Dimension */ - SKP_int cQ /* I Q value for c vector 0 - 15 */ -); - -/* Calculates residual energies of input subframes where all subframes have LPC_order */ -/* of preceeding samples */ -void SKP_Silk_residual_energy_FIX( - SKP_int32 nrgs[ NB_SUBFR ], /* O Residual energy per subframe */ - SKP_int nrgsQ[ NB_SUBFR ], /* O Q value per subframe */ - const SKP_int16 x[], /* I Input signal */ - const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],/* I AR coefs for each frame half */ - const SKP_int32 gains_Qx[ NB_SUBFR ], /* I Quantization gains in Qx */ - const SKP_int Qx, /* I Quantization gains Q value */ - const SKP_int subfr_length, /* I Subframe length */ - const SKP_int LPC_order /* I LPC order */ -); - -#ifndef FORCE_CPP_BUILD -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* FORCE_CPP_BUILD */ -#endif /* SKP_SILK_MAIN_FIX_H */ +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_MAIN_FIX_H +#define SKP_SILK_MAIN_FIX_H + +#include +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_structs_FIX.h" +#include "SKP_Silk_main.h" +#include "SKP_Silk_define_FIX.h" +#include "SKP_Silk_PLC.h" +#define TIC(TAG_NAME) +#define TOC(TAG_NAME) + +#ifndef FORCE_CPP_BUILD +#ifdef __cplusplus +extern "C" +{ +#endif +#endif + +/*********************/ +/* Encoder Functions */ +/*********************/ + +/* Initializes the Silk encoder state */ +SKP_int SKP_Silk_init_encoder_FIX( + SKP_Silk_encoder_state_FIX *psEnc /* I/O Pointer to Silk FIX encoder state */ +); + +/* Control the Silk encoder */ +SKP_int SKP_Silk_control_encoder_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ + const SKP_int API_fs_kHz, /* I External (API) sampling rate (kHz) */ + const SKP_int PacketSize_ms, /* I Packet length (ms) */ + SKP_int32 TargetRate_bps, /* I Target max bitrate (bps) (used if SNR_dB == 0) */ + const SKP_int PacketLoss_perc, /* I Packet loss rate (in percent) */ + const SKP_int INBandFec_enabled, /* I Enable (1) / disable (0) inband FEC */ + const SKP_int DTX_enabled, /* I Enable / disable DTX */ + const SKP_int InputFramesize_ms, /* I Inputframe in ms */ + const SKP_int Complexity /* I Complexity (0->low; 1->medium; 2->high) */ +); + +/* Encoder main function */ +SKP_int SKP_Silk_encode_frame_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ + SKP_uint8 *pCode, /* O Pointer to payload */ + SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes; */ + /* input: max length; output: used */ + const SKP_int16 *pIn /* I Pointer to input speech frame */ +); + +/* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode with lower bitrate */ +void SKP_Silk_LBRR_encode_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Pointer to Silk FIX encoder control struct */ + SKP_uint8 *pCode, /* O Pointer to payload */ + SKP_int16 *pnBytesOut, /* I/O Pointer to number of payload bytes */ + SKP_int16 xfw[] /* I Input signal */ +); + +/* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */ +void SKP_Silk_HP_variable_cutoff_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control */ + SKP_int16 *out, /* O high-pass filtered output signal */ + const SKP_int16 *in /* I input signal */ +); + +/****************/ +/* Prefiltering */ +/****************/ +void SKP_Silk_prefilter_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ + const SKP_Silk_encoder_control_FIX *psEncCtrl, /* I Encoder control */ + SKP_int16 xw[], /* O Weighted signal */ + const SKP_int16 x[] /* I Speech signal */ +); + +/**************************************************************/ +/* Compute noise shaping coefficients and initial gain values */ +/**************************************************************/ +void SKP_Silk_noise_shape_analysis_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control */ + const SKP_int16 *pitch_res, /* I LPC residual from pitch analysis */ + const SKP_int16 *x /* I Input signal [ 2 * frame_length + la_shape ]*/ +); + +/* Processing of gains */ +void SKP_Silk_process_gains_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O Encoder control */ +); + + +/* Control low bitrate redundancy usage */ +void SKP_Silk_LBRR_ctrl_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control */ +); + +/* Calculation of LTP state scaling */ +void SKP_Silk_LTP_scale_ctrl_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O encoder control */ +); + +/**********************************************/ +/* Prediction Analysis */ +/**********************************************/ + +/* Find pitch lags */ +void SKP_Silk_find_pitch_lags_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ + SKP_int16 res[], /* O residual */ + const SKP_int16 x[] /* I Speech signal */ +); + +void SKP_Silk_find_pred_coefs_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ + const SKP_int16 res_pitch[] /* I Residual from pitch analysis */ +); + +void SKP_Silk_find_LPC_FIX( + SKP_int NLSF_Q15[], /* O LSFs */ + SKP_int *interpIndex, /* O LSF interpolation index, only used for LSF interpolation */ + const SKP_int prev_NLSFq_Q15[], /* I previous LSFs, only used for LSF interpolation */ + const SKP_int useInterpolatedLSFs, /* I Flag */ + const SKP_int LPC_order, /* I LPC order */ + const SKP_int16 x[], /* I Input signal */ + const SKP_int subfr_length /* I Input signal subframe length including preceeding samples */ +); + +void SKP_Silk_LTP_analysis_filter_FIX( + SKP_int16 *LTP_res, /* O: LTP residual signal of length NB_SUBFR * ( pre_length + subfr_length ) */ + const SKP_int16 *x, /* I: Pointer to input signal with at least max( pitchL ) preceeding samples */ + const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ],/* I: LTP_ORDER LTP coefficients for each NB_SUBFR subframe */ + const SKP_int pitchL[ NB_SUBFR ], /* I: Pitch lag, one for each subframe */ + const SKP_int32 invGains_Qxx[ NB_SUBFR ], /* I: Inverse quantization gains, one for each subframe */ + const SKP_int Qxx, /* I: Inverse quantization gains Q domain */ + const SKP_int subfr_length, /* I: Length of each subframe */ + const SKP_int pre_length /* I: Length of the preceeding samples starting at &x[0] for each subframe */ +); + +/* Finds LTP vector from correlations */ +void SKP_Silk_find_LTP_FIX( + SKP_int16 b_Q14[ NB_SUBFR * LTP_ORDER ], /* O LTP coefs */ + SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ + SKP_int *LTPredCodGain_Q7, /* O LTP coding gain */ + const SKP_int16 r_first[], /* I residual signal after LPC signal + state for first 10 ms */ + const SKP_int16 r_last[], /* I residual signal after LPC signal + state for last 10 ms */ + const SKP_int lag[ NB_SUBFR ], /* I LTP lags */ + const SKP_int32 Wght_Q15[ NB_SUBFR ], /* I weights */ + const SKP_int subfr_length, /* I subframe length */ + const SKP_int mem_offset, /* I number of samples in LTP memory */ + SKP_int corr_rshifts[ NB_SUBFR ] /* O right shifts applied to correlations */ +); + +/* LTP tap quantizer */ +void SKP_Silk_quant_LTP_gains_FIX( + SKP_int16 B_Q14[], /* I/O (un)quantized LTP gains */ + SKP_int cbk_index[], /* O Codebook Index */ + SKP_int *periodicity_index, /* O Periodicity Index */ + const SKP_int32 W_Q18[], /* I Error Weights in Q18 */ + SKP_int mu_Q8, /* I Mu value (R/D tradeoff) */ + SKP_int lowComplexity /* I Flag for low complexity */ +); + +/******************/ +/* NLSF Quantizer */ +/******************/ + +/* Limit, stabilize, convert and quantize NLSFs. */ +void SKP_Silk_process_NLSFs_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O encoder state */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ + SKP_int *pNLSF_Q15 /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ +); + +/* LSF vector encoder */ +void SKP_Silk_NLSF_MSVQ_encode_FIX( + SKP_int *NLSFIndices, /* O Codebook path vector [ CB_STAGES ] */ + SKP_int *pNLSF_Q15, /* I/O Quantized NLSF vector [ LPC_ORDER ] */ + const SKP_Silk_NLSF_CB_struct *psNLSF_CB, /* I Codebook object */ + const SKP_int *pNLSF_q_Q15_prev, /* I Prev. quantized NLSF vector [LPC_ORDER] */ + const SKP_int *pW_Q6, /* I NLSF weight vector [ LPC_ORDER ] */ + const SKP_int NLSF_mu_Q15, /* I Rate weight for the RD optimization */ + const SKP_int NLSF_mu_fluc_red_Q16, /* I Fluctuation reduction error weight */ + const SKP_int NLSF_MSVQ_Survivors, /* I Max survivors from each stage */ + const SKP_int LPC_order, /* I LPC order */ + const SKP_int deactivate_fluc_red /* I Deactivate fluctuation reduction */ +); + +/* Rate-Distortion calculations for multiple input data vectors */ +void SKP_Silk_NLSF_VQ_rate_distortion_FIX( + SKP_int32 *pRD_Q20, /* O Rate-distortion values [psNLSF_CBS->nVectors*N] */ + const SKP_Silk_NLSF_CBS *psNLSF_CBS, /* I NLSF codebook stage struct */ + const SKP_int *in_Q15, /* I Input vectors to be quantized */ + const SKP_int *w_Q6, /* I Weight vector */ + const SKP_int32 *rate_acc_Q5, /* I Accumulated rates from previous stage */ + const SKP_int mu_Q15, /* I Weight between weighted error and rate */ + const SKP_int N, /* I Number of input vectors to be quantized */ + const SKP_int LPC_order /* I LPC order */ +); + +/* Compute weighted quantization errors for an LPC_order element input vector, over one codebook stage */ +void SKP_Silk_NLSF_VQ_sum_error_FIX( + SKP_int32 *err_Q20, /* O Weighted quantization errors [N*K] */ + const SKP_int *in_Q15, /* I Input vectors to be quantized [N*LPC_order] */ + const SKP_int *w_Q6, /* I Weighting vectors [N*LPC_order] */ + const SKP_int16 *pCB_Q15, /* I Codebook vectors [K*LPC_order] */ + const SKP_int N, /* I Number of input vectors */ + const SKP_int K, /* I Number of codebook vectors */ + const SKP_int LPC_order /* I Number of LPCs */ +); + +/* Entropy constrained MATRIX-weighted VQ, for a single input data vector */ +void SKP_Silk_VQ_WMat_EC_FIX( + SKP_int *ind, /* O index of best codebook vector */ + SKP_int32 *rate_dist_Q14, /* O best weighted quantization error + mu * rate*/ + const SKP_int16 *in_Q14, /* I input vector to be quantized */ + const SKP_int32 *W_Q18, /* I weighting matrix */ + const SKP_int16 *cb_Q14, /* I codebook */ + const SKP_int16 *cl_Q6, /* I code length for each codebook vector */ + const SKP_int mu_Q8, /* I tradeoff between weighted error and rate */ + SKP_int L /* I number of vectors in codebook */ +); + +/******************/ +/* Linear Algebra */ +/******************/ + +/* Calculates correlation matrix X'*X */ +void SKP_Silk_corrMatrix_FIX( + const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ + const SKP_int L, /* I Length of vectors */ + const SKP_int order, /* I Max lag for correlation */ + SKP_int32 *XX, /* O Pointer to X'*X correlation matrix [ order x order ]*/ + SKP_int *rshifts /* I/O Right shifts of correlations */ +); + +/* Calculates correlation vector X'*t */ +void SKP_Silk_corrVector_FIX( + const SKP_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ + const SKP_int16 *t, /* I target vector [L] */ + const SKP_int L, /* I Length of vectors */ + const SKP_int order, /* I Max lag for correlation */ + SKP_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */ + const SKP_int rshifts /* I Right shifts of correlations */ +); + +/* Add noise to matrix diagonal */ +void SKP_Silk_regularize_correlations_FIX( + SKP_int32 *XX, /* I/O Correlation matrices */ + SKP_int32 *xx, /* I/O Correlation values */ + SKP_int32 noise, /* I Noise to add */ + SKP_int D /* I Dimension of XX */ +); + +/* Solves Ax = b, assuming A is symmetric */ +void SKP_Silk_solve_LDL_FIX( + SKP_int32 *A, /* I Pointer to symetric square matrix A */ + SKP_int M, /* I Size of matrix */ + const SKP_int32 *b, /* I Pointer to b vector */ + SKP_int32 *x_Q16 /* O Pointer to x solution vector */ +); + +/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ +SKP_int32 SKP_Silk_residual_energy16_covar_FIX( + const SKP_int16 *c, /* I Prediction vector */ + const SKP_int32 *wXX, /* I Correlation matrix */ + const SKP_int32 *wXx, /* I Correlation vector */ + SKP_int32 wxx, /* I Signal energy */ + SKP_int D, /* I Dimension */ + SKP_int cQ /* I Q value for c vector 0 - 15 */ +); + +/* Calculates residual energies of input subframes where all subframes have LPC_order */ +/* of preceeding samples */ +void SKP_Silk_residual_energy_FIX( + SKP_int32 nrgs[ NB_SUBFR ], /* O Residual energy per subframe */ + SKP_int nrgsQ[ NB_SUBFR ], /* O Q value per subframe */ + const SKP_int16 x[], /* I Input signal */ + const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],/* I AR coefs for each frame half */ + const SKP_int32 gains_Qx[ NB_SUBFR ], /* I Quantization gains in Qx */ + const SKP_int Qx, /* I Quantization gains Q value */ + const SKP_int subfr_length, /* I Subframe length */ + const SKP_int LPC_order /* I LPC order */ +); + +#ifndef FORCE_CPP_BUILD +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* FORCE_CPP_BUILD */ +#endif /* SKP_SILK_MAIN_FIX_H */ diff --git a/jni/silk/src/SKP_Silk_noise_shape_analysis_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_noise_shape_analysis_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_noise_shape_analysis_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_noise_shape_analysis_FIX.c index b0f8849..d487a35 100644 --- a/jni/silk/src/SKP_Silk_noise_shape_analysis_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_noise_shape_analysis_FIX.c @@ -1,338 +1,338 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" -#include "SKP_Silk_perceptual_parameters_FIX.h" - - -/**************************************************************/ -/* Compute noise shaping coefficients and initial gain values */ -/**************************************************************/ -void SKP_Silk_noise_shape_analysis_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */ - const SKP_int16 *pitch_res, /* I LPC residual from pitch analysis */ - const SKP_int16 *x /* I Input signal [ 2 * frame_length + la_shape ]*/ -) -{ - SKP_Silk_shape_state_FIX *psShapeSt = &psEnc->sShape; - SKP_int k, nSamples, lz, Qnrg, b_Q14, scale = 0, sz; - SKP_int32 SNR_adj_dB_Q7, HarmBoost_Q16, HarmShapeGain_Q16, Tilt_Q16, tmp32; - SKP_int32 nrg, pre_nrg_Q30, log_energy_Q7, log_energy_prev_Q7, energy_variation_Q7; - SKP_int32 delta_Q16, BWExp1_Q16, BWExp2_Q16, gain_mult_Q16, gain_add_Q16, strength_Q16, b_Q8; - SKP_int32 auto_corr[ SHAPE_LPC_ORDER_MAX + 1 ]; - SKP_int32 refl_coef_Q16[ SHAPE_LPC_ORDER_MAX ]; - SKP_int32 AR_Q24[ SHAPE_LPC_ORDER_MAX ]; - SKP_int16 x_windowed[ SHAPE_LPC_WIN_MAX ]; - const SKP_int16 *x_ptr, *pitch_res_ptr; - - SKP_int32 sqrt_nrg[ NB_SUBFR ], Qnrg_vec[ NB_SUBFR ]; - - /* Point to start of first LPC analysis block */ - x_ptr = x + psEnc->sCmn.la_shape - SKP_SMULBB( SHAPE_LPC_WIN_MS, psEnc->sCmn.fs_kHz ) + psEnc->sCmn.frame_length / NB_SUBFR; - - /****************/ - /* CONTROL SNR */ - /****************/ - /* Reduce SNR_dB values if recent bitstream has exceeded TargetRate */ - psEncCtrl->current_SNR_dB_Q7 = psEnc->SNR_dB_Q7 - SKP_SMULWB( SKP_LSHIFT( ( SKP_int32 )psEnc->BufferedInChannel_ms, 7 ), 3277 ); - - /* Reduce SNR_dB if inband FEC used */ - if( psEnc->speech_activity_Q8 > LBRR_SPEECH_ACTIVITY_THRES_Q8 ) { - psEncCtrl->current_SNR_dB_Q7 -= SKP_RSHIFT( psEnc->inBandFEC_SNR_comp_Q8, 1 ); - } - - /****************/ - /* GAIN CONTROL */ - /****************/ - /* Input quality is the average of the quality in the lowest two VAD bands */ - psEncCtrl->input_quality_Q14 = ( SKP_int )SKP_RSHIFT( ( SKP_int32 )psEncCtrl->input_quality_bands_Q15[ 0 ] - + psEncCtrl->input_quality_bands_Q15[ 1 ], 2 ); - /* Coding quality level, between 0.0_Q0 and 1.0_Q0, but in Q14 */ - psEncCtrl->coding_quality_Q14 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->current_SNR_dB_Q7 - ( 18 << 7 ), 4 ) ), 1 ); - - /* Reduce coding SNR during low speech activity */ - b_Q8 = ( 1 << 8 ) - psEnc->speech_activity_Q8; - b_Q8 = SKP_SMULWB( SKP_LSHIFT( b_Q8, 8 ), b_Q8 ); - SNR_adj_dB_Q7 = SKP_SMLAWB( psEncCtrl->current_SNR_dB_Q7, - SKP_SMULBB( -BG_SNR_DECR_dB_Q7 >> ( 4 + 1 ), b_Q8 ), // Q11 - SKP_SMULWB( ( 1 << 14 ) + psEncCtrl->input_quality_Q14, psEncCtrl->coding_quality_Q14 ) ); // Q12 - - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /* Reduce gains for periodic signals */ - SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, HARM_SNR_INCR_dB_Q7 << 1, psEnc->LTPCorr_Q15 ); - } else { - /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */ - SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, - SKP_SMLAWB( 6 << ( 7 + 2 ), -104856, psEncCtrl->current_SNR_dB_Q7 ), //-104856_Q18 = -0.4_Q0, Q9 - ( 1 << 14 ) - psEncCtrl->input_quality_Q14 ); // Q14 - } - - /*************************/ - /* SPARSENESS PROCESSING */ - /*************************/ - /* Set quantizer offset */ - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /* Initally set to 0; may be overruled in process_gains(..) */ - psEncCtrl->sCmn.QuantOffsetType = 0; - psEncCtrl->sparseness_Q8 = 0; - } else { - /* Sparseness measure, based on relative fluctuations of energy per 2 milliseconds */ - nSamples = SKP_LSHIFT( psEnc->sCmn.fs_kHz, 1 ); - energy_variation_Q7 = 0; - log_energy_prev_Q7 = 0; - pitch_res_ptr = pitch_res; - for( k = 0; k < FRAME_LENGTH_MS / 2; k++ ) { - SKP_Silk_sum_sqr_shift( &nrg, &scale, pitch_res_ptr, nSamples ); - nrg += SKP_RSHIFT( nSamples, scale ); // Q(-scale) - - log_energy_Q7 = SKP_Silk_lin2log( nrg ); - if( k > 0 ) { - energy_variation_Q7 += SKP_abs( log_energy_Q7 - log_energy_prev_Q7 ); - } - log_energy_prev_Q7 = log_energy_Q7; - pitch_res_ptr += nSamples; - } - - psEncCtrl->sparseness_Q8 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_SMULWB( energy_variation_Q7 - ( 5 << 7 ), 6554 ) ), 7 ); // 6554_Q16 = 0.1_Q0 - - /* Set quantization offset depending on sparseness measure */ - if( psEncCtrl->sparseness_Q8 > SPARSENESS_THRESHOLD_QNT_OFFSET_Q8 ) { - psEncCtrl->sCmn.QuantOffsetType = 0; - } else { - psEncCtrl->sCmn.QuantOffsetType = 1; - } - - /* Increase coding SNR for sparse signals */ - SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, SPARSE_SNR_INCR_dB_Q7 << 8, psEncCtrl->sparseness_Q8 - ( 1 << 7 ) ); - } - - /*******************************/ - /* Control bandwidth expansion */ - /*******************************/ - delta_Q16 = SKP_SMULWB( ( 1 << 16 ) - SKP_SMULBB( 3, psEncCtrl->coding_quality_Q14 ), LOW_RATE_BANDWIDTH_EXPANSION_DELTA_Q16 ); - BWExp1_Q16 = BANDWIDTH_EXPANSION_Q16 - delta_Q16; - BWExp2_Q16 = BANDWIDTH_EXPANSION_Q16 + delta_Q16; - if( psEnc->sCmn.fs_kHz == 24 ) { - /* Less bandwidth expansion for super wideband */ - BWExp1_Q16 = ( 1 << 16 ) - SKP_SMULWB( SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16, ( 1 << 16 ) - BWExp1_Q16 ); - BWExp2_Q16 = ( 1 << 16 ) - SKP_SMULWB( SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16, ( 1 << 16 ) - BWExp2_Q16 ); - } - /* BWExp1 will be applied after BWExp2, so make it relative */ - BWExp1_Q16 = SKP_DIV32_16( SKP_LSHIFT( BWExp1_Q16, 14 ), SKP_RSHIFT( BWExp2_Q16, 2 ) ); - - /********************************************/ - /* Compute noise shaping AR coefs and gains */ - /********************************************/ - sz = ( SKP_int )SKP_SMULBB( SHAPE_LPC_WIN_MS, psEnc->sCmn.fs_kHz ); - for( k = 0; k < NB_SUBFR; k++ ) { - /* Apply window */ - SKP_Silk_apply_sine_window( x_windowed, x_ptr, 0, SHAPE_LPC_WIN_MS * psEnc->sCmn.fs_kHz ); - - /* Update pointer: next LPC analysis block */ - x_ptr += psEnc->sCmn.frame_length / NB_SUBFR; - - /* Calculate auto correlation */ - SKP_Silk_autocorr( auto_corr, &scale, x_windowed, sz, psEnc->sCmn.shapingLPCOrder + 1 ); - - /* Add white noise, as a fraction of energy */ - auto_corr[0] = SKP_ADD32( auto_corr[0], SKP_max_32( SKP_SMULWB( SKP_RSHIFT( auto_corr[ 0 ], 4 ), SHAPE_WHITE_NOISE_FRACTION_Q20 ), 1 ) ); - - /* Calculate the reflection coefficients using schur */ - nrg = SKP_Silk_schur64( refl_coef_Q16, auto_corr, psEnc->sCmn.shapingLPCOrder ); - - /* Convert reflection coefficients to prediction coefficients */ - SKP_Silk_k2a_Q16( AR_Q24, refl_coef_Q16, psEnc->sCmn.shapingLPCOrder ); - - /* Bandwidth expansion for synthesis filter shaping */ - SKP_Silk_bwexpander_32( AR_Q24, psEnc->sCmn.shapingLPCOrder, BWExp2_Q16 ); - - /* Make sure to fit in Q13 SKP_int16 */ - SKP_Silk_LPC_fit( &psEncCtrl->AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ], AR_Q24, 13, psEnc->sCmn.shapingLPCOrder ); - - /* Compute noise shaping filter coefficients */ - SKP_memcpy( - &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ], - &psEncCtrl->AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ], - psEnc->sCmn.shapingLPCOrder * sizeof( SKP_int16 ) ); - - /* Bandwidth expansion for analysis filter shaping */ - SKP_assert( BWExp1_Q16 <= ( 1 << 16 ) ); // If ever breaking, use LPC_stabilize() in these cases to stay within range - SKP_Silk_bwexpander( &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ], psEnc->sCmn.shapingLPCOrder, BWExp1_Q16 ); - - /* Increase residual energy */ - nrg = SKP_SMLAWB( nrg, SKP_RSHIFT( auto_corr[ 0 ], 8 ), SHAPE_MIN_ENERGY_RATIO_Q24 ); - - Qnrg = -scale; // range: -12...30 - SKP_assert( Qnrg >= -12 ); - SKP_assert( Qnrg <= 30 ); - - /* Make sure that Qnrg is an even number */ - if( Qnrg & 1 ) { - Qnrg -= 1; - nrg >>= 1; - } - - tmp32 = SKP_Silk_SQRT_APPROX( nrg ); - Qnrg >>= 1; // range: -6...15 - - sqrt_nrg[ k ] = tmp32; - Qnrg_vec[ k ] = Qnrg; - - psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( tmp32, 16 - Qnrg ); - /* Ratio of prediction gains, in energy domain */ - SKP_Silk_LPC_inverse_pred_gain_Q13( &pre_nrg_Q30, &psEncCtrl->AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ], psEnc->sCmn.shapingLPCOrder ); - SKP_Silk_LPC_inverse_pred_gain_Q13( &nrg, &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ], psEnc->sCmn.shapingLPCOrder ); - - lz = SKP_min_32( SKP_Silk_CLZ32( pre_nrg_Q30 ) - 1, 19 ); - pre_nrg_Q30 = SKP_DIV32( SKP_LSHIFT( pre_nrg_Q30, lz ), SKP_RSHIFT( nrg, 20 - lz ) + 1 ); // Q20 - pre_nrg_Q30 = SKP_RSHIFT( SKP_LSHIFT_SAT32( pre_nrg_Q30, 9 ), 1 ); /* Q28 */ - psEncCtrl->GainsPre_Q14[ k ] = ( SKP_int )SKP_Silk_SQRT_APPROX( pre_nrg_Q30 ); - } - - /*****************/ - /* Gain tweaking */ - /*****************/ - /* Increase gains during low speech activity and put lower limit on gains */ - gain_mult_Q16 = SKP_Silk_log2lin( -SKP_SMLAWB( -16 << 7, SNR_adj_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0 - gain_add_Q16 = SKP_Silk_log2lin( SKP_SMLAWB( 16 << 7, NOISE_FLOOR_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0 - tmp32 = SKP_Silk_log2lin( SKP_SMLAWB( 16 << 7, RELATIVE_MIN_GAIN_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0 - tmp32 = SKP_SMULWW( psEnc->avgGain_Q16, tmp32 ); - gain_add_Q16 = SKP_ADD_SAT32( gain_add_Q16, tmp32 ); - SKP_assert( gain_mult_Q16 >= 0 ); - - for( k = 0; k < NB_SUBFR; k++ ) { - psEncCtrl->Gains_Q16[ k ] = SKP_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 ); - SKP_assert( psEncCtrl->Gains_Q16[ k ] >= 0 ); - } - - for( k = 0; k < NB_SUBFR; k++ ) { - psEncCtrl->Gains_Q16[ k ] = SKP_ADD_POS_SAT32( psEncCtrl->Gains_Q16[ k ], gain_add_Q16 ); - psEnc->avgGain_Q16 = SKP_ADD_SAT32( - psEnc->avgGain_Q16, - SKP_SMULWB( - psEncCtrl->Gains_Q16[ k ] - psEnc->avgGain_Q16, - SKP_RSHIFT_ROUND( SKP_SMULBB( psEnc->speech_activity_Q8, GAIN_SMOOTHING_COEF_Q10 ), 2 ) - ) ); - } - - /************************************************/ - /* Decrease level during fricatives (de-essing) */ - /************************************************/ - gain_mult_Q16 = ( 1 << 16 ) + SKP_RSHIFT_ROUND( SKP_MLA( INPUT_TILT_Q26, psEncCtrl->coding_quality_Q14, HIGH_RATE_INPUT_TILT_Q12 ), 10 ); - - if( psEncCtrl->input_tilt_Q15 <= 0 && psEncCtrl->sCmn.sigtype == SIG_TYPE_UNVOICED ) { - if( psEnc->sCmn.fs_kHz == 24 ) { - SKP_int32 essStrength_Q15 = SKP_SMULWW( -psEncCtrl->input_tilt_Q15, - SKP_SMULBB( psEnc->speech_activity_Q8, ( 1 << 8 ) - psEncCtrl->sparseness_Q8 ) ); - tmp32 = SKP_Silk_log2lin( ( 16 << 7 ) - SKP_SMULWB( essStrength_Q15, - SKP_SMULWB( DE_ESSER_COEF_SWB_dB_Q7, 20972 ) ) ); // 20972_Q17 = 0.16_Q0 - gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, tmp32 ); - } else if( psEnc->sCmn.fs_kHz == 16 ) { - SKP_int32 essStrength_Q15 = SKP_SMULWW(-psEncCtrl->input_tilt_Q15, - SKP_SMULBB( psEnc->speech_activity_Q8, ( 1 << 8 ) - psEncCtrl->sparseness_Q8 )); - tmp32 = SKP_Silk_log2lin( ( 16 << 7 ) - SKP_SMULWB( essStrength_Q15, - SKP_SMULWB( DE_ESSER_COEF_WB_dB_Q7, 20972 ) ) ); // 20972_Q17 = 0.16_Q0 - gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, tmp32 ); - } else { - SKP_assert( psEnc->sCmn.fs_kHz == 12 || psEnc->sCmn.fs_kHz == 8 ); - } - } - - for( k = 0; k < NB_SUBFR; k++ ) { - psEncCtrl->GainsPre_Q14[ k ] = SKP_SMULWB( gain_mult_Q16, psEncCtrl->GainsPre_Q14[ k ] ); - } - - /************************************************/ - /* Control low-frequency shaping and noise tilt */ - /************************************************/ - /* Less low frequency shaping for noisy inputs */ - strength_Q16 = SKP_MUL( LOW_FREQ_SHAPING_Q0, ( 1 << 16 ) + SKP_SMULBB( LOW_QUALITY_LOW_FREQ_SHAPING_DECR_Q1, psEncCtrl->input_quality_bands_Q15[ 0 ] - ( 1 << 15 ) ) ); - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */ - /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/ - SKP_int fs_kHz_inv = SKP_DIV32_16( 3277, psEnc->sCmn.fs_kHz ); // 0.2_Q0 = 3277_Q14 - for( k = 0; k < NB_SUBFR; k++ ) { - b_Q14 = fs_kHz_inv + SKP_DIV32_16( ( 3 << 14 ), psEncCtrl->sCmn.pitchL[ k ] ); - /* Pack two coefficients in one int32 */ - psEncCtrl->LF_shp_Q14[ k ] = SKP_LSHIFT( ( 1 << 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, b_Q14 ), 16 ); - psEncCtrl->LF_shp_Q14[ k ] |= (SKP_uint16)( b_Q14 - ( 1 << 14 ) ); - } - SKP_assert( HARM_HP_NOISE_COEF_Q24 < ( 1 << 23 ) ); // Guarantees that second argument to SMULWB() is within range of an SKP_int16 - Tilt_Q16 = - HP_NOISE_COEF_Q16 - - SKP_SMULWB( ( 1 << 16 ) - HP_NOISE_COEF_Q16, SKP_SMULWB( HARM_HP_NOISE_COEF_Q24, psEnc->speech_activity_Q8 ) ); - } else { - b_Q14 = SKP_DIV32_16( 21299, psEnc->sCmn.fs_kHz ); // 1.3_Q0 = 21299_Q14 - /* Pack two coefficients in one int32 */ - psEncCtrl->LF_shp_Q14[ 0 ] = SKP_LSHIFT( ( 1 << 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, SKP_SMULWB( 39322, b_Q14 ) ), 16 ); // 0.6_Q0 = 39322_Q16 - psEncCtrl->LF_shp_Q14[ 0 ] |= (SKP_uint16)( b_Q14 - ( 1 << 14 ) ); - for( k = 1; k < NB_SUBFR; k++ ) { - psEncCtrl->LF_shp_Q14[ k ] = psEncCtrl->LF_shp_Q14[ k - 1 ]; - } - Tilt_Q16 = -HP_NOISE_COEF_Q16; - } - - /****************************/ - /* HARMONIC SHAPING CONTROL */ - /****************************/ - /* Control boosting of harmonic frequencies */ - HarmBoost_Q16 = SKP_SMULWB( SKP_SMULWB( ( 1 << 17 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 3 ), - psEnc->LTPCorr_Q15 ), LOW_RATE_HARMONIC_BOOST_Q16 ); - - /* More harmonic boost for noisy input signals */ - HarmBoost_Q16 = SKP_SMLAWB( HarmBoost_Q16, - ( 1 << 16 ) - SKP_LSHIFT( psEncCtrl->input_quality_Q14, 2 ), LOW_INPUT_QUALITY_HARMONIC_BOOST_Q16 ); - - if( USE_HARM_SHAPING && psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /* More harmonic noise shaping for high bitrates or noisy input */ - HarmShapeGain_Q16 = SKP_SMLAWB( HARMONIC_SHAPING_Q16, - ( 1 << 16 ) - SKP_SMULWB( ( 1 << 18 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 4 ), - psEncCtrl->input_quality_Q14 ), HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING_Q16 ); - - /* Less harmonic noise shaping for less periodic signals */ - HarmShapeGain_Q16 = SKP_SMULWB( SKP_LSHIFT( HarmShapeGain_Q16, 1 ), - SKP_Silk_SQRT_APPROX( SKP_LSHIFT( psEnc->LTPCorr_Q15, 15 ) ) ); - } else { - HarmShapeGain_Q16 = 0; - } - - /*************************/ - /* Smooth over subframes */ - /*************************/ - for( k = 0; k < NB_SUBFR; k++ ) { - psShapeSt->HarmBoost_smth_Q16 = - SKP_SMLAWB( psShapeSt->HarmBoost_smth_Q16, HarmBoost_Q16 - psShapeSt->HarmBoost_smth_Q16, SUBFR_SMTH_COEF_Q16 ); - psShapeSt->HarmShapeGain_smth_Q16 = - SKP_SMLAWB( psShapeSt->HarmShapeGain_smth_Q16, HarmShapeGain_Q16 - psShapeSt->HarmShapeGain_smth_Q16, SUBFR_SMTH_COEF_Q16 ); - psShapeSt->Tilt_smth_Q16 = - SKP_SMLAWB( psShapeSt->Tilt_smth_Q16, Tilt_Q16 - psShapeSt->Tilt_smth_Q16, SUBFR_SMTH_COEF_Q16 ); - - psEncCtrl->HarmBoost_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmBoost_smth_Q16, 2 ); - psEncCtrl->HarmShapeGain_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmShapeGain_smth_Q16, 2 ); - psEncCtrl->Tilt_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->Tilt_smth_Q16, 2 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" +#include "SKP_Silk_perceptual_parameters_FIX.h" + + +/**************************************************************/ +/* Compute noise shaping coefficients and initial gain values */ +/**************************************************************/ +void SKP_Silk_noise_shape_analysis_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */ + const SKP_int16 *pitch_res, /* I LPC residual from pitch analysis */ + const SKP_int16 *x /* I Input signal [ 2 * frame_length + la_shape ]*/ +) +{ + SKP_Silk_shape_state_FIX *psShapeSt = &psEnc->sShape; + SKP_int k, nSamples, lz, Qnrg, b_Q14, scale = 0, sz; + SKP_int32 SNR_adj_dB_Q7, HarmBoost_Q16, HarmShapeGain_Q16, Tilt_Q16, tmp32; + SKP_int32 nrg, pre_nrg_Q30, log_energy_Q7, log_energy_prev_Q7, energy_variation_Q7; + SKP_int32 delta_Q16, BWExp1_Q16, BWExp2_Q16, gain_mult_Q16, gain_add_Q16, strength_Q16, b_Q8; + SKP_int32 auto_corr[ SHAPE_LPC_ORDER_MAX + 1 ]; + SKP_int32 refl_coef_Q16[ SHAPE_LPC_ORDER_MAX ]; + SKP_int32 AR_Q24[ SHAPE_LPC_ORDER_MAX ]; + SKP_int16 x_windowed[ SHAPE_LPC_WIN_MAX ]; + const SKP_int16 *x_ptr, *pitch_res_ptr; + + SKP_int32 sqrt_nrg[ NB_SUBFR ], Qnrg_vec[ NB_SUBFR ]; + + /* Point to start of first LPC analysis block */ + x_ptr = x + psEnc->sCmn.la_shape - SKP_SMULBB( SHAPE_LPC_WIN_MS, psEnc->sCmn.fs_kHz ) + psEnc->sCmn.frame_length / NB_SUBFR; + + /****************/ + /* CONTROL SNR */ + /****************/ + /* Reduce SNR_dB values if recent bitstream has exceeded TargetRate */ + psEncCtrl->current_SNR_dB_Q7 = psEnc->SNR_dB_Q7 - SKP_SMULWB( SKP_LSHIFT( ( SKP_int32 )psEnc->BufferedInChannel_ms, 7 ), 3277 ); + + /* Reduce SNR_dB if inband FEC used */ + if( psEnc->speech_activity_Q8 > LBRR_SPEECH_ACTIVITY_THRES_Q8 ) { + psEncCtrl->current_SNR_dB_Q7 -= SKP_RSHIFT( psEnc->inBandFEC_SNR_comp_Q8, 1 ); + } + + /****************/ + /* GAIN CONTROL */ + /****************/ + /* Input quality is the average of the quality in the lowest two VAD bands */ + psEncCtrl->input_quality_Q14 = ( SKP_int )SKP_RSHIFT( ( SKP_int32 )psEncCtrl->input_quality_bands_Q15[ 0 ] + + psEncCtrl->input_quality_bands_Q15[ 1 ], 2 ); + /* Coding quality level, between 0.0_Q0 and 1.0_Q0, but in Q14 */ + psEncCtrl->coding_quality_Q14 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->current_SNR_dB_Q7 - ( 18 << 7 ), 4 ) ), 1 ); + + /* Reduce coding SNR during low speech activity */ + b_Q8 = ( 1 << 8 ) - psEnc->speech_activity_Q8; + b_Q8 = SKP_SMULWB( SKP_LSHIFT( b_Q8, 8 ), b_Q8 ); + SNR_adj_dB_Q7 = SKP_SMLAWB( psEncCtrl->current_SNR_dB_Q7, + SKP_SMULBB( -BG_SNR_DECR_dB_Q7 >> ( 4 + 1 ), b_Q8 ), // Q11 + SKP_SMULWB( ( 1 << 14 ) + psEncCtrl->input_quality_Q14, psEncCtrl->coding_quality_Q14 ) ); // Q12 + + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /* Reduce gains for periodic signals */ + SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, HARM_SNR_INCR_dB_Q7 << 1, psEnc->LTPCorr_Q15 ); + } else { + /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */ + SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, + SKP_SMLAWB( 6 << ( 7 + 2 ), -104856, psEncCtrl->current_SNR_dB_Q7 ), //-104856_Q18 = -0.4_Q0, Q9 + ( 1 << 14 ) - psEncCtrl->input_quality_Q14 ); // Q14 + } + + /*************************/ + /* SPARSENESS PROCESSING */ + /*************************/ + /* Set quantizer offset */ + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /* Initally set to 0; may be overruled in process_gains(..) */ + psEncCtrl->sCmn.QuantOffsetType = 0; + psEncCtrl->sparseness_Q8 = 0; + } else { + /* Sparseness measure, based on relative fluctuations of energy per 2 milliseconds */ + nSamples = SKP_LSHIFT( psEnc->sCmn.fs_kHz, 1 ); + energy_variation_Q7 = 0; + log_energy_prev_Q7 = 0; + pitch_res_ptr = pitch_res; + for( k = 0; k < FRAME_LENGTH_MS / 2; k++ ) { + SKP_Silk_sum_sqr_shift( &nrg, &scale, pitch_res_ptr, nSamples ); + nrg += SKP_RSHIFT( nSamples, scale ); // Q(-scale) + + log_energy_Q7 = SKP_Silk_lin2log( nrg ); + if( k > 0 ) { + energy_variation_Q7 += SKP_abs( log_energy_Q7 - log_energy_prev_Q7 ); + } + log_energy_prev_Q7 = log_energy_Q7; + pitch_res_ptr += nSamples; + } + + psEncCtrl->sparseness_Q8 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_SMULWB( energy_variation_Q7 - ( 5 << 7 ), 6554 ) ), 7 ); // 6554_Q16 = 0.1_Q0 + + /* Set quantization offset depending on sparseness measure */ + if( psEncCtrl->sparseness_Q8 > SPARSENESS_THRESHOLD_QNT_OFFSET_Q8 ) { + psEncCtrl->sCmn.QuantOffsetType = 0; + } else { + psEncCtrl->sCmn.QuantOffsetType = 1; + } + + /* Increase coding SNR for sparse signals */ + SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, SPARSE_SNR_INCR_dB_Q7 << 8, psEncCtrl->sparseness_Q8 - ( 1 << 7 ) ); + } + + /*******************************/ + /* Control bandwidth expansion */ + /*******************************/ + delta_Q16 = SKP_SMULWB( ( 1 << 16 ) - SKP_SMULBB( 3, psEncCtrl->coding_quality_Q14 ), LOW_RATE_BANDWIDTH_EXPANSION_DELTA_Q16 ); + BWExp1_Q16 = BANDWIDTH_EXPANSION_Q16 - delta_Q16; + BWExp2_Q16 = BANDWIDTH_EXPANSION_Q16 + delta_Q16; + if( psEnc->sCmn.fs_kHz == 24 ) { + /* Less bandwidth expansion for super wideband */ + BWExp1_Q16 = ( 1 << 16 ) - SKP_SMULWB( SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16, ( 1 << 16 ) - BWExp1_Q16 ); + BWExp2_Q16 = ( 1 << 16 ) - SKP_SMULWB( SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16, ( 1 << 16 ) - BWExp2_Q16 ); + } + /* BWExp1 will be applied after BWExp2, so make it relative */ + BWExp1_Q16 = SKP_DIV32_16( SKP_LSHIFT( BWExp1_Q16, 14 ), SKP_RSHIFT( BWExp2_Q16, 2 ) ); + + /********************************************/ + /* Compute noise shaping AR coefs and gains */ + /********************************************/ + sz = ( SKP_int )SKP_SMULBB( SHAPE_LPC_WIN_MS, psEnc->sCmn.fs_kHz ); + for( k = 0; k < NB_SUBFR; k++ ) { + /* Apply window */ + SKP_Silk_apply_sine_window( x_windowed, x_ptr, 0, SHAPE_LPC_WIN_MS * psEnc->sCmn.fs_kHz ); + + /* Update pointer: next LPC analysis block */ + x_ptr += psEnc->sCmn.frame_length / NB_SUBFR; + + /* Calculate auto correlation */ + SKP_Silk_autocorr( auto_corr, &scale, x_windowed, sz, psEnc->sCmn.shapingLPCOrder + 1 ); + + /* Add white noise, as a fraction of energy */ + auto_corr[0] = SKP_ADD32( auto_corr[0], SKP_max_32( SKP_SMULWB( SKP_RSHIFT( auto_corr[ 0 ], 4 ), SHAPE_WHITE_NOISE_FRACTION_Q20 ), 1 ) ); + + /* Calculate the reflection coefficients using schur */ + nrg = SKP_Silk_schur64( refl_coef_Q16, auto_corr, psEnc->sCmn.shapingLPCOrder ); + + /* Convert reflection coefficients to prediction coefficients */ + SKP_Silk_k2a_Q16( AR_Q24, refl_coef_Q16, psEnc->sCmn.shapingLPCOrder ); + + /* Bandwidth expansion for synthesis filter shaping */ + SKP_Silk_bwexpander_32( AR_Q24, psEnc->sCmn.shapingLPCOrder, BWExp2_Q16 ); + + /* Make sure to fit in Q13 SKP_int16 */ + SKP_Silk_LPC_fit( &psEncCtrl->AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ], AR_Q24, 13, psEnc->sCmn.shapingLPCOrder ); + + /* Compute noise shaping filter coefficients */ + SKP_memcpy( + &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ], + &psEncCtrl->AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ], + psEnc->sCmn.shapingLPCOrder * sizeof( SKP_int16 ) ); + + /* Bandwidth expansion for analysis filter shaping */ + SKP_assert( BWExp1_Q16 <= ( 1 << 16 ) ); // If ever breaking, use LPC_stabilize() in these cases to stay within range + SKP_Silk_bwexpander( &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ], psEnc->sCmn.shapingLPCOrder, BWExp1_Q16 ); + + /* Increase residual energy */ + nrg = SKP_SMLAWB( nrg, SKP_RSHIFT( auto_corr[ 0 ], 8 ), SHAPE_MIN_ENERGY_RATIO_Q24 ); + + Qnrg = -scale; // range: -12...30 + SKP_assert( Qnrg >= -12 ); + SKP_assert( Qnrg <= 30 ); + + /* Make sure that Qnrg is an even number */ + if( Qnrg & 1 ) { + Qnrg -= 1; + nrg >>= 1; + } + + tmp32 = SKP_Silk_SQRT_APPROX( nrg ); + Qnrg >>= 1; // range: -6...15 + + sqrt_nrg[ k ] = tmp32; + Qnrg_vec[ k ] = Qnrg; + + psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( tmp32, 16 - Qnrg ); + /* Ratio of prediction gains, in energy domain */ + SKP_Silk_LPC_inverse_pred_gain_Q13( &pre_nrg_Q30, &psEncCtrl->AR2_Q13[ k * SHAPE_LPC_ORDER_MAX ], psEnc->sCmn.shapingLPCOrder ); + SKP_Silk_LPC_inverse_pred_gain_Q13( &nrg, &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ], psEnc->sCmn.shapingLPCOrder ); + + lz = SKP_min_32( SKP_Silk_CLZ32( pre_nrg_Q30 ) - 1, 19 ); + pre_nrg_Q30 = SKP_DIV32( SKP_LSHIFT( pre_nrg_Q30, lz ), SKP_RSHIFT( nrg, 20 - lz ) + 1 ); // Q20 + pre_nrg_Q30 = SKP_RSHIFT( SKP_LSHIFT_SAT32( pre_nrg_Q30, 9 ), 1 ); /* Q28 */ + psEncCtrl->GainsPre_Q14[ k ] = ( SKP_int )SKP_Silk_SQRT_APPROX( pre_nrg_Q30 ); + } + + /*****************/ + /* Gain tweaking */ + /*****************/ + /* Increase gains during low speech activity and put lower limit on gains */ + gain_mult_Q16 = SKP_Silk_log2lin( -SKP_SMLAWB( -16 << 7, SNR_adj_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0 + gain_add_Q16 = SKP_Silk_log2lin( SKP_SMLAWB( 16 << 7, NOISE_FLOOR_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0 + tmp32 = SKP_Silk_log2lin( SKP_SMLAWB( 16 << 7, RELATIVE_MIN_GAIN_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0 + tmp32 = SKP_SMULWW( psEnc->avgGain_Q16, tmp32 ); + gain_add_Q16 = SKP_ADD_SAT32( gain_add_Q16, tmp32 ); + SKP_assert( gain_mult_Q16 >= 0 ); + + for( k = 0; k < NB_SUBFR; k++ ) { + psEncCtrl->Gains_Q16[ k ] = SKP_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 ); + SKP_assert( psEncCtrl->Gains_Q16[ k ] >= 0 ); + } + + for( k = 0; k < NB_SUBFR; k++ ) { + psEncCtrl->Gains_Q16[ k ] = SKP_ADD_POS_SAT32( psEncCtrl->Gains_Q16[ k ], gain_add_Q16 ); + psEnc->avgGain_Q16 = SKP_ADD_SAT32( + psEnc->avgGain_Q16, + SKP_SMULWB( + psEncCtrl->Gains_Q16[ k ] - psEnc->avgGain_Q16, + SKP_RSHIFT_ROUND( SKP_SMULBB( psEnc->speech_activity_Q8, GAIN_SMOOTHING_COEF_Q10 ), 2 ) + ) ); + } + + /************************************************/ + /* Decrease level during fricatives (de-essing) */ + /************************************************/ + gain_mult_Q16 = ( 1 << 16 ) + SKP_RSHIFT_ROUND( SKP_MLA( INPUT_TILT_Q26, psEncCtrl->coding_quality_Q14, HIGH_RATE_INPUT_TILT_Q12 ), 10 ); + + if( psEncCtrl->input_tilt_Q15 <= 0 && psEncCtrl->sCmn.sigtype == SIG_TYPE_UNVOICED ) { + if( psEnc->sCmn.fs_kHz == 24 ) { + SKP_int32 essStrength_Q15 = SKP_SMULWW( -psEncCtrl->input_tilt_Q15, + SKP_SMULBB( psEnc->speech_activity_Q8, ( 1 << 8 ) - psEncCtrl->sparseness_Q8 ) ); + tmp32 = SKP_Silk_log2lin( ( 16 << 7 ) - SKP_SMULWB( essStrength_Q15, + SKP_SMULWB( DE_ESSER_COEF_SWB_dB_Q7, 20972 ) ) ); // 20972_Q17 = 0.16_Q0 + gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, tmp32 ); + } else if( psEnc->sCmn.fs_kHz == 16 ) { + SKP_int32 essStrength_Q15 = SKP_SMULWW(-psEncCtrl->input_tilt_Q15, + SKP_SMULBB( psEnc->speech_activity_Q8, ( 1 << 8 ) - psEncCtrl->sparseness_Q8 )); + tmp32 = SKP_Silk_log2lin( ( 16 << 7 ) - SKP_SMULWB( essStrength_Q15, + SKP_SMULWB( DE_ESSER_COEF_WB_dB_Q7, 20972 ) ) ); // 20972_Q17 = 0.16_Q0 + gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, tmp32 ); + } else { + SKP_assert( psEnc->sCmn.fs_kHz == 12 || psEnc->sCmn.fs_kHz == 8 ); + } + } + + for( k = 0; k < NB_SUBFR; k++ ) { + psEncCtrl->GainsPre_Q14[ k ] = SKP_SMULWB( gain_mult_Q16, psEncCtrl->GainsPre_Q14[ k ] ); + } + + /************************************************/ + /* Control low-frequency shaping and noise tilt */ + /************************************************/ + /* Less low frequency shaping for noisy inputs */ + strength_Q16 = SKP_MUL( LOW_FREQ_SHAPING_Q0, ( 1 << 16 ) + SKP_SMULBB( LOW_QUALITY_LOW_FREQ_SHAPING_DECR_Q1, psEncCtrl->input_quality_bands_Q15[ 0 ] - ( 1 << 15 ) ) ); + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */ + /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/ + SKP_int fs_kHz_inv = SKP_DIV32_16( 3277, psEnc->sCmn.fs_kHz ); // 0.2_Q0 = 3277_Q14 + for( k = 0; k < NB_SUBFR; k++ ) { + b_Q14 = fs_kHz_inv + SKP_DIV32_16( ( 3 << 14 ), psEncCtrl->sCmn.pitchL[ k ] ); + /* Pack two coefficients in one int32 */ + psEncCtrl->LF_shp_Q14[ k ] = SKP_LSHIFT( ( 1 << 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, b_Q14 ), 16 ); + psEncCtrl->LF_shp_Q14[ k ] |= (SKP_uint16)( b_Q14 - ( 1 << 14 ) ); + } + SKP_assert( HARM_HP_NOISE_COEF_Q24 < ( 1 << 23 ) ); // Guarantees that second argument to SMULWB() is within range of an SKP_int16 + Tilt_Q16 = - HP_NOISE_COEF_Q16 - + SKP_SMULWB( ( 1 << 16 ) - HP_NOISE_COEF_Q16, SKP_SMULWB( HARM_HP_NOISE_COEF_Q24, psEnc->speech_activity_Q8 ) ); + } else { + b_Q14 = SKP_DIV32_16( 21299, psEnc->sCmn.fs_kHz ); // 1.3_Q0 = 21299_Q14 + /* Pack two coefficients in one int32 */ + psEncCtrl->LF_shp_Q14[ 0 ] = SKP_LSHIFT( ( 1 << 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, SKP_SMULWB( 39322, b_Q14 ) ), 16 ); // 0.6_Q0 = 39322_Q16 + psEncCtrl->LF_shp_Q14[ 0 ] |= (SKP_uint16)( b_Q14 - ( 1 << 14 ) ); + for( k = 1; k < NB_SUBFR; k++ ) { + psEncCtrl->LF_shp_Q14[ k ] = psEncCtrl->LF_shp_Q14[ k - 1 ]; + } + Tilt_Q16 = -HP_NOISE_COEF_Q16; + } + + /****************************/ + /* HARMONIC SHAPING CONTROL */ + /****************************/ + /* Control boosting of harmonic frequencies */ + HarmBoost_Q16 = SKP_SMULWB( SKP_SMULWB( ( 1 << 17 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 3 ), + psEnc->LTPCorr_Q15 ), LOW_RATE_HARMONIC_BOOST_Q16 ); + + /* More harmonic boost for noisy input signals */ + HarmBoost_Q16 = SKP_SMLAWB( HarmBoost_Q16, + ( 1 << 16 ) - SKP_LSHIFT( psEncCtrl->input_quality_Q14, 2 ), LOW_INPUT_QUALITY_HARMONIC_BOOST_Q16 ); + + if( USE_HARM_SHAPING && psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /* More harmonic noise shaping for high bitrates or noisy input */ + HarmShapeGain_Q16 = SKP_SMLAWB( HARMONIC_SHAPING_Q16, + ( 1 << 16 ) - SKP_SMULWB( ( 1 << 18 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 4 ), + psEncCtrl->input_quality_Q14 ), HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING_Q16 ); + + /* Less harmonic noise shaping for less periodic signals */ + HarmShapeGain_Q16 = SKP_SMULWB( SKP_LSHIFT( HarmShapeGain_Q16, 1 ), + SKP_Silk_SQRT_APPROX( SKP_LSHIFT( psEnc->LTPCorr_Q15, 15 ) ) ); + } else { + HarmShapeGain_Q16 = 0; + } + + /*************************/ + /* Smooth over subframes */ + /*************************/ + for( k = 0; k < NB_SUBFR; k++ ) { + psShapeSt->HarmBoost_smth_Q16 = + SKP_SMLAWB( psShapeSt->HarmBoost_smth_Q16, HarmBoost_Q16 - psShapeSt->HarmBoost_smth_Q16, SUBFR_SMTH_COEF_Q16 ); + psShapeSt->HarmShapeGain_smth_Q16 = + SKP_SMLAWB( psShapeSt->HarmShapeGain_smth_Q16, HarmShapeGain_Q16 - psShapeSt->HarmShapeGain_smth_Q16, SUBFR_SMTH_COEF_Q16 ); + psShapeSt->Tilt_smth_Q16 = + SKP_SMLAWB( psShapeSt->Tilt_smth_Q16, Tilt_Q16 - psShapeSt->Tilt_smth_Q16, SUBFR_SMTH_COEF_Q16 ); + + psEncCtrl->HarmBoost_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmBoost_smth_Q16, 2 ); + psEncCtrl->HarmShapeGain_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmShapeGain_smth_Q16, 2 ); + psEncCtrl->Tilt_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->Tilt_smth_Q16, 2 ); + } +} diff --git a/jni/silk/src/SKP_Silk_perceptual_parameters_FIX.h b/app/src/main/jni/silk/src/SKP_Silk_perceptual_parameters_FIX.h similarity index 97% rename from jni/silk/src/SKP_Silk_perceptual_parameters_FIX.h rename to app/src/main/jni/silk/src/SKP_Silk_perceptual_parameters_FIX.h index dfa7c45..c627ba8 100644 --- a/jni/silk/src/SKP_Silk_perceptual_parameters_FIX.h +++ b/app/src/main/jni/silk/src/SKP_Silk_perceptual_parameters_FIX.h @@ -1,121 +1,121 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_PERCEPTUAL_PARAMETERS_FIX_H -#define SKP_SILK_PERCEPTUAL_PARAMETERS_FIX_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* reduction in coding SNR during low speech activity */ -#define BG_SNR_DECR_dB_Q7 (3<<7) - -/* factor for reducing quantization noise during voiced speech */ -#define HARM_SNR_INCR_dB_Q7 (2<<7) - -/* factor for reducing quantization noise for unvoiced sparse signals */ -#define SPARSE_SNR_INCR_dB_Q7 (2<<7) - -/* threshold for sparseness measure above which to use lower quantization offset during unvoiced */ -#define SPARSENESS_THRESHOLD_QNT_OFFSET_Q8 (3<<6) // 0.75 - - -/* noise shaping filter chirp factor */ -#define BANDWIDTH_EXPANSION_Q16 61604 // 0.94 - -/* difference between chirp factors for analysis and synthesis noise shaping filters at low bitrates */ -#define LOW_RATE_BANDWIDTH_EXPANSION_DELTA_Q16 655 //0.01f - -/* factor to reduce all bandwidth expansion coefficients for super wideband, relative to wideband */ -#define SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16 (1<<16) // 1.0f; - -/* gain reduction for fricatives */ -#define DE_ESSER_COEF_SWB_dB_Q7 (2 << 7) -#define DE_ESSER_COEF_WB_dB_Q7 (1 << 7) - - -/* extra harmonic boosting (signal shaping) at low bitrates */ -#define LOW_RATE_HARMONIC_BOOST_Q16 6554 // 0.1 - -/* extra harmonic boosting (signal shaping) for noisy input signals */ -#define LOW_INPUT_QUALITY_HARMONIC_BOOST_Q16 6554 // 0.1 - -/* harmonic noise shaping */ -#define HARMONIC_SHAPING_Q16 19661 // 0.3 - -/* extra harmonic noise shaping for high bitrates or noisy input */ -#define HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING_Q16 13107 // 0.2 - - -/* parameter for shaping noise towards higher frequencies */ -#define HP_NOISE_COEF_Q16 19661 // 0.3 - -/* parameter for shaping noise extra towards higher frequencies during voiced speech */ -#define HARM_HP_NOISE_COEF_Q24 7549747 // 0.45 - -/* parameter for applying a high-pass tilt to the input signal */ -#define INPUT_TILT_Q26 2684355 // 0.04 - -/* parameter for extra high-pass tilt to the input signal at high rates */ -#define HIGH_RATE_INPUT_TILT_Q12 246 // 0.06 - -/* parameter for reducing noise at the very low frequencies */ -#define LOW_FREQ_SHAPING_Q0 3 - -/* less reduction of noise at the very low frequencies for signals with low SNR at low frequencies */ -#define LOW_QUALITY_LOW_FREQ_SHAPING_DECR_Q1 1 // 0.5_Q0 - -/* fraction added to first autocorrelation value */ -#define SHAPE_WHITE_NOISE_FRACTION_Q20 50 // 50_Q20 = 4.7684e-5 - -/* fraction of first autocorrelation value added to residual energy value; limits prediction gain */ -#define SHAPE_MIN_ENERGY_RATIO_Q24 256 - -/* noise floor to put a low limit on the quantization step size */ -#define NOISE_FLOOR_dB_Q7 (4 << 7) - -/* noise floor relative to active speech gain level */ -#define RELATIVE_MIN_GAIN_dB_Q7 -6400 // -50_Q0 = -6400_Q7 - -/* subframe smoothing coefficient for determining active speech gain level (lower -> more smoothing) */ -#define GAIN_SMOOTHING_COEF_Q10 1 // 1e-3_Q0 = 1.024_Q10 - -/* subframe smoothing coefficient for HarmBoost, HarmShapeGain, Tilt (lower -> more smoothing) */ -#define SUBFR_SMTH_COEF_Q16 26214 // 0.4 - -#define NOISE_GAIN_VL_Q16 7864 -#define NOISE_GAIN_VH_Q16 7864 -#define NOISE_GAIN_UVL_Q16 6554 -#define NOISE_GAIN_UVH_Q16 9830 - -#ifdef __cplusplus -} -#endif - -#endif //SKP_SILK_PERCEPTUAL_PARAMETERS_FIX_H +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_PERCEPTUAL_PARAMETERS_FIX_H +#define SKP_SILK_PERCEPTUAL_PARAMETERS_FIX_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* reduction in coding SNR during low speech activity */ +#define BG_SNR_DECR_dB_Q7 (3<<7) + +/* factor for reducing quantization noise during voiced speech */ +#define HARM_SNR_INCR_dB_Q7 (2<<7) + +/* factor for reducing quantization noise for unvoiced sparse signals */ +#define SPARSE_SNR_INCR_dB_Q7 (2<<7) + +/* threshold for sparseness measure above which to use lower quantization offset during unvoiced */ +#define SPARSENESS_THRESHOLD_QNT_OFFSET_Q8 (3<<6) // 0.75 + + +/* noise shaping filter chirp factor */ +#define BANDWIDTH_EXPANSION_Q16 61604 // 0.94 + +/* difference between chirp factors for analysis and synthesis noise shaping filters at low bitrates */ +#define LOW_RATE_BANDWIDTH_EXPANSION_DELTA_Q16 655 //0.01f + +/* factor to reduce all bandwidth expansion coefficients for super wideband, relative to wideband */ +#define SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16 (1<<16) // 1.0f; + +/* gain reduction for fricatives */ +#define DE_ESSER_COEF_SWB_dB_Q7 (2 << 7) +#define DE_ESSER_COEF_WB_dB_Q7 (1 << 7) + + +/* extra harmonic boosting (signal shaping) at low bitrates */ +#define LOW_RATE_HARMONIC_BOOST_Q16 6554 // 0.1 + +/* extra harmonic boosting (signal shaping) for noisy input signals */ +#define LOW_INPUT_QUALITY_HARMONIC_BOOST_Q16 6554 // 0.1 + +/* harmonic noise shaping */ +#define HARMONIC_SHAPING_Q16 19661 // 0.3 + +/* extra harmonic noise shaping for high bitrates or noisy input */ +#define HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING_Q16 13107 // 0.2 + + +/* parameter for shaping noise towards higher frequencies */ +#define HP_NOISE_COEF_Q16 19661 // 0.3 + +/* parameter for shaping noise extra towards higher frequencies during voiced speech */ +#define HARM_HP_NOISE_COEF_Q24 7549747 // 0.45 + +/* parameter for applying a high-pass tilt to the input signal */ +#define INPUT_TILT_Q26 2684355 // 0.04 + +/* parameter for extra high-pass tilt to the input signal at high rates */ +#define HIGH_RATE_INPUT_TILT_Q12 246 // 0.06 + +/* parameter for reducing noise at the very low frequencies */ +#define LOW_FREQ_SHAPING_Q0 3 + +/* less reduction of noise at the very low frequencies for signals with low SNR at low frequencies */ +#define LOW_QUALITY_LOW_FREQ_SHAPING_DECR_Q1 1 // 0.5_Q0 + +/* fraction added to first autocorrelation value */ +#define SHAPE_WHITE_NOISE_FRACTION_Q20 50 // 50_Q20 = 4.7684e-5 + +/* fraction of first autocorrelation value added to residual energy value; limits prediction gain */ +#define SHAPE_MIN_ENERGY_RATIO_Q24 256 + +/* noise floor to put a low limit on the quantization step size */ +#define NOISE_FLOOR_dB_Q7 (4 << 7) + +/* noise floor relative to active speech gain level */ +#define RELATIVE_MIN_GAIN_dB_Q7 -6400 // -50_Q0 = -6400_Q7 + +/* subframe smoothing coefficient for determining active speech gain level (lower -> more smoothing) */ +#define GAIN_SMOOTHING_COEF_Q10 1 // 1e-3_Q0 = 1.024_Q10 + +/* subframe smoothing coefficient for HarmBoost, HarmShapeGain, Tilt (lower -> more smoothing) */ +#define SUBFR_SMTH_COEF_Q16 26214 // 0.4 + +#define NOISE_GAIN_VL_Q16 7864 +#define NOISE_GAIN_VH_Q16 7864 +#define NOISE_GAIN_UVL_Q16 6554 +#define NOISE_GAIN_UVH_Q16 9830 + +#ifdef __cplusplus +} +#endif + +#endif //SKP_SILK_PERCEPTUAL_PARAMETERS_FIX_H diff --git a/jni/silk/src/SKP_Silk_pitch_analysis_core.c b/app/src/main/jni/silk/src/SKP_Silk_pitch_analysis_core.c similarity index 97% rename from jni/silk/src/SKP_Silk_pitch_analysis_core.c rename to app/src/main/jni/silk/src/SKP_Silk_pitch_analysis_core.c index 6d68c1a..f66d34b 100644 --- a/jni/silk/src/SKP_Silk_pitch_analysis_core.c +++ b/app/src/main/jni/silk/src/SKP_Silk_pitch_analysis_core.c @@ -1,747 +1,747 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/*********************************************************** -* Pitch analyser function -********************************************************** */ -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_pitch_est_defines.h" -#include "SKP_Silk_resample_rom.h" - -#define SCRATCH_SIZE 22 - -/************************************************************/ -/* Internally used functions */ -/************************************************************/ -void SKP_FIX_P_Ana_calc_corr_st3( - SKP_int32 cross_corr_st3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX][PITCH_EST_NB_STAGE3_LAGS],/* (O) 3 DIM correlation array */ - const SKP_int16 signal[], /* I vector to correlate */ - SKP_int start_lag, /* I lag offset to search around */ - SKP_int sf_length, /* I length of a 5 ms subframe */ - SKP_int complexity /* I Complexity setting */ -); - -void SKP_FIX_P_Ana_calc_energy_st3( - SKP_int32 energies_st3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX][PITCH_EST_NB_STAGE3_LAGS],/* (O) 3 DIM energy array */ - const SKP_int16 signal[], /* I vector to calc energy in */ - SKP_int start_lag, /* I lag offset to search around */ - SKP_int sf_length, /* I length of one 5 ms subframe */ - SKP_int complexity /* I Complexity setting */ -); - -SKP_int32 SKP_FIX_P_Ana_find_scaling( - const SKP_int16 *signal, - const SKP_int signal_length, - const SKP_int sum_sqr_len -); - -void SKP_Silk_decode_pitch( - SKP_int lagIndex, /* I */ - SKP_int contourIndex, /* O */ - SKP_int pitch_lags[], /* O 4 pitch values */ - SKP_int Fs_kHz /* I sampling frequency (kHz) */ -) -{ - SKP_int lag, i, min_lag; - - min_lag = SKP_SMULBB( PITCH_EST_MIN_LAG_MS, Fs_kHz ); - - /* Only for 24 / 16 kHz version for now */ - lag = min_lag + lagIndex; - if( Fs_kHz == 8 ) { - /* Only a small codebook for 8 khz */ - for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) { - pitch_lags[ i ] = lag + SKP_Silk_CB_lags_stage2[ i ][ contourIndex ]; - } - } else { - for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) { - pitch_lags[ i ] = lag + SKP_Silk_CB_lags_stage3[ i ][ contourIndex ]; - } - } -} - -/*************************************************************/ -/* FIXED POINT CORE PITCH ANALYSIS FUNCTION */ -/*************************************************************/ -SKP_int SKP_Silk_pitch_analysis_core( /* O Voicing estimate: 0 voiced, 1 unvoiced */ - const SKP_int16 *signal, /* I Signal of length PITCH_EST_FRAME_LENGTH_MS*Fs_kHz */ - SKP_int *pitch_out, /* O 4 pitch lag values */ - SKP_int *lagIndex, /* O Lag Index */ - SKP_int *contourIndex, /* O Pitch contour Index */ - SKP_int *LTPCorr_Q15, /* I/O Normalized correlation; input: value from previous frame */ - SKP_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */ - const SKP_int32 search_thres1_Q16, /* I First stage threshold for lag candidates 0 - 1 */ - const SKP_int search_thres2_Q15, /* I Final threshold for lag candidates 0 - 1 */ - const SKP_int Fs_kHz, /* I Sample frequency (kHz) */ - const SKP_int complexity /* I Complexity setting, 0-2, where 2 is highest */ -) -{ - SKP_int16 signal_8kHz[ PITCH_EST_MAX_FRAME_LENGTH_ST_2 ]; - SKP_int16 signal_4kHz[ PITCH_EST_MAX_FRAME_LENGTH_ST_1 ]; - SKP_int32 scratch_mem[ 3 * PITCH_EST_MAX_FRAME_LENGTH ]; - SKP_int16 *input_signal_ptr; - SKP_int32 filt_state[ PITCH_EST_MAX_DECIMATE_STATE_LENGTH ]; - SKP_int i, k, d, j; - SKP_int16 C[ PITCH_EST_NB_SUBFR ][ ( PITCH_EST_MAX_LAG >> 1 ) + 5 ]; - const SKP_int16 *target_ptr, *basis_ptr; - SKP_int32 cross_corr, normalizer, energy, shift, energy_basis, energy_target; - SKP_int d_srch[ PITCH_EST_D_SRCH_LENGTH ]; - SKP_int16 d_comp[ ( PITCH_EST_MAX_LAG >> 1 ) + 5 ]; - SKP_int Cmax, length_d_srch, length_d_comp; - SKP_int32 sum, threshold, temp32; - SKP_int CBimax, CBimax_new, CBimax_old, lag, start_lag, end_lag, lag_new; - SKP_int32 CC[ PITCH_EST_NB_CBKS_STAGE2_EXT ], CCmax, CCmax_b, CCmax_new_b, CCmax_new; - SKP_int32 energies_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ]; - SKP_int32 crosscorr_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ]; - SKP_int32 lag_counter; - SKP_int frame_length, frame_length_8kHz, frame_length_4kHz, max_sum_sq_length; - SKP_int sf_length, sf_length_8kHz, sf_length_4kHz; - SKP_int min_lag, min_lag_8kHz, min_lag_4kHz; - SKP_int max_lag, max_lag_8kHz, max_lag_4kHz; - SKP_int32 contour_bias, diff; - SKP_int32 lz, lshift; - SKP_int cbk_offset, cbk_size, nb_cbks_stage2; - SKP_int32 delta_lag_log2_sqr_Q7, lag_log2_Q7, prevLag_log2_Q7, prev_lag_bias_Q15, corr_thres_Q15; - - /* Check for valid sampling frequency */ - SKP_assert( Fs_kHz == 8 || Fs_kHz == 12 || Fs_kHz == 16 || Fs_kHz == 24 ); - - /* Check for valid complexity setting */ - SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX ); - SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX ); - - SKP_assert( search_thres1_Q16 >= 0 && search_thres1_Q16 <= (1<<16) ); - SKP_assert( search_thres2_Q15 >= 0 && search_thres2_Q15 <= (1<<15) ); - - /* Setup frame lengths max / min lag for the sampling frequency */ - frame_length = PITCH_EST_FRAME_LENGTH_MS * Fs_kHz; - frame_length_4kHz = PITCH_EST_FRAME_LENGTH_MS * 4; - frame_length_8kHz = PITCH_EST_FRAME_LENGTH_MS * 8; - sf_length = SKP_RSHIFT( frame_length, 3 ); - sf_length_4kHz = SKP_RSHIFT( frame_length_4kHz, 3 ); - sf_length_8kHz = SKP_RSHIFT( frame_length_8kHz, 3 ); - min_lag = PITCH_EST_MIN_LAG_MS * Fs_kHz; - min_lag_4kHz = PITCH_EST_MIN_LAG_MS * 4; - min_lag_8kHz = PITCH_EST_MIN_LAG_MS * 8; - max_lag = PITCH_EST_MAX_LAG_MS * Fs_kHz; - max_lag_4kHz = PITCH_EST_MAX_LAG_MS * 4; - max_lag_8kHz = PITCH_EST_MAX_LAG_MS * 8; - - SKP_memset( C, 0, sizeof( SKP_int16 ) * PITCH_EST_NB_SUBFR * ( ( PITCH_EST_MAX_LAG >> 1 ) + 5) ); - - /* Resample from input sampled at Fs_kHz to 8 kHz */ - if( Fs_kHz == 12 ) { - SKP_int16 R23[ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ]; - SKP_memset( R23, 0, ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); - - SKP_Silk_resample_2_3_coarsest( signal_8kHz, R23, signal, - PITCH_EST_FRAME_LENGTH_MS * 12, (SKP_int16*)scratch_mem ); - } else if( Fs_kHz == 16 ) { - if( complexity == SigProc_PITCH_EST_MAX_COMPLEX ) { - SKP_assert( 4 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); - SKP_memset( filt_state, 0, 4 * sizeof( SKP_int32 ) ); - - SKP_Silk_resample_1_2_coarse( signal, filt_state, signal_8kHz, - scratch_mem, frame_length_8kHz ); - } else { - SKP_assert( 2 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); - SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) ); - - SKP_Silk_resample_1_2_coarsest( signal, filt_state, signal_8kHz, - scratch_mem, frame_length_8kHz ); - } - } else if( Fs_kHz == 24 ) { - /* Resample to 24 -> 8 khz */ - SKP_assert( 7 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); - SKP_memset( filt_state, 0, 7 * sizeof( SKP_int32 ) ); - - SKP_Silk_resample_1_3( signal_8kHz, filt_state, signal, 24 * PITCH_EST_FRAME_LENGTH_MS ); - - } else { - SKP_assert( Fs_kHz == 8 ); - SKP_memcpy( signal_8kHz, signal, frame_length_8kHz * sizeof( SKP_int16 ) ); - } - - /* Decimate again to 4 kHz. Set mem to zero */ - if( complexity == SigProc_PITCH_EST_MAX_COMPLEX ) { - SKP_assert( 4 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); - SKP_memset( filt_state, 0, 4 * sizeof( SKP_int32 ) ); - SKP_Silk_resample_1_2_coarse( signal_8kHz, filt_state, - signal_4kHz, scratch_mem, frame_length_4kHz ); - } else { - SKP_assert( 2 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); - SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) ); - SKP_Silk_resample_1_2_coarsest( signal_8kHz, filt_state, - signal_4kHz, scratch_mem, frame_length_4kHz ); - } - - /* Low-pass filter */ - for( i = frame_length_4kHz - 1; i > 0; i-- ) { - signal_4kHz[ i ] = SKP_ADD_SAT16( signal_4kHz[ i ], signal_4kHz[ i - 1 ] ); - } - - /******************************************************************************* - ** Scale 4 kHz signal down to prevent correlations measures from overflowing - ** find scaling as max scaling for each 8kHz(?) subframe - *******************************************************************************/ - - /* Inner product is calculated with different lengths, so scale for the worst case */ - max_sum_sq_length = SKP_max_32( sf_length_8kHz, SKP_RSHIFT( frame_length_4kHz, 1 ) ); - shift = SKP_FIX_P_Ana_find_scaling( signal_4kHz, frame_length_4kHz, max_sum_sq_length ); - if( shift > 0 ) { - for( i = 0; i < frame_length_4kHz; i++ ) { - signal_4kHz[ i ] = SKP_RSHIFT( signal_4kHz[ i ], shift ); - } - } - - /****************************************************************************** - * FIRST STAGE, operating in 4 khz - ******************************************************************************/ - target_ptr = &signal_4kHz[ SKP_RSHIFT( frame_length_4kHz, 1 ) ]; - for( k = 0; k < 2; k++ ) { - /* Check that we are within range of the array */ - SKP_assert( target_ptr >= signal_4kHz ); - SKP_assert( target_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz ); - - basis_ptr = target_ptr - min_lag_4kHz; - - /* Check that we are within range of the array */ - SKP_assert( basis_ptr >= signal_4kHz ); - SKP_assert( basis_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz ); - - normalizer = 0; - cross_corr = 0; - /* Calculate first vector products before loop */ - cross_corr = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz ); - normalizer = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length_8kHz ); - normalizer = SKP_ADD_SAT32( normalizer, 1000 ); - - temp32 = SKP_DIV32( cross_corr, SKP_Silk_SQRT_APPROX( normalizer ) + 1 ); - C[ k ][ min_lag_4kHz ] = (SKP_int16)SKP_SAT16( temp32 ); /* Q0 */ - - /* From now on normalizer is computed recursively */ - for( d = min_lag_4kHz + 1; d <= max_lag_4kHz; d++ ) { - basis_ptr--; - - /* Check that we are within range of the array */ - SKP_assert( basis_ptr >= signal_4kHz ); - SKP_assert( basis_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz ); - - cross_corr = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz ); - - /* Add contribution of new sample and remove contribution from oldest sample */ - normalizer += - SKP_SMULBB( basis_ptr[ 0 ], basis_ptr[ 0 ] ) - - SKP_SMULBB( basis_ptr[ sf_length_8kHz ], basis_ptr[ sf_length_8kHz ] ); - - temp32 = SKP_DIV32( cross_corr, SKP_Silk_SQRT_APPROX( normalizer ) + 1 ); - C[ k ][ d ] = (SKP_int16)SKP_SAT16( temp32 ); /* Q0 */ - } - /* Update target pointer */ - target_ptr += sf_length_8kHz; - } - - /* Combine two subframes into single correlation measure and apply short-lag bias */ - for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) { - sum = (SKP_int32)C[ 0 ][ i ] + (SKP_int32)C[ 1 ][ i ]; /* Q0 */ - SKP_assert( SKP_RSHIFT( sum, 1 ) == SKP_SAT16( SKP_RSHIFT( sum, 1 ) ) ); - sum = SKP_RSHIFT( sum, 1 ); /* Q-1 */ - SKP_assert( SKP_LSHIFT( (SKP_int32)-i, 4 ) == SKP_SAT16( SKP_LSHIFT( (SKP_int32)-i, 4 ) ) ); - sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) ); /* Q-1 */ - SKP_assert( sum == SKP_SAT16( sum ) ); - C[ 0 ][ i ] = (SKP_int16)sum; /* Q-1 */ - } - - /* Sort */ - length_d_srch = 5 + complexity; - SKP_assert( length_d_srch <= PITCH_EST_D_SRCH_LENGTH ); - SKP_Silk_insertion_sort_decreasing_int16( &C[ 0 ][ min_lag_4kHz ], d_srch, max_lag_4kHz - min_lag_4kHz + 1, length_d_srch ); - - /* Escape if correlation is very low already here */ - target_ptr = &signal_4kHz[ SKP_RSHIFT( frame_length_4kHz, 1 ) ]; - energy = SKP_Silk_inner_prod_aligned( target_ptr, target_ptr, SKP_RSHIFT( frame_length_4kHz, 1 ) ); - energy = SKP_ADD_SAT32( energy, 1000 ); /* Q0 */ - Cmax = (SKP_int)C[ 0 ][ min_lag_4kHz ]; /* Q-1 */ - threshold = SKP_SMULBB( Cmax, Cmax ); /* Q-2 */ - /* Compare in Q-2 domain */ - if( SKP_RSHIFT( energy, 4 + 2 ) > threshold ) { - SKP_memset( pitch_out, 0, PITCH_EST_NB_SUBFR * sizeof( SKP_int ) ); - *LTPCorr_Q15 = 0; - *lagIndex = 0; - *contourIndex = 0; - return 1; - } - - threshold = SKP_SMULWB( search_thres1_Q16, Cmax ); - for( i = 0; i < length_d_srch; i++ ) { - /* Convert to 8 kHz indices for the sorted correlation that exceeds the threshold */ - if( C[ 0 ][ min_lag_4kHz + i ] > threshold ) { - d_srch[ i ] = SKP_LSHIFT( d_srch[ i ] + min_lag_4kHz, 1 ); - } else { - length_d_srch = i; - break; - } - } - SKP_assert( length_d_srch > 0 ); - - for( i = min_lag_8kHz - 5; i < max_lag_8kHz + 5; i++ ) { - d_comp[ i ] = 0; - } - for( i = 0; i < length_d_srch; i++ ) { - d_comp[ d_srch[ i ] ] = 1; - } - - /* Convolution */ - for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) { - d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ]; - } - - length_d_srch = 0; - for( i = min_lag_8kHz; i < max_lag_8kHz + 1; i++ ) { - if( d_comp[ i + 1 ] > 0 ) { - d_srch[ length_d_srch ] = i; - length_d_srch++; - } - } - - /* Convolution */ - for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) { - d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ] + d_comp[ i - 3 ]; - } - - length_d_comp = 0; - for( i = min_lag_8kHz; i < max_lag_8kHz + 4; i++ ) { - if( d_comp[ i ] > 0 ) { - d_comp[ length_d_comp ] = i - 2; - length_d_comp++; - } - } - - /********************************************************************************** - ** SECOND STAGE, operating at 8 kHz, on lag sections with high correlation - *************************************************************************************/ - - /****************************************************************************** - ** Scale signal down to avoid correlations measures from overflowing - *******************************************************************************/ - /* find scaling as max scaling for each subframe */ - shift = SKP_FIX_P_Ana_find_scaling( signal_8kHz, frame_length_8kHz, sf_length_8kHz ); - if( shift > 0 ) { - for( i = 0; i < frame_length_8kHz; i++ ) { - signal_8kHz[ i ] = SKP_RSHIFT( signal_8kHz[ i ], shift ); - } - } - - /********************************************************************************* - * Find energy of each subframe projected onto its history, for a range of delays - *********************************************************************************/ - SKP_memset( C, 0, PITCH_EST_NB_SUBFR * ( ( PITCH_EST_MAX_LAG >> 1 ) + 5 ) * sizeof( SKP_int16 ) ); - - target_ptr = &signal_8kHz[ frame_length_4kHz ]; /* point to middle of frame */ - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - - /* Check that we are within range of the array */ - SKP_assert( target_ptr >= signal_8kHz ); - SKP_assert( target_ptr + sf_length_8kHz <= signal_8kHz + frame_length_8kHz ); - - energy_target = SKP_Silk_inner_prod_aligned( target_ptr, target_ptr, sf_length_8kHz ); - // ToDo: Calculate 1 / energy_target here and save one division inside next for loop - for( j = 0; j < length_d_comp; j++ ) { - d = d_comp[ j ]; - basis_ptr = target_ptr - d; - - /* Check that we are within range of the array */ - SKP_assert( basis_ptr >= signal_8kHz ); - SKP_assert( basis_ptr + sf_length_8kHz <= signal_8kHz + frame_length_8kHz ); - - cross_corr = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz ); - energy_basis = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length_8kHz ); - if( cross_corr > 0 ) { - energy = SKP_max( energy_target, energy_basis ); /* Find max to make sure first division < 1.0 */ - lz = SKP_Silk_CLZ32( cross_corr ); - lshift = SKP_LIMIT( lz - 1, 0, 15 ); - temp32 = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); /* Q15 */ - SKP_assert( temp32 == SKP_SAT16( temp32 ) ); - temp32 = SKP_SMULWB( cross_corr, temp32 ); /* Q(-1), cc * ( cc / max(b, t) ) */ - temp32 = SKP_ADD_SAT32( temp32, temp32 ); /* Q(0) */ - lz = SKP_Silk_CLZ32( temp32 ); - lshift = SKP_LIMIT( lz - 1, 0, 15 ); - energy = SKP_min( energy_target, energy_basis ); - C[ k ][ d ] = SKP_DIV32( SKP_LSHIFT( temp32, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); // Q15 - } else { - C[ k ][ d ] = 0; - } - } - target_ptr += sf_length_8kHz; - } - - /* search over lag range and lags codebook */ - /* scale factor for lag codebook, as a function of center lag */ - - CCmax = SKP_int32_MIN; - CCmax_b = SKP_int32_MIN; - - CBimax = 0; /* To avoid returning undefined lag values */ - lag = -1; /* To check if lag with strong enough correlation has been found */ - - if( prevLag > 0 ) { - if( Fs_kHz == 12 ) { - prevLag = SKP_DIV32_16( SKP_LSHIFT( prevLag, 1 ), 3 ); - } else if( Fs_kHz == 16 ) { - prevLag = SKP_RSHIFT( prevLag, 1 ); - } else if( Fs_kHz == 24 ) { - prevLag = SKP_DIV32_16( prevLag, 3 ); - } - prevLag_log2_Q7 = SKP_Silk_lin2log( (SKP_int32)prevLag ); - } else { - prevLag_log2_Q7 = 0; - } - SKP_assert( search_thres2_Q15 == SKP_SAT16( search_thres2_Q15 ) ); - corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 13 ); - - /* If input is 8 khz use a larger codebook here because it is last stage */ - if( Fs_kHz == 8 && complexity > SigProc_PITCH_EST_MIN_COMPLEX ) { - nb_cbks_stage2 = PITCH_EST_NB_CBKS_STAGE2_EXT; - } else { - nb_cbks_stage2 = PITCH_EST_NB_CBKS_STAGE2; - } - - for( k = 0; k < length_d_srch; k++ ) { - d = d_srch[ k ]; - for( j = 0; j < nb_cbks_stage2; j++ ) { - CC[ j ] = 0; - for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) { - /* Try all codebooks */ - CC[ j ] = CC[ j ] + (SKP_int32)C[ i ][ d + SKP_Silk_CB_lags_stage2[ i ][ j ] ]; - } - } - /* Find best codebook */ - CCmax_new = SKP_int32_MIN; - CBimax_new = 0; - for( i = 0; i < nb_cbks_stage2; i++ ) { - if( CC[ i ] > CCmax_new ) { - CCmax_new = CC[ i ]; - CBimax_new = i; - } - } - - /* Bias towards shorter lags */ - lag_log2_Q7 = SKP_Silk_lin2log( (SKP_int32)d ); /* Q7 */ - SKP_assert( lag_log2_Q7 == SKP_SAT16( lag_log2_Q7 ) ); - SKP_assert( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15 == SKP_SAT16( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15 ) ); - CCmax_new_b = CCmax_new - SKP_RSHIFT( SKP_SMULBB( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15, lag_log2_Q7 ), 7 ); /* Q15 */ - - /* Bias towards previous lag */ - SKP_assert( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15 == SKP_SAT16( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15 ) ); - if( prevLag > 0 ) { - delta_lag_log2_sqr_Q7 = lag_log2_Q7 - prevLag_log2_Q7; - SKP_assert( delta_lag_log2_sqr_Q7 == SKP_SAT16( delta_lag_log2_sqr_Q7 ) ); - delta_lag_log2_sqr_Q7 = SKP_RSHIFT( SKP_SMULBB( delta_lag_log2_sqr_Q7, delta_lag_log2_sqr_Q7 ), 7 ); - prev_lag_bias_Q15 = SKP_RSHIFT( SKP_SMULBB( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15, ( *LTPCorr_Q15 ) ), 15 ); /* Q15 */ - prev_lag_bias_Q15 = SKP_DIV32( SKP_MUL( prev_lag_bias_Q15, delta_lag_log2_sqr_Q7 ), delta_lag_log2_sqr_Q7 + ( 1 << 6 ) ); - CCmax_new_b -= prev_lag_bias_Q15; /* Q15 */ - } - - if( CCmax_new_b > CCmax_b && CCmax_new > corr_thres_Q15 ) { - CCmax_b = CCmax_new_b; - CCmax = CCmax_new; - lag = d; - CBimax = CBimax_new; - } - } - - if( lag == -1 ) { - /* No suitable candidate found */ - SKP_memset( pitch_out, 0, PITCH_EST_NB_SUBFR * sizeof( SKP_int ) ); - *LTPCorr_Q15 = 0; - *lagIndex = 0; - *contourIndex = 0; - return 1; - } - - if( Fs_kHz > 8 ) { - - /****************************************************************************** - ** Scale input signal down to avoid correlations measures from overflowing - *******************************************************************************/ - /* find scaling as max scaling for each subframe */ - shift = SKP_FIX_P_Ana_find_scaling( signal, frame_length, sf_length ); - if( shift > 0 ) { - /* Move signal to scratch mem because the input signal should be unchanged */ - /* Reuse the 32 bit scratch mem vector, use a 16 bit pointer from now */ - input_signal_ptr = (SKP_int16*)scratch_mem; - for( i = 0; i < frame_length; i++ ) { - input_signal_ptr[ i ] = SKP_RSHIFT( signal[ i ], shift ); - } - } else { - input_signal_ptr = (SKP_int16*)signal; - } - /*********************************************************************************/ - - /* Search in original signal */ - - CBimax_old = CBimax; - /* Compensate for decimation */ - SKP_assert( lag == SKP_SAT16( lag ) ); - if( Fs_kHz == 12 ) { - lag = SKP_RSHIFT( SKP_SMULBB( lag, 3 ), 1 ); - } else if( Fs_kHz == 16 ) { - lag = SKP_LSHIFT( lag, 1 ); - } else { - lag = SKP_SMULBB( lag, 3 ); - } - - lag = SKP_LIMIT( lag, min_lag, max_lag ); - start_lag = SKP_max_int( lag - 2, min_lag ); - end_lag = SKP_min_int( lag + 2, max_lag ); - lag_new = lag; /* to avoid undefined lag */ - CBimax = 0; /* to avoid undefined lag */ - SKP_assert( SKP_LSHIFT( CCmax, 13 ) >= 0 ); - *LTPCorr_Q15 = (SKP_int)SKP_Silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */ - - CCmax = SKP_int32_MIN; - /* pitch lags according to second stage */ - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - pitch_out[ k ] = lag + 2 * SKP_Silk_CB_lags_stage2[ k ][ CBimax_old ]; - } - /* Calculate the correlations and energies needed in stage 3 */ - SKP_FIX_P_Ana_calc_corr_st3( crosscorr_st3, input_signal_ptr, start_lag, sf_length, complexity ); - SKP_FIX_P_Ana_calc_energy_st3( energies_st3, input_signal_ptr, start_lag, sf_length, complexity ); - - lag_counter = 0; - SKP_assert( lag == SKP_SAT16( lag ) ); - contour_bias = SKP_DIV32_16( PITCH_EST_FLATCONTOUR_BIAS_Q20, lag ); - - /* Setup cbk parameters acording to complexity setting */ - cbk_size = (SKP_int)SKP_Silk_cbk_sizes_stage3[ complexity ]; - cbk_offset = (SKP_int)SKP_Silk_cbk_offsets_stage3[ complexity ]; - - for( d = start_lag; d <= end_lag; d++ ) { - for( j = cbk_offset; j < ( cbk_offset + cbk_size ); j++ ) { - cross_corr = 0; - energy = 0; - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - SKP_assert( PITCH_EST_NB_SUBFR == 4 ); - energy += SKP_RSHIFT( energies_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */ - SKP_assert( energy >= 0 ); - cross_corr += SKP_RSHIFT( crosscorr_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */ - } - if( cross_corr > 0 ) { - /* Divide cross_corr / energy and get result in Q15 */ - lz = SKP_Silk_CLZ32( cross_corr ); - /* Divide with result in Q13, cross_corr could be larger than energy */ - lshift = SKP_LIMIT( lz - 1, 0, 13 ); - CCmax_new = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 13 - lshift ) + 1 ); - CCmax_new = SKP_SAT16( CCmax_new ); - CCmax_new = SKP_SMULWB( cross_corr, CCmax_new ); - /* Saturate */ - if( CCmax_new > SKP_RSHIFT( SKP_int32_MAX, 3 ) ) { - CCmax_new = SKP_int32_MAX; - } else { - CCmax_new = SKP_LSHIFT( CCmax_new, 3 ); - } - /* Reduce depending on flatness of contour */ - diff = j - SKP_RSHIFT( PITCH_EST_NB_CBKS_STAGE3_MAX, 1 ); - diff = SKP_MUL( diff, diff ); - diff = SKP_int16_MAX - SKP_RSHIFT( SKP_MUL( contour_bias, diff ), 5 ); /* Q20 -> Q15 */ - SKP_assert( diff == SKP_SAT16( diff ) ); - CCmax_new = SKP_LSHIFT( SKP_SMULWB( CCmax_new, diff ), 1 ); - } else { - CCmax_new = 0; - } - - if( CCmax_new > CCmax ) { - CCmax = CCmax_new; - lag_new = d; - CBimax = j; - } - } - lag_counter++; - } - - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - pitch_out[ k ] = lag_new + SKP_Silk_CB_lags_stage3[ k ][ CBimax ]; - } - *lagIndex = lag_new - min_lag; - *contourIndex = CBimax; - } else { - /* Save Lags and correlation */ - CCmax = SKP_max( CCmax, 0 ); - *LTPCorr_Q15 = (SKP_int)SKP_Silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */ - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - pitch_out[ k ] = lag + SKP_Silk_CB_lags_stage2[ k ][ CBimax ]; - } - *lagIndex = lag - min_lag_8kHz; - *contourIndex = CBimax; - } - SKP_assert( *lagIndex >= 0 ); - /* return as voiced */ - return 0; -} - -/*************************************************************************/ -/* Calculates the correlations used in stage 3 search. In order to cover */ -/* the whole lag codebook for all the searched offset lags (lag +- 2), */ -/*************************************************************************/ -void SKP_FIX_P_Ana_calc_corr_st3( - SKP_int32 cross_corr_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */ - const SKP_int16 signal[], /* I vector to correlate */ - SKP_int start_lag, /* I lag offset to search around */ - SKP_int sf_length, /* I length of a 5 ms subframe */ - SKP_int complexity /* I Complexity setting */ -) -{ - const SKP_int16 *target_ptr, *basis_ptr; - SKP_int32 cross_corr; - SKP_int i, j, k, lag_counter; - SKP_int cbk_offset, cbk_size, delta, idx; - SKP_int32 scratch_mem[ SCRATCH_SIZE ]; - - SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX ); - SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX ); - - cbk_offset = SKP_Silk_cbk_offsets_stage3[ complexity ]; - cbk_size = SKP_Silk_cbk_sizes_stage3[ complexity ]; - - target_ptr = &signal[ SKP_LSHIFT( sf_length, 2 ) ]; /* Pointer to middle of frame */ - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - lag_counter = 0; - - /* Calculate the correlations for each subframe */ - for( j = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; j <= SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 1 ]; j++ ) { - basis_ptr = target_ptr - ( start_lag + j ); - cross_corr = SKP_Silk_inner_prod_aligned( (SKP_int16*)target_ptr, (SKP_int16*)basis_ptr, sf_length ); - SKP_assert( lag_counter < SCRATCH_SIZE ); - scratch_mem[ lag_counter ] = cross_corr; - lag_counter++; - } - - delta = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; - for( i = cbk_offset; i < ( cbk_offset + cbk_size ); i++ ) { - /* Fill out the 3 dim array that stores the correlations for */ - /* each code_book vector for each start lag */ - idx = SKP_Silk_CB_lags_stage3[ k ][ i ] - delta; - for( j = 0; j < PITCH_EST_NB_STAGE3_LAGS; j++ ) { - SKP_assert( idx + j < SCRATCH_SIZE ); - SKP_assert( idx + j < lag_counter ); - cross_corr_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ]; - } - } - target_ptr += sf_length; - } -} - -/********************************************************************/ -/* Calculate the energies for first two subframes. The energies are */ -/* calculated recursively. */ -/********************************************************************/ -void SKP_FIX_P_Ana_calc_energy_st3( - SKP_int32 energies_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */ - const SKP_int16 signal[], /* I vector to calc energy in */ - SKP_int start_lag, /* I lag offset to search around */ - SKP_int sf_length, /* I length of one 5 ms subframe */ - SKP_int complexity /* I Complexity setting */ -) -{ - const SKP_int16 *target_ptr, *basis_ptr; - SKP_int32 energy; - SKP_int k, i, j, lag_counter; - SKP_int cbk_offset, cbk_size, delta, idx; - SKP_int32 scratch_mem[ SCRATCH_SIZE ]; - - SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX ); - SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX ); - - cbk_offset = SKP_Silk_cbk_offsets_stage3[ complexity ]; - cbk_size = SKP_Silk_cbk_sizes_stage3[ complexity ]; - - target_ptr = &signal[ SKP_LSHIFT( sf_length, 2 ) ]; - for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { - lag_counter = 0; - - /* Calculate the energy for first lag */ - basis_ptr = target_ptr - ( start_lag + SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ] ); - energy = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length ); - SKP_assert( energy >= 0 ); - scratch_mem[ lag_counter ] = energy; - lag_counter++; - - for( i = 1; i < ( SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 1 ] - SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ] + 1 ); i++ ) { - /* remove part outside new window */ - energy -= SKP_SMULBB( basis_ptr[ sf_length - i ], basis_ptr[ sf_length - i ] ); - SKP_assert( energy >= 0 ); - - /* add part that comes into window */ - energy = SKP_ADD_SAT32( energy, SKP_SMULBB( basis_ptr[ -i ], basis_ptr[ -i ] ) ); - SKP_assert( energy >= 0 ); - SKP_assert( lag_counter < SCRATCH_SIZE ); - scratch_mem[ lag_counter ] = energy; - lag_counter++; - } - - delta = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; - for( i = cbk_offset; i < ( cbk_offset + cbk_size ); i++ ) { - /* Fill out the 3 dim array that stores the correlations for */ - /* each code_book vector for each start lag */ - idx = SKP_Silk_CB_lags_stage3[ k ][ i ] - delta; - for( j = 0; j < PITCH_EST_NB_STAGE3_LAGS; j++ ) { - SKP_assert( idx + j < SCRATCH_SIZE ); - SKP_assert( idx + j < lag_counter ); - energies_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ]; - SKP_assert( energies_st3[ k ][ i ][ j ] >= 0.0f ); - } - } - target_ptr += sf_length; - } -} - -SKP_int32 SKP_FIX_P_Ana_find_scaling( - const SKP_int16 *signal, - const SKP_int signal_length, - const SKP_int sum_sqr_len -) -{ - SKP_int32 nbits, x_max; - - x_max = SKP_Silk_int16_array_maxabs( signal, signal_length ); - - if( x_max < SKP_int16_MAX ) { - /* Number of bits needed for the sum of the squares */ - nbits = 32 - SKP_Silk_CLZ32( SKP_SMULBB( x_max, x_max ) ); - } else { - /* Here we don't know if x_max should have been SKP_int16_MAX + 1, so we expect the worst case */ - nbits = 30; - } - nbits += 17 - SKP_Silk_CLZ16( sum_sqr_len ); - - /* Without a guarantee of saturation, we need to keep the 31st bit free */ - if( nbits < 31 ) { - return 0; - } else { - return( nbits - 30 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/*********************************************************** +* Pitch analyser function +********************************************************** */ +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_pitch_est_defines.h" +#include "SKP_Silk_resample_rom.h" + +#define SCRATCH_SIZE 22 + +/************************************************************/ +/* Internally used functions */ +/************************************************************/ +void SKP_FIX_P_Ana_calc_corr_st3( + SKP_int32 cross_corr_st3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX][PITCH_EST_NB_STAGE3_LAGS],/* (O) 3 DIM correlation array */ + const SKP_int16 signal[], /* I vector to correlate */ + SKP_int start_lag, /* I lag offset to search around */ + SKP_int sf_length, /* I length of a 5 ms subframe */ + SKP_int complexity /* I Complexity setting */ +); + +void SKP_FIX_P_Ana_calc_energy_st3( + SKP_int32 energies_st3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX][PITCH_EST_NB_STAGE3_LAGS],/* (O) 3 DIM energy array */ + const SKP_int16 signal[], /* I vector to calc energy in */ + SKP_int start_lag, /* I lag offset to search around */ + SKP_int sf_length, /* I length of one 5 ms subframe */ + SKP_int complexity /* I Complexity setting */ +); + +SKP_int32 SKP_FIX_P_Ana_find_scaling( + const SKP_int16 *signal, + const SKP_int signal_length, + const SKP_int sum_sqr_len +); + +void SKP_Silk_decode_pitch( + SKP_int lagIndex, /* I */ + SKP_int contourIndex, /* O */ + SKP_int pitch_lags[], /* O 4 pitch values */ + SKP_int Fs_kHz /* I sampling frequency (kHz) */ +) +{ + SKP_int lag, i, min_lag; + + min_lag = SKP_SMULBB( PITCH_EST_MIN_LAG_MS, Fs_kHz ); + + /* Only for 24 / 16 kHz version for now */ + lag = min_lag + lagIndex; + if( Fs_kHz == 8 ) { + /* Only a small codebook for 8 khz */ + for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) { + pitch_lags[ i ] = lag + SKP_Silk_CB_lags_stage2[ i ][ contourIndex ]; + } + } else { + for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) { + pitch_lags[ i ] = lag + SKP_Silk_CB_lags_stage3[ i ][ contourIndex ]; + } + } +} + +/*************************************************************/ +/* FIXED POINT CORE PITCH ANALYSIS FUNCTION */ +/*************************************************************/ +SKP_int SKP_Silk_pitch_analysis_core( /* O Voicing estimate: 0 voiced, 1 unvoiced */ + const SKP_int16 *signal, /* I Signal of length PITCH_EST_FRAME_LENGTH_MS*Fs_kHz */ + SKP_int *pitch_out, /* O 4 pitch lag values */ + SKP_int *lagIndex, /* O Lag Index */ + SKP_int *contourIndex, /* O Pitch contour Index */ + SKP_int *LTPCorr_Q15, /* I/O Normalized correlation; input: value from previous frame */ + SKP_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */ + const SKP_int32 search_thres1_Q16, /* I First stage threshold for lag candidates 0 - 1 */ + const SKP_int search_thres2_Q15, /* I Final threshold for lag candidates 0 - 1 */ + const SKP_int Fs_kHz, /* I Sample frequency (kHz) */ + const SKP_int complexity /* I Complexity setting, 0-2, where 2 is highest */ +) +{ + SKP_int16 signal_8kHz[ PITCH_EST_MAX_FRAME_LENGTH_ST_2 ]; + SKP_int16 signal_4kHz[ PITCH_EST_MAX_FRAME_LENGTH_ST_1 ]; + SKP_int32 scratch_mem[ 3 * PITCH_EST_MAX_FRAME_LENGTH ]; + SKP_int16 *input_signal_ptr; + SKP_int32 filt_state[ PITCH_EST_MAX_DECIMATE_STATE_LENGTH ]; + SKP_int i, k, d, j; + SKP_int16 C[ PITCH_EST_NB_SUBFR ][ ( PITCH_EST_MAX_LAG >> 1 ) + 5 ]; + const SKP_int16 *target_ptr, *basis_ptr; + SKP_int32 cross_corr, normalizer, energy, shift, energy_basis, energy_target; + SKP_int d_srch[ PITCH_EST_D_SRCH_LENGTH ]; + SKP_int16 d_comp[ ( PITCH_EST_MAX_LAG >> 1 ) + 5 ]; + SKP_int Cmax, length_d_srch, length_d_comp; + SKP_int32 sum, threshold, temp32; + SKP_int CBimax, CBimax_new, CBimax_old, lag, start_lag, end_lag, lag_new; + SKP_int32 CC[ PITCH_EST_NB_CBKS_STAGE2_EXT ], CCmax, CCmax_b, CCmax_new_b, CCmax_new; + SKP_int32 energies_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ]; + SKP_int32 crosscorr_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ]; + SKP_int32 lag_counter; + SKP_int frame_length, frame_length_8kHz, frame_length_4kHz, max_sum_sq_length; + SKP_int sf_length, sf_length_8kHz, sf_length_4kHz; + SKP_int min_lag, min_lag_8kHz, min_lag_4kHz; + SKP_int max_lag, max_lag_8kHz, max_lag_4kHz; + SKP_int32 contour_bias, diff; + SKP_int32 lz, lshift; + SKP_int cbk_offset, cbk_size, nb_cbks_stage2; + SKP_int32 delta_lag_log2_sqr_Q7, lag_log2_Q7, prevLag_log2_Q7, prev_lag_bias_Q15, corr_thres_Q15; + + /* Check for valid sampling frequency */ + SKP_assert( Fs_kHz == 8 || Fs_kHz == 12 || Fs_kHz == 16 || Fs_kHz == 24 ); + + /* Check for valid complexity setting */ + SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX ); + SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX ); + + SKP_assert( search_thres1_Q16 >= 0 && search_thres1_Q16 <= (1<<16) ); + SKP_assert( search_thres2_Q15 >= 0 && search_thres2_Q15 <= (1<<15) ); + + /* Setup frame lengths max / min lag for the sampling frequency */ + frame_length = PITCH_EST_FRAME_LENGTH_MS * Fs_kHz; + frame_length_4kHz = PITCH_EST_FRAME_LENGTH_MS * 4; + frame_length_8kHz = PITCH_EST_FRAME_LENGTH_MS * 8; + sf_length = SKP_RSHIFT( frame_length, 3 ); + sf_length_4kHz = SKP_RSHIFT( frame_length_4kHz, 3 ); + sf_length_8kHz = SKP_RSHIFT( frame_length_8kHz, 3 ); + min_lag = PITCH_EST_MIN_LAG_MS * Fs_kHz; + min_lag_4kHz = PITCH_EST_MIN_LAG_MS * 4; + min_lag_8kHz = PITCH_EST_MIN_LAG_MS * 8; + max_lag = PITCH_EST_MAX_LAG_MS * Fs_kHz; + max_lag_4kHz = PITCH_EST_MAX_LAG_MS * 4; + max_lag_8kHz = PITCH_EST_MAX_LAG_MS * 8; + + SKP_memset( C, 0, sizeof( SKP_int16 ) * PITCH_EST_NB_SUBFR * ( ( PITCH_EST_MAX_LAG >> 1 ) + 5) ); + + /* Resample from input sampled at Fs_kHz to 8 kHz */ + if( Fs_kHz == 12 ) { + SKP_int16 R23[ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ]; + SKP_memset( R23, 0, ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); + + SKP_Silk_resample_2_3_coarsest( signal_8kHz, R23, signal, + PITCH_EST_FRAME_LENGTH_MS * 12, (SKP_int16*)scratch_mem ); + } else if( Fs_kHz == 16 ) { + if( complexity == SigProc_PITCH_EST_MAX_COMPLEX ) { + SKP_assert( 4 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); + SKP_memset( filt_state, 0, 4 * sizeof( SKP_int32 ) ); + + SKP_Silk_resample_1_2_coarse( signal, filt_state, signal_8kHz, + scratch_mem, frame_length_8kHz ); + } else { + SKP_assert( 2 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); + SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) ); + + SKP_Silk_resample_1_2_coarsest( signal, filt_state, signal_8kHz, + scratch_mem, frame_length_8kHz ); + } + } else if( Fs_kHz == 24 ) { + /* Resample to 24 -> 8 khz */ + SKP_assert( 7 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); + SKP_memset( filt_state, 0, 7 * sizeof( SKP_int32 ) ); + + SKP_Silk_resample_1_3( signal_8kHz, filt_state, signal, 24 * PITCH_EST_FRAME_LENGTH_MS ); + + } else { + SKP_assert( Fs_kHz == 8 ); + SKP_memcpy( signal_8kHz, signal, frame_length_8kHz * sizeof( SKP_int16 ) ); + } + + /* Decimate again to 4 kHz. Set mem to zero */ + if( complexity == SigProc_PITCH_EST_MAX_COMPLEX ) { + SKP_assert( 4 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); + SKP_memset( filt_state, 0, 4 * sizeof( SKP_int32 ) ); + SKP_Silk_resample_1_2_coarse( signal_8kHz, filt_state, + signal_4kHz, scratch_mem, frame_length_4kHz ); + } else { + SKP_assert( 2 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH ); + SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) ); + SKP_Silk_resample_1_2_coarsest( signal_8kHz, filt_state, + signal_4kHz, scratch_mem, frame_length_4kHz ); + } + + /* Low-pass filter */ + for( i = frame_length_4kHz - 1; i > 0; i-- ) { + signal_4kHz[ i ] = SKP_ADD_SAT16( signal_4kHz[ i ], signal_4kHz[ i - 1 ] ); + } + + /******************************************************************************* + ** Scale 4 kHz signal down to prevent correlations measures from overflowing + ** find scaling as max scaling for each 8kHz(?) subframe + *******************************************************************************/ + + /* Inner product is calculated with different lengths, so scale for the worst case */ + max_sum_sq_length = SKP_max_32( sf_length_8kHz, SKP_RSHIFT( frame_length_4kHz, 1 ) ); + shift = SKP_FIX_P_Ana_find_scaling( signal_4kHz, frame_length_4kHz, max_sum_sq_length ); + if( shift > 0 ) { + for( i = 0; i < frame_length_4kHz; i++ ) { + signal_4kHz[ i ] = SKP_RSHIFT( signal_4kHz[ i ], shift ); + } + } + + /****************************************************************************** + * FIRST STAGE, operating in 4 khz + ******************************************************************************/ + target_ptr = &signal_4kHz[ SKP_RSHIFT( frame_length_4kHz, 1 ) ]; + for( k = 0; k < 2; k++ ) { + /* Check that we are within range of the array */ + SKP_assert( target_ptr >= signal_4kHz ); + SKP_assert( target_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz ); + + basis_ptr = target_ptr - min_lag_4kHz; + + /* Check that we are within range of the array */ + SKP_assert( basis_ptr >= signal_4kHz ); + SKP_assert( basis_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz ); + + normalizer = 0; + cross_corr = 0; + /* Calculate first vector products before loop */ + cross_corr = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz ); + normalizer = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length_8kHz ); + normalizer = SKP_ADD_SAT32( normalizer, 1000 ); + + temp32 = SKP_DIV32( cross_corr, SKP_Silk_SQRT_APPROX( normalizer ) + 1 ); + C[ k ][ min_lag_4kHz ] = (SKP_int16)SKP_SAT16( temp32 ); /* Q0 */ + + /* From now on normalizer is computed recursively */ + for( d = min_lag_4kHz + 1; d <= max_lag_4kHz; d++ ) { + basis_ptr--; + + /* Check that we are within range of the array */ + SKP_assert( basis_ptr >= signal_4kHz ); + SKP_assert( basis_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz ); + + cross_corr = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz ); + + /* Add contribution of new sample and remove contribution from oldest sample */ + normalizer += + SKP_SMULBB( basis_ptr[ 0 ], basis_ptr[ 0 ] ) - + SKP_SMULBB( basis_ptr[ sf_length_8kHz ], basis_ptr[ sf_length_8kHz ] ); + + temp32 = SKP_DIV32( cross_corr, SKP_Silk_SQRT_APPROX( normalizer ) + 1 ); + C[ k ][ d ] = (SKP_int16)SKP_SAT16( temp32 ); /* Q0 */ + } + /* Update target pointer */ + target_ptr += sf_length_8kHz; + } + + /* Combine two subframes into single correlation measure and apply short-lag bias */ + for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) { + sum = (SKP_int32)C[ 0 ][ i ] + (SKP_int32)C[ 1 ][ i ]; /* Q0 */ + SKP_assert( SKP_RSHIFT( sum, 1 ) == SKP_SAT16( SKP_RSHIFT( sum, 1 ) ) ); + sum = SKP_RSHIFT( sum, 1 ); /* Q-1 */ + SKP_assert( SKP_LSHIFT( (SKP_int32)-i, 4 ) == SKP_SAT16( SKP_LSHIFT( (SKP_int32)-i, 4 ) ) ); + sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) ); /* Q-1 */ + SKP_assert( sum == SKP_SAT16( sum ) ); + C[ 0 ][ i ] = (SKP_int16)sum; /* Q-1 */ + } + + /* Sort */ + length_d_srch = 5 + complexity; + SKP_assert( length_d_srch <= PITCH_EST_D_SRCH_LENGTH ); + SKP_Silk_insertion_sort_decreasing_int16( &C[ 0 ][ min_lag_4kHz ], d_srch, max_lag_4kHz - min_lag_4kHz + 1, length_d_srch ); + + /* Escape if correlation is very low already here */ + target_ptr = &signal_4kHz[ SKP_RSHIFT( frame_length_4kHz, 1 ) ]; + energy = SKP_Silk_inner_prod_aligned( target_ptr, target_ptr, SKP_RSHIFT( frame_length_4kHz, 1 ) ); + energy = SKP_ADD_SAT32( energy, 1000 ); /* Q0 */ + Cmax = (SKP_int)C[ 0 ][ min_lag_4kHz ]; /* Q-1 */ + threshold = SKP_SMULBB( Cmax, Cmax ); /* Q-2 */ + /* Compare in Q-2 domain */ + if( SKP_RSHIFT( energy, 4 + 2 ) > threshold ) { + SKP_memset( pitch_out, 0, PITCH_EST_NB_SUBFR * sizeof( SKP_int ) ); + *LTPCorr_Q15 = 0; + *lagIndex = 0; + *contourIndex = 0; + return 1; + } + + threshold = SKP_SMULWB( search_thres1_Q16, Cmax ); + for( i = 0; i < length_d_srch; i++ ) { + /* Convert to 8 kHz indices for the sorted correlation that exceeds the threshold */ + if( C[ 0 ][ min_lag_4kHz + i ] > threshold ) { + d_srch[ i ] = SKP_LSHIFT( d_srch[ i ] + min_lag_4kHz, 1 ); + } else { + length_d_srch = i; + break; + } + } + SKP_assert( length_d_srch > 0 ); + + for( i = min_lag_8kHz - 5; i < max_lag_8kHz + 5; i++ ) { + d_comp[ i ] = 0; + } + for( i = 0; i < length_d_srch; i++ ) { + d_comp[ d_srch[ i ] ] = 1; + } + + /* Convolution */ + for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) { + d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ]; + } + + length_d_srch = 0; + for( i = min_lag_8kHz; i < max_lag_8kHz + 1; i++ ) { + if( d_comp[ i + 1 ] > 0 ) { + d_srch[ length_d_srch ] = i; + length_d_srch++; + } + } + + /* Convolution */ + for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) { + d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ] + d_comp[ i - 3 ]; + } + + length_d_comp = 0; + for( i = min_lag_8kHz; i < max_lag_8kHz + 4; i++ ) { + if( d_comp[ i ] > 0 ) { + d_comp[ length_d_comp ] = i - 2; + length_d_comp++; + } + } + + /********************************************************************************** + ** SECOND STAGE, operating at 8 kHz, on lag sections with high correlation + *************************************************************************************/ + + /****************************************************************************** + ** Scale signal down to avoid correlations measures from overflowing + *******************************************************************************/ + /* find scaling as max scaling for each subframe */ + shift = SKP_FIX_P_Ana_find_scaling( signal_8kHz, frame_length_8kHz, sf_length_8kHz ); + if( shift > 0 ) { + for( i = 0; i < frame_length_8kHz; i++ ) { + signal_8kHz[ i ] = SKP_RSHIFT( signal_8kHz[ i ], shift ); + } + } + + /********************************************************************************* + * Find energy of each subframe projected onto its history, for a range of delays + *********************************************************************************/ + SKP_memset( C, 0, PITCH_EST_NB_SUBFR * ( ( PITCH_EST_MAX_LAG >> 1 ) + 5 ) * sizeof( SKP_int16 ) ); + + target_ptr = &signal_8kHz[ frame_length_4kHz ]; /* point to middle of frame */ + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + + /* Check that we are within range of the array */ + SKP_assert( target_ptr >= signal_8kHz ); + SKP_assert( target_ptr + sf_length_8kHz <= signal_8kHz + frame_length_8kHz ); + + energy_target = SKP_Silk_inner_prod_aligned( target_ptr, target_ptr, sf_length_8kHz ); + // ToDo: Calculate 1 / energy_target here and save one division inside next for loop + for( j = 0; j < length_d_comp; j++ ) { + d = d_comp[ j ]; + basis_ptr = target_ptr - d; + + /* Check that we are within range of the array */ + SKP_assert( basis_ptr >= signal_8kHz ); + SKP_assert( basis_ptr + sf_length_8kHz <= signal_8kHz + frame_length_8kHz ); + + cross_corr = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz ); + energy_basis = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length_8kHz ); + if( cross_corr > 0 ) { + energy = SKP_max( energy_target, energy_basis ); /* Find max to make sure first division < 1.0 */ + lz = SKP_Silk_CLZ32( cross_corr ); + lshift = SKP_LIMIT( lz - 1, 0, 15 ); + temp32 = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); /* Q15 */ + SKP_assert( temp32 == SKP_SAT16( temp32 ) ); + temp32 = SKP_SMULWB( cross_corr, temp32 ); /* Q(-1), cc * ( cc / max(b, t) ) */ + temp32 = SKP_ADD_SAT32( temp32, temp32 ); /* Q(0) */ + lz = SKP_Silk_CLZ32( temp32 ); + lshift = SKP_LIMIT( lz - 1, 0, 15 ); + energy = SKP_min( energy_target, energy_basis ); + C[ k ][ d ] = SKP_DIV32( SKP_LSHIFT( temp32, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); // Q15 + } else { + C[ k ][ d ] = 0; + } + } + target_ptr += sf_length_8kHz; + } + + /* search over lag range and lags codebook */ + /* scale factor for lag codebook, as a function of center lag */ + + CCmax = SKP_int32_MIN; + CCmax_b = SKP_int32_MIN; + + CBimax = 0; /* To avoid returning undefined lag values */ + lag = -1; /* To check if lag with strong enough correlation has been found */ + + if( prevLag > 0 ) { + if( Fs_kHz == 12 ) { + prevLag = SKP_DIV32_16( SKP_LSHIFT( prevLag, 1 ), 3 ); + } else if( Fs_kHz == 16 ) { + prevLag = SKP_RSHIFT( prevLag, 1 ); + } else if( Fs_kHz == 24 ) { + prevLag = SKP_DIV32_16( prevLag, 3 ); + } + prevLag_log2_Q7 = SKP_Silk_lin2log( (SKP_int32)prevLag ); + } else { + prevLag_log2_Q7 = 0; + } + SKP_assert( search_thres2_Q15 == SKP_SAT16( search_thres2_Q15 ) ); + corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 13 ); + + /* If input is 8 khz use a larger codebook here because it is last stage */ + if( Fs_kHz == 8 && complexity > SigProc_PITCH_EST_MIN_COMPLEX ) { + nb_cbks_stage2 = PITCH_EST_NB_CBKS_STAGE2_EXT; + } else { + nb_cbks_stage2 = PITCH_EST_NB_CBKS_STAGE2; + } + + for( k = 0; k < length_d_srch; k++ ) { + d = d_srch[ k ]; + for( j = 0; j < nb_cbks_stage2; j++ ) { + CC[ j ] = 0; + for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) { + /* Try all codebooks */ + CC[ j ] = CC[ j ] + (SKP_int32)C[ i ][ d + SKP_Silk_CB_lags_stage2[ i ][ j ] ]; + } + } + /* Find best codebook */ + CCmax_new = SKP_int32_MIN; + CBimax_new = 0; + for( i = 0; i < nb_cbks_stage2; i++ ) { + if( CC[ i ] > CCmax_new ) { + CCmax_new = CC[ i ]; + CBimax_new = i; + } + } + + /* Bias towards shorter lags */ + lag_log2_Q7 = SKP_Silk_lin2log( (SKP_int32)d ); /* Q7 */ + SKP_assert( lag_log2_Q7 == SKP_SAT16( lag_log2_Q7 ) ); + SKP_assert( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15 == SKP_SAT16( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15 ) ); + CCmax_new_b = CCmax_new - SKP_RSHIFT( SKP_SMULBB( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15, lag_log2_Q7 ), 7 ); /* Q15 */ + + /* Bias towards previous lag */ + SKP_assert( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15 == SKP_SAT16( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15 ) ); + if( prevLag > 0 ) { + delta_lag_log2_sqr_Q7 = lag_log2_Q7 - prevLag_log2_Q7; + SKP_assert( delta_lag_log2_sqr_Q7 == SKP_SAT16( delta_lag_log2_sqr_Q7 ) ); + delta_lag_log2_sqr_Q7 = SKP_RSHIFT( SKP_SMULBB( delta_lag_log2_sqr_Q7, delta_lag_log2_sqr_Q7 ), 7 ); + prev_lag_bias_Q15 = SKP_RSHIFT( SKP_SMULBB( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15, ( *LTPCorr_Q15 ) ), 15 ); /* Q15 */ + prev_lag_bias_Q15 = SKP_DIV32( SKP_MUL( prev_lag_bias_Q15, delta_lag_log2_sqr_Q7 ), delta_lag_log2_sqr_Q7 + ( 1 << 6 ) ); + CCmax_new_b -= prev_lag_bias_Q15; /* Q15 */ + } + + if( CCmax_new_b > CCmax_b && CCmax_new > corr_thres_Q15 ) { + CCmax_b = CCmax_new_b; + CCmax = CCmax_new; + lag = d; + CBimax = CBimax_new; + } + } + + if( lag == -1 ) { + /* No suitable candidate found */ + SKP_memset( pitch_out, 0, PITCH_EST_NB_SUBFR * sizeof( SKP_int ) ); + *LTPCorr_Q15 = 0; + *lagIndex = 0; + *contourIndex = 0; + return 1; + } + + if( Fs_kHz > 8 ) { + + /****************************************************************************** + ** Scale input signal down to avoid correlations measures from overflowing + *******************************************************************************/ + /* find scaling as max scaling for each subframe */ + shift = SKP_FIX_P_Ana_find_scaling( signal, frame_length, sf_length ); + if( shift > 0 ) { + /* Move signal to scratch mem because the input signal should be unchanged */ + /* Reuse the 32 bit scratch mem vector, use a 16 bit pointer from now */ + input_signal_ptr = (SKP_int16*)scratch_mem; + for( i = 0; i < frame_length; i++ ) { + input_signal_ptr[ i ] = SKP_RSHIFT( signal[ i ], shift ); + } + } else { + input_signal_ptr = (SKP_int16*)signal; + } + /*********************************************************************************/ + + /* Search in original signal */ + + CBimax_old = CBimax; + /* Compensate for decimation */ + SKP_assert( lag == SKP_SAT16( lag ) ); + if( Fs_kHz == 12 ) { + lag = SKP_RSHIFT( SKP_SMULBB( lag, 3 ), 1 ); + } else if( Fs_kHz == 16 ) { + lag = SKP_LSHIFT( lag, 1 ); + } else { + lag = SKP_SMULBB( lag, 3 ); + } + + lag = SKP_LIMIT( lag, min_lag, max_lag ); + start_lag = SKP_max_int( lag - 2, min_lag ); + end_lag = SKP_min_int( lag + 2, max_lag ); + lag_new = lag; /* to avoid undefined lag */ + CBimax = 0; /* to avoid undefined lag */ + SKP_assert( SKP_LSHIFT( CCmax, 13 ) >= 0 ); + *LTPCorr_Q15 = (SKP_int)SKP_Silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */ + + CCmax = SKP_int32_MIN; + /* pitch lags according to second stage */ + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + pitch_out[ k ] = lag + 2 * SKP_Silk_CB_lags_stage2[ k ][ CBimax_old ]; + } + /* Calculate the correlations and energies needed in stage 3 */ + SKP_FIX_P_Ana_calc_corr_st3( crosscorr_st3, input_signal_ptr, start_lag, sf_length, complexity ); + SKP_FIX_P_Ana_calc_energy_st3( energies_st3, input_signal_ptr, start_lag, sf_length, complexity ); + + lag_counter = 0; + SKP_assert( lag == SKP_SAT16( lag ) ); + contour_bias = SKP_DIV32_16( PITCH_EST_FLATCONTOUR_BIAS_Q20, lag ); + + /* Setup cbk parameters acording to complexity setting */ + cbk_size = (SKP_int)SKP_Silk_cbk_sizes_stage3[ complexity ]; + cbk_offset = (SKP_int)SKP_Silk_cbk_offsets_stage3[ complexity ]; + + for( d = start_lag; d <= end_lag; d++ ) { + for( j = cbk_offset; j < ( cbk_offset + cbk_size ); j++ ) { + cross_corr = 0; + energy = 0; + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + SKP_assert( PITCH_EST_NB_SUBFR == 4 ); + energy += SKP_RSHIFT( energies_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */ + SKP_assert( energy >= 0 ); + cross_corr += SKP_RSHIFT( crosscorr_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */ + } + if( cross_corr > 0 ) { + /* Divide cross_corr / energy and get result in Q15 */ + lz = SKP_Silk_CLZ32( cross_corr ); + /* Divide with result in Q13, cross_corr could be larger than energy */ + lshift = SKP_LIMIT( lz - 1, 0, 13 ); + CCmax_new = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 13 - lshift ) + 1 ); + CCmax_new = SKP_SAT16( CCmax_new ); + CCmax_new = SKP_SMULWB( cross_corr, CCmax_new ); + /* Saturate */ + if( CCmax_new > SKP_RSHIFT( SKP_int32_MAX, 3 ) ) { + CCmax_new = SKP_int32_MAX; + } else { + CCmax_new = SKP_LSHIFT( CCmax_new, 3 ); + } + /* Reduce depending on flatness of contour */ + diff = j - SKP_RSHIFT( PITCH_EST_NB_CBKS_STAGE3_MAX, 1 ); + diff = SKP_MUL( diff, diff ); + diff = SKP_int16_MAX - SKP_RSHIFT( SKP_MUL( contour_bias, diff ), 5 ); /* Q20 -> Q15 */ + SKP_assert( diff == SKP_SAT16( diff ) ); + CCmax_new = SKP_LSHIFT( SKP_SMULWB( CCmax_new, diff ), 1 ); + } else { + CCmax_new = 0; + } + + if( CCmax_new > CCmax ) { + CCmax = CCmax_new; + lag_new = d; + CBimax = j; + } + } + lag_counter++; + } + + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + pitch_out[ k ] = lag_new + SKP_Silk_CB_lags_stage3[ k ][ CBimax ]; + } + *lagIndex = lag_new - min_lag; + *contourIndex = CBimax; + } else { + /* Save Lags and correlation */ + CCmax = SKP_max( CCmax, 0 ); + *LTPCorr_Q15 = (SKP_int)SKP_Silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */ + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + pitch_out[ k ] = lag + SKP_Silk_CB_lags_stage2[ k ][ CBimax ]; + } + *lagIndex = lag - min_lag_8kHz; + *contourIndex = CBimax; + } + SKP_assert( *lagIndex >= 0 ); + /* return as voiced */ + return 0; +} + +/*************************************************************************/ +/* Calculates the correlations used in stage 3 search. In order to cover */ +/* the whole lag codebook for all the searched offset lags (lag +- 2), */ +/*************************************************************************/ +void SKP_FIX_P_Ana_calc_corr_st3( + SKP_int32 cross_corr_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */ + const SKP_int16 signal[], /* I vector to correlate */ + SKP_int start_lag, /* I lag offset to search around */ + SKP_int sf_length, /* I length of a 5 ms subframe */ + SKP_int complexity /* I Complexity setting */ +) +{ + const SKP_int16 *target_ptr, *basis_ptr; + SKP_int32 cross_corr; + SKP_int i, j, k, lag_counter; + SKP_int cbk_offset, cbk_size, delta, idx; + SKP_int32 scratch_mem[ SCRATCH_SIZE ]; + + SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX ); + SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX ); + + cbk_offset = SKP_Silk_cbk_offsets_stage3[ complexity ]; + cbk_size = SKP_Silk_cbk_sizes_stage3[ complexity ]; + + target_ptr = &signal[ SKP_LSHIFT( sf_length, 2 ) ]; /* Pointer to middle of frame */ + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + lag_counter = 0; + + /* Calculate the correlations for each subframe */ + for( j = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; j <= SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 1 ]; j++ ) { + basis_ptr = target_ptr - ( start_lag + j ); + cross_corr = SKP_Silk_inner_prod_aligned( (SKP_int16*)target_ptr, (SKP_int16*)basis_ptr, sf_length ); + SKP_assert( lag_counter < SCRATCH_SIZE ); + scratch_mem[ lag_counter ] = cross_corr; + lag_counter++; + } + + delta = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; + for( i = cbk_offset; i < ( cbk_offset + cbk_size ); i++ ) { + /* Fill out the 3 dim array that stores the correlations for */ + /* each code_book vector for each start lag */ + idx = SKP_Silk_CB_lags_stage3[ k ][ i ] - delta; + for( j = 0; j < PITCH_EST_NB_STAGE3_LAGS; j++ ) { + SKP_assert( idx + j < SCRATCH_SIZE ); + SKP_assert( idx + j < lag_counter ); + cross_corr_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ]; + } + } + target_ptr += sf_length; + } +} + +/********************************************************************/ +/* Calculate the energies for first two subframes. The energies are */ +/* calculated recursively. */ +/********************************************************************/ +void SKP_FIX_P_Ana_calc_energy_st3( + SKP_int32 energies_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */ + const SKP_int16 signal[], /* I vector to calc energy in */ + SKP_int start_lag, /* I lag offset to search around */ + SKP_int sf_length, /* I length of one 5 ms subframe */ + SKP_int complexity /* I Complexity setting */ +) +{ + const SKP_int16 *target_ptr, *basis_ptr; + SKP_int32 energy; + SKP_int k, i, j, lag_counter; + SKP_int cbk_offset, cbk_size, delta, idx; + SKP_int32 scratch_mem[ SCRATCH_SIZE ]; + + SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX ); + SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX ); + + cbk_offset = SKP_Silk_cbk_offsets_stage3[ complexity ]; + cbk_size = SKP_Silk_cbk_sizes_stage3[ complexity ]; + + target_ptr = &signal[ SKP_LSHIFT( sf_length, 2 ) ]; + for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) { + lag_counter = 0; + + /* Calculate the energy for first lag */ + basis_ptr = target_ptr - ( start_lag + SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ] ); + energy = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length ); + SKP_assert( energy >= 0 ); + scratch_mem[ lag_counter ] = energy; + lag_counter++; + + for( i = 1; i < ( SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 1 ] - SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ] + 1 ); i++ ) { + /* remove part outside new window */ + energy -= SKP_SMULBB( basis_ptr[ sf_length - i ], basis_ptr[ sf_length - i ] ); + SKP_assert( energy >= 0 ); + + /* add part that comes into window */ + energy = SKP_ADD_SAT32( energy, SKP_SMULBB( basis_ptr[ -i ], basis_ptr[ -i ] ) ); + SKP_assert( energy >= 0 ); + SKP_assert( lag_counter < SCRATCH_SIZE ); + scratch_mem[ lag_counter ] = energy; + lag_counter++; + } + + delta = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; + for( i = cbk_offset; i < ( cbk_offset + cbk_size ); i++ ) { + /* Fill out the 3 dim array that stores the correlations for */ + /* each code_book vector for each start lag */ + idx = SKP_Silk_CB_lags_stage3[ k ][ i ] - delta; + for( j = 0; j < PITCH_EST_NB_STAGE3_LAGS; j++ ) { + SKP_assert( idx + j < SCRATCH_SIZE ); + SKP_assert( idx + j < lag_counter ); + energies_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ]; + SKP_assert( energies_st3[ k ][ i ][ j ] >= 0.0f ); + } + } + target_ptr += sf_length; + } +} + +SKP_int32 SKP_FIX_P_Ana_find_scaling( + const SKP_int16 *signal, + const SKP_int signal_length, + const SKP_int sum_sqr_len +) +{ + SKP_int32 nbits, x_max; + + x_max = SKP_Silk_int16_array_maxabs( signal, signal_length ); + + if( x_max < SKP_int16_MAX ) { + /* Number of bits needed for the sum of the squares */ + nbits = 32 - SKP_Silk_CLZ32( SKP_SMULBB( x_max, x_max ) ); + } else { + /* Here we don't know if x_max should have been SKP_int16_MAX + 1, so we expect the worst case */ + nbits = 30; + } + nbits += 17 - SKP_Silk_CLZ16( sum_sqr_len ); + + /* Without a guarantee of saturation, we need to keep the 31st bit free */ + if( nbits < 31 ) { + return 0; + } else { + return( nbits - 30 ); + } +} diff --git a/jni/silk/src/SKP_Silk_pitch_est_defines.h b/app/src/main/jni/silk/src/SKP_Silk_pitch_est_defines.h similarity index 98% rename from jni/silk/src/SKP_Silk_pitch_est_defines.h rename to app/src/main/jni/silk/src/SKP_Silk_pitch_est_defines.h index 97dd0a9..9f9a677 100644 --- a/jni/silk/src/SKP_Silk_pitch_est_defines.h +++ b/app/src/main/jni/silk/src/SKP_Silk_pitch_est_defines.h @@ -1,43 +1,43 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SIGPROCFIX_PITCH_EST_DEFINES_H -#define SIGPROCFIX_PITCH_EST_DEFINES_H - -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_common_pitch_est_defines.h" - -/************************************************************/ -/* Definitions For Fix pitch estimator */ -/************************************************************/ - -#define PITCH_EST_SHORTLAG_BIAS_Q15 6554 /* 0.2f. for logarithmic weighting */ -#define PITCH_EST_PREVLAG_BIAS_Q15 6554 /* Prev lag bias */ -#define PITCH_EST_FLATCONTOUR_BIAS_Q20 52429 /* 0.05f */ - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SIGPROCFIX_PITCH_EST_DEFINES_H +#define SIGPROCFIX_PITCH_EST_DEFINES_H + +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_common_pitch_est_defines.h" + +/************************************************************/ +/* Definitions For Fix pitch estimator */ +/************************************************************/ + +#define PITCH_EST_SHORTLAG_BIAS_Q15 6554 /* 0.2f. for logarithmic weighting */ +#define PITCH_EST_PREVLAG_BIAS_Q15 6554 /* Prev lag bias */ +#define PITCH_EST_FLATCONTOUR_BIAS_Q20 52429 /* 0.05f */ + +#endif + diff --git a/jni/silk/src/SKP_Silk_pitch_est_tables.c b/app/src/main/jni/silk/src/SKP_Silk_pitch_est_tables.c similarity index 97% rename from jni/silk/src/SKP_Silk_pitch_est_tables.c rename to app/src/main/jni/silk/src/SKP_Silk_pitch_est_tables.c index c151819..32c5019 100644 --- a/jni/silk/src/SKP_Silk_pitch_est_tables.c +++ b/app/src/main/jni/silk/src/SKP_Silk_pitch_est_tables.c @@ -1,89 +1,89 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_typedef.h" -#include "SKP_Silk_pitch_est_defines.h" - -/********************************************************/ -/* Auto Generated File from generate_pitch_est_tables.m */ -/********************************************************/ - -const SKP_int16 SKP_Silk_CB_lags_stage2[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE2_EXT] = -{ - {0, 2,-1,-1,-1, 0, 0, 1, 1, 0, 1}, - {0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, - {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0}, - {0,-1, 2, 1, 0, 1, 1, 0, 0,-1,-1} -}; - -const SKP_int16 SKP_Silk_CB_lags_stage3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX] = -{ - {-9,-7,-6,-5,-5,-4,-4,-3,-3,-2,-2,-2,-1,-1,-1, 0, 0, 0, 1, 1, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 5, 6, 8}, - {-3,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0,-1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 2, 1, 2, 2, 2, 2, 3}, - { 3, 3, 2, 2, 2, 2, 1, 2, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,-1, 0, 0,-1,-1,-1,-1,-1,-2,-2,-2}, - { 9, 8, 6, 5, 6, 5, 4, 4, 3, 3, 2, 2, 2, 1, 0, 1, 1, 0, 0, 0,-1,-1,-1,-2,-2,-2,-3,-3,-4,-4,-5,-5,-6,-7} - }; - -const SKP_int16 SKP_Silk_Lag_range_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ] [ PITCH_EST_NB_SUBFR ][ 2 ] = -{ - /* Lags to search for low number of stage3 cbks */ - { - {-2,6}, - {-1,5}, - {-1,5}, - {-2,7} - }, - /* Lags to search for middle number of stage3 cbks */ - { - {-4,8}, - {-1,6}, - {-1,6}, - {-4,9} - }, - /* Lags to search for max number of stage3 cbks */ - { - {-9,12}, - {-3,7}, - {-2,7}, - {-7,13} - } -}; - -const SKP_int16 SKP_Silk_cbk_sizes_stage3[SigProc_PITCH_EST_MAX_COMPLEX + 1] = -{ - PITCH_EST_NB_CBKS_STAGE3_MIN, - PITCH_EST_NB_CBKS_STAGE3_MID, - PITCH_EST_NB_CBKS_STAGE3_MAX -}; - -const SKP_int16 SKP_Silk_cbk_offsets_stage3[SigProc_PITCH_EST_MAX_COMPLEX + 1] = -{ - ((PITCH_EST_NB_CBKS_STAGE3_MAX - PITCH_EST_NB_CBKS_STAGE3_MIN) >> 1), - ((PITCH_EST_NB_CBKS_STAGE3_MAX - PITCH_EST_NB_CBKS_STAGE3_MID) >> 1), - 0 -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_typedef.h" +#include "SKP_Silk_pitch_est_defines.h" + +/********************************************************/ +/* Auto Generated File from generate_pitch_est_tables.m */ +/********************************************************/ + +const SKP_int16 SKP_Silk_CB_lags_stage2[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE2_EXT] = +{ + {0, 2,-1,-1,-1, 0, 0, 1, 1, 0, 1}, + {0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0}, + {0,-1, 2, 1, 0, 1, 1, 0, 0,-1,-1} +}; + +const SKP_int16 SKP_Silk_CB_lags_stage3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX] = +{ + {-9,-7,-6,-5,-5,-4,-4,-3,-3,-2,-2,-2,-1,-1,-1, 0, 0, 0, 1, 1, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 5, 6, 8}, + {-3,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0,-1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 2, 1, 2, 2, 2, 2, 3}, + { 3, 3, 2, 2, 2, 2, 1, 2, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,-1, 0, 0,-1,-1,-1,-1,-1,-2,-2,-2}, + { 9, 8, 6, 5, 6, 5, 4, 4, 3, 3, 2, 2, 2, 1, 0, 1, 1, 0, 0, 0,-1,-1,-1,-2,-2,-2,-3,-3,-4,-4,-5,-5,-6,-7} + }; + +const SKP_int16 SKP_Silk_Lag_range_stage3[ SigProc_PITCH_EST_MAX_COMPLEX + 1 ] [ PITCH_EST_NB_SUBFR ][ 2 ] = +{ + /* Lags to search for low number of stage3 cbks */ + { + {-2,6}, + {-1,5}, + {-1,5}, + {-2,7} + }, + /* Lags to search for middle number of stage3 cbks */ + { + {-4,8}, + {-1,6}, + {-1,6}, + {-4,9} + }, + /* Lags to search for max number of stage3 cbks */ + { + {-9,12}, + {-3,7}, + {-2,7}, + {-7,13} + } +}; + +const SKP_int16 SKP_Silk_cbk_sizes_stage3[SigProc_PITCH_EST_MAX_COMPLEX + 1] = +{ + PITCH_EST_NB_CBKS_STAGE3_MIN, + PITCH_EST_NB_CBKS_STAGE3_MID, + PITCH_EST_NB_CBKS_STAGE3_MAX +}; + +const SKP_int16 SKP_Silk_cbk_offsets_stage3[SigProc_PITCH_EST_MAX_COMPLEX + 1] = +{ + ((PITCH_EST_NB_CBKS_STAGE3_MAX - PITCH_EST_NB_CBKS_STAGE3_MIN) >> 1), + ((PITCH_EST_NB_CBKS_STAGE3_MAX - PITCH_EST_NB_CBKS_STAGE3_MID) >> 1), + 0 +}; + diff --git a/jni/silk/src/SKP_Silk_prefilter_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_prefilter_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_prefilter_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_prefilter_FIX.c index d4c98b6..3609cf6 100644 --- a/jni/silk/src/SKP_Silk_prefilter_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_prefilter_FIX.c @@ -1,166 +1,166 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" -#include "SKP_Silk_perceptual_parameters_FIX.h" - -/* SKP_Silk_prefilter. Prefilter for finding Quantizer input signal */ -SKP_INLINE void SKP_Silk_prefilt_FIX( - SKP_Silk_prefilter_state_FIX *P, /* I/O state */ - SKP_int32 st_res_Q12[], /* I short term residual signal */ - SKP_int16 xw[], /* O prefiltered signal */ - SKP_int32 HarmShapeFIRPacked_Q12, /* I Harmonic shaping coeficients */ - SKP_int Tilt_Q14, /* I Tilt shaping coeficient */ - SKP_int32 LF_shp_Q14, /* I Low-frequancy shaping coeficients*/ - SKP_int lag, /* I Lag for harmonic shaping */ - SKP_int length /* I Length of signals */ -); - -void SKP_Silk_prefilter_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ - const SKP_Silk_encoder_control_FIX *psEncCtrl, /* I Encoder control FIX */ - SKP_int16 xw[], /* O Weighted signal */ - const SKP_int16 x[] /* I Speech signal */ -) -{ - SKP_Silk_prefilter_state_FIX *P = &psEnc->sPrefilt; - SKP_int j, k, lag; - SKP_int32 tmp_32, B_Q12; - const SKP_int16 *AR1_shp_Q13; - const SKP_int16 *px; - SKP_int16 *pxw, *pst_res; - SKP_int HarmShapeGain_Q12, Tilt_Q14, LF_shp_Q14; - SKP_int32 HarmShapeFIRPacked_Q12; - SKP_int32 x_filt_Q12[ MAX_FRAME_LENGTH / NB_SUBFR ], filterState[ MAX_LPC_ORDER ]; - SKP_int16 st_res[ ( MAX_FRAME_LENGTH / NB_SUBFR ) + MAX_LPC_ORDER ]; - - /* Setup pointers */ - px = x; - pxw = xw; - lag = P->lagPrev; - for( k = 0; k < NB_SUBFR; k++ ) { - /* Update Variables that change per sub frame */ - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - lag = psEncCtrl->sCmn.pitchL[ k ]; - } - - /* Noise shape parameters */ - HarmShapeGain_Q12 = SKP_SMULWB( psEncCtrl->HarmShapeGain_Q14[ k ], 16384 - psEncCtrl->HarmBoost_Q14[ k ] ); - SKP_assert( HarmShapeGain_Q12 >= 0 ); - HarmShapeFIRPacked_Q12 = SKP_RSHIFT( HarmShapeGain_Q12, 2 ); - HarmShapeFIRPacked_Q12 |= SKP_LSHIFT( ( SKP_int32 )SKP_RSHIFT( HarmShapeGain_Q12, 1 ), 16 ); - Tilt_Q14 = psEncCtrl->Tilt_Q14[ k ]; - LF_shp_Q14 = psEncCtrl->LF_shp_Q14[ k ]; - AR1_shp_Q13 = &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ]; - - /* Short term FIR filtering*/ - SKP_memset( filterState, 0, psEnc->sCmn.shapingLPCOrder * sizeof( SKP_int32 ) ); - SKP_Silk_MA_Prediction_Q13( px - psEnc->sCmn.shapingLPCOrder, AR1_shp_Q13, filterState, - st_res, psEnc->sCmn.subfr_length + psEnc->sCmn.shapingLPCOrder, psEnc->sCmn.shapingLPCOrder ); - - pst_res = st_res + psEnc->sCmn.shapingLPCOrder; /* Point to first sample */ - - /* reduce (mainly) low frequencies during harmonic emphasis */ - B_Q12 = SKP_RSHIFT_ROUND( psEncCtrl->GainsPre_Q14[ k ], 2 ); - tmp_32 = SKP_SMLABB( INPUT_TILT_Q26, psEncCtrl->HarmBoost_Q14[ k ], HarmShapeGain_Q12 ); /* Q26 */ - tmp_32 = SKP_SMLABB( tmp_32, psEncCtrl->coding_quality_Q14, HIGH_RATE_INPUT_TILT_Q12 ); /* Q26 */ - tmp_32 = SKP_SMULWB( tmp_32, -psEncCtrl->GainsPre_Q14[ k ] ); /* Q24 */ - tmp_32 = SKP_RSHIFT_ROUND( tmp_32, 12 ); /* Q12 */ - B_Q12 |= SKP_LSHIFT( SKP_SAT16( tmp_32 ), 16 ); - - /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ - /* SMLABB and SMLABT instructions. On a big-endian CPU the two int16 variables would be */ - /* loaded in reverse order and the code will give the wrong result. In that case swapping */ - /* the SMLABB and SMLABT instructions should solve the problem. */ - x_filt_Q12[ 0 ] = SKP_SMLABT( SKP_SMULBB( pst_res[ 0 ], B_Q12 ), P->sHarmHP, B_Q12 ); - for( j = 1; j < psEnc->sCmn.subfr_length; j++ ) { - x_filt_Q12[ j ] = SKP_SMLABT( SKP_SMULBB( pst_res[ j ], B_Q12 ), pst_res[ j - 1 ], B_Q12 ); - } - P->sHarmHP = pst_res[ psEnc->sCmn.subfr_length - 1 ]; - - SKP_Silk_prefilt_FIX( P, x_filt_Q12, pxw, HarmShapeFIRPacked_Q12, Tilt_Q14, - LF_shp_Q14, lag, psEnc->sCmn.subfr_length ); - - px += psEnc->sCmn.subfr_length; - pxw += psEnc->sCmn.subfr_length; - } - - P->lagPrev = psEncCtrl->sCmn.pitchL[ NB_SUBFR - 1 ]; -} - -/* SKP_Silk_prefilter. Prefilter for finding Quantizer input signal */ -SKP_INLINE void SKP_Silk_prefilt_FIX( - SKP_Silk_prefilter_state_FIX *P, /* I/O state */ - SKP_int32 st_res_Q12[], /* I short term residual signal */ - SKP_int16 xw[], /* O prefiltered signal */ - SKP_int32 HarmShapeFIRPacked_Q12, /* I Harmonic shaping coeficients */ - SKP_int Tilt_Q14, /* I Tilt shaping coeficient */ - SKP_int32 LF_shp_Q14, /* I Low-frequancy shaping coeficients*/ - SKP_int lag, /* I Lag for harmonic shaping */ - SKP_int length /* I Length of signals */ -) -{ - SKP_int i, idx, LTP_shp_buf_idx; - SKP_int32 n_LTP_Q12, n_Tilt_Q10, n_LF_Q10; - SKP_int32 sLF_MA_shp_Q12, sLF_AR_shp_Q12; - SKP_int16 *LTP_shp_buf; - - /* To speed up use temp variables instead of using the struct */ - LTP_shp_buf = P->sLTP_shp1; - LTP_shp_buf_idx = P->sLTP_shp_buf_idx1; - sLF_AR_shp_Q12 = P->sLF_AR_shp1_Q12; - sLF_MA_shp_Q12 = P->sLF_MA_shp1_Q12; - - for( i = 0; i < length; i++ ) { - if( lag > 0 ) { - /* unrolled loop */ - SKP_assert( HARM_SHAPE_FIR_TAPS == 3 ); - idx = lag + LTP_shp_buf_idx; - n_LTP_Q12 = SKP_SMULBB( LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 - 1) & LTP_MASK ], HarmShapeFIRPacked_Q12 ); - n_LTP_Q12 = SKP_SMLABT( n_LTP_Q12, LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 ) & LTP_MASK ], HarmShapeFIRPacked_Q12 ); - n_LTP_Q12 = SKP_SMLABB( n_LTP_Q12, LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 + 1) & LTP_MASK ], HarmShapeFIRPacked_Q12 ); - } else { - n_LTP_Q12 = 0; - } - - n_LF_Q10 = SKP_SMLAWB( SKP_SMULWT( sLF_AR_shp_Q12, LF_shp_Q14 ), sLF_MA_shp_Q12, LF_shp_Q14 ); - n_Tilt_Q10 = SKP_SMULWB( sLF_AR_shp_Q12, Tilt_Q14 ); - - sLF_AR_shp_Q12 = SKP_SUB32( st_res_Q12[ i ], SKP_LSHIFT( n_Tilt_Q10, 2 ) ); - sLF_MA_shp_Q12 = SKP_SUB32( sLF_AR_shp_Q12, SKP_LSHIFT( n_LF_Q10, 2 ) ); - - LTP_shp_buf_idx = ( LTP_shp_buf_idx - 1 ) & LTP_MASK; - LTP_shp_buf[ LTP_shp_buf_idx ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( sLF_MA_shp_Q12, 12 ) ); - - xw[i] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SUB32( sLF_MA_shp_Q12, n_LTP_Q12 ), 12 ) ); - } - - /* Copy temp variable back to state */ - P->sLF_AR_shp1_Q12 = sLF_AR_shp_Q12; - P->sLF_MA_shp1_Q12 = sLF_MA_shp_Q12; - P->sLTP_shp_buf_idx1 = LTP_shp_buf_idx; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" +#include "SKP_Silk_perceptual_parameters_FIX.h" + +/* SKP_Silk_prefilter. Prefilter for finding Quantizer input signal */ +SKP_INLINE void SKP_Silk_prefilt_FIX( + SKP_Silk_prefilter_state_FIX *P, /* I/O state */ + SKP_int32 st_res_Q12[], /* I short term residual signal */ + SKP_int16 xw[], /* O prefiltered signal */ + SKP_int32 HarmShapeFIRPacked_Q12, /* I Harmonic shaping coeficients */ + SKP_int Tilt_Q14, /* I Tilt shaping coeficient */ + SKP_int32 LF_shp_Q14, /* I Low-frequancy shaping coeficients*/ + SKP_int lag, /* I Lag for harmonic shaping */ + SKP_int length /* I Length of signals */ +); + +void SKP_Silk_prefilter_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ + const SKP_Silk_encoder_control_FIX *psEncCtrl, /* I Encoder control FIX */ + SKP_int16 xw[], /* O Weighted signal */ + const SKP_int16 x[] /* I Speech signal */ +) +{ + SKP_Silk_prefilter_state_FIX *P = &psEnc->sPrefilt; + SKP_int j, k, lag; + SKP_int32 tmp_32, B_Q12; + const SKP_int16 *AR1_shp_Q13; + const SKP_int16 *px; + SKP_int16 *pxw, *pst_res; + SKP_int HarmShapeGain_Q12, Tilt_Q14, LF_shp_Q14; + SKP_int32 HarmShapeFIRPacked_Q12; + SKP_int32 x_filt_Q12[ MAX_FRAME_LENGTH / NB_SUBFR ], filterState[ MAX_LPC_ORDER ]; + SKP_int16 st_res[ ( MAX_FRAME_LENGTH / NB_SUBFR ) + MAX_LPC_ORDER ]; + + /* Setup pointers */ + px = x; + pxw = xw; + lag = P->lagPrev; + for( k = 0; k < NB_SUBFR; k++ ) { + /* Update Variables that change per sub frame */ + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + lag = psEncCtrl->sCmn.pitchL[ k ]; + } + + /* Noise shape parameters */ + HarmShapeGain_Q12 = SKP_SMULWB( psEncCtrl->HarmShapeGain_Q14[ k ], 16384 - psEncCtrl->HarmBoost_Q14[ k ] ); + SKP_assert( HarmShapeGain_Q12 >= 0 ); + HarmShapeFIRPacked_Q12 = SKP_RSHIFT( HarmShapeGain_Q12, 2 ); + HarmShapeFIRPacked_Q12 |= SKP_LSHIFT( ( SKP_int32 )SKP_RSHIFT( HarmShapeGain_Q12, 1 ), 16 ); + Tilt_Q14 = psEncCtrl->Tilt_Q14[ k ]; + LF_shp_Q14 = psEncCtrl->LF_shp_Q14[ k ]; + AR1_shp_Q13 = &psEncCtrl->AR1_Q13[ k * SHAPE_LPC_ORDER_MAX ]; + + /* Short term FIR filtering*/ + SKP_memset( filterState, 0, psEnc->sCmn.shapingLPCOrder * sizeof( SKP_int32 ) ); + SKP_Silk_MA_Prediction_Q13( px - psEnc->sCmn.shapingLPCOrder, AR1_shp_Q13, filterState, + st_res, psEnc->sCmn.subfr_length + psEnc->sCmn.shapingLPCOrder, psEnc->sCmn.shapingLPCOrder ); + + pst_res = st_res + psEnc->sCmn.shapingLPCOrder; /* Point to first sample */ + + /* reduce (mainly) low frequencies during harmonic emphasis */ + B_Q12 = SKP_RSHIFT_ROUND( psEncCtrl->GainsPre_Q14[ k ], 2 ); + tmp_32 = SKP_SMLABB( INPUT_TILT_Q26, psEncCtrl->HarmBoost_Q14[ k ], HarmShapeGain_Q12 ); /* Q26 */ + tmp_32 = SKP_SMLABB( tmp_32, psEncCtrl->coding_quality_Q14, HIGH_RATE_INPUT_TILT_Q12 ); /* Q26 */ + tmp_32 = SKP_SMULWB( tmp_32, -psEncCtrl->GainsPre_Q14[ k ] ); /* Q24 */ + tmp_32 = SKP_RSHIFT_ROUND( tmp_32, 12 ); /* Q12 */ + B_Q12 |= SKP_LSHIFT( SKP_SAT16( tmp_32 ), 16 ); + + /* NOTE: the code below loads two int16 values in an int32, and multiplies each using the */ + /* SMLABB and SMLABT instructions. On a big-endian CPU the two int16 variables would be */ + /* loaded in reverse order and the code will give the wrong result. In that case swapping */ + /* the SMLABB and SMLABT instructions should solve the problem. */ + x_filt_Q12[ 0 ] = SKP_SMLABT( SKP_SMULBB( pst_res[ 0 ], B_Q12 ), P->sHarmHP, B_Q12 ); + for( j = 1; j < psEnc->sCmn.subfr_length; j++ ) { + x_filt_Q12[ j ] = SKP_SMLABT( SKP_SMULBB( pst_res[ j ], B_Q12 ), pst_res[ j - 1 ], B_Q12 ); + } + P->sHarmHP = pst_res[ psEnc->sCmn.subfr_length - 1 ]; + + SKP_Silk_prefilt_FIX( P, x_filt_Q12, pxw, HarmShapeFIRPacked_Q12, Tilt_Q14, + LF_shp_Q14, lag, psEnc->sCmn.subfr_length ); + + px += psEnc->sCmn.subfr_length; + pxw += psEnc->sCmn.subfr_length; + } + + P->lagPrev = psEncCtrl->sCmn.pitchL[ NB_SUBFR - 1 ]; +} + +/* SKP_Silk_prefilter. Prefilter for finding Quantizer input signal */ +SKP_INLINE void SKP_Silk_prefilt_FIX( + SKP_Silk_prefilter_state_FIX *P, /* I/O state */ + SKP_int32 st_res_Q12[], /* I short term residual signal */ + SKP_int16 xw[], /* O prefiltered signal */ + SKP_int32 HarmShapeFIRPacked_Q12, /* I Harmonic shaping coeficients */ + SKP_int Tilt_Q14, /* I Tilt shaping coeficient */ + SKP_int32 LF_shp_Q14, /* I Low-frequancy shaping coeficients*/ + SKP_int lag, /* I Lag for harmonic shaping */ + SKP_int length /* I Length of signals */ +) +{ + SKP_int i, idx, LTP_shp_buf_idx; + SKP_int32 n_LTP_Q12, n_Tilt_Q10, n_LF_Q10; + SKP_int32 sLF_MA_shp_Q12, sLF_AR_shp_Q12; + SKP_int16 *LTP_shp_buf; + + /* To speed up use temp variables instead of using the struct */ + LTP_shp_buf = P->sLTP_shp1; + LTP_shp_buf_idx = P->sLTP_shp_buf_idx1; + sLF_AR_shp_Q12 = P->sLF_AR_shp1_Q12; + sLF_MA_shp_Q12 = P->sLF_MA_shp1_Q12; + + for( i = 0; i < length; i++ ) { + if( lag > 0 ) { + /* unrolled loop */ + SKP_assert( HARM_SHAPE_FIR_TAPS == 3 ); + idx = lag + LTP_shp_buf_idx; + n_LTP_Q12 = SKP_SMULBB( LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 - 1) & LTP_MASK ], HarmShapeFIRPacked_Q12 ); + n_LTP_Q12 = SKP_SMLABT( n_LTP_Q12, LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 ) & LTP_MASK ], HarmShapeFIRPacked_Q12 ); + n_LTP_Q12 = SKP_SMLABB( n_LTP_Q12, LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 + 1) & LTP_MASK ], HarmShapeFIRPacked_Q12 ); + } else { + n_LTP_Q12 = 0; + } + + n_LF_Q10 = SKP_SMLAWB( SKP_SMULWT( sLF_AR_shp_Q12, LF_shp_Q14 ), sLF_MA_shp_Q12, LF_shp_Q14 ); + n_Tilt_Q10 = SKP_SMULWB( sLF_AR_shp_Q12, Tilt_Q14 ); + + sLF_AR_shp_Q12 = SKP_SUB32( st_res_Q12[ i ], SKP_LSHIFT( n_Tilt_Q10, 2 ) ); + sLF_MA_shp_Q12 = SKP_SUB32( sLF_AR_shp_Q12, SKP_LSHIFT( n_LF_Q10, 2 ) ); + + LTP_shp_buf_idx = ( LTP_shp_buf_idx - 1 ) & LTP_MASK; + LTP_shp_buf[ LTP_shp_buf_idx ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( sLF_MA_shp_Q12, 12 ) ); + + xw[i] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( SKP_SUB32( sLF_MA_shp_Q12, n_LTP_Q12 ), 12 ) ); + } + + /* Copy temp variable back to state */ + P->sLF_AR_shp1_Q12 = sLF_AR_shp_Q12; + P->sLF_MA_shp1_Q12 = sLF_MA_shp_Q12; + P->sLTP_shp_buf_idx1 = LTP_shp_buf_idx; +} diff --git a/jni/silk/src/SKP_Silk_process_NLSFs_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_process_NLSFs_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_process_NLSFs_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_process_NLSFs_FIX.c index 6bff532..2451e6b 100644 --- a/jni/silk/src/SKP_Silk_process_NLSFs_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_process_NLSFs_FIX.c @@ -1,127 +1,127 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Limit, stabilize, convert and quantize NLSFs. */ -void SKP_Silk_process_NLSFs_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ - SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */ - SKP_int *pNLSF_Q15 /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ -) -{ - SKP_int doInterpolate; - SKP_int pNLSFW_Q6[ MAX_LPC_ORDER ]; - SKP_int NLSF_mu_Q15, NLSF_mu_fluc_red_Q16; - SKP_int32 i_sqr_Q15; - const SKP_Silk_NLSF_CB_struct *psNLSF_CB; - - /* Used only for NLSF interpolation */ - SKP_int pNLSF0_temp_Q15[ MAX_LPC_ORDER ]; - SKP_int pNLSFW0_temp_Q6[ MAX_LPC_ORDER ]; - SKP_int i; - - SKP_assert( psEnc->speech_activity_Q8 >= 0 ); - SKP_assert( psEnc->speech_activity_Q8 <= 256 ); - SKP_assert( psEncCtrl->sparseness_Q8 >= 0 ); - SKP_assert( psEncCtrl->sparseness_Q8 <= 256 ); - SKP_assert( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED || psEncCtrl->sCmn.sigtype == SIG_TYPE_UNVOICED ); - - /***********************/ - /* Calculate mu values */ - /***********************/ - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /* NLSF_mu = 0.002f - 0.001f * psEnc->speech_activity; */ - /* NLSF_mu_fluc_red = 0.1f - 0.05f * psEnc->speech_activity; */ - NLSF_mu_Q15 = SKP_SMLAWB( 66, -8388, psEnc->speech_activity_Q8 ); - NLSF_mu_fluc_red_Q16 = SKP_SMLAWB( 6554, -838848, psEnc->speech_activity_Q8 ); - } else { - /* NLSF_mu = 0.005f - 0.004f * psEnc->speech_activity; */ - /* NLSF_mu_fluc_red = 0.2f - 0.1f * psEnc->speech_activity - 0.1f * psEncCtrl->sparseness; */ - NLSF_mu_Q15 = SKP_SMLAWB( 164, -33554, psEnc->speech_activity_Q8 ); - NLSF_mu_fluc_red_Q16 = SKP_SMLAWB( 13107, -1677696, psEnc->speech_activity_Q8 + psEncCtrl->sparseness_Q8 ); - } - SKP_assert( NLSF_mu_Q15 >= 0 ); - SKP_assert( NLSF_mu_Q15 <= 164 ); - SKP_assert( NLSF_mu_fluc_red_Q16 >= 0 ); - SKP_assert( NLSF_mu_fluc_red_Q16 <= 13107 ); - - NLSF_mu_Q15 = SKP_max( NLSF_mu_Q15, 1 ); - - /* Calculate NLSF weights */ - TIC(NLSF_weights_FIX) - SKP_Silk_NLSF_VQ_weights_laroia( pNLSFW_Q6, pNLSF_Q15, psEnc->sCmn.predictLPCOrder ); - TOC(NLSF_weights_FIX) - - /* Update NLSF weights for interpolated NLSFs */ - doInterpolate = ( psEnc->sCmn.useInterpolatedNLSFs == 1 ) && ( psEncCtrl->sCmn.NLSFInterpCoef_Q2 < ( 1 << 2 ) ); - if( doInterpolate ) { - - /* Calculate the interpolated NLSF vector for the first half */ - SKP_Silk_interpolate( pNLSF0_temp_Q15, psEnc->sPred.prev_NLSFq_Q15, pNLSF_Q15, - psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEnc->sCmn.predictLPCOrder ); - - /* Calculate first half NLSF weights for the interpolated NLSFs */ - TIC(NLSF_weights_FIX) - SKP_Silk_NLSF_VQ_weights_laroia( pNLSFW0_temp_Q6, pNLSF0_temp_Q15, psEnc->sCmn.predictLPCOrder ); - TOC(NLSF_weights_FIX) - - /* Update NLSF weights with contribution from first half */ - i_sqr_Q15 = SKP_LSHIFT( SKP_SMULBB( psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->sCmn.NLSFInterpCoef_Q2 ), 11 ); - for( i = 0; i < psEnc->sCmn.predictLPCOrder; i++ ) { - pNLSFW_Q6[ i ] = SKP_SMLAWB( SKP_RSHIFT( pNLSFW_Q6[ i ], 1 ), pNLSFW0_temp_Q6[ i ], i_sqr_Q15 ); - SKP_assert( pNLSFW_Q6[ i ] <= SKP_int16_MAX ); - SKP_assert( pNLSFW_Q6[ i ] >= 1 ); - } - } - - /* Set pointer to the NLSF codebook for the current signal type and LPC order */ - psNLSF_CB = psEnc->sCmn.psNLSF_CB[ psEncCtrl->sCmn.sigtype ]; - - /* Quantize NLSF parameters given the trained NLSF codebooks */ - TIC(MSVQ_encode_FIX) - SKP_Silk_NLSF_MSVQ_encode_FIX( psEncCtrl->sCmn.NLSFIndices, pNLSF_Q15, psNLSF_CB, - psEnc->sPred.prev_NLSFq_Q15, pNLSFW_Q6, NLSF_mu_Q15, NLSF_mu_fluc_red_Q16, - psEnc->sCmn.NLSF_MSVQ_Survivors, psEnc->sCmn.predictLPCOrder, psEnc->sCmn.first_frame_after_reset ); - TOC(MSVQ_encode_FIX) - - /* Convert quantized NLSFs back to LPC coefficients */ - SKP_Silk_NLSF2A_stable( psEncCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psEnc->sCmn.predictLPCOrder ); - - if( doInterpolate ) { - /* Calculate the interpolated, quantized LSF vector for the first half */ - SKP_Silk_interpolate( pNLSF0_temp_Q15, psEnc->sPred.prev_NLSFq_Q15, pNLSF_Q15, - psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEnc->sCmn.predictLPCOrder ); - - /* Convert back to LPC coefficients */ - SKP_Silk_NLSF2A_stable( psEncCtrl->PredCoef_Q12[ 0 ], pNLSF0_temp_Q15, psEnc->sCmn.predictLPCOrder ); - - } else { - /* Copy LPC coefficients for first half from second half */ - SKP_memcpy( psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->PredCoef_Q12[ 1 ], psEnc->sCmn.predictLPCOrder * sizeof( SKP_int16 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Limit, stabilize, convert and quantize NLSFs. */ +void SKP_Silk_process_NLSFs_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */ + SKP_Silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */ + SKP_int *pNLSF_Q15 /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ +) +{ + SKP_int doInterpolate; + SKP_int pNLSFW_Q6[ MAX_LPC_ORDER ]; + SKP_int NLSF_mu_Q15, NLSF_mu_fluc_red_Q16; + SKP_int32 i_sqr_Q15; + const SKP_Silk_NLSF_CB_struct *psNLSF_CB; + + /* Used only for NLSF interpolation */ + SKP_int pNLSF0_temp_Q15[ MAX_LPC_ORDER ]; + SKP_int pNLSFW0_temp_Q6[ MAX_LPC_ORDER ]; + SKP_int i; + + SKP_assert( psEnc->speech_activity_Q8 >= 0 ); + SKP_assert( psEnc->speech_activity_Q8 <= 256 ); + SKP_assert( psEncCtrl->sparseness_Q8 >= 0 ); + SKP_assert( psEncCtrl->sparseness_Q8 <= 256 ); + SKP_assert( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED || psEncCtrl->sCmn.sigtype == SIG_TYPE_UNVOICED ); + + /***********************/ + /* Calculate mu values */ + /***********************/ + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /* NLSF_mu = 0.002f - 0.001f * psEnc->speech_activity; */ + /* NLSF_mu_fluc_red = 0.1f - 0.05f * psEnc->speech_activity; */ + NLSF_mu_Q15 = SKP_SMLAWB( 66, -8388, psEnc->speech_activity_Q8 ); + NLSF_mu_fluc_red_Q16 = SKP_SMLAWB( 6554, -838848, psEnc->speech_activity_Q8 ); + } else { + /* NLSF_mu = 0.005f - 0.004f * psEnc->speech_activity; */ + /* NLSF_mu_fluc_red = 0.2f - 0.1f * psEnc->speech_activity - 0.1f * psEncCtrl->sparseness; */ + NLSF_mu_Q15 = SKP_SMLAWB( 164, -33554, psEnc->speech_activity_Q8 ); + NLSF_mu_fluc_red_Q16 = SKP_SMLAWB( 13107, -1677696, psEnc->speech_activity_Q8 + psEncCtrl->sparseness_Q8 ); + } + SKP_assert( NLSF_mu_Q15 >= 0 ); + SKP_assert( NLSF_mu_Q15 <= 164 ); + SKP_assert( NLSF_mu_fluc_red_Q16 >= 0 ); + SKP_assert( NLSF_mu_fluc_red_Q16 <= 13107 ); + + NLSF_mu_Q15 = SKP_max( NLSF_mu_Q15, 1 ); + + /* Calculate NLSF weights */ + TIC(NLSF_weights_FIX) + SKP_Silk_NLSF_VQ_weights_laroia( pNLSFW_Q6, pNLSF_Q15, psEnc->sCmn.predictLPCOrder ); + TOC(NLSF_weights_FIX) + + /* Update NLSF weights for interpolated NLSFs */ + doInterpolate = ( psEnc->sCmn.useInterpolatedNLSFs == 1 ) && ( psEncCtrl->sCmn.NLSFInterpCoef_Q2 < ( 1 << 2 ) ); + if( doInterpolate ) { + + /* Calculate the interpolated NLSF vector for the first half */ + SKP_Silk_interpolate( pNLSF0_temp_Q15, psEnc->sPred.prev_NLSFq_Q15, pNLSF_Q15, + psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEnc->sCmn.predictLPCOrder ); + + /* Calculate first half NLSF weights for the interpolated NLSFs */ + TIC(NLSF_weights_FIX) + SKP_Silk_NLSF_VQ_weights_laroia( pNLSFW0_temp_Q6, pNLSF0_temp_Q15, psEnc->sCmn.predictLPCOrder ); + TOC(NLSF_weights_FIX) + + /* Update NLSF weights with contribution from first half */ + i_sqr_Q15 = SKP_LSHIFT( SKP_SMULBB( psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->sCmn.NLSFInterpCoef_Q2 ), 11 ); + for( i = 0; i < psEnc->sCmn.predictLPCOrder; i++ ) { + pNLSFW_Q6[ i ] = SKP_SMLAWB( SKP_RSHIFT( pNLSFW_Q6[ i ], 1 ), pNLSFW0_temp_Q6[ i ], i_sqr_Q15 ); + SKP_assert( pNLSFW_Q6[ i ] <= SKP_int16_MAX ); + SKP_assert( pNLSFW_Q6[ i ] >= 1 ); + } + } + + /* Set pointer to the NLSF codebook for the current signal type and LPC order */ + psNLSF_CB = psEnc->sCmn.psNLSF_CB[ psEncCtrl->sCmn.sigtype ]; + + /* Quantize NLSF parameters given the trained NLSF codebooks */ + TIC(MSVQ_encode_FIX) + SKP_Silk_NLSF_MSVQ_encode_FIX( psEncCtrl->sCmn.NLSFIndices, pNLSF_Q15, psNLSF_CB, + psEnc->sPred.prev_NLSFq_Q15, pNLSFW_Q6, NLSF_mu_Q15, NLSF_mu_fluc_red_Q16, + psEnc->sCmn.NLSF_MSVQ_Survivors, psEnc->sCmn.predictLPCOrder, psEnc->sCmn.first_frame_after_reset ); + TOC(MSVQ_encode_FIX) + + /* Convert quantized NLSFs back to LPC coefficients */ + SKP_Silk_NLSF2A_stable( psEncCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psEnc->sCmn.predictLPCOrder ); + + if( doInterpolate ) { + /* Calculate the interpolated, quantized LSF vector for the first half */ + SKP_Silk_interpolate( pNLSF0_temp_Q15, psEnc->sPred.prev_NLSFq_Q15, pNLSF_Q15, + psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEnc->sCmn.predictLPCOrder ); + + /* Convert back to LPC coefficients */ + SKP_Silk_NLSF2A_stable( psEncCtrl->PredCoef_Q12[ 0 ], pNLSF0_temp_Q15, psEnc->sCmn.predictLPCOrder ); + + } else { + /* Copy LPC coefficients for first half from second half */ + SKP_memcpy( psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->PredCoef_Q12[ 1 ], psEnc->sCmn.predictLPCOrder * sizeof( SKP_int16 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_process_gains_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_process_gains_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_process_gains_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_process_gains_FIX.c index e6400aa..c6cdebc 100644 --- a/jni/silk/src/SKP_Silk_process_gains_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_process_gains_FIX.c @@ -1,114 +1,114 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Processing of gains */ -void SKP_Silk_process_gains_FIX( - SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state_FIX */ - SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O Encoder control_FIX */ -) -{ - SKP_Silk_shape_state_FIX *psShapeSt = &psEnc->sShape; - SKP_int k; - SKP_int32 s_Q16, InvMaxSqrVal_Q16, gain, gain_squared, ResNrg, ResNrgPart; - - /* Gain reduction when LTP coding gain is high */ - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - /*s = -0.5f * SKP_sigmoid( 0.25f * ( psEncCtrl->LTPredCodGain - 12.0f ) ); */ - s_Q16 = -SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->LTPredCodGain_Q7 - (12 << 7), 4 ) ); - for( k = 0; k < NB_SUBFR; k++ ) { - psEncCtrl->Gains_Q16[ k ] = SKP_SMLAWB( psEncCtrl->Gains_Q16[ k ], psEncCtrl->Gains_Q16[ k ], s_Q16 ); - } - } - - /* Limit the quantized signal */ - /* 69 = 21.0f + 16/0.33 */ - InvMaxSqrVal_Q16 = SKP_DIV32_16( SKP_Silk_log2lin( - SKP_SMULWB( (69 << 7) - psEncCtrl->current_SNR_dB_Q7, SKP_FIX_CONST( 0.33, 16 )) ), psEnc->sCmn.subfr_length ); - - for( k = 0; k < NB_SUBFR; k++ ) { - /* Soft limit on ratio residual energy and squared gains */ - ResNrg = psEncCtrl->ResNrg[ k ]; - ResNrgPart = SKP_SMULWW( ResNrg, InvMaxSqrVal_Q16 ); - if( psEncCtrl->ResNrgQ[ k ] > 0 ) { - if( psEncCtrl->ResNrgQ[ k ] < 32 ) { - ResNrgPart = SKP_RSHIFT_ROUND( ResNrgPart, psEncCtrl->ResNrgQ[ k ] ); - } else { - ResNrgPart = 0; - } - } else if( psEncCtrl->ResNrgQ[k] != 0 ) { - if( ResNrgPart > SKP_RSHIFT( SKP_int32_MAX, -psEncCtrl->ResNrgQ[ k ] ) ) { - ResNrgPart = SKP_int32_MAX; - } else { - ResNrgPart = SKP_LSHIFT( ResNrgPart, -psEncCtrl->ResNrgQ[ k ] ); - } - } - gain = psEncCtrl->Gains_Q16[ k ]; - gain_squared = SKP_ADD_SAT32( ResNrgPart, SKP_SMMUL( gain, gain ) ); - if( gain_squared < SKP_int16_MAX ) { - /* recalculate with higher precision */ - gain_squared = SKP_SMLAWW( SKP_LSHIFT( ResNrgPart, 16 ), gain, gain ); - SKP_assert( gain_squared > 0 ); - gain = SKP_Silk_SQRT_APPROX( gain_squared ); /* Q8 */ - psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 8 ); /* Q16 */ - } else { - gain = SKP_Silk_SQRT_APPROX( gain_squared ); /* Q0 */ - psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 16 ); /* Q16 */ - } - } - - /* Noise shaping quantization */ - SKP_Silk_gains_quant( psEncCtrl->sCmn.GainsIndices, psEncCtrl->Gains_Q16, - &psShapeSt->LastGainIndex, psEnc->sCmn.nFramesInPayloadBuf ); - /* Set quantizer offset for voiced signals. Larger offset when LTP coding gain is low or tilt is high (ie low-pass) */ - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - if( psEncCtrl->LTPredCodGain_Q7 + SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) > ( 1 << 7 ) ) { - psEncCtrl->sCmn.QuantOffsetType = 0; - } else { - psEncCtrl->sCmn.QuantOffsetType = 1; - } - } - - /* Quantizer boundary adjustment */ - if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { - psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( 1.3, 10 ) - - SKP_SMULWB( SKP_FIX_CONST( 0.5, 18 ), psEnc->speech_activity_Q8 ) - - SKP_SMULWB( SKP_FIX_CONST( 0.3, 12 ), psEncCtrl->input_quality_Q14 ) - + SKP_SMULBB( SKP_FIX_CONST( 0.2, 10 ), psEncCtrl->sCmn.QuantOffsetType ) - - SKP_SMULWB( SKP_FIX_CONST( 0.1, 12 ), psEncCtrl->coding_quality_Q14 ); - } else { - psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( 1.3, 10 ) - - SKP_SMULWB( SKP_FIX_CONST( 0.5, 18 ), psEnc->speech_activity_Q8 ) - - SKP_SMULWB( SKP_FIX_CONST( 0.4, 12 ), psEncCtrl->input_quality_Q14 ) - + SKP_SMULBB( SKP_FIX_CONST( 0.4, 10 ), psEncCtrl->sCmn.QuantOffsetType ) - - SKP_SMULWB( SKP_FIX_CONST( 0.1, 12 ), psEncCtrl->coding_quality_Q14 ); - } - SKP_assert( psEncCtrl->Lambda_Q10 >= 0 ); - SKP_assert( psEncCtrl->Lambda_Q10 < SKP_FIX_CONST( 2, 10 ) ); - -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Processing of gains */ +void SKP_Silk_process_gains_FIX( + SKP_Silk_encoder_state_FIX *psEnc, /* I/O Encoder state_FIX */ + SKP_Silk_encoder_control_FIX *psEncCtrl /* I/O Encoder control_FIX */ +) +{ + SKP_Silk_shape_state_FIX *psShapeSt = &psEnc->sShape; + SKP_int k; + SKP_int32 s_Q16, InvMaxSqrVal_Q16, gain, gain_squared, ResNrg, ResNrgPart; + + /* Gain reduction when LTP coding gain is high */ + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + /*s = -0.5f * SKP_sigmoid( 0.25f * ( psEncCtrl->LTPredCodGain - 12.0f ) ); */ + s_Q16 = -SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->LTPredCodGain_Q7 - (12 << 7), 4 ) ); + for( k = 0; k < NB_SUBFR; k++ ) { + psEncCtrl->Gains_Q16[ k ] = SKP_SMLAWB( psEncCtrl->Gains_Q16[ k ], psEncCtrl->Gains_Q16[ k ], s_Q16 ); + } + } + + /* Limit the quantized signal */ + /* 69 = 21.0f + 16/0.33 */ + InvMaxSqrVal_Q16 = SKP_DIV32_16( SKP_Silk_log2lin( + SKP_SMULWB( (69 << 7) - psEncCtrl->current_SNR_dB_Q7, SKP_FIX_CONST( 0.33, 16 )) ), psEnc->sCmn.subfr_length ); + + for( k = 0; k < NB_SUBFR; k++ ) { + /* Soft limit on ratio residual energy and squared gains */ + ResNrg = psEncCtrl->ResNrg[ k ]; + ResNrgPart = SKP_SMULWW( ResNrg, InvMaxSqrVal_Q16 ); + if( psEncCtrl->ResNrgQ[ k ] > 0 ) { + if( psEncCtrl->ResNrgQ[ k ] < 32 ) { + ResNrgPart = SKP_RSHIFT_ROUND( ResNrgPart, psEncCtrl->ResNrgQ[ k ] ); + } else { + ResNrgPart = 0; + } + } else if( psEncCtrl->ResNrgQ[k] != 0 ) { + if( ResNrgPart > SKP_RSHIFT( SKP_int32_MAX, -psEncCtrl->ResNrgQ[ k ] ) ) { + ResNrgPart = SKP_int32_MAX; + } else { + ResNrgPart = SKP_LSHIFT( ResNrgPart, -psEncCtrl->ResNrgQ[ k ] ); + } + } + gain = psEncCtrl->Gains_Q16[ k ]; + gain_squared = SKP_ADD_SAT32( ResNrgPart, SKP_SMMUL( gain, gain ) ); + if( gain_squared < SKP_int16_MAX ) { + /* recalculate with higher precision */ + gain_squared = SKP_SMLAWW( SKP_LSHIFT( ResNrgPart, 16 ), gain, gain ); + SKP_assert( gain_squared > 0 ); + gain = SKP_Silk_SQRT_APPROX( gain_squared ); /* Q8 */ + psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 8 ); /* Q16 */ + } else { + gain = SKP_Silk_SQRT_APPROX( gain_squared ); /* Q0 */ + psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 16 ); /* Q16 */ + } + } + + /* Noise shaping quantization */ + SKP_Silk_gains_quant( psEncCtrl->sCmn.GainsIndices, psEncCtrl->Gains_Q16, + &psShapeSt->LastGainIndex, psEnc->sCmn.nFramesInPayloadBuf ); + /* Set quantizer offset for voiced signals. Larger offset when LTP coding gain is low or tilt is high (ie low-pass) */ + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + if( psEncCtrl->LTPredCodGain_Q7 + SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) > ( 1 << 7 ) ) { + psEncCtrl->sCmn.QuantOffsetType = 0; + } else { + psEncCtrl->sCmn.QuantOffsetType = 1; + } + } + + /* Quantizer boundary adjustment */ + if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) { + psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( 1.3, 10 ) + - SKP_SMULWB( SKP_FIX_CONST( 0.5, 18 ), psEnc->speech_activity_Q8 ) + - SKP_SMULWB( SKP_FIX_CONST( 0.3, 12 ), psEncCtrl->input_quality_Q14 ) + + SKP_SMULBB( SKP_FIX_CONST( 0.2, 10 ), psEncCtrl->sCmn.QuantOffsetType ) + - SKP_SMULWB( SKP_FIX_CONST( 0.1, 12 ), psEncCtrl->coding_quality_Q14 ); + } else { + psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( 1.3, 10 ) + - SKP_SMULWB( SKP_FIX_CONST( 0.5, 18 ), psEnc->speech_activity_Q8 ) + - SKP_SMULWB( SKP_FIX_CONST( 0.4, 12 ), psEncCtrl->input_quality_Q14 ) + + SKP_SMULBB( SKP_FIX_CONST( 0.4, 10 ), psEncCtrl->sCmn.QuantOffsetType ) + - SKP_SMULWB( SKP_FIX_CONST( 0.1, 12 ), psEncCtrl->coding_quality_Q14 ); + } + SKP_assert( psEncCtrl->Lambda_Q10 >= 0 ); + SKP_assert( psEncCtrl->Lambda_Q10 < SKP_FIX_CONST( 2, 10 ) ); + +} diff --git a/jni/silk/src/SKP_Silk_pulses_to_bytes.c b/app/src/main/jni/silk/src/SKP_Silk_pulses_to_bytes.c similarity index 98% rename from jni/silk/src/SKP_Silk_pulses_to_bytes.c rename to app/src/main/jni/silk/src/SKP_Silk_pulses_to_bytes.c index 85c5e7e..6849c45 100644 --- a/jni/silk/src/SKP_Silk_pulses_to_bytes.c +++ b/app/src/main/jni/silk/src/SKP_Silk_pulses_to_bytes.c @@ -1,77 +1,77 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* - * File Name: SKP_Silk_pulses_to_bytes.c - */ - -#include -#include "SKP_Silk_main.h" - -/* nBytes = sum_over_shell_blocks( POLY_FIT_0 + POLY_FIT_1 * sum_abs_val + POLY_FIT_2 * sum_abs_val^2 ) */ -#define POLY_FIT_0_Q15 12520 -#define POLY_FIT_1_Q15 15862 -#define POLY_FIT_2_Q20 -9222 // ToDo better training with - -/* Predict number of bytes used to encode q */ -SKP_int SKP_Silk_pulses_to_bytes( /* O Return value, predicted number of bytes used to encode q */ - SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ - SKP_int q[] /* I Pulse signal */ -) -{ - SKP_int i, j, iter, *q_ptr; - SKP_int32 sum_abs_val, nBytes, acc_nBytes; - /* Take the absolute value of the pulses */ - iter = psEncC->frame_length / SHELL_CODEC_FRAME_LENGTH; - - /* Calculate rate as a nonlinaer mapping of sum abs value of each Shell block */ - q_ptr = q; - acc_nBytes = 0; - for( j = 0; j < iter; j++ ) { - sum_abs_val = 0; - for(i = 0; i < SHELL_CODEC_FRAME_LENGTH; i+=4){ - sum_abs_val += SKP_abs( q_ptr[ i + 0 ] ); - sum_abs_val += SKP_abs( q_ptr[ i + 1 ] ); - sum_abs_val += SKP_abs( q_ptr[ i + 2 ] ); - sum_abs_val += SKP_abs( q_ptr[ i + 3 ] ); - } - /* Calculate nBytes used for thi sshell frame */ - nBytes = SKP_SMULWB( SKP_SMULBB( sum_abs_val, sum_abs_val ), POLY_FIT_2_Q20 ); // Q4 - nBytes = SKP_LSHIFT_SAT32( nBytes, 11 ); // Q15 - nBytes += SKP_SMULBB( sum_abs_val, POLY_FIT_1_Q15 ); // Q15 - nBytes += POLY_FIT_0_Q15; // Q15 - - acc_nBytes += nBytes; - - q_ptr += SHELL_CODEC_FRAME_LENGTH; /* update pointer */ - } - - acc_nBytes = SKP_RSHIFT_ROUND( acc_nBytes, 15 ); // Q0 - acc_nBytes = SKP_SAT16( acc_nBytes ); // just to be sure // Q0 - - return(( SKP_int )acc_nBytes); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* + * File Name: SKP_Silk_pulses_to_bytes.c + */ + +#include +#include "SKP_Silk_main.h" + +/* nBytes = sum_over_shell_blocks( POLY_FIT_0 + POLY_FIT_1 * sum_abs_val + POLY_FIT_2 * sum_abs_val^2 ) */ +#define POLY_FIT_0_Q15 12520 +#define POLY_FIT_1_Q15 15862 +#define POLY_FIT_2_Q20 -9222 // ToDo better training with + +/* Predict number of bytes used to encode q */ +SKP_int SKP_Silk_pulses_to_bytes( /* O Return value, predicted number of bytes used to encode q */ + SKP_Silk_encoder_state *psEncC, /* I/O Encoder State */ + SKP_int q[] /* I Pulse signal */ +) +{ + SKP_int i, j, iter, *q_ptr; + SKP_int32 sum_abs_val, nBytes, acc_nBytes; + /* Take the absolute value of the pulses */ + iter = psEncC->frame_length / SHELL_CODEC_FRAME_LENGTH; + + /* Calculate rate as a nonlinaer mapping of sum abs value of each Shell block */ + q_ptr = q; + acc_nBytes = 0; + for( j = 0; j < iter; j++ ) { + sum_abs_val = 0; + for(i = 0; i < SHELL_CODEC_FRAME_LENGTH; i+=4){ + sum_abs_val += SKP_abs( q_ptr[ i + 0 ] ); + sum_abs_val += SKP_abs( q_ptr[ i + 1 ] ); + sum_abs_val += SKP_abs( q_ptr[ i + 2 ] ); + sum_abs_val += SKP_abs( q_ptr[ i + 3 ] ); + } + /* Calculate nBytes used for thi sshell frame */ + nBytes = SKP_SMULWB( SKP_SMULBB( sum_abs_val, sum_abs_val ), POLY_FIT_2_Q20 ); // Q4 + nBytes = SKP_LSHIFT_SAT32( nBytes, 11 ); // Q15 + nBytes += SKP_SMULBB( sum_abs_val, POLY_FIT_1_Q15 ); // Q15 + nBytes += POLY_FIT_0_Q15; // Q15 + + acc_nBytes += nBytes; + + q_ptr += SHELL_CODEC_FRAME_LENGTH; /* update pointer */ + } + + acc_nBytes = SKP_RSHIFT_ROUND( acc_nBytes, 15 ); // Q0 + acc_nBytes = SKP_SAT16( acc_nBytes ); // just to be sure // Q0 + + return(( SKP_int )acc_nBytes); +} diff --git a/jni/silk/src/SKP_Silk_quant_LTP_gains_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_quant_LTP_gains_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_quant_LTP_gains_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_quant_LTP_gains_FIX.c index 63c5a0f..14928b7 100644 --- a/jni/silk/src/SKP_Silk_quant_LTP_gains_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_quant_LTP_gains_FIX.c @@ -1,106 +1,106 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -void SKP_Silk_quant_LTP_gains_FIX( - SKP_int16 B_Q14[], /* I/O (un)quantized LTP gains */ - SKP_int cbk_index[], /* O Codebook Index */ - SKP_int *periodicity_index, /* O Periodicity Index */ - const SKP_int32 W_Q18[], /* I Error Weights in Q18 */ - SKP_int mu_Q8, /* I Mu value (R/D tradeoff) */ - SKP_int lowComplexity /* I Flag for low complexity */ -) -{ - SKP_int j, k, temp_idx[ NB_SUBFR ], cbk_size; - const SKP_uint16 *cdf_ptr; - const SKP_int16 *cl_ptr; - const SKP_int16 *cbk_ptr_Q14; - const SKP_int16 *b_Q14_ptr; - const SKP_int32 *W_Q18_ptr; - SKP_int32 rate_dist_subfr, rate_dist, min_rate_dist; - - - - /***************************************************/ - /* iterate over different codebooks with different */ - /* rates/distortions, and choose best */ - /***************************************************/ - min_rate_dist = SKP_int32_MAX; - for( k = 0; k < 3; k++ ) { - cdf_ptr = SKP_Silk_LTP_gain_CDF_ptrs[ k ]; - cl_ptr = SKP_Silk_LTP_gain_BITS_Q6_ptrs[ k ]; - cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ k ]; - cbk_size = SKP_Silk_LTP_vq_sizes[ k ]; - - /* Setup pointer to first subframe */ - W_Q18_ptr = W_Q18; - b_Q14_ptr = B_Q14; - - rate_dist = 0; - for( j = 0; j < NB_SUBFR; j++ ) { - - SKP_Silk_VQ_WMat_EC_FIX( - &temp_idx[ j ], /* O index of best codebook vector */ - &rate_dist_subfr, /* O best weighted quantization error + mu * rate */ - b_Q14_ptr, /* I input vector to be quantized */ - W_Q18_ptr, /* I weighting matrix */ - cbk_ptr_Q14, /* I codebook */ - cl_ptr, /* I code length for each codebook vector */ - mu_Q8, /* I tradeoff between weighted error and rate */ - cbk_size /* I number of vectors in codebook */ - ); - - rate_dist = SKP_ADD_POS_SAT32( rate_dist, rate_dist_subfr ); - - b_Q14_ptr += LTP_ORDER; - W_Q18_ptr += LTP_ORDER * LTP_ORDER; - } - - /* Avoid never finding a codebook */ - rate_dist = SKP_min( SKP_int32_MAX - 1, rate_dist ); - - if( rate_dist < min_rate_dist ) { - min_rate_dist = rate_dist; - SKP_memcpy( cbk_index, temp_idx, NB_SUBFR * sizeof( SKP_int ) ); - *periodicity_index = k; - } - - /* Break early in low-complexity mode if rate distortion is below threshold */ - if( lowComplexity && ( rate_dist < SKP_Silk_LTP_gain_middle_avg_RD_Q14 ) ) { - break; - } - } - - cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ *periodicity_index ]; - for( j = 0; j < NB_SUBFR; j++ ) { - for( k = 0; k < LTP_ORDER; k++ ) { - B_Q14[ j * LTP_ORDER + k ] = cbk_ptr_Q14[ SKP_MLA( k, cbk_index[ j ], LTP_ORDER ) ]; - } - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +void SKP_Silk_quant_LTP_gains_FIX( + SKP_int16 B_Q14[], /* I/O (un)quantized LTP gains */ + SKP_int cbk_index[], /* O Codebook Index */ + SKP_int *periodicity_index, /* O Periodicity Index */ + const SKP_int32 W_Q18[], /* I Error Weights in Q18 */ + SKP_int mu_Q8, /* I Mu value (R/D tradeoff) */ + SKP_int lowComplexity /* I Flag for low complexity */ +) +{ + SKP_int j, k, temp_idx[ NB_SUBFR ], cbk_size; + const SKP_uint16 *cdf_ptr; + const SKP_int16 *cl_ptr; + const SKP_int16 *cbk_ptr_Q14; + const SKP_int16 *b_Q14_ptr; + const SKP_int32 *W_Q18_ptr; + SKP_int32 rate_dist_subfr, rate_dist, min_rate_dist; + + + + /***************************************************/ + /* iterate over different codebooks with different */ + /* rates/distortions, and choose best */ + /***************************************************/ + min_rate_dist = SKP_int32_MAX; + for( k = 0; k < 3; k++ ) { + cdf_ptr = SKP_Silk_LTP_gain_CDF_ptrs[ k ]; + cl_ptr = SKP_Silk_LTP_gain_BITS_Q6_ptrs[ k ]; + cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ k ]; + cbk_size = SKP_Silk_LTP_vq_sizes[ k ]; + + /* Setup pointer to first subframe */ + W_Q18_ptr = W_Q18; + b_Q14_ptr = B_Q14; + + rate_dist = 0; + for( j = 0; j < NB_SUBFR; j++ ) { + + SKP_Silk_VQ_WMat_EC_FIX( + &temp_idx[ j ], /* O index of best codebook vector */ + &rate_dist_subfr, /* O best weighted quantization error + mu * rate */ + b_Q14_ptr, /* I input vector to be quantized */ + W_Q18_ptr, /* I weighting matrix */ + cbk_ptr_Q14, /* I codebook */ + cl_ptr, /* I code length for each codebook vector */ + mu_Q8, /* I tradeoff between weighted error and rate */ + cbk_size /* I number of vectors in codebook */ + ); + + rate_dist = SKP_ADD_POS_SAT32( rate_dist, rate_dist_subfr ); + + b_Q14_ptr += LTP_ORDER; + W_Q18_ptr += LTP_ORDER * LTP_ORDER; + } + + /* Avoid never finding a codebook */ + rate_dist = SKP_min( SKP_int32_MAX - 1, rate_dist ); + + if( rate_dist < min_rate_dist ) { + min_rate_dist = rate_dist; + SKP_memcpy( cbk_index, temp_idx, NB_SUBFR * sizeof( SKP_int ) ); + *periodicity_index = k; + } + + /* Break early in low-complexity mode if rate distortion is below threshold */ + if( lowComplexity && ( rate_dist < SKP_Silk_LTP_gain_middle_avg_RD_Q14 ) ) { + break; + } + } + + cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ *periodicity_index ]; + for( j = 0; j < NB_SUBFR; j++ ) { + for( k = 0; k < LTP_ORDER; k++ ) { + B_Q14[ j * LTP_ORDER + k ] = cbk_ptr_Q14[ SKP_MLA( k, cbk_index[ j ], LTP_ORDER ) ]; + } + } +} + diff --git a/jni/silk/src/SKP_Silk_range_coder.c b/app/src/main/jni/silk/src/SKP_Silk_range_coder.c similarity index 97% rename from jni/silk/src/SKP_Silk_range_coder.c rename to app/src/main/jni/silk/src/SKP_Silk_range_coder.c index b42fa42..f522846 100644 --- a/jni/silk/src/SKP_Silk_range_coder.c +++ b/app/src/main/jni/silk/src/SKP_Silk_range_coder.c @@ -1,372 +1,372 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* Range encoder for one symbol */ -void SKP_Silk_range_encoder( - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_int data, /* I uncompressed data */ - const SKP_uint16 prob[] /* I cumulative density functions */ -) -{ - SKP_uint32 low_Q16, high_Q16; - SKP_uint32 base_tmp, range_Q32; - - /* Copy structure data */ - SKP_uint32 base_Q32 = psRC->base_Q32; - SKP_uint32 range_Q16 = psRC->range_Q16; - SKP_int32 bufferIx = psRC->bufferIx; - SKP_uint8 *buffer = psRC->buffer; - - if( psRC->error ) { - return; - } - - /* Update interval */ - low_Q16 = prob[ data ]; - high_Q16 = prob[ data + 1 ]; - base_tmp = base_Q32; /* save current base, to test for carry */ - base_Q32 += SKP_MUL_uint( range_Q16, low_Q16 ); - range_Q32 = SKP_MUL_uint( range_Q16, high_Q16 - low_Q16 ); - - /* Check for carry */ - if( base_Q32 < base_tmp ) { - /* Propagate carry in buffer */ - SKP_int bufferIx_tmp = bufferIx; - while( ( ++buffer[ --bufferIx_tmp ] ) == 0 ); - } - - /* Check normalization */ - if( range_Q32 & 0xFF000000 ) { - /* No normalization */ - range_Q16 = SKP_RSHIFT_uint( range_Q32, 16 ); - } else { - if( range_Q32 & 0xFFFF0000 ) { - /* Normalization of 8 bits shift */ - range_Q16 = SKP_RSHIFT_uint( range_Q32, 8 ); - } else { - /* Normalization of 16 bits shift */ - range_Q16 = range_Q32; - /* Make sure not to write beyond buffer */ - if( bufferIx >= psRC->bufferLength ) { - psRC->error = RANGE_CODER_WRITE_BEYOND_BUFFER; - return; - } - /* Write one byte to buffer */ - buffer[ bufferIx++ ] = (SKP_uint8)( SKP_RSHIFT_uint( base_Q32, 24 ) ); - base_Q32 = SKP_LSHIFT_ovflw( base_Q32, 8 ); - } - /* Make sure not to write beyond buffer */ - if( bufferIx >= psRC->bufferLength ) { - psRC->error = RANGE_CODER_WRITE_BEYOND_BUFFER; - return; - } - /* Write one byte to buffer */ - buffer[ bufferIx++ ] = (SKP_uint8)( SKP_RSHIFT_uint( base_Q32, 24 ) ); - base_Q32 = SKP_LSHIFT_ovflw( base_Q32, 8 ); - } - - /* Copy structure data back */ - psRC->base_Q32 = base_Q32; - psRC->range_Q16 = range_Q16; - psRC->bufferIx = bufferIx; -} - -/* Range encoder for multiple symbols */ -void SKP_Silk_range_encoder_multi( - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_int data[], /* I uncompressed data [nSymbols] */ - const SKP_uint16 * const prob[], /* I cumulative density functions */ - const SKP_int nSymbols /* I number of data symbols */ -) -{ - SKP_int k; - for( k = 0; k < nSymbols; k++ ) { - SKP_Silk_range_encoder( psRC, data[ k ], prob[ k ] ); - } -} - -/* Range decoder for one symbol */ -void SKP_Silk_range_decoder( - SKP_int data[], /* O uncompressed data */ - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_uint16 prob[], /* I cumulative density function */ - SKP_int probIx /* I initial (middle) entry of cdf */ -) -{ - SKP_uint32 low_Q16, high_Q16; - SKP_uint32 base_tmp, range_Q32; - - /* Copy structure data */ - SKP_uint32 base_Q32 = psRC->base_Q32; - SKP_uint32 range_Q16 = psRC->range_Q16; - SKP_int32 bufferIx = psRC->bufferIx; - SKP_uint8 *buffer = &psRC->buffer[ 4 ]; - - if( psRC->error ) { - /* Set output to zero */ - *data = 0; - return; - } - - high_Q16 = prob[ probIx ]; - base_tmp = SKP_MUL_uint( range_Q16, high_Q16 ); - if( base_tmp > base_Q32 ) { - while( 1 ) { - low_Q16 = prob[ --probIx ]; - base_tmp = SKP_MUL_uint( range_Q16, low_Q16 ); - if( base_tmp <= base_Q32 ) { - break; - } - high_Q16 = low_Q16; - /* Test for out of range */ - if( high_Q16 == 0 ) { - psRC->error = RANGE_CODER_CDF_OUT_OF_RANGE; - /* Set output to zero */ - *data = 0; - return; - } - } - } else { - while( 1 ) { - low_Q16 = high_Q16; - high_Q16 = prob[ ++probIx ]; - base_tmp = SKP_MUL_uint( range_Q16, high_Q16 ); - if( base_tmp > base_Q32 ) { - probIx--; - break; - } - /* Test for out of range */ - if( high_Q16 == 0xFFFF ) { - psRC->error = RANGE_CODER_CDF_OUT_OF_RANGE; - /* Set output to zero */ - *data = 0; - return; - } - } - } - *data = probIx; - base_Q32 -= SKP_MUL_uint( range_Q16, low_Q16 ); - range_Q32 = SKP_MUL_uint( range_Q16, high_Q16 - low_Q16 ); - - /* Check normalization */ - if( range_Q32 & 0xFF000000 ) { - /* No normalization */ - range_Q16 = SKP_RSHIFT_uint( range_Q32, 16 ); - } else { - if( range_Q32 & 0xFFFF0000 ) { - /* Normalization of 8 bits shift */ - range_Q16 = SKP_RSHIFT_uint( range_Q32, 8 ); - /* Check for errors */ - if( SKP_RSHIFT_uint( base_Q32, 24 ) ) { - psRC->error = RANGE_CODER_NORMALIZATION_FAILED; - /* Set output to zero */ - *data = 0; - return; - } - } else { - /* Normalization of 16 bits shift */ - range_Q16 = range_Q32; - /* Check for errors */ - if( SKP_RSHIFT( base_Q32, 16 ) ) { - psRC->error = RANGE_CODER_NORMALIZATION_FAILED; - /* Set output to zero */ - *data = 0; - return; - } - /* Update base */ - base_Q32 = SKP_LSHIFT_uint( base_Q32, 8 ); - /* Make sure not to read beyond buffer */ - if( bufferIx < psRC->bufferLength ) { - /* Read one byte from buffer */ - base_Q32 |= (SKP_uint32)buffer[ bufferIx++ ]; - } - } - /* Update base */ - base_Q32 = SKP_LSHIFT_uint( base_Q32, 8 ); - /* Make sure not to read beyond buffer */ - if( bufferIx < psRC->bufferLength ) { - /* Read one byte from buffer */ - base_Q32 |= (SKP_uint32)buffer[ bufferIx++ ]; - } - } - - /* Check for zero interval length */ - if( range_Q16 == 0 ) { - psRC->error = RANGE_CODER_ZERO_INTERVAL_WIDTH; - /* Set output to zero */ - *data = 0; - return; - } - - /* Copy structure data back */ - psRC->base_Q32 = base_Q32; - psRC->range_Q16 = range_Q16; - psRC->bufferIx = bufferIx; -} - -/* Range decoder for multiple symbols */ -void SKP_Silk_range_decoder_multi( - SKP_int data[], /* O uncompressed data [nSymbols] */ - SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ - const SKP_uint16 * const prob[], /* I cumulative density functions */ - const SKP_int probStartIx[], /* I initial (middle) entries of cdfs [nSymbols] */ - const SKP_int nSymbols /* I number of data symbols */ -) -{ - SKP_int k; - for( k = 0; k < nSymbols; k++ ) { - SKP_Silk_range_decoder( &data[ k ], psRC, prob[ k ], probStartIx[ k ] ); - } -} - -/* Initialize range encoder */ -void SKP_Silk_range_enc_init( - SKP_Silk_range_coder_state *psRC /* O compressor data structure */ -) -{ - /* Initialize structure */ - psRC->bufferLength = MAX_ARITHM_BYTES; - psRC->range_Q16 = 0x0000FFFF; - psRC->bufferIx = 0; - psRC->base_Q32 = 0; - psRC->error = 0; -} - -/* Initialize range decoder */ -void SKP_Silk_range_dec_init( - SKP_Silk_range_coder_state *psRC, /* O compressor data structure */ - const SKP_uint8 buffer[], /* I buffer for compressed data [bufferLength] */ - const SKP_int32 bufferLength /* I buffer length (in bytes) */ -) -{ - /* check input */ - if( bufferLength > MAX_ARITHM_BYTES ) { - psRC->error = RANGE_CODER_DEC_PAYLOAD_TOO_LONG; - return; - } - /* Initialize structure */ - /* Copy to internal buffer */ - SKP_memcpy( psRC->buffer, buffer, bufferLength * sizeof( SKP_uint8 ) ); - psRC->bufferLength = bufferLength; - psRC->bufferIx = 0; - psRC->base_Q32 = - SKP_LSHIFT_uint( (SKP_uint32)buffer[ 0 ], 24 ) | - SKP_LSHIFT_uint( (SKP_uint32)buffer[ 1 ], 16 ) | - SKP_LSHIFT_uint( (SKP_uint32)buffer[ 2 ], 8 ) | - (SKP_uint32)buffer[ 3 ]; - psRC->range_Q16 = 0x0000FFFF; - psRC->error = 0; -} - -/* Determine length of bitstream */ -SKP_int SKP_Silk_range_coder_get_length( /* O returns number of BITS in stream */ - const SKP_Silk_range_coder_state *psRC, /* I compressed data structure */ - SKP_int *nBytes /* O number of BYTES in stream */ -) -{ - SKP_int nBits; - - /* Number of additional bits (1..9) required to be stored to stream */ - nBits = SKP_LSHIFT( psRC->bufferIx, 3 ) + SKP_Silk_CLZ32( psRC->range_Q16 - 1 ) - 14; - - *nBytes = SKP_RSHIFT( nBits + 7, 3 ); - - /* Return number of bits in bitstream */ - return nBits; -} - -/* Write shortest uniquely decodable stream to buffer, and determine its length */ -void SKP_Silk_range_enc_wrap_up( - SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ -) -{ - SKP_int bufferIx_tmp, bits_to_store, bits_in_stream, nBytes, mask; - SKP_uint32 base_Q24; - - /* Lower limit of interval, shifted 8 bits to the right */ - base_Q24 = SKP_RSHIFT_uint( psRC->base_Q32, 8 ); - - bits_in_stream = SKP_Silk_range_coder_get_length( psRC, &nBytes ); - - /* Number of additional bits (1..9) required to be stored to stream */ - bits_to_store = bits_in_stream - SKP_LSHIFT( psRC->bufferIx, 3 ); - /* Round up to required resolution */ - base_Q24 += SKP_RSHIFT_uint( 0x00800000, bits_to_store - 1 ); - base_Q24 &= SKP_LSHIFT_ovflw( 0xFFFFFFFF, 24 - bits_to_store ); - - /* Check for carry */ - if( base_Q24 & 0x01000000 ) { - /* Propagate carry in buffer */ - bufferIx_tmp = psRC->bufferIx; - while( ( ++( psRC->buffer[ --bufferIx_tmp ] ) ) == 0 ); - } - - /* Store to stream, making sure not to write beyond buffer */ - if( psRC->bufferIx < psRC->bufferLength ) { - psRC->buffer[ psRC->bufferIx++ ] = (SKP_uint8)SKP_RSHIFT_uint( base_Q24, 16 ); - if( bits_to_store > 8 ) { - if( psRC->bufferIx < psRC->bufferLength ) { - psRC->buffer[ psRC->bufferIx++ ] = (SKP_uint8)SKP_RSHIFT_uint( base_Q24, 8 ); - } - } - } - - /* Fill up any remaining bits in the last byte with 1s */ - if( bits_in_stream & 7 ) { - mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 ); - if( nBytes - 1 < psRC->bufferLength ) { - psRC->buffer[ nBytes - 1 ] |= mask; - } - } -} - -/* Check that any remaining bits in the last byte are set to 1 */ -void SKP_Silk_range_coder_check_after_decoding( - SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ -) -{ - SKP_int bits_in_stream, nBytes, mask; - - bits_in_stream = SKP_Silk_range_coder_get_length( psRC, &nBytes ); - - /* Make sure not to read beyond buffer */ - if( nBytes - 1 >= psRC->bufferLength ) { - psRC->error = RANGE_CODER_DECODER_CHECK_FAILED; - return; - } - - /* Test any remaining bits in last byte */ - if( bits_in_stream & 7 ) { - mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 ); - if( ( psRC->buffer[ nBytes - 1 ] & mask ) != mask ) { - psRC->error = RANGE_CODER_DECODER_CHECK_FAILED; - return; - } - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* Range encoder for one symbol */ +void SKP_Silk_range_encoder( + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_int data, /* I uncompressed data */ + const SKP_uint16 prob[] /* I cumulative density functions */ +) +{ + SKP_uint32 low_Q16, high_Q16; + SKP_uint32 base_tmp, range_Q32; + + /* Copy structure data */ + SKP_uint32 base_Q32 = psRC->base_Q32; + SKP_uint32 range_Q16 = psRC->range_Q16; + SKP_int32 bufferIx = psRC->bufferIx; + SKP_uint8 *buffer = psRC->buffer; + + if( psRC->error ) { + return; + } + + /* Update interval */ + low_Q16 = prob[ data ]; + high_Q16 = prob[ data + 1 ]; + base_tmp = base_Q32; /* save current base, to test for carry */ + base_Q32 += SKP_MUL_uint( range_Q16, low_Q16 ); + range_Q32 = SKP_MUL_uint( range_Q16, high_Q16 - low_Q16 ); + + /* Check for carry */ + if( base_Q32 < base_tmp ) { + /* Propagate carry in buffer */ + SKP_int bufferIx_tmp = bufferIx; + while( ( ++buffer[ --bufferIx_tmp ] ) == 0 ); + } + + /* Check normalization */ + if( range_Q32 & 0xFF000000 ) { + /* No normalization */ + range_Q16 = SKP_RSHIFT_uint( range_Q32, 16 ); + } else { + if( range_Q32 & 0xFFFF0000 ) { + /* Normalization of 8 bits shift */ + range_Q16 = SKP_RSHIFT_uint( range_Q32, 8 ); + } else { + /* Normalization of 16 bits shift */ + range_Q16 = range_Q32; + /* Make sure not to write beyond buffer */ + if( bufferIx >= psRC->bufferLength ) { + psRC->error = RANGE_CODER_WRITE_BEYOND_BUFFER; + return; + } + /* Write one byte to buffer */ + buffer[ bufferIx++ ] = (SKP_uint8)( SKP_RSHIFT_uint( base_Q32, 24 ) ); + base_Q32 = SKP_LSHIFT_ovflw( base_Q32, 8 ); + } + /* Make sure not to write beyond buffer */ + if( bufferIx >= psRC->bufferLength ) { + psRC->error = RANGE_CODER_WRITE_BEYOND_BUFFER; + return; + } + /* Write one byte to buffer */ + buffer[ bufferIx++ ] = (SKP_uint8)( SKP_RSHIFT_uint( base_Q32, 24 ) ); + base_Q32 = SKP_LSHIFT_ovflw( base_Q32, 8 ); + } + + /* Copy structure data back */ + psRC->base_Q32 = base_Q32; + psRC->range_Q16 = range_Q16; + psRC->bufferIx = bufferIx; +} + +/* Range encoder for multiple symbols */ +void SKP_Silk_range_encoder_multi( + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_int data[], /* I uncompressed data [nSymbols] */ + const SKP_uint16 * const prob[], /* I cumulative density functions */ + const SKP_int nSymbols /* I number of data symbols */ +) +{ + SKP_int k; + for( k = 0; k < nSymbols; k++ ) { + SKP_Silk_range_encoder( psRC, data[ k ], prob[ k ] ); + } +} + +/* Range decoder for one symbol */ +void SKP_Silk_range_decoder( + SKP_int data[], /* O uncompressed data */ + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_uint16 prob[], /* I cumulative density function */ + SKP_int probIx /* I initial (middle) entry of cdf */ +) +{ + SKP_uint32 low_Q16, high_Q16; + SKP_uint32 base_tmp, range_Q32; + + /* Copy structure data */ + SKP_uint32 base_Q32 = psRC->base_Q32; + SKP_uint32 range_Q16 = psRC->range_Q16; + SKP_int32 bufferIx = psRC->bufferIx; + SKP_uint8 *buffer = &psRC->buffer[ 4 ]; + + if( psRC->error ) { + /* Set output to zero */ + *data = 0; + return; + } + + high_Q16 = prob[ probIx ]; + base_tmp = SKP_MUL_uint( range_Q16, high_Q16 ); + if( base_tmp > base_Q32 ) { + while( 1 ) { + low_Q16 = prob[ --probIx ]; + base_tmp = SKP_MUL_uint( range_Q16, low_Q16 ); + if( base_tmp <= base_Q32 ) { + break; + } + high_Q16 = low_Q16; + /* Test for out of range */ + if( high_Q16 == 0 ) { + psRC->error = RANGE_CODER_CDF_OUT_OF_RANGE; + /* Set output to zero */ + *data = 0; + return; + } + } + } else { + while( 1 ) { + low_Q16 = high_Q16; + high_Q16 = prob[ ++probIx ]; + base_tmp = SKP_MUL_uint( range_Q16, high_Q16 ); + if( base_tmp > base_Q32 ) { + probIx--; + break; + } + /* Test for out of range */ + if( high_Q16 == 0xFFFF ) { + psRC->error = RANGE_CODER_CDF_OUT_OF_RANGE; + /* Set output to zero */ + *data = 0; + return; + } + } + } + *data = probIx; + base_Q32 -= SKP_MUL_uint( range_Q16, low_Q16 ); + range_Q32 = SKP_MUL_uint( range_Q16, high_Q16 - low_Q16 ); + + /* Check normalization */ + if( range_Q32 & 0xFF000000 ) { + /* No normalization */ + range_Q16 = SKP_RSHIFT_uint( range_Q32, 16 ); + } else { + if( range_Q32 & 0xFFFF0000 ) { + /* Normalization of 8 bits shift */ + range_Q16 = SKP_RSHIFT_uint( range_Q32, 8 ); + /* Check for errors */ + if( SKP_RSHIFT_uint( base_Q32, 24 ) ) { + psRC->error = RANGE_CODER_NORMALIZATION_FAILED; + /* Set output to zero */ + *data = 0; + return; + } + } else { + /* Normalization of 16 bits shift */ + range_Q16 = range_Q32; + /* Check for errors */ + if( SKP_RSHIFT( base_Q32, 16 ) ) { + psRC->error = RANGE_CODER_NORMALIZATION_FAILED; + /* Set output to zero */ + *data = 0; + return; + } + /* Update base */ + base_Q32 = SKP_LSHIFT_uint( base_Q32, 8 ); + /* Make sure not to read beyond buffer */ + if( bufferIx < psRC->bufferLength ) { + /* Read one byte from buffer */ + base_Q32 |= (SKP_uint32)buffer[ bufferIx++ ]; + } + } + /* Update base */ + base_Q32 = SKP_LSHIFT_uint( base_Q32, 8 ); + /* Make sure not to read beyond buffer */ + if( bufferIx < psRC->bufferLength ) { + /* Read one byte from buffer */ + base_Q32 |= (SKP_uint32)buffer[ bufferIx++ ]; + } + } + + /* Check for zero interval length */ + if( range_Q16 == 0 ) { + psRC->error = RANGE_CODER_ZERO_INTERVAL_WIDTH; + /* Set output to zero */ + *data = 0; + return; + } + + /* Copy structure data back */ + psRC->base_Q32 = base_Q32; + psRC->range_Q16 = range_Q16; + psRC->bufferIx = bufferIx; +} + +/* Range decoder for multiple symbols */ +void SKP_Silk_range_decoder_multi( + SKP_int data[], /* O uncompressed data [nSymbols] */ + SKP_Silk_range_coder_state *psRC, /* I/O compressor data structure */ + const SKP_uint16 * const prob[], /* I cumulative density functions */ + const SKP_int probStartIx[], /* I initial (middle) entries of cdfs [nSymbols] */ + const SKP_int nSymbols /* I number of data symbols */ +) +{ + SKP_int k; + for( k = 0; k < nSymbols; k++ ) { + SKP_Silk_range_decoder( &data[ k ], psRC, prob[ k ], probStartIx[ k ] ); + } +} + +/* Initialize range encoder */ +void SKP_Silk_range_enc_init( + SKP_Silk_range_coder_state *psRC /* O compressor data structure */ +) +{ + /* Initialize structure */ + psRC->bufferLength = MAX_ARITHM_BYTES; + psRC->range_Q16 = 0x0000FFFF; + psRC->bufferIx = 0; + psRC->base_Q32 = 0; + psRC->error = 0; +} + +/* Initialize range decoder */ +void SKP_Silk_range_dec_init( + SKP_Silk_range_coder_state *psRC, /* O compressor data structure */ + const SKP_uint8 buffer[], /* I buffer for compressed data [bufferLength] */ + const SKP_int32 bufferLength /* I buffer length (in bytes) */ +) +{ + /* check input */ + if( bufferLength > MAX_ARITHM_BYTES ) { + psRC->error = RANGE_CODER_DEC_PAYLOAD_TOO_LONG; + return; + } + /* Initialize structure */ + /* Copy to internal buffer */ + SKP_memcpy( psRC->buffer, buffer, bufferLength * sizeof( SKP_uint8 ) ); + psRC->bufferLength = bufferLength; + psRC->bufferIx = 0; + psRC->base_Q32 = + SKP_LSHIFT_uint( (SKP_uint32)buffer[ 0 ], 24 ) | + SKP_LSHIFT_uint( (SKP_uint32)buffer[ 1 ], 16 ) | + SKP_LSHIFT_uint( (SKP_uint32)buffer[ 2 ], 8 ) | + (SKP_uint32)buffer[ 3 ]; + psRC->range_Q16 = 0x0000FFFF; + psRC->error = 0; +} + +/* Determine length of bitstream */ +SKP_int SKP_Silk_range_coder_get_length( /* O returns number of BITS in stream */ + const SKP_Silk_range_coder_state *psRC, /* I compressed data structure */ + SKP_int *nBytes /* O number of BYTES in stream */ +) +{ + SKP_int nBits; + + /* Number of additional bits (1..9) required to be stored to stream */ + nBits = SKP_LSHIFT( psRC->bufferIx, 3 ) + SKP_Silk_CLZ32( psRC->range_Q16 - 1 ) - 14; + + *nBytes = SKP_RSHIFT( nBits + 7, 3 ); + + /* Return number of bits in bitstream */ + return nBits; +} + +/* Write shortest uniquely decodable stream to buffer, and determine its length */ +void SKP_Silk_range_enc_wrap_up( + SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ +) +{ + SKP_int bufferIx_tmp, bits_to_store, bits_in_stream, nBytes, mask; + SKP_uint32 base_Q24; + + /* Lower limit of interval, shifted 8 bits to the right */ + base_Q24 = SKP_RSHIFT_uint( psRC->base_Q32, 8 ); + + bits_in_stream = SKP_Silk_range_coder_get_length( psRC, &nBytes ); + + /* Number of additional bits (1..9) required to be stored to stream */ + bits_to_store = bits_in_stream - SKP_LSHIFT( psRC->bufferIx, 3 ); + /* Round up to required resolution */ + base_Q24 += SKP_RSHIFT_uint( 0x00800000, bits_to_store - 1 ); + base_Q24 &= SKP_LSHIFT_ovflw( 0xFFFFFFFF, 24 - bits_to_store ); + + /* Check for carry */ + if( base_Q24 & 0x01000000 ) { + /* Propagate carry in buffer */ + bufferIx_tmp = psRC->bufferIx; + while( ( ++( psRC->buffer[ --bufferIx_tmp ] ) ) == 0 ); + } + + /* Store to stream, making sure not to write beyond buffer */ + if( psRC->bufferIx < psRC->bufferLength ) { + psRC->buffer[ psRC->bufferIx++ ] = (SKP_uint8)SKP_RSHIFT_uint( base_Q24, 16 ); + if( bits_to_store > 8 ) { + if( psRC->bufferIx < psRC->bufferLength ) { + psRC->buffer[ psRC->bufferIx++ ] = (SKP_uint8)SKP_RSHIFT_uint( base_Q24, 8 ); + } + } + } + + /* Fill up any remaining bits in the last byte with 1s */ + if( bits_in_stream & 7 ) { + mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 ); + if( nBytes - 1 < psRC->bufferLength ) { + psRC->buffer[ nBytes - 1 ] |= mask; + } + } +} + +/* Check that any remaining bits in the last byte are set to 1 */ +void SKP_Silk_range_coder_check_after_decoding( + SKP_Silk_range_coder_state *psRC /* I/O compressed data structure */ +) +{ + SKP_int bits_in_stream, nBytes, mask; + + bits_in_stream = SKP_Silk_range_coder_get_length( psRC, &nBytes ); + + /* Make sure not to read beyond buffer */ + if( nBytes - 1 >= psRC->bufferLength ) { + psRC->error = RANGE_CODER_DECODER_CHECK_FAILED; + return; + } + + /* Test any remaining bits in last byte */ + if( bits_in_stream & 7 ) { + mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 ); + if( ( psRC->buffer[ nBytes - 1 ] & mask ) != mask ) { + psRC->error = RANGE_CODER_DECODER_CHECK_FAILED; + return; + } + } +} diff --git a/jni/silk/src/SKP_Silk_regularize_correlations_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_regularize_correlations_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_regularize_correlations_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_regularize_correlations_FIX.c index fa2e8cd..4803fc9 100644 --- a/jni/silk/src/SKP_Silk_regularize_correlations_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_regularize_correlations_FIX.c @@ -1,43 +1,43 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Add noise to matrix diagonal */ -void SKP_Silk_regularize_correlations_FIX( - SKP_int32 *XX, /* I/O Correlation matrices */ - SKP_int32 *xx, /* I/O Correlation values */ - SKP_int32 noise, /* I Noise to add */ - SKP_int D /* I Dimension of XX */ -) -{ - SKP_int i; - for( i = 0; i < D; i++ ) { - matrix_ptr( &XX[ 0 ], i, i, D ) = SKP_ADD32( matrix_ptr( &XX[ 0 ], i, i, D ), noise ); - } - xx[ 0 ] += noise; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Add noise to matrix diagonal */ +void SKP_Silk_regularize_correlations_FIX( + SKP_int32 *XX, /* I/O Correlation matrices */ + SKP_int32 *xx, /* I/O Correlation values */ + SKP_int32 noise, /* I Noise to add */ + SKP_int D /* I Dimension of XX */ +) +{ + SKP_int i; + for( i = 0; i < D; i++ ) { + matrix_ptr( &XX[ 0 ], i, i, D ) = SKP_ADD32( matrix_ptr( &XX[ 0 ], i, i, D ), noise ); + } + xx[ 0 ] += noise; +} diff --git a/jni/silk/src/SKP_Silk_resample_1_2.c b/app/src/main/jni/silk/src/SKP_Silk_resample_1_2.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_1_2.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_1_2.c index 712e32d..a5b634b 100644 --- a/jni/silk/src/SKP_Silk_resample_1_2.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_1_2.c @@ -1,77 +1,77 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_resample_1_2 * - * * - * Downsample by a factor 2 * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Coefficients for 2-fold resampling */ -static SKP_int16 A20_Resample_1_2[ 3 ] = { 1254, 10102, 22898 }; -static SKP_int16 A21_Resample_1_2[ 3 ] = { 4810, 16371, 29374 }; - - -/* Downsample by a factor 2 */ -void SKP_Silk_resample_1_2( - const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ - SKP_int32 *S, /* I/O: State vector [6] */ - SKP_int16 *out, /* O: 8 kHz signal [len] */ - SKP_int32 *scratch, /* I: Scratch memory [4*len] */ - const SKP_int32 len /* I: Number of OUTPUT samples*/ -) -{ - SKP_int32 k, idx; - - /* De-interleave allpass inputs, and convert Q15 -> Q25 */ - for( k = 0; k < len; k++ ) { - idx = SKP_LSHIFT( k, 1 ); - scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ idx ], 10 ); - scratch[ k + len ] = SKP_LSHIFT( (SKP_int32)in[ idx + 1 ], 10 ); - } - - idx = SKP_LSHIFT( len, 1 ); - - /* Allpass filters */ - SKP_Silk_allpass_int( scratch, S, A21_Resample_1_2[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + idx, S + 1, A21_Resample_1_2[ 1 ], scratch + idx + len, len ); - SKP_Silk_allpass_int( scratch + idx + len, S + 2, A21_Resample_1_2[ 2 ], scratch, len ); - - SKP_Silk_allpass_int( scratch + len, S + 3, A20_Resample_1_2[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + idx, S + 4, A20_Resample_1_2[ 1 ], scratch + idx + len, len ); - SKP_Silk_allpass_int( scratch + idx + len, S + 5, A20_Resample_1_2[ 2 ], scratch + len, len ); - - /* Add two allpass outputs */ - for( k = 0; k < len; k++ ) { - out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ] + scratch[ k + len ], 11 ) ); - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_resample_1_2 * + * * + * Downsample by a factor 2 * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Coefficients for 2-fold resampling */ +static SKP_int16 A20_Resample_1_2[ 3 ] = { 1254, 10102, 22898 }; +static SKP_int16 A21_Resample_1_2[ 3 ] = { 4810, 16371, 29374 }; + + +/* Downsample by a factor 2 */ +void SKP_Silk_resample_1_2( + const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ + SKP_int32 *S, /* I/O: State vector [6] */ + SKP_int16 *out, /* O: 8 kHz signal [len] */ + SKP_int32 *scratch, /* I: Scratch memory [4*len] */ + const SKP_int32 len /* I: Number of OUTPUT samples*/ +) +{ + SKP_int32 k, idx; + + /* De-interleave allpass inputs, and convert Q15 -> Q25 */ + for( k = 0; k < len; k++ ) { + idx = SKP_LSHIFT( k, 1 ); + scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ idx ], 10 ); + scratch[ k + len ] = SKP_LSHIFT( (SKP_int32)in[ idx + 1 ], 10 ); + } + + idx = SKP_LSHIFT( len, 1 ); + + /* Allpass filters */ + SKP_Silk_allpass_int( scratch, S, A21_Resample_1_2[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + idx, S + 1, A21_Resample_1_2[ 1 ], scratch + idx + len, len ); + SKP_Silk_allpass_int( scratch + idx + len, S + 2, A21_Resample_1_2[ 2 ], scratch, len ); + + SKP_Silk_allpass_int( scratch + len, S + 3, A20_Resample_1_2[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + idx, S + 4, A20_Resample_1_2[ 1 ], scratch + idx + len, len ); + SKP_Silk_allpass_int( scratch + idx + len, S + 5, A20_Resample_1_2[ 2 ], scratch + len, len ); + + /* Add two allpass outputs */ + for( k = 0; k < len; k++ ) { + out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ] + scratch[ k + len ], 11 ) ); + } +} + diff --git a/jni/silk/src/SKP_Silk_resample_1_2_coarse.c b/app/src/main/jni/silk/src/SKP_Silk_resample_1_2_coarse.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_1_2_coarse.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_1_2_coarse.c index f90b059..16fd71b 100644 --- a/jni/silk/src/SKP_Silk_resample_1_2_coarse.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_1_2_coarse.c @@ -1,74 +1,74 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_resample_1_2_coarse.c * - * * - * Downsample by a factor 2, coarser * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* downsample by a factor 2, coarser */ -void SKP_Silk_resample_1_2_coarse( - const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ - SKP_int32 *S, /* I/O: State vector [4] */ - SKP_int16 *out, /* O: 8 kHz signal [len] */ - SKP_int32 *scratch, /* I: Scratch memory [3*len] */ - const SKP_int32 len /* I: Number of OUTPUT samples*/ -) -{ - SKP_int32 k, idx; - - /* Coefficients for coarser 2-fold resampling */ - const SKP_int16 A20c[ 2 ] = { 2119, 16663 }; - const SKP_int16 A21c[ 2 ] = { 8050, 26861 }; - - /* De-interleave allpass inputs, and convert Q15 -> Q25 */ - for( k = 0; k < len; k++ ) { - idx = SKP_LSHIFT( k, 1 ); - scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ idx ], 10 ); - scratch[ k + len ] = SKP_LSHIFT( (SKP_int32)in[ idx + 1 ], 10 ); - } - - idx = SKP_LSHIFT( len, 1 ); - /* Allpass filters */ - SKP_Silk_allpass_int( scratch, S, A21c[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + idx, S + 1, A21c[ 1 ], scratch, len ); - - SKP_Silk_allpass_int( scratch + len, S + 2, A20c[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + idx, S + 3, A20c[ 1 ], scratch + len, len ); - - /* Add two allpass outputs */ - for( k = 0; k < len; k++ ) { - out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ] + scratch[ k + len ], 11 ) ); - } -} - - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_resample_1_2_coarse.c * + * * + * Downsample by a factor 2, coarser * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* downsample by a factor 2, coarser */ +void SKP_Silk_resample_1_2_coarse( + const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ + SKP_int32 *S, /* I/O: State vector [4] */ + SKP_int16 *out, /* O: 8 kHz signal [len] */ + SKP_int32 *scratch, /* I: Scratch memory [3*len] */ + const SKP_int32 len /* I: Number of OUTPUT samples*/ +) +{ + SKP_int32 k, idx; + + /* Coefficients for coarser 2-fold resampling */ + const SKP_int16 A20c[ 2 ] = { 2119, 16663 }; + const SKP_int16 A21c[ 2 ] = { 8050, 26861 }; + + /* De-interleave allpass inputs, and convert Q15 -> Q25 */ + for( k = 0; k < len; k++ ) { + idx = SKP_LSHIFT( k, 1 ); + scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ idx ], 10 ); + scratch[ k + len ] = SKP_LSHIFT( (SKP_int32)in[ idx + 1 ], 10 ); + } + + idx = SKP_LSHIFT( len, 1 ); + /* Allpass filters */ + SKP_Silk_allpass_int( scratch, S, A21c[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + idx, S + 1, A21c[ 1 ], scratch, len ); + + SKP_Silk_allpass_int( scratch + len, S + 2, A20c[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + idx, S + 3, A20c[ 1 ], scratch + len, len ); + + /* Add two allpass outputs */ + for( k = 0; k < len; k++ ) { + out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ] + scratch[ k + len ], 11 ) ); + } +} + + diff --git a/jni/silk/src/SKP_Silk_resample_1_2_coarsest.c b/app/src/main/jni/silk/src/SKP_Silk_resample_1_2_coarsest.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_1_2_coarsest.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_1_2_coarsest.c index 8e72965..796fe7c 100644 --- a/jni/silk/src/SKP_Silk_resample_1_2_coarsest.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_1_2_coarsest.c @@ -1,71 +1,71 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_resample_1_2_coarsest.c * - * * - * Downsample by a factor 2, coarsest * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - - -/* Coefficients for coarsest 2-fold resampling */ -static SKP_int16 A20cst[ 1 ] = { 3786 }; -static SKP_int16 A21cst[ 1 ] = { 17908 }; - -/* Downsample by a factor 2, coarsest */ -void SKP_Silk_resample_1_2_coarsest( - const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ - SKP_int32 *S, /* I/O: State vector [2] */ - SKP_int16 *out, /* O: 8 kHz signal [len] */ - SKP_int32 *scratch, /* I: Scratch memory [3*len] */ - const SKP_int32 len /* I: Number of OUTPUT samples*/ -) -{ - SKP_int32 k, idx; - - /* De-interleave allpass inputs, and convert Q15 -> Q25 */ - for( k = 0; k < len; k++ ) { - idx = SKP_LSHIFT( k, 1 ); - scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ idx ], 10 ); - scratch[ k + len ] = SKP_LSHIFT( (SKP_int32)in[ idx + 1 ], 10 ); - } - - idx = SKP_LSHIFT( len, 1 ); - /* Allpass filters */ - SKP_Silk_allpass_int( scratch, S, A21cst[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + len, S + 1, A20cst[ 0 ], scratch, len ); - - /* Add two allpass outputs */ - for( k = 0; k < len; k++ ) { - out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ] + scratch[ k + idx ], 11 ) ); - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_resample_1_2_coarsest.c * + * * + * Downsample by a factor 2, coarsest * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + + +/* Coefficients for coarsest 2-fold resampling */ +static SKP_int16 A20cst[ 1 ] = { 3786 }; +static SKP_int16 A21cst[ 1 ] = { 17908 }; + +/* Downsample by a factor 2, coarsest */ +void SKP_Silk_resample_1_2_coarsest( + const SKP_int16 *in, /* I: 16 kHz signal [2*len] */ + SKP_int32 *S, /* I/O: State vector [2] */ + SKP_int16 *out, /* O: 8 kHz signal [len] */ + SKP_int32 *scratch, /* I: Scratch memory [3*len] */ + const SKP_int32 len /* I: Number of OUTPUT samples*/ +) +{ + SKP_int32 k, idx; + + /* De-interleave allpass inputs, and convert Q15 -> Q25 */ + for( k = 0; k < len; k++ ) { + idx = SKP_LSHIFT( k, 1 ); + scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ idx ], 10 ); + scratch[ k + len ] = SKP_LSHIFT( (SKP_int32)in[ idx + 1 ], 10 ); + } + + idx = SKP_LSHIFT( len, 1 ); + /* Allpass filters */ + SKP_Silk_allpass_int( scratch, S, A21cst[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + len, S + 1, A20cst[ 0 ], scratch, len ); + + /* Add two allpass outputs */ + for( k = 0; k < len; k++ ) { + out[ k ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ] + scratch[ k + idx ], 11 ) ); + } +} + diff --git a/jni/silk/src/SKP_Silk_resample_1_3.c b/app/src/main/jni/silk/src/SKP_Silk_resample_1_3.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_1_3.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_1_3.c index 0f77412..4718b85 100644 --- a/jni/silk/src/SKP_Silk_resample_1_3.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_1_3.c @@ -1,101 +1,101 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_resample_1_3.c * - * * - * Downsamples by a factor 3 * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 081113 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -#define OUT_SUBFR_LEN 80 - -/* Downsamples by a factor 3 */ -void SKP_Silk_resample_1_3( - SKP_int16 *out, /* O: Fs_low signal [inLen/3] */ - SKP_int32 *S, /* I/O: State vector [7] */ - const SKP_int16 *in, /* I: Fs_high signal [inLen] */ - const SKP_int32 inLen /* I: Input length, must be a multiple of 3 */ -) -{ - SKP_int k, outLen, LSubFrameIn, LSubFrameOut; - SKP_int32 out_tmp, limit = 102258000; // (102258000 + 1560) * 21 * 2^(-16) = 32767.5 - SKP_int32 scratch0[ 3 * OUT_SUBFR_LEN ]; - SKP_int32 scratch10[ OUT_SUBFR_LEN ], scratch11[ OUT_SUBFR_LEN ], scratch12[ OUT_SUBFR_LEN ]; - /* coefficients for 3-fold resampling */ - const SKP_int16 A30[ 2 ] = { 1773, 17818 }; - const SKP_int16 A31[ 2 ] = { 4942, 25677 }; - const SKP_int16 A32[ 2 ] = { 11786, 29304 }; - - /* Check that input is multiple of 3 */ - SKP_assert( inLen % 3 == 0 ); - - outLen = SKP_DIV32_16( inLen, 3 ); - while( outLen > 0 ) { - LSubFrameOut = SKP_min_int( OUT_SUBFR_LEN, outLen ); - LSubFrameIn = SKP_SMULBB( 3, LSubFrameOut ); - - /* Low-pass filter, Q15 -> Q25 */ - SKP_Silk_lowpass_short( in, S, scratch0, LSubFrameIn ); - - /* De-interleave three allpass inputs */ - for( k = 0; k < LSubFrameOut; k++ ) { - scratch10[ k ] = scratch0[ 3 * k ]; - scratch11[ k ] = scratch0[ 3 * k + 1 ]; - scratch12[ k ] = scratch0[ 3 * k + 2 ]; - } - - /* Allpass filters */ - SKP_Silk_allpass_int( scratch10, S + 1, A32[ 0 ], scratch0, LSubFrameOut ); - SKP_Silk_allpass_int( scratch0, S + 2, A32[ 1 ], scratch10, LSubFrameOut ); - - SKP_Silk_allpass_int( scratch11, S + 3, A31[ 0 ], scratch0, LSubFrameOut ); - SKP_Silk_allpass_int( scratch0, S + 4, A31[ 1 ], scratch11, LSubFrameOut ); - - SKP_Silk_allpass_int( scratch12, S + 5, A30[ 0 ], scratch0, LSubFrameOut ); - SKP_Silk_allpass_int( scratch0, S + 6, A30[ 1 ], scratch12, LSubFrameOut ); - - /* Add three allpass outputs */ - for( k = 0; k < LSubFrameOut; k++ ) { - out_tmp = scratch10[ k ] + scratch11[ k ] + scratch12[ k ]; - if( out_tmp - limit > 0 ) { - out[ k ] = SKP_int16_MAX; - } else if( out_tmp + limit < 0 ) { - out[ k ] = SKP_int16_MIN; - } else { - out[ k ] = (SKP_int16) SKP_SMULWB( out_tmp + 1560, 21 ); - } - } - - in += LSubFrameIn; - out += LSubFrameOut; - outLen -= LSubFrameOut; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_resample_1_3.c * + * * + * Downsamples by a factor 3 * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 081113 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +#define OUT_SUBFR_LEN 80 + +/* Downsamples by a factor 3 */ +void SKP_Silk_resample_1_3( + SKP_int16 *out, /* O: Fs_low signal [inLen/3] */ + SKP_int32 *S, /* I/O: State vector [7] */ + const SKP_int16 *in, /* I: Fs_high signal [inLen] */ + const SKP_int32 inLen /* I: Input length, must be a multiple of 3 */ +) +{ + SKP_int k, outLen, LSubFrameIn, LSubFrameOut; + SKP_int32 out_tmp, limit = 102258000; // (102258000 + 1560) * 21 * 2^(-16) = 32767.5 + SKP_int32 scratch0[ 3 * OUT_SUBFR_LEN ]; + SKP_int32 scratch10[ OUT_SUBFR_LEN ], scratch11[ OUT_SUBFR_LEN ], scratch12[ OUT_SUBFR_LEN ]; + /* coefficients for 3-fold resampling */ + const SKP_int16 A30[ 2 ] = { 1773, 17818 }; + const SKP_int16 A31[ 2 ] = { 4942, 25677 }; + const SKP_int16 A32[ 2 ] = { 11786, 29304 }; + + /* Check that input is multiple of 3 */ + SKP_assert( inLen % 3 == 0 ); + + outLen = SKP_DIV32_16( inLen, 3 ); + while( outLen > 0 ) { + LSubFrameOut = SKP_min_int( OUT_SUBFR_LEN, outLen ); + LSubFrameIn = SKP_SMULBB( 3, LSubFrameOut ); + + /* Low-pass filter, Q15 -> Q25 */ + SKP_Silk_lowpass_short( in, S, scratch0, LSubFrameIn ); + + /* De-interleave three allpass inputs */ + for( k = 0; k < LSubFrameOut; k++ ) { + scratch10[ k ] = scratch0[ 3 * k ]; + scratch11[ k ] = scratch0[ 3 * k + 1 ]; + scratch12[ k ] = scratch0[ 3 * k + 2 ]; + } + + /* Allpass filters */ + SKP_Silk_allpass_int( scratch10, S + 1, A32[ 0 ], scratch0, LSubFrameOut ); + SKP_Silk_allpass_int( scratch0, S + 2, A32[ 1 ], scratch10, LSubFrameOut ); + + SKP_Silk_allpass_int( scratch11, S + 3, A31[ 0 ], scratch0, LSubFrameOut ); + SKP_Silk_allpass_int( scratch0, S + 4, A31[ 1 ], scratch11, LSubFrameOut ); + + SKP_Silk_allpass_int( scratch12, S + 5, A30[ 0 ], scratch0, LSubFrameOut ); + SKP_Silk_allpass_int( scratch0, S + 6, A30[ 1 ], scratch12, LSubFrameOut ); + + /* Add three allpass outputs */ + for( k = 0; k < LSubFrameOut; k++ ) { + out_tmp = scratch10[ k ] + scratch11[ k ] + scratch12[ k ]; + if( out_tmp - limit > 0 ) { + out[ k ] = SKP_int16_MAX; + } else if( out_tmp + limit < 0 ) { + out[ k ] = SKP_int16_MIN; + } else { + out[ k ] = (SKP_int16) SKP_SMULWB( out_tmp + 1560, 21 ); + } + } + + in += LSubFrameIn; + out += LSubFrameOut; + outLen -= LSubFrameOut; + } +} diff --git a/jni/silk/src/SKP_Silk_resample_2_1_coarse.c b/app/src/main/jni/silk/src/SKP_Silk_resample_2_1_coarse.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_2_1_coarse.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_2_1_coarse.c index f01fbe9..58709ed 100644 --- a/jni/silk/src/SKP_Silk_resample_2_1_coarse.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_2_1_coarse.c @@ -1,73 +1,73 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_resample_2_1_coarse.c * - * * - * Upsample by a factor 2, coarser * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Upsample by a factor 2, coarser */ -void SKP_Silk_resample_2_1_coarse( - const SKP_int16 *in, /* I: 8 kHz signal [len] */ - SKP_int32 *S, /* I/O: State vector [4] */ - SKP_int16 *out, /* O: 16 kHz signal [2*len] */ - SKP_int32 *scratch, /* I: Scratch memory [3*len] */ - const SKP_int32 len /* I: Number of INPUT samples */ -) -{ - SKP_int32 k, idx; - - /* Coefficients for coarser 2-fold resampling */ - const SKP_int16 A20c[ 2 ] = { 2119, 16663 }; - const SKP_int16 A21c[ 2 ] = { 8050, 26861 }; - - /* Convert Q15 -> Q25 */ - for( k = 0; k < len; k++ ) { - scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ k ], 10 ); - } - - idx = SKP_LSHIFT( len, 1 ); - - /* Allpass filters */ - SKP_Silk_allpass_int( scratch, S, A20c[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + idx, S + 1, A20c[ 1 ], scratch + len, len ); - - SKP_Silk_allpass_int( scratch, S + 2, A21c[ 0 ], scratch + idx, len ); - SKP_Silk_allpass_int( scratch + idx, S + 3, A21c[ 1 ], scratch, len ); - - /* Interleave two allpass outputs */ - for( k = 0; k < len; k++ ) { - idx = SKP_LSHIFT( k, 1 ); - out[ idx ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k + len ], 10 ) ); - out[ idx + 1 ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ], 10 ) ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_resample_2_1_coarse.c * + * * + * Upsample by a factor 2, coarser * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Upsample by a factor 2, coarser */ +void SKP_Silk_resample_2_1_coarse( + const SKP_int16 *in, /* I: 8 kHz signal [len] */ + SKP_int32 *S, /* I/O: State vector [4] */ + SKP_int16 *out, /* O: 16 kHz signal [2*len] */ + SKP_int32 *scratch, /* I: Scratch memory [3*len] */ + const SKP_int32 len /* I: Number of INPUT samples */ +) +{ + SKP_int32 k, idx; + + /* Coefficients for coarser 2-fold resampling */ + const SKP_int16 A20c[ 2 ] = { 2119, 16663 }; + const SKP_int16 A21c[ 2 ] = { 8050, 26861 }; + + /* Convert Q15 -> Q25 */ + for( k = 0; k < len; k++ ) { + scratch[ k ] = SKP_LSHIFT( (SKP_int32)in[ k ], 10 ); + } + + idx = SKP_LSHIFT( len, 1 ); + + /* Allpass filters */ + SKP_Silk_allpass_int( scratch, S, A20c[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + idx, S + 1, A20c[ 1 ], scratch + len, len ); + + SKP_Silk_allpass_int( scratch, S + 2, A21c[ 0 ], scratch + idx, len ); + SKP_Silk_allpass_int( scratch + idx, S + 3, A21c[ 1 ], scratch, len ); + + /* Interleave two allpass outputs */ + for( k = 0; k < len; k++ ) { + idx = SKP_LSHIFT( k, 1 ); + out[ idx ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k + len ], 10 ) ); + out[ idx + 1 ] = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( scratch[ k ], 10 ) ); + } +} diff --git a/jni/silk/src/SKP_Silk_resample_2_3.c b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_2_3.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_2_3.c index dbb16fe..81c15b2 100644 --- a/jni/silk/src/SKP_Silk_resample_2_3.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3.c @@ -1,74 +1,74 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_2_3.c * - * * - * Resamples by a factor 2/3 * - * * - * Copyright 2008 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 081113 * - * */ - -#include "SKP_Silk_SigProc_FIX.h" - -#define OUT_SUBFR_LEN 80 - -/* Resamples by a factor 2/3 */ -void SKP_Silk_resample_2_3( - SKP_int16 *out, /* O: Fs_low signal [inLen * 2/3] */ - SKP_int32 *S, /* I/O: State vector [7+4] */ - const SKP_int16 *in, /* I: Fs_high signal [inLen] */ - const SKP_int inLen /* I: Input length, must be a multiple of 3 */ -) -{ - SKP_int outLen, LSubFrameIn, LSubFrameOut; - SKP_int16 outH[ 3 * OUT_SUBFR_LEN ]; - SKP_int32 scratch[ ( 9 * OUT_SUBFR_LEN ) / 2 ]; - - /* Check that input length is multiple of 3 */ - SKP_assert( inLen % 3 == 0 ); - - outLen = SKP_DIV32_16( SKP_LSHIFT( inLen, 1 ), 3 ); - while( outLen > 0 ) { - LSubFrameOut = SKP_min_int( OUT_SUBFR_LEN, outLen ); - LSubFrameIn = SKP_SMULWB( 98304, LSubFrameOut ); /* 98304_Q16 = 3/2_Q0 */ - - /* Upsample by a factor 2 */ - /* Scratch size needs to be: 3 * LSubFrameIn * sizeof( SKP_int32 ) */ - SKP_Silk_resample_2_1_coarse( in, &S[ 0 ], outH, scratch, LSubFrameIn ); - - /* Downsample by a factor 3 */ - SKP_Silk_resample_1_3( out, &S[ 4 ], outH, SKP_LSHIFT( LSubFrameIn, 1 ) ); - - in += LSubFrameIn; - out += LSubFrameOut; - outLen -= LSubFrameOut; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_2_3.c * + * * + * Resamples by a factor 2/3 * + * * + * Copyright 2008 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 081113 * + * */ + +#include "SKP_Silk_SigProc_FIX.h" + +#define OUT_SUBFR_LEN 80 + +/* Resamples by a factor 2/3 */ +void SKP_Silk_resample_2_3( + SKP_int16 *out, /* O: Fs_low signal [inLen * 2/3] */ + SKP_int32 *S, /* I/O: State vector [7+4] */ + const SKP_int16 *in, /* I: Fs_high signal [inLen] */ + const SKP_int inLen /* I: Input length, must be a multiple of 3 */ +) +{ + SKP_int outLen, LSubFrameIn, LSubFrameOut; + SKP_int16 outH[ 3 * OUT_SUBFR_LEN ]; + SKP_int32 scratch[ ( 9 * OUT_SUBFR_LEN ) / 2 ]; + + /* Check that input length is multiple of 3 */ + SKP_assert( inLen % 3 == 0 ); + + outLen = SKP_DIV32_16( SKP_LSHIFT( inLen, 1 ), 3 ); + while( outLen > 0 ) { + LSubFrameOut = SKP_min_int( OUT_SUBFR_LEN, outLen ); + LSubFrameIn = SKP_SMULWB( 98304, LSubFrameOut ); /* 98304_Q16 = 3/2_Q0 */ + + /* Upsample by a factor 2 */ + /* Scratch size needs to be: 3 * LSubFrameIn * sizeof( SKP_int32 ) */ + SKP_Silk_resample_2_1_coarse( in, &S[ 0 ], outH, scratch, LSubFrameIn ); + + /* Downsample by a factor 3 */ + SKP_Silk_resample_1_3( out, &S[ 4 ], outH, SKP_LSHIFT( LSubFrameIn, 1 ) ); + + in += LSubFrameIn; + out += LSubFrameOut; + outLen -= LSubFrameOut; + } +} diff --git a/jni/silk/src/SKP_Silk_resample_2_3_coarse.c b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3_coarse.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_2_3_coarse.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_2_3_coarse.c index 76c597b..e558d07 100644 --- a/jni/silk/src/SKP_Silk_resample_2_3_coarse.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3_coarse.c @@ -1,112 +1,112 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_2_3_coarse.c * - * * - * Description: Linear phase FIR polyphase implementation of resampling * - * * - * Copyright 2009 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 090423 * - * */ - -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_resample_rom.h" - -/* Resamples input data with a factor 2/3 */ -void SKP_Silk_resample_2_3_coarse( - SKP_int16 *out, /* O: Output signal */ - SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ - const SKP_int16 *in, /* I: Input signal */ - const SKP_int frameLenIn, /* I: Number of input samples */ - SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ -) -{ - SKP_int32 n, ind, interpol_ind, tmp, index_Q16; - SKP_int16 *in_ptr; - SKP_int frameLenOut; - const SKP_int16 *interpol_ptr; - - /* Copy buffered samples to start of scratch */ - SKP_memcpy( scratch, S, ( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); - - /* Then append by the input signal */ - SKP_memcpy( &scratch[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ], in, frameLenIn * sizeof( SKP_int16 ) ); - - frameLenOut = SKP_DIV32_16( SKP_MUL( 2, frameLenIn ), 3 ); - index_Q16 = 0; - - SKP_assert( frameLenIn == ( ( frameLenOut * 3 ) / 2 ) ); - - /* Interpolate */ - for( n = frameLenOut; n > 0; n-- ) { - - /* Integer part */ - ind = SKP_RSHIFT( index_Q16, 16 ); - - /* Pointer to buffered input */ - in_ptr = scratch + ind; - - /* Fractional part */ - interpol_ind = ( SKP_SMULWB( index_Q16, SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS ) & - ( SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS - 1 ) ); - - /* Pointer to FIR taps */ - interpol_ptr = SigProc_Resample_2_3_coarse_INTERPOL[ interpol_ind ]; - - /* Interpolate */ - /* Hardcoded for 32 FIR taps */ - SKP_assert( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS == 32 ); - tmp = (SKP_int32)interpol_ptr[ 0 ] * in_ptr[ 0 ] + (SKP_int32)interpol_ptr[ 1 ] * in_ptr[ 1 ] + - (SKP_int32)interpol_ptr[ 2 ] * in_ptr[ 2 ] + (SKP_int32)interpol_ptr[ 3 ] * in_ptr[ 3 ] + - (SKP_int32)interpol_ptr[ 4 ] * in_ptr[ 4 ] + (SKP_int32)interpol_ptr[ 5 ] * in_ptr[ 5 ] + - (SKP_int32)interpol_ptr[ 6 ] * in_ptr[ 6 ] + (SKP_int32)interpol_ptr[ 7 ] * in_ptr[ 7 ] + - (SKP_int32)interpol_ptr[ 8 ] * in_ptr[ 8 ] + (SKP_int32)interpol_ptr[ 9 ] * in_ptr[ 9 ] + - (SKP_int32)interpol_ptr[ 10 ] * in_ptr[ 10 ] + (SKP_int32)interpol_ptr[ 11 ] * in_ptr[ 11 ] + - (SKP_int32)interpol_ptr[ 12 ] * in_ptr[ 12 ] + (SKP_int32)interpol_ptr[ 13 ] * in_ptr[ 13 ] + - (SKP_int32)interpol_ptr[ 14 ] * in_ptr[ 14 ] + (SKP_int32)interpol_ptr[ 15 ] * in_ptr[ 15 ] + - (SKP_int32)interpol_ptr[ 16 ] * in_ptr[ 16 ] + (SKP_int32)interpol_ptr[ 17 ] * in_ptr[ 17 ] + - (SKP_int32)interpol_ptr[ 18 ] * in_ptr[ 18 ] + (SKP_int32)interpol_ptr[ 19 ] * in_ptr[ 19 ] + - (SKP_int32)interpol_ptr[ 20 ] * in_ptr[ 20 ] + (SKP_int32)interpol_ptr[ 21 ] * in_ptr[ 21 ] + - (SKP_int32)interpol_ptr[ 22 ] * in_ptr[ 22 ] + (SKP_int32)interpol_ptr[ 23 ] * in_ptr[ 23 ] + - (SKP_int32)interpol_ptr[ 24 ] * in_ptr[ 24 ] + (SKP_int32)interpol_ptr[ 25 ] * in_ptr[ 25 ] + - (SKP_int32)interpol_ptr[ 26 ] * in_ptr[ 26 ] + (SKP_int32)interpol_ptr[ 27 ] * in_ptr[ 27 ] + - (SKP_int32)interpol_ptr[ 28 ] * in_ptr[ 28 ] + (SKP_int32)interpol_ptr[ 29 ] * in_ptr[ 29 ]; - - /* Round, saturate and store to output array */ - *out++ = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( tmp, 15 ) ); - - /* Update index */ - index_Q16 += ( ( 1 << 16 ) + ( 1 << 15 ) ); // (3/2)_Q0; - } - - /* Move last part of input signal to the sample buffer to prepare for the next call */ - SKP_memcpy( S, &in[ frameLenIn - ( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) ], - ( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_2_3_coarse.c * + * * + * Description: Linear phase FIR polyphase implementation of resampling * + * * + * Copyright 2009 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 090423 * + * */ + +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_resample_rom.h" + +/* Resamples input data with a factor 2/3 */ +void SKP_Silk_resample_2_3_coarse( + SKP_int16 *out, /* O: Output signal */ + SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ + const SKP_int16 *in, /* I: Input signal */ + const SKP_int frameLenIn, /* I: Number of input samples */ + SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ] */ +) +{ + SKP_int32 n, ind, interpol_ind, tmp, index_Q16; + SKP_int16 *in_ptr; + SKP_int frameLenOut; + const SKP_int16 *interpol_ptr; + + /* Copy buffered samples to start of scratch */ + SKP_memcpy( scratch, S, ( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); + + /* Then append by the input signal */ + SKP_memcpy( &scratch[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ], in, frameLenIn * sizeof( SKP_int16 ) ); + + frameLenOut = SKP_DIV32_16( SKP_MUL( 2, frameLenIn ), 3 ); + index_Q16 = 0; + + SKP_assert( frameLenIn == ( ( frameLenOut * 3 ) / 2 ) ); + + /* Interpolate */ + for( n = frameLenOut; n > 0; n-- ) { + + /* Integer part */ + ind = SKP_RSHIFT( index_Q16, 16 ); + + /* Pointer to buffered input */ + in_ptr = scratch + ind; + + /* Fractional part */ + interpol_ind = ( SKP_SMULWB( index_Q16, SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS ) & + ( SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS - 1 ) ); + + /* Pointer to FIR taps */ + interpol_ptr = SigProc_Resample_2_3_coarse_INTERPOL[ interpol_ind ]; + + /* Interpolate */ + /* Hardcoded for 32 FIR taps */ + SKP_assert( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS == 32 ); + tmp = (SKP_int32)interpol_ptr[ 0 ] * in_ptr[ 0 ] + (SKP_int32)interpol_ptr[ 1 ] * in_ptr[ 1 ] + + (SKP_int32)interpol_ptr[ 2 ] * in_ptr[ 2 ] + (SKP_int32)interpol_ptr[ 3 ] * in_ptr[ 3 ] + + (SKP_int32)interpol_ptr[ 4 ] * in_ptr[ 4 ] + (SKP_int32)interpol_ptr[ 5 ] * in_ptr[ 5 ] + + (SKP_int32)interpol_ptr[ 6 ] * in_ptr[ 6 ] + (SKP_int32)interpol_ptr[ 7 ] * in_ptr[ 7 ] + + (SKP_int32)interpol_ptr[ 8 ] * in_ptr[ 8 ] + (SKP_int32)interpol_ptr[ 9 ] * in_ptr[ 9 ] + + (SKP_int32)interpol_ptr[ 10 ] * in_ptr[ 10 ] + (SKP_int32)interpol_ptr[ 11 ] * in_ptr[ 11 ] + + (SKP_int32)interpol_ptr[ 12 ] * in_ptr[ 12 ] + (SKP_int32)interpol_ptr[ 13 ] * in_ptr[ 13 ] + + (SKP_int32)interpol_ptr[ 14 ] * in_ptr[ 14 ] + (SKP_int32)interpol_ptr[ 15 ] * in_ptr[ 15 ] + + (SKP_int32)interpol_ptr[ 16 ] * in_ptr[ 16 ] + (SKP_int32)interpol_ptr[ 17 ] * in_ptr[ 17 ] + + (SKP_int32)interpol_ptr[ 18 ] * in_ptr[ 18 ] + (SKP_int32)interpol_ptr[ 19 ] * in_ptr[ 19 ] + + (SKP_int32)interpol_ptr[ 20 ] * in_ptr[ 20 ] + (SKP_int32)interpol_ptr[ 21 ] * in_ptr[ 21 ] + + (SKP_int32)interpol_ptr[ 22 ] * in_ptr[ 22 ] + (SKP_int32)interpol_ptr[ 23 ] * in_ptr[ 23 ] + + (SKP_int32)interpol_ptr[ 24 ] * in_ptr[ 24 ] + (SKP_int32)interpol_ptr[ 25 ] * in_ptr[ 25 ] + + (SKP_int32)interpol_ptr[ 26 ] * in_ptr[ 26 ] + (SKP_int32)interpol_ptr[ 27 ] * in_ptr[ 27 ] + + (SKP_int32)interpol_ptr[ 28 ] * in_ptr[ 28 ] + (SKP_int32)interpol_ptr[ 29 ] * in_ptr[ 29 ]; + + /* Round, saturate and store to output array */ + *out++ = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( tmp, 15 ) ); + + /* Update index */ + index_Q16 += ( ( 1 << 16 ) + ( 1 << 15 ) ); // (3/2)_Q0; + } + + /* Move last part of input signal to the sample buffer to prepare for the next call */ + SKP_memcpy( S, &in[ frameLenIn - ( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) ], + ( SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); +} diff --git a/jni/silk/src/SKP_Silk_resample_2_3_coarsest.c b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3_coarsest.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_2_3_coarsest.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_2_3_coarsest.c index 6a57eaa..7ebcf0f 100644 --- a/jni/silk/src/SKP_Silk_resample_2_3_coarsest.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3_coarsest.c @@ -1,131 +1,131 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_2_3_coarsest.c * - * * - * Description: Linear phase FIR polyphase implementation of resampling * - * * - * Copyright 2009 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 090423 * - * */ - -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_resample_rom.h" - -/* Resamples input data with a factor 2/3 */ -void SKP_Silk_resample_2_3_coarsest( - SKP_int16 *out, /* O: Output signal */ - SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ - const SKP_int16 *in, /* I: Input signal */ - const SKP_int frameLenIn, /* I: Number of input samples */ - SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ -) -{ - SKP_int32 n, ind, interpol_ind, tmp, index_Q16; - SKP_int16 *in_ptr; - SKP_int frameLenOut; - const SKP_int16 *interpol_ptr; -#if ( EMBEDDED_ARM>=6 ) && defined (__GNUC__) - SKP_int32 in_val, interpol_val; -#endif - - /* Copy buffered samples to start of scratch */ - SKP_memcpy( scratch, S, ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); - - /* Then append by the input signal */ - SKP_memcpy( &scratch[ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ], in, frameLenIn * sizeof( SKP_int16 ) ); - - frameLenOut = SKP_SMULWB( SKP_LSHIFT( (SKP_int32)frameLenIn, 1 ), 21846 ); // 21846_Q15 = (2/3)_Q0 rounded _up_ - index_Q16 = 0; - - SKP_assert( frameLenIn == ( ( frameLenOut * 3 ) / 2 ) ); - - /* Interpolate */ - for( n = frameLenOut; n > 0; n-- ) { - - /* Integer part */ - ind = SKP_RSHIFT( index_Q16, 16 ); - - /* Pointer to buffered input */ - in_ptr = scratch + ind; - - /* Fractional part */ - interpol_ind = ( SKP_SMULWB( index_Q16, SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS ) & - ( SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS - 1 ) ); - - /* Pointer to FIR taps */ - interpol_ptr = SigProc_Resample_2_3_coarsest_INTERPOL[ interpol_ind ]; - - /* Interpolate: Hardcoded for 10 FIR taps */ -#if ( EMBEDDED_ARM>=6 ) && defined (__GNUC__) /*It doesn't improve efficiency on iphone.*/ - /*tmp = SKP_SMUAD( *((SKP_int32 *)interpol_ptr)++, *((SKP_int32 *)in_ptr)++); - tmp = SKP_SMLAD( tmp, *((SKP_int32 *)interpol_ptr)++, *((SKP_int32 *)in_ptr)++); - tmp = SKP_SMLAD( tmp, *((SKP_int32 *)interpol_ptr), *((SKP_int32 *)in_ptr) );*/ - __asm__ __volatile__ ( "ldr %1, [%3], #4 \n\t" - "ldr %2, [%4], #4 \n\t" - "smuad %0, %1, %2 \n\t" - "ldr %1, [%3], #4 \n\t" - "ldr %2, [%4], #4 \n\t" - "smlad %0, %1, %2, %0\n\t" - "ldr %1, [%3], #4 \n\t" - "ldr %2, [%4], #4 \n\t" - "smlad %0, %1, %2, %0\n\t" - "ldr %1, [%3], #4 \n\t" - "ldr %2, [%4], #4 \n\t" - "smlad %0, %1, %2, %0\n\t" - "ldr %1, [%3] \n\t" - "ldr %2, [%4] \n\t" - "smlad %0, %1, %2, %0\n\t" - : "=r" (tmp), "=r" (interpol_val), "=r" (in_val), "=r" (interpol_ptr), "=r" (in_ptr) - : "3" (interpol_ptr), "4" (in_ptr)); -#else - SKP_assert( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS == 10 ); - tmp = SKP_SMULBB( interpol_ptr[ 0 ], in_ptr[ 0 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 1 ], in_ptr[ 1 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 2 ], in_ptr[ 2 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 3 ], in_ptr[ 3 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 4 ], in_ptr[ 4 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 5 ], in_ptr[ 5 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 6 ], in_ptr[ 6 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 7 ], in_ptr[ 7 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 8 ], in_ptr[ 8 ] ); - tmp = SKP_SMLABB( tmp, interpol_ptr[ 9 ], in_ptr[ 9 ] ); -#endif - /* Round, saturate and store to output array */ - *out++ = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( tmp, 15 ) ); - - /* Update index */ - index_Q16 += ( ( 1 << 16 ) + ( 1 << 15 ) ); // (3/2)_Q0; - } - - /* Move last part of input signal to the sample buffer to prepare for the next call */ - SKP_memcpy( S, &in[ frameLenIn - ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) ], - ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_2_3_coarsest.c * + * * + * Description: Linear phase FIR polyphase implementation of resampling * + * * + * Copyright 2009 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 090423 * + * */ + +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_resample_rom.h" + +/* Resamples input data with a factor 2/3 */ +void SKP_Silk_resample_2_3_coarsest( + SKP_int16 *out, /* O: Output signal */ + SKP_int16 *S, /* I/O: Resampler state [ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ + const SKP_int16 *in, /* I: Input signal */ + const SKP_int frameLenIn, /* I: Number of input samples */ + SKP_int16 *scratch /* I: Scratch memory [ frameLenIn + SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ] */ +) +{ + SKP_int32 n, ind, interpol_ind, tmp, index_Q16; + SKP_int16 *in_ptr; + SKP_int frameLenOut; + const SKP_int16 *interpol_ptr; +#if ( EMBEDDED_ARM>=6 ) && defined (__GNUC__) + SKP_int32 in_val, interpol_val; +#endif + + /* Copy buffered samples to start of scratch */ + SKP_memcpy( scratch, S, ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); + + /* Then append by the input signal */ + SKP_memcpy( &scratch[ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ], in, frameLenIn * sizeof( SKP_int16 ) ); + + frameLenOut = SKP_SMULWB( SKP_LSHIFT( (SKP_int32)frameLenIn, 1 ), 21846 ); // 21846_Q15 = (2/3)_Q0 rounded _up_ + index_Q16 = 0; + + SKP_assert( frameLenIn == ( ( frameLenOut * 3 ) / 2 ) ); + + /* Interpolate */ + for( n = frameLenOut; n > 0; n-- ) { + + /* Integer part */ + ind = SKP_RSHIFT( index_Q16, 16 ); + + /* Pointer to buffered input */ + in_ptr = scratch + ind; + + /* Fractional part */ + interpol_ind = ( SKP_SMULWB( index_Q16, SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS ) & + ( SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS - 1 ) ); + + /* Pointer to FIR taps */ + interpol_ptr = SigProc_Resample_2_3_coarsest_INTERPOL[ interpol_ind ]; + + /* Interpolate: Hardcoded for 10 FIR taps */ +#if ( EMBEDDED_ARM>=6 ) && defined (__GNUC__) /*It doesn't improve efficiency on iphone.*/ + /*tmp = SKP_SMUAD( *((SKP_int32 *)interpol_ptr)++, *((SKP_int32 *)in_ptr)++); + tmp = SKP_SMLAD( tmp, *((SKP_int32 *)interpol_ptr)++, *((SKP_int32 *)in_ptr)++); + tmp = SKP_SMLAD( tmp, *((SKP_int32 *)interpol_ptr), *((SKP_int32 *)in_ptr) );*/ + __asm__ __volatile__ ( "ldr %1, [%3], #4 \n\t" + "ldr %2, [%4], #4 \n\t" + "smuad %0, %1, %2 \n\t" + "ldr %1, [%3], #4 \n\t" + "ldr %2, [%4], #4 \n\t" + "smlad %0, %1, %2, %0\n\t" + "ldr %1, [%3], #4 \n\t" + "ldr %2, [%4], #4 \n\t" + "smlad %0, %1, %2, %0\n\t" + "ldr %1, [%3], #4 \n\t" + "ldr %2, [%4], #4 \n\t" + "smlad %0, %1, %2, %0\n\t" + "ldr %1, [%3] \n\t" + "ldr %2, [%4] \n\t" + "smlad %0, %1, %2, %0\n\t" + : "=r" (tmp), "=r" (interpol_val), "=r" (in_val), "=r" (interpol_ptr), "=r" (in_ptr) + : "3" (interpol_ptr), "4" (in_ptr)); +#else + SKP_assert( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS == 10 ); + tmp = SKP_SMULBB( interpol_ptr[ 0 ], in_ptr[ 0 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 1 ], in_ptr[ 1 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 2 ], in_ptr[ 2 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 3 ], in_ptr[ 3 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 4 ], in_ptr[ 4 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 5 ], in_ptr[ 5 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 6 ], in_ptr[ 6 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 7 ], in_ptr[ 7 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 8 ], in_ptr[ 8 ] ); + tmp = SKP_SMLABB( tmp, interpol_ptr[ 9 ], in_ptr[ 9 ] ); +#endif + /* Round, saturate and store to output array */ + *out++ = (SKP_int16)SKP_SAT16( SKP_RSHIFT_ROUND( tmp, 15 ) ); + + /* Update index */ + index_Q16 += ( ( 1 << 16 ) + ( 1 << 15 ) ); // (3/2)_Q0; + } + + /* Move last part of input signal to the sample buffer to prepare for the next call */ + SKP_memcpy( S, &in[ frameLenIn - ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) ], + ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) ); +} diff --git a/jni/silk/src/SKP_Silk_resample_2_3_rom.c b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3_rom.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_2_3_rom.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_2_3_rom.c index 2c396a9..682778b 100644 --- a/jni/silk/src/SKP_Silk_resample_2_3_rom.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_2_3_rom.c @@ -1,50 +1,50 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_2_3_rom.c * - * * - * Description: Filter coefficients for FIR polyphase resampling * - * * - * Copyright 2009 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 090424 * - * */ - -#include "SKP_Silk_resample_rom.h" - -const SKP_int16 SigProc_Resample_2_3_coarse_INTERPOL[ SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS ][ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS ] = { - { 0, -74, 109, 0, -234, 329, 0, -610, 813, 0, -1437, 1954, 0, -4358, 8953, 21845, 8953, -4358, 0, 1954, -1437, 0, 813, -610, 0, 329, -234, 0, 109, -74, 0, 45 }, - { 48, -62, 0, 133, -195, 0, 386, -526, 0, 936, -1243, 0, 2311, -3417, 0, 18026, 18026, 0, -3417, 2311, 0, -1243, 936, 0, -526, 386, 0, -195, 133, 0, -62, 48 }, -}; - -const SKP_int16 SigProc_Resample_2_3_coarsest_INTERPOL[ SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS ][ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS ] = { - { 379, 0, -3081, 8239, 21845, 8239, -3081, 0, 379, -145 }, - { 0, 696, -1951, 0, 17659, 17659, 0, -1951, 696, 0 }, -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_2_3_rom.c * + * * + * Description: Filter coefficients for FIR polyphase resampling * + * * + * Copyright 2009 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 090424 * + * */ + +#include "SKP_Silk_resample_rom.h" + +const SKP_int16 SigProc_Resample_2_3_coarse_INTERPOL[ SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS ][ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS ] = { + { 0, -74, 109, 0, -234, 329, 0, -610, 813, 0, -1437, 1954, 0, -4358, 8953, 21845, 8953, -4358, 0, 1954, -1437, 0, 813, -610, 0, 329, -234, 0, 109, -74, 0, 45 }, + { 48, -62, 0, 133, -195, 0, 386, -526, 0, 936, -1243, 0, 2311, -3417, 0, 18026, 18026, 0, -3417, 2311, 0, -1243, 936, 0, -526, 386, 0, -195, 133, 0, -62, 48 }, +}; + +const SKP_int16 SigProc_Resample_2_3_coarsest_INTERPOL[ SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS ][ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS ] = { + { 379, 0, -3081, 8239, 21845, 8239, -3081, 0, 379, -145 }, + { 0, 696, -1951, 0, 17659, 17659, 0, -1951, 696, 0 }, +}; + diff --git a/jni/silk/src/SKP_Silk_resample_3_1.c b/app/src/main/jni/silk/src/SKP_Silk_resample_3_1.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_3_1.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_3_1.c index 5272bb8..cd2de0c 100644 --- a/jni/silk/src/SKP_Silk_resample_3_1.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_3_1.c @@ -1,100 +1,100 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_resample_3_1.c * - * * - * Upsamples by a factor 3 * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 081113 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -#define IN_SUBFR_LEN_RESAMPLE_3_1 40 - -/* Resamples by a factor 3/1 */ -void SKP_Silk_resample_3_1( - SKP_int16 *out, /* O: Fs_high signal [inLen*3] */ - SKP_int32 *S, /* I/O: State vector [7] */ - const SKP_int16 *in, /* I: Fs_low signal [inLen] */ - const SKP_int32 inLen /* I: Input length */ -) -{ - SKP_int k, LSubFrameIn, LSubFrameOut; - SKP_int32 out_tmp, idx, inLenTmp = inLen; - SKP_int32 scratch00[ IN_SUBFR_LEN_RESAMPLE_3_1 ]; - SKP_int32 scratch0[ 3 * IN_SUBFR_LEN_RESAMPLE_3_1 ]; - SKP_int32 scratch1[ 3 * IN_SUBFR_LEN_RESAMPLE_3_1 ]; - - /* Coefficients for 3-fold resampling */ - const SKP_int16 A30[ 2 ] = { 1773, 17818 }; - const SKP_int16 A31[ 2 ] = { 4942, 25677 }; - const SKP_int16 A32[ 2 ] = { 11786, 29304 }; - - while( inLenTmp > 0 ) { - LSubFrameIn = SKP_min_int( IN_SUBFR_LEN_RESAMPLE_3_1, inLenTmp ); - LSubFrameOut = SKP_SMULBB( 3, LSubFrameIn ); - - /* Convert Q15 -> Q25 */ - for( k = 0; k < LSubFrameIn; k++ ) { - scratch00[k] = SKP_LSHIFT( (SKP_int32)in[ k ], 10 ); - } - - /* Allpass filtering */ - /* Scratch size: 2 * 3* LSubFrame * sizeof(SKP_int32) */ - SKP_Silk_allpass_int( scratch00, S + 1, A30[ 0 ], scratch1, LSubFrameIn ); - SKP_Silk_allpass_int( scratch1, S + 2, A30[ 1 ], scratch0, LSubFrameIn ); - - SKP_Silk_allpass_int( scratch00, S + 3, A31[ 0 ], scratch1, LSubFrameIn ); - SKP_Silk_allpass_int( scratch1, S + 4, A31[ 1 ], scratch0 + IN_SUBFR_LEN_RESAMPLE_3_1, LSubFrameIn ); - - SKP_Silk_allpass_int( scratch00, S + 5, A32[ 0 ], scratch1, LSubFrameIn ); - SKP_Silk_allpass_int( scratch1, S + 6, A32[ 1 ], scratch0 + 2 * IN_SUBFR_LEN_RESAMPLE_3_1, LSubFrameIn ); - - /* Interleave three allpass outputs */ - for( k = 0; k < LSubFrameIn; k++ ) { - idx = SKP_SMULBB( 3, k ); - scratch1[ idx ] = scratch0[ k ]; - scratch1[ idx + 1 ] = scratch0[ k + IN_SUBFR_LEN_RESAMPLE_3_1 ]; - scratch1[ idx + 2 ] = scratch0[ k + 2 * IN_SUBFR_LEN_RESAMPLE_3_1 ]; - } - - /* Low-pass filtering */ - SKP_Silk_lowpass_int( scratch1, S, scratch0, LSubFrameOut ); - - /* Saturate and convert to SKP_int16 */ - for( k = 0; k < LSubFrameOut; k++ ) { - out_tmp = scratch0[ k ]; - out[ k ] = (SKP_int16) SKP_SAT16( SKP_RSHIFT_ROUND( out_tmp, 10 ) ); - } - - in += LSubFrameIn; - inLenTmp -= LSubFrameIn; - out += LSubFrameOut; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_resample_3_1.c * + * * + * Upsamples by a factor 3 * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 081113 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +#define IN_SUBFR_LEN_RESAMPLE_3_1 40 + +/* Resamples by a factor 3/1 */ +void SKP_Silk_resample_3_1( + SKP_int16 *out, /* O: Fs_high signal [inLen*3] */ + SKP_int32 *S, /* I/O: State vector [7] */ + const SKP_int16 *in, /* I: Fs_low signal [inLen] */ + const SKP_int32 inLen /* I: Input length */ +) +{ + SKP_int k, LSubFrameIn, LSubFrameOut; + SKP_int32 out_tmp, idx, inLenTmp = inLen; + SKP_int32 scratch00[ IN_SUBFR_LEN_RESAMPLE_3_1 ]; + SKP_int32 scratch0[ 3 * IN_SUBFR_LEN_RESAMPLE_3_1 ]; + SKP_int32 scratch1[ 3 * IN_SUBFR_LEN_RESAMPLE_3_1 ]; + + /* Coefficients for 3-fold resampling */ + const SKP_int16 A30[ 2 ] = { 1773, 17818 }; + const SKP_int16 A31[ 2 ] = { 4942, 25677 }; + const SKP_int16 A32[ 2 ] = { 11786, 29304 }; + + while( inLenTmp > 0 ) { + LSubFrameIn = SKP_min_int( IN_SUBFR_LEN_RESAMPLE_3_1, inLenTmp ); + LSubFrameOut = SKP_SMULBB( 3, LSubFrameIn ); + + /* Convert Q15 -> Q25 */ + for( k = 0; k < LSubFrameIn; k++ ) { + scratch00[k] = SKP_LSHIFT( (SKP_int32)in[ k ], 10 ); + } + + /* Allpass filtering */ + /* Scratch size: 2 * 3* LSubFrame * sizeof(SKP_int32) */ + SKP_Silk_allpass_int( scratch00, S + 1, A30[ 0 ], scratch1, LSubFrameIn ); + SKP_Silk_allpass_int( scratch1, S + 2, A30[ 1 ], scratch0, LSubFrameIn ); + + SKP_Silk_allpass_int( scratch00, S + 3, A31[ 0 ], scratch1, LSubFrameIn ); + SKP_Silk_allpass_int( scratch1, S + 4, A31[ 1 ], scratch0 + IN_SUBFR_LEN_RESAMPLE_3_1, LSubFrameIn ); + + SKP_Silk_allpass_int( scratch00, S + 5, A32[ 0 ], scratch1, LSubFrameIn ); + SKP_Silk_allpass_int( scratch1, S + 6, A32[ 1 ], scratch0 + 2 * IN_SUBFR_LEN_RESAMPLE_3_1, LSubFrameIn ); + + /* Interleave three allpass outputs */ + for( k = 0; k < LSubFrameIn; k++ ) { + idx = SKP_SMULBB( 3, k ); + scratch1[ idx ] = scratch0[ k ]; + scratch1[ idx + 1 ] = scratch0[ k + IN_SUBFR_LEN_RESAMPLE_3_1 ]; + scratch1[ idx + 2 ] = scratch0[ k + 2 * IN_SUBFR_LEN_RESAMPLE_3_1 ]; + } + + /* Low-pass filtering */ + SKP_Silk_lowpass_int( scratch1, S, scratch0, LSubFrameOut ); + + /* Saturate and convert to SKP_int16 */ + for( k = 0; k < LSubFrameOut; k++ ) { + out_tmp = scratch0[ k ]; + out[ k ] = (SKP_int16) SKP_SAT16( SKP_RSHIFT_ROUND( out_tmp, 10 ) ); + } + + in += LSubFrameIn; + inLenTmp -= LSubFrameIn; + out += LSubFrameOut; + } +} diff --git a/jni/silk/src/SKP_Silk_resample_3_2.c b/app/src/main/jni/silk/src/SKP_Silk_resample_3_2.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_3_2.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_3_2.c index 53fe509..24f1263 100644 --- a/jni/silk/src/SKP_Silk_resample_3_2.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_3_2.c @@ -1,73 +1,73 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_3_2.c * - * * - * Resamples by a factor 3/2 * - * * - * Copyright 2008 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 081113 * - * */ - -#include "SKP_Silk_SigProc_FIX.h" - -#define IN_SUBFR_LEN_RESAMPLE_3_2 80 - -/* Resamples by a factor 3/2 */ -void SKP_Silk_resample_3_2( - SKP_int16 *out, /* O: Fs_high signal [inLen*3/2] */ - SKP_int32 *S, /* I/O: State vector [7+4] */ - const SKP_int16 *in, /* I: Fs_low signal [inLen] */ - SKP_int inLen /* I: Input length, must be a multiple of 2 */ -) -{ - SKP_int LSubFrameIn, LSubFrameOut; - SKP_int16 outH[ 3 * IN_SUBFR_LEN_RESAMPLE_3_2 ]; - SKP_int32 scratch[ ( 9 * IN_SUBFR_LEN_RESAMPLE_3_2 ) / 2 ]; - - /* Check that input is multiple of 2 */ - SKP_assert( inLen % 2 == 0 ); - - while( inLen > 0 ) { - LSubFrameIn = SKP_min_int( IN_SUBFR_LEN_RESAMPLE_3_2, inLen ); - LSubFrameOut = SKP_SMULWB( 98304, LSubFrameIn ); - - /* Upsample by a factor 3 */ - SKP_Silk_resample_3_1( outH, &S[ 0 ], in, LSubFrameIn ); - - /* Downsample by a factor 2 */ - /* Scratch size needs to be: 3 * LSubFrameOut * sizeof( SKP_int32 ) */ - SKP_Silk_resample_1_2_coarse( outH, &S[ 7 ], out, scratch, LSubFrameOut ); - - in += LSubFrameIn; - out += LSubFrameOut; - inLen -= LSubFrameIn; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_3_2.c * + * * + * Resamples by a factor 3/2 * + * * + * Copyright 2008 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 081113 * + * */ + +#include "SKP_Silk_SigProc_FIX.h" + +#define IN_SUBFR_LEN_RESAMPLE_3_2 80 + +/* Resamples by a factor 3/2 */ +void SKP_Silk_resample_3_2( + SKP_int16 *out, /* O: Fs_high signal [inLen*3/2] */ + SKP_int32 *S, /* I/O: State vector [7+4] */ + const SKP_int16 *in, /* I: Fs_low signal [inLen] */ + SKP_int inLen /* I: Input length, must be a multiple of 2 */ +) +{ + SKP_int LSubFrameIn, LSubFrameOut; + SKP_int16 outH[ 3 * IN_SUBFR_LEN_RESAMPLE_3_2 ]; + SKP_int32 scratch[ ( 9 * IN_SUBFR_LEN_RESAMPLE_3_2 ) / 2 ]; + + /* Check that input is multiple of 2 */ + SKP_assert( inLen % 2 == 0 ); + + while( inLen > 0 ) { + LSubFrameIn = SKP_min_int( IN_SUBFR_LEN_RESAMPLE_3_2, inLen ); + LSubFrameOut = SKP_SMULWB( 98304, LSubFrameIn ); + + /* Upsample by a factor 3 */ + SKP_Silk_resample_3_1( outH, &S[ 0 ], in, LSubFrameIn ); + + /* Downsample by a factor 2 */ + /* Scratch size needs to be: 3 * LSubFrameOut * sizeof( SKP_int32 ) */ + SKP_Silk_resample_1_2_coarse( outH, &S[ 7 ], out, scratch, LSubFrameOut ); + + in += LSubFrameIn; + out += LSubFrameOut; + inLen -= LSubFrameIn; + } +} diff --git a/jni/silk/src/SKP_Silk_resample_3_2_rom.c b/app/src/main/jni/silk/src/SKP_Silk_resample_3_2_rom.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_3_2_rom.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_3_2_rom.c index 3710604..af3a51c 100644 --- a/jni/silk/src/SKP_Silk_resample_3_2_rom.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_3_2_rom.c @@ -1,46 +1,46 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_3_2_rom.c * - * * - * Description: Filter coefficients for FIR polyphase resampling * - * * - * Copyright 2009 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 090424 * - * */ - -#include "SKP_Silk_resample_rom.h" - -const SKP_int16 SigProc_Resample_3_2_coarse_INTERPOL[ SigProc_Resample_3_2_coarse_NUM_INTERPOLATORS ][ SigProc_Resample_3_2_coarse_NUM_FIR_COEFS ] = { - { 0, 0, 0, 32768, 0, 0, 0, 0 }, - { -384, 1630, -5217, 26674, 12714, -3572, 1050, -236 }, - { -236, 1050, -3572, 12714, 26674, -5217, 1630, -384 }, -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_3_2_rom.c * + * * + * Description: Filter coefficients for FIR polyphase resampling * + * * + * Copyright 2009 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 090424 * + * */ + +#include "SKP_Silk_resample_rom.h" + +const SKP_int16 SigProc_Resample_3_2_coarse_INTERPOL[ SigProc_Resample_3_2_coarse_NUM_INTERPOLATORS ][ SigProc_Resample_3_2_coarse_NUM_FIR_COEFS ] = { + { 0, 0, 0, 32768, 0, 0, 0, 0 }, + { -384, 1630, -5217, 26674, 12714, -3572, 1050, -236 }, + { -236, 1050, -3572, 12714, 26674, -5217, 1630, -384 }, +}; + diff --git a/jni/silk/src/SKP_Silk_resample_3_4.c b/app/src/main/jni/silk/src/SKP_Silk_resample_3_4.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_3_4.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_3_4.c index 4b83a7a..2c00562 100644 --- a/jni/silk/src/SKP_Silk_resample_3_4.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_3_4.c @@ -1,79 +1,79 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_3_4.c * - * * - * Resamples by a factor 3/4 * - * * - * Copyright 2009 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 090408 * - * */ - -#include "SKP_Silk_SigProc_FIX.h" - -#define IN_SUBFR_LEN_RESAMPLE_3_4 80 - -/* Resamples by a factor 3/4 */ -void SKP_Silk_resample_3_4( - SKP_int16 *out, /* O: Fs_high signal [inLen*3/4] */ - SKP_int32 *S, /* I/O: State vector [7+2+6] */ - const SKP_int16 *in, /* I: Fs_low signal [inLen] */ - SKP_int inLen /* I: Input length, must be a multiple of 4 */ -) -{ - SKP_int LSubFrameIn, LSubFrameOut; - SKP_int16 outH[ 3 * IN_SUBFR_LEN_RESAMPLE_3_4 ]; - SKP_int16 outL[ ( 3 * IN_SUBFR_LEN_RESAMPLE_3_4 ) / 2 ]; - SKP_int32 scratch[ ( 9 * IN_SUBFR_LEN_RESAMPLE_3_4 ) / 2 ]; - - /* Check that input is multiple of 4 */ - SKP_assert( inLen % 4 == 0 ); - - while( inLen > 0 ) { - LSubFrameIn = SKP_min_int( IN_SUBFR_LEN_RESAMPLE_3_4, inLen ); - LSubFrameOut = SKP_SMULWB( 49152, LSubFrameIn ); - - /* Upsample by a factor 3 */ - SKP_Silk_resample_3_1( outH, &S[ 0 ], in, LSubFrameIn ); - - /* Downsample by a factor 2 twice */ - /* Scratch size needs to be: 3 * 2 * LSubFrameOut * sizeof( SKP_int32 ) */ - /* I: state vector [2], scratch memory [3*len] */ - SKP_Silk_resample_1_2_coarsest( outH, &S[ 7 ], outL, scratch, SKP_LSHIFT( LSubFrameOut, 1 ) ); - - /* Scratch size needs to be: 3 * LSubFrameOut * sizeof( SKP_int32 ) */ - /* I: state vector [6], scratch memory [3*len] */ - SKP_Silk_resample_1_2_coarse( outL, &S[ 9 ], out, scratch, LSubFrameOut ); - - in += LSubFrameIn; - out += LSubFrameOut; - inLen -= LSubFrameIn; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_3_4.c * + * * + * Resamples by a factor 3/4 * + * * + * Copyright 2009 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 090408 * + * */ + +#include "SKP_Silk_SigProc_FIX.h" + +#define IN_SUBFR_LEN_RESAMPLE_3_4 80 + +/* Resamples by a factor 3/4 */ +void SKP_Silk_resample_3_4( + SKP_int16 *out, /* O: Fs_high signal [inLen*3/4] */ + SKP_int32 *S, /* I/O: State vector [7+2+6] */ + const SKP_int16 *in, /* I: Fs_low signal [inLen] */ + SKP_int inLen /* I: Input length, must be a multiple of 4 */ +) +{ + SKP_int LSubFrameIn, LSubFrameOut; + SKP_int16 outH[ 3 * IN_SUBFR_LEN_RESAMPLE_3_4 ]; + SKP_int16 outL[ ( 3 * IN_SUBFR_LEN_RESAMPLE_3_4 ) / 2 ]; + SKP_int32 scratch[ ( 9 * IN_SUBFR_LEN_RESAMPLE_3_4 ) / 2 ]; + + /* Check that input is multiple of 4 */ + SKP_assert( inLen % 4 == 0 ); + + while( inLen > 0 ) { + LSubFrameIn = SKP_min_int( IN_SUBFR_LEN_RESAMPLE_3_4, inLen ); + LSubFrameOut = SKP_SMULWB( 49152, LSubFrameIn ); + + /* Upsample by a factor 3 */ + SKP_Silk_resample_3_1( outH, &S[ 0 ], in, LSubFrameIn ); + + /* Downsample by a factor 2 twice */ + /* Scratch size needs to be: 3 * 2 * LSubFrameOut * sizeof( SKP_int32 ) */ + /* I: state vector [2], scratch memory [3*len] */ + SKP_Silk_resample_1_2_coarsest( outH, &S[ 7 ], outL, scratch, SKP_LSHIFT( LSubFrameOut, 1 ) ); + + /* Scratch size needs to be: 3 * LSubFrameOut * sizeof( SKP_int32 ) */ + /* I: state vector [6], scratch memory [3*len] */ + SKP_Silk_resample_1_2_coarse( outL, &S[ 9 ], out, scratch, LSubFrameOut ); + + in += LSubFrameIn; + out += LSubFrameOut; + inLen -= LSubFrameIn; + } +} diff --git a/jni/silk/src/SKP_Silk_resample_4_3.c b/app/src/main/jni/silk/src/SKP_Silk_resample_4_3.c similarity index 98% rename from jni/silk/src/SKP_Silk_resample_4_3.c rename to app/src/main/jni/silk/src/SKP_Silk_resample_4_3.c index f5c4f4d..f171428 100644 --- a/jni/silk/src/SKP_Silk_resample_4_3.c +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_4_3.c @@ -1,77 +1,77 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_4_3.c * - * * - * Resamples by a factor 4/3 * - * * - * Copyright 2009 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 090407 * - * */ - -#include "SKP_Silk_SigProc_FIX.h" - -#define OUT_SUBFR_LEN 80 - -/* Resamples by a factor 4/3 */ -void SKP_Silk_resample_4_3( - SKP_int16 *out, /* O: Fs_low signal [inLen * 4/3] */ - SKP_int32 *S, /* I/O: State vector [7+4+4] */ - const SKP_int16 *in, /* I: Fs_high signal [inLen] */ - const SKP_int inLen /* I: input length, must be a multiple of 3 */ -) -{ - SKP_int outLen, LSubFrameIn, LSubFrameOut; - SKP_int16 outH[ 3 * OUT_SUBFR_LEN ]; - SKP_int16 outHH[ 6 * OUT_SUBFR_LEN ]; - SKP_int32 scratch[ 9 * OUT_SUBFR_LEN / 2 ]; - - /* Check that input is multiple of 3 */ - SKP_assert( inLen % 3 == 0 ); - - outLen = SKP_DIV32_16( SKP_LSHIFT( inLen, 2 ), 3 ); - while( outLen > 0 ) { - LSubFrameOut = SKP_min_int( OUT_SUBFR_LEN, outLen ); - LSubFrameIn = SKP_SMULWB( 49152, LSubFrameOut ); - - /* Upsample two times by a factor 2 */ - /* Scratch size needs to be: 3 * LSubFrameIn * sizeof( SKP_int32 ) */ - SKP_Silk_resample_2_1_coarse( in, &S[ 0 ], outH, scratch, LSubFrameIn ); - /* Scratch size needs to be: 6 * LSubFrameIn * sizeof( SKP_int32 ) */ - SKP_Silk_resample_2_1_coarse( outH, &S[ 4 ], outHH, scratch, SKP_LSHIFT( LSubFrameIn, 1 ) ); - - /* Downsample by a factor 3 */ - SKP_Silk_resample_1_3( out, &S[ 8 ], outHH, SKP_LSHIFT( LSubFrameIn, 2 ) ); - - in += LSubFrameIn; - out += LSubFrameOut; - outLen -= LSubFrameOut; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_4_3.c * + * * + * Resamples by a factor 4/3 * + * * + * Copyright 2009 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 090407 * + * */ + +#include "SKP_Silk_SigProc_FIX.h" + +#define OUT_SUBFR_LEN 80 + +/* Resamples by a factor 4/3 */ +void SKP_Silk_resample_4_3( + SKP_int16 *out, /* O: Fs_low signal [inLen * 4/3] */ + SKP_int32 *S, /* I/O: State vector [7+4+4] */ + const SKP_int16 *in, /* I: Fs_high signal [inLen] */ + const SKP_int inLen /* I: input length, must be a multiple of 3 */ +) +{ + SKP_int outLen, LSubFrameIn, LSubFrameOut; + SKP_int16 outH[ 3 * OUT_SUBFR_LEN ]; + SKP_int16 outHH[ 6 * OUT_SUBFR_LEN ]; + SKP_int32 scratch[ 9 * OUT_SUBFR_LEN / 2 ]; + + /* Check that input is multiple of 3 */ + SKP_assert( inLen % 3 == 0 ); + + outLen = SKP_DIV32_16( SKP_LSHIFT( inLen, 2 ), 3 ); + while( outLen > 0 ) { + LSubFrameOut = SKP_min_int( OUT_SUBFR_LEN, outLen ); + LSubFrameIn = SKP_SMULWB( 49152, LSubFrameOut ); + + /* Upsample two times by a factor 2 */ + /* Scratch size needs to be: 3 * LSubFrameIn * sizeof( SKP_int32 ) */ + SKP_Silk_resample_2_1_coarse( in, &S[ 0 ], outH, scratch, LSubFrameIn ); + /* Scratch size needs to be: 6 * LSubFrameIn * sizeof( SKP_int32 ) */ + SKP_Silk_resample_2_1_coarse( outH, &S[ 4 ], outHH, scratch, SKP_LSHIFT( LSubFrameIn, 1 ) ); + + /* Downsample by a factor 3 */ + SKP_Silk_resample_1_3( out, &S[ 8 ], outHH, SKP_LSHIFT( LSubFrameIn, 2 ) ); + + in += LSubFrameIn; + out += LSubFrameOut; + outLen -= LSubFrameOut; + } +} diff --git a/jni/silk/src/SKP_Silk_resample_rom.h b/app/src/main/jni/silk/src/SKP_Silk_resample_rom.h similarity index 98% rename from jni/silk/src/SKP_Silk_resample_rom.h rename to app/src/main/jni/silk/src/SKP_Silk_resample_rom.h index 9a32648..dce0692 100644 --- a/jni/silk/src/SKP_Silk_resample_rom.h +++ b/app/src/main/jni/silk/src/SKP_Silk_resample_rom.h @@ -1,107 +1,107 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * File Name: SKP_Silk_resample_rom.h * - * * - * Description: Header file for FIR resampling of * - * 32 and 44 kHz input * - * * - * Copyright 2007 (c), Skype Limited * - * All rights reserved. * - * * - * Date: 070807 * - * */ - -#ifndef _SKP_SILK_FIX_RESAMPLE_ROM_H_ -#define _SKP_SILK_FIX_RESAMPLE_ROM_H_ - -#include "SKP_Silk_typedef.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define SigProc_Resample_bw_1_4_NUM_INTERPOLATORS_LOG2 7 -#define SigProc_Resample_bw_1_4_NUM_INTERPOLATORS (1 << SigProc_Resample_bw_1_4_NUM_INTERPOLATORS_LOG2) -#define SigProc_Resample_bw_1_4_NUM_FIR_COEFS 6 - -extern const SKP_int16 SigProc_Resample_bw_1_4_INTERPOL[SigProc_Resample_bw_1_4_NUM_INTERPOLATORS][SigProc_Resample_bw_1_4_NUM_FIR_COEFS]; - - -#define SigProc_Resample_bw_80_441_NUM_INTERPOLATORS_LOG2 6 -#define SigProc_Resample_bw_80_441_NUM_INTERPOLATORS (1 << SigProc_Resample_bw_80_441_NUM_INTERPOLATORS_LOG2) -#define SigProc_Resample_bw_80_441_NUM_FIR_COEFS 4 - -extern const SKP_int16 SigProc_Resample_bw_80_441_INTERPOL[SigProc_Resample_bw_80_441_NUM_INTERPOLATORS][SigProc_Resample_bw_80_441_NUM_FIR_COEFS]; - -#define SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS 2 -#define SigProc_Resample_2_3_coarse_NUM_FIR_COEFS 32 - -extern const SKP_int16 SigProc_Resample_2_3_coarse_INTERPOL[SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS][SigProc_Resample_2_3_coarse_NUM_FIR_COEFS]; - -#define SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS 2 -#define SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS 10 - -extern const SKP_int16 SigProc_Resample_2_3_coarsest_INTERPOL[SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS][SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS]; - -#define SigProc_Resample_3_2_coarse_NUM_INTERPOLATORS 3 -#define SigProc_Resample_3_2_coarse_NUM_FIR_COEFS 8 - -extern const SKP_int16 SigProc_Resample_3_2_coarse_INTERPOL[SigProc_Resample_3_2_coarse_NUM_INTERPOLATORS][SigProc_Resample_3_2_coarse_NUM_FIR_COEFS]; - -#define SigProc_Resample_147_40_NUM_INTERPOLATORS 147 -#define SigProc_Resample_147_40_NUM_FIR_COEFS 20 - -extern const SKP_int16 SigProc_Resample_147_40_INTERPOL[SigProc_Resample_147_40_NUM_INTERPOLATORS][SigProc_Resample_147_40_NUM_FIR_COEFS]; - -#define SigProc_Resample_147_40_alt_NUM_INTERPOLATORS 147 -#define SigProc_Resample_147_40_alt_NUM_FIR_COEFS 10 - -extern const SKP_int16 SigProc_Resample_147_40_alt_INTERPOL[SigProc_Resample_147_40_alt_NUM_INTERPOLATORS][SigProc_Resample_147_40_alt_NUM_FIR_COEFS]; - -#define SigProc_Resample_147_40_coarse_NUM_INTERPOLATORS 147 -#define SigProc_Resample_147_40_coarse_NUM_FIR_COEFS 16 - -extern const SKP_int16 SigProc_Resample_147_40_coarse_INTERPOL[SigProc_Resample_147_40_coarse_NUM_INTERPOLATORS][SigProc_Resample_147_40_coarse_NUM_FIR_COEFS]; - -#define SigProc_Resample_40_147_NUM_INTERPOLATORS 40 -#define SigProc_Resample_40_147_NUM_FIR_COEFS 60 - -extern const SKP_int16 SigProc_Resample_40_147_INTERPOL[SigProc_Resample_40_147_NUM_INTERPOLATORS][SigProc_Resample_40_147_NUM_FIR_COEFS]; - -#define SigProc_Resample_40_147_coarse_NUM_INTERPOLATORS 40 -#define SigProc_Resample_40_147_coarse_NUM_FIR_COEFS 30 - -extern const SKP_int16 SigProc_Resample_40_147_coarse_INTERPOL[SigProc_Resample_40_147_coarse_NUM_INTERPOLATORS][SigProc_Resample_40_147_coarse_NUM_FIR_COEFS]; - -#ifdef __cplusplus -} -#endif - -#endif // _SKP_SILK_FIX_RESAMPLE_ROM_H_ +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * File Name: SKP_Silk_resample_rom.h * + * * + * Description: Header file for FIR resampling of * + * 32 and 44 kHz input * + * * + * Copyright 2007 (c), Skype Limited * + * All rights reserved. * + * * + * Date: 070807 * + * */ + +#ifndef _SKP_SILK_FIX_RESAMPLE_ROM_H_ +#define _SKP_SILK_FIX_RESAMPLE_ROM_H_ + +#include "SKP_Silk_typedef.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define SigProc_Resample_bw_1_4_NUM_INTERPOLATORS_LOG2 7 +#define SigProc_Resample_bw_1_4_NUM_INTERPOLATORS (1 << SigProc_Resample_bw_1_4_NUM_INTERPOLATORS_LOG2) +#define SigProc_Resample_bw_1_4_NUM_FIR_COEFS 6 + +extern const SKP_int16 SigProc_Resample_bw_1_4_INTERPOL[SigProc_Resample_bw_1_4_NUM_INTERPOLATORS][SigProc_Resample_bw_1_4_NUM_FIR_COEFS]; + + +#define SigProc_Resample_bw_80_441_NUM_INTERPOLATORS_LOG2 6 +#define SigProc_Resample_bw_80_441_NUM_INTERPOLATORS (1 << SigProc_Resample_bw_80_441_NUM_INTERPOLATORS_LOG2) +#define SigProc_Resample_bw_80_441_NUM_FIR_COEFS 4 + +extern const SKP_int16 SigProc_Resample_bw_80_441_INTERPOL[SigProc_Resample_bw_80_441_NUM_INTERPOLATORS][SigProc_Resample_bw_80_441_NUM_FIR_COEFS]; + +#define SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS 2 +#define SigProc_Resample_2_3_coarse_NUM_FIR_COEFS 32 + +extern const SKP_int16 SigProc_Resample_2_3_coarse_INTERPOL[SigProc_Resample_2_3_coarse_NUM_INTERPOLATORS][SigProc_Resample_2_3_coarse_NUM_FIR_COEFS]; + +#define SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS 2 +#define SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS 10 + +extern const SKP_int16 SigProc_Resample_2_3_coarsest_INTERPOL[SigProc_Resample_2_3_coarsest_NUM_INTERPOLATORS][SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS]; + +#define SigProc_Resample_3_2_coarse_NUM_INTERPOLATORS 3 +#define SigProc_Resample_3_2_coarse_NUM_FIR_COEFS 8 + +extern const SKP_int16 SigProc_Resample_3_2_coarse_INTERPOL[SigProc_Resample_3_2_coarse_NUM_INTERPOLATORS][SigProc_Resample_3_2_coarse_NUM_FIR_COEFS]; + +#define SigProc_Resample_147_40_NUM_INTERPOLATORS 147 +#define SigProc_Resample_147_40_NUM_FIR_COEFS 20 + +extern const SKP_int16 SigProc_Resample_147_40_INTERPOL[SigProc_Resample_147_40_NUM_INTERPOLATORS][SigProc_Resample_147_40_NUM_FIR_COEFS]; + +#define SigProc_Resample_147_40_alt_NUM_INTERPOLATORS 147 +#define SigProc_Resample_147_40_alt_NUM_FIR_COEFS 10 + +extern const SKP_int16 SigProc_Resample_147_40_alt_INTERPOL[SigProc_Resample_147_40_alt_NUM_INTERPOLATORS][SigProc_Resample_147_40_alt_NUM_FIR_COEFS]; + +#define SigProc_Resample_147_40_coarse_NUM_INTERPOLATORS 147 +#define SigProc_Resample_147_40_coarse_NUM_FIR_COEFS 16 + +extern const SKP_int16 SigProc_Resample_147_40_coarse_INTERPOL[SigProc_Resample_147_40_coarse_NUM_INTERPOLATORS][SigProc_Resample_147_40_coarse_NUM_FIR_COEFS]; + +#define SigProc_Resample_40_147_NUM_INTERPOLATORS 40 +#define SigProc_Resample_40_147_NUM_FIR_COEFS 60 + +extern const SKP_int16 SigProc_Resample_40_147_INTERPOL[SigProc_Resample_40_147_NUM_INTERPOLATORS][SigProc_Resample_40_147_NUM_FIR_COEFS]; + +#define SigProc_Resample_40_147_coarse_NUM_INTERPOLATORS 40 +#define SigProc_Resample_40_147_coarse_NUM_FIR_COEFS 30 + +extern const SKP_int16 SigProc_Resample_40_147_coarse_INTERPOL[SigProc_Resample_40_147_coarse_NUM_INTERPOLATORS][SigProc_Resample_40_147_coarse_NUM_FIR_COEFS]; + +#ifdef __cplusplus +} +#endif + +#endif // _SKP_SILK_FIX_RESAMPLE_ROM_H_ diff --git a/jni/silk/src/SKP_Silk_residual_energy16_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_residual_energy16_FIX.c similarity index 97% rename from jni/silk/src/SKP_Silk_residual_energy16_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_residual_energy16_FIX.c index 223ad14..3e923ec 100644 --- a/jni/silk/src/SKP_Silk_residual_energy16_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_residual_energy16_FIX.c @@ -1,99 +1,99 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ -SKP_int32 SKP_Silk_residual_energy16_covar_FIX( - const SKP_int16 *c, /* I Prediction vector */ - const SKP_int32 *wXX, /* I Correlation matrix */ - const SKP_int32 *wXx, /* I Correlation vector */ - SKP_int32 wxx, /* I Signal energy */ - SKP_int D, /* I Dimension */ - SKP_int cQ /* I Q value for c vector 0 - 15 */ -) -{ - SKP_int i, j, lshifts, Qxtra; - SKP_int32 c_max, w_max, tmp, tmp2, nrg; - SKP_int cn[ MAX_MATRIX_SIZE ]; - const SKP_int32 *pRow; - - /* Safety checks */ - SKP_assert( D >= 0 ); - SKP_assert( D <= 16 ); - SKP_assert( cQ > 0 ); - SKP_assert( cQ < 16 ); - - lshifts = 16 - cQ; - Qxtra = lshifts; - - c_max = 0; - for( i = 0; i < D; i++ ) { - c_max = SKP_max_32( c_max, SKP_abs( ( SKP_int32 )c[ i ] ) ); - } - Qxtra = SKP_min_int( Qxtra, SKP_Silk_CLZ32( c_max ) - 17 ); - - w_max = SKP_max_32( wXX[ 0 ], wXX[ D * D - 1 ] ); - Qxtra = SKP_min_int( Qxtra, SKP_Silk_CLZ32( SKP_MUL( D, SKP_RSHIFT( SKP_SMULWB( w_max, c_max ), 4 ) ) ) - 5 ); - Qxtra = SKP_max_int( Qxtra, 0 ); - for( i = 0; i < D; i++ ) { - cn[ i ] = SKP_LSHIFT( ( SKP_int )c[ i ], Qxtra ); - SKP_assert( SKP_abs(cn[i]) <= ( SKP_int16_MAX + 1 ) ); /* Check that SKP_SMLAWB can be used */ - } - lshifts -= Qxtra; - - /* Compute wxx - 2 * wXx * c */ - tmp = 0; - for( i = 0; i < D; i++ ) { - tmp = SKP_SMLAWB( tmp, wXx[ i ], cn[ i ] ); - } - nrg = SKP_RSHIFT( wxx, 1 + lshifts ) - tmp; /* Q: -lshifts - 1 */ - - /* Add c' * wXX * c, assuming wXX is symmetric */ - tmp2 = 0; - for( i = 0; i < D; i++ ) { - tmp = 0; - pRow = &wXX[ i * D ]; - for( j = i + 1; j < D; j++ ) { - tmp = SKP_SMLAWB( tmp, pRow[ j ], cn[ j ] ); - } - tmp = SKP_SMLAWB( tmp, SKP_RSHIFT( pRow[ i ], 1 ), cn[ i ] ); - tmp2 = SKP_SMLAWB( tmp2, tmp, cn[ i ] ); - } - nrg = SKP_ADD_LSHIFT32( nrg, tmp2, lshifts ); /* Q: -lshifts - 1 */ - - /* Keep one bit free always, because we add them for LSF interpolation */ - if( nrg < 1 ) { - nrg = 1; - } else if( nrg > SKP_RSHIFT( SKP_int32_MAX, lshifts + 2 ) ) { - nrg = SKP_int32_MAX >> 1; - } else { - nrg = SKP_LSHIFT( nrg, lshifts + 1 ); /* Q0 */ - } - return nrg; - -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ +SKP_int32 SKP_Silk_residual_energy16_covar_FIX( + const SKP_int16 *c, /* I Prediction vector */ + const SKP_int32 *wXX, /* I Correlation matrix */ + const SKP_int32 *wXx, /* I Correlation vector */ + SKP_int32 wxx, /* I Signal energy */ + SKP_int D, /* I Dimension */ + SKP_int cQ /* I Q value for c vector 0 - 15 */ +) +{ + SKP_int i, j, lshifts, Qxtra; + SKP_int32 c_max, w_max, tmp, tmp2, nrg; + SKP_int cn[ MAX_MATRIX_SIZE ]; + const SKP_int32 *pRow; + + /* Safety checks */ + SKP_assert( D >= 0 ); + SKP_assert( D <= 16 ); + SKP_assert( cQ > 0 ); + SKP_assert( cQ < 16 ); + + lshifts = 16 - cQ; + Qxtra = lshifts; + + c_max = 0; + for( i = 0; i < D; i++ ) { + c_max = SKP_max_32( c_max, SKP_abs( ( SKP_int32 )c[ i ] ) ); + } + Qxtra = SKP_min_int( Qxtra, SKP_Silk_CLZ32( c_max ) - 17 ); + + w_max = SKP_max_32( wXX[ 0 ], wXX[ D * D - 1 ] ); + Qxtra = SKP_min_int( Qxtra, SKP_Silk_CLZ32( SKP_MUL( D, SKP_RSHIFT( SKP_SMULWB( w_max, c_max ), 4 ) ) ) - 5 ); + Qxtra = SKP_max_int( Qxtra, 0 ); + for( i = 0; i < D; i++ ) { + cn[ i ] = SKP_LSHIFT( ( SKP_int )c[ i ], Qxtra ); + SKP_assert( SKP_abs(cn[i]) <= ( SKP_int16_MAX + 1 ) ); /* Check that SKP_SMLAWB can be used */ + } + lshifts -= Qxtra; + + /* Compute wxx - 2 * wXx * c */ + tmp = 0; + for( i = 0; i < D; i++ ) { + tmp = SKP_SMLAWB( tmp, wXx[ i ], cn[ i ] ); + } + nrg = SKP_RSHIFT( wxx, 1 + lshifts ) - tmp; /* Q: -lshifts - 1 */ + + /* Add c' * wXX * c, assuming wXX is symmetric */ + tmp2 = 0; + for( i = 0; i < D; i++ ) { + tmp = 0; + pRow = &wXX[ i * D ]; + for( j = i + 1; j < D; j++ ) { + tmp = SKP_SMLAWB( tmp, pRow[ j ], cn[ j ] ); + } + tmp = SKP_SMLAWB( tmp, SKP_RSHIFT( pRow[ i ], 1 ), cn[ i ] ); + tmp2 = SKP_SMLAWB( tmp2, tmp, cn[ i ] ); + } + nrg = SKP_ADD_LSHIFT32( nrg, tmp2, lshifts ); /* Q: -lshifts - 1 */ + + /* Keep one bit free always, because we add them for LSF interpolation */ + if( nrg < 1 ) { + nrg = 1; + } else if( nrg > SKP_RSHIFT( SKP_int32_MAX, lshifts + 2 ) ) { + nrg = SKP_int32_MAX >> 1; + } else { + nrg = SKP_LSHIFT( nrg, lshifts + 1 ); /* Q0 */ + } + return nrg; + +} diff --git a/jni/silk/src/SKP_Silk_residual_energy_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_residual_energy_FIX.c similarity index 98% rename from jni/silk/src/SKP_Silk_residual_energy_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_residual_energy_FIX.c index 640bcc2..6f0b45d 100644 --- a/jni/silk/src/SKP_Silk_residual_energy_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_residual_energy_FIX.c @@ -1,89 +1,89 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/* Calculates residual energies of input subframes where all subframes have LPC_order */ -/* of preceeding samples */ -void SKP_Silk_residual_energy_FIX( - SKP_int32 nrgs[ NB_SUBFR ], /* O Residual energy per subframe */ - SKP_int nrgsQ[ NB_SUBFR ], /* O Q value per subframe */ - const SKP_int16 x[], /* I Input signal */ - const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],/* I AR coefs for each frame half */ - const SKP_int32 gains_Qx[ NB_SUBFR ], /* I Quantization gains in Qx */ - const SKP_int Qx, /* I Quantization gains Q value */ - const SKP_int subfr_length, /* I Subframe length */ - const SKP_int LPC_order /* I LPC order */ -) -{ - SKP_int offset, i, j, rshift, lz1, lz2; - SKP_int16 *LPC_res_ptr, LPC_res[ ( MAX_FRAME_LENGTH + NB_SUBFR * MAX_LPC_ORDER ) / 2 ]; - const SKP_int16 *x_ptr; - SKP_int16 S[ MAX_LPC_ORDER ]; - SKP_int32 tmp32; - - x_ptr = x; - offset = LPC_order + subfr_length; - - /* Filter input to create the LPC residual for each frame half, and measure subframe energies */ - for( i = 0; i < 2; i++ ) { - /* Calculate half frame LPC residual signal including preceeding samples */ - SKP_memset( S, 0, LPC_order * sizeof( SKP_int16 ) ); - SKP_Silk_LPC_analysis_filter( x_ptr, a_Q12[ i ], S, LPC_res, ( NB_SUBFR >> 1 ) * offset, LPC_order ); - - /* Point to first subframe of the just calculated LPC residual signal */ - LPC_res_ptr = LPC_res + LPC_order; - for( j = 0; j < ( NB_SUBFR >> 1 ); j++ ) { - /* Measure subframe energy */ - SKP_Silk_sum_sqr_shift( &nrgs[ i * ( NB_SUBFR >> 1 ) + j ], &rshift, LPC_res_ptr, subfr_length ); - - /* Set Q values for the measured energy */ - nrgsQ[ i * ( NB_SUBFR >> 1 ) + j ] = -rshift; - - /* Move to next subframe */ - LPC_res_ptr += offset; - } - /* Move to next frame half */ - x_ptr += ( NB_SUBFR >> 1 ) * offset; - } - - /* Apply the squared subframe gains */ - for( i = 0; i < NB_SUBFR; i++ ) { - /* Fully upscale gains and energies */ - lz1 = SKP_Silk_CLZ32( nrgs[ i ] ) - 1; - lz2 = SKP_Silk_CLZ32( gains_Qx[ i ] ) - 1; - - tmp32 = SKP_LSHIFT32( gains_Qx[ i ], lz2 ); - - /* Find squared gains */ - tmp32 = SKP_SMMUL( tmp32, tmp32 ); // Q( 2 * ( Qx + lz2 ) - 32 ) - - /* Scale energies */ - nrgs[ i ] = SKP_SMMUL( tmp32, SKP_LSHIFT32( nrgs[ i ], lz1 ) ); // Q( nrgsQ[ i ] + lz1 + 2 * ( Qx + lz2 ) - 32 - 32 ) - nrgsQ[ i ] += lz1 + 2 * ( Qx + lz2 ) - 32 - 32; - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/* Calculates residual energies of input subframes where all subframes have LPC_order */ +/* of preceeding samples */ +void SKP_Silk_residual_energy_FIX( + SKP_int32 nrgs[ NB_SUBFR ], /* O Residual energy per subframe */ + SKP_int nrgsQ[ NB_SUBFR ], /* O Q value per subframe */ + const SKP_int16 x[], /* I Input signal */ + const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],/* I AR coefs for each frame half */ + const SKP_int32 gains_Qx[ NB_SUBFR ], /* I Quantization gains in Qx */ + const SKP_int Qx, /* I Quantization gains Q value */ + const SKP_int subfr_length, /* I Subframe length */ + const SKP_int LPC_order /* I LPC order */ +) +{ + SKP_int offset, i, j, rshift, lz1, lz2; + SKP_int16 *LPC_res_ptr, LPC_res[ ( MAX_FRAME_LENGTH + NB_SUBFR * MAX_LPC_ORDER ) / 2 ]; + const SKP_int16 *x_ptr; + SKP_int16 S[ MAX_LPC_ORDER ]; + SKP_int32 tmp32; + + x_ptr = x; + offset = LPC_order + subfr_length; + + /* Filter input to create the LPC residual for each frame half, and measure subframe energies */ + for( i = 0; i < 2; i++ ) { + /* Calculate half frame LPC residual signal including preceeding samples */ + SKP_memset( S, 0, LPC_order * sizeof( SKP_int16 ) ); + SKP_Silk_LPC_analysis_filter( x_ptr, a_Q12[ i ], S, LPC_res, ( NB_SUBFR >> 1 ) * offset, LPC_order ); + + /* Point to first subframe of the just calculated LPC residual signal */ + LPC_res_ptr = LPC_res + LPC_order; + for( j = 0; j < ( NB_SUBFR >> 1 ); j++ ) { + /* Measure subframe energy */ + SKP_Silk_sum_sqr_shift( &nrgs[ i * ( NB_SUBFR >> 1 ) + j ], &rshift, LPC_res_ptr, subfr_length ); + + /* Set Q values for the measured energy */ + nrgsQ[ i * ( NB_SUBFR >> 1 ) + j ] = -rshift; + + /* Move to next subframe */ + LPC_res_ptr += offset; + } + /* Move to next frame half */ + x_ptr += ( NB_SUBFR >> 1 ) * offset; + } + + /* Apply the squared subframe gains */ + for( i = 0; i < NB_SUBFR; i++ ) { + /* Fully upscale gains and energies */ + lz1 = SKP_Silk_CLZ32( nrgs[ i ] ) - 1; + lz2 = SKP_Silk_CLZ32( gains_Qx[ i ] ) - 1; + + tmp32 = SKP_LSHIFT32( gains_Qx[ i ], lz2 ); + + /* Find squared gains */ + tmp32 = SKP_SMMUL( tmp32, tmp32 ); // Q( 2 * ( Qx + lz2 ) - 32 ) + + /* Scale energies */ + nrgs[ i ] = SKP_SMMUL( tmp32, SKP_LSHIFT32( nrgs[ i ], lz1 ) ); // Q( nrgsQ[ i ] + lz1 + 2 * ( Qx + lz2 ) - 32 - 32 ) + nrgsQ[ i ] += lz1 + 2 * ( Qx + lz2 ) - 32 - 32; + } +} diff --git a/jni/silk/src/SKP_Silk_scale_copy_vector16.c b/app/src/main/jni/silk/src/SKP_Silk_scale_copy_vector16.c similarity index 98% rename from jni/silk/src/SKP_Silk_scale_copy_vector16.c rename to app/src/main/jni/silk/src/SKP_Silk_scale_copy_vector16.c index 5605772..32b56c0 100644 --- a/jni/silk/src/SKP_Silk_scale_copy_vector16.c +++ b/app/src/main/jni/silk/src/SKP_Silk_scale_copy_vector16.c @@ -1,45 +1,45 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -/* Copy and multiply a vector by a constant */ -void SKP_Silk_scale_copy_vector16( - SKP_int16 *data_out, - const SKP_int16 *data_in, - SKP_int32 gain_Q16, /* (I): gain in Q16 */ - const SKP_int dataSize /* (I): length */ -) -{ - SKP_int i; - SKP_int32 tmp32; - - for( i = 0; i < dataSize; i++ ) { - tmp32 = SKP_SMULWB( gain_Q16, data_in[ i ] ); - data_out[ i ] = (SKP_int16)SKP_CHECK_FIT16( tmp32 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +/* Copy and multiply a vector by a constant */ +void SKP_Silk_scale_copy_vector16( + SKP_int16 *data_out, + const SKP_int16 *data_in, + SKP_int32 gain_Q16, /* (I): gain in Q16 */ + const SKP_int dataSize /* (I): length */ +) +{ + SKP_int i; + SKP_int32 tmp32; + + for( i = 0; i < dataSize; i++ ) { + tmp32 = SKP_SMULWB( gain_Q16, data_in[ i ] ); + data_out[ i ] = (SKP_int16)SKP_CHECK_FIT16( tmp32 ); + } +} diff --git a/jni/silk/src/SKP_Silk_scale_vector.c b/app/src/main/jni/silk/src/SKP_Silk_scale_vector.c similarity index 97% rename from jni/silk/src/SKP_Silk_scale_vector.c rename to app/src/main/jni/silk/src/SKP_Silk_scale_vector.c index 78f4e13..0ca3132 100644 --- a/jni/silk/src/SKP_Silk_scale_vector.c +++ b/app/src/main/jni/silk/src/SKP_Silk_scale_vector.c @@ -1,122 +1,122 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_SigProc_FIX.h" - -/* Multiply a vector by a constant */ -void SKP_Silk_scale_vector16_Q14( - SKP_int16 *data1, - SKP_int gain_Q14, /* Gain in Q14 */ - SKP_int dataSize -) -{ - SKP_int i; - SKP_int32 data32, gain_Q16; - - SKP_assert( gain_Q14 < 32768 ); - SKP_assert( gain_Q14 >= -32768 ); - - gain_Q16 = SKP_LSHIFT( gain_Q14, 2 ); - - if( (SKP_int32)( (SKP_int_ptr_size)data1 & 3 ) != 0 ) { - /* Input is not 4-byte aligned */ - data1[ 0 ] = SKP_SMULWB( gain_Q16, data1[ 0 ] ); - i = 1; - } else { - i = 0; - } - dataSize--; - for( ; i < dataSize; i += 2 ) { - data32 = *( (SKP_int32 *)&data1[ i ] ); /* load two values at once */ - data1[ i ] = SKP_SMULWB( gain_Q16, data32 ); - data1[ i + 1 ] = SKP_SMULWT( gain_Q16, data32 ); - } - if( i == dataSize ) { - /* One sample left to process */ - data1[ i ] = SKP_SMULWB( gain_Q16, data1[ i ] ); - } -} - -/* Multiply a vector by a constant */ -void SKP_Silk_scale_vector32_Q26_lshift_18( - SKP_int32 *data1, /* (I/O): Q0/Q18 */ - SKP_int32 gain_Q26, /* (I): Q26 */ - SKP_int dataSize /* (I): length */ -) -{ - SKP_int i; - - for( i = 0; i < dataSize; i++ ) { - data1[ i ] = (SKP_int32)SKP_CHECK_FIT32( SKP_RSHIFT64( SKP_SMULL( data1[ i ], gain_Q26 ), 8 ) );// OUTPUT: Q18 - } -} - -/* Multiply a vector by a constant */ -void SKP_Silk_scale_vector32_16_Q14( - SKP_int32 *data1, /* (I/O): Q0/Q0 */ - SKP_int gain_Q14, /* (I): Q14 */ - SKP_int dataSize /* (I): length */ -) -{ - SKP_int i, gain_Q16; - - if( gain_Q14 < ( SKP_int16_MAX >> 2 ) ) { - gain_Q16 = SKP_LSHIFT( gain_Q14, 2 ); - for( i = 0; i < dataSize; i++ ) { - data1[ i ] = SKP_SMULWB( data1[ i ], gain_Q16 ); - } - } else { - SKP_assert( gain_Q14 >= SKP_int16_MIN ); - for( i = 0; i < dataSize; i++ ) { - data1[ i ] = SKP_LSHIFT( SKP_SMULWB( data1[ i ], gain_Q14 ), 2 ); - } - } -} - -/* Multiply a vector by a constant, does not saturate output data */ -void SKP_Silk_scale_vector32_Q16( - SKP_int32 *data1, /* (I/O): Q0/Q0 */ - SKP_int32 gain_Q16, /* (I): gain in Q16 ( SKP_int16_MIN <= gain_Q16 <= SKP_int16_MAX + 65536 ) */ - const SKP_int dataSize /* (I): length */ -) -{ - SKP_int i; - - SKP_assert( gain_Q16 <= SKP_int16_MAX + 65536 ); - SKP_assert( gain_Q16 >= SKP_int16_MIN ); - - if( gain_Q16 > SKP_int16_MAX ) { - gain_Q16 -= 65536; - for( i = 0; i < dataSize; i++ ) { - data1[ i ] = SKP_SMLAWB( data1[ i ], data1[ i ], gain_Q16 ); - } - } else { - for( i = 0; i < dataSize; i++ ) { - data1[ i ] = SKP_SMULWB( data1[ i ], gain_Q16 ); - } - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_SigProc_FIX.h" + +/* Multiply a vector by a constant */ +void SKP_Silk_scale_vector16_Q14( + SKP_int16 *data1, + SKP_int gain_Q14, /* Gain in Q14 */ + SKP_int dataSize +) +{ + SKP_int i; + SKP_int32 data32, gain_Q16; + + SKP_assert( gain_Q14 < 32768 ); + SKP_assert( gain_Q14 >= -32768 ); + + gain_Q16 = SKP_LSHIFT( gain_Q14, 2 ); + + if( (SKP_int32)( (SKP_int_ptr_size)data1 & 3 ) != 0 ) { + /* Input is not 4-byte aligned */ + data1[ 0 ] = SKP_SMULWB( gain_Q16, data1[ 0 ] ); + i = 1; + } else { + i = 0; + } + dataSize--; + for( ; i < dataSize; i += 2 ) { + data32 = *( (SKP_int32 *)&data1[ i ] ); /* load two values at once */ + data1[ i ] = SKP_SMULWB( gain_Q16, data32 ); + data1[ i + 1 ] = SKP_SMULWT( gain_Q16, data32 ); + } + if( i == dataSize ) { + /* One sample left to process */ + data1[ i ] = SKP_SMULWB( gain_Q16, data1[ i ] ); + } +} + +/* Multiply a vector by a constant */ +void SKP_Silk_scale_vector32_Q26_lshift_18( + SKP_int32 *data1, /* (I/O): Q0/Q18 */ + SKP_int32 gain_Q26, /* (I): Q26 */ + SKP_int dataSize /* (I): length */ +) +{ + SKP_int i; + + for( i = 0; i < dataSize; i++ ) { + data1[ i ] = (SKP_int32)SKP_CHECK_FIT32( SKP_RSHIFT64( SKP_SMULL( data1[ i ], gain_Q26 ), 8 ) );// OUTPUT: Q18 + } +} + +/* Multiply a vector by a constant */ +void SKP_Silk_scale_vector32_16_Q14( + SKP_int32 *data1, /* (I/O): Q0/Q0 */ + SKP_int gain_Q14, /* (I): Q14 */ + SKP_int dataSize /* (I): length */ +) +{ + SKP_int i, gain_Q16; + + if( gain_Q14 < ( SKP_int16_MAX >> 2 ) ) { + gain_Q16 = SKP_LSHIFT( gain_Q14, 2 ); + for( i = 0; i < dataSize; i++ ) { + data1[ i ] = SKP_SMULWB( data1[ i ], gain_Q16 ); + } + } else { + SKP_assert( gain_Q14 >= SKP_int16_MIN ); + for( i = 0; i < dataSize; i++ ) { + data1[ i ] = SKP_LSHIFT( SKP_SMULWB( data1[ i ], gain_Q14 ), 2 ); + } + } +} + +/* Multiply a vector by a constant, does not saturate output data */ +void SKP_Silk_scale_vector32_Q16( + SKP_int32 *data1, /* (I/O): Q0/Q0 */ + SKP_int32 gain_Q16, /* (I): gain in Q16 ( SKP_int16_MIN <= gain_Q16 <= SKP_int16_MAX + 65536 ) */ + const SKP_int dataSize /* (I): length */ +) +{ + SKP_int i; + + SKP_assert( gain_Q16 <= SKP_int16_MAX + 65536 ); + SKP_assert( gain_Q16 >= SKP_int16_MIN ); + + if( gain_Q16 > SKP_int16_MAX ) { + gain_Q16 -= 65536; + for( i = 0; i < dataSize; i++ ) { + data1[ i ] = SKP_SMLAWB( data1[ i ], data1[ i ], gain_Q16 ); + } + } else { + for( i = 0; i < dataSize; i++ ) { + data1[ i ] = SKP_SMULWB( data1[ i ], gain_Q16 ); + } + } +} diff --git a/jni/silk/src/SKP_Silk_schur.c b/app/src/main/jni/silk/src/SKP_Silk_schur.c similarity index 97% rename from jni/silk/src/SKP_Silk_schur.c rename to app/src/main/jni/silk/src/SKP_Silk_schur.c index c2da483..c6fc65c 100644 --- a/jni/silk/src/SKP_Silk_schur.c +++ b/app/src/main/jni/silk/src/SKP_Silk_schur.c @@ -1,91 +1,91 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_schur.c * - * * - * Calculates the reflection coefficients from the correlation sequence * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 080103 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Faster than schur64(), but much less accurate. */ -/* uses SMLAWB(), requiring armv5E and higher. */ -void SKP_Silk_schur( - SKP_int16 *rc_Q15, /* O: reflection coefficients [order] Q15 */ - const SKP_int32 *c, /* I: correlations [order+1] */ - const SKP_int32 order /* I: prediction order */ -) -{ - SKP_int k, n, lz; - SKP_int32 C[ SigProc_MAX_ORDER_LPC + 1 ][ 2 ]; - SKP_int32 Ctmp1, Ctmp2, rc_tmp_Q15; - - /* Get number of leading zeros */ - lz = SKP_Silk_CLZ32( c[ 0 ] ); - - /* Copy correlations and adjust level to Q30 */ - if( lz < 2 ) { - /* lz must be 1, so shift one to the right */ - for( k = 0; k < order + 1; k++ ) { - C[ k ][ 0 ] = C[ k ][ 1 ] = SKP_RSHIFT( c[ k ], 1 ); - } - } else if( lz > 2 ) { - /* Shift to the left */ - lz -= 2; - for( k = 0; k < order + 1; k++ ) { - C[ k ][ 0 ] = C[ k ][ 1 ] = SKP_LSHIFT( c[k], lz ); - } - } else { - /* No need to shift */ - for( k = 0; k < order + 1; k++ ) { - C[ k ][ 0 ] = C[ k ][ 1 ] = c[ k ]; - } - } - - for( k = 0; k < order; k++ ) { - - /* Get reflection coefficient */ - rc_tmp_Q15 = -SKP_DIV32_16( C[ k + 1 ][ 0 ], SKP_max_32( SKP_RSHIFT( C[ 0 ][ 1 ], 15 ), 1 ) ); - - /* Clip (shouldn't happen for properly conditioned inputs) */ - rc_tmp_Q15 = SKP_SAT16( rc_tmp_Q15 ); - - /* Store */ - rc_Q15[ k ] = (SKP_int16)rc_tmp_Q15; - - /* Update correlations */ - for( n = 0; n < order - k; n++ ) { - Ctmp1 = C[ n + k + 1 ][ 0 ]; - Ctmp2 = C[ n ][ 1 ]; - C[ n + k + 1 ][ 0 ] = SKP_SMLAWB( Ctmp1, SKP_LSHIFT( Ctmp2, 1 ), rc_tmp_Q15 ); - C[ n ][ 1 ] = SKP_SMLAWB( Ctmp2, SKP_LSHIFT( Ctmp1, 1 ), rc_tmp_Q15 ); - } - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_schur.c * + * * + * Calculates the reflection coefficients from the correlation sequence * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 080103 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Faster than schur64(), but much less accurate. */ +/* uses SMLAWB(), requiring armv5E and higher. */ +void SKP_Silk_schur( + SKP_int16 *rc_Q15, /* O: reflection coefficients [order] Q15 */ + const SKP_int32 *c, /* I: correlations [order+1] */ + const SKP_int32 order /* I: prediction order */ +) +{ + SKP_int k, n, lz; + SKP_int32 C[ SigProc_MAX_ORDER_LPC + 1 ][ 2 ]; + SKP_int32 Ctmp1, Ctmp2, rc_tmp_Q15; + + /* Get number of leading zeros */ + lz = SKP_Silk_CLZ32( c[ 0 ] ); + + /* Copy correlations and adjust level to Q30 */ + if( lz < 2 ) { + /* lz must be 1, so shift one to the right */ + for( k = 0; k < order + 1; k++ ) { + C[ k ][ 0 ] = C[ k ][ 1 ] = SKP_RSHIFT( c[ k ], 1 ); + } + } else if( lz > 2 ) { + /* Shift to the left */ + lz -= 2; + for( k = 0; k < order + 1; k++ ) { + C[ k ][ 0 ] = C[ k ][ 1 ] = SKP_LSHIFT( c[k], lz ); + } + } else { + /* No need to shift */ + for( k = 0; k < order + 1; k++ ) { + C[ k ][ 0 ] = C[ k ][ 1 ] = c[ k ]; + } + } + + for( k = 0; k < order; k++ ) { + + /* Get reflection coefficient */ + rc_tmp_Q15 = -SKP_DIV32_16( C[ k + 1 ][ 0 ], SKP_max_32( SKP_RSHIFT( C[ 0 ][ 1 ], 15 ), 1 ) ); + + /* Clip (shouldn't happen for properly conditioned inputs) */ + rc_tmp_Q15 = SKP_SAT16( rc_tmp_Q15 ); + + /* Store */ + rc_Q15[ k ] = (SKP_int16)rc_tmp_Q15; + + /* Update correlations */ + for( n = 0; n < order - k; n++ ) { + Ctmp1 = C[ n + k + 1 ][ 0 ]; + Ctmp2 = C[ n ][ 1 ]; + C[ n + k + 1 ][ 0 ] = SKP_SMLAWB( Ctmp1, SKP_LSHIFT( Ctmp2, 1 ), rc_tmp_Q15 ); + C[ n ][ 1 ] = SKP_SMLAWB( Ctmp2, SKP_LSHIFT( Ctmp1, 1 ), rc_tmp_Q15 ); + } + } +} diff --git a/jni/silk/src/SKP_Silk_schur64.c b/app/src/main/jni/silk/src/SKP_Silk_schur64.c similarity index 98% rename from jni/silk/src/SKP_Silk_schur64.c rename to app/src/main/jni/silk/src/SKP_Silk_schur64.c index 048b684..2cfae34 100644 --- a/jni/silk/src/SKP_Silk_schur64.c +++ b/app/src/main/jni/silk/src/SKP_Silk_schur64.c @@ -1,80 +1,80 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_schur64.c * - * * - * Calculates the reflection coefficients from the correlation sequence * - * using extra precision * - * * - * Copyright 2008 (c), Skype Limited * - * Date: 080103 * - * */ -#include "SKP_Silk_SigProc_FIX.h" - -/* Slower than schur(), but more accurate. */ -/* Uses SMULL(), available on armv4 */ -SKP_int32 SKP_Silk_schur64( /* O: Returns residual energy */ - SKP_int32 rc_Q16[], /* O: Reflection coefficients [order] Q16 */ - const SKP_int32 c[], /* I: Correlations [order+1] */ - SKP_int32 order /* I: Prediction order */ -) -{ - SKP_int k, n; - SKP_int32 C[ SigProc_MAX_ORDER_LPC + 1 ][ 2 ]; - SKP_int32 Ctmp1_Q30, Ctmp2_Q30, rc_tmp_Q31; - - /* Check for invalid input */ - if( c[ 0 ] <= 0 ) { - SKP_memset( rc_Q16, 0, order * sizeof( SKP_int32 ) ); - return 0; - } - - for( k = 0; k < order + 1; k++ ) { - C[ k ][ 0 ] = C[ k ][ 1 ] = c[ k ]; - } - - for( k = 0; k < order; k++ ) { - /* Get reflection coefficient: divide two Q30 values and get result in Q31 */ - rc_tmp_Q31 = SKP_DIV32_varQ( -C[ k + 1 ][ 0 ], C[ 0 ][ 1 ], 31 ); - - /* Save the output */ - rc_Q16[ k ] = SKP_RSHIFT_ROUND( rc_tmp_Q31, 15 ); - - /* Update correlations */ - for( n = 0; n < order - k; n++ ) { - Ctmp1_Q30 = C[ n + k + 1 ][ 0 ]; - Ctmp2_Q30 = C[ n ][ 1 ]; - - /* Multiply and add the highest int32 */ - C[ n + k + 1 ][ 0 ] = Ctmp1_Q30 + SKP_SMMUL( SKP_LSHIFT( Ctmp2_Q30, 1 ), rc_tmp_Q31 ); - C[ n ][ 1 ] = Ctmp2_Q30 + SKP_SMMUL( SKP_LSHIFT( Ctmp1_Q30, 1 ), rc_tmp_Q31 ); - } - } - - return( C[ 0 ][ 1 ] ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_schur64.c * + * * + * Calculates the reflection coefficients from the correlation sequence * + * using extra precision * + * * + * Copyright 2008 (c), Skype Limited * + * Date: 080103 * + * */ +#include "SKP_Silk_SigProc_FIX.h" + +/* Slower than schur(), but more accurate. */ +/* Uses SMULL(), available on armv4 */ +SKP_int32 SKP_Silk_schur64( /* O: Returns residual energy */ + SKP_int32 rc_Q16[], /* O: Reflection coefficients [order] Q16 */ + const SKP_int32 c[], /* I: Correlations [order+1] */ + SKP_int32 order /* I: Prediction order */ +) +{ + SKP_int k, n; + SKP_int32 C[ SigProc_MAX_ORDER_LPC + 1 ][ 2 ]; + SKP_int32 Ctmp1_Q30, Ctmp2_Q30, rc_tmp_Q31; + + /* Check for invalid input */ + if( c[ 0 ] <= 0 ) { + SKP_memset( rc_Q16, 0, order * sizeof( SKP_int32 ) ); + return 0; + } + + for( k = 0; k < order + 1; k++ ) { + C[ k ][ 0 ] = C[ k ][ 1 ] = c[ k ]; + } + + for( k = 0; k < order; k++ ) { + /* Get reflection coefficient: divide two Q30 values and get result in Q31 */ + rc_tmp_Q31 = SKP_DIV32_varQ( -C[ k + 1 ][ 0 ], C[ 0 ][ 1 ], 31 ); + + /* Save the output */ + rc_Q16[ k ] = SKP_RSHIFT_ROUND( rc_tmp_Q31, 15 ); + + /* Update correlations */ + for( n = 0; n < order - k; n++ ) { + Ctmp1_Q30 = C[ n + k + 1 ][ 0 ]; + Ctmp2_Q30 = C[ n ][ 1 ]; + + /* Multiply and add the highest int32 */ + C[ n + k + 1 ][ 0 ] = Ctmp1_Q30 + SKP_SMMUL( SKP_LSHIFT( Ctmp2_Q30, 1 ), rc_tmp_Q31 ); + C[ n ][ 1 ] = Ctmp2_Q30 + SKP_SMMUL( SKP_LSHIFT( Ctmp1_Q30, 1 ), rc_tmp_Q31 ); + } + } + + return( C[ 0 ][ 1 ] ); +} diff --git a/jni/silk/src/SKP_Silk_shell_coder.c b/app/src/main/jni/silk/src/SKP_Silk_shell_coder.c similarity index 98% rename from jni/silk/src/SKP_Silk_shell_coder.c rename to app/src/main/jni/silk/src/SKP_Silk_shell_coder.c index 8e39582..b95a9d9 100644 --- a/jni/silk/src/SKP_Silk_shell_coder.c +++ b/app/src/main/jni/silk/src/SKP_Silk_shell_coder.c @@ -1,155 +1,155 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main.h" - -/* shell coder; pulse-subframe length is hardcoded */ - -SKP_INLINE void combine_pulses( - SKP_int *out, /* O: combined pulses vector [len] */ - const SKP_int *in, /* I: input vector [2 * len] */ - const SKP_int len /* I: number of OUTPUT samples */ -) -{ - SKP_int k; - for( k = 0; k < len; k++ ) { - out[ k ] = in[ 2 * k ] + in[ 2 * k + 1 ]; - } -} - -SKP_INLINE void encode_split( - SKP_Silk_range_coder_state *sRC, /* I/O: compressor data structure */ - const SKP_int p_child1, /* I: pulse amplitude of first child subframe */ - const SKP_int p, /* I: pulse amplitude of current subframe */ - const SKP_uint16 *shell_table /* I: table of shell cdfs */ -) -{ - const SKP_uint16 *cdf; - - if( p > 0 ) { - cdf = &shell_table[ SKP_Silk_shell_code_table_offsets[ p ] ]; - SKP_Silk_range_encoder( sRC, p_child1, cdf ); - } -} - -SKP_INLINE void decode_split( - SKP_int *p_child1, /* O: pulse amplitude of first child subframe */ - SKP_int *p_child2, /* O: pulse amplitude of second child subframe */ - SKP_Silk_range_coder_state *sRC, /* I/O: compressor data structure */ - const SKP_int p, /* I: pulse amplitude of current subframe */ - const SKP_uint16 *shell_table /* I: table of shell cdfs */ -) -{ - SKP_int cdf_middle; - const SKP_uint16 *cdf; - - if( p > 0 ) { - cdf_middle = SKP_RSHIFT( p, 1 ); - cdf = &shell_table[ SKP_Silk_shell_code_table_offsets[ p ] ]; - SKP_Silk_range_decoder( p_child1, sRC, cdf, cdf_middle ); - p_child2[ 0 ] = p - p_child1[ 0 ]; - } else { - p_child1[ 0 ] = 0; - p_child2[ 0 ] = 0; - } -} - -/* Shell encoder, operates on one shell code frame of 16 pulses */ -void SKP_Silk_shell_encoder( - SKP_Silk_range_coder_state *sRC, /* I/O compressor data structure */ - const SKP_int *pulses0 /* I data: nonnegative pulse amplitudes */ -) -{ - SKP_int pulses1[ 8 ], pulses2[ 4 ], pulses3[ 2 ], pulses4[ 1 ]; - - /* this function operates on one shell code frame of 16 pulses */ - SKP_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); - - /* tree representation per pulse-subframe */ - combine_pulses( pulses1, pulses0, 8 ); - combine_pulses( pulses2, pulses1, 4 ); - combine_pulses( pulses3, pulses2, 2 ); - combine_pulses( pulses4, pulses3, 1 ); - - encode_split( sRC, pulses3[ 0 ], pulses4[ 0 ], SKP_Silk_shell_code_table3 ); - - encode_split( sRC, pulses2[ 0 ], pulses3[ 0 ], SKP_Silk_shell_code_table2 ); - - encode_split( sRC, pulses1[ 0 ], pulses2[ 0 ], SKP_Silk_shell_code_table1 ); - encode_split( sRC, pulses0[ 0 ], pulses1[ 0 ], SKP_Silk_shell_code_table0 ); - encode_split( sRC, pulses0[ 2 ], pulses1[ 1 ], SKP_Silk_shell_code_table0 ); - - encode_split( sRC, pulses1[ 2 ], pulses2[ 1 ], SKP_Silk_shell_code_table1 ); - encode_split( sRC, pulses0[ 4 ], pulses1[ 2 ], SKP_Silk_shell_code_table0 ); - encode_split( sRC, pulses0[ 6 ], pulses1[ 3 ], SKP_Silk_shell_code_table0 ); - - encode_split( sRC, pulses2[ 2 ], pulses3[ 1 ], SKP_Silk_shell_code_table2 ); - - encode_split( sRC, pulses1[ 4 ], pulses2[ 2 ], SKP_Silk_shell_code_table1 ); - encode_split( sRC, pulses0[ 8 ], pulses1[ 4 ], SKP_Silk_shell_code_table0 ); - encode_split( sRC, pulses0[ 10 ], pulses1[ 5 ], SKP_Silk_shell_code_table0 ); - - encode_split( sRC, pulses1[ 6 ], pulses2[ 3 ], SKP_Silk_shell_code_table1 ); - encode_split( sRC, pulses0[ 12 ], pulses1[ 6 ], SKP_Silk_shell_code_table0 ); - encode_split( sRC, pulses0[ 14 ], pulses1[ 7 ], SKP_Silk_shell_code_table0 ); -} - - -/* Shell decoder, operates on one shell code frame of 16 pulses */ -void SKP_Silk_shell_decoder( - SKP_int *pulses0, /* O data: nonnegative pulse amplitudes */ - SKP_Silk_range_coder_state *sRC, /* I/O compressor data structure */ - const SKP_int pulses4 /* I number of pulses per pulse-subframe */ -) -{ - SKP_int pulses3[ 2 ], pulses2[ 4 ], pulses1[ 8 ]; - - /* this function operates on one shell code frame of 16 pulses */ - SKP_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); - - decode_split( &pulses3[ 0 ], &pulses3[ 1 ], sRC, pulses4, SKP_Silk_shell_code_table3 ); - - decode_split( &pulses2[ 0 ], &pulses2[ 1 ], sRC, pulses3[ 0 ], SKP_Silk_shell_code_table2 ); - - decode_split( &pulses1[ 0 ], &pulses1[ 1 ], sRC, pulses2[ 0 ], SKP_Silk_shell_code_table1 ); - decode_split( &pulses0[ 0 ], &pulses0[ 1 ], sRC, pulses1[ 0 ], SKP_Silk_shell_code_table0 ); - decode_split( &pulses0[ 2 ], &pulses0[ 3 ], sRC, pulses1[ 1 ], SKP_Silk_shell_code_table0 ); - - decode_split( &pulses1[ 2 ], &pulses1[ 3 ], sRC, pulses2[ 1 ], SKP_Silk_shell_code_table1 ); - decode_split( &pulses0[ 4 ], &pulses0[ 5 ], sRC, pulses1[ 2 ], SKP_Silk_shell_code_table0 ); - decode_split( &pulses0[ 6 ], &pulses0[ 7 ], sRC, pulses1[ 3 ], SKP_Silk_shell_code_table0 ); - - decode_split( &pulses2[ 2 ], &pulses2[ 3 ], sRC, pulses3[ 1 ], SKP_Silk_shell_code_table2 ); - - decode_split( &pulses1[ 4 ], &pulses1[ 5 ], sRC, pulses2[ 2 ], SKP_Silk_shell_code_table1 ); - decode_split( &pulses0[ 8 ], &pulses0[ 9 ], sRC, pulses1[ 4 ], SKP_Silk_shell_code_table0 ); - decode_split( &pulses0[ 10 ], &pulses0[ 11 ], sRC, pulses1[ 5 ], SKP_Silk_shell_code_table0 ); - - decode_split( &pulses1[ 6 ], &pulses1[ 7 ], sRC, pulses2[ 3 ], SKP_Silk_shell_code_table1 ); - decode_split( &pulses0[ 12 ], &pulses0[ 13 ], sRC, pulses1[ 6 ], SKP_Silk_shell_code_table0 ); - decode_split( &pulses0[ 14 ], &pulses0[ 15 ], sRC, pulses1[ 7 ], SKP_Silk_shell_code_table0 ); -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main.h" + +/* shell coder; pulse-subframe length is hardcoded */ + +SKP_INLINE void combine_pulses( + SKP_int *out, /* O: combined pulses vector [len] */ + const SKP_int *in, /* I: input vector [2 * len] */ + const SKP_int len /* I: number of OUTPUT samples */ +) +{ + SKP_int k; + for( k = 0; k < len; k++ ) { + out[ k ] = in[ 2 * k ] + in[ 2 * k + 1 ]; + } +} + +SKP_INLINE void encode_split( + SKP_Silk_range_coder_state *sRC, /* I/O: compressor data structure */ + const SKP_int p_child1, /* I: pulse amplitude of first child subframe */ + const SKP_int p, /* I: pulse amplitude of current subframe */ + const SKP_uint16 *shell_table /* I: table of shell cdfs */ +) +{ + const SKP_uint16 *cdf; + + if( p > 0 ) { + cdf = &shell_table[ SKP_Silk_shell_code_table_offsets[ p ] ]; + SKP_Silk_range_encoder( sRC, p_child1, cdf ); + } +} + +SKP_INLINE void decode_split( + SKP_int *p_child1, /* O: pulse amplitude of first child subframe */ + SKP_int *p_child2, /* O: pulse amplitude of second child subframe */ + SKP_Silk_range_coder_state *sRC, /* I/O: compressor data structure */ + const SKP_int p, /* I: pulse amplitude of current subframe */ + const SKP_uint16 *shell_table /* I: table of shell cdfs */ +) +{ + SKP_int cdf_middle; + const SKP_uint16 *cdf; + + if( p > 0 ) { + cdf_middle = SKP_RSHIFT( p, 1 ); + cdf = &shell_table[ SKP_Silk_shell_code_table_offsets[ p ] ]; + SKP_Silk_range_decoder( p_child1, sRC, cdf, cdf_middle ); + p_child2[ 0 ] = p - p_child1[ 0 ]; + } else { + p_child1[ 0 ] = 0; + p_child2[ 0 ] = 0; + } +} + +/* Shell encoder, operates on one shell code frame of 16 pulses */ +void SKP_Silk_shell_encoder( + SKP_Silk_range_coder_state *sRC, /* I/O compressor data structure */ + const SKP_int *pulses0 /* I data: nonnegative pulse amplitudes */ +) +{ + SKP_int pulses1[ 8 ], pulses2[ 4 ], pulses3[ 2 ], pulses4[ 1 ]; + + /* this function operates on one shell code frame of 16 pulses */ + SKP_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); + + /* tree representation per pulse-subframe */ + combine_pulses( pulses1, pulses0, 8 ); + combine_pulses( pulses2, pulses1, 4 ); + combine_pulses( pulses3, pulses2, 2 ); + combine_pulses( pulses4, pulses3, 1 ); + + encode_split( sRC, pulses3[ 0 ], pulses4[ 0 ], SKP_Silk_shell_code_table3 ); + + encode_split( sRC, pulses2[ 0 ], pulses3[ 0 ], SKP_Silk_shell_code_table2 ); + + encode_split( sRC, pulses1[ 0 ], pulses2[ 0 ], SKP_Silk_shell_code_table1 ); + encode_split( sRC, pulses0[ 0 ], pulses1[ 0 ], SKP_Silk_shell_code_table0 ); + encode_split( sRC, pulses0[ 2 ], pulses1[ 1 ], SKP_Silk_shell_code_table0 ); + + encode_split( sRC, pulses1[ 2 ], pulses2[ 1 ], SKP_Silk_shell_code_table1 ); + encode_split( sRC, pulses0[ 4 ], pulses1[ 2 ], SKP_Silk_shell_code_table0 ); + encode_split( sRC, pulses0[ 6 ], pulses1[ 3 ], SKP_Silk_shell_code_table0 ); + + encode_split( sRC, pulses2[ 2 ], pulses3[ 1 ], SKP_Silk_shell_code_table2 ); + + encode_split( sRC, pulses1[ 4 ], pulses2[ 2 ], SKP_Silk_shell_code_table1 ); + encode_split( sRC, pulses0[ 8 ], pulses1[ 4 ], SKP_Silk_shell_code_table0 ); + encode_split( sRC, pulses0[ 10 ], pulses1[ 5 ], SKP_Silk_shell_code_table0 ); + + encode_split( sRC, pulses1[ 6 ], pulses2[ 3 ], SKP_Silk_shell_code_table1 ); + encode_split( sRC, pulses0[ 12 ], pulses1[ 6 ], SKP_Silk_shell_code_table0 ); + encode_split( sRC, pulses0[ 14 ], pulses1[ 7 ], SKP_Silk_shell_code_table0 ); +} + + +/* Shell decoder, operates on one shell code frame of 16 pulses */ +void SKP_Silk_shell_decoder( + SKP_int *pulses0, /* O data: nonnegative pulse amplitudes */ + SKP_Silk_range_coder_state *sRC, /* I/O compressor data structure */ + const SKP_int pulses4 /* I number of pulses per pulse-subframe */ +) +{ + SKP_int pulses3[ 2 ], pulses2[ 4 ], pulses1[ 8 ]; + + /* this function operates on one shell code frame of 16 pulses */ + SKP_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); + + decode_split( &pulses3[ 0 ], &pulses3[ 1 ], sRC, pulses4, SKP_Silk_shell_code_table3 ); + + decode_split( &pulses2[ 0 ], &pulses2[ 1 ], sRC, pulses3[ 0 ], SKP_Silk_shell_code_table2 ); + + decode_split( &pulses1[ 0 ], &pulses1[ 1 ], sRC, pulses2[ 0 ], SKP_Silk_shell_code_table1 ); + decode_split( &pulses0[ 0 ], &pulses0[ 1 ], sRC, pulses1[ 0 ], SKP_Silk_shell_code_table0 ); + decode_split( &pulses0[ 2 ], &pulses0[ 3 ], sRC, pulses1[ 1 ], SKP_Silk_shell_code_table0 ); + + decode_split( &pulses1[ 2 ], &pulses1[ 3 ], sRC, pulses2[ 1 ], SKP_Silk_shell_code_table1 ); + decode_split( &pulses0[ 4 ], &pulses0[ 5 ], sRC, pulses1[ 2 ], SKP_Silk_shell_code_table0 ); + decode_split( &pulses0[ 6 ], &pulses0[ 7 ], sRC, pulses1[ 3 ], SKP_Silk_shell_code_table0 ); + + decode_split( &pulses2[ 2 ], &pulses2[ 3 ], sRC, pulses3[ 1 ], SKP_Silk_shell_code_table2 ); + + decode_split( &pulses1[ 4 ], &pulses1[ 5 ], sRC, pulses2[ 2 ], SKP_Silk_shell_code_table1 ); + decode_split( &pulses0[ 8 ], &pulses0[ 9 ], sRC, pulses1[ 4 ], SKP_Silk_shell_code_table0 ); + decode_split( &pulses0[ 10 ], &pulses0[ 11 ], sRC, pulses1[ 5 ], SKP_Silk_shell_code_table0 ); + + decode_split( &pulses1[ 6 ], &pulses1[ 7 ], sRC, pulses2[ 3 ], SKP_Silk_shell_code_table1 ); + decode_split( &pulses0[ 12 ], &pulses0[ 13 ], sRC, pulses1[ 6 ], SKP_Silk_shell_code_table0 ); + decode_split( &pulses0[ 14 ], &pulses0[ 15 ], sRC, pulses1[ 7 ], SKP_Silk_shell_code_table0 ); +} diff --git a/jni/silk/src/SKP_Silk_sigm_Q15.c b/app/src/main/jni/silk/src/SKP_Silk_sigm_Q15.c similarity index 97% rename from jni/silk/src/SKP_Silk_sigm_Q15.c rename to app/src/main/jni/silk/src/SKP_Silk_sigm_Q15.c index 95c0563..d7705f9 100644 --- a/jni/silk/src/SKP_Silk_sigm_Q15.c +++ b/app/src/main/jni/silk/src/SKP_Silk_sigm_Q15.c @@ -1,78 +1,78 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_sigm_Q15.c * - * * - * Approximate sigmoid function * - * * - * Copyright 2006 (c), Skype Limited * - * Date: 060221 * - * */ -#include "SKP_Silk_SigProc_FIX.h" -/********************************/ -/* approximate sigmoid function */ -/********************************/ -/* fprintf(1, '%d, ', round(1024 * ([1 ./ (1 + exp(-(1:5))), 1] - 1 ./ (1 + exp(-(0:5)))))); */ -static const SKP_int32 sigm_LUT_slope_Q10[ 6 ] = { - 237, 153, 73, 30, 12, 7 -}; -/* fprintf(1, '%d, ', round(32767 * 1 ./ (1 + exp(-(0:5))))); */ -static const SKP_int32 sigm_LUT_pos_Q15[ 6 ] = { - 16384, 23955, 28861, 31213, 32178, 32548 -}; -/* fprintf(1, '%d, ', round(32767 * 1 ./ (1 + exp((0:5))))); */ -static const SKP_int32 sigm_LUT_neg_Q15[ 6 ] = { - 16384, 8812, 3906, 1554, 589, 219 -}; - -SKP_int SKP_Silk_sigm_Q15( SKP_int in_Q5 ) -{ - SKP_int ind; - - if( in_Q5 < 0 ) { - /* Negative input */ - in_Q5 = -in_Q5; - if( in_Q5 >= 6 * 32 ) { - return 0; /* Clip */ - } else { - /* Linear interpolation of look up table */ - ind = SKP_RSHIFT( in_Q5, 5 ); - return( sigm_LUT_neg_Q15[ ind ] - SKP_SMULBB( sigm_LUT_slope_Q10[ ind ], in_Q5 & 0x1F ) ); - } - } else { - /* Positive input */ - if( in_Q5 >= 6 * 32 ) { - return 32767; /* clip */ - } else { - /* Linear interpolation of look up table */ - ind = SKP_RSHIFT( in_Q5, 5 ); - return( sigm_LUT_pos_Q15[ ind ] + SKP_SMULBB( sigm_LUT_slope_Q10[ ind ], in_Q5 & 0x1F ) ); - } - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_sigm_Q15.c * + * * + * Approximate sigmoid function * + * * + * Copyright 2006 (c), Skype Limited * + * Date: 060221 * + * */ +#include "SKP_Silk_SigProc_FIX.h" +/********************************/ +/* approximate sigmoid function */ +/********************************/ +/* fprintf(1, '%d, ', round(1024 * ([1 ./ (1 + exp(-(1:5))), 1] - 1 ./ (1 + exp(-(0:5)))))); */ +static const SKP_int32 sigm_LUT_slope_Q10[ 6 ] = { + 237, 153, 73, 30, 12, 7 +}; +/* fprintf(1, '%d, ', round(32767 * 1 ./ (1 + exp(-(0:5))))); */ +static const SKP_int32 sigm_LUT_pos_Q15[ 6 ] = { + 16384, 23955, 28861, 31213, 32178, 32548 +}; +/* fprintf(1, '%d, ', round(32767 * 1 ./ (1 + exp((0:5))))); */ +static const SKP_int32 sigm_LUT_neg_Q15[ 6 ] = { + 16384, 8812, 3906, 1554, 589, 219 +}; + +SKP_int SKP_Silk_sigm_Q15( SKP_int in_Q5 ) +{ + SKP_int ind; + + if( in_Q5 < 0 ) { + /* Negative input */ + in_Q5 = -in_Q5; + if( in_Q5 >= 6 * 32 ) { + return 0; /* Clip */ + } else { + /* Linear interpolation of look up table */ + ind = SKP_RSHIFT( in_Q5, 5 ); + return( sigm_LUT_neg_Q15[ ind ] - SKP_SMULBB( sigm_LUT_slope_Q10[ ind ], in_Q5 & 0x1F ) ); + } + } else { + /* Positive input */ + if( in_Q5 >= 6 * 32 ) { + return 32767; /* clip */ + } else { + /* Linear interpolation of look up table */ + ind = SKP_RSHIFT( in_Q5, 5 ); + return( sigm_LUT_pos_Q15[ ind ] + SKP_SMULBB( sigm_LUT_slope_Q10[ ind ], in_Q5 & 0x1F ) ); + } + } +} + diff --git a/jni/silk/src/SKP_Silk_solve_LS_FIX.c b/app/src/main/jni/silk/src/SKP_Silk_solve_LS_FIX.c similarity index 97% rename from jni/silk/src/SKP_Silk_solve_LS_FIX.c rename to app/src/main/jni/silk/src/SKP_Silk_solve_LS_FIX.c index 23c0c9c..fc451ea 100644 --- a/jni/silk/src/SKP_Silk_solve_LS_FIX.c +++ b/app/src/main/jni/silk/src/SKP_Silk_solve_LS_FIX.c @@ -1,240 +1,240 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_main_FIX.h" - -/*****************************/ -/* Internal function headers */ -/*****************************/ - -typedef struct { - SKP_int32 Q36_part; - SKP_int32 Q48_part; -} inv_D_t; - -/* Factorize square matrix A into LDL form */ -SKP_INLINE void SKP_Silk_LDL_factorize_FIX( - SKP_int32 *A, /* I/O Pointer to Symetric Square Matrix */ - SKP_int M, /* I Size of Matrix */ - SKP_int32 *L_Q16, /* I/O Pointer to Square Upper triangular Matrix */ - inv_D_t *inv_D /* I/O Pointer to vector holding inverted diagonal elements of D */ -); - -/* Solve Lx = b, when L is lower triangular and has ones on the diagonal */ -SKP_INLINE void SKP_Silk_LS_SolveFirst_FIX( - const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ - SKP_int M, /* I Dim of Matrix equation */ - const SKP_int32 *b, /* I b Vector */ - SKP_int32 *x_Q16 /* O x Vector */ -); - -/* Solve L^t*x = b, where L is lower triangular with ones on the diagonal */ -SKP_INLINE void SKP_Silk_LS_SolveLast_FIX( - const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ - const SKP_int M, /* I Dim of Matrix equation */ - const SKP_int32 *b, /* I b Vector */ - SKP_int32 *x_Q16 /* O x Vector */ -); - -SKP_INLINE void SKP_Silk_LS_divide_Q16_FIX( - SKP_int32 T[], /* I/O Numenator vector */ - inv_D_t *inv_D, /* I 1 / D vector */ - SKP_int M /* I dimension */ -); - -/* Solves Ax = b, assuming A is symmetric */ -void SKP_Silk_solve_LDL_FIX( - SKP_int32 *A, /* I Pointer to symetric square matrix A */ - SKP_int M, /* I Size of matrix */ - const SKP_int32 *b, /* I Pointer to b vector */ - SKP_int32 *x_Q16 /* O Pointer to x solution vector */ -) -{ - SKP_int32 L_Q16[ MAX_MATRIX_SIZE * MAX_MATRIX_SIZE ]; - SKP_int32 Y[ MAX_MATRIX_SIZE ]; - inv_D_t inv_D[ MAX_MATRIX_SIZE ]; - - SKP_assert( M <= MAX_MATRIX_SIZE ); - - /*************************************************** - Factorize A by LDL such that A = L*D*L', - where L is lower triangular with ones on diagonal - ****************************************************/ - SKP_Silk_LDL_factorize_FIX( A, M, L_Q16, inv_D ); - - /**************************************************** - * substitute D*L'*x = Y. ie: - L*D*L'*x = b => L*Y = b <=> Y = inv(L)*b - ******************************************************/ - SKP_Silk_LS_SolveFirst_FIX( L_Q16, M, b, Y ); - - /**************************************************** - D*L'*x = Y <=> L'*x = inv(D)*Y, because D is - diagonal just multiply with 1/d_i - ****************************************************/ - SKP_Silk_LS_divide_Q16_FIX( Y, inv_D, M ); - - /**************************************************** - x = inv(L') * inv(D) * Y - *****************************************************/ - SKP_Silk_LS_SolveLast_FIX( L_Q16, M, Y, x_Q16 ); -} - -SKP_INLINE void SKP_Silk_LDL_factorize_FIX( - SKP_int32 *A, /* I Pointer to Symetric Square Matrix */ - SKP_int M, /* I Size of Matrix */ - SKP_int32 *L_Q16, /* I/O Pointer to Square Upper triangular Matrix */ - inv_D_t *inv_D /* I/O Pointer to vector holding inverted diagonal elements of D */ -) -{ - SKP_int i, j, k, status, loop_count; - const SKP_int32 *ptr1, *ptr2; - SKP_int32 diag_min_value, tmp_32, err; - SKP_int32 v_Q0[ MAX_MATRIX_SIZE ], D_Q0[ MAX_MATRIX_SIZE ]; - SKP_int32 one_div_diag_Q36, one_div_diag_Q40, one_div_diag_Q48; - - SKP_assert( M <= MAX_MATRIX_SIZE ); - - status = 1; - diag_min_value = SKP_max_32( SKP_SMMUL( SKP_ADD_SAT32( A[ 0 ], A[ SKP_SMULBB( M, M ) - 1 ] ), FIND_LTP_COND_FAC_Q31 ), 1 << 9 ); - for( loop_count = 0; loop_count < M && status == 1; loop_count++ ) { - status = 0; - for( j = 0; j < M; j++ ) { - ptr1 = matrix_adr( L_Q16, j, 0, M ); - tmp_32 = 0; - for( i = 0; i < j; i++ ) { - v_Q0[ i ] = SKP_SMULWW( D_Q0[ i ], ptr1[ i ] ); /* Q0 */ - tmp_32 = SKP_SMLAWW( tmp_32, v_Q0[ i ], ptr1[ i ] ); /* Q0 */ - } - tmp_32 = SKP_SUB32( matrix_ptr( A, j, j, M ), tmp_32 ); - - if( tmp_32 < diag_min_value ) { - tmp_32 = SKP_SUB32( SKP_SMULBB( loop_count + 1, diag_min_value ), tmp_32 ); - /* Matrix not positive semi-definite, or ill conditioned */ - for( i = 0; i < M; i++ ) { - matrix_ptr( A, i, i, M ) = SKP_ADD32( matrix_ptr( A, i, i, M ), tmp_32 ); - } - status = 1; - break; - } - D_Q0[ j ] = tmp_32; /* always < max(Correlation) */ - - /* two-step division */ - one_div_diag_Q36 = SKP_INVERSE32_varQ( tmp_32, 36 ); /* Q36 */ - one_div_diag_Q40 = SKP_LSHIFT( one_div_diag_Q36, 4 ); /* Q40 */ - err = SKP_SUB32( 1 << 24, SKP_SMULWW( tmp_32, one_div_diag_Q40 ) ); /* Q24 */ - one_div_diag_Q48 = SKP_SMULWW( err, one_div_diag_Q40 ); /* Q48 */ - - /* Save 1/Ds */ - inv_D[ j ].Q36_part = one_div_diag_Q36; - inv_D[ j ].Q48_part = one_div_diag_Q48; - - matrix_ptr( L_Q16, j, j, M ) = 65536; /* 1.0 in Q16 */ - ptr1 = matrix_adr( A, j, 0, M ); - ptr2 = matrix_adr( L_Q16, j + 1, 0, M ); - for( i = j + 1; i < M; i++ ) { - tmp_32 = 0; - for( k = 0; k < j; k++ ) { - tmp_32 = SKP_SMLAWW( tmp_32, v_Q0[ k ], ptr2[ k ] ); /* Q0 */ - } - tmp_32 = SKP_SUB32( ptr1[ i ], tmp_32 ); /* always < max(Correlation) */ - - /* tmp_32 / D_Q0[j] : Divide to Q16 */ - matrix_ptr( L_Q16, i, j, M ) = SKP_ADD32( SKP_SMMUL( tmp_32, one_div_diag_Q48 ), - SKP_RSHIFT( SKP_SMULWW( tmp_32, one_div_diag_Q36 ), 4 ) ); - - /* go to next column */ - ptr2 += M; - } - } - } - - SKP_assert( status == 0 ); -} - -SKP_INLINE void SKP_Silk_LS_divide_Q16_FIX( - SKP_int32 T[], /* I/O Numenator vector */ - inv_D_t *inv_D, /* I 1 / D vector */ - SKP_int M /* I Order */ -) -{ - SKP_int i; - SKP_int32 tmp_32; - SKP_int32 one_div_diag_Q36, one_div_diag_Q48; - - for( i = 0; i < M; i++ ) { - one_div_diag_Q36 = inv_D[ i ].Q36_part; - one_div_diag_Q48 = inv_D[ i ].Q48_part; - - tmp_32 = T[ i ]; - T[ i ] = SKP_ADD32( SKP_SMMUL( tmp_32, one_div_diag_Q48 ), SKP_RSHIFT( SKP_SMULWW( tmp_32, one_div_diag_Q36 ), 4 ) ); - } -} - -/* Solve Lx = b, when L is lower triangular and has ones on the diagonal */ -SKP_INLINE void SKP_Silk_LS_SolveFirst_FIX( - const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ - SKP_int M, /* I Dim of Matrix equation */ - const SKP_int32 *b, /* I b Vector */ - SKP_int32 *x_Q16 /* O x Vector */ -) -{ - SKP_int i, j; - const SKP_int32 *ptr32; - SKP_int32 tmp_32; - - for( i = 0; i < M; i++ ) { - ptr32 = matrix_adr( L_Q16, i, 0, M ); - tmp_32 = 0; - for( j = 0; j < i; j++ ) { - tmp_32 = SKP_SMLAWW( tmp_32, ptr32[ j ], x_Q16[ j ] ); - } - x_Q16[ i ] = SKP_SUB32( b[ i ], tmp_32 ); - } -} - -/* Solve L^t*x = b, where L is lower triangular with ones on the diagonal */ -SKP_INLINE void SKP_Silk_LS_SolveLast_FIX( - const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ - const SKP_int M, /* I Dim of Matrix equation */ - const SKP_int32 *b, /* I b Vector */ - SKP_int32 *x_Q16 /* O x Vector */ -) -{ - SKP_int i, j; - const SKP_int32 *ptr32; - SKP_int32 tmp_32; - - for( i = M - 1; i >= 0; i-- ) { - ptr32 = matrix_adr( L_Q16, 0, i, M ); - tmp_32 = 0; - for( j = M - 1; j > i; j-- ) { - tmp_32 = SKP_SMLAWW( tmp_32, ptr32[ SKP_SMULBB( j, M ) ], x_Q16[ j ] ); - } - x_Q16[ i ] = SKP_SUB32( b[ i ], tmp_32 ); - } -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_main_FIX.h" + +/*****************************/ +/* Internal function headers */ +/*****************************/ + +typedef struct { + SKP_int32 Q36_part; + SKP_int32 Q48_part; +} inv_D_t; + +/* Factorize square matrix A into LDL form */ +SKP_INLINE void SKP_Silk_LDL_factorize_FIX( + SKP_int32 *A, /* I/O Pointer to Symetric Square Matrix */ + SKP_int M, /* I Size of Matrix */ + SKP_int32 *L_Q16, /* I/O Pointer to Square Upper triangular Matrix */ + inv_D_t *inv_D /* I/O Pointer to vector holding inverted diagonal elements of D */ +); + +/* Solve Lx = b, when L is lower triangular and has ones on the diagonal */ +SKP_INLINE void SKP_Silk_LS_SolveFirst_FIX( + const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ + SKP_int M, /* I Dim of Matrix equation */ + const SKP_int32 *b, /* I b Vector */ + SKP_int32 *x_Q16 /* O x Vector */ +); + +/* Solve L^t*x = b, where L is lower triangular with ones on the diagonal */ +SKP_INLINE void SKP_Silk_LS_SolveLast_FIX( + const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ + const SKP_int M, /* I Dim of Matrix equation */ + const SKP_int32 *b, /* I b Vector */ + SKP_int32 *x_Q16 /* O x Vector */ +); + +SKP_INLINE void SKP_Silk_LS_divide_Q16_FIX( + SKP_int32 T[], /* I/O Numenator vector */ + inv_D_t *inv_D, /* I 1 / D vector */ + SKP_int M /* I dimension */ +); + +/* Solves Ax = b, assuming A is symmetric */ +void SKP_Silk_solve_LDL_FIX( + SKP_int32 *A, /* I Pointer to symetric square matrix A */ + SKP_int M, /* I Size of matrix */ + const SKP_int32 *b, /* I Pointer to b vector */ + SKP_int32 *x_Q16 /* O Pointer to x solution vector */ +) +{ + SKP_int32 L_Q16[ MAX_MATRIX_SIZE * MAX_MATRIX_SIZE ]; + SKP_int32 Y[ MAX_MATRIX_SIZE ]; + inv_D_t inv_D[ MAX_MATRIX_SIZE ]; + + SKP_assert( M <= MAX_MATRIX_SIZE ); + + /*************************************************** + Factorize A by LDL such that A = L*D*L', + where L is lower triangular with ones on diagonal + ****************************************************/ + SKP_Silk_LDL_factorize_FIX( A, M, L_Q16, inv_D ); + + /**************************************************** + * substitute D*L'*x = Y. ie: + L*D*L'*x = b => L*Y = b <=> Y = inv(L)*b + ******************************************************/ + SKP_Silk_LS_SolveFirst_FIX( L_Q16, M, b, Y ); + + /**************************************************** + D*L'*x = Y <=> L'*x = inv(D)*Y, because D is + diagonal just multiply with 1/d_i + ****************************************************/ + SKP_Silk_LS_divide_Q16_FIX( Y, inv_D, M ); + + /**************************************************** + x = inv(L') * inv(D) * Y + *****************************************************/ + SKP_Silk_LS_SolveLast_FIX( L_Q16, M, Y, x_Q16 ); +} + +SKP_INLINE void SKP_Silk_LDL_factorize_FIX( + SKP_int32 *A, /* I Pointer to Symetric Square Matrix */ + SKP_int M, /* I Size of Matrix */ + SKP_int32 *L_Q16, /* I/O Pointer to Square Upper triangular Matrix */ + inv_D_t *inv_D /* I/O Pointer to vector holding inverted diagonal elements of D */ +) +{ + SKP_int i, j, k, status, loop_count; + const SKP_int32 *ptr1, *ptr2; + SKP_int32 diag_min_value, tmp_32, err; + SKP_int32 v_Q0[ MAX_MATRIX_SIZE ], D_Q0[ MAX_MATRIX_SIZE ]; + SKP_int32 one_div_diag_Q36, one_div_diag_Q40, one_div_diag_Q48; + + SKP_assert( M <= MAX_MATRIX_SIZE ); + + status = 1; + diag_min_value = SKP_max_32( SKP_SMMUL( SKP_ADD_SAT32( A[ 0 ], A[ SKP_SMULBB( M, M ) - 1 ] ), FIND_LTP_COND_FAC_Q31 ), 1 << 9 ); + for( loop_count = 0; loop_count < M && status == 1; loop_count++ ) { + status = 0; + for( j = 0; j < M; j++ ) { + ptr1 = matrix_adr( L_Q16, j, 0, M ); + tmp_32 = 0; + for( i = 0; i < j; i++ ) { + v_Q0[ i ] = SKP_SMULWW( D_Q0[ i ], ptr1[ i ] ); /* Q0 */ + tmp_32 = SKP_SMLAWW( tmp_32, v_Q0[ i ], ptr1[ i ] ); /* Q0 */ + } + tmp_32 = SKP_SUB32( matrix_ptr( A, j, j, M ), tmp_32 ); + + if( tmp_32 < diag_min_value ) { + tmp_32 = SKP_SUB32( SKP_SMULBB( loop_count + 1, diag_min_value ), tmp_32 ); + /* Matrix not positive semi-definite, or ill conditioned */ + for( i = 0; i < M; i++ ) { + matrix_ptr( A, i, i, M ) = SKP_ADD32( matrix_ptr( A, i, i, M ), tmp_32 ); + } + status = 1; + break; + } + D_Q0[ j ] = tmp_32; /* always < max(Correlation) */ + + /* two-step division */ + one_div_diag_Q36 = SKP_INVERSE32_varQ( tmp_32, 36 ); /* Q36 */ + one_div_diag_Q40 = SKP_LSHIFT( one_div_diag_Q36, 4 ); /* Q40 */ + err = SKP_SUB32( 1 << 24, SKP_SMULWW( tmp_32, one_div_diag_Q40 ) ); /* Q24 */ + one_div_diag_Q48 = SKP_SMULWW( err, one_div_diag_Q40 ); /* Q48 */ + + /* Save 1/Ds */ + inv_D[ j ].Q36_part = one_div_diag_Q36; + inv_D[ j ].Q48_part = one_div_diag_Q48; + + matrix_ptr( L_Q16, j, j, M ) = 65536; /* 1.0 in Q16 */ + ptr1 = matrix_adr( A, j, 0, M ); + ptr2 = matrix_adr( L_Q16, j + 1, 0, M ); + for( i = j + 1; i < M; i++ ) { + tmp_32 = 0; + for( k = 0; k < j; k++ ) { + tmp_32 = SKP_SMLAWW( tmp_32, v_Q0[ k ], ptr2[ k ] ); /* Q0 */ + } + tmp_32 = SKP_SUB32( ptr1[ i ], tmp_32 ); /* always < max(Correlation) */ + + /* tmp_32 / D_Q0[j] : Divide to Q16 */ + matrix_ptr( L_Q16, i, j, M ) = SKP_ADD32( SKP_SMMUL( tmp_32, one_div_diag_Q48 ), + SKP_RSHIFT( SKP_SMULWW( tmp_32, one_div_diag_Q36 ), 4 ) ); + + /* go to next column */ + ptr2 += M; + } + } + } + + SKP_assert( status == 0 ); +} + +SKP_INLINE void SKP_Silk_LS_divide_Q16_FIX( + SKP_int32 T[], /* I/O Numenator vector */ + inv_D_t *inv_D, /* I 1 / D vector */ + SKP_int M /* I Order */ +) +{ + SKP_int i; + SKP_int32 tmp_32; + SKP_int32 one_div_diag_Q36, one_div_diag_Q48; + + for( i = 0; i < M; i++ ) { + one_div_diag_Q36 = inv_D[ i ].Q36_part; + one_div_diag_Q48 = inv_D[ i ].Q48_part; + + tmp_32 = T[ i ]; + T[ i ] = SKP_ADD32( SKP_SMMUL( tmp_32, one_div_diag_Q48 ), SKP_RSHIFT( SKP_SMULWW( tmp_32, one_div_diag_Q36 ), 4 ) ); + } +} + +/* Solve Lx = b, when L is lower triangular and has ones on the diagonal */ +SKP_INLINE void SKP_Silk_LS_SolveFirst_FIX( + const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ + SKP_int M, /* I Dim of Matrix equation */ + const SKP_int32 *b, /* I b Vector */ + SKP_int32 *x_Q16 /* O x Vector */ +) +{ + SKP_int i, j; + const SKP_int32 *ptr32; + SKP_int32 tmp_32; + + for( i = 0; i < M; i++ ) { + ptr32 = matrix_adr( L_Q16, i, 0, M ); + tmp_32 = 0; + for( j = 0; j < i; j++ ) { + tmp_32 = SKP_SMLAWW( tmp_32, ptr32[ j ], x_Q16[ j ] ); + } + x_Q16[ i ] = SKP_SUB32( b[ i ], tmp_32 ); + } +} + +/* Solve L^t*x = b, where L is lower triangular with ones on the diagonal */ +SKP_INLINE void SKP_Silk_LS_SolveLast_FIX( + const SKP_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */ + const SKP_int M, /* I Dim of Matrix equation */ + const SKP_int32 *b, /* I b Vector */ + SKP_int32 *x_Q16 /* O x Vector */ +) +{ + SKP_int i, j; + const SKP_int32 *ptr32; + SKP_int32 tmp_32; + + for( i = M - 1; i >= 0; i-- ) { + ptr32 = matrix_adr( L_Q16, 0, i, M ); + tmp_32 = 0; + for( j = M - 1; j > i; j-- ) { + tmp_32 = SKP_SMLAWW( tmp_32, ptr32[ SKP_SMULBB( j, M ) ], x_Q16[ j ] ); + } + x_Q16[ i ] = SKP_SUB32( b[ i ], tmp_32 ); + } +} diff --git a/jni/silk/src/SKP_Silk_sort.c b/app/src/main/jni/silk/src/SKP_Silk_sort.c similarity index 97% rename from jni/silk/src/SKP_Silk_sort.c rename to app/src/main/jni/silk/src/SKP_Silk_sort.c index 70930b5..94bec30 100644 --- a/jni/silk/src/SKP_Silk_sort.c +++ b/app/src/main/jni/silk/src/SKP_Silk_sort.c @@ -1,195 +1,195 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* Insertion sort (fast for already almost sorted arrays): */ -/* Best case: O(n) for an already sorted array */ -/* Worst case: O(n^2) for an inversely sorted array */ -/* */ -/* To be implemented: */ -/* Shell short: http://en.wikipedia.org/wiki/Shell_sort */ - -#include "SKP_Silk_SigProc_FIX.h" - -void SKP_Silk_insertion_sort_increasing( - SKP_int32 *a, /* I/O: Unsorted / Sorted vector */ - SKP_int *index, /* O: Index vector for the sorted elements */ - const SKP_int L, /* I: Vector length */ - const SKP_int K /* I: Number of correctly sorted positions */ -) -{ - SKP_int32 value; - SKP_int i, j; - - /* Safety checks */ - SKP_assert( K > 0 ); - SKP_assert( L > 0 ); - SKP_assert( L >= K ); - - /* Write start indices in index vector */ - for( i = 0; i < K; i++ ) { - index[ i ] = i; - } - - /* Sort vector elements by value, increasing order */ - for( i = 1; i < K; i++ ) { - value = a[ i ]; - for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - index[ j + 1 ] = index[ j ]; /* Shift index */ - } - a[ j + 1 ] = value; /* Write value */ - index[ j + 1 ] = i; /* Write index */ - } - - /* If less than L values are asked check the remaining values, */ - /* but only spend CPU to ensure that the K first values are correct */ - for( i = K; i < L; i++ ) { - value = a[ i ]; - if( value < a[ K - 1 ] ) { - for( j = K - 2; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - index[ j + 1 ] = index[ j ]; /* Shift index */ - } - a[ j + 1 ] = value; /* Write value */ - index[ j + 1 ] = i; /* Write index */ - } - } -} - -void SKP_Silk_insertion_sort_decreasing( - SKP_int *a, /* I/O: Unsorted / Sorted vector */ - SKP_int *index, /* O: Index vector for the sorted elements */ - const SKP_int L, /* I: Vector length */ - const SKP_int K /* I: Number of correctly sorted positions */ -) -{ - SKP_int value; - SKP_int i, j; - - /* Safety checks */ - SKP_assert( K > 0 ); - SKP_assert( L > 0 ); - SKP_assert( L >= K ); - - /* Write start indices in index vector */ - for( i = 0; i < K; i++ ) { - index[ i ] = i; - } - - /* Sort vector elements by value, decreasing order */ - for( i = 1; i < K; i++ ) { - value = a[ i ]; - for( j = i - 1; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - index[ j + 1 ] = index[ j ]; /* Shift index */ - } - a[ j + 1 ] = value; /* Write value */ - index[ j + 1 ] = i; /* Write index */ - } - - /* If less than L values are asked check the remaining values, */ - /* but only spend CPU to ensure that the K first values are correct */ - for( i = K; i < L; i++ ) { - value = a[ i ]; - if( value > a[ K - 1 ] ) { - for( j = K - 2; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - index[ j + 1 ] = index[ j ]; /* Shift index */ - } - a[ j + 1 ] = value; /* Write value */ - index[ j + 1 ] = i; /* Write index */ - } - } -} - -void SKP_Silk_insertion_sort_decreasing_int16( - SKP_int16 *a, /* I/O: Unsorted / Sorted vector */ - SKP_int *index, /* O: Index vector for the sorted elements */ - const SKP_int L, /* I: Vector length */ - const SKP_int K /* I: Number of correctly sorted positions */ -) -{ - SKP_int i, j; - SKP_int value; - - /* Safety checks */ - SKP_assert( K > 0 ); - SKP_assert( L > 0 ); - SKP_assert( L >= K ); - - /* Write start indices in index vector */ - for( i = 0; i < K; i++ ) { - index[ i ] = i; - } - - /* Sort vector elements by value, decreasing order */ - for( i = 1; i < K; i++ ) { - value = a[ i ]; - for( j = i - 1; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - index[ j + 1 ] = index[ j ]; /* Shift index */ - } - a[ j + 1 ] = value; /* Write value */ - index[ j + 1 ] = i; /* Write index */ - } - - /* If less than L values are asked check the remaining values, */ - /* but only spend CPU to ensure that the K first values are correct */ - for( i = K; i < L; i++ ) { - value = a[ i ]; - if( value > a[ K - 1 ] ) { - for( j = K - 2; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - index[ j + 1 ] = index[ j ]; /* Shift index */ - } - a[ j + 1 ] = value; /* Write value */ - index[ j + 1 ] = i; /* Write index */ - } - } -} - -void SKP_Silk_insertion_sort_increasing_all_values( - SKP_int *a, /* I/O: Unsorted / Sorted vector */ - const SKP_int L /* I: Vector length */ -) -{ - SKP_int value; - SKP_int i, j; - - /* Safety checks */ - SKP_assert( L > 0 ); - - /* Sort vector elements by value, increasing order */ - for( i = 1; i < L; i++ ) { - value = a[ i ]; - for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { - a[ j + 1 ] = a[ j ]; /* Shift value */ - } - a[ j + 1 ] = value; /* Write value */ - } -} - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* Insertion sort (fast for already almost sorted arrays): */ +/* Best case: O(n) for an already sorted array */ +/* Worst case: O(n^2) for an inversely sorted array */ +/* */ +/* To be implemented: */ +/* Shell short: http://en.wikipedia.org/wiki/Shell_sort */ + +#include "SKP_Silk_SigProc_FIX.h" + +void SKP_Silk_insertion_sort_increasing( + SKP_int32 *a, /* I/O: Unsorted / Sorted vector */ + SKP_int *index, /* O: Index vector for the sorted elements */ + const SKP_int L, /* I: Vector length */ + const SKP_int K /* I: Number of correctly sorted positions */ +) +{ + SKP_int32 value; + SKP_int i, j; + + /* Safety checks */ + SKP_assert( K > 0 ); + SKP_assert( L > 0 ); + SKP_assert( L >= K ); + + /* Write start indices in index vector */ + for( i = 0; i < K; i++ ) { + index[ i ] = i; + } + + /* Sort vector elements by value, increasing order */ + for( i = 1; i < K; i++ ) { + value = a[ i ]; + for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + index[ j + 1 ] = index[ j ]; /* Shift index */ + } + a[ j + 1 ] = value; /* Write value */ + index[ j + 1 ] = i; /* Write index */ + } + + /* If less than L values are asked check the remaining values, */ + /* but only spend CPU to ensure that the K first values are correct */ + for( i = K; i < L; i++ ) { + value = a[ i ]; + if( value < a[ K - 1 ] ) { + for( j = K - 2; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + index[ j + 1 ] = index[ j ]; /* Shift index */ + } + a[ j + 1 ] = value; /* Write value */ + index[ j + 1 ] = i; /* Write index */ + } + } +} + +void SKP_Silk_insertion_sort_decreasing( + SKP_int *a, /* I/O: Unsorted / Sorted vector */ + SKP_int *index, /* O: Index vector for the sorted elements */ + const SKP_int L, /* I: Vector length */ + const SKP_int K /* I: Number of correctly sorted positions */ +) +{ + SKP_int value; + SKP_int i, j; + + /* Safety checks */ + SKP_assert( K > 0 ); + SKP_assert( L > 0 ); + SKP_assert( L >= K ); + + /* Write start indices in index vector */ + for( i = 0; i < K; i++ ) { + index[ i ] = i; + } + + /* Sort vector elements by value, decreasing order */ + for( i = 1; i < K; i++ ) { + value = a[ i ]; + for( j = i - 1; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + index[ j + 1 ] = index[ j ]; /* Shift index */ + } + a[ j + 1 ] = value; /* Write value */ + index[ j + 1 ] = i; /* Write index */ + } + + /* If less than L values are asked check the remaining values, */ + /* but only spend CPU to ensure that the K first values are correct */ + for( i = K; i < L; i++ ) { + value = a[ i ]; + if( value > a[ K - 1 ] ) { + for( j = K - 2; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + index[ j + 1 ] = index[ j ]; /* Shift index */ + } + a[ j + 1 ] = value; /* Write value */ + index[ j + 1 ] = i; /* Write index */ + } + } +} + +void SKP_Silk_insertion_sort_decreasing_int16( + SKP_int16 *a, /* I/O: Unsorted / Sorted vector */ + SKP_int *index, /* O: Index vector for the sorted elements */ + const SKP_int L, /* I: Vector length */ + const SKP_int K /* I: Number of correctly sorted positions */ +) +{ + SKP_int i, j; + SKP_int value; + + /* Safety checks */ + SKP_assert( K > 0 ); + SKP_assert( L > 0 ); + SKP_assert( L >= K ); + + /* Write start indices in index vector */ + for( i = 0; i < K; i++ ) { + index[ i ] = i; + } + + /* Sort vector elements by value, decreasing order */ + for( i = 1; i < K; i++ ) { + value = a[ i ]; + for( j = i - 1; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + index[ j + 1 ] = index[ j ]; /* Shift index */ + } + a[ j + 1 ] = value; /* Write value */ + index[ j + 1 ] = i; /* Write index */ + } + + /* If less than L values are asked check the remaining values, */ + /* but only spend CPU to ensure that the K first values are correct */ + for( i = K; i < L; i++ ) { + value = a[ i ]; + if( value > a[ K - 1 ] ) { + for( j = K - 2; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + index[ j + 1 ] = index[ j ]; /* Shift index */ + } + a[ j + 1 ] = value; /* Write value */ + index[ j + 1 ] = i; /* Write index */ + } + } +} + +void SKP_Silk_insertion_sort_increasing_all_values( + SKP_int *a, /* I/O: Unsorted / Sorted vector */ + const SKP_int L /* I: Vector length */ +) +{ + SKP_int value; + SKP_int i, j; + + /* Safety checks */ + SKP_assert( L > 0 ); + + /* Sort vector elements by value, increasing order */ + for( i = 1; i < L; i++ ) { + value = a[ i ]; + for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { + a[ j + 1 ] = a[ j ]; /* Shift value */ + } + a[ j + 1 ] = value; /* Write value */ + } +} + diff --git a/jni/silk/src/SKP_Silk_structs.h b/app/src/main/jni/silk/src/SKP_Silk_structs.h similarity index 98% rename from jni/silk/src/SKP_Silk_structs.h rename to app/src/main/jni/silk/src/SKP_Silk_structs.h index ce96e22..4fbecd7 100644 --- a/jni/silk/src/SKP_Silk_structs.h +++ b/app/src/main/jni/silk/src/SKP_Silk_structs.h @@ -1,381 +1,381 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_STRUCTS_H -#define SKP_SILK_STRUCTS_H - -#include "SKP_Silk_typedef.h" -#include "SKP_Silk_SigProc_FIX.h" -#include "SKP_Silk_define.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - - -/************************************/ -/* Noise shaping quantization state */ -/************************************/ -typedef struct { - SKP_int16 xq[ 2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */ - SKP_int32 sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ]; - SKP_int32 sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ]; - SKP_int32 sLF_AR_shp_Q12; - SKP_int lagPrev; - SKP_int sLTP_buf_idx; - SKP_int sLTP_shp_buf_idx; - SKP_int32 rand_seed; - SKP_int32 prev_inv_gain_Q16; - SKP_int rewhite_flag; -} SKP_Silk_nsq_state; /* FIX*/ - -/* Struct for Low BitRate Redundant (LBRR) information */ -typedef struct { - SKP_uint8 payload[ MAX_ARITHM_BYTES ]; - SKP_int nBytes; /* Number of bytes in payload */ - SKP_int usage; /* Tells how the payload should be used as FEC */ -} SKP_SILK_LBRR_struct; - -/********************************/ -/* VAD state */ -/********************************/ -typedef struct { - SKP_int32 AnaState[ 2 ]; /* Analysis filterbank state: 0-8 kHz */ - SKP_int32 AnaState1[ 2 ]; /* Analysis filterbank state: 0-4 kHz */ - SKP_int32 AnaState2[ 2 ]; /* Analysis filterbank state: 0-2 kHz */ - SKP_int32 XnrgSubfr[ VAD_N_BANDS ]; /* Subframe energies */ - SKP_int32 NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band */ - SKP_int16 HPstate; /* State of differentiator in the lowest band */ - SKP_int32 NL[ VAD_N_BANDS ]; /* Noise energy level in each band */ - SKP_int32 inv_NL[ VAD_N_BANDS ]; /* Inverse noise energy level in each band */ - SKP_int32 NoiseLevelBias[ VAD_N_BANDS ]; /* Noise level estimator bias/offset */ - SKP_int32 counter; /* Frame counter used in the initial phase */ -} SKP_Silk_VAD_state; - -/*******************************/ -/* Range encoder/decoder state */ -/*******************************/ -typedef struct { - SKP_int32 bufferLength; - SKP_int32 bufferIx; - SKP_uint32 base_Q32; - SKP_uint32 range_Q16; - SKP_int32 error; - SKP_uint8 buffer[ MAX_ARITHM_BYTES ]; /* Buffer containing payload */ -} SKP_Silk_range_coder_state; - -/* Input frequency range detection struct */ -typedef struct { - SKP_int32 S_HP_8_kHz[ NB_SOS ][ 2 ]; /* HP filter State */ - SKP_int32 ConsecSmplsAboveThres; - SKP_int32 ActiveSpeech_ms; /* Accumulated time with active speech */ - SKP_int SWB_detected; /* Flag to indicate SWB input */ - SKP_int WB_detected; /* Flag to indicate WB input */ -} SKP_Silk_detect_SWB_state; - -#if SWITCH_TRANSITION_FILTERING -/* Variable cut-off low-pass filter state */ -typedef struct { - SKP_int32 In_LP_State[ 2 ]; /* Low pass filter state */ - SKP_int32 transition_frame_no; /* Counter which is mapped to a cut-off frequency */ - SKP_int mode; /* Operating mode, 0: switch down, 1: switch up */ -} SKP_Silk_LP_state; -#endif - -/* Structure for one stage of MSVQ */ -typedef struct { - const SKP_int32 nVectors; - const SKP_int16 *CB_NLSF_Q15; - const SKP_int16 *Rates_Q5; -} SKP_Silk_NLSF_CBS; - -/* Structure containing NLSF MSVQ codebook */ -typedef struct { - const SKP_int32 nStages; - - /* Fields for (de)quantizing */ - const SKP_Silk_NLSF_CBS *CBStages; - const SKP_int *NDeltaMin_Q15; - - /* Fields for arithmetic (de)coding */ - const SKP_uint16 *CDF; - const SKP_uint16 * const *StartPtr; - const SKP_int *MiddleIx; -} SKP_Silk_NLSF_CB_struct; - -/********************************/ -/* Encoder state */ -/********************************/ -typedef struct { - SKP_Silk_range_coder_state sRC; /* Range coder state */ - SKP_Silk_range_coder_state sRC_LBRR; /* Range coder state (for low bitrate redundancy) */ -#if HIGH_PASS_INPUT - SKP_int32 In_HP_State[ 2 ]; /* High pass filter state */ -#endif -#if SWITCH_TRANSITION_FILTERING - SKP_Silk_LP_state sLP; /* Low pass filter state */ -#endif - SKP_Silk_VAD_state sVAD; /* Voice activity detector state */ - - SKP_int LBRRprevLastGainIndex; - SKP_int prev_sigtype; - SKP_int typeOffsetPrev; /* Previous signal type and quantization offset */ - SKP_int prevLag; - SKP_int prev_lagIndex; - SKP_int fs_kHz; /* Sampling frequency (kHz) */ - SKP_int fs_kHz_changed; /* Did we switch yet? */ - SKP_int frame_length; /* Frame length (samples) */ - SKP_int subfr_length; /* Subframe length (samples) */ - SKP_int la_pitch; /* Look-ahead for pitch analysis (samples) */ - SKP_int la_shape; /* Look-ahead for noise shape analysis (samples) */ - SKP_int32 TargetRate_bps; /* Target bitrate (bps) */ - SKP_int PacketSize_ms; /* Number of milliseconds to put in each packet */ - SKP_int PacketLoss_perc; /* Packet loss rate measured by farend */ - SKP_int32 frameCounter; - SKP_int Complexity; /* Complexity setting: 0-> low; 1-> medium; 2->high */ - SKP_int nStatesDelayedDecision; /* Number of states in delayed decision quantization */ - SKP_int useInterpolatedNLSFs; /* Flag for using NLSF interpolation */ - SKP_int shapingLPCOrder; /* Filter order for noise shaping filters */ - SKP_int predictLPCOrder; /* Filter order for prediction filters */ - SKP_int pitchEstimationComplexity; /* Complexity level for pitch estimator */ - SKP_int pitchEstimationLPCOrder; /* Whitening filter order for pitch estimator */ - SKP_int LTPQuantLowComplexity; /* Flag for low complexity LTP quantization */ - SKP_int NLSF_MSVQ_Survivors; /* Number of survivors in NLSF MSVQ */ - SKP_int first_frame_after_reset; /* Flag for deactivating NLSF interp. and fluc. reduction after resets */ - - /* Input/output buffering */ - SKP_int16 inputBuf[ MAX_FRAME_LENGTH ]; /* buffer containin input signal */ - SKP_int inputBufIx; - SKP_int nFramesInPayloadBuf; /* number of frames sitting in outputBuf */ - SKP_int nBytesInPayloadBuf; /* number of bytes sitting in outputBuf */ - - /* Parameters For LTP scaling Control */ - SKP_int frames_since_onset; - - const SKP_Silk_NLSF_CB_struct *psNLSF_CB[ 2 ]; /* Pointers to voiced/unvoiced NLSF codebooks */ - - /* Struct for Inband LBRR */ - SKP_SILK_LBRR_struct LBRR_buffer[ MAX_LBRR_DELAY ]; - SKP_int oldest_LBRR_idx; - SKP_int LBRR_enabled; - SKP_int LBRR_GainIncreases; /* Number of shifts to Gains to get LBRR rate Voiced frames */ - - /* Bitrate control */ - SKP_int32 bitrateDiff; /* accumulated diff. between the target bitrate and the SWB/WB limits */ - -#if LOW_COMPLEXITY_ONLY - /* state for downsampling from 24 to 16 kHz in low complexity mode */ - SKP_int16 resample24To16state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; -#else - SKP_int32 resample24To16state[ 11 ]; /* state for downsampling from 24 to 16 kHz */ -#endif - SKP_int32 resample24To12state[ 6 ]; /* state for downsampling from 24 to 12 kHz */ - SKP_int32 resample24To8state[ 7 ]; /* state for downsampling from 24 to 8 kHz */ - SKP_int32 resample16To12state[ 15 ]; /* state for downsampling from 16 to 12 kHz */ - SKP_int32 resample16To8state[ 6 ]; /* state for downsampling from 16 to 8 kHz */ -#if LOW_COMPLEXITY_ONLY - /* state for downsampling from 12 to 8 kHz in low complexity mode */ - SKP_int16 resample12To8state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; -#else - SKP_int32 resample12To8state[ 11 ]; /* state for downsampling from 12 to 8 kHz */ -#endif - - /* DTX */ - SKP_int noSpeechCounter; /* Counts concecutive nonactive frames, used by DTX */ - SKP_int useDTX; /* Flag to enable DTX */ - SKP_int inDTX; /* Flag to signal DTX period */ - SKP_int vadFlag; /* Flag to indicate Voice Activity */ - - /* Struct for detecting SWB input */ - SKP_Silk_detect_SWB_state sSWBdetect; - - - /********************************************/ - /* Buffers etc used by the new bitstream V4 */ - /********************************************/ - SKP_int q[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */ - SKP_int q_LBRR[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */ - SKP_int sigtype[ MAX_FRAMES_PER_PACKET ]; - SKP_int QuantOffsetType[ MAX_FRAMES_PER_PACKET ]; - SKP_int extension_layer; /* Add extension layer */ - SKP_int bitstream_v; /* Holds bitstream version */ -} SKP_Silk_encoder_state; - - -/************************/ -/* Encoder control */ -/************************/ -typedef struct { - /* Quantization indices */ - SKP_int lagIndex; - SKP_int contourIndex; - SKP_int PERIndex; - SKP_int LTPIndex[ NB_SUBFR ]; - SKP_int NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ]; /* NLSF path of quantized LSF vector */ - SKP_int NLSFInterpCoef_Q2; - SKP_int GainsIndices[ NB_SUBFR ]; - SKP_int32 Seed; - SKP_int LTP_scaleIndex; - SKP_int RateLevelIndex; - SKP_int QuantOffsetType; - SKP_int sigtype; - - /* Prediction and coding parameters */ - SKP_int pitchL[ NB_SUBFR ]; - - SKP_int LBRR_usage; /* Low bitrate redundancy usage */ -} SKP_Silk_encoder_control; - -/* Struct for Packet Loss Concealment */ -typedef struct { - SKP_int32 pitchL_Q8; /* Pitch lag to use for voiced concealment */ - SKP_int16 LTPCoef_Q14[ LTP_ORDER ]; /* LTP coeficients to use for voiced concealment */ - SKP_int16 prevLPC_Q12[ MAX_LPC_ORDER ]; - SKP_int last_frame_lost; /* Was previous frame lost */ - SKP_int32 rand_seed; /* Seed for unvoiced signal generation */ - SKP_int16 randScale_Q14; /* Scaling of unvoiced random signal */ - SKP_int32 conc_energy; - SKP_int conc_energy_shift; - SKP_int16 prevLTP_scale_Q14; - SKP_int32 prevGain_Q16[ NB_SUBFR ]; - SKP_int fs_kHz; -} SKP_Silk_PLC_struct; - -/* Struct for CNG */ -typedef struct { - SKP_int32 CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ]; - SKP_int CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ]; - SKP_int32 CNG_synth_state[ MAX_LPC_ORDER ]; - SKP_int32 CNG_smth_Gain_Q16; - SKP_int32 rand_seed; - SKP_int fs_kHz; -} SKP_Silk_CNG_struct; - -/********************************/ -/* Decoder state */ -/********************************/ -typedef struct { - SKP_Silk_range_coder_state sRC; /* Range coder state */ - SKP_int32 prev_inv_gain_Q16; - SKP_int32 sLTP_Q16[ 2 * MAX_FRAME_LENGTH ]; - SKP_int32 sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ]; - SKP_int32 exc_Q10[ MAX_FRAME_LENGTH ]; - SKP_int32 res_Q10[ MAX_FRAME_LENGTH ]; - SKP_int16 outBuf[ 2 * MAX_FRAME_LENGTH ]; /* Buffer for output signal */ - SKP_int sLTP_buf_idx; /* LTP_buf_index */ - SKP_int lagPrev; /* Previous Lag */ - SKP_int LastGainIndex; /* Previous gain index */ - SKP_int LastGainIndex_EnhLayer; /* Previous gain index */ - SKP_int typeOffsetPrev; /* Previous signal type and quantization offset */ - SKP_int32 HPState[ DEC_HP_ORDER ]; /* HP filter state */ - const SKP_int16 *HP_A; /* HP filter AR coefficients */ - const SKP_int16 *HP_B; /* HP filter MA coefficients */ - SKP_int fs_kHz; /* Sampling frequency in kHz */ - SKP_int frame_length; /* Frame length (samples) */ - SKP_int subfr_length; /* Subframe length (samples) */ - SKP_int LPC_order; /* LPC order */ - SKP_int prevNLSF_Q15[ MAX_LPC_ORDER ]; /* Used to interpolate LSFs */ - SKP_int first_frame_after_reset; /* Flag for deactivating NLSF interp. and fluc. reduction after resets */ - - /* For buffering payload in case of more frames per packet */ - SKP_int nBytesLeft; - SKP_int nFramesDecoded; - SKP_int nFramesInPacket; - SKP_int moreInternalDecoderFrames; - SKP_int FrameTermination; - - SKP_int32 resampleState[ 15 ]; - - const SKP_Silk_NLSF_CB_struct *psNLSF_CB[ 2 ]; /* Pointers to voiced/unvoiced NLSF codebooks */ - - SKP_int sigtype[ MAX_FRAMES_PER_PACKET ]; - SKP_int QuantOffsetType[ MAX_FRAMES_PER_PACKET ]; - SKP_int GainsIndices[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ]; - SKP_int GainsIndices_EnhLayer[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ]; - SKP_int NLSFIndices[ MAX_FRAMES_PER_PACKET ][ NLSF_MSVQ_MAX_CB_STAGES ]; - SKP_int NLSFInterpCoef_Q2[ MAX_FRAMES_PER_PACKET ]; - SKP_int lagIndex[ MAX_FRAMES_PER_PACKET ]; - SKP_int contourIndex[ MAX_FRAMES_PER_PACKET ]; - SKP_int PERIndex[ MAX_FRAMES_PER_PACKET ]; - SKP_int LTPIndex[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ]; - SKP_int LTP_scaleIndex[ MAX_FRAMES_PER_PACKET ]; - SKP_int Seed[ MAX_FRAMES_PER_PACKET ]; - SKP_int vadFlagBuf[ MAX_FRAMES_PER_PACKET ]; - - /* Parameters used to investigate if inband FEC is used */ - SKP_int vadFlag; - SKP_int no_FEC_counter; /* Counts number of frames wo inband FEC */ - SKP_int inband_FEC_offset; /* 0: no FEC, 1: FEC with 1 packet offset, 2: FEC w 2 packets offset */ - - /* CNG state */ - SKP_Silk_CNG_struct sCNG; - - /* Stuff used for PLC */ - SKP_Silk_PLC_struct sPLC; - SKP_int lossCnt; - SKP_int prev_sigtype; /* Previous sigtype */ -#ifdef USE_INTERPOLATION_PLC - SKP_int16 prevQuant[ MAX_FRAME_LENGTH ]; - SKP_int prevPitchL[ NB_SUBFR ]; /* Previous Lags used */ - SKP_int16 prevLTPCoef_Q14[ NB_SUBFR * LTP_ORDER ]; /* Previous LTCoefs used */ - SKP_int16 prevAR_Q12[ MAX_LPC_ORDER ]; - SKP_int interpolDistance; /* Number of frames between old and new recieved packet */ -#endif - - - - - SKP_int bitstream_v; /* Holds bitstream version */ -} SKP_Silk_decoder_state; - -/************************/ -/* Decoder control */ -/************************/ -typedef struct { - /* prediction and coding parameters */ - SKP_int pitchL[ NB_SUBFR ]; - SKP_int32 Gains_Q16[ NB_SUBFR ]; - SKP_int32 Seed; - /* holds interpolated and final coefficients, 4-byte aligned */ - SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER ); - SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ]; - SKP_int LTP_scale_Q14; - - /* quantization indices */ - SKP_int PERIndex; - SKP_int RateLevelIndex; - SKP_int QuantOffsetType; - SKP_int sigtype; - SKP_int NLSFInterpCoef_Q2; -} SKP_Silk_decoder_control; - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_STRUCTS_H +#define SKP_SILK_STRUCTS_H + +#include "SKP_Silk_typedef.h" +#include "SKP_Silk_SigProc_FIX.h" +#include "SKP_Silk_define.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/************************************/ +/* Noise shaping quantization state */ +/************************************/ +typedef struct { + SKP_int16 xq[ 2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */ + SKP_int32 sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ]; + SKP_int32 sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ]; + SKP_int32 sLF_AR_shp_Q12; + SKP_int lagPrev; + SKP_int sLTP_buf_idx; + SKP_int sLTP_shp_buf_idx; + SKP_int32 rand_seed; + SKP_int32 prev_inv_gain_Q16; + SKP_int rewhite_flag; +} SKP_Silk_nsq_state; /* FIX*/ + +/* Struct for Low BitRate Redundant (LBRR) information */ +typedef struct { + SKP_uint8 payload[ MAX_ARITHM_BYTES ]; + SKP_int nBytes; /* Number of bytes in payload */ + SKP_int usage; /* Tells how the payload should be used as FEC */ +} SKP_SILK_LBRR_struct; + +/********************************/ +/* VAD state */ +/********************************/ +typedef struct { + SKP_int32 AnaState[ 2 ]; /* Analysis filterbank state: 0-8 kHz */ + SKP_int32 AnaState1[ 2 ]; /* Analysis filterbank state: 0-4 kHz */ + SKP_int32 AnaState2[ 2 ]; /* Analysis filterbank state: 0-2 kHz */ + SKP_int32 XnrgSubfr[ VAD_N_BANDS ]; /* Subframe energies */ + SKP_int32 NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band */ + SKP_int16 HPstate; /* State of differentiator in the lowest band */ + SKP_int32 NL[ VAD_N_BANDS ]; /* Noise energy level in each band */ + SKP_int32 inv_NL[ VAD_N_BANDS ]; /* Inverse noise energy level in each band */ + SKP_int32 NoiseLevelBias[ VAD_N_BANDS ]; /* Noise level estimator bias/offset */ + SKP_int32 counter; /* Frame counter used in the initial phase */ +} SKP_Silk_VAD_state; + +/*******************************/ +/* Range encoder/decoder state */ +/*******************************/ +typedef struct { + SKP_int32 bufferLength; + SKP_int32 bufferIx; + SKP_uint32 base_Q32; + SKP_uint32 range_Q16; + SKP_int32 error; + SKP_uint8 buffer[ MAX_ARITHM_BYTES ]; /* Buffer containing payload */ +} SKP_Silk_range_coder_state; + +/* Input frequency range detection struct */ +typedef struct { + SKP_int32 S_HP_8_kHz[ NB_SOS ][ 2 ]; /* HP filter State */ + SKP_int32 ConsecSmplsAboveThres; + SKP_int32 ActiveSpeech_ms; /* Accumulated time with active speech */ + SKP_int SWB_detected; /* Flag to indicate SWB input */ + SKP_int WB_detected; /* Flag to indicate WB input */ +} SKP_Silk_detect_SWB_state; + +#if SWITCH_TRANSITION_FILTERING +/* Variable cut-off low-pass filter state */ +typedef struct { + SKP_int32 In_LP_State[ 2 ]; /* Low pass filter state */ + SKP_int32 transition_frame_no; /* Counter which is mapped to a cut-off frequency */ + SKP_int mode; /* Operating mode, 0: switch down, 1: switch up */ +} SKP_Silk_LP_state; +#endif + +/* Structure for one stage of MSVQ */ +typedef struct { + const SKP_int32 nVectors; + const SKP_int16 *CB_NLSF_Q15; + const SKP_int16 *Rates_Q5; +} SKP_Silk_NLSF_CBS; + +/* Structure containing NLSF MSVQ codebook */ +typedef struct { + const SKP_int32 nStages; + + /* Fields for (de)quantizing */ + const SKP_Silk_NLSF_CBS *CBStages; + const SKP_int *NDeltaMin_Q15; + + /* Fields for arithmetic (de)coding */ + const SKP_uint16 *CDF; + const SKP_uint16 * const *StartPtr; + const SKP_int *MiddleIx; +} SKP_Silk_NLSF_CB_struct; + +/********************************/ +/* Encoder state */ +/********************************/ +typedef struct { + SKP_Silk_range_coder_state sRC; /* Range coder state */ + SKP_Silk_range_coder_state sRC_LBRR; /* Range coder state (for low bitrate redundancy) */ +#if HIGH_PASS_INPUT + SKP_int32 In_HP_State[ 2 ]; /* High pass filter state */ +#endif +#if SWITCH_TRANSITION_FILTERING + SKP_Silk_LP_state sLP; /* Low pass filter state */ +#endif + SKP_Silk_VAD_state sVAD; /* Voice activity detector state */ + + SKP_int LBRRprevLastGainIndex; + SKP_int prev_sigtype; + SKP_int typeOffsetPrev; /* Previous signal type and quantization offset */ + SKP_int prevLag; + SKP_int prev_lagIndex; + SKP_int fs_kHz; /* Sampling frequency (kHz) */ + SKP_int fs_kHz_changed; /* Did we switch yet? */ + SKP_int frame_length; /* Frame length (samples) */ + SKP_int subfr_length; /* Subframe length (samples) */ + SKP_int la_pitch; /* Look-ahead for pitch analysis (samples) */ + SKP_int la_shape; /* Look-ahead for noise shape analysis (samples) */ + SKP_int32 TargetRate_bps; /* Target bitrate (bps) */ + SKP_int PacketSize_ms; /* Number of milliseconds to put in each packet */ + SKP_int PacketLoss_perc; /* Packet loss rate measured by farend */ + SKP_int32 frameCounter; + SKP_int Complexity; /* Complexity setting: 0-> low; 1-> medium; 2->high */ + SKP_int nStatesDelayedDecision; /* Number of states in delayed decision quantization */ + SKP_int useInterpolatedNLSFs; /* Flag for using NLSF interpolation */ + SKP_int shapingLPCOrder; /* Filter order for noise shaping filters */ + SKP_int predictLPCOrder; /* Filter order for prediction filters */ + SKP_int pitchEstimationComplexity; /* Complexity level for pitch estimator */ + SKP_int pitchEstimationLPCOrder; /* Whitening filter order for pitch estimator */ + SKP_int LTPQuantLowComplexity; /* Flag for low complexity LTP quantization */ + SKP_int NLSF_MSVQ_Survivors; /* Number of survivors in NLSF MSVQ */ + SKP_int first_frame_after_reset; /* Flag for deactivating NLSF interp. and fluc. reduction after resets */ + + /* Input/output buffering */ + SKP_int16 inputBuf[ MAX_FRAME_LENGTH ]; /* buffer containin input signal */ + SKP_int inputBufIx; + SKP_int nFramesInPayloadBuf; /* number of frames sitting in outputBuf */ + SKP_int nBytesInPayloadBuf; /* number of bytes sitting in outputBuf */ + + /* Parameters For LTP scaling Control */ + SKP_int frames_since_onset; + + const SKP_Silk_NLSF_CB_struct *psNLSF_CB[ 2 ]; /* Pointers to voiced/unvoiced NLSF codebooks */ + + /* Struct for Inband LBRR */ + SKP_SILK_LBRR_struct LBRR_buffer[ MAX_LBRR_DELAY ]; + SKP_int oldest_LBRR_idx; + SKP_int LBRR_enabled; + SKP_int LBRR_GainIncreases; /* Number of shifts to Gains to get LBRR rate Voiced frames */ + + /* Bitrate control */ + SKP_int32 bitrateDiff; /* accumulated diff. between the target bitrate and the SWB/WB limits */ + +#if LOW_COMPLEXITY_ONLY + /* state for downsampling from 24 to 16 kHz in low complexity mode */ + SKP_int16 resample24To16state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; +#else + SKP_int32 resample24To16state[ 11 ]; /* state for downsampling from 24 to 16 kHz */ +#endif + SKP_int32 resample24To12state[ 6 ]; /* state for downsampling from 24 to 12 kHz */ + SKP_int32 resample24To8state[ 7 ]; /* state for downsampling from 24 to 8 kHz */ + SKP_int32 resample16To12state[ 15 ]; /* state for downsampling from 16 to 12 kHz */ + SKP_int32 resample16To8state[ 6 ]; /* state for downsampling from 16 to 8 kHz */ +#if LOW_COMPLEXITY_ONLY + /* state for downsampling from 12 to 8 kHz in low complexity mode */ + SKP_int16 resample12To8state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ]; +#else + SKP_int32 resample12To8state[ 11 ]; /* state for downsampling from 12 to 8 kHz */ +#endif + + /* DTX */ + SKP_int noSpeechCounter; /* Counts concecutive nonactive frames, used by DTX */ + SKP_int useDTX; /* Flag to enable DTX */ + SKP_int inDTX; /* Flag to signal DTX period */ + SKP_int vadFlag; /* Flag to indicate Voice Activity */ + + /* Struct for detecting SWB input */ + SKP_Silk_detect_SWB_state sSWBdetect; + + + /********************************************/ + /* Buffers etc used by the new bitstream V4 */ + /********************************************/ + SKP_int q[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */ + SKP_int q_LBRR[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */ + SKP_int sigtype[ MAX_FRAMES_PER_PACKET ]; + SKP_int QuantOffsetType[ MAX_FRAMES_PER_PACKET ]; + SKP_int extension_layer; /* Add extension layer */ + SKP_int bitstream_v; /* Holds bitstream version */ +} SKP_Silk_encoder_state; + + +/************************/ +/* Encoder control */ +/************************/ +typedef struct { + /* Quantization indices */ + SKP_int lagIndex; + SKP_int contourIndex; + SKP_int PERIndex; + SKP_int LTPIndex[ NB_SUBFR ]; + SKP_int NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ]; /* NLSF path of quantized LSF vector */ + SKP_int NLSFInterpCoef_Q2; + SKP_int GainsIndices[ NB_SUBFR ]; + SKP_int32 Seed; + SKP_int LTP_scaleIndex; + SKP_int RateLevelIndex; + SKP_int QuantOffsetType; + SKP_int sigtype; + + /* Prediction and coding parameters */ + SKP_int pitchL[ NB_SUBFR ]; + + SKP_int LBRR_usage; /* Low bitrate redundancy usage */ +} SKP_Silk_encoder_control; + +/* Struct for Packet Loss Concealment */ +typedef struct { + SKP_int32 pitchL_Q8; /* Pitch lag to use for voiced concealment */ + SKP_int16 LTPCoef_Q14[ LTP_ORDER ]; /* LTP coeficients to use for voiced concealment */ + SKP_int16 prevLPC_Q12[ MAX_LPC_ORDER ]; + SKP_int last_frame_lost; /* Was previous frame lost */ + SKP_int32 rand_seed; /* Seed for unvoiced signal generation */ + SKP_int16 randScale_Q14; /* Scaling of unvoiced random signal */ + SKP_int32 conc_energy; + SKP_int conc_energy_shift; + SKP_int16 prevLTP_scale_Q14; + SKP_int32 prevGain_Q16[ NB_SUBFR ]; + SKP_int fs_kHz; +} SKP_Silk_PLC_struct; + +/* Struct for CNG */ +typedef struct { + SKP_int32 CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ]; + SKP_int CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ]; + SKP_int32 CNG_synth_state[ MAX_LPC_ORDER ]; + SKP_int32 CNG_smth_Gain_Q16; + SKP_int32 rand_seed; + SKP_int fs_kHz; +} SKP_Silk_CNG_struct; + +/********************************/ +/* Decoder state */ +/********************************/ +typedef struct { + SKP_Silk_range_coder_state sRC; /* Range coder state */ + SKP_int32 prev_inv_gain_Q16; + SKP_int32 sLTP_Q16[ 2 * MAX_FRAME_LENGTH ]; + SKP_int32 sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ]; + SKP_int32 exc_Q10[ MAX_FRAME_LENGTH ]; + SKP_int32 res_Q10[ MAX_FRAME_LENGTH ]; + SKP_int16 outBuf[ 2 * MAX_FRAME_LENGTH ]; /* Buffer for output signal */ + SKP_int sLTP_buf_idx; /* LTP_buf_index */ + SKP_int lagPrev; /* Previous Lag */ + SKP_int LastGainIndex; /* Previous gain index */ + SKP_int LastGainIndex_EnhLayer; /* Previous gain index */ + SKP_int typeOffsetPrev; /* Previous signal type and quantization offset */ + SKP_int32 HPState[ DEC_HP_ORDER ]; /* HP filter state */ + const SKP_int16 *HP_A; /* HP filter AR coefficients */ + const SKP_int16 *HP_B; /* HP filter MA coefficients */ + SKP_int fs_kHz; /* Sampling frequency in kHz */ + SKP_int frame_length; /* Frame length (samples) */ + SKP_int subfr_length; /* Subframe length (samples) */ + SKP_int LPC_order; /* LPC order */ + SKP_int prevNLSF_Q15[ MAX_LPC_ORDER ]; /* Used to interpolate LSFs */ + SKP_int first_frame_after_reset; /* Flag for deactivating NLSF interp. and fluc. reduction after resets */ + + /* For buffering payload in case of more frames per packet */ + SKP_int nBytesLeft; + SKP_int nFramesDecoded; + SKP_int nFramesInPacket; + SKP_int moreInternalDecoderFrames; + SKP_int FrameTermination; + + SKP_int32 resampleState[ 15 ]; + + const SKP_Silk_NLSF_CB_struct *psNLSF_CB[ 2 ]; /* Pointers to voiced/unvoiced NLSF codebooks */ + + SKP_int sigtype[ MAX_FRAMES_PER_PACKET ]; + SKP_int QuantOffsetType[ MAX_FRAMES_PER_PACKET ]; + SKP_int GainsIndices[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ]; + SKP_int GainsIndices_EnhLayer[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ]; + SKP_int NLSFIndices[ MAX_FRAMES_PER_PACKET ][ NLSF_MSVQ_MAX_CB_STAGES ]; + SKP_int NLSFInterpCoef_Q2[ MAX_FRAMES_PER_PACKET ]; + SKP_int lagIndex[ MAX_FRAMES_PER_PACKET ]; + SKP_int contourIndex[ MAX_FRAMES_PER_PACKET ]; + SKP_int PERIndex[ MAX_FRAMES_PER_PACKET ]; + SKP_int LTPIndex[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ]; + SKP_int LTP_scaleIndex[ MAX_FRAMES_PER_PACKET ]; + SKP_int Seed[ MAX_FRAMES_PER_PACKET ]; + SKP_int vadFlagBuf[ MAX_FRAMES_PER_PACKET ]; + + /* Parameters used to investigate if inband FEC is used */ + SKP_int vadFlag; + SKP_int no_FEC_counter; /* Counts number of frames wo inband FEC */ + SKP_int inband_FEC_offset; /* 0: no FEC, 1: FEC with 1 packet offset, 2: FEC w 2 packets offset */ + + /* CNG state */ + SKP_Silk_CNG_struct sCNG; + + /* Stuff used for PLC */ + SKP_Silk_PLC_struct sPLC; + SKP_int lossCnt; + SKP_int prev_sigtype; /* Previous sigtype */ +#ifdef USE_INTERPOLATION_PLC + SKP_int16 prevQuant[ MAX_FRAME_LENGTH ]; + SKP_int prevPitchL[ NB_SUBFR ]; /* Previous Lags used */ + SKP_int16 prevLTPCoef_Q14[ NB_SUBFR * LTP_ORDER ]; /* Previous LTCoefs used */ + SKP_int16 prevAR_Q12[ MAX_LPC_ORDER ]; + SKP_int interpolDistance; /* Number of frames between old and new recieved packet */ +#endif + + + + + SKP_int bitstream_v; /* Holds bitstream version */ +} SKP_Silk_decoder_state; + +/************************/ +/* Decoder control */ +/************************/ +typedef struct { + /* prediction and coding parameters */ + SKP_int pitchL[ NB_SUBFR ]; + SKP_int32 Gains_Q16[ NB_SUBFR ]; + SKP_int32 Seed; + /* holds interpolated and final coefficients, 4-byte aligned */ + SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER ); + SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ]; + SKP_int LTP_scale_Q14; + + /* quantization indices */ + SKP_int PERIndex; + SKP_int RateLevelIndex; + SKP_int QuantOffsetType; + SKP_int sigtype; + SKP_int NLSFInterpCoef_Q2; +} SKP_Silk_decoder_control; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_structs_FIX.h b/app/src/main/jni/silk/src/SKP_Silk_structs_FIX.h similarity index 97% rename from jni/silk/src/SKP_Silk_structs_FIX.h rename to app/src/main/jni/silk/src/SKP_Silk_structs_FIX.h index d38bea7..fccfd53 100644 --- a/jni/silk/src/SKP_Silk_structs_FIX.h +++ b/app/src/main/jni/silk/src/SKP_Silk_structs_FIX.h @@ -1,195 +1,195 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_STRUCTS_FIX_H -#define SKP_SILK_STRUCTS_FIX_H - -#include "SKP_Silk_typedef.h" -#include "SKP_Silk_define_FIX.h" -#include "SKP_Silk_main.h" -#include "SKP_Silk_structs.h" - -#if LOW_COMPLEXITY_ONLY -#include "SKP_Silk_resample_rom.h" -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -/********************************/ -/* Noise shaping analysis state */ -/********************************/ -typedef struct { - SKP_int LastGainIndex; - SKP_int32 HarmBoost_smth_Q16; - SKP_int32 HarmShapeGain_smth_Q16; - SKP_int32 Tilt_smth_Q16; -} SKP_Silk_shape_state_FIX; - -/********************************/ -/* Prefilter state */ -/********************************/ -typedef struct { - SKP_int16 sLTP_shp1[ LTP_BUF_LENGTH ]; - SKP_int32 sAR_shp2_Q14[ SHAPE_LPC_ORDER_MAX ]; - SKP_int16 sLTP_shp2_FIX[ LTP_BUF_LENGTH ]; - SKP_int sLTP_shp_buf_idx1; - SKP_int sAR_shp_buf_idx2; - SKP_int sLTP_shp_buf_idx2; - SKP_int32 sLF_AR_shp1_Q12; - SKP_int32 sLF_MA_shp1_Q12; - SKP_int32 sLF_AR_shp2_Q12; - SKP_int32 sLF_MA_shp2_Q12; - SKP_int sHarmHP; - SKP_int32 rand_seed; - SKP_int lagPrev; -} SKP_Silk_prefilter_state_FIX; - -/*****************************/ -/* Prediction analysis state */ -/*****************************/ -typedef struct { - SKP_int pitch_LPC_win_length; - SKP_int min_pitch_lag; /* Lowest possible pitch lag (samples) */ - SKP_int max_pitch_lag; /* Highest possible pitch lag (samples) */ - SKP_int prev_NLSFq_Q15[ MAX_LPC_ORDER ]; /* Prev. quant. normalized LSF vector */ -} SKP_Silk_predict_state_FIX; - -#if( defined( SAVE_ALL_INTERNAL_DATA ) || defined( USE_FLT2FIX_WRAPPER ) ) -/*******************************************/ -/* Structure containing NLSF MSVQ codebook */ -/*******************************************/ -/* structure for one stage of MSVQ */ -typedef struct { - const SKP_int32 nVectors; - const SKP_float *CB; - const SKP_float *Rates; -} SKP_Silk_NLSF_CBS_FLP_TMP; - -typedef struct { - const SKP_int32 nStages; - - /* fields for (de)quantizing */ - const SKP_Silk_NLSF_CBS_FLP_TMP *CBStages; - const SKP_float *NDeltaMin; - - /* fields for arithmetic (de)coding */ - const SKP_uint16 *CDF; - const SKP_uint16 * const *StartPtr; - const SKP_int *MiddleIx; -} SKP_Silk_NLSF_CB_struct_FLP_TMP; -#endif - -/********************************/ -/* Encoder state FIX */ -/********************************/ -typedef struct { - SKP_Silk_encoder_state sCmn; /* Common struct, shared with floating-point code */ - -#if HIGH_PASS_INPUT - SKP_int32 variable_HP_smth1_Q15; /* State of first smoother */ - SKP_int32 variable_HP_smth2_Q15; /* State of second smoother */ -#endif - SKP_Silk_shape_state_FIX sShape; /* Shape state */ - SKP_Silk_prefilter_state_FIX sPrefilt; /* Prefilter State */ - SKP_Silk_predict_state_FIX sPred; /* Prediction state */ - SKP_Silk_nsq_state sNSQ; /* Noise Shape Quantizer State */ - SKP_Silk_nsq_state sNSQ_LBRR; /* Noise Shape Quantizer State ( for low bitrate redundancy ) */ - - /* Function pointer to noise shaping quantizer (will be set to SKP_Silk_NSQ or SKP_Silk_NSQ_del_dec) */ - void (* NoiseShapingQuantizer)( SKP_Silk_encoder_state *, SKP_Silk_encoder_control *, SKP_Silk_nsq_state *, const SKP_int16 *, - SKP_int *, const SKP_int, const SKP_int16 *, const SKP_int16 *, const SKP_int16 *, const SKP_int *, - const SKP_int *, const SKP_int32 *, const SKP_int32 *, SKP_int, const SKP_int - ); - - /* Buffer for find pitch and noise shape analysis */ - SKP_array_of_int16_4_byte_aligned( x_buf, 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ); - SKP_int LTPCorr_Q15; /* Normalized correlation from pitch lag estimator */ - SKP_int mu_LTP_Q8; /* Rate-distortion tradeoff in LTP quantization */ - SKP_int32 SNR_dB_Q7; /* Quality setting */ - SKP_int32 avgGain_Q16; /* average gain during active speech */ - SKP_int32 avgGain_Q16_one_bit_per_sample; /* average gain during active speech */ - SKP_int BufferedInChannel_ms; /* Simulated number of ms buffer because of exceeded TargetRate_bps */ - SKP_int speech_activity_Q8; /* Speech activity in Q8 */ - SKP_int32 pitchEstimationThreshold_Q16; /* Threshold for pitch estimator */ - - /* Parameters For LTP scaling Control */ - SKP_int prevLTPredCodGain_Q7; - SKP_int HPLTPredCodGain_Q7; - - SKP_int32 inBandFEC_SNR_comp_Q8; /* Compensation to SNR_dB when using inband FEC Voiced */ - -#ifdef USE_FLT2FIX_WRAPPER - const SKP_Silk_NLSF_CB_struct_FLP_TMP *psNLSF_CB_FLP[ 2 ]; /* Pointers to voiced/unvoiced NLSF codebooks */ -#endif -} SKP_Silk_encoder_state_FIX; - -/************************/ -/* Encoder control FIX */ -/************************/ -typedef struct { - SKP_Silk_encoder_control sCmn; /* Common struct, shared with floating-point code */ - - /* Prediction and coding parameters */ - SKP_int32 Gains_Q16[ NB_SUBFR ]; - SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER ); - SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ]; - SKP_int LTP_scale_Q14; - - /* Noise shaping parameters */ - /* Testing */ - SKP_array_of_int16_4_byte_aligned( AR1_Q13, NB_SUBFR * SHAPE_LPC_ORDER_MAX ); - SKP_array_of_int16_4_byte_aligned( AR2_Q13, NB_SUBFR * SHAPE_LPC_ORDER_MAX ); - SKP_int32 LF_shp_Q14[ NB_SUBFR ]; /* Packs two int16 coefficients per int32 value */ - SKP_int GainsPre_Q14[ NB_SUBFR ]; - SKP_int HarmBoost_Q14[ NB_SUBFR ]; - SKP_int Tilt_Q14[ NB_SUBFR ]; - SKP_int HarmShapeGain_Q14[ NB_SUBFR ]; - SKP_int Lambda_Q10; - SKP_int input_quality_Q14; - SKP_int coding_quality_Q14; - SKP_int32 pitch_freq_low_Hz; - SKP_int current_SNR_dB_Q7; - - /* measures */ - SKP_int sparseness_Q8; - SKP_int LTPredCodGain_Q7; - SKP_int input_quality_bands_Q15[ VAD_N_BANDS ]; - SKP_int input_tilt_Q15; - SKP_int32 ResNrg[ NB_SUBFR ]; /* Residual energy per subframe */ - SKP_int ResNrgQ[ NB_SUBFR ]; /* Q domain for the residual energy > 0 */ - -} SKP_Silk_encoder_control_FIX; - - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_STRUCTS_FIX_H +#define SKP_SILK_STRUCTS_FIX_H + +#include "SKP_Silk_typedef.h" +#include "SKP_Silk_define_FIX.h" +#include "SKP_Silk_main.h" +#include "SKP_Silk_structs.h" + +#if LOW_COMPLEXITY_ONLY +#include "SKP_Silk_resample_rom.h" +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +/********************************/ +/* Noise shaping analysis state */ +/********************************/ +typedef struct { + SKP_int LastGainIndex; + SKP_int32 HarmBoost_smth_Q16; + SKP_int32 HarmShapeGain_smth_Q16; + SKP_int32 Tilt_smth_Q16; +} SKP_Silk_shape_state_FIX; + +/********************************/ +/* Prefilter state */ +/********************************/ +typedef struct { + SKP_int16 sLTP_shp1[ LTP_BUF_LENGTH ]; + SKP_int32 sAR_shp2_Q14[ SHAPE_LPC_ORDER_MAX ]; + SKP_int16 sLTP_shp2_FIX[ LTP_BUF_LENGTH ]; + SKP_int sLTP_shp_buf_idx1; + SKP_int sAR_shp_buf_idx2; + SKP_int sLTP_shp_buf_idx2; + SKP_int32 sLF_AR_shp1_Q12; + SKP_int32 sLF_MA_shp1_Q12; + SKP_int32 sLF_AR_shp2_Q12; + SKP_int32 sLF_MA_shp2_Q12; + SKP_int sHarmHP; + SKP_int32 rand_seed; + SKP_int lagPrev; +} SKP_Silk_prefilter_state_FIX; + +/*****************************/ +/* Prediction analysis state */ +/*****************************/ +typedef struct { + SKP_int pitch_LPC_win_length; + SKP_int min_pitch_lag; /* Lowest possible pitch lag (samples) */ + SKP_int max_pitch_lag; /* Highest possible pitch lag (samples) */ + SKP_int prev_NLSFq_Q15[ MAX_LPC_ORDER ]; /* Prev. quant. normalized LSF vector */ +} SKP_Silk_predict_state_FIX; + +#if( defined( SAVE_ALL_INTERNAL_DATA ) || defined( USE_FLT2FIX_WRAPPER ) ) +/*******************************************/ +/* Structure containing NLSF MSVQ codebook */ +/*******************************************/ +/* structure for one stage of MSVQ */ +typedef struct { + const SKP_int32 nVectors; + const SKP_float *CB; + const SKP_float *Rates; +} SKP_Silk_NLSF_CBS_FLP_TMP; + +typedef struct { + const SKP_int32 nStages; + + /* fields for (de)quantizing */ + const SKP_Silk_NLSF_CBS_FLP_TMP *CBStages; + const SKP_float *NDeltaMin; + + /* fields for arithmetic (de)coding */ + const SKP_uint16 *CDF; + const SKP_uint16 * const *StartPtr; + const SKP_int *MiddleIx; +} SKP_Silk_NLSF_CB_struct_FLP_TMP; +#endif + +/********************************/ +/* Encoder state FIX */ +/********************************/ +typedef struct { + SKP_Silk_encoder_state sCmn; /* Common struct, shared with floating-point code */ + +#if HIGH_PASS_INPUT + SKP_int32 variable_HP_smth1_Q15; /* State of first smoother */ + SKP_int32 variable_HP_smth2_Q15; /* State of second smoother */ +#endif + SKP_Silk_shape_state_FIX sShape; /* Shape state */ + SKP_Silk_prefilter_state_FIX sPrefilt; /* Prefilter State */ + SKP_Silk_predict_state_FIX sPred; /* Prediction state */ + SKP_Silk_nsq_state sNSQ; /* Noise Shape Quantizer State */ + SKP_Silk_nsq_state sNSQ_LBRR; /* Noise Shape Quantizer State ( for low bitrate redundancy ) */ + + /* Function pointer to noise shaping quantizer (will be set to SKP_Silk_NSQ or SKP_Silk_NSQ_del_dec) */ + void (* NoiseShapingQuantizer)( SKP_Silk_encoder_state *, SKP_Silk_encoder_control *, SKP_Silk_nsq_state *, const SKP_int16 *, + SKP_int *, const SKP_int, const SKP_int16 *, const SKP_int16 *, const SKP_int16 *, const SKP_int *, + const SKP_int *, const SKP_int32 *, const SKP_int32 *, SKP_int, const SKP_int + ); + + /* Buffer for find pitch and noise shape analysis */ + SKP_array_of_int16_4_byte_aligned( x_buf, 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ); + SKP_int LTPCorr_Q15; /* Normalized correlation from pitch lag estimator */ + SKP_int mu_LTP_Q8; /* Rate-distortion tradeoff in LTP quantization */ + SKP_int32 SNR_dB_Q7; /* Quality setting */ + SKP_int32 avgGain_Q16; /* average gain during active speech */ + SKP_int32 avgGain_Q16_one_bit_per_sample; /* average gain during active speech */ + SKP_int BufferedInChannel_ms; /* Simulated number of ms buffer because of exceeded TargetRate_bps */ + SKP_int speech_activity_Q8; /* Speech activity in Q8 */ + SKP_int32 pitchEstimationThreshold_Q16; /* Threshold for pitch estimator */ + + /* Parameters For LTP scaling Control */ + SKP_int prevLTPredCodGain_Q7; + SKP_int HPLTPredCodGain_Q7; + + SKP_int32 inBandFEC_SNR_comp_Q8; /* Compensation to SNR_dB when using inband FEC Voiced */ + +#ifdef USE_FLT2FIX_WRAPPER + const SKP_Silk_NLSF_CB_struct_FLP_TMP *psNLSF_CB_FLP[ 2 ]; /* Pointers to voiced/unvoiced NLSF codebooks */ +#endif +} SKP_Silk_encoder_state_FIX; + +/************************/ +/* Encoder control FIX */ +/************************/ +typedef struct { + SKP_Silk_encoder_control sCmn; /* Common struct, shared with floating-point code */ + + /* Prediction and coding parameters */ + SKP_int32 Gains_Q16[ NB_SUBFR ]; + SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER ); + SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ]; + SKP_int LTP_scale_Q14; + + /* Noise shaping parameters */ + /* Testing */ + SKP_array_of_int16_4_byte_aligned( AR1_Q13, NB_SUBFR * SHAPE_LPC_ORDER_MAX ); + SKP_array_of_int16_4_byte_aligned( AR2_Q13, NB_SUBFR * SHAPE_LPC_ORDER_MAX ); + SKP_int32 LF_shp_Q14[ NB_SUBFR ]; /* Packs two int16 coefficients per int32 value */ + SKP_int GainsPre_Q14[ NB_SUBFR ]; + SKP_int HarmBoost_Q14[ NB_SUBFR ]; + SKP_int Tilt_Q14[ NB_SUBFR ]; + SKP_int HarmShapeGain_Q14[ NB_SUBFR ]; + SKP_int Lambda_Q10; + SKP_int input_quality_Q14; + SKP_int coding_quality_Q14; + SKP_int32 pitch_freq_low_Hz; + SKP_int current_SNR_dB_Q7; + + /* measures */ + SKP_int sparseness_Q8; + SKP_int LTPredCodGain_Q7; + SKP_int input_quality_bands_Q15[ VAD_N_BANDS ]; + SKP_int input_tilt_Q15; + SKP_int32 ResNrg[ NB_SUBFR ]; /* Residual energy per subframe */ + SKP_int ResNrgQ[ NB_SUBFR ]; /* Q domain for the residual energy > 0 */ + +} SKP_Silk_encoder_control_FIX; + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_sum_sqr_shift.c b/app/src/main/jni/silk/src/SKP_Silk_sum_sqr_shift.c similarity index 97% rename from jni/silk/src/SKP_Silk_sum_sqr_shift.c rename to app/src/main/jni/silk/src/SKP_Silk_sum_sqr_shift.c index 4904f8a..6713caa 100644 --- a/jni/silk/src/SKP_Silk_sum_sqr_shift.c +++ b/app/src/main/jni/silk/src/SKP_Silk_sum_sqr_shift.c @@ -1,98 +1,98 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/* * - * SKP_Silk_sum_sqr_shift.c * - * * - * compute number of bits to right shift the sum of squares of a vector * - * of int16s to make it fit in an int32 * - * * - * Copyright 2006-2008 (c), Skype Limited * - * */ -#include "SKP_Silk_SigProc_FIX.h" -/* Compute number of bits to right shift the sum of squares of a vector */ -/* of int16s to make it fit in an int32 */ -void SKP_Silk_sum_sqr_shift( - SKP_int32 *energy, /* O Energy of x, after shifting to the right */ - SKP_int *shift, /* O Number of bits right shift applied to energy */ - const SKP_int16 *x, /* I Input vector */ - SKP_int len /* I Length of input vector */ -) -{ - SKP_int i, shft; - SKP_int32 in32, nrg_tmp, nrg; - - if( (SKP_int32)( (SKP_int_ptr_size)x & 2 ) != 0 ) { - /* Input is not 4-byte aligned */ - nrg = SKP_SMULBB( x[ 0 ], x[ 0 ] ); - i = 1; - } else { - nrg = 0; - i = 0; - } - shft = 0; - len--; - for( ; i < len; i += 2 ) { - /* Load two values at once */ - in32 = *( (SKP_int32 *)&x[ i ] ); - nrg = SKP_SMLABB_ovflw( nrg, in32, in32 ); - nrg = SKP_SMLATT_ovflw( nrg, in32, in32 ); - if( nrg < 0 ) { - /* Scale down */ - nrg = (SKP_int32)SKP_RSHIFT_uint( (SKP_uint32)nrg, 2 ); - shft = 2; - break; - } - } - for( ; i < len; i += 2 ) { - /* Load two values at once */ - in32 = *( (SKP_int32 *)&x[ i ] ); - nrg_tmp = SKP_SMULBB( in32, in32 ); - nrg_tmp = SKP_SMLATT_ovflw( nrg_tmp, in32, in32 ); - nrg = (SKP_int32)SKP_ADD_RSHIFT_uint( nrg, (SKP_uint32)nrg_tmp, shft ); - if( nrg < 0 ) { - /* Scale down */ - nrg = (SKP_int32)SKP_RSHIFT_uint( (SKP_uint32)nrg, 2 ); - shft += 2; - } - } - if( i == len ) { - /* One sample left to process */ - nrg_tmp = SKP_SMULBB( x[ i ], x[ i ] ); - nrg = (SKP_int32)SKP_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); - } - - /* Make sure to have at least one extra leading zero (two leading zeros in total) */ - if( nrg & 0xC0000000 ) { - nrg = SKP_RSHIFT_uint( (SKP_uint32)nrg, 2 ); - shft += 2; - } - - /* Output arguments */ - *shift = shft; - *energy = nrg; -} +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/* * + * SKP_Silk_sum_sqr_shift.c * + * * + * compute number of bits to right shift the sum of squares of a vector * + * of int16s to make it fit in an int32 * + * * + * Copyright 2006-2008 (c), Skype Limited * + * */ +#include "SKP_Silk_SigProc_FIX.h" +/* Compute number of bits to right shift the sum of squares of a vector */ +/* of int16s to make it fit in an int32 */ +void SKP_Silk_sum_sqr_shift( + SKP_int32 *energy, /* O Energy of x, after shifting to the right */ + SKP_int *shift, /* O Number of bits right shift applied to energy */ + const SKP_int16 *x, /* I Input vector */ + SKP_int len /* I Length of input vector */ +) +{ + SKP_int i, shft; + SKP_int32 in32, nrg_tmp, nrg; + + if( (SKP_int32)( (SKP_int_ptr_size)x & 2 ) != 0 ) { + /* Input is not 4-byte aligned */ + nrg = SKP_SMULBB( x[ 0 ], x[ 0 ] ); + i = 1; + } else { + nrg = 0; + i = 0; + } + shft = 0; + len--; + for( ; i < len; i += 2 ) { + /* Load two values at once */ + in32 = *( (SKP_int32 *)&x[ i ] ); + nrg = SKP_SMLABB_ovflw( nrg, in32, in32 ); + nrg = SKP_SMLATT_ovflw( nrg, in32, in32 ); + if( nrg < 0 ) { + /* Scale down */ + nrg = (SKP_int32)SKP_RSHIFT_uint( (SKP_uint32)nrg, 2 ); + shft = 2; + break; + } + } + for( ; i < len; i += 2 ) { + /* Load two values at once */ + in32 = *( (SKP_int32 *)&x[ i ] ); + nrg_tmp = SKP_SMULBB( in32, in32 ); + nrg_tmp = SKP_SMLATT_ovflw( nrg_tmp, in32, in32 ); + nrg = (SKP_int32)SKP_ADD_RSHIFT_uint( nrg, (SKP_uint32)nrg_tmp, shft ); + if( nrg < 0 ) { + /* Scale down */ + nrg = (SKP_int32)SKP_RSHIFT_uint( (SKP_uint32)nrg, 2 ); + shft += 2; + } + } + if( i == len ) { + /* One sample left to process */ + nrg_tmp = SKP_SMULBB( x[ i ], x[ i ] ); + nrg = (SKP_int32)SKP_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); + } + + /* Make sure to have at least one extra leading zero (two leading zeros in total) */ + if( nrg & 0xC0000000 ) { + nrg = SKP_RSHIFT_uint( (SKP_uint32)nrg, 2 ); + shft += 2; + } + + /* Output arguments */ + *shift = shft; + *energy = nrg; +} diff --git a/jni/silk/src/SKP_Silk_tables.h b/app/src/main/jni/silk/src/SKP_Silk_tables.h similarity index 98% rename from jni/silk/src/SKP_Silk_tables.h rename to app/src/main/jni/silk/src/SKP_Silk_tables.h index b23b8d9..143cac6 100644 --- a/jni/silk/src/SKP_Silk_tables.h +++ b/app/src/main/jni/silk/src/SKP_Silk_tables.h @@ -1,171 +1,171 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_TABLES_H -#define SKP_SILK_TABLES_H - -#include "SKP_Silk_define.h" -#include "SKP_Silk_structs.h" - -#define PITCH_EST_MAX_LAG_MS 18 /* 18 ms -> 56 Hz */ -#define PITCH_EST_MIN_LAG_MS 2 /* 2 ms -> 500 Hz */ - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* entropy coding tables */ -extern const SKP_uint16 SKP_Silk_type_offset_CDF[ 5 ]; /* 5 */ -extern const SKP_uint16 SKP_Silk_type_offset_joint_CDF[ 4 ][ 5 ]; /* 20 */ -extern const SKP_int SKP_Silk_type_offset_CDF_offset; - -extern const SKP_uint16 SKP_Silk_gain_CDF[ 2 ][ N_LEVELS_QGAIN + 1 ]; /* 130 */ -extern const SKP_int SKP_Silk_gain_CDF_offset; -extern const SKP_uint16 SKP_Silk_delta_gain_CDF[ MAX_DELTA_GAIN_QUANT - MIN_DELTA_GAIN_QUANT + 2 ]; /* 46 */ -extern const SKP_int SKP_Silk_delta_gain_CDF_offset; - -extern const SKP_uint16 SKP_Silk_pitch_lag_NB_CDF[ 8 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 130 */ -extern const SKP_int SKP_Silk_pitch_lag_NB_CDF_offset; -extern const SKP_uint16 SKP_Silk_pitch_lag_MB_CDF[ 12 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 194 */ -extern const SKP_int SKP_Silk_pitch_lag_MB_CDF_offset; -extern const SKP_uint16 SKP_Silk_pitch_lag_WB_CDF[ 16 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 258 */ -extern const SKP_int SKP_Silk_pitch_lag_WB_CDF_offset; -extern const SKP_uint16 SKP_Silk_pitch_lag_SWB_CDF[ 24 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 386 */ -extern const SKP_int SKP_Silk_pitch_lag_SWB_CDF_offset; - -extern const SKP_uint16 SKP_Silk_pitch_contour_CDF[ 35 ]; /* 35 */ -extern const SKP_int SKP_Silk_pitch_contour_CDF_offset; -extern const SKP_uint16 SKP_Silk_pitch_contour_NB_CDF[ 12 ]; /* 12 */ -extern const SKP_int SKP_Silk_pitch_contour_NB_CDF_offset; -extern const SKP_uint16 SKP_Silk_pitch_delta_CDF[23]; /* 23 */ -extern const SKP_int SKP_Silk_pitch_delta_CDF_offset; - -extern const SKP_uint16 SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS ][ MAX_PULSES + 3 ]; /* 210 */ -extern const SKP_int SKP_Silk_pulses_per_block_CDF_offset; -extern const SKP_int16 SKP_Silk_pulses_per_block_BITS_Q6[ N_RATE_LEVELS - 1 ][ MAX_PULSES + 2 ]; /* 180 */ - -extern const SKP_uint16 SKP_Silk_rate_levels_CDF[ 2 ][ N_RATE_LEVELS ]; /* 20 */ -extern const SKP_int SKP_Silk_rate_levels_CDF_offset; -extern const SKP_int16 SKP_Silk_rate_levels_BITS_Q6[ 2 ][ N_RATE_LEVELS - 1 ]; /* 18 */ - -extern const SKP_int SKP_Silk_max_pulses_table[ 4 ]; /* 4 */ - -extern const SKP_uint16 SKP_Silk_shell_code_table0[ 33 ]; /* 33 */ -extern const SKP_uint16 SKP_Silk_shell_code_table1[ 52 ]; /* 52 */ -extern const SKP_uint16 SKP_Silk_shell_code_table2[ 102 ]; /* 102 */ -extern const SKP_uint16 SKP_Silk_shell_code_table3[ 207 ]; /* 207 */ -extern const SKP_uint16 SKP_Silk_shell_code_table_offsets[ 19 ]; /* 19 */ - -extern const SKP_uint16 SKP_Silk_lsb_CDF[ 3 ]; /* 3 */ - -extern const SKP_uint16 SKP_Silk_sign_CDF[ 36 ][ 3 ]; /* 108 */ - -extern const SKP_uint16 SKP_Silk_LTP_per_index_CDF[ 4 ]; /* 4 */ -extern const SKP_int SKP_Silk_LTP_per_index_CDF_offset; -extern const SKP_int16 * const SKP_Silk_LTP_gain_BITS_Q6_ptrs[ NB_LTP_CBKS ]; /* 3 */ -extern const SKP_uint16 * const SKP_Silk_LTP_gain_CDF_ptrs[ NB_LTP_CBKS ]; /* 3 */ -extern const SKP_int SKP_Silk_LTP_gain_CDF_offsets[ NB_LTP_CBKS ]; /* 3 */ -extern const SKP_int32 SKP_Silk_LTP_gain_middle_avg_RD_Q14; -extern const SKP_uint16 SKP_Silk_LTPscale_CDF[ 4 ]; /* 4 */ -extern const SKP_int SKP_Silk_LTPscale_offset; - -/* Tables for LTPScale */ -extern const SKP_int16 SKP_Silk_LTPScales_table_Q14[ 3 ]; - -extern const SKP_uint16 SKP_Silk_vadflag_CDF[ 3 ]; /* 3 */ -extern const SKP_int SKP_Silk_vadflag_offset; - -extern const SKP_int SKP_Silk_SamplingRates_table[ 4 ]; /* 4 */ -extern const SKP_uint16 SKP_Silk_SamplingRates_CDF[ 5 ]; /* 5 */ -extern const SKP_int SKP_Silk_SamplingRates_offset; - -extern const SKP_uint16 SKP_Silk_NLSF_interpolation_factor_CDF[ 6 ]; -extern const SKP_int SKP_Silk_NLSF_interpolation_factor_offset; - -/* NLSF codebooks */ -extern const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_16, SKP_Silk_NLSF_CB1_16; -extern const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_10, SKP_Silk_NLSF_CB1_10; - -/* quantization tables */ -extern const SKP_int16 * const SKP_Silk_LTP_vq_ptrs_Q14[ NB_LTP_CBKS ]; /* 168 */ -extern const SKP_int SKP_Silk_LTP_vq_sizes[ NB_LTP_CBKS ]; /* 3 */ - -/* Piece-wise linear mapping from bitrate in kbps to coding quality in dB SNR */ -extern const SKP_int32 TargetRate_table_NB[ TARGET_RATE_TAB_SZ ]; -extern const SKP_int32 TargetRate_table_MB[ TARGET_RATE_TAB_SZ ]; -extern const SKP_int32 TargetRate_table_WB[ TARGET_RATE_TAB_SZ ]; -extern const SKP_int32 TargetRate_table_SWB[ TARGET_RATE_TAB_SZ ]; -extern const SKP_int32 SNR_table_Q1[ TARGET_RATE_TAB_SZ ]; - -extern const SKP_int32 SNR_table_one_bit_per_sample_Q7[ 4 ]; - -/* Filter coeficicnts for HP filter: 4. Order filter implementad as two biquad filters */ -extern const SKP_int16 SKP_Silk_SWB_detect_B_HP_Q13[ NB_SOS ][ 3 ]; -extern const SKP_int16 SKP_Silk_SWB_detect_A_HP_Q13[ NB_SOS ][ 2 ]; - -/* Decoder high-pass filter coefficients for 24 kHz sampling */ -extern const SKP_int16 SKP_Silk_Dec_A_HP_24[ DEC_HP_ORDER ]; /* 2 */ -extern const SKP_int16 SKP_Silk_Dec_B_HP_24[ DEC_HP_ORDER + 1 ]; /* 3 */ - -/* Decoder high-pass filter coefficients for 16 kHz sampling */ -extern const SKP_int16 SKP_Silk_Dec_A_HP_16[ DEC_HP_ORDER ]; /* 2 */ -extern const SKP_int16 SKP_Silk_Dec_B_HP_16[ DEC_HP_ORDER + 1 ]; /* 3 */ - -/* Decoder high-pass filter coefficients for 12 kHz sampling */ -extern const SKP_int16 SKP_Silk_Dec_A_HP_12[ DEC_HP_ORDER ]; /* 2 */ -extern const SKP_int16 SKP_Silk_Dec_B_HP_12[ DEC_HP_ORDER + 1 ]; /* 3 */ - -/* Decoder high-pass filter coefficients for 8 kHz sampling */ -extern const SKP_int16 SKP_Silk_Dec_A_HP_8[ DEC_HP_ORDER ]; /* 2 */ -extern const SKP_int16 SKP_Silk_Dec_B_HP_8[ DEC_HP_ORDER + 1 ]; /* 3 */ - -/* Table for frame termination indication */ -extern const SKP_uint16 SKP_Silk_FrameTermination_CDF[ 5 ]; -extern const SKP_int SKP_Silk_FrameTermination_offset; - -extern const SKP_uint16 SKP_Silk_FrameTermination_v4_CDF[ 6 ]; -extern const SKP_int SKP_Silk_FrameTermination_v4_offset; - -/* Table for random seed */ -extern const SKP_uint16 SKP_Silk_Seed_CDF[ 5 ]; -extern const SKP_int SKP_Silk_Seed_offset; - -/* Quantization offsets */ -extern const SKP_int16 SKP_Silk_Quantization_Offsets_Q10[ 2 ][ 2 ]; - -#if SWITCH_TRANSITION_FILTERING -/* Interpolation points for filter coefficients used in the bandwidth transition smoother */ -extern const SKP_int32 SKP_Silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NB ]; -extern const SKP_int32 SKP_Silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NA ]; -#endif - -#ifdef __cplusplus -} -#endif - -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_TABLES_H +#define SKP_SILK_TABLES_H + +#include "SKP_Silk_define.h" +#include "SKP_Silk_structs.h" + +#define PITCH_EST_MAX_LAG_MS 18 /* 18 ms -> 56 Hz */ +#define PITCH_EST_MIN_LAG_MS 2 /* 2 ms -> 500 Hz */ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* entropy coding tables */ +extern const SKP_uint16 SKP_Silk_type_offset_CDF[ 5 ]; /* 5 */ +extern const SKP_uint16 SKP_Silk_type_offset_joint_CDF[ 4 ][ 5 ]; /* 20 */ +extern const SKP_int SKP_Silk_type_offset_CDF_offset; + +extern const SKP_uint16 SKP_Silk_gain_CDF[ 2 ][ N_LEVELS_QGAIN + 1 ]; /* 130 */ +extern const SKP_int SKP_Silk_gain_CDF_offset; +extern const SKP_uint16 SKP_Silk_delta_gain_CDF[ MAX_DELTA_GAIN_QUANT - MIN_DELTA_GAIN_QUANT + 2 ]; /* 46 */ +extern const SKP_int SKP_Silk_delta_gain_CDF_offset; + +extern const SKP_uint16 SKP_Silk_pitch_lag_NB_CDF[ 8 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 130 */ +extern const SKP_int SKP_Silk_pitch_lag_NB_CDF_offset; +extern const SKP_uint16 SKP_Silk_pitch_lag_MB_CDF[ 12 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 194 */ +extern const SKP_int SKP_Silk_pitch_lag_MB_CDF_offset; +extern const SKP_uint16 SKP_Silk_pitch_lag_WB_CDF[ 16 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 258 */ +extern const SKP_int SKP_Silk_pitch_lag_WB_CDF_offset; +extern const SKP_uint16 SKP_Silk_pitch_lag_SWB_CDF[ 24 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ]; /* 386 */ +extern const SKP_int SKP_Silk_pitch_lag_SWB_CDF_offset; + +extern const SKP_uint16 SKP_Silk_pitch_contour_CDF[ 35 ]; /* 35 */ +extern const SKP_int SKP_Silk_pitch_contour_CDF_offset; +extern const SKP_uint16 SKP_Silk_pitch_contour_NB_CDF[ 12 ]; /* 12 */ +extern const SKP_int SKP_Silk_pitch_contour_NB_CDF_offset; +extern const SKP_uint16 SKP_Silk_pitch_delta_CDF[23]; /* 23 */ +extern const SKP_int SKP_Silk_pitch_delta_CDF_offset; + +extern const SKP_uint16 SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS ][ MAX_PULSES + 3 ]; /* 210 */ +extern const SKP_int SKP_Silk_pulses_per_block_CDF_offset; +extern const SKP_int16 SKP_Silk_pulses_per_block_BITS_Q6[ N_RATE_LEVELS - 1 ][ MAX_PULSES + 2 ]; /* 180 */ + +extern const SKP_uint16 SKP_Silk_rate_levels_CDF[ 2 ][ N_RATE_LEVELS ]; /* 20 */ +extern const SKP_int SKP_Silk_rate_levels_CDF_offset; +extern const SKP_int16 SKP_Silk_rate_levels_BITS_Q6[ 2 ][ N_RATE_LEVELS - 1 ]; /* 18 */ + +extern const SKP_int SKP_Silk_max_pulses_table[ 4 ]; /* 4 */ + +extern const SKP_uint16 SKP_Silk_shell_code_table0[ 33 ]; /* 33 */ +extern const SKP_uint16 SKP_Silk_shell_code_table1[ 52 ]; /* 52 */ +extern const SKP_uint16 SKP_Silk_shell_code_table2[ 102 ]; /* 102 */ +extern const SKP_uint16 SKP_Silk_shell_code_table3[ 207 ]; /* 207 */ +extern const SKP_uint16 SKP_Silk_shell_code_table_offsets[ 19 ]; /* 19 */ + +extern const SKP_uint16 SKP_Silk_lsb_CDF[ 3 ]; /* 3 */ + +extern const SKP_uint16 SKP_Silk_sign_CDF[ 36 ][ 3 ]; /* 108 */ + +extern const SKP_uint16 SKP_Silk_LTP_per_index_CDF[ 4 ]; /* 4 */ +extern const SKP_int SKP_Silk_LTP_per_index_CDF_offset; +extern const SKP_int16 * const SKP_Silk_LTP_gain_BITS_Q6_ptrs[ NB_LTP_CBKS ]; /* 3 */ +extern const SKP_uint16 * const SKP_Silk_LTP_gain_CDF_ptrs[ NB_LTP_CBKS ]; /* 3 */ +extern const SKP_int SKP_Silk_LTP_gain_CDF_offsets[ NB_LTP_CBKS ]; /* 3 */ +extern const SKP_int32 SKP_Silk_LTP_gain_middle_avg_RD_Q14; +extern const SKP_uint16 SKP_Silk_LTPscale_CDF[ 4 ]; /* 4 */ +extern const SKP_int SKP_Silk_LTPscale_offset; + +/* Tables for LTPScale */ +extern const SKP_int16 SKP_Silk_LTPScales_table_Q14[ 3 ]; + +extern const SKP_uint16 SKP_Silk_vadflag_CDF[ 3 ]; /* 3 */ +extern const SKP_int SKP_Silk_vadflag_offset; + +extern const SKP_int SKP_Silk_SamplingRates_table[ 4 ]; /* 4 */ +extern const SKP_uint16 SKP_Silk_SamplingRates_CDF[ 5 ]; /* 5 */ +extern const SKP_int SKP_Silk_SamplingRates_offset; + +extern const SKP_uint16 SKP_Silk_NLSF_interpolation_factor_CDF[ 6 ]; +extern const SKP_int SKP_Silk_NLSF_interpolation_factor_offset; + +/* NLSF codebooks */ +extern const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_16, SKP_Silk_NLSF_CB1_16; +extern const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_10, SKP_Silk_NLSF_CB1_10; + +/* quantization tables */ +extern const SKP_int16 * const SKP_Silk_LTP_vq_ptrs_Q14[ NB_LTP_CBKS ]; /* 168 */ +extern const SKP_int SKP_Silk_LTP_vq_sizes[ NB_LTP_CBKS ]; /* 3 */ + +/* Piece-wise linear mapping from bitrate in kbps to coding quality in dB SNR */ +extern const SKP_int32 TargetRate_table_NB[ TARGET_RATE_TAB_SZ ]; +extern const SKP_int32 TargetRate_table_MB[ TARGET_RATE_TAB_SZ ]; +extern const SKP_int32 TargetRate_table_WB[ TARGET_RATE_TAB_SZ ]; +extern const SKP_int32 TargetRate_table_SWB[ TARGET_RATE_TAB_SZ ]; +extern const SKP_int32 SNR_table_Q1[ TARGET_RATE_TAB_SZ ]; + +extern const SKP_int32 SNR_table_one_bit_per_sample_Q7[ 4 ]; + +/* Filter coeficicnts for HP filter: 4. Order filter implementad as two biquad filters */ +extern const SKP_int16 SKP_Silk_SWB_detect_B_HP_Q13[ NB_SOS ][ 3 ]; +extern const SKP_int16 SKP_Silk_SWB_detect_A_HP_Q13[ NB_SOS ][ 2 ]; + +/* Decoder high-pass filter coefficients for 24 kHz sampling */ +extern const SKP_int16 SKP_Silk_Dec_A_HP_24[ DEC_HP_ORDER ]; /* 2 */ +extern const SKP_int16 SKP_Silk_Dec_B_HP_24[ DEC_HP_ORDER + 1 ]; /* 3 */ + +/* Decoder high-pass filter coefficients for 16 kHz sampling */ +extern const SKP_int16 SKP_Silk_Dec_A_HP_16[ DEC_HP_ORDER ]; /* 2 */ +extern const SKP_int16 SKP_Silk_Dec_B_HP_16[ DEC_HP_ORDER + 1 ]; /* 3 */ + +/* Decoder high-pass filter coefficients for 12 kHz sampling */ +extern const SKP_int16 SKP_Silk_Dec_A_HP_12[ DEC_HP_ORDER ]; /* 2 */ +extern const SKP_int16 SKP_Silk_Dec_B_HP_12[ DEC_HP_ORDER + 1 ]; /* 3 */ + +/* Decoder high-pass filter coefficients for 8 kHz sampling */ +extern const SKP_int16 SKP_Silk_Dec_A_HP_8[ DEC_HP_ORDER ]; /* 2 */ +extern const SKP_int16 SKP_Silk_Dec_B_HP_8[ DEC_HP_ORDER + 1 ]; /* 3 */ + +/* Table for frame termination indication */ +extern const SKP_uint16 SKP_Silk_FrameTermination_CDF[ 5 ]; +extern const SKP_int SKP_Silk_FrameTermination_offset; + +extern const SKP_uint16 SKP_Silk_FrameTermination_v4_CDF[ 6 ]; +extern const SKP_int SKP_Silk_FrameTermination_v4_offset; + +/* Table for random seed */ +extern const SKP_uint16 SKP_Silk_Seed_CDF[ 5 ]; +extern const SKP_int SKP_Silk_Seed_offset; + +/* Quantization offsets */ +extern const SKP_int16 SKP_Silk_Quantization_Offsets_Q10[ 2 ][ 2 ]; + +#if SWITCH_TRANSITION_FILTERING +/* Interpolation points for filter coefficients used in the bandwidth transition smoother */ +extern const SKP_int32 SKP_Silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NB ]; +extern const SKP_int32 SKP_Silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NA ]; +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/jni/silk/src/SKP_Silk_tables_LTP.c b/app/src/main/jni/silk/src/SKP_Silk_tables_LTP.c similarity index 96% rename from jni/silk/src/SKP_Silk_tables_LTP.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_LTP.c index ea52fe3..f5fcf9f 100644 --- a/jni/silk/src/SKP_Silk_tables_LTP.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_LTP.c @@ -1,324 +1,324 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_tables.h" - -const SKP_uint16 SKP_Silk_LTP_per_index_CDF[ 4 ] = { - 0, 20992, 40788, 65535 -}; - -const SKP_int SKP_Silk_LTP_per_index_CDF_offset = 1; - - -const SKP_uint16 SKP_Silk_LTP_gain_CDF_0[ 11 ] = { - 0, 49380, 54463, 56494, 58437, 60101, 61683, 62985, - 64066, 64823, 65535 -}; - -const SKP_uint16 SKP_Silk_LTP_gain_CDF_1[ 21 ] = { - 0, 25290, 30654, 35710, 40386, 42937, 45250, 47459, - 49411, 51348, 52974, 54517, 55976, 57423, 58865, 60285, - 61667, 62895, 63827, 64724, 65535 -}; - -const SKP_uint16 SKP_Silk_LTP_gain_CDF_2[ 41 ] = { - 0, 4958, 9439, 13581, 17638, 21651, 25015, 28025, - 30287, 32406, 34330, 36240, 38130, 39790, 41281, 42764, - 44229, 45676, 47081, 48431, 49675, 50849, 51932, 52966, - 53957, 54936, 55869, 56789, 57708, 58504, 59285, 60043, - 60796, 61542, 62218, 62871, 63483, 64076, 64583, 65062, - 65535 -}; - -const SKP_int SKP_Silk_LTP_gain_CDF_offsets[ 3 ] = { - 1, 3, 10 -}; - -const SKP_int32 SKP_Silk_LTP_gain_middle_avg_RD_Q14 = 11010; - -const SKP_int16 SKP_Silk_LTP_gain_BITS_Q6_0[ 10 ] = { - 26, 236, 321, 325, 339, 344, 362, 379, - 412, 418 -}; - -const SKP_int16 SKP_Silk_LTP_gain_BITS_Q6_1[ 20 ] = { - 88, 231, 237, 244, 300, 309, 313, 324, - 325, 341, 346, 351, 352, 352, 354, 356, - 367, 393, 396, 406 -}; - -const SKP_int16 SKP_Silk_LTP_gain_BITS_Q6_2[ 40 ] = { - 238, 248, 255, 257, 258, 274, 284, 311, - 317, 326, 326, 327, 339, 349, 350, 351, - 352, 355, 358, 366, 371, 379, 383, 387, - 388, 393, 394, 394, 407, 409, 412, 412, - 413, 422, 426, 432, 434, 449, 454, 455 -}; - -const SKP_uint16 * const SKP_Silk_LTP_gain_CDF_ptrs[ NB_LTP_CBKS ] = { - SKP_Silk_LTP_gain_CDF_0, - SKP_Silk_LTP_gain_CDF_1, - SKP_Silk_LTP_gain_CDF_2 -}; - -const SKP_int16 * const SKP_Silk_LTP_gain_BITS_Q6_ptrs[ NB_LTP_CBKS ] = { - SKP_Silk_LTP_gain_BITS_Q6_0, - SKP_Silk_LTP_gain_BITS_Q6_1, - SKP_Silk_LTP_gain_BITS_Q6_2 -}; - -const SKP_int16 SKP_Silk_LTP_gain_vq_0_Q14[ 10 ][ 5 ] = -{ -{ - 594, 984, 2840, 1021, 669 -}, -{ - 10, 35, 304, -1, 23 -}, -{ - -694, 1923, 4603, 2975, 2335 -}, -{ - 2437, 3176, 3778, 1940, 481 -}, -{ - 214, -46, 7870, 4406, -521 -}, -{ - -896, 4818, 8501, 1623, -887 -}, -{ - -696, 3178, 6480, -302, 1081 -}, -{ - 517, 599, 1002, 567, 560 -}, -{ - -2075, -834, 4712, -340, 896 -}, -{ - 1435, -644, 3993, -612, -2063 -} -}; - -const SKP_int16 SKP_Silk_LTP_gain_vq_1_Q14[ 20 ][ 5 ] = -{ -{ - 1655, 2918, 5001, 3010, 1775 -}, -{ - 113, 198, 856, 176, 178 -}, -{ - -843, 2479, 7858, 5371, 574 -}, -{ - 59, 5356, 7648, 2850, -315 -}, -{ - 3840, 4851, 6527, 1583, -1233 -}, -{ - 1620, 1760, 2330, 1876, 2045 -}, -{ - -545, 1854, 11792, 1547, -307 -}, -{ - -604, 689, 5369, 5074, 4265 -}, -{ - 521, -1331, 9829, 6209, -1211 -}, -{ - -1315, 6747, 9929, -1410, 546 -}, -{ - 117, -144, 2810, 1649, 5240 -}, -{ - 5392, 3476, 2425, -38, 633 -}, -{ - 14, -449, 5274, 3547, -171 -}, -{ - -98, 395, 9114, 1676, 844 -}, -{ - -908, 3843, 8861, -957, 1474 -}, -{ - 396, 6747, 5379, -329, 1269 -}, -{ - -335, 2830, 4281, 270, -54 -}, -{ - 1502, 5609, 8958, 6045, 2059 -}, -{ - -370, 479, 5267, 5726, 1174 -}, -{ - 5237, -1144, 6510, 455, 512 -} -}; - -const SKP_int16 SKP_Silk_LTP_gain_vq_2_Q14[ 40 ][ 5 ] = -{ -{ - -278, 415, 9345, 7106, -431 -}, -{ - -1006, 3863, 9524, 4724, -871 -}, -{ - -954, 4624, 11722, 973, -300 -}, -{ - -117, 7066, 8331, 1959, -901 -}, -{ - 593, 3412, 6070, 4914, 1567 -}, -{ - 54, -51, 12618, 4228, -844 -}, -{ - 3157, 4822, 5229, 2313, 717 -}, -{ - -244, 1161, 14198, 779, 69 -}, -{ - -1218, 5603, 12894, -2301, 1001 -}, -{ - -132, 3960, 9526, 577, 1806 -}, -{ - -1633, 8815, 10484, -2452, 895 -}, -{ - 235, 450, 1243, 667, 437 -}, -{ - 959, -2630, 10897, 8772, -1852 -}, -{ - 2420, 2046, 8893, 4427, -1569 -}, -{ - 23, 7091, 8356, -1285, 1508 -}, -{ - -1133, 835, 7662, 6043, 2800 -}, -{ - 439, 391, 11016, 2253, 1362 -}, -{ - -1020, 2876, 13436, 4015, -3020 -}, -{ - 1060, -2690, 13512, 5565, -1394 -}, -{ - -1420, 8007, 11421, -152, -1672 -}, -{ - -893, 2895, 15434, -1490, 159 -}, -{ - -1054, 428, 12208, 8538, -3344 -}, -{ - 1772, -1304, 7593, 6185, 561 -}, -{ - 525, -1207, 6659, 11151, -1170 -}, -{ - 439, 2667, 4743, 2359, 5515 -}, -{ - 2951, 7432, 7909, -230, -1564 -}, -{ - -72, 2140, 5477, 1391, 1580 -}, -{ - 476, -1312, 15912, 2174, -1027 -}, -{ - 5737, 441, 2493, 2043, 2757 -}, -{ - 228, -43, 1803, 6663, 7064 -}, -{ - 4596, 9182, 1917, -200, 203 -}, -{ - -704, 12039, 5451, -1188, 542 -}, -{ - 1782, -1040, 10078, 7513, -2767 -}, -{ - -2626, 7747, 9019, 62, 1710 -}, -{ - 235, -233, 2954, 10921, 1947 -}, -{ - 10854, 2814, 1232, -111, 222 -}, -{ - 2267, 2778, 12325, 156, -1658 -}, -{ - -2950, 8095, 16330, 268, -3626 -}, -{ - 67, 2083, 7950, -80, -2432 -}, -{ - 518, -66, 1718, 415, 11435 -} -}; - -const SKP_int16 * const SKP_Silk_LTP_vq_ptrs_Q14[ NB_LTP_CBKS ] = { - &SKP_Silk_LTP_gain_vq_0_Q14[ 0 ][ 0 ], - &SKP_Silk_LTP_gain_vq_1_Q14[ 0 ][ 0 ], - &SKP_Silk_LTP_gain_vq_2_Q14[ 0 ][ 0 ] -}; - -const SKP_int SKP_Silk_LTP_vq_sizes[ NB_LTP_CBKS ] = { - 10, 20, 40 -}; +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_tables.h" + +const SKP_uint16 SKP_Silk_LTP_per_index_CDF[ 4 ] = { + 0, 20992, 40788, 65535 +}; + +const SKP_int SKP_Silk_LTP_per_index_CDF_offset = 1; + + +const SKP_uint16 SKP_Silk_LTP_gain_CDF_0[ 11 ] = { + 0, 49380, 54463, 56494, 58437, 60101, 61683, 62985, + 64066, 64823, 65535 +}; + +const SKP_uint16 SKP_Silk_LTP_gain_CDF_1[ 21 ] = { + 0, 25290, 30654, 35710, 40386, 42937, 45250, 47459, + 49411, 51348, 52974, 54517, 55976, 57423, 58865, 60285, + 61667, 62895, 63827, 64724, 65535 +}; + +const SKP_uint16 SKP_Silk_LTP_gain_CDF_2[ 41 ] = { + 0, 4958, 9439, 13581, 17638, 21651, 25015, 28025, + 30287, 32406, 34330, 36240, 38130, 39790, 41281, 42764, + 44229, 45676, 47081, 48431, 49675, 50849, 51932, 52966, + 53957, 54936, 55869, 56789, 57708, 58504, 59285, 60043, + 60796, 61542, 62218, 62871, 63483, 64076, 64583, 65062, + 65535 +}; + +const SKP_int SKP_Silk_LTP_gain_CDF_offsets[ 3 ] = { + 1, 3, 10 +}; + +const SKP_int32 SKP_Silk_LTP_gain_middle_avg_RD_Q14 = 11010; + +const SKP_int16 SKP_Silk_LTP_gain_BITS_Q6_0[ 10 ] = { + 26, 236, 321, 325, 339, 344, 362, 379, + 412, 418 +}; + +const SKP_int16 SKP_Silk_LTP_gain_BITS_Q6_1[ 20 ] = { + 88, 231, 237, 244, 300, 309, 313, 324, + 325, 341, 346, 351, 352, 352, 354, 356, + 367, 393, 396, 406 +}; + +const SKP_int16 SKP_Silk_LTP_gain_BITS_Q6_2[ 40 ] = { + 238, 248, 255, 257, 258, 274, 284, 311, + 317, 326, 326, 327, 339, 349, 350, 351, + 352, 355, 358, 366, 371, 379, 383, 387, + 388, 393, 394, 394, 407, 409, 412, 412, + 413, 422, 426, 432, 434, 449, 454, 455 +}; + +const SKP_uint16 * const SKP_Silk_LTP_gain_CDF_ptrs[ NB_LTP_CBKS ] = { + SKP_Silk_LTP_gain_CDF_0, + SKP_Silk_LTP_gain_CDF_1, + SKP_Silk_LTP_gain_CDF_2 +}; + +const SKP_int16 * const SKP_Silk_LTP_gain_BITS_Q6_ptrs[ NB_LTP_CBKS ] = { + SKP_Silk_LTP_gain_BITS_Q6_0, + SKP_Silk_LTP_gain_BITS_Q6_1, + SKP_Silk_LTP_gain_BITS_Q6_2 +}; + +const SKP_int16 SKP_Silk_LTP_gain_vq_0_Q14[ 10 ][ 5 ] = +{ +{ + 594, 984, 2840, 1021, 669 +}, +{ + 10, 35, 304, -1, 23 +}, +{ + -694, 1923, 4603, 2975, 2335 +}, +{ + 2437, 3176, 3778, 1940, 481 +}, +{ + 214, -46, 7870, 4406, -521 +}, +{ + -896, 4818, 8501, 1623, -887 +}, +{ + -696, 3178, 6480, -302, 1081 +}, +{ + 517, 599, 1002, 567, 560 +}, +{ + -2075, -834, 4712, -340, 896 +}, +{ + 1435, -644, 3993, -612, -2063 +} +}; + +const SKP_int16 SKP_Silk_LTP_gain_vq_1_Q14[ 20 ][ 5 ] = +{ +{ + 1655, 2918, 5001, 3010, 1775 +}, +{ + 113, 198, 856, 176, 178 +}, +{ + -843, 2479, 7858, 5371, 574 +}, +{ + 59, 5356, 7648, 2850, -315 +}, +{ + 3840, 4851, 6527, 1583, -1233 +}, +{ + 1620, 1760, 2330, 1876, 2045 +}, +{ + -545, 1854, 11792, 1547, -307 +}, +{ + -604, 689, 5369, 5074, 4265 +}, +{ + 521, -1331, 9829, 6209, -1211 +}, +{ + -1315, 6747, 9929, -1410, 546 +}, +{ + 117, -144, 2810, 1649, 5240 +}, +{ + 5392, 3476, 2425, -38, 633 +}, +{ + 14, -449, 5274, 3547, -171 +}, +{ + -98, 395, 9114, 1676, 844 +}, +{ + -908, 3843, 8861, -957, 1474 +}, +{ + 396, 6747, 5379, -329, 1269 +}, +{ + -335, 2830, 4281, 270, -54 +}, +{ + 1502, 5609, 8958, 6045, 2059 +}, +{ + -370, 479, 5267, 5726, 1174 +}, +{ + 5237, -1144, 6510, 455, 512 +} +}; + +const SKP_int16 SKP_Silk_LTP_gain_vq_2_Q14[ 40 ][ 5 ] = +{ +{ + -278, 415, 9345, 7106, -431 +}, +{ + -1006, 3863, 9524, 4724, -871 +}, +{ + -954, 4624, 11722, 973, -300 +}, +{ + -117, 7066, 8331, 1959, -901 +}, +{ + 593, 3412, 6070, 4914, 1567 +}, +{ + 54, -51, 12618, 4228, -844 +}, +{ + 3157, 4822, 5229, 2313, 717 +}, +{ + -244, 1161, 14198, 779, 69 +}, +{ + -1218, 5603, 12894, -2301, 1001 +}, +{ + -132, 3960, 9526, 577, 1806 +}, +{ + -1633, 8815, 10484, -2452, 895 +}, +{ + 235, 450, 1243, 667, 437 +}, +{ + 959, -2630, 10897, 8772, -1852 +}, +{ + 2420, 2046, 8893, 4427, -1569 +}, +{ + 23, 7091, 8356, -1285, 1508 +}, +{ + -1133, 835, 7662, 6043, 2800 +}, +{ + 439, 391, 11016, 2253, 1362 +}, +{ + -1020, 2876, 13436, 4015, -3020 +}, +{ + 1060, -2690, 13512, 5565, -1394 +}, +{ + -1420, 8007, 11421, -152, -1672 +}, +{ + -893, 2895, 15434, -1490, 159 +}, +{ + -1054, 428, 12208, 8538, -3344 +}, +{ + 1772, -1304, 7593, 6185, 561 +}, +{ + 525, -1207, 6659, 11151, -1170 +}, +{ + 439, 2667, 4743, 2359, 5515 +}, +{ + 2951, 7432, 7909, -230, -1564 +}, +{ + -72, 2140, 5477, 1391, 1580 +}, +{ + 476, -1312, 15912, 2174, -1027 +}, +{ + 5737, 441, 2493, 2043, 2757 +}, +{ + 228, -43, 1803, 6663, 7064 +}, +{ + 4596, 9182, 1917, -200, 203 +}, +{ + -704, 12039, 5451, -1188, 542 +}, +{ + 1782, -1040, 10078, 7513, -2767 +}, +{ + -2626, 7747, 9019, 62, 1710 +}, +{ + 235, -233, 2954, 10921, 1947 +}, +{ + 10854, 2814, 1232, -111, 222 +}, +{ + 2267, 2778, 12325, 156, -1658 +}, +{ + -2950, 8095, 16330, 268, -3626 +}, +{ + 67, 2083, 7950, -80, -2432 +}, +{ + 518, -66, 1718, 415, 11435 +} +}; + +const SKP_int16 * const SKP_Silk_LTP_vq_ptrs_Q14[ NB_LTP_CBKS ] = { + &SKP_Silk_LTP_gain_vq_0_Q14[ 0 ][ 0 ], + &SKP_Silk_LTP_gain_vq_1_Q14[ 0 ][ 0 ], + &SKP_Silk_LTP_gain_vq_2_Q14[ 0 ][ 0 ] +}; + +const SKP_int SKP_Silk_LTP_vq_sizes[ NB_LTP_CBKS ] = { + 10, 20, 40 +}; diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.c b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.c similarity index 97% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.c index 3f9cadb..75256bb 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.c @@ -1,889 +1,889 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/**********************************************/ -/* This file has been automatically generated */ -/* */ -/* ROM usage: 138+1331 Words */ -/**********************************************/ - -#include "SKP_Silk_structs.h" -#include "SKP_Silk_tables_NLSF_CB0_10.h" - -const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ NLSF_MSVQ_CB0_10_VECTORS + NLSF_MSVQ_CB0_10_STAGES ] = -{ - 0, - 2658, - 4420, - 6107, - 7757, - 9408, - 10955, - 12502, - 13983, - 15432, - 16882, - 18331, - 19750, - 21108, - 22409, - 23709, - 25010, - 26256, - 27501, - 28747, - 29965, - 31158, - 32351, - 33544, - 34736, - 35904, - 36997, - 38091, - 39185, - 40232, - 41280, - 42327, - 43308, - 44290, - 45271, - 46232, - 47192, - 48132, - 49032, - 49913, - 50775, - 51618, - 52462, - 53287, - 54095, - 54885, - 55675, - 56449, - 57222, - 57979, - 58688, - 59382, - 60076, - 60726, - 61363, - 61946, - 62505, - 63052, - 63543, - 63983, - 64396, - 64766, - 65023, - 65279, - 65535, - 0, - 4977, - 9542, - 14106, - 18671, - 23041, - 27319, - 31596, - 35873, - 39969, - 43891, - 47813, - 51652, - 55490, - 59009, - 62307, - 65535, - 0, - 8571, - 17142, - 25529, - 33917, - 42124, - 49984, - 57844, - 65535, - 0, - 8732, - 17463, - 25825, - 34007, - 42189, - 50196, - 58032, - 65535, - 0, - 8948, - 17704, - 25733, - 33762, - 41791, - 49821, - 57678, - 65535, - 0, - 4374, - 8655, - 12936, - 17125, - 21313, - 25413, - 29512, - 33611, - 37710, - 41809, - 45820, - 49832, - 53843, - 57768, - 61694, - 65535 -}; - -const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_10_CDF_start_ptr[ NLSF_MSVQ_CB0_10_STAGES ] = -{ - &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 0 ], - &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 65 ], - &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 82 ], - &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 91 ], - &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 100 ], - &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 109 ] -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB0_10_CDF_middle_idx[ NLSF_MSVQ_CB0_10_STAGES ] = -{ - 23, - 8, - 5, - 5, - 5, - 9 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ NLSF_MSVQ_CB0_10_VECTORS ] = -{ - 148, 167, - 169, 170, - 170, 173, - 173, 175, - 176, 176, - 176, 177, - 179, 181, - 181, 181, - 183, 183, - 183, 184, - 185, 185, - 185, 185, - 186, 189, - 189, 189, - 191, 191, - 191, 194, - 194, 194, - 195, 195, - 196, 198, - 199, 200, - 201, 201, - 202, 203, - 204, 204, - 205, 205, - 206, 209, - 210, 210, - 213, 214, - 218, 220, - 221, 226, - 231, 234, - 239, 256, - 256, 256, - 119, 123, - 123, 123, - 125, 126, - 126, 126, - 128, 130, - 130, 131, - 131, 135, - 138, 139, - 94, 94, - 95, 95, - 96, 98, - 98, 99, - 93, 93, - 95, 96, - 96, 97, - 98, 100, - 92, 93, - 97, 97, - 97, 97, - 98, 98, - 125, 126, - 126, 127, - 127, 128, - 128, 128, - 128, 128, - 129, 129, - 129, 130, - 130, 131 -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB0_10_ndelta_min_Q15[ 10 + 1 ] = -{ - 563, - 3, - 22, - 20, - 3, - 3, - 132, - 119, - 358, - 86, - 964 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * NLSF_MSVQ_CB0_10_VECTORS ] = -{ - 2210, 4023, - 6981, 9260, - 12573, 15687, - 19207, 22383, - 25981, 29142, - 3285, 4172, - 6116, 10856, - 15289, 16826, - 19701, 22010, - 24721, 29313, - 1554, 2511, - 6577, 10337, - 13837, 16511, - 20086, 23214, - 26480, 29464, - 3062, 4017, - 5771, 10037, - 13365, 14952, - 20140, 22891, - 25229, 29603, - 2085, 3457, - 5934, 8718, - 11501, 13670, - 17997, 21817, - 24935, 28745, - 2776, 4093, - 6421, 10413, - 15111, 16806, - 20825, 23826, - 26308, 29411, - 2717, 4034, - 5697, 8463, - 14301, 16354, - 19007, 23413, - 25812, 28506, - 2872, 3702, - 5881, 11034, - 17141, 18879, - 21146, 23451, - 25817, 29600, - 2999, 4015, - 7357, 11219, - 12866, 17307, - 20081, 22644, - 26774, 29107, - 2942, 3866, - 5918, 11915, - 13909, 16072, - 20453, 22279, - 27310, 29826, - 2271, 3527, - 6606, 9729, - 12943, 17382, - 20224, 22345, - 24602, 28290, - 2207, 3310, - 5844, 9339, - 11141, 15651, - 18576, 21177, - 25551, 28228, - 3963, 4975, - 6901, 11588, - 13466, 15577, - 19231, 21368, - 25510, 27759, - 2749, 3549, - 6966, 13808, - 15653, 17645, - 20090, 22599, - 26467, 28537, - 2126, 3504, - 5109, 9954, - 12550, 14620, - 19703, 21687, - 26457, 29106, - 3966, 5745, - 7442, 9757, - 14468, 16404, - 19135, 23048, - 25375, 28391, - 3197, 4751, - 6451, 9298, - 13038, 14874, - 17962, 20627, - 23835, 28464, - 3195, 4081, - 6499, 12252, - 14289, 16040, - 18357, 20730, - 26980, 29309, - 1533, 2471, - 4486, 7796, - 12332, 15758, - 19567, 22298, - 25673, 29051, - 2002, 2971, - 4985, 8083, - 13181, 15435, - 18237, 21517, - 24595, 28351, - 3808, 4925, - 6710, 10201, - 12011, 14300, - 18457, 20391, - 26525, 28956, - 2281, 3418, - 4979, 8726, - 15964, 18104, - 20250, 22771, - 25286, 28954, - 3051, 5479, - 7290, 9848, - 12744, 14503, - 18665, 23684, - 26065, 28947, - 2364, 3565, - 5502, 9621, - 14922, 16621, - 19005, 20996, - 26310, 29302, - 4093, 5212, - 6833, 9880, - 16303, 18286, - 20571, 23614, - 26067, 29128, - 2941, 3996, - 6038, 10638, - 12668, 14451, - 16798, 19392, - 26051, 28517, - 3863, 5212, - 7019, 9468, - 11039, 13214, - 19942, 22344, - 25126, 29539, - 4615, 6172, - 7853, 10252, - 12611, 14445, - 19719, 22441, - 24922, 29341, - 3566, 4512, - 6985, 8684, - 10544, 16097, - 18058, 22475, - 26066, 28167, - 4481, 5489, - 7432, 11414, - 13191, 15225, - 20161, 22258, - 26484, 29716, - 3320, 4320, - 6621, 9867, - 11581, 14034, - 21168, 23210, - 26588, 29903, - 3794, 4689, - 6916, 8655, - 10143, 16144, - 19568, 21588, - 27557, 29593, - 2446, 3276, - 5918, 12643, - 16601, 18013, - 21126, 23175, - 27300, 29634, - 2450, 3522, - 5437, 8560, - 15285, 19911, - 21826, 24097, - 26567, 29078, - 2580, 3796, - 5580, 8338, - 9969, 12675, - 18907, 22753, - 25450, 29292, - 3325, 4312, - 6241, 7709, - 9164, 14452, - 21665, 23797, - 27096, 29857, - 3338, 4163, - 7738, 11114, - 12668, 14753, - 16931, 22736, - 25671, 28093, - 3840, 4755, - 7755, 13471, - 15338, 17180, - 20077, 22353, - 27181, 29743, - 2504, 4079, - 8351, 12118, - 15046, 18595, - 21684, 24704, - 27519, 29937, - 5234, 6342, - 8267, 11821, - 15155, 16760, - 20667, 23488, - 25949, 29307, - 2681, 3562, - 6028, 10827, - 18458, 20458, - 22303, 24701, - 26912, 29956, - 3374, 4528, - 6230, 8256, - 9513, 12730, - 18666, 20720, - 26007, 28425, - 2731, 3629, - 8320, 12450, - 14112, 16431, - 18548, 22098, - 25329, 27718, - 3481, 4401, - 7321, 9319, - 11062, 13093, - 15121, 22315, - 26331, 28740, - 3577, 4945, - 6669, 8792, - 10299, 12645, - 19505, 24766, - 26996, 29634, - 4058, 5060, - 7288, 10190, - 11724, 13936, - 15849, 18539, - 26701, 29845, - 4262, 5390, - 7057, 8982, - 10187, 15264, - 20480, 22340, - 25958, 28072, - 3404, 4329, - 6629, 7946, - 10121, 17165, - 19640, 22244, - 25062, 27472, - 3157, 4168, - 6195, 9319, - 10771, 13325, - 15416, 19816, - 24672, 27634, - 2503, 3473, - 5130, 6767, - 8571, 14902, - 19033, 21926, - 26065, 28728, - 4133, 5102, - 7553, 10054, - 11757, 14924, - 17435, 20186, - 23987, 26272, - 4972, 6139, - 7894, 9633, - 11320, 14295, - 21737, 24306, - 26919, 29907, - 2958, 3816, - 6851, 9204, - 10895, 18052, - 20791, 23338, - 27556, 29609, - 5234, 6028, - 8034, 10154, - 11242, 14789, - 18948, 20966, - 26585, 29127, - 5241, 6838, - 10526, 12819, - 14681, 17328, - 19928, 22336, - 26193, 28697, - 3412, 4251, - 5988, 7094, - 9907, 18243, - 21669, 23777, - 26969, 29087, - 2470, 3217, - 7797, 15296, - 17365, 19135, - 21979, 24256, - 27322, 29442, - 4939, 5804, - 8145, 11809, - 13873, 15598, - 17234, 19423, - 26476, 29645, - 5051, 6167, - 8223, 9655, - 12159, 17995, - 20464, 22832, - 26616, 28462, - 4987, 5907, - 9319, 11245, - 13132, 15024, - 17485, 22687, - 26011, 28273, - 5137, 6884, - 11025, 14950, - 17191, 19425, - 21807, 24393, - 26938, 29288, - 7057, 7884, - 9528, 10483, - 10960, 14811, - 19070, 21675, - 25645, 28019, - 6759, 7160, - 8546, 11779, - 12295, 13023, - 16627, 21099, - 24697, 28287, - 3863, 9762, - 11068, 11445, - 12049, 13960, - 18085, 21507, - 25224, 28997, - 397, 335, - 651, 1168, - 640, 765, - 465, 331, - 214, -194, - -578, -647, - -657, 750, - 564, 613, - 549, 630, - 304, -52, - 828, 922, - 443, 111, - 138, 124, - 169, 14, - 144, 83, - 132, 58, - -413, -752, - 869, 336, - 385, 69, - 56, 830, - -227, -266, - -368, -440, - -1195, 163, - 126, -228, - 802, 156, - 188, 120, - 376, 59, - -358, -558, - -1326, -254, - -202, -789, - 296, 92, - -70, -129, - -718, -1135, - 292, -29, - -631, 487, - -157, -153, - -279, 2, - -419, -342, - -34, -514, - -799, -1571, - -687, -609, - -546, -130, - -215, -252, - -446, -574, - -1337, 207, - -72, 32, - 103, -642, - 942, 733, - 187, 29, - -211, -814, - 143, 225, - 20, 24, - -268, -377, - 1623, 1133, - 667, 164, - 307, 366, - 187, 34, - 62, -313, - -832, -1482, - -1181, 483, - -42, -39, - -450, -1406, - -587, -52, - -760, 334, - 98, -60, - -500, -488, - -1058, 299, - 131, -250, - -251, -703, - 1037, 568, - -413, -265, - 1687, 573, - 345, 323, - 98, 61, - -102, 31, - 135, 149, - 617, 365, - -39, 34, - -611, 1201, - 1421, 736, - -414, -393, - -492, -343, - -316, -532, - 528, 172, - 90, 322, - -294, -319, - -541, 503, - 639, 401, - 1, -149, - -73, -167, - 150, 118, - 308, 218, - 121, 195, - -143, -261, - -1013, -802, - 387, 436, - 130, -427, - -448, -681, - 123, -87, - -251, -113, - 274, 310, - 445, 501, - 354, 272, - 141, -285, - 569, 656, - 37, -49, - 251, -386, - -263, 1122, - 604, 606, - 336, 95, - 34, 0, - 85, 180, - 207, -367, - -622, 1070, - -6, -79, - -160, -92, - -137, -276, - -323, -371, - -696, -1036, - 407, 102, - -86, -214, - -482, -647, - -28, -291, - -97, -180, - -250, -435, - -18, -76, - -332, 410, - 407, 168, - 539, 411, - 254, 111, - 58, -145, - 200, 30, - 187, 116, - 131, -367, - -475, 781, - -559, 561, - 195, -115, - 8, -168, - 30, 55, - -122, 131, - 82, -5, - -273, -50, - -632, 668, - 4, 32, - -26, -279, - 315, 165, - 197, 377, - 155, -41, - -138, -324, - -109, -617, - 360, 98, - -53, -319, - -114, -245, - -82, 507, - 468, 263, - -137, -389, - 652, 354, - -18, -227, - -462, -135, - 317, 53, - -16, 66, - -72, -126, - -356, -347, - -328, -72, - -337, 324, - 152, 349, - 169, -196, - 179, 254, - 260, 325, - -74, -80, - 75, -31, - 270, 275, - 87, 278, - -446, -301, - 309, 71, - -25, -242, - 516, 161, - -162, -83, - 329, 230, - -311, -259, - 177, -26, - -462, 89, - 257, 6, - -130, -93, - -456, -317, - -221, -206, - -417, -182, - -74, 234, - 48, 261, - 359, 231, - 258, 85, - -282, 252, - -147, -222, - 251, -207, - 443, 123, - -417, -36, - 273, -241, - 240, -112, - 44, -167, - 126, -124, - -77, 58, - -401, 333, - -118, 82, - 126, 151, - -433, 359, - -130, -102, - 131, -244, - 86, 85, - -462, 414, - -240, 16, - 145, 28, - -205, -481, - 373, 293, - -72, -174, - 62, 259, - -8, -18, - 362, 233, - 185, 43, - 278, 27, - 193, 570, - -248, 189, - 92, 31, - -275, -3, - 243, 176, - 438, 209, - 206, -51, - 79, 109, - 168, -185, - -308, -68, - -618, 385, - -310, -108, - -164, 165, - 61, -152, - -101, -412, - -268, -257, - -40, -20, - -28, -158, - -301, 271, - 380, -338, - -367, -132, - 64, 114, - -131, -225, - -156, -260, - -63, -116, - 155, -586, - -202, 254, - -287, 178, - 227, -106, - -294, 164, - 298, -100, - 185, 317, - 193, -45, - 28, 80, - -87, -433, - 22, -48, - 48, -237, - -229, -139, - 120, -364, - 268, -136, - 396, 125, - 130, -89, - -272, 118, - -256, -68, - -451, 488, - 143, -165, - -48, -190, - 106, 219, - 47, 435, - 245, 97, - 75, -418, - 121, -187, - 570, -200, - -351, 225, - -21, -217, - 234, -111, - 194, 14, - 242, 118, - 140, -397, - 355, 361, - -45, -195 -}; - -const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB0_10_Stage_info[ NLSF_MSVQ_CB0_10_STAGES ] = -{ - { 64, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 0 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 0 ] }, - { 16, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 64 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 64 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 80 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 80 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 88 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 88 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 96 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 96 ] }, - { 16, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 104 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 104 ] } -}; - -const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_10 = -{ - NLSF_MSVQ_CB0_10_STAGES, - SKP_Silk_NLSF_CB0_10_Stage_info, - SKP_Silk_NLSF_MSVQ_CB0_10_ndelta_min_Q15, - SKP_Silk_NLSF_MSVQ_CB0_10_CDF, - SKP_Silk_NLSF_MSVQ_CB0_10_CDF_start_ptr, - SKP_Silk_NLSF_MSVQ_CB0_10_CDF_middle_idx -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/**********************************************/ +/* This file has been automatically generated */ +/* */ +/* ROM usage: 138+1331 Words */ +/**********************************************/ + +#include "SKP_Silk_structs.h" +#include "SKP_Silk_tables_NLSF_CB0_10.h" + +const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ NLSF_MSVQ_CB0_10_VECTORS + NLSF_MSVQ_CB0_10_STAGES ] = +{ + 0, + 2658, + 4420, + 6107, + 7757, + 9408, + 10955, + 12502, + 13983, + 15432, + 16882, + 18331, + 19750, + 21108, + 22409, + 23709, + 25010, + 26256, + 27501, + 28747, + 29965, + 31158, + 32351, + 33544, + 34736, + 35904, + 36997, + 38091, + 39185, + 40232, + 41280, + 42327, + 43308, + 44290, + 45271, + 46232, + 47192, + 48132, + 49032, + 49913, + 50775, + 51618, + 52462, + 53287, + 54095, + 54885, + 55675, + 56449, + 57222, + 57979, + 58688, + 59382, + 60076, + 60726, + 61363, + 61946, + 62505, + 63052, + 63543, + 63983, + 64396, + 64766, + 65023, + 65279, + 65535, + 0, + 4977, + 9542, + 14106, + 18671, + 23041, + 27319, + 31596, + 35873, + 39969, + 43891, + 47813, + 51652, + 55490, + 59009, + 62307, + 65535, + 0, + 8571, + 17142, + 25529, + 33917, + 42124, + 49984, + 57844, + 65535, + 0, + 8732, + 17463, + 25825, + 34007, + 42189, + 50196, + 58032, + 65535, + 0, + 8948, + 17704, + 25733, + 33762, + 41791, + 49821, + 57678, + 65535, + 0, + 4374, + 8655, + 12936, + 17125, + 21313, + 25413, + 29512, + 33611, + 37710, + 41809, + 45820, + 49832, + 53843, + 57768, + 61694, + 65535 +}; + +const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_10_CDF_start_ptr[ NLSF_MSVQ_CB0_10_STAGES ] = +{ + &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 0 ], + &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 65 ], + &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 82 ], + &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 91 ], + &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 100 ], + &SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ 109 ] +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB0_10_CDF_middle_idx[ NLSF_MSVQ_CB0_10_STAGES ] = +{ + 23, + 8, + 5, + 5, + 5, + 9 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ NLSF_MSVQ_CB0_10_VECTORS ] = +{ + 148, 167, + 169, 170, + 170, 173, + 173, 175, + 176, 176, + 176, 177, + 179, 181, + 181, 181, + 183, 183, + 183, 184, + 185, 185, + 185, 185, + 186, 189, + 189, 189, + 191, 191, + 191, 194, + 194, 194, + 195, 195, + 196, 198, + 199, 200, + 201, 201, + 202, 203, + 204, 204, + 205, 205, + 206, 209, + 210, 210, + 213, 214, + 218, 220, + 221, 226, + 231, 234, + 239, 256, + 256, 256, + 119, 123, + 123, 123, + 125, 126, + 126, 126, + 128, 130, + 130, 131, + 131, 135, + 138, 139, + 94, 94, + 95, 95, + 96, 98, + 98, 99, + 93, 93, + 95, 96, + 96, 97, + 98, 100, + 92, 93, + 97, 97, + 97, 97, + 98, 98, + 125, 126, + 126, 127, + 127, 128, + 128, 128, + 128, 128, + 129, 129, + 129, 130, + 130, 131 +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB0_10_ndelta_min_Q15[ 10 + 1 ] = +{ + 563, + 3, + 22, + 20, + 3, + 3, + 132, + 119, + 358, + 86, + 964 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * NLSF_MSVQ_CB0_10_VECTORS ] = +{ + 2210, 4023, + 6981, 9260, + 12573, 15687, + 19207, 22383, + 25981, 29142, + 3285, 4172, + 6116, 10856, + 15289, 16826, + 19701, 22010, + 24721, 29313, + 1554, 2511, + 6577, 10337, + 13837, 16511, + 20086, 23214, + 26480, 29464, + 3062, 4017, + 5771, 10037, + 13365, 14952, + 20140, 22891, + 25229, 29603, + 2085, 3457, + 5934, 8718, + 11501, 13670, + 17997, 21817, + 24935, 28745, + 2776, 4093, + 6421, 10413, + 15111, 16806, + 20825, 23826, + 26308, 29411, + 2717, 4034, + 5697, 8463, + 14301, 16354, + 19007, 23413, + 25812, 28506, + 2872, 3702, + 5881, 11034, + 17141, 18879, + 21146, 23451, + 25817, 29600, + 2999, 4015, + 7357, 11219, + 12866, 17307, + 20081, 22644, + 26774, 29107, + 2942, 3866, + 5918, 11915, + 13909, 16072, + 20453, 22279, + 27310, 29826, + 2271, 3527, + 6606, 9729, + 12943, 17382, + 20224, 22345, + 24602, 28290, + 2207, 3310, + 5844, 9339, + 11141, 15651, + 18576, 21177, + 25551, 28228, + 3963, 4975, + 6901, 11588, + 13466, 15577, + 19231, 21368, + 25510, 27759, + 2749, 3549, + 6966, 13808, + 15653, 17645, + 20090, 22599, + 26467, 28537, + 2126, 3504, + 5109, 9954, + 12550, 14620, + 19703, 21687, + 26457, 29106, + 3966, 5745, + 7442, 9757, + 14468, 16404, + 19135, 23048, + 25375, 28391, + 3197, 4751, + 6451, 9298, + 13038, 14874, + 17962, 20627, + 23835, 28464, + 3195, 4081, + 6499, 12252, + 14289, 16040, + 18357, 20730, + 26980, 29309, + 1533, 2471, + 4486, 7796, + 12332, 15758, + 19567, 22298, + 25673, 29051, + 2002, 2971, + 4985, 8083, + 13181, 15435, + 18237, 21517, + 24595, 28351, + 3808, 4925, + 6710, 10201, + 12011, 14300, + 18457, 20391, + 26525, 28956, + 2281, 3418, + 4979, 8726, + 15964, 18104, + 20250, 22771, + 25286, 28954, + 3051, 5479, + 7290, 9848, + 12744, 14503, + 18665, 23684, + 26065, 28947, + 2364, 3565, + 5502, 9621, + 14922, 16621, + 19005, 20996, + 26310, 29302, + 4093, 5212, + 6833, 9880, + 16303, 18286, + 20571, 23614, + 26067, 29128, + 2941, 3996, + 6038, 10638, + 12668, 14451, + 16798, 19392, + 26051, 28517, + 3863, 5212, + 7019, 9468, + 11039, 13214, + 19942, 22344, + 25126, 29539, + 4615, 6172, + 7853, 10252, + 12611, 14445, + 19719, 22441, + 24922, 29341, + 3566, 4512, + 6985, 8684, + 10544, 16097, + 18058, 22475, + 26066, 28167, + 4481, 5489, + 7432, 11414, + 13191, 15225, + 20161, 22258, + 26484, 29716, + 3320, 4320, + 6621, 9867, + 11581, 14034, + 21168, 23210, + 26588, 29903, + 3794, 4689, + 6916, 8655, + 10143, 16144, + 19568, 21588, + 27557, 29593, + 2446, 3276, + 5918, 12643, + 16601, 18013, + 21126, 23175, + 27300, 29634, + 2450, 3522, + 5437, 8560, + 15285, 19911, + 21826, 24097, + 26567, 29078, + 2580, 3796, + 5580, 8338, + 9969, 12675, + 18907, 22753, + 25450, 29292, + 3325, 4312, + 6241, 7709, + 9164, 14452, + 21665, 23797, + 27096, 29857, + 3338, 4163, + 7738, 11114, + 12668, 14753, + 16931, 22736, + 25671, 28093, + 3840, 4755, + 7755, 13471, + 15338, 17180, + 20077, 22353, + 27181, 29743, + 2504, 4079, + 8351, 12118, + 15046, 18595, + 21684, 24704, + 27519, 29937, + 5234, 6342, + 8267, 11821, + 15155, 16760, + 20667, 23488, + 25949, 29307, + 2681, 3562, + 6028, 10827, + 18458, 20458, + 22303, 24701, + 26912, 29956, + 3374, 4528, + 6230, 8256, + 9513, 12730, + 18666, 20720, + 26007, 28425, + 2731, 3629, + 8320, 12450, + 14112, 16431, + 18548, 22098, + 25329, 27718, + 3481, 4401, + 7321, 9319, + 11062, 13093, + 15121, 22315, + 26331, 28740, + 3577, 4945, + 6669, 8792, + 10299, 12645, + 19505, 24766, + 26996, 29634, + 4058, 5060, + 7288, 10190, + 11724, 13936, + 15849, 18539, + 26701, 29845, + 4262, 5390, + 7057, 8982, + 10187, 15264, + 20480, 22340, + 25958, 28072, + 3404, 4329, + 6629, 7946, + 10121, 17165, + 19640, 22244, + 25062, 27472, + 3157, 4168, + 6195, 9319, + 10771, 13325, + 15416, 19816, + 24672, 27634, + 2503, 3473, + 5130, 6767, + 8571, 14902, + 19033, 21926, + 26065, 28728, + 4133, 5102, + 7553, 10054, + 11757, 14924, + 17435, 20186, + 23987, 26272, + 4972, 6139, + 7894, 9633, + 11320, 14295, + 21737, 24306, + 26919, 29907, + 2958, 3816, + 6851, 9204, + 10895, 18052, + 20791, 23338, + 27556, 29609, + 5234, 6028, + 8034, 10154, + 11242, 14789, + 18948, 20966, + 26585, 29127, + 5241, 6838, + 10526, 12819, + 14681, 17328, + 19928, 22336, + 26193, 28697, + 3412, 4251, + 5988, 7094, + 9907, 18243, + 21669, 23777, + 26969, 29087, + 2470, 3217, + 7797, 15296, + 17365, 19135, + 21979, 24256, + 27322, 29442, + 4939, 5804, + 8145, 11809, + 13873, 15598, + 17234, 19423, + 26476, 29645, + 5051, 6167, + 8223, 9655, + 12159, 17995, + 20464, 22832, + 26616, 28462, + 4987, 5907, + 9319, 11245, + 13132, 15024, + 17485, 22687, + 26011, 28273, + 5137, 6884, + 11025, 14950, + 17191, 19425, + 21807, 24393, + 26938, 29288, + 7057, 7884, + 9528, 10483, + 10960, 14811, + 19070, 21675, + 25645, 28019, + 6759, 7160, + 8546, 11779, + 12295, 13023, + 16627, 21099, + 24697, 28287, + 3863, 9762, + 11068, 11445, + 12049, 13960, + 18085, 21507, + 25224, 28997, + 397, 335, + 651, 1168, + 640, 765, + 465, 331, + 214, -194, + -578, -647, + -657, 750, + 564, 613, + 549, 630, + 304, -52, + 828, 922, + 443, 111, + 138, 124, + 169, 14, + 144, 83, + 132, 58, + -413, -752, + 869, 336, + 385, 69, + 56, 830, + -227, -266, + -368, -440, + -1195, 163, + 126, -228, + 802, 156, + 188, 120, + 376, 59, + -358, -558, + -1326, -254, + -202, -789, + 296, 92, + -70, -129, + -718, -1135, + 292, -29, + -631, 487, + -157, -153, + -279, 2, + -419, -342, + -34, -514, + -799, -1571, + -687, -609, + -546, -130, + -215, -252, + -446, -574, + -1337, 207, + -72, 32, + 103, -642, + 942, 733, + 187, 29, + -211, -814, + 143, 225, + 20, 24, + -268, -377, + 1623, 1133, + 667, 164, + 307, 366, + 187, 34, + 62, -313, + -832, -1482, + -1181, 483, + -42, -39, + -450, -1406, + -587, -52, + -760, 334, + 98, -60, + -500, -488, + -1058, 299, + 131, -250, + -251, -703, + 1037, 568, + -413, -265, + 1687, 573, + 345, 323, + 98, 61, + -102, 31, + 135, 149, + 617, 365, + -39, 34, + -611, 1201, + 1421, 736, + -414, -393, + -492, -343, + -316, -532, + 528, 172, + 90, 322, + -294, -319, + -541, 503, + 639, 401, + 1, -149, + -73, -167, + 150, 118, + 308, 218, + 121, 195, + -143, -261, + -1013, -802, + 387, 436, + 130, -427, + -448, -681, + 123, -87, + -251, -113, + 274, 310, + 445, 501, + 354, 272, + 141, -285, + 569, 656, + 37, -49, + 251, -386, + -263, 1122, + 604, 606, + 336, 95, + 34, 0, + 85, 180, + 207, -367, + -622, 1070, + -6, -79, + -160, -92, + -137, -276, + -323, -371, + -696, -1036, + 407, 102, + -86, -214, + -482, -647, + -28, -291, + -97, -180, + -250, -435, + -18, -76, + -332, 410, + 407, 168, + 539, 411, + 254, 111, + 58, -145, + 200, 30, + 187, 116, + 131, -367, + -475, 781, + -559, 561, + 195, -115, + 8, -168, + 30, 55, + -122, 131, + 82, -5, + -273, -50, + -632, 668, + 4, 32, + -26, -279, + 315, 165, + 197, 377, + 155, -41, + -138, -324, + -109, -617, + 360, 98, + -53, -319, + -114, -245, + -82, 507, + 468, 263, + -137, -389, + 652, 354, + -18, -227, + -462, -135, + 317, 53, + -16, 66, + -72, -126, + -356, -347, + -328, -72, + -337, 324, + 152, 349, + 169, -196, + 179, 254, + 260, 325, + -74, -80, + 75, -31, + 270, 275, + 87, 278, + -446, -301, + 309, 71, + -25, -242, + 516, 161, + -162, -83, + 329, 230, + -311, -259, + 177, -26, + -462, 89, + 257, 6, + -130, -93, + -456, -317, + -221, -206, + -417, -182, + -74, 234, + 48, 261, + 359, 231, + 258, 85, + -282, 252, + -147, -222, + 251, -207, + 443, 123, + -417, -36, + 273, -241, + 240, -112, + 44, -167, + 126, -124, + -77, 58, + -401, 333, + -118, 82, + 126, 151, + -433, 359, + -130, -102, + 131, -244, + 86, 85, + -462, 414, + -240, 16, + 145, 28, + -205, -481, + 373, 293, + -72, -174, + 62, 259, + -8, -18, + 362, 233, + 185, 43, + 278, 27, + 193, 570, + -248, 189, + 92, 31, + -275, -3, + 243, 176, + 438, 209, + 206, -51, + 79, 109, + 168, -185, + -308, -68, + -618, 385, + -310, -108, + -164, 165, + 61, -152, + -101, -412, + -268, -257, + -40, -20, + -28, -158, + -301, 271, + 380, -338, + -367, -132, + 64, 114, + -131, -225, + -156, -260, + -63, -116, + 155, -586, + -202, 254, + -287, 178, + 227, -106, + -294, 164, + 298, -100, + 185, 317, + 193, -45, + 28, 80, + -87, -433, + 22, -48, + 48, -237, + -229, -139, + 120, -364, + 268, -136, + 396, 125, + 130, -89, + -272, 118, + -256, -68, + -451, 488, + 143, -165, + -48, -190, + 106, 219, + 47, 435, + 245, 97, + 75, -418, + 121, -187, + 570, -200, + -351, 225, + -21, -217, + 234, -111, + 194, 14, + 242, 118, + 140, -397, + 355, 361, + -45, -195 +}; + +const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB0_10_Stage_info[ NLSF_MSVQ_CB0_10_STAGES ] = +{ + { 64, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 0 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 0 ] }, + { 16, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 64 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 64 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 80 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 80 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 88 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 88 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 96 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 96 ] }, + { 16, &SKP_Silk_NLSF_MSVQ_CB0_10_Q15[ 10 * 104 ], &SKP_Silk_NLSF_MSVQ_CB0_10_rates_Q5[ 104 ] } +}; + +const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_10 = +{ + NLSF_MSVQ_CB0_10_STAGES, + SKP_Silk_NLSF_CB0_10_Stage_info, + SKP_Silk_NLSF_MSVQ_CB0_10_ndelta_min_Q15, + SKP_Silk_NLSF_MSVQ_CB0_10_CDF, + SKP_Silk_NLSF_MSVQ_CB0_10_CDF_start_ptr, + SKP_Silk_NLSF_MSVQ_CB0_10_CDF_middle_idx +}; + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.h b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.h similarity index 97% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.h rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.h index 4aae583..4d8d314 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.h +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_10.h @@ -1,51 +1,51 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_TABLES_NLSF_CB0_10_H -#define SKP_SILK_TABLES_NLSF_CB0_10_H - -#include "SKP_Silk_define.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define NLSF_MSVQ_CB0_10_STAGES 6 -#define NLSF_MSVQ_CB0_10_VECTORS 120 - -/* NLSF codebook entropy coding tables */ -extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ NLSF_MSVQ_CB0_10_VECTORS + NLSF_MSVQ_CB0_10_STAGES ]; -extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_10_CDF_start_ptr[ NLSF_MSVQ_CB0_10_STAGES ]; -extern const SKP_int SKP_Silk_NLSF_MSVQ_CB0_10_CDF_middle_idx[ NLSF_MSVQ_CB0_10_STAGES ]; - -#ifdef __cplusplus -} -#endif - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_TABLES_NLSF_CB0_10_H +#define SKP_SILK_TABLES_NLSF_CB0_10_H + +#include "SKP_Silk_define.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define NLSF_MSVQ_CB0_10_STAGES 6 +#define NLSF_MSVQ_CB0_10_VECTORS 120 + +/* NLSF codebook entropy coding tables */ +extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_10_CDF[ NLSF_MSVQ_CB0_10_VECTORS + NLSF_MSVQ_CB0_10_STAGES ]; +extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_10_CDF_start_ptr[ NLSF_MSVQ_CB0_10_STAGES ]; +extern const SKP_int SKP_Silk_NLSF_MSVQ_CB0_10_CDF_middle_idx[ NLSF_MSVQ_CB0_10_STAGES ]; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.c b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.c similarity index 98% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.c index 895b346..ba6c309 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.c @@ -1,1319 +1,1319 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/**********************************************/ -/* This file has been automatically generated */ -/* */ -/* ROM usage: 246+3689 Words */ -/**********************************************/ - -#include "SKP_Silk_structs.h" -#include "SKP_Silk_tables_NLSF_CB0_16.h" - -const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ NLSF_MSVQ_CB0_16_VECTORS + NLSF_MSVQ_CB0_16_STAGES ] = -{ - 0, - 1449, - 2749, - 4022, - 5267, - 6434, - 7600, - 8647, - 9695, - 10742, - 11681, - 12601, - 13444, - 14251, - 15008, - 15764, - 16521, - 17261, - 18002, - 18710, - 19419, - 20128, - 20837, - 21531, - 22225, - 22919, - 23598, - 24277, - 24956, - 25620, - 26256, - 26865, - 27475, - 28071, - 28667, - 29263, - 29859, - 30443, - 31026, - 31597, - 32168, - 32727, - 33273, - 33808, - 34332, - 34855, - 35379, - 35902, - 36415, - 36927, - 37439, - 37941, - 38442, - 38932, - 39423, - 39914, - 40404, - 40884, - 41364, - 41844, - 42324, - 42805, - 43285, - 43754, - 44224, - 44694, - 45164, - 45623, - 46083, - 46543, - 46993, - 47443, - 47892, - 48333, - 48773, - 49213, - 49653, - 50084, - 50515, - 50946, - 51377, - 51798, - 52211, - 52614, - 53018, - 53422, - 53817, - 54212, - 54607, - 55002, - 55388, - 55775, - 56162, - 56548, - 56910, - 57273, - 57635, - 57997, - 58352, - 58698, - 59038, - 59370, - 59702, - 60014, - 60325, - 60630, - 60934, - 61239, - 61537, - 61822, - 62084, - 62346, - 62602, - 62837, - 63072, - 63302, - 63517, - 63732, - 63939, - 64145, - 64342, - 64528, - 64701, - 64867, - 65023, - 65151, - 65279, - 65407, - 65535, - 0, - 5099, - 9982, - 14760, - 19538, - 24213, - 28595, - 32976, - 36994, - 41012, - 44944, - 48791, - 52557, - 56009, - 59388, - 62694, - 65535, - 0, - 9955, - 19697, - 28825, - 36842, - 44686, - 52198, - 58939, - 65535, - 0, - 8949, - 17335, - 25720, - 33926, - 41957, - 49987, - 57845, - 65535, - 0, - 9724, - 18642, - 26998, - 35355, - 43532, - 51534, - 59365, - 65535, - 0, - 8750, - 17499, - 26249, - 34448, - 42471, - 50494, - 58178, - 65535, - 0, - 8730, - 17273, - 25816, - 34176, - 42536, - 50203, - 57869, - 65535, - 0, - 8769, - 17538, - 26307, - 34525, - 42742, - 50784, - 58319, - 65535, - 0, - 8736, - 17101, - 25466, - 33653, - 41839, - 50025, - 57864, - 65535, - 0, - 4368, - 8735, - 12918, - 17100, - 21283, - 25465, - 29558, - 33651, - 37744, - 41836, - 45929, - 50022, - 54027, - 57947, - 61782, - 65535 -}; - -const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_16_CDF_start_ptr[ NLSF_MSVQ_CB0_16_STAGES ] = -{ - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 0 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 129 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 146 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 155 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 164 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 173 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 182 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 191 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 200 ], - &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 209 ] -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB0_16_CDF_middle_idx[ NLSF_MSVQ_CB0_16_STAGES ] = -{ - 42, - 8, - 4, - 5, - 5, - 5, - 5, - 5, - 5, - 9 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ NLSF_MSVQ_CB0_16_VECTORS ] = -{ - 176, 181, - 182, 183, - 186, 186, - 191, 191, - 191, 196, - 197, 201, - 203, 206, - 206, 206, - 207, 207, - 209, 209, - 209, 209, - 210, 210, - 210, 211, - 211, 211, - 212, 214, - 216, 216, - 217, 217, - 217, 217, - 218, 218, - 219, 219, - 220, 221, - 222, 223, - 223, 223, - 223, 224, - 224, 224, - 225, 225, - 226, 226, - 226, 226, - 227, 227, - 227, 227, - 227, 227, - 228, 228, - 228, 228, - 229, 229, - 229, 230, - 230, 230, - 231, 231, - 231, 231, - 232, 232, - 232, 232, - 233, 234, - 235, 235, - 235, 236, - 236, 236, - 236, 237, - 237, 237, - 237, 240, - 240, 240, - 240, 241, - 242, 243, - 244, 244, - 247, 247, - 248, 248, - 248, 249, - 251, 255, - 255, 256, - 260, 260, - 261, 264, - 264, 266, - 266, 268, - 271, 274, - 276, 279, - 288, 288, - 288, 288, - 118, 120, - 121, 121, - 122, 125, - 125, 129, - 129, 130, - 131, 132, - 136, 137, - 138, 145, - 87, 88, - 91, 97, - 98, 100, - 105, 106, - 92, 95, - 95, 96, - 97, 97, - 98, 99, - 88, 92, - 95, 95, - 96, 97, - 98, 109, - 93, 93, - 93, 96, - 97, 97, - 99, 101, - 93, 94, - 94, 95, - 95, 99, - 99, 99, - 93, 93, - 93, 96, - 96, 97, - 100, 102, - 93, 95, - 95, 96, - 96, 96, - 98, 99, - 125, 125, - 127, 127, - 127, 127, - 128, 128, - 128, 128, - 128, 128, - 129, 130, - 131, 132 -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB0_16_ndelta_min_Q15[ 16 + 1 ] = -{ - 266, - 3, - 40, - 3, - 3, - 16, - 78, - 89, - 107, - 141, - 188, - 146, - 272, - 240, - 235, - 215, - 632 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * NLSF_MSVQ_CB0_16_VECTORS ] = -{ - 1170, 2278, 3658, 5374, - 7666, 9113, 11298, 13304, - 15371, 17549, 19587, 21487, - 23798, 26038, 28318, 30201, - 1628, 2334, 4115, 6036, - 7818, 9544, 11777, 14021, - 15787, 17408, 19466, 21261, - 22886, 24565, 26714, 28059, - 1724, 2670, 4056, 6532, - 8357, 10119, 12093, 14061, - 16491, 18795, 20417, 22402, - 24251, 26224, 28410, 29956, - 1493, 3427, 4789, 6399, - 8435, 10168, 12000, 14066, - 16229, 18210, 20040, 22098, - 24153, 26095, 28183, 30121, - 1119, 2089, 4295, 6245, - 8691, 10741, 12688, 15057, - 17028, 18792, 20717, 22514, - 24497, 26548, 28619, 30630, - 1363, 2417, 3927, 5556, - 7422, 9315, 11879, 13767, - 16143, 18520, 20458, 22578, - 24539, 26436, 28318, 30318, - 1122, 2503, 5216, 7148, - 9310, 11078, 13175, 14800, - 16864, 18700, 20436, 22488, - 24572, 26602, 28555, 30426, - 600, 1317, 2970, 5609, - 7694, 9784, 12169, 14087, - 16379, 18378, 20551, 22686, - 24739, 26697, 28646, 30355, - 941, 1882, 4274, 5540, - 8482, 9858, 11940, 14287, - 16091, 18501, 20326, 22612, - 24711, 26638, 28814, 30430, - 635, 1699, 4376, 5948, - 8097, 10115, 12274, 14178, - 16111, 17813, 19695, 21773, - 23927, 25866, 28022, 30134, - 1408, 2222, 3524, 5615, - 7345, 8849, 10989, 12772, - 15352, 17026, 18919, 21062, - 23329, 25215, 27209, 29023, - 701, 1307, 3548, 6301, - 7744, 9574, 11227, 12978, - 15170, 17565, 19775, 22097, - 24230, 26335, 28377, 30231, - 1752, 2364, 4879, 6569, - 7813, 9796, 11199, 14290, - 15795, 18000, 20396, 22417, - 24308, 26124, 28360, 30633, - 901, 1629, 3356, 4635, - 7256, 8767, 9971, 11558, - 15215, 17544, 19523, 21852, - 23900, 25978, 28133, 30184, - 981, 1669, 3323, 4693, - 6213, 8692, 10614, 12956, - 15211, 17711, 19856, 22122, - 24344, 26592, 28723, 30481, - 1607, 2577, 4220, 5512, - 8532, 10388, 11627, 13671, - 15752, 17199, 19840, 21859, - 23494, 25786, 28091, 30131, - 811, 1471, 3144, 5041, - 7430, 9389, 11174, 13255, - 15157, 16741, 19583, 22167, - 24115, 26142, 28383, 30395, - 1543, 2144, 3629, 6347, - 7333, 9339, 10710, 13596, - 15099, 17340, 20102, 21886, - 23732, 25637, 27818, 29917, - 492, 1185, 2940, 5488, - 7095, 8751, 11596, 13579, - 16045, 18015, 20178, 22127, - 24265, 26406, 28484, 30357, - 1547, 2282, 3693, 6341, - 7758, 9607, 11848, 13236, - 16564, 18069, 19759, 21404, - 24110, 26606, 28786, 30655, - 685, 1338, 3409, 5262, - 6950, 9222, 11414, 14523, - 16337, 17893, 19436, 21298, - 23293, 25181, 27973, 30520, - 887, 1581, 3057, 4318, - 7192, 8617, 10047, 13106, - 16265, 17893, 20233, 22350, - 24379, 26384, 28314, 30189, - 2285, 3745, 5662, 7576, - 9323, 11320, 13239, 15191, - 17175, 19225, 21108, 22972, - 24821, 26655, 28561, 30460, - 1496, 2108, 3448, 6898, - 8328, 9656, 11252, 12823, - 14979, 16482, 18180, 20085, - 22962, 25160, 27705, 29629, - 575, 1261, 3861, 6627, - 8294, 10809, 12705, 14768, - 17076, 19047, 20978, 23055, - 24972, 26703, 28720, 30345, - 1682, 2213, 3882, 6238, - 7208, 9646, 10877, 13431, - 14805, 16213, 17941, 20873, - 23550, 25765, 27756, 29461, - 888, 1616, 3924, 5195, - 7206, 8647, 9842, 11473, - 16067, 18221, 20343, 22774, - 24503, 26412, 28054, 29731, - 805, 1454, 2683, 4472, - 7936, 9360, 11398, 14345, - 16205, 17832, 19453, 21646, - 23899, 25928, 28387, 30463, - 1640, 2383, 3484, 5082, - 6032, 8606, 11640, 12966, - 15842, 17368, 19346, 21182, - 23638, 25889, 28368, 30299, - 1632, 2204, 4510, 7580, - 8718, 10512, 11962, 14096, - 15640, 17194, 19143, 22247, - 24563, 26561, 28604, 30509, - 2043, 2612, 3985, 6851, - 8038, 9514, 10979, 12789, - 15426, 16728, 18899, 20277, - 22902, 26209, 28711, 30618, - 2224, 2798, 4465, 5320, - 7108, 9436, 10986, 13222, - 14599, 18317, 20141, 21843, - 23601, 25700, 28184, 30582, - 835, 1541, 4083, 5769, - 7386, 9399, 10971, 12456, - 15021, 18642, 20843, 23100, - 25292, 26966, 28952, 30422, - 1795, 2343, 4809, 5896, - 7178, 8545, 10223, 13370, - 14606, 16469, 18273, 20736, - 23645, 26257, 28224, 30390, - 1734, 2254, 4031, 5188, - 6506, 7872, 9651, 13025, - 14419, 17305, 19495, 22190, - 24403, 26302, 28195, 30177, - 1841, 2349, 3968, 4764, - 6376, 9825, 11048, 13345, - 14682, 16252, 18183, 21363, - 23918, 26156, 28031, 29935, - 1432, 2047, 5631, 6927, - 8198, 9675, 11358, 13506, - 14802, 16419, 18339, 22019, - 24124, 26177, 28130, 30586, - 1730, 2320, 3744, 4808, - 6007, 9666, 10997, 13622, - 15234, 17495, 20088, 22002, - 23603, 25400, 27379, 29254, - 1267, 1915, 5483, 6812, - 8229, 9919, 11589, 13337, - 14747, 17965, 20552, 22167, - 24519, 26819, 28883, 30642, - 1526, 2229, 4240, 7388, - 8953, 10450, 11899, 13718, - 16861, 18323, 20379, 22672, - 24797, 26906, 28906, 30622, - 2175, 2791, 4104, 6875, - 8612, 9798, 12152, 13536, - 15623, 17682, 19213, 21060, - 24382, 26760, 28633, 30248, - 454, 1231, 4339, 5738, - 7550, 9006, 10320, 13525, - 16005, 17849, 20071, 21992, - 23949, 26043, 28245, 30175, - 2250, 2791, 4230, 5283, - 6762, 10607, 11879, 13821, - 15797, 17264, 20029, 22266, - 24588, 26437, 28244, 30419, - 1696, 2216, 4308, 8385, - 9766, 11030, 12556, 14099, - 16322, 17640, 19166, 20590, - 23967, 26858, 28798, 30562, - 2452, 3236, 4369, 6118, - 7156, 9003, 11509, 12796, - 15749, 17291, 19491, 22241, - 24530, 26474, 28273, 30073, - 1811, 2541, 3555, 5480, - 9123, 10527, 11894, 13659, - 15262, 16899, 19366, 21069, - 22694, 24314, 27256, 29983, - 1553, 2246, 4559, 5500, - 6754, 7874, 11739, 13571, - 15188, 17879, 20281, 22510, - 24614, 26649, 28786, 30755, - 1982, 2768, 3834, 5964, - 8732, 9908, 11797, 14813, - 16311, 17946, 21097, 22851, - 24456, 26304, 28166, 29755, - 1824, 2529, 3817, 5449, - 6854, 8714, 10381, 12286, - 14194, 15774, 19524, 21374, - 23695, 26069, 28096, 30212, - 2212, 2854, 3947, 5898, - 9930, 11556, 12854, 14788, - 16328, 17700, 20321, 22098, - 23672, 25291, 26976, 28586, - 2023, 2599, 4024, 4916, - 6613, 11149, 12457, 14626, - 16320, 17822, 19673, 21172, - 23115, 26051, 28825, 30758, - 1628, 2206, 3467, 4364, - 8679, 10173, 11864, 13679, - 14998, 16938, 19207, 21364, - 23850, 26115, 28124, 30273, - 2014, 2603, 4114, 7254, - 8516, 10043, 11822, 13503, - 16329, 17826, 19697, 21280, - 23151, 24661, 26807, 30161, - 2376, 2980, 4422, 5770, - 7016, 9723, 11125, 13516, - 15485, 16985, 19160, 20587, - 24401, 27180, 29046, 30647, - 2454, 3502, 4624, 6019, - 7632, 8849, 10792, 13964, - 15523, 17085, 19611, 21238, - 22856, 25108, 28106, 29890, - 1573, 2274, 3308, 5999, - 8977, 10104, 12457, 14258, - 15749, 18180, 19974, 21253, - 23045, 25058, 27741, 30315, - 1943, 2730, 4140, 6160, - 7491, 8986, 11309, 12775, - 14820, 16558, 17909, 19757, - 21512, 23605, 27274, 29527, - 2021, 2582, 4494, 5835, - 6993, 8245, 9827, 14733, - 16462, 17894, 19647, 21083, - 23764, 26667, 29072, 30990, - 1052, 1775, 3218, 4378, - 7666, 9403, 11248, 13327, - 14972, 17962, 20758, 22354, - 25071, 27209, 29001, 30609, - 2218, 2866, 4223, 5352, - 6581, 9980, 11587, 13121, - 15193, 16583, 18386, 20080, - 22013, 25317, 28127, 29880, - 2146, 2840, 4397, 5840, - 7449, 8721, 10512, 11936, - 13595, 17253, 19310, 20891, - 23417, 25627, 27749, 30231, - 1972, 2619, 3756, 6367, - 7641, 8814, 12286, 13768, - 15309, 18036, 19557, 20904, - 22582, 24876, 27800, 30440, - 2005, 2577, 4272, 7373, - 8558, 10223, 11770, 13402, - 16502, 18000, 19645, 21104, - 22990, 26806, 29505, 30942, - 1153, 1822, 3724, 5443, - 6990, 8702, 10289, 11899, - 13856, 15315, 17601, 21064, - 23692, 26083, 28586, 30639, - 1304, 1869, 3318, 7195, - 9613, 10733, 12393, 13728, - 15822, 17474, 18882, 20692, - 23114, 25540, 27684, 29244, - 2093, 2691, 4018, 6658, - 7947, 9147, 10497, 11881, - 15888, 17821, 19333, 21233, - 23371, 25234, 27553, 29998, - 575, 1331, 5304, 6910, - 8425, 10086, 11577, 13498, - 16444, 18527, 20565, 22847, - 24914, 26692, 28759, 30157, - 1435, 2024, 3283, 4156, - 7611, 10592, 12049, 13927, - 15459, 18413, 20495, 22270, - 24222, 26093, 28065, 30099, - 1632, 2168, 5540, 7478, - 8630, 10391, 11644, 14321, - 15741, 17357, 18756, 20434, - 22799, 26060, 28542, 30696, - 1407, 2245, 3405, 5639, - 9419, 10685, 12104, 13495, - 15535, 18357, 19996, 21689, - 24351, 26550, 28853, 30564, - 1675, 2226, 4005, 8223, - 9975, 11155, 12822, 14316, - 16504, 18137, 19574, 21050, - 22759, 24912, 28296, 30634, - 1080, 1614, 3622, 7565, - 8748, 10303, 11713, 13848, - 15633, 17434, 19761, 21825, - 23571, 25393, 27406, 29063, - 1693, 2229, 3456, 4354, - 5670, 10890, 12563, 14167, - 15879, 17377, 19817, 21971, - 24094, 26131, 28298, 30099, - 2042, 2959, 4195, 5740, - 7106, 8267, 11126, 14973, - 16914, 18295, 20532, 21982, - 23711, 25769, 27609, 29351, - 984, 1612, 3808, 5265, - 6885, 8411, 9547, 10889, - 12522, 16520, 19549, 21639, - 23746, 26058, 28310, 30374, - 2036, 2538, 4166, 7761, - 9146, 10412, 12144, 13609, - 15588, 17169, 18559, 20113, - 21820, 24313, 28029, 30612, - 1871, 2355, 4061, 5143, - 7464, 10129, 11941, 15001, - 16680, 18354, 19957, 22279, - 24861, 26872, 28988, 30615, - 2566, 3161, 4643, 6227, - 7406, 9970, 11618, 13416, - 15889, 17364, 19121, 20817, - 22592, 24720, 28733, 31082, - 1700, 2327, 4828, 5939, - 7567, 9154, 11087, 12771, - 14209, 16121, 20222, 22671, - 24648, 26656, 28696, 30745, - 3169, 3873, 5046, 6868, - 8184, 9480, 12335, 14068, - 15774, 17971, 20231, 21711, - 23520, 25245, 27026, 28730, - 1564, 2391, 4229, 6730, - 8905, 10459, 13026, 15033, - 17265, 19809, 21849, 23741, - 25490, 27312, 29061, 30527, - 2864, 3559, 4719, 6441, - 9592, 11055, 12763, 14784, - 16428, 18164, 20486, 22262, - 24183, 26263, 28383, 30224, - 2673, 3449, 4581, 5983, - 6863, 8311, 12464, 13911, - 15738, 17791, 19416, 21182, - 24025, 26561, 28723, 30440, - 2419, 3049, 4274, 6384, - 8564, 9661, 11288, 12676, - 14447, 17578, 19816, 21231, - 23099, 25270, 26899, 28926, - 1278, 2001, 3000, 5353, - 9995, 11777, 13018, 14570, - 16050, 17762, 19982, 21617, - 23371, 25083, 27656, 30172, - 932, 1624, 2798, 4570, - 8592, 9988, 11552, 13050, - 16921, 18677, 20415, 22810, - 24817, 26819, 28804, 30385, - 2324, 2973, 4156, 5702, - 6919, 8806, 10259, 12503, - 15015, 16567, 19418, 21375, - 22943, 24550, 27024, 29849, - 1564, 2373, 3455, 4907, - 5975, 7436, 11786, 14505, - 16107, 18148, 20019, 21653, - 23740, 25814, 28578, 30372, - 3025, 3729, 4866, 6520, - 9487, 10943, 12358, 14258, - 16174, 17501, 19476, 21408, - 23227, 24906, 27347, 29407, - 1270, 1965, 6802, 7995, - 9204, 10828, 12507, 14230, - 15759, 17860, 20369, 22502, - 24633, 26514, 28535, 30525, - 2210, 2749, 4266, 7487, - 9878, 11018, 12823, 14431, - 16247, 18626, 20450, 22054, - 23739, 25291, 27074, 29169, - 1275, 1926, 4330, 6573, - 8441, 10920, 13260, 15008, - 16927, 18573, 20644, 22217, - 23983, 25474, 27372, 28645, - 3015, 3670, 5086, 6372, - 7888, 9309, 10966, 12642, - 14495, 16172, 18080, 19972, - 22454, 24899, 27362, 29975, - 2882, 3733, 5113, 6482, - 8125, 9685, 11598, 13288, - 15405, 17192, 20178, 22426, - 24801, 27014, 29212, 30811, - 2300, 2968, 4101, 5442, - 6327, 7910, 12455, 13862, - 15747, 17505, 19053, 20679, - 22615, 24658, 27499, 30065, - 2257, 2940, 4430, 5991, - 7042, 8364, 9414, 11224, - 15723, 17420, 19253, 21469, - 23915, 26053, 28430, 30384, - 1227, 2045, 3818, 5011, - 6990, 9231, 11024, 13011, - 17341, 19017, 20583, 22799, - 25195, 26876, 29351, 30805, - 1354, 1924, 3789, 8077, - 10453, 11639, 13352, 14817, - 16743, 18189, 20095, 22014, - 24593, 26677, 28647, 30256, - 3142, 4049, 6197, 7417, - 8753, 10156, 11533, 13181, - 15947, 17655, 19606, 21402, - 23487, 25659, 28123, 30304, - 1317, 2263, 4725, 7611, - 9667, 11634, 14143, 16258, - 18724, 20698, 22379, 24007, - 25775, 27251, 28930, 30593, - 1570, 2323, 3818, 6215, - 9893, 11556, 13070, 14631, - 16152, 18290, 21386, 23346, - 25114, 26923, 28712, 30168, - 2297, 3905, 6287, 8558, - 10668, 12766, 15019, 17102, - 19036, 20677, 22341, 23871, - 25478, 27085, 28851, 30520, - 1915, 2507, 4033, 5749, - 7059, 8871, 10659, 12198, - 13937, 15383, 16869, 18707, - 23175, 25818, 28514, 30501, - 2404, 2918, 5190, 6252, - 7426, 9887, 12387, 14795, - 16754, 18368, 20338, 22003, - 24236, 26456, 28490, 30397, - 1621, 2227, 3479, 5085, - 9425, 12892, 14246, 15652, - 17205, 18674, 20446, 22209, - 23778, 25867, 27931, 30093, - 1869, 2390, 4105, 7021, - 11221, 12775, 14059, 15590, - 17024, 18608, 20595, 22075, - 23649, 25154, 26914, 28671, - 2551, 3252, 4688, 6562, - 7869, 9125, 10475, 11800, - 15402, 18780, 20992, 22555, - 24289, 25968, 27465, 29232, - 2705, 3493, 4735, 6360, - 7905, 9352, 11538, 13430, - 15239, 16919, 18619, 20094, - 21800, 23342, 25200, 29257, - 2166, 2791, 4011, 5081, - 5896, 9038, 13407, 14703, - 16543, 18189, 19896, 21857, - 24872, 26971, 28955, 30514, - 1865, 3021, 4696, 6534, - 8343, 9914, 12789, 14103, - 16533, 17729, 21340, 22439, - 24873, 26330, 28428, 30154, - 3369, 4345, 6573, 8763, - 10309, 11713, 13367, 14784, - 16483, 18145, 19839, 21247, - 23292, 25477, 27555, 29447, - 1265, 2184, 5443, 7893, - 10591, 13139, 15105, 16639, - 18402, 19826, 21419, 22995, - 24719, 26437, 28363, 30125, - 1584, 2004, 3535, 4450, - 8662, 10764, 12832, 14978, - 16972, 18794, 20932, 22547, - 24636, 26521, 28701, 30567, - 3419, 4528, 6602, 7890, - 9508, 10875, 12771, 14357, - 16051, 18330, 20630, 22490, - 25070, 26936, 28946, 30542, - 1726, 2252, 4597, 6950, - 8379, 9823, 11363, 12794, - 14306, 15476, 16798, 18018, - 21671, 25550, 28148, 30367, - 3385, 3870, 5307, 6388, - 7141, 8684, 12695, 14939, - 16480, 18277, 20537, 22048, - 23947, 25965, 28214, 29956, - 2771, 3306, 4450, 5560, - 6453, 9493, 13548, 14754, - 16743, 18447, 20028, 21736, - 23746, 25353, 27141, 29066, - 3028, 3900, 6617, 7893, - 9211, 10480, 12047, 13583, - 15182, 16662, 18502, 20092, - 22190, 24358, 26302, 28957, - 2000, 2550, 4067, 6837, - 9628, 11002, 12594, 14098, - 15589, 17195, 18679, 20099, - 21530, 23085, 24641, 29022, - 2844, 3302, 5103, 6107, - 6911, 8598, 12416, 14054, - 16026, 18567, 20672, 22270, - 23952, 25771, 27658, 30026, - 4043, 5150, 7268, 9056, - 10916, 12638, 14543, 16184, - 17948, 19691, 21357, 22981, - 24825, 26591, 28479, 30233, - 2109, 2625, 4320, 5525, - 7454, 10220, 12980, 14698, - 17627, 19263, 20485, 22381, - 24279, 25777, 27847, 30458, - 1550, 2667, 6473, 9496, - 10985, 12352, 13795, 15233, - 17099, 18642, 20461, 22116, - 24197, 26291, 28403, 30132, - 2411, 3084, 4145, 5394, - 6367, 8154, 13125, 16049, - 17561, 19125, 21258, 22762, - 24459, 26317, 28255, 29702, - 4159, 4516, 5956, 7635, - 8254, 8980, 11208, 14133, - 16210, 17875, 20196, 21864, - 23840, 25747, 28058, 30012, - 2026, 2431, 2845, 3618, - 7950, 9802, 12721, 14460, - 16576, 18984, 21376, 23319, - 24961, 26718, 28971, 30640, - 3429, 3833, 4472, 4912, - 7723, 10386, 12981, 15322, - 16699, 18807, 20778, 22551, - 24627, 26494, 28334, 30482, - 4740, 5169, 5796, 6485, - 6998, 8830, 11777, 14414, - 16831, 18413, 20789, 22369, - 24236, 25835, 27807, 30021, - 150, 168, -17, -107, - -142, -229, -320, -406, - -503, -620, -867, -935, - -902, -680, -398, -114, - -398, -355, 49, 255, - 114, 260, 399, 264, - 317, 431, 514, 531, - 435, 356, 238, 106, - -43, -36, -169, -224, - -391, -633, -776, -970, - -844, -455, -181, -12, - 85, 85, 164, 195, - 122, 85, -158, -640, - -903, 9, 7, -124, - 149, 32, 220, 369, - 242, 115, 79, 84, - -146, -216, -70, 1024, - 751, 574, 440, 377, - 352, 203, 30, 16, - -3, 81, 161, 100, - -148, -176, 933, 750, - 404, 171, -2, -146, - -411, -442, -541, -552, - -442, -269, -240, -52, - 603, 635, 405, 178, - 215, 19, -153, -167, - -290, -219, 151, 271, - 151, 119, 303, 266, - 100, 69, -293, -657, - 939, 659, 442, 351, - 132, 98, -16, -1, - -135, -200, -223, -89, - 167, 154, 172, 237, - -45, -183, -228, -486, - 263, 608, 158, -125, - -390, -227, -118, 43, - -457, -392, -769, -840, - 20, -117, -194, -189, - -173, -173, -33, 32, - 174, 144, 115, 167, - 57, 44, 14, 147, - 96, -54, -142, -129, - -254, -331, 304, 310, - -52, -419, -846, -1060, - -88, -123, -202, -343, - -554, -961, -951, 327, - 159, 81, 255, 227, - 120, 203, 256, 192, - 164, 224, 290, 195, - 216, 209, 128, 832, - 1028, 889, 698, 504, - 408, 355, 218, 32, - -115, -84, -276, -100, - -312, -484, 899, 682, - 465, 456, 241, -12, - -275, -425, -461, -367, - -33, -28, -102, -194, - -527, 863, 906, 463, - 245, 13, -212, -305, - -105, 163, 279, 176, - 93, 67, 115, 192, - 61, -50, -132, -175, - -224, -271, -629, -252, - 1158, 972, 638, 280, - 300, 326, 143, -152, - -214, -287, 53, -42, - -236, -352, -423, -248, - -129, -163, -178, -119, - 85, 57, 514, 382, - 374, 402, 424, 423, - 271, 197, 97, 40, - 39, -97, -191, -164, - -230, -256, -410, 396, - 327, 127, 10, -119, - -167, -291, -274, -141, - -99, -226, -218, -139, - -224, -209, -268, -442, - -413, 222, 58, 521, - 344, 258, 76, -42, - -142, -165, -123, -92, - 47, 8, -3, -191, - -11, -164, -167, -351, - -740, 311, 538, 291, - 184, 29, -105, 9, - -30, -54, -17, -77, - -271, -412, -622, -648, - 476, 186, -66, -197, - -73, -94, -15, 47, - 28, 112, -58, -33, - 65, 19, 84, 86, - 276, 114, 472, 786, - 799, 625, 415, 178, - -35, -26, 5, 9, - 83, 39, 37, 39, - -184, -374, -265, -362, - -501, 337, 716, 478, - -60, -125, -163, 362, - 17, -122, -233, 279, - 138, 157, 318, 193, - 189, 209, 266, 252, - -46, -56, -277, -429, - 464, 386, 142, 44, - -43, 66, 264, 182, - 47, 14, -26, -79, - 49, 15, -128, -203, - -400, -478, 325, 27, - 234, 411, 205, 129, - 12, 58, 123, 57, - 171, 137, 96, 128, - -32, 134, -12, 57, - 119, 26, -22, -165, - -500, -701, -528, -116, - 64, -8, 97, -9, - -162, -66, -156, -194, - -303, -546, -341, 546, - 358, 95, 45, 76, - 270, 403, 205, 100, - 123, 50, -53, -144, - -110, -13, 32, -228, - -130, 353, 296, 56, - -372, -253, 365, 73, - 10, -34, -139, -191, - -96, 5, 44, -85, - -179, -129, -192, -246, - -85, -110, -155, -44, - -27, 145, 138, 79, - 32, -148, -577, -634, - 191, 94, -9, -35, - -77, -84, -56, -171, - -298, -271, -243, -156, - -328, -235, -76, -128, - -121, 129, 13, -22, - 32, 45, -248, -65, - 193, -81, 299, 57, - -147, 192, -165, -354, - -334, -106, -156, -40, - -3, -68, 124, -257, - 78, 124, 170, 412, - 227, 105, -104, 12, - 154, 250, 274, 258, - 4, -27, 235, 152, - 51, 338, 300, 7, - -314, -411, 215, 170, - -9, -93, -77, 76, - 67, 54, 200, 315, - 163, 72, -91, -402, - 158, 187, -156, -91, - 290, 267, 167, 91, - 140, 171, 112, 9, - -42, -177, -440, 385, - 80, 15, 172, 129, - 41, -129, -372, -24, - -75, -30, -170, 10, - -118, 57, 78, -101, - 232, 161, 123, 256, - 277, 101, -192, -629, - -100, -60, -232, 66, - 13, -13, -80, -239, - 239, 37, 32, 89, - -319, -579, 450, 360, - 3, -29, -299, -89, - -54, -110, -246, -164, - 6, -188, 338, 176, - -92, 197, 137, 134, - 12, -2, 56, -183, - 114, -36, -131, -204, - 75, -25, -174, 191, - -15, -290, -429, -267, - 79, 37, 106, 23, - -384, 425, 70, -14, - 212, 105, 15, -2, - -42, -37, -123, 108, - 28, -48, 193, 197, - 173, -33, 37, 73, - -57, 256, 137, -58, - -430, -228, 217, -51, - -10, -58, -6, 22, - 104, 61, -119, 169, - 144, 16, -46, -394, - 60, 454, -80, -298, - -65, 25, 0, -24, - -65, -417, 465, 276, - -3, -194, -13, 130, - 19, -6, -21, -24, - -180, -53, -85, 20, - 118, 147, 113, -75, - -289, 226, -122, 227, - 270, 125, 109, 197, - 125, 138, 44, 60, - 25, -55, -167, -32, - -139, -193, -173, -316, - 287, -208, 253, 239, - 27, -80, -188, -28, - -182, -235, 156, -117, - 128, -48, -58, -226, - 172, 181, 167, 19, - 62, 10, 2, 181, - 151, 108, -16, -11, - -78, -331, 411, 133, - 17, 104, 64, -184, - 24, -30, -3, -283, - 121, 204, -8, -199, - -21, -80, -169, -157, - -191, -136, 81, 155, - 14, -131, 244, 74, - -57, -47, -280, 347, - 111, -77, -128, -142, - -194, -125, -6, -68, - 91, 1, 23, 14, - -154, -34, 23, -38, - -343, 503, 146, -38, - -46, -41, 58, 31, - 63, -48, -117, 45, - 28, 1, -89, -5, - -44, -29, -448, 487, - 204, 81, 46, -106, - -302, 380, 120, -38, - -12, -39, 70, -3, - 25, -65, 30, -11, - 34, -15, 22, -115, - 0, -79, -83, 45, - 114, 43, 150, 36, - 233, 149, 195, 5, - 25, -52, -475, 274, - 28, -39, -8, -66, - -255, 258, 56, 143, - -45, -190, 165, -60, - 20, 2, 125, -129, - 51, -8, -335, 288, - 38, 59, 25, -42, - 23, -118, -112, 11, - -55, -133, -109, 24, - -105, 78, -64, -245, - 202, -65, -127, 162, - 40, -94, 89, -85, - -119, -103, 97, 9, - -70, -28, 194, 86, - -112, -92, -114, 74, - -49, 46, -84, -178, - 113, 52, -205, 333, - 88, 222, 56, -55, - 13, 86, 4, -77, - 224, 114, -105, 112, - 125, -29, -18, -144, - 22, -58, -99, 28, - 114, -66, -32, -169, - -314, 285, 72, -74, - 179, 28, -79, -182, - 13, -55, 147, 13, - 12, -54, 31, -84, - -17, -75, -228, 83, - -375, 436, 110, -63, - -27, -136, 169, -56, - -8, -171, 184, -42, - 148, 68, 204, 235, - 110, -229, 91, 171, - -43, -3, -26, -99, - -111, 71, -170, 202, - -67, 181, -37, 109, - -120, 3, -55, -260, - -16, 152, 91, 142, - 42, 44, 134, 47, - 17, -35, 22, 79, - -169, 41, 46, 277, - -93, -49, -126, 37, - -103, -34, -22, -90, - -134, -205, 92, -9, - 1, -195, -239, 45, - 54, 18, -23, -1, - -80, -98, -20, -261, - 306, 72, 20, -89, - -217, 11, 6, -82, - 89, 13, -129, -89, - 83, -71, -55, 130, - -98, -146, -27, -57, - 53, 275, 17, 170, - -5, -54, 132, -64, - 72, 160, -125, -168, - 72, 40, 170, 78, - 248, 116, 20, 84, - 31, -34, 190, 38, - 13, -106, 225, 27, - -168, 24, -157, -122, - 165, 11, -161, -213, - -12, -51, -101, 42, - 101, 27, 55, 111, - 75, 71, -96, -1, - 65, -277, 393, -26, - -44, -68, -84, -66, - -95, 235, 179, -25, - -41, 27, -91, -128, - -222, 146, -72, -30, - -24, 55, -126, -68, - -58, -127, 13, -97, - -106, 174, -100, 155, - 101, -146, -21, 261, - 22, 38, -66, 65, - 4, 70, 64, 144, - 59, 213, 71, -337, - 303, -52, 51, -56, - 1, 10, -15, -5, - 34, 52, 228, 131, - 161, -127, -214, 238, - 123, 64, -147, -50, - -34, -127, 204, 162, - 85, 41, 5, -140, - 73, -150, 56, -96, - -66, -20, 2, -235, - 59, -22, -107, 150, - -16, -47, -4, 81, - -67, 167, 149, 149, - -157, 288, -156, -27, - -8, 18, 83, -24, - -41, -167, 158, -100, - 93, 53, 201, 15, - 42, 266, 278, -12, - -6, -37, 85, 6, - 20, -188, -271, 107, - -13, -80, 51, 202, - 173, -69, 78, -188, - 46, 4, 153, 12, - -138, 169, 5, -58, - -123, -108, -243, 150, - 10, -191, 246, -15, - 38, 25, -10, 14, - 61, 50, -206, -215, - -220, 90, 5, -149, - -219, 56, 142, 24, - -376, 77, -80, 75, - 6, 42, -101, 16, - 56, 14, -57, 3, - -17, 80, 57, -36, - 88, -59, -97, -19, - -148, 46, -219, 226, - 114, -4, -72, -15, - 37, -49, -28, 247, - 44, 123, 47, -122, - -38, 17, 4, -113, - -32, -224, 154, -134, - 196, 71, -267, -85, - 28, -70, 89, -120, - 99, -2, 64, 76, - -166, -48, 189, -35, - -92, -169, -123, 339, - 38, -25, 38, -35, - 225, -139, -50, -63, - 246, 60, -185, -109, - -49, -53, -167, 51, - 149, 60, -101, -33, - 25, -76, 120, 32, - -30, -83, 102, 91, - -186, -261, 131, -197 -}; - -const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB0_16_Stage_info[ NLSF_MSVQ_CB0_16_STAGES ] = -{ - { 128, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 0 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 0 ] }, - { 16, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 128 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 128 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 144 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 144 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 152 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 152 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 160 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 160 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 168 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 168 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 176 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 176 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 184 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 184 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 192 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 192 ] }, - { 16, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 200 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 200 ] } -}; - -const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_16 = -{ - NLSF_MSVQ_CB0_16_STAGES, - SKP_Silk_NLSF_CB0_16_Stage_info, - SKP_Silk_NLSF_MSVQ_CB0_16_ndelta_min_Q15, - SKP_Silk_NLSF_MSVQ_CB0_16_CDF, - SKP_Silk_NLSF_MSVQ_CB0_16_CDF_start_ptr, - SKP_Silk_NLSF_MSVQ_CB0_16_CDF_middle_idx -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/**********************************************/ +/* This file has been automatically generated */ +/* */ +/* ROM usage: 246+3689 Words */ +/**********************************************/ + +#include "SKP_Silk_structs.h" +#include "SKP_Silk_tables_NLSF_CB0_16.h" + +const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ NLSF_MSVQ_CB0_16_VECTORS + NLSF_MSVQ_CB0_16_STAGES ] = +{ + 0, + 1449, + 2749, + 4022, + 5267, + 6434, + 7600, + 8647, + 9695, + 10742, + 11681, + 12601, + 13444, + 14251, + 15008, + 15764, + 16521, + 17261, + 18002, + 18710, + 19419, + 20128, + 20837, + 21531, + 22225, + 22919, + 23598, + 24277, + 24956, + 25620, + 26256, + 26865, + 27475, + 28071, + 28667, + 29263, + 29859, + 30443, + 31026, + 31597, + 32168, + 32727, + 33273, + 33808, + 34332, + 34855, + 35379, + 35902, + 36415, + 36927, + 37439, + 37941, + 38442, + 38932, + 39423, + 39914, + 40404, + 40884, + 41364, + 41844, + 42324, + 42805, + 43285, + 43754, + 44224, + 44694, + 45164, + 45623, + 46083, + 46543, + 46993, + 47443, + 47892, + 48333, + 48773, + 49213, + 49653, + 50084, + 50515, + 50946, + 51377, + 51798, + 52211, + 52614, + 53018, + 53422, + 53817, + 54212, + 54607, + 55002, + 55388, + 55775, + 56162, + 56548, + 56910, + 57273, + 57635, + 57997, + 58352, + 58698, + 59038, + 59370, + 59702, + 60014, + 60325, + 60630, + 60934, + 61239, + 61537, + 61822, + 62084, + 62346, + 62602, + 62837, + 63072, + 63302, + 63517, + 63732, + 63939, + 64145, + 64342, + 64528, + 64701, + 64867, + 65023, + 65151, + 65279, + 65407, + 65535, + 0, + 5099, + 9982, + 14760, + 19538, + 24213, + 28595, + 32976, + 36994, + 41012, + 44944, + 48791, + 52557, + 56009, + 59388, + 62694, + 65535, + 0, + 9955, + 19697, + 28825, + 36842, + 44686, + 52198, + 58939, + 65535, + 0, + 8949, + 17335, + 25720, + 33926, + 41957, + 49987, + 57845, + 65535, + 0, + 9724, + 18642, + 26998, + 35355, + 43532, + 51534, + 59365, + 65535, + 0, + 8750, + 17499, + 26249, + 34448, + 42471, + 50494, + 58178, + 65535, + 0, + 8730, + 17273, + 25816, + 34176, + 42536, + 50203, + 57869, + 65535, + 0, + 8769, + 17538, + 26307, + 34525, + 42742, + 50784, + 58319, + 65535, + 0, + 8736, + 17101, + 25466, + 33653, + 41839, + 50025, + 57864, + 65535, + 0, + 4368, + 8735, + 12918, + 17100, + 21283, + 25465, + 29558, + 33651, + 37744, + 41836, + 45929, + 50022, + 54027, + 57947, + 61782, + 65535 +}; + +const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_16_CDF_start_ptr[ NLSF_MSVQ_CB0_16_STAGES ] = +{ + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 0 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 129 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 146 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 155 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 164 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 173 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 182 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 191 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 200 ], + &SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ 209 ] +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB0_16_CDF_middle_idx[ NLSF_MSVQ_CB0_16_STAGES ] = +{ + 42, + 8, + 4, + 5, + 5, + 5, + 5, + 5, + 5, + 9 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ NLSF_MSVQ_CB0_16_VECTORS ] = +{ + 176, 181, + 182, 183, + 186, 186, + 191, 191, + 191, 196, + 197, 201, + 203, 206, + 206, 206, + 207, 207, + 209, 209, + 209, 209, + 210, 210, + 210, 211, + 211, 211, + 212, 214, + 216, 216, + 217, 217, + 217, 217, + 218, 218, + 219, 219, + 220, 221, + 222, 223, + 223, 223, + 223, 224, + 224, 224, + 225, 225, + 226, 226, + 226, 226, + 227, 227, + 227, 227, + 227, 227, + 228, 228, + 228, 228, + 229, 229, + 229, 230, + 230, 230, + 231, 231, + 231, 231, + 232, 232, + 232, 232, + 233, 234, + 235, 235, + 235, 236, + 236, 236, + 236, 237, + 237, 237, + 237, 240, + 240, 240, + 240, 241, + 242, 243, + 244, 244, + 247, 247, + 248, 248, + 248, 249, + 251, 255, + 255, 256, + 260, 260, + 261, 264, + 264, 266, + 266, 268, + 271, 274, + 276, 279, + 288, 288, + 288, 288, + 118, 120, + 121, 121, + 122, 125, + 125, 129, + 129, 130, + 131, 132, + 136, 137, + 138, 145, + 87, 88, + 91, 97, + 98, 100, + 105, 106, + 92, 95, + 95, 96, + 97, 97, + 98, 99, + 88, 92, + 95, 95, + 96, 97, + 98, 109, + 93, 93, + 93, 96, + 97, 97, + 99, 101, + 93, 94, + 94, 95, + 95, 99, + 99, 99, + 93, 93, + 93, 96, + 96, 97, + 100, 102, + 93, 95, + 95, 96, + 96, 96, + 98, 99, + 125, 125, + 127, 127, + 127, 127, + 128, 128, + 128, 128, + 128, 128, + 129, 130, + 131, 132 +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB0_16_ndelta_min_Q15[ 16 + 1 ] = +{ + 266, + 3, + 40, + 3, + 3, + 16, + 78, + 89, + 107, + 141, + 188, + 146, + 272, + 240, + 235, + 215, + 632 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * NLSF_MSVQ_CB0_16_VECTORS ] = +{ + 1170, 2278, 3658, 5374, + 7666, 9113, 11298, 13304, + 15371, 17549, 19587, 21487, + 23798, 26038, 28318, 30201, + 1628, 2334, 4115, 6036, + 7818, 9544, 11777, 14021, + 15787, 17408, 19466, 21261, + 22886, 24565, 26714, 28059, + 1724, 2670, 4056, 6532, + 8357, 10119, 12093, 14061, + 16491, 18795, 20417, 22402, + 24251, 26224, 28410, 29956, + 1493, 3427, 4789, 6399, + 8435, 10168, 12000, 14066, + 16229, 18210, 20040, 22098, + 24153, 26095, 28183, 30121, + 1119, 2089, 4295, 6245, + 8691, 10741, 12688, 15057, + 17028, 18792, 20717, 22514, + 24497, 26548, 28619, 30630, + 1363, 2417, 3927, 5556, + 7422, 9315, 11879, 13767, + 16143, 18520, 20458, 22578, + 24539, 26436, 28318, 30318, + 1122, 2503, 5216, 7148, + 9310, 11078, 13175, 14800, + 16864, 18700, 20436, 22488, + 24572, 26602, 28555, 30426, + 600, 1317, 2970, 5609, + 7694, 9784, 12169, 14087, + 16379, 18378, 20551, 22686, + 24739, 26697, 28646, 30355, + 941, 1882, 4274, 5540, + 8482, 9858, 11940, 14287, + 16091, 18501, 20326, 22612, + 24711, 26638, 28814, 30430, + 635, 1699, 4376, 5948, + 8097, 10115, 12274, 14178, + 16111, 17813, 19695, 21773, + 23927, 25866, 28022, 30134, + 1408, 2222, 3524, 5615, + 7345, 8849, 10989, 12772, + 15352, 17026, 18919, 21062, + 23329, 25215, 27209, 29023, + 701, 1307, 3548, 6301, + 7744, 9574, 11227, 12978, + 15170, 17565, 19775, 22097, + 24230, 26335, 28377, 30231, + 1752, 2364, 4879, 6569, + 7813, 9796, 11199, 14290, + 15795, 18000, 20396, 22417, + 24308, 26124, 28360, 30633, + 901, 1629, 3356, 4635, + 7256, 8767, 9971, 11558, + 15215, 17544, 19523, 21852, + 23900, 25978, 28133, 30184, + 981, 1669, 3323, 4693, + 6213, 8692, 10614, 12956, + 15211, 17711, 19856, 22122, + 24344, 26592, 28723, 30481, + 1607, 2577, 4220, 5512, + 8532, 10388, 11627, 13671, + 15752, 17199, 19840, 21859, + 23494, 25786, 28091, 30131, + 811, 1471, 3144, 5041, + 7430, 9389, 11174, 13255, + 15157, 16741, 19583, 22167, + 24115, 26142, 28383, 30395, + 1543, 2144, 3629, 6347, + 7333, 9339, 10710, 13596, + 15099, 17340, 20102, 21886, + 23732, 25637, 27818, 29917, + 492, 1185, 2940, 5488, + 7095, 8751, 11596, 13579, + 16045, 18015, 20178, 22127, + 24265, 26406, 28484, 30357, + 1547, 2282, 3693, 6341, + 7758, 9607, 11848, 13236, + 16564, 18069, 19759, 21404, + 24110, 26606, 28786, 30655, + 685, 1338, 3409, 5262, + 6950, 9222, 11414, 14523, + 16337, 17893, 19436, 21298, + 23293, 25181, 27973, 30520, + 887, 1581, 3057, 4318, + 7192, 8617, 10047, 13106, + 16265, 17893, 20233, 22350, + 24379, 26384, 28314, 30189, + 2285, 3745, 5662, 7576, + 9323, 11320, 13239, 15191, + 17175, 19225, 21108, 22972, + 24821, 26655, 28561, 30460, + 1496, 2108, 3448, 6898, + 8328, 9656, 11252, 12823, + 14979, 16482, 18180, 20085, + 22962, 25160, 27705, 29629, + 575, 1261, 3861, 6627, + 8294, 10809, 12705, 14768, + 17076, 19047, 20978, 23055, + 24972, 26703, 28720, 30345, + 1682, 2213, 3882, 6238, + 7208, 9646, 10877, 13431, + 14805, 16213, 17941, 20873, + 23550, 25765, 27756, 29461, + 888, 1616, 3924, 5195, + 7206, 8647, 9842, 11473, + 16067, 18221, 20343, 22774, + 24503, 26412, 28054, 29731, + 805, 1454, 2683, 4472, + 7936, 9360, 11398, 14345, + 16205, 17832, 19453, 21646, + 23899, 25928, 28387, 30463, + 1640, 2383, 3484, 5082, + 6032, 8606, 11640, 12966, + 15842, 17368, 19346, 21182, + 23638, 25889, 28368, 30299, + 1632, 2204, 4510, 7580, + 8718, 10512, 11962, 14096, + 15640, 17194, 19143, 22247, + 24563, 26561, 28604, 30509, + 2043, 2612, 3985, 6851, + 8038, 9514, 10979, 12789, + 15426, 16728, 18899, 20277, + 22902, 26209, 28711, 30618, + 2224, 2798, 4465, 5320, + 7108, 9436, 10986, 13222, + 14599, 18317, 20141, 21843, + 23601, 25700, 28184, 30582, + 835, 1541, 4083, 5769, + 7386, 9399, 10971, 12456, + 15021, 18642, 20843, 23100, + 25292, 26966, 28952, 30422, + 1795, 2343, 4809, 5896, + 7178, 8545, 10223, 13370, + 14606, 16469, 18273, 20736, + 23645, 26257, 28224, 30390, + 1734, 2254, 4031, 5188, + 6506, 7872, 9651, 13025, + 14419, 17305, 19495, 22190, + 24403, 26302, 28195, 30177, + 1841, 2349, 3968, 4764, + 6376, 9825, 11048, 13345, + 14682, 16252, 18183, 21363, + 23918, 26156, 28031, 29935, + 1432, 2047, 5631, 6927, + 8198, 9675, 11358, 13506, + 14802, 16419, 18339, 22019, + 24124, 26177, 28130, 30586, + 1730, 2320, 3744, 4808, + 6007, 9666, 10997, 13622, + 15234, 17495, 20088, 22002, + 23603, 25400, 27379, 29254, + 1267, 1915, 5483, 6812, + 8229, 9919, 11589, 13337, + 14747, 17965, 20552, 22167, + 24519, 26819, 28883, 30642, + 1526, 2229, 4240, 7388, + 8953, 10450, 11899, 13718, + 16861, 18323, 20379, 22672, + 24797, 26906, 28906, 30622, + 2175, 2791, 4104, 6875, + 8612, 9798, 12152, 13536, + 15623, 17682, 19213, 21060, + 24382, 26760, 28633, 30248, + 454, 1231, 4339, 5738, + 7550, 9006, 10320, 13525, + 16005, 17849, 20071, 21992, + 23949, 26043, 28245, 30175, + 2250, 2791, 4230, 5283, + 6762, 10607, 11879, 13821, + 15797, 17264, 20029, 22266, + 24588, 26437, 28244, 30419, + 1696, 2216, 4308, 8385, + 9766, 11030, 12556, 14099, + 16322, 17640, 19166, 20590, + 23967, 26858, 28798, 30562, + 2452, 3236, 4369, 6118, + 7156, 9003, 11509, 12796, + 15749, 17291, 19491, 22241, + 24530, 26474, 28273, 30073, + 1811, 2541, 3555, 5480, + 9123, 10527, 11894, 13659, + 15262, 16899, 19366, 21069, + 22694, 24314, 27256, 29983, + 1553, 2246, 4559, 5500, + 6754, 7874, 11739, 13571, + 15188, 17879, 20281, 22510, + 24614, 26649, 28786, 30755, + 1982, 2768, 3834, 5964, + 8732, 9908, 11797, 14813, + 16311, 17946, 21097, 22851, + 24456, 26304, 28166, 29755, + 1824, 2529, 3817, 5449, + 6854, 8714, 10381, 12286, + 14194, 15774, 19524, 21374, + 23695, 26069, 28096, 30212, + 2212, 2854, 3947, 5898, + 9930, 11556, 12854, 14788, + 16328, 17700, 20321, 22098, + 23672, 25291, 26976, 28586, + 2023, 2599, 4024, 4916, + 6613, 11149, 12457, 14626, + 16320, 17822, 19673, 21172, + 23115, 26051, 28825, 30758, + 1628, 2206, 3467, 4364, + 8679, 10173, 11864, 13679, + 14998, 16938, 19207, 21364, + 23850, 26115, 28124, 30273, + 2014, 2603, 4114, 7254, + 8516, 10043, 11822, 13503, + 16329, 17826, 19697, 21280, + 23151, 24661, 26807, 30161, + 2376, 2980, 4422, 5770, + 7016, 9723, 11125, 13516, + 15485, 16985, 19160, 20587, + 24401, 27180, 29046, 30647, + 2454, 3502, 4624, 6019, + 7632, 8849, 10792, 13964, + 15523, 17085, 19611, 21238, + 22856, 25108, 28106, 29890, + 1573, 2274, 3308, 5999, + 8977, 10104, 12457, 14258, + 15749, 18180, 19974, 21253, + 23045, 25058, 27741, 30315, + 1943, 2730, 4140, 6160, + 7491, 8986, 11309, 12775, + 14820, 16558, 17909, 19757, + 21512, 23605, 27274, 29527, + 2021, 2582, 4494, 5835, + 6993, 8245, 9827, 14733, + 16462, 17894, 19647, 21083, + 23764, 26667, 29072, 30990, + 1052, 1775, 3218, 4378, + 7666, 9403, 11248, 13327, + 14972, 17962, 20758, 22354, + 25071, 27209, 29001, 30609, + 2218, 2866, 4223, 5352, + 6581, 9980, 11587, 13121, + 15193, 16583, 18386, 20080, + 22013, 25317, 28127, 29880, + 2146, 2840, 4397, 5840, + 7449, 8721, 10512, 11936, + 13595, 17253, 19310, 20891, + 23417, 25627, 27749, 30231, + 1972, 2619, 3756, 6367, + 7641, 8814, 12286, 13768, + 15309, 18036, 19557, 20904, + 22582, 24876, 27800, 30440, + 2005, 2577, 4272, 7373, + 8558, 10223, 11770, 13402, + 16502, 18000, 19645, 21104, + 22990, 26806, 29505, 30942, + 1153, 1822, 3724, 5443, + 6990, 8702, 10289, 11899, + 13856, 15315, 17601, 21064, + 23692, 26083, 28586, 30639, + 1304, 1869, 3318, 7195, + 9613, 10733, 12393, 13728, + 15822, 17474, 18882, 20692, + 23114, 25540, 27684, 29244, + 2093, 2691, 4018, 6658, + 7947, 9147, 10497, 11881, + 15888, 17821, 19333, 21233, + 23371, 25234, 27553, 29998, + 575, 1331, 5304, 6910, + 8425, 10086, 11577, 13498, + 16444, 18527, 20565, 22847, + 24914, 26692, 28759, 30157, + 1435, 2024, 3283, 4156, + 7611, 10592, 12049, 13927, + 15459, 18413, 20495, 22270, + 24222, 26093, 28065, 30099, + 1632, 2168, 5540, 7478, + 8630, 10391, 11644, 14321, + 15741, 17357, 18756, 20434, + 22799, 26060, 28542, 30696, + 1407, 2245, 3405, 5639, + 9419, 10685, 12104, 13495, + 15535, 18357, 19996, 21689, + 24351, 26550, 28853, 30564, + 1675, 2226, 4005, 8223, + 9975, 11155, 12822, 14316, + 16504, 18137, 19574, 21050, + 22759, 24912, 28296, 30634, + 1080, 1614, 3622, 7565, + 8748, 10303, 11713, 13848, + 15633, 17434, 19761, 21825, + 23571, 25393, 27406, 29063, + 1693, 2229, 3456, 4354, + 5670, 10890, 12563, 14167, + 15879, 17377, 19817, 21971, + 24094, 26131, 28298, 30099, + 2042, 2959, 4195, 5740, + 7106, 8267, 11126, 14973, + 16914, 18295, 20532, 21982, + 23711, 25769, 27609, 29351, + 984, 1612, 3808, 5265, + 6885, 8411, 9547, 10889, + 12522, 16520, 19549, 21639, + 23746, 26058, 28310, 30374, + 2036, 2538, 4166, 7761, + 9146, 10412, 12144, 13609, + 15588, 17169, 18559, 20113, + 21820, 24313, 28029, 30612, + 1871, 2355, 4061, 5143, + 7464, 10129, 11941, 15001, + 16680, 18354, 19957, 22279, + 24861, 26872, 28988, 30615, + 2566, 3161, 4643, 6227, + 7406, 9970, 11618, 13416, + 15889, 17364, 19121, 20817, + 22592, 24720, 28733, 31082, + 1700, 2327, 4828, 5939, + 7567, 9154, 11087, 12771, + 14209, 16121, 20222, 22671, + 24648, 26656, 28696, 30745, + 3169, 3873, 5046, 6868, + 8184, 9480, 12335, 14068, + 15774, 17971, 20231, 21711, + 23520, 25245, 27026, 28730, + 1564, 2391, 4229, 6730, + 8905, 10459, 13026, 15033, + 17265, 19809, 21849, 23741, + 25490, 27312, 29061, 30527, + 2864, 3559, 4719, 6441, + 9592, 11055, 12763, 14784, + 16428, 18164, 20486, 22262, + 24183, 26263, 28383, 30224, + 2673, 3449, 4581, 5983, + 6863, 8311, 12464, 13911, + 15738, 17791, 19416, 21182, + 24025, 26561, 28723, 30440, + 2419, 3049, 4274, 6384, + 8564, 9661, 11288, 12676, + 14447, 17578, 19816, 21231, + 23099, 25270, 26899, 28926, + 1278, 2001, 3000, 5353, + 9995, 11777, 13018, 14570, + 16050, 17762, 19982, 21617, + 23371, 25083, 27656, 30172, + 932, 1624, 2798, 4570, + 8592, 9988, 11552, 13050, + 16921, 18677, 20415, 22810, + 24817, 26819, 28804, 30385, + 2324, 2973, 4156, 5702, + 6919, 8806, 10259, 12503, + 15015, 16567, 19418, 21375, + 22943, 24550, 27024, 29849, + 1564, 2373, 3455, 4907, + 5975, 7436, 11786, 14505, + 16107, 18148, 20019, 21653, + 23740, 25814, 28578, 30372, + 3025, 3729, 4866, 6520, + 9487, 10943, 12358, 14258, + 16174, 17501, 19476, 21408, + 23227, 24906, 27347, 29407, + 1270, 1965, 6802, 7995, + 9204, 10828, 12507, 14230, + 15759, 17860, 20369, 22502, + 24633, 26514, 28535, 30525, + 2210, 2749, 4266, 7487, + 9878, 11018, 12823, 14431, + 16247, 18626, 20450, 22054, + 23739, 25291, 27074, 29169, + 1275, 1926, 4330, 6573, + 8441, 10920, 13260, 15008, + 16927, 18573, 20644, 22217, + 23983, 25474, 27372, 28645, + 3015, 3670, 5086, 6372, + 7888, 9309, 10966, 12642, + 14495, 16172, 18080, 19972, + 22454, 24899, 27362, 29975, + 2882, 3733, 5113, 6482, + 8125, 9685, 11598, 13288, + 15405, 17192, 20178, 22426, + 24801, 27014, 29212, 30811, + 2300, 2968, 4101, 5442, + 6327, 7910, 12455, 13862, + 15747, 17505, 19053, 20679, + 22615, 24658, 27499, 30065, + 2257, 2940, 4430, 5991, + 7042, 8364, 9414, 11224, + 15723, 17420, 19253, 21469, + 23915, 26053, 28430, 30384, + 1227, 2045, 3818, 5011, + 6990, 9231, 11024, 13011, + 17341, 19017, 20583, 22799, + 25195, 26876, 29351, 30805, + 1354, 1924, 3789, 8077, + 10453, 11639, 13352, 14817, + 16743, 18189, 20095, 22014, + 24593, 26677, 28647, 30256, + 3142, 4049, 6197, 7417, + 8753, 10156, 11533, 13181, + 15947, 17655, 19606, 21402, + 23487, 25659, 28123, 30304, + 1317, 2263, 4725, 7611, + 9667, 11634, 14143, 16258, + 18724, 20698, 22379, 24007, + 25775, 27251, 28930, 30593, + 1570, 2323, 3818, 6215, + 9893, 11556, 13070, 14631, + 16152, 18290, 21386, 23346, + 25114, 26923, 28712, 30168, + 2297, 3905, 6287, 8558, + 10668, 12766, 15019, 17102, + 19036, 20677, 22341, 23871, + 25478, 27085, 28851, 30520, + 1915, 2507, 4033, 5749, + 7059, 8871, 10659, 12198, + 13937, 15383, 16869, 18707, + 23175, 25818, 28514, 30501, + 2404, 2918, 5190, 6252, + 7426, 9887, 12387, 14795, + 16754, 18368, 20338, 22003, + 24236, 26456, 28490, 30397, + 1621, 2227, 3479, 5085, + 9425, 12892, 14246, 15652, + 17205, 18674, 20446, 22209, + 23778, 25867, 27931, 30093, + 1869, 2390, 4105, 7021, + 11221, 12775, 14059, 15590, + 17024, 18608, 20595, 22075, + 23649, 25154, 26914, 28671, + 2551, 3252, 4688, 6562, + 7869, 9125, 10475, 11800, + 15402, 18780, 20992, 22555, + 24289, 25968, 27465, 29232, + 2705, 3493, 4735, 6360, + 7905, 9352, 11538, 13430, + 15239, 16919, 18619, 20094, + 21800, 23342, 25200, 29257, + 2166, 2791, 4011, 5081, + 5896, 9038, 13407, 14703, + 16543, 18189, 19896, 21857, + 24872, 26971, 28955, 30514, + 1865, 3021, 4696, 6534, + 8343, 9914, 12789, 14103, + 16533, 17729, 21340, 22439, + 24873, 26330, 28428, 30154, + 3369, 4345, 6573, 8763, + 10309, 11713, 13367, 14784, + 16483, 18145, 19839, 21247, + 23292, 25477, 27555, 29447, + 1265, 2184, 5443, 7893, + 10591, 13139, 15105, 16639, + 18402, 19826, 21419, 22995, + 24719, 26437, 28363, 30125, + 1584, 2004, 3535, 4450, + 8662, 10764, 12832, 14978, + 16972, 18794, 20932, 22547, + 24636, 26521, 28701, 30567, + 3419, 4528, 6602, 7890, + 9508, 10875, 12771, 14357, + 16051, 18330, 20630, 22490, + 25070, 26936, 28946, 30542, + 1726, 2252, 4597, 6950, + 8379, 9823, 11363, 12794, + 14306, 15476, 16798, 18018, + 21671, 25550, 28148, 30367, + 3385, 3870, 5307, 6388, + 7141, 8684, 12695, 14939, + 16480, 18277, 20537, 22048, + 23947, 25965, 28214, 29956, + 2771, 3306, 4450, 5560, + 6453, 9493, 13548, 14754, + 16743, 18447, 20028, 21736, + 23746, 25353, 27141, 29066, + 3028, 3900, 6617, 7893, + 9211, 10480, 12047, 13583, + 15182, 16662, 18502, 20092, + 22190, 24358, 26302, 28957, + 2000, 2550, 4067, 6837, + 9628, 11002, 12594, 14098, + 15589, 17195, 18679, 20099, + 21530, 23085, 24641, 29022, + 2844, 3302, 5103, 6107, + 6911, 8598, 12416, 14054, + 16026, 18567, 20672, 22270, + 23952, 25771, 27658, 30026, + 4043, 5150, 7268, 9056, + 10916, 12638, 14543, 16184, + 17948, 19691, 21357, 22981, + 24825, 26591, 28479, 30233, + 2109, 2625, 4320, 5525, + 7454, 10220, 12980, 14698, + 17627, 19263, 20485, 22381, + 24279, 25777, 27847, 30458, + 1550, 2667, 6473, 9496, + 10985, 12352, 13795, 15233, + 17099, 18642, 20461, 22116, + 24197, 26291, 28403, 30132, + 2411, 3084, 4145, 5394, + 6367, 8154, 13125, 16049, + 17561, 19125, 21258, 22762, + 24459, 26317, 28255, 29702, + 4159, 4516, 5956, 7635, + 8254, 8980, 11208, 14133, + 16210, 17875, 20196, 21864, + 23840, 25747, 28058, 30012, + 2026, 2431, 2845, 3618, + 7950, 9802, 12721, 14460, + 16576, 18984, 21376, 23319, + 24961, 26718, 28971, 30640, + 3429, 3833, 4472, 4912, + 7723, 10386, 12981, 15322, + 16699, 18807, 20778, 22551, + 24627, 26494, 28334, 30482, + 4740, 5169, 5796, 6485, + 6998, 8830, 11777, 14414, + 16831, 18413, 20789, 22369, + 24236, 25835, 27807, 30021, + 150, 168, -17, -107, + -142, -229, -320, -406, + -503, -620, -867, -935, + -902, -680, -398, -114, + -398, -355, 49, 255, + 114, 260, 399, 264, + 317, 431, 514, 531, + 435, 356, 238, 106, + -43, -36, -169, -224, + -391, -633, -776, -970, + -844, -455, -181, -12, + 85, 85, 164, 195, + 122, 85, -158, -640, + -903, 9, 7, -124, + 149, 32, 220, 369, + 242, 115, 79, 84, + -146, -216, -70, 1024, + 751, 574, 440, 377, + 352, 203, 30, 16, + -3, 81, 161, 100, + -148, -176, 933, 750, + 404, 171, -2, -146, + -411, -442, -541, -552, + -442, -269, -240, -52, + 603, 635, 405, 178, + 215, 19, -153, -167, + -290, -219, 151, 271, + 151, 119, 303, 266, + 100, 69, -293, -657, + 939, 659, 442, 351, + 132, 98, -16, -1, + -135, -200, -223, -89, + 167, 154, 172, 237, + -45, -183, -228, -486, + 263, 608, 158, -125, + -390, -227, -118, 43, + -457, -392, -769, -840, + 20, -117, -194, -189, + -173, -173, -33, 32, + 174, 144, 115, 167, + 57, 44, 14, 147, + 96, -54, -142, -129, + -254, -331, 304, 310, + -52, -419, -846, -1060, + -88, -123, -202, -343, + -554, -961, -951, 327, + 159, 81, 255, 227, + 120, 203, 256, 192, + 164, 224, 290, 195, + 216, 209, 128, 832, + 1028, 889, 698, 504, + 408, 355, 218, 32, + -115, -84, -276, -100, + -312, -484, 899, 682, + 465, 456, 241, -12, + -275, -425, -461, -367, + -33, -28, -102, -194, + -527, 863, 906, 463, + 245, 13, -212, -305, + -105, 163, 279, 176, + 93, 67, 115, 192, + 61, -50, -132, -175, + -224, -271, -629, -252, + 1158, 972, 638, 280, + 300, 326, 143, -152, + -214, -287, 53, -42, + -236, -352, -423, -248, + -129, -163, -178, -119, + 85, 57, 514, 382, + 374, 402, 424, 423, + 271, 197, 97, 40, + 39, -97, -191, -164, + -230, -256, -410, 396, + 327, 127, 10, -119, + -167, -291, -274, -141, + -99, -226, -218, -139, + -224, -209, -268, -442, + -413, 222, 58, 521, + 344, 258, 76, -42, + -142, -165, -123, -92, + 47, 8, -3, -191, + -11, -164, -167, -351, + -740, 311, 538, 291, + 184, 29, -105, 9, + -30, -54, -17, -77, + -271, -412, -622, -648, + 476, 186, -66, -197, + -73, -94, -15, 47, + 28, 112, -58, -33, + 65, 19, 84, 86, + 276, 114, 472, 786, + 799, 625, 415, 178, + -35, -26, 5, 9, + 83, 39, 37, 39, + -184, -374, -265, -362, + -501, 337, 716, 478, + -60, -125, -163, 362, + 17, -122, -233, 279, + 138, 157, 318, 193, + 189, 209, 266, 252, + -46, -56, -277, -429, + 464, 386, 142, 44, + -43, 66, 264, 182, + 47, 14, -26, -79, + 49, 15, -128, -203, + -400, -478, 325, 27, + 234, 411, 205, 129, + 12, 58, 123, 57, + 171, 137, 96, 128, + -32, 134, -12, 57, + 119, 26, -22, -165, + -500, -701, -528, -116, + 64, -8, 97, -9, + -162, -66, -156, -194, + -303, -546, -341, 546, + 358, 95, 45, 76, + 270, 403, 205, 100, + 123, 50, -53, -144, + -110, -13, 32, -228, + -130, 353, 296, 56, + -372, -253, 365, 73, + 10, -34, -139, -191, + -96, 5, 44, -85, + -179, -129, -192, -246, + -85, -110, -155, -44, + -27, 145, 138, 79, + 32, -148, -577, -634, + 191, 94, -9, -35, + -77, -84, -56, -171, + -298, -271, -243, -156, + -328, -235, -76, -128, + -121, 129, 13, -22, + 32, 45, -248, -65, + 193, -81, 299, 57, + -147, 192, -165, -354, + -334, -106, -156, -40, + -3, -68, 124, -257, + 78, 124, 170, 412, + 227, 105, -104, 12, + 154, 250, 274, 258, + 4, -27, 235, 152, + 51, 338, 300, 7, + -314, -411, 215, 170, + -9, -93, -77, 76, + 67, 54, 200, 315, + 163, 72, -91, -402, + 158, 187, -156, -91, + 290, 267, 167, 91, + 140, 171, 112, 9, + -42, -177, -440, 385, + 80, 15, 172, 129, + 41, -129, -372, -24, + -75, -30, -170, 10, + -118, 57, 78, -101, + 232, 161, 123, 256, + 277, 101, -192, -629, + -100, -60, -232, 66, + 13, -13, -80, -239, + 239, 37, 32, 89, + -319, -579, 450, 360, + 3, -29, -299, -89, + -54, -110, -246, -164, + 6, -188, 338, 176, + -92, 197, 137, 134, + 12, -2, 56, -183, + 114, -36, -131, -204, + 75, -25, -174, 191, + -15, -290, -429, -267, + 79, 37, 106, 23, + -384, 425, 70, -14, + 212, 105, 15, -2, + -42, -37, -123, 108, + 28, -48, 193, 197, + 173, -33, 37, 73, + -57, 256, 137, -58, + -430, -228, 217, -51, + -10, -58, -6, 22, + 104, 61, -119, 169, + 144, 16, -46, -394, + 60, 454, -80, -298, + -65, 25, 0, -24, + -65, -417, 465, 276, + -3, -194, -13, 130, + 19, -6, -21, -24, + -180, -53, -85, 20, + 118, 147, 113, -75, + -289, 226, -122, 227, + 270, 125, 109, 197, + 125, 138, 44, 60, + 25, -55, -167, -32, + -139, -193, -173, -316, + 287, -208, 253, 239, + 27, -80, -188, -28, + -182, -235, 156, -117, + 128, -48, -58, -226, + 172, 181, 167, 19, + 62, 10, 2, 181, + 151, 108, -16, -11, + -78, -331, 411, 133, + 17, 104, 64, -184, + 24, -30, -3, -283, + 121, 204, -8, -199, + -21, -80, -169, -157, + -191, -136, 81, 155, + 14, -131, 244, 74, + -57, -47, -280, 347, + 111, -77, -128, -142, + -194, -125, -6, -68, + 91, 1, 23, 14, + -154, -34, 23, -38, + -343, 503, 146, -38, + -46, -41, 58, 31, + 63, -48, -117, 45, + 28, 1, -89, -5, + -44, -29, -448, 487, + 204, 81, 46, -106, + -302, 380, 120, -38, + -12, -39, 70, -3, + 25, -65, 30, -11, + 34, -15, 22, -115, + 0, -79, -83, 45, + 114, 43, 150, 36, + 233, 149, 195, 5, + 25, -52, -475, 274, + 28, -39, -8, -66, + -255, 258, 56, 143, + -45, -190, 165, -60, + 20, 2, 125, -129, + 51, -8, -335, 288, + 38, 59, 25, -42, + 23, -118, -112, 11, + -55, -133, -109, 24, + -105, 78, -64, -245, + 202, -65, -127, 162, + 40, -94, 89, -85, + -119, -103, 97, 9, + -70, -28, 194, 86, + -112, -92, -114, 74, + -49, 46, -84, -178, + 113, 52, -205, 333, + 88, 222, 56, -55, + 13, 86, 4, -77, + 224, 114, -105, 112, + 125, -29, -18, -144, + 22, -58, -99, 28, + 114, -66, -32, -169, + -314, 285, 72, -74, + 179, 28, -79, -182, + 13, -55, 147, 13, + 12, -54, 31, -84, + -17, -75, -228, 83, + -375, 436, 110, -63, + -27, -136, 169, -56, + -8, -171, 184, -42, + 148, 68, 204, 235, + 110, -229, 91, 171, + -43, -3, -26, -99, + -111, 71, -170, 202, + -67, 181, -37, 109, + -120, 3, -55, -260, + -16, 152, 91, 142, + 42, 44, 134, 47, + 17, -35, 22, 79, + -169, 41, 46, 277, + -93, -49, -126, 37, + -103, -34, -22, -90, + -134, -205, 92, -9, + 1, -195, -239, 45, + 54, 18, -23, -1, + -80, -98, -20, -261, + 306, 72, 20, -89, + -217, 11, 6, -82, + 89, 13, -129, -89, + 83, -71, -55, 130, + -98, -146, -27, -57, + 53, 275, 17, 170, + -5, -54, 132, -64, + 72, 160, -125, -168, + 72, 40, 170, 78, + 248, 116, 20, 84, + 31, -34, 190, 38, + 13, -106, 225, 27, + -168, 24, -157, -122, + 165, 11, -161, -213, + -12, -51, -101, 42, + 101, 27, 55, 111, + 75, 71, -96, -1, + 65, -277, 393, -26, + -44, -68, -84, -66, + -95, 235, 179, -25, + -41, 27, -91, -128, + -222, 146, -72, -30, + -24, 55, -126, -68, + -58, -127, 13, -97, + -106, 174, -100, 155, + 101, -146, -21, 261, + 22, 38, -66, 65, + 4, 70, 64, 144, + 59, 213, 71, -337, + 303, -52, 51, -56, + 1, 10, -15, -5, + 34, 52, 228, 131, + 161, -127, -214, 238, + 123, 64, -147, -50, + -34, -127, 204, 162, + 85, 41, 5, -140, + 73, -150, 56, -96, + -66, -20, 2, -235, + 59, -22, -107, 150, + -16, -47, -4, 81, + -67, 167, 149, 149, + -157, 288, -156, -27, + -8, 18, 83, -24, + -41, -167, 158, -100, + 93, 53, 201, 15, + 42, 266, 278, -12, + -6, -37, 85, 6, + 20, -188, -271, 107, + -13, -80, 51, 202, + 173, -69, 78, -188, + 46, 4, 153, 12, + -138, 169, 5, -58, + -123, -108, -243, 150, + 10, -191, 246, -15, + 38, 25, -10, 14, + 61, 50, -206, -215, + -220, 90, 5, -149, + -219, 56, 142, 24, + -376, 77, -80, 75, + 6, 42, -101, 16, + 56, 14, -57, 3, + -17, 80, 57, -36, + 88, -59, -97, -19, + -148, 46, -219, 226, + 114, -4, -72, -15, + 37, -49, -28, 247, + 44, 123, 47, -122, + -38, 17, 4, -113, + -32, -224, 154, -134, + 196, 71, -267, -85, + 28, -70, 89, -120, + 99, -2, 64, 76, + -166, -48, 189, -35, + -92, -169, -123, 339, + 38, -25, 38, -35, + 225, -139, -50, -63, + 246, 60, -185, -109, + -49, -53, -167, 51, + 149, 60, -101, -33, + 25, -76, 120, 32, + -30, -83, 102, 91, + -186, -261, 131, -197 +}; + +const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB0_16_Stage_info[ NLSF_MSVQ_CB0_16_STAGES ] = +{ + { 128, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 0 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 0 ] }, + { 16, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 128 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 128 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 144 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 144 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 152 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 152 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 160 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 160 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 168 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 168 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 176 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 176 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 184 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 184 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 192 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 192 ] }, + { 16, &SKP_Silk_NLSF_MSVQ_CB0_16_Q15[ 16 * 200 ], &SKP_Silk_NLSF_MSVQ_CB0_16_rates_Q5[ 200 ] } +}; + +const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB0_16 = +{ + NLSF_MSVQ_CB0_16_STAGES, + SKP_Silk_NLSF_CB0_16_Stage_info, + SKP_Silk_NLSF_MSVQ_CB0_16_ndelta_min_Q15, + SKP_Silk_NLSF_MSVQ_CB0_16_CDF, + SKP_Silk_NLSF_MSVQ_CB0_16_CDF_start_ptr, + SKP_Silk_NLSF_MSVQ_CB0_16_CDF_middle_idx +}; + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.h b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.h similarity index 97% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.h rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.h index b0dd766..f65f064 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.h +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB0_16.h @@ -1,51 +1,51 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_TABLES_NLSF_CB0_16_H -#define SKP_SILK_TABLES_NLSF_CB0_16_H - -#include "SKP_Silk_define.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define NLSF_MSVQ_CB0_16_STAGES 10 -#define NLSF_MSVQ_CB0_16_VECTORS 216 - -/* NLSF codebook entropy coding tables */ -extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ NLSF_MSVQ_CB0_16_VECTORS + NLSF_MSVQ_CB0_16_STAGES ]; -extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_16_CDF_start_ptr[ NLSF_MSVQ_CB0_16_STAGES ]; -extern const SKP_int SKP_Silk_NLSF_MSVQ_CB0_16_CDF_middle_idx[ NLSF_MSVQ_CB0_16_STAGES ]; - -#ifdef __cplusplus -} -#endif - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_TABLES_NLSF_CB0_16_H +#define SKP_SILK_TABLES_NLSF_CB0_16_H + +#include "SKP_Silk_define.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define NLSF_MSVQ_CB0_16_STAGES 10 +#define NLSF_MSVQ_CB0_16_VECTORS 216 + +/* NLSF codebook entropy coding tables */ +extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB0_16_CDF[ NLSF_MSVQ_CB0_16_VECTORS + NLSF_MSVQ_CB0_16_STAGES ]; +extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB0_16_CDF_start_ptr[ NLSF_MSVQ_CB0_16_STAGES ]; +extern const SKP_int SKP_Silk_NLSF_MSVQ_CB0_16_CDF_middle_idx[ NLSF_MSVQ_CB0_16_STAGES ]; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.c b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.c similarity index 97% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.c index 8c9abe4..9ea7be0 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.c @@ -1,577 +1,577 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/**********************************************/ -/* This file has been automatically generated */ -/* */ -/* ROM usage: 90+803 Words */ -/**********************************************/ - -#include "SKP_Silk_structs.h" -#include "SKP_Silk_tables_NLSF_CB1_10.h" - -const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ NLSF_MSVQ_CB1_10_VECTORS + NLSF_MSVQ_CB1_10_STAGES ] = -{ - 0, - 17096, - 24130, - 28997, - 33179, - 36696, - 40213, - 42493, - 44252, - 45973, - 47551, - 49095, - 50542, - 51898, - 53196, - 54495, - 55685, - 56851, - 57749, - 58628, - 59435, - 60207, - 60741, - 61220, - 61700, - 62179, - 62659, - 63138, - 63617, - 64097, - 64576, - 65056, - 65535, - 0, - 20378, - 33032, - 40395, - 46721, - 51707, - 56585, - 61157, - 65535, - 0, - 15055, - 25472, - 35447, - 42501, - 48969, - 54773, - 60212, - 65535, - 0, - 12069, - 22440, - 32812, - 40145, - 46870, - 53595, - 59630, - 65535, - 0, - 10839, - 19954, - 27957, - 35961, - 43965, - 51465, - 58805, - 65535, - 0, - 8933, - 17674, - 26415, - 34785, - 42977, - 50820, - 58496, - 65535 -}; - -const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_10_CDF_start_ptr[ NLSF_MSVQ_CB1_10_STAGES ] = -{ - &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 0 ], - &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 33 ], - &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 42 ], - &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 51 ], - &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 60 ], - &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 69 ] -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB1_10_CDF_middle_idx[ NLSF_MSVQ_CB1_10_STAGES ] = -{ - 5, - 3, - 4, - 4, - 5, - 5 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ NLSF_MSVQ_CB1_10_VECTORS ] = -{ - 62, 103, - 120, 127, - 135, 135, - 155, 167, - 168, 172, - 173, 176, - 179, 181, - 181, 185, - 186, 198, - 199, 203, - 205, 222, - 227, 227, - 227, 227, - 227, 227, - 227, 227, - 227, 227, - 54, 76, - 101, 108, - 119, 120, - 123, 125, - 68, 85, - 87, 103, - 107, 112, - 115, 116, - 78, 85, - 85, 101, - 105, 105, - 110, 111, - 83, 91, - 97, 97, - 97, 100, - 101, 105, - 92, 93, - 93, 95, - 96, 98, - 99, 103 -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB1_10_ndelta_min_Q15[ 10 + 1 ] = -{ - 462, - 3, - 64, - 74, - 98, - 50, - 97, - 68, - 120, - 53, - 639 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * NLSF_MSVQ_CB1_10_VECTORS ] = -{ - 1877, 4646, - 7712, 10745, - 13964, 17028, - 20239, 23182, - 26471, 29287, - 1612, 3278, - 7086, 9975, - 13228, 16264, - 19596, 22690, - 26037, 28965, - 2169, 3830, - 6460, 8958, - 11960, 14750, - 18408, 21659, - 25018, 28043, - 3680, 6024, - 8986, 12256, - 15201, 18188, - 21741, 24460, - 27484, 30059, - 2584, 5187, - 7799, 10902, - 13179, 15765, - 19017, 22431, - 25891, 28698, - 3731, 5751, - 8650, 11742, - 15090, 17407, - 20391, 23421, - 26228, 29247, - 2107, 6323, - 8915, 12226, - 14775, 17791, - 20664, 23679, - 26829, 29353, - 1677, 2870, - 5386, 8077, - 11817, 15176, - 18657, 22006, - 25513, 28689, - 2111, 3625, - 7027, 10588, - 14059, 17193, - 21137, 24260, - 27577, 30036, - 2428, 4010, - 5765, 9376, - 13805, 15821, - 19444, 22389, - 25295, 29310, - 2256, 4628, - 8377, 12441, - 15283, 19462, - 22257, 25551, - 28432, 30304, - 2352, 3675, - 6129, 11868, - 14551, 16655, - 19624, 21883, - 26526, 28849, - 5243, 7248, - 10558, 13269, - 15651, 17919, - 21141, 23827, - 27102, 29519, - 4422, 6725, - 10449, 13273, - 16124, 19921, - 22826, 26061, - 28763, 30583, - 4508, 6291, - 9504, 11809, - 13827, 15950, - 19077, 22084, - 25740, 28658, - 2540, 4297, - 8579, 13578, - 16634, 19101, - 21547, 23887, - 26777, 29146, - 3377, 6358, - 10224, 14518, - 17905, 21056, - 23637, 25784, - 28161, 30109, - 4177, 5942, - 8159, 10108, - 12130, 15470, - 20191, 23326, - 26782, 29359, - 2492, 3801, - 6144, 9825, - 16000, 18671, - 20893, 23663, - 25899, 28974, - 3011, 4727, - 6834, 10505, - 12465, 14496, - 17065, 20052, - 25265, 28057, - 4149, 7197, - 12338, 15076, - 18002, 20190, - 22187, 24723, - 27083, 29125, - 2975, 4578, - 6448, 8378, - 9671, 13225, - 19502, 22277, - 26058, 28850, - 4102, 5760, - 7744, 9484, - 10744, 12308, - 14677, 19607, - 24841, 28381, - 4931, 9287, - 12477, 13395, - 13712, 14351, - 16048, 19867, - 24188, 28994, - 4141, 7867, - 13140, 17720, - 20064, 21108, - 21692, 22722, - 23736, 27449, - 4011, 8720, - 13234, 16206, - 17601, 18289, - 18524, 19689, - 23234, 27882, - 3420, 5995, - 11230, 15117, - 15907, 16783, - 17762, 23347, - 26898, 29946, - 3080, 6786, - 10465, 13676, - 18059, 23615, - 27058, 29082, - 29563, 29905, - 3038, 5620, - 9266, 12870, - 18803, 19610, - 20010, 20802, - 23882, 29306, - 3314, 6420, - 9046, 13262, - 15869, 23117, - 23667, 24215, - 24487, 25915, - 3469, 6963, - 10103, 15282, - 20531, 23240, - 25024, 26021, - 26736, 27255, - 3041, 6459, - 9777, 12896, - 16315, 19410, - 24070, 29353, - 31795, 32075, - -200, -134, - -113, -204, - -347, -440, - -352, -211, - -418, -172, - -313, 59, - 495, 772, - 721, 614, - 334, 444, - 225, 242, - 161, 16, - 274, 564, - -73, -188, - -395, -171, - 777, 508, - 1340, 1145, - 699, 196, - 223, 173, - 90, 25, - -26, 18, - 133, -105, - -360, -277, - 859, 634, - 41, -557, - -768, -926, - -601, -1021, - -1189, -365, - 225, 107, - 374, -50, - 433, 417, - 156, 39, - -597, -1397, - -1594, -592, - -485, -292, - 253, 87, - -0, -6, - -25, -345, - -240, 120, - 1261, 946, - 166, -277, - 241, 167, - 170, 429, - 518, 714, - 602, 254, - 134, 92, - -152, -324, - -394, 49, - -151, -304, - -724, -657, - -162, -369, - -35, 3, - -2, -312, - -200, -92, - -227, 242, - 628, 565, - -124, 1056, - 770, 101, - -84, -33, - 4, -192, - -272, 5, - -627, -977, - 419, 472, - 53, -103, - 145, 322, - -95, -31, - -100, -303, - -560, -1067, - -413, 714, - 283, 2, - -223, -367, - 523, 360, - -38, -115, - 378, -591, - -718, 448, - -481, -274, - 180, -88, - -581, -157, - -696, -1265, - 394, -479, - -23, 124, - -43, 19, - -113, -236, - -412, -659, - -200, 2, - -69, -342, - 199, 55, - 58, -36, - -51, -62, - 507, 507, - 427, 442, - 36, 601, - -141, 68, - 274, 274, - 68, -12, - -4, 71, - -193, -464, - -425, -383, - 408, 203, - -337, 236, - 410, -59, - -25, -341, - -449, 28, - -9, 90, - 332, -14, - -905, 96, - -540, -242, - 679, -59, - 192, -24, - 60, -217, - 5, -37, - 179, -20, - 311, 519, - 274, 72, - -326, -1030, - -262, 213, - 380, 82, - 328, 411, - -540, 574, - -283, 151, - 181, -402, - -278, -240, - -110, -227, - -264, -89, - -250, -259, - -27, 106, - -239, -98, - -390, 118, - 61, 104, - 294, 532, - 92, -13, - 60, -233, - 335, 541, - 307, -26, - -110, -91, - -231, -460, - 170, 201, - 96, -372, - 132, 435, - -302, 216, - -279, -41, - 74, 190, - 368, 273, - -186, -608, - -157, 159, - 12, 278, - 245, 307, - 25, -187, - -16, 55, - 30, -163, - 548, -307, - 106, -5, - 27, 330, - -416, 475, - 438, -235, - 104, 137, - 21, -5, - -300, -468, - 521, -347, - 170, -200, - -219, 308, - -122, -133, - 219, -16, - 359, 412, - -89, -111, - 48, 322, - 142, 177, - -286, -127, - -39, -63, - -42, -451, - 160, 308, - -57, 193, - -48, 74, - -346, 59, - -27, 27, - -469, -277, - -344, 282, - 262, 122, - 171, -249, - 27, 258, - 188, -3, - 67, -206, - -284, 291, - -117, -88, - -477, 375, - 50, 106, - 99, -182, - 438, -376, - -401, -49, - 119, -23, - -10, -48, - -116, -200, - -310, 121, - 73, 7, - 237, -226, - 139, -456, - 397, 35, - 3, -108, - 323, -75, - 332, 198, - -99, -21 -}; - -const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB1_10_Stage_info[ NLSF_MSVQ_CB1_10_STAGES ] = -{ - { 32, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 0 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 0 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 32 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 32 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 40 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 40 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 48 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 48 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 56 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 56 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 64 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 64 ] } -}; - -const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB1_10 = -{ - NLSF_MSVQ_CB1_10_STAGES, - SKP_Silk_NLSF_CB1_10_Stage_info, - SKP_Silk_NLSF_MSVQ_CB1_10_ndelta_min_Q15, - SKP_Silk_NLSF_MSVQ_CB1_10_CDF, - SKP_Silk_NLSF_MSVQ_CB1_10_CDF_start_ptr, - SKP_Silk_NLSF_MSVQ_CB1_10_CDF_middle_idx -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/**********************************************/ +/* This file has been automatically generated */ +/* */ +/* ROM usage: 90+803 Words */ +/**********************************************/ + +#include "SKP_Silk_structs.h" +#include "SKP_Silk_tables_NLSF_CB1_10.h" + +const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ NLSF_MSVQ_CB1_10_VECTORS + NLSF_MSVQ_CB1_10_STAGES ] = +{ + 0, + 17096, + 24130, + 28997, + 33179, + 36696, + 40213, + 42493, + 44252, + 45973, + 47551, + 49095, + 50542, + 51898, + 53196, + 54495, + 55685, + 56851, + 57749, + 58628, + 59435, + 60207, + 60741, + 61220, + 61700, + 62179, + 62659, + 63138, + 63617, + 64097, + 64576, + 65056, + 65535, + 0, + 20378, + 33032, + 40395, + 46721, + 51707, + 56585, + 61157, + 65535, + 0, + 15055, + 25472, + 35447, + 42501, + 48969, + 54773, + 60212, + 65535, + 0, + 12069, + 22440, + 32812, + 40145, + 46870, + 53595, + 59630, + 65535, + 0, + 10839, + 19954, + 27957, + 35961, + 43965, + 51465, + 58805, + 65535, + 0, + 8933, + 17674, + 26415, + 34785, + 42977, + 50820, + 58496, + 65535 +}; + +const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_10_CDF_start_ptr[ NLSF_MSVQ_CB1_10_STAGES ] = +{ + &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 0 ], + &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 33 ], + &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 42 ], + &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 51 ], + &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 60 ], + &SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ 69 ] +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB1_10_CDF_middle_idx[ NLSF_MSVQ_CB1_10_STAGES ] = +{ + 5, + 3, + 4, + 4, + 5, + 5 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ NLSF_MSVQ_CB1_10_VECTORS ] = +{ + 62, 103, + 120, 127, + 135, 135, + 155, 167, + 168, 172, + 173, 176, + 179, 181, + 181, 185, + 186, 198, + 199, 203, + 205, 222, + 227, 227, + 227, 227, + 227, 227, + 227, 227, + 227, 227, + 54, 76, + 101, 108, + 119, 120, + 123, 125, + 68, 85, + 87, 103, + 107, 112, + 115, 116, + 78, 85, + 85, 101, + 105, 105, + 110, 111, + 83, 91, + 97, 97, + 97, 100, + 101, 105, + 92, 93, + 93, 95, + 96, 98, + 99, 103 +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB1_10_ndelta_min_Q15[ 10 + 1 ] = +{ + 462, + 3, + 64, + 74, + 98, + 50, + 97, + 68, + 120, + 53, + 639 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * NLSF_MSVQ_CB1_10_VECTORS ] = +{ + 1877, 4646, + 7712, 10745, + 13964, 17028, + 20239, 23182, + 26471, 29287, + 1612, 3278, + 7086, 9975, + 13228, 16264, + 19596, 22690, + 26037, 28965, + 2169, 3830, + 6460, 8958, + 11960, 14750, + 18408, 21659, + 25018, 28043, + 3680, 6024, + 8986, 12256, + 15201, 18188, + 21741, 24460, + 27484, 30059, + 2584, 5187, + 7799, 10902, + 13179, 15765, + 19017, 22431, + 25891, 28698, + 3731, 5751, + 8650, 11742, + 15090, 17407, + 20391, 23421, + 26228, 29247, + 2107, 6323, + 8915, 12226, + 14775, 17791, + 20664, 23679, + 26829, 29353, + 1677, 2870, + 5386, 8077, + 11817, 15176, + 18657, 22006, + 25513, 28689, + 2111, 3625, + 7027, 10588, + 14059, 17193, + 21137, 24260, + 27577, 30036, + 2428, 4010, + 5765, 9376, + 13805, 15821, + 19444, 22389, + 25295, 29310, + 2256, 4628, + 8377, 12441, + 15283, 19462, + 22257, 25551, + 28432, 30304, + 2352, 3675, + 6129, 11868, + 14551, 16655, + 19624, 21883, + 26526, 28849, + 5243, 7248, + 10558, 13269, + 15651, 17919, + 21141, 23827, + 27102, 29519, + 4422, 6725, + 10449, 13273, + 16124, 19921, + 22826, 26061, + 28763, 30583, + 4508, 6291, + 9504, 11809, + 13827, 15950, + 19077, 22084, + 25740, 28658, + 2540, 4297, + 8579, 13578, + 16634, 19101, + 21547, 23887, + 26777, 29146, + 3377, 6358, + 10224, 14518, + 17905, 21056, + 23637, 25784, + 28161, 30109, + 4177, 5942, + 8159, 10108, + 12130, 15470, + 20191, 23326, + 26782, 29359, + 2492, 3801, + 6144, 9825, + 16000, 18671, + 20893, 23663, + 25899, 28974, + 3011, 4727, + 6834, 10505, + 12465, 14496, + 17065, 20052, + 25265, 28057, + 4149, 7197, + 12338, 15076, + 18002, 20190, + 22187, 24723, + 27083, 29125, + 2975, 4578, + 6448, 8378, + 9671, 13225, + 19502, 22277, + 26058, 28850, + 4102, 5760, + 7744, 9484, + 10744, 12308, + 14677, 19607, + 24841, 28381, + 4931, 9287, + 12477, 13395, + 13712, 14351, + 16048, 19867, + 24188, 28994, + 4141, 7867, + 13140, 17720, + 20064, 21108, + 21692, 22722, + 23736, 27449, + 4011, 8720, + 13234, 16206, + 17601, 18289, + 18524, 19689, + 23234, 27882, + 3420, 5995, + 11230, 15117, + 15907, 16783, + 17762, 23347, + 26898, 29946, + 3080, 6786, + 10465, 13676, + 18059, 23615, + 27058, 29082, + 29563, 29905, + 3038, 5620, + 9266, 12870, + 18803, 19610, + 20010, 20802, + 23882, 29306, + 3314, 6420, + 9046, 13262, + 15869, 23117, + 23667, 24215, + 24487, 25915, + 3469, 6963, + 10103, 15282, + 20531, 23240, + 25024, 26021, + 26736, 27255, + 3041, 6459, + 9777, 12896, + 16315, 19410, + 24070, 29353, + 31795, 32075, + -200, -134, + -113, -204, + -347, -440, + -352, -211, + -418, -172, + -313, 59, + 495, 772, + 721, 614, + 334, 444, + 225, 242, + 161, 16, + 274, 564, + -73, -188, + -395, -171, + 777, 508, + 1340, 1145, + 699, 196, + 223, 173, + 90, 25, + -26, 18, + 133, -105, + -360, -277, + 859, 634, + 41, -557, + -768, -926, + -601, -1021, + -1189, -365, + 225, 107, + 374, -50, + 433, 417, + 156, 39, + -597, -1397, + -1594, -592, + -485, -292, + 253, 87, + -0, -6, + -25, -345, + -240, 120, + 1261, 946, + 166, -277, + 241, 167, + 170, 429, + 518, 714, + 602, 254, + 134, 92, + -152, -324, + -394, 49, + -151, -304, + -724, -657, + -162, -369, + -35, 3, + -2, -312, + -200, -92, + -227, 242, + 628, 565, + -124, 1056, + 770, 101, + -84, -33, + 4, -192, + -272, 5, + -627, -977, + 419, 472, + 53, -103, + 145, 322, + -95, -31, + -100, -303, + -560, -1067, + -413, 714, + 283, 2, + -223, -367, + 523, 360, + -38, -115, + 378, -591, + -718, 448, + -481, -274, + 180, -88, + -581, -157, + -696, -1265, + 394, -479, + -23, 124, + -43, 19, + -113, -236, + -412, -659, + -200, 2, + -69, -342, + 199, 55, + 58, -36, + -51, -62, + 507, 507, + 427, 442, + 36, 601, + -141, 68, + 274, 274, + 68, -12, + -4, 71, + -193, -464, + -425, -383, + 408, 203, + -337, 236, + 410, -59, + -25, -341, + -449, 28, + -9, 90, + 332, -14, + -905, 96, + -540, -242, + 679, -59, + 192, -24, + 60, -217, + 5, -37, + 179, -20, + 311, 519, + 274, 72, + -326, -1030, + -262, 213, + 380, 82, + 328, 411, + -540, 574, + -283, 151, + 181, -402, + -278, -240, + -110, -227, + -264, -89, + -250, -259, + -27, 106, + -239, -98, + -390, 118, + 61, 104, + 294, 532, + 92, -13, + 60, -233, + 335, 541, + 307, -26, + -110, -91, + -231, -460, + 170, 201, + 96, -372, + 132, 435, + -302, 216, + -279, -41, + 74, 190, + 368, 273, + -186, -608, + -157, 159, + 12, 278, + 245, 307, + 25, -187, + -16, 55, + 30, -163, + 548, -307, + 106, -5, + 27, 330, + -416, 475, + 438, -235, + 104, 137, + 21, -5, + -300, -468, + 521, -347, + 170, -200, + -219, 308, + -122, -133, + 219, -16, + 359, 412, + -89, -111, + 48, 322, + 142, 177, + -286, -127, + -39, -63, + -42, -451, + 160, 308, + -57, 193, + -48, 74, + -346, 59, + -27, 27, + -469, -277, + -344, 282, + 262, 122, + 171, -249, + 27, 258, + 188, -3, + 67, -206, + -284, 291, + -117, -88, + -477, 375, + 50, 106, + 99, -182, + 438, -376, + -401, -49, + 119, -23, + -10, -48, + -116, -200, + -310, 121, + 73, 7, + 237, -226, + 139, -456, + 397, 35, + 3, -108, + 323, -75, + 332, 198, + -99, -21 +}; + +const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB1_10_Stage_info[ NLSF_MSVQ_CB1_10_STAGES ] = +{ + { 32, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 0 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 0 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 32 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 32 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 40 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 40 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 48 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 48 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 56 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 56 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_10_Q15[ 10 * 64 ], &SKP_Silk_NLSF_MSVQ_CB1_10_rates_Q5[ 64 ] } +}; + +const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB1_10 = +{ + NLSF_MSVQ_CB1_10_STAGES, + SKP_Silk_NLSF_CB1_10_Stage_info, + SKP_Silk_NLSF_MSVQ_CB1_10_ndelta_min_Q15, + SKP_Silk_NLSF_MSVQ_CB1_10_CDF, + SKP_Silk_NLSF_MSVQ_CB1_10_CDF_start_ptr, + SKP_Silk_NLSF_MSVQ_CB1_10_CDF_middle_idx +}; + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.h b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.h similarity index 97% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.h rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.h index cdac82c..6b9a92b 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.h +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_10.h @@ -1,51 +1,51 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_TABLES_NLSF_CB1_10_H -#define SKP_SILK_TABLES_NLSF_CB1_10_H - -#include "SKP_Silk_define.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define NLSF_MSVQ_CB1_10_STAGES 6 -#define NLSF_MSVQ_CB1_10_VECTORS 72 - -/* NLSF codebook entropy coding tables */ -extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ NLSF_MSVQ_CB1_10_VECTORS + NLSF_MSVQ_CB1_10_STAGES ]; -extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_10_CDF_start_ptr[ NLSF_MSVQ_CB1_10_STAGES ]; -extern const SKP_int SKP_Silk_NLSF_MSVQ_CB1_10_CDF_middle_idx[ NLSF_MSVQ_CB1_10_STAGES ]; - -#ifdef __cplusplus -} -#endif - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_TABLES_NLSF_CB1_10_H +#define SKP_SILK_TABLES_NLSF_CB1_10_H + +#include "SKP_Silk_define.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define NLSF_MSVQ_CB1_10_STAGES 6 +#define NLSF_MSVQ_CB1_10_VECTORS 72 + +/* NLSF codebook entropy coding tables */ +extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_10_CDF[ NLSF_MSVQ_CB1_10_VECTORS + NLSF_MSVQ_CB1_10_STAGES ]; +extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_10_CDF_start_ptr[ NLSF_MSVQ_CB1_10_STAGES ]; +extern const SKP_int SKP_Silk_NLSF_MSVQ_CB1_10_CDF_middle_idx[ NLSF_MSVQ_CB1_10_STAGES ]; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.c b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.c similarity index 98% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.c index 0780b2d..56d1aa9 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.c @@ -1,703 +1,703 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -/**********************************************/ -/* This file has been automatically generated */ -/* */ -/* ROM usage: 134+1785 Words */ -/**********************************************/ - -#include "SKP_Silk_structs.h" -#include "SKP_Silk_tables_NLSF_CB1_16.h" - -const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ NLSF_MSVQ_CB1_16_VECTORS + NLSF_MSVQ_CB1_16_STAGES ] = -{ - 0, - 19099, - 26957, - 30639, - 34242, - 37546, - 40447, - 43287, - 46005, - 48445, - 49865, - 51284, - 52673, - 53975, - 55221, - 56441, - 57267, - 58025, - 58648, - 59232, - 59768, - 60248, - 60729, - 61210, - 61690, - 62171, - 62651, - 63132, - 63613, - 64093, - 64574, - 65054, - 65535, - 0, - 28808, - 38775, - 46801, - 51785, - 55886, - 59410, - 62572, - 65535, - 0, - 27376, - 38639, - 45052, - 51465, - 55448, - 59021, - 62594, - 65535, - 0, - 33403, - 39569, - 45102, - 49961, - 54047, - 57959, - 61788, - 65535, - 0, - 25851, - 43356, - 47828, - 52204, - 55964, - 59413, - 62507, - 65535, - 0, - 34277, - 40337, - 45432, - 50311, - 54326, - 58171, - 61853, - 65535, - 0, - 33538, - 39865, - 45302, - 50076, - 54549, - 58478, - 62159, - 65535, - 0, - 27445, - 35258, - 40665, - 46072, - 51362, - 56540, - 61086, - 65535, - 0, - 22080, - 30779, - 37065, - 43085, - 48849, - 54613, - 60133, - 65535, - 0, - 13417, - 21748, - 30078, - 38231, - 46383, - 53091, - 59515, - 65535 -}; - -const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_16_CDF_start_ptr[ NLSF_MSVQ_CB1_16_STAGES ] = -{ - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 0 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 33 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 42 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 51 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 60 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 69 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 78 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 87 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 96 ], - &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 105 ] -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB1_16_CDF_middle_idx[ NLSF_MSVQ_CB1_16_STAGES ] = -{ - 5, - 2, - 2, - 2, - 2, - 2, - 2, - 3, - 3, - 4 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ NLSF_MSVQ_CB1_16_VECTORS ] = -{ - 57, 98, - 133, 134, - 138, 144, - 145, 147, - 152, 177, - 177, 178, - 181, 183, - 184, 202, - 206, 215, - 218, 222, - 227, 227, - 227, 227, - 227, 227, - 227, 227, - 227, 227, - 227, 227, - 38, 87, - 97, 119, - 128, 135, - 140, 143, - 40, 81, - 107, 107, - 129, 134, - 134, 143, - 31, 109, - 114, 120, - 128, 130, - 131, 132, - 43, 61, - 124, 125, - 132, 136, - 141, 142, - 30, 110, - 118, 120, - 129, 131, - 133, 133, - 31, 108, - 115, 121, - 124, 130, - 133, 137, - 40, 98, - 115, 115, - 116, 117, - 123, 124, - 50, 93, - 108, 110, - 112, 112, - 114, 115, - 73, 95, - 95, 96, - 96, 105, - 107, 110 -}; - -const SKP_int SKP_Silk_NLSF_MSVQ_CB1_16_ndelta_min_Q15[ 16 + 1 ] = -{ - 148, - 3, - 60, - 68, - 117, - 86, - 121, - 124, - 152, - 153, - 207, - 151, - 225, - 239, - 126, - 183, - 792 -}; - -const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * NLSF_MSVQ_CB1_16_VECTORS ] = -{ - 1309, 3060, 5071, 6996, - 9028, 10938, 12934, 14891, - 16933, 18854, 20792, 22764, - 24753, 26659, 28626, 30501, - 1264, 2745, 4610, 6408, - 8286, 10043, 12084, 14108, - 16118, 18163, 20095, 22164, - 24264, 26316, 28329, 30251, - 1044, 2080, 3672, 5179, - 7140, 9100, 11070, 13065, - 15423, 17790, 19931, 22101, - 24290, 26361, 28499, 30418, - 1131, 2476, 4478, 6149, - 7902, 9875, 11938, 13809, - 15869, 17730, 19948, 21707, - 23761, 25535, 27426, 28917, - 1040, 2004, 4026, 6100, - 8432, 10494, 12610, 14694, - 16797, 18775, 20799, 22782, - 24772, 26682, 28631, 30516, - 2310, 3812, 5913, 7933, - 10033, 11881, 13885, 15798, - 17751, 19576, 21482, 23276, - 25157, 27010, 28833, 30623, - 1254, 2847, 5013, 6781, - 8626, 10370, 12726, 14633, - 16281, 17852, 19870, 21472, - 23002, 24629, 26710, 27960, - 1468, 3059, 4987, 7026, - 8741, 10412, 12281, 14020, - 15970, 17723, 19640, 21522, - 23472, 25661, 27986, 30225, - 2171, 3566, 5605, 7384, - 9404, 11220, 13030, 14758, - 16687, 18417, 20346, 22091, - 24055, 26212, 28356, 30397, - 2409, 4676, 7543, 9786, - 11419, 12935, 14368, 15653, - 17366, 18943, 20762, 22477, - 24440, 26327, 28284, 30242, - 2354, 4222, 6820, 9107, - 11596, 13934, 15973, 17682, - 19158, 20517, 21991, 23420, - 25178, 26936, 28794, 30527, - 1323, 2414, 4184, 6039, - 7534, 9398, 11099, 13097, - 14799, 16451, 18434, 20887, - 23490, 25838, 28046, 30225, - 1361, 3243, 6048, 8511, - 11001, 13145, 15073, 16608, - 18126, 19381, 20912, 22607, - 24660, 26668, 28663, 30566, - 1216, 2648, 5901, 8422, - 10037, 11425, 12973, 14603, - 16686, 18600, 20555, 22415, - 24450, 26280, 28206, 30077, - 2417, 4048, 6316, 8433, - 10510, 12757, 15072, 17295, - 19573, 21503, 23329, 24782, - 26235, 27689, 29214, 30819, - 1012, 2345, 4991, 7377, - 9465, 11916, 14296, 16566, - 18672, 20544, 22292, 23838, - 25415, 27050, 28848, 30551, - 1937, 3693, 6267, 8019, - 10372, 12194, 14287, 15657, - 17431, 18864, 20769, 22206, - 24037, 25463, 27383, 28602, - 1969, 3305, 5017, 6726, - 8375, 9993, 11634, 13280, - 15078, 16751, 18464, 20119, - 21959, 23858, 26224, 29298, - 1198, 2647, 5428, 7423, - 9775, 12155, 14665, 16344, - 18121, 19790, 21557, 22847, - 24484, 25742, 27639, 28711, - 1636, 3353, 5447, 7597, - 9837, 11647, 13964, 16019, - 17862, 20116, 22319, 24037, - 25966, 28086, 29914, 31294, - 2676, 4105, 6378, 8223, - 10058, 11549, 13072, 14453, - 15956, 17355, 18931, 20402, - 22183, 23884, 25717, 27723, - 1373, 2593, 4449, 5633, - 7300, 8425, 9474, 10818, - 12769, 15722, 19002, 21429, - 23682, 25924, 28135, 30333, - 1596, 3183, 5378, 7164, - 8670, 10105, 11470, 12834, - 13991, 15042, 16642, 17903, - 20759, 25283, 27770, 30240, - 2037, 3987, 6237, 8117, - 9954, 12245, 14217, 15892, - 17775, 20114, 22314, 25942, - 26305, 26483, 26796, 28561, - 2181, 3858, 5760, 7924, - 10041, 11577, 13769, 15700, - 17429, 19879, 23583, 24538, - 25212, 25693, 28688, 30507, - 1992, 3882, 6474, 7883, - 9381, 12672, 14340, 15701, - 16658, 17832, 20850, 22885, - 24677, 26457, 28491, 30460, - 2391, 3988, 5448, 7432, - 11014, 12579, 13140, 14146, - 15898, 18592, 21104, 22993, - 24673, 27186, 28142, 29612, - 1713, 5102, 6989, 7798, - 8670, 10110, 12746, 14881, - 16709, 18407, 20126, 22107, - 24181, 26198, 28237, 30137, - 1612, 3617, 6148, 8359, - 9576, 11528, 14936, 17809, - 18287, 18729, 19001, 21111, - 24631, 26596, 28740, 30643, - 2266, 4168, 7862, 9546, - 9618, 9703, 10134, 13897, - 16265, 18432, 20587, 22605, - 24754, 26994, 29125, 30840, - 1840, 3917, 6272, 7809, - 9714, 11438, 13767, 15799, - 19244, 21972, 22980, 23180, - 23723, 25650, 29117, 31085, - 1458, 3612, 6008, 7488, - 9827, 11893, 14086, 15734, - 17440, 19535, 22424, 24767, - 29246, 29928, 30516, 30947, - -102, -121, -31, -6, - 5, -2, 8, -18, - -4, 6, 14, -2, - -12, -16, -12, -60, - -126, -353, -574, -677, - -657, -617, -498, -393, - -348, -277, -225, -164, - -102, -70, -31, 33, - 4, 379, 387, 551, - 605, 620, 532, 482, - 442, 454, 385, 347, - 322, 299, 266, 200, - 1168, 951, 672, 246, - 60, -161, -259, -234, - -253, -282, -203, -187, - -155, -176, -198, -178, - 10, 170, 393, 609, - 555, 208, -330, -571, - -769, -633, -319, -43, - 95, 105, 106, 116, - -152, -140, -125, 5, - 173, 274, 264, 331, - -37, -293, -609, -786, - -959, -814, -645, -238, - -91, 36, -11, -101, - -279, -227, -40, 90, - 530, 677, 890, 1104, - 999, 835, 564, 295, - -280, -364, -340, -331, - -284, 288, 761, 880, - 988, 627, 146, -226, - -203, -181, -142, 39, - 24, -26, -107, -92, - -161, -135, -131, -88, - -160, -156, -75, -43, - -36, -6, -33, 33, - -324, -415, -108, 124, - 157, 191, 203, 197, - 144, 109, 152, 176, - 190, 122, 101, 159, - 663, 668, 480, 400, - 379, 444, 446, 458, - 343, 351, 310, 228, - 133, 44, 75, 63, - -84, 39, -29, 35, - -94, -233, -261, -354, - 77, 262, -24, -145, - -333, -409, -404, -597, - -488, -300, 910, 592, - 412, 120, 130, -51, - -37, -77, -172, -181, - -159, -148, -72, -62, - 510, 516, 113, -585, - -1075, -957, -417, -195, - 9, 7, -88, -173, - -91, 54, 98, 95, - -28, 197, -527, -621, - 157, 122, -168, 147, - 309, 300, 336, 315, - 396, 408, 376, 106, - -162, -170, -315, 98, - 821, 908, 570, -33, - -312, -568, -572, -378, - -107, 23, 156, 93, - -129, -87, 20, -72, - -37, 40, 21, 27, - 48, 75, 77, 65, - 46, 71, 66, 47, - 136, 344, 236, 322, - 170, 283, 269, 291, - 162, -43, -204, -259, - -240, -305, -350, -312, - 447, 348, 345, 257, - 71, -131, -77, -190, - -202, -40, 35, 133, - 261, 365, 438, 303, - -8, 22, 140, 137, - -300, -641, -764, -268, - -23, -25, 73, -162, - -150, -212, -72, 6, - 39, 78, 104, -93, - -308, -136, 117, -71, - -513, -820, -700, -450, - -161, -23, 29, 78, - 337, 106, -406, -782, - -112, 233, 383, 62, - -126, 6, -77, -29, - -146, -123, -51, -27, - -27, -381, -641, 402, - 539, 8, -207, -366, - -36, -27, -204, -227, - -237, -189, -64, 51, - -92, -137, -281, 62, - 233, 92, 148, 294, - 363, 416, 564, 625, - 370, -36, -469, -462, - 102, 168, 32, 117, - -21, 97, 139, 89, - 104, 35, 4, 82, - 66, 58, 73, 93, - -76, -320, -236, -189, - -203, -142, -27, -73, - 9, -9, -25, 12, - -15, 4, 4, -50, - 314, 180, 162, -49, - 199, -108, -227, -66, - -447, -67, -264, -394, - 5, 55, -133, -176, - -116, -241, 272, 109, - 282, 262, 192, -64, - -392, -514, 156, 203, - 154, 72, -34, -160, - -73, 3, -33, -431, - 321, 18, -567, -590, - -108, 88, 66, 51, - -31, -193, -46, 65, - -29, -23, 215, -31, - 101, -113, 32, 304, - 88, 320, 448, 5, - -439, -562, -508, -135, - -13, -171, -8, 182, - -99, -181, -149, 376, - 476, 64, -396, -652, - -150, 176, 222, 65, - -590, 719, 271, 399, - 245, 72, -156, -152, - -176, 59, 94, 125, - -9, -7, 9, 1, - -61, -116, -82, 1, - 79, 22, -44, -15, - -48, -65, -62, -101, - -102, -54, -70, -78, - -80, -25, 398, 71, - 139, 38, 90, 194, - 222, 249, 165, 94, - 221, 262, 163, 91, - -206, 573, 200, -287, - -147, 5, -18, -85, - -74, -125, -87, 85, - 141, 4, -4, 28, - 234, 48, -150, -111, - -506, 237, -209, 345, - 94, -124, 77, 121, - 143, 12, -80, -48, - 191, 144, -93, -65, - -151, -643, 435, 106, - 87, 7, 65, 102, - 94, 68, 5, 99, - 222, 93, 94, 355, - -13, -89, -228, -503, - 287, 109, 108, 449, - 253, -29, -109, -116, - 15, -73, -20, 131, - -147, 72, 59, -150, - -594, 273, 316, 132, - 199, 106, 198, 212, - 220, 82, 45, -13, - 223, 137, 270, 38, - 252, 135, -177, -207, - -360, -102, 403, 406, - -14, 83, 64, 51, - -7, -99, -97, -88, - -124, -65, 42, 32, - 28, 29, 12, 20, - 119, -26, -212, -201, - 373, 251, 141, 103, - 36, -52, 66, 18, - -6, -95, -196, 5, - 98, -85, -108, 218, - -164, 20, 356, 172, - 37, 266, 23, 112, - -24, -99, -92, -178, - 29, -278, 388, -60, - -220, 300, -13, 154, - 191, 15, -37, -110, - -153, -150, -114, -7, - -94, -31, -62, -177, - 4, -70, 35, 453, - 147, -247, -328, 101, - 20, -114, 147, 108, - -119, -109, -102, -238, - 55, -102, 173, -89, - 129, 138, -330, -160, - 485, 154, -59, -170, - -20, -34, -261, -40, - -129, 77, -84, 69, - 83, 160, 169, 63, - -516, 30, 336, 52, - -0, -52, -124, 158, - 19, 197, -10, -375, - 405, 285, 114, -395, - -47, 196, 62, 87, - -106, -65, -75, -69, - -13, 34, 99, 59, - 83, 98, 44, 0, - 24, 18, 17, 70, - -22, 194, 208, 144, - -79, -15, 32, -104, - -28, -105, -186, -212, - -228, -79, -76, 51, - -71, 72, 118, -34, - -3, -171, 5, 2, - -108, -125, 62, -58, - 58, -121, 73, -466, - 92, 63, -94, -78, - -76, 212, 36, -225, - -71, -354, 152, 143, - -79, -246, -51, -31, - -6, -270, 240, 210, - 30, -157, -231, 74, - -146, 88, -273, 156, - 92, 56, 71, 2, - 318, 164, 32, -110, - -35, -41, -95, -106, - 11, 132, -68, 55, - 123, -83, -149, 212, - 132, 0, -194, 55, - 206, -108, -353, 289, - -195, 1, 233, -22, - -60, 20, 26, 68, - 166, 27, -58, 130, - 112, 107, 27, -165, - 115, -93, -37, 38, - 83, 483, 65, -229, - -13, 157, 85, 50, - 136, 10, 32, 83, - 82, 55, 5, -9, - -52, -78, -81, -51, - 40, 18, -127, -224, - -41, 53, -210, -113, - 24, -17, -187, -89, - 8, 121, 83, 77, - 91, -74, -35, -112, - -161, -173, 102, 132, - -125, -61, 103, -260, - 52, 166, -32, -156, - -87, -56, 60, -70, - -124, 242, 114, -251, - -166, 201, 127, 28, - -11, 23, -80, -115, - -20, -51, -348, 340, - -34, 133, 13, 92, - -124, -136, -120, -26, - -6, 17, 28, 21, - 120, -168, 160, -35, - 115, 28, 9, 7, - -56, 39, 156, 256, - -18, 1, 277, 82, - -70, -144, -88, -13, - -59, -157, 8, -134, - 21, -40, 58, -21, - 194, -276, 97, 279, - -56, -140, 125, 57, - -184, -204, -70, -2, - 128, -202, -78, 230, - -23, 161, -102, 1, - 1, 180, -31, -86, - -167, -57, -60, 27, - -13, 99, 108, 111, - 76, 69, 34, -21, - 53, 38, 34, 78, - 73, 219, 51, 15, - -72, -103, -207, 30, - 213, -14, 31, -94, - -40, -144, 67, 4, - 105, 59, -240, 25, - 244, 69, 58, 23, - -24, -5, -15, -133, - -71, -67, 181, 29, - -45, 121, 96, 51, - -72, -53, 56, -153, - -27, 85, 183, 211, - 105, -34, -46, 43, - -72, -93, 36, -128, - 29, 111, -95, -156, - -179, -235, 21, -39, - -71, -33, -61, -252, - 230, -131, 157, -21, - -85, -28, -123, 80, - -160, 63, 47, -6, - -49, -96, -19, 17, - -58, 17, -0, -13, - -170, 25, -35, 59, - 10, -31, -413, 81, - 62, 18, -164, 245, - 92, -165, 42, 26, - 126, -248, 193, -55, - 16, 39, 14, 50 -}; - -const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB1_16_Stage_info[ NLSF_MSVQ_CB1_16_STAGES ] = -{ - { 32, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 0 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 0 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 32 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 32 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 40 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 40 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 48 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 48 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 56 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 56 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 64 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 64 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 72 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 72 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 80 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 80 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 88 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 88 ] }, - { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 96 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 96 ] } -}; - -const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB1_16 = -{ - NLSF_MSVQ_CB1_16_STAGES, - SKP_Silk_NLSF_CB1_16_Stage_info, - SKP_Silk_NLSF_MSVQ_CB1_16_ndelta_min_Q15, - SKP_Silk_NLSF_MSVQ_CB1_16_CDF, - SKP_Silk_NLSF_MSVQ_CB1_16_CDF_start_ptr, - SKP_Silk_NLSF_MSVQ_CB1_16_CDF_middle_idx -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +/**********************************************/ +/* This file has been automatically generated */ +/* */ +/* ROM usage: 134+1785 Words */ +/**********************************************/ + +#include "SKP_Silk_structs.h" +#include "SKP_Silk_tables_NLSF_CB1_16.h" + +const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ NLSF_MSVQ_CB1_16_VECTORS + NLSF_MSVQ_CB1_16_STAGES ] = +{ + 0, + 19099, + 26957, + 30639, + 34242, + 37546, + 40447, + 43287, + 46005, + 48445, + 49865, + 51284, + 52673, + 53975, + 55221, + 56441, + 57267, + 58025, + 58648, + 59232, + 59768, + 60248, + 60729, + 61210, + 61690, + 62171, + 62651, + 63132, + 63613, + 64093, + 64574, + 65054, + 65535, + 0, + 28808, + 38775, + 46801, + 51785, + 55886, + 59410, + 62572, + 65535, + 0, + 27376, + 38639, + 45052, + 51465, + 55448, + 59021, + 62594, + 65535, + 0, + 33403, + 39569, + 45102, + 49961, + 54047, + 57959, + 61788, + 65535, + 0, + 25851, + 43356, + 47828, + 52204, + 55964, + 59413, + 62507, + 65535, + 0, + 34277, + 40337, + 45432, + 50311, + 54326, + 58171, + 61853, + 65535, + 0, + 33538, + 39865, + 45302, + 50076, + 54549, + 58478, + 62159, + 65535, + 0, + 27445, + 35258, + 40665, + 46072, + 51362, + 56540, + 61086, + 65535, + 0, + 22080, + 30779, + 37065, + 43085, + 48849, + 54613, + 60133, + 65535, + 0, + 13417, + 21748, + 30078, + 38231, + 46383, + 53091, + 59515, + 65535 +}; + +const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_16_CDF_start_ptr[ NLSF_MSVQ_CB1_16_STAGES ] = +{ + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 0 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 33 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 42 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 51 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 60 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 69 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 78 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 87 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 96 ], + &SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ 105 ] +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB1_16_CDF_middle_idx[ NLSF_MSVQ_CB1_16_STAGES ] = +{ + 5, + 2, + 2, + 2, + 2, + 2, + 2, + 3, + 3, + 4 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ NLSF_MSVQ_CB1_16_VECTORS ] = +{ + 57, 98, + 133, 134, + 138, 144, + 145, 147, + 152, 177, + 177, 178, + 181, 183, + 184, 202, + 206, 215, + 218, 222, + 227, 227, + 227, 227, + 227, 227, + 227, 227, + 227, 227, + 227, 227, + 38, 87, + 97, 119, + 128, 135, + 140, 143, + 40, 81, + 107, 107, + 129, 134, + 134, 143, + 31, 109, + 114, 120, + 128, 130, + 131, 132, + 43, 61, + 124, 125, + 132, 136, + 141, 142, + 30, 110, + 118, 120, + 129, 131, + 133, 133, + 31, 108, + 115, 121, + 124, 130, + 133, 137, + 40, 98, + 115, 115, + 116, 117, + 123, 124, + 50, 93, + 108, 110, + 112, 112, + 114, 115, + 73, 95, + 95, 96, + 96, 105, + 107, 110 +}; + +const SKP_int SKP_Silk_NLSF_MSVQ_CB1_16_ndelta_min_Q15[ 16 + 1 ] = +{ + 148, + 3, + 60, + 68, + 117, + 86, + 121, + 124, + 152, + 153, + 207, + 151, + 225, + 239, + 126, + 183, + 792 +}; + +const SKP_int16 SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * NLSF_MSVQ_CB1_16_VECTORS ] = +{ + 1309, 3060, 5071, 6996, + 9028, 10938, 12934, 14891, + 16933, 18854, 20792, 22764, + 24753, 26659, 28626, 30501, + 1264, 2745, 4610, 6408, + 8286, 10043, 12084, 14108, + 16118, 18163, 20095, 22164, + 24264, 26316, 28329, 30251, + 1044, 2080, 3672, 5179, + 7140, 9100, 11070, 13065, + 15423, 17790, 19931, 22101, + 24290, 26361, 28499, 30418, + 1131, 2476, 4478, 6149, + 7902, 9875, 11938, 13809, + 15869, 17730, 19948, 21707, + 23761, 25535, 27426, 28917, + 1040, 2004, 4026, 6100, + 8432, 10494, 12610, 14694, + 16797, 18775, 20799, 22782, + 24772, 26682, 28631, 30516, + 2310, 3812, 5913, 7933, + 10033, 11881, 13885, 15798, + 17751, 19576, 21482, 23276, + 25157, 27010, 28833, 30623, + 1254, 2847, 5013, 6781, + 8626, 10370, 12726, 14633, + 16281, 17852, 19870, 21472, + 23002, 24629, 26710, 27960, + 1468, 3059, 4987, 7026, + 8741, 10412, 12281, 14020, + 15970, 17723, 19640, 21522, + 23472, 25661, 27986, 30225, + 2171, 3566, 5605, 7384, + 9404, 11220, 13030, 14758, + 16687, 18417, 20346, 22091, + 24055, 26212, 28356, 30397, + 2409, 4676, 7543, 9786, + 11419, 12935, 14368, 15653, + 17366, 18943, 20762, 22477, + 24440, 26327, 28284, 30242, + 2354, 4222, 6820, 9107, + 11596, 13934, 15973, 17682, + 19158, 20517, 21991, 23420, + 25178, 26936, 28794, 30527, + 1323, 2414, 4184, 6039, + 7534, 9398, 11099, 13097, + 14799, 16451, 18434, 20887, + 23490, 25838, 28046, 30225, + 1361, 3243, 6048, 8511, + 11001, 13145, 15073, 16608, + 18126, 19381, 20912, 22607, + 24660, 26668, 28663, 30566, + 1216, 2648, 5901, 8422, + 10037, 11425, 12973, 14603, + 16686, 18600, 20555, 22415, + 24450, 26280, 28206, 30077, + 2417, 4048, 6316, 8433, + 10510, 12757, 15072, 17295, + 19573, 21503, 23329, 24782, + 26235, 27689, 29214, 30819, + 1012, 2345, 4991, 7377, + 9465, 11916, 14296, 16566, + 18672, 20544, 22292, 23838, + 25415, 27050, 28848, 30551, + 1937, 3693, 6267, 8019, + 10372, 12194, 14287, 15657, + 17431, 18864, 20769, 22206, + 24037, 25463, 27383, 28602, + 1969, 3305, 5017, 6726, + 8375, 9993, 11634, 13280, + 15078, 16751, 18464, 20119, + 21959, 23858, 26224, 29298, + 1198, 2647, 5428, 7423, + 9775, 12155, 14665, 16344, + 18121, 19790, 21557, 22847, + 24484, 25742, 27639, 28711, + 1636, 3353, 5447, 7597, + 9837, 11647, 13964, 16019, + 17862, 20116, 22319, 24037, + 25966, 28086, 29914, 31294, + 2676, 4105, 6378, 8223, + 10058, 11549, 13072, 14453, + 15956, 17355, 18931, 20402, + 22183, 23884, 25717, 27723, + 1373, 2593, 4449, 5633, + 7300, 8425, 9474, 10818, + 12769, 15722, 19002, 21429, + 23682, 25924, 28135, 30333, + 1596, 3183, 5378, 7164, + 8670, 10105, 11470, 12834, + 13991, 15042, 16642, 17903, + 20759, 25283, 27770, 30240, + 2037, 3987, 6237, 8117, + 9954, 12245, 14217, 15892, + 17775, 20114, 22314, 25942, + 26305, 26483, 26796, 28561, + 2181, 3858, 5760, 7924, + 10041, 11577, 13769, 15700, + 17429, 19879, 23583, 24538, + 25212, 25693, 28688, 30507, + 1992, 3882, 6474, 7883, + 9381, 12672, 14340, 15701, + 16658, 17832, 20850, 22885, + 24677, 26457, 28491, 30460, + 2391, 3988, 5448, 7432, + 11014, 12579, 13140, 14146, + 15898, 18592, 21104, 22993, + 24673, 27186, 28142, 29612, + 1713, 5102, 6989, 7798, + 8670, 10110, 12746, 14881, + 16709, 18407, 20126, 22107, + 24181, 26198, 28237, 30137, + 1612, 3617, 6148, 8359, + 9576, 11528, 14936, 17809, + 18287, 18729, 19001, 21111, + 24631, 26596, 28740, 30643, + 2266, 4168, 7862, 9546, + 9618, 9703, 10134, 13897, + 16265, 18432, 20587, 22605, + 24754, 26994, 29125, 30840, + 1840, 3917, 6272, 7809, + 9714, 11438, 13767, 15799, + 19244, 21972, 22980, 23180, + 23723, 25650, 29117, 31085, + 1458, 3612, 6008, 7488, + 9827, 11893, 14086, 15734, + 17440, 19535, 22424, 24767, + 29246, 29928, 30516, 30947, + -102, -121, -31, -6, + 5, -2, 8, -18, + -4, 6, 14, -2, + -12, -16, -12, -60, + -126, -353, -574, -677, + -657, -617, -498, -393, + -348, -277, -225, -164, + -102, -70, -31, 33, + 4, 379, 387, 551, + 605, 620, 532, 482, + 442, 454, 385, 347, + 322, 299, 266, 200, + 1168, 951, 672, 246, + 60, -161, -259, -234, + -253, -282, -203, -187, + -155, -176, -198, -178, + 10, 170, 393, 609, + 555, 208, -330, -571, + -769, -633, -319, -43, + 95, 105, 106, 116, + -152, -140, -125, 5, + 173, 274, 264, 331, + -37, -293, -609, -786, + -959, -814, -645, -238, + -91, 36, -11, -101, + -279, -227, -40, 90, + 530, 677, 890, 1104, + 999, 835, 564, 295, + -280, -364, -340, -331, + -284, 288, 761, 880, + 988, 627, 146, -226, + -203, -181, -142, 39, + 24, -26, -107, -92, + -161, -135, -131, -88, + -160, -156, -75, -43, + -36, -6, -33, 33, + -324, -415, -108, 124, + 157, 191, 203, 197, + 144, 109, 152, 176, + 190, 122, 101, 159, + 663, 668, 480, 400, + 379, 444, 446, 458, + 343, 351, 310, 228, + 133, 44, 75, 63, + -84, 39, -29, 35, + -94, -233, -261, -354, + 77, 262, -24, -145, + -333, -409, -404, -597, + -488, -300, 910, 592, + 412, 120, 130, -51, + -37, -77, -172, -181, + -159, -148, -72, -62, + 510, 516, 113, -585, + -1075, -957, -417, -195, + 9, 7, -88, -173, + -91, 54, 98, 95, + -28, 197, -527, -621, + 157, 122, -168, 147, + 309, 300, 336, 315, + 396, 408, 376, 106, + -162, -170, -315, 98, + 821, 908, 570, -33, + -312, -568, -572, -378, + -107, 23, 156, 93, + -129, -87, 20, -72, + -37, 40, 21, 27, + 48, 75, 77, 65, + 46, 71, 66, 47, + 136, 344, 236, 322, + 170, 283, 269, 291, + 162, -43, -204, -259, + -240, -305, -350, -312, + 447, 348, 345, 257, + 71, -131, -77, -190, + -202, -40, 35, 133, + 261, 365, 438, 303, + -8, 22, 140, 137, + -300, -641, -764, -268, + -23, -25, 73, -162, + -150, -212, -72, 6, + 39, 78, 104, -93, + -308, -136, 117, -71, + -513, -820, -700, -450, + -161, -23, 29, 78, + 337, 106, -406, -782, + -112, 233, 383, 62, + -126, 6, -77, -29, + -146, -123, -51, -27, + -27, -381, -641, 402, + 539, 8, -207, -366, + -36, -27, -204, -227, + -237, -189, -64, 51, + -92, -137, -281, 62, + 233, 92, 148, 294, + 363, 416, 564, 625, + 370, -36, -469, -462, + 102, 168, 32, 117, + -21, 97, 139, 89, + 104, 35, 4, 82, + 66, 58, 73, 93, + -76, -320, -236, -189, + -203, -142, -27, -73, + 9, -9, -25, 12, + -15, 4, 4, -50, + 314, 180, 162, -49, + 199, -108, -227, -66, + -447, -67, -264, -394, + 5, 55, -133, -176, + -116, -241, 272, 109, + 282, 262, 192, -64, + -392, -514, 156, 203, + 154, 72, -34, -160, + -73, 3, -33, -431, + 321, 18, -567, -590, + -108, 88, 66, 51, + -31, -193, -46, 65, + -29, -23, 215, -31, + 101, -113, 32, 304, + 88, 320, 448, 5, + -439, -562, -508, -135, + -13, -171, -8, 182, + -99, -181, -149, 376, + 476, 64, -396, -652, + -150, 176, 222, 65, + -590, 719, 271, 399, + 245, 72, -156, -152, + -176, 59, 94, 125, + -9, -7, 9, 1, + -61, -116, -82, 1, + 79, 22, -44, -15, + -48, -65, -62, -101, + -102, -54, -70, -78, + -80, -25, 398, 71, + 139, 38, 90, 194, + 222, 249, 165, 94, + 221, 262, 163, 91, + -206, 573, 200, -287, + -147, 5, -18, -85, + -74, -125, -87, 85, + 141, 4, -4, 28, + 234, 48, -150, -111, + -506, 237, -209, 345, + 94, -124, 77, 121, + 143, 12, -80, -48, + 191, 144, -93, -65, + -151, -643, 435, 106, + 87, 7, 65, 102, + 94, 68, 5, 99, + 222, 93, 94, 355, + -13, -89, -228, -503, + 287, 109, 108, 449, + 253, -29, -109, -116, + 15, -73, -20, 131, + -147, 72, 59, -150, + -594, 273, 316, 132, + 199, 106, 198, 212, + 220, 82, 45, -13, + 223, 137, 270, 38, + 252, 135, -177, -207, + -360, -102, 403, 406, + -14, 83, 64, 51, + -7, -99, -97, -88, + -124, -65, 42, 32, + 28, 29, 12, 20, + 119, -26, -212, -201, + 373, 251, 141, 103, + 36, -52, 66, 18, + -6, -95, -196, 5, + 98, -85, -108, 218, + -164, 20, 356, 172, + 37, 266, 23, 112, + -24, -99, -92, -178, + 29, -278, 388, -60, + -220, 300, -13, 154, + 191, 15, -37, -110, + -153, -150, -114, -7, + -94, -31, -62, -177, + 4, -70, 35, 453, + 147, -247, -328, 101, + 20, -114, 147, 108, + -119, -109, -102, -238, + 55, -102, 173, -89, + 129, 138, -330, -160, + 485, 154, -59, -170, + -20, -34, -261, -40, + -129, 77, -84, 69, + 83, 160, 169, 63, + -516, 30, 336, 52, + -0, -52, -124, 158, + 19, 197, -10, -375, + 405, 285, 114, -395, + -47, 196, 62, 87, + -106, -65, -75, -69, + -13, 34, 99, 59, + 83, 98, 44, 0, + 24, 18, 17, 70, + -22, 194, 208, 144, + -79, -15, 32, -104, + -28, -105, -186, -212, + -228, -79, -76, 51, + -71, 72, 118, -34, + -3, -171, 5, 2, + -108, -125, 62, -58, + 58, -121, 73, -466, + 92, 63, -94, -78, + -76, 212, 36, -225, + -71, -354, 152, 143, + -79, -246, -51, -31, + -6, -270, 240, 210, + 30, -157, -231, 74, + -146, 88, -273, 156, + 92, 56, 71, 2, + 318, 164, 32, -110, + -35, -41, -95, -106, + 11, 132, -68, 55, + 123, -83, -149, 212, + 132, 0, -194, 55, + 206, -108, -353, 289, + -195, 1, 233, -22, + -60, 20, 26, 68, + 166, 27, -58, 130, + 112, 107, 27, -165, + 115, -93, -37, 38, + 83, 483, 65, -229, + -13, 157, 85, 50, + 136, 10, 32, 83, + 82, 55, 5, -9, + -52, -78, -81, -51, + 40, 18, -127, -224, + -41, 53, -210, -113, + 24, -17, -187, -89, + 8, 121, 83, 77, + 91, -74, -35, -112, + -161, -173, 102, 132, + -125, -61, 103, -260, + 52, 166, -32, -156, + -87, -56, 60, -70, + -124, 242, 114, -251, + -166, 201, 127, 28, + -11, 23, -80, -115, + -20, -51, -348, 340, + -34, 133, 13, 92, + -124, -136, -120, -26, + -6, 17, 28, 21, + 120, -168, 160, -35, + 115, 28, 9, 7, + -56, 39, 156, 256, + -18, 1, 277, 82, + -70, -144, -88, -13, + -59, -157, 8, -134, + 21, -40, 58, -21, + 194, -276, 97, 279, + -56, -140, 125, 57, + -184, -204, -70, -2, + 128, -202, -78, 230, + -23, 161, -102, 1, + 1, 180, -31, -86, + -167, -57, -60, 27, + -13, 99, 108, 111, + 76, 69, 34, -21, + 53, 38, 34, 78, + 73, 219, 51, 15, + -72, -103, -207, 30, + 213, -14, 31, -94, + -40, -144, 67, 4, + 105, 59, -240, 25, + 244, 69, 58, 23, + -24, -5, -15, -133, + -71, -67, 181, 29, + -45, 121, 96, 51, + -72, -53, 56, -153, + -27, 85, 183, 211, + 105, -34, -46, 43, + -72, -93, 36, -128, + 29, 111, -95, -156, + -179, -235, 21, -39, + -71, -33, -61, -252, + 230, -131, 157, -21, + -85, -28, -123, 80, + -160, 63, 47, -6, + -49, -96, -19, 17, + -58, 17, -0, -13, + -170, 25, -35, 59, + 10, -31, -413, 81, + 62, 18, -164, 245, + 92, -165, 42, 26, + 126, -248, 193, -55, + 16, 39, 14, 50 +}; + +const SKP_Silk_NLSF_CBS SKP_Silk_NLSF_CB1_16_Stage_info[ NLSF_MSVQ_CB1_16_STAGES ] = +{ + { 32, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 0 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 0 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 32 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 32 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 40 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 40 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 48 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 48 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 56 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 56 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 64 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 64 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 72 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 72 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 80 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 80 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 88 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 88 ] }, + { 8, &SKP_Silk_NLSF_MSVQ_CB1_16_Q15[ 16 * 96 ], &SKP_Silk_NLSF_MSVQ_CB1_16_rates_Q5[ 96 ] } +}; + +const SKP_Silk_NLSF_CB_struct SKP_Silk_NLSF_CB1_16 = +{ + NLSF_MSVQ_CB1_16_STAGES, + SKP_Silk_NLSF_CB1_16_Stage_info, + SKP_Silk_NLSF_MSVQ_CB1_16_ndelta_min_Q15, + SKP_Silk_NLSF_MSVQ_CB1_16_CDF, + SKP_Silk_NLSF_MSVQ_CB1_16_CDF_start_ptr, + SKP_Silk_NLSF_MSVQ_CB1_16_CDF_middle_idx +}; + diff --git a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.h b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.h similarity index 97% rename from jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.h rename to app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.h index 00d3928..9b835ce 100644 --- a/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.h +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_NLSF_CB1_16.h @@ -1,51 +1,51 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#ifndef SKP_SILK_TABLES_NLSF_CB1_16_H -#define SKP_SILK_TABLES_NLSF_CB1_16_H - -#include "SKP_Silk_define.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define NLSF_MSVQ_CB1_16_STAGES 10 -#define NLSF_MSVQ_CB1_16_VECTORS 104 - -/* NLSF codebook entropy coding tables */ -extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ NLSF_MSVQ_CB1_16_VECTORS + NLSF_MSVQ_CB1_16_STAGES ]; -extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_16_CDF_start_ptr[ NLSF_MSVQ_CB1_16_STAGES ]; -extern const SKP_int SKP_Silk_NLSF_MSVQ_CB1_16_CDF_middle_idx[ NLSF_MSVQ_CB1_16_STAGES ]; - -#ifdef __cplusplus -} -#endif - -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#ifndef SKP_SILK_TABLES_NLSF_CB1_16_H +#define SKP_SILK_TABLES_NLSF_CB1_16_H + +#include "SKP_Silk_define.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define NLSF_MSVQ_CB1_16_STAGES 10 +#define NLSF_MSVQ_CB1_16_VECTORS 104 + +/* NLSF codebook entropy coding tables */ +extern const SKP_uint16 SKP_Silk_NLSF_MSVQ_CB1_16_CDF[ NLSF_MSVQ_CB1_16_VECTORS + NLSF_MSVQ_CB1_16_STAGES ]; +extern const SKP_uint16 * const SKP_Silk_NLSF_MSVQ_CB1_16_CDF_start_ptr[ NLSF_MSVQ_CB1_16_STAGES ]; +extern const SKP_int SKP_Silk_NLSF_MSVQ_CB1_16_CDF_middle_idx[ NLSF_MSVQ_CB1_16_STAGES ]; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/jni/silk/src/SKP_Silk_tables_gain.c b/app/src/main/jni/silk/src/SKP_Silk_tables_gain.c similarity index 97% rename from jni/silk/src/SKP_Silk_tables_gain.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_gain.c index 673d837..1318b2e 100644 --- a/jni/silk/src/SKP_Silk_tables_gain.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_gain.c @@ -1,77 +1,77 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_tables.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -const SKP_uint16 SKP_Silk_gain_CDF[ 2 ][ 65 ] = -{ -{ - 0, 18, 45, 94, 181, 320, 519, 777, - 1093, 1468, 1909, 2417, 2997, 3657, 4404, 5245, - 6185, 7228, 8384, 9664, 11069, 12596, 14244, 16022, - 17937, 19979, 22121, 24345, 26646, 29021, 31454, 33927, - 36438, 38982, 41538, 44068, 46532, 48904, 51160, 53265, - 55184, 56904, 58422, 59739, 60858, 61793, 62568, 63210, - 63738, 64165, 64504, 64769, 64976, 65133, 65249, 65330, - 65386, 65424, 65451, 65471, 65487, 65501, 65513, 65524, - 65535 -}, -{ - 0, 214, 581, 1261, 2376, 3920, 5742, 7632, - 9449, 11157, 12780, 14352, 15897, 17427, 18949, 20462, - 21957, 23430, 24889, 26342, 27780, 29191, 30575, 31952, - 33345, 34763, 36200, 37642, 39083, 40519, 41930, 43291, - 44602, 45885, 47154, 48402, 49619, 50805, 51959, 53069, - 54127, 55140, 56128, 57101, 58056, 58979, 59859, 60692, - 61468, 62177, 62812, 63368, 63845, 64242, 64563, 64818, - 65023, 65184, 65306, 65391, 65447, 65482, 65505, 65521, - 65535 -} -}; - -const SKP_int SKP_Silk_gain_CDF_offset = 32; - - -const SKP_uint16 SKP_Silk_delta_gain_CDF[ 46 ] = { - 0, 2358, 3856, 7023, 15376, 53058, 59135, 61555, - 62784, 63498, 63949, 64265, 64478, 64647, 64783, 64894, - 64986, 65052, 65113, 65169, 65213, 65252, 65284, 65314, - 65338, 65359, 65377, 65392, 65403, 65415, 65424, 65432, - 65440, 65448, 65455, 65462, 65470, 65477, 65484, 65491, - 65499, 65506, 65513, 65521, 65528, 65535 -}; - -const SKP_int SKP_Silk_delta_gain_CDF_offset = 5; - -#ifdef __cplusplus -} -#endif +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_tables.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +const SKP_uint16 SKP_Silk_gain_CDF[ 2 ][ 65 ] = +{ +{ + 0, 18, 45, 94, 181, 320, 519, 777, + 1093, 1468, 1909, 2417, 2997, 3657, 4404, 5245, + 6185, 7228, 8384, 9664, 11069, 12596, 14244, 16022, + 17937, 19979, 22121, 24345, 26646, 29021, 31454, 33927, + 36438, 38982, 41538, 44068, 46532, 48904, 51160, 53265, + 55184, 56904, 58422, 59739, 60858, 61793, 62568, 63210, + 63738, 64165, 64504, 64769, 64976, 65133, 65249, 65330, + 65386, 65424, 65451, 65471, 65487, 65501, 65513, 65524, + 65535 +}, +{ + 0, 214, 581, 1261, 2376, 3920, 5742, 7632, + 9449, 11157, 12780, 14352, 15897, 17427, 18949, 20462, + 21957, 23430, 24889, 26342, 27780, 29191, 30575, 31952, + 33345, 34763, 36200, 37642, 39083, 40519, 41930, 43291, + 44602, 45885, 47154, 48402, 49619, 50805, 51959, 53069, + 54127, 55140, 56128, 57101, 58056, 58979, 59859, 60692, + 61468, 62177, 62812, 63368, 63845, 64242, 64563, 64818, + 65023, 65184, 65306, 65391, 65447, 65482, 65505, 65521, + 65535 +} +}; + +const SKP_int SKP_Silk_gain_CDF_offset = 32; + + +const SKP_uint16 SKP_Silk_delta_gain_CDF[ 46 ] = { + 0, 2358, 3856, 7023, 15376, 53058, 59135, 61555, + 62784, 63498, 63949, 64265, 64478, 64647, 64783, 64894, + 64986, 65052, 65113, 65169, 65213, 65252, 65284, 65314, + 65338, 65359, 65377, 65392, 65403, 65415, 65424, 65432, + 65440, 65448, 65455, 65462, 65470, 65477, 65484, 65491, + 65499, 65506, 65513, 65521, 65528, 65535 +}; + +const SKP_int SKP_Silk_delta_gain_CDF_offset = 5; + +#ifdef __cplusplus +} +#endif diff --git a/jni/silk/src/SKP_Silk_tables_other.c b/app/src/main/jni/silk/src/SKP_Silk_tables_other.c similarity index 97% rename from jni/silk/src/SKP_Silk_tables_other.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_other.c index 48f57cb..ae6f95d 100644 --- a/jni/silk/src/SKP_Silk_tables_other.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_other.c @@ -1,151 +1,151 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_structs.h" -#include "SKP_Silk_define.h" -#include "SKP_Silk_tables.h" -#ifdef __cplusplus -extern "C" -{ -#endif - -/* Piece-wise linear mapping from bitrate in kbps to coding quality in dB SNR */ -const SKP_int32 TargetRate_table_NB[ TARGET_RATE_TAB_SZ ] = { - 0, 8000, 9000, 11000, 13000, 16000, 22000, 100000 -}; -const SKP_int32 TargetRate_table_MB[ TARGET_RATE_TAB_SZ ] = { - 0, 10000, 12000, 14000, 17000, 21000, 28000, 100000 -}; -const SKP_int32 TargetRate_table_WB[ TARGET_RATE_TAB_SZ ] = { - 0, 11000, 14000, 17000, 21000, 26000, 36000, 100000 -}; -const SKP_int32 TargetRate_table_SWB[ TARGET_RATE_TAB_SZ ] = { - 0, 13000, 16000, 19000, 25000, 32000, 46000, 100000 -}; -const SKP_int32 SNR_table_Q1[ TARGET_RATE_TAB_SZ ] = { - 19, 31, 35, 39, 43, 47, 54, 59 -}; - -const SKP_int32 SNR_table_one_bit_per_sample_Q7[ 4 ] = { - 1984, 2240, 2408, 2708 -}; - -/* Filter coeficicnts for HP filter: 4. Order filter implementad as two biquad filters */ -const SKP_int16 SKP_Silk_SWB_detect_B_HP_Q13[ NB_SOS ][ 3 ] = { - //{400, -550, 400}, {400, 130, 400}, {400, 390, 400} - {575, -948, 575}, {575, -221, 575}, {575, 104, 575} -}; -const SKP_int16 SKP_Silk_SWB_detect_A_HP_Q13[ NB_SOS ][ 2 ] = { - {14613, 6868}, {12883, 7337}, {11586, 7911} - //{14880, 6900}, {14400, 7300}, {13700, 7800} -}; - -/* Decoder high-pass filter coefficients for 24 kHz sampling, -6 dB @ 44 Hz */ -const SKP_int16 SKP_Silk_Dec_A_HP_24[ DEC_HP_ORDER ] = {-16220, 8030}; // second order AR coefs, Q13 -const SKP_int16 SKP_Silk_Dec_B_HP_24[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 - -/* Decoder high-pass filter coefficients for 16 kHz sampling, - 6 dB @ 46 Hz */ -const SKP_int16 SKP_Silk_Dec_A_HP_16[ DEC_HP_ORDER ] = {-16127, 7940}; // second order AR coefs, Q13 -const SKP_int16 SKP_Silk_Dec_B_HP_16[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 - -/* Decoder high-pass filter coefficients for 12 kHz sampling, -6 dB @ 44 Hz */ -const SKP_int16 SKP_Silk_Dec_A_HP_12[ DEC_HP_ORDER ] = {-16043, 7859}; // second order AR coefs, Q13 -const SKP_int16 SKP_Silk_Dec_B_HP_12[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 - -/* Decoder high-pass filter coefficients for 8 kHz sampling, -6 dB @ 43 Hz */ -const SKP_int16 SKP_Silk_Dec_A_HP_8[ DEC_HP_ORDER ] = {-15885, 7710}; // second order AR coefs, Q13 -const SKP_int16 SKP_Silk_Dec_B_HP_8[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 - -/* table for LSB coding */ -const SKP_uint16 SKP_Silk_lsb_CDF[ 3 ] = {0, 40000, 65535}; - -/* tables for LTPScale */ -const SKP_uint16 SKP_Silk_LTPscale_CDF[ 4 ] = {0, 32000, 48000, 65535}; -const SKP_int SKP_Silk_LTPscale_offset = 2; - -/* tables for VAD flag */ -const SKP_uint16 SKP_Silk_vadflag_CDF[ 3 ] = {0, 22000, 65535}; // 66% for speech, 33% for no speech -const SKP_int SKP_Silk_vadflag_offset = 1; - -/* tables for sampling rate */ -const SKP_int SKP_Silk_SamplingRates_table[ 4 ] = {8, 12, 16, 24}; -const SKP_uint16 SKP_Silk_SamplingRates_CDF[ 5 ] = {0, 16000, 32000, 48000, 65535}; -const SKP_int SKP_Silk_SamplingRates_offset = 2; - -/* tables for NLSF interpolation factor */ -const SKP_uint16 SKP_Silk_NLSF_interpolation_factor_CDF[ 6 ] = {0, 3706, 8703, 19226, 30926, 65535}; -const SKP_int SKP_Silk_NLSF_interpolation_factor_offset = 4; - -/* Table for frame termination indication */ -const SKP_uint16 SKP_Silk_FrameTermination_CDF[ 5 ] = {0, 20000, 45000, 56000, 65535}; -const SKP_int SKP_Silk_FrameTermination_offset = 2; - -const SKP_uint16 SKP_Silk_FrameTermination_v4_CDF[ 6 ] = {0, 13107, 26214, 39321, 52428, 65535}; -const SKP_int SKP_Silk_FrameTermination_v4_offset = 4; - -/* Table for random seed */ -const SKP_uint16 SKP_Silk_Seed_CDF[ 5 ] = {0, 16384, 32768, 49152, 65535}; -const SKP_int SKP_Silk_Seed_offset = 2; - -/* Quantization offsets */ -const SKP_int16 SKP_Silk_Quantization_Offsets_Q10[ 2 ][ 2 ] = { - { OFFSET_VL_Q10, OFFSET_VH_Q10 }, { OFFSET_UVL_Q10, OFFSET_UVH_Q10 } -}; - -/* Table for LTPScale */ -const SKP_int16 SKP_Silk_LTPScales_table_Q14[ 3 ] = { 15565, 11469, 8192 }; - -#if SWITCH_TRANSITION_FILTERING -/* Elliptic/Cauer filters designed with 0.1 dB passband ripple, - 80 dB minimum stopband attenuation, and - [0.95 : 0.15 : 0.35] normalized cut off frequencies. */ - -/* Interpolation points for filter coefficients used in the bandwidth transition smoother */ -const SKP_int32 SKP_Silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NB ] = -{ -{ 250767114, 501534038, 250767114 }, -{ 209867381, 419732057, 209867381 }, -{ 170987846, 341967853, 170987846 }, -{ 131531482, 263046905, 131531482 }, -{ 89306658, 178584282, 89306658 } -}; - -/* Interpolation points for filter coefficients used in the bandwidth transition smoother */ -const SKP_int32 SKP_Silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NA ] = -{ -{ 506393414, 239854379 }, -{ 411067935, 169683996 }, -{ 306733530, 116694253 }, -{ 185807084, 77959395 }, -{ 35497197, 57401098 } -}; -#endif - -#ifdef __cplusplus -} -#endif - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_structs.h" +#include "SKP_Silk_define.h" +#include "SKP_Silk_tables.h" +#ifdef __cplusplus +extern "C" +{ +#endif + +/* Piece-wise linear mapping from bitrate in kbps to coding quality in dB SNR */ +const SKP_int32 TargetRate_table_NB[ TARGET_RATE_TAB_SZ ] = { + 0, 8000, 9000, 11000, 13000, 16000, 22000, 100000 +}; +const SKP_int32 TargetRate_table_MB[ TARGET_RATE_TAB_SZ ] = { + 0, 10000, 12000, 14000, 17000, 21000, 28000, 100000 +}; +const SKP_int32 TargetRate_table_WB[ TARGET_RATE_TAB_SZ ] = { + 0, 11000, 14000, 17000, 21000, 26000, 36000, 100000 +}; +const SKP_int32 TargetRate_table_SWB[ TARGET_RATE_TAB_SZ ] = { + 0, 13000, 16000, 19000, 25000, 32000, 46000, 100000 +}; +const SKP_int32 SNR_table_Q1[ TARGET_RATE_TAB_SZ ] = { + 19, 31, 35, 39, 43, 47, 54, 59 +}; + +const SKP_int32 SNR_table_one_bit_per_sample_Q7[ 4 ] = { + 1984, 2240, 2408, 2708 +}; + +/* Filter coeficicnts for HP filter: 4. Order filter implementad as two biquad filters */ +const SKP_int16 SKP_Silk_SWB_detect_B_HP_Q13[ NB_SOS ][ 3 ] = { + //{400, -550, 400}, {400, 130, 400}, {400, 390, 400} + {575, -948, 575}, {575, -221, 575}, {575, 104, 575} +}; +const SKP_int16 SKP_Silk_SWB_detect_A_HP_Q13[ NB_SOS ][ 2 ] = { + {14613, 6868}, {12883, 7337}, {11586, 7911} + //{14880, 6900}, {14400, 7300}, {13700, 7800} +}; + +/* Decoder high-pass filter coefficients for 24 kHz sampling, -6 dB @ 44 Hz */ +const SKP_int16 SKP_Silk_Dec_A_HP_24[ DEC_HP_ORDER ] = {-16220, 8030}; // second order AR coefs, Q13 +const SKP_int16 SKP_Silk_Dec_B_HP_24[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 + +/* Decoder high-pass filter coefficients for 16 kHz sampling, - 6 dB @ 46 Hz */ +const SKP_int16 SKP_Silk_Dec_A_HP_16[ DEC_HP_ORDER ] = {-16127, 7940}; // second order AR coefs, Q13 +const SKP_int16 SKP_Silk_Dec_B_HP_16[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 + +/* Decoder high-pass filter coefficients for 12 kHz sampling, -6 dB @ 44 Hz */ +const SKP_int16 SKP_Silk_Dec_A_HP_12[ DEC_HP_ORDER ] = {-16043, 7859}; // second order AR coefs, Q13 +const SKP_int16 SKP_Silk_Dec_B_HP_12[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 + +/* Decoder high-pass filter coefficients for 8 kHz sampling, -6 dB @ 43 Hz */ +const SKP_int16 SKP_Silk_Dec_A_HP_8[ DEC_HP_ORDER ] = {-15885, 7710}; // second order AR coefs, Q13 +const SKP_int16 SKP_Silk_Dec_B_HP_8[ DEC_HP_ORDER + 1 ] = {8000, -16000, 8000}; // second order MA coefs, Q13 + +/* table for LSB coding */ +const SKP_uint16 SKP_Silk_lsb_CDF[ 3 ] = {0, 40000, 65535}; + +/* tables for LTPScale */ +const SKP_uint16 SKP_Silk_LTPscale_CDF[ 4 ] = {0, 32000, 48000, 65535}; +const SKP_int SKP_Silk_LTPscale_offset = 2; + +/* tables for VAD flag */ +const SKP_uint16 SKP_Silk_vadflag_CDF[ 3 ] = {0, 22000, 65535}; // 66% for speech, 33% for no speech +const SKP_int SKP_Silk_vadflag_offset = 1; + +/* tables for sampling rate */ +const SKP_int SKP_Silk_SamplingRates_table[ 4 ] = {8, 12, 16, 24}; +const SKP_uint16 SKP_Silk_SamplingRates_CDF[ 5 ] = {0, 16000, 32000, 48000, 65535}; +const SKP_int SKP_Silk_SamplingRates_offset = 2; + +/* tables for NLSF interpolation factor */ +const SKP_uint16 SKP_Silk_NLSF_interpolation_factor_CDF[ 6 ] = {0, 3706, 8703, 19226, 30926, 65535}; +const SKP_int SKP_Silk_NLSF_interpolation_factor_offset = 4; + +/* Table for frame termination indication */ +const SKP_uint16 SKP_Silk_FrameTermination_CDF[ 5 ] = {0, 20000, 45000, 56000, 65535}; +const SKP_int SKP_Silk_FrameTermination_offset = 2; + +const SKP_uint16 SKP_Silk_FrameTermination_v4_CDF[ 6 ] = {0, 13107, 26214, 39321, 52428, 65535}; +const SKP_int SKP_Silk_FrameTermination_v4_offset = 4; + +/* Table for random seed */ +const SKP_uint16 SKP_Silk_Seed_CDF[ 5 ] = {0, 16384, 32768, 49152, 65535}; +const SKP_int SKP_Silk_Seed_offset = 2; + +/* Quantization offsets */ +const SKP_int16 SKP_Silk_Quantization_Offsets_Q10[ 2 ][ 2 ] = { + { OFFSET_VL_Q10, OFFSET_VH_Q10 }, { OFFSET_UVL_Q10, OFFSET_UVH_Q10 } +}; + +/* Table for LTPScale */ +const SKP_int16 SKP_Silk_LTPScales_table_Q14[ 3 ] = { 15565, 11469, 8192 }; + +#if SWITCH_TRANSITION_FILTERING +/* Elliptic/Cauer filters designed with 0.1 dB passband ripple, + 80 dB minimum stopband attenuation, and + [0.95 : 0.15 : 0.35] normalized cut off frequencies. */ + +/* Interpolation points for filter coefficients used in the bandwidth transition smoother */ +const SKP_int32 SKP_Silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NB ] = +{ +{ 250767114, 501534038, 250767114 }, +{ 209867381, 419732057, 209867381 }, +{ 170987846, 341967853, 170987846 }, +{ 131531482, 263046905, 131531482 }, +{ 89306658, 178584282, 89306658 } +}; + +/* Interpolation points for filter coefficients used in the bandwidth transition smoother */ +const SKP_int32 SKP_Silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM ][ TRANSITION_NA ] = +{ +{ 506393414, 239854379 }, +{ 411067935, 169683996 }, +{ 306733530, 116694253 }, +{ 185807084, 77959395 }, +{ 35497197, 57401098 } +}; +#endif + +#ifdef __cplusplus +} +#endif + diff --git a/jni/silk/src/SKP_Silk_tables_pitch_lag.c b/app/src/main/jni/silk/src/SKP_Silk_tables_pitch_lag.c similarity index 98% rename from jni/silk/src/SKP_Silk_tables_pitch_lag.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_pitch_lag.c index 7f33fb9..55313b7 100644 --- a/jni/silk/src/SKP_Silk_tables_pitch_lag.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_pitch_lag.c @@ -1,199 +1,199 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_tables.h" - -const SKP_uint16 SKP_Silk_pitch_lag_NB_CDF[ 8 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { - 0, 194, 395, 608, 841, 1099, 1391, 1724, - 2105, 2544, 3047, 3624, 4282, 5027, 5865, 6799, - 7833, 8965, 10193, 11510, 12910, 14379, 15905, 17473, - 19065, 20664, 22252, 23814, 25335, 26802, 28206, 29541, - 30803, 31992, 33110, 34163, 35156, 36098, 36997, 37861, - 38698, 39515, 40319, 41115, 41906, 42696, 43485, 44273, - 45061, 45847, 46630, 47406, 48175, 48933, 49679, 50411, - 51126, 51824, 52502, 53161, 53799, 54416, 55011, 55584, - 56136, 56666, 57174, 57661, 58126, 58570, 58993, 59394, - 59775, 60134, 60472, 60790, 61087, 61363, 61620, 61856, - 62075, 62275, 62458, 62625, 62778, 62918, 63045, 63162, - 63269, 63368, 63459, 63544, 63623, 63698, 63769, 63836, - 63901, 63963, 64023, 64081, 64138, 64194, 64248, 64301, - 64354, 64406, 64457, 64508, 64558, 64608, 64657, 64706, - 64754, 64803, 64851, 64899, 64946, 64994, 65041, 65088, - 65135, 65181, 65227, 65272, 65317, 65361, 65405, 65449, - 65492, 65535 -}; - -const SKP_int SKP_Silk_pitch_lag_NB_CDF_offset = 43; - -const SKP_uint16 SKP_Silk_pitch_contour_NB_CDF[ 12 ] = { - 0, 14445, 18587, 25628, 30013, 34859, 40597, 48426, - 54460, 59033, 62990, 65535 -}; - -const SKP_int SKP_Silk_pitch_contour_NB_CDF_offset = 5; - -const SKP_uint16 SKP_Silk_pitch_lag_MB_CDF[ 12 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { - 0, 132, 266, 402, 542, 686, 838, 997, - 1167, 1349, 1546, 1760, 1993, 2248, 2528, 2835, - 3173, 3544, 3951, 4397, 4882, 5411, 5984, 6604, - 7270, 7984, 8745, 9552, 10405, 11300, 12235, 13206, - 14209, 15239, 16289, 17355, 18430, 19507, 20579, 21642, - 22688, 23712, 24710, 25677, 26610, 27507, 28366, 29188, - 29971, 30717, 31427, 32104, 32751, 33370, 33964, 34537, - 35091, 35630, 36157, 36675, 37186, 37692, 38195, 38697, - 39199, 39701, 40206, 40713, 41222, 41733, 42247, 42761, - 43277, 43793, 44309, 44824, 45336, 45845, 46351, 46851, - 47347, 47836, 48319, 48795, 49264, 49724, 50177, 50621, - 51057, 51484, 51902, 52312, 52714, 53106, 53490, 53866, - 54233, 54592, 54942, 55284, 55618, 55944, 56261, 56571, - 56873, 57167, 57453, 57731, 58001, 58263, 58516, 58762, - 58998, 59226, 59446, 59656, 59857, 60050, 60233, 60408, - 60574, 60732, 60882, 61024, 61159, 61288, 61410, 61526, - 61636, 61742, 61843, 61940, 62033, 62123, 62210, 62293, - 62374, 62452, 62528, 62602, 62674, 62744, 62812, 62879, - 62945, 63009, 63072, 63135, 63196, 63256, 63316, 63375, - 63434, 63491, 63549, 63605, 63661, 63717, 63772, 63827, - 63881, 63935, 63988, 64041, 64094, 64147, 64199, 64252, - 64304, 64356, 64409, 64461, 64513, 64565, 64617, 64669, - 64721, 64773, 64824, 64875, 64925, 64975, 65024, 65072, - 65121, 65168, 65215, 65262, 65308, 65354, 65399, 65445, - 65490, 65535 -}; - -const SKP_int SKP_Silk_pitch_lag_MB_CDF_offset = 64; - -const SKP_uint16 SKP_Silk_pitch_lag_WB_CDF[ 16 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { - 0, 106, 213, 321, 429, 539, 651, 766, - 884, 1005, 1132, 1264, 1403, 1549, 1705, 1870, - 2047, 2236, 2439, 2658, 2893, 3147, 3420, 3714, - 4030, 4370, 4736, 5127, 5546, 5993, 6470, 6978, - 7516, 8086, 8687, 9320, 9985, 10680, 11405, 12158, - 12938, 13744, 14572, 15420, 16286, 17166, 18057, 18955, - 19857, 20759, 21657, 22547, 23427, 24293, 25141, 25969, - 26774, 27555, 28310, 29037, 29736, 30406, 31048, 31662, - 32248, 32808, 33343, 33855, 34345, 34815, 35268, 35704, - 36127, 36537, 36938, 37330, 37715, 38095, 38471, 38844, - 39216, 39588, 39959, 40332, 40707, 41084, 41463, 41844, - 42229, 42615, 43005, 43397, 43791, 44186, 44583, 44982, - 45381, 45780, 46179, 46578, 46975, 47371, 47765, 48156, - 48545, 48930, 49312, 49690, 50064, 50433, 50798, 51158, - 51513, 51862, 52206, 52544, 52877, 53204, 53526, 53842, - 54152, 54457, 54756, 55050, 55338, 55621, 55898, 56170, - 56436, 56697, 56953, 57204, 57449, 57689, 57924, 58154, - 58378, 58598, 58812, 59022, 59226, 59426, 59620, 59810, - 59994, 60173, 60348, 60517, 60681, 60840, 60993, 61141, - 61284, 61421, 61553, 61679, 61800, 61916, 62026, 62131, - 62231, 62326, 62417, 62503, 62585, 62663, 62737, 62807, - 62874, 62938, 62999, 63057, 63113, 63166, 63217, 63266, - 63314, 63359, 63404, 63446, 63488, 63528, 63567, 63605, - 63642, 63678, 63713, 63748, 63781, 63815, 63847, 63879, - 63911, 63942, 63973, 64003, 64033, 64063, 64092, 64121, - 64150, 64179, 64207, 64235, 64263, 64291, 64319, 64347, - 64374, 64401, 64428, 64455, 64481, 64508, 64534, 64560, - 64585, 64610, 64635, 64660, 64685, 64710, 64734, 64758, - 64782, 64807, 64831, 64855, 64878, 64902, 64926, 64950, - 64974, 64998, 65022, 65045, 65069, 65093, 65116, 65139, - 65163, 65186, 65209, 65231, 65254, 65276, 65299, 65321, - 65343, 65364, 65386, 65408, 65429, 65450, 65471, 65493, - 65514, 65535 -}; - -const SKP_int SKP_Silk_pitch_lag_WB_CDF_offset = 86; - - -const SKP_uint16 SKP_Silk_pitch_lag_SWB_CDF[ 24 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { - 0, 253, 505, 757, 1008, 1258, 1507, 1755, - 2003, 2249, 2494, 2738, 2982, 3225, 3469, 3713, - 3957, 4202, 4449, 4698, 4949, 5203, 5460, 5720, - 5983, 6251, 6522, 6798, 7077, 7361, 7650, 7942, - 8238, 8539, 8843, 9150, 9461, 9775, 10092, 10411, - 10733, 11057, 11383, 11710, 12039, 12370, 12701, 13034, - 13368, 13703, 14040, 14377, 14716, 15056, 15398, 15742, - 16087, 16435, 16785, 17137, 17492, 17850, 18212, 18577, - 18946, 19318, 19695, 20075, 20460, 20849, 21243, 21640, - 22041, 22447, 22856, 23269, 23684, 24103, 24524, 24947, - 25372, 25798, 26225, 26652, 27079, 27504, 27929, 28352, - 28773, 29191, 29606, 30018, 30427, 30831, 31231, 31627, - 32018, 32404, 32786, 33163, 33535, 33902, 34264, 34621, - 34973, 35320, 35663, 36000, 36333, 36662, 36985, 37304, - 37619, 37929, 38234, 38535, 38831, 39122, 39409, 39692, - 39970, 40244, 40513, 40778, 41039, 41295, 41548, 41796, - 42041, 42282, 42520, 42754, 42985, 43213, 43438, 43660, - 43880, 44097, 44312, 44525, 44736, 44945, 45153, 45359, - 45565, 45769, 45972, 46175, 46377, 46578, 46780, 46981, - 47182, 47383, 47585, 47787, 47989, 48192, 48395, 48599, - 48804, 49009, 49215, 49422, 49630, 49839, 50049, 50259, - 50470, 50682, 50894, 51107, 51320, 51533, 51747, 51961, - 52175, 52388, 52601, 52813, 53025, 53236, 53446, 53655, - 53863, 54069, 54274, 54477, 54679, 54879, 55078, 55274, - 55469, 55662, 55853, 56042, 56230, 56415, 56598, 56779, - 56959, 57136, 57311, 57484, 57654, 57823, 57989, 58152, - 58314, 58473, 58629, 58783, 58935, 59084, 59230, 59373, - 59514, 59652, 59787, 59919, 60048, 60174, 60297, 60417, - 60533, 60647, 60757, 60865, 60969, 61070, 61167, 61262, - 61353, 61442, 61527, 61609, 61689, 61765, 61839, 61910, - 61979, 62045, 62109, 62170, 62230, 62287, 62343, 62396, - 62448, 62498, 62547, 62594, 62640, 62685, 62728, 62770, - 62811, 62852, 62891, 62929, 62967, 63004, 63040, 63075, - 63110, 63145, 63178, 63212, 63244, 63277, 63308, 63340, - 63371, 63402, 63432, 63462, 63491, 63521, 63550, 63578, - 63607, 63635, 63663, 63690, 63718, 63744, 63771, 63798, - 63824, 63850, 63875, 63900, 63925, 63950, 63975, 63999, - 64023, 64046, 64069, 64092, 64115, 64138, 64160, 64182, - 64204, 64225, 64247, 64268, 64289, 64310, 64330, 64351, - 64371, 64391, 64411, 64431, 64450, 64470, 64489, 64508, - 64527, 64545, 64564, 64582, 64600, 64617, 64635, 64652, - 64669, 64686, 64702, 64719, 64735, 64750, 64766, 64782, - 64797, 64812, 64827, 64842, 64857, 64872, 64886, 64901, - 64915, 64930, 64944, 64959, 64974, 64988, 65003, 65018, - 65033, 65048, 65063, 65078, 65094, 65109, 65125, 65141, - 65157, 65172, 65188, 65204, 65220, 65236, 65252, 65268, - 65283, 65299, 65314, 65330, 65345, 65360, 65375, 65390, - 65405, 65419, 65434, 65449, 65463, 65477, 65492, 65506, - 65521, 65535 -}; - -const SKP_int SKP_Silk_pitch_lag_SWB_CDF_offset = 128; - - -const SKP_uint16 SKP_Silk_pitch_contour_CDF[ 35 ] = { - 0, 372, 843, 1315, 1836, 2644, 3576, 4719, - 6088, 7621, 9396, 11509, 14245, 17618, 20777, 24294, - 27992, 33116, 40100, 44329, 47558, 50679, 53130, 55557, - 57510, 59022, 60285, 61345, 62316, 63140, 63762, 64321, - 64729, 65099, 65535 -}; - -const SKP_int SKP_Silk_pitch_contour_CDF_offset = 17; - -const SKP_uint16 SKP_Silk_pitch_delta_CDF[23] = { - 0, 343, 740, 1249, 1889, 2733, 3861, 5396, - 7552, 10890, 16053, 24152, 30220, 34680, 37973, 40405, - 42243, 43708, 44823, 45773, 46462, 47055, 65535 -}; - -const SKP_int SKP_Silk_pitch_delta_CDF_offset = 11; +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_tables.h" + +const SKP_uint16 SKP_Silk_pitch_lag_NB_CDF[ 8 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { + 0, 194, 395, 608, 841, 1099, 1391, 1724, + 2105, 2544, 3047, 3624, 4282, 5027, 5865, 6799, + 7833, 8965, 10193, 11510, 12910, 14379, 15905, 17473, + 19065, 20664, 22252, 23814, 25335, 26802, 28206, 29541, + 30803, 31992, 33110, 34163, 35156, 36098, 36997, 37861, + 38698, 39515, 40319, 41115, 41906, 42696, 43485, 44273, + 45061, 45847, 46630, 47406, 48175, 48933, 49679, 50411, + 51126, 51824, 52502, 53161, 53799, 54416, 55011, 55584, + 56136, 56666, 57174, 57661, 58126, 58570, 58993, 59394, + 59775, 60134, 60472, 60790, 61087, 61363, 61620, 61856, + 62075, 62275, 62458, 62625, 62778, 62918, 63045, 63162, + 63269, 63368, 63459, 63544, 63623, 63698, 63769, 63836, + 63901, 63963, 64023, 64081, 64138, 64194, 64248, 64301, + 64354, 64406, 64457, 64508, 64558, 64608, 64657, 64706, + 64754, 64803, 64851, 64899, 64946, 64994, 65041, 65088, + 65135, 65181, 65227, 65272, 65317, 65361, 65405, 65449, + 65492, 65535 +}; + +const SKP_int SKP_Silk_pitch_lag_NB_CDF_offset = 43; + +const SKP_uint16 SKP_Silk_pitch_contour_NB_CDF[ 12 ] = { + 0, 14445, 18587, 25628, 30013, 34859, 40597, 48426, + 54460, 59033, 62990, 65535 +}; + +const SKP_int SKP_Silk_pitch_contour_NB_CDF_offset = 5; + +const SKP_uint16 SKP_Silk_pitch_lag_MB_CDF[ 12 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { + 0, 132, 266, 402, 542, 686, 838, 997, + 1167, 1349, 1546, 1760, 1993, 2248, 2528, 2835, + 3173, 3544, 3951, 4397, 4882, 5411, 5984, 6604, + 7270, 7984, 8745, 9552, 10405, 11300, 12235, 13206, + 14209, 15239, 16289, 17355, 18430, 19507, 20579, 21642, + 22688, 23712, 24710, 25677, 26610, 27507, 28366, 29188, + 29971, 30717, 31427, 32104, 32751, 33370, 33964, 34537, + 35091, 35630, 36157, 36675, 37186, 37692, 38195, 38697, + 39199, 39701, 40206, 40713, 41222, 41733, 42247, 42761, + 43277, 43793, 44309, 44824, 45336, 45845, 46351, 46851, + 47347, 47836, 48319, 48795, 49264, 49724, 50177, 50621, + 51057, 51484, 51902, 52312, 52714, 53106, 53490, 53866, + 54233, 54592, 54942, 55284, 55618, 55944, 56261, 56571, + 56873, 57167, 57453, 57731, 58001, 58263, 58516, 58762, + 58998, 59226, 59446, 59656, 59857, 60050, 60233, 60408, + 60574, 60732, 60882, 61024, 61159, 61288, 61410, 61526, + 61636, 61742, 61843, 61940, 62033, 62123, 62210, 62293, + 62374, 62452, 62528, 62602, 62674, 62744, 62812, 62879, + 62945, 63009, 63072, 63135, 63196, 63256, 63316, 63375, + 63434, 63491, 63549, 63605, 63661, 63717, 63772, 63827, + 63881, 63935, 63988, 64041, 64094, 64147, 64199, 64252, + 64304, 64356, 64409, 64461, 64513, 64565, 64617, 64669, + 64721, 64773, 64824, 64875, 64925, 64975, 65024, 65072, + 65121, 65168, 65215, 65262, 65308, 65354, 65399, 65445, + 65490, 65535 +}; + +const SKP_int SKP_Silk_pitch_lag_MB_CDF_offset = 64; + +const SKP_uint16 SKP_Silk_pitch_lag_WB_CDF[ 16 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { + 0, 106, 213, 321, 429, 539, 651, 766, + 884, 1005, 1132, 1264, 1403, 1549, 1705, 1870, + 2047, 2236, 2439, 2658, 2893, 3147, 3420, 3714, + 4030, 4370, 4736, 5127, 5546, 5993, 6470, 6978, + 7516, 8086, 8687, 9320, 9985, 10680, 11405, 12158, + 12938, 13744, 14572, 15420, 16286, 17166, 18057, 18955, + 19857, 20759, 21657, 22547, 23427, 24293, 25141, 25969, + 26774, 27555, 28310, 29037, 29736, 30406, 31048, 31662, + 32248, 32808, 33343, 33855, 34345, 34815, 35268, 35704, + 36127, 36537, 36938, 37330, 37715, 38095, 38471, 38844, + 39216, 39588, 39959, 40332, 40707, 41084, 41463, 41844, + 42229, 42615, 43005, 43397, 43791, 44186, 44583, 44982, + 45381, 45780, 46179, 46578, 46975, 47371, 47765, 48156, + 48545, 48930, 49312, 49690, 50064, 50433, 50798, 51158, + 51513, 51862, 52206, 52544, 52877, 53204, 53526, 53842, + 54152, 54457, 54756, 55050, 55338, 55621, 55898, 56170, + 56436, 56697, 56953, 57204, 57449, 57689, 57924, 58154, + 58378, 58598, 58812, 59022, 59226, 59426, 59620, 59810, + 59994, 60173, 60348, 60517, 60681, 60840, 60993, 61141, + 61284, 61421, 61553, 61679, 61800, 61916, 62026, 62131, + 62231, 62326, 62417, 62503, 62585, 62663, 62737, 62807, + 62874, 62938, 62999, 63057, 63113, 63166, 63217, 63266, + 63314, 63359, 63404, 63446, 63488, 63528, 63567, 63605, + 63642, 63678, 63713, 63748, 63781, 63815, 63847, 63879, + 63911, 63942, 63973, 64003, 64033, 64063, 64092, 64121, + 64150, 64179, 64207, 64235, 64263, 64291, 64319, 64347, + 64374, 64401, 64428, 64455, 64481, 64508, 64534, 64560, + 64585, 64610, 64635, 64660, 64685, 64710, 64734, 64758, + 64782, 64807, 64831, 64855, 64878, 64902, 64926, 64950, + 64974, 64998, 65022, 65045, 65069, 65093, 65116, 65139, + 65163, 65186, 65209, 65231, 65254, 65276, 65299, 65321, + 65343, 65364, 65386, 65408, 65429, 65450, 65471, 65493, + 65514, 65535 +}; + +const SKP_int SKP_Silk_pitch_lag_WB_CDF_offset = 86; + + +const SKP_uint16 SKP_Silk_pitch_lag_SWB_CDF[ 24 * ( PITCH_EST_MAX_LAG_MS - PITCH_EST_MIN_LAG_MS ) + 2 ] = { + 0, 253, 505, 757, 1008, 1258, 1507, 1755, + 2003, 2249, 2494, 2738, 2982, 3225, 3469, 3713, + 3957, 4202, 4449, 4698, 4949, 5203, 5460, 5720, + 5983, 6251, 6522, 6798, 7077, 7361, 7650, 7942, + 8238, 8539, 8843, 9150, 9461, 9775, 10092, 10411, + 10733, 11057, 11383, 11710, 12039, 12370, 12701, 13034, + 13368, 13703, 14040, 14377, 14716, 15056, 15398, 15742, + 16087, 16435, 16785, 17137, 17492, 17850, 18212, 18577, + 18946, 19318, 19695, 20075, 20460, 20849, 21243, 21640, + 22041, 22447, 22856, 23269, 23684, 24103, 24524, 24947, + 25372, 25798, 26225, 26652, 27079, 27504, 27929, 28352, + 28773, 29191, 29606, 30018, 30427, 30831, 31231, 31627, + 32018, 32404, 32786, 33163, 33535, 33902, 34264, 34621, + 34973, 35320, 35663, 36000, 36333, 36662, 36985, 37304, + 37619, 37929, 38234, 38535, 38831, 39122, 39409, 39692, + 39970, 40244, 40513, 40778, 41039, 41295, 41548, 41796, + 42041, 42282, 42520, 42754, 42985, 43213, 43438, 43660, + 43880, 44097, 44312, 44525, 44736, 44945, 45153, 45359, + 45565, 45769, 45972, 46175, 46377, 46578, 46780, 46981, + 47182, 47383, 47585, 47787, 47989, 48192, 48395, 48599, + 48804, 49009, 49215, 49422, 49630, 49839, 50049, 50259, + 50470, 50682, 50894, 51107, 51320, 51533, 51747, 51961, + 52175, 52388, 52601, 52813, 53025, 53236, 53446, 53655, + 53863, 54069, 54274, 54477, 54679, 54879, 55078, 55274, + 55469, 55662, 55853, 56042, 56230, 56415, 56598, 56779, + 56959, 57136, 57311, 57484, 57654, 57823, 57989, 58152, + 58314, 58473, 58629, 58783, 58935, 59084, 59230, 59373, + 59514, 59652, 59787, 59919, 60048, 60174, 60297, 60417, + 60533, 60647, 60757, 60865, 60969, 61070, 61167, 61262, + 61353, 61442, 61527, 61609, 61689, 61765, 61839, 61910, + 61979, 62045, 62109, 62170, 62230, 62287, 62343, 62396, + 62448, 62498, 62547, 62594, 62640, 62685, 62728, 62770, + 62811, 62852, 62891, 62929, 62967, 63004, 63040, 63075, + 63110, 63145, 63178, 63212, 63244, 63277, 63308, 63340, + 63371, 63402, 63432, 63462, 63491, 63521, 63550, 63578, + 63607, 63635, 63663, 63690, 63718, 63744, 63771, 63798, + 63824, 63850, 63875, 63900, 63925, 63950, 63975, 63999, + 64023, 64046, 64069, 64092, 64115, 64138, 64160, 64182, + 64204, 64225, 64247, 64268, 64289, 64310, 64330, 64351, + 64371, 64391, 64411, 64431, 64450, 64470, 64489, 64508, + 64527, 64545, 64564, 64582, 64600, 64617, 64635, 64652, + 64669, 64686, 64702, 64719, 64735, 64750, 64766, 64782, + 64797, 64812, 64827, 64842, 64857, 64872, 64886, 64901, + 64915, 64930, 64944, 64959, 64974, 64988, 65003, 65018, + 65033, 65048, 65063, 65078, 65094, 65109, 65125, 65141, + 65157, 65172, 65188, 65204, 65220, 65236, 65252, 65268, + 65283, 65299, 65314, 65330, 65345, 65360, 65375, 65390, + 65405, 65419, 65434, 65449, 65463, 65477, 65492, 65506, + 65521, 65535 +}; + +const SKP_int SKP_Silk_pitch_lag_SWB_CDF_offset = 128; + + +const SKP_uint16 SKP_Silk_pitch_contour_CDF[ 35 ] = { + 0, 372, 843, 1315, 1836, 2644, 3576, 4719, + 6088, 7621, 9396, 11509, 14245, 17618, 20777, 24294, + 27992, 33116, 40100, 44329, 47558, 50679, 53130, 55557, + 57510, 59022, 60285, 61345, 62316, 63140, 63762, 64321, + 64729, 65099, 65535 +}; + +const SKP_int SKP_Silk_pitch_contour_CDF_offset = 17; + +const SKP_uint16 SKP_Silk_pitch_delta_CDF[23] = { + 0, 343, 740, 1249, 1889, 2733, 3861, 5396, + 7552, 10890, 16053, 24152, 30220, 34680, 37973, 40405, + 42243, 43708, 44823, 45773, 46462, 47055, 65535 +}; + +const SKP_int SKP_Silk_pitch_delta_CDF_offset = 11; diff --git a/jni/silk/src/SKP_Silk_tables_pulses_per_block.c b/app/src/main/jni/silk/src/SKP_Silk_tables_pulses_per_block.c similarity index 97% rename from jni/silk/src/SKP_Silk_tables_pulses_per_block.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_pulses_per_block.c index f978651..6942439 100644 --- a/jni/silk/src/SKP_Silk_tables_pulses_per_block.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_pulses_per_block.c @@ -1,235 +1,235 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_tables.h" - -const SKP_int SKP_Silk_max_pulses_table[ 4 ] = { - 6, 8, 12, 18 -}; - -const SKP_uint16 SKP_Silk_pulses_per_block_CDF[ 10 ][ 21 ] = -{ -{ - 0, 47113, 61501, 64590, 65125, 65277, 65352, 65407, - 65450, 65474, 65488, 65501, 65508, 65514, 65516, 65520, - 65521, 65523, 65524, 65526, 65535 -}, -{ - 0, 26368, 47760, 58803, 63085, 64567, 65113, 65333, - 65424, 65474, 65498, 65511, 65517, 65520, 65523, 65525, - 65526, 65528, 65529, 65530, 65535 -}, -{ - 0, 9601, 28014, 45877, 57210, 62560, 64611, 65260, - 65447, 65500, 65511, 65519, 65521, 65525, 65526, 65529, - 65530, 65531, 65532, 65534, 65535 -}, -{ - 0, 3351, 12462, 25972, 39782, 50686, 57644, 61525, - 63521, 64506, 65009, 65255, 65375, 65441, 65471, 65488, - 65497, 65505, 65509, 65512, 65535 -}, -{ - 0, 488, 2944, 9295, 19712, 32160, 43976, 53121, - 59144, 62518, 64213, 65016, 65346, 65470, 65511, 65515, - 65525, 65529, 65531, 65534, 65535 -}, -{ - 0, 17013, 30405, 40812, 48142, 53466, 57166, 59845, - 61650, 62873, 63684, 64223, 64575, 64811, 64959, 65051, - 65111, 65143, 65165, 65183, 65535 -}, -{ - 0, 2994, 8323, 15845, 24196, 32300, 39340, 45140, - 49813, 53474, 56349, 58518, 60167, 61397, 62313, 62969, - 63410, 63715, 63906, 64056, 65535 -}, -{ - 0, 88, 721, 2795, 7542, 14888, 24420, 34593, - 43912, 51484, 56962, 60558, 62760, 64037, 64716, 65069, - 65262, 65358, 65398, 65420, 65535 -}, -{ - 0, 287, 789, 2064, 4398, 8174, 13534, 20151, - 27347, 34533, 41295, 47242, 52070, 55772, 58458, 60381, - 61679, 62533, 63109, 63519, 65535 -}, -{ - 0, 1, 3, 91, 4521, 14708, 28329, 41955, - 52116, 58375, 61729, 63534, 64459, 64924, 65092, 65164, - 65182, 65198, 65203, 65211, 65535 -} -}; - -const SKP_int SKP_Silk_pulses_per_block_CDF_offset = 6; - - -const SKP_int16 SKP_Silk_pulses_per_block_BITS_Q6[ 9 ][ 20 ] = -{ -{ - 30, 140, 282, 444, 560, 625, 654, 677, - 731, 780, 787, 844, 859, 960, 896, 1024, - 960, 1024, 960, 821 -}, -{ - 84, 103, 164, 252, 350, 442, 526, 607, - 663, 731, 787, 859, 923, 923, 960, 1024, - 960, 1024, 1024, 875 -}, -{ - 177, 117, 120, 162, 231, 320, 426, 541, - 657, 803, 832, 960, 896, 1024, 923, 1024, - 1024, 1024, 960, 1024 -}, -{ - 275, 182, 146, 144, 166, 207, 261, 322, - 388, 450, 516, 582, 637, 710, 762, 821, - 832, 896, 923, 734 -}, -{ - 452, 303, 216, 170, 153, 158, 182, 220, - 274, 337, 406, 489, 579, 681, 896, 811, - 896, 960, 923, 1024 -}, -{ - 125, 147, 170, 202, 232, 265, 295, 332, - 368, 406, 443, 483, 520, 563, 606, 646, - 704, 739, 757, 483 -}, -{ - 285, 232, 200, 190, 193, 206, 224, 244, - 266, 289, 315, 340, 367, 394, 425, 462, - 496, 539, 561, 350 -}, -{ - 611, 428, 319, 242, 202, 178, 172, 180, - 199, 229, 268, 313, 364, 422, 482, 538, - 603, 683, 739, 586 -}, -{ - 501, 450, 364, 308, 264, 231, 212, 204, - 204, 210, 222, 241, 265, 295, 326, 362, - 401, 437, 469, 321 -} -}; - -const SKP_uint16 SKP_Silk_rate_levels_CDF[ 2 ][ 10 ] = -{ -{ - 0, 2005, 12717, 20281, 31328, 36234, 45816, 57753, - 63104, 65535 -}, -{ - 0, 8553, 23489, 36031, 46295, 53519, 56519, 59151, - 64185, 65535 -} -}; - -const SKP_int SKP_Silk_rate_levels_CDF_offset = 4; - - -const SKP_int16 SKP_Silk_rate_levels_BITS_Q6[ 2 ][ 9 ] = -{ -{ - 322, 167, 199, 164, 239, 178, 157, 231, - 304 -}, -{ - 188, 137, 153, 171, 204, 285, 297, 237, - 358 -} -}; - -const SKP_uint16 SKP_Silk_shell_code_table0[ 33 ] = { - 0, 32748, 65535, 0, 9505, 56230, 65535, 0, - 4093, 32204, 61720, 65535, 0, 2285, 16207, 48750, - 63424, 65535, 0, 1709, 9446, 32026, 55752, 63876, - 65535, 0, 1623, 6986, 21845, 45381, 59147, 64186, - 65535 -}; - -const SKP_uint16 SKP_Silk_shell_code_table1[ 52 ] = { - 0, 32691, 65535, 0, 12782, 52752, 65535, 0, - 4847, 32665, 60899, 65535, 0, 2500, 17305, 47989, - 63369, 65535, 0, 1843, 10329, 32419, 55433, 64277, - 65535, 0, 1485, 7062, 21465, 43414, 59079, 64623, - 65535, 0, 0, 4841, 14797, 31799, 49667, 61309, - 65535, 65535, 0, 0, 0, 8032, 21695, 41078, - 56317, 65535, 65535, 65535 -}; - -const SKP_uint16 SKP_Silk_shell_code_table2[ 102 ] = { - 0, 32615, 65535, 0, 14447, 50912, 65535, 0, - 6301, 32587, 59361, 65535, 0, 3038, 18640, 46809, - 62852, 65535, 0, 1746, 10524, 32509, 55273, 64278, - 65535, 0, 1234, 6360, 21259, 43712, 59651, 64805, - 65535, 0, 1020, 4461, 14030, 32286, 51249, 61904, - 65100, 65535, 0, 851, 3435, 10006, 23241, 40797, - 55444, 63009, 65252, 65535, 0, 0, 2075, 7137, - 17119, 31499, 46982, 58723, 63976, 65535, 65535, 0, - 0, 0, 3820, 11572, 23038, 37789, 51969, 61243, - 65535, 65535, 65535, 0, 0, 0, 0, 6882, - 16828, 30444, 44844, 57365, 65535, 65535, 65535, 65535, - 0, 0, 0, 0, 0, 10093, 22963, 38779, - 54426, 65535, 65535, 65535, 65535, 65535 -}; - -const SKP_uint16 SKP_Silk_shell_code_table3[ 207 ] = { - 0, 32324, 65535, 0, 15328, 49505, 65535, 0, - 7474, 32344, 57955, 65535, 0, 3944, 19450, 45364, - 61873, 65535, 0, 2338, 11698, 32435, 53915, 63734, - 65535, 0, 1506, 7074, 21778, 42972, 58861, 64590, - 65535, 0, 1027, 4490, 14383, 32264, 50980, 61712, - 65043, 65535, 0, 760, 3022, 9696, 23264, 41465, - 56181, 63253, 65251, 65535, 0, 579, 2256, 6873, - 16661, 31951, 48250, 59403, 64198, 65360, 65535, 0, - 464, 1783, 5181, 12269, 24247, 39877, 53490, 61502, - 64591, 65410, 65535, 0, 366, 1332, 3880, 9273, - 18585, 32014, 45928, 56659, 62616, 64899, 65483, 65535, - 0, 286, 1065, 3089, 6969, 14148, 24859, 38274, - 50715, 59078, 63448, 65091, 65481, 65535, 0, 0, - 482, 2010, 5302, 10408, 18988, 30698, 43634, 54233, - 60828, 64119, 65288, 65535, 65535, 0, 0, 0, - 1006, 3531, 7857, 14832, 24543, 36272, 47547, 56883, - 62327, 64746, 65535, 65535, 65535, 0, 0, 0, - 0, 1863, 4950, 10730, 19284, 29397, 41382, 52335, - 59755, 63834, 65535, 65535, 65535, 65535, 0, 0, - 0, 0, 0, 2513, 7290, 14487, 24275, 35312, - 46240, 55841, 62007, 65535, 65535, 65535, 65535, 65535, - 0, 0, 0, 0, 0, 0, 3606, 9573, - 18764, 28667, 40220, 51290, 59924, 65535, 65535, 65535, - 65535, 65535, 65535, 0, 0, 0, 0, 0, - 0, 0, 4879, 13091, 23376, 36061, 49395, 59315, - 65535, 65535, 65535, 65535, 65535, 65535, 65535 -}; - -const SKP_uint16 SKP_Silk_shell_code_table_offsets[ 19 ] = { - 0, 0, 3, 7, 12, 18, 25, 33, - 42, 52, 63, 75, 88, 102, 117, 133, - 150, 168, 187 -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_tables.h" + +const SKP_int SKP_Silk_max_pulses_table[ 4 ] = { + 6, 8, 12, 18 +}; + +const SKP_uint16 SKP_Silk_pulses_per_block_CDF[ 10 ][ 21 ] = +{ +{ + 0, 47113, 61501, 64590, 65125, 65277, 65352, 65407, + 65450, 65474, 65488, 65501, 65508, 65514, 65516, 65520, + 65521, 65523, 65524, 65526, 65535 +}, +{ + 0, 26368, 47760, 58803, 63085, 64567, 65113, 65333, + 65424, 65474, 65498, 65511, 65517, 65520, 65523, 65525, + 65526, 65528, 65529, 65530, 65535 +}, +{ + 0, 9601, 28014, 45877, 57210, 62560, 64611, 65260, + 65447, 65500, 65511, 65519, 65521, 65525, 65526, 65529, + 65530, 65531, 65532, 65534, 65535 +}, +{ + 0, 3351, 12462, 25972, 39782, 50686, 57644, 61525, + 63521, 64506, 65009, 65255, 65375, 65441, 65471, 65488, + 65497, 65505, 65509, 65512, 65535 +}, +{ + 0, 488, 2944, 9295, 19712, 32160, 43976, 53121, + 59144, 62518, 64213, 65016, 65346, 65470, 65511, 65515, + 65525, 65529, 65531, 65534, 65535 +}, +{ + 0, 17013, 30405, 40812, 48142, 53466, 57166, 59845, + 61650, 62873, 63684, 64223, 64575, 64811, 64959, 65051, + 65111, 65143, 65165, 65183, 65535 +}, +{ + 0, 2994, 8323, 15845, 24196, 32300, 39340, 45140, + 49813, 53474, 56349, 58518, 60167, 61397, 62313, 62969, + 63410, 63715, 63906, 64056, 65535 +}, +{ + 0, 88, 721, 2795, 7542, 14888, 24420, 34593, + 43912, 51484, 56962, 60558, 62760, 64037, 64716, 65069, + 65262, 65358, 65398, 65420, 65535 +}, +{ + 0, 287, 789, 2064, 4398, 8174, 13534, 20151, + 27347, 34533, 41295, 47242, 52070, 55772, 58458, 60381, + 61679, 62533, 63109, 63519, 65535 +}, +{ + 0, 1, 3, 91, 4521, 14708, 28329, 41955, + 52116, 58375, 61729, 63534, 64459, 64924, 65092, 65164, + 65182, 65198, 65203, 65211, 65535 +} +}; + +const SKP_int SKP_Silk_pulses_per_block_CDF_offset = 6; + + +const SKP_int16 SKP_Silk_pulses_per_block_BITS_Q6[ 9 ][ 20 ] = +{ +{ + 30, 140, 282, 444, 560, 625, 654, 677, + 731, 780, 787, 844, 859, 960, 896, 1024, + 960, 1024, 960, 821 +}, +{ + 84, 103, 164, 252, 350, 442, 526, 607, + 663, 731, 787, 859, 923, 923, 960, 1024, + 960, 1024, 1024, 875 +}, +{ + 177, 117, 120, 162, 231, 320, 426, 541, + 657, 803, 832, 960, 896, 1024, 923, 1024, + 1024, 1024, 960, 1024 +}, +{ + 275, 182, 146, 144, 166, 207, 261, 322, + 388, 450, 516, 582, 637, 710, 762, 821, + 832, 896, 923, 734 +}, +{ + 452, 303, 216, 170, 153, 158, 182, 220, + 274, 337, 406, 489, 579, 681, 896, 811, + 896, 960, 923, 1024 +}, +{ + 125, 147, 170, 202, 232, 265, 295, 332, + 368, 406, 443, 483, 520, 563, 606, 646, + 704, 739, 757, 483 +}, +{ + 285, 232, 200, 190, 193, 206, 224, 244, + 266, 289, 315, 340, 367, 394, 425, 462, + 496, 539, 561, 350 +}, +{ + 611, 428, 319, 242, 202, 178, 172, 180, + 199, 229, 268, 313, 364, 422, 482, 538, + 603, 683, 739, 586 +}, +{ + 501, 450, 364, 308, 264, 231, 212, 204, + 204, 210, 222, 241, 265, 295, 326, 362, + 401, 437, 469, 321 +} +}; + +const SKP_uint16 SKP_Silk_rate_levels_CDF[ 2 ][ 10 ] = +{ +{ + 0, 2005, 12717, 20281, 31328, 36234, 45816, 57753, + 63104, 65535 +}, +{ + 0, 8553, 23489, 36031, 46295, 53519, 56519, 59151, + 64185, 65535 +} +}; + +const SKP_int SKP_Silk_rate_levels_CDF_offset = 4; + + +const SKP_int16 SKP_Silk_rate_levels_BITS_Q6[ 2 ][ 9 ] = +{ +{ + 322, 167, 199, 164, 239, 178, 157, 231, + 304 +}, +{ + 188, 137, 153, 171, 204, 285, 297, 237, + 358 +} +}; + +const SKP_uint16 SKP_Silk_shell_code_table0[ 33 ] = { + 0, 32748, 65535, 0, 9505, 56230, 65535, 0, + 4093, 32204, 61720, 65535, 0, 2285, 16207, 48750, + 63424, 65535, 0, 1709, 9446, 32026, 55752, 63876, + 65535, 0, 1623, 6986, 21845, 45381, 59147, 64186, + 65535 +}; + +const SKP_uint16 SKP_Silk_shell_code_table1[ 52 ] = { + 0, 32691, 65535, 0, 12782, 52752, 65535, 0, + 4847, 32665, 60899, 65535, 0, 2500, 17305, 47989, + 63369, 65535, 0, 1843, 10329, 32419, 55433, 64277, + 65535, 0, 1485, 7062, 21465, 43414, 59079, 64623, + 65535, 0, 0, 4841, 14797, 31799, 49667, 61309, + 65535, 65535, 0, 0, 0, 8032, 21695, 41078, + 56317, 65535, 65535, 65535 +}; + +const SKP_uint16 SKP_Silk_shell_code_table2[ 102 ] = { + 0, 32615, 65535, 0, 14447, 50912, 65535, 0, + 6301, 32587, 59361, 65535, 0, 3038, 18640, 46809, + 62852, 65535, 0, 1746, 10524, 32509, 55273, 64278, + 65535, 0, 1234, 6360, 21259, 43712, 59651, 64805, + 65535, 0, 1020, 4461, 14030, 32286, 51249, 61904, + 65100, 65535, 0, 851, 3435, 10006, 23241, 40797, + 55444, 63009, 65252, 65535, 0, 0, 2075, 7137, + 17119, 31499, 46982, 58723, 63976, 65535, 65535, 0, + 0, 0, 3820, 11572, 23038, 37789, 51969, 61243, + 65535, 65535, 65535, 0, 0, 0, 0, 6882, + 16828, 30444, 44844, 57365, 65535, 65535, 65535, 65535, + 0, 0, 0, 0, 0, 10093, 22963, 38779, + 54426, 65535, 65535, 65535, 65535, 65535 +}; + +const SKP_uint16 SKP_Silk_shell_code_table3[ 207 ] = { + 0, 32324, 65535, 0, 15328, 49505, 65535, 0, + 7474, 32344, 57955, 65535, 0, 3944, 19450, 45364, + 61873, 65535, 0, 2338, 11698, 32435, 53915, 63734, + 65535, 0, 1506, 7074, 21778, 42972, 58861, 64590, + 65535, 0, 1027, 4490, 14383, 32264, 50980, 61712, + 65043, 65535, 0, 760, 3022, 9696, 23264, 41465, + 56181, 63253, 65251, 65535, 0, 579, 2256, 6873, + 16661, 31951, 48250, 59403, 64198, 65360, 65535, 0, + 464, 1783, 5181, 12269, 24247, 39877, 53490, 61502, + 64591, 65410, 65535, 0, 366, 1332, 3880, 9273, + 18585, 32014, 45928, 56659, 62616, 64899, 65483, 65535, + 0, 286, 1065, 3089, 6969, 14148, 24859, 38274, + 50715, 59078, 63448, 65091, 65481, 65535, 0, 0, + 482, 2010, 5302, 10408, 18988, 30698, 43634, 54233, + 60828, 64119, 65288, 65535, 65535, 0, 0, 0, + 1006, 3531, 7857, 14832, 24543, 36272, 47547, 56883, + 62327, 64746, 65535, 65535, 65535, 0, 0, 0, + 0, 1863, 4950, 10730, 19284, 29397, 41382, 52335, + 59755, 63834, 65535, 65535, 65535, 65535, 0, 0, + 0, 0, 0, 2513, 7290, 14487, 24275, 35312, + 46240, 55841, 62007, 65535, 65535, 65535, 65535, 65535, + 0, 0, 0, 0, 0, 0, 3606, 9573, + 18764, 28667, 40220, 51290, 59924, 65535, 65535, 65535, + 65535, 65535, 65535, 0, 0, 0, 0, 0, + 0, 0, 4879, 13091, 23376, 36061, 49395, 59315, + 65535, 65535, 65535, 65535, 65535, 65535, 65535 +}; + +const SKP_uint16 SKP_Silk_shell_code_table_offsets[ 19 ] = { + 0, 0, 3, 7, 12, 18, 25, 33, + 42, 52, 63, 75, 88, 102, 117, 133, + 150, 168, 187 +}; + diff --git a/jni/silk/src/SKP_Silk_tables_sign.c b/app/src/main/jni/silk/src/SKP_Silk_tables_sign.c similarity index 95% rename from jni/silk/src/SKP_Silk_tables_sign.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_sign.c index 5ada010..800a04a 100644 --- a/jni/silk/src/SKP_Silk_tables_sign.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_sign.c @@ -1,141 +1,141 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_tables.h" - -const SKP_uint16 SKP_Silk_sign_CDF[ 36 ][ 3 ] = -{ -{ - 0, 37840, 65535 -}, -{ - 0, 36944, 65535 -}, -{ - 0, 36251, 65535 -}, -{ - 0, 35304, 65535 -}, -{ - 0, 34715, 65535 -}, -{ - 0, 35503, 65535 -}, -{ - 0, 34529, 65535 -}, -{ - 0, 34296, 65535 -}, -{ - 0, 34016, 65535 -}, -{ - 0, 47659, 65535 -}, -{ - 0, 44945, 65535 -}, -{ - 0, 42503, 65535 -}, -{ - 0, 40235, 65535 -}, -{ - 0, 38569, 65535 -}, -{ - 0, 40254, 65535 -}, -{ - 0, 37851, 65535 -}, -{ - 0, 37243, 65535 -}, -{ - 0, 36595, 65535 -}, -{ - 0, 43410, 65535 -}, -{ - 0, 44121, 65535 -}, -{ - 0, 43127, 65535 -}, -{ - 0, 40978, 65535 -}, -{ - 0, 38845, 65535 -}, -{ - 0, 40433, 65535 -}, -{ - 0, 38252, 65535 -}, -{ - 0, 37795, 65535 -}, -{ - 0, 36637, 65535 -}, -{ - 0, 59159, 65535 -}, -{ - 0, 55630, 65535 -}, -{ - 0, 51806, 65535 -}, -{ - 0, 48073, 65535 -}, -{ - 0, 45036, 65535 -}, -{ - 0, 48416, 65535 -}, -{ - 0, 43857, 65535 -}, -{ - 0, 42678, 65535 -}, -{ - 0, 41146, 65535 -} -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_tables.h" + +const SKP_uint16 SKP_Silk_sign_CDF[ 36 ][ 3 ] = +{ +{ + 0, 37840, 65535 +}, +{ + 0, 36944, 65535 +}, +{ + 0, 36251, 65535 +}, +{ + 0, 35304, 65535 +}, +{ + 0, 34715, 65535 +}, +{ + 0, 35503, 65535 +}, +{ + 0, 34529, 65535 +}, +{ + 0, 34296, 65535 +}, +{ + 0, 34016, 65535 +}, +{ + 0, 47659, 65535 +}, +{ + 0, 44945, 65535 +}, +{ + 0, 42503, 65535 +}, +{ + 0, 40235, 65535 +}, +{ + 0, 38569, 65535 +}, +{ + 0, 40254, 65535 +}, +{ + 0, 37851, 65535 +}, +{ + 0, 37243, 65535 +}, +{ + 0, 36595, 65535 +}, +{ + 0, 43410, 65535 +}, +{ + 0, 44121, 65535 +}, +{ + 0, 43127, 65535 +}, +{ + 0, 40978, 65535 +}, +{ + 0, 38845, 65535 +}, +{ + 0, 40433, 65535 +}, +{ + 0, 38252, 65535 +}, +{ + 0, 37795, 65535 +}, +{ + 0, 36637, 65535 +}, +{ + 0, 59159, 65535 +}, +{ + 0, 55630, 65535 +}, +{ + 0, 51806, 65535 +}, +{ + 0, 48073, 65535 +}, +{ + 0, 45036, 65535 +}, +{ + 0, 48416, 65535 +}, +{ + 0, 43857, 65535 +}, +{ + 0, 42678, 65535 +}, +{ + 0, 41146, 65535 +} +}; + diff --git a/jni/silk/src/SKP_Silk_tables_type_offset.c b/app/src/main/jni/silk/src/SKP_Silk_tables_type_offset.c similarity index 97% rename from jni/silk/src/SKP_Silk_tables_type_offset.c rename to app/src/main/jni/silk/src/SKP_Silk_tables_type_offset.c index 5a50756..ca51e5f 100644 --- a/jni/silk/src/SKP_Silk_tables_type_offset.c +++ b/app/src/main/jni/silk/src/SKP_Silk_tables_type_offset.c @@ -1,52 +1,52 @@ -/*********************************************************************** -Copyright (c) 2006-2010, Skype Limited. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, (subject to the limitations in the disclaimer below) -are permitted provided that the following conditions are met: -- Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. -- Neither the name of Skype Limited, nor the names of specific -contributors, may be used to endorse or promote products derived from -this software without specific prior written permission. -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED -BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND -CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -***********************************************************************/ - -#include "SKP_Silk_tables.h" - -const SKP_uint16 SKP_Silk_type_offset_CDF[ 5 ] = { - 0, 37522, 41030, 44212, 65535 -}; - -const SKP_int SKP_Silk_type_offset_CDF_offset = 2; - - -const SKP_uint16 SKP_Silk_type_offset_joint_CDF[ 4 ][ 5 ] = -{ -{ - 0, 57686, 61230, 62358, 65535 -}, -{ - 0, 18346, 40067, 43659, 65535 -}, -{ - 0, 22694, 24279, 35507, 65535 -}, -{ - 0, 6067, 7215, 13010, 65535 -} -}; - +/*********************************************************************** +Copyright (c) 2006-2010, Skype Limited. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, (subject to the limitations in the disclaimer below) +are permitted provided that the following conditions are met: +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +- Neither the name of Skype Limited, nor the names of specific +contributors, may be used to endorse or promote products derived from +this software without specific prior written permission. +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED +BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +***********************************************************************/ + +#include "SKP_Silk_tables.h" + +const SKP_uint16 SKP_Silk_type_offset_CDF[ 5 ] = { + 0, 37522, 41030, 44212, 65535 +}; + +const SKP_int SKP_Silk_type_offset_CDF_offset = 2; + + +const SKP_uint16 SKP_Silk_type_offset_joint_CDF[ 4 ][ 5 ] = +{ +{ + 0, 57686, 61230, 62358, 65535 +}, +{ + 0, 18346, 40067, 43659, 65535 +}, +{ + 0, 22694, 24279, 35507, 65535 +}, +{ + 0, 6067, 7215, 13010, 65535 +} +}; + diff --git a/jni/silk/src/Silk_FIX.vcproj b/app/src/main/jni/silk/src/Silk_FIX.vcproj similarity index 94% rename from jni/silk/src/Silk_FIX.vcproj rename to app/src/main/jni/silk/src/Silk_FIX.vcproj index 2e76109..e821666 100644 --- a/jni/silk/src/Silk_FIX.vcproj +++ b/app/src/main/jni/silk/src/Silk_FIX.vcproj @@ -1,700 +1,700 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jni/silk16_jni.cpp b/app/src/main/jni/silk16_jni.cpp similarity index 100% rename from jni/silk16_jni.cpp rename to app/src/main/jni/silk16_jni.cpp diff --git a/jni/silk24_jni.cpp b/app/src/main/jni/silk24_jni.cpp similarity index 97% rename from jni/silk24_jni.cpp rename to app/src/main/jni/silk24_jni.cpp index 0dcb213..1f2ef30 100644 --- a/jni/silk24_jni.cpp +++ b/app/src/main/jni/silk24_jni.cpp @@ -1,308 +1,308 @@ -/* - * Copyright (C) 2009 The Sipdroid Open Source Project - * - * This file is part of Sipdroid (http://www.sipdroid.org) - * - * Sipdroid 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 source code 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 source code; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - - -#include - -#include -#include -#include -#include - -/* Define codec specific settings */ -#define MAX_BYTES_ENC_PER_FRAME 250 // Equals peak bitrate of 100 kbps -#define MAX_BYTES_DEC_PER_FRAME 1024 - -#define MAX_INPUT_FRAMES 5 -#define MAX_LBRR_DELAY 2 -#define MAX_FRAME_LENGTH 480 - -#define MAX_FRAME 480 - -#include - -#define LOG_TAG "silk" // text for log tag - -#include "SKP_Silk_SDK_API.h" -#include "SKP_Silk_SigProc_FIX.h" - -#undef DEBUG_SILK24 - -// the header length of the RTP frame (must skip when en/decoding) -#define RTP_HDR_SIZE 12 - -static int codec_open = 0; - -static JavaVM *gJavaVM; -const char *kInterfacePath = "org/sipdroid/pjlib/SILK24"; - -/* encoder parameters */ - - SKP_int32 encSizeBytes; - void *psEnc; - - /* default settings */ - SKP_int fs_kHz = 24; - SKP_int targetRate_bps = 20000; - SKP_int packetSize_ms = 20; - SKP_int frameSizeReadFromFile_ms = 20; - SKP_int packetLoss_perc = 0, smplsSinceLastPacket; - SKP_int INBandFec_enabled = 0, DTX_enabled = 0, quiet = 0; - SKP_SILK_SDK_EncControlStruct encControl; // Struct for input to encoder - - -/* decoder parameters */ - - jbyte payloadToDec[ MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ]; - jshort out[ ( MAX_FRAME_LENGTH << 1 ) * MAX_INPUT_FRAMES ], *outPtr; - SKP_int32 decSizeBytes; - void *psDec; - SKP_SILK_SDK_DecControlStruct DecControl; - -extern "C" -JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK24_open - (JNIEnv *env, jobject obj, jint compression) { - int ret; - - if (codec_open++ != 0) - return (jint)0; - - /* Set the samplingrate that is requested for the output */ - DecControl.sampleRate = 24000; - - /* Create decoder */ - ret = SKP_Silk_SDK_Get_Decoder_Size( &decSizeBytes ); - if( ret ) { - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "\n!!!!!!!! SKP_Silk_SDK_Get_Decoder_Size returned %d", ret ); - } -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "### INIT Decoder decSizeBytes = %d\n", decSizeBytes); -#endif - psDec = malloc( decSizeBytes ); - - /* Reset decoder */ - ret = SKP_Silk_SDK_InitDecoder( psDec ); - if( ret ) { - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "\n!!!!!!!! SKP_Silk_InitDecoder returned %d", ret ); - } - - - /* Create Encoder */ - ret = SKP_Silk_SDK_Get_Encoder_Size( &encSizeBytes ); - if( ret ) { - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "\n!!!!!!!! SKP_Silk_SDK_Get_Encoder_Size returned %d", ret ); - } -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "### INIT Encoder encSizeBytes = %d\n", encSizeBytes); -#endif - psEnc = malloc( encSizeBytes ); - - /* Reset Encoder */ - ret = SKP_Silk_SDK_InitEncoder( psEnc, &encControl ); - if( ret ) { - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "\n!!!!!!!! SKP_Silk_SDK_InitEncoder returned %d", ret ); - } - - /* Set Encoder parameters */ - encControl.sampleRate = fs_kHz * 1000; - encControl.packetSize = packetSize_ms * fs_kHz; - encControl.packetLossPercentage = packetLoss_perc; - encControl.useInBandFEC = INBandFec_enabled; - encControl.useDTX = DTX_enabled; - encControl.complexity = compression; - encControl.bitRate = targetRate_bps; - - return (jint)0; -} - -void Print_Decode_Error_Msg(int errcode) { - switch (errcode) { - case SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nOutput sampling frequency lower than internal decoded sampling frequency\n", errcode); - break; - case SKP_SILK_DEC_PAYLOAD_TOO_LARGE: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nPayload size exceeded the maximum allowed 1024 bytes\n", errcode); - break; - case SKP_SILK_DEC_PAYLOAD_ERROR: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nPayload has bit errors\n", errcode); - break; - } -} - -void Print_Encode_Error_Msg(int errcode) { - switch (errcode) { - case SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nInput length is not a multiplum of 10 ms, or length is longer than the packet length\n", errcode); - break; - case SKP_SILK_ENC_FS_NOT_SUPPORTED: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nSampling frequency not 8000, 12000, 16000 or 24000 Hertz \n", errcode); - break; - case SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nPacket size not 20, 40, 60, 80 or 100 ms\n", errcode); - break; - case SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nAllocated payload buffer too short \n", errcode); - break; - case SKP_SILK_ENC_WRONG_LOSS_RATE: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nLoss rate not between 0 and 100 percent\n", errcode); - break; - case SKP_SILK_ENC_WRONG_COMPLEXITY_SETTING: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nComplexity setting not valid, use 0, 1 or 2\n", errcode); - break; - case SKP_SILK_ENC_WRONG_INBAND_FEC_SETTING: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nInband FEC setting not valid, use 0 or 1\n", errcode); - break; - case SKP_SILK_ENC_WRONG_DTX_SETTING: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nDTX setting not valid, use 0 or 1\n", errcode); - break; - case SKP_SILK_ENC_INTERNAL_ERROR: - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!!!!! Decode_Error_Message: %d\nInternal encoder error\n", errcode); - break; - } -} - -extern "C" -JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK24_encode - (JNIEnv *env, jobject obj, jshortArray lin, jint offset, jbyteArray encoded, jint size) { - - jbyte enc_payload[ MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES ]; - jshort in[ MAX_FRAME_LENGTH * MAX_INPUT_FRAMES ]; - int ret,i,frsz=MAX_FRAME; - SKP_int16 nBytes; - unsigned int lin_pos = 0; - - if (!codec_open) - return 0; - -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "encoding frame size: %d\toffset: %d\n", size, offset); -#endif - - - for (i = 0; i < size; i+=MAX_FRAME) { -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "encoding frame size: %d\toffset: %d i: %d\n", size, offset, i); -#endif - - env->GetShortArrayRegion(lin, offset + i,frsz, in); - /* max payload size */ - nBytes = MAX_BYTES_ENC_PER_FRAME * MAX_INPUT_FRAMES; - - ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)frsz, (SKP_uint8 *)enc_payload, &nBytes ); - if( ret ) { - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!! SKP_Silk_Encode returned: %d\n", ret); - Print_Encode_Error_Msg(ret); - break; - } -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "Enocded nBytes: %d\n", nBytes); -#endif - /* Write payload */ - env->SetByteArrayRegion(encoded, RTP_HDR_SIZE+ lin_pos, nBytes, enc_payload); - lin_pos += nBytes; - } -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "encoding **END** frame size: %d\toffset: %d i: %d lin_pos: %d\n", size, offset, i, lin_pos); -#endif - - return (jint)lin_pos; -} - -extern "C" -JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK24_decode - (JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) { - - jbyte buffer [MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ]; - jshort output_buffer[( MAX_FRAME_LENGTH << 1 ) * MAX_INPUT_FRAMES ]; -// SKP_int16 *outPtr; - - int ret; - SKP_int16 len; -// int tot_len,frames; - - if (!codec_open) - return 0; - -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "##### BEGIN DECODE ******** decoding frame size: %d\n", size); -#endif - - env->GetByteArrayRegion(encoded, RTP_HDR_SIZE, size, buffer); - -// outPtr = output_buffer; -// tot_len = 0; -// frames = 0; - -// do { - ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0,(SKP_uint8 *) buffer, size, output_buffer,&len ); - if( ret ) { - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "!!!!!!!! SKP_Silk_SDK_Decode returned: %d\n", ret); - Print_Decode_Error_Msg(ret); - } -#ifdef DEBUG_SILK24 - __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, - "##### DECODED length: %d\n\t Frame #: %d", len); -#endif -// frames++; -// outPtr += len; -// tot_len += len; - -// } while( DecControl.moreInternalDecoderFrames ); - - env->SetShortArrayRegion(lin, 0, len,output_buffer); - return (jint)len; -} - -extern "C" -JNIEXPORT void JNICALL Java_org_sipdroid_codecs_SILK24_close - (JNIEnv *env, jobject obj) { - - if (--codec_open != 0) - return; - /* Free decoder */ - free( psDec ); - /* Free Encoder */ - free( psEnc ); -} +/* + * Copyright (C) 2009 The Sipdroid Open Source Project + * + * This file is part of Sipdroid (http://www.sipdroid.org) + * + * Sipdroid 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 source code 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 source code; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include + +#include +#include +#include +#include + +/* Define codec specific settings */ +#define MAX_BYTES_ENC_PER_FRAME 250 // Equals peak bitrate of 100 kbps +#define MAX_BYTES_DEC_PER_FRAME 1024 + +#define MAX_INPUT_FRAMES 5 +#define MAX_LBRR_DELAY 2 +#define MAX_FRAME_LENGTH 480 + +#define MAX_FRAME 480 + +#include + +#define LOG_TAG "silk" // text for log tag + +#include "SKP_Silk_SDK_API.h" +#include "SKP_Silk_SigProc_FIX.h" + +#undef DEBUG_SILK24 + +// the header length of the RTP frame (must skip when en/decoding) +#define RTP_HDR_SIZE 12 + +static int codec_open = 0; + +static JavaVM *gJavaVM; +const char *kInterfacePath = "org/sipdroid/pjlib/SILK24"; + +/* encoder parameters */ + + SKP_int32 encSizeBytes; + void *psEnc; + + /* default settings */ + SKP_int fs_kHz = 24; + SKP_int targetRate_bps = 20000; + SKP_int packetSize_ms = 20; + SKP_int frameSizeReadFromFile_ms = 20; + SKP_int packetLoss_perc = 0, smplsSinceLastPacket; + SKP_int INBandFec_enabled = 0, DTX_enabled = 0, quiet = 0; + SKP_SILK_SDK_EncControlStruct encControl; // Struct for input to encoder + + +/* decoder parameters */ + + jbyte payloadToDec[ MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ]; + jshort out[ ( MAX_FRAME_LENGTH << 1 ) * MAX_INPUT_FRAMES ], *outPtr; + SKP_int32 decSizeBytes; + void *psDec; + SKP_SILK_SDK_DecControlStruct DecControl; + +extern "C" +JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK24_open + (JNIEnv *env, jobject obj, jint compression) { + int ret; + + if (codec_open++ != 0) + return (jint)0; + + /* Set the samplingrate that is requested for the output */ + DecControl.sampleRate = 24000; + + /* Create decoder */ + ret = SKP_Silk_SDK_Get_Decoder_Size( &decSizeBytes ); + if( ret ) { + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "\n!!!!!!!! SKP_Silk_SDK_Get_Decoder_Size returned %d", ret ); + } +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "### INIT Decoder decSizeBytes = %d\n", decSizeBytes); +#endif + psDec = malloc( decSizeBytes ); + + /* Reset decoder */ + ret = SKP_Silk_SDK_InitDecoder( psDec ); + if( ret ) { + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "\n!!!!!!!! SKP_Silk_InitDecoder returned %d", ret ); + } + + + /* Create Encoder */ + ret = SKP_Silk_SDK_Get_Encoder_Size( &encSizeBytes ); + if( ret ) { + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "\n!!!!!!!! SKP_Silk_SDK_Get_Encoder_Size returned %d", ret ); + } +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "### INIT Encoder encSizeBytes = %d\n", encSizeBytes); +#endif + psEnc = malloc( encSizeBytes ); + + /* Reset Encoder */ + ret = SKP_Silk_SDK_InitEncoder( psEnc, &encControl ); + if( ret ) { + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "\n!!!!!!!! SKP_Silk_SDK_InitEncoder returned %d", ret ); + } + + /* Set Encoder parameters */ + encControl.sampleRate = fs_kHz * 1000; + encControl.packetSize = packetSize_ms * fs_kHz; + encControl.packetLossPercentage = packetLoss_perc; + encControl.useInBandFEC = INBandFec_enabled; + encControl.useDTX = DTX_enabled; + encControl.complexity = compression; + encControl.bitRate = targetRate_bps; + + return (jint)0; +} + +void Print_Decode_Error_Msg(int errcode) { + switch (errcode) { + case SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nOutput sampling frequency lower than internal decoded sampling frequency\n", errcode); + break; + case SKP_SILK_DEC_PAYLOAD_TOO_LARGE: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nPayload size exceeded the maximum allowed 1024 bytes\n", errcode); + break; + case SKP_SILK_DEC_PAYLOAD_ERROR: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nPayload has bit errors\n", errcode); + break; + } +} + +void Print_Encode_Error_Msg(int errcode) { + switch (errcode) { + case SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nInput length is not a multiplum of 10 ms, or length is longer than the packet length\n", errcode); + break; + case SKP_SILK_ENC_FS_NOT_SUPPORTED: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nSampling frequency not 8000, 12000, 16000 or 24000 Hertz \n", errcode); + break; + case SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nPacket size not 20, 40, 60, 80 or 100 ms\n", errcode); + break; + case SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nAllocated payload buffer too short \n", errcode); + break; + case SKP_SILK_ENC_WRONG_LOSS_RATE: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nLoss rate not between 0 and 100 percent\n", errcode); + break; + case SKP_SILK_ENC_WRONG_COMPLEXITY_SETTING: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nComplexity setting not valid, use 0, 1 or 2\n", errcode); + break; + case SKP_SILK_ENC_WRONG_INBAND_FEC_SETTING: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nInband FEC setting not valid, use 0 or 1\n", errcode); + break; + case SKP_SILK_ENC_WRONG_DTX_SETTING: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nDTX setting not valid, use 0 or 1\n", errcode); + break; + case SKP_SILK_ENC_INTERNAL_ERROR: + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!!!!! Decode_Error_Message: %d\nInternal encoder error\n", errcode); + break; + } +} + +extern "C" +JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK24_encode + (JNIEnv *env, jobject obj, jshortArray lin, jint offset, jbyteArray encoded, jint size) { + + jbyte enc_payload[ MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES ]; + jshort in[ MAX_FRAME_LENGTH * MAX_INPUT_FRAMES ]; + int ret,i,frsz=MAX_FRAME; + SKP_int16 nBytes; + unsigned int lin_pos = 0; + + if (!codec_open) + return 0; + +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "encoding frame size: %d\toffset: %d\n", size, offset); +#endif + + + for (i = 0; i < size; i+=MAX_FRAME) { +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "encoding frame size: %d\toffset: %d i: %d\n", size, offset, i); +#endif + + env->GetShortArrayRegion(lin, offset + i,frsz, in); + /* max payload size */ + nBytes = MAX_BYTES_ENC_PER_FRAME * MAX_INPUT_FRAMES; + + ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)frsz, (SKP_uint8 *)enc_payload, &nBytes ); + if( ret ) { + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!! SKP_Silk_Encode returned: %d\n", ret); + Print_Encode_Error_Msg(ret); + break; + } +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "Enocded nBytes: %d\n", nBytes); +#endif + /* Write payload */ + env->SetByteArrayRegion(encoded, RTP_HDR_SIZE+ lin_pos, nBytes, enc_payload); + lin_pos += nBytes; + } +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "encoding **END** frame size: %d\toffset: %d i: %d lin_pos: %d\n", size, offset, i, lin_pos); +#endif + + return (jint)lin_pos; +} + +extern "C" +JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_SILK24_decode + (JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) { + + jbyte buffer [MAX_BYTES_DEC_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ]; + jshort output_buffer[( MAX_FRAME_LENGTH << 1 ) * MAX_INPUT_FRAMES ]; +// SKP_int16 *outPtr; + + int ret; + SKP_int16 len; +// int tot_len,frames; + + if (!codec_open) + return 0; + +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "##### BEGIN DECODE ******** decoding frame size: %d\n", size); +#endif + + env->GetByteArrayRegion(encoded, RTP_HDR_SIZE, size, buffer); + +// outPtr = output_buffer; +// tot_len = 0; +// frames = 0; + +// do { + ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0,(SKP_uint8 *) buffer, size, output_buffer,&len ); + if( ret ) { + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "!!!!!!!! SKP_Silk_SDK_Decode returned: %d\n", ret); + Print_Decode_Error_Msg(ret); + } +#ifdef DEBUG_SILK24 + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, + "##### DECODED length: %d\n\t Frame #: %d", len); +#endif +// frames++; +// outPtr += len; +// tot_len += len; + +// } while( DecControl.moreInternalDecoderFrames ); + + env->SetShortArrayRegion(lin, 0, len,output_buffer); + return (jint)len; +} + +extern "C" +JNIEXPORT void JNICALL Java_org_sipdroid_codecs_SILK24_close + (JNIEnv *env, jobject obj) { + + if (--codec_open != 0) + return; + /* Free decoder */ + free( psDec ); + /* Free Encoder */ + free( psEnc ); +} diff --git a/jni/silk8_jni.cpp b/app/src/main/jni/silk8_jni.cpp similarity index 100% rename from jni/silk8_jni.cpp rename to app/src/main/jni/silk8_jni.cpp diff --git a/jni/spandsp/floating_fudge.h b/app/src/main/jni/spandsp/floating_fudge.h similarity index 100% rename from jni/spandsp/floating_fudge.h rename to app/src/main/jni/spandsp/floating_fudge.h diff --git a/jni/spandsp/g722.c b/app/src/main/jni/spandsp/g722.c similarity index 100% rename from jni/spandsp/g722.c rename to app/src/main/jni/spandsp/g722.c diff --git a/jni/spandsp/gsm0610_decode.c b/app/src/main/jni/spandsp/gsm0610_decode.c similarity index 100% rename from jni/spandsp/gsm0610_decode.c rename to app/src/main/jni/spandsp/gsm0610_decode.c diff --git a/jni/spandsp/gsm0610_encode.c b/app/src/main/jni/spandsp/gsm0610_encode.c similarity index 100% rename from jni/spandsp/gsm0610_encode.c rename to app/src/main/jni/spandsp/gsm0610_encode.c diff --git a/jni/spandsp/gsm0610_local.h b/app/src/main/jni/spandsp/gsm0610_local.h similarity index 100% rename from jni/spandsp/gsm0610_local.h rename to app/src/main/jni/spandsp/gsm0610_local.h diff --git a/jni/spandsp/gsm0610_long_term.c b/app/src/main/jni/spandsp/gsm0610_long_term.c similarity index 100% rename from jni/spandsp/gsm0610_long_term.c rename to app/src/main/jni/spandsp/gsm0610_long_term.c diff --git a/jni/spandsp/gsm0610_lpc.c b/app/src/main/jni/spandsp/gsm0610_lpc.c similarity index 100% rename from jni/spandsp/gsm0610_lpc.c rename to app/src/main/jni/spandsp/gsm0610_lpc.c diff --git a/jni/spandsp/gsm0610_preprocess.c b/app/src/main/jni/spandsp/gsm0610_preprocess.c similarity index 100% rename from jni/spandsp/gsm0610_preprocess.c rename to app/src/main/jni/spandsp/gsm0610_preprocess.c diff --git a/jni/spandsp/gsm0610_rpe.c b/app/src/main/jni/spandsp/gsm0610_rpe.c similarity index 100% rename from jni/spandsp/gsm0610_rpe.c rename to app/src/main/jni/spandsp/gsm0610_rpe.c diff --git a/jni/spandsp/gsm0610_short_term.c b/app/src/main/jni/spandsp/gsm0610_short_term.c similarity index 100% rename from jni/spandsp/gsm0610_short_term.c rename to app/src/main/jni/spandsp/gsm0610_short_term.c diff --git a/jni/spandsp/mmx_sse_decs.h b/app/src/main/jni/spandsp/mmx_sse_decs.h similarity index 100% rename from jni/spandsp/mmx_sse_decs.h rename to app/src/main/jni/spandsp/mmx_sse_decs.h diff --git a/jni/spandsp/spandsp.h b/app/src/main/jni/spandsp/spandsp.h similarity index 100% rename from jni/spandsp/spandsp.h rename to app/src/main/jni/spandsp/spandsp.h diff --git a/jni/spandsp/spandsp/adsi.h b/app/src/main/jni/spandsp/spandsp/adsi.h similarity index 100% rename from jni/spandsp/spandsp/adsi.h rename to app/src/main/jni/spandsp/spandsp/adsi.h diff --git a/jni/spandsp/spandsp/arctan2.h b/app/src/main/jni/spandsp/spandsp/arctan2.h similarity index 100% rename from jni/spandsp/spandsp/arctan2.h rename to app/src/main/jni/spandsp/spandsp/arctan2.h diff --git a/jni/spandsp/spandsp/async.h b/app/src/main/jni/spandsp/spandsp/async.h similarity index 100% rename from jni/spandsp/spandsp/async.h rename to app/src/main/jni/spandsp/spandsp/async.h diff --git a/jni/spandsp/spandsp/at_interpreter.h b/app/src/main/jni/spandsp/spandsp/at_interpreter.h similarity index 100% rename from jni/spandsp/spandsp/at_interpreter.h rename to app/src/main/jni/spandsp/spandsp/at_interpreter.h diff --git a/jni/spandsp/spandsp/awgn.h b/app/src/main/jni/spandsp/spandsp/awgn.h similarity index 100% rename from jni/spandsp/spandsp/awgn.h rename to app/src/main/jni/spandsp/spandsp/awgn.h diff --git a/jni/spandsp/spandsp/bell_r2_mf.h b/app/src/main/jni/spandsp/spandsp/bell_r2_mf.h similarity index 100% rename from jni/spandsp/spandsp/bell_r2_mf.h rename to app/src/main/jni/spandsp/spandsp/bell_r2_mf.h diff --git a/jni/spandsp/spandsp/bert.h b/app/src/main/jni/spandsp/spandsp/bert.h similarity index 100% rename from jni/spandsp/spandsp/bert.h rename to app/src/main/jni/spandsp/spandsp/bert.h diff --git a/jni/spandsp/spandsp/biquad.h b/app/src/main/jni/spandsp/spandsp/biquad.h similarity index 100% rename from jni/spandsp/spandsp/biquad.h rename to app/src/main/jni/spandsp/spandsp/biquad.h diff --git a/jni/spandsp/spandsp/bit_operations.h b/app/src/main/jni/spandsp/spandsp/bit_operations.h similarity index 100% rename from jni/spandsp/spandsp/bit_operations.h rename to app/src/main/jni/spandsp/spandsp/bit_operations.h diff --git a/jni/spandsp/spandsp/bitstream.h b/app/src/main/jni/spandsp/spandsp/bitstream.h similarity index 100% rename from jni/spandsp/spandsp/bitstream.h rename to app/src/main/jni/spandsp/spandsp/bitstream.h diff --git a/jni/spandsp/spandsp/complex.h b/app/src/main/jni/spandsp/spandsp/complex.h similarity index 100% rename from jni/spandsp/spandsp/complex.h rename to app/src/main/jni/spandsp/spandsp/complex.h diff --git a/jni/spandsp/spandsp/complex_filters.h b/app/src/main/jni/spandsp/spandsp/complex_filters.h similarity index 100% rename from jni/spandsp/spandsp/complex_filters.h rename to app/src/main/jni/spandsp/spandsp/complex_filters.h diff --git a/jni/spandsp/spandsp/complex_vector_float.h b/app/src/main/jni/spandsp/spandsp/complex_vector_float.h similarity index 100% rename from jni/spandsp/spandsp/complex_vector_float.h rename to app/src/main/jni/spandsp/spandsp/complex_vector_float.h diff --git a/jni/spandsp/spandsp/complex_vector_int.h b/app/src/main/jni/spandsp/spandsp/complex_vector_int.h similarity index 100% rename from jni/spandsp/spandsp/complex_vector_int.h rename to app/src/main/jni/spandsp/spandsp/complex_vector_int.h diff --git a/jni/spandsp/spandsp/crc.h b/app/src/main/jni/spandsp/spandsp/crc.h similarity index 100% rename from jni/spandsp/spandsp/crc.h rename to app/src/main/jni/spandsp/spandsp/crc.h diff --git a/jni/spandsp/spandsp/dc_restore.h b/app/src/main/jni/spandsp/spandsp/dc_restore.h similarity index 100% rename from jni/spandsp/spandsp/dc_restore.h rename to app/src/main/jni/spandsp/spandsp/dc_restore.h diff --git a/jni/spandsp/spandsp/dds.h b/app/src/main/jni/spandsp/spandsp/dds.h similarity index 100% rename from jni/spandsp/spandsp/dds.h rename to app/src/main/jni/spandsp/spandsp/dds.h diff --git a/jni/spandsp/spandsp/dtmf.h b/app/src/main/jni/spandsp/spandsp/dtmf.h similarity index 100% rename from jni/spandsp/spandsp/dtmf.h rename to app/src/main/jni/spandsp/spandsp/dtmf.h diff --git a/jni/spandsp/spandsp/echo.h b/app/src/main/jni/spandsp/spandsp/echo.h similarity index 100% rename from jni/spandsp/spandsp/echo.h rename to app/src/main/jni/spandsp/spandsp/echo.h diff --git a/jni/spandsp/spandsp/expose.h b/app/src/main/jni/spandsp/spandsp/expose.h similarity index 100% rename from jni/spandsp/spandsp/expose.h rename to app/src/main/jni/spandsp/spandsp/expose.h diff --git a/jni/spandsp/spandsp/fast_convert.h b/app/src/main/jni/spandsp/spandsp/fast_convert.h similarity index 100% rename from jni/spandsp/spandsp/fast_convert.h rename to app/src/main/jni/spandsp/spandsp/fast_convert.h diff --git a/jni/spandsp/spandsp/fax.h b/app/src/main/jni/spandsp/spandsp/fax.h similarity index 100% rename from jni/spandsp/spandsp/fax.h rename to app/src/main/jni/spandsp/spandsp/fax.h diff --git a/jni/spandsp/spandsp/fax_modems.h b/app/src/main/jni/spandsp/spandsp/fax_modems.h similarity index 100% rename from jni/spandsp/spandsp/fax_modems.h rename to app/src/main/jni/spandsp/spandsp/fax_modems.h diff --git a/jni/spandsp/spandsp/fir.h b/app/src/main/jni/spandsp/spandsp/fir.h similarity index 100% rename from jni/spandsp/spandsp/fir.h rename to app/src/main/jni/spandsp/spandsp/fir.h diff --git a/jni/spandsp/spandsp/fsk.h b/app/src/main/jni/spandsp/spandsp/fsk.h similarity index 100% rename from jni/spandsp/spandsp/fsk.h rename to app/src/main/jni/spandsp/spandsp/fsk.h diff --git a/jni/spandsp/spandsp/g168models.h b/app/src/main/jni/spandsp/spandsp/g168models.h similarity index 100% rename from jni/spandsp/spandsp/g168models.h rename to app/src/main/jni/spandsp/spandsp/g168models.h diff --git a/jni/spandsp/spandsp/g711.h b/app/src/main/jni/spandsp/spandsp/g711.h similarity index 100% rename from jni/spandsp/spandsp/g711.h rename to app/src/main/jni/spandsp/spandsp/g711.h diff --git a/jni/spandsp/spandsp/g722.h b/app/src/main/jni/spandsp/spandsp/g722.h similarity index 100% rename from jni/spandsp/spandsp/g722.h rename to app/src/main/jni/spandsp/spandsp/g722.h diff --git a/jni/spandsp/spandsp/g726.h b/app/src/main/jni/spandsp/spandsp/g726.h similarity index 100% rename from jni/spandsp/spandsp/g726.h rename to app/src/main/jni/spandsp/spandsp/g726.h diff --git a/jni/spandsp/spandsp/gsm0610.h b/app/src/main/jni/spandsp/spandsp/gsm0610.h similarity index 100% rename from jni/spandsp/spandsp/gsm0610.h rename to app/src/main/jni/spandsp/spandsp/gsm0610.h diff --git a/jni/spandsp/spandsp/hdlc.h b/app/src/main/jni/spandsp/spandsp/hdlc.h similarity index 100% rename from jni/spandsp/spandsp/hdlc.h rename to app/src/main/jni/spandsp/spandsp/hdlc.h diff --git a/jni/spandsp/spandsp/ima_adpcm.h b/app/src/main/jni/spandsp/spandsp/ima_adpcm.h similarity index 100% rename from jni/spandsp/spandsp/ima_adpcm.h rename to app/src/main/jni/spandsp/spandsp/ima_adpcm.h diff --git a/jni/spandsp/spandsp/logging.h b/app/src/main/jni/spandsp/spandsp/logging.h similarity index 100% rename from jni/spandsp/spandsp/logging.h rename to app/src/main/jni/spandsp/spandsp/logging.h diff --git a/jni/spandsp/spandsp/lpc10.h b/app/src/main/jni/spandsp/spandsp/lpc10.h similarity index 100% rename from jni/spandsp/spandsp/lpc10.h rename to app/src/main/jni/spandsp/spandsp/lpc10.h diff --git a/jni/spandsp/spandsp/modem_connect_tones.h b/app/src/main/jni/spandsp/spandsp/modem_connect_tones.h similarity index 100% rename from jni/spandsp/spandsp/modem_connect_tones.h rename to app/src/main/jni/spandsp/spandsp/modem_connect_tones.h diff --git a/jni/spandsp/spandsp/modem_echo.h b/app/src/main/jni/spandsp/spandsp/modem_echo.h similarity index 100% rename from jni/spandsp/spandsp/modem_echo.h rename to app/src/main/jni/spandsp/spandsp/modem_echo.h diff --git a/jni/spandsp/spandsp/myinttypes.h b/app/src/main/jni/spandsp/spandsp/myinttypes.h similarity index 100% rename from jni/spandsp/spandsp/myinttypes.h rename to app/src/main/jni/spandsp/spandsp/myinttypes.h diff --git a/jni/spandsp/spandsp/noise.h b/app/src/main/jni/spandsp/spandsp/noise.h similarity index 100% rename from jni/spandsp/spandsp/noise.h rename to app/src/main/jni/spandsp/spandsp/noise.h diff --git a/jni/spandsp/spandsp/oki_adpcm.h b/app/src/main/jni/spandsp/spandsp/oki_adpcm.h similarity index 100% rename from jni/spandsp/spandsp/oki_adpcm.h rename to app/src/main/jni/spandsp/spandsp/oki_adpcm.h diff --git a/jni/spandsp/spandsp/playout.h b/app/src/main/jni/spandsp/spandsp/playout.h similarity index 100% rename from jni/spandsp/spandsp/playout.h rename to app/src/main/jni/spandsp/spandsp/playout.h diff --git a/jni/spandsp/spandsp/plc.h b/app/src/main/jni/spandsp/spandsp/plc.h similarity index 100% rename from jni/spandsp/spandsp/plc.h rename to app/src/main/jni/spandsp/spandsp/plc.h diff --git a/jni/spandsp/spandsp/power_meter.h b/app/src/main/jni/spandsp/spandsp/power_meter.h similarity index 100% rename from jni/spandsp/spandsp/power_meter.h rename to app/src/main/jni/spandsp/spandsp/power_meter.h diff --git a/jni/spandsp/spandsp/private/g722.h b/app/src/main/jni/spandsp/spandsp/private/g722.h similarity index 100% rename from jni/spandsp/spandsp/private/g722.h rename to app/src/main/jni/spandsp/spandsp/private/g722.h diff --git a/jni/spandsp/spandsp/private/gsm0610.h b/app/src/main/jni/spandsp/spandsp/private/gsm0610.h similarity index 100% rename from jni/spandsp/spandsp/private/gsm0610.h rename to app/src/main/jni/spandsp/spandsp/private/gsm0610.h diff --git a/jni/spandsp/spandsp/queue.h b/app/src/main/jni/spandsp/spandsp/queue.h similarity index 100% rename from jni/spandsp/spandsp/queue.h rename to app/src/main/jni/spandsp/spandsp/queue.h diff --git a/jni/spandsp/spandsp/saturated.h b/app/src/main/jni/spandsp/spandsp/saturated.h similarity index 100% rename from jni/spandsp/spandsp/saturated.h rename to app/src/main/jni/spandsp/spandsp/saturated.h diff --git a/jni/spandsp/spandsp/schedule.h b/app/src/main/jni/spandsp/spandsp/schedule.h similarity index 100% rename from jni/spandsp/spandsp/schedule.h rename to app/src/main/jni/spandsp/spandsp/schedule.h diff --git a/jni/spandsp/spandsp/sig_tone.h b/app/src/main/jni/spandsp/spandsp/sig_tone.h similarity index 100% rename from jni/spandsp/spandsp/sig_tone.h rename to app/src/main/jni/spandsp/spandsp/sig_tone.h diff --git a/jni/spandsp/spandsp/silence_gen.h b/app/src/main/jni/spandsp/spandsp/silence_gen.h similarity index 100% rename from jni/spandsp/spandsp/silence_gen.h rename to app/src/main/jni/spandsp/spandsp/silence_gen.h diff --git a/jni/spandsp/spandsp/super_tone_rx.h b/app/src/main/jni/spandsp/spandsp/super_tone_rx.h similarity index 100% rename from jni/spandsp/spandsp/super_tone_rx.h rename to app/src/main/jni/spandsp/spandsp/super_tone_rx.h diff --git a/jni/spandsp/spandsp/super_tone_tx.h b/app/src/main/jni/spandsp/spandsp/super_tone_tx.h similarity index 100% rename from jni/spandsp/spandsp/super_tone_tx.h rename to app/src/main/jni/spandsp/spandsp/super_tone_tx.h diff --git a/jni/spandsp/spandsp/swept_tone.h b/app/src/main/jni/spandsp/spandsp/swept_tone.h similarity index 100% rename from jni/spandsp/spandsp/swept_tone.h rename to app/src/main/jni/spandsp/spandsp/swept_tone.h diff --git a/jni/spandsp/spandsp/t30.h b/app/src/main/jni/spandsp/spandsp/t30.h similarity index 100% rename from jni/spandsp/spandsp/t30.h rename to app/src/main/jni/spandsp/spandsp/t30.h diff --git a/jni/spandsp/spandsp/t30_api.h b/app/src/main/jni/spandsp/spandsp/t30_api.h similarity index 100% rename from jni/spandsp/spandsp/t30_api.h rename to app/src/main/jni/spandsp/spandsp/t30_api.h diff --git a/jni/spandsp/spandsp/t30_fcf.h b/app/src/main/jni/spandsp/spandsp/t30_fcf.h similarity index 100% rename from jni/spandsp/spandsp/t30_fcf.h rename to app/src/main/jni/spandsp/spandsp/t30_fcf.h diff --git a/jni/spandsp/spandsp/t30_logging.h b/app/src/main/jni/spandsp/spandsp/t30_logging.h similarity index 100% rename from jni/spandsp/spandsp/t30_logging.h rename to app/src/main/jni/spandsp/spandsp/t30_logging.h diff --git a/jni/spandsp/spandsp/t31.h b/app/src/main/jni/spandsp/spandsp/t31.h similarity index 100% rename from jni/spandsp/spandsp/t31.h rename to app/src/main/jni/spandsp/spandsp/t31.h diff --git a/jni/spandsp/spandsp/t35.h b/app/src/main/jni/spandsp/spandsp/t35.h similarity index 100% rename from jni/spandsp/spandsp/t35.h rename to app/src/main/jni/spandsp/spandsp/t35.h diff --git a/jni/spandsp/spandsp/t38_core.h b/app/src/main/jni/spandsp/spandsp/t38_core.h similarity index 100% rename from jni/spandsp/spandsp/t38_core.h rename to app/src/main/jni/spandsp/spandsp/t38_core.h diff --git a/jni/spandsp/spandsp/t38_gateway.h b/app/src/main/jni/spandsp/spandsp/t38_gateway.h similarity index 100% rename from jni/spandsp/spandsp/t38_gateway.h rename to app/src/main/jni/spandsp/spandsp/t38_gateway.h diff --git a/jni/spandsp/spandsp/t38_non_ecm_buffer.h b/app/src/main/jni/spandsp/spandsp/t38_non_ecm_buffer.h similarity index 100% rename from jni/spandsp/spandsp/t38_non_ecm_buffer.h rename to app/src/main/jni/spandsp/spandsp/t38_non_ecm_buffer.h diff --git a/jni/spandsp/spandsp/t38_terminal.h b/app/src/main/jni/spandsp/spandsp/t38_terminal.h similarity index 100% rename from jni/spandsp/spandsp/t38_terminal.h rename to app/src/main/jni/spandsp/spandsp/t38_terminal.h diff --git a/jni/spandsp/spandsp/t4_rx.h b/app/src/main/jni/spandsp/spandsp/t4_rx.h similarity index 100% rename from jni/spandsp/spandsp/t4_rx.h rename to app/src/main/jni/spandsp/spandsp/t4_rx.h diff --git a/jni/spandsp/spandsp/t4_tx.h b/app/src/main/jni/spandsp/spandsp/t4_tx.h similarity index 100% rename from jni/spandsp/spandsp/t4_tx.h rename to app/src/main/jni/spandsp/spandsp/t4_tx.h diff --git a/jni/spandsp/spandsp/telephony.h b/app/src/main/jni/spandsp/spandsp/telephony.h similarity index 100% rename from jni/spandsp/spandsp/telephony.h rename to app/src/main/jni/spandsp/spandsp/telephony.h diff --git a/jni/spandsp/spandsp/time_scale.h b/app/src/main/jni/spandsp/spandsp/time_scale.h similarity index 100% rename from jni/spandsp/spandsp/time_scale.h rename to app/src/main/jni/spandsp/spandsp/time_scale.h diff --git a/jni/spandsp/spandsp/timing.h b/app/src/main/jni/spandsp/spandsp/timing.h similarity index 100% rename from jni/spandsp/spandsp/timing.h rename to app/src/main/jni/spandsp/spandsp/timing.h diff --git a/jni/spandsp/spandsp/tone_detect.h b/app/src/main/jni/spandsp/spandsp/tone_detect.h similarity index 100% rename from jni/spandsp/spandsp/tone_detect.h rename to app/src/main/jni/spandsp/spandsp/tone_detect.h diff --git a/jni/spandsp/spandsp/tone_generate.h b/app/src/main/jni/spandsp/spandsp/tone_generate.h similarity index 100% rename from jni/spandsp/spandsp/tone_generate.h rename to app/src/main/jni/spandsp/spandsp/tone_generate.h diff --git a/jni/spandsp/spandsp/v17rx.h b/app/src/main/jni/spandsp/spandsp/v17rx.h similarity index 100% rename from jni/spandsp/spandsp/v17rx.h rename to app/src/main/jni/spandsp/spandsp/v17rx.h diff --git a/jni/spandsp/spandsp/v17tx.h b/app/src/main/jni/spandsp/spandsp/v17tx.h similarity index 100% rename from jni/spandsp/spandsp/v17tx.h rename to app/src/main/jni/spandsp/spandsp/v17tx.h diff --git a/jni/spandsp/spandsp/v18.h b/app/src/main/jni/spandsp/spandsp/v18.h similarity index 100% rename from jni/spandsp/spandsp/v18.h rename to app/src/main/jni/spandsp/spandsp/v18.h diff --git a/jni/spandsp/spandsp/v22bis.h b/app/src/main/jni/spandsp/spandsp/v22bis.h similarity index 100% rename from jni/spandsp/spandsp/v22bis.h rename to app/src/main/jni/spandsp/spandsp/v22bis.h diff --git a/jni/spandsp/spandsp/v27ter_rx.h b/app/src/main/jni/spandsp/spandsp/v27ter_rx.h similarity index 100% rename from jni/spandsp/spandsp/v27ter_rx.h rename to app/src/main/jni/spandsp/spandsp/v27ter_rx.h diff --git a/jni/spandsp/spandsp/v27ter_tx.h b/app/src/main/jni/spandsp/spandsp/v27ter_tx.h similarity index 100% rename from jni/spandsp/spandsp/v27ter_tx.h rename to app/src/main/jni/spandsp/spandsp/v27ter_tx.h diff --git a/jni/spandsp/spandsp/v29rx.h b/app/src/main/jni/spandsp/spandsp/v29rx.h similarity index 100% rename from jni/spandsp/spandsp/v29rx.h rename to app/src/main/jni/spandsp/spandsp/v29rx.h diff --git a/jni/spandsp/spandsp/v29tx.h b/app/src/main/jni/spandsp/spandsp/v29tx.h similarity index 100% rename from jni/spandsp/spandsp/v29tx.h rename to app/src/main/jni/spandsp/spandsp/v29tx.h diff --git a/jni/spandsp/spandsp/v42.h b/app/src/main/jni/spandsp/spandsp/v42.h similarity index 100% rename from jni/spandsp/spandsp/v42.h rename to app/src/main/jni/spandsp/spandsp/v42.h diff --git a/jni/spandsp/spandsp/v42bis.h b/app/src/main/jni/spandsp/spandsp/v42bis.h similarity index 100% rename from jni/spandsp/spandsp/v42bis.h rename to app/src/main/jni/spandsp/spandsp/v42bis.h diff --git a/jni/spandsp/spandsp/v8.h b/app/src/main/jni/spandsp/spandsp/v8.h similarity index 100% rename from jni/spandsp/spandsp/v8.h rename to app/src/main/jni/spandsp/spandsp/v8.h diff --git a/jni/spandsp/spandsp/vector_float.h b/app/src/main/jni/spandsp/spandsp/vector_float.h similarity index 100% rename from jni/spandsp/spandsp/vector_float.h rename to app/src/main/jni/spandsp/spandsp/vector_float.h diff --git a/jni/spandsp/spandsp/vector_int.h b/app/src/main/jni/spandsp/spandsp/vector_int.h similarity index 100% rename from jni/spandsp/spandsp/vector_int.h rename to app/src/main/jni/spandsp/spandsp/vector_int.h diff --git a/jni/spandsp/spandsp/version.h b/app/src/main/jni/spandsp/spandsp/version.h similarity index 100% rename from jni/spandsp/spandsp/version.h rename to app/src/main/jni/spandsp/spandsp/version.h diff --git a/jni/spandsp/vector_int.c b/app/src/main/jni/spandsp/vector_int.c similarity index 100% rename from jni/spandsp/vector_int.c rename to app/src/main/jni/spandsp/vector_int.c diff --git a/jni/speex-1.2rc1/COPYING b/app/src/main/jni/speex-1.2rc1/COPYING similarity index 100% rename from jni/speex-1.2rc1/COPYING rename to app/src/main/jni/speex-1.2rc1/COPYING diff --git a/jni/speex-1.2rc1/README.android b/app/src/main/jni/speex-1.2rc1/README.android similarity index 100% rename from jni/speex-1.2rc1/README.android rename to app/src/main/jni/speex-1.2rc1/README.android diff --git a/jni/speex-1.2rc1/include/speex/speex.h b/app/src/main/jni/speex-1.2rc1/include/speex/speex.h similarity index 100% rename from jni/speex-1.2rc1/include/speex/speex.h rename to app/src/main/jni/speex-1.2rc1/include/speex/speex.h diff --git a/jni/speex-1.2rc1/include/speex/speex_bits.h b/app/src/main/jni/speex-1.2rc1/include/speex/speex_bits.h similarity index 100% rename from jni/speex-1.2rc1/include/speex/speex_bits.h rename to app/src/main/jni/speex-1.2rc1/include/speex/speex_bits.h diff --git a/jni/speex-1.2rc1/include/speex/speex_callbacks.h b/app/src/main/jni/speex-1.2rc1/include/speex/speex_callbacks.h similarity index 100% rename from jni/speex-1.2rc1/include/speex/speex_callbacks.h rename to app/src/main/jni/speex-1.2rc1/include/speex/speex_callbacks.h diff --git a/jni/speex-1.2rc1/include/speex/speex_config_types.h b/app/src/main/jni/speex-1.2rc1/include/speex/speex_config_types.h similarity index 100% rename from jni/speex-1.2rc1/include/speex/speex_config_types.h rename to app/src/main/jni/speex-1.2rc1/include/speex/speex_config_types.h diff --git a/jni/speex-1.2rc1/include/speex/speex_types.h b/app/src/main/jni/speex-1.2rc1/include/speex/speex_types.h similarity index 100% rename from jni/speex-1.2rc1/include/speex/speex_types.h rename to app/src/main/jni/speex-1.2rc1/include/speex/speex_types.h diff --git a/jni/speex-1.2rc1/libspeex/arch.h b/app/src/main/jni/speex-1.2rc1/libspeex/arch.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/arch.h rename to app/src/main/jni/speex-1.2rc1/libspeex/arch.h diff --git a/jni/speex-1.2rc1/libspeex/bits.c b/app/src/main/jni/speex-1.2rc1/libspeex/bits.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/bits.c rename to app/src/main/jni/speex-1.2rc1/libspeex/bits.c diff --git a/jni/speex-1.2rc1/libspeex/cb_search.c b/app/src/main/jni/speex-1.2rc1/libspeex/cb_search.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/cb_search.c rename to app/src/main/jni/speex-1.2rc1/libspeex/cb_search.c diff --git a/jni/speex-1.2rc1/libspeex/cb_search.h b/app/src/main/jni/speex-1.2rc1/libspeex/cb_search.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/cb_search.h rename to app/src/main/jni/speex-1.2rc1/libspeex/cb_search.h diff --git a/jni/speex-1.2rc1/libspeex/exc_10_16_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/exc_10_16_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/exc_10_16_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/exc_10_16_table.c diff --git a/jni/speex-1.2rc1/libspeex/exc_10_32_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/exc_10_32_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/exc_10_32_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/exc_10_32_table.c diff --git a/jni/speex-1.2rc1/libspeex/exc_20_32_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/exc_20_32_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/exc_20_32_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/exc_20_32_table.c diff --git a/jni/speex-1.2rc1/libspeex/exc_5_256_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/exc_5_256_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/exc_5_256_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/exc_5_256_table.c diff --git a/jni/speex-1.2rc1/libspeex/exc_5_64_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/exc_5_64_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/exc_5_64_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/exc_5_64_table.c diff --git a/jni/speex-1.2rc1/libspeex/exc_8_128_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/exc_8_128_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/exc_8_128_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/exc_8_128_table.c diff --git a/jni/speex-1.2rc1/libspeex/filters.c b/app/src/main/jni/speex-1.2rc1/libspeex/filters.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/filters.c rename to app/src/main/jni/speex-1.2rc1/libspeex/filters.c diff --git a/jni/speex-1.2rc1/libspeex/filters.h b/app/src/main/jni/speex-1.2rc1/libspeex/filters.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/filters.h rename to app/src/main/jni/speex-1.2rc1/libspeex/filters.h diff --git a/jni/speex-1.2rc1/libspeex/fixed_generic.h b/app/src/main/jni/speex-1.2rc1/libspeex/fixed_generic.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/fixed_generic.h rename to app/src/main/jni/speex-1.2rc1/libspeex/fixed_generic.h diff --git a/jni/speex-1.2rc1/libspeex/gain_table.c b/app/src/main/jni/speex-1.2rc1/libspeex/gain_table.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/gain_table.c rename to app/src/main/jni/speex-1.2rc1/libspeex/gain_table.c diff --git a/jni/speex-1.2rc1/libspeex/gain_table_lbr.c b/app/src/main/jni/speex-1.2rc1/libspeex/gain_table_lbr.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/gain_table_lbr.c rename to app/src/main/jni/speex-1.2rc1/libspeex/gain_table_lbr.c diff --git a/jni/speex-1.2rc1/libspeex/high_lsp_tables.c b/app/src/main/jni/speex-1.2rc1/libspeex/high_lsp_tables.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/high_lsp_tables.c rename to app/src/main/jni/speex-1.2rc1/libspeex/high_lsp_tables.c diff --git a/jni/speex-1.2rc1/libspeex/lpc.c b/app/src/main/jni/speex-1.2rc1/libspeex/lpc.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/lpc.c rename to app/src/main/jni/speex-1.2rc1/libspeex/lpc.c diff --git a/jni/speex-1.2rc1/libspeex/lpc.h b/app/src/main/jni/speex-1.2rc1/libspeex/lpc.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/lpc.h rename to app/src/main/jni/speex-1.2rc1/libspeex/lpc.h diff --git a/jni/speex-1.2rc1/libspeex/lsp.c b/app/src/main/jni/speex-1.2rc1/libspeex/lsp.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/lsp.c rename to app/src/main/jni/speex-1.2rc1/libspeex/lsp.c diff --git a/jni/speex-1.2rc1/libspeex/lsp.h b/app/src/main/jni/speex-1.2rc1/libspeex/lsp.h similarity index 97% rename from jni/speex-1.2rc1/libspeex/lsp.h rename to app/src/main/jni/speex-1.2rc1/libspeex/lsp.h index 648652f..b55bd42 100644 --- a/jni/speex-1.2rc1/libspeex/lsp.h +++ b/app/src/main/jni/speex-1.2rc1/libspeex/lsp.h @@ -1,64 +1,64 @@ -/*---------------------------------------------------------------------------*\ -Original Copyright - FILE........: AK2LSPD.H - TYPE........: Turbo C header file - COMPANY.....: Voicetronix - AUTHOR......: James Whitehall - DATE CREATED: 21/11/95 - -Modified by Jean-Marc Valin - - This file contains functions for converting Linear Prediction - Coefficients (LPC) to Line Spectral Pair (LSP) and back. Note that the - LSP coefficients are not in radians format but in the x domain of the - unit circle. - -\*---------------------------------------------------------------------------*/ -/** - @file lsp.h - @brief Line Spectral Pair (LSP) functions. -*/ -/* Speex License: - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef __AK2LSPD__ -#define __AK2LSPD__ - -#include "arch.h" - -int lpc_to_lsp (spx_coef_t *a, int lpcrdr, spx_lsp_t *freq, int nb, spx_word16_t delta, char *stack); -void lsp_to_lpc(spx_lsp_t *freq, spx_coef_t *ak, int lpcrdr, char *stack); - -/*Added by JMV*/ -void lsp_enforce_margin(spx_lsp_t *lsp, int len, spx_word16_t margin); - -void lsp_interpolate(spx_lsp_t *old_lsp, spx_lsp_t *new_lsp, spx_lsp_t *interp_lsp, int len, int subframe, int nb_subframes); - -#endif /* __AK2LSPD__ */ +/*---------------------------------------------------------------------------*\ +Original Copyright + FILE........: AK2LSPD.H + TYPE........: Turbo C header file + COMPANY.....: Voicetronix + AUTHOR......: James Whitehall + DATE CREATED: 21/11/95 + +Modified by Jean-Marc Valin + + This file contains functions for converting Linear Prediction + Coefficients (LPC) to Line Spectral Pair (LSP) and back. Note that the + LSP coefficients are not in radians format but in the x domain of the + unit circle. + +\*---------------------------------------------------------------------------*/ +/** + @file lsp.h + @brief Line Spectral Pair (LSP) functions. +*/ +/* Speex License: + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + - Neither the name of the Xiph.org Foundation nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef __AK2LSPD__ +#define __AK2LSPD__ + +#include "arch.h" + +int lpc_to_lsp (spx_coef_t *a, int lpcrdr, spx_lsp_t *freq, int nb, spx_word16_t delta, char *stack); +void lsp_to_lpc(spx_lsp_t *freq, spx_coef_t *ak, int lpcrdr, char *stack); + +/*Added by JMV*/ +void lsp_enforce_margin(spx_lsp_t *lsp, int len, spx_word16_t margin); + +void lsp_interpolate(spx_lsp_t *old_lsp, spx_lsp_t *new_lsp, spx_lsp_t *interp_lsp, int len, int subframe, int nb_subframes); + +#endif /* __AK2LSPD__ */ diff --git a/jni/speex-1.2rc1/libspeex/lsp_tables_nb.c b/app/src/main/jni/speex-1.2rc1/libspeex/lsp_tables_nb.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/lsp_tables_nb.c rename to app/src/main/jni/speex-1.2rc1/libspeex/lsp_tables_nb.c diff --git a/jni/speex-1.2rc1/libspeex/ltp.c b/app/src/main/jni/speex-1.2rc1/libspeex/ltp.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/ltp.c rename to app/src/main/jni/speex-1.2rc1/libspeex/ltp.c diff --git a/jni/speex-1.2rc1/libspeex/ltp.h b/app/src/main/jni/speex-1.2rc1/libspeex/ltp.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/ltp.h rename to app/src/main/jni/speex-1.2rc1/libspeex/ltp.h diff --git a/jni/speex-1.2rc1/libspeex/math_approx.h b/app/src/main/jni/speex-1.2rc1/libspeex/math_approx.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/math_approx.h rename to app/src/main/jni/speex-1.2rc1/libspeex/math_approx.h diff --git a/jni/speex-1.2rc1/libspeex/modes.c b/app/src/main/jni/speex-1.2rc1/libspeex/modes.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/modes.c rename to app/src/main/jni/speex-1.2rc1/libspeex/modes.c diff --git a/jni/speex-1.2rc1/libspeex/modes.h b/app/src/main/jni/speex-1.2rc1/libspeex/modes.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/modes.h rename to app/src/main/jni/speex-1.2rc1/libspeex/modes.h diff --git a/jni/speex-1.2rc1/libspeex/nb_celp.c b/app/src/main/jni/speex-1.2rc1/libspeex/nb_celp.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/nb_celp.c rename to app/src/main/jni/speex-1.2rc1/libspeex/nb_celp.c diff --git a/jni/speex-1.2rc1/libspeex/nb_celp.h b/app/src/main/jni/speex-1.2rc1/libspeex/nb_celp.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/nb_celp.h rename to app/src/main/jni/speex-1.2rc1/libspeex/nb_celp.h diff --git a/jni/speex-1.2rc1/libspeex/os_support.h b/app/src/main/jni/speex-1.2rc1/libspeex/os_support.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/os_support.h rename to app/src/main/jni/speex-1.2rc1/libspeex/os_support.h diff --git a/jni/speex-1.2rc1/libspeex/quant_lsp.c b/app/src/main/jni/speex-1.2rc1/libspeex/quant_lsp.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/quant_lsp.c rename to app/src/main/jni/speex-1.2rc1/libspeex/quant_lsp.c diff --git a/jni/speex-1.2rc1/libspeex/quant_lsp.h b/app/src/main/jni/speex-1.2rc1/libspeex/quant_lsp.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/quant_lsp.h rename to app/src/main/jni/speex-1.2rc1/libspeex/quant_lsp.h diff --git a/jni/speex-1.2rc1/libspeex/sb_celp.h b/app/src/main/jni/speex-1.2rc1/libspeex/sb_celp.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/sb_celp.h rename to app/src/main/jni/speex-1.2rc1/libspeex/sb_celp.h diff --git a/jni/speex-1.2rc1/libspeex/speex.c b/app/src/main/jni/speex-1.2rc1/libspeex/speex.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/speex.c rename to app/src/main/jni/speex-1.2rc1/libspeex/speex.c diff --git a/jni/speex-1.2rc1/libspeex/speex_callbacks.c b/app/src/main/jni/speex-1.2rc1/libspeex/speex_callbacks.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/speex_callbacks.c rename to app/src/main/jni/speex-1.2rc1/libspeex/speex_callbacks.c diff --git a/jni/speex-1.2rc1/libspeex/stack_alloc.h b/app/src/main/jni/speex-1.2rc1/libspeex/stack_alloc.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/stack_alloc.h rename to app/src/main/jni/speex-1.2rc1/libspeex/stack_alloc.h diff --git a/jni/speex-1.2rc1/libspeex/vbr.c b/app/src/main/jni/speex-1.2rc1/libspeex/vbr.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/vbr.c rename to app/src/main/jni/speex-1.2rc1/libspeex/vbr.c diff --git a/jni/speex-1.2rc1/libspeex/vbr.h b/app/src/main/jni/speex-1.2rc1/libspeex/vbr.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/vbr.h rename to app/src/main/jni/speex-1.2rc1/libspeex/vbr.h diff --git a/jni/speex-1.2rc1/libspeex/vq.c b/app/src/main/jni/speex-1.2rc1/libspeex/vq.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/vq.c rename to app/src/main/jni/speex-1.2rc1/libspeex/vq.c diff --git a/jni/speex-1.2rc1/libspeex/vq.h b/app/src/main/jni/speex-1.2rc1/libspeex/vq.h similarity index 100% rename from jni/speex-1.2rc1/libspeex/vq.h rename to app/src/main/jni/speex-1.2rc1/libspeex/vq.h diff --git a/jni/speex-1.2rc1/libspeex/window.c b/app/src/main/jni/speex-1.2rc1/libspeex/window.c similarity index 100% rename from jni/speex-1.2rc1/libspeex/window.c rename to app/src/main/jni/speex-1.2rc1/libspeex/window.c diff --git a/jni/speex_jni.cpp b/app/src/main/jni/speex_jni.cpp similarity index 100% rename from jni/speex_jni.cpp rename to app/src/main/jni/speex_jni.cpp diff --git a/res/drawable-finger/btn_dial.xml b/app/src/main/res/drawable-finger/btn_dial.xml similarity index 100% rename from res/drawable-finger/btn_dial.xml rename to app/src/main/res/drawable-finger/btn_dial.xml diff --git a/res/drawable-finger/btn_dial_delete.xml b/app/src/main/res/drawable-finger/btn_dial_delete.xml similarity index 100% rename from res/drawable-finger/btn_dial_delete.xml rename to app/src/main/res/drawable-finger/btn_dial_delete.xml diff --git a/res/drawable-finger/btn_dial_delete_activated.9.png b/app/src/main/res/drawable-finger/btn_dial_delete_activated.9.png similarity index 100% rename from res/drawable-finger/btn_dial_delete_activated.9.png rename to app/src/main/res/drawable-finger/btn_dial_delete_activated.9.png diff --git a/res/drawable-finger/btn_dial_delete_active.xml b/app/src/main/res/drawable-finger/btn_dial_delete_active.xml similarity index 100% rename from res/drawable-finger/btn_dial_delete_active.xml rename to app/src/main/res/drawable-finger/btn_dial_delete_active.xml diff --git a/res/drawable-finger/btn_dial_delete_normal.9.png b/app/src/main/res/drawable-finger/btn_dial_delete_normal.9.png similarity index 100% rename from res/drawable-finger/btn_dial_delete_normal.9.png rename to app/src/main/res/drawable-finger/btn_dial_delete_normal.9.png diff --git a/res/drawable-finger/btn_dial_delete_pressed.9.png b/app/src/main/res/drawable-finger/btn_dial_delete_pressed.9.png similarity index 100% rename from res/drawable-finger/btn_dial_delete_pressed.9.png rename to app/src/main/res/drawable-finger/btn_dial_delete_pressed.9.png diff --git a/res/drawable-finger/btn_dial_delete_selected.9.png b/app/src/main/res/drawable-finger/btn_dial_delete_selected.9.png similarity index 100% rename from res/drawable-finger/btn_dial_delete_selected.9.png rename to app/src/main/res/drawable-finger/btn_dial_delete_selected.9.png diff --git a/res/drawable-finger/btn_dial_normal.png b/app/src/main/res/drawable-finger/btn_dial_normal.png similarity index 100% rename from res/drawable-finger/btn_dial_normal.png rename to app/src/main/res/drawable-finger/btn_dial_normal.png diff --git a/res/drawable-finger/btn_dial_pressed.png b/app/src/main/res/drawable-finger/btn_dial_pressed.png similarity index 100% rename from res/drawable-finger/btn_dial_pressed.png rename to app/src/main/res/drawable-finger/btn_dial_pressed.png diff --git a/res/drawable-finger/btn_dial_selected.png b/app/src/main/res/drawable-finger/btn_dial_selected.png similarity index 100% rename from res/drawable-finger/btn_dial_selected.png rename to app/src/main/res/drawable-finger/btn_dial_selected.png diff --git a/res/drawable-finger/btn_dial_textfield.xml b/app/src/main/res/drawable-finger/btn_dial_textfield.xml similarity index 100% rename from res/drawable-finger/btn_dial_textfield.xml rename to app/src/main/res/drawable-finger/btn_dial_textfield.xml diff --git a/res/drawable-finger/btn_dial_textfield_activated.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_activated.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_activated.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_activated.9.png diff --git a/res/drawable-finger/btn_dial_textfield_activated_full.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_activated_full.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_activated_full.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_activated_full.9.png diff --git a/res/drawable-finger/btn_dial_textfield_active.xml b/app/src/main/res/drawable-finger/btn_dial_textfield_active.xml similarity index 100% rename from res/drawable-finger/btn_dial_textfield_active.xml rename to app/src/main/res/drawable-finger/btn_dial_textfield_active.xml diff --git a/res/drawable-finger/btn_dial_textfield_normal.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_normal.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_normal.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_normal.9.png diff --git a/res/drawable-finger/btn_dial_textfield_normal_full.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_normal_full.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_normal_full.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_normal_full.9.png diff --git a/res/drawable-finger/btn_dial_textfield_pressed.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_pressed.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_pressed.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_pressed.9.png diff --git a/res/drawable-finger/btn_dial_textfield_pressed_full.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_pressed_full.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_pressed_full.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_pressed_full.9.png diff --git a/res/drawable-finger/btn_dial_textfield_selected.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_selected.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_selected.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_selected.9.png diff --git a/res/drawable-finger/btn_dial_textfield_selected_full.9.png b/app/src/main/res/drawable-finger/btn_dial_textfield_selected_full.9.png similarity index 100% rename from res/drawable-finger/btn_dial_textfield_selected_full.9.png rename to app/src/main/res/drawable-finger/btn_dial_textfield_selected_full.9.png diff --git a/res/drawable-finger/btn_dialer.xml b/app/src/main/res/drawable-finger/btn_dialer.xml similarity index 100% rename from res/drawable-finger/btn_dialer.xml rename to app/src/main/res/drawable-finger/btn_dialer.xml diff --git a/res/drawable-finger/btn_dialer_default.png b/app/src/main/res/drawable-finger/btn_dialer_default.png similarity index 100% rename from res/drawable-finger/btn_dialer_default.png rename to app/src/main/res/drawable-finger/btn_dialer_default.png diff --git a/res/drawable-finger/btn_dialer_default_dial.png b/app/src/main/res/drawable-finger/btn_dialer_default_dial.png similarity index 100% rename from res/drawable-finger/btn_dialer_default_dial.png rename to app/src/main/res/drawable-finger/btn_dialer_default_dial.png diff --git a/res/drawable-finger/btn_dialer_dial.xml b/app/src/main/res/drawable-finger/btn_dialer_dial.xml similarity index 100% rename from res/drawable-finger/btn_dialer_dial.xml rename to app/src/main/res/drawable-finger/btn_dialer_dial.xml diff --git a/res/drawable-finger/btn_dialer_pressed.9.png b/app/src/main/res/drawable-finger/btn_dialer_pressed.9.png similarity index 100% rename from res/drawable-finger/btn_dialer_pressed.9.png rename to app/src/main/res/drawable-finger/btn_dialer_pressed.9.png diff --git a/res/drawable-finger/btn_dialer_selected.9.png b/app/src/main/res/drawable-finger/btn_dialer_selected.9.png similarity index 100% rename from res/drawable-finger/btn_dialer_selected.9.png rename to app/src/main/res/drawable-finger/btn_dialer_selected.9.png diff --git a/res/drawable-finger/dark_header.9.png b/app/src/main/res/drawable-finger/dark_header.9.png similarity index 100% rename from res/drawable-finger/dark_header.9.png rename to app/src/main/res/drawable-finger/dark_header.9.png diff --git a/res/drawable-finger/dark_header_dithered.xml b/app/src/main/res/drawable-finger/dark_header_dithered.xml similarity index 100% rename from res/drawable-finger/dark_header_dithered.xml rename to app/src/main/res/drawable-finger/dark_header_dithered.xml diff --git a/res/drawable-finger/dial_num_0.xml b/app/src/main/res/drawable-finger/dial_num_0.xml similarity index 100% rename from res/drawable-finger/dial_num_0.xml rename to app/src/main/res/drawable-finger/dial_num_0.xml diff --git a/res/drawable-finger/dial_num_0_blk.png b/app/src/main/res/drawable-finger/dial_num_0_blk.png similarity index 100% rename from res/drawable-finger/dial_num_0_blk.png rename to app/src/main/res/drawable-finger/dial_num_0_blk.png diff --git a/res/drawable-finger/dial_num_0_wht.png b/app/src/main/res/drawable-finger/dial_num_0_wht.png similarity index 100% rename from res/drawable-finger/dial_num_0_wht.png rename to app/src/main/res/drawable-finger/dial_num_0_wht.png diff --git a/res/drawable-finger/dial_num_1.xml b/app/src/main/res/drawable-finger/dial_num_1.xml similarity index 100% rename from res/drawable-finger/dial_num_1.xml rename to app/src/main/res/drawable-finger/dial_num_1.xml diff --git a/res/drawable-finger/dial_num_1_blk.png b/app/src/main/res/drawable-finger/dial_num_1_blk.png similarity index 100% rename from res/drawable-finger/dial_num_1_blk.png rename to app/src/main/res/drawable-finger/dial_num_1_blk.png diff --git a/res/drawable-finger/dial_num_1_no_vm.xml b/app/src/main/res/drawable-finger/dial_num_1_no_vm.xml similarity index 100% rename from res/drawable-finger/dial_num_1_no_vm.xml rename to app/src/main/res/drawable-finger/dial_num_1_no_vm.xml diff --git a/res/drawable-finger/dial_num_1_no_vm_blk.png b/app/src/main/res/drawable-finger/dial_num_1_no_vm_blk.png similarity index 100% rename from res/drawable-finger/dial_num_1_no_vm_blk.png rename to app/src/main/res/drawable-finger/dial_num_1_no_vm_blk.png diff --git a/res/drawable-finger/dial_num_1_no_vm_wht.png b/app/src/main/res/drawable-finger/dial_num_1_no_vm_wht.png similarity index 100% rename from res/drawable-finger/dial_num_1_no_vm_wht.png rename to app/src/main/res/drawable-finger/dial_num_1_no_vm_wht.png diff --git a/res/drawable-finger/dial_num_1_wht.png b/app/src/main/res/drawable-finger/dial_num_1_wht.png similarity index 100% rename from res/drawable-finger/dial_num_1_wht.png rename to app/src/main/res/drawable-finger/dial_num_1_wht.png diff --git a/res/drawable-finger/dial_num_2.xml b/app/src/main/res/drawable-finger/dial_num_2.xml similarity index 100% rename from res/drawable-finger/dial_num_2.xml rename to app/src/main/res/drawable-finger/dial_num_2.xml diff --git a/res/drawable-finger/dial_num_2_blk.png b/app/src/main/res/drawable-finger/dial_num_2_blk.png similarity index 100% rename from res/drawable-finger/dial_num_2_blk.png rename to app/src/main/res/drawable-finger/dial_num_2_blk.png diff --git a/res/drawable-finger/dial_num_2_wht.png b/app/src/main/res/drawable-finger/dial_num_2_wht.png similarity index 100% rename from res/drawable-finger/dial_num_2_wht.png rename to app/src/main/res/drawable-finger/dial_num_2_wht.png diff --git a/res/drawable-finger/dial_num_3.xml b/app/src/main/res/drawable-finger/dial_num_3.xml similarity index 100% rename from res/drawable-finger/dial_num_3.xml rename to app/src/main/res/drawable-finger/dial_num_3.xml diff --git a/res/drawable-finger/dial_num_3_blk.png b/app/src/main/res/drawable-finger/dial_num_3_blk.png similarity index 100% rename from res/drawable-finger/dial_num_3_blk.png rename to app/src/main/res/drawable-finger/dial_num_3_blk.png diff --git a/res/drawable-finger/dial_num_3_wht.png b/app/src/main/res/drawable-finger/dial_num_3_wht.png similarity index 100% rename from res/drawable-finger/dial_num_3_wht.png rename to app/src/main/res/drawable-finger/dial_num_3_wht.png diff --git a/res/drawable-finger/dial_num_4.xml b/app/src/main/res/drawable-finger/dial_num_4.xml similarity index 100% rename from res/drawable-finger/dial_num_4.xml rename to app/src/main/res/drawable-finger/dial_num_4.xml diff --git a/res/drawable-finger/dial_num_4_blk.png b/app/src/main/res/drawable-finger/dial_num_4_blk.png similarity index 100% rename from res/drawable-finger/dial_num_4_blk.png rename to app/src/main/res/drawable-finger/dial_num_4_blk.png diff --git a/res/drawable-finger/dial_num_4_wht.png b/app/src/main/res/drawable-finger/dial_num_4_wht.png similarity index 100% rename from res/drawable-finger/dial_num_4_wht.png rename to app/src/main/res/drawable-finger/dial_num_4_wht.png diff --git a/res/drawable-finger/dial_num_5.xml b/app/src/main/res/drawable-finger/dial_num_5.xml similarity index 100% rename from res/drawable-finger/dial_num_5.xml rename to app/src/main/res/drawable-finger/dial_num_5.xml diff --git a/res/drawable-finger/dial_num_5_blk.png b/app/src/main/res/drawable-finger/dial_num_5_blk.png similarity index 100% rename from res/drawable-finger/dial_num_5_blk.png rename to app/src/main/res/drawable-finger/dial_num_5_blk.png diff --git a/res/drawable-finger/dial_num_5_wht.png b/app/src/main/res/drawable-finger/dial_num_5_wht.png similarity index 100% rename from res/drawable-finger/dial_num_5_wht.png rename to app/src/main/res/drawable-finger/dial_num_5_wht.png diff --git a/res/drawable-finger/dial_num_6.xml b/app/src/main/res/drawable-finger/dial_num_6.xml similarity index 100% rename from res/drawable-finger/dial_num_6.xml rename to app/src/main/res/drawable-finger/dial_num_6.xml diff --git a/res/drawable-finger/dial_num_6_blk.png b/app/src/main/res/drawable-finger/dial_num_6_blk.png similarity index 100% rename from res/drawable-finger/dial_num_6_blk.png rename to app/src/main/res/drawable-finger/dial_num_6_blk.png diff --git a/res/drawable-finger/dial_num_6_wht.png b/app/src/main/res/drawable-finger/dial_num_6_wht.png similarity index 100% rename from res/drawable-finger/dial_num_6_wht.png rename to app/src/main/res/drawable-finger/dial_num_6_wht.png diff --git a/res/drawable-finger/dial_num_7.xml b/app/src/main/res/drawable-finger/dial_num_7.xml similarity index 100% rename from res/drawable-finger/dial_num_7.xml rename to app/src/main/res/drawable-finger/dial_num_7.xml diff --git a/res/drawable-finger/dial_num_7_blk.png b/app/src/main/res/drawable-finger/dial_num_7_blk.png similarity index 100% rename from res/drawable-finger/dial_num_7_blk.png rename to app/src/main/res/drawable-finger/dial_num_7_blk.png diff --git a/res/drawable-finger/dial_num_7_wht.png b/app/src/main/res/drawable-finger/dial_num_7_wht.png similarity index 100% rename from res/drawable-finger/dial_num_7_wht.png rename to app/src/main/res/drawable-finger/dial_num_7_wht.png diff --git a/res/drawable-finger/dial_num_8.xml b/app/src/main/res/drawable-finger/dial_num_8.xml similarity index 100% rename from res/drawable-finger/dial_num_8.xml rename to app/src/main/res/drawable-finger/dial_num_8.xml diff --git a/res/drawable-finger/dial_num_8_blk.png b/app/src/main/res/drawable-finger/dial_num_8_blk.png similarity index 100% rename from res/drawable-finger/dial_num_8_blk.png rename to app/src/main/res/drawable-finger/dial_num_8_blk.png diff --git a/res/drawable-finger/dial_num_8_wht.png b/app/src/main/res/drawable-finger/dial_num_8_wht.png similarity index 100% rename from res/drawable-finger/dial_num_8_wht.png rename to app/src/main/res/drawable-finger/dial_num_8_wht.png diff --git a/res/drawable-finger/dial_num_9.xml b/app/src/main/res/drawable-finger/dial_num_9.xml similarity index 100% rename from res/drawable-finger/dial_num_9.xml rename to app/src/main/res/drawable-finger/dial_num_9.xml diff --git a/res/drawable-finger/dial_num_9_blk.png b/app/src/main/res/drawable-finger/dial_num_9_blk.png similarity index 100% rename from res/drawable-finger/dial_num_9_blk.png rename to app/src/main/res/drawable-finger/dial_num_9_blk.png diff --git a/res/drawable-finger/dial_num_9_wht.png b/app/src/main/res/drawable-finger/dial_num_9_wht.png similarity index 100% rename from res/drawable-finger/dial_num_9_wht.png rename to app/src/main/res/drawable-finger/dial_num_9_wht.png diff --git a/res/drawable-finger/dial_num_pound.xml b/app/src/main/res/drawable-finger/dial_num_pound.xml similarity index 100% rename from res/drawable-finger/dial_num_pound.xml rename to app/src/main/res/drawable-finger/dial_num_pound.xml diff --git a/res/drawable-finger/dial_num_pound_blk.png b/app/src/main/res/drawable-finger/dial_num_pound_blk.png similarity index 100% rename from res/drawable-finger/dial_num_pound_blk.png rename to app/src/main/res/drawable-finger/dial_num_pound_blk.png diff --git a/res/drawable-finger/dial_num_pound_wht.png b/app/src/main/res/drawable-finger/dial_num_pound_wht.png similarity index 100% rename from res/drawable-finger/dial_num_pound_wht.png rename to app/src/main/res/drawable-finger/dial_num_pound_wht.png diff --git a/res/drawable-finger/dial_num_star.xml b/app/src/main/res/drawable-finger/dial_num_star.xml similarity index 100% rename from res/drawable-finger/dial_num_star.xml rename to app/src/main/res/drawable-finger/dial_num_star.xml diff --git a/res/drawable-finger/dial_num_star_blk.png b/app/src/main/res/drawable-finger/dial_num_star_blk.png similarity index 100% rename from res/drawable-finger/dial_num_star_blk.png rename to app/src/main/res/drawable-finger/dial_num_star_blk.png diff --git a/res/drawable-finger/dial_num_star_wht.png b/app/src/main/res/drawable-finger/dial_num_star_wht.png similarity index 100% rename from res/drawable-finger/dial_num_star_wht.png rename to app/src/main/res/drawable-finger/dial_num_star_wht.png diff --git a/res/drawable-finger/ic_delete_phone_number.xml b/app/src/main/res/drawable-finger/ic_delete_phone_number.xml similarity index 100% rename from res/drawable-finger/ic_delete_phone_number.xml rename to app/src/main/res/drawable-finger/ic_delete_phone_number.xml diff --git a/res/drawable-finger/ic_delete_phone_number_blk.png b/app/src/main/res/drawable-finger/ic_delete_phone_number_blk.png similarity index 100% rename from res/drawable-finger/ic_delete_phone_number_blk.png rename to app/src/main/res/drawable-finger/ic_delete_phone_number_blk.png diff --git a/res/drawable-finger/ic_delete_phone_number_wht.png b/app/src/main/res/drawable-finger/ic_delete_phone_number_wht.png similarity index 100% rename from res/drawable-finger/ic_delete_phone_number_wht.png rename to app/src/main/res/drawable-finger/ic_delete_phone_number_wht.png diff --git a/res/drawable-finger/ic_dial_number.xml b/app/src/main/res/drawable-finger/ic_dial_number.xml similarity index 100% rename from res/drawable-finger/ic_dial_number.xml rename to app/src/main/res/drawable-finger/ic_dial_number.xml diff --git a/res/drawable-finger/ic_dial_number_blk.png b/app/src/main/res/drawable-finger/ic_dial_number_blk.png similarity index 100% rename from res/drawable-finger/ic_dial_number_blk.png rename to app/src/main/res/drawable-finger/ic_dial_number_blk.png diff --git a/res/drawable-finger/ic_dial_number_wht.png b/app/src/main/res/drawable-finger/ic_dial_number_wht.png similarity index 100% rename from res/drawable-finger/ic_dial_number_wht.png rename to app/src/main/res/drawable-finger/ic_dial_number_wht.png diff --git a/res/drawable-finger/ic_dialer_voicemail.xml b/app/src/main/res/drawable-finger/ic_dialer_voicemail.xml similarity index 100% rename from res/drawable-finger/ic_dialer_voicemail.xml rename to app/src/main/res/drawable-finger/ic_dialer_voicemail.xml diff --git a/res/drawable-finger/ic_dialer_voicemail_black.png b/app/src/main/res/drawable-finger/ic_dialer_voicemail_black.png similarity index 100% rename from res/drawable-finger/ic_dialer_voicemail_black.png rename to app/src/main/res/drawable-finger/ic_dialer_voicemail_black.png diff --git a/res/drawable-finger/ic_dialer_voicemail_white.png b/app/src/main/res/drawable-finger/ic_dialer_voicemail_white.png similarity index 100% rename from res/drawable-finger/ic_dialer_voicemail_white.png rename to app/src/main/res/drawable-finger/ic_dialer_voicemail_white.png diff --git a/res/drawable-finger/ic_dialpad_tray.png b/app/src/main/res/drawable-finger/ic_dialpad_tray.png similarity index 100% rename from res/drawable-finger/ic_dialpad_tray.png rename to app/src/main/res/drawable-finger/ic_dialpad_tray.png diff --git a/res/drawable-finger/ic_headphone.png b/app/src/main/res/drawable-finger/ic_headphone.png similarity index 100% rename from res/drawable-finger/ic_headphone.png rename to app/src/main/res/drawable-finger/ic_headphone.png diff --git a/res/drawable-finger/ic_headphone_deactive.png b/app/src/main/res/drawable-finger/ic_headphone_deactive.png similarity index 100% rename from res/drawable-finger/ic_headphone_deactive.png rename to app/src/main/res/drawable-finger/ic_headphone_deactive.png diff --git a/res/drawable-finger/ic_launcher_contacts.png b/app/src/main/res/drawable-finger/ic_launcher_contacts.png similarity index 100% rename from res/drawable-finger/ic_launcher_contacts.png rename to app/src/main/res/drawable-finger/ic_launcher_contacts.png diff --git a/res/drawable-finger/ic_menu_contact.png b/app/src/main/res/drawable-finger/ic_menu_contact.png similarity index 100% rename from res/drawable-finger/ic_menu_contact.png rename to app/src/main/res/drawable-finger/ic_menu_contact.png diff --git a/res/drawable-finger/ic_tab_contacts.xml b/app/src/main/res/drawable-finger/ic_tab_contacts.xml similarity index 100% rename from res/drawable-finger/ic_tab_contacts.xml rename to app/src/main/res/drawable-finger/ic_tab_contacts.xml diff --git a/res/drawable-finger/ic_tab_dialer.xml b/app/src/main/res/drawable-finger/ic_tab_dialer.xml similarity index 100% rename from res/drawable-finger/ic_tab_dialer.xml rename to app/src/main/res/drawable-finger/ic_tab_dialer.xml diff --git a/res/drawable-finger/ic_tab_recent.xml b/app/src/main/res/drawable-finger/ic_tab_recent.xml similarity index 100% rename from res/drawable-finger/ic_tab_recent.xml rename to app/src/main/res/drawable-finger/ic_tab_recent.xml diff --git a/res/drawable-finger/ic_tab_selected_contacts.png b/app/src/main/res/drawable-finger/ic_tab_selected_contacts.png similarity index 100% rename from res/drawable-finger/ic_tab_selected_contacts.png rename to app/src/main/res/drawable-finger/ic_tab_selected_contacts.png diff --git a/res/drawable-finger/ic_tab_selected_dialer.png b/app/src/main/res/drawable-finger/ic_tab_selected_dialer.png similarity index 100% rename from res/drawable-finger/ic_tab_selected_dialer.png rename to app/src/main/res/drawable-finger/ic_tab_selected_dialer.png diff --git a/res/drawable-finger/ic_tab_selected_recent.png b/app/src/main/res/drawable-finger/ic_tab_selected_recent.png similarity index 100% rename from res/drawable-finger/ic_tab_selected_recent.png rename to app/src/main/res/drawable-finger/ic_tab_selected_recent.png diff --git a/res/drawable-finger/ic_tab_selected_stared.png b/app/src/main/res/drawable-finger/ic_tab_selected_stared.png similarity index 100% rename from res/drawable-finger/ic_tab_selected_stared.png rename to app/src/main/res/drawable-finger/ic_tab_selected_stared.png diff --git a/res/drawable-finger/ic_tab_starred.xml b/app/src/main/res/drawable-finger/ic_tab_starred.xml similarity index 100% rename from res/drawable-finger/ic_tab_starred.xml rename to app/src/main/res/drawable-finger/ic_tab_starred.xml diff --git a/res/drawable-finger/ic_tab_unselected_contacts.png b/app/src/main/res/drawable-finger/ic_tab_unselected_contacts.png similarity index 100% rename from res/drawable-finger/ic_tab_unselected_contacts.png rename to app/src/main/res/drawable-finger/ic_tab_unselected_contacts.png diff --git a/res/drawable-finger/ic_tab_unselected_dialer.png b/app/src/main/res/drawable-finger/ic_tab_unselected_dialer.png similarity index 100% rename from res/drawable-finger/ic_tab_unselected_dialer.png rename to app/src/main/res/drawable-finger/ic_tab_unselected_dialer.png diff --git a/res/drawable-finger/ic_tab_unselected_recent.png b/app/src/main/res/drawable-finger/ic_tab_unselected_recent.png similarity index 100% rename from res/drawable-finger/ic_tab_unselected_recent.png rename to app/src/main/res/drawable-finger/ic_tab_unselected_recent.png diff --git a/res/drawable-finger/ic_tab_unselected_stared.png b/app/src/main/res/drawable-finger/ic_tab_unselected_stared.png similarity index 100% rename from res/drawable-finger/ic_tab_unselected_stared.png rename to app/src/main/res/drawable-finger/ic_tab_unselected_stared.png diff --git a/res/drawable-finger/tray_handle.xml b/app/src/main/res/drawable-finger/tray_handle.xml similarity index 100% rename from res/drawable-finger/tray_handle.xml rename to app/src/main/res/drawable-finger/tray_handle.xml diff --git a/res/drawable-finger/tray_handle_normal.9.png b/app/src/main/res/drawable-finger/tray_handle_normal.9.png similarity index 100% rename from res/drawable-finger/tray_handle_normal.9.png rename to app/src/main/res/drawable-finger/tray_handle_normal.9.png diff --git a/res/drawable-finger/tray_handle_pressed.9.png b/app/src/main/res/drawable-finger/tray_handle_pressed.9.png similarity index 100% rename from res/drawable-finger/tray_handle_pressed.9.png rename to app/src/main/res/drawable-finger/tray_handle_pressed.9.png diff --git a/res/drawable-finger/tray_handle_selected.9.png b/app/src/main/res/drawable-finger/tray_handle_selected.9.png similarity index 100% rename from res/drawable-finger/tray_handle_selected.9.png rename to app/src/main/res/drawable-finger/tray_handle_selected.9.png diff --git a/res/drawable/auto_answer.png b/app/src/main/res/drawable/auto_answer.png similarity index 100% rename from res/drawable/auto_answer.png rename to app/src/main/res/drawable/auto_answer.png diff --git a/res/drawable/auto_answer_disabled.png b/app/src/main/res/drawable/auto_answer_disabled.png similarity index 100% rename from res/drawable/auto_answer_disabled.png rename to app/src/main/res/drawable/auto_answer_disabled.png diff --git a/res/drawable/conf.png b/app/src/main/res/drawable/conf.png similarity index 100% rename from res/drawable/conf.png rename to app/src/main/res/drawable/conf.png diff --git a/res/drawable/configure.png b/app/src/main/res/drawable/configure.png similarity index 100% rename from res/drawable/configure.png rename to app/src/main/res/drawable/configure.png diff --git a/res/drawable/contacts.png b/app/src/main/res/drawable/contacts.png similarity index 100% rename from res/drawable/contacts.png rename to app/src/main/res/drawable/contacts.png diff --git a/res/drawable/exit.png b/app/src/main/res/drawable/exit.png similarity index 100% rename from res/drawable/exit.png rename to app/src/main/res/drawable/exit.png diff --git a/res/drawable/goback.png b/app/src/main/res/drawable/goback.png similarity index 100% rename from res/drawable/goback.png rename to app/src/main/res/drawable/goback.png diff --git a/res/drawable/home.png b/app/src/main/res/drawable/home.png similarity index 100% rename from res/drawable/home.png rename to app/src/main/res/drawable/home.png diff --git a/res/drawable/ic_contacts_edit_contacts.png b/app/src/main/res/drawable/ic_contacts_edit_contacts.png similarity index 100% rename from res/drawable/ic_contacts_edit_contacts.png rename to app/src/main/res/drawable/ic_contacts_edit_contacts.png diff --git a/res/drawable/ic_incall_add.png b/app/src/main/res/drawable/ic_incall_add.png similarity index 100% rename from res/drawable/ic_incall_add.png rename to app/src/main/res/drawable/ic_incall_add.png diff --git a/res/drawable/ic_incall_answer.png b/app/src/main/res/drawable/ic_incall_answer.png similarity index 100% rename from res/drawable/ic_incall_answer.png rename to app/src/main/res/drawable/ic_incall_answer.png diff --git a/res/drawable/ic_incall_end.png b/app/src/main/res/drawable/ic_incall_end.png similarity index 100% rename from res/drawable/ic_incall_end.png rename to app/src/main/res/drawable/ic_incall_end.png diff --git a/res/drawable/ic_incall_hangup.png b/app/src/main/res/drawable/ic_incall_hangup.png similarity index 100% rename from res/drawable/ic_incall_hangup.png rename to app/src/main/res/drawable/ic_incall_hangup.png diff --git a/res/drawable/ic_incall_ongoing.png b/app/src/main/res/drawable/ic_incall_ongoing.png similarity index 100% rename from res/drawable/ic_incall_ongoing.png rename to app/src/main/res/drawable/ic_incall_ongoing.png diff --git a/res/drawable/ic_incall_onhold.png b/app/src/main/res/drawable/ic_incall_onhold.png similarity index 100% rename from res/drawable/ic_incall_onhold.png rename to app/src/main/res/drawable/ic_incall_onhold.png diff --git a/res/drawable/ic_launcher_phone.png b/app/src/main/res/drawable/ic_launcher_phone.png similarity index 100% rename from res/drawable/ic_launcher_phone.png rename to app/src/main/res/drawable/ic_launcher_phone.png diff --git a/res/drawable/ic_menu_dial_pad.png b/app/src/main/res/drawable/ic_menu_dial_pad.png similarity index 100% rename from res/drawable/ic_menu_dial_pad.png rename to app/src/main/res/drawable/ic_menu_dial_pad.png diff --git a/res/drawable/ic_menu_end_call.png b/app/src/main/res/drawable/ic_menu_end_call.png similarity index 100% rename from res/drawable/ic_menu_end_call.png rename to app/src/main/res/drawable/ic_menu_end_call.png diff --git a/res/drawable/ic_menu_preferences.png b/app/src/main/res/drawable/ic_menu_preferences.png similarity index 100% rename from res/drawable/ic_menu_preferences.png rename to app/src/main/res/drawable/ic_menu_preferences.png diff --git a/res/drawable/ic_search_contacts.png b/app/src/main/res/drawable/ic_search_contacts.png similarity index 100% rename from res/drawable/ic_search_contacts.png rename to app/src/main/res/drawable/ic_search_contacts.png diff --git a/res/drawable/icon22.png b/app/src/main/res/drawable/icon22.png similarity index 100% rename from res/drawable/icon22.png rename to app/src/main/res/drawable/icon22.png diff --git a/res/drawable/icon32.png b/app/src/main/res/drawable/icon32.png similarity index 100% rename from res/drawable/icon32.png rename to app/src/main/res/drawable/icon32.png diff --git a/res/drawable/icon64.png b/app/src/main/res/drawable/icon64.png similarity index 100% rename from res/drawable/icon64.png rename to app/src/main/res/drawable/icon64.png diff --git a/res/drawable/im.png b/app/src/main/res/drawable/im.png similarity index 100% rename from res/drawable/im.png rename to app/src/main/res/drawable/im.png diff --git a/res/drawable/incall_frame_connected_short.9.png b/app/src/main/res/drawable/incall_frame_connected_short.9.png similarity index 100% rename from res/drawable/incall_frame_connected_short.9.png rename to app/src/main/res/drawable/incall_frame_connected_short.9.png diff --git a/res/drawable/incall_frame_connected_tall_land.9.png b/app/src/main/res/drawable/incall_frame_connected_tall_land.9.png similarity index 100% rename from res/drawable/incall_frame_connected_tall_land.9.png rename to app/src/main/res/drawable/incall_frame_connected_tall_land.9.png diff --git a/res/drawable/incall_frame_connected_tall_port.9.png b/app/src/main/res/drawable/incall_frame_connected_tall_port.9.png similarity index 100% rename from res/drawable/incall_frame_connected_tall_port.9.png rename to app/src/main/res/drawable/incall_frame_connected_tall_port.9.png diff --git a/res/drawable/incall_frame_ended_short.9.png b/app/src/main/res/drawable/incall_frame_ended_short.9.png similarity index 100% rename from res/drawable/incall_frame_ended_short.9.png rename to app/src/main/res/drawable/incall_frame_ended_short.9.png diff --git a/res/drawable/incall_frame_ended_tall_land.9.png b/app/src/main/res/drawable/incall_frame_ended_tall_land.9.png similarity index 100% rename from res/drawable/incall_frame_ended_tall_land.9.png rename to app/src/main/res/drawable/incall_frame_ended_tall_land.9.png diff --git a/res/drawable/incall_frame_ended_tall_port.9.png b/app/src/main/res/drawable/incall_frame_ended_tall_port.9.png similarity index 100% rename from res/drawable/incall_frame_ended_tall_port.9.png rename to app/src/main/res/drawable/incall_frame_ended_tall_port.9.png diff --git a/res/drawable/incall_frame_hold_short.9.png b/app/src/main/res/drawable/incall_frame_hold_short.9.png similarity index 100% rename from res/drawable/incall_frame_hold_short.9.png rename to app/src/main/res/drawable/incall_frame_hold_short.9.png diff --git a/res/drawable/incall_frame_hold_tall_land.9.png b/app/src/main/res/drawable/incall_frame_hold_tall_land.9.png similarity index 100% rename from res/drawable/incall_frame_hold_tall_land.9.png rename to app/src/main/res/drawable/incall_frame_hold_tall_land.9.png diff --git a/res/drawable/incall_frame_hold_tall_port.9.png b/app/src/main/res/drawable/incall_frame_hold_tall_port.9.png similarity index 100% rename from res/drawable/incall_frame_hold_tall_port.9.png rename to app/src/main/res/drawable/incall_frame_hold_tall_port.9.png diff --git a/res/drawable/incall_frame_normal_short.9.png b/app/src/main/res/drawable/incall_frame_normal_short.9.png similarity index 100% rename from res/drawable/incall_frame_normal_short.9.png rename to app/src/main/res/drawable/incall_frame_normal_short.9.png diff --git a/res/drawable/incall_frame_normal_tall_land.9.png b/app/src/main/res/drawable/incall_frame_normal_tall_land.9.png similarity index 100% rename from res/drawable/incall_frame_normal_tall_land.9.png rename to app/src/main/res/drawable/incall_frame_normal_tall_land.9.png diff --git a/res/drawable/incall_frame_normal_tall_port.9.png b/app/src/main/res/drawable/incall_frame_normal_tall_port.9.png similarity index 100% rename from res/drawable/incall_frame_normal_tall_port.9.png rename to app/src/main/res/drawable/incall_frame_normal_tall_port.9.png diff --git a/res/drawable/incall_photo_border.9.png b/app/src/main/res/drawable/incall_photo_border.9.png similarity index 100% rename from res/drawable/incall_photo_border.9.png rename to app/src/main/res/drawable/incall_photo_border.9.png diff --git a/res/drawable/mute.png b/app/src/main/res/drawable/mute.png similarity index 100% rename from res/drawable/mute.png rename to app/src/main/res/drawable/mute.png diff --git a/res/drawable/people.png b/app/src/main/res/drawable/people.png similarity index 100% rename from res/drawable/people.png rename to app/src/main/res/drawable/people.png diff --git a/res/drawable/picture_busy.png b/app/src/main/res/drawable/picture_busy.png similarity index 100% rename from res/drawable/picture_busy.png rename to app/src/main/res/drawable/picture_busy.png diff --git a/res/drawable/picture_conference.png b/app/src/main/res/drawable/picture_conference.png similarity index 100% rename from res/drawable/picture_conference.png rename to app/src/main/res/drawable/picture_conference.png diff --git a/res/drawable/picture_dialing.png b/app/src/main/res/drawable/picture_dialing.png similarity index 100% rename from res/drawable/picture_dialing.png rename to app/src/main/res/drawable/picture_dialing.png diff --git a/res/drawable/picture_end.png b/app/src/main/res/drawable/picture_end.png similarity index 100% rename from res/drawable/picture_end.png rename to app/src/main/res/drawable/picture_end.png diff --git a/res/drawable/picture_frame.png b/app/src/main/res/drawable/picture_frame.png similarity index 100% rename from res/drawable/picture_frame.png rename to app/src/main/res/drawable/picture_frame.png diff --git a/res/drawable/picture_unknown.png b/app/src/main/res/drawable/picture_unknown.png similarity index 100% rename from res/drawable/picture_unknown.png rename to app/src/main/res/drawable/picture_unknown.png diff --git a/res/drawable/picture_unknown_2.png b/app/src/main/res/drawable/picture_unknown_2.png similarity index 100% rename from res/drawable/picture_unknown_2.png rename to app/src/main/res/drawable/picture_unknown_2.png diff --git a/res/drawable/picture_unknown_3.png b/app/src/main/res/drawable/picture_unknown_3.png similarity index 100% rename from res/drawable/picture_unknown_3.png rename to app/src/main/res/drawable/picture_unknown_3.png diff --git a/res/drawable/save.png b/app/src/main/res/drawable/save.png similarity index 100% rename from res/drawable/save.png rename to app/src/main/res/drawable/save.png diff --git a/res/drawable/stat_sys_phone_call.png b/app/src/main/res/drawable/stat_sys_phone_call.png similarity index 100% rename from res/drawable/stat_sys_phone_call.png rename to app/src/main/res/drawable/stat_sys_phone_call.png diff --git a/res/drawable/stat_sys_phone_call_bluetooth.png b/app/src/main/res/drawable/stat_sys_phone_call_bluetooth.png similarity index 100% rename from res/drawable/stat_sys_phone_call_bluetooth.png rename to app/src/main/res/drawable/stat_sys_phone_call_bluetooth.png diff --git a/res/drawable/status_bar_item_app_background_normal.png b/app/src/main/res/drawable/status_bar_item_app_background_normal.png similarity index 100% rename from res/drawable/status_bar_item_app_background_normal.png rename to app/src/main/res/drawable/status_bar_item_app_background_normal.png diff --git a/res/drawable/sym_call.png b/app/src/main/res/drawable/sym_call.png similarity index 100% rename from res/drawable/sym_call.png rename to app/src/main/res/drawable/sym_call.png diff --git a/res/drawable/sym_call_add_call.png b/app/src/main/res/drawable/sym_call_add_call.png similarity index 100% rename from res/drawable/sym_call_add_call.png rename to app/src/main/res/drawable/sym_call_add_call.png diff --git a/res/drawable/sym_call_done.png b/app/src/main/res/drawable/sym_call_done.png similarity index 100% rename from res/drawable/sym_call_done.png rename to app/src/main/res/drawable/sym_call_done.png diff --git a/res/drawable/sym_call_end.png b/app/src/main/res/drawable/sym_call_end.png similarity index 100% rename from res/drawable/sym_call_end.png rename to app/src/main/res/drawable/sym_call_end.png diff --git a/res/drawable/sym_call_hold_off.png b/app/src/main/res/drawable/sym_call_hold_off.png similarity index 100% rename from res/drawable/sym_call_hold_off.png rename to app/src/main/res/drawable/sym_call_hold_off.png diff --git a/res/drawable/sym_call_hold_on.png b/app/src/main/res/drawable/sym_call_hold_on.png similarity index 100% rename from res/drawable/sym_call_hold_on.png rename to app/src/main/res/drawable/sym_call_hold_on.png diff --git a/res/drawable/sym_call_lcd.png b/app/src/main/res/drawable/sym_call_lcd.png similarity index 100% rename from res/drawable/sym_call_lcd.png rename to app/src/main/res/drawable/sym_call_lcd.png diff --git a/res/drawable/sym_call_merge.png b/app/src/main/res/drawable/sym_call_merge.png similarity index 100% rename from res/drawable/sym_call_merge.png rename to app/src/main/res/drawable/sym_call_merge.png diff --git a/res/drawable/sym_call_redial.png b/app/src/main/res/drawable/sym_call_redial.png similarity index 100% rename from res/drawable/sym_call_redial.png rename to app/src/main/res/drawable/sym_call_redial.png diff --git a/res/drawable/sym_call_speakerphone_off.png b/app/src/main/res/drawable/sym_call_speakerphone_off.png similarity index 100% rename from res/drawable/sym_call_speakerphone_off.png rename to app/src/main/res/drawable/sym_call_speakerphone_off.png diff --git a/res/drawable/sym_call_speakerphone_on.png b/app/src/main/res/drawable/sym_call_speakerphone_on.png similarity index 100% rename from res/drawable/sym_call_speakerphone_on.png rename to app/src/main/res/drawable/sym_call_speakerphone_on.png diff --git a/res/drawable/sym_fav_five.png b/app/src/main/res/drawable/sym_fav_five.png similarity index 100% rename from res/drawable/sym_fav_five.png rename to app/src/main/res/drawable/sym_fav_five.png diff --git a/res/drawable/sym_incoming_call_answer.png b/app/src/main/res/drawable/sym_incoming_call_answer.png similarity index 100% rename from res/drawable/sym_incoming_call_answer.png rename to app/src/main/res/drawable/sym_incoming_call_answer.png diff --git a/res/drawable/sym_incoming_call_answer_options.png b/app/src/main/res/drawable/sym_incoming_call_answer_options.png similarity index 100% rename from res/drawable/sym_incoming_call_answer_options.png rename to app/src/main/res/drawable/sym_incoming_call_answer_options.png diff --git a/res/drawable/sym_incoming_call_dont_answer.png b/app/src/main/res/drawable/sym_incoming_call_dont_answer.png similarity index 100% rename from res/drawable/sym_incoming_call_dont_answer.png rename to app/src/main/res/drawable/sym_incoming_call_dont_answer.png diff --git a/res/drawable/sym_incoming_call_hold_answer.png b/app/src/main/res/drawable/sym_incoming_call_hold_answer.png similarity index 100% rename from res/drawable/sym_incoming_call_hold_answer.png rename to app/src/main/res/drawable/sym_incoming_call_hold_answer.png diff --git a/res/drawable/sym_item_indicator.png b/app/src/main/res/drawable/sym_item_indicator.png similarity index 100% rename from res/drawable/sym_item_indicator.png rename to app/src/main/res/drawable/sym_item_indicator.png diff --git a/res/drawable/sym_item_indicator_focus.png b/app/src/main/res/drawable/sym_item_indicator_focus.png similarity index 100% rename from res/drawable/sym_item_indicator_focus.png rename to app/src/main/res/drawable/sym_item_indicator_focus.png diff --git a/res/drawable/sym_presence_available.png b/app/src/main/res/drawable/sym_presence_available.png similarity index 100% rename from res/drawable/sym_presence_available.png rename to app/src/main/res/drawable/sym_presence_available.png diff --git a/res/drawable/sym_presence_away.png b/app/src/main/res/drawable/sym_presence_away.png similarity index 100% rename from res/drawable/sym_presence_away.png rename to app/src/main/res/drawable/sym_presence_away.png diff --git a/res/drawable/sym_presence_idle.png b/app/src/main/res/drawable/sym_presence_idle.png similarity index 100% rename from res/drawable/sym_presence_idle.png rename to app/src/main/res/drawable/sym_presence_idle.png diff --git a/res/drawable/sym_presence_offline.png b/app/src/main/res/drawable/sym_presence_offline.png similarity index 100% rename from res/drawable/sym_presence_offline.png rename to app/src/main/res/drawable/sym_presence_offline.png diff --git a/res/layout-finger/dialpad.xml b/app/src/main/res/layout-finger/dialpad.xml similarity index 100% rename from res/layout-finger/dialpad.xml rename to app/src/main/res/layout-finger/dialpad.xml diff --git a/res/layout-finger/dtmf_display.xml b/app/src/main/res/layout-finger/dtmf_display.xml similarity index 100% rename from res/layout-finger/dtmf_display.xml rename to app/src/main/res/layout-finger/dtmf_display.xml diff --git a/res/layout-finger/dtmf_twelve_key_dialer.xml b/app/src/main/res/layout-finger/dtmf_twelve_key_dialer.xml similarity index 100% rename from res/layout-finger/dtmf_twelve_key_dialer.xml rename to app/src/main/res/layout-finger/dtmf_twelve_key_dialer.xml diff --git a/res/layout-land/call_card_person_info.xml b/app/src/main/res/layout-land/call_card_person_info.xml similarity index 97% rename from res/layout-land/call_card_person_info.xml rename to app/src/main/res/layout-land/call_card_person_info.xml index 513a3c7..ca49265 100644 --- a/res/layout-land/call_card_person_info.xml +++ b/app/src/main/res/layout-land/call_card_person_info.xml @@ -36,8 +36,8 @@ scaleType="centerInside" here (rather than the scaleType="center" we use in portrait mode, where photos are drawn at their true size.) --> diff --git a/res/layout/call_card.xml b/app/src/main/res/layout/call_card.xml similarity index 100% rename from res/layout/call_card.xml rename to app/src/main/res/layout/call_card.xml diff --git a/res/layout/call_card_person_info.xml b/app/src/main/res/layout/call_card_person_info.xml similarity index 97% rename from res/layout/call_card_person_info.xml rename to app/src/main/res/layout/call_card_person_info.xml index 1f1b806..fd022bb 100644 --- a/res/layout/call_card_person_info.xml +++ b/app/src/main/res/layout/call_card_person_info.xml @@ -36,8 +36,8 @@ you see the white border *and* lots of gray space around the photo... Need to confirm this is OK. --> diff --git a/res/layout/call_card_popup.xml b/app/src/main/res/layout/call_card_popup.xml similarity index 100% rename from res/layout/call_card_popup.xml rename to app/src/main/res/layout/call_card_popup.xml diff --git a/res/layout/create_dialog.xml b/app/src/main/res/layout/create_dialog.xml similarity index 97% rename from res/layout/create_dialog.xml rename to app/src/main/res/layout/create_dialog.xml index 7698c06..dea3e61 100644 --- a/res/layout/create_dialog.xml +++ b/app/src/main/res/layout/create_dialog.xml @@ -1,137 +1,137 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/layout/incall.xml b/app/src/main/res/layout/incall.xml similarity index 97% rename from res/layout/incall.xml rename to app/src/main/res/layout/incall.xml index f7bc79e..57fb426 100644 --- a/res/layout/incall.xml +++ b/app/src/main/res/layout/incall.xml @@ -1,144 +1,144 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/layout/ongoing_call_notification.xml b/app/src/main/res/layout/ongoing_call_notification.xml similarity index 94% rename from res/layout/ongoing_call_notification.xml rename to app/src/main/res/layout/ongoing_call_notification.xml index 5dbf1d2..a8208bd 100644 --- a/res/layout/ongoing_call_notification.xml +++ b/app/src/main/res/layout/ongoing_call_notification.xml @@ -43,14 +43,14 @@ - - - - - - - - - - - - - - - - - -