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

Home > 利用方

利用方

データの構造は 設計、メソッドの一覧は 機能 にあります。

例のファイルは次のものです。

  • frame.vp6: VP6 のキーフレーム 1 つ (9 バイト)。量子化値 60、サブバージョン 8、マクロブロックが縦 2・横 3
  • ea.vp6: EA VP6 コンテナ。MVhd (48x32、2 フレーム、30fps) に続いて、frame.vp6 と同じキーフレームの MV0K と、キーフレームでない 3 バイトの MV0F

composer で入れた場合は require_once 'IO/VP6.php'; などの代わりに require 'vendor/autoload.php'; と書きます。

ヘッダを読む

parse() はヘッダを読みながら標準出力に表示します。

require_once 'IO/VP6.php';

$vp6 = new IO_VP6();
$vp6->parse(file_get_contents('frame.vp6'));  // prints the header
frameType:0 quantizer:60 separated_coeff:0
sub_version:8 filter_header:3 interlace:0
rows:2 cols:3 disp_rows:2 disp_cols:3
range_decoder (code_word:0xabcdef)

ヘッダの値を取り出す

parse() は値を返さないので、値を使いたいときは表示を受け取って読みます。

require_once 'IO/VP6.php';

$vp6 = new IO_VP6();
ob_start();
$vp6->parse(file_get_contents('frame.vp6'));
$text = ob_get_clean();

// "name:value" pairs of the printed header
preg_match_all('/(\w+):(\w+)/', $text, $m);
$header = array_combine($m[1], $m[2]);
printf("key frame:%s size:%dx%d (macroblocks)\n",
       $header['frameType'] == 0 ? 'yes' : 'no', $header['cols'], $header['rows']);
key frame:yes size:3x2 (macroblocks)

EA コンテナからフレームを取り出す

チャンクの一覧を表示し、映像のフレームを 1 つずつ VP6 として読む例です。 IO/VP6/EA.php は IO_Bit を読まないので、先に IO/VP6.php を読みます。

require_once 'IO/VP6.php';
require_once 'IO/VP6/EA.php';

$data = file_get_contents('ea.vp6');
if (! IO_VP6_EA::is_ea($data)) {
    exit("not an EA VP6 container\n");
}
$ea = new IO_VP6_EA();
$ea->parse($data);
foreach ($ea->chunks as $chunk) {
    echo $chunk['name'].": ".strlen($chunk['data'])." bytes\n";
}
foreach ($ea->getFrames() as $idx => $frame) {
    echo "[$idx]\n";
    $vp6 = new IO_VP6();
    $vp6->parse($frame['Data']);
}
MVhd: 24 bytes
MV0K: 9 bytes
MV0F: 3 bytes
[0]
frameType:0 quantizer:60 separated_coeff:0
sub_version:8 filter_header:3 interlace:0
rows:2 cols:3 disp_rows:2 disp_cols:3
range_decoder (code_word:0xabcdef)
[1]
frameType:1 quantizer:60 separated_coeff:0

dump() はチャンクの名前と長さ、MVhd (と AVhd) の中身を表示します。

require_once 'IO/VP6.php';
require_once 'IO/VP6/EA.php';

$ea = new IO_VP6_EA();
$ea->parse(file_get_contents('ea.vp6'));
$ea->dump();
## name:MVhd len:24
MVhd: width:48 height:32 numFrames:2 largest:17 rate=30/1
## name:MV0K len:9
## name:MV0F len:3

エラーを扱う

データが途中で切れていると parse() が IO_Bit_Exception を投げます。Exception の子クラスなので、まとめて受けられます。 例の broken.vp6 は frame.vp6 の先頭 4 バイトです。それまでに読んだ分は表示されます。

require_once 'IO/VP6.php';

$vp6 = new IO_VP6();
try {
    $vp6->parse(file_get_contents('broken.vp6'));
} catch (Exception $e) {  // IO_Bit_Exception too
    echo get_class($e).": ".$e->getMessage()."\n";
}
frameType:0 quantizer:60 separated_coeff:0
sub_version:8 filter_header:3 interlace:0
IO_Bit_Exception: getUI8: 4 < 4 + 1

サブバージョンが 8 より大きい、インターレース、縦横の数が 0 のときは、例外ではなく STDERR にメッセージ (sub_version:9 > 8. など) を出して戻ります。

ダンプする

sample/vp6dump.php は、EA コンテナならチャンクを表示してから映像のフレームを、そうでなければファイル全体を 1 つのフレームとして parse() します。 Composer で入れたプロジェクトのトップで動かします。

% php vendor/yoya/io_vp6/sample/vp6dump.php -f frame.vp6
--- EA container
--- VP6 code
[0]
frameType:0 quantizer:60 separated_coeff:0
sub_version:8 filter_header:3 interlace:0
rows:2 cols:3 disp_rows:2 disp_cols:3
range_decoder (code_word:0xabcdef)
% php vendor/yoya/io_vp6/sample/vp6dump.php -f ea.vp6
--- EA container
## name:MVhd len:24
MVhd: width:48 height:32 numFrames:2 largest:17 rate=30/1
## name:MV0K len:9
## name:MV0F len:3
--- VP6 code
[0]
frameType:0 quantizer:60 separated_coeff:0
sub_version:8 filter_header:3 interlace:0
rows:2 cols:3 disp_rows:2 disp_cols:3
range_decoder (code_word:0xabcdef)
[1]
frameType:1 quantizer:60 separated_coeff:0

-f - で標準入力から読みます。

% php vendor/yoya/io_vp6/sample/vp6dump.php -f - < frame.vp6

-f が無いか読めないファイルでは、使い方を STDERR に出して終了コード 1 で終わります。 データが途中で切れているときは、ERROR: vp6dump: <ファイル名>: に続けて例外のメッセージとスタックトレースを標準出力に出し、終了コード 1 で終わります。

Clone this wiki locally