| [ 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 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 resource $handle Bytes will be read from this resource. 61 * @param int $num_bytes Number of bytes read. Must be greater than 0. 62 * @return 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 resource $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 int 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 int 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 resource $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 int 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 int $num_remaining_bytes The number of bytes that should be available from the resource. 340 * @return int FOUND on success or an error on failure. 341 */ 342 private function parse_ipco( $num_remaining_bytes ) { 343 $box_index = 1; // 1-based index. Used for iterating over properties. 344 do { 345 $box = new Box(); 346 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 347 if ( $status != FOUND ) { 348 return $status; 349 } 350 351 if ( $box->type == 'ispe' ) { 352 // See ISO/IEC 23008-12:2017(E) 6.5.3.2 353 if ( $box->content_size < 8 ) { 354 return INVALID; 355 } 356 if ( !( $data = read( $this->handle, 8 ) ) ) { 357 return TRUNCATED; 358 } 359 $width = read_big_endian( substr( $data, 0, 4 ), 4 ); 360 $height = read_big_endian( substr( $data, 4, 4 ), 4 ); 361 if ( $width == 0 || $height == 0 ) { 362 return INVALID; 363 } 364 if ( count( $this->features->dim_props ) <= MAX_FEATURES && 365 $box_index <= MAX_VALUE ) { 366 $dim_prop_count = count( $this->features->dim_props ); 367 $this->features->dim_props[$dim_prop_count] = new Dim_Prop(); 368 $this->features->dim_props[$dim_prop_count]->property_index = $box_index; 369 $this->features->dim_props[$dim_prop_count]->width = $width; 370 $this->features->dim_props[$dim_prop_count]->height = $height; 371 } else { 372 $this->data_was_skipped = true; 373 } 374 if ( !skip( $this->handle, $box->content_size - 8 ) ) { 375 return TRUNCATED; 376 } 377 } else if ( $box->type == 'pixi' ) { 378 // See ISO/IEC 23008-12:2017(E) 6.5.6.2 379 if ( $box->content_size < 1 ) { 380 return INVALID; 381 } 382 if ( !( $data = read( $this->handle, 1 ) ) ) { 383 return TRUNCATED; 384 } 385 $num_channels = read_big_endian( $data, 1 ); 386 if ( $num_channels < 1 ) { 387 return INVALID; 388 } 389 if ( $box->content_size < 1 + $num_channels ) { 390 return INVALID; 391 } 392 if ( !( $data = read( $this->handle, 1 ) ) ) { 393 return TRUNCATED; 394 } 395 $bit_depth = read_big_endian( $data, 1 ); 396 if ( $bit_depth < 1 ) { 397 return INVALID; 398 } 399 for ( $i = 1; $i < $num_channels; ++$i ) { 400 if ( !( $data = read( $this->handle, 1 ) ) ) { 401 return TRUNCATED; 402 } 403 // Bit depth should be the same for all channels. 404 if ( read_big_endian( $data, 1 ) != $bit_depth ) { 405 return INVALID; 406 } 407 if ( $i > 32 ) { 408 return ABORTED; // Be reasonable. 409 } 410 } 411 if ( count( $this->features->chan_props ) <= MAX_FEATURES && 412 $box_index <= MAX_VALUE && $bit_depth <= MAX_VALUE && 413 $num_channels <= MAX_VALUE ) { 414 $chan_prop_count = count( $this->features->chan_props ); 415 $this->features->chan_props[$chan_prop_count] = new Chan_Prop(); 416 $this->features->chan_props[$chan_prop_count]->property_index = $box_index; 417 $this->features->chan_props[$chan_prop_count]->bit_depth = $bit_depth; 418 $this->features->chan_props[$chan_prop_count]->num_channels = $num_channels; 419 } else { 420 $this->data_was_skipped = true; 421 } 422 if ( !skip( $this->handle, $box->content_size - ( 1 + $num_channels ) ) ) { 423 return TRUNCATED; 424 } 425 } else if ( $box->type == 'av1C' ) { 426 // See AV1 Codec ISO Media File Format Binding 2.3.1 427 // at https://aomediacodec.github.io/av1-isobmff/#av1c 428 // Only parse the necessary third byte. Assume that the others are valid. 429 if ( $box->content_size < 3 ) { 430 return INVALID; 431 } 432 if ( !( $data = read( $this->handle, 3 ) ) ) { 433 return TRUNCATED; 434 } 435 $byte = read_big_endian( substr( $data, 2, 1 ), 1 ); 436 $high_bitdepth = ( $byte & 0x40 ) != 0; 437 $twelve_bit = ( $byte & 0x20 ) != 0; 438 $monochrome = ( $byte & 0x10 ) != 0; 439 if ( $twelve_bit && !$high_bitdepth ) { 440 return INVALID; 441 } 442 if ( count( $this->features->chan_props ) <= MAX_FEATURES && 443 $box_index <= MAX_VALUE ) { 444 $chan_prop_count = count( $this->features->chan_props ); 445 $this->features->chan_props[$chan_prop_count] = new Chan_Prop(); 446 $this->features->chan_props[$chan_prop_count]->property_index = $box_index; 447 $this->features->chan_props[$chan_prop_count]->bit_depth = 448 $high_bitdepth ? $twelve_bit ? 12 : 10 : 8; 449 $this->features->chan_props[$chan_prop_count]->num_channels = $monochrome ? 1 : 3; 450 } else { 451 $this->data_was_skipped = true; 452 } 453 if ( !skip( $this->handle, $box->content_size - 3 ) ) { 454 return TRUNCATED; 455 } 456 } else if ( $box->type == 'auxC' ) { 457 // See AV1 Image File Format (AVIF) 4 458 // at https://aomediacodec.github.io/av1-avif/#auxiliary-images 459 $kAlphaStr = "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0"; 460 $kAlphaStrLength = 44; // Includes terminating character. 461 if ( $box->content_size >= $kAlphaStrLength ) { 462 if ( !( $data = read( $this->handle, $kAlphaStrLength ) ) ) { 463 return TRUNCATED; 464 } 465 if ( substr( $data, 0, $kAlphaStrLength ) == $kAlphaStr ) { 466 // Note: It is unlikely but it is possible that this alpha plane does 467 // not belong to the primary item or a tile. Ignore this issue. 468 $this->features->has_alpha = true; 469 } 470 if ( !skip( $this->handle, $box->content_size - $kAlphaStrLength ) ) { 471 return TRUNCATED; 472 } 473 } else { 474 if ( !skip( $this->handle, $box->content_size ) ) { 475 return TRUNCATED; 476 } 477 } 478 } else { 479 if ( !skip( $this->handle, $box->content_size ) ) { 480 return TRUNCATED; 481 } 482 } 483 ++$box_index; 484 $num_remaining_bytes -= $box->size; 485 } while ( $num_remaining_bytes > 0 ); 486 return NOT_FOUND; 487 } 488 489 /** 490 * Parses an "iprp" box. 491 * 492 * The "ipco" box contains the properties which are linked to items by the "ipma" box. 493 * 494 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 495 * @return int FOUND on success or an error on failure. 496 */ 497 private function parse_iprp( $num_remaining_bytes ) { 498 do { 499 $box = new Box(); 500 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 501 if ( $status != FOUND ) { 502 return $status; 503 } 504 505 if ( $box->type == 'ipco' ) { 506 $status = $this->parse_ipco( $box->content_size ); 507 if ( $status != NOT_FOUND ) { 508 return $status; 509 } 510 } else if ( $box->type == 'ipma' ) { 511 // See ISO/IEC 23008-12:2017(E) 9.3.2 512 $num_read_bytes = 4; 513 if ( $box->content_size < $num_read_bytes ) { 514 return INVALID; 515 } 516 if ( !( $data = read( $this->handle, $num_read_bytes ) ) ) { 517 return TRUNCATED; 518 } 519 $entry_count = read_big_endian( $data, 4 ); 520 $id_num_bytes = ( $box->version < 1 ) ? 2 : 4; 521 $index_num_bytes = ( $box->flags & 1 ) ? 2 : 1; 522 $essential_bit_mask = ( $box->flags & 1 ) ? 0x8000 : 0x80; 523 524 for ( $entry = 0; $entry < $entry_count; ++$entry ) { 525 if ( $entry >= MAX_PROPS || 526 count( $this->features->props ) >= MAX_PROPS ) { 527 $this->data_was_skipped = true; 528 break; 529 } 530 $num_read_bytes += $id_num_bytes + 1; 531 if ( $box->content_size < $num_read_bytes ) { 532 return INVALID; 533 } 534 if ( !( $data = read( $this->handle, $id_num_bytes + 1 ) ) ) { 535 return TRUNCATED; 536 } 537 $item_id = read_big_endian( 538 substr( $data, 0, $id_num_bytes ), $id_num_bytes ); 539 $association_count = read_big_endian( 540 substr( $data, $id_num_bytes, 1 ), 1 ); 541 542 for ( $property = 0; $property < $association_count; ++$property ) { 543 if ( $property >= MAX_PROPS || 544 count( $this->features->props ) >= MAX_PROPS ) { 545 $this->data_was_skipped = true; 546 break; 547 } 548 $num_read_bytes += $index_num_bytes; 549 if ( $box->content_size < $num_read_bytes ) { 550 return INVALID; 551 } 552 if ( !( $data = read( $this->handle, $index_num_bytes ) ) ) { 553 return TRUNCATED; 554 } 555 $value = read_big_endian( $data, $index_num_bytes ); 556 // $essential = ($value & $essential_bit_mask); // Unused. 557 $property_index = ( $value & ~$essential_bit_mask ); 558 if ( $property_index <= MAX_VALUE && $item_id <= MAX_VALUE ) { 559 $prop_count = count( $this->features->props ); 560 $this->features->props[$prop_count] = new Prop(); 561 $this->features->props[$prop_count]->property_index = $property_index; 562 $this->features->props[$prop_count]->item_id = $item_id; 563 } else { 564 $this->data_was_skipped = true; 565 } 566 } 567 if ( $property < $association_count ) { 568 break; // Do not read garbage. 569 } 570 } 571 572 // If all features are available now, do not look further. 573 $status = $this->features->get_primary_item_features(); 574 if ( $status != NOT_FOUND ) { 575 return $status; 576 } 577 578 // Mostly if 'data_was_skipped'. 579 if ( !skip( $this->handle, $box->content_size - $num_read_bytes ) ) { 580 return TRUNCATED; 581 } 582 } else { 583 if ( !skip( $this->handle, $box->content_size ) ) { 584 return TRUNCATED; 585 } 586 } 587 $num_remaining_bytes -= $box->size; 588 } while ( $num_remaining_bytes > 0 ); 589 return NOT_FOUND; 590 } 591 592 /** 593 * Parses an "iref" box. 594 * 595 * The "dimg" boxes contain links between tiles and their parent items, which 596 * can be used to infer bit depth and number of channels for the primary item 597 * when the latter does not have these properties. 598 * 599 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 600 * @return int FOUND on success or an error on failure. 601 */ 602 private function parse_iref( $num_remaining_bytes ) { 603 while ( $num_remaining_bytes > 0 ) { 604 $box = new Box(); 605 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 606 if ( $status != FOUND ) { 607 return $status; 608 } 609 610 if ( $box->type == 'dimg' ) { 611 // See ISO/IEC 14496-12:2015(E) 8.11.12.2 612 $num_bytes_per_id = ( $box->version == 0 ) ? 2 : 4; 613 $num_read_bytes = $num_bytes_per_id + 2; 614 if ( $box->content_size < $num_read_bytes ) { 615 return INVALID; 616 } 617 if ( !( $data = read( $this->handle, $num_read_bytes ) ) ) { 618 return TRUNCATED; 619 } 620 $from_item_id = read_big_endian( $data, $num_bytes_per_id ); 621 $reference_count = read_big_endian( substr( $data, $num_bytes_per_id, 2 ), 2 ); 622 623 for ( $i = 0; $i < $reference_count; ++$i ) { 624 if ( $i >= MAX_TILES ) { 625 $this->data_was_skipped = true; 626 break; 627 } 628 $num_read_bytes += $num_bytes_per_id; 629 if ( $box->content_size < $num_read_bytes ) { 630 return INVALID; 631 } 632 if ( !( $data = read( $this->handle, $num_bytes_per_id ) ) ) { 633 return TRUNCATED; 634 } 635 $to_item_id = read_big_endian( $data, $num_bytes_per_id ); 636 $tile_count = count( $this->features->tiles ); 637 if ( $from_item_id <= MAX_VALUE && $to_item_id <= MAX_VALUE && 638 $tile_count < MAX_TILES ) { 639 $this->features->tiles[$tile_count] = new Tile(); 640 $this->features->tiles[$tile_count]->tile_item_id = $to_item_id; 641 $this->features->tiles[$tile_count]->parent_item_id = $from_item_id; 642 } else { 643 $this->data_was_skipped = true; 644 } 645 } 646 647 // If all features are available now, do not look further. 648 $status = $this->features->get_primary_item_features(); 649 if ( $status != NOT_FOUND ) { 650 return $status; 651 } 652 653 // Mostly if 'data_was_skipped'. 654 if ( !skip( $this->handle, $box->content_size - $num_read_bytes ) ) { 655 return TRUNCATED; 656 } 657 } else { 658 if ( !skip( $this->handle, $box->content_size ) ) { 659 return TRUNCATED; 660 } 661 } 662 $num_remaining_bytes -= $box->size; 663 } 664 return NOT_FOUND; 665 } 666 667 /** 668 * Parses a "meta" box. 669 * 670 * It looks for the primary item ID in the "pitm" box and recurses into other boxes 671 * to find its features. 672 * 673 * @param int $num_remaining_bytes The number of bytes that should be available from the resource. 674 * @return int FOUND on success or an error on failure. 675 */ 676 private function parse_meta( $num_remaining_bytes ) { 677 do { 678 $box = new Box(); 679 $status = $box->parse( $this->handle, $this->num_parsed_boxes, $num_remaining_bytes ); 680 if ( $status != FOUND ) { 681 return $status; 682 } 683 684 if ( $box->type == 'pitm' ) { 685 // See ISO/IEC 14496-12:2015(E) 8.11.4.2 686 $num_bytes_per_id = ( $box->version == 0 ) ? 2 : 4; 687 if ( $num_bytes_per_id > $num_remaining_bytes ) { 688 return INVALID; 689 } 690 if ( !( $data = read( $this->handle, $num_bytes_per_id ) ) ) { 691 return TRUNCATED; 692 } 693 $primary_item_id = read_big_endian( $data, $num_bytes_per_id ); 694 if ( $primary_item_id > MAX_VALUE ) { 695 return ABORTED; 696 } 697 $this->features->has_primary_item = true; 698 $this->features->primary_item_id = $primary_item_id; 699 if ( !skip( $this->handle, $box->content_size - $num_bytes_per_id ) ) { 700 return TRUNCATED; 701 } 702 } else if ( $box->type == 'iprp' ) { 703 $status = $this->parse_iprp( $box->content_size ); 704 if ( $status != NOT_FOUND ) { 705 return $status; 706 } 707 } else if ( $box->type == 'iref' ) { 708 $status = $this->parse_iref( $box->content_size ); 709 if ( $status != NOT_FOUND ) { 710 return $status; 711 } 712 } else { 713 if ( !skip( $this->handle, $box->content_size ) ) { 714 return TRUNCATED; 715 } 716 } 717 $num_remaining_bytes -= $box->size; 718 } while ( $num_remaining_bytes != 0 ); 719 // According to ISO/IEC 14496-12:2012(E) 8.11.1.1 there is at most one "meta". 720 return INVALID; 721 } 722 723 /** 724 * Parses a file stream. 725 * 726 * The file type is checked through the "ftyp" box. 727 * 728 * @return bool True if the input stream is an AVIF bitstream or false. 729 */ 730 public function parse_ftyp() { 731 $box = new Box(); 732 $status = $box->parse( $this->handle, $this->num_parsed_boxes ); 733 if ( $status != FOUND ) { 734 return false; 735 } 736 737 if ( $box->type != 'ftyp' ) { 738 return false; 739 } 740 // Iterate over brands. See ISO/IEC 14496-12:2012(E) 4.3.1 741 if ( $box->content_size < 8 ) { 742 return false; 743 } 744 for ( $i = 0; $i + 4 <= $box->content_size; $i += 4 ) { 745 if ( !( $data = read( $this->handle, 4 ) ) ) { 746 return false; 747 } 748 if ( $i == 4 ) { 749 continue; // Skip minor_version. 750 } 751 if ( substr( $data, 0, 4 ) == 'avif' || substr( $data, 0, 4 ) == 'avis' ) { 752 return skip( $this->handle, $box->content_size - ( $i + 4 ) ); 753 } 754 if ( $i > 32 * 4 ) { 755 return false; // Be reasonable. 756 } 757 758 } 759 return false; // No AVIF brand no good. 760 } 761 762 /** 763 * Parses a file stream. 764 * 765 * Features are extracted from the "meta" box. 766 * 767 * @return bool True if the main features of the primary item were parsed or false. 768 */ 769 public function parse_file() { 770 $box = new Box(); 771 while ( $box->parse( $this->handle, $this->num_parsed_boxes ) == FOUND ) { 772 if ( $box->type === 'meta' ) { 773 if ( $this->parse_meta( $box->content_size ) != FOUND ) { 774 return false; 775 } 776 return true; 777 } 778 if ( !skip( $this->handle, $box->content_size ) ) { 779 return false; 780 } 781 } 782 return false; // No "meta" no good. 783 } 784 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 3 08:20:36 2026 | Cross-referenced by PHPXref |