Skip to content
Yoshihiro Yamazaki edited this page Nov 27, 2026 · 1 revision

Home > 利用方

利用方

メソッドの一覧は 機能、配列のキーは 設計 にあります。 例の input.ivf と input.obu は、aomenc で 64x48 の 3 フレームを IVF と OBU の列 (--obu) に書き出したものです。

PHP 8.2 以降では動的プロパティの Deprecated メッセージが出るので、例は php -d error_reporting='E_ALL & ~E_DEPRECATED' で動かしています。

画像の大きさ

parse() には IVF でも OBU の列でも渡せます。sequence header から大きさなどを読みます。

require_once 'IO/AV1.php';

$av1 = new IO_AV1();
$av1->parse(file_get_contents($argv[1]));
if ($av1->IVF !== null) {
    $ivf = $av1->IVF;
    echo $ivf->codec, ' ', $ivf->width, 'x', $ivf->height, ' ', $ivf->fps, 'fps frames:', $ivf->getFrameNum(), PHP_EOL;
    $obuLists = array();
    foreach ($ivf->frames as $frame) {
        $obuLists []= $frame['obu'];
    }
} else {
    $obuLists = array($av1->OBU);
}
foreach ($obuLists as $obus) {
    foreach ($obus->OBUs as $obu) {
        if (isset($obu['sequence_header_obu'])) {
            $sh = $obu['sequence_header_obu'];
            $cc = $sh['color_config'];
            echo 'profile:', $sh['seq_profile'], ' level_idx:', $sh['seq_level_idx'][0];
            echo ' ', $sh['frame_width_minus_1'] + 1, 'x', $sh['frame_height_minus_1'] + 1;
            echo ' high_bitdepth:', $cc['high_bitdepth'], ' mono_chrome:', $cc['mono_chrome'];
            echo ' subsampling:', $cc['subsampling_x'], ',', $cc['subsampling_y'], PHP_EOL;
            break 2;
        }
    }
}
% php info.php input.ivf
AV01 64x48 30fps frames:3
profile:0 level_idx:0 64x48 high_bitdepth:0 mono_chrome:0 subsampling:1,1
% php info.php input.obu
profile:0 level_idx:0 64x48 high_bitdepth:0 mono_chrome:0 subsampling:1,1

OBU の一覧

require_once 'IO/AV1.php';

$av1 = new IO_AV1();
$av1->parse(file_get_contents('input.obu'));
foreach ($av1->OBU->OBUs as $i => $obu) {
    $type = $obu['obu_header']['obu_type'];
    echo $i, ' ', IO_AV1_OBU::getOBUTypeName($type);
    echo ' offset:', $obu['_offset'], ' size:', $obu['obu_size'], PHP_EOL;
}
0 OBU_TEMPORAL_DELIMITER offset:2 size:0
1 OBU_SEQUENCE_HEADER offset:4 size:10
2 OBU_FRAME offset:17 size:756
3 OBU_TEMPORAL_DELIMITER offset:775 size:0
4 OBU_FRAME offset:777 size:72
5 OBU_TEMPORAL_DELIMITER offset:851 size:0
6 OBU_FRAME offset:853 size:67

_offset は中身の先頭 (OBU ヘッダと大きさの後) の位置です。temporal delimiter は中身が空です。

IVF のフレーム

フレームごとのタイムスタンプと OBU の種類を表示し、最初のフレームの中身を OBU の列として書き出します。

require_once 'IO/AV1.php';

$av1 = new IO_AV1();
$av1->parse(file_get_contents('input.ivf'));
$ivf = $av1->IVF;
foreach ($ivf->frames as $i => $frame) {
    $types = array();
    foreach ($frame['obu']->OBUs as $obu) {
        $types []= $obu['obu_header']['obu_type'];
    }
    echo $i, ' time:', $frame['timestamp'], ' size:', $frame['frameSize'];
    echo ' obu_type:', implode(',', $types), PHP_EOL;
}
file_put_contents('frame0.obu', $ivf->getPayload(0));  // OBU 列のまま書き出す
0 time:0 size:773 obu_type:2,1,6
1 time:1 size:76 obu_type:2,6
2 time:2 size:71 obu_type:2,6

IO_AV1_Bit

仕様書の記法 (f(n), uvlc(), le(n), leb128()) で読めます。

require_once 'IO/AV1/Bit.php';

$bit = new IO_AV1_Bit();
$bit->input("\xe5\x8e\x26\x01\x02\x28");
echo $bit->get_leb128(), PHP_EOL;  // 624485 (e5 8e 26)
echo $bit->get_le(2), PHP_EOL;     // 513 (01 02)
echo $bit->get_f(2), PHP_EOL;      // 0 (0x28 = 00 1 010 00)
echo $bit->get_uvlc(), PHP_EOL;    // 0 (1)
echo $bit->get_uvlc(), PHP_EOL;    // 1 (010)

av1dump.php

構造をダンプします。-f - で標準入力から読みます。-h を付けると、OBU ごとに中身の 16 進ダンプも出します。

% php vendor/yoya/io_av1/sample/av1dump.php -f input.ivf
signature:DKIF version:0 headerLength:32
codec:AV01 width:64 height:48 frameRate:30=30/1 frameNum:3
[0]frameSize:773 time:0 payload:12 00 0a 0a 00 00 00 02 ...
  [0]obu_type:2 (OBU_TEMPORAL_DELIMITER)  obu_size:0
  obu_header:
    obu_forbidden_bit:0 obu_type:2 obu_extension_flag:0
    obu_has_size_field:1 obu_reserved_1bit:0
  dump_temporal_delimiter_obu:
    SeenFrameHeader:0
  [1]obu_type:1 (OBU_SEQUENCE_HEADER)  obu_size:10
  obu_header:
    obu_forbidden_bit:0 obu_type:1 obu_extension_flag:0
    obu_has_size_field:1 obu_reserved_1bit:0
  sequence_header_obu:
(omit)
    frame_width_bits_minus_1:5 frame_height_bits_minus_1:5
    frame_width_minus_1:63 frame_height_minus_1:47
        frame_id_numbers_present_flag:0
(omit)
          (subsampling_x:1) (subsampling_y:1)
        chroma_sample_position:0(CSP_UNKNOWN)
      separate_uv_delta_q:0
    film_grain_params_present:0
  [2]obu_type:6 (OBU_FRAME)  obu_size:756
  obu_header:
    obu_forbidden_bit:0 obu_type:6 obu_extension_flag:0
    obu_has_size_field:1 obu_reserved_1bit:0
(omit)

-f が無いか、ファイルが読めないときは、使い方を標準エラーに出して終了コード 1 で終わります。

Clone this wiki locally