25#define PL_MPEG_IMPLEMENTATION
30constexpr int kMaxInstances = 16;
34 plm_frame_t* frame =
nullptr;
35 bool newFrame =
false;
37 bool frameIndexedSeek =
false;
40Instance g_instances[kMaxInstances];
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;
48void onVideoFrame(plm_t*, plm_frame_t* frame,
void* user) {
49 auto* inst =
static_cast<Instance*
>(user);
51 inst->newFrame =
true;
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++;
69 plm_demux_destroy(demux);
70 return fps > 0 ? pictures / fps : 0;
86 const auto* source =
reinterpret_cast<const uint8_t*
>(dataPtr);
87 if (!source || size == 0)
return 0;
89 for (
int i = 0; i < kMaxInstances; i++) {
90 if (!g_instances[i].plm) { slot = i;
break; }
92 if (slot < 0)
return 0;
93 auto* owned =
static_cast<uint8_t*
>(malloc(size));
95 memcpy(owned, source, size);
96 plm_t* plm = plm_create_with_memory(owned, size, TRUE);
105 if (plm_get_width(plm) <= 0 || plm_get_framerate(plm) <= 0) {
109 plm_set_audio_enabled(plm, FALSE);
110 Instance& inst = g_instances[slot];
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));
119 plm_set_video_decode_callback(plm, onVideoFrame, &inst);
124 Instance* inst = get(handle);
125 return inst ?
static_cast<uint32_t
>(plm_get_width(inst->plm)) : 0u;
129 Instance* inst = get(handle);
130 return inst ?
static_cast<uint32_t
>(plm_get_height(inst->plm)) : 0u;
134 Instance* inst = get(handle);
135 return inst ? inst->duration : 0.0;
139 Instance* inst = get(handle);
140 return inst ? plm_get_framerate(inst->plm) : 0.0;
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);
152 Instance* inst = get(handle);
153 if (inst) plm_set_loop(inst->plm, loop ? TRUE : FALSE);
157 Instance* inst = get(handle);
158 return inst ? plm_has_ended(inst->plm) : 1;
167 Instance* inst = get(handle);
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);
173 return inst->newFrame ? 1 : 0;
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;
193 memset(out, 0xFF,
static_cast<size_t>(stride) * h);
197 plm_frame_to_rgba(inst->frame, out +
static_cast<size_t>(h - 1) * stride, -stride);
198 inst->newFrame =
false;
207 Instance* inst = get(handle);
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);
219 return plm_seek(inst->plm, time, TRUE) ? 1 : 0;
224 if (handle < 1 || handle > kMaxInstances)
return;
225 Instance& inst = g_instances[handle - 1];
226 if (inst.plm) plm_destroy(inst.plm);
228 inst.frame =
nullptr;
229 inst.newFrame =
false;
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)