add nv12 pixel

This commit is contained in:
2025-05-26 23:37:10 -05:00
parent ae5ba67fc8
commit 00df0899fa
3 changed files with 26 additions and 35 deletions

View File

@@ -431,6 +431,7 @@ function handle_renderer(msg) {
var tex; var tex;
// Direct surface data // Direct surface data
var surf = new surface(msg.data) var surf = new surface(msg.data)
if (!surf) if (!surf)
return {error: "Must provide surface_id or surface data"}; return {error: "Must provide surface_id or surface data"};

View File

@@ -154,25 +154,32 @@ JSC_CCALL(surface_dup,
return SDL_Surface2js(js,conv); return SDL_Surface2js(js,conv);
) )
JSC_CCALL(surface_pixels, void *surface_pixel_dup(SDL_Surface *surf, size_t *size)
SDL_Surface *surf = js2SDL_Surface(js, self); {
int locked = 0; int locked = 0;
if (SDL_MUSTLOCK(surf)) if (SDL_MUSTLOCK(surf))
if (SDL_LockSurface(surf) < 0) if (SDL_LockSurface(surf) < 0)
return JS_ThrowReferenceError(js, "Lock surface failed: %s", SDL_GetError()); return NULL;
size_t byte_size; if (surf->format == SDL_PIXELFORMAT_NV12) {
if (SDL_ISPIXELFORMAT_FOURCC(surf->format)) { *size = surf->pitch * (surf->h*3.0/2.0);
/* Planar/YUV formats: use BitsPerPixel to compute true size */
printf("FOURCC!!! Bits is %d\n", SDL_BYTESPERPIXEL(surf->format));
byte_size = (size_t)surf->pitch * surf->h * SDL_BYTESPERPIXEL(surf->format);
} else } else
byte_size = (size_t)surf->pitch * surf->h; *size = (size_t)surf->pitch * surf->h;
ret = JS_NewArrayBufferCopy(js, surf->pixels, byte_size); void *data = malloc(*size);
memcpy(data, surf->pixels, *size);
if (locked) SDL_UnlockSurface(surf); if (locked) SDL_UnlockSurface(surf);
return data;
}
JSC_CCALL(surface_pixels,
SDL_Surface *surf = js2SDL_Surface(js, self);
size_t size;
void *data = surface_pixel_dup(surf, &size);
return JS_NewArrayBufferCopy(js, data, size);
) )
JSC_CCALL(surface_get_width, JSC_CCALL(surface_get_width,
@@ -211,25 +218,10 @@ JSC_CCALL(surface_toJSON,
// Add pitch // Add pitch
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, surf->pitch)); JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, surf->pitch));
// Lock surface if needed size_t size;
int locked = 0; void *pixels = surface_pixel_dup(surf, &size);
if (SDL_MUSTLOCK(surf)) {
if (SDL_LockSurface(surf) < 0) {
JS_FreeValue(js, obj);
return JS_ThrowInternalError(js, "Lock surface failed: %s", SDL_GetError());
}
locked = 1;
}
// Add pixels as ArrayBuffer
size_t byte_size = surf->pitch * surf->h;
JSValue pixels = JS_NewArrayBufferCopy(js, surf->pixels, byte_size);
JS_SetPropertyStr(js, obj, "pixels", pixels);
// Unlock if we locked
if (locked)
SDL_UnlockSurface(surf);
JS_SetPropertyStr(js, obj, "pixels", JS_NewArrayBufferCopy(js, pixels, size));
return obj; return obj;
) )

View File

@@ -163,9 +163,7 @@ function start_capturing() {
draw2d.clear(); draw2d.clear();
// Capture frame from camera // Capture frame from camera
var surface = cam_obj.capture(); var surface = cam_obj.capture().convert("rgba8888");
surface = surface.convert("abgr8888")
if (surface) { if (surface) {
// Create texture from surface directly // Create texture from surface directly
@@ -247,4 +245,4 @@ function start_capturing() {
} }
// Stop after 12 seconds if not already stopped // Stop after 12 seconds if not already stopped
$_.delay($_.stop, 12); $_.delay($_.stop, 12);1