| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Copyright (c) 2021, Alliance for Open Media. All rights reserved 4 * 5 * This source code is subject to the terms of the BSD 2 Clause License and 6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 7 * was not distributed with this source code in the LICENSE file, you can 8 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 9 * Media Patent License 1.0 was not distributed with this source code in the 10 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 11 * 12 * Note: this class is from libavifinfo - https://aomedia.googlesource.com/libavifinfo/+/refs/heads/main/avifinfo.php at 2b924de. 13 * It is used as a fallback to parse AVIF files when the server doesn't support AVIF, 14 * primarily to identify the width and height of the image. 15 * 16 * Note PHP 8.2 added native support for AVIF, so this class can be removed when WordPress requires PHP 8.2. 17 */ 18 19 namespace Avifinfo; 20 21 const FOUND = 0; // Input correctly parsed and information retrieved. 22 const NOT_FOUND = 1; // Input correctly parsed but information is missing or elsewhere. 23 const TRUNCATED = 2; // Input correctly parsed until missing bytes to continue. 24 const ABORTED = 3; // Input correctly parsed until stopped to avoid timeout or crash. 25 const INVALID = 4; // Input incorrectly parsed. 26 27 const MAX_SIZE = 4294967295; // Unlikely to be insufficient to parse AVIF headers. 28 const MAX_NUM_BOXES = 4096; // Be reasonable. Avoid timeouts and out-of-memory. 29 const MAX_VALUE = 255; 30 const MAX_TILES = 16; 31 const MAX_PROPS = 32; 32 const MAX_FEATURES = 8; 33 const UNDEFINED = 0; // Value was not yet parsed. 34 35 /** 36 * Reads an unsigned integer with most significant bits first. 37 * 38 * @param binary string $input Must be at least $num_bytes-long. 39 * @param int $num_bytes Number of parsed bytes. 40 * @return int Value. 41 */ 42 function read_big_endian( $input, $num_bytes ) { 43 if ( $num_bytes == 1 ) { 44 return unpack( 'C', $input ) [1]; 45 } else if ( $num_bytes == 2 ) { 46 return unpack( 'n', $input ) [1]; 47 } else if ( $num_bytes == 3 ) { 48 $bytes = unpack( 'C3', $input ); 49 return ( $bytes[1] << 16 ) | ( $bytes[2] << 8 ) | $bytes[3]; 50 } else { // $num_bytes is 4 51 // This might fail to read unsigned values >= 2^31 on 32-bit systems. 52 // See https://www.php.net/manual/en/function.unpack.php#106041 53 return unpack( 'N', $input ) [1]; 54 } 55 } 56 57 /** 58 * Reads bytes and advances the stream position by the same count. 59 * 60 * @param stream $handle Bytes will be read from this resource. 61 * @param int $num_bytes Number of bytes read. Must be greater than 0. 62 * @return binary string|false The raw bytes or false on failure. 63 */ 64 function read( $handle, $num_bytes ) { 65 $data = fread( $handle, $num_bytes ); 66 return ( $data !== false && strlen( $data ) >= $num_bytes ) ? $data : false; 67 } 68 69 /** 70 * Advances the stream position by the given offset. 71 * 72 * @param stream $handle Bytes will be skipped from this resource. 73 * @param int $num_bytes Number of skipped bytes. Can be 0. 74 * @return bool True on success or false on failure. 75 */ 76 // Skips 'num_bytes' from the 'stream'. 'num_bytes' can be zero. 77 function skip( $handle, $num_bytes ) { 78 return ( fseek( $handle, $num_bytes, SEEK_CUR ) == 0 ); 79 } 80 81 //------------------------------------------------------------------------------ 82 // Features are parsed into temporary property associations. 83 84 class Tile { // Tile item id <-> parent item id associations. 85 public $tile_item_id; 86 public $parent_item_id; 87 } 88 89 class Prop { // Property index <-> item id associations. 90 public $property_index; 91 public $item_id; 92 } 93 94 class Dim_Prop { // Property <-> features associations. 95 public $property_index; 96 public $width; 97 public $height; 98 } 99 100 class Chan_Prop { // Property <-> features associations. 101 public $property_index; 102 public $bit_depth; 103 public $num_channels; 104 } 105 106 class Features { 107 public $has_primary_item = false; // True if "pitm" was parsed. 108 public $has_alpha = false; // True if an alpha "auxC" was parsed. 109 public $primary_item_id; 110 public $primary_item_features = array( // Deduced from the data below. 111 'width' => UNDEFINED, // In number of pixels. 112 'height' => UNDEFINED, // Ignores crop and rotation. 113 'bit_depth' => UNDEFINED, // Likely 8, 10 or 12 bits per channel per pixel. 114 'num_channels' => UNDEFINED // Likely 1, 2, 3 or 4 channels: 115 // (1 monochrome or 3 colors) + (0 or 1 alpha) 116 ); 117 118 public $tiles = array(); // Tile[] 119 public $props = array(); // Prop[] 120 public $dim_props = array(); // Dim_Prop[] 121 public $chan_props = array(); // Chan_Prop[] 122 123 /** 124 * Binds the width, height, bit depth and number of channels from stored internal features. 125 * 126 * @param int $target_item_id Id of the item whose features will be bound. 127 * @param int $tile_depth Maximum recursion to search within tile-parent relations. 128 * @return Status FOUND on success or NOT_FOUND on failure. 129 */ 130 private function get_item_features( $target_item_id, $tile_depth ) { 131 foreach ( $this->props as $prop ) { 132 if ( $prop->item_id != $target_item_id ) { 133 continue; 134 } 135 136 // Retrieve the width and height of the primary item if not already done. 137 if ( $target_item_id == $this->primary_item_id && 138 ( $this->primary_item_features['width'] == UNDEFINED || 139 $this->primary_item_features['height'] == UNDEFINED ) ) { 140 foreach ( $this->dim_props as $dim_prop ) { 141 if ( $dim_prop->property_index != $prop->property_index ) { 142 continue; 143 } 144 $this->primary_item_features['width'] = $dim_prop->width; 145 $this->primary_item_features['height'] = $dim_prop->height; 146 if ( $this->primary_item_features['bit_depth'] != UNDEFINED && 147 $this->primary_item_features['num_channels'] != UNDEFINED ) { 148 return FOUND; 149 } 150 break; 151 } 152 } 153 // Retrieve the bit depth and number of channels of the target item if not 154 // already done. 155 if ( $this->primary_item_features['bit_depth'] == UNDEFINED || 156 $this->primary_item_features['num_channels'] == UNDEFINED ) { 157 foreach ( $this->chan_props as $chan_prop ) { 158 if ( $chan_prop->property_index != $prop->property_index ) { 159 continue; 160 } 161 $this->primary_item_features['bit_depth'] = $chan_prop->bit_depth; 162 $this->primary_item_features['num_channels'] = $chan_prop->num_channels; 163 if ( $this->primary_item_features['width'] != UNDEFINED && 164 $this->primary_item_features['height'] != UNDEFINED ) { 165 return FOUND; 166 } 167 break; 168 } 169 } 170 } 171 172 // Check for the bit_depth and num_channels in a tile if not yet found. 173 if ( $tile_depth < 3 ) { 174 foreach ( $this->tiles as $tile ) { 175 if ( $tile->parent_item_id != $target_item_id ) { 176 continue; 177 } 178 $status = $this->get_item_features( $tile->tile_item_id, $tile_depth + 1 ); 179 if ( $status != NOT_FOUND ) { 180 return $status; 181 } 182 } 183 } 184 return NOT_FOUND; 185 } 186 187 /** 188 * Finds the width, height, bit depth and number of channels of the primary item. 189 * 190 * @return Status FOUND on success or NOT_FOUND on failure. 191 */ 192 public function get_primary_item_features() { 193 // Nothing to do without the primary item ID. 194 if ( !$this->has_primary_item ) { 195 return NOT_FOUND; 196 } 197 // Early exit. 198 if ( empty( $this->dim_props ) || empty( $this->chan_props ) ) { 199 return NOT_FOUND; 200 } 201 $status = $this->get_item_features( $this->primary_item_id, /*tile_depth=*/ 0 ); 202 if ( $status != FOUND ) { 203 return $status; 204 } 205 206 // "auxC" is parsed before the "ipma" properties so it is known now, if any. 207 if ( $this->has_alpha ) { 208 ++$this->primary_item_features['num_channels']; 209 } 210 return FOUND; 211 } 212 } 213 214 //------------------------------------------------------------------------------ 215 216 class Box { 217 public $size; // In bytes. 218 public $type; // Four characters. 219 public $version; // 0 or actual version if this is a full box. 220 public $flags; // 0 or actual value if this is a full box. 221 public $content_size; // 'size' minus the header size. 222 223 /** 224 * Reads the box header. 225 * 226 * @param stream $handle The resource the header will be parsed from. 227 * @param int $num_parsed_boxes The total number of parsed boxes. Prevents timeouts. 228 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 229 * @return Status FOUND on success or an error on failure. 230 */ 231 public function parse( $handle, &$num_parsed_boxes, $num_remaining_bytes = MAX_SIZE ) { 232 // See ISO/IEC 14496-12:2012(E) 4.2 233 $header_size = 8; // box 32b size + 32b type (at least) 234 if ( $header_size > $num_remaining_bytes ) { 235 return INVALID; 236 } 237 if ( !( $data = read( $handle, 8 ) ) ) { 238 return TRUNCATED; 239 } 240 $this->size = read_big_endian( $data, 4 ); 241 $this->type = substr( $data, 4, 4 ); 242 // 'box->size==1' means 64-bit size should be read after the box type. 243 // 'box->size==0' means this box extends to all remaining bytes. 244 if ( $this->size == 1 ) { 245 $header_size += 8; 246 if ( $header_size > $num_remaining_bytes ) { 247 return INVALID; 248 } 249 if ( !( $data = read( $handle, 8 ) ) ) { 250 return TRUNCATED; 251 } 252 // Stop the parsing if any box has a size greater than 4GB. 253 if ( read_big_endian( $data, 4 ) != 0 ) { 254 return ABORTED; 255 } 256 // Read the 32 least-significant bits. 257 $this->size = read_big_endian( substr( $data, 4, 4 ), 4 ); 258 } else if ( $this->size == 0 ) { 259 // ISO/IEC 14496-12 4.2.2: 260 // if size is 0, then this box shall be in a top-level box 261 // (i.e. not contained in another box) 262 // Unfortunately the presence of a parent box is unknown here. 263 $this->size = $num_remaining_bytes; 264 } 265 if ( $this->size < $header_size ) { 266 return INVALID; 267 } 268 if ( $this->size > $num_remaining_bytes ) { 269 return INVALID; 270 } 271 272 // 16 bytes of usertype should be read here if the box type is 'uuid'. 273 // 'uuid' boxes are skipped so usertype is part of the skipped body. 274 275 $has_fullbox_header = $this->type == 'meta' || $this->type == 'pitm' || 276 $this->type == 'ipma' || $this->type == 'ispe' || 277 $this->type == 'pixi' || $this->type == 'iref' || 278 $this->type == 'auxC'; 279 if ( $has_fullbox_header ) { 280 $header_size += 4; 281 } 282 if ( $this->size < $header_size ) { 283 return INVALID; 284 } 285 $this->content_size = $this->size - $header_size; 286 // Avoid timeouts. The maximum number of parsed boxes is arbitrary. 287 ++$num_parsed_boxes; 288 if ( $num_parsed_boxes >= MAX_NUM_BOXES ) { 289 return ABORTED; 290 } 291 292 $this->version = 0; 293 $this->flags = 0; 294 if ( $has_fullbox_header ) { 295 if ( !( $data = read( $handle, 4 ) ) ) { 296 return TRUNCATED; 297 } 298 $this->version = read_big_endian( $data, 1 ); 299 $this->flags = read_big_endian( substr( $data, 1, 3 ), 3 ); 300 // See AV1 Image File Format (AVIF) 8.1 301 // at https://aomediacodec.github.io/av1-avif/#avif-boxes (available when 302 // https://github.com/AOMediaCodec/av1-avif/pull/170 is merged). 303 $is_parsable = ( $this->type == 'meta' && $this->version <= 0 ) || 304 ( $this->type == 'pitm' && $this->version <= 1 ) || 305 ( $this->type == 'ipma' && $this->version <= 1 ) || 306 ( $this->type == 'ispe' && $this->version <= 0 ) || 307 ( $this->type == 'pixi' && $this->version <= 0 ) || 308 ( $this->type == 'iref' && $this->version <= 1 ) || 309 ( $this->type == 'auxC' && $this->version <= 0 ); 310 // Instead of considering this file as invalid, skip unparsable boxes. 311 if ( !$is_parsable ) { 312 $this->type = 'skip'; // FreeSpaceBox. To be ignored by readers. 313 } 314 } 315 // print_r( $this ); // Uncomment to print all boxes. 316 return FOUND; 317 } 318 } 319 320 //------------------------------------------------------------------------------ 321 322 class Parser { 323 private $handle; // Input stream. 324 private $num_parsed_boxes = 0; 325 private $data_was_skipped = false; 326 public $features; 327 328 function __construct( $handle ) { 329 $this->handle = $handle; 330 $this->features = new Features(); 331 } 332 333 /** 334 * Parses an "ipco" box. 335 * 336 * "ispe" is used for width and height, "pixi" and "av1C" are used for bit depth 337 * and number of channels, and "auxC" is used for alpha. 338 * 339 * @param stream $handle The resource the box will be parsed from. 340 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 341 * @return Status FOUND on success or an error on failure. 342 */ 343 private function parse_ipco( $num_remaining_bytes ) { 344 $box_index = 1; // 1-based index. Used for iterating over properties. 345 do { 346 $box = new Box(); 347 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 348 if ( $status != FOUND ) { 349 return $status; 350 } 351 352 if ( $box->type == 'ispe' ) { 353 // See ISO/IEC 23008-12:2017(E) 6.5.3.2 354 if ( $box->content_size < 8 ) { 355 return INVALID; 356 } 357 if ( !( $data = read( $this->handle, 8 ) ) ) { 358 return TRUNCATED; 359 } 360 $width = read_big_endian( substr( $data, 0, 4 ), 4 ); 361 $height = read_big_endian( substr( $data, 4, 4 ), 4 ); 362 if ( $width == 0 || $height == 0 ) { 363 return INVALID; 364 } 365 if ( count( $this->features->dim_props ) <= MAX_FEATURES && 366 $box_index <= MAX_VALUE ) { 367 $dim_prop_count = count( $this->features->dim_props ); 368 $this->features->dim_props[$dim_prop_count] = new Dim_Prop(); 369 $this->features->dim_props[$dim_prop_count]->property_index = $box_index; 370 $this->features->dim_props[$dim_prop_count]->width = $width; 371 $this->features->dim_props[$dim_prop_count]->height = $height; 372 } else { 373 $this->data_was_skipped = true; 374 } 375 if ( !skip( $this->handle, $box->content_size - 8 ) ) { 376 return TRUNCATED; 377 } 378 } else if ( $box->type == 'pixi' ) { 379 // See ISO/IEC 23008-12:2017(E) 6.5.6.2 380 if ( $box->content_size < 1 ) { 381 return INVALID; 382 } 383 if ( !( $data = read( $this->handle, 1 ) ) ) { 384 return TRUNCATED; 385 } 386 $num_channels = read_big_endian( $data, 1 ); 387 if ( $num_channels < 1 ) { 388 return INVALID; 389 } 390 if ( $box->content_size < 1 + $num_channels ) { 391 return INVALID; 392 } 393 if ( !( $data = read( $this->handle, 1 ) ) ) { 394 return TRUNCATED; 395 } 396 $bit_depth = read_big_endian( $data, 1 ); 397 if ( $bit_depth < 1 ) { 398 return INVALID; 399 } 400 for ( $i = 1; $i < $num_channels; ++$i ) { 401 if ( !( $data = read( $this->handle, 1 ) ) ) { 402 return TRUNCATED; 403 } 404 // Bit depth should be the same for all channels. 405 if ( read_big_endian( $data, 1 ) != $bit_depth ) { 406 return INVALID; 407 } 408 if ( $i > 32 ) { 409 return ABORTED; // Be reasonable. 410 } 411 } 412 if ( count( $this->features->chan_props ) <= MAX_FEATURES && 413 $box_index <= MAX_VALUE && $bit_depth <= MAX_VALUE && 414 $num_channels <= MAX_VALUE ) { 415 $chan_prop_count = count( $this->features->chan_props ); 416 $this->features->chan_props[$chan_prop_count] = new Chan_Prop(); 417 $this->features->chan_props[$chan_prop_count]->property_index = $box_index; 418 $this->features->chan_props[$chan_prop_count]->bit_depth = $bit_depth; 419 $this->features->chan_props[$chan_prop_count]->num_channels = $num_channels; 420 } else { 421 $this->data_was_skipped = true; 422 } 423 if ( !skip( $this->handle, $box->content_size - ( 1 + $num_channels ) ) ) { 424 return TRUNCATED; 425 } 426 } else if ( $box->type == 'av1C' ) { 427 // See AV1 Codec ISO Media File Format Binding 2.3.1 428 // at https://aomediacodec.github.io/av1-isobmff/#av1c 429 // Only parse the necessary third byte. Assume that the others are valid. 430 if ( $box->content_size < 3 ) { 431 return INVALID; 432 } 433 if ( !( $data = read( $this->handle, 3 ) ) ) { 434 return TRUNCATED; 435 } 436 $byte = read_big_endian( substr( $data, 2, 1 ), 1 ); 437 $high_bitdepth = ( $byte & 0x40 ) != 0; 438 $twelve_bit = ( $byte & 0x20 ) != 0; 439 $monochrome = ( $byte & 0x10 ) != 0; 440 if ( $twelve_bit && !$high_bitdepth ) { 441 return INVALID; 442 } 443 if ( count( $this->features->chan_props ) <= MAX_FEATURES && 444 $box_index <= MAX_VALUE ) { 445 $chan_prop_count = count( $this->features->chan_props ); 446 $this->features->chan_props[$chan_prop_count] = new Chan_Prop(); 447 $this->features->chan_props[$chan_prop_count]->property_index = $box_index; 448 $this->features->chan_props[$chan_prop_count]->bit_depth = 449 $high_bitdepth ? $twelve_bit ? 12 : 10 : 8; 450 $this->features->chan_props[$chan_prop_count]->num_channels = $monochrome ? 1 : 3; 451 } else { 452 $this->data_was_skipped = true; 453 } 454 if ( !skip( $this->handle, $box->content_size - 3 ) ) { 455 return TRUNCATED; 456 } 457 } else if ( $box->type == 'auxC' ) { 458 // See AV1 Image File Format (AVIF) 4 459 // at https://aomediacodec.github.io/av1-avif/#auxiliary-images 460 $kAlphaStr = "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0"; 461 $kAlphaStrLength = 44; // Includes terminating character. 462 if ( $box->content_size >= $kAlphaStrLength ) { 463 if ( !( $data = read( $this->handle, $kAlphaStrLength ) ) ) { 464 return TRUNCATED; 465 } 466 if ( substr( $data, 0, $kAlphaStrLength ) == $kAlphaStr ) { 467 // Note: It is unlikely but it is possible that this alpha plane does 468 // not belong to the primary item or a tile. Ignore this issue. 469 $this->features->has_alpha = true; 470 } 471 if ( !skip( $this->handle, $box->content_size - $kAlphaStrLength ) ) { 472 return TRUNCATED; 473 } 474 } else { 475 if ( !skip( $this->handle, $box->content_size ) ) { 476 return TRUNCATED; 477 } 478 } 479 } else { 480 if ( !skip( $this->handle, $box->content_size ) ) { 481 return TRUNCATED; 482 } 483 } 484 ++$box_index; 485 $num_remaining_bytes -= $box->size; 486 } while ( $num_remaining_bytes > 0 ); 487 return NOT_FOUND; 488 } 489 490 /** 491 * Parses an "iprp" box. 492 * 493 * The "ipco" box contains the properties which are linked to items by the "ipma" box. 494 * 495 * @param stream $handle The resource the box will be parsed from. 496 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 497 * @return Status FOUND on success or an error on failure. 498 */ 499 private function parse_iprp( $num_remaining_bytes ) { 500 do { 501 $box = new Box(); 502 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 503 if ( $status != FOUND ) { 504 return $status; 505 } 506 507 if ( $box->type == 'ipco' ) { 508 $status = $this->parse_ipco( $box->content_size ); 509 if ( $status != NOT_FOUND ) { 510 return $status; 511 } 512 } else if ( $box->type == 'ipma' ) { 513 // See ISO/IEC 23008-12:2017(E) 9.3.2 514 $num_read_bytes = 4; 515 if ( $box->content_size < $num_read_bytes ) { 516 return INVALID; 517 } 518 if ( !( $data = read( $this->handle, $num_read_bytes ) ) ) { 519 return TRUNCATED; 520 } 521 $entry_count = read_big_endian( $data, 4 ); 522 $id_num_bytes = ( $box->version < 1 ) ? 2 : 4; 523 $index_num_bytes = ( $box->flags & 1 ) ? 2 : 1; 524 $essential_bit_mask = ( $box->flags & 1 ) ? 0x8000 : 0x80; 525 526 for ( $entry = 0; $entry < $entry_count; ++$entry ) { 527 if ( $entry >= MAX_PROPS || 528 count( $this->features->props ) >= MAX_PROPS ) { 529 $this->data_was_skipped = true; 530 break; 531 } 532 $num_read_bytes += $id_num_bytes + 1; 533 if ( $box->content_size < $num_read_bytes ) { 534 return INVALID; 535 } 536 if ( !( $data = read( $this->handle, $id_num_bytes + 1 ) ) ) { 537 return TRUNCATED; 538 } 539 $item_id = read_big_endian( 540 substr( $data, 0, $id_num_bytes ), $id_num_bytes ); 541 $association_count = read_big_endian( 542 substr( $data, $id_num_bytes, 1 ), 1 ); 543 544 for ( $property = 0; $property < $association_count; ++$property ) { 545 if ( $property >= MAX_PROPS || 546 count( $this->features->props ) >= MAX_PROPS ) { 547 $this->data_was_skipped = true; 548 break; 549 } 550 $num_read_bytes += $index_num_bytes; 551 if ( $box->content_size < $num_read_bytes ) { 552 return INVALID; 553 } 554 if ( !( $data = read( $this->handle, $index_num_bytes ) ) ) { 555 return TRUNCATED; 556 } 557 $value = read_big_endian( $data, $index_num_bytes ); 558 // $essential = ($value & $essential_bit_mask); // Unused. 559 $property_index = ( $value & ~$essential_bit_mask ); 560 if ( $property_index <= MAX_VALUE && $item_id <= MAX_VALUE ) { 561 $prop_count = count( $this->features->props ); 562 $this->features->props[$prop_count] = new Prop(); 563 $this->features->props[$prop_count]->property_index = $property_index; 564 $this->features->props[$prop_count]->item_id = $item_id; 565 } else { 566 $this->data_was_skipped = true; 567 } 568 } 569 if ( $property < $association_count ) { 570 break; // Do not read garbage. 571 } 572 } 573 574 // If all features are available now, do not look further. 575 $status = $this->features->get_primary_item_features(); 576 if ( $status != NOT_FOUND ) { 577 return $status; 578 } 579 580 // Mostly if 'data_was_skipped'. 581 if ( !skip( $this->handle, $box->content_size - $num_read_bytes ) ) { 582 return TRUNCATED; 583 } 584 } else { 585 if ( !skip( $this->handle, $box->content_size ) ) { 586 return TRUNCATED; 587 } 588 } 589 $num_remaining_bytes -= $box->size; 590 } while ( $num_remaining_bytes > 0 ); 591 return NOT_FOUND; 592 } 593 594 /** 595 * Parses an "iref" box. 596 * 597 * The "dimg" boxes contain links between tiles and their parent items, which 598 * can be used to infer bit depth and number of channels for the primary item 599 * when the latter does not have these properties. 600 * 601 * @param stream $handle The resource the box will be parsed from. 602 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 603 * @return Status FOUND on success or an error on failure. 604 */ 605 private function parse_iref( $num_remaining_bytes ) { 606 while ( $num_remaining_bytes > 0 ) { 607 $box = new Box(); 608 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 609 if ( $status != FOUND ) { 610 return $status; 611 } 612 613 if ( $box->type == 'dimg' ) { 614 // See ISO/IEC 14496-12:2015(E) 8.11.12.2 615 $num_bytes_per_id = ( $box->version == 0 ) ? 2 : 4; 616 $num_read_bytes = $num_bytes_per_id + 2; 617 if ( $box->content_size < $num_read_bytes ) { 618 return INVALID; 619 } 620 if ( !( $data = read( $this->handle, $num_read_bytes ) ) ) { 621 return TRUNCATED; 622 } 623 $from_item_id = read_big_endian( $data, $num_bytes_per_id ); 624 $reference_count = read_big_endian( substr( $data, $num_bytes_per_id, 2 ), 2 ); 625 626 for ( $i = 0; $i < $reference_count; ++$i ) { 627 if ( $i >= MAX_TILES ) { 628 $this->data_was_skipped = true; 629 break; 630 } 631 $num_read_bytes += $num_bytes_per_id; 632 if ( $box->content_size < $num_read_bytes ) { 633 return INVALID; 634 } 635 if ( !( $data = read( $this->handle, $num_bytes_per_id ) ) ) { 636 return TRUNCATED; 637 } 638 $to_item_id = read_big_endian( $data, $num_bytes_per_id ); 639 $tile_count = count( $this->features->tiles ); 640 if ( $from_item_id <= MAX_VALUE && $to_item_id <= MAX_VALUE && 641 $tile_count < MAX_TILES ) { 642 $this->features->tiles[$tile_count] = new Tile(); 643 $this->features->tiles[$tile_count]->tile_item_id = $to_item_id; 644 $this->features->tiles[$tile_count]->parent_item_id = $from_item_id; 645 } else { 646 $this->data_was_skipped = true; 647 } 648 } 649 650 // If all features are available now, do not look further. 651 $status = $this->features->get_primary_item_features(); 652 if ( $status != NOT_FOUND ) { 653 return $status; 654 } 655 656 // Mostly if 'data_was_skipped'. 657 if ( !skip( $this->handle, $box->content_size - $num_read_bytes ) ) { 658 return TRUNCATED; 659 } 660 } else { 661 if ( !skip( $this->handle, $box->content_size ) ) { 662 return TRUNCATED; 663 } 664 } 665 $num_remaining_bytes -= $box->size; 666 } 667 return NOT_FOUND; 668 } 669 670 /** 671 * Parses a "meta" box. 672 * 673 * It looks for the primary item ID in the "pitm" box and recurses into other boxes 674 * to find its features. 675 * 676 * @param stream $handle The resource the box will be parsed from. 677 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 678 * @return Status FOUND on success or an error on failure. 679 */ 680 private function parse_meta( $num_remaining_bytes ) { 681 do { 682 $box = new Box(); 683 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 684 if ( $status != FOUND ) { 685 return $status; 686 } 687 688 if ( $box->type == 'pitm' ) { 689 // See ISO/IEC 14496-12:2015(E) 8.11.4.2 690 $num_bytes_per_id = ( $box->version == 0 ) ? 2 : 4; 691 if ( $num_bytes_per_id > $num_remaining_bytes ) { 692 return INVALID; 693 } 694 if ( !( $data = read( $this->handle, $num_bytes_per_id ) ) ) { 695 return TRUNCATED; 696 } 697 $primary_item_id = read_big_endian( $data, $num_bytes_per_id ); 698 if ( $primary_item_id > MAX_VALUE ) { 699 return ABORTED; 700 } 701 $this->features->has_primary_item = true; 702 $this->features->primary_item_id = $primary_item_id; 703 if ( !skip( $this->handle, $box->content_size - $num_bytes_per_id ) ) { 704 return TRUNCATED; 705 } 706 } else if ( $box->type == 'iprp' ) { 707 $status = $this->parse_iprp( $box->content_size ); 708 if ( $status != NOT_FOUND ) { 709 return $status; 710 } 711 } else if ( $box->type == 'iref' ) { 712 $status = $this->parse_iref( $box->content_size ); 713 if ( $status != NOT_FOUND ) { 714 return $status; 715 } 716 } else { 717 if ( !skip( $this->handle, $box->content_size ) ) { 718 return TRUNCATED; 719 } 720 } 721 $num_remaining_bytes -= $box->size; 722 } while ( $num_remaining_bytes != 0 ); 723 // According to ISO/IEC 14496-12:2012(E) 8.11.1.1 there is at most one "meta". 724 return INVALID; 725 } 726 727 /** 728 * Parses a file stream. 729 * 730 * The file type is checked through the "ftyp" box. 731 * 732 * @return bool True if the input stream is an AVIF bitstream or false. 733 */ 734 public function parse_ftyp() { 735 $box = new Box(); 736 $status = $box->parse( $this->handle, $this->num_parsed_boxes ); 737 if ( $status != FOUND ) { 738 return false; 739 } 740 741 if ( $box->type != 'ftyp' ) { 742 return false; 743 } 744 // Iterate over brands. See ISO/IEC 14496-12:2012(E) 4.3.1 745 if ( $box->content_size < 8 ) { 746 return false; 747 } 748 for ( $i = 0; $i + 4 <= $box->content_size; $i += 4 ) { 749 if ( !( $data = read( $this->handle, 4 ) ) ) { 750 return false; 751 } 752 if ( $i == 4 ) { 753 continue; // Skip minor_version. 754 } 755 if ( substr( $data, 0, 4 ) == 'avif' || substr( $data, 0, 4 ) == 'avis' ) { 756 return skip( $this->handle, $box->content_size - ( $i + 4 ) ); 757 } 758 if ( $i > 32 * 4 ) { 759 return false; // Be reasonable. 760 } 761 762 } 763 return false; // No AVIF brand no good. 764 } 765 766 /** 767 * Parses a file stream. 768 * 769 * Features are extracted from the "meta" box. 770 * 771 * @return bool True if the main features of the primary item were parsed or false. 772 */ 773 public function parse_file() { 774 $box = new Box(); 775 while ( $box->parse( $this->handle, $this->num_parsed_boxes ) == FOUND ) { 776 if ( $box->type === 'meta' ) { 777 if ( $this->parse_meta( $box->content_size ) != FOUND ) { 778 return false; 779 } 780 return true; 781 } 782 if ( !skip( $this->handle, $box->content_size ) ) { 783 return false; 784 } 785 } 786 return false; // No "meta" no good. 787 } 788 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Jun 14 08:20:09 2026 | Cross-referenced by PHPXref |