MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::FlowParser Class Reference

#include <FlowParser.hpp>

Collaboration diagram for mxl::lib::FlowParser:

Public Member Functions

 FlowParser (std::string const &in_flowDef)
template<typename T>
T get (std::string const &field) const
std::size_t getChannelCount () const
mxlDataFormat getFormat () const
mxlRational getGrainRate () const
uuids::uuid const & getId () const
std::size_t getPayloadSize () const
std::array< std::uint32_t, MXL_MAX_PLANES_PER_GRAIN > getPayloadSliceLengths () const
std::size_t getTotalPayloadSlices () const

Private Attributes

mxlDataFormat _format
mxlRational _grainRate
uuids::uuid _id
bool _interlaced
picojson::object _root

Detailed Description

Parses a NMOS Flow resource and extracts / computes key elements based on the flow attributes.

Definition at line 21 of file FlowParser.hpp.

Constructor & Destructor Documentation

◆ FlowParser()

mxl::lib::FlowParser::FlowParser ( std::string const & in_flowDef)

Parses a json flow definition

Parameters
in_flowDefThe NMOS flow definition
Exceptions
std::runtime_erroron any parse error

Definition at line 212 of file FlowParser.cpp.

213 : _id{}
215 , _interlaced{false}
216 , _grainRate{0, 1}
217 , _root{}
218 {
219 //
220 // Parse the json flow
221 //
222 auto jsonValue = picojson::value{};
223 auto const err = parseJson(jsonValue, in_flowDef);
224 if (!err.empty())
225 {
226 throw std::invalid_argument{"Invalid JSON flow definition. " + err};
227 }
228
229 // Confirm that the root is a json object
230 if (!jsonValue.is<picojson::object>())
231 {
232 throw std::invalid_argument{"Expected a JSON Object"};
233 }
234 _root = jsonValue.get<picojson::object>();
235
236 auto id = uuids::uuid::from_string(fetchAs<std::string>(_root, "id"));
237 if (!id.has_value())
238 {
239 throw std::invalid_argument{"Invalid flow 'id'."};
240 }
241
242 _id = *id;
243
244 // Read the media format
245 _format = translateFlowFormat(fetchAs<std::string>(_root, "format"));
246
247 // Read the grain rate if this is not an audio flow.
249 {
250 _grainRate = extractRational(fetchAs<picojson::object>(_root, "grain_rate"));
251 }
252 else if (_format == MXL_DATA_FORMAT_AUDIO)
253 {
254 _grainRate = extractRational(fetchAs<picojson::object>(_root, "sample_rate"));
255 }
256 else
257 {
258 throw std::domain_error{"Unsupported flow format."};
259 }
260
261 // A rate of zero or below gives a ring buffer with no entries.
262 if ((_grainRate.numerator <= 0) || (_grainRate.denominator <= 0))
263 {
264 throw std::invalid_argument{(_format == MXL_DATA_FORMAT_AUDIO) ? "sample_rate must be positive." : "grain_rate must be positive."};
265 }
266
267 // Validate that we have a non empty label
268 auto const label = fetchAs<std::string>(_root, "label");
269 if (label.empty())
270 {
271 throw std::domain_error{"Empty flow label."};
272 }
273
274 // Validate that we have a valid group hint tag
275 if (!validateGroupHint(_root))
276 {
277 throw std::domain_error{"Invalid or missing group hint tag."};
278 }
279
280 // Validate that grain_rate if we are interlaced video
281 // Grain rate must be either 30000/1001 or 25/1
283 {
284 auto const width = fetchInteger<std::size_t>(_root, "frame_width");
285 auto const height = fetchInteger<std::size_t>(_root, "frame_height");
286
287 if ((width < 2) || (width > MAX_WIDTH) || (height < 1) || (height > MAX_HEIGHT))
288 {
289 auto msg = fmt::format("Invalid video dimensions: {}x{}. range is 2x1 to {}x{}", width, height, MAX_WIDTH, MAX_HEIGHT);
290 throw std::invalid_argument{std::move(msg)};
291 }
292
293 auto interlaceMode = std::string{};
294 if (auto const it = _root.find("interlace_mode"); it != _root.end())
295 {
296 interlaceMode = it->second.get<std::string>();
297 }
298 else
299 {
300 // the interlace_mode field is absent. This means that the flow is progressive. No need to validate the grain rate.
301 // We will use the grain rate as is.
302 interlaceMode = "progressive";
303 }
304
305 constexpr auto validValues = std::array{"progressive", "interlaced_tff", "interlaced_bff"};
306 auto const match = std::ranges::any_of(validValues, [&](auto v) noexcept { return interlaceMode == v; });
307 if (!match)
308 {
309 auto msg = fmt::format("Invalid interlace_mode: {}", interlaceMode);
310 throw std::invalid_argument{std::move(msg)};
311 }
312
313 if ((interlaceMode == "interlaced_tff") || (interlaceMode == "interlaced_bff"))
314 {
315 // This is an interlaced video flow. confirm that the grain rate is defined to 30000/1001 or 25/1
316 if ((_grainRate != mxlRational{30000, 1001}) && (_grainRate != mxlRational{25, 1}))
317 {
318 auto msg = std::string{"Invalid grain_rate for interlaced video. Expected 30000/1001 or 25/1."};
319 throw std::invalid_argument{std::move(msg)};
320 }
321
322 // The grain rate is valid and we are in interlaced mode. Double the grain rate to express a field rate.
323 _grainRate.numerator *= 2;
324 _interlaced = true;
325 }
326 }
327 }
picojson::object _root
mxlDataFormat _format
int mxlIsEventDataFormat(int format)
Test whether a format uses the event queue API.
Definition dataformat.h:91
int mxlIsDiscreteDataFormat(int format)
Definition dataformat.h:75
@ MXL_DATA_FORMAT_AUDIO
Definition dataformat.h:28
@ MXL_DATA_FORMAT_UNSPECIFIED
Definition dataformat.h:26
@ MXL_DATA_FORMAT_VIDEO
Definition dataformat.h:27
std::string parseJson(picojson::value &out, std::string_view text, JsonLimits const &limits={})
struct mxlRational_t mxlRational

Member Function Documentation

◆ get()

template<typename T>
T mxl::lib::FlowParser::get ( std::string const & field) const
inlinenodiscard

Generic accessor for json fields.

Parameters
in_fieldThe field name.
Returns
The field value if found.
Exceptions
Ifthe field is not found or T is incompatible

Definition at line 116 of file FlowParser.hpp.

117 {
118 if (auto const it = _root.find(field); it != _root.end())
119 {
120 return it->second.get<T>();
121 }
122 else
123 {
124 auto msg = std::string{"Required '"} + field + "' not found.";
125 throw std::invalid_argument{std::move(msg)};
126 }
127 }

◆ getChannelCount()

std::size_t mxl::lib::FlowParser::getChannelCount ( ) const
nodiscard

Get the number of channels in an audio flow.

Returns
The number of channels in an audio flow, or 0 if the flow format indicates that this not an audio flow.

Definition at line 517 of file FlowParser.cpp.

518 {
519 if (auto const it = _root.find("channel_count"); it != _root.end())
520 {
521 if (auto const value = jsonToInteger<std::size_t>(it->second, "channel_count"); value > 0U)
522 {
523 return value;
524 }
525 throw std::invalid_argument{"Unsupported channel count: 0"};
526 }
527 return 1U;
528 }
T jsonToInteger(picojson::value const &value, std::string_view name)

◆ getFormat()

mxlDataFormat mxl::lib::FlowParser::getFormat ( ) const
nodiscard

Accessor for the 'format' field

Returns
The flow format if found and valid

Definition at line 339 of file FlowParser.cpp.

340 {
341 return _format;
342 }

◆ getGrainRate()

mxlRational mxl::lib::FlowParser::getGrainRate ( ) const
nodiscard

Accessor for the 'grain_rate' field, which either contains the 'grain rate' for discrete and event flows, or the 'sample rate' for continuous flows.

Returns
The grain rate or sample rate respectively if found and valid.

Definition at line 334 of file FlowParser.cpp.

335 {
336 return _grainRate;
337 }

◆ getId()

uuids::uuid const & mxl::lib::FlowParser::getId ( ) const
nodiscard

Accessor for the 'id' field

Returns
The id field if valid and defined

Definition at line 329 of file FlowParser.cpp.

330 {
331 return _id;
332 }

◆ getPayloadSize()

std::size_t mxl::lib::FlowParser::getPayloadSize ( ) const
nodiscard

Computes the grain payload size

Returns
The payload size
Todo
Implement audio formats.

Definition at line 344 of file FlowParser.cpp.

345 {
346 auto payloadSize = std::size_t{0};
347
349 {
350 auto const width = fetchInteger<std::size_t>(_root, "frame_width");
351 auto const height = fetchInteger<std::size_t>(_root, "frame_height");
352 auto const mediaType = fetchAs<std::string>(_root, "media_type");
353
354 if (mediaType == "video/v210")
355 {
356 if (!_interlaced || ((height % 2) == 0))
357 {
358 // Interlaced media is handled as separate fields.
359 auto const h = _interlaced ? height / 2 : height;
360 payloadSize = getV210LineLength(width) * h;
361 }
362 else
363 {
364 auto msg = std::string{"Invalid video height for interlaced v210. Must be even."};
365 throw std::invalid_argument{std::move(msg)};
366 }
367 }
368 else if (mediaType == "video/v210a")
369 {
370 if (!_interlaced || ((height % 2) == 0))
371 {
372 // Interlaced media is handled as separate fields.
373 auto const h = _interlaced ? height / 2 : height;
374
375 // Fill size
376 auto fillPayloadSize = getV210LineLength(width) * h;
377
378 // Key is stored as 10 bits per pixel, 3x10 bit words in a 32 bit word, little-endian.
379 // the last 2 bits of the 32 bit group of 3 pixels are unused.
381
382 // Total payload size is the sum of the fill and key sizes
383 payloadSize = fillPayloadSize + keyPayloadSize;
384 }
385 else
386 {
387 auto msg = std::string{"Invalid video height for interlaced v210a. Must be even."};
388 throw std::invalid_argument{std::move(msg)};
389 }
390 }
391 else
392 {
393 auto msg = std::string{"Unsupported video media_type: "} + mediaType;
394 throw std::invalid_argument{std::move(msg)};
395 }
396 }
397 else if (_format == MXL_DATA_FORMAT_DATA)
398 {
399 auto const mediaType = fetchAs<std::string>(_root, "media_type");
400 if (mediaType == "video/smpte291")
401 {
402 payloadSize = MXL_DATA_FORMAT_GRAIN_SIZE;
403 }
404 else
405 {
406 auto msg = std::string{"Unsupported data media_type: "} + mediaType;
407 throw std::invalid_argument{std::move(msg)};
408 }
409 }
410 else if (_format == MXL_DATA_FORMAT_EVENT)
411 {
412 payloadSize = MXL_DATA_FORMAT_GRAIN_SIZE;
413 }
414 else if (_format == MXL_DATA_FORMAT_AUDIO)
415 {
416 // TODO: Also check the media type once we agreed on how to encode
417 // single precision IEEE floats.
418 auto const bitDepth = fetchAs<double>(_root, "bit_depth");
419 if ((bitDepth != 32.0) && (bitDepth != 64.0))
420 {
421 auto msg = fmt::format("Unsupported bit depth: {}", bitDepth);
422 throw std::invalid_argument{std::move(msg)};
423 }
424
425 payloadSize = static_cast<std::size_t>(bitDepth) / 8U;
426 }
427 else
428 {
430 throw std::invalid_argument{"Format not implemented."};
431 }
432
433 return payloadSize;
434 }
@ MXL_DATA_FORMAT_EVENT
Asynchronous queue of complete events or event fragments.
Definition dataformat.h:30
@ MXL_DATA_FORMAT_DATA
Definition dataformat.h:29
#define MXL_DATA_FORMAT_GRAIN_SIZE
Definition dataformat.h:15
std::uint32_t get10BitAlphaLineLength(std::size_t width)
std::uint32_t getV210LineLength(std::size_t width)
Definition MediaUtils.cpp:8
constexpr auto h
auto keyPayloadSize
auto fillPayloadSize

◆ getPayloadSliceLengths()

std::array< std::uint32_t, MXL_MAX_PLANES_PER_GRAIN > mxl::lib::FlowParser::getPayloadSliceLengths ( ) const
nodiscard

Computes the length of the slices of each plane of the grain.

Returns
The length of slices for each plane.

Definition at line 436 of file FlowParser.cpp.

437 {
438 auto sliceLengths = std::array<std::uint32_t, MXL_MAX_PLANES_PER_GRAIN>{0, 0, 0, 0};
439
440 switch (_format)
441 {
443 {
444 // For "data" flows, the slice length is 1 byte
445 sliceLengths[0] = 1;
446 return sliceLengths;
447 }
448
450 {
451 // For video flows the slice length is the byte-length of a single
452 // line of v210 video.
453 auto const width = fetchInteger<std::size_t>(_root, "frame_width");
454 auto const mediaType = fetchAs<std::string>(_root, "media_type");
455 auto const v210fillSize = getV210LineLength(width);
456
457 if (mediaType == "video/v210")
458 {
459 sliceLengths[0] = v210fillSize;
460 }
461 else if (mediaType == "video/v210a")
462 {
463 sliceLengths[0] = v210fillSize;
464 sliceLengths[1] = get10BitAlphaLineLength(width);
465 }
466 else
467 {
468 auto msg = std::string{"Unsupported video media_type: "} + mediaType;
469 throw std::invalid_argument{std::move(msg)};
470 }
471
472 return sliceLengths;
473 }
474
475 default:
476 {
477 throw std::invalid_argument{"Cannot compute slice length for this data format."};
478 }
479 }
480 }

◆ getTotalPayloadSlices()

std::size_t mxl::lib::FlowParser::getTotalPayloadSlices ( ) const
nodiscard

Computes the number of slices that make up a full grain.

Returns
The number of slices

Definition at line 482 of file FlowParser.cpp.

483 {
484 switch (_format)
485 {
487 {
488 // Since the slice length for data flows is 1 byte, the number of slices must be the
489 // grain size.
491 }
492
494 {
495 if (auto const mediaType = fetchAs<std::string>(_root, "media_type"); mediaType != "video/v210" && mediaType != "video/v210a")
496 {
497 auto msg = std::string{"Unsupported video media_type: "} + mediaType;
498 throw std::invalid_argument{std::move(msg)};
499 }
500
501 // For v210, the number of slices is always the number of video lines
502 auto h = fetchInteger<std::size_t>(_root, "frame_height");
503 if (_interlaced)
504 {
505 return h / 2;
506 }
507
508 return h;
509 }
510 default:
511 {
512 throw std::invalid_argument{"Cannot compute slice length for this data format."};
513 }
514 }
515 }

Member Data Documentation

◆ _format

mxlDataFormat mxl::lib::FlowParser::_format
private

The flow format read from the 'format' field.

Definition at line 102 of file FlowParser.hpp.

◆ _grainRate

mxlRational mxl::lib::FlowParser::_grainRate
private

The flow grain rate, if defined, 0/1 if undefined.

Definition at line 106 of file FlowParser.hpp.

◆ _id

uuids::uuid mxl::lib::FlowParser::_id
private

The flow id read from the 'id' field.

Definition at line 100 of file FlowParser.hpp.

◆ _interlaced

bool mxl::lib::FlowParser::_interlaced
private

True if video and interlaced, false otherwise.

Definition at line 104 of file FlowParser.hpp.

◆ _root

picojson::object mxl::lib::FlowParser::_root
private

The parsed flow object.

Definition at line 108 of file FlowParser.hpp.


The documentation for this class was generated from the following files: