openh264

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 6 Imported by: 1

README

openh264-go

OpenH264の静的ライブラリバインディングを提供するGoパッケージです。

pion/mediadevicesのOpenH264実装をベースに、静的ライブラリを同梱した独立パッケージとして切り出しました。

特徴

  • 静的リンク: OpenH264の静的ライブラリ(.a)を同梱しているため、システムにOpenH264をインストールする必要がありません。go buildするだけで、OpenH264が実行バイナリに組み込まれます。
  • クロスプラットフォーム: Linux (amd64/arm64/armv7)、macOS (amd64/arm64)、Windows (amd64)をサポート
  • シンプルなAPI: エンコーダーとデコーダーを独立した構造体として提供
  • 外部依存なし: 純粋なGoとCGOのみで動作

インストール

go get github.com/Azunyan1111/openh264-go

使用例

エンコード
package main

import (
    "image"
    openh264 "github.com/Azunyan1111/openh264-go"
)

func main() {
    // エンコーダーの作成
    params := openh264.NewEncoderParams()
    params.Width = 640
    params.Height = 480
    params.BitRate = 500000

    encoder, err := openh264.NewEncoder(params)
    if err != nil {
        panic(err)
    }
    defer encoder.Close()

    // YCbCr画像をエンコード
    img := image.NewYCbCr(
        image.Rect(0, 0, 640, 480),
        image.YCbCrSubsampleRatio420,
    )
    // ... imgにデータを設定 ...

    h264Data, err := encoder.Encode(img)
    if err != nil {
        panic(err)
    }
    // h264Dataを使用
}
デコード
package main

import (
    "io"
    openh264 "github.com/Azunyan1111/openh264-go"
)

func main() {
    // io.ReaderからH.264ストリームを読み込む場合
    var h264Stream io.Reader // = ...

    decoder, err := openh264.NewDecoder(h264Stream)
    if err != nil {
        panic(err)
    }
    defer decoder.Close()

    for {
        img, err := decoder.Read()
        if err == io.EOF {
            break
        }
        if err != nil {
            panic(err)
        }
        // imgを使用 (*image.YCbCr)
    }
}
生データを直接デコード
// Decodeメソッドで生のH.264データを直接デコード
img, err := decoder.Decode(h264Data)
if err != nil {
    panic(err)
}
if img != nil {
    // フレームが利用可能
}

// ストリーム終了時にバッファに残ったフレームを取得
for {
    img, err := decoder.Flush()
    if err != nil || img == nil {
        break
    }
    // 残りのフレームを処理
}

動的リンク

システムにインストールされたOpenH264を使用する場合は、dynamicビルドタグを指定します:

go build -tags dynamic

この場合、pkg-configでOpenH264が見つかる必要があります。

ライセンス

  • Goバインディングコード: MIT License (based on pion/mediadevices)
  • OpenH264静的ライブラリ (lib/): BSD-2-Clause (Cisco Systems) - lib/LICENSE

謝辞

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder represents an H.264 decoder

func NewDecoder

func NewDecoder(r io.Reader) (*Decoder, error)

NewDecoder creates a new OpenH264 decoder that reads from the given reader

func NewDecoderWithParams added in v0.2.0

func NewDecoderWithParams(r io.Reader, params DecoderParams) (*Decoder, error)

NewDecoderWithParams creates a new OpenH264 decoder with the given parameters

func (*Decoder) Close

func (d *Decoder) Close() error

Close releases the decoder resources

func (*Decoder) Decode

func (d *Decoder) Decode(data []byte) (*image.YCbCr, error)

Decode decodes raw H.264 data and returns the decoded frame if available

func (*Decoder) Flush

func (d *Decoder) Flush() (*image.YCbCr, error)

Flush flushes any remaining frames from the decoder buffer

func (*Decoder) Read

func (d *Decoder) Read() (*image.YCbCr, error)

Read reads and decodes the next frame from the stream

type DecoderParams added in v0.2.0

type DecoderParams struct {
	ErrorConcealment ErrorConcealmentMode
}

DecoderParams stores libopenh264 specific decoding parameters.

func NewDecoderParams added in v0.2.0

func NewDecoderParams() DecoderParams

NewDecoderParams returns default openh264 decoder parameters.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder represents an H.264 encoder

func NewEncoder

func NewEncoder(params EncoderParams) (*Encoder, error)

NewEncoder creates a new OpenH264 encoder with the given parameters

func (*Encoder) Close

func (e *Encoder) Close() error

Close releases the encoder resources

func (*Encoder) Encode

func (e *Encoder) Encode(img *image.YCbCr) ([]byte, error)

Encode encodes a YCbCr image and returns the encoded H.264 data

func (*Encoder) ForceKeyFrame

func (e *Encoder) ForceKeyFrame() error

ForceKeyFrame forces the next frame to be encoded as a key frame

func (*Encoder) SetBitRate

func (e *Encoder) SetBitRate(bitrate int) error

SetBitRate sets the target bitrate dynamically

type EncoderParams

type EncoderParams struct {
	Width               int
	Height              int
	BitRate             int
	MaxFrameRate        float32
	UsageType           UsageTypeEnum
	RCMode              RCModeEnum
	EnableFrameSkip     bool
	MaxNalSize          uint
	IntraPeriod         uint
	MultipleThreadIdc   int
	SliceNum            uint
	SliceMode           SliceModeEnum
	SliceSizeConstraint uint
}

EncoderParams stores libopenh264 specific encoding parameters.

func NewEncoderParams

func NewEncoderParams() EncoderParams

NewEncoderParams returns default openh264 encoder parameters.

type ErrorConcealmentMode added in v0.2.0

type ErrorConcealmentMode int

ErrorConcealmentMode represents the error concealment mode for the decoder

const (
	ErrorConDisable   ErrorConcealmentMode = C.ERROR_CON_DISABLE
	ErrorConSliceCopy ErrorConcealmentMode = C.ERROR_CON_SLICE_COPY
)

type RCModeEnum

type RCModeEnum int
const (
	RCQualityMode         RCModeEnum = C.RC_QUALITY_MODE
	RCBitrateMode         RCModeEnum = C.RC_BITRATE_MODE
	RCBufferbaseedMode    RCModeEnum = C.RC_BUFFERBASED_MODE
	RCTimestampMode       RCModeEnum = C.RC_TIMESTAMP_MODE
	RCBitrateModePostSkip RCModeEnum = C.RC_BITRATE_MODE_POST_SKIP
	RCOffMode             RCModeEnum = C.RC_OFF_MODE
)

type SliceModeEnum

type SliceModeEnum uint
const (
	SMSingleSlice      SliceModeEnum = C.SM_SINGLE_SLICE
	SMFixedslcnumSlice SliceModeEnum = C.SM_FIXEDSLCNUM_SLICE
	SMRasterSlice      SliceModeEnum = C.SM_RASTER_SLICE
	SMSizelimitedSlice SliceModeEnum = C.SM_SIZELIMITED_SLICE
)

type UsageTypeEnum

type UsageTypeEnum int
const (
	CameraVideoRealTime      UsageTypeEnum = C.CAMERA_VIDEO_REAL_TIME
	ScreenContentRealTime    UsageTypeEnum = C.SCREEN_CONTENT_REAL_TIME
	CameraVideoNonRealTime   UsageTypeEnum = C.CAMERA_VIDEO_NON_REAL_TIME
	ScreenContentNonRealTime UsageTypeEnum = C.SCREEN_CONTENT_NON_REAL_TIME
	InputContentTypeAll      UsageTypeEnum = C.INPUT_CONTENT_TYPE_ALL
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL