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

Home > 利用方

利用方

メソッドの一覧は 機能、プロパティや NAL ユニットの分け方は 設計 にあります。 例の input.265 は、VPS・SPS (64x64)・PPS・IDR の 4 つの NAL ユニットからなるストリームです。

NAL ユニットの一覧

require_once 'IO/HEVC.php';

$hevc = new IO_HEVC();
$hevc->parse(file_get_contents($argv[1]));
foreach ($hevc->nalList as $nal) {
    $type = $nal->header['nal_unit_type'];
    echo $type, '(', $nal->getUnitTypeString($type), ')';
    echo ' offset:', $nal->_offset, ' length:', $nal->_length, PHP_EOL;
}
% php list.php input.265
32(VPS_NUT) offset:4 length:22
33(SPS_NUT) offset:30 length:23
34(PPS_NUT) offset:57 length:7
19(IDR_W_RADL) offset:68 length:6

SPS の値

画像の大きさ、色差フォーマット、ビット深度、プロファイルとレベルを表示します。general_level_idc はレベルの 30 倍です。

require_once 'IO/HEVC.php';

$hevc = new IO_HEVC();
$hevc->parse(file_get_contents($argv[1]));
$sps = $hevc->getNALByType(33);  // SPS_NUT
if ($sps === null) {
    exit("no SPS\n");
}
$u = $sps->unit;
$ptl = $u->profile_tier_level;
$chroma = array(0 => '4:0:0', 1 => '4:2:0', 2 => '4:2:2', 3 => '4:4:4');
echo $u->pic_width_in_luma_samples, 'x', $u->pic_height_in_luma_samples;
echo ' ', $chroma[$u->chroma_format_idc];
echo ' ', 8 + $u->bit_depth_luma_minus8, 'bit';
echo ' profile_idc:', $ptl->general_profile_idc;
echo ' level:', $ptl->general_level_idc / 30, PHP_EOL;
% php info.php input.265
64x64 4:2:0 8bit profile_idc:1 level:3.1

conformance window (conformance_window_flag が 1) があるときは、表示する大きさは conf_win_*_offset の分だけ小さくなります。

元のバイト列を取り出す

getNALRawDataByType() は、開始コードを除いた NAL ユニットのバイト列を返します。NAL ヘッダとエミュレーション防止バイトを含むので、hvcC などにそのまま入れられます。

require_once 'IO/HEVC.php';

$hevc = new IO_HEVC();
$hevc->parse(file_get_contents('input.265'));
echo bin2hex($hevc->getNALRawDataByType(34)), PHP_EOL;  // PPS_NUT
4401c172b46240

指数ゴロム符号を読む

IO_HEVC_Bit は、HEVC の RBSP を読むための IO_Bit です。

require_once 'IO/HEVC.php';

$bit = new IO_HEVC_Bit();
$bit->input("\x00\x00\x03\x01\xa6");
echo $bit->getUIBits(16), PHP_EOL;  // 0
echo $bit->getUI8(), PHP_EOL;       // 1 (03 を読み飛ばす)
// 0xa6 = 1 010 011 0
echo $bit->getUIBitsEG(), PHP_EOL;  // 0
echo $bit->getUIBitsEG(), PHP_EOL;  // 1
echo $bit->getSIBitsEG(), PHP_EOL;  // -1 (符号無しで 2)

サンプル

hevcdump.php

NAL ユニットごとにダンプします。-f - で標準入力から読みます。

% php vendor/yoya/io_hevc/sample/hevcdump.php -f input.265
header:
    forbidden_zero_bit:0 nal_unit_type:32(VPS_NUT)
    nuh_layer_id:0 nuh_temporal_id_plus1:1
unit:
    vps_video_parameter_set_id:0 vps_base_layer_internal_flag:1
    vps_base_layer_available_flag:1 vps_max_layers_minus1:0
    vps_max_sub_layers_minus1:0 vps_temporal_id_nesting_flag:1
    profile_tier_level: profilePresentFlag:1 maxNumSubLayersMinus1:0
        general_profile_space:0 general_tier_flag:0 general_profile_idc:1
        general_profile_compatibility_flag: 01000000 00000000 00000000 00000000
        general_progressive_source_flag:1 general_interlaced_source_flag:0
        general_non_packed_constraint_flag:0
        general_frame_only_constraint_flag:1
        general_inbld_flag:0
        general_level_idc:93
        sub_layer_profile_present_flag, sub_layer_level_present_flag: (count:0)
(omit)
        general_level_idc:93
        sub_layer_profile_present_flag, sub_layer_level_present_flag: (count:0)
    ps_seq_parameter_set_id:0 chroma_format_idc:1
    pic_width_in_luma_samples:64 pic_height_in_luma_samples:64
    conformance_window_flag:0
    bit_depth_luma_minus8:0 bit_depth_chroma_minus8:0
    log2_max_pic_order_cnt_lsb_minus4:4
    sps_sub_layer_ordering_info_present_flag:1
header:
    forbidden_zero_bit:0 nal_unit_type:34(PPS_NUT)
    nuh_layer_id:0 nuh_temporal_id_plus1:1
unit:
header:
    forbidden_zero_bit:0 nal_unit_type:19(IDR_W_RADL)
    nuh_layer_id:0 nuh_temporal_id_plus1:1
unit:

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

hevcnalu.php

-t で指定した種類の最初の NAL ユニットを、NAL ヘッダ込みのバイト列で標準出力に出します。

% php vendor/yoya/io_hevc/sample/hevcnalu.php -f input.265 -t 33 > sps.bin
% xxd sps.bin
00000000: 4201 0101 4000 0003 0090 0000 0300 0003  B...@...........
00000010: 005d a020 8105 97                        .]. ...

Clone this wiki locally