forked from theluyuan/avs2bdnxml
initial commit of avs2bdnxml 2.08.
This commit is contained in:
commit
879b32e9e0
674
COPYING
Normal file
674
COPYING
Normal file
@ -0,0 +1,674 @@
|
|||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
|
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.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
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
|
||||||
|
<http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
||||||
|
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
26
Makefile
Normal file
26
Makefile
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
CC=i586-mingw32msvc-gcc
|
||||||
|
CFLAGS=-O3 -Iinc/ -Wall -DLE_ARCH
|
||||||
|
LDFLAGS=-lpng -lz -lvfw32 -Llib/ -liberty
|
||||||
|
OBJS=avs2bdnxml.o auto_split.o palletize.o sup.o sort.o
|
||||||
|
ASMOBJS=frame-a.o
|
||||||
|
EXE=avs2bdnxml.exe
|
||||||
|
|
||||||
|
%.o: %.c %.h Makefile
|
||||||
|
$(CC) -c $< $(CFLAGS)
|
||||||
|
|
||||||
|
$(EXE): $(OBJS) $(ASMOBJS)
|
||||||
|
$(CC) -o $(EXE) $(OBJS) $(ASMOBJS) $(LDFLAGS)
|
||||||
|
|
||||||
|
all: $(EXE)
|
||||||
|
|
||||||
|
$(ASMOBJS): $(ASMOBJS:%.o=%.asm)
|
||||||
|
yasm -f win32 -m x86 -DARCH_X86_64=0 -DPREFIX=1 $< -o $(<:%.asm=%.o)
|
||||||
|
|
||||||
|
dist: clean all
|
||||||
|
strip -s $(EXE)
|
||||||
|
upx-ucl --best $(EXE)
|
||||||
|
rm -f $(OBJS) $(ASMOBJS)
|
||||||
|
|
||||||
|
.phony clean:
|
||||||
|
rm -f $(EXE) $(OBJS) $(ASMOBJS)
|
||||||
|
|
22
Makefile.linux
Normal file
22
Makefile.linux
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
CC=gcc
|
||||||
|
CFLAGS=-DLINUX -O3 -Wall -DLE_ARCH
|
||||||
|
LDFLAGS=-lpng -lz
|
||||||
|
OBJS=avs2bdnxml.o auto_split.o palletize.o sup.o sort.o
|
||||||
|
EXE=avs2bdnxml
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) -c $< $(CFLAGS)
|
||||||
|
|
||||||
|
$(EXE): $(OBJS)
|
||||||
|
$(CC) -o $(EXE) $(OBJS) $(LDFLAGS)
|
||||||
|
|
||||||
|
all: $(EXE)
|
||||||
|
|
||||||
|
dist: clean all
|
||||||
|
strip -s $(EXE)
|
||||||
|
upx-ucl --best $(EXE)
|
||||||
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
.phony clean:
|
||||||
|
rm -f $(EXE) $(OBJS)
|
||||||
|
|
67
README
Normal file
67
README
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
AVS to BluRay SUP/PGS and BDN XML
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
This program can be used to transform AviSynth scripts, which produce RGBA
|
||||||
|
output, to BDN XML+PNG format. This in turn can be transformed into a SUP file,
|
||||||
|
which can be used to master a BluRay disc with subtitles.
|
||||||
|
|
||||||
|
Usage instructions:
|
||||||
|
|
||||||
|
0. If you want to build it:
|
||||||
|
i586-mingw32msvc-gcc avs2bdnxml.c -o avs2bdnxml.exe -lpng -lz -lvfw32 \
|
||||||
|
-Llib/ -O3 -Iinc/
|
||||||
|
|
||||||
|
1. Prepare subtitles. You can either produce subtitles in a normal format like
|
||||||
|
SRT or ASS/SSA, or produce an RGBA video beforehand.
|
||||||
|
|
||||||
|
2. Create an AviSynth script. If you made a regular subtitle file, you can use
|
||||||
|
something like this:
|
||||||
|
|
||||||
|
video=AviSource("video.avi")
|
||||||
|
# This requires at least VSFilter 2.39
|
||||||
|
MaskSub("subtitles.ext",video.width,video.height,
|
||||||
|
video.framerate,video.framecount)
|
||||||
|
|
||||||
|
If you created an RGBA video, do something like this instead:
|
||||||
|
|
||||||
|
AviSource("subtitles_RGBA.avi")
|
||||||
|
FlipVertical()
|
||||||
|
|
||||||
|
3. Run the program:
|
||||||
|
avs2bdnxml input.avs Undefined und 1080p 23.976 output.xml
|
||||||
|
|
||||||
|
4. For some programs, you have to convert the PNG files to 8bit RGBA palette.
|
||||||
|
This doesn't apply to BDSupEdit. You can do it with a tool like pngquant or
|
||||||
|
pngnq.
|
||||||
|
|
||||||
|
pngquant: http://www.libpng.org/pub/png/apps/pngquant.html
|
||||||
|
pngnq: http://pngnq.sourceforge.net/
|
||||||
|
|
||||||
|
5. You get a BDN XML file in the following format:
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<BDN Version="0.93" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="BD-03-006-0093b BDN File Format.xsd">
|
||||||
|
<Description>
|
||||||
|
<Name Title="Undefined" Content=""/>
|
||||||
|
<Language Code="und"/>
|
||||||
|
<Format VideoFormat="[ 480i / 480p / 576i / 720p / 1080i /1080p ]" FrameRate="[ 23.976 / 24 / 25 / 29.97 / 50 / 59.94 ]" DropFrame="false"/>
|
||||||
|
<Events LastEventOutTC="00:00:00:00" FirstEventInTC="00:00:00:00" ContentInTC="00:00:00:00"
|
||||||
|
ContentOutTC="00:00:00:00" NumberofEvents="[ number of encoded frames ]" Type="Graphic"/>
|
||||||
|
</Description>
|
||||||
|
<Events>
|
||||||
|
<Event Forced="[ False / True ]" InTC="00:00:00:00" OutTC="00:00:00:00">
|
||||||
|
<Graphic Width="0" Height="0" X="0" Y="0">000000.png</Graphic>
|
||||||
|
</Event>
|
||||||
|
</Events>
|
||||||
|
</BDN>
|
||||||
|
|
||||||
|
6. Use a program like BDSupEdit or BDSup2Sub to convert the BDN XML
|
||||||
|
to a BD-SUP file. The rest is left as an exercise for the reader.
|
||||||
|
|
||||||
|
BDSupEdit: http://forum.doom9.org/showthread.php?t=146157
|
||||||
|
BDSup2Sub: http://forum.doom9.org/showthread.php?t=145277
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- http://ps-auxw.de/avs2bdnxml/
|
200
abstract_lists.h
Normal file
200
abstract_lists.h
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* abstract_lists.h - Simple, typesafe, doubly linked lists for C
|
||||||
|
* Copyright (C) 2010 Arne Bochem <abstract.lists at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef ABSTRACT_LISTS_H
|
||||||
|
#define ABSTRACT_LISTS_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define DECLARE_LIST_BACKEND(kind, prefix, type) \
|
||||||
|
typedef struct prefix##_list_s prefix##_list_t;\
|
||||||
|
typedef struct prefix##_list_node_s prefix##_list_node_t;\
|
||||||
|
struct prefix##_list_s\
|
||||||
|
{\
|
||||||
|
prefix##_list_node_t *current, *first, *last;\
|
||||||
|
};\
|
||||||
|
struct prefix##_list_node_s\
|
||||||
|
{\
|
||||||
|
type *v;\
|
||||||
|
prefix##_list_node_t *prev, *next;\
|
||||||
|
};\
|
||||||
|
kind prefix##_list_t *prefix##_list_new ();\
|
||||||
|
kind type *prefix##_list_get (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind type *prefix##_list_next (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind type *prefix##_list_prev (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind type *prefix##_list_first (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind type *prefix##_list_last (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind int prefix##_list_empty (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_set (prefix##_list_t *l, type *v) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_insert (prefix##_list_t *l, type *v) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_insert_after (prefix##_list_t *l, type *v) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_delete (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_remove (prefix##_list_t *l, type *v) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_destroy (prefix##_list_t *l) __attribute__ ((unused));\
|
||||||
|
kind void prefix##_list_destroy_deep (prefix##_list_t *l) __attribute__ ((unused));
|
||||||
|
|
||||||
|
#define IMPLEMENT_LIST_BACKEND(kind, prefix, type) \
|
||||||
|
kind prefix##_list_t *prefix##_list_new ()\
|
||||||
|
{\
|
||||||
|
prefix##_list_t *l = malloc(sizeof(prefix##_list_t));\
|
||||||
|
l->first = NULL;\
|
||||||
|
l->last = NULL;\
|
||||||
|
l->current = NULL;\
|
||||||
|
return l;\
|
||||||
|
}\
|
||||||
|
kind type *prefix##_list_get (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
if (l->current == NULL)\
|
||||||
|
return NULL;\
|
||||||
|
return l->current->v;\
|
||||||
|
}\
|
||||||
|
kind type *prefix##_list_next (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
l->current = l->current->next;\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
return l->current->v;\
|
||||||
|
return NULL;\
|
||||||
|
}\
|
||||||
|
kind type *prefix##_list_prev (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
l->current = l->current->prev;\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
return l->current->v;\
|
||||||
|
return NULL;\
|
||||||
|
}\
|
||||||
|
kind type *prefix##_list_first (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
l->current = l->first;\
|
||||||
|
if (l->current == NULL)\
|
||||||
|
return NULL;\
|
||||||
|
return l->current->v;\
|
||||||
|
}\
|
||||||
|
kind type *prefix##_list_last (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
l->current = l->last;\
|
||||||
|
if (l->current == NULL)\
|
||||||
|
return NULL;\
|
||||||
|
return l->current->v;\
|
||||||
|
}\
|
||||||
|
kind int prefix##_list_empty (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
return (l->first == NULL && l->last == NULL);\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_set (prefix##_list_t *l, type *v)\
|
||||||
|
{\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
l->current->v = v;\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_insert (prefix##_list_t *l, type *v)\
|
||||||
|
{\
|
||||||
|
prefix##_list_node_t *new;\
|
||||||
|
new = calloc(1, sizeof(prefix##_list_node_t));\
|
||||||
|
new->v = v;\
|
||||||
|
new->next = l->current;\
|
||||||
|
new->prev = NULL;\
|
||||||
|
if (l->first == l->current)\
|
||||||
|
l->first = new;\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
{\
|
||||||
|
new->prev = l->current->prev;\
|
||||||
|
if (l->current->prev != NULL)\
|
||||||
|
l->current->prev->next = new;\
|
||||||
|
l->current->prev = new;\
|
||||||
|
}\
|
||||||
|
l->current = new;\
|
||||||
|
if (l->last == NULL)\
|
||||||
|
l->last = new;\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_insert_after (prefix##_list_t *l, type *v)\
|
||||||
|
{\
|
||||||
|
prefix##_list_node_t *new;\
|
||||||
|
new = calloc(1, sizeof(prefix##_list_node_t));\
|
||||||
|
new->v = v;\
|
||||||
|
new->next = NULL;\
|
||||||
|
new->prev = l->current;\
|
||||||
|
if (l->last == l->current)\
|
||||||
|
l->last = new;\
|
||||||
|
if (l->current != NULL)\
|
||||||
|
{\
|
||||||
|
new->next = l->current->next;\
|
||||||
|
if (l->current->next != NULL)\
|
||||||
|
l->current->next->prev = new;\
|
||||||
|
l->current->next = new;\
|
||||||
|
}\
|
||||||
|
l->current = new;\
|
||||||
|
if (l->first == NULL)\
|
||||||
|
l->first = new;\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_delete (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
prefix##_list_node_t *node = l->current;\
|
||||||
|
if (node == NULL)\
|
||||||
|
return;\
|
||||||
|
if (node->prev != NULL)\
|
||||||
|
node->prev->next = node->next;\
|
||||||
|
if (node->next != NULL)\
|
||||||
|
node->next->prev = node->prev;\
|
||||||
|
l->current = node->next;\
|
||||||
|
if (l->first == node)\
|
||||||
|
l->first = node->next;\
|
||||||
|
if (l->last == node)\
|
||||||
|
l->last = node->prev;\
|
||||||
|
free(node);\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_remove (prefix##_list_t *l, type *v)\
|
||||||
|
{\
|
||||||
|
type *c = prefix##_list_first(l);\
|
||||||
|
do\
|
||||||
|
{\
|
||||||
|
if (c == v)\
|
||||||
|
{\
|
||||||
|
prefix##_list_delete(l);\
|
||||||
|
l->current = l->first;\
|
||||||
|
return;\
|
||||||
|
}\
|
||||||
|
c = prefix##_list_next(l);\
|
||||||
|
}\
|
||||||
|
while (c != NULL);\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_destroy (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
prefix##_list_first(l);\
|
||||||
|
while (!prefix##_list_empty(l))\
|
||||||
|
prefix##_list_delete(l);\
|
||||||
|
free(l);\
|
||||||
|
}\
|
||||||
|
kind void prefix##_list_destroy_deep (prefix##_list_t *l)\
|
||||||
|
{\
|
||||||
|
prefix##_list_first(l);\
|
||||||
|
while (!prefix##_list_empty(l))\
|
||||||
|
{\
|
||||||
|
free(l->current->v);\
|
||||||
|
prefix##_list_delete(l);\
|
||||||
|
}\
|
||||||
|
free(l);\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DECLARE_LIST(prefix, type) DECLARE_LIST_BACKEND(;, prefix, type)
|
||||||
|
#define IMPLEMENT_LIST(prefix, type) IMPLEMENT_LIST_BACKEND(;, prefix, type)
|
||||||
|
#define DECLARE_STATIC_LIST(prefix, type) DECLARE_LIST_BACKEND(static, prefix, type)
|
||||||
|
#define IMPLEMENT_STATIC_LIST(prefix, type) IMPLEMENT_LIST_BACKEND(static, prefix, type)
|
||||||
|
#define STATIC_LIST(prefix, type) DECLARE_STATIC_LIST(prefix, type) IMPLEMENT_STATIC_LIST(prefix, type)
|
||||||
|
|
||||||
|
#endif
|
563
auto_split.c
Normal file
563
auto_split.c
Normal file
@ -0,0 +1,563 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* avs2bdnxml - Generates BluRay subtitle stuff from RGBA AviSynth scripts
|
||||||
|
* Copyright (C) 2008-2013 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include "auto_split.h"
|
||||||
|
#include "abstract_lists.h"
|
||||||
|
#include "sort.h"
|
||||||
|
|
||||||
|
/* Transparent pixels are assumed to be set to zero */
|
||||||
|
|
||||||
|
void auto_crop (pic_t p, crop_t *c)
|
||||||
|
{
|
||||||
|
uint32_t *b = (uint32_t *)p.b;
|
||||||
|
int min_x = INT_MAX, max_x = INT_MIN, min_y = INT_MAX, max_y = INT_MIN;
|
||||||
|
int seen_pixel = 0;
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
for (y = c->y; y < c->y + c->h && y < p.h; y++)
|
||||||
|
for (x = c->x; x < c->x + c->w && x < p.w; x++)
|
||||||
|
if (b[x + p.s * y])
|
||||||
|
{
|
||||||
|
seen_pixel = 1;
|
||||||
|
if (x < min_x)
|
||||||
|
min_x = x;
|
||||||
|
if (x > max_x)
|
||||||
|
max_x = x;
|
||||||
|
if (y < min_y)
|
||||||
|
min_y = y;
|
||||||
|
if (y > max_y)
|
||||||
|
max_y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!seen_pixel)
|
||||||
|
{
|
||||||
|
c->w = 0;
|
||||||
|
c->h = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
c->x = min_x;
|
||||||
|
c->y = min_y;
|
||||||
|
c->w = max_x - min_x + 1;
|
||||||
|
c->h = max_y - min_y + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Ensure no forbidden/tiny results are produced */
|
||||||
|
if (c->w < 8)
|
||||||
|
{
|
||||||
|
if (c->x + 8 > p.w)
|
||||||
|
c->x -= c->x + 8 - p.w;
|
||||||
|
c->w = 8;
|
||||||
|
}
|
||||||
|
if (c->h < 8)
|
||||||
|
{
|
||||||
|
if (c->y + 8 > p.h)
|
||||||
|
c->y -= c->y + 8 - p.h;
|
||||||
|
c->h = 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int block_state (pic_t p, crop_t c)
|
||||||
|
{
|
||||||
|
uint32_t *b = (uint32_t *)p.b;
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
for (y = c.y; y < c.y + c.h && y < p.h; y++)
|
||||||
|
for (x = c.x; x < c.x + c.w && x < p.w; x++)
|
||||||
|
if (b[x + p.s * y])
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GRID_BLOCKS 24 /* GCD of 480, 576, 720, 1080 */
|
||||||
|
|
||||||
|
static int line_ok (int grid[GRID_BLOCKS + 1][GRID_BLOCKS + 1], int x, int y, int w)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (grid[y][MAX(0, x - 1)] == -1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for (i = 0; i < w; i++)
|
||||||
|
if (grid[y][x + i] != -1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (grid[y][MIN(GRID_BLOCKS, x + i)] == -1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_line (int grid[GRID_BLOCKS + 1][GRID_BLOCKS + 1], int x, int y, int w, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < w; i++)
|
||||||
|
grid[y][x + i] = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static rect_t make_rect (int grid[GRID_BLOCKS + 1][GRID_BLOCKS + 1], int x, int y, int n)
|
||||||
|
{
|
||||||
|
rect_t r = {x, y, 1, 1};
|
||||||
|
int line_length = 1;
|
||||||
|
|
||||||
|
/* Get length of first rectangle line, and assign rect number */
|
||||||
|
grid[y][x] = n;
|
||||||
|
while (line_length + x < GRID_BLOCKS + 1 && grid[y][x + line_length] == -1)
|
||||||
|
{
|
||||||
|
grid[y][x + line_length] = n;
|
||||||
|
line_length++;
|
||||||
|
}
|
||||||
|
r.w = line_length;
|
||||||
|
|
||||||
|
/* Add lines while available */
|
||||||
|
while (line_ok(grid, x, r.y + r.h, r.w))
|
||||||
|
set_line(grid, x, r.y + r.h++, r.w, n);
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
rect_t merge_rects (rect_t r1, rect_t r2)
|
||||||
|
{
|
||||||
|
rect_t r;
|
||||||
|
|
||||||
|
/* Set rectangle that covers both input rectangles */
|
||||||
|
r.x = MIN(r1.x, r2.x);
|
||||||
|
r.y = MIN(r1.y, r2.y);
|
||||||
|
r.w = MAX(r1.x + r1.w, r2.x + r2.w) - r.x;
|
||||||
|
r.h = MAX(r1.y + r1.h, r2.y + r2.h) - r.y;
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int score_rect (rect_t r)
|
||||||
|
{
|
||||||
|
return r.w * r.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int check_close (rect_t r1, rect_t r2, int distance)
|
||||||
|
{
|
||||||
|
rect_t a, b;
|
||||||
|
|
||||||
|
if (r1.x <= r2.x)
|
||||||
|
{
|
||||||
|
a = r1;
|
||||||
|
b = r2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
a = r2;
|
||||||
|
b = r1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The rectangles are not too close, if the left border of of the right-most
|
||||||
|
* rectangle has a distance of at least distance to the right border of the
|
||||||
|
* left-most rectangle.
|
||||||
|
*/
|
||||||
|
if (b.x > a.x + a.w + distance)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (r1.y <= r2.y)
|
||||||
|
{
|
||||||
|
a = r1;
|
||||||
|
b = r2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
a = r2;
|
||||||
|
b = r1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Same as before, except vertically */
|
||||||
|
if (b.y > a.y + a.h + distance)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Too close! */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* crop_t *c - Array of length 2 */
|
||||||
|
int auto_split (pic_t p, crop_t *c, int ugly, int even_y)
|
||||||
|
{
|
||||||
|
crop_t c1 = {0, 0, 0, 0};
|
||||||
|
crop_t c2 = {0, 0, 0, 0};
|
||||||
|
crop_t null = {0, 0, 0, 0};
|
||||||
|
crop_t t;
|
||||||
|
rect_t rects[(GRID_BLOCKS + 1) * (GRID_BLOCKS + 1)];
|
||||||
|
rect_t r1 = {0};
|
||||||
|
rect_t r2 = {0};
|
||||||
|
rect_t rt1, rt2;
|
||||||
|
int grid[GRID_BLOCKS + 1][GRID_BLOCKS + 1];
|
||||||
|
int score_t1, score_t2, score_r1 = 0, score_r2 = 0;
|
||||||
|
int n_rect = 0;
|
||||||
|
int score = 0;
|
||||||
|
int bw, bh;
|
||||||
|
int x, y;
|
||||||
|
int i, j;
|
||||||
|
int n_res;
|
||||||
|
|
||||||
|
/* Initialize grid */
|
||||||
|
memset(grid, 0, sizeof(int) * (GRID_BLOCKS + 1) * (GRID_BLOCKS + 1));
|
||||||
|
bw = p.w / GRID_BLOCKS;
|
||||||
|
bh = p.h / GRID_BLOCKS;
|
||||||
|
|
||||||
|
/* Ensure block height is even, if even_y is enabled */
|
||||||
|
if (even_y && (bh % 2))
|
||||||
|
bh--;
|
||||||
|
|
||||||
|
/* Ensure block dimensions are not zero */
|
||||||
|
if (!bw)
|
||||||
|
bw = 1;
|
||||||
|
if (!bh && even_y)
|
||||||
|
bh = 2;
|
||||||
|
else if (!bh)
|
||||||
|
bh = 1;
|
||||||
|
|
||||||
|
/* Determine state of blocks */
|
||||||
|
t.w = bw;
|
||||||
|
t.h = bh;
|
||||||
|
for (y = 0; (t.y = y * bh) < p.h; y++)
|
||||||
|
for (x = 0; (t.x = x * bw) < p.w; x++)
|
||||||
|
grid[y][x] = block_state(p, t);
|
||||||
|
|
||||||
|
/* Create rectangles */
|
||||||
|
for (y = 0; y < GRID_BLOCKS + 1; y++)
|
||||||
|
for (x = 0; x < GRID_BLOCKS + 1; x++)
|
||||||
|
if (grid[y][x] == -1)
|
||||||
|
{
|
||||||
|
rects[n_rect] = make_rect(grid, x, y, n_rect);
|
||||||
|
n_rect++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Shouldn't happen, empty frame */
|
||||||
|
if (!n_rect)
|
||||||
|
{
|
||||||
|
c[0] = c1;
|
||||||
|
c[1] = c2;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Single rectangle */
|
||||||
|
if (n_rect == 1)
|
||||||
|
{
|
||||||
|
c1.x = rects[0].x * bw;
|
||||||
|
c1.y = rects[0].y * bh;
|
||||||
|
c1.w = rects[0].w * bw;
|
||||||
|
c1.h = rects[0].h * bh;
|
||||||
|
auto_crop(p, &c1);
|
||||||
|
c[0] = c1;
|
||||||
|
c[1] = c2;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Two rectangles */
|
||||||
|
n_res = 2;
|
||||||
|
|
||||||
|
/* Any other number of rectangles, first find most "distant" ones */
|
||||||
|
for (i = 0; i < n_rect; i++)
|
||||||
|
for (j = 0; j < n_rect; j++)
|
||||||
|
if (i == j)
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rt1 = merge_rects(rects[i], rects[j]);
|
||||||
|
score_t1 = score_rect(rt1);
|
||||||
|
if (score <= score_t1)
|
||||||
|
{
|
||||||
|
score = score_t1;
|
||||||
|
r1 = rects[i];
|
||||||
|
r2 = rects[j];
|
||||||
|
score_r1 = score_rect(r1);
|
||||||
|
score_r2 = score_rect(r2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Merge all other rectangles with the "nearest" one */
|
||||||
|
for (i = 0; i < n_rect; i++)
|
||||||
|
{
|
||||||
|
rt1 = merge_rects(r1, rects[i]);
|
||||||
|
rt2 = merge_rects(r2, rects[i]);
|
||||||
|
score_t1 = score_rect(rt1);
|
||||||
|
score_t2 = score_rect(rt2);
|
||||||
|
if (score_t1 - score_r1 < score_t2 - score_r2)
|
||||||
|
{
|
||||||
|
r1 = rt1;
|
||||||
|
score_r1 = score_t1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
r2 = rt2;
|
||||||
|
score_r2 = score_t2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Turn rectangles into crops */
|
||||||
|
c1.x = r1.x * bw;
|
||||||
|
c1.y = r1.y * bh;
|
||||||
|
c1.w = r1.w * bw;
|
||||||
|
c1.h = r1.h * bh;
|
||||||
|
c2.x = r2.x * bw;
|
||||||
|
c2.y = r2.y * bh;
|
||||||
|
c2.w = r2.w * bw;
|
||||||
|
c2.h = r2.h * bh;
|
||||||
|
|
||||||
|
/* Minimize surfaces and return them */
|
||||||
|
auto_crop(p, &c1);
|
||||||
|
auto_crop(p, &c2);
|
||||||
|
|
||||||
|
/* Merge in rare cases of closeness or overlap */
|
||||||
|
if ((!ugly && check_close(c1, c2, 0)) || (ugly && check_close(c1, c2, -1)))
|
||||||
|
{
|
||||||
|
c1 = merge_rects(c1, c2);
|
||||||
|
c2 = null;
|
||||||
|
auto_crop(p, &c1);
|
||||||
|
n_res = 1;
|
||||||
|
}
|
||||||
|
else if (!ugly)
|
||||||
|
{
|
||||||
|
/* Check whether split is ugly due to small gains */
|
||||||
|
rt1 = merge_rects(c1, c2);
|
||||||
|
score_t1 = score_rect(rt1);
|
||||||
|
score_t2 = score_rect(c1) + score_rect(c2);
|
||||||
|
|
||||||
|
/* Merge if area taken by the merged rectangle is less than 1.5 * sum of
|
||||||
|
* split rectangles and the difference is below a hard limit.
|
||||||
|
*/
|
||||||
|
if ((score_t1 < 3 * score_t2 / 2) && (score_t1 - score_t2 < 500 * 300))
|
||||||
|
{
|
||||||
|
c1 = merge_rects(c1, c2);
|
||||||
|
c2 = null;
|
||||||
|
auto_crop(p, &c1);
|
||||||
|
n_res = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c[0] = c1;
|
||||||
|
c[1] = c2;
|
||||||
|
return n_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find two minimal non-overlapping windows covering the given rectangles. */
|
||||||
|
|
||||||
|
typedef struct window_s
|
||||||
|
{
|
||||||
|
rect_t r; /* Position and size. */
|
||||||
|
int s; /* Score. */
|
||||||
|
} window_t;
|
||||||
|
|
||||||
|
typedef struct interval_s
|
||||||
|
{
|
||||||
|
rect_t r;
|
||||||
|
int start, end;
|
||||||
|
} interval_t;
|
||||||
|
|
||||||
|
STATIC_LIST(interval, interval_t)
|
||||||
|
|
||||||
|
int cmp_rect_x (rect_t *a, rect_t *b)
|
||||||
|
{
|
||||||
|
return a->x > b->x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmp_rect_y (rect_t *a, rect_t *b)
|
||||||
|
{
|
||||||
|
return a->y > b->y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rect_bounds_x (rect_t *c, int *x, int *w)
|
||||||
|
{
|
||||||
|
*x = c->x;
|
||||||
|
*w = c->w;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rect_bounds_y (rect_t *c, int *y, int *h)
|
||||||
|
{
|
||||||
|
*y = c->y;
|
||||||
|
*h = c->h;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The windows argument must point to 2 * sizeof(rect_t) allocated memory. */
|
||||||
|
int find_windows (rect_t *rects, int n_rects, rect_t *windows)
|
||||||
|
{
|
||||||
|
void (*rect_bounds)(rect_t *, int *, int *);
|
||||||
|
rect_t **sorted = malloc(sizeof(void *) * n_rects);
|
||||||
|
void *work[2][2] = { {rect_bounds_x, cmp_rect_x}
|
||||||
|
, {rect_bounds_y, cmp_rect_y}
|
||||||
|
};
|
||||||
|
interval_list_t *segs;
|
||||||
|
interval_t *iv = NULL;
|
||||||
|
window_t *fwd, *bwd;
|
||||||
|
rect_t best[2], tmp;
|
||||||
|
int i, dir, edge, a, b, n_ivs, found;
|
||||||
|
int score = -1;
|
||||||
|
|
||||||
|
if (!n_rects)
|
||||||
|
{
|
||||||
|
free(sorted);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(best, 0, 2 * sizeof(rect_t));
|
||||||
|
found = 0;
|
||||||
|
|
||||||
|
for (dir = 0; dir < 2; dir++)
|
||||||
|
{
|
||||||
|
/* Sort rectangles from left/top to right/bottom. */
|
||||||
|
for (i = 0; i < n_rects; i++)
|
||||||
|
sorted[i] = &(rects[i]);
|
||||||
|
sort((sort_func_t)work[dir][1], (void **)sorted, n_rects);
|
||||||
|
|
||||||
|
/* Group overlapping rectangles. */
|
||||||
|
segs = interval_list_new();
|
||||||
|
n_ivs = 0;
|
||||||
|
edge = -1;
|
||||||
|
|
||||||
|
rect_bounds = work[dir][0];
|
||||||
|
for (i = 0; i < n_rects; i++)
|
||||||
|
{
|
||||||
|
rect_bounds(sorted[i], &a, &b);
|
||||||
|
if (a < edge)
|
||||||
|
{
|
||||||
|
iv->end = MAX(iv->end, a + b);
|
||||||
|
iv->r = merge_rects(iv->r, *(sorted[i]));
|
||||||
|
edge = iv->end;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iv = malloc(sizeof(interval_t));
|
||||||
|
iv->start = a;
|
||||||
|
iv->end = a + b;
|
||||||
|
iv->r = *(sorted[i]);
|
||||||
|
interval_list_insert_after(segs, iv);
|
||||||
|
n_ivs++;
|
||||||
|
edge = iv->end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!n_ivs)
|
||||||
|
{
|
||||||
|
interval_list_destroy_deep(segs);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sum up scores for interval 0-n and vice versa. */
|
||||||
|
fwd = malloc(sizeof(window_t) * n_ivs);
|
||||||
|
bwd = malloc(sizeof(window_t) * n_ivs);
|
||||||
|
|
||||||
|
iv = interval_list_first(segs);
|
||||||
|
/* Initialize first candidate. */
|
||||||
|
fwd[0].r = iv->r;
|
||||||
|
fwd[0].s = score_rect(fwd[0].r);
|
||||||
|
/* Merge/sum up the rest. */
|
||||||
|
for (i = 1; i < n_ivs && iv != NULL; i++)
|
||||||
|
{
|
||||||
|
iv = interval_list_next(segs);
|
||||||
|
fwd[i].r = merge_rects(fwd[i - 1].r, iv->r);
|
||||||
|
fwd[i].s = score_rect(fwd[i].r);
|
||||||
|
}
|
||||||
|
|
||||||
|
iv = interval_list_last(segs);
|
||||||
|
/* Corresponding to a "merged all" forward window is a null window. */
|
||||||
|
memset(&(bwd[n_ivs - 1]), 0, sizeof(window_t));
|
||||||
|
/* Initialize first real candidate if it exists. */
|
||||||
|
if (n_ivs > 1)
|
||||||
|
{
|
||||||
|
bwd[n_ivs - 2].r = iv->r;
|
||||||
|
bwd[n_ivs - 2].s = score_rect(bwd[n_ivs - 2].r);
|
||||||
|
}
|
||||||
|
/* Merge/sum up the rest. */
|
||||||
|
for (i = n_ivs - 3; i >= 0 && iv != NULL; i--)
|
||||||
|
{
|
||||||
|
iv = interval_list_prev(segs);
|
||||||
|
bwd[i].r = merge_rects(bwd[i + 1].r, iv->r);
|
||||||
|
bwd[i].s = score_rect(bwd[i].r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find best pair of two windows. */
|
||||||
|
for (i = 0; i < n_ivs; i++)
|
||||||
|
if (fwd[i].s + bwd[i].s < score || score == -1)
|
||||||
|
{
|
||||||
|
score = fwd[i].s + bwd[i].s;
|
||||||
|
best[0] = fwd[i].r;
|
||||||
|
best[1] = bwd[i].r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Cleanup. */
|
||||||
|
free(bwd);
|
||||||
|
free(fwd);
|
||||||
|
interval_list_destroy_deep(segs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Is any of the best rectangles not null? */
|
||||||
|
if ((best[0].w != 0 && best[0].h != 0) || (best[1].w != 0 && best[1].h != 0))
|
||||||
|
{
|
||||||
|
/* We have at least one. */
|
||||||
|
found = 1;
|
||||||
|
|
||||||
|
/* If the first one is null, set it to the rect of the second and null that one. */
|
||||||
|
if (best[0].w == 0 || best[0].h == 0)
|
||||||
|
{
|
||||||
|
best[0] = best[1];
|
||||||
|
memset(&(best[1]), 0, sizeof(rect_t));
|
||||||
|
}
|
||||||
|
/* If the second rect is still not null... */
|
||||||
|
if (best[1].w != 0 && best[1].h != 0)
|
||||||
|
{
|
||||||
|
/* We found two. */
|
||||||
|
found = 2;
|
||||||
|
/* Let's order them, so the one closer to 0/0 is the second. */
|
||||||
|
if (best[1].y > best[0].y || (best[1].y == best[0].y && best[1].x > best[0].x))
|
||||||
|
{
|
||||||
|
tmp = best[0];
|
||||||
|
best[0] = best[1];
|
||||||
|
best[1] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memcpy(windows, best, 2 * sizeof(rect_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Cleanup. */
|
||||||
|
free(sorted);
|
||||||
|
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
void enforce_even_y (crop_t *c, int n)
|
||||||
|
{
|
||||||
|
int mod;
|
||||||
|
|
||||||
|
if (!n)
|
||||||
|
return;
|
||||||
|
/* Since block borders used to auto split always lie on even rows, y will
|
||||||
|
* only be odd when it was shrunk to below a block border, meaning expanding it
|
||||||
|
* back up by one row should be harmless and never lead to overlap.
|
||||||
|
*/
|
||||||
|
mod = c[0].y % 2;
|
||||||
|
c[0].y -= mod;
|
||||||
|
c[0].h += mod;
|
||||||
|
if (n > 1)
|
||||||
|
{
|
||||||
|
mod = c[1].y % 2;
|
||||||
|
c[1].y -= mod;
|
||||||
|
c[1].h += mod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
51
auto_split.h
Normal file
51
auto_split.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* avs2bdnxml - Generates BluRay subtitle stuff from RGBA AviSynth scripts
|
||||||
|
* Copyright (C) 2008-2013 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef AUTO_SPLIT_H
|
||||||
|
#define AUTO_SPLIT_H
|
||||||
|
|
||||||
|
#define MAX(x,y) ((x) > (y) ? (x) : (y))
|
||||||
|
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
||||||
|
|
||||||
|
typedef struct pic_s
|
||||||
|
{
|
||||||
|
char *b; /* Buffer */
|
||||||
|
int w;
|
||||||
|
int h;
|
||||||
|
int s; /* Stride */
|
||||||
|
} pic_t;
|
||||||
|
|
||||||
|
typedef struct crop_s
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int w;
|
||||||
|
int h;
|
||||||
|
} crop_t;
|
||||||
|
|
||||||
|
typedef crop_t rect_t;
|
||||||
|
|
||||||
|
void auto_crop (pic_t p, crop_t *c);
|
||||||
|
int find_windows (crop_t *rects, int n_rects, crop_t *windows);
|
||||||
|
int auto_split (pic_t p, crop_t *c, int ugly, int even_y);
|
||||||
|
rect_t merge_rects (rect_t r1, rect_t r2);
|
||||||
|
int score_rect (rect_t r);
|
||||||
|
void enforce_even_y (crop_t *c, int n);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
BIN
auto_split.o
Normal file
BIN
auto_split.o
Normal file
Binary file not shown.
1334
avs2bdnxml.c
Normal file
1334
avs2bdnxml.c
Normal file
File diff suppressed because it is too large
Load Diff
BIN
avs2bdnxml.exe
Executable file
BIN
avs2bdnxml.exe
Executable file
Binary file not shown.
0
avs2bdnxml.h
Normal file
0
avs2bdnxml.h
Normal file
BIN
avs2bdnxml.o
Normal file
BIN
avs2bdnxml.o
Normal file
Binary file not shown.
22
debug/Makefile
Normal file
22
debug/Makefile
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
CC=i586-mingw32msvc-gcc
|
||||||
|
CFLAGS=-O3 -Wall -DLE_ARCH
|
||||||
|
LDFLAGS=-lm
|
||||||
|
OBJS=pgsparse.o
|
||||||
|
EXE=pgsparse.exe
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) -c $< $(CFLAGS)
|
||||||
|
|
||||||
|
$(EXE): $(OBJS)
|
||||||
|
$(CC) -o $(EXE) $(OBJS) $(LDFLAGS)
|
||||||
|
|
||||||
|
all: $(EXE)
|
||||||
|
|
||||||
|
dist: clean all
|
||||||
|
strip -s $(EXE)
|
||||||
|
upx-ucl --best $(EXE)
|
||||||
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
.phony clean:
|
||||||
|
rm -f $(EXE) $(OBJS)
|
||||||
|
|
22
debug/Makefile.linux
Normal file
22
debug/Makefile.linux
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
CC=gcc
|
||||||
|
CFLAGS=-O3 -Wall -DLE_ARCH
|
||||||
|
LDFLAGS=-lm
|
||||||
|
OBJS=pgsparse.o
|
||||||
|
EXE=pgsparse
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) -c $< $(CFLAGS)
|
||||||
|
|
||||||
|
$(EXE): $(OBJS)
|
||||||
|
$(CC) -o $(EXE) $(OBJS) $(LDFLAGS)
|
||||||
|
|
||||||
|
all: $(EXE)
|
||||||
|
|
||||||
|
dist: clean all
|
||||||
|
strip -s $(EXE)
|
||||||
|
upx-ucl --best $(EXE)
|
||||||
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
.phony clean:
|
||||||
|
rm -f $(EXE) $(OBJS)
|
||||||
|
|
546
debug/pgsparse.c
Normal file
546
debug/pgsparse.c
Normal file
@ -0,0 +1,546 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* pgsparse - Parses BluRay PGS/SUP files
|
||||||
|
* Copyright (C) 2010 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#ifdef BE_ARCH
|
||||||
|
#define SWAP32(x) (x)
|
||||||
|
#define SWAP16(x) (x)
|
||||||
|
#endif
|
||||||
|
#ifdef LE_ARCH
|
||||||
|
#define SWAP32(x) ((int32_t)(((x & 0xff000000) >> 24) | ((x & 0xff0000) >> 8) | ((x & 0xff00) << 8) | ((x & 0xff) << 24)))
|
||||||
|
#define SWAP16(x) ((int16_t)(((x & 0xff) << 8) | ((x & 0xff00) >> 8)))
|
||||||
|
#endif
|
||||||
|
#ifndef LE_ARCH
|
||||||
|
#ifndef BE_ARCH
|
||||||
|
#error "Please specify endian-ness with -DLE_ARCH or -DBE_ARCH."
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||||
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
|
typedef struct parser_state_s
|
||||||
|
{
|
||||||
|
FILE *fh;
|
||||||
|
int total_object_sizes;
|
||||||
|
int images;
|
||||||
|
int palettes;
|
||||||
|
int diff_palettes;
|
||||||
|
char last_pal[256 * 5];
|
||||||
|
} parser_state_t;
|
||||||
|
|
||||||
|
void clear_palette (parser_state_t *ps)
|
||||||
|
{
|
||||||
|
memset(ps->last_pal, 0, 256*5);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_stats (parser_state_t *ps)
|
||||||
|
{
|
||||||
|
ps->total_object_sizes = 0;
|
||||||
|
ps->images = 0;
|
||||||
|
ps->palettes = 0;
|
||||||
|
ps->diff_palettes = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
parser_state_t *new_sup_parser (char *filename)
|
||||||
|
{
|
||||||
|
parser_state_t *ps = malloc(sizeof(parser_state_t));
|
||||||
|
|
||||||
|
if ((ps->fh = fopen(filename, "rb")) == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Couldn't open SUP file (%s): ", filename);
|
||||||
|
perror(NULL);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
clear_stats(ps);
|
||||||
|
clear_palette(ps);
|
||||||
|
|
||||||
|
return ps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void die (FILE *fh, int offset, char *error)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error at %08x: %s\n", (int)ftell(fh) - offset, error);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void safe_read (void *p, int size, FILE *fh, char *desc)
|
||||||
|
{
|
||||||
|
char *error;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if ((r = fread(p, 1, size, fh)) != size)
|
||||||
|
{
|
||||||
|
error = malloc(128);
|
||||||
|
error[127] = 0;
|
||||||
|
snprintf(error, 127, "Could not read all %uB of %s.", size, desc);
|
||||||
|
die(fh, r, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void safe_seek (FILE *fh, int pos, char *desc)
|
||||||
|
{
|
||||||
|
char *error;
|
||||||
|
|
||||||
|
if (fseek(fh, pos, SEEK_CUR))
|
||||||
|
{
|
||||||
|
error = malloc(128);
|
||||||
|
error[127] = 0;
|
||||||
|
snprintf(error, 127, "Unexpected seek failure during %s.", desc);
|
||||||
|
perror(error);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct fps_id_s
|
||||||
|
{
|
||||||
|
int num;
|
||||||
|
int den;
|
||||||
|
int id;
|
||||||
|
} fps_id_t;
|
||||||
|
|
||||||
|
int get_fps (int id, int *fps_num, int *fps_den)
|
||||||
|
{
|
||||||
|
fps_id_t ids[] = {{24000, 1001, 16}, {24, 1, 32}, {25, 1, 48}, {30000, 1001, 64}, {50, 1, 96}, {60000, 1001, 112}, {0, 0, 0}};
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (ids[i].id)
|
||||||
|
{
|
||||||
|
if (ids[i].id == id)
|
||||||
|
{
|
||||||
|
*fps_num = ids[i].num;
|
||||||
|
*fps_den = ids[i].den;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_header_s
|
||||||
|
{
|
||||||
|
uint8_t m1; /* 'P' */
|
||||||
|
uint8_t m2; /* 'G' */
|
||||||
|
uint32_t start_time;
|
||||||
|
uint32_t dts;
|
||||||
|
uint8_t packet_type; /* 0x16 = pcs_start/end, 0x17 = wds, 0x14 = palette, 0x15 = ods_first, 0x80 = null */
|
||||||
|
uint16_t packet_len;
|
||||||
|
} __attribute ((packed)) sup_header_t;
|
||||||
|
|
||||||
|
void conv_sup_header (sup_header_t *h)
|
||||||
|
{
|
||||||
|
h->start_time = SWAP32(h->start_time);
|
||||||
|
h->dts = SWAP32(h->dts);
|
||||||
|
h->packet_len = SWAP16(h->packet_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_ts (uint32_t ts)
|
||||||
|
{
|
||||||
|
printf("%.7fs (%u/90000s)", ((double)ts) / 90000, ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_palette_s
|
||||||
|
{
|
||||||
|
uint16_t palette; /* FIXME */
|
||||||
|
} __attribute ((packed)) sup_palette_t;
|
||||||
|
|
||||||
|
void conv_sup_palette (sup_palette_t *p)
|
||||||
|
{
|
||||||
|
p->palette = SWAP16(p->palette);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_palette (parser_state_t *ps, sup_header_t h)
|
||||||
|
{
|
||||||
|
sup_palette_t p;
|
||||||
|
char *palette = malloc(5*256);
|
||||||
|
int i, p_size;
|
||||||
|
|
||||||
|
if (h.packet_len < 2+5)
|
||||||
|
die(ps->fh, sizeof(h), "Undersized palette packet.");
|
||||||
|
if (h.packet_len > 2+5*256)
|
||||||
|
die(ps->fh, sizeof(h), "Oversized palette packet.");
|
||||||
|
|
||||||
|
safe_read(&p, sizeof(p), ps->fh, "palette marker");
|
||||||
|
conv_sup_palette(&p);
|
||||||
|
|
||||||
|
printf("Palette\n");
|
||||||
|
printf("\tpalette = %u\n", p.palette);
|
||||||
|
(ps->palettes)++;
|
||||||
|
|
||||||
|
p_size = h.packet_len - 2;
|
||||||
|
safe_read(palette, p_size, ps->fh, "palette");
|
||||||
|
for (i = 0; i < p_size; i++)
|
||||||
|
if (palette[i] != ps->last_pal[i])
|
||||||
|
{
|
||||||
|
(ps->diff_palettes)++;
|
||||||
|
clear_palette(ps);
|
||||||
|
memcpy(ps->last_pal, palette, p_size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
free(palette);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_ods_next_s
|
||||||
|
{
|
||||||
|
uint16_t picture;
|
||||||
|
uint8_t m; /* 0 */
|
||||||
|
uint8_t last; /* 0 if not, if 64 yes */
|
||||||
|
} __attribute ((packed)) sup_ods_next_t;
|
||||||
|
|
||||||
|
void conv_sup_ods_next (sup_ods_next_t *odsn)
|
||||||
|
{
|
||||||
|
odsn->picture = SWAP16(odsn->picture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_odsn (parser_state_t *ps, sup_header_t h)
|
||||||
|
{
|
||||||
|
sup_ods_next_t odsn;
|
||||||
|
|
||||||
|
if (h.packet_len < sizeof(odsn))
|
||||||
|
die(ps->fh, sizeof(h), "Undersized ODSN packet.");
|
||||||
|
|
||||||
|
safe_read(&odsn, sizeof(odsn), ps->fh, "ODSN structure");
|
||||||
|
conv_sup_ods_next(&odsn);
|
||||||
|
if (odsn.m != 0 || (odsn.last != 0 && odsn.last != 64))
|
||||||
|
die(ps->fh, sizeof(odsn), "Invalid ODSN magic.");
|
||||||
|
printf("ODS next\n");
|
||||||
|
printf("\tpicture = %u\n", odsn.picture);
|
||||||
|
printf("\tlast = %s (%u)\n", odsn.last ? "yes" : "no", odsn.last);
|
||||||
|
|
||||||
|
safe_seek(ps->fh, h.packet_len - sizeof(odsn), "ODSN data");
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_ods_first_s
|
||||||
|
{
|
||||||
|
uint16_t picture;
|
||||||
|
uint8_t palette; /* FIXME */
|
||||||
|
uint32_t magic_len; /* (single_packet ? 0xc0000000 : 0x80000000) | (length + 4) */
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height;
|
||||||
|
} __attribute ((packed)) sup_ods_first_t;
|
||||||
|
|
||||||
|
void conv_sup_ods_first (sup_ods_first_t *odsf)
|
||||||
|
{
|
||||||
|
odsf->picture = SWAP16(odsf->picture);
|
||||||
|
odsf->magic_len = SWAP32(odsf->magic_len);
|
||||||
|
odsf->width = SWAP16(odsf->width);
|
||||||
|
odsf->height = SWAP16(odsf->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_odsf (parser_state_t *ps, sup_header_t h)
|
||||||
|
{
|
||||||
|
sup_ods_first_t odsf;
|
||||||
|
|
||||||
|
if (h.packet_len < sizeof(odsf))
|
||||||
|
die(ps->fh, sizeof(h), "Undersized ODSF packet.");
|
||||||
|
|
||||||
|
safe_read(&odsf, sizeof(odsf), ps->fh, "ODSF structure");
|
||||||
|
conv_sup_ods_first(&odsf);
|
||||||
|
|
||||||
|
if ((odsf.magic_len & 0x80000000) && (odsf.magic_len & 0x40000000))
|
||||||
|
{
|
||||||
|
printf("ODS first\n\todsf type = single\n");
|
||||||
|
}
|
||||||
|
else if (odsf.magic_len & 0x80000000)
|
||||||
|
{
|
||||||
|
printf("ODS first\n\todsf type = multi\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fseek(ps->fh, -sizeof(odsf), SEEK_CUR);
|
||||||
|
read_odsn(ps, h);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ps->total_object_sizes += odsf.width * odsf.height;
|
||||||
|
(ps->images)++;
|
||||||
|
printf("\tpicture = %u\n", odsf.picture);
|
||||||
|
printf("\tpalette = %u\n", odsf.palette);
|
||||||
|
printf("\tlength = %u (incl. + 4)\n", odsf.magic_len & 0x3fffffff);
|
||||||
|
printf("\twidth = %u\n", odsf.width);
|
||||||
|
printf("\theight = %u\n", odsf.height);
|
||||||
|
|
||||||
|
safe_seek(ps->fh, h.packet_len - sizeof(odsf), "OSDF data");
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_wds_s
|
||||||
|
{
|
||||||
|
uint8_t objects; /* 1 or 2 */
|
||||||
|
} __attribute ((packed)) sup_wds_t;
|
||||||
|
|
||||||
|
void conv_sup_wds (sup_wds_t *wds)
|
||||||
|
{
|
||||||
|
/* Do nothing. */
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_wds_obj_s
|
||||||
|
{
|
||||||
|
uint8_t object; /* 0 or 1 */
|
||||||
|
uint16_t x_off;
|
||||||
|
uint16_t y_off;
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height;
|
||||||
|
} __attribute ((packed)) sup_wds_obj_t;
|
||||||
|
|
||||||
|
void conv_sup_wds_obj (sup_wds_obj_t *wdso)
|
||||||
|
{
|
||||||
|
wdso->x_off = SWAP16(wdso->x_off);
|
||||||
|
wdso->y_off = SWAP16(wdso->y_off);
|
||||||
|
wdso->width = SWAP16(wdso->width);
|
||||||
|
wdso->height = SWAP16(wdso->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_wds (parser_state_t *ps, sup_header_t h)
|
||||||
|
{
|
||||||
|
sup_wds_t wds;
|
||||||
|
sup_wds_obj_t wdso;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (h.packet_len != sizeof(wds) + sizeof(wdso) && h.packet_len != sizeof(wds) + 2 * sizeof(wdso))
|
||||||
|
die(ps->fh, sizeof(h), "Bad size for WDS packet.");
|
||||||
|
|
||||||
|
safe_read(&wds, sizeof(wds), ps->fh, "WDS structure");
|
||||||
|
conv_sup_wds(&wds);
|
||||||
|
if (wds.objects != 1 && wds.objects != 2)
|
||||||
|
die(ps->fh, sizeof(wds), "Invalid number of WDS objects.");
|
||||||
|
|
||||||
|
printf("WDS\n");
|
||||||
|
printf("\tobjects = %u\n", wds.objects);
|
||||||
|
|
||||||
|
for (i = 0; i < wds.objects; i++)
|
||||||
|
{
|
||||||
|
safe_read(&wdso, sizeof(wdso), ps->fh, "WDS object structure");
|
||||||
|
conv_sup_wds_obj(&wdso);
|
||||||
|
|
||||||
|
if (wdso.object != 0 && wdso.object != 1)
|
||||||
|
die(ps->fh, sizeof(wdso), "Invalid object id in WDS object.");
|
||||||
|
|
||||||
|
printf("\tObject %u\n", wdso.object + 1);
|
||||||
|
printf("\t\tx offset = %u\n", wdso.x_off);
|
||||||
|
printf("\t\ty offset = %u\n", wdso.y_off);
|
||||||
|
printf("\t\twidth = %u\n", wdso.width);
|
||||||
|
printf("\t\theight = %u\n", wdso.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_pcs_start_s
|
||||||
|
{
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height; /* height - 2 * Core.getCropOfsY */
|
||||||
|
uint8_t fps_id; /* getFpsId() */
|
||||||
|
uint16_t comp_num;
|
||||||
|
uint8_t follower; /* 0x80 if first or single, 0x40 if follows directly (end = start) or the frame after */
|
||||||
|
uint16_t m; /* 0 */
|
||||||
|
uint8_t objects; /* 1 */
|
||||||
|
} __attribute ((packed)) sup_pcs_start_t;
|
||||||
|
|
||||||
|
void conv_sup_pcs_start (sup_pcs_start_t *pcss)
|
||||||
|
{
|
||||||
|
pcss->width = SWAP16(pcss->width);
|
||||||
|
pcss->height = SWAP16(pcss->height);
|
||||||
|
pcss->comp_num = SWAP16(pcss->comp_num);
|
||||||
|
pcss->m = SWAP16(pcss->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_pcs_start_obj_s
|
||||||
|
{
|
||||||
|
uint16_t picture;
|
||||||
|
uint8_t window;
|
||||||
|
uint8_t forced; /* forced ? 64 : 0 */
|
||||||
|
uint16_t x_off;
|
||||||
|
uint16_t y_off;
|
||||||
|
} __attribute ((packed)) sup_pcs_start_obj_t;
|
||||||
|
|
||||||
|
void conv_sup_pcs_start_obj (sup_pcs_start_obj_t *pcsso)
|
||||||
|
{
|
||||||
|
pcsso->picture = SWAP16(pcsso->picture);
|
||||||
|
pcsso->x_off = SWAP16(pcsso->x_off);
|
||||||
|
pcsso->y_off = SWAP16(pcsso->y_off);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_pcs_start (parser_state_t *ps, sup_header_t h)
|
||||||
|
{
|
||||||
|
sup_pcs_start_t pcss;
|
||||||
|
sup_pcs_start_obj_t pcsso;
|
||||||
|
int fps_num, fps_den, i;
|
||||||
|
|
||||||
|
safe_read(&pcss, sizeof(pcss), ps->fh, "PCSS structure");
|
||||||
|
conv_sup_pcs_start(&pcss);
|
||||||
|
|
||||||
|
if (pcss.m != 0)
|
||||||
|
die(ps->fh, sizeof(pcss), "Invalid PCSS magic.");
|
||||||
|
|
||||||
|
printf("PCS start\n");
|
||||||
|
|
||||||
|
printf("\tframe width = %u\n", pcss.width);
|
||||||
|
printf("\tframe height = %u\n", pcss.height);
|
||||||
|
if (!get_fps(pcss.fps_id, &fps_num, &fps_den))
|
||||||
|
die(ps->fh, sizeof(pcss), "Invalid FPS ID in PCSS.");
|
||||||
|
printf("\tfps id = %u (%u/%u)\n", pcss.fps_id, fps_num, fps_den);
|
||||||
|
printf("\tcomposition = %u\n", pcss.comp_num);
|
||||||
|
printf("\tfollower = 0x%02X (%s)\n", pcss.follower, pcss.follower == 0x80 ? "no" : "within 2f");
|
||||||
|
printf("\tobjects = %u\n", pcss.objects);
|
||||||
|
|
||||||
|
if (pcss.objects > 2)
|
||||||
|
die(ps->fh, sizeof(pcss), "Invalid number of objects (must be 1 or 2).");
|
||||||
|
|
||||||
|
for (i = 0; i < pcss.objects; i++)
|
||||||
|
{
|
||||||
|
safe_read(&pcsso, sizeof(pcsso), ps->fh, "PCSSO structure");
|
||||||
|
conv_sup_pcs_start_obj(&pcsso);
|
||||||
|
|
||||||
|
if (pcsso.window != 0 && pcsso.window != 1)
|
||||||
|
die(ps->fh, sizeof(pcsso), "Invalid window id in PCSS object.");
|
||||||
|
printf("\tObject %u\n", i + 1);
|
||||||
|
if (pcsso.forced && (pcsso.forced != 64))
|
||||||
|
die(ps->fh, sizeof(pcsso), "Invalid forced flag in PCSS object.");
|
||||||
|
printf("\t\tpicture = %u\n", pcsso.picture);
|
||||||
|
printf("\t\twindow = %u\n", pcsso.window);
|
||||||
|
printf("\t\tforced = %u\n", pcsso.forced);
|
||||||
|
printf("\t\tx offset = %u\n", pcsso.x_off);
|
||||||
|
printf("\t\ty offset = %u\n", pcsso.y_off);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_pcs_end_s
|
||||||
|
{
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height;
|
||||||
|
uint8_t fps_id;
|
||||||
|
uint16_t comp_num;
|
||||||
|
uint32_t m;
|
||||||
|
} __attribute ((packed)) sup_pcs_end_t;
|
||||||
|
|
||||||
|
void conv_sup_pcs_end (sup_pcs_end_t *pcse)
|
||||||
|
{
|
||||||
|
pcse->width = SWAP16(pcse->width);
|
||||||
|
pcse->height = SWAP16(pcse->height);
|
||||||
|
pcse->comp_num = SWAP16(pcse->comp_num);
|
||||||
|
pcse->m = SWAP32(pcse->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_pcs_end (parser_state_t *ps, sup_header_t h)
|
||||||
|
{
|
||||||
|
sup_pcs_end_t pcse;
|
||||||
|
int fps_num, fps_den;
|
||||||
|
|
||||||
|
safe_read(&pcse, sizeof(pcse), ps->fh, "PCSE structure");
|
||||||
|
conv_sup_pcs_end(&pcse);
|
||||||
|
|
||||||
|
if (pcse.m != 0)
|
||||||
|
die(ps->fh, sizeof(pcse), "Invalid PCSE magic.");
|
||||||
|
|
||||||
|
printf("PCS end\n");
|
||||||
|
|
||||||
|
printf("\tsubtitle width = %u\n", pcse.width);
|
||||||
|
printf("\tsubtitle height = %u\n", pcse.height);
|
||||||
|
if (!get_fps(pcse.fps_id, &fps_num, &fps_den))
|
||||||
|
die(ps->fh, sizeof(pcse), "Invalid FPS ID in PCSE.");
|
||||||
|
printf("\tfps id = %u (%u/%u)\n", pcse.fps_id, fps_num, fps_den);
|
||||||
|
printf("\tcomposition = %u\n", pcse.comp_num);
|
||||||
|
printf("\tStats\n\t\ttot_ob_size = %u\n\t\timages = %u\n\t\tpalettes = %u\n\t\tdiff_palettes = %u\n", ps->total_object_sizes, ps->images, ps->palettes, ps->diff_palettes);
|
||||||
|
clear_stats(ps);
|
||||||
|
clear_palette(ps);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_sup (parser_state_t *ps)
|
||||||
|
{
|
||||||
|
sup_header_t h;
|
||||||
|
|
||||||
|
safe_read(&h, sizeof(h), ps->fh, "PG header");
|
||||||
|
conv_sup_header(&h);
|
||||||
|
|
||||||
|
if (h.m1 != 80 || h.m2 != 71)
|
||||||
|
die(ps->fh, sizeof(h), "Invalid PG header.");
|
||||||
|
|
||||||
|
printf("Packet at 0x%08x:\nstart_time = ", (unsigned int)(ftell(ps->fh) - sizeof(h)));
|
||||||
|
print_ts(h.start_time);
|
||||||
|
printf("\ndts = ");
|
||||||
|
print_ts(h.dts);
|
||||||
|
printf("\ntype = 0x%02X\n", h.packet_type);
|
||||||
|
printf("length = %u\n", h.packet_len);
|
||||||
|
switch (h.packet_type)
|
||||||
|
{
|
||||||
|
case 0x14:
|
||||||
|
read_palette(ps, h);
|
||||||
|
break;
|
||||||
|
case 0x15:
|
||||||
|
read_odsf(ps, h);
|
||||||
|
break;
|
||||||
|
case 0x16:
|
||||||
|
switch (h.packet_len)
|
||||||
|
{
|
||||||
|
case 11:
|
||||||
|
read_pcs_end(ps, h);
|
||||||
|
break;
|
||||||
|
case 19:
|
||||||
|
case 27:
|
||||||
|
read_pcs_start(ps, h);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
die(ps->fh, sizeof(h), "Invalid PCS size.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x17:
|
||||||
|
read_wds(ps, h);
|
||||||
|
break;
|
||||||
|
case 0x80:
|
||||||
|
if (h.packet_len == 0)
|
||||||
|
printf("Marker\n");
|
||||||
|
else
|
||||||
|
die(ps->fh, sizeof(h), "Marker with payload.");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
die(ps->fh, sizeof(h), "Unknown packet type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
parser_state_t *ps;
|
||||||
|
size_t last;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
printf("Usage: pgsparse SUPFILE\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ps = new_sup_parser(argv[1]);
|
||||||
|
|
||||||
|
fseek(ps->fh, 0, SEEK_END);
|
||||||
|
last = ftell(ps->fh);
|
||||||
|
fseek(ps->fh, 0, SEEK_SET);
|
||||||
|
|
||||||
|
while (!feof(ps->fh) && ftell(ps->fh) < last)
|
||||||
|
read_sup(ps);
|
||||||
|
|
||||||
|
fclose(ps->fh);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
BIN
debug/pgsparse.exe
Executable file
BIN
debug/pgsparse.exe
Executable file
Binary file not shown.
157
frame-a.asm
Normal file
157
frame-a.asm
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
;*****************************************************************************
|
||||||
|
;* frame-a.asm: x86 SSE2 frame operations
|
||||||
|
;*****************************************************************************
|
||||||
|
;* Copyright (C) 2013 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
;*
|
||||||
|
;* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
;*****************************************************************************
|
||||||
|
|
||||||
|
%include "x86inc.asm"
|
||||||
|
|
||||||
|
SECTION_RODATA
|
||||||
|
|
||||||
|
zero: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||||
|
a_mask: db 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff
|
||||||
|
full: db 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||||
|
ag_mask: db 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff
|
||||||
|
rb_mask: db 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
|
||||||
|
|
||||||
|
|
||||||
|
SECTION .text
|
||||||
|
|
||||||
|
struc stream_info
|
||||||
|
.width resd 1
|
||||||
|
.height resd 1
|
||||||
|
.fpsden resd 1
|
||||||
|
.fpsnum resd 1
|
||||||
|
endstruc
|
||||||
|
|
||||||
|
INIT_XMM
|
||||||
|
cglobal is_identical_sse2, 3,5,6
|
||||||
|
mov r3, [r0+stream_info.width]
|
||||||
|
imul r3, [r0+stream_info.height]
|
||||||
|
lea r3, [r1+r3*4]
|
||||||
|
mov r4, 0
|
||||||
|
mova m0, [zero]
|
||||||
|
mova m1, [a_mask]
|
||||||
|
mova m2, [full]
|
||||||
|
.loop:
|
||||||
|
test r4, r4
|
||||||
|
jnz .diff
|
||||||
|
cmp r1, r3
|
||||||
|
je .ident
|
||||||
|
|
||||||
|
mova m3, [r1]
|
||||||
|
mova m4, m3
|
||||||
|
pand m4, m1
|
||||||
|
mova m5, [r2]
|
||||||
|
pcmpeqd m4, m0
|
||||||
|
pxor m4, m2
|
||||||
|
pand m3, m4
|
||||||
|
mova [r1], m3
|
||||||
|
psadbw m3, m5
|
||||||
|
movd r4, m3
|
||||||
|
|
||||||
|
add r1, 16
|
||||||
|
add r2, 16
|
||||||
|
jmp .loop
|
||||||
|
.diff:
|
||||||
|
mov eax, 0
|
||||||
|
jmp .end
|
||||||
|
.ident:
|
||||||
|
mov eax, 1
|
||||||
|
.end:
|
||||||
|
RET
|
||||||
|
|
||||||
|
INIT_XMM
|
||||||
|
cglobal is_empty_sse2, 2,4,3
|
||||||
|
mov r2, [r0+stream_info.width]
|
||||||
|
imul r2, [r0+stream_info.height]
|
||||||
|
lea r2, [r1+r2*4]
|
||||||
|
mov r3, 0
|
||||||
|
mova m0, [zero]
|
||||||
|
mova m1, [a_mask]
|
||||||
|
.loop:
|
||||||
|
test r3, r3
|
||||||
|
jnz .not
|
||||||
|
cmp r1, r2
|
||||||
|
je .empty
|
||||||
|
|
||||||
|
mova m2, [r1]
|
||||||
|
pand m2, m1
|
||||||
|
psadbw m2, m0
|
||||||
|
movd r3, m2
|
||||||
|
|
||||||
|
add r1, 16
|
||||||
|
jmp .loop
|
||||||
|
.not:
|
||||||
|
mov eax, 0
|
||||||
|
jmp .end
|
||||||
|
.empty:
|
||||||
|
mov eax, 1
|
||||||
|
.end:
|
||||||
|
RET
|
||||||
|
|
||||||
|
INIT_XMM
|
||||||
|
cglobal zero_transparent_sse2, 2,3,5
|
||||||
|
mov r2, [r0+stream_info.width]
|
||||||
|
imul r2, [r0+stream_info.height]
|
||||||
|
lea r2, [r1+r2*4]
|
||||||
|
mova m0, [zero]
|
||||||
|
mova m1, [a_mask]
|
||||||
|
mova m2, [full]
|
||||||
|
.loop:
|
||||||
|
cmp r1, r2
|
||||||
|
je .end
|
||||||
|
|
||||||
|
mova m3, [r1]
|
||||||
|
mova m4, m3
|
||||||
|
pand m4, m1
|
||||||
|
pcmpeqd m4, m0
|
||||||
|
pxor m4, m2
|
||||||
|
pand m3, m4
|
||||||
|
mova [r1], m3
|
||||||
|
|
||||||
|
add r1, 16
|
||||||
|
jmp .loop
|
||||||
|
.end:
|
||||||
|
RET
|
||||||
|
|
||||||
|
INIT_XMM
|
||||||
|
cglobal swap_rb_sse2, 3,4,5
|
||||||
|
mov r3, [r0+stream_info.width]
|
||||||
|
imul r3, [r0+stream_info.height]
|
||||||
|
lea r3, [r1+r3*4]
|
||||||
|
mova m0, [ag_mask]
|
||||||
|
mova m1, [rb_mask]
|
||||||
|
.loop:
|
||||||
|
cmp r1, r3
|
||||||
|
je .end
|
||||||
|
|
||||||
|
mova m2, [r1]
|
||||||
|
mova m3, m2
|
||||||
|
mova m4, m2
|
||||||
|
psrld m3, 16
|
||||||
|
pslld m2, 16
|
||||||
|
por m2, m3
|
||||||
|
pand m4, m0
|
||||||
|
pand m2, m1
|
||||||
|
por m2, m4
|
||||||
|
mova [r2], m2
|
||||||
|
|
||||||
|
add r1, 16
|
||||||
|
add r2, 16
|
||||||
|
jmp .loop
|
||||||
|
.end:
|
||||||
|
RET
|
1437
inc/pngconf.h
Normal file
1437
inc/pngconf.h
Normal file
File diff suppressed because it is too large
Load Diff
332
inc/zconf.h
Normal file
332
inc/zconf.h
Normal file
@ -0,0 +1,332 @@
|
|||||||
|
/* zconf.h -- configuration of the zlib compression library
|
||||||
|
* Copyright (C) 1995-2005 Jean-loup Gailly.
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @(#) $Id$ */
|
||||||
|
|
||||||
|
#ifndef ZCONF_H
|
||||||
|
#define ZCONF_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If you *really* need a unique prefix for all types and library functions,
|
||||||
|
* compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
|
||||||
|
*/
|
||||||
|
#ifdef Z_PREFIX
|
||||||
|
# define deflateInit_ z_deflateInit_
|
||||||
|
# define deflate z_deflate
|
||||||
|
# define deflateEnd z_deflateEnd
|
||||||
|
# define inflateInit_ z_inflateInit_
|
||||||
|
# define inflate z_inflate
|
||||||
|
# define inflateEnd z_inflateEnd
|
||||||
|
# define deflateInit2_ z_deflateInit2_
|
||||||
|
# define deflateSetDictionary z_deflateSetDictionary
|
||||||
|
# define deflateCopy z_deflateCopy
|
||||||
|
# define deflateReset z_deflateReset
|
||||||
|
# define deflateParams z_deflateParams
|
||||||
|
# define deflateBound z_deflateBound
|
||||||
|
# define deflatePrime z_deflatePrime
|
||||||
|
# define inflateInit2_ z_inflateInit2_
|
||||||
|
# define inflateSetDictionary z_inflateSetDictionary
|
||||||
|
# define inflateSync z_inflateSync
|
||||||
|
# define inflateSyncPoint z_inflateSyncPoint
|
||||||
|
# define inflateCopy z_inflateCopy
|
||||||
|
# define inflateReset z_inflateReset
|
||||||
|
# define inflateBack z_inflateBack
|
||||||
|
# define inflateBackEnd z_inflateBackEnd
|
||||||
|
# define compress z_compress
|
||||||
|
# define compress2 z_compress2
|
||||||
|
# define compressBound z_compressBound
|
||||||
|
# define uncompress z_uncompress
|
||||||
|
# define adler32 z_adler32
|
||||||
|
# define crc32 z_crc32
|
||||||
|
# define get_crc_table z_get_crc_table
|
||||||
|
# define zError z_zError
|
||||||
|
|
||||||
|
# define alloc_func z_alloc_func
|
||||||
|
# define free_func z_free_func
|
||||||
|
# define in_func z_in_func
|
||||||
|
# define out_func z_out_func
|
||||||
|
# define Byte z_Byte
|
||||||
|
# define uInt z_uInt
|
||||||
|
# define uLong z_uLong
|
||||||
|
# define Bytef z_Bytef
|
||||||
|
# define charf z_charf
|
||||||
|
# define intf z_intf
|
||||||
|
# define uIntf z_uIntf
|
||||||
|
# define uLongf z_uLongf
|
||||||
|
# define voidpf z_voidpf
|
||||||
|
# define voidp z_voidp
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__MSDOS__) && !defined(MSDOS)
|
||||||
|
# define MSDOS
|
||||||
|
#endif
|
||||||
|
#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
|
||||||
|
# define OS2
|
||||||
|
#endif
|
||||||
|
#if defined(_WINDOWS) && !defined(WINDOWS)
|
||||||
|
# define WINDOWS
|
||||||
|
#endif
|
||||||
|
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
|
||||||
|
# ifndef WIN32
|
||||||
|
# define WIN32
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
|
||||||
|
# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
|
||||||
|
# ifndef SYS16BIT
|
||||||
|
# define SYS16BIT
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compile with -DMAXSEG_64K if the alloc function cannot allocate more
|
||||||
|
* than 64k bytes at a time (needed on systems with 16-bit int).
|
||||||
|
*/
|
||||||
|
#ifdef SYS16BIT
|
||||||
|
# define MAXSEG_64K
|
||||||
|
#endif
|
||||||
|
#ifdef MSDOS
|
||||||
|
# define UNALIGNED_OK
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __STDC_VERSION__
|
||||||
|
# ifndef STDC
|
||||||
|
# define STDC
|
||||||
|
# endif
|
||||||
|
# if __STDC_VERSION__ >= 199901L
|
||||||
|
# ifndef STDC99
|
||||||
|
# define STDC99
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STDC
|
||||||
|
# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
|
||||||
|
# define const /* note: need a more gentle solution here */
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Some Mac compilers merge all .h files incorrectly: */
|
||||||
|
#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
|
||||||
|
# define NO_DUMMY_DECL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Maximum value for memLevel in deflateInit2 */
|
||||||
|
#ifndef MAX_MEM_LEVEL
|
||||||
|
# ifdef MAXSEG_64K
|
||||||
|
# define MAX_MEM_LEVEL 8
|
||||||
|
# else
|
||||||
|
# define MAX_MEM_LEVEL 9
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Maximum value for windowBits in deflateInit2 and inflateInit2.
|
||||||
|
* WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
|
||||||
|
* created by gzip. (Files created by minigzip can still be extracted by
|
||||||
|
* gzip.)
|
||||||
|
*/
|
||||||
|
#ifndef MAX_WBITS
|
||||||
|
# define MAX_WBITS 15 /* 32K LZ77 window */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The memory requirements for deflate are (in bytes):
|
||||||
|
(1 << (windowBits+2)) + (1 << (memLevel+9))
|
||||||
|
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
|
||||||
|
plus a few kilobytes for small objects. For example, if you want to reduce
|
||||||
|
the default memory requirements from 256K to 128K, compile with
|
||||||
|
make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
|
||||||
|
Of course this will generally degrade compression (there's no free lunch).
|
||||||
|
|
||||||
|
The memory requirements for inflate are (in bytes) 1 << windowBits
|
||||||
|
that is, 32K for windowBits=15 (default value) plus a few kilobytes
|
||||||
|
for small objects.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Type declarations */
|
||||||
|
|
||||||
|
#ifndef OF /* function prototypes */
|
||||||
|
# ifdef STDC
|
||||||
|
# define OF(args) args
|
||||||
|
# else
|
||||||
|
# define OF(args) ()
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The following definitions for FAR are needed only for MSDOS mixed
|
||||||
|
* model programming (small or medium model with some far allocations).
|
||||||
|
* This was tested only with MSC; for other MSDOS compilers you may have
|
||||||
|
* to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
|
||||||
|
* just define FAR to be empty.
|
||||||
|
*/
|
||||||
|
#ifdef SYS16BIT
|
||||||
|
# if defined(M_I86SM) || defined(M_I86MM)
|
||||||
|
/* MSC small or medium model */
|
||||||
|
# define SMALL_MEDIUM
|
||||||
|
# ifdef _MSC_VER
|
||||||
|
# define FAR _far
|
||||||
|
# else
|
||||||
|
# define FAR far
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# if (defined(__SMALL__) || defined(__MEDIUM__))
|
||||||
|
/* Turbo C small or medium model */
|
||||||
|
# define SMALL_MEDIUM
|
||||||
|
# ifdef __BORLANDC__
|
||||||
|
# define FAR _far
|
||||||
|
# else
|
||||||
|
# define FAR far
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(WINDOWS) || defined(WIN32)
|
||||||
|
/* If building or using zlib as a DLL, define ZLIB_DLL.
|
||||||
|
* This is not mandatory, but it offers a little performance increase.
|
||||||
|
*/
|
||||||
|
# ifdef ZLIB_DLL
|
||||||
|
# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
|
||||||
|
# ifdef ZLIB_INTERNAL
|
||||||
|
# define ZEXTERN extern __declspec(dllexport)
|
||||||
|
# else
|
||||||
|
# define ZEXTERN extern __declspec(dllimport)
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# endif /* ZLIB_DLL */
|
||||||
|
/* If building or using zlib with the WINAPI/WINAPIV calling convention,
|
||||||
|
* define ZLIB_WINAPI.
|
||||||
|
* Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
|
||||||
|
*/
|
||||||
|
# ifdef ZLIB_WINAPI
|
||||||
|
# ifdef FAR
|
||||||
|
# undef FAR
|
||||||
|
# endif
|
||||||
|
# include <windows.h>
|
||||||
|
/* No need for _export, use ZLIB.DEF instead. */
|
||||||
|
/* For complete Windows compatibility, use WINAPI, not __stdcall. */
|
||||||
|
# define ZEXPORT WINAPI
|
||||||
|
# ifdef WIN32
|
||||||
|
# define ZEXPORTVA WINAPIV
|
||||||
|
# else
|
||||||
|
# define ZEXPORTVA FAR CDECL
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (__BEOS__)
|
||||||
|
# ifdef ZLIB_DLL
|
||||||
|
# ifdef ZLIB_INTERNAL
|
||||||
|
# define ZEXPORT __declspec(dllexport)
|
||||||
|
# define ZEXPORTVA __declspec(dllexport)
|
||||||
|
# else
|
||||||
|
# define ZEXPORT __declspec(dllimport)
|
||||||
|
# define ZEXPORTVA __declspec(dllimport)
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ZEXTERN
|
||||||
|
# define ZEXTERN extern
|
||||||
|
#endif
|
||||||
|
#ifndef ZEXPORT
|
||||||
|
# define ZEXPORT
|
||||||
|
#endif
|
||||||
|
#ifndef ZEXPORTVA
|
||||||
|
# define ZEXPORTVA
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FAR
|
||||||
|
# define FAR
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(__MACTYPES__)
|
||||||
|
typedef unsigned char Byte; /* 8 bits */
|
||||||
|
#endif
|
||||||
|
typedef unsigned int uInt; /* 16 bits or more */
|
||||||
|
typedef unsigned long uLong; /* 32 bits or more */
|
||||||
|
|
||||||
|
#ifdef SMALL_MEDIUM
|
||||||
|
/* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
|
||||||
|
# define Bytef Byte FAR
|
||||||
|
#else
|
||||||
|
typedef Byte FAR Bytef;
|
||||||
|
#endif
|
||||||
|
typedef char FAR charf;
|
||||||
|
typedef int FAR intf;
|
||||||
|
typedef uInt FAR uIntf;
|
||||||
|
typedef uLong FAR uLongf;
|
||||||
|
|
||||||
|
#ifdef STDC
|
||||||
|
typedef void const *voidpc;
|
||||||
|
typedef void FAR *voidpf;
|
||||||
|
typedef void *voidp;
|
||||||
|
#else
|
||||||
|
typedef Byte const *voidpc;
|
||||||
|
typedef Byte FAR *voidpf;
|
||||||
|
typedef Byte *voidp;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 1 /* HAVE_UNISTD_H -- this line is updated by ./configure */
|
||||||
|
# include <sys/types.h> /* for off_t */
|
||||||
|
# include <unistd.h> /* for SEEK_* and off_t */
|
||||||
|
# ifdef VMS
|
||||||
|
# include <unixio.h> /* for off_t */
|
||||||
|
# endif
|
||||||
|
# define z_off_t off_t
|
||||||
|
#endif
|
||||||
|
#ifndef SEEK_SET
|
||||||
|
# define SEEK_SET 0 /* Seek from beginning of file. */
|
||||||
|
# define SEEK_CUR 1 /* Seek from current position. */
|
||||||
|
# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
|
||||||
|
#endif
|
||||||
|
#ifndef z_off_t
|
||||||
|
# define z_off_t long
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__OS400__)
|
||||||
|
# define NO_vsnprintf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__MVS__)
|
||||||
|
# define NO_vsnprintf
|
||||||
|
# ifdef FAR
|
||||||
|
# undef FAR
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* MVS linker does not support external names larger than 8 bytes */
|
||||||
|
#if defined(__MVS__)
|
||||||
|
# pragma map(deflateInit_,"DEIN")
|
||||||
|
# pragma map(deflateInit2_,"DEIN2")
|
||||||
|
# pragma map(deflateEnd,"DEEND")
|
||||||
|
# pragma map(deflateBound,"DEBND")
|
||||||
|
# pragma map(inflateInit_,"ININ")
|
||||||
|
# pragma map(inflateInit2_,"ININ2")
|
||||||
|
# pragma map(inflateEnd,"INEND")
|
||||||
|
# pragma map(inflateSync,"INSY")
|
||||||
|
# pragma map(inflateSetDictionary,"INSEDI")
|
||||||
|
# pragma map(compressBound,"CMBND")
|
||||||
|
# pragma map(inflate_table,"INTABL")
|
||||||
|
# pragma map(inflate_fast,"INFA")
|
||||||
|
# pragma map(inflate_copyright,"INCOPY")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* ZCONF_H */
|
1357
inc/zlib.h
Normal file
1357
inc/zlib.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
lib/libpng.a
Normal file
BIN
lib/libpng.a
Normal file
Binary file not shown.
BIN
lib/libz.a
Normal file
BIN
lib/libz.a
Normal file
Binary file not shown.
312
palletize.c
Normal file
312
palletize.c
Normal file
@ -0,0 +1,312 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* avs2bdnxml - Generates BluRay subtitle stuff from RGBA AviSynth scripts
|
||||||
|
* Copyright (C) 2008-2010 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------
|
||||||
|
* Inspired by the OctTree code by Jerry Huxtable and 0xdeadbeef, thank you.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "abstract_lists.h"
|
||||||
|
|
||||||
|
#define LEVELS 5
|
||||||
|
#define COLORS 254 /* One reserved for 100% transparent */
|
||||||
|
|
||||||
|
typedef struct hexnode_s hexnode_t;
|
||||||
|
struct hexnode_s
|
||||||
|
{
|
||||||
|
unsigned int v[4];
|
||||||
|
hexnode_t *nodes[16];
|
||||||
|
int children;
|
||||||
|
int leaf;
|
||||||
|
int count;
|
||||||
|
int index;
|
||||||
|
};
|
||||||
|
|
||||||
|
static hexnode_t *new_hexnode ()
|
||||||
|
{
|
||||||
|
hexnode_t *n = calloc(1, sizeof(hexnode_t));
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
n->v[i] = 0;
|
||||||
|
n->nodes[i] = NULL;
|
||||||
|
}
|
||||||
|
for (i = 4; i < 16; i++)
|
||||||
|
n->nodes[i] = NULL;
|
||||||
|
n->children = 0;
|
||||||
|
n->leaf = 0;
|
||||||
|
n->count = 0;
|
||||||
|
n->index = 0;
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tree_destroy (hexnode_t *n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 16; i++)
|
||||||
|
if (n->nodes[i] != NULL)
|
||||||
|
tree_destroy(n->nodes[i]);
|
||||||
|
|
||||||
|
free(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC_LIST(pal, hexnode_t)
|
||||||
|
|
||||||
|
typedef struct quantizer_s
|
||||||
|
{
|
||||||
|
hexnode_t *root;
|
||||||
|
pal_list_t *levels[LEVELS + 1];
|
||||||
|
int colors;
|
||||||
|
int nodes;
|
||||||
|
} quantizer_t;
|
||||||
|
|
||||||
|
static quantizer_t *new_quantizer ()
|
||||||
|
{
|
||||||
|
quantizer_t *q = calloc(sizeof(quantizer_t), 1);
|
||||||
|
int i;
|
||||||
|
|
||||||
|
q->root = new_hexnode();
|
||||||
|
for (i = 0; i <= LEVELS; i++)
|
||||||
|
q->levels[i] = pal_list_new();
|
||||||
|
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void destroy_quantizer (quantizer_t *q)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (q->root != NULL)
|
||||||
|
tree_destroy(q->root);
|
||||||
|
for (i = 0; i <= LEVELS; i++)
|
||||||
|
pal_list_destroy(q->levels[i]);
|
||||||
|
|
||||||
|
free(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int exec_find_node (hexnode_t *n, uint32_t color, hexnode_t **found, hexnode_t **last, int *index, int *level)
|
||||||
|
{
|
||||||
|
int pows[4] = {1, 2, 4, 8};
|
||||||
|
uint8_t *v = (uint8_t *)&color;
|
||||||
|
int i;
|
||||||
|
int idx;
|
||||||
|
|
||||||
|
for (; *level <= LEVELS; (*level)++)
|
||||||
|
{
|
||||||
|
idx = 0;
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
idx += pows[i] * !!(v[i] & (0x80 >> *level));
|
||||||
|
|
||||||
|
*last = n;
|
||||||
|
*index = idx;
|
||||||
|
if ((*found = n->nodes[idx]) == NULL)
|
||||||
|
return 0;
|
||||||
|
else if ((*found)->leaf)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
n = *found;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int find_node (hexnode_t *n, uint32_t color, hexnode_t **found, hexnode_t **last, int *index, int *level)
|
||||||
|
{
|
||||||
|
hexnode_t *f, *l;
|
||||||
|
int i, r;
|
||||||
|
int lv = level == NULL ? 0 : *level;
|
||||||
|
|
||||||
|
r = exec_find_node(n, color, &f, &l, &i, &lv);
|
||||||
|
if (found != NULL)
|
||||||
|
*found = f;
|
||||||
|
if (last != NULL)
|
||||||
|
*last = l;
|
||||||
|
if (index != NULL)
|
||||||
|
*index = i;
|
||||||
|
if (level != NULL)
|
||||||
|
*level = lv;
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_color_index (quantizer_t *q, uint32_t color)
|
||||||
|
{
|
||||||
|
hexnode_t *f, *l;
|
||||||
|
|
||||||
|
if (!color)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (find_node(q->root, color, &f, &l, NULL, NULL))
|
||||||
|
return f->index;
|
||||||
|
else
|
||||||
|
return l->index;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reduce (quantizer_t *q)
|
||||||
|
{
|
||||||
|
pal_list_t *l;
|
||||||
|
hexnode_t *n, *c;
|
||||||
|
int i, j, k;
|
||||||
|
|
||||||
|
if (q->colors <= COLORS)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (i = LEVELS - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
l = q->levels[i];
|
||||||
|
if (pal_list_empty(l))
|
||||||
|
continue;
|
||||||
|
n = pal_list_first(l);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (!n->children)
|
||||||
|
continue;
|
||||||
|
for (j = 0; j < 16; j++)
|
||||||
|
if ((c = n->nodes[j]) != NULL)
|
||||||
|
{
|
||||||
|
n->count += c->count;
|
||||||
|
/* UHD compliance on 32bit arch */
|
||||||
|
if (n->count >= 11480800)
|
||||||
|
{
|
||||||
|
n->count >>= 1;
|
||||||
|
for (k = 0; k < 4; k++)
|
||||||
|
n->v[k] >>= 1;
|
||||||
|
}
|
||||||
|
n->children--;
|
||||||
|
for (k = 0; k < 4; k++)
|
||||||
|
n->v[k] += c->v[k];
|
||||||
|
n->nodes[j] = NULL;
|
||||||
|
q->colors--;
|
||||||
|
q->nodes--;
|
||||||
|
pal_list_remove(q->levels[i+1], c);
|
||||||
|
tree_destroy(c);
|
||||||
|
}
|
||||||
|
n->leaf = 1;
|
||||||
|
q->colors++;
|
||||||
|
if (q->colors <= COLORS)
|
||||||
|
return;
|
||||||
|
} while ((n = pal_list_next(l)) != NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void insert_color (quantizer_t *q, uint32_t color)
|
||||||
|
{
|
||||||
|
uint8_t *v = (uint8_t *)&color;
|
||||||
|
hexnode_t *n = q->root;
|
||||||
|
hexnode_t *f, *l;
|
||||||
|
int i, j, level = 0;
|
||||||
|
|
||||||
|
/* 100% transparent pixels will be ignored */
|
||||||
|
if (!color)
|
||||||
|
return;
|
||||||
|
|
||||||
|
while (level <= LEVELS)
|
||||||
|
{
|
||||||
|
if (find_node(n, color, &f, &l, &i, &level))
|
||||||
|
{
|
||||||
|
f->count++;
|
||||||
|
for (j = 0; j < 4; j++)
|
||||||
|
f->v[j] += v[j];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
f = new_hexnode();
|
||||||
|
l->children++;
|
||||||
|
l->nodes[i] = f;
|
||||||
|
level++;
|
||||||
|
|
||||||
|
q->nodes++;
|
||||||
|
pal_list_insert(q->levels[level], f);
|
||||||
|
|
||||||
|
if (level == LEVELS)
|
||||||
|
{
|
||||||
|
f->leaf = 1;
|
||||||
|
f->count = 1;
|
||||||
|
for (j = 0; j < 4; j++)
|
||||||
|
f->v[j] = v[j];
|
||||||
|
q->colors++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (q->colors > COLORS)
|
||||||
|
reduce(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int recursive_get_palette (hexnode_t *n, uint32_t pal[COLORS + 1], int index)
|
||||||
|
{
|
||||||
|
uint8_t v[4];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (n->leaf)
|
||||||
|
{
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
v[i] = n->v[i] / n->count;
|
||||||
|
pal[n->index = index++] = *(uint32_t *)v;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
for (i = 0; i < 16; i++)
|
||||||
|
if (n->nodes[i] != NULL)
|
||||||
|
{
|
||||||
|
n->index = index;
|
||||||
|
index = recursive_get_palette(n->nodes[i], pal, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_palette (quantizer_t *q, uint32_t pal[COLORS + 1])
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
|
||||||
|
pal[0] = 0;
|
||||||
|
if (q->colors > COLORS)
|
||||||
|
reduce(q);
|
||||||
|
index = recursive_get_palette(q->root, pal, 1);
|
||||||
|
|
||||||
|
if (index <= COLORS && !pal[index])
|
||||||
|
pal[index] = 0xc0decafe;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t *palletize (uint8_t *im, int w, int h)
|
||||||
|
{
|
||||||
|
uint32_t *pal = calloc(256, sizeof(uint32_t));
|
||||||
|
uint32_t *i = (uint32_t *)im;
|
||||||
|
quantizer_t *q = new_quantizer();
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
for (y = 0; y < h; y++)
|
||||||
|
for (x = 0; x < w; x++)
|
||||||
|
insert_color(q, i[x + y * w]);
|
||||||
|
|
||||||
|
get_palette(q, pal);
|
||||||
|
|
||||||
|
for (y = 0; y < h; y++)
|
||||||
|
for (x = 0; x < w; x++)
|
||||||
|
im[x + y * w] = get_color_index(q, i[x + y * w]);
|
||||||
|
|
||||||
|
destroy_quantizer(q);
|
||||||
|
|
||||||
|
return pal;
|
||||||
|
}
|
||||||
|
|
26
palletize.h
Normal file
26
palletize.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* avs2bdnxml - Generates BluRay subtitle stuff from RGBA AviSynth scripts
|
||||||
|
* Copyright (C) 2008-2010 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef QUANTIZE_H
|
||||||
|
#define QUANTIZE_H
|
||||||
|
|
||||||
|
/* Return malloced palette and overwrite im with 8bpp data */
|
||||||
|
uint32_t *palletize (char *im, int w, int h);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
BIN
palletize.o
Normal file
BIN
palletize.o
Normal file
Binary file not shown.
144
sort.c
Normal file
144
sort.c
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* sort.c - Generic heapsort
|
||||||
|
* Copyright (C) 2011 Arne Bochem <heapsort at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Returns 1 if a > b, 0 otherwise. */
|
||||||
|
typedef int (*sort_func_t)(void *, void *);
|
||||||
|
|
||||||
|
static inline uint32_t depth_log (uint32_t i)
|
||||||
|
{
|
||||||
|
uint32_t j;
|
||||||
|
|
||||||
|
#if defined(__i386__)
|
||||||
|
asm __volatile__ ("bsr %1,%0\n":"=r"(j):"r"(i));
|
||||||
|
#else
|
||||||
|
j = 0;
|
||||||
|
while (i >>= 1)
|
||||||
|
j++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void swap (void **data, uint32_t i, uint32_t j)
|
||||||
|
{
|
||||||
|
static void *tmp;
|
||||||
|
|
||||||
|
tmp = data[i];
|
||||||
|
data[i] = data[j];
|
||||||
|
data[j] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void reheap (sort_func_t cmp, void **data, uint32_t k, uint32_t hs)
|
||||||
|
{
|
||||||
|
uint32_t max_son;
|
||||||
|
uint32_t l, r;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
if (hs <= (l = 2 * k + 1))
|
||||||
|
/* Return if k has no sons. */
|
||||||
|
return;
|
||||||
|
else
|
||||||
|
if (hs > (r = l + 1) && cmp(data[r], data[l]))
|
||||||
|
/* Set max_son to right son if k has a right son and it is greater than the left son. */
|
||||||
|
max_son = r;
|
||||||
|
else
|
||||||
|
/* If there is no right son, or it is smaller, set max_son to left son. */
|
||||||
|
max_son = l;
|
||||||
|
|
||||||
|
/* Return if k is greater than max_son. */
|
||||||
|
if (!cmp(data[max_son], data[k]))
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Otherwise swap k and max_son, and recurse. */
|
||||||
|
swap(data, k, max_son);
|
||||||
|
|
||||||
|
/* "Recurse." */
|
||||||
|
k = max_son;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bottom_up (sort_func_t cmp, void **data, uint32_t *path, uint32_t hs)
|
||||||
|
{
|
||||||
|
uint32_t depth = depth_log(hs);
|
||||||
|
uint32_t i, l, r;
|
||||||
|
uint32_t k = 0;
|
||||||
|
void *tmp;
|
||||||
|
|
||||||
|
/* Check if there's anything left. */
|
||||||
|
if (!depth)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (i = 1; i <= depth; i++)
|
||||||
|
{
|
||||||
|
/* Find maximum son. */
|
||||||
|
if (hs <= (l = 2 * k + 1))
|
||||||
|
{
|
||||||
|
/* Terminate path early, if no son found. */
|
||||||
|
depth--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* See reheap function. Get maximum son and add to path. */
|
||||||
|
if (hs > (r = l + 1) && cmp(data[r], data[l]))
|
||||||
|
k = path[i] = r;
|
||||||
|
else
|
||||||
|
k = path[i] = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find new position for heap root. */
|
||||||
|
for (i = depth; i > 0; i--)
|
||||||
|
if (cmp(data[path[i]], data[0]))
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Move values along path. */
|
||||||
|
tmp = data[0];
|
||||||
|
for (k = 1; k <= i; k++)
|
||||||
|
data[path[k - 1]] = data[path[k]];
|
||||||
|
data[path[i]] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort (sort_func_t cmp, void **data, uint32_t len)
|
||||||
|
{
|
||||||
|
uint32_t path[33]; /* Path of maximum sons. */
|
||||||
|
uint32_t hs = len; /* Heapsize. */
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Small arrays are always sorted. */
|
||||||
|
if (len < 2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Build heap. */
|
||||||
|
for (i = len / 2 - 1; i >= 0; i--)
|
||||||
|
reheap(cmp, data, i, hs);
|
||||||
|
|
||||||
|
/* Initialize start point of the path as the heap root. */
|
||||||
|
path[0] = 0;
|
||||||
|
|
||||||
|
while (hs >= 2)
|
||||||
|
{
|
||||||
|
/* Swap last item with root. */
|
||||||
|
swap(data, 0, hs - 1);
|
||||||
|
|
||||||
|
/* Rebuild heap structure. */
|
||||||
|
/*bottom_up(cmp, data, path, --hs);*/ /* Faster for bigger arrays. */
|
||||||
|
reheap(cmp, data, 0, --hs); /* Faster for smaller arrays, such as here. */
|
||||||
|
}
|
||||||
|
}
|
29
sort.h
Normal file
29
sort.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* sort.h - Generic heapsort header
|
||||||
|
* Copyright (C) 2011 Arne Bochem <heapsort at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef SORT_H
|
||||||
|
#define SORT_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* Returns 1 if a > b, 0 otherwise. */
|
||||||
|
typedef int (*sort_func_t)(void *, void *);
|
||||||
|
|
||||||
|
void sort (sort_func_t cmp, void **data, uint32_t len);
|
||||||
|
|
||||||
|
#endif
|
836
sup.c
Normal file
836
sup.c
Normal file
@ -0,0 +1,836 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* avs2bdnxml - Generates BluRay subtitle stuff from RGBA AviSynth scripts
|
||||||
|
* Copyright (C) 2008-2013 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "auto_split.h"
|
||||||
|
#include "sup.h"
|
||||||
|
#include "abstract_lists.h"
|
||||||
|
|
||||||
|
#ifdef BE_ARCH
|
||||||
|
#define SWAP32(x) (x)
|
||||||
|
#define SWAP16(x) (x)
|
||||||
|
#endif
|
||||||
|
#ifdef LE_ARCH
|
||||||
|
#define SWAP32(x) ((int32_t)(((x & 0xff000000) >> 24) | ((x & 0xff0000) >> 8) | ((x & 0xff00) << 8) | ((x & 0xff) << 24)))
|
||||||
|
#define SWAP16(x) ((int16_t)(((x & 0xff) << 8) | ((x & 0xff00) >> 8)))
|
||||||
|
#endif
|
||||||
|
#ifndef LE_ARCH
|
||||||
|
#ifndef BE_ARCH
|
||||||
|
#error "Please specify endian-ness with -DLE_ARCH or -DBE_ARCH."
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef DEBUG
|
||||||
|
#define DEBUG 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int count (uint8_t *im, int x, int w, int col)
|
||||||
|
{
|
||||||
|
int c = 0;
|
||||||
|
for (; x < w && im[x] == col && c <= 16383; x++) /* 14bit max */
|
||||||
|
c++;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Output buffer will contain malloced RLE data */
|
||||||
|
#define PUSH(x) {*(b++)=(x);(*len)++;}
|
||||||
|
#define FLAG_COLOR 0x80
|
||||||
|
#define FLAG_LONG 0x40
|
||||||
|
static uint8_t *rl_encode (uint8_t *im, int w, int h, rect_t crop, int *len)
|
||||||
|
{
|
||||||
|
uint8_t *b = calloc(crop.w * crop.h, 4); /* Over-allocation */
|
||||||
|
uint8_t *rle = b;
|
||||||
|
uint8_t col, o;
|
||||||
|
int x, y, c;
|
||||||
|
|
||||||
|
*len = 0;
|
||||||
|
for (y = crop.y; y < crop.y + crop.h && y < h; y++)
|
||||||
|
{
|
||||||
|
for (x = crop.x; x < crop.x + crop.w && x < w; x += c)
|
||||||
|
{
|
||||||
|
col = im[x + y * w];
|
||||||
|
c = count(im + y * w, x, MIN(crop.x + crop.w, w), col);
|
||||||
|
|
||||||
|
/* Shorter than shortest range encoding */
|
||||||
|
if (c < 3 && col)
|
||||||
|
{
|
||||||
|
for (o = 0; o < c; o++)
|
||||||
|
PUSH(col);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Range marker */
|
||||||
|
PUSH (0);
|
||||||
|
|
||||||
|
/* Set color flag, if necessary */
|
||||||
|
o = 0;
|
||||||
|
if (col)
|
||||||
|
o |= FLAG_COLOR;
|
||||||
|
|
||||||
|
/* Encode length */
|
||||||
|
if (c >= 64) /* 6bit max */
|
||||||
|
{
|
||||||
|
o |= FLAG_LONG;
|
||||||
|
o |= c >> 8;
|
||||||
|
PUSH(o);
|
||||||
|
PUSH(c & 0xFF);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
PUSH(o | c);
|
||||||
|
|
||||||
|
/* Output color, if necessary */
|
||||||
|
if (col)
|
||||||
|
PUSH(col);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* End of line marker */
|
||||||
|
PUSH(0);
|
||||||
|
PUSH(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rle;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_header_s
|
||||||
|
{
|
||||||
|
uint8_t m1; /* 'P' */
|
||||||
|
uint8_t m2; /* 'G' */
|
||||||
|
uint32_t start_time;
|
||||||
|
uint32_t dts;
|
||||||
|
uint8_t packet_type; /* 0x16 = pcs_start/end, 0x17 = wds, 0x14 = palette, 0x15 = ods_first, 0x80 = null */
|
||||||
|
uint16_t packet_len;
|
||||||
|
} __attribute ((packed)) sup_header_t;
|
||||||
|
|
||||||
|
void conv_sup_header (sup_header_t *h)
|
||||||
|
{
|
||||||
|
h->start_time = SWAP32(h->start_time);
|
||||||
|
h->dts = SWAP32(h->dts);
|
||||||
|
h->packet_len = SWAP16(h->packet_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_header (FILE *fh, int start_time, int dts, int packet_type, int packet_len)
|
||||||
|
{
|
||||||
|
sup_header_t h;
|
||||||
|
|
||||||
|
h.m1 = 80; h.m2 = 71;
|
||||||
|
h.start_time = start_time;
|
||||||
|
h.dts = dts;
|
||||||
|
h.packet_type = packet_type;
|
||||||
|
h.packet_len = packet_len;
|
||||||
|
|
||||||
|
conv_sup_header(&h);
|
||||||
|
fwrite(&h, sizeof(h), 1, fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_pcs_start_s
|
||||||
|
{
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height; /* height - 2 * Core.getCropOfsY */
|
||||||
|
uint8_t fps_id; /* getFpsId() */
|
||||||
|
uint16_t comp_num;
|
||||||
|
uint8_t follower; /* 0x80 if first or single, 0x40 if follows directly (end = start) or the frame after */
|
||||||
|
uint16_t m; /* 0 */
|
||||||
|
uint8_t objects; /* 1 */
|
||||||
|
} __attribute ((packed)) sup_pcs_start_t;
|
||||||
|
|
||||||
|
void conv_sup_pcs_start (sup_pcs_start_t *pcss)
|
||||||
|
{
|
||||||
|
pcss->width = SWAP16(pcss->width);
|
||||||
|
pcss->height = SWAP16(pcss->height);
|
||||||
|
pcss->comp_num = SWAP16(pcss->comp_num);
|
||||||
|
pcss->m = SWAP16(pcss->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_pcs_start_obj_s
|
||||||
|
{
|
||||||
|
uint16_t picture;
|
||||||
|
uint8_t window;
|
||||||
|
uint8_t forced; /* forced ? 64 : 0 */
|
||||||
|
uint16_t x_off;
|
||||||
|
uint16_t y_off;
|
||||||
|
} __attribute ((packed)) sup_pcs_start_obj_t;
|
||||||
|
|
||||||
|
void conv_sup_pcs_start_obj (sup_pcs_start_obj_t *pcsso)
|
||||||
|
{
|
||||||
|
pcsso->picture = SWAP16(pcsso->picture);
|
||||||
|
pcsso->x_off = SWAP16(pcsso->x_off);
|
||||||
|
pcsso->y_off = SWAP16(pcsso->y_off);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_pcs_start (FILE *fh, int start_time, int dts, int follower, int objects, int vid_w, int vid_h, int fps_id, int comp_num)
|
||||||
|
{
|
||||||
|
sup_pcs_start_t pcss;
|
||||||
|
|
||||||
|
write_header(fh, start_time, dts, 22, sizeof(pcss) + objects * sizeof(sup_pcs_start_obj_t));
|
||||||
|
|
||||||
|
pcss.m = 0;
|
||||||
|
pcss.width = vid_w;
|
||||||
|
pcss.height = vid_h;
|
||||||
|
pcss.fps_id = fps_id;
|
||||||
|
pcss.comp_num = comp_num;
|
||||||
|
pcss.follower = !follower ? 0x80 : 0x40; /* 0x80 for single lines, and first lines, 0x40 for following directly or with one frame between */
|
||||||
|
pcss.objects = objects;
|
||||||
|
|
||||||
|
conv_sup_pcs_start(&pcss);
|
||||||
|
fwrite(&pcss, sizeof(pcss), 1, fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_pcs_start_obj (FILE *fh, int picture, int window, int x_off, int y_off)
|
||||||
|
{
|
||||||
|
sup_pcs_start_obj_t pcsso;
|
||||||
|
|
||||||
|
pcsso.picture = picture;
|
||||||
|
pcsso.window = window;
|
||||||
|
pcsso.forced = 0;
|
||||||
|
pcsso.x_off = x_off;
|
||||||
|
pcsso.y_off = y_off;
|
||||||
|
|
||||||
|
conv_sup_pcs_start_obj(&pcsso);
|
||||||
|
fwrite(&pcsso, sizeof(pcsso), 1, fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_wds_s
|
||||||
|
{
|
||||||
|
uint8_t windows; /* 1 or 2 */
|
||||||
|
} __attribute ((packed)) sup_wds_t;
|
||||||
|
|
||||||
|
void conv_sup_wds (sup_wds_t *wds)
|
||||||
|
{
|
||||||
|
/* Do nothing. */
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_wds_obj_s
|
||||||
|
{
|
||||||
|
uint8_t window; /* 0 or 1 */
|
||||||
|
uint16_t x_off;
|
||||||
|
uint16_t y_off;
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height;
|
||||||
|
} __attribute ((packed)) sup_wds_obj_t;
|
||||||
|
|
||||||
|
void conv_sup_wds_obj (sup_wds_obj_t *wdso)
|
||||||
|
{
|
||||||
|
wdso->x_off = SWAP16(wdso->x_off);
|
||||||
|
wdso->y_off = SWAP16(wdso->y_off);
|
||||||
|
wdso->width = SWAP16(wdso->width);
|
||||||
|
wdso->height = SWAP16(wdso->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_wds (FILE *fh, int timestamp, int dts, int windows)
|
||||||
|
{
|
||||||
|
sup_wds_t wds;
|
||||||
|
|
||||||
|
write_header(fh, timestamp, dts, 23, sizeof(wds) + windows * sizeof(sup_wds_obj_t));
|
||||||
|
|
||||||
|
wds.windows = windows;
|
||||||
|
|
||||||
|
conv_sup_wds(&wds);
|
||||||
|
fwrite(&wds, sizeof(wds), 1, fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_wds_obj (FILE *fh, int window, int w, int h, int x_off, int y_off)
|
||||||
|
{
|
||||||
|
sup_wds_obj_t wdso;
|
||||||
|
|
||||||
|
wdso.window = window;
|
||||||
|
wdso.x_off = x_off;
|
||||||
|
wdso.y_off = y_off;
|
||||||
|
wdso.width = w;
|
||||||
|
wdso.height = h;
|
||||||
|
|
||||||
|
conv_sup_wds_obj(&wdso);
|
||||||
|
fwrite(&wdso, sizeof(wdso), 1, fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CLAMP(x,min,max) (MAX(MIN(x,max),min))
|
||||||
|
static uint8_t get_y (uint32_t c, int s)
|
||||||
|
{
|
||||||
|
uint8_t *v = (uint8_t *)&c;
|
||||||
|
uint8_t r = v[0], g = v[1], b = v[2];
|
||||||
|
if (s)
|
||||||
|
return (uint8_t)CLAMP(16 + (int)floor(0.5 + (double)(r * 0.299 * 219.0 / 255.0 + g * 0.587 * 219.0 / 255.0 + b * 0.114 * 219.0 / 255.0)), 16, 235);
|
||||||
|
else
|
||||||
|
return (uint8_t)CLAMP(16 + (int)floor(0.5 + (double)(r * 0.2126 * 219.0 / 255.0 + g * 0.7152 * 219.0 / 255.0 + b * 0.0722 * 219.0 / 255.0)), 16, 235);
|
||||||
|
}
|
||||||
|
static uint8_t get_u (uint32_t c, int s)
|
||||||
|
{
|
||||||
|
uint8_t *v = (uint8_t *)&c;
|
||||||
|
uint8_t r = v[0], g = v[1], b = v[2];
|
||||||
|
if (s)
|
||||||
|
return (uint8_t)CLAMP(128 + (int)floor(0.5 + (double)(r * 0.5 * 224.0 / 255.0 - (g * 0.418688 * 224.0 / 255.0) - (b * 0.081312 * 224.0 / 255.0))), 16, 240);
|
||||||
|
else
|
||||||
|
return (uint8_t)CLAMP(128 + (int)floor(0.5 + (double)(r * 0.5 * 224.0 / 255.0 - (g * 0.7152 / 1.5748 * 224.0 / 255.0) - (b * 0.0722 / 1.5748 * 224.0 / 255.0))), 16, 240);
|
||||||
|
}
|
||||||
|
static uint8_t get_v (uint32_t c, int s)
|
||||||
|
{
|
||||||
|
uint8_t *v = (uint8_t *)&c;
|
||||||
|
uint8_t r = v[0], g = v[1], b = v[2];
|
||||||
|
if (s)
|
||||||
|
return (uint8_t)CLAMP(128 + (int)floor(0.5 + (double)(-r * 0.168736 * 224.0 / 255.0 - (g * 0.331264 * 224.0 / 255.0) + b * 0.5 * 224.0 / 255.0)), 16, 240);
|
||||||
|
else
|
||||||
|
return (uint8_t)CLAMP(128 + (int)floor(0.5 + (double)(-r * 0.2126 / 1.8556 * 224.0 / 255.0 - (g * 0.7152 / 1.8556 * 224.0 / 255.0) + b * 0.5 * 224.0 / 255.0)), 16, 240);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_palette_s
|
||||||
|
{
|
||||||
|
uint16_t palette;
|
||||||
|
} __attribute ((packed)) sup_palette_t;
|
||||||
|
|
||||||
|
void conv_sup_palette (sup_palette_t *p)
|
||||||
|
{
|
||||||
|
p->palette = SWAP16(p->palette);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Colorspace = 1 for 480p/576p, 0 otherwise */
|
||||||
|
#define PUT(x) { t = (uint8_t)(x); fwrite(&t, 1, 1, fh); }
|
||||||
|
static void write_palette (FILE *fh, int dts, int palette, uint32_t *pal, int colorspace)
|
||||||
|
{
|
||||||
|
sup_palette_t p;
|
||||||
|
int entries = 1, i;
|
||||||
|
uint8_t t;
|
||||||
|
|
||||||
|
for (i = 1; i < 256 && pal[i]; i++)
|
||||||
|
entries++;
|
||||||
|
write_header(fh, dts, 0, 20, sizeof(p) + entries * 5);
|
||||||
|
|
||||||
|
p.palette = palette;
|
||||||
|
conv_sup_palette(&p);
|
||||||
|
fwrite(&p, sizeof(p), 1, fh);
|
||||||
|
|
||||||
|
for (i = 0; i < entries; i++)
|
||||||
|
{
|
||||||
|
PUT(i)
|
||||||
|
PUT(get_y(pal[i], colorspace))
|
||||||
|
PUT(get_u(pal[i], colorspace))
|
||||||
|
PUT(get_v(pal[i], colorspace))
|
||||||
|
PUT(((uint8_t *)&(pal[i]))[3])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_ods_first_s
|
||||||
|
{
|
||||||
|
uint16_t picture;
|
||||||
|
uint8_t m; /* 0 */
|
||||||
|
uint32_t magic_len; /* (single_packet ? 0xc0000000 : 0x80000000) | (length + 4) */
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height;
|
||||||
|
} __attribute ((packed)) sup_ods_first_t;
|
||||||
|
|
||||||
|
void conv_sup_ods_first (sup_ods_first_t *odsf)
|
||||||
|
{
|
||||||
|
odsf->picture = SWAP16(odsf->picture);
|
||||||
|
odsf->magic_len = SWAP32(odsf->magic_len);
|
||||||
|
odsf->width = SWAP16(odsf->width);
|
||||||
|
odsf->height = SWAP16(odsf->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_ods_next_s
|
||||||
|
{
|
||||||
|
uint16_t picture;
|
||||||
|
uint8_t m; /* 0 */
|
||||||
|
uint8_t last; /* 0 if not, if 64 yes */
|
||||||
|
} __attribute ((packed)) sup_ods_next_t;
|
||||||
|
|
||||||
|
void conv_sup_ods_next (sup_ods_next_t *odsn)
|
||||||
|
{
|
||||||
|
odsn->picture = SWAP16(odsn->picture);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_image (FILE *fh, int timestamp, int dts, int picture, int w, int h, uint8_t *rle, int rle_len)
|
||||||
|
{
|
||||||
|
sup_ods_first_t odsf;
|
||||||
|
sup_ods_next_t odsn = {picture, 0, 0};
|
||||||
|
int length = 0x80000000 | (rle_len + 4);
|
||||||
|
int size;
|
||||||
|
|
||||||
|
if (rle_len > 65508)
|
||||||
|
size = 65508;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
size = rle_len;
|
||||||
|
length |= 0x40000000;
|
||||||
|
}
|
||||||
|
rle_len -= size;
|
||||||
|
|
||||||
|
write_header(fh, timestamp, dts, 21, sizeof(odsf) + size);
|
||||||
|
|
||||||
|
odsf.picture = picture;
|
||||||
|
odsf.m = 0;
|
||||||
|
odsf.magic_len = length;
|
||||||
|
odsf.width = w;
|
||||||
|
odsf.height = h;
|
||||||
|
|
||||||
|
conv_sup_ods_first(&odsf);
|
||||||
|
fwrite(&odsf, sizeof(odsf), 1, fh);
|
||||||
|
fwrite(rle, size, 1, fh);
|
||||||
|
rle += size;
|
||||||
|
|
||||||
|
while (rle_len)
|
||||||
|
{
|
||||||
|
if (rle_len > 65515)
|
||||||
|
size = 65515;
|
||||||
|
else
|
||||||
|
size = rle_len;
|
||||||
|
rle_len -= size;
|
||||||
|
|
||||||
|
if (!rle_len)
|
||||||
|
odsn.last = 64;
|
||||||
|
|
||||||
|
write_header(fh, timestamp, dts, 21, sizeof(odsn) + size);
|
||||||
|
conv_sup_ods_next(&odsn);
|
||||||
|
fwrite(&odsn, sizeof(odsn), 1, fh);
|
||||||
|
fwrite(rle, size, 1, fh);
|
||||||
|
rle += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_marker (FILE *fh, int time)
|
||||||
|
{
|
||||||
|
write_header(fh, time, 0, 0x80, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct sup_pcs_end_s
|
||||||
|
{
|
||||||
|
uint16_t width;
|
||||||
|
uint16_t height;
|
||||||
|
uint8_t fps_id;
|
||||||
|
uint16_t comp_num;
|
||||||
|
uint32_t m;
|
||||||
|
} __attribute ((packed)) sup_pcs_end_t;
|
||||||
|
|
||||||
|
void conv_sup_pcs_end (sup_pcs_end_t *pcse)
|
||||||
|
{
|
||||||
|
pcse->width = SWAP16(pcse->width);
|
||||||
|
pcse->height = SWAP16(pcse->height);
|
||||||
|
pcse->comp_num = SWAP16(pcse->comp_num);
|
||||||
|
pcse->m = SWAP32(pcse->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_pcs_end (FILE *fh, int end_time, int dts, int w, int h, int fps_id, int comp_num)
|
||||||
|
{
|
||||||
|
sup_pcs_end_t pcse;
|
||||||
|
|
||||||
|
write_header(fh, end_time, dts, 22, sizeof(pcse));
|
||||||
|
|
||||||
|
pcse.width = w;
|
||||||
|
pcse.height = h;
|
||||||
|
pcse.fps_id = fps_id;
|
||||||
|
pcse.comp_num = comp_num;
|
||||||
|
pcse.m = 0;
|
||||||
|
|
||||||
|
conv_sup_pcs_end(&pcse);
|
||||||
|
fwrite(&pcse, sizeof(pcse), 1, fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct fps_id_s
|
||||||
|
{
|
||||||
|
int num;
|
||||||
|
int den;
|
||||||
|
int id;
|
||||||
|
} fps_id_t;
|
||||||
|
|
||||||
|
static int get_id (int fps_num, int fps_den)
|
||||||
|
{
|
||||||
|
fps_id_t ids[] = {{24000, 1001, 16}, {24, 1, 32}, {25, 1, 48}, {30000, 1001, 64}, {50, 1, 96}, {60000, 1001, 112}, {0, 0, 0}};
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (ids[i].den)
|
||||||
|
{
|
||||||
|
if (ids[i].num == fps_num && ids[i].den == fps_den)
|
||||||
|
return ids[i].id;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
sup_writer_t *new_sup_writer (char *filename, int im_w, int im_h, int fps_num, int fps_den)
|
||||||
|
{
|
||||||
|
sup_writer_t *sw = malloc(sizeof(sup_writer_t));
|
||||||
|
|
||||||
|
if ((sw->fh = fopen(filename, "wb")) == NULL)
|
||||||
|
{
|
||||||
|
perror("Error opening output SUP/PGS file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
sw->non_new = 0;
|
||||||
|
sw->im_w = im_w;
|
||||||
|
sw->im_h = im_h;
|
||||||
|
|
||||||
|
if (im_h == 480 || im_h == 576)
|
||||||
|
sw->colorspace = 1;
|
||||||
|
else
|
||||||
|
sw->colorspace = 0;
|
||||||
|
|
||||||
|
sw->fps_num = fps_num;
|
||||||
|
sw->fps_den = fps_den;
|
||||||
|
sw->fps_id = get_id(fps_num, fps_den);
|
||||||
|
sw->comp_num = 0;
|
||||||
|
sw->end = -2;
|
||||||
|
sw->follower_end = -2;
|
||||||
|
sw->objects = 0;
|
||||||
|
sw->palettes = 0;
|
||||||
|
sw->buffer = 0;
|
||||||
|
sw->palette_offset = 0;
|
||||||
|
sw->picture_offset = 0;
|
||||||
|
sw->last_end_ts = 0;
|
||||||
|
sw->last_window_ts = 0;
|
||||||
|
sw->window_num = 0;
|
||||||
|
sw->sil = si_list_new();
|
||||||
|
|
||||||
|
memset(sw->windows, 0, 2 * sizeof(rect_t));
|
||||||
|
|
||||||
|
return sw;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_si (subtitle_info_t *si)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < si->num_crop; i++)
|
||||||
|
free(si->rle[i]);
|
||||||
|
|
||||||
|
free(si);
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_subtitle (sup_writer_t *sw, uint8_t **rle, int *rle_len, int num_crop, rect_t *crops, uint32_t *pal, int start, int end, int new_composition)
|
||||||
|
{
|
||||||
|
uint32_t frame_ts, window_ts, decode_ts;
|
||||||
|
uint32_t window_ts_list[2], decode_ts_list[2];
|
||||||
|
uint32_t later_window;
|
||||||
|
int in_window[2];
|
||||||
|
uint32_t dts;
|
||||||
|
uint32_t start_ts, end_ts, ts;
|
||||||
|
int follower = 0;
|
||||||
|
uint32_t im_ts = 0;
|
||||||
|
int i, j;
|
||||||
|
double tick_fac = 90000;
|
||||||
|
|
||||||
|
/* For stuff following frame by frame (endts = startts):
|
||||||
|
* - Write: PCSS, WDS, PAL, IMG, MARK, [PCSS, WDS, PAL, IMG, MARK...], PCSE, WDS, MARK
|
||||||
|
*
|
||||||
|
* Behaviour in reference file:
|
||||||
|
* During an epoch of followers, if number of objects/position/size changes, increase
|
||||||
|
* composition by one, increase all further picture values by previous number of
|
||||||
|
* composition objects, increase palette by one. Object id in PCSS is 1 for all.
|
||||||
|
* Do some stuff to WDS too. Keep it to first value, if stuff still fits in the window?
|
||||||
|
* It seems WDS has to stay constant within a single composition. What a pain.
|
||||||
|
*/
|
||||||
|
tick_fac *= ((double)sw->fps_den) / ((double)sw->fps_num);
|
||||||
|
|
||||||
|
start_ts = (int)floor((double)start * tick_fac + 0.5);
|
||||||
|
end_ts = (int)floor((double)end * tick_fac + 0.5);
|
||||||
|
|
||||||
|
/* Calculate some timestamps/modifiers */
|
||||||
|
frame_ts = (sw->im_w * sw->im_h * 9 + 3199) / 3200;
|
||||||
|
window_ts = 0;
|
||||||
|
|
||||||
|
if (sw->non_new && ((start == sw->follower_end) || (start == sw->follower_end + 1)))
|
||||||
|
follower = 1;
|
||||||
|
sw->follower_end = end;
|
||||||
|
|
||||||
|
for (i = 0; i < sw->window_num; i++)
|
||||||
|
{
|
||||||
|
window_ts_list[i] = (sw->windows[i].w * sw->windows[i].h * 9 + 3199) / 3200;
|
||||||
|
window_ts += window_ts_list[i];
|
||||||
|
}
|
||||||
|
decode_ts = 0;
|
||||||
|
for (i = 0; i < num_crop; i++)
|
||||||
|
{
|
||||||
|
decode_ts_list[i] = (crops[i].w * crops[i].h * 9 + 1599) / 1600;
|
||||||
|
decode_ts += decode_ts_list[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sw->window_num > 1)
|
||||||
|
later_window = window_ts_list[1];
|
||||||
|
else
|
||||||
|
later_window = window_ts_list[0];
|
||||||
|
|
||||||
|
/* Calculate dts */
|
||||||
|
if (num_crop == 1)
|
||||||
|
{
|
||||||
|
if (new_composition)
|
||||||
|
dts = start_ts - frame_ts - window_ts;
|
||||||
|
else
|
||||||
|
dts = start_ts - window_ts - decode_ts;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (new_composition)
|
||||||
|
dts = start_ts - frame_ts - window_ts;
|
||||||
|
else
|
||||||
|
dts = start_ts - decode_ts - later_window;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Determine windows */
|
||||||
|
for (i = 0; i < num_crop; i++)
|
||||||
|
for (j = 0; j < sw->window_num; j++)
|
||||||
|
if (crops[i].x >= sw->windows[j].x && crops[i].x + crops[i].w <= sw->windows[j].x + sw->windows[j].w && crops[i].y >= sw->windows[j].y && crops[i].y + crops[i].h <= sw->windows[j].y + sw->windows[j].h)
|
||||||
|
{
|
||||||
|
in_window[i] = j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write PCSS */
|
||||||
|
write_pcs_start(sw->fh, start_ts, dts, follower, num_crop, sw->im_w, sw->im_h, sw->fps_id, sw->comp_num);
|
||||||
|
for (i = 0; i < num_crop; i++)
|
||||||
|
write_pcs_start_obj(sw->fh, sw->picture_offset + i, in_window[i], crops[i].x, crops[i].y);
|
||||||
|
|
||||||
|
/* Write WDS */
|
||||||
|
ts = start_ts - window_ts; /* Can be very slightly off, possible rounding error (FIXME: fixed?) */
|
||||||
|
write_wds(sw->fh, ts, dts, sw->window_num);
|
||||||
|
for (i = 0; i < sw->window_num; i++)
|
||||||
|
write_wds_obj(sw->fh, i, sw->windows[i].w, sw->windows[i].h, sw->windows[i].x, sw->windows[i].y);
|
||||||
|
|
||||||
|
/* Write palette */
|
||||||
|
write_palette(sw->fh, dts, sw->palette_offset, pal, sw->colorspace);
|
||||||
|
|
||||||
|
/* Write image data */
|
||||||
|
for (i = 0; i < num_crop; i++)
|
||||||
|
{
|
||||||
|
if (num_crop == 1)
|
||||||
|
{
|
||||||
|
if (new_composition)
|
||||||
|
im_ts = start_ts - frame_ts + window_ts; /* This one can be off a bit. (FIXME) */
|
||||||
|
else
|
||||||
|
im_ts = start_ts - window_ts;
|
||||||
|
}
|
||||||
|
else if (i == 0)
|
||||||
|
{
|
||||||
|
if (new_composition)
|
||||||
|
im_ts = start_ts - frame_ts + window_ts_list[0] - later_window; /* This one can be off a bit, ~5/90000s was observed (FIXME: fixed?) */
|
||||||
|
else
|
||||||
|
im_ts = start_ts - later_window - decode_ts_list[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (new_composition)
|
||||||
|
{
|
||||||
|
im_ts = start_ts - frame_ts + window_ts; /* Can be slightly off, possible rounding error (FIXME: fixed?) */
|
||||||
|
dts = start_ts - frame_ts + window_ts_list[0] - later_window;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
im_ts = start_ts - later_window;
|
||||||
|
dts = start_ts - later_window - decode_ts_list[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write_image(sw->fh, im_ts, dts, sw->picture_offset + i, crops[i].w, crops[i].h, rle[i], rle_len[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write marker */
|
||||||
|
write_marker(sw->fh, im_ts);
|
||||||
|
|
||||||
|
/* Remember data for creation of composition end */
|
||||||
|
sw->last_end_ts = end_ts;
|
||||||
|
sw->last_window_ts = window_ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_composition (sup_writer_t *sw)
|
||||||
|
{
|
||||||
|
rect_t *rects;
|
||||||
|
subtitle_info_t *si;
|
||||||
|
int last_num_crop = 0;
|
||||||
|
rect_t last_crops[2];
|
||||||
|
int new_composition = 1;
|
||||||
|
int si_rects = 0;
|
||||||
|
int ts, dts;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Only write anything if there is a non-empty composition */
|
||||||
|
if (!sw->non_new)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Count subtitles */
|
||||||
|
si = si_list_first(sw->sil);
|
||||||
|
while (si != NULL)
|
||||||
|
{
|
||||||
|
si_rects += si->num_crop;
|
||||||
|
si = si_list_next(sw->sil);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Gather crop rects. */
|
||||||
|
rects = malloc(si_rects * sizeof(rect_t));
|
||||||
|
si = si_list_first(sw->sil);
|
||||||
|
si_rects = 0;
|
||||||
|
while (si != NULL)
|
||||||
|
{
|
||||||
|
for (i = 0; i < si->num_crop; i++)
|
||||||
|
rects[si_rects++] = si->crops[i];
|
||||||
|
si = si_list_next(sw->sil);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Calculate windows */
|
||||||
|
sw->window_num = find_windows(rects, si_rects, sw->windows);
|
||||||
|
|
||||||
|
if (!sw->window_num)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Warning: WDS failure, skipping.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write subtitles */
|
||||||
|
si = si_list_first(sw->sil);
|
||||||
|
while (si != NULL)
|
||||||
|
{
|
||||||
|
if (!new_composition && (last_num_crop != si->num_crop || memcmp(last_crops, si->crops, MIN(last_num_crop, si->num_crop) * sizeof(rect_t))))
|
||||||
|
{
|
||||||
|
(sw->comp_num)++;
|
||||||
|
(sw->palette_offset)++;
|
||||||
|
(sw->picture_offset) += last_num_crop;
|
||||||
|
}
|
||||||
|
last_num_crop = si->num_crop;
|
||||||
|
memcpy(last_crops, si->crops, si->num_crop * sizeof(rect_t));
|
||||||
|
write_subtitle(sw, si->rle, si->rle_len, si->num_crop, si->crops, si->pal, si->start, si->end, new_composition);
|
||||||
|
new_composition = 0;
|
||||||
|
si_list_delete(sw->sil);
|
||||||
|
destroy_si(si);
|
||||||
|
si = si_list_get(sw->sil);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write PCSE */
|
||||||
|
dts = sw->last_end_ts - sw->last_window_ts - 1;
|
||||||
|
write_pcs_end(sw->fh, sw->last_end_ts, dts, sw->im_w, sw->im_h, sw->fps_id, ++(sw->comp_num));
|
||||||
|
|
||||||
|
/* Write WDS */
|
||||||
|
ts = sw->last_end_ts - sw->last_window_ts;
|
||||||
|
write_wds(sw->fh, ts, dts, sw->window_num);
|
||||||
|
for (i = 0; i < sw->window_num; i++)
|
||||||
|
write_wds_obj(sw->fh, i, sw->windows[i].w, sw->windows[i].h, sw->windows[i].x, sw->windows[i].y);
|
||||||
|
|
||||||
|
/* Write marker */
|
||||||
|
write_marker(sw->fh, dts);
|
||||||
|
|
||||||
|
/* Cleanup */
|
||||||
|
free(rects);
|
||||||
|
|
||||||
|
/* New composition */
|
||||||
|
(sw->comp_num)++;
|
||||||
|
|
||||||
|
/* Reset picture and palette count, new buffer. */
|
||||||
|
sw->objects = 0;
|
||||||
|
sw->palettes = 0;
|
||||||
|
sw->palette_offset = 0;
|
||||||
|
sw->picture_offset = 0;
|
||||||
|
sw->buffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
subtitle_info_t *collect_si (sup_writer_t *sw, uint8_t *im, int num_crop, rect_t *crops, uint32_t *pal, int start, int end)
|
||||||
|
{
|
||||||
|
subtitle_info_t *si = malloc(sizeof(subtitle_info_t));
|
||||||
|
int i;
|
||||||
|
|
||||||
|
si->start = start;
|
||||||
|
si->end = end;
|
||||||
|
si->num_crop = num_crop;
|
||||||
|
for (i = 0; i < num_crop; i++)
|
||||||
|
{
|
||||||
|
si->crops[i].w = crops[i].w;
|
||||||
|
si->crops[i].h = crops[i].h;
|
||||||
|
si->crops[i].x = crops[i].x;
|
||||||
|
si->crops[i].y = crops[i].y;
|
||||||
|
si->rle[i] = rl_encode(im, sw->im_w, sw->im_h, si->crops[i], &(si->rle_len[i]));
|
||||||
|
}
|
||||||
|
memcpy(si->pal, pal, 256 * sizeof(uint32_t));
|
||||||
|
|
||||||
|
return si;
|
||||||
|
}
|
||||||
|
|
||||||
|
void close_sup_writer (sup_writer_t *sw)
|
||||||
|
{
|
||||||
|
write_composition(sw);
|
||||||
|
|
||||||
|
si_list_first(sw->sil);
|
||||||
|
while(!si_list_empty(sw->sil))
|
||||||
|
{
|
||||||
|
destroy_si(si_list_first(sw->sil));
|
||||||
|
si_list_delete(sw->sil);
|
||||||
|
}
|
||||||
|
si_list_destroy(sw->sil);
|
||||||
|
|
||||||
|
fclose(sw->fh);
|
||||||
|
free(sw);
|
||||||
|
}
|
||||||
|
|
||||||
|
IMPLEMENT_LIST(si, subtitle_info_t)
|
||||||
|
|
||||||
|
void write_sup (sup_writer_t *sw, uint8_t *im, int num_crop, rect_t *crops, uint32_t *pal, int start, int end, int strict)
|
||||||
|
{
|
||||||
|
rect_t tmp;
|
||||||
|
int buffer_increase;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
buffer_increase = 0;
|
||||||
|
for (i = 0; i < num_crop; i++)
|
||||||
|
buffer_increase += crops[i].w * crops[i].h + 16;
|
||||||
|
/* Disabled some conditions for now. */
|
||||||
|
if (sw->non_new && ((start > sw->end + 1) || (sw->objects + num_crop > 64) || (strict && ((sw->buffer + buffer_increase >= 4 * 1024 * 1024) || (sw->palettes + 1 > 8)))))
|
||||||
|
{
|
||||||
|
# if DEBUG != 0
|
||||||
|
# warning "DEBUG enabled."
|
||||||
|
printf("Starting new composition ");
|
||||||
|
if (start > sw->end)
|
||||||
|
printf("due to time difference. %u > %u + 1\n", start, sw->end);
|
||||||
|
else if (sw->buffer + buffer_increase >= 4 * 1024 * 1024)
|
||||||
|
printf("due to buffer overflow. %u + %u = %u > %u\n", sw->buffer, buffer_increase, sw->buffer + buffer_increase, 4*1024*1024);
|
||||||
|
else if (sw->objects + num_crop > 64)
|
||||||
|
printf("due to number of composition objects. %u + %u = %u > %u\n", sw->objects, num_crop, sw->picture + num_crop, 64);
|
||||||
|
else if (sw->palette + 1 > 8)
|
||||||
|
printf("due to number of palettes. %u + %u = %u > %u\n", sw->palette, 1, sw->palette + 1, 8);
|
||||||
|
else
|
||||||
|
printf("for unknown reasons.\n");
|
||||||
|
# else
|
||||||
|
if (strict)
|
||||||
|
{
|
||||||
|
if (sw->buffer + buffer_increase >= 4 * 1024 * 1024)
|
||||||
|
{
|
||||||
|
printf("Warning: Starting new epoch due to buffer overflow (%u -> %u > %u) for event starting at frame %u (including offsets) in stricter mode.\n", sw->buffer, sw->buffer + buffer_increase, 4 * 1024 * 1024, start);
|
||||||
|
}
|
||||||
|
else if (sw->palettes + 1 > 8)
|
||||||
|
{
|
||||||
|
printf("Warning: Starting new epoch due to too many palettes for event starting at frame %u (including offsets) in stricter mode.\n", start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
write_composition(sw);
|
||||||
|
}
|
||||||
|
sw->non_new = 1;
|
||||||
|
sw->end = end;
|
||||||
|
sw->buffer += buffer_increase;
|
||||||
|
sw->objects += num_crop;
|
||||||
|
(sw->palettes)++; /* FIXME: It's probably okay not to increase this if the palette is identical to the last, but this would require further testing and possibly additional code so identical palettes are not written to the stream multiple times? */
|
||||||
|
|
||||||
|
if (num_crop > 1)
|
||||||
|
{
|
||||||
|
/* Let's order them, so the one closer to 0/0 is the second. */
|
||||||
|
if (crops[0].y < crops[1].y || (crops[0].y == crops[1].y && crops[0].x < crops[1].x))
|
||||||
|
{
|
||||||
|
tmp = crops[0];
|
||||||
|
crops[0] = crops[1];
|
||||||
|
crops[1] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
si_list_insert_after(sw->sil, collect_si(sw, im, num_crop, crops, pal, start, end));
|
||||||
|
}
|
||||||
|
|
73
sup.h
Normal file
73
sup.h
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
* avs2bdnxml - Generates BluRay subtitle stuff from RGBA AviSynth scripts
|
||||||
|
* Copyright (C) 2008-2010 Arne Bochem <avs2bdnxml at ps-auxw de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef SUP_H
|
||||||
|
#define SUP_H
|
||||||
|
|
||||||
|
#include "auto_split.h"
|
||||||
|
#include "abstract_lists.h"
|
||||||
|
|
||||||
|
typedef struct subtitle_info_s
|
||||||
|
{
|
||||||
|
int start;
|
||||||
|
int end;
|
||||||
|
int num_crop;
|
||||||
|
rect_t crops[2];
|
||||||
|
int rle_len[2];
|
||||||
|
uint8_t *rle[2];
|
||||||
|
uint32_t pal[256];
|
||||||
|
} subtitle_info_t;
|
||||||
|
|
||||||
|
DECLARE_LIST(si, subtitle_info_t)
|
||||||
|
|
||||||
|
typedef struct sup_writer_s
|
||||||
|
{
|
||||||
|
FILE *fh;
|
||||||
|
int non_new;
|
||||||
|
int im_w;
|
||||||
|
int im_h;
|
||||||
|
int colorspace;
|
||||||
|
int fps_num;
|
||||||
|
int fps_den;
|
||||||
|
int fps_id;
|
||||||
|
uint16_t comp_num;
|
||||||
|
unsigned int end;
|
||||||
|
unsigned int follower_end;
|
||||||
|
int buffer;
|
||||||
|
int objects;
|
||||||
|
int palettes;
|
||||||
|
int palette_offset;
|
||||||
|
int picture_offset;
|
||||||
|
int last_end_ts;
|
||||||
|
int last_window_ts;
|
||||||
|
int window_num;
|
||||||
|
rect_t windows[2];
|
||||||
|
si_list_t *sil;
|
||||||
|
} sup_writer_t;
|
||||||
|
|
||||||
|
/* Create a new sup writer state */
|
||||||
|
sup_writer_t *new_sup_writer (char *filename, int im_w, int im_h, int fps_num, int fps_den);
|
||||||
|
|
||||||
|
/* Write sup data for subtitle */
|
||||||
|
void write_sup (sup_writer_t *sw, uint8_t *im, int num_crop, rect_t *crops, uint32_t *pal, int start, int end, int strict);
|
||||||
|
|
||||||
|
/* Call this once at the end */
|
||||||
|
void close_sup_writer (sup_writer_t *sw);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
1457
x86inc.asm
Normal file
1457
x86inc.asm
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user