Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wasm build support #3577

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
24 changes: 23 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on:
on:
push:
pull_request:

Expand Down Expand Up @@ -138,3 +138,25 @@ jobs:
with:
name: artifacts
path: ./out/artifacts

wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: mymindstorm/setup-emsdk@v11
with:
version: 3.1.16
- uses: actions/setup-node@v3
with:
node-version: 16
- run: node -v
- run: emcc -v
- run: sudo apt-get update -qq
- run: sudo apt-get install -qq nasm g++-multilib gcc-multilib libc6-dev-i386
- run: make gmp-bootstrap
- run: make gtest-bootstrap
- run: bash ./build/mktargets.sh
- run: ( cd test/encoder_binary_comparison ; ./run_PrepareAllTestData.sh 64 wasm )
- run: ./run_Test.sh BinaryCompareWasm BA_MW_D.264
- run: ./run_Test.sh BinaryCompareWasm Adobe_PDF_sample_a_1024x768_50Frms.264
- run: ./run_Test.sh BinaryCompareWasm Zhling_1280x720.264
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ testbin/test_vd_rc.264
testbin/test_vd_rc.yuv
testbin/test.264
testbin/test.yuv
testbin/Static.264
testbin/Static.yuv

# iOS output files
codec/build/iOS/common/build/
Expand All @@ -63,3 +65,11 @@ codec/build/iOS/dec/welsdec/build/
/gradle/
/gradlew.bat
/local.properties

