github.com/puellanivis/breton@v0.2.16/lib/mpeg/ts/psi/syntax.go (about) 1 package psi 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // SectionSyntax defines the MPEG-TS Section Syntax which is common to all PSI tables. 9 type SectionSyntax struct { 10 TableIDExtension uint16 11 Version uint8 12 Current bool 13 SectionNumber uint8 14 LastSectionNumber uint8 15 } 16 17 const ( 18 shiftSyntaxVersion = 1 19 maskSyntaxVersion = 0x1F 20 21 flagSyntaxCurrent = 0x01 22 ) 23 24 func (s *SectionSyntax) String() string { 25 out := []string{ 26 fmt.Sprintf("TIE:x%04X", s.TableIDExtension), 27 fmt.Sprintf("VER:%X", s.Version), 28 } 29 30 if s.Current { 31 out = append(out, "CUR") 32 } 33 34 if s.SectionNumber|s.LastSectionNumber != 0 { 35 out = append(out, fmt.Sprintf("SecNum:x%02X LastSec:x%02X", s.SectionNumber, s.LastSectionNumber)) 36 } 37 38 return fmt.Sprintf("{%s}", strings.Join(out, " ")) 39 } 40 41 // Unmarshal decodes a byte slice into the SectionSyntax. 42 func (s *SectionSyntax) Unmarshal(b []byte) error { 43 s.TableIDExtension = (uint16(b[0]) << 8) | uint16(b[1]) 44 s.Version = (b[2] >> shiftSyntaxVersion) & maskSyntaxVersion 45 s.Current = b[2]&flagSyntaxCurrent != 0 46 s.SectionNumber = b[3] 47 s.LastSectionNumber = b[4] 48 49 return nil 50 } 51 52 // Marshal encodes the SectionSyntax into a byte slice. 53 func (s *SectionSyntax) Marshal() ([]byte, error) { 54 b := make([]byte, 5) 55 56 b[0] = byte((s.TableIDExtension >> 8) & 0xFF) 57 b[1] = byte(s.TableIDExtension & 0xFF) 58 b[2] = 0xC0 | (s.Version&maskSyntaxVersion)<<shiftSyntaxVersion 59 60 if s.Current { 61 b[2] |= flagSyntaxCurrent 62 } 63 64 b[3] = s.SectionNumber 65 b[4] = s.LastSectionNumber 66 67 return b, nil 68 }