Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
VideoModuleEntry.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
16
17#include <cstdint>
18#include <cstdio> // pl_mpeg's declarations expect size_t / FILE from the includer
19#include <cstdlib>
20#include <cstring>
21#include <algorithm>
22
23#include "./VideoBindings.hpp"
24
25#define PL_MPEG_IMPLEMENTATION
26#include "pl_mpeg.h"
27
28namespace {
29
30constexpr int kMaxInstances = 16;
31
32struct Instance {
33 plm_t* plm = nullptr;
34 plm_frame_t* frame = nullptr; // latest decoded frame; owned by plm
35 bool newFrame = false;
36 double duration = 0;
37 bool frameIndexedSeek = false;
38};
39
40Instance g_instances[kMaxInstances];
41
42Instance* get(int handle) {
43 if (handle < 1 || handle > kMaxInstances) return nullptr;
44 Instance& inst = g_instances[handle - 1];
45 return inst.plm ? &inst : nullptr;
46}
47
48void onVideoFrame(plm_t*, plm_frame_t* frame, void* user) {
49 auto* inst = static_cast<Instance*>(user);
50 inst->frame = frame;
51 inst->newFrame = true;
52}
53
55double singleTimestampDuration(uint8_t* bytes, uint32_t size, double fps) {
56 plm_buffer_t* buffer = plm_buffer_create_with_memory(bytes, size, FALSE);
57 if (!buffer) return 0;
58 plm_demux_t* demux = plm_demux_create(buffer, TRUE);
59 if (!demux) { plm_buffer_destroy(buffer); return 0; }
60 uint32_t code = 0xffffffff;
61 uint32_t pictures = 0;
62 while (plm_packet_t* packet = plm_demux_decode(demux)) {
63 if (packet->type != PLM_DEMUX_PACKET_VIDEO_1) continue;
64 for (size_t i = 0; i < packet->length; i++) {
65 code = (code << 8) | packet->data[i];
66 if (code == 0x00000100) pictures++;
67 }
68 }
69 plm_demux_destroy(demux);
70 return fps > 0 ? pictures / fps : 0;
71}
72
73} // namespace
74
75extern "C" {
76
85int es_video_open(uintptr_t dataPtr, uint32_t size) {
86 const auto* source = reinterpret_cast<const uint8_t*>(dataPtr);
87 if (!source || size == 0) return 0;
88 int slot = -1;
89 for (int i = 0; i < kMaxInstances; i++) {
90 if (!g_instances[i].plm) { slot = i; break; }
91 }
92 if (slot < 0) return 0;
93 auto* owned = static_cast<uint8_t*>(malloc(size));
94 if (!owned) return 0;
95 memcpy(owned, source, size);
96 plm_t* plm = plm_create_with_memory(owned, size, /* free_when_done */ TRUE);
97 if (!plm) {
98 free(owned);
99 return 0;
100 }
101 // Validate via the parsed sequence header, not plm_probe(): probe scans
102 // forward from the current buffer position, so a stream whose only video
103 // PES packet was already consumed during header parsing (tiny single-sector
104 // files) would probe as empty despite decoding fine.
105 if (plm_get_width(plm) <= 0 || plm_get_framerate(plm) <= 0) {
106 plm_destroy(plm);
107 return 0;
108 }
109 plm_set_audio_enabled(plm, FALSE);
110 Instance& inst = g_instances[slot];
111 inst.plm = plm;
112 inst.frame = nullptr;
113 inst.newFrame = false;
114 inst.duration = plm_get_duration(plm);
115 inst.frameIndexedSeek = inst.duration <= 0;
116 if (inst.frameIndexedSeek) {
117 inst.duration = singleTimestampDuration(owned, size, plm_get_framerate(plm));
118 }
119 plm_set_video_decode_callback(plm, onVideoFrame, &inst);
120 return slot + 1;
121}
122
123uint32_t es_video_width(int handle) {
124 Instance* inst = get(handle);
125 return inst ? static_cast<uint32_t>(plm_get_width(inst->plm)) : 0u;
126}
127
128uint32_t es_video_height(int handle) {
129 Instance* inst = get(handle);
130 return inst ? static_cast<uint32_t>(plm_get_height(inst->plm)) : 0u;
131}
132
133double es_video_duration(int handle) {
134 Instance* inst = get(handle);
135 return inst ? inst->duration : 0.0;
136}
137
138double es_video_framerate(int handle) {
139 Instance* inst = get(handle);
140 return inst ? plm_get_framerate(inst->plm) : 0.0;
141}
142
144double es_video_time(int handle) {
145 Instance* inst = get(handle);
146 if (!inst) return 0.0;
147 return plm_has_ended(inst->plm) ? inst->duration : plm_get_time(inst->plm);
148}
149
151void es_video_set_loop(int handle, int loop) {
152 Instance* inst = get(handle);
153 if (inst) plm_set_loop(inst->plm, loop ? TRUE : FALSE);
154}
155
156int es_video_has_ended(int handle) {
157 Instance* inst = get(handle);
158 return inst ? plm_has_ended(inst->plm) : 1;
159}
160
166int es_video_advance(int handle, double dt) {
167 Instance* inst = get(handle);
168 if (!inst) return 0;
169 plm_decode(inst->plm, dt);
170 if (plm_has_ended(inst->plm) && inst->frame) {
171 inst->duration = inst->frame->time + 1.0 / plm_get_framerate(inst->plm);
172 }
173 return inst->newFrame ? 1 : 0;
174}
175
183int es_video_frame_rgba(int handle, uintptr_t outPtr, uint32_t outSize) {
184 Instance* inst = get(handle);
185 auto* out = reinterpret_cast<uint8_t*>(outPtr);
186 if (!inst || !inst->frame || !out) return 0;
187 const int w = plm_get_width(inst->plm);
188 const int h = plm_get_height(inst->plm);
189 const int stride = w * 4;
190 if (outSize < static_cast<uint32_t>(stride * h)) return 0;
191 // plm_frame_to_rgba writes only the RGB bytes of each pixel — prefill so
192 // alpha is opaque.
193 memset(out, 0xFF, static_cast<size_t>(stride) * h);
194 // Negative stride writes rows bottom-first: the converter indexes rows as
195 // `dest[row * stride + …]`, so anchoring dest at the last row flips vertically
196 // with no extra pass. (Row pairs stay adjacent; column offsets are positive.)
197 plm_frame_to_rgba(inst->frame, out + static_cast<size_t>(h - 1) * stride, -stride);
198 inst->newFrame = false;
199 return 1;
200}
201
206int es_video_seek(int handle, double time) {
207 Instance* inst = get(handle);
208 if (!inst) return 0;
209 if (inst->frameIndexedSeek) {
210 const double fps = plm_get_framerate(inst->plm);
211 const double target = std::max(0.0, std::min(time, inst->duration - 1.0 / fps));
212 plm_rewind(inst->plm);
213 plm_frame_t* frame = plm_decode_video(inst->plm);
214 while (frame && frame->time < target) frame = plm_decode_video(inst->plm);
215 if (!frame) return 0;
216 onVideoFrame(inst->plm, frame, inst);
217 return 1;
218 }
219 return plm_seek(inst->plm, time, /* seek_exact */ TRUE) ? 1 : 0;
220}
221
223void es_video_close(int handle) {
224 if (handle < 1 || handle > kMaxInstances) return;
225 Instance& inst = g_instances[handle - 1];
226 if (inst.plm) plm_destroy(inst.plm);
227 inst.plm = nullptr;
228 inst.frame = nullptr;
229 inst.newFrame = false;
230}
231
232} // extern "C"
The MPEG-1 decoder's entry points, declared once for both platforms.
void es_video_set_loop(int handle, int loop)
int es_video_has_ended(int handle)
uint32_t es_video_width(int handle)
uint32_t es_video_height(int handle)
int es_video_advance(int handle, double dt)
double es_video_duration(int handle)
int es_video_seek(int handle, double time)
double es_video_framerate(int handle)
int es_video_open(uintptr_t dataPtr, uint32_t size)
double es_video_time(int handle)
int es_video_frame_rgba(int handle, uintptr_t outPtr, uint32_t outSize)
void es_video_close(int handle)