mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-08 08:10:30 -10:00
formats: add self-contained XMA and WMA audio decoders
Adapt LGPL WMA spectral decoding and codebooks behind a C89 packet API without a libavcodec dependency. Decode raw XMA bit spans directly and lend planar float PCM from reusable context storage. Allocate only at creation, retain bounded split-packet history, and keep file I/O, playback and resampling outside the decoder. Add optional HAVE_RWMA sources, analytical XMA/IMDCT and packet-history tests, malformed-input and allocation checks, and a test-only FFmpeg PCM oracle in CI. Linux ASan/UBSan, MinGW and MSVC focused tests pass. Core migration, history serialization and gameplay validation remain separate.
This commit is contained in:
1 parent
dfbfe56b05
commit
7777f39f5c
16 files changed
+16726
-1
No files matched your search
@@ -41,7 +41,7 @@ jobs:
|
||||
group: [codecs, formats, core]
|
||||
env:
|
||||
SAMPLE_GROUP: ${{ matrix.group }}
|
||||
APT_PKGS: build-essential zlib1g-dev clang ffmpeg ccache
|
||||
APT_PKGS: build-essential zlib1g-dev clang ffmpeg libavcodec-dev libavformat-dev libavutil-dev pkg-config ccache
|
||||
timeout-minutes: 15
|
||||
|
||||
steps:
|
||||
@@ -397,6 +397,9 @@ jobs:
|
||||
# stereo stream (tones, gain points, swap and negate masks)
|
||||
# and a mono one, then bad frames and garbage.
|
||||
ratrac3p_test
|
||||
# Raw XMA analytical PCM, WMA packet history, malformed
|
||||
# input and allocation-free decode/reset.
|
||||
rwma_test
|
||||
# Animation playback against a scripted stream: order,
|
||||
# pacing, loops, catch-up, the worker's two jobs.
|
||||
video_player_test
|
||||
@@ -985,6 +988,22 @@ jobs:
|
||||
INERT_ONLY=1 ./data_transfer_prefix_test
|
||||
./data_transfer_window_test
|
||||
|
||||
- name: Compare WMA decoders with FFmpeg
|
||||
if: ${{ always() && matrix.group == 'codecs' }}
|
||||
shell: bash
|
||||
working-directory: libretro-common/samples/formats/wma
|
||||
run: |
|
||||
set -eu
|
||||
make -j2 reference SANITIZER=address,undefined
|
||||
out="$RUNNER_TEMP/rwma-fixtures"
|
||||
mkdir -p "$out"
|
||||
ffmpeg -v error -f lavfi -i 'sine=frequency=997:sample_rate=22050:duration=1' -c:a wmav1 -b:a 32k -flags +bitexact -y "$out/v1.wma"
|
||||
ffmpeg -v error -f lavfi -i 'aevalsrc=0.3*sin(2*PI*997*t)|0.2*sin(2*PI*2399*t):s=48000:d=1' -c:a wmav2 -b:a 128k -flags +bitexact -y "$out/v2-stereo.wma"
|
||||
ffmpeg -v error -f lavfi -i 'sine=frequency=733:sample_rate=48000:duration=1' -c:a wmav2 -b:a 24k -flags +bitexact -y "$out/v2-noise.wma"
|
||||
for file in "$out"/*.wma; do
|
||||
ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1 ./rwma_reference "$file"
|
||||
done
|
||||
|
||||
- name: ccache statistics
|
||||
if: ${{ !cancelled() }}
|
||||
continue-on-error: true
|
||||
|
||||
@@ -398,3 +398,4 @@ samples/gfx/d3d12_upscaler/Zdir_none/
|
||||
samples/record/ffmpeg_pipeline/record_finalize_fragment.c
|
||||
samples/record/ffmpeg_pipeline/record_start_fragment.c
|
||||
samples/input/sensor_subdevice/core_sensor_fragment.c
|
||||
libretro-common/samples/formats/wma/rwma_reference
|
||||
@@ -1,4 +1,5 @@
|
||||
# Future
|
||||
- LIBRETRO-COMMON: Add optional C89 XMA raw-frame and WMA v1/v2/Pro decoders with borrowed float PCM, reusable storage and no FFmpeg runtime dependency
|
||||
- D3D11: Software-rendering cores draw straight into lent staging textures, copied to the frame texture on the GPU instead of by the CPU
|
||||
- VULKAN: Keep the pipeline cache on disk between runs, one file per GPU, so stock and slang shader pipelines are not recompiled by the driver on every launch, preset load or driver reinit
|
||||
- LINUX: Game Mode implemented natively (I/O priority plus a power-profiles-daemon performance hold); Feral libgamemode is no longer loaded and deps/feralgamemode is removed
|
||||
|
||||
@@ -2301,6 +2301,12 @@ ifeq ($(HAVE_RAAC), 1)
|
||||
OBJ += $(LIBRETRO_COMM_DIR)/formats/aac/raac.o
|
||||
endif
|
||||
|
||||
ifeq ($(HAVE_RWMA), 1)
|
||||
DEFINES += -DHAVE_RWMA
|
||||
OBJ += $(LIBRETRO_COMM_DIR)/formats/wma/rwma_pro.o \
|
||||
$(LIBRETRO_COMM_DIR)/formats/wma/rwma_standard.o
|
||||
endif
|
||||
|
||||
ifeq ($(HAVE_ROPUS), 1)
|
||||
DEFINES += -DHAVE_ROPUS
|
||||
OBJ += $(LIBRETRO_COMM_DIR)/formats/opus/ropus.o
|
||||
|
||||
@@ -0,0 +1,502 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 2.1, February 1999
|
||||
|
||||
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
[This is the first released version of the Lesser GPL. It also counts
|
||||
as the successor of the GNU Library Public License, version 2, hence
|
||||
the version number 2.1.]
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
Licenses are intended to guarantee your freedom to share and change
|
||||
free software--to make sure the software is free for all its users.
|
||||
|
||||
This license, the Lesser General Public License, applies to some
|
||||
specially designated software packages--typically libraries--of the
|
||||
Free Software Foundation and other authors who decide to use it. You
|
||||
can use it too, but we suggest you first think carefully about whether
|
||||
this license or the ordinary General Public License is the better
|
||||
strategy to use in any particular case, based on the explanations below.
|
||||
|
||||
When we speak of free software, we are referring to freedom of use,
|
||||
not price. Our General Public Licenses are designed to make sure that
|
||||
you have the freedom to distribute copies of free software (and charge
|
||||
for this service if you wish); that you receive source code or can get
|
||||
it if you want it; that you can change the software and use pieces of
|
||||
it in new free programs; and that you are informed that you can do
|
||||
these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
distributors to deny you these rights or to ask you to surrender these
|
||||
rights. These restrictions translate to certain responsibilities for
|
||||
you if you distribute copies of the library or if you modify it.
|
||||
|
||||
For example, if you distribute copies of the library, whether gratis
|
||||
or for a fee, you must give the recipients all the rights that we gave
|
||||
you. You must make sure that they, too, receive or can get the source
|
||||
code. If you link other code with the library, you must provide
|
||||
complete object files to the recipients, so that they can relink them
|
||||
with the library after making changes to the library and recompiling
|
||||
it. And you must show them these terms so they know their rights.
|
||||
|
||||
We protect your rights with a two-step method: (1) we copyright the
|
||||
library, and (2) we offer you this license, which gives you legal
|
||||
permission to copy, distribute and/or modify the library.
|
||||
|
||||
To protect each distributor, we want to make it very clear that
|
||||
there is no warranty for the free library. Also, if the library is
|
||||
modified by someone else and passed on, the recipients should know
|
||||
that what they have is not the original version, so that the original
|
||||
author's reputation will not be affected by problems that might be
|
||||
introduced by others.
|
||||
|
||||
Finally, software patents pose a constant threat to the existence of
|
||||
any free program. We wish to make sure that a company cannot
|
||||
effectively restrict the users of a free program by obtaining a
|
||||
restrictive license from a patent holder. Therefore, we insist that
|
||||
any patent license obtained for a version of the library must be
|
||||
consistent with the full freedom of use specified in this license.
|
||||
|
||||
Most GNU software, including some libraries, is covered by the
|
||||
ordinary GNU General Public License. This license, the GNU Lesser
|
||||
General Public License, applies to certain designated libraries, and
|
||||
is quite different from the ordinary General Public License. We use
|
||||
this license for certain libraries in order to permit linking those
|
||||
libraries into non-free programs.
|
||||
|
||||
When a program is linked with a library, whether statically or using
|
||||
a shared library, the combination of the two is legally speaking a
|
||||
combined work, a derivative of the original library. The ordinary
|
||||
General Public License therefore permits such linking only if the
|
||||
entire combination fits its criteria of freedom. The Lesser General
|
||||
Public License permits more lax criteria for linking other code with
|
||||
the library.
|
||||
|
||||
We call this license the "Lesser" General Public License because it
|
||||
does Less to protect the user's freedom than the ordinary General
|
||||
Public License. It also provides other free software developers Less
|
||||
of an advantage over competing non-free programs. These disadvantages
|
||||
are the reason we use the ordinary General Public License for many
|
||||
libraries. However, the Lesser license provides advantages in certain
|
||||
special circumstances.
|
||||
|
||||
For example, on rare occasions, there may be a special need to
|
||||
encourage the widest possible use of a certain library, so that it becomes
|
||||
a de-facto standard. To achieve this, non-free programs must be
|
||||
allowed to use the library. A more frequent case is that a free
|
||||
library does the same job as widely used non-free libraries. In this
|
||||
case, there is little to gain by limiting the free library to free
|
||||
software only, so we use the Lesser General Public License.
|
||||
|
||||
In other cases, permission to use a particular library in non-free
|
||||
programs enables a greater number of people to use a large body of
|
||||
free software. For example, permission to use the GNU C Library in
|
||||
non-free programs enables many more people to use the whole GNU
|
||||
operating system, as well as its variant, the GNU/Linux operating
|
||||
system.
|
||||
|
||||
Although the Lesser General Public License is Less protective of the
|
||||
users' freedom, it does ensure that the user of a program that is
|
||||
linked with the Library has the freedom and the wherewithal to run
|
||||
that program using a modified version of the Library.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow. Pay close attention to the difference between a
|
||||
"work based on the library" and a "work that uses the library". The
|
||||
former contains code derived from the library, whereas the latter must
|
||||
be combined with the library in order to run.
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License Agreement applies to any software library or other
|
||||
program which contains a notice placed by the copyright holder or
|
||||
other authorized party saying it may be distributed under the terms of
|
||||
this Lesser General Public License (also called "this License").
|
||||
Each licensee is addressed as "you".
|
||||
|
||||
A "library" means a collection of software functions and/or data
|
||||
prepared so as to be conveniently linked with application programs
|
||||
(which use some of those functions and data) to form executables.
|
||||
|
||||
The "Library", below, refers to any such software library or work
|
||||
which has been distributed under these terms. A "work based on the
|
||||
Library" means either the Library or any derivative work under
|
||||
copyright law: that is to say, a work containing the Library or a
|
||||
portion of it, either verbatim or with modifications and/or translated
|
||||
straightforwardly into another language. (Hereinafter, translation is
|
||||
included without limitation in the term "modification".)
|
||||
|
||||
"Source code" for a work means the preferred form of the work for
|
||||
making modifications to it. For a library, complete source code means
|
||||
all the source code for all modules it contains, plus any associated
|
||||
interface definition files, plus the scripts used to control compilation
|
||||
and installation of the library.
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running a program using the Library is not restricted, and output from
|
||||
such a program is covered only if its contents constitute a work based
|
||||
on the Library (independent of the use of the Library in a tool for
|
||||
writing it). Whether that is true depends on what the Library does
|
||||
and what the program that uses the Library does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Library's
|
||||
complete source code as you receive it, in any medium, provided that
|
||||
you conspicuously and appropriately publish on each copy an
|
||||
appropriate copyright notice and disclaimer of warranty; keep intact
|
||||
all the notices that refer to this License and to the absence of any
|
||||
warranty; and distribute a copy of this License along with the
|
||||
Library.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy,
|
||||
and you may at your option offer warranty protection in exchange for a
|
||||
fee.
|
||||
|
||||
2. You may modify your copy or copies of the Library or any portion
|
||||
of it, thus forming a work based on the Library, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) The modified work must itself be a software library.
|
||||
|
||||
b) You must cause the files modified to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
c) You must cause the whole of the work to be licensed at no
|
||||
charge to all third parties under the terms of this License.
|
||||
|
||||
d) If a facility in the modified Library refers to a function or a
|
||||
table of data to be supplied by an application program that uses
|
||||
the facility, other than as an argument passed when the facility
|
||||
is invoked, then you must make a good faith effort to ensure that,
|
||||
in the event an application does not supply such function or
|
||||
table, the facility still operates, and performs whatever part of
|
||||
its purpose remains meaningful.
|
||||
|
||||
(For example, a function in a library to compute square roots has
|
||||
a purpose that is entirely well-defined independent of the
|
||||
application. Therefore, Subsection 2d requires that any
|
||||
application-supplied function or table used by this function must
|
||||
be optional: if the application does not supply it, the square
|
||||
root function must still compute square roots.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Library,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Library, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote
|
||||
it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Library.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Library
|
||||
with the Library (or with a work based on the Library) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||
License instead of this License to a given copy of the Library. To do
|
||||
this, you must alter all the notices that refer to this License, so
|
||||
that they refer to the ordinary GNU General Public License, version 2,
|
||||
instead of to this License. (If a newer version than version 2 of the
|
||||
ordinary GNU General Public License has appeared, then you can specify
|
||||
that version instead if you wish.) Do not make any other change in
|
||||
these notices.
|
||||
|
||||
Once this change is made in a given copy, it is irreversible for
|
||||
that copy, so the ordinary GNU General Public License applies to all
|
||||
subsequent copies and derivative works made from that copy.
|
||||
|
||||
This option is useful when you wish to copy part of the code of
|
||||
the Library into a program that is not a library.
|
||||
|
||||
4. You may copy and distribute the Library (or a portion or
|
||||
derivative of it, under Section 2) in object code or executable form
|
||||
under the terms of Sections 1 and 2 above provided that you accompany
|
||||
it with the complete corresponding machine-readable source code, which
|
||||
must be distributed under the terms of Sections 1 and 2 above on a
|
||||
medium customarily used for software interchange.
|
||||
|
||||
If distribution of object code is made by offering access to copy
|
||||
from a designated place, then offering equivalent access to copy the
|
||||
source code from the same place satisfies the requirement to
|
||||
distribute the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
5. A program that contains no derivative of any portion of the
|
||||
Library, but is designed to work with the Library by being compiled or
|
||||
linked with it, is called a "work that uses the Library". Such a
|
||||
work, in isolation, is not a derivative work of the Library, and
|
||||
therefore falls outside the scope of this License.
|
||||
|
||||
However, linking a "work that uses the Library" with the Library
|
||||
creates an executable that is a derivative of the Library (because it
|
||||
contains portions of the Library), rather than a "work that uses the
|
||||
library". The executable is therefore covered by this License.
|
||||
Section 6 states terms for distribution of such executables.
|
||||
|
||||
When a "work that uses the Library" uses material from a header file
|
||||
that is part of the Library, the object code for the work may be a
|
||||
derivative work of the Library even though the source code is not.
|
||||
Whether this is true is especially significant if the work can be
|
||||
linked without the Library, or if the work is itself a library. The
|
||||
threshold for this to be true is not precisely defined by law.
|
||||
|
||||
If such an object file uses only numerical parameters, data
|
||||
structure layouts and accessors, and small macros and small inline
|
||||
functions (ten lines or less in length), then the use of the object
|
||||
file is unrestricted, regardless of whether it is legally a derivative
|
||||
work. (Executables containing this object code plus portions of the
|
||||
Library will still fall under Section 6.)
|
||||
|
||||
Otherwise, if the work is a derivative of the Library, you may
|
||||
distribute the object code for the work under the terms of Section 6.
|
||||
Any executables containing that work also fall under Section 6,
|
||||
whether or not they are linked directly with the Library itself.
|
||||
|
||||
6. As an exception to the Sections above, you may also combine or
|
||||
link a "work that uses the Library" with the Library to produce a
|
||||
work containing portions of the Library, and distribute that work
|
||||
under terms of your choice, provided that the terms permit
|
||||
modification of the work for the customer's own use and reverse
|
||||
engineering for debugging such modifications.
|
||||
|
||||
You must give prominent notice with each copy of the work that the
|
||||
Library is used in it and that the Library and its use are covered by
|
||||
this License. You must supply a copy of this License. If the work
|
||||
during execution displays copyright notices, you must include the
|
||||
copyright notice for the Library among them, as well as a reference
|
||||
directing the user to the copy of this License. Also, you must do one
|
||||
of these things:
|
||||
|
||||
a) Accompany the work with the complete corresponding
|
||||
machine-readable source code for the Library including whatever
|
||||
changes were used in the work (which must be distributed under
|
||||
Sections 1 and 2 above); and, if the work is an executable linked
|
||||
with the Library, with the complete machine-readable "work that
|
||||
uses the Library", as object code and/or source code, so that the
|
||||
user can modify the Library and then relink to produce a modified
|
||||
executable containing the modified Library. (It is understood
|
||||
that the user who changes the contents of definitions files in the
|
||||
Library will not necessarily be able to recompile the application
|
||||
to use the modified definitions.)
|
||||
|
||||
b) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (1) uses at run time a
|
||||
copy of the library already present on the user's computer system,
|
||||
rather than copying library functions into the executable, and (2)
|
||||
will operate properly with a modified version of the library, if
|
||||
the user installs one, as long as the modified version is
|
||||
interface-compatible with the version that the work was made with.
|
||||
|
||||
c) Accompany the work with a written offer, valid for at
|
||||
least three years, to give the same user the materials
|
||||
specified in Subsection 6a, above, for a charge no more
|
||||
than the cost of performing this distribution.
|
||||
|
||||
d) If distribution of the work is made by offering access to copy
|
||||
from a designated place, offer equivalent access to copy the above
|
||||
specified materials from the same place.
|
||||
|
||||
e) Verify that the user has already received a copy of these
|
||||
materials or that you have already sent this user a copy.
|
||||
|
||||
For an executable, the required form of the "work that uses the
|
||||
Library" must include any data and utility programs needed for
|
||||
reproducing the executable from it. However, as a special exception,
|
||||
the materials to be distributed need not include anything that is
|
||||
normally distributed (in either source or binary form) with the major
|
||||
components (compiler, kernel, and so on) of the operating system on
|
||||
which the executable runs, unless that component itself accompanies
|
||||
the executable.
|
||||
|
||||
It may happen that this requirement contradicts the license
|
||||
restrictions of other proprietary libraries that do not normally
|
||||
accompany the operating system. Such a contradiction means you cannot
|
||||
use both them and the Library together in an executable that you
|
||||
distribute.
|
||||
|
||||
7. You may place library facilities that are a work based on the
|
||||
Library side-by-side in a single library together with other library
|
||||
facilities not covered by this License, and distribute such a combined
|
||||
library, provided that the separate distribution of the work based on
|
||||
the Library and of the other library facilities is otherwise
|
||||
permitted, and provided that you do these two things:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work
|
||||
based on the Library, uncombined with any other library
|
||||
facilities. This must be distributed under the terms of the
|
||||
Sections above.
|
||||
|
||||
b) Give prominent notice with the combined library of the fact
|
||||
that part of it is a work based on the Library, and explaining
|
||||
where to find the accompanying uncombined form of the same work.
|
||||
|
||||
8. You may not copy, modify, sublicense, link with, or distribute
|
||||
the Library except as expressly provided under this License. Any
|
||||
attempt otherwise to copy, modify, sublicense, link with, or
|
||||
distribute the Library is void, and will automatically terminate your
|
||||
rights under this License. However, parties who have received copies,
|
||||
or rights, from you under this License will not have their licenses
|
||||
terminated so long as such parties remain in full compliance.
|
||||
|
||||
9. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Library or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Library (or any work based on the
|
||||
Library), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Library or works based on it.
|
||||
|
||||
10. Each time you redistribute the Library (or any work based on the
|
||||
Library), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute, link with or modify the Library
|
||||
subject to these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties with
|
||||
this License.
|
||||
|
||||
11. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Library at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Library by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Library.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under any
|
||||
particular circumstance, the balance of the section is intended to apply,
|
||||
and the section as a whole is intended to apply in other circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
12. If the distribution and/or use of the Library is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Library under this License may add
|
||||
an explicit geographical distribution limitation excluding those countries,
|
||||
so that distribution is permitted only in or among countries not thus
|
||||
excluded. In such case, this License incorporates the limitation as if
|
||||
written in the body of this License.
|
||||
|
||||
13. The Free Software Foundation may publish revised and/or new
|
||||
versions of the Lesser General Public License from time to time.
|
||||
Such new versions will be similar in spirit to the present version,
|
||||
but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library
|
||||
specifies a version number of this License which applies to it and
|
||||
"any later version", you have the option of following the terms and
|
||||
conditions either of that version or of any later version published by
|
||||
the Free Software Foundation. If the Library does not specify a
|
||||
license version number, you may choose any version ever published by
|
||||
the Free Software Foundation.
|
||||
|
||||
14. If you wish to incorporate parts of the Library into other free
|
||||
programs whose distribution conditions are incompatible with these,
|
||||
write to the author to ask for permission. For software which is
|
||||
copyrighted by the Free Software Foundation, write to the Free
|
||||
Software Foundation; we sometimes make exceptions for this. Our
|
||||
decision will be guided by the two goals of preserving the free status
|
||||
of all derivatives of our free software and of promoting the sharing
|
||||
and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
|
||||
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
|
||||
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
|
||||
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
|
||||
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
|
||||
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
|
||||
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
|
||||
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
|
||||
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
|
||||
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
|
||||
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
|
||||
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Libraries
|
||||
|
||||
If you develop a new library, and you want it to be of the greatest
|
||||
possible use to the public, we recommend making it free software that
|
||||
everyone can redistribute and change. You can do so by permitting
|
||||
redistribution under these terms (or, alternatively, under the terms of the
|
||||
ordinary General Public License).
|
||||
|
||||
To apply these terms, attach the following notices to the library. It is
|
||||
safest to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least the
|
||||
"copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the library's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the library, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the
|
||||
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1990
|
||||
Ty Coon, President of Vice
|
||||
|
||||
That's all there is to it!
|
||||
@@ -0,0 +1,76 @@
|
||||
# XMA / WMA decoding
|
||||
|
||||
`include/formats/rwma.h` provides C89 raw XMA-frame and WMA v1/v2/Pro packet
|
||||
decoders. Compile `rwma_pro.c` and `rwma_standard.c` and link the platform math
|
||||
library. RetroArch's optional `HAVE_RWMA=1` Makefile setting adds these sources;
|
||||
it does not enable a frontend player or alter existing playback.
|
||||
|
||||
The spectral, channel-transform and overlap algorithms and codebooks are adapted
|
||||
from FFmpeg's LGPL-2.1-or-later WMA decoders. Their notices and license are retained.
|
||||
This is a self-contained decoder port, not an FFmpeg API wrapper: it has
|
||||
no libavcodec/libavformat dependency. The private radix-2 DCT-IV uses reusable
|
||||
storage, and all Huffman trees are immutable compile-time tables. There is no
|
||||
runtime global table initialization, shared mutable codec state or locking.
|
||||
The source reference is `libretro-fork/parallel-xbox360` at `34a3fb2ad`,
|
||||
`third_party/FFmpeg/libavcodec/{wmaprodec,wmadec,wma}.c` and their codebooks;
|
||||
the raw-XMA frame interface includes that fork's packet-loss reset behavior.
|
||||
|
||||
## Input and output ownership
|
||||
|
||||
- Create one context per independent stream with `rwma_create`. All allocation
|
||||
happens here. Decode and reset reuse the storage; free releases it.
|
||||
- XMA input is one complete raw frame including the 15-bit length prefix. Pass
|
||||
the actual bit offset and length with `rwma_decode_xma`: no padding byte,
|
||||
aligned reconstruction or encoded-frame copy is needed. Mono and stereo
|
||||
streams decode independently; the caller handles the Xbox packet transport,
|
||||
multistream layout, guest output rings and scheduling.
|
||||
- WMA input is already demuxed. Supply its format extradata, block alignment,
|
||||
sample rate, channel count and (for v1/v2) bitrate at creation. Pass a packet
|
||||
window to `rwma_decode_packet`. Advance by `consumed`. Zero consumption means
|
||||
the same window must remain lent for the next frame; positive consumption
|
||||
releases that prefix. Do not replace an outstanding input window.
|
||||
- Returned float planes borrow decoder storage until the next decode, reset or
|
||||
free. Consume them directly or convert straight into the final destination.
|
||||
No separate PCM output buffer or per-frame PCM publication copy exists.
|
||||
- Raw XMA trim values and WMA v1/v2 priming delay are metadata in the returned
|
||||
frame; apply them once in the consumer. WMA Pro follows its packet decode
|
||||
semantics: suppress the initial priming frame and apply stream trims.
|
||||
- After finishing packet input, a zero-byte packet call returns the remaining
|
||||
overlap once. Empty streams and repeated drains produce no frame. Reset starts a new stream;
|
||||
it is not a savestate restore. Errors clear the output view; reset before
|
||||
continuing after malformed compressed data.
|
||||
|
||||
Complete length-prefixed WMA Pro frames borrow packet bits. A compressed frame
|
||||
split across packets needs a bounded retained reservoir copy. WMA v1/v2 retain
|
||||
their packet tail for the same reason. Overlap history still moves inside
|
||||
reused storage; this is not an entirely copy-free codec. Nothing here reads a
|
||||
file, resamples, mixes, paces playback or submits to an audio device.
|
||||
|
||||
WMA Voice, WMA Lossless, DRM and ASF/XWMA/WAV demultiplexing are not implemented.
|
||||
WMA Pro handles up to eight channels, with the inherited limitation on default
|
||||
decorrelation matrices for groups larger than six; unsupported syntax fails.
|
||||
Porting the core still needs packet/trim integration, decoder-history savestate
|
||||
support and full-game audio validation. FFmpeg remains in parallel-xbox360.
|
||||
|
||||
## Verification
|
||||
|
||||
`samples/formats/wma` builds without FFmpeg. Its procedural fixtures check
|
||||
196,608 XMA PCM values against an independent cosine-sum/overlap reference,
|
||||
mono/stereo channels, unaligned input, signs, spectral positions and retained
|
||||
history. IMDCT lengths 64 through 8192 are checked independently. Allocation
|
||||
failure, 10,000 malformed frames/packets, reset, WMA packet continuation and EOF
|
||||
are covered. Allocation hooks assert no decode/reset allocation. A polarity
|
||||
negative control fails the PCM test. Linux ASan/UBSan and MinGW checks pass.
|
||||
|
||||
The optional `make reference` target needs FFmpeg development libraries only
|
||||
for the test oracle. CI encodes procedural WMA v1/v2 streams, including a
|
||||
low-bitrate noise-coding case, and compares native float PCM. The oracle's
|
||||
headers require a C99 compiler mode; the decoder sources remain strict C89.
|
||||
No music or game samples are checked in or downloaded by CI.
|
||||
|
||||
Additional local differentials used 376 game XMA frames (385,024 channel
|
||||
samples), and the FFmpeg FATE WMA Pro stereo/5.1 samples (44/102 output frames).
|
||||
Maximum absolute PCM differences in those runs were below 3.6e-7 for XMA and
|
||||
7.5e-8 for Pro. Both implementations rejected the truncated terminal packets
|
||||
in the Pro files. These are local coverage, not public CI fixtures or a claim
|
||||
of exhaustive bitstream compatibility, gameplay accuracy or faster decoding.
|
||||
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* WMA compatible codec
|
||||
* Copyright (c) 2002-2007 The FFmpeg Project
|
||||
*
|
||||
* Adapted from FFmpeg for libretro-common; no libavcodec dependency.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/* LGPL-2.1-or-later. Private bounded bit reader and scalar DSP. */
|
||||
#ifndef LIBRETRO_RWMA_INTERNAL_H
|
||||
#define LIBRETRO_RWMA_INTERNAL_H
|
||||
#include <assert.h>
|
||||
#include <float.h>
|
||||
#include <formats/rwma.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <retro_inline.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define RWMA_PI 3.14159265358979323846
|
||||
#define RWMA_MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define RWMA_MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define RWMA_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||
static INLINE int rwma_frame_valid(const rwma_frame *frame)
|
||||
{
|
||||
unsigned c, i;
|
||||
for (c = 0; c < frame->channels; c++)
|
||||
for (i = 0; i < frame->samples; i++)
|
||||
if (!(frame->planes[c][i] <= FLT_MAX && frame->planes[c][i] >= -FLT_MAX))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
#define WMAPRO_MAX_CHANNELS 8
|
||||
#define MAX_SUBFRAMES 32
|
||||
#define MAX_BANDS 29
|
||||
#define WMAPRO_BLOCK_MIN_BITS 6
|
||||
#define WMAPRO_BLOCK_MAX_BITS 13
|
||||
#define WMAPRO_BLOCK_SIZES 8
|
||||
#define WMAPRO_BLOCK_MAX_SIZE 8192
|
||||
#define WMAPRO_BLOCK_MIN_SIZE 64
|
||||
#define MAX_FRAMESIZE 32768
|
||||
typedef struct rwma_packet
|
||||
{
|
||||
const uint8_t *data;
|
||||
int size;
|
||||
} rwma_packet;
|
||||
typedef struct rwma_bits
|
||||
{
|
||||
const uint8_t *data;
|
||||
int size, pos, error;
|
||||
} rwma_bits;
|
||||
typedef struct rwma_vlc_node
|
||||
{
|
||||
int16_t next[2];
|
||||
int16_t symbol;
|
||||
} rwma_vlc_node;
|
||||
static INLINE unsigned get_bits(rwma_bits *b, int n)
|
||||
{
|
||||
unsigned value = 0;
|
||||
if (n < 0 || n > 32 || b->pos > b->size - n)
|
||||
{
|
||||
b->error = 1;
|
||||
return 0;
|
||||
}
|
||||
while (n)
|
||||
{
|
||||
unsigned take = RWMA_MIN(n, 8 - (b->pos & 7));
|
||||
value = (value << take) | ((b->data[b->pos >> 3] >> (8 - (b->pos & 7) - take)) &
|
||||
((1u << take) - 1));
|
||||
b->pos += take;
|
||||
n -= take;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
static INLINE unsigned get_bits1(rwma_bits *b) { return get_bits(b, 1); }
|
||||
static INLINE int get_sbits(rwma_bits *b, int n)
|
||||
{
|
||||
unsigned v = get_bits(b, n), sign = 1u << (n - 1);
|
||||
return (int)(v ^ sign) - (int)sign;
|
||||
}
|
||||
static INLINE unsigned get_bitsz(rwma_bits *b, int n) { return get_bits(b, n); }
|
||||
static INLINE int get_bits_count(const rwma_bits *b) { return b->pos; }
|
||||
static INLINE int get_bits_left(const rwma_bits *b) { return b->size - b->pos; }
|
||||
static INLINE int init_get_bits(rwma_bits *b, const uint8_t *data, int bits)
|
||||
{
|
||||
b->data = data;
|
||||
b->size = bits;
|
||||
b->pos = b->error = 0;
|
||||
return bits < 0 ? RWMA_BAD_DATA : RWMA_OK;
|
||||
}
|
||||
static INLINE int init_get_bits8(rwma_bits *b, const uint8_t *data, int bytes)
|
||||
{
|
||||
if (bytes < 0 || bytes > INT_MAX / 8)
|
||||
return RWMA_BAD_DATA;
|
||||
return init_get_bits(b, data, bytes * 8);
|
||||
}
|
||||
static INLINE unsigned show_bits(rwma_bits *b, int n)
|
||||
{
|
||||
int pos = b->pos;
|
||||
unsigned value = get_bits(b, n);
|
||||
b->pos = pos;
|
||||
return value;
|
||||
}
|
||||
static INLINE void skip_bits(rwma_bits *b, int n)
|
||||
{
|
||||
if (n < 0 || b->pos > b->size - n)
|
||||
b->error = 1;
|
||||
else
|
||||
b->pos += n;
|
||||
}
|
||||
#define skip_bits_long skip_bits
|
||||
static INLINE int rwma_vlc(rwma_bits *b, const rwma_vlc_node *table)
|
||||
{
|
||||
int node = 0, depth;
|
||||
for (depth = 0; depth < 32; depth++)
|
||||
{
|
||||
if (table[node].next[0] < 0 && table[node].next[1] < 0)
|
||||
return table[node].symbol;
|
||||
node = table[node].next[get_bits1(b)];
|
||||
if (b->error || node < 0)
|
||||
break;
|
||||
}
|
||||
b->error = 1;
|
||||
return 0;
|
||||
}
|
||||
static INLINE int rwma_log2(unsigned x)
|
||||
{
|
||||
int n = 0;
|
||||
while (x >>= 1)
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
static INLINE uint32_t rwma_float_bits(float value)
|
||||
{
|
||||
uint32_t bits;
|
||||
memcpy(&bits, &value, sizeof(bits));
|
||||
return bits;
|
||||
}
|
||||
static INLINE void rwma_store_float(float *dst, uint32_t bits)
|
||||
{
|
||||
memcpy(dst, &bits, sizeof(bits));
|
||||
}
|
||||
static INLINE float rwma_exp10(double x) { return (float)pow(10.0, x); }
|
||||
static INLINE unsigned rwma_large_value(rwma_bits *b)
|
||||
{
|
||||
int n = 8;
|
||||
if (get_bits1(b))
|
||||
{
|
||||
n += 8;
|
||||
if (get_bits1(b))
|
||||
{
|
||||
n += 8;
|
||||
if (get_bits1(b))
|
||||
n += 7;
|
||||
}
|
||||
}
|
||||
return get_bits(b, n);
|
||||
}
|
||||
static INLINE int rwma_run_level(rwma_bits *b, const rwma_vlc_node *vlc,
|
||||
const float *levels, const uint16_t *runs, int version,
|
||||
float *dst, int offset, int count, int block,
|
||||
int escape_bits, int coefficient_bits)
|
||||
{
|
||||
int code, sign;
|
||||
unsigned level;
|
||||
for (; offset < count; offset++)
|
||||
{
|
||||
code = rwma_vlc(b, vlc);
|
||||
if (b->error || code < 0)
|
||||
return RWMA_BAD_DATA;
|
||||
if (code == 1)
|
||||
break;
|
||||
if (code > 1)
|
||||
{
|
||||
offset += runs[code];
|
||||
sign = (int)get_bits1(b) - 1;
|
||||
if (offset >= count)
|
||||
return RWMA_BAD_DATA;
|
||||
rwma_store_float(dst + offset, rwma_float_bits(levels[code]) ^
|
||||
((uint32_t)sign & 0x80000000u));
|
||||
}
|
||||
else
|
||||
{
|
||||
level = version ? rwma_large_value(b) : get_bits(b, coefficient_bits);
|
||||
if (!version)
|
||||
offset += get_bits(b, escape_bits);
|
||||
else if (get_bits1(b))
|
||||
{
|
||||
if (get_bits1(b))
|
||||
{
|
||||
if (get_bits1(b))
|
||||
return RWMA_BAD_DATA;
|
||||
offset += get_bits(b, escape_bits) + 4;
|
||||
}
|
||||
else
|
||||
offset += get_bits(b, 2) + 1;
|
||||
}
|
||||
sign = (int)get_bits1(b) - 1;
|
||||
if (offset >= count)
|
||||
return RWMA_BAD_DATA;
|
||||
dst[offset] = sign ? -(float)level : (float)level;
|
||||
}
|
||||
if (b->error)
|
||||
return RWMA_BAD_DATA;
|
||||
}
|
||||
(void)block;
|
||||
return RWMA_OK;
|
||||
}
|
||||
int rwma_standard_create(void **out, const rwma_config *cfg);
|
||||
void rwma_standard_free(void *);
|
||||
void rwma_standard_reset(void *);
|
||||
int rwma_standard_decode(void *, const uint8_t *, size_t, size_t *, rwma_frame *);
|
||||
static INLINE void rwma_multiply(float *dst, const float *src, float scale, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
dst[i] = src[i] * scale;
|
||||
}
|
||||
static INLINE void rwma_window(float *dst, const float *src0, const float *src1,
|
||||
const float *window, int n)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0, j = n - 1; i < n; i++, j--)
|
||||
{
|
||||
float a = src0[i], b = src1[j];
|
||||
float wi = window[i], wj = window[2 * n - 1 - i];
|
||||
dst[i] = a * wj - b * wi;
|
||||
dst[2 * n - 1 - i] = a * wi + b * wj;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,138 @@
|
||||
/* Copyright (C) 2010-2026 The RetroArch team
|
||||
*
|
||||
* ---------------------------------------------------------------------------------------
|
||||
* The following license statement only applies to this file (rwma_mdct.h).
|
||||
* ---------------------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge,
|
||||
* to any person obtaining a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction, including without
|
||||
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies
|
||||
* or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/* C89 radix-2 DCT-IV, reusable caller storage. */
|
||||
#ifndef LIBRETRO_RWMA_MDCT_H
|
||||
#define LIBRETRO_RWMA_MDCT_H
|
||||
typedef struct rwma_mdct
|
||||
{
|
||||
const float *windows[WMAPRO_BLOCK_SIZES];
|
||||
const float *pre_re[WMAPRO_BLOCK_SIZES], *pre_im[WMAPRO_BLOCK_SIZES];
|
||||
const float *post_re[WMAPRO_BLOCK_SIZES], *post_im[WMAPRO_BLOCK_SIZES];
|
||||
float *twiddle_re, *twiddle_im;
|
||||
float *fft_re, *fft_im;
|
||||
unsigned max_frame;
|
||||
} rwma_mdct;
|
||||
static void rwma_mdct_init(rwma_mdct *m, float *p, unsigned max_frame)
|
||||
{
|
||||
unsigned i, c, n;
|
||||
m->max_frame = max_frame;
|
||||
m->fft_re = p;
|
||||
p += m->max_frame / 2;
|
||||
m->fft_im = p;
|
||||
p += m->max_frame / 2;
|
||||
m->twiddle_re = p;
|
||||
p += m->max_frame / 2;
|
||||
m->twiddle_im = p;
|
||||
p += m->max_frame / 2;
|
||||
for (i = 0; i < m->max_frame / 2; i++)
|
||||
{
|
||||
double angle = -2 * RWMA_PI * i / (m->max_frame / 2);
|
||||
m->twiddle_re[i] = (float)cos(angle);
|
||||
m->twiddle_im[i] = (float)sin(angle);
|
||||
}
|
||||
for (n = 64, i = 0; n <= m->max_frame; n *= 2, i++)
|
||||
{
|
||||
m->windows[i] = p;
|
||||
p += n;
|
||||
m->pre_re[i] = p;
|
||||
p += n / 2;
|
||||
m->pre_im[i] = p;
|
||||
p += n / 2;
|
||||
m->post_re[i] = p;
|
||||
p += n / 2;
|
||||
m->post_im[i] = p;
|
||||
p += n / 2;
|
||||
for (c = 0; c < n; c++)
|
||||
((float *)m->windows[i])[c] = (float)sin((c + 0.5) * RWMA_PI / (2 * n));
|
||||
for (c = 0; c < n / 2; c++)
|
||||
{
|
||||
double a = -RWMA_PI * (4 * c + 1) / (4 * n);
|
||||
double b = -RWMA_PI * c / n;
|
||||
((float *)m->pre_re[i])[c] = (float)cos(a);
|
||||
((float *)m->pre_im[i])[c] = (float)sin(a);
|
||||
((float *)m->post_re[i])[c] = (float)cos(b);
|
||||
((float *)m->post_im[i])[c] = (float)sin(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* DCT-IV through an N/2-point FFT, scattered directly in IMDCT order. */
|
||||
static void rwma_mdct_run(rwma_mdct *s, float *dst, const float *x, int n, float scale)
|
||||
{
|
||||
int k, i, j, len, half, q, size = n / 2;
|
||||
int plan = rwma_log2(n) - 6;
|
||||
float *re = s->fft_re, *im = s->fft_im;
|
||||
const float *pr = s->pre_re[plan], *pi = s->pre_im[plan];
|
||||
const float *qr = s->post_re[plan], *qi = s->post_im[plan];
|
||||
for (k = 0; k < size; k++)
|
||||
{
|
||||
float a = x[2 * k], b = x[n - 1 - 2 * k];
|
||||
re[k] = a * pr[k] - b * pi[k];
|
||||
im[k] = a * pi[k] + b * pr[k];
|
||||
}
|
||||
for (i = 1, j = 0; i < size; i++)
|
||||
{
|
||||
int bit = size / 2;
|
||||
for (; j & bit; bit >>= 1)
|
||||
j ^= bit;
|
||||
j ^= bit;
|
||||
if (i < j)
|
||||
{
|
||||
float a = re[i], b = im[i];
|
||||
re[i] = re[j];
|
||||
im[i] = im[j];
|
||||
re[j] = a;
|
||||
im[j] = b;
|
||||
}
|
||||
}
|
||||
for (len = 2; len <= size; len *= 2)
|
||||
{
|
||||
half = len / 2;
|
||||
for (i = 0; i < size; i += len)
|
||||
for (j = 0; j < half; j++)
|
||||
{
|
||||
int a = i + j, b = a + half;
|
||||
float wr, wi, vr, vi, ur = re[a], ui = im[a];
|
||||
q = j * (s->max_frame / 2 / len);
|
||||
wr = s->twiddle_re[q];
|
||||
wi = s->twiddle_im[q];
|
||||
vr = re[b] * wr - im[b] * wi;
|
||||
vi = re[b] * wi + im[b] * wr;
|
||||
re[a] = ur + vr;
|
||||
im[a] = ui + vi;
|
||||
re[b] = ur - vr;
|
||||
im[b] = ui - vi;
|
||||
}
|
||||
}
|
||||
for (k = 0; k < size; k++)
|
||||
{
|
||||
float yr = re[k] * qr[k] - im[k] * qi[k];
|
||||
float yi = re[k] * qi[k] + im[k] * qr[k];
|
||||
dst[n - 1 - 2 * k] = yr * scale;
|
||||
dst[2 * k] = -yi * scale;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,49 @@
|
||||
/* XMA raw frames and WMA v1/v2/Pro audio decoding. LGPL-2.1-or-later. */
|
||||
#ifndef LIBRETRO_RWMA_H
|
||||
#define LIBRETRO_RWMA_H
|
||||
#include <retro_common_api.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
RETRO_BEGIN_DECLS
|
||||
#define RWMA_OK 0
|
||||
#define RWMA_BAD_DATA (-1)
|
||||
#define RWMA_UNSUPPORTED (-2)
|
||||
#define RWMA_NO_MEMORY (-3)
|
||||
#define RWMA_XMA_RAW 1
|
||||
#define RWMA_PRO 2
|
||||
#define RWMA_V1 3
|
||||
#define RWMA_V2 4
|
||||
typedef struct rwma_t rwma_t;
|
||||
typedef struct rwma_config
|
||||
{
|
||||
const uint8_t *extra;
|
||||
size_t extra_size;
|
||||
unsigned codec, sample_rate, channels, block_align, bit_rate;
|
||||
} rwma_config;
|
||||
typedef struct rwma_frame
|
||||
{
|
||||
const float *planes[8];
|
||||
unsigned samples, channels, sample_rate, trim_start, trim_end;
|
||||
} rwma_frame;
|
||||
/* Allocation only at creation. No file I/O, threads, resampling or pacing.
|
||||
* Borrowed PCM survives until the next decode, reset or free on this context.
|
||||
* One context has one owner; independent contexts can decode concurrently. */
|
||||
int rwma_create(rwma_t **out, const rwma_config *config);
|
||||
void rwma_free(rwma_t *decoder);
|
||||
void rwma_reset(rwma_t *decoder);
|
||||
/* Complete raw frame, including its length prefix, with an arbitrary bit
|
||||
* offset. No padding, alignment or encoded-frame copy is required. Trims
|
||||
* are reported separately, never applied silently. An error invalidates
|
||||
* history: reset before decoding again. A view is cleared on failure. */
|
||||
int rwma_decode_xma(rwma_t *decoder, const uint8_t *data, size_t bytes,
|
||||
size_t bit_offset, size_t bit_count, rwma_frame *frame);
|
||||
/* WMA packet window: advance by consumed bytes and call again. If consumed
|
||||
* is zero, lend the same input again to obtain the next frame. Input need
|
||||
* not survive a positive consumption; decoded output has its own lifetime.
|
||||
* A zero-byte input drains overlap once at EOF. Containers/demuxing remain
|
||||
* caller-owned. WMA v1/v2 return priming delay in trim_start as metadata;
|
||||
* WMA Pro applies stream trims and suppresses its initial priming frame. */
|
||||
int rwma_decode_packet(rwma_t *decoder, const uint8_t *data, size_t bytes,
|
||||
size_t *consumed, rwma_frame *frame);
|
||||
RETRO_END_DECLS
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
include ../../sample_objdir.mk
|
||||
TARGET := rwma_test
|
||||
LIBRETRO_COMM_DIR := ../../..
|
||||
SOURCES := rwma_test.c $(LIBRETRO_COMM_DIR)/formats/wma/rwma_pro.c $(LIBRETRO_COMM_DIR)/formats/wma/rwma_standard.c
|
||||
OBJS := $(SAMPLE_OBJDIR)/rwma_test.o $(call sample_objs,$(filter-out rwma_test.o,$(SOURCES:.c=.o)))
|
||||
CFLAGS += -std=c89 -pedantic -Wall -Wextra -Werror -Wno-unused-function -O2 -I$(LIBRETRO_COMM_DIR)/include $(EXTRA_CFLAGS)
|
||||
ifneq ($(SANITIZER),)
|
||||
CFLAGS += -fsanitize=$(SANITIZER) -fno-omit-frame-pointer
|
||||
LDFLAGS += -fsanitize=$(SANITIZER)
|
||||
endif
|
||||
all: $(TARGET)
|
||||
$(OBJS): CFLAGS += -Dcalloc=rwma_test_calloc -Dfree=rwma_test_free
|
||||
$(SAMPLE_OBJDIR)/rwma_test.o: CFLAGS := $(filter-out -Dcalloc=rwma_test_calloc -Dfree=rwma_test_free,$(CFLAGS))
|
||||
$(SAMPLE_OBJDIR)/%.o: $$(call sample_src,$$*).c
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) -o $@ $^ $(LDFLAGS) -lm
|
||||
reference: rwma_reference
|
||||
rwma_reference: rwma_reference.c $(LIBRETRO_COMM_DIR)/formats/wma/rwma_pro.c $(LIBRETRO_COMM_DIR)/formats/wma/rwma_standard.c
|
||||
@mkdir -p $(SAMPLE_OBJDIR)/reference
|
||||
$(CC) $(CFLAGS) -c $(LIBRETRO_COMM_DIR)/formats/wma/rwma_pro.c -o $(SAMPLE_OBJDIR)/reference/pro.o
|
||||
$(CC) $(CFLAGS) -c $(LIBRETRO_COMM_DIR)/formats/wma/rwma_standard.c -o $(SAMPLE_OBJDIR)/reference/standard.o
|
||||
$(CC) -std=c99 -Wall -Wextra -Werror -O2 -I$(LIBRETRO_COMM_DIR)/include $$(pkg-config --cflags libavcodec libavformat libavutil) -c rwma_reference.c -o $(SAMPLE_OBJDIR)/reference/oracle.o
|
||||
$(CC) -o $@ $(SAMPLE_OBJDIR)/reference/*.o $(LDFLAGS) $$(pkg-config --libs libavcodec libavformat libavutil) -lm
|
||||
clean:
|
||||
rm -rf $(SAMPLE_OBJDIR)
|
||||
rm -f $(TARGET) $(OBJS)
|
||||
.PHONY: all clean reference
|
||||
@@ -0,0 +1,183 @@
|
||||
/* Copyright (C) 2010-2026 The RetroArch team
|
||||
*
|
||||
* ---------------------------------------------------------------------------------------
|
||||
* The following license statement only applies to this file (rwma_reference.c).
|
||||
* ---------------------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge,
|
||||
* to any person obtaining a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction, including without
|
||||
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies
|
||||
* or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "libavcodec/avcodec.h"
|
||||
#include "libavformat/avformat.h"
|
||||
#include <float.h>
|
||||
#include <formats/rwma.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
static float reference[8][262144];
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
rwma_t *d = NULL;
|
||||
rwma_config cfg;
|
||||
rwma_frame view;
|
||||
AVFormatContext *format = NULL;
|
||||
AVCodecContext *c;
|
||||
const AVCodec *codec;
|
||||
AVFrame *f = av_frame_alloc();
|
||||
AVPacket *pkt = av_packet_alloc();
|
||||
unsigned frames = 0, samples = 0, i, ch, packets = 0;
|
||||
double max_error = 0, squared = 0, energy = 0;
|
||||
int stream, ret;
|
||||
if (argc != 2 || avformat_open_input(&format, argv[1], NULL, NULL) < 0)
|
||||
return 1;
|
||||
if (avformat_find_stream_info(format, NULL) < 0)
|
||||
return 2;
|
||||
stream = av_find_best_stream(format, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
|
||||
if (stream < 0)
|
||||
return 3;
|
||||
codec = avcodec_find_decoder(format->streams[stream]->codecpar->codec_id);
|
||||
if (!codec)
|
||||
return 4;
|
||||
c = avcodec_alloc_context3(codec);
|
||||
if (avcodec_parameters_to_context(c, format->streams[stream]->codecpar) < 0)
|
||||
return 5;
|
||||
c->flags |= AV_CODEC_FLAG_BITEXACT;
|
||||
c->flags2 |= AV_CODEC_FLAG2_SKIP_MANUAL;
|
||||
if (avcodec_open2(c, codec, NULL) < 0)
|
||||
return 6;
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.codec = c->codec_id == AV_CODEC_ID_WMAPRO ? RWMA_PRO
|
||||
: c->codec_id == AV_CODEC_ID_WMAV1 ? RWMA_V1
|
||||
: RWMA_V2;
|
||||
cfg.channels = c->ch_layout.nb_channels;
|
||||
cfg.sample_rate = c->sample_rate;
|
||||
cfg.block_align = c->block_align;
|
||||
cfg.bit_rate = (unsigned)c->bit_rate;
|
||||
cfg.extra = c->extradata;
|
||||
cfg.extra_size = c->extradata_size;
|
||||
printf("codec %u channels %u rate %u align %u bitrate %u extra %u\n", cfg.codec,
|
||||
cfg.channels, cfg.sample_rate, cfg.block_align, cfg.bit_rate,
|
||||
(unsigned)cfg.extra_size);
|
||||
ret = rwma_create(&d, &cfg);
|
||||
if (ret)
|
||||
{
|
||||
fprintf(stderr, "create %d\n", ret);
|
||||
return 7;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
const uint8_t *data;
|
||||
size_t left;
|
||||
unsigned out_at = 0, guard = 0, reference_count = 0;
|
||||
ret = av_read_frame(format, pkt);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (ret != AVERROR_EOF)
|
||||
return 17;
|
||||
break;
|
||||
}
|
||||
if (pkt->stream_index != stream)
|
||||
{
|
||||
av_packet_unref(pkt);
|
||||
continue;
|
||||
}
|
||||
packets++;
|
||||
ret = avcodec_send_packet(c, pkt);
|
||||
if (ret < 0)
|
||||
return 8;
|
||||
while ((ret = avcodec_receive_frame(c, f)) >= 0)
|
||||
{
|
||||
if (reference_count + (unsigned)f->nb_samples > 262144)
|
||||
return 21;
|
||||
for (ch = 0; ch < cfg.channels; ch++)
|
||||
memcpy(reference[ch] + reference_count, f->extended_data[ch],
|
||||
f->nb_samples * sizeof(float));
|
||||
reference_count += f->nb_samples;
|
||||
av_frame_unref(f);
|
||||
}
|
||||
if (ret < 0 && ret != AVERROR(EAGAIN))
|
||||
{
|
||||
fprintf(stderr, "ref %d\n", ret);
|
||||
return 9;
|
||||
}
|
||||
data = pkt->data;
|
||||
left = pkt->size;
|
||||
while (left)
|
||||
{
|
||||
size_t used = 0;
|
||||
int r = rwma_decode_packet(d, data, left, &used, &view);
|
||||
if (r || used > left || ++guard > 100)
|
||||
{
|
||||
fprintf(stderr, "packet %u native %d left %u guard %u\n", packets, r,
|
||||
(unsigned)left, guard);
|
||||
return 10;
|
||||
}
|
||||
if (!reference_count && view.samples)
|
||||
{
|
||||
fprintf(stderr, "unexpected native samples\n");
|
||||
return 11;
|
||||
}
|
||||
if (view.samples && out_at + view.samples > reference_count)
|
||||
{
|
||||
fprintf(stderr, "too many native samples\n");
|
||||
return 12;
|
||||
}
|
||||
for (ch = 0; ch < view.channels; ch++)
|
||||
for (i = 0; i < view.samples; i++)
|
||||
{
|
||||
double a = view.planes[ch][i], b = reference[ch][out_at + i],
|
||||
e = fabs(a - b);
|
||||
if (!(a <= FLT_MAX && a >= -FLT_MAX))
|
||||
return 13;
|
||||
if (e > max_error)
|
||||
max_error = e;
|
||||
energy += b * b;
|
||||
squared += (a - b) * (a - b);
|
||||
samples++;
|
||||
if (e > 0.00003 + fabs(b) * 0.0001)
|
||||
{
|
||||
fprintf(stderr, "packet %u ch%u sample%u native %.9g ref %.9g\n",
|
||||
packets, ch, out_at + i, a, b);
|
||||
return 14;
|
||||
}
|
||||
}
|
||||
out_at += view.samples;
|
||||
if (view.samples)
|
||||
frames++;
|
||||
data += used;
|
||||
left -= used;
|
||||
}
|
||||
if (out_at != reference_count)
|
||||
{
|
||||
fprintf(stderr, "sample count %u vs %d\n", out_at, (int)reference_count);
|
||||
return 15;
|
||||
}
|
||||
av_packet_unref(pkt);
|
||||
av_frame_unref(f);
|
||||
}
|
||||
printf("PASS %u packets %u frames %u samples max error %.9g relative RMS %.9g\n",
|
||||
packets, frames, samples, max_error, sqrt(squared / energy));
|
||||
rwma_free(d);
|
||||
avcodec_free_context(&c);
|
||||
av_frame_free(&f);
|
||||
av_packet_free(&pkt);
|
||||
avformat_close_input(&format);
|
||||
return frames ? 0 : 16;
|
||||
}
|
||||
@@ -0,0 +1,460 @@
|
||||
/* Copyright (C) 2010-2026 The RetroArch team
|
||||
*
|
||||
* ---------------------------------------------------------------------------------------
|
||||
* The following license statement only applies to this file (rwma_test.c).
|
||||
* ---------------------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge,
|
||||
* to any person obtaining a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction, including without
|
||||
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies
|
||||
* or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "../../../formats/wma/rwma_internal.h"
|
||||
#include "../../../formats/wma/rwma_mdct.h"
|
||||
#include "../../../formats/wma/rwma_pro_tables.h"
|
||||
#include <float.h>
|
||||
#include <formats/rwma.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define CHECK(x) \
|
||||
do \
|
||||
{ \
|
||||
if (!(x)) \
|
||||
{ \
|
||||
fprintf(stderr, "rwma line %u\n", (unsigned)__LINE__); \
|
||||
exit(1); \
|
||||
} \
|
||||
} while (0)
|
||||
static unsigned allocations, fail_allocation;
|
||||
static uint8_t encoded[4096];
|
||||
static float history[2][768], half_mdct[512];
|
||||
static float math_storage[8192 * 8], transform_input[8192], transform_output[8192];
|
||||
static rwma_mdct math_state;
|
||||
static unsigned bit_at;
|
||||
void *rwma_test_calloc(size_t n, size_t size)
|
||||
{
|
||||
allocations++;
|
||||
if (fail_allocation && allocations == fail_allocation)
|
||||
return NULL;
|
||||
return calloc(n, size);
|
||||
}
|
||||
void rwma_test_free(void *ptr) { free(ptr); }
|
||||
static void put(unsigned value, unsigned count)
|
||||
{
|
||||
unsigned i;
|
||||
CHECK(bit_at + count <= sizeof(encoded) * 8);
|
||||
for (i = count; i; i--)
|
||||
{
|
||||
if ((value >> (i - 1)) & 1)
|
||||
encoded[bit_at / 8] |= 1u << (7 - bit_at % 8);
|
||||
bit_at++;
|
||||
}
|
||||
}
|
||||
static void symbol(const rwma_vlc_node *nodes, unsigned count, int value)
|
||||
{
|
||||
unsigned i, node = 0, parent, path = 0, bits = 0;
|
||||
for (i = 0; i < count; i++)
|
||||
if (nodes[i].next[0] < 0 && nodes[i].next[1] < 0 && nodes[i].symbol == value)
|
||||
{
|
||||
node = i;
|
||||
break;
|
||||
}
|
||||
CHECK(i < count);
|
||||
while (node)
|
||||
{
|
||||
for (parent = 0; parent < node; parent++)
|
||||
if (nodes[parent].next[0] == (int)node || nodes[parent].next[1] == (int)node)
|
||||
break;
|
||||
CHECK(parent < node && bits < 31);
|
||||
path |= (nodes[parent].next[1] == (int)node) << bits++;
|
||||
node = parent;
|
||||
}
|
||||
put(path, bits);
|
||||
}
|
||||
static unsigned xma_frame(unsigned channels, unsigned offset, unsigned frequency,
|
||||
int negative, int coded)
|
||||
{
|
||||
unsigned c, x, band = 1, previous = 0, start, length;
|
||||
memset(encoded, 0, sizeof(encoded));
|
||||
bit_at = offset;
|
||||
put(0, 15);
|
||||
put(1, 1);
|
||||
put(0, 1);
|
||||
if (channels == 2)
|
||||
put(0, 1);
|
||||
put(0, 8);
|
||||
put(0, 1);
|
||||
put(0, 1);
|
||||
put(0, 1);
|
||||
if (channels == 2)
|
||||
{
|
||||
put(0, 1);
|
||||
put(2, 2);
|
||||
}
|
||||
for (c = 0; c < channels; c++)
|
||||
put(coded != 0, 1);
|
||||
if (coded)
|
||||
{
|
||||
put(1, 1);
|
||||
for (c = 0; c < channels; c++)
|
||||
put(0, 8);
|
||||
put(0, 6);
|
||||
if (channels == 2)
|
||||
{
|
||||
put(0, 3);
|
||||
put(0, 1);
|
||||
put(0, 1);
|
||||
}
|
||||
for (x = 0; x < 28 && previous < 512; x++)
|
||||
{
|
||||
unsigned next = ((1024 * critical_freq[x]) / 48000 + 2) & ~3u;
|
||||
if (next > previous)
|
||||
{
|
||||
band++;
|
||||
previous = next;
|
||||
}
|
||||
if (next >= 512)
|
||||
break;
|
||||
}
|
||||
for (c = 0; c < channels; c++)
|
||||
{
|
||||
put(0, 2);
|
||||
for (x = 0; x < band - 1; x++)
|
||||
symbol(sf_vlc, RWMA_ARRAY_SIZE(sf_vlc), 0);
|
||||
}
|
||||
for (c = 0; c < channels; c++)
|
||||
{
|
||||
put(0, 1);
|
||||
symbol(coef_vlc0, RWMA_ARRAY_SIZE(coef_vlc0), 0);
|
||||
put(0, 1);
|
||||
put(1, 8);
|
||||
if (frequency == 0)
|
||||
put(0, 1);
|
||||
else if (frequency <= 4)
|
||||
{
|
||||
put(1, 1);
|
||||
put(0, 1);
|
||||
put(frequency - 1, 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
put(1, 1);
|
||||
put(1, 1);
|
||||
put(0, 1);
|
||||
put(frequency - 4, 9);
|
||||
}
|
||||
put(negative ? 0 : 1, 1);
|
||||
if (frequency < 511)
|
||||
symbol(coef_vlc0, RWMA_ARRAY_SIZE(coef_vlc0), 1);
|
||||
}
|
||||
}
|
||||
put(0, 2);
|
||||
length = bit_at - offset;
|
||||
start = bit_at;
|
||||
bit_at = offset;
|
||||
put(length, 15);
|
||||
bit_at = start;
|
||||
return length;
|
||||
}
|
||||
static void reference(unsigned channels, unsigned frequency, int negative, int coded)
|
||||
{
|
||||
unsigned c, i;
|
||||
double scale = 1.0 / 256 / 32768;
|
||||
double gain = (float)pow(10.0, 4.5);
|
||||
for (i = 0; i < 512; i++)
|
||||
half_mdct[i] =
|
||||
coded ? (float)((negative ? -gain : gain) * scale *
|
||||
cos(RWMA_PI / 512 * (511 - i + 0.5) * (frequency + 0.5)))
|
||||
: 0;
|
||||
for (c = 0; c < channels; c++)
|
||||
{
|
||||
memcpy(history[c] + 256, half_mdct, sizeof(half_mdct));
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
float a = history[c][i], b = history[c][511 - i];
|
||||
float wi = (float)sin((i + 0.5) * RWMA_PI / 1024);
|
||||
float wj = (float)sin((511 - i + 0.5) * RWMA_PI / 1024);
|
||||
history[c][i] = a * wj - b * wi;
|
||||
history[c][511 - i] = a * wi + b * wj;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void xma_tests(void)
|
||||
{
|
||||
rwma_config cfg;
|
||||
rwma_t *d;
|
||||
rwma_frame frame;
|
||||
unsigned channels, pass, count = 0, i, c, before;
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.codec = RWMA_XMA_RAW;
|
||||
cfg.sample_rate = 48000;
|
||||
for (channels = 1; channels <= 2; channels++)
|
||||
{
|
||||
cfg.channels = channels;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_OK);
|
||||
memset(history, 0, sizeof(history));
|
||||
before = allocations;
|
||||
for (pass = 0; pass < 128; pass++)
|
||||
{
|
||||
unsigned offset = pass % 8, frequency = (pass * 37) % 512;
|
||||
unsigned length =
|
||||
xma_frame(channels, offset, frequency, pass & 1, pass % 3 != 0);
|
||||
CHECK(rwma_decode_xma(d, encoded, (bit_at + 7) / 8, offset, length, &frame) ==
|
||||
RWMA_OK);
|
||||
CHECK(frame.samples == 512 && frame.channels == channels);
|
||||
reference(channels, frequency, pass & 1, pass % 3 != 0);
|
||||
for (c = 0; c < channels; c++)
|
||||
{
|
||||
for (i = 0; i < 512; i++)
|
||||
{
|
||||
double expected = history[c][i];
|
||||
#ifdef RWMA_TEST_BAD_POLARITY
|
||||
expected = -expected;
|
||||
#endif
|
||||
CHECK(fabs(frame.planes[c][i] - expected) < 0.00000003);
|
||||
count++;
|
||||
}
|
||||
memmove(history[c], history[c] + 512, 256 * sizeof(float));
|
||||
}
|
||||
}
|
||||
CHECK(allocations == before);
|
||||
rwma_reset(d);
|
||||
CHECK(allocations == before);
|
||||
rwma_free(d);
|
||||
}
|
||||
printf("PASS XMA: %u analytical PCM comparisons, arbitrary bit offsets, mono/stereo "
|
||||
"and reused overlap\n",
|
||||
count);
|
||||
}
|
||||
static void transform_tests(void)
|
||||
{
|
||||
unsigned n, k, j;
|
||||
rwma_mdct_init(&math_state, math_storage, 8192);
|
||||
for (n = 64; n <= 8192; n *= 2)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
transform_input[j] = (float)sin(j * 0.13);
|
||||
rwma_mdct_run(&math_state, transform_output, transform_input, n, 0.01f);
|
||||
for (k = 0; k < n; k++)
|
||||
{
|
||||
double value = 0;
|
||||
for (j = 0; j < n; j++)
|
||||
value += transform_input[j] * cos(RWMA_PI / n * (n - k - 0.5) * (j + 0.5));
|
||||
CHECK(fabs(transform_output[k] - value * 0.01) < 0.00001);
|
||||
}
|
||||
}
|
||||
puts("PASS radix-2 IMDCT against independent cosine sums");
|
||||
}
|
||||
static void failure_tests(void)
|
||||
{
|
||||
rwma_config cfg;
|
||||
rwma_t *d = NULL;
|
||||
rwma_frame frame;
|
||||
unsigned i, j, channels, before, seed = 7;
|
||||
uint8_t extra[18] = {0};
|
||||
size_t used;
|
||||
int ret;
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.codec = RWMA_XMA_RAW;
|
||||
cfg.sample_rate = 48000;
|
||||
cfg.channels = 2;
|
||||
before = allocations;
|
||||
fail_allocation = before + 1;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_NO_MEMORY && !d);
|
||||
fail_allocation = allocations + 2;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_NO_MEMORY && !d);
|
||||
fail_allocation = 0;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_OK);
|
||||
before = allocations;
|
||||
for (i = 0; i < 4000; i++)
|
||||
{
|
||||
unsigned bytes = 8 + i % 248, bits = bytes * 8;
|
||||
for (j = 0; j < bytes; j++)
|
||||
{
|
||||
seed = seed * 1664525u + 1013904223u;
|
||||
encoded[j] = (uint8_t)(seed >> 24);
|
||||
}
|
||||
encoded[0] = (uint8_t)(bits >> 7);
|
||||
encoded[1] = (uint8_t)((encoded[1] & 1) | ((bits & 127) << 1));
|
||||
rwma_reset(d);
|
||||
ret = rwma_decode_xma(d, encoded, bytes, 0, bits, &frame);
|
||||
if (ret)
|
||||
CHECK(frame.samples == 0 && !frame.planes[0]);
|
||||
else
|
||||
for (channels = 0; channels < frame.channels; channels++)
|
||||
for (j = 0; j < frame.samples; j++)
|
||||
CHECK(frame.planes[channels][j] <= FLT_MAX &&
|
||||
frame.planes[channels][j] >= -FLT_MAX);
|
||||
}
|
||||
CHECK(allocations == before);
|
||||
rwma_free(d);
|
||||
cfg.extra = extra;
|
||||
cfg.extra_size = sizeof(extra);
|
||||
cfg.block_align = 64;
|
||||
cfg.bit_rate = 128000;
|
||||
for (cfg.codec = RWMA_PRO; cfg.codec <= RWMA_V2; cfg.codec++)
|
||||
{
|
||||
memset(extra, 0, sizeof(extra));
|
||||
extra[0] = 16;
|
||||
extra[2] = 3;
|
||||
extra[14] = 0xe0;
|
||||
extra[4] = 1;
|
||||
if (cfg.codec == RWMA_PRO)
|
||||
extra[4] = 0;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_OK);
|
||||
before = allocations;
|
||||
for (i = 0; i < 2000; i++)
|
||||
{
|
||||
for (j = 0; j < 64; j++)
|
||||
{
|
||||
seed = seed * 1664525u + 1013904223u;
|
||||
encoded[j] = (uint8_t)(seed >> 24);
|
||||
}
|
||||
rwma_reset(d);
|
||||
ret = rwma_decode_packet(d, encoded, 64, &used, &frame);
|
||||
if (ret)
|
||||
CHECK(frame.samples == 0 && !frame.planes[0]);
|
||||
else
|
||||
CHECK(used <= 64);
|
||||
}
|
||||
CHECK(allocations == before);
|
||||
rwma_free(d);
|
||||
}
|
||||
puts("PASS allocation failure, malformed frames and no decode/reset allocations");
|
||||
}
|
||||
static void packet_tests(void)
|
||||
{
|
||||
rwma_config cfg;
|
||||
rwma_t *d;
|
||||
rwma_frame frame;
|
||||
uint8_t extra[10] = {0};
|
||||
unsigned packet, calls, frames = 0, i, c, before;
|
||||
size_t consumed;
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.codec = RWMA_V2;
|
||||
cfg.channels = 2;
|
||||
cfg.sample_rate = 48000;
|
||||
cfg.bit_rate = 128000;
|
||||
cfg.block_align = 64;
|
||||
cfg.extra = extra;
|
||||
cfg.extra_size = sizeof(extra);
|
||||
extra[4] = 3;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_OK);
|
||||
CHECK(rwma_decode_packet(d, NULL, 0, &consumed, &frame) == RWMA_OK
|
||||
&& !frame.samples && !consumed);
|
||||
before = allocations;
|
||||
for (packet = 0; packet < 4; packet++)
|
||||
{
|
||||
memset(encoded, 0, sizeof(encoded));
|
||||
bit_at = 0;
|
||||
put(packet, 4);
|
||||
put(packet ? 2 : 3, 4);
|
||||
put(0, 13);
|
||||
for (calls = 0; calls < 8; calls++)
|
||||
{
|
||||
CHECK(rwma_decode_packet(d, encoded, 64, &consumed, &frame) == RWMA_OK);
|
||||
CHECK(frame.samples == 2048 && frame.channels == 2);
|
||||
for (c = 0; c < 2; c++)
|
||||
for (i = 0; i < frame.samples; i++)
|
||||
CHECK(frame.planes[c][i] == 0);
|
||||
frames++;
|
||||
if (consumed)
|
||||
{
|
||||
CHECK(consumed == 64);
|
||||
break;
|
||||
}
|
||||
}
|
||||
CHECK(calls == 1);
|
||||
}
|
||||
CHECK(frames == 8);
|
||||
CHECK(rwma_decode_packet(d, NULL, 0, &consumed, &frame) == RWMA_OK &&
|
||||
frame.samples == 2048);
|
||||
CHECK(rwma_decode_packet(d, NULL, 0, &consumed, &frame) == RWMA_OK &&
|
||||
!frame.samples);
|
||||
CHECK(allocations == before);
|
||||
rwma_free(d);
|
||||
puts("PASS WMA packet reservoir, borrowed per-frame output and one-shot EOF drain");
|
||||
}
|
||||
static void pro_tests(void)
|
||||
{
|
||||
rwma_config cfg;
|
||||
rwma_t *d;
|
||||
rwma_frame frame;
|
||||
uint8_t extra[18] = {0};
|
||||
unsigned pass, c, i, frames = 0;
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.codec = RWMA_PRO;
|
||||
cfg.channels = 2;
|
||||
cfg.sample_rate = 48000;
|
||||
cfg.block_align = 2048;
|
||||
cfg.extra = extra;
|
||||
cfg.extra_size = sizeof(extra);
|
||||
extra[0] = 16;
|
||||
extra[2] = 3;
|
||||
extra[14] = 0xd6;
|
||||
extra[15] = 0x10;
|
||||
CHECK(rwma_create(&d, &cfg) == RWMA_OK);
|
||||
{
|
||||
size_t used;
|
||||
CHECK(rwma_decode_packet(d, NULL, 0, &used, &frame) == RWMA_OK
|
||||
&& !frame.samples && !used);
|
||||
}
|
||||
memset(history, 0, sizeof(history));
|
||||
for (pass = 0; pass < 32; pass++)
|
||||
{
|
||||
const uint8_t *data = encoded;
|
||||
size_t left = 2048, used;
|
||||
unsigned guard = 0, frequency = pass * 13;
|
||||
xma_frame(2, 21, frequency, pass & 1, 1);
|
||||
bit_at = 0;
|
||||
put(pass & 15, 4);
|
||||
put(0, 2);
|
||||
put(0, 15);
|
||||
reference(2, frequency, pass & 1, 1);
|
||||
while (left)
|
||||
{
|
||||
CHECK(rwma_decode_packet(d, data, left, &used, &frame) == RWMA_OK);
|
||||
CHECK(used <= left && ++guard < 8);
|
||||
if (frame.samples)
|
||||
{
|
||||
CHECK(pass != 0 && frame.samples == 512 && frame.channels == 2);
|
||||
for (c = 0; c < 2; c++)
|
||||
for (i = 0; i < 512; i++)
|
||||
CHECK(fabs(frame.planes[c][i] - history[c][i]) < 0.00000003);
|
||||
frames++;
|
||||
}
|
||||
data += used;
|
||||
left -= used;
|
||||
}
|
||||
for (c = 0; c < 2; c++)
|
||||
memmove(history[c], history[c] + 512, 256 * sizeof(float));
|
||||
}
|
||||
CHECK(frames == 31);
|
||||
rwma_free(d);
|
||||
puts("PASS WMA Pro nonzero spectral packets, priming and overlap continuity");
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
transform_tests();
|
||||
xma_tests();
|
||||
failure_tests();
|
||||
packet_tests();
|
||||
pro_tests();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user