Debian Codegraph @

sidedata.go

raw symbols SCIP
1 package ffprobe
2
3 import (
4 "encoding/json"
5 "errors"
6 )
7
8 var (
9 ErrSideDataNotFound = errors.New("side data not found")
10 ErrSideDataUnexpectedType = errors.New("unexpected data type")
11 )
12
13 // All names and structures of side data types got from
14 // https://github.com/FFmpeg/FFmpeg/blob/4ab1184fae88bd47b9d195ac8224853c6f4e94cf/libavcodec/avpacket.c#L268
15 // https://github.com/FFmpeg/FFmpeg/blob/master/fftools/ffprobe.c#L2291
16 const (
17 SideDataTypeUnknown = "unknown"
18 SideDataTypeDisplayMatrix = "Display Matrix"
19 SideDataTypeStereo3D = "Stereo 3D"
20 SideDataTypeSphericalMapping = "Spherical Mapping"
21 SideDataTypeSkipSamples = "Skip Samples"
22 SideDataTypeMasteringDisplayMetadata = "Mastering display metadata"
23 SideDataTypeContentLightLevel = "Content light level metadata"
24 )
25
26 type SideDataBase struct {
27 Type string `json:"side_data_type"`
28 }
29
30 // SideDataDisplayMatrix represents the display matrix side data.
31 type SideDataDisplayMatrix struct {
32 SideDataBase
33 Data string `json:"displaymatrix"`
34 Rotation int `json:"rotation"`
35 }
36
37 // SideDataStereo3D represents the stereo 3D side data.
38 type SideDataStereo3D struct {
39 SideDataBase
40 Type string `json:"type"`
41 Inverted bool `json:"inverted"`
42 }
43
44 // SideDataSphericalMapping represents the spherical mapping side data.
45 type SideDataSphericalMapping struct {
46 SideDataBase
47 Projection string `json:"projection"`
48 Padding int `json:"padding,omitempty"`
49 BoundLeft int `json:"bound_left,omitempty"`
50 BoundTop int `json:"bound_top,omitempty"`
51 BoundRight int `json:"bound_right,omitempty"`
52 BoundBottom int `json:"bound_bottom,omitempty"`
53 Yaw int `json:"yaw,omitempty"`
54 Pitch int `json:"pitch,omitempty"`
55 Roll int `json:"roll,omitempty"`
56 }
57
58 // SideDataSkipSamples represents the skip samples side data.
59 type SideDataSkipSamples struct {
60 SideDataBase
61 SkipSamples int `json:"skip_samples"`
62 DiscardPadding int `json:"discard_padding"`
63 SkipReason int `json:"skip_reason"`
64 DiscardReason int `json:"discard_reason"`
65 }
66
67 // SideDataMasteringDisplayMetadata represents the mastering display metadata side data.
68 type SideDataMasteringDisplayMetadata struct {
69 SideDataBase
70 RedX int `json:"red_x,omitempty"`
71 RedY int `json:"red_y,omitempty"`
72 GreenX int `json:"green_x,omitempty"`
73 GreenY int `json:"green_y,omitempty"`
74 BlueX int `json:"blue_x,omitempty"`
75 BlueY int `json:"blue_y,omitempty"`
76 WhitePointX int `json:"white_point_x,omitempty"`
77 WhitePointY int `json:"white_point_y,omitempty"`
78 MinLuminance int `json:"min_luminance,omitempty"`
79 MaxLuminance int `json:"max_luminance,omitempty"`
80 }
81
82 // SideDataContentLightLevel represents the content light level side data.
83 type SideDataContentLightLevel struct {
84 SideDataBase
85 MaxContent int `json:"max_content,omitempty"`
86 MaxAverage int `json:"max_average,omitempty"`
87 }
88
89 // SideDataUnknown represents an unknown side data.
90 type SideDataUnknown Tags
91
92 // SideData represents a side data packet.
93 type SideData struct {
94 SideDataBase
95 Data interface{} `json:"-"`
96 }
97
98 func (sd *SideData) UnmarshalJSON(b []byte) error {
99 type Alias SideData
100 aux := &struct {
101 *Alias
102 }{
103 Alias: (*Alias)(sd),
104 }
105
106 if err := json.Unmarshal(b, aux); err != nil {
107 return err
108 }
109
110 switch sd.Type {
111 case SideDataTypeDisplayMatrix:
112 sd.Data = &SideDataDisplayMatrix{}
113 case SideDataTypeStereo3D:
114 sd.Data = new(SideDataStereo3D)
115 case SideDataTypeSphericalMapping:
116 sd.Data = new(SideDataSphericalMapping)
117 case SideDataTypeSkipSamples:
118 sd.Data = new(SideDataSkipSamples)
119 case SideDataTypeMasteringDisplayMetadata:
120 sd.Data = new(SideDataMasteringDisplayMetadata)
121 case SideDataTypeContentLightLevel:
122 sd.Data = new(SideDataContentLightLevel)
123 default:
124 sd.Data = new(SideDataUnknown)
125 }
126
127 return json.Unmarshal(b, sd.Data)
128 }
129
130 func (sd *SideData) MarshalJSON() ([]byte, error) {
131 return json.Marshal(sd.Data)
132 }
133
134 // SideDataList represents a list of side data packets.
135 type SideDataList []SideData
136
137 // UnmarshalJSON for SideDataList
138 func (s *SideDataList) UnmarshalJSON(b []byte) error {
139 var raw []json.RawMessage
140 if err := json.Unmarshal(b, &raw); err != nil {
141 return err
142 }
143
144 for _, r := range raw {
145 var sd SideData
146 if err := json.Unmarshal(r, &sd); err != nil {
147 return err
148 }
149 *s = append(*s, sd)
150 }
151
152 return nil
153 }
154
155 // FindUnknownSideData searches for SideData of type SideDataUnknown in the SideDataList.
156 // If such SideData is found, it is returned, otherwise, an error is returned
157 // indicating that the SideData of type SideDataUnknown was not found or the found
158 // SideData was of an unexpected type.
159 func (s *SideDataList) FindUnknownSideData(sideDataType string) (*SideDataUnknown, error) {
160 data, found := s.findSideDataByName(sideDataType)
161 if !found {
162 return nil, ErrSideDataNotFound
163 }
164
165 unknownSideData, ok := data.(*SideDataUnknown)
166 if !ok {
167 return nil, ErrSideDataUnexpectedType
168 }
169
170 return unknownSideData, nil
171 }
172
173 // FindSideData searches for SideData by its type in the SideDataList.
174 // If SideData of the specified type is found, it is returned, otherwise,
175 // an error is returned indicating that the SideData of the specified type was not found.
176 func (s *SideDataList) FindSideData(sideDataType string) (interface{}, error) {
177 data, found := s.findSideDataByName(sideDataType)
178 if !found {
179 return nil, ErrSideDataNotFound
180 }
181
182 return data, nil
183 }
184
185 // GetDisplayMatrix retrieves the DisplayMatrix from the SideData. If the DisplayMatrix is not found or
186 // the SideData is of the wrong type, an error is returned.
187 func (s *SideDataList) GetDisplayMatrix() (*SideDataDisplayMatrix, error) {
188 data, found := s.findSideDataByName(SideDataTypeDisplayMatrix)
189 if !found {
190 return nil, ErrSideDataNotFound
191 }
192 displayMatrix, ok := data.(*SideDataDisplayMatrix)
193 if !ok {
194 return nil, ErrSideDataUnexpectedType
195 }
196 return displayMatrix, nil
197 }
198
199 // GetStereo3D retrieves the Stereo3D data from the SideData. If the Stereo3D data is not found or
200 // the SideData is of the wrong type, an error is returned.
201 func (s *SideDataList) GetStereo3D() (*SideDataStereo3D, error) {
202 data, found := s.findSideDataByName(SideDataTypeStereo3D)
203 if !found {
204 return nil, ErrSideDataNotFound
205 }
206 stereo3D, ok := data.(*SideDataStereo3D)
207 if !ok {
208 return nil, ErrSideDataUnexpectedType
209 }
210 return stereo3D, nil
211 }
212
213 // GetSphericalMapping retrieves the SphericalMapping data from the SideData. If the SphericalMapping data is not found or
214 // the SideData is of the wrong type, an error is returned.
215 func (s *SideDataList) GetSphericalMapping() (*SideDataSphericalMapping, error) {
216 data, found := s.findSideDataByName(SideDataTypeSphericalMapping)
217 if !found {
218 return nil, ErrSideDataNotFound
219 }
220 sphericalMapping, ok := data.(*SideDataSphericalMapping)
221 if !ok {
222 return nil, ErrSideDataUnexpectedType
223 }
224 return sphericalMapping, nil
225 }
226
227 // GetSkipSamples retrieves the SkipSamples data from the SideData. If the SkipSamples data is not found or
228 // the SideData is of the wrong type, an error is returned.
229 func (s *SideDataList) GetSkipSamples() (*SideDataSkipSamples, error) {
230 data, found := s.findSideDataByName(SideDataTypeSkipSamples)
231 if !found {
232 return nil, ErrSideDataNotFound
233 }
234 skipSamples, ok := data.(*SideDataSkipSamples)
235 if !ok {
236 return nil, ErrSideDataUnexpectedType
237 }
238 return skipSamples, nil
239 }
240
241 // GetMasteringDisplayMetadata retrieves the MasteringDisplayMetadata from the SideData. If the MasteringDisplayMetadata is not found or
242 // the SideData is of the wrong type, an error is returned.
243 func (s *SideDataList) GetMasteringDisplayMetadata() (*SideDataMasteringDisplayMetadata, error) {
244 data, found := s.findSideDataByName(SideDataTypeMasteringDisplayMetadata)
245 if !found {
246 return nil, ErrSideDataNotFound
247 }
248 masteringDisplayMetadata, ok := data.(*SideDataMasteringDisplayMetadata)
249 if !ok {
250 return nil, ErrSideDataUnexpectedType
251 }
252 return masteringDisplayMetadata, nil
253 }
254
255 // GetContentLightLevel retrieves the ContentLightLevel from the SideData. If the ContentLightLevel is not found or
256 // the SideData is of the wrong type, an error is returned.
257 func (s *SideDataList) GetContentLightLevel() (*SideDataContentLightLevel, error) {
258 data, found := s.findSideDataByName(SideDataTypeContentLightLevel)
259 if !found {
260 return nil, ErrSideDataNotFound
261 }
262 contentLightLevel, ok := data.(*SideDataContentLightLevel)
263 if !ok {
264 return nil, ErrSideDataUnexpectedType
265 }
266 return contentLightLevel, nil
267 }
268
269 func (s *SideDataList) findSideDataByName(sideDataType string) (interface{}, bool) {
270 for _, sd := range *s {
271 if sd.Type == sideDataType {
272 return sd.Data, true
273 }
274 }
275 return nil, false
276 }