Repository navigation
Expand file tree
/
Copy pathframe.cpp
More file actions
577 lines (497 loc) · 20.3 KB
/
Copy pathframe.cpp
File metadata and controls
577 lines (497 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include "frame.h"
#include <cstring>
Napi::FunctionReference VideoFrameNative::constructor;
Napi::Object VideoFrameNative::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "VideoFrameNative", {
InstanceMethod("allocationSize", &VideoFrameNative::AllocationSize),
InstanceMethod("copyTo", &VideoFrameNative::CopyTo),
InstanceMethod("clone", &VideoFrameNative::Clone),
InstanceMethod("scale", &VideoFrameNative::Scale),
InstanceMethod("close", &VideoFrameNative::Close),
InstanceAccessor("width", &VideoFrameNative::GetWidth, nullptr),
InstanceAccessor("height", &VideoFrameNative::GetHeight, nullptr),
InstanceAccessor("format", &VideoFrameNative::GetFormat, nullptr),
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("VideoFrameNative", func);
return exports;
}
VideoFrameNative::VideoFrameNative(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<VideoFrameNative>(info), frame_(nullptr), closed_(false), ownsFrame_(true) {
Napi::Env env = info.Env();
// Constructor can be called:
// 1. With no args (for NewInstance with external frame)
// 2. With buffer, format, width, height
if (info.Length() == 0) {
// Will be set via SetFrame
return;
}
if (info.Length() < 4) {
Napi::TypeError::New(env, "Expected 4 arguments: buffer, format, width, height").ThrowAsJavaScriptException();
return;
}
// Get buffer data
if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "First argument must be a Buffer").ThrowAsJavaScriptException();
return;
}
Napi::Buffer<uint8_t> buffer = info[0].As<Napi::Buffer<uint8_t>>();
std::string format = info[1].As<Napi::String>().Utf8Value();
int width = info[2].As<Napi::Number>().Int32Value();
int height = info[3].As<Napi::Number>().Int32Value();
AVPixelFormat pixFmt = StringToPixelFormat(format);
if (pixFmt == AV_PIX_FMT_NONE) {
Napi::TypeError::New(env, "Unsupported pixel format: " + format).ThrowAsJavaScriptException();
return;
}
frame_ = av_frame_alloc();
if (!frame_) {
Napi::Error::New(env, "Failed to allocate frame").ThrowAsJavaScriptException();
return;
}
frame_->format = pixFmt;
frame_->width = width;
frame_->height = height;
int ret = av_frame_get_buffer(frame_, 0);
if (ret < 0) {
av_frame_free(&frame_);
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
Napi::Error::New(env, std::string("Failed to allocate frame buffer: ") + errBuf).ThrowAsJavaScriptException();
return;
}
// Copy data into frame based on pixel format
const uint8_t* src = buffer.Data();
size_t srcLen = buffer.Length();
if (pixFmt == AV_PIX_FMT_RGBA || pixFmt == AV_PIX_FMT_BGRA ||
pixFmt == AV_PIX_FMT_RGB0 || pixFmt == AV_PIX_FMT_BGR0) {
// Packed RGBA/BGRA format - single plane
size_t lineSize = width * 4;
for (int y = 0; y < height && (size_t)(y * lineSize) < srcLen; y++) {
memcpy(frame_->data[0] + y * frame_->linesize[0],
src + y * lineSize,
std::min(lineSize, (size_t)frame_->linesize[0]));
}
} else if (pixFmt == AV_PIX_FMT_YUV420P) {
// I420 - Y, U, V planes
size_t ySize = width * height;
size_t uvWidth = (width + 1) / 2;
size_t uvHeight = (height + 1) / 2;
size_t uvSize = uvWidth * uvHeight;
// Copy Y plane
for (int y = 0; y < height; y++) {
memcpy(frame_->data[0] + y * frame_->linesize[0],
src + y * width,
width);
}
// Copy U plane
if (srcLen > ySize) {
for (size_t y = 0; y < uvHeight; y++) {
memcpy(frame_->data[1] + y * frame_->linesize[1],
src + ySize + y * uvWidth,
uvWidth);
}
}
// Copy V plane
if (srcLen > ySize + uvSize) {
for (size_t y = 0; y < uvHeight; y++) {
memcpy(frame_->data[2] + y * frame_->linesize[2],
src + ySize + uvSize + y * uvWidth,
uvWidth);
}
}
} else if (pixFmt == AV_PIX_FMT_NV12) {
// NV12 - Y plane, interleaved UV plane
size_t ySize = width * height;
size_t uvHeight = (height + 1) / 2;
// Copy Y plane
for (int y = 0; y < height; y++) {
memcpy(frame_->data[0] + y * frame_->linesize[0],
src + y * width,
width);
}
// Copy UV plane (interleaved)
if (srcLen > ySize) {
for (size_t y = 0; y < uvHeight; y++) {
memcpy(frame_->data[1] + y * frame_->linesize[1],
src + ySize + y * width,
width);
}
}
} else if (pixFmt == AV_PIX_FMT_YUV422P) {
// I422 - Y, U, V planes (4:2:2)
size_t ySize = width * height;
size_t uvWidth = (width + 1) / 2;
size_t uvSize = uvWidth * height;
// Copy Y plane
for (int y = 0; y < height; y++) {
memcpy(frame_->data[0] + y * frame_->linesize[0],
src + y * width,
width);
}
// Copy U plane
if (srcLen > ySize) {
for (int y = 0; y < height; y++) {
memcpy(frame_->data[1] + y * frame_->linesize[1],
src + ySize + y * uvWidth,
uvWidth);
}
}
// Copy V plane
if (srcLen > ySize + uvSize) {
for (int y = 0; y < height; y++) {
memcpy(frame_->data[2] + y * frame_->linesize[2],
src + ySize + uvSize + y * uvWidth,
uvWidth);
}
}
} else if (pixFmt == AV_PIX_FMT_YUV444P) {
// I444 - Y, U, V planes (4:4:4)
size_t planeSize = width * height;
// Copy Y plane
for (int y = 0; y < height; y++) {
memcpy(frame_->data[0] + y * frame_->linesize[0],
src + y * width,
width);
}
// Copy U plane
if (srcLen > planeSize) {
for (int y = 0; y < height; y++) {
memcpy(frame_->data[1] + y * frame_->linesize[1],
src + planeSize + y * width,
width);
}
}
// Copy V plane
if (srcLen > planeSize * 2) {
for (int y = 0; y < height; y++) {
memcpy(frame_->data[2] + y * frame_->linesize[2],
src + planeSize * 2 + y * width,
width);
}
}
}
}
VideoFrameNative::~VideoFrameNative() {
if (frame_ && !closed_ && ownsFrame_) {
av_frame_free(&frame_);
}
}
Napi::Value VideoFrameNative::AllocationSize(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (closed_ || !frame_) {
Napi::Error::New(env, "Frame is closed").ThrowAsJavaScriptException();
return env.Undefined();
}
int size = av_image_get_buffer_size(
(AVPixelFormat)frame_->format,
frame_->width,
frame_->height,
1
);
if (size < 0) {
Napi::Error::New(env, "Failed to calculate allocation size").ThrowAsJavaScriptException();
return env.Undefined();
}
return Napi::Number::New(env, size);
}
Napi::Value VideoFrameNative::CopyTo(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (closed_ || !frame_) {
Napi::Error::New(env, "Frame is closed").ThrowAsJavaScriptException();
return env.Undefined();
}
if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "First argument must be a Buffer").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Buffer<uint8_t> dest = info[0].As<Napi::Buffer<uint8_t>>();
// Parse options for format conversion and rect cropping
AVPixelFormat targetFormat = static_cast<AVPixelFormat>(frame_->format);
int rectX = 0, rectY = 0;
int rectW = frame_->width, rectH = frame_->height;
if (info.Length() > 1 && info[1].IsObject()) {
Napi::Object options = info[1].As<Napi::Object>();
// Format conversion
if (options.Has("format") && options.Get("format").IsString()) {
std::string fmt = options.Get("format").As<Napi::String>().Utf8Value();
AVPixelFormat newFmt = StringToPixelFormat(fmt);
if (newFmt != AV_PIX_FMT_NONE) {
targetFormat = newFmt;
}
}
// Rect cropping
if (options.Has("rect") && options.Get("rect").IsObject()) {
Napi::Object rect = options.Get("rect").As<Napi::Object>();
if (rect.Has("x")) rectX = rect.Get("x").As<Napi::Number>().Int32Value();
if (rect.Has("y")) rectY = rect.Get("y").As<Napi::Number>().Int32Value();
if (rect.Has("width")) rectW = rect.Get("width").As<Napi::Number>().Int32Value();
if (rect.Has("height")) rectH = rect.Get("height").As<Napi::Number>().Int32Value();
// Clamp rect to frame bounds
if (rectX < 0) rectX = 0;
if (rectY < 0) rectY = 0;
if (rectX + rectW > frame_->width) rectW = frame_->width - rectX;
if (rectY + rectH > frame_->height) rectH = frame_->height - rectY;
}
}
// Check if we need conversion/cropping
bool needsConversion = (targetFormat != frame_->format) ||
(rectX != 0 || rectY != 0 ||
rectW != frame_->width || rectH != frame_->height);
if (needsConversion) {
// Use swscale for format conversion and/or cropping
SwsContext* swsCtx = sws_getContext(
rectW, rectH, static_cast<AVPixelFormat>(frame_->format),
rectW, rectH, targetFormat,
SWS_BILINEAR, nullptr, nullptr, nullptr
);
if (!swsCtx) {
Napi::Error::New(env, "Failed to create conversion context").ThrowAsJavaScriptException();
return env.Undefined();
}
// Create output frame
AVFrame* outFrame = av_frame_alloc();
if (!outFrame) {
sws_freeContext(swsCtx);
Napi::Error::New(env, "Failed to allocate output frame").ThrowAsJavaScriptException();
return env.Undefined();
}
outFrame->format = targetFormat;
outFrame->width = rectW;
outFrame->height = rectH;
int ret = av_frame_get_buffer(outFrame, 0);
if (ret < 0) {
av_frame_free(&outFrame);
sws_freeContext(swsCtx);
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
Napi::Error::New(env, std::string("Failed to allocate output buffer: ") + errBuf).ThrowAsJavaScriptException();
return env.Undefined();
}
// Adjust source pointers for rect offset
uint8_t* srcSlice[4] = {nullptr, nullptr, nullptr, nullptr};
int srcStride[4] = {0, 0, 0, 0};
AVPixelFormat srcFmt = static_cast<AVPixelFormat>(frame_->format);
// Calculate byte offset for the crop region
if (srcFmt == AV_PIX_FMT_YUV420P || srcFmt == AV_PIX_FMT_YUVA420P) {
// YUV420P: Y is full res, U/V are half res
srcSlice[0] = frame_->data[0] + rectY * frame_->linesize[0] + rectX;
srcSlice[1] = frame_->data[1] + (rectY / 2) * frame_->linesize[1] + (rectX / 2);
srcSlice[2] = frame_->data[2] + (rectY / 2) * frame_->linesize[2] + (rectX / 2);
if (srcFmt == AV_PIX_FMT_YUVA420P && frame_->data[3]) {
srcSlice[3] = frame_->data[3] + rectY * frame_->linesize[3] + rectX;
}
srcStride[0] = frame_->linesize[0];
srcStride[1] = frame_->linesize[1];
srcStride[2] = frame_->linesize[2];
srcStride[3] = frame_->linesize[3];
} else if (srcFmt == AV_PIX_FMT_YUV422P) {
// YUV422P: Y is full res, U/V are half width, full height
srcSlice[0] = frame_->data[0] + rectY * frame_->linesize[0] + rectX;
srcSlice[1] = frame_->data[1] + rectY * frame_->linesize[1] + (rectX / 2);
srcSlice[2] = frame_->data[2] + rectY * frame_->linesize[2] + (rectX / 2);
srcStride[0] = frame_->linesize[0];
srcStride[1] = frame_->linesize[1];
srcStride[2] = frame_->linesize[2];
} else if (srcFmt == AV_PIX_FMT_YUV444P) {
// YUV444P: all planes are full res
srcSlice[0] = frame_->data[0] + rectY * frame_->linesize[0] + rectX;
srcSlice[1] = frame_->data[1] + rectY * frame_->linesize[1] + rectX;
srcSlice[2] = frame_->data[2] + rectY * frame_->linesize[2] + rectX;
srcStride[0] = frame_->linesize[0];
srcStride[1] = frame_->linesize[1];
srcStride[2] = frame_->linesize[2];
} else if (srcFmt == AV_PIX_FMT_NV12) {
// NV12: Y plane, interleaved UV plane (half res)
srcSlice[0] = frame_->data[0] + rectY * frame_->linesize[0] + rectX;
srcSlice[1] = frame_->data[1] + (rectY / 2) * frame_->linesize[1] + (rectX & ~1);
srcStride[0] = frame_->linesize[0];
srcStride[1] = frame_->linesize[1];
} else {
// Packed formats (RGBA, BGRA, etc.) - 4 bytes per pixel
int bytesPerPixel = 4;
srcSlice[0] = frame_->data[0] + rectY * frame_->linesize[0] + rectX * bytesPerPixel;
srcStride[0] = frame_->linesize[0];
}
// Perform the conversion
sws_scale(swsCtx, srcSlice, srcStride, 0, rectH,
outFrame->data, outFrame->linesize);
// Copy converted frame to destination buffer
int size = av_image_copy_to_buffer(
dest.Data(),
dest.Length(),
outFrame->data,
outFrame->linesize,
targetFormat,
rectW,
rectH,
1
);
av_frame_free(&outFrame);
sws_freeContext(swsCtx);
if (size < 0) {
char errBuf[256];
av_strerror(size, errBuf, sizeof(errBuf));
Napi::Error::New(env, std::string("Failed to copy converted frame: ") + errBuf).ThrowAsJavaScriptException();
return env.Undefined();
}
} else {
// Direct copy - no conversion needed
int size = av_image_copy_to_buffer(
dest.Data(),
dest.Length(),
frame_->data,
frame_->linesize,
(AVPixelFormat)frame_->format,
frame_->width,
frame_->height,
1
);
if (size < 0) {
char errBuf[256];
av_strerror(size, errBuf, sizeof(errBuf));
Napi::Error::New(env, std::string("Failed to copy frame data: ") + errBuf).ThrowAsJavaScriptException();
return env.Undefined();
}
}
return env.Undefined();
}
Napi::Value VideoFrameNative::Clone(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (closed_ || !frame_) {
Napi::Error::New(env, "Frame is closed").ThrowAsJavaScriptException();
return env.Undefined();
}
AVFrame* cloned = av_frame_clone(frame_);
if (!cloned) {
Napi::Error::New(env, "Failed to clone frame").ThrowAsJavaScriptException();
return env.Undefined();
}
return NewInstance(env, cloned);
}
void VideoFrameNative::Close(const Napi::CallbackInfo& info) {
if (!closed_ && frame_ && ownsFrame_) {
av_frame_free(&frame_);
frame_ = nullptr;
closed_ = true;
}
}
Napi::Value VideoFrameNative::GetWidth(const Napi::CallbackInfo& info) {
if (closed_ || !frame_) {
return info.Env().Undefined();
}
return Napi::Number::New(info.Env(), frame_->width);
}
Napi::Value VideoFrameNative::GetHeight(const Napi::CallbackInfo& info) {
if (closed_ || !frame_) {
return info.Env().Undefined();
}
return Napi::Number::New(info.Env(), frame_->height);
}
Napi::Value VideoFrameNative::GetFormat(const Napi::CallbackInfo& info) {
if (closed_ || !frame_) {
return info.Env().Undefined();
}
return Napi::String::New(info.Env(), PixelFormatToString((AVPixelFormat)frame_->format));
}
Napi::Value VideoFrameNative::Scale(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!frame_) {
Napi::Error::New(env, "Frame is closed").ThrowAsJavaScriptException();
return env.Undefined();
}
if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "scale(width, height) expects two numbers").ThrowAsJavaScriptException();
return env.Undefined();
}
int dstW = info[0].As<Napi::Number>().Int32Value();
int dstH = info[1].As<Napi::Number>().Int32Value();
if (dstW <= 0 || dstH <= 0) {
Napi::TypeError::New(env, "scale dimensions must be positive").ThrowAsJavaScriptException();
return env.Undefined();
}
AVFrame* out = av_frame_alloc();
out->format = frame_->format;
out->width = dstW;
out->height = dstH;
out->pts = frame_->pts;
NWC_FRAME_DURATION(out) = NWC_FRAME_DURATION(frame_);
if (av_frame_get_buffer(out, 0) < 0) {
av_frame_free(&out);
Napi::Error::New(env, "Failed to allocate scaled frame").ThrowAsJavaScriptException();
return env.Undefined();
}
// threads=0 -> auto; sws_getContext would be single-threaded
SwsContext* sws = sws_alloc_context();
av_opt_set_int(sws, "srcw", frame_->width, 0);
av_opt_set_int(sws, "srch", frame_->height, 0);
av_opt_set_int(sws, "src_format", frame_->format, 0);
av_opt_set_int(sws, "dstw", dstW, 0);
av_opt_set_int(sws, "dsth", dstH, 0);
av_opt_set_int(sws, "dst_format", frame_->format, 0);
av_opt_set_int(sws, "sws_flags", SWS_BILINEAR, 0);
av_opt_set_int(sws, "threads", 0, 0);
if (sws_init_context(sws, nullptr, nullptr) < 0) {
sws_freeContext(sws);
av_frame_free(&out);
Napi::Error::New(env, "Failed to create scaler for this pixel format").ThrowAsJavaScriptException();
return env.Undefined();
}
int ret = sws_scale_frame(sws, out, frame_);
sws_freeContext(sws);
if (ret < 0) {
av_frame_free(&out);
Napi::Error::New(env, "Scaling failed").ThrowAsJavaScriptException();
return env.Undefined();
}
av_frame_copy_props(out, frame_);
return NewInstance(env, out);
}
Napi::Object VideoFrameNative::NewInstance(Napi::Env env, AVFrame* frame) {
Napi::Object obj = constructor.New({});
VideoFrameNative* instance = Napi::ObjectWrap<VideoFrameNative>::Unwrap(obj);
instance->frame_ = frame;
instance->ownsFrame_ = true;
return obj;
}
AVPixelFormat StringToPixelFormat(const std::string& format) {
if (format == "I420") return AV_PIX_FMT_YUV420P;
if (format == "I420A") return AV_PIX_FMT_YUVA420P;
if (format == "I422") return AV_PIX_FMT_YUV422P;
if (format == "I444") return AV_PIX_FMT_YUV444P;
if (format == "NV12") return AV_PIX_FMT_NV12;
if (format == "RGBA") return AV_PIX_FMT_RGBA;
if (format == "RGBX") return AV_PIX_FMT_RGB0;
if (format == "BGRA") return AV_PIX_FMT_BGRA;
if (format == "BGRX") return AV_PIX_FMT_BGR0;
return AV_PIX_FMT_NONE;
}
std::string PixelFormatToString(AVPixelFormat format) {
switch (format) {
case AV_PIX_FMT_YUV420P: return "I420";
case AV_PIX_FMT_YUVA420P: return "I420A";
case AV_PIX_FMT_YUV422P: return "I422";
case AV_PIX_FMT_YUV444P: return "I444";
case AV_PIX_FMT_NV12: return "NV12";
case AV_PIX_FMT_RGBA: return "RGBA";
case AV_PIX_FMT_RGB0: return "RGBX";
case AV_PIX_FMT_BGRA: return "BGRA";
case AV_PIX_FMT_BGR0: return "BGRX";
default: return "";
}
}
Napi::Value CreateVideoFrame(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 4) {
Napi::TypeError::New(env, "Expected 4 arguments: buffer, format, width, height").ThrowAsJavaScriptException();
return env.Undefined();
}
// Create new VideoFrameNative instance
return VideoFrameNative::constructor.New({
info[0], // buffer
info[1], // format
info[2], // width
info[3], // height
});
}