FastLED 3.10.8
Loading...
Searching...
No Matches
mpeg1.cpp.hpp
Go to the documentation of this file.
1#include "fl/codec/mpeg1.h"
2#include "fl/stl/utility.h"
4#include "fl/stl/noexcept.h"
5
6namespace fl {
7
8// MPEG1 factory implementation
9IDecoderPtr Mpeg1::createDecoder(const Mpeg1Config& config, fl::string* error_message) FL_NO_EXCEPT {
10 FL_UNUSED(error_message);
11 // MPEG1 is currently supported via software decoder on all platforms
13}
14
16 // Software MPEG1 decoder is available on all platforms
17 return true;
18}
19
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}
124
125} // namespace fl
static IDecoderPtr createDecoder(const Mpeg1Config &config, fl::string *error_message=nullptr) FL_NO_EXCEPT
Definition mpeg1.cpp.hpp:9
static bool isSupported() FL_NO_EXCEPT
Definition mpeg1.cpp.hpp:15
static Mpeg1Info parseMpeg1Info(fl::span< const fl::u8 > data, fl::string *error_message=nullptr) FL_NO_EXCEPT
Definition mpeg1.cpp.hpp:20
#define FL_UNUSED(x)
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
u8 u8 height
Definition blur.h:186
u8 width
Definition blur.h:186
third_party::Mpeg1Config Mpeg1Config
Definition mpeg1.h:29
Base definition for an LED controller.
Definition crgb.hpp:179
fl::u32 frameCount
Definition mpeg1.h:16
fl::u16 height
Definition mpeg1.h:14
bool hasAudio
Definition mpeg1.h:18
fl::u32 duration
Definition mpeg1.h:17
fl::u16 frameRate
Definition mpeg1.h:15
fl::u16 width
Definition mpeg1.h:13
bool isValid
Definition mpeg1.h:19