forked from futzu/cuei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upids.go
168 lines (148 loc) · 3.49 KB
/
upids.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package cuei
import (
"fmt"
)
var uriUpids = map[uint8]string{
0x01: "Deprecated",
0x02: "Deprecated",
0x03: "AdID",
0x07: "TID",
0x08: "AiringID",
0x09: "ADI",
0x10: "UUID",
0x11: "ACR",
0x0a: "EIDR",
0x0b: "ATSC",
0x0c: "MPU",
0x0d: "MID",
0x0e: "ADS Info",
0x0f: "URI",
}
/*
Upid is the Struct for Segmentation Upids
Non-standard UPID types are returned as bytes.
*/
type Upid struct {
Name string `json:",omitempty"`
UpidType uint8 `json:",omitempty"`
Value string `json:",omitempty"`
TSID uint16 `json:",omitempty"`
Reserved uint8 `json:",omitempty"`
EndOfDay uint8 `json:",omitempty"`
UniqueFor uint16 `json:",omitempty"`
ContentID []byte `json:",omitempty"`
Upids []Upid `json:",omitempty"`
FormatIdentifier string `json:",omitempty"`
PrivateData []byte `json:",omitempty"`
}
// Decode Upids
func (upid *Upid) Decode(bd *bitDecoder, upidType uint8, upidlen uint8) {
upid.UpidType = upidType
name, ok := uriUpids[upidType]
if ok {
upid.Name = name
upid.uri(bd, upidlen)
} else {
switch upidType {
case 0x05, 0x06:
upid.Name = "ISAN"
upid.isan(bd, upidlen)
case 0x08:
upid.Name = "AiringID"
upid.airid(bd, upidlen)
case 0x0a:
upid.Name = "EIDR"
upid.eidr(bd, upidlen)
case 0x0b:
upid.Name = "ATSC"
upid.atsc(bd, upidlen)
case 0x0c:
upid.Name = "MPU"
upid.mpu(bd, upidlen)
case 0x0d:
upid.Name = "MID"
upid.mid(bd, upidlen)
default:
upid.Name = "UPID"
upid.uri(bd, upidlen)
}
}
}
// Decode for AirId
func (upid *Upid) airid(bd *bitDecoder, upidlen uint8) {
upid.Value = bd.asHex(uint(upidlen << 3))
}
// Decode for Isan Upid
func (upid *Upid) isan(bd *bitDecoder, upidlen uint8) {
upid.Value = bd.asAscii(uint(upidlen << 3))
}
// Decode for URI Upid
func (upid *Upid) uri(bd *bitDecoder, upidlen uint8) {
upid.Value = bd.asAscii(uint(upidlen) << 3)
}
// Decode for ATSC Upid
func (upid *Upid) atsc(bd *bitDecoder, upidlen uint8) {
upid.TSID = bd.uInt16(16)
upid.Reserved = bd.uInt8(2)
upid.EndOfDay = bd.uInt8(5)
upid.UniqueFor = bd.uInt16(9)
upid.ContentID = bd.asBytes(uint((upidlen - 4) << 3))
}
// Decode for EIDR Upid
func (upid *Upid) eidr(bd *bitDecoder, upidlen uint8) {
if upidlen == 12 {
head := bd.uInt64(16)
tail := bd.asHex(80)
upid.Value = fmt.Sprintf("10%v/%v", head, tail)
}
}
// Decode for MPU Upid
func (upid *Upid) mpu(bd *bitDecoder, upidlen uint8) {
ulb := uint(upidlen) << 3
upid.FormatIdentifier = bd.asHex(32)
upid.PrivateData = bd.asBytes(ulb - 32)
}
// Decode for MID Upid
func (upid *Upid) mid(bd *bitDecoder, upidlen uint8) {
var i uint8
i = 0
for i < upidlen {
utype := bd.uInt8(8)
i++
ulen := bd.uInt8(8)
i++
i += ulen
var mupid Upid
upid.Decode(bd, utype, ulen)
upid.Upids = append(upid.Upids, mupid)
}
}
// Encode Upids
func (upid *Upid) Encode(be *bitEncoder, upidType uint8) {
switch upid.UpidType {
case 0x05, 0x06:
upid.encodeIsan(be)
case 0x08:
upid.encodeAirId(be)
default:
upid.encodeUri(be)
}
}
// encode for Uri Upids
func (upid *Upid) encodeUri(be *bitEncoder) {
if len(upid.Value) > 0 {
be.AddBytes([]byte(upid.Value), uint(len(upid.Value)<<3))
}
}
// encode for AirId
func (upid *Upid) encodeAirId(be *bitEncoder) {
if len(upid.Value) > 0 {
be.AddBytes([]byte(upid.Value), uint(len(upid.Value)<<3))
}
}
// encode for Isan Upid
func (upid *Upid) encodeIsan(be *bitEncoder) {
if len(upid.Value) > 0 {
be.AddBytes([]byte(upid.Value), uint(len(upid.Value)<<3))
}
}