35#include <freerdp/config.h>
40#include <emscripten.h>
41#include <emscripten/proxying.h>
42#include <emscripten/threading.h>
44#include <winpr/assert.h>
45#include <winpr/string.h>
46#include <freerdp/log.h>
50#define H264_WC_NEEDS_BUFFER 2
51#define H264_WC_YUV444 3
66} H264_CONTEXT_WEBCODECS;
86extern void h264_wc_js_open(
void* ctx,
int* handle,
const char* probe);
87extern void h264_wc_js_close(
int handle);
88extern void h264_wc_js_decode(
void* ctx,
int handle,
const BYTE* src, UINT32 size,
int key,
89 const char* codec, BYTE* y, BYTE* u, BYTE* v, UINT32 ys, UINT32 cs,
90 int gbr, UINT32 maxWidth, UINT32 maxHeight, UINT32* dim,
int* rc);
91extern void h264_wc_js_copy(
void* ctx,
int handle, BYTE* y, BYTE* u, BYTE* v, UINT32 ys, UINT32 cs,
94EMSCRIPTEN_KEEPALIVE
void freerdp_h264_wc_finish(em_proxying_ctx* ctx)
96 emscripten_proxy_finish(ctx);
105static void main_open(em_proxying_ctx* ctx,
void* arg)
107 h264_wc_open* open = arg;
108 h264_wc_js_open(ctx, &open->handle, open->probe);
111static void main_close(
void* arg)
113 h264_wc_js_close(*(
int*)arg);
116static void main_decode(em_proxying_ctx* ctx,
void* arg)
118 h264_wc_job* job = arg;
119 h264_wc_js_decode(ctx, job->handle, job->src, job->size, job->key, job->codec, job->plane[0],
120 job->plane[1], job->plane[2], job->stride[0], job->stride[1], job->gbr,
121 job->maxWidth, job->maxHeight, job->dim, &job->rc);
124static void main_copy(em_proxying_ctx* ctx,
void* arg)
126 h264_wc_job* job = arg;
127 h264_wc_js_copy(ctx, job->handle, job->plane[0], job->plane[1], job->plane[2], job->stride[0],
128 job->stride[1], job->gbr, &job->rc);
132static BOOL on_main(
void (*fn)(em_proxying_ctx*,
void*),
void* arg)
134 return emscripten_proxy_sync_with_ctx(emscripten_proxy_get_system_queue(),
135 emscripten_main_runtime_thread_id(), fn, arg) == 1;
140static BOOL next_nal(
const BYTE* data,
size_t size,
size_t* offset,
const BYTE** nal,
143 size_t pos = *offset;
144 if ((size < 3) || (pos == SIZE_MAX - size))
146 while ((pos <= size - 3ull) &&
147 !((data[pos] == 0) && (data[pos + 1] == 0) && (data[pos + 2] == 1)))
149 if (pos >= size - 3ull)
152 const size_t start = pos + 3;
154 while ((pos <= size - 3ull) &&
155 !((data[pos] == 0) && (data[pos + 1] == 0) && (data[pos + 2] == 1)))
157 size_t end = (pos <= size - 3ull) ? pos : size;
158 while ((end > start) && (data[end - 1] == 0))
162 *length = end - start;
167#define H264_NAL_SLICE 1
168#define H264_NAL_IDR 5
169#define H264_NAL_SPS 7
170#define H264_NAL_PPS 8
171#define HEVC_NAL_BLA_W_LP 16
172#define HEVC_NAL_CRA 21
173#define HEVC_NAL_VPS 32
174#define HEVC_NAL_SPS 33
175#define HEVC_NAL_PPS 34
178static BYTE nal_type(
const H264_CONTEXT_WEBCODECS* sys,
const BYTE* nal)
180 return sys->hevc ? (nal[0] >> 1) & 0x3F : nal[0] & 0x1F;
184static BOOL nal_is_slice(
const H264_CONTEXT_WEBCODECS* sys, BYTE type)
186 return sys->hevc ? (type < HEVC_NAL_VPS) : ((type == H264_NAL_SLICE) || (type == H264_NAL_IDR));
190static BOOL nal_is_key(
const H264_CONTEXT_WEBCODECS* sys, BYTE type)
192 return sys->hevc ? ((type >= HEVC_NAL_BLA_W_LP) && (type <= HEVC_NAL_CRA))
193 : (type == H264_NAL_IDR);
198static BOOL nal_is_sps(
const H264_CONTEXT_WEBCODECS* sys, BYTE type)
200 return sys->hevc ? ((type == HEVC_NAL_VPS) || (type == HEVC_NAL_SPS)) : (type == H264_NAL_SPS);
204static BOOL nal_is_parameter_set(
const H264_CONTEXT_WEBCODECS* sys, BYTE type)
206 return nal_is_sps(sys, type) || (type == (sys->hevc ? HEVC_NAL_PPS : H264_NAL_PPS));
211static BOOL nal_is_saved(
const H264_CONTEXT_WEBCODECS* sys, BYTE type,
size_t length)
213 if (!nal_is_parameter_set(sys, type))
217 return (type != H264_NAL_SPS) || (length >= 4);
223static BOOL save_parameters(H264_CONTEXT_WEBCODECS* sys,
const BYTE* data,
size_t size, BOOL hasSps)
225 size_t total = hasSps ? 0 : sys->paramsSize;
227 const BYTE* nal =
nullptr;
229 while (next_nal(data, size, &offset, &nal, &length))
233 if (nal_is_saved(sys, nal_type(sys, nal), length))
237 BYTE* params = malloc(total);
241 if (!hasSps && (sys->paramsSize > 0))
243 memcpy(params, sys->params, sys->paramsSize);
244 pos = sys->paramsSize;
247 while (next_nal(data, size, &offset, &nal, &length))
251 if (!nal_is_saved(sys, nal_type(sys, nal), length))
253 const BYTE startCode[] = { 0, 0, 0, 1 };
254 memcpy(¶ms[pos], startCode,
sizeof(startCode));
255 memcpy(¶ms[pos + 4], nal, length);
258 WINPR_ASSERT(pos == total);
260 sys->params = params;
261 sys->paramsSize = total;
266static void codec_h264(
const H264_CONTEXT_WEBCODECS* sys,
char* codec,
size_t size)
268 (void)_snprintf(codec, size,
"avc1.42e01f");
269 if ((sys->paramsSize > 7) && ((sys->params[4] & 0x1F) == H264_NAL_SPS))
270 (void)_snprintf(codec, size,
"avc1.%02x%02x%02x", sys->params[5], sys->params[6],
276static size_t nal_rbsp(
const BYTE* data,
size_t length, BYTE* rbsp,
size_t max)
280 for (
size_t x = 0; (x < length) && (count < max); x++)
282 if ((zeros >= 2) && (data[x] == 3))
287 zeros = (data[x] == 0) ? zeros + 1 : 0;
288 rbsp[count++] = data[x];
296static void codec_hevc(
const H264_CONTEXT_WEBCODECS* sys,
char* codec,
size_t size)
298 (void)_snprintf(codec, size,
"hvc1.1.6.L93.B0");
301 const BYTE* nal =
nullptr;
303 while (next_nal(sys->params, sys->paramsSize, &offset, &nal, &length))
305 if ((length < 2) || (nal_type(sys, nal) != HEVC_NAL_SPS))
311 BYTE ptl[13] = WINPR_C_ARRAY_INIT;
312 if (nal_rbsp(&nal[2], length - 2, ptl,
sizeof(ptl)) <
sizeof(ptl))
315 const char* spaces[] = {
"",
"A",
"B",
"C" };
316 const UINT32 flags = ((UINT32)ptl[2] << 24) | ((UINT32)ptl[3] << 16) |
317 ((UINT32)ptl[4] << 8) | (UINT32)ptl[5];
319 for (
size_t x = 0; x < 32; x++)
320 reversed |= ((flags >> x) & 1u) << (31 - x);
322 char buffer[64] = WINPR_C_ARRAY_INIT;
323 int len = _snprintf(buffer,
sizeof(buffer),
"hvc1.%s%u.%" PRIX32
".%c%u",
324 spaces[ptl[1] >> 6], (
unsigned)(ptl[1] & 0x1F), reversed,
325 (ptl[1] & 0x20) ?
'H' :
'L', (
unsigned)ptl[12]);
326 size_t constraints = 6;
327 while ((constraints > 0) && (ptl[5 + constraints] == 0))
329 for (
size_t x = 0; (x < constraints) && (len > 0) && ((size_t)len <
sizeof(buffer)); x++)
330 len += _snprintf(&buffer[len],
sizeof(buffer) - (
size_t)len,
".%02X",
331 (
unsigned)ptl[6 + x]);
332 if ((len > 0) && ((
size_t)len < size))
333 (void)_snprintf(codec, size,
"%s", buffer);
339static BOOL ensure_planes(H264_CONTEXT_WEBCODECS* sys, UINT32 width, UINT32 height)
341 if (sys->yuv && (width <= sys->width) && (height <= sys->height))
343 width = MAX(width, sys->width);
344 height = MAX(height, sys->height);
345 if ((width == 0) || (height == 0) || (width > 16384) || (height > 16384))
350 const UINT32 ystride = width + 32 - width % 16;
351 const UINT32 cstride = ystride;
352 const size_t lines = height + 32 - height % 16;
353 const size_t ysize = (size_t)ystride * lines;
354 const size_t csize = ysize;
355 BYTE* yuv = winpr_aligned_calloc(ysize + 2 * csize, 1, 16);
358 winpr_aligned_free(sys->yuv);
361 sys->height = height;
362 sys->stride[0] = ystride;
363 sys->stride[1] = cstride;
364 sys->stride[2] = cstride;
367 sys->plane[1] = &yuv[ysize];
368 sys->plane[2] = &yuv[ysize + csize];
373static int webcodecs_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
374 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
377 WINPR_ASSERT(pSrcData || (SrcSize == 0));
379 H264_CONTEXT_WEBCODECS* sys = h264->pSystemData;
382 if (emscripten_is_main_browser_thread())
385 BOOL hasSlice = FALSE;
390 const BYTE* nal =
nullptr;
392 while (next_nal(pSrcData, SrcSize, &offset, &nal, &length))
396 const BYTE type = nal_type(sys, nal);
397 hasSlice |= nal_is_slice(sys, type);
398 hasIdr |= nal_is_key(sys, type);
399 hasSps |= nal_is_sps(sys, type);
400 hasPps |= nal_is_parameter_set(sys, type);
402 if ((hasSps || hasPps) && !save_parameters(sys, pSrcData, SrcSize, hasSps))
407 char codec[64] = WINPR_C_ARRAY_INIT;
409 codec_hevc(sys, codec,
sizeof(codec));
411 codec_h264(sys, codec,
sizeof(codec));
414 const BYTE* src = pSrcData;
415 UINT32 size = SrcSize;
416 BYTE* combined =
nullptr;
417 if (hasIdr && !hasSps && (sys->paramsSize > 0))
419 if (sys->paramsSize > UINT32_MAX - SrcSize)
421 size = (UINT32)sys->paramsSize + SrcSize;
422 combined = malloc(size);
425 memcpy(combined, sys->params, sys->paramsSize);
426 memcpy(&combined[sys->paramsSize], pSrcData, SrcSize);
431 h264_wc_job job = { .handle = sys->handle,
437 .stride = sys->stride,
440 if (!ensure_planes(sys, MAX(h264->width, 16), MAX(h264->height, 16)))
442 job.maxWidth = sys->width;
443 job.maxHeight = sys->height;
444 if (!on_main(main_decode, &job))
446 if (job.rc == H264_WC_NEEDS_BUFFER)
449 if (!ensure_planes(sys, job.dim[0], job.dim[1]) || !on_main(main_copy, &job))
454 WLog_Print(h264->log, WLOG_WARN,
"WebCodecs %s decode failed (%d)",
455 sys->hevc ?
"HEVC" :
"H.264", rc);
456 if ((rc != 1) && (rc != H264_WC_YUV444))
458 h264->yuv444 = (rc == H264_WC_YUV444);
461 for (
size_t x = 0; x < 3; x++)
463 h264->pYUVData[x] = sys->plane[x];
464 h264->iStride[x] = sys->stride[x];
466 h264->YUVWidth = job.dim[0];
467 h264->YUVHeight = job.dim[1];
473static void webcodecs_uninit(H264_CONTEXT* h264)
477 H264_CONTEXT_WEBCODECS* sys = h264->pSystemData;
480 if ((sys->handle > 0) && !emscripten_is_main_browser_thread())
481 (void)emscripten_proxy_sync(emscripten_proxy_get_system_queue(),
482 emscripten_main_runtime_thread_id(), main_close, &sys->handle);
483 else if (sys->handle > 0)
484 main_close(&sys->handle);
485 for (
size_t x = 0; x < 3; x++)
486 h264->pYUVData[x] =
nullptr;
487 winpr_aligned_free(sys->yuv);
490 h264->pSystemData =
nullptr;
494static BOOL webcodecs_init(H264_CONTEXT* h264)
499 if (h264->Compressor || emscripten_is_main_browser_thread())
502 H264_CONTEXT_WEBCODECS* sys = calloc(1,
sizeof(*sys));
505 sys->hevc = h264->hevc;
506 sys->hevc444 = h264->hevc && h264->hevc444;
511 const char* probe =
"avc1.42e01f";
513 probe = sys->hevc444 ?
"hvc1.4.10.L120.9E.08" :
"hvc1.1.6.L93.B0";
514 h264_wc_open open = { .handle = 0, .probe = probe };
515 if (!on_main(main_open, &open) || (open.handle <= 0))
517 WLog_Print(h264->log, WLOG_DEBUG,
"the browser cannot decode %s with WebCodecs",
518 sys->hevc ? (sys->hevc444 ?
"HEVC 4:4:4" :
"HEVC") :
"H.264");
522 sys->handle = open.handle;
523 h264->pSystemData = sys;
527const H264_CONTEXT_SUBSYSTEM g_Subsystem_WebCodecs = {
"WebCodecs", webcodecs_init,
528 webcodecs_uninit, webcodecs_decompress,