#wasm
.vscode/*
*.html
*.js
*.wat
*.wasm
*.data
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ OS=$(shell uname | tr A-Z a-z | tr -d \\-0-9. | sed -E 's/^(net|open|free)bsd/bs
ARCH=$(shell uname -m)
LIBPREFIX=lib
LIBSUFFIX=a
# EMFS = nodefs | memfs
EMFS=
CCAS=$(CC)
CXX_O=-o $@
CXX_LINK_O=-o $@
Expand Down Expand Up @@ -170,7 +172,7 @@ clean:
ifeq (android,$(OS))
clean: clean_Android
endif
$(QUIET)rm -f $(OBJS) $(OBJS:.$(OBJ)=.d) $(OBJS:.$(OBJ)=.obj) $(LIBRARIES) $(BINARIES) *.lib *.a *.dylib *.dll *.so *.so.* *.exe *.pdb *.exp *.pc *.res *.map $(SRC_PATH)codec/common/inc/version_gen.h
$(QUIET)rm -f $(OBJS) $(OBJS:.$(OBJ)=.d) $(OBJS:.$(OBJ)=.obj) $(LIBRARIES) $(BINARIES) *.lib *.a *.js *.html *.wasm *.wat *.data *.dylib *.dll *.so *.so.* *.exe *.pdb *.exp *.pc *.res *.map $(SRC_PATH)codec/common/inc/version_gen.h

gmp-bootstrap:
if [ ! -d gmp-api ] ; then git clone https://github.com/mozilla/gmp-api gmp-api ; fi
Expand All @@ -192,8 +194,10 @@ endif

test: codec_unittest$(EXEEXT)
ifeq ($(BUILD_UT_EXE), Yes)
ifneq (wasm,$(OS))
./codec_unittest
endif
endif

else
test:
Expand Down Expand Up @@ -384,4 +388,4 @@ OBJDIRS = $(sort $(dir $(OBJS)))
$(OBJDIRS):
$(QUIET)mkdir -p $@

$(OBJS): | $(OBJDIRS)
$(OBJS): | $(OBJDIRS)
2 changes: 1 addition & 1 deletion build/mktargets.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,4 @@ def find_sources():
f.write("binaries: %s$(EXEEXT)\n"%args.binary)
f.write("BINARIES += %s$(EXEEXT)\n"%args.binary)

f.close()
f.close()
28 changes: 14 additions & 14 deletions build/mktargets.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/sh
cd "$(git rev-parse --show-toplevel 2>/dev/null)" >/dev/null 2>&1
python build/mktargets.py --directory codec/decoder --library decoder
python build/mktargets.py --directory codec/encoder --library encoder --exclude DllEntry.cpp
python build/mktargets.py --directory codec/common --library common --exclude asm_inc.asm --exclude arm_arch_common_macro.S --exclude arm_arch64_common_macro.S
python build/mktargets.py --directory codec/processing --library processing
python2 build/mktargets.py --directory codec/decoder --library decoder
python2 build/mktargets.py --directory codec/encoder --library encoder --exclude DllEntry.cpp
python2 build/mktargets.py --directory codec/common --library common --exclude asm_inc.asm --exclude arm_arch_common_macro.S --exclude arm_arch64_common_macro.S
python2 build/mktargets.py --directory codec/processing --library processing

python build/mktargets.py --directory codec/console/dec --binary h264dec
python build/mktargets.py --directory codec/console/enc --binary h264enc
python build/mktargets.py --directory codec/console/common --library console_common
python build/mktargets.py --directory test/encoder --prefix encoder_unittest
python build/mktargets.py --directory test/decoder --prefix decoder_unittest
python build/mktargets.py --directory test/processing --prefix processing_unittest
python build/mktargets.py --directory test/api --prefix api_test
python build/mktargets.py --directory test/common --prefix common_unittest
python build/mktargets.py --directory module --prefix module
python build/mktargets.py --directory gtest/googletest --library gtest --out build/gtest-targets.mk --cpp-suffix .cc --include gtest-all.cc
python2 build/mktargets.py --directory codec/console/dec --binary h264dec
python2 build/mktargets.py --directory codec/console/enc --binary h264enc
python2 build/mktargets.py --directory codec/console/common --library console_common
python2 build/mktargets.py --directory test/encoder --prefix encoder_unittest
python2 build/mktargets.py --directory test/decoder --prefix decoder_unittest
python2 build/mktargets.py --directory test/processing --prefix processing_unittest
python2 build/mktargets.py --directory test/api --prefix api_test
python2 build/mktargets.py --directory test/common --prefix common_unittest
python2 build/mktargets.py --directory module --prefix module
python2 build/mktargets.py --directory gtest/googletest --library gtest --out build/gtest-targets.mk --cpp-suffix .cc --include gtest-all.cc
35 changes: 35 additions & 0 deletions build/platform-wasm.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
include $(SRC_PATH)build/arch.mk
EXEEXT = .html
SHAREDLIBSUFFIX = so
SHAREDLIBSUFFIXFULLVER=$(SHAREDLIBSUFFIX).$(FULL_VERSION)
SHAREDLIBSUFFIXMAJORVER=$(SHAREDLIBSUFFIX).$(SHAREDLIB_MAJORVERSION)
SHLDFLAGS = -Wl,-soname,$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXMAJORVER)
CFLAGS += -Wall -fno-strict-aliasing -fPIC -MMD -MP
# fix undifined symbol: __stack_chk_guard bug
# flags needed to build wasm
CFLAGS += -U_FORTIFY_SOURCE -pthread -DWASMSIMD -msimd128 -sINITIAL_MEMORY=134217728 -sALLOW_MEMORY_GROWTH -sPROXY_TO_PTHREAD -sEXIT_RUNTIME
CXXFLAGS += -U_FORTIFY_SOURCE -pthread -DWASMSIMD -msimd128 -sINITIAL_MEMORY=134217728 -sALLOW_MEMORY_GROWTH -sPROXY_TO_PTHREAD -sEXIT_RUNTIME
LDFLAGS += -U_FORTIFY_SOURCE -pthread -msimd128 -sINITIAL_MEMORY=134217728 -sALLOW_MEMORY_GROWTH -sPROXY_TO_PTHREAD -sEXIT_RUNTIME
ifeq ($(EMFS), nodefs)
CFLAGS += -DNODEFS
CXXFLAGS += -DNODEFS
LDFLAGS += -lnodefs.js
endif
ifeq ($(EMFS), noderawfs)
CFLAGS += -DNODERAWFS
CXXFLAGS += -DNODERAWFS
LDFLAGS += -s NODERAWFS=1
endif
ifeq ($(EMFS), memfs)
CFLAGS += -DMEMFS
CXXFLAGS += -DMEMFS
LDFLAGS += --preload-file ./testbin/workload
endif
ifeq ($(WASMDEBUG), True)
CFLAGS += -g2
CXXFLAGS += -g2
LDFLAGS += -g2
endif

STATIC_LDFLAGS += -lm
AR_OPTS = crD $@
47 changes: 44 additions & 3 deletions codec/console/dec/src/h264dec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#if defined (ANDROID_NDK)
#include <android/log.h>
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include "codec_def.h"
#include "codec_app_def.h"
#include "codec_api.h"
Expand All @@ -60,6 +64,13 @@ float g_fDecFPS = 0.0;
int g_iDecodedFrameNum = 0;
#endif

#if defined(NODEFS)
#define CWD "/workload/"
#endif
#if defined(MEMFS)
#define CWD "testbin/workload/"
#endif

#if defined(ANDROID_NDK)
#define LOG_TAG "welsdec"
#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Expand Down Expand Up @@ -256,7 +267,7 @@ void H264DecodeInstance (ISVCDecoder* pDecoder, const char* kpH264FileName, cons
if (kpH264FileName) {
pH264File = fopen (kpH264FileName, "rb");
if (pH264File == NULL) {
fprintf (stderr, "Can not open h264 source file, check its legal path related please..\n");
fprintf (stderr, "Can not open h264 source file: %s, check its legal path related please..\n", kpH264FileName);
return;
}
fprintf (stderr, "H264 source file name: %s..\n", kpH264FileName);
Expand Down Expand Up @@ -505,14 +516,27 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
sDecParam.sVideoProperty.size = sizeof (sDecParam.sVideoProperty);
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_MV_COPY_CROSS_IDR_FREEZE_RES_CHANGE;

#if defined(NODEFS)
EM_ASM(
FS.mkdir('/workload');
FS.mount(NODEFS, { root: '.' }, '/workload');
);
#endif

if (iArgC < 2) {
printf ("usage 1: h264dec.exe welsdec.cfg\n");
printf ("usage 2: h264dec.exe welsdec.264 out.yuv\n");
printf ("usage 3: h264dec.exe welsdec.264\n");
return 1;
} else if (iArgC == 2) {
if (strstr (pArgV[1], ".cfg")) { // read config file //confirmed_safe_unsafe_usage
CReadConfig cReadCfg (pArgV[1]);
string cfgFilePath;
#if defined(NODEFS) || defined(MEMFS)
cfgFilePath = string(CWD) + string(pArgV[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the string(CWD) + needed?

Copy link
Author

@Yuhengwe1 Yuhengwe1 Oct 10, 2022

Choose a reason for hiding this comment

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

This is for the Emscripten File System

node.js environment for example, we map the main project dir to /workload in virtual filesystem.

After the mount we can run h264enc.js like: node h264enc.js testbin/xxx.cfg

The string(CWD) + add the path prefix. Without it we may need to run h264enc.js like node h264enc.js workload/testbin/xxx.cfg

Copy link
Contributor

Choose a reason for hiding this comment

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

Why map the main project dir to /workload instead of just the /?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I tried to mount host dir ./ to / in virtual FS for nods.js env, but this will cause error.

In browser env, packaging all the main will lead to longer file reading time.

#else
cfgFilePath = string(pArgV[1]);
#endif
CReadConfig cReadCfg (cfgFilePath.c_str());
string strTag[4];
string strReconFile ("");

Expand Down Expand Up @@ -554,14 +578,23 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
}
} else if (strstr (pArgV[1],
".264")) { // no output dump yuv file, just try to render the decoded pictures //confirmed_safe_unsafe_usage
#if defined(NODEFS) || defined(MEMFS)
strInputFile = string(CWD) + string(pArgV[1]);
#else
strInputFile = pArgV[1];
#endif
sDecParam.uiTargetDqLayer = (uint8_t) - 1;
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
}
} else { //iArgC > 2
#if defined(NODEFS) || defined(MEMFS)
strInputFile = string(CWD) + string(pArgV[1]);
strOutputFile = string(CWD) + string(pArgV[2]);
#else
strInputFile = pArgV[1];
strOutputFile = pArgV[2];
#endif
sDecParam.uiTargetDqLayer = (uint8_t) - 1;
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
Expand All @@ -571,7 +604,11 @@ int32_t main (int32_t iArgC, char* pArgV[]) {

if (!strcmp (cmd, "-options")) {
if (i + 1 < iArgC)
#if defined(NODEFS) || defined(MEMFS)
strOptionFile = string(CWD) + string(pArgV[++i]);
#else
strOptionFile = pArgV[++i];
#endif
else {
printf ("options file not specified.\n");
return 1;
Expand All @@ -585,7 +622,11 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
}
} else if (!strcmp (cmd, "-length")) {
if (i + 1 < iArgC)
#if defined(NODEFS) || defined(MEMFS)
strLengthFile = string(CWD) + string(pArgV[++i]);
#else
strLengthFile = pArgV[++i];
#endif
else {
printf ("lenght file not specified.\n");
return 1;
Expand Down Expand Up @@ -655,4 +696,4 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
}

return 0;
}
}
8 changes: 8 additions & 0 deletions codec/console/dec/targets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ $(H264DEC_SRCDIR)/%.$(OBJ): $(H264DEC_SRCDIR)/%.cpp
h264dec$(EXEEXT): $(H264DEC_OBJS) $(H264DEC_DEPS)
$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(H264DEC_OBJS) $(H264DEC_LDFLAGS) $(LDFLAGS)

h264dec.html: $(H264DEC_OBJS) $(H264DEC_DEPS)
$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(H264DEC_OBJS) $(H264DEC_LDFLAGS) $(LDFLAGS)

ifeq ($(OS), wasm)
binaries: h264dec.html
BINARIES += h264dec.html
else
binaries: h264dec$(EXEEXT)
BINARIES += h264dec$(EXEEXT)
endif
Loading