FastLED 3.10.8
Loading...
Searching...
No Matches

◆ parseMpeg1Info()

Mpeg1Info fl::Mpeg1::parseMpeg1Info ( fl::span< const fl::u8 > data,
fl::string * error_message = nullptr )
static

Definition at line 20 of file mpeg1.cpp.hpp.

20 {
21 Mpeg1Info info;
22
23 // Validate input data
24 if (data.empty()) {
25 if (error_message) {
26 *error_message = "Empty MPEG1 data";
27 }
28 return info; // returns invalid info
29 }
30
31 // Minimum size check - MPEG1 needs at least the pack header and system header
32 if (data.size() < 12) {
33 if (error_message) {
34 *error_message = "MPEG1 data too small";
35 }
36 return info;
37 }
38
39 // Check for MPEG1 start codes
40 // Look for Pack Start Code (0x000001BA) or System Start Code (0x000001BB)
41 bool foundMpegHeader = false;
42 fl::size headerOffset = 0;
43
44 for (fl::size i = 0; i <= data.size() - 4; i++) {
45 if (data[i] == 0x00 && data[i + 1] == 0x00 && data[i + 2] == 0x01) {
46 if (data[i + 3] == 0xBA || data[i + 3] == 0xBB) {
47 foundMpegHeader = true;
48 headerOffset = i;
49 break;
50 }
51 }
52 }
53
54 if (!foundMpegHeader) {
55 if (error_message) {
56 *error_message = "Invalid MPEG1 stream - no valid start code found";
57 }
58 return info;
59 }
60
61 // For detailed parsing, we need to use the actual decoder to extract metadata
62 // Since MPEG1 headers are complex, we'll use a temporary decoder instance
63 Mpeg1Config tempConfig;
64 tempConfig.mode = Mpeg1Config::SingleFrame;
65 tempConfig.skipAudio = true;
66
68 auto stream = fl::make_shared<fl::memorybuf>(data.size());
69 stream->write(data);
70
71 if (decoder->begin(stream)) {
72 // Extract metadata using decoder methods
73 info.width = decoder->getWidth();
74 info.height = decoder->getHeight();
75 info.frameRate = decoder->getFrameRate();
76 info.frameCount = decoder->getFrameCount();
77
78 // Calculate duration if we have frame count and frame rate
79 if (info.frameCount > 0 && info.frameRate > 0) {
80 info.duration = (info.frameCount * 1000) / info.frameRate;
81 }
82
83 // MPEG1 can have audio, but our decoder is configured to skip it
84 // In a real implementation, we'd parse the stream to detect audio tracks
85 info.hasAudio = false;
86 info.isValid = true;
87
88 decoder->end();
89 } else {
90 // Fallback: basic MPEG1 sequence header parsing
91 // Look for sequence header start code (0x000001B3)
92 for (fl::size i = headerOffset; i <= data.size() - 8; i++) {
93 if (data[i] == 0x00 && data[i + 1] == 0x00 &&
94 data[i + 2] == 0x01 && data[i + 3] == 0xB3) {
95
96 // Found sequence header, parse basic info
97 if (i + 7 < data.size()) {
98 // Width is in bits 4-15 of bytes 4-5
99 // Height is in bits 4-15 of bytes 6-7
100 fl::u16 width = ((data[i + 4] << 4) | ((data[i + 5] & 0xF0) >> 4));
101 fl::u16 height = (((data[i + 5] & 0x0F) << 8) | data[i + 6]);
102
103 if (width > 0 && height > 0) {
104 info.width = width;
105 info.height = height;
106 info.frameRate = 25; // Default assumption
107 info.frameCount = 0; // Unknown
108 info.duration = 0; // Unknown
109 info.hasAudio = false;
110 info.isValid = true;
111 }
112 }
113 break;
114 }
115 }
116
117 if (!info.isValid && error_message) {
118 *error_message = "Failed to parse MPEG1 stream metadata";
119 }
120 }
121
122 return info;
123}
constexpr bool empty() const FL_NO_EXCEPT
Definition span.h:535
constexpr fl::size size() const FL_NO_EXCEPT
Definition span.h:483
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
u8 u8 height
Definition blur.h:186
u8 width
Definition blur.h:186
third_party::Mpeg1Config Mpeg1Config
Definition mpeg1.h:29

References fl::Mpeg1Info::duration, fl::FL_NO_EXCEPT, fl::Mpeg1Info::frameCount, fl::Mpeg1Info::frameRate, fl::Mpeg1Info::hasAudio, fl::height, fl::Mpeg1Info::height, fl::Mpeg1Info::isValid, fl::adl_barrier::make_shared(), fl::third_party::Mpeg1Config::mode, fl::third_party::Mpeg1Config::SingleFrame, fl::third_party::Mpeg1Config::skipAudio, fl::Mpeg1Info::width, and fl::width.

+ Here is the call graph for this function: