av.go multi changes

This commit is contained in:
nareix 2016-06-22 19:21:01 +08:00
parent 9b9965cca0
commit 1420483262

86
av.go
View File

@ -2,9 +2,10 @@ package av
import (
"fmt"
"time"
)
type SampleFormat int
type SampleFormat uint8
const (
U8 = SampleFormat(iota + 1)
@ -71,7 +72,7 @@ func (self SampleFormat) IsPlanar() bool {
}
}
type ChannelLayout uint64
type ChannelLayout uint16
func (self ChannelLayout) String() string {
return fmt.Sprintf("%dch", self.Count())
@ -106,17 +107,55 @@ func (self ChannelLayout) Count() (n int) {
return
}
const (
H264 = iota+0x264
AAC
PCM_MULAW
PCM_ALAW
type CodecType uint32
const codecTypeAudioBit = 0x1
const codecTypeOtherBits = 1
func (self CodecType) String() string {
switch self {
case H264:
return "H264"
case AAC:
return "AAC"
case PCM_MULAW:
return "PCM_MULAW"
case PCM_ALAW:
return "PCM_ALAW"
}
return ""
}
func (self CodecType) IsAudio() bool {
return self&codecTypeAudioBit != 0
}
func (self CodecType) IsVideo() bool {
return self&codecTypeAudioBit == 0
}
func MakeAudioCodecType(base uint32) (c CodecType) {
c = CodecType(base)<<codecTypeOtherBits | CodecType(codecTypeAudioBit)
return
}
func MakeVideoCodecType(base uint32) (c CodecType) {
c = CodecType(base) << codecTypeOtherBits
return
}
const avCodecTypeMagic = 16531653
var (
H264 = MakeVideoCodecType(avCodecTypeMagic + 1)
AAC = MakeAudioCodecType(avCodecTypeMagic + 1)
PCM_MULAW = MakeAudioCodecType(avCodecTypeMagic + 2)
PCM_ALAW = MakeAudioCodecType(avCodecTypeMagic + 3)
)
type CodecData interface {
IsVideo() bool
IsAudio() bool
Type() int
Type() CodecType
}
type VideoCodecData interface {
@ -130,34 +169,48 @@ type AudioCodecData interface {
SampleFormat() SampleFormat
SampleRate() int
ChannelLayout() ChannelLayout
PacketDuration([]byte) (time.Duration, error)
}
type PacketWriter interface {
WritePacket(Packet) error
}
type PacketReader interface {
ReadPacket() (Packet,error)
}
type Muxer interface {
PacketWriter
WriteHeader([]CodecData) error
WritePacket(int, Packet) error
WriteTrailer() error
}
type Demuxer interface {
ReadPacket() (int, Packet, error)
PacketReader
Streams() ([]CodecData, error)
}
type Packet struct {
IsKeyFrame bool
Idx int8
CompositionTime time.Duration
Time time.Duration
Data []byte
Duration float64
CompositionTime float64
}
type AudioFrame struct {
SampleRate int
SampleFormat SampleFormat
ChannelLayout ChannelLayout
SampleCount int
SampleRate int
Data [][]byte
}
func (self AudioFrame) Duration() time.Duration {
return time.Second * time.Duration(self.SampleCount) / time.Duration(self.SampleRate)
}
func (self AudioFrame) HasSameFormat(other AudioFrame) bool {
if self.SampleRate != other.SampleRate {
return false
@ -194,7 +247,7 @@ func (self AudioFrame) Concat(in AudioFrame) (out AudioFrame) {
type AudioEncoder interface {
CodecData() AudioCodecData
Encode(AudioFrame) ([]Packet, error)
Encode(AudioFrame) ([][]byte, error)
Close()
//Flush() ([]Packet, error)
}
@ -208,4 +261,3 @@ type AudioDecoder interface {
type AudioResampler interface {
Resample(AudioFrame) (AudioFrame, error)
}