Search by

yoya / io_hevc

yoya

HEVC library

2.1 2026-11-27 00:26 UTC

This package is auto-updated.

Last update: 2026-11-28 11:39:32 UTC


README

English version: README.en.md

HEVC (H.265) のバイトストリーム (Annex B 形式) を NAL ユニットに分けて解釈する為のライブラリです。 VPS と SPS の主な値 (プロファイル・レベル、画像サイズ、色差フォーマット、ビット深度) を読めます。 IO_HEIF が HEVC の Annex B バイトストリームから HEIC の hvcC box を作るときに使います。 IO_Bit が必要です。利用に制限はかけません。コピーも改変もご自由にどうぞ。MIT ライセンスにしました。 自分の環境だと動かないとか、機能が欲しいとかいった要望があれば連絡下さい。

License

MIT License

Install

% composer require yoya/io_hevc

Usage

  • 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
(omit)
        general_level_idc:93
        sub_layer_profile_present_flag, sub_layer_level_present_flag: (count:0)
header:
    forbidden_zero_bit:0 nal_unit_type:33(SPS_NUT)
    nuh_layer_id:0 nuh_temporal_id_plus1:1
unit:
(omit)
    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:
  • hevcnalu.php (指定した種類の最初の 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                        .]. ...
  • PHP から使う
require_once 'IO/HEVC.php';

$hevc = new IO_HEVC();
$hevc->parse(file_get_contents('input.265'));
$sps = $hevc->getNALByType(33);               // SPS_NUT
echo $sps->unit->pic_width_in_luma_samples, "x", $sps->unit->pic_height_in_luma_samples, "\n";
echo "profile_idc:", $sps->unit->profile_tier_level->general_profile_idc, "\n";
echo "level_idc:", $sps->unit->profile_tier_level->general_level_idc, "\n";
$pps = $hevc->getNALRawDataByType(34);        // PPS_NUT (NAL ヘッダ込みのバイト列)
echo bin2hex($pps), "\n";
64x64
profile_idc:1
level_idc:93
4401c172b46240

設計概要

クラス 役割
IO_HEVC 入口。開始コード (00 00 01 / 00 00 00 01) でデータを NAL ユニットに分け、それぞれを IO_HEVC_NAL で解釈する。getNALByType(), getNALRawDataByType(), dump()
IO_HEVC_NAL NAL ユニット 1 個。ヘッダ (header 配列: nal_unit_type, nuh_layer_id, nuh_temporal_id_plus1) と、種類ごとの中身 (unit) を持つ
IO_HEVC_NAL_VPS VPS。profile_tier_level まで
IO_HEVC_NAL_SPS SPS。profile_tier_level、画像サイズ、conformance window、ビット深度、log2_max_pic_order_cnt_lsb_minus4 まで
IO_HEVC_NAL_PPS, IO_HEVC_NAL_Unknown 中身を解釈しない (PPS とそれ以外の種類)
IO_HEVC_ProfileTierLevel profile_tier_level。サブレイヤの値も読む (IO/HEVC/ProfileTierLevel.php。以前のファイル名 IO/HEVC/ProfleTierLevel.php も require できる)
IO_HEVC_Bit IO_Bit の派生。getUI8 / getUIBit / getData でエミュレーション防止バイト (00 00 03 の 03) を読み飛ばす。指数ゴロム符号の getUIBitsEG() / getSIBitsEG()
IO_Bit (別パッケージ) ビット単位の読み込み
  • 解釈した値は、仕様書のシンタックス要素名のプロパティで持つ ($nal->unit->pic_width_in_luma_samples など)。
  • profile_tier_level のサブレイヤ i の値 (sub_layer_profile_idc[i], sub_layer_level_idc[i] など) は、sub_layers[i] にシンタックス要素名をキーにした配列で持つ。present_flag が 0 の値は入らない。
  • getNALByType() / getNALRawDataByType() は、その種類の最初の NAL ユニットを返す。無ければ null。
  • getNALRawDataByType() は開始コードを除き、NAL ヘッダとエミュレーション防止バイトを含んだ元のバイト列を返す。
  • 書き出し (build) の機能はありません。

既知の制限

  • VPS と SPS は先頭の一部だけを解釈する。PPS、スライス、SEI などは解釈しない。
  • nuh_layer_id が 0 でない SPS (H.265 Annex F) には対応していない。
  • SPS のプロパティ名 ps_seq_parameter_set_id は仕様書の sps_seq_parameter_set_id の誤記だが、互換性のためそのままにしている。
  • hevcdump.php の -h -t -v -d は受け付けるが効果は無い。hevcnalu.php は -t が必須で、標準入力 (-f -) には対応していない。

Test

% php tests/run.php

Reference

IO_Bit の利用箇所

IO_HEVC_Bit は IO_Bit を継承し、VPS・SPS のビットフィールドの読み込みに Exp-Golomb 符号の読み取りを加えます。

IO_Bit Wiki