Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 27 additions & 34 deletions src/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "frame.h"
#include "hwcontext.h"
#include <libavutil/pixdesc.h>

napi_value getFrameLinesize(napi_env env, napi_callback_info info) {
napi_status status;
Expand Down Expand Up @@ -1051,50 +1052,42 @@ napi_value getFrameData(napi_env env, napi_callback_info info) {
napi_status status;
napi_value array, element;
frameData* f;
uint8_t* data;
AVBufferRef* ref;
size_t size;
int curElem;

status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &f);
void* resultData;
int curElem = 0;
status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**)&f);
CHECK_STATUS;

status = napi_create_array(env, &array);
CHECK_STATUS;

data = f->frame->data[0];
ref = f->frame->buf[0] ? av_buffer_ref(f->frame->buf[0]) : nullptr;
size = ref ? ref->size : 0;
curElem = 0;
// work through frame bufs checking whether allocation refcounts are shared
for ( int x = 1 ; x < AV_NUM_DATA_POINTERS ; x++ ) {
// printf("Buffer %i is %p\n", x, f->frame->data[x]);
if (f->frame->data[x] == nullptr) continue;
size_t bufSize = size;
if (f->frame->buf[x] == nullptr)
bufSize = f->frame->data[x] - f->frame->data[x-1];
status = napi_create_external_buffer(env, bufSize, data, frameBufferFinalizer, ref, &element);
CHECK_STATUS;
status = napi_set_element(env, array, curElem, element);
CHECK_STATUS;
data = f->frame->data[x];
if (f->frame->buf[x]) {
ref = av_buffer_ref(f->frame->buf[x]);
size = ref->size;
} else {
ref = nullptr;
size -= f->frame->data[x] - f->frame->data[x-1];
AVFrame* frame = f->frame;
enum AVPixelFormat fmt = (enum AVPixelFormat)frame->format;
const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(fmt);
if (!desc) return array; // unknown format

for (int x = 0; x < AV_NUM_DATA_POINTERS && frame->data[x]; x++) {
// Determine the number of rows for this plane
int plane_h = frame->height;

// YUVA420P: Y=full, U/V=half, A=full
if (fmt == AV_PIX_FMT_YUVA420P) {
if (x == 1 || x == 2) plane_h = (frame->height + 1) / 2;
}
curElem++;
}
if (data) {
status = napi_create_external_buffer(env, size, data, frameBufferFinalizer, ref, &element);
// For other planar formats, adjust with chroma subsampling if needed
else if (desc->log2_chroma_h && x > 0) {
plane_h = (frame->height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣
absolutely wild bitwise operation

}

size_t plane_size = frame->linesize[x] * plane_h;

// Copy exactly plane_size bytes for this plane
status = napi_create_buffer_copy(env, plane_size, frame->data[x], &resultData, &element);
CHECK_STATUS;
status = napi_set_element(env, array, curElem, element);

status = napi_set_element(env, array, curElem++, element);
CHECK_STATUS;
}

CHECK_STATUS;
return array;
}

Expand Down
2 changes: 1 addition & 1 deletion src/governor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void readComplete(napi_env env, napi_status asyncStatus, void *data) {
}
REJECT_STATUS;

c->status = napi_create_external_buffer(env, c->readLen, c->readBuf, readFinalizer, (void*)(uint64_t)c->readLen, &result);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, DONT LOOK AT THIS CHANGE

c->status = napi_create_buffer(env, c->readLen, &c->readBuf, &result);
REJECT_STATUS;

c->status = napi_adjust_external_memory(env, c->readLen, &externalMemory);
Expand Down
4 changes: 2 additions & 2 deletions src/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ napi_value getPacketData(napi_env env, napi_callback_info info) {
napi_value result;
packetData* p;
AVBufferRef* hintRef;
void* resultData;

status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &p);
CHECK_STATUS;
Expand All @@ -135,8 +136,7 @@ napi_value getPacketData(napi_env env, napi_callback_info info) {
status = napi_get_null(env, &result);
} else {
hintRef = av_buffer_ref(p->packet->buf);
status = napi_create_external_buffer(env, hintRef->size, hintRef->data,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, DONT LOOK AT THIS CHANGE

packetBufferFinalizer, hintRef, &result);
status = napi_create_buffer_copy(env, hintRef->size, hintRef->data, &resultData, &result);
CHECK_STATUS;
}

Expand Down