Merged in codec-pes (pull request #440)

codecutils/pes: move SID code to pes from codecutils, create EncodeADPCM and EncodePCM

Approved-by: Saxon Milton
This commit is contained in:
Trek Hopton 2020-12-23 01:33:02 +00:00
commit 3e30e4846b
7 changed files with 41 additions and 30 deletions

View File

@ -39,14 +39,6 @@ import (
"bitbucket.org/ausocean/utils/realtime" "bitbucket.org/ausocean/utils/realtime"
) )
// Stream IDs as per ITU-T Rec. H.222.0 / ISO/IEC 13818-1 [1], tables 2-22 and 2-34.
const (
streamIDH264 = 27
streamIDH265 = 36
streamIDMJPEG = 28
streamIDAudio = 0xc0 // ADPCM audio stream ID.
)
// These three constants are used to select between the three different // These three constants are used to select between the three different
// methods of when the PSI is sent. // methods of when the PSI is sent.
const ( const (
@ -60,7 +52,8 @@ const (
EncodeH264 = iota EncodeH264 = iota
EncodeH265 EncodeH265
EncodeMJPEG EncodeMJPEG
EncodeAudio EncodePCM
EncodeADPCM
) )
// The program IDs we assign to different types of media. // The program IDs we assign to different types of media.
@ -102,7 +95,7 @@ const (
const ( const (
defaultRate = 25 // FPS defaultRate = 25 // FPS
defaultPSIMethod = psiMethodNAL defaultPSIMethod = psiMethodNAL
defaultStreamID = streamIDH264 defaultStreamID = pes.H264SID
defaultMediaPID = PIDVideo defaultMediaPID = PIDVideo
) )

View File

@ -191,7 +191,7 @@ func TestEncodePcm(t *testing.T) {
sampleSize := 2 sampleSize := 2
blockSize := 16000 blockSize := 16000
writeFreq := float64(sampleRate*sampleSize) / float64(blockSize) writeFreq := float64(sampleRate*sampleSize) / float64(blockSize)
e, err := NewEncoder(nopCloser{&buf}, (*testLogger)(t), PacketBasedPSI(10), Rate(writeFreq), MediaType(EncodeAudio)) e, err := NewEncoder(nopCloser{&buf}, (*testLogger)(t), PacketBasedPSI(10), Rate(writeFreq), MediaType(EncodePCM))
if err != nil { if err != nil {
t.Fatalf("could not create MTS encoder, failed with error: %v", err) t.Fatalf("could not create MTS encoder, failed with error: %v", err)
} }

View File

@ -134,7 +134,7 @@ func writePSI(b *bytes.Buffer) error {
func writeFrame(b *bytes.Buffer, frame []byte, pts uint64) error { func writeFrame(b *bytes.Buffer, frame []byte, pts uint64) error {
// Prepare PES data. // Prepare PES data.
pesPkt := pes.Packet{ pesPkt := pes.Packet{
StreamID: streamIDH264, StreamID: pes.H264SID,
PDI: hasPTS, PDI: hasPTS,
PTS: pts, PTS: pts,
Data: frame, Data: frame,
@ -210,7 +210,7 @@ func TestGetPTSRange2(t *testing.T) {
clip.Reset() clip.Reset()
for j := 0; j < nPackets; j++ { for j := 0; j < nPackets; j++ {
pesPkt := pes.Packet{ pesPkt := pes.Packet{
StreamID: streamIDH264, StreamID: pes.H264SID,
PDI: hasPTS, PDI: hasPTS,
PTS: test.pts[j], PTS: test.pts[j],
Data: []byte{}, Data: []byte{},

View File

@ -29,6 +29,8 @@ package mts
import ( import (
"errors" "errors"
"time" "time"
"bitbucket.org/ausocean/av/container/mts/pes"
) )
var ( var (
@ -65,26 +67,30 @@ func TimeBasedPSI(dur time.Duration) func(*Encoder) error {
// MediaType is an option that can be passed to NewEncoder. It is used to // MediaType is an option that can be passed to NewEncoder. It is used to
// specifiy the media type/codec of the data we are packetising using the // specifiy the media type/codec of the data we are packetising using the
// encoder. Currently supported options are EncodeH264, EncodeH265, EncodeMJPEG // encoder. Currently supported options are EncodeH264, EncodeH265, EncodeMJPEG, EncodePCM
// and EncodeAudio. // and EncodeADPCM.
func MediaType(mt int) func(*Encoder) error { func MediaType(mt int) func(*Encoder) error {
return func(e *Encoder) error { return func(e *Encoder) error {
switch mt { switch mt {
case EncodeAudio: case EncodePCM:
e.mediaPID = PIDAudio e.mediaPID = PIDAudio
e.streamID = streamIDAudio e.streamID = pes.PCMSID
e.log.Debug("configured for audio packetisation") e.log.Debug("configured for PCM packetisation")
case EncodeADPCM:
e.mediaPID = PIDAudio
e.streamID = pes.ADPCMSID
e.log.Debug("configured for ADPCM packetisation")
case EncodeH265: case EncodeH265:
e.mediaPID = PIDVideo e.mediaPID = PIDVideo
e.streamID = streamIDH265 e.streamID = pes.H265SID
e.log.Debug("configured for h.265 packetisation") e.log.Debug("configured for h.265 packetisation")
case EncodeH264: case EncodeH264:
e.mediaPID = PIDVideo e.mediaPID = PIDVideo
e.streamID = streamIDH264 e.streamID = pes.H264SID
e.log.Debug("configured for h.264 packetisation") e.log.Debug("configured for h.264 packetisation")
case EncodeMJPEG: case EncodeMJPEG:
e.mediaPID = PIDVideo e.mediaPID = PIDVideo
e.streamID = streamIDMJPEG e.streamID = pes.MJPEGSID
e.log.Debug("configured for MJPEG packetisation") e.log.Debug("configured for MJPEG packetisation")
default: default:
return ErrUnsupportedMedia return ErrUnsupportedMedia

View File

@ -36,6 +36,7 @@ import (
"time" "time"
"bitbucket.org/ausocean/av/container/mts/meta" "bitbucket.org/ausocean/av/container/mts/meta"
"bitbucket.org/ausocean/av/container/mts/pes"
"bitbucket.org/ausocean/av/container/mts/psi" "bitbucket.org/ausocean/av/container/mts/psi"
) )
@ -94,7 +95,7 @@ func TestExtract(t *testing.T) {
want.frames = append(want.frames, Frame{ want.frames = append(want.frames, Frame{
Media: frame, Media: frame,
PTS: nextPTS, PTS: nextPTS,
ID: streamIDH264, ID: pes.H264SID,
Meta: metaMap, Meta: metaMap,
}) })
} }

View File

@ -22,17 +22,17 @@ LICENSE
in gpl.txt. If not, see http://www.gnu.org/licenses. in gpl.txt. If not, see http://www.gnu.org/licenses.
*/ */
package codecutil package pes
import "errors" import "errors"
// Stream types // Stream types AKA stream IDs as per ITU-T Rec. H.222.0 / ISO/IEC 13818-1 [1], tables 2-22 and 2-34.
const ( const (
H264SID = 27 H264SID = 27
H265SID = 36 H265SID = 36
MJPEGSID = 28 MJPEGSID = 28
PCMSID = 191 PCMSID = 192
ADPCMSID = 192 ADPCMSID = 193
) )
// SIDToMIMEType will return the corresponding MIME type for passed stream ID. // SIDToMIMEType will return the corresponding MIME type for passed stream ID.

View File

@ -95,14 +95,25 @@ func (r *Revid) reset(c config.Config) error {
encOptions = append(encOptions, mts.TimeBasedPSI(time.Duration(r.cfg.PSITime)*time.Second)) encOptions = append(encOptions, mts.TimeBasedPSI(time.Duration(r.cfg.PSITime)*time.Second))
r.cfg.CBR = true r.cfg.CBR = true
case codecutil.PCM, codecutil.ADPCM: case codecutil.PCM, codecutil.ADPCM:
return nil, errors.New(fmt.Sprintf("invalid input codec: %v for input: %v", r.cfg.InputCodec, r.cfg.Input)) return nil, fmt.Errorf("invalid input codec: %v for input: %v", r.cfg.InputCodec, r.cfg.Input)
default: default:
panic("unknown input codec") panic("unknown input codec")
} }
case config.InputAudio: case config.InputAudio:
st = mts.EncodeAudio switch r.cfg.InputCodec {
case codecutil.PCM:
st = mts.EncodePCM
encOptions = append(encOptions, mts.TimeBasedPSI(time.Duration(r.cfg.PSITime)*time.Second)) encOptions = append(encOptions, mts.TimeBasedPSI(time.Duration(r.cfg.PSITime)*time.Second))
rate = 1 / r.cfg.RecPeriod rate = 1 / r.cfg.RecPeriod
case codecutil.ADPCM:
st = mts.EncodeADPCM
encOptions = append(encOptions, mts.TimeBasedPSI(time.Duration(r.cfg.PSITime)*time.Second))
rate = 1 / r.cfg.RecPeriod
case codecutil.H264, codecutil.H265, codecutil.MJPEG:
return nil, fmt.Errorf("invalid input codec: %v for input: %v", r.cfg.InputCodec, r.cfg.Input)
default:
panic("unknown input codec")
}
default: default:
panic("unknown input type") panic("unknown input type")
} }