Skip to content

Commit af0ab03

Browse files
committed
Add initial support for hw acceleration properties
1 parent 593d712 commit af0ab03

File tree

11 files changed

+668
-10
lines changed

11 files changed

+668
-10
lines changed

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"src/encode.cc", "src/mux.cc",
88
"src/packet.cc", "src/frame.cc",
99
"src/codec_par.cc", "src/format.cc",
10-
"src/codec.cc" ],
10+
"src/codec.cc", "src/hwcontext.cc" ],
1111
"conditions": [
1212
['OS!="win"', {
1313
"defines": [

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export * from "./types/Filter"
1010
export * from "./types/Encoder"
1111
export * from "./types/Muxer"
1212
export * from "./types/Beamstreams"
13+
export * from "./types/HWContext"
1314

1415
export const AV_NOPTS_VALUE: number
1516

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "beamcoder",
3-
"version": "0.6.3",
3+
"version": "0.6.4",
44
"description": "Node.js native bindings to FFmpeg.",
55
"main": "index.js",
66
"types": "index.d.ts",

src/beamcoder.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,9 @@ napi_value Init(napi_env env, napi_value exports) {
919919
status = napi_define_properties(env, exports, 28, desc);
920920
CHECK_STATUS;
921921

922+
avdevice_register_all();
923+
avformat_network_init();
924+
922925
// Iterate over all codecs to makes sure they are registered
923926
void* opaque = nullptr;
924927
const AVCodec* codec;

src/codec.cc

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include "codec.h"
23+
#include "hwcontext.h"
2324

2425
napi_value getCodecCtxCodecID(napi_env env, napi_callback_info info) {
2526
napi_status status;
@@ -6495,6 +6496,56 @@ napi_value getCodecCtxCodedSideData(napi_env env, napi_callback_info info) {
64956496
return result;
64966497
}
64976498

6499+
napi_value getCodecHWFramesCtx(napi_env env, napi_callback_info info) {
6500+
napi_status status;
6501+
napi_value result;
6502+
AVCodecContext* codec;
6503+
6504+
size_t argc = 0;
6505+
status = napi_get_cb_info(env, info, &argc, nullptr, nullptr, (void**) &codec);
6506+
CHECK_STATUS;
6507+
6508+
if (codec->hw_frames_ctx == nullptr) {
6509+
status = napi_get_null(env, &result);
6510+
CHECK_STATUS;
6511+
} else {
6512+
status = fromHWFramesContext(env, codec->hw_frames_ctx, &result);
6513+
CHECK_STATUS;
6514+
}
6515+
6516+
return result;
6517+
}
6518+
6519+
napi_value setCodecHWFramesCtx(napi_env env, napi_callback_info info) {
6520+
napi_status status;
6521+
napi_value result, contextExt;
6522+
napi_valuetype type;
6523+
AVCodecContext* codec;
6524+
AVBufferRef* contextRef;
6525+
6526+
size_t argc = 1;
6527+
napi_value args[1];
6528+
status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &codec);
6529+
CHECK_STATUS;
6530+
if (argc < 1) {
6531+
NAPI_THROW_ERROR("A value is required to set the hw_frames_context property.");
6532+
}
6533+
status = napi_typeof(env, args[0], &type);
6534+
CHECK_STATUS;
6535+
if (type != napi_object) {
6536+
NAPI_THROW_ERROR("An object is required to set the hw_frames_context property.");
6537+
}
6538+
status = napi_get_named_property(env, args[0], "_framesContext", &contextExt);
6539+
CHECK_STATUS;
6540+
status = napi_get_value_external(env, contextExt, (void**) &contextRef);
6541+
CHECK_STATUS;
6542+
codec->hw_frames_ctx = av_buffer_ref(contextRef);
6543+
6544+
status = napi_get_undefined(env, &result);
6545+
CHECK_STATUS;
6546+
return result;
6547+
}
6548+
64986549
napi_value getCodecCtxSubTextFmt(napi_env env, napi_callback_info info) {
64996550
napi_status status;
65006551
napi_value result;
@@ -6618,6 +6669,26 @@ napi_value setCodecCtxMaxPixels(napi_env env, napi_callback_info info) {
66186669
return result;
66196670
}
66206671

6672+
napi_value getCodecHWDeviceCtx(napi_env env, napi_callback_info info) {
6673+
napi_status status;
6674+
napi_value result;
6675+
AVCodecContext* codec;
6676+
6677+
size_t argc = 0;
6678+
status = napi_get_cb_info(env, info, &argc, nullptr, nullptr, (void**) &codec);
6679+
CHECK_STATUS;
6680+
6681+
if (codec->hw_device_ctx == nullptr) {
6682+
status = napi_get_null(env, &result);
6683+
CHECK_STATUS;
6684+
} else {
6685+
status = fromHWDeviceContext(env, codec->hw_device_ctx, &result);
6686+
CHECK_STATUS;
6687+
}
6688+
6689+
return result;
6690+
}
6691+
66216692
napi_value getCodecCtxHwAccelFlags(napi_env env, napi_callback_info info) {
66226693
napi_status status;
66236694
napi_value result;
@@ -7227,17 +7298,20 @@ napi_status fromAVCodecContext(napi_env env, AVCodecContext* codec,
72277298
{ "coded_side_data", nullptr, nullptr,
72287299
encoding ? getCodecCtxCodedSideData : nullptr, failBoth, nullptr,
72297300
encoding ? napi_enumerable : napi_default, codec},
7230-
// TODO hw_frames_ctx
7301+
{ "hw_frames_ctx", nullptr, nullptr,
7302+
getCodecHWFramesCtx,
7303+
encoding ? setCodecHWFramesCtx : failDecoding, nullptr,
7304+
encoding ? (napi_property_attributes) (napi_writable | napi_enumerable) : napi_enumerable, codec},
7305+
// 130
72317306
{ "sub_text_format", nullptr, nullptr,
72327307
encoding ? nullptr : getCodecCtxSubTextFmt,
72337308
encoding ? failEncoding : setCodecCtxSubTextFmt, nullptr,
72347309
encoding ? napi_default : (napi_property_attributes) (napi_writable | napi_enumerable), codec},
7235-
// 130
72367310
{ "trailing_padding", nullptr, nullptr, getCodecCtxTrailPad, setCodecCtxTrailPad, nullptr,
72377311
(napi_property_attributes) (napi_writable | napi_enumerable), codec},
72387312
{ "max_pixels", nullptr, nullptr, getCodecCtxMaxPixels, setCodecCtxMaxPixels, nullptr,
72397313
(napi_property_attributes) (napi_writable | napi_enumerable), codec},
7240-
// TODO hw_device_ctx
7314+
{ "hw_device_ctx", nullptr, nullptr, getCodecHWDeviceCtx, nullptr, nullptr, napi_enumerable, codec},
72417315
{ "hwaccel_flags", nullptr, nullptr,
72427316
encoding ? nullptr : getCodecCtxHwAccelFlags,
72437317
encoding ? failEncoding : setCodecCtxHwAccelFlags, nullptr,
@@ -7256,15 +7330,15 @@ napi_status fromAVCodecContext(napi_env env, AVCodecContext* codec,
72567330
encoding ? flushEnc : flushDec, nullptr, nullptr, nullptr, napi_enumerable, codec},
72577331
{ "extractParams", nullptr, extractParams, nullptr, nullptr, nullptr, napi_enumerable, nullptr},
72587332
{ "useParams", nullptr, useParams, nullptr, nullptr, nullptr, napi_enumerable, nullptr},
7333+
// 140
72597334
// Hidden values - to allow Object.assign to work
72607335
{ "params", nullptr, nullptr, nullptr, nop, undef, // Set for muxing
72617336
napi_writable, nullptr},
72627337
{ "stream_index", nullptr, nullptr, nullptr, nop, undef, napi_writable, nullptr },
7263-
// 140
72647338
{ "demuxer", nullptr, nullptr, nullptr, nop, undef, napi_writable, nullptr},
72657339
{ "_CodecContext", nullptr, nullptr, nullptr, nullptr, extCodec, napi_default, nullptr }
72667340
};
7267-
status = napi_define_properties(env, jsCodec, 142, desc);
7341+
status = napi_define_properties(env, jsCodec, 144, desc);
72687342
PASS_STATUS;
72697343

72707344
*result = jsCodec;

src/frame.cc

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include "frame.h"
23+
#include "hwcontext.h"
2324

2425
napi_value getFrameLinesize(napi_env env, napi_callback_info info) {
2526
napi_status status;
@@ -1994,6 +1995,56 @@ napi_value setFramePktSize(napi_env env, napi_callback_info info) {
19941995
return result;
19951996
}
19961997

1998+
napi_value getFrameHWFramesCtx(napi_env env, napi_callback_info info) {
1999+
napi_status status;
2000+
napi_value result;
2001+
frameData* f;
2002+
2003+
size_t argc = 0;
2004+
status = napi_get_cb_info(env, info, &argc, nullptr, nullptr, (void**) &f);
2005+
CHECK_STATUS;
2006+
2007+
if (f->frame->hw_frames_ctx == nullptr) {
2008+
status = napi_get_null(env, &result);
2009+
CHECK_STATUS;
2010+
} else {
2011+
status = fromHWFramesContext(env, f->frame->hw_frames_ctx, &result);
2012+
CHECK_STATUS;
2013+
}
2014+
2015+
return result;
2016+
}
2017+
2018+
napi_value setFrameHWFramesCtx(napi_env env, napi_callback_info info) {
2019+
napi_status status;
2020+
napi_value result, contextExt;
2021+
napi_valuetype type;
2022+
frameData* f;
2023+
AVBufferRef* contextRef;
2024+
2025+
size_t argc = 1;
2026+
napi_value args[1];
2027+
status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &f);
2028+
CHECK_STATUS;
2029+
if (argc < 1) {
2030+
NAPI_THROW_ERROR("A value is required to set the hw_frames_context property.");
2031+
}
2032+
status = napi_typeof(env, args[0], &type);
2033+
CHECK_STATUS;
2034+
if (type != napi_object) {
2035+
NAPI_THROW_ERROR("An object is required to set the hw_frames_context property.");
2036+
}
2037+
status = napi_get_named_property(env, args[0], "_framesContext", &contextExt);
2038+
CHECK_STATUS;
2039+
status = napi_get_value_external(env, contextExt, (void**) &contextRef);
2040+
CHECK_STATUS;
2041+
f->frame->hw_frames_ctx = av_buffer_ref(contextRef);
2042+
2043+
status = napi_get_undefined(env, &result);
2044+
CHECK_STATUS;
2045+
return result;
2046+
}
2047+
19972048
napi_value getFrameCropTop(napi_env env, napi_callback_info info) {
19982049
napi_status status;
19992050
napi_value result;
@@ -2534,20 +2585,22 @@ napi_status fromAVFrame(napi_env env, frameData* f, napi_value* result) {
25342585
(napi_property_attributes) (napi_writable | napi_enumerable), f },
25352586
{ "pkt_size", nullptr, nullptr, getFramePktSize, setFramePktSize, nullptr,
25362587
(napi_property_attributes) (napi_writable | napi_enumerable), f },
2588+
{ "hw_frames_ctx", nullptr, nullptr, getFrameHWFramesCtx, setFrameHWFramesCtx, nullptr,
2589+
(napi_property_attributes) (napi_writable | napi_enumerable), f},
25372590
{ "crop_top", nullptr, nullptr, getFrameCropTop, setFrameCropTop, nullptr,
25382591
(napi_property_attributes) (napi_writable | napi_enumerable), f },
25392592
{ "crop_bottom", nullptr, nullptr, getFrameCropBottom, setFrameCropBottom, nullptr,
25402593
(napi_property_attributes) (napi_writable | napi_enumerable), f },
2594+
// 40
25412595
{ "crop_left", nullptr, nullptr, getFrameCropLeft, setFrameCropLeft, nullptr,
25422596
(napi_property_attributes) (napi_writable | napi_enumerable), f },
2543-
// 40
25442597
{ "crop_right", nullptr, nullptr, getFrameCropRight, setFrameCropRight, nullptr,
25452598
(napi_property_attributes) (napi_writable | napi_enumerable), f },
25462599
{ "alloc", nullptr, alloc, nullptr, nullptr, nullptr, napi_enumerable, nullptr },
25472600
{ "toJSON", nullptr, frameToJSON, nullptr, nullptr, nullptr, napi_default, f },
25482601
{ "_frame", nullptr, nullptr, nullptr, nullptr, extFrame, napi_default, nullptr }
25492602
};
2550-
status = napi_define_properties(env, jsFrame, 43, desc);
2603+
status = napi_define_properties(env, jsFrame, 44, desc);
25512604
PASS_STATUS;
25522605

25532606
for ( int x = 0 ; x < AV_NUM_DATA_POINTERS ; x++ ) {

0 commit comments

Comments
 (0)