2#include "fx/video/pixel_stream.h"
7#define INT32_MAX 0x7fffffff
10#define DBG FASTLED_DBG
12using fl::FileHandlePtr;
13using fl::ByteStreamPtr;
17PixelStream::PixelStream(
int bytes_per_frame) : mbytesPerFrame(bytes_per_frame), mUsingByteStream(false) {
20PixelStream::~PixelStream() {
24bool PixelStream::begin(FileHandlePtr h) {
27 mUsingByteStream =
false;
28 return mFileHandle->available();
31bool PixelStream::beginStream(ByteStreamPtr s) {
34 mUsingByteStream =
true;
35 return mByteStream->available(mbytesPerFrame);
38void PixelStream::close() {
39 if (!mUsingByteStream && mFileHandle) {
46int32_t PixelStream::bytesPerFrame() {
47 return mbytesPerFrame;
50bool PixelStream::readPixel(
CRGB* dst) {
51 if (mUsingByteStream) {
52 return mByteStream->read(&dst->
r, 1) && mByteStream->read(&dst->
g, 1) && mByteStream->read(&dst->
b, 1);
54 return mFileHandle->read(&dst->
r, 1) && mFileHandle->read(&dst->
g, 1) && mFileHandle->read(&dst->
b, 1);
58bool PixelStream::available()
const {
59 if (mUsingByteStream) {
60 return mByteStream->available(mbytesPerFrame);
62 return mFileHandle->available();
66bool PixelStream::atEnd()
const {
67 if (mUsingByteStream) {
70 return !mFileHandle->available();
74bool PixelStream::readFrame(Frame* frame) {
78 if (!mUsingByteStream) {
79 if (!framesRemaining()) {
82 size_t n = mFileHandle->readCRGB(frame->rgb(), mbytesPerFrame / 3);
83 DBG(
"pos: " << mFileHandle->pos());
84 return n*3 == size_t(mbytesPerFrame);
86 size_t n = mByteStream->readCRGB(frame->rgb(), mbytesPerFrame / 3);
87 return n*3 == size_t(mbytesPerFrame);
90bool PixelStream::hasFrame(uint32_t frameNumber) {
91 if (mUsingByteStream) {
93 DBG(
"Not implemented and therefore always returns true");
96 size_t total_bytes = mFileHandle->size();
97 return frameNumber * mbytesPerFrame < total_bytes;
101bool PixelStream::readFrameAt(uint32_t frameNumber, Frame* frame) {
103 if (mUsingByteStream) {
105 FASTLED_DBG(
"ByteStream doesn't support seeking");
109 mFileHandle->seek(frameNumber * mbytesPerFrame);
110 if (mFileHandle->bytesLeft() == 0) {
113 size_t read = mFileHandle->readCRGB(frame->rgb(), mbytesPerFrame / 3) * 3;
117 bool ok = int(read) == mbytesPerFrame;
119 DBG(
"readFrameAt failed - read: " << read <<
", mbytesPerFrame: " << mbytesPerFrame <<
", frame:" << frameNumber <<
", left: " << mFileHandle->bytesLeft());
125int32_t PixelStream::framesRemaining()
const {
126 if (mbytesPerFrame == 0)
return 0;
127 int32_t bytes_left = bytesRemaining();
128 if (bytes_left <= 0) {
131 return bytes_left / mbytesPerFrame;
134int32_t PixelStream::framesDisplayed()
const {
135 if (mUsingByteStream) {
139 int32_t bytes_played = mFileHandle->pos();
140 return bytes_played / mbytesPerFrame;
144int32_t PixelStream::bytesRemaining()
const {
145 if (mUsingByteStream) {
148 return mFileHandle->bytesLeft();
152int32_t PixelStream::bytesRemainingInFrame()
const {
153 return bytesRemaining() % mbytesPerFrame;
156bool PixelStream::rewind() {
157 if (mUsingByteStream) {
161 mFileHandle->seek(0);
166PixelStream::Type PixelStream::getType()
const {
167 return mUsingByteStream ? Type::kStreaming : Type::kFile;
170size_t PixelStream::readBytes(uint8_t* dst,
size_t len) {
171 uint16_t bytesRead = 0;
172 if (mUsingByteStream) {
173 while (bytesRead < len && mByteStream->available(len)) {
175 if (mByteStream->read(dst + bytesRead, 1)) {
182 while (bytesRead < len && mFileHandle->available()) {
183 if (mFileHandle->read(dst + bytesRead, 1)) {
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Representation of an RGB pixel (Red, Green, Blue)
uint8_t r
Red channel value.
uint8_t g
Green channel value.
uint8_t b
Blue channel value.