Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish AVIF support in getimagesize() #7711

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.REDIST.BINS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
15. ext/phar/zip.c portion extracted from libzip
16. libbcmath (ext/bcmath) see ext/bcmath/libbcmath/LICENSE
17. ext/mbstring/ucgendat portions based on the ucgendat.c from the OpenLDAP
18. avifinfo (ext/standard/libavifinfo) see ext/standard/libavifinfo/LICENSE


3. pcre2lib (ext/pcre)
Expand Down Expand Up @@ -591,7 +592,7 @@ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


16. ext/mbstring/ucgendat portions based on the ucgendat.c from the OpenLDAP
17. ext/mbstring/ucgendat portions based on the ucgendat.c from the OpenLDAP
y-guyon marked this conversation as resolved.
Show resolved Hide resolved

The OpenLDAP Public License
Version 2.8, 17 August 2003
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32.
http_fopen_wrapper.c php_fopen_wrapper.c credits.c css.c \
var_unserializer.c ftok.c sha1.c user_filters.c uuencode.c \
filters.c proc_open.c streamsfuncs.c http.c password.c \
random.c net.c hrtime.c crc32_x86.c,,,
random.c net.c hrtime.c crc32_x86.c libavifinfo/avifinfo.c,,,
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

PHP_ADD_MAKEFILE_FRAGMENT
Expand Down
1 change: 1 addition & 0 deletions ext/standard/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ EXTENSION("standard", "array.c base64.c basic_functions.c browscap.c \
user_filters.c uuencode.c filters.c proc_open.c password.c \
streamsfuncs.c http.c flock_compat.c random.c hrtime.c", false /* never shared */,
'/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
ADD_SOURCES("ext/standard/libavifinfo", "avifinfo.c", "standard");
PHP_STANDARD = "yes";
ADD_MAKEFILE_FRAGMENT();
PHP_INSTALL_HEADERS("", "ext/standard");
136 changes: 59 additions & 77 deletions ext/standard/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#endif
#include "fopen_wrappers.h"
#include "ext/standard/fsock.h"
#include "libavifinfo/avifinfo.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
Expand Down Expand Up @@ -1155,95 +1156,76 @@ static struct gfxinfo *php_handle_webp(php_stream * stream)
}
/* }}} */

/* {{{ php_handle_avif
* There's no simple way to get this information - so, for now, this is unsupported.
* Simply return 0 for everything.
*/
static struct gfxinfo *php_handle_avif(php_stream * stream) {
return ecalloc(1, sizeof(struct gfxinfo));
}
/* }}} */

/* {{{ php_ntohl
* Convert a big-endian network uint32 to host order -
* which may be either little-endian or big-endian.
* Thanks to Rob Pike via Joe Drago:
* https://commandcenter.blogspot.nl/2012/04/byte-order-fallacy.html
*/
static uint32_t php_ntohl(uint32_t val) {
uint8_t data[4];

memcpy(&data, &val, sizeof(data));
return ((uint32_t)data[3] << 0) |
((uint32_t)data[2] << 8) |
((uint32_t)data[1] << 16) |
((uint32_t)data[0] << 24);
}
/* }}} */

/* {{{ php_is_image_avif
* detect whether an image is of type AVIF
*
* An AVIF image will start off a header "box".
* This starts with with a four-byte integer containing the number of bytes in the filetype box.
* This must be followed by the string "ftyp".
* Next comes a four-byte string indicating the "major brand".
* If that's "avif" or "avis", this is an AVIF image.
* Next, there's a four-byte "minor version" field, which we can ignore.
* Next comes an array of four-byte strings containing "compatible brands".
* These extend to the end of the box.
* If any of the compatible brands is "avif" or "avis", then this is an AVIF image.
* Otherwise, well, it's not.
* For more, see https://mpeg.chiariglione.org/standards/mpeg-4/iso-base-media-file-format/text-isoiec-14496-12-5th-edition
*/
bool php_is_image_avif(php_stream * stream) {
uint32_t header_size_reversed, header_size, i;
char box_type[4], brand[4];
/* {{{ User struct and stream read/skip implementations for libavifinfo API */
struct php_avif_stream {
php_stream* stream;
uint8_t buffer[AVIFINFO_MAX_NUM_READ_BYTES];
};

ZEND_ASSERT(stream != NULL);
static const uint8_t* php_avif_stream_read(void* stream, size_t num_bytes) {
struct php_avif_stream* avif_stream = (struct php_avif_stream*)stream;

if (php_stream_read(stream, (char *) &header_size_reversed, 4) != 4) {
return 0;
if (avif_stream == NULL || avif_stream->stream == NULL) {
return NULL;
}

header_size = php_ntohl(header_size_reversed);

/* If the box type isn't "ftyp", it can't be an AVIF image. */
if (php_stream_read(stream, box_type, 4) != 4) {
return 0;
if (php_stream_read(avif_stream->stream, (char*)avif_stream->buffer, num_bytes) != num_bytes) {
avif_stream->stream = NULL; /* fail further calls */
return NULL;
}
return avif_stream->buffer;
}

if (memcmp(box_type, "ftyp", 4)) {
return 0;
}

/* If the major brand is "avif" or "avis", it's an AVIF image. */
if (php_stream_read(stream, brand, 4) != 4) {
return 0;
}
static void php_avif_stream_skip(void* stream, size_t num_bytes) {
struct php_avif_stream* avif_stream = (struct php_avif_stream*)stream;

if (!memcmp(brand, "avif", 4) || !memcmp(brand, "avis", 4)) {
return 1;
if (avif_stream == NULL || avif_stream->stream == NULL) {
return;
}

/* Skip the next four bytes, which are the "minor version". */
if (php_stream_read(stream, brand, 4) != 4) {
return 0;
if (php_stream_seek(avif_stream->stream, num_bytes, SEEK_CUR)) {
avif_stream->stream = NULL; /* fail further calls */
}
}
/* }}} */

/* Look for "avif" or "avis" in any member of compatible_brands[], to the end of the header.
Note we've already read four groups of four bytes. */
/* {{{ php_handle_avif
* Parse AVIF features
*
* The stream must be positioned at the beginning of a box, so it does not
* matter whether the "ftyp" box was already read by php_is_image_avif() or not.
* It will read bytes from the stream until features are found or the file is
* declared as invalid. Around 450 bytes are usually enough.
* Transforms such as mirror and rotation are not applied on width and height.
*/
static struct gfxinfo *php_handle_avif(php_stream * stream) {
struct gfxinfo* result = NULL;
AvifInfoFeatures features;
struct php_avif_stream avif_stream;
avif_stream.stream = stream;

if (AvifInfoGetFeaturesStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip, &features) == kAvifInfoOk) {
result = (struct gfxinfo*)ecalloc(1, sizeof(struct gfxinfo));
result->width = features.width;
result->height = features.height;
result->bits = features.bit_depth;
result->channels = features.num_channels;
}
return result;
}
/* }}} */

for (i = 16; i < header_size; i += 4) {
if (php_stream_read(stream, brand, 4) != 4) {
return 0;
}
/* {{{ php_is_image_avif
* Detect whether an image is of type AVIF
*
* Only the first "ftyp" box is read.
* For a valid file, 12 bytes are usually read, but more might be necessary.
*/
bool php_is_image_avif(php_stream* stream) {
struct php_avif_stream avif_stream;
avif_stream.stream = stream;

if (!memcmp(brand, "avif", 4) || !memcmp(brand, "avis", 4)) {
return 1;
}
if (AvifInfoIdentifyStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip) == kAvifInfoOk) {
return 1;
}

return 0;
}
/* }}} */
Expand Down
26 changes: 26 additions & 0 deletions ext/standard/libavifinfo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2021, Alliance for Open Media. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
107 changes: 107 additions & 0 deletions ext/standard/libavifinfo/PATENTS
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Alliance for Open Media Patent License 1.0

1. License Terms.

1.1. Patent License. Subject to the terms and conditions of this License, each
Licensor, on behalf of itself and successors in interest and assigns,
grants Licensee a non-sublicensable, perpetual, worldwide, non-exclusive,
no-charge, royalty-free, irrevocable (except as expressly stated in this
License) patent license to its Necessary Claims to make, use, sell, offer
for sale, import or distribute any Implementation.

1.2. Conditions.

1.2.1. Availability. As a condition to the grant of rights to Licensee to make,
sell, offer for sale, import or distribute an Implementation under
Section 1.1, Licensee must make its Necessary Claims available under
this License, and must reproduce this License with any Implementation
as follows:

a. For distribution in source code, by including this License in the
root directory of the source code with its Implementation.

b. For distribution in any other form (including binary, object form,
and/or hardware description code (e.g., HDL, RTL, Gate Level Netlist,
GDSII, etc.)), by including this License in the documentation, legal
notices, and/or other written materials provided with the
Implementation.

1.2.2. Additional Conditions. This license is directly from Licensor to
Licensee. Licensee acknowledges as a condition of benefiting from it
that no rights from Licensor are received from suppliers, distributors,
or otherwise in connection with this License.

1.3. Defensive Termination. If any Licensee, its Affiliates, or its agents
initiates patent litigation or files, maintains, or voluntarily
participates in a lawsuit against another entity or any person asserting
that any Implementation infringes Necessary Claims, any patent licenses
granted under this License directly to the Licensee are immediately
terminated as of the date of the initiation of action unless 1) that suit
was in response to a corresponding suit regarding an Implementation first
brought against an initiating entity, or 2) that suit was brought to
enforce the terms of this License (including intervention in a third-party
action by a Licensee).

1.4. Disclaimers. The Reference Implementation and Specification are provided
"AS IS" and without warranty. The entire risk as to implementing or
otherwise using the Reference Implementation or Specification is assumed
by the implementer and user. Licensor expressly disclaims any warranties
(express, implied, or otherwise), including implied warranties of
merchantability, non-infringement, fitness for a particular purpose, or
title, related to the material. IN NO EVENT WILL LICENSOR BE LIABLE TO
ANY OTHER PARTY FOR LOST PROFITS OR ANY FORM OF INDIRECT, SPECIAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER FROM ANY CAUSES OF
ACTION OF ANY KIND WITH RESPECT TO THIS LICENSE, WHETHER BASED ON BREACH
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), OR OTHERWISE, AND WHETHER OR
NOT THE OTHER PARTRY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2. Definitions.

2.1. Affiliate. "Affiliate" means an entity that directly or indirectly
Controls, is Controlled by, or is under common Control of that party.

2.2. Control. "Control" means direct or indirect control of more than 50% of
the voting power to elect directors of that corporation, or for any other
entity, the power to direct management of such entity.

2.3. Decoder. "Decoder" means any decoder that conforms fully with all
non-optional portions of the Specification.

2.4. Encoder. "Encoder" means any encoder that produces a bitstream that can
be decoded by a Decoder only to the extent it produces such a bitstream.

2.5. Final Deliverable. "Final Deliverable" means the final version of a
deliverable approved by the Alliance for Open Media as a Final
Deliverable.

2.6. Implementation. "Implementation" means any implementation, including the
Reference Implementation, that is an Encoder and/or a Decoder. An
Implementation also includes components of an Implementation only to the
extent they are used as part of an Implementation.

2.7. License. "License" means this license.

2.8. Licensee. "Licensee" means any person or entity who exercises patent
rights granted under this License.

2.9. Licensor. "Licensor" means (i) any Licensee that makes, sells, offers
for sale, imports or distributes any Implementation, or (ii) a person
or entity that has a licensing obligation to the Implementation as a
result of its membership and/or participation in the Alliance for Open
Media working group that developed the Specification.

2.10. Necessary Claims. "Necessary Claims" means all claims of patents or
patent applications, (a) that currently or at any time in the future,
are owned or controlled by the Licensor, and (b) (i) would be an
Essential Claim as defined by the W3C Policy as of February 5, 2004
(https://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential)
as if the Specification was a W3C Recommendation; or (ii) are infringed
by the Reference Implementation.

2.11. Reference Implementation. "Reference Implementation" means an Encoder
and/or Decoder released by the Alliance for Open Media as a Final
Deliverable.

2.12. Specification. "Specification" means the specification designated by
the Alliance for Open Media as a Final Deliverable for which this
License was issued.
11 changes: 11 additions & 0 deletions ext/standard/libavifinfo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# AVIF-info

There is no compact, reliable way to determine the size of an AVIF image. A
standalone C snippet called
[libavifinfo](https://aomedia.googlesource.com/libavifinfo) was created to
partially parse an AVIF payload and to extract the width, height, bit depth and
channel count without depending on the full libavif library.

`avifinfo.h`, `avifinfo.c`, `LICENSE` and `PATENTS` were copied verbatim from: \
https://aomedia.googlesource.com/libavifinfo/+/96f34d945ac7dac229feddfa94dbae66e202b838 \
They can easily be kept up-to-date the same way.
Loading