Skip to content

Commit 91b8324

Browse files
committed
add wasm build support
1 parent 0c19a5c commit 91b8324

14 files changed

+480
-13
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ testbin/test_vd_rc.264
4646
testbin/test_vd_rc.yuv
4747
testbin/test.264
4848
testbin/test.yuv
49+
testbin/Static.264
50+
testbin/Static.yuv
4951

5052
# iOS output files
5153
codec/build/iOS/common/build/
@@ -57,3 +59,11 @@ codec/build/iOS/dec/welsdec/build/
5759
# editor files
5860
*~
5961

62+
#wasm
63+
.vscode/*
64+
*.html
65+
*.js
66+
*.wat
67+
*.wasm
68+
*.data
69+

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ OS=$(shell uname | tr A-Z a-z | tr -d \\-0-9. | sed -E 's/^(net|open|free)bsd/bs
1111
ARCH=$(shell uname -m)
1212
LIBPREFIX=lib
1313
LIBSUFFIX=a
14+
# EMFS = nodefs | memfs
15+
EMFS=
1416
CCAS=$(CC)
1517
CXX_O=-o $@
1618
CXX_LINK_O=-o $@
@@ -170,7 +172,7 @@ clean:
170172
ifeq (android,$(OS))
171173
clean: clean_Android
172174
endif
173-
$(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
175+
$(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
174176

175177
gmp-bootstrap:
176178
if [ ! -d gmp-api ] ; then git clone https://github.com/mozilla/gmp-api gmp-api ; fi
@@ -382,4 +384,4 @@ OBJDIRS = $(sort $(dir $(OBJS)))
382384
$(OBJDIRS):
383385
$(QUIET)mkdir -p $@
384386

385-
$(OBJS): | $(OBJDIRS)
387+
$(OBJS): | $(OBJDIRS)

build/mktargets.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,16 @@ def find_sources():
268268
if args.binary is not None:
269269
f.write("%s$(EXEEXT): $(%s_OBJS) $(%s_DEPS)\n"%(args.binary, PREFIX, PREFIX))
270270
f.write("\t$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(%s_OBJS) $(%s_LDFLAGS) $(LDFLAGS)\n\n"%(PREFIX, PREFIX))
271+
# for wasm build
272+
f.write("%s.html: $(%s_OBJS) $(%s_DEPS)\n"%(args.binary, PREFIX, PREFIX))
273+
f.write("\t$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(%s_OBJS) $(%s_LDFLAGS) $(LDFLAGS)\n\n"%(PREFIX, PREFIX))
274+
275+
f.write("ifeq ($(OS), wasm)\n")
276+
f.write("binaries: %s.html\n"%args.binary)
277+
f.write("BINARIES += %s.html\n"%args.binary)
278+
f.write("else\n")
271279
f.write("binaries: %s$(EXEEXT)\n"%args.binary)
272280
f.write("BINARIES += %s$(EXEEXT)\n"%args.binary)
281+
f.write("endif")
273282

274-
f.close()
283+
f.close()

build/platform-wasm.mk

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
include $(SRC_PATH)build/arch.mk
2+
SHAREDLIBSUFFIX = so
3+
SHAREDLIBSUFFIXFULLVER=$(SHAREDLIBSUFFIX).$(FULL_VERSION)
4+
SHAREDLIBSUFFIXMAJORVER=$(SHAREDLIBSUFFIX).$(SHAREDLIB_MAJORVERSION)
5+
SHLDFLAGS = -Wl,-soname,$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXMAJORVER)
6+
CFLAGS += -Wall -fno-strict-aliasing -fPIC -MMD -MP
7+
# fix undifined symbol: __stack_chk_guard bug
8+
# flags needed to build wasm
9+
CFLAGS += -U_FORTIFY_SOURCE -pthread -DWASMSIMD -msimd128
10+
CXXFLAGS += -U_FORTIFY_SOURCE -pthread -DWASMSIMD -msimd128
11+
LDFLAGS += -U_FORTIFY_SOURCE -pthread -msimd128
12+
ifeq ($(EMFS), nodefs)
13+
CFLAGS += -DNODEFS
14+
CXXFLAGS += -DNODEFS
15+
LDFLAGS += -lnodefs.js
16+
endif
17+
ifeq ($(EMFS), memfs)
18+
CFLAGS += -DMEMFS
19+
CXXFLAGS += -DMEMFS
20+
LDFLAGS += --preload-file ./testbin/workload
21+
endif
22+
ifeq ($(WASMDEBUG), True)
23+
CFLAGS += -g2
24+
CXXFLAGS += -g2
25+
LDFLAGS += -g2
26+
endif
27+
28+
STATIC_LDFLAGS += -lm
29+
AR_OPTS = crD $@

codec/console/dec/src/h264dec.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
#if defined (ANDROID_NDK)
4545
#include <android/log.h>
4646
#endif
47+
#ifdef __EMSCRIPTEN__
48+
#include <emscripten.h>
49+
#endif
50+
4751
#include "codec_def.h"
4852
#include "codec_app_def.h"
4953
#include "codec_api.h"
@@ -60,6 +64,13 @@ float g_fDecFPS = 0.0;
6064
int g_iDecodedFrameNum = 0;
6165
#endif
6266

67+
#if defined(NODEFS)
68+
#define CWD "/workload/"
69+
#endif
70+
#if defined(MEMFS)
71+
#define CWD "testbin/workload/"
72+
#endif
73+
6374
#if defined(ANDROID_NDK)
6475
#define LOG_TAG "welsdec"
6576
#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
@@ -256,7 +267,7 @@ void H264DecodeInstance (ISVCDecoder* pDecoder, const char* kpH264FileName, cons
256267
if (kpH264FileName) {
257268
pH264File = fopen (kpH264FileName, "rb");
258269
if (pH264File == NULL) {
259-
fprintf (stderr, "Can not open h264 source file, check its legal path related please..\n");
270+
fprintf (stderr, "Can not open h264 source file: %s, check its legal path related please..\n", kpH264FileName);
260271
return;
261272
}
262273
fprintf (stderr, "H264 source file name: %s..\n", kpH264FileName);
@@ -505,14 +516,27 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
505516
sDecParam.sVideoProperty.size = sizeof (sDecParam.sVideoProperty);
506517
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_MV_COPY_CROSS_IDR_FREEZE_RES_CHANGE;
507518

519+
#if defined(NODEFS)
520+
EM_ASM(
521+
FS.mkdir('/workload');
522+
FS.mount(NODEFS, { root: '.' }, '/workload');
523+
);
524+
#endif
525+
508526
if (iArgC < 2) {
509527
printf ("usage 1: h264dec.exe welsdec.cfg\n");
510528
printf ("usage 2: h264dec.exe welsdec.264 out.yuv\n");
511529
printf ("usage 3: h264dec.exe welsdec.264\n");
512530
return 1;
513531
} else if (iArgC == 2) {
514532
if (strstr (pArgV[1], ".cfg")) { // read config file //confirmed_safe_unsafe_usage
515-
CReadConfig cReadCfg (pArgV[1]);
533+
string cfgFilePath;
534+
#if defined(NODEFS) || defined(MEMFS)
535+
cfgFilePath = string(CWD) + string(pArgV[1]);
536+
#else
537+
cfgFilePath = string(pArgV[1]);
538+
#endif
539+
CReadConfig cReadCfg (cfgFilePath.c_str());
516540
string strTag[4];
517541
string strReconFile ("");
518542

@@ -554,14 +578,23 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
554578
}
555579
} else if (strstr (pArgV[1],
556580
".264")) { // no output dump yuv file, just try to render the decoded pictures //confirmed_safe_unsafe_usage
581+
#if defined(NODEFS) || defined(MEMFS)
582+
strInputFile = string(CWD) + string(pArgV[1]);
583+
#else
557584
strInputFile = pArgV[1];
585+
#endif
558586
sDecParam.uiTargetDqLayer = (uint8_t) - 1;
559587
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
560588
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
561589
}
562590
} else { //iArgC > 2
591+
#if defined(NODEFS) || defined(MEMFS)
592+
strInputFile = string(CWD) + string(pArgV[1]);
593+
strOutputFile = string(CWD) + string(pArgV[2]);
594+
#else
563595
strInputFile = pArgV[1];
564596
strOutputFile = pArgV[2];
597+
#endif
565598
sDecParam.uiTargetDqLayer = (uint8_t) - 1;
566599
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
567600
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
@@ -571,7 +604,11 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
571604

572605
if (!strcmp (cmd, "-options")) {
573606
if (i + 1 < iArgC)
607+
#if defined(NODEFS) || defined(MEMFS)
608+
strOptionFile = string(CWD) + string(pArgV[++i]);
609+
#else
574610
strOptionFile = pArgV[++i];
611+
#endif
575612
else {
576613
printf ("options file not specified.\n");
577614
return 1;
@@ -585,7 +622,11 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
585622
}
586623
} else if (!strcmp (cmd, "-length")) {
587624
if (i + 1 < iArgC)
625+
#if defined(NODEFS) || defined(MEMFS)
626+
strLengthFile = string(CWD) + string(pArgV[++i]);
627+
#else
588628
strLengthFile = pArgV[++i];
629+
#endif
589630
else {
590631
printf ("lenght file not specified.\n");
591632
return 1;
@@ -655,4 +696,4 @@ int32_t main (int32_t iArgC, char* pArgV[]) {
655696
}
656697

657698
return 0;
658-
}
699+
}

codec/console/dec/targets.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@ $(H264DEC_SRCDIR)/%.$(OBJ): $(H264DEC_SRCDIR)/%.cpp
1616
h264dec$(EXEEXT): $(H264DEC_OBJS) $(H264DEC_DEPS)
1717
$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(H264DEC_OBJS) $(H264DEC_LDFLAGS) $(LDFLAGS)
1818

19+
h264dec.html: $(H264DEC_OBJS) $(H264DEC_DEPS)
20+
$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(H264DEC_OBJS) $(H264DEC_LDFLAGS) $(LDFLAGS)
21+
22+
ifeq ($(OS), wasm)
23+
binaries: h264dec.html
24+
BINARIES += h264dec.html
25+
else
1926
binaries: h264dec$(EXEEXT)
2027
BINARIES += h264dec$(EXEEXT)
28+
endif

codec/console/enc/src/welsenc.cpp

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#include <assert.h>
3737
#include <signal.h>
3838
#include <stdarg.h>
39+
#ifdef __EMSCRIPTEN__
40+
#include <emscripten.h>
41+
#endif
3942
#if defined (ANDROID_NDK)
4043
#include <android/log.h>
4144
#endif
@@ -88,6 +91,13 @@ int g_iEncodedFrame = 0;
8891
#endif
8992
#endif /* _WIN32 */
9093

94+
#if defined(NODEFS)
95+
#define CWD "/workload/"
96+
#endif
97+
#if defined(MEMFS)
98+
#define CWD "testbin/workload/"
99+
#endif
100+
91101
#if defined(__linux__) || defined(__unix__)
92102
#define _FILE_OFFSET_BITS 64
93103
#endif
@@ -238,10 +248,22 @@ int ParseConfig (CReadConfig& cRdCfg, SSourcePicture* pSrcPic, SEncParamExt& pSv
238248
} else if (strTag[0].compare ("SourceHeight") == 0) {
239249
pSrcPic->iPicHeight = atoi (strTag[1].c_str());
240250
} else if (strTag[0].compare ("InputFile") == 0) {
251+
string inputFile;
252+
#if defined(NODEFS) || defined(MEMFS)
253+
inputFile = string(CWD) + strTag[1];
254+
#else
255+
inputFile = strTag[1];
256+
#endif
241257
if (strTag[1].length() > 0)
242-
sFileSet.strSeqFile = strTag[1];
258+
sFileSet.strSeqFile = inputFile;
243259
} else if (strTag[0].compare ("OutputFile") == 0) {
244-
sFileSet.strBsFile = strTag[1];
260+
string outputFile;
261+
#if defined(NODEFS) || defined(MEMFS)
262+
outputFile = string(CWD) + strTag[1];
263+
#else
264+
outputFile = strTag[1];
265+
#endif
266+
sFileSet.strBsFile = outputFile;
245267
} else if (strTag[0].compare ("MaxFrameRate") == 0) {
246268
pSvcParam.fMaxFrameRate = (float)atof (strTag[1].c_str());
247269
} else if (strTag[0].compare ("FramesToBeEncoded") == 0) {
@@ -356,10 +378,17 @@ int ParseConfig (CReadConfig& cRdCfg, SSourcePicture* pSrcPic, SEncParamExt& pSv
356378
break;
357379
}
358380
} else if (strTag[0].compare ("LayerCfg") == 0) {
359-
if (strTag[1].length() > 0)
360-
sFileSet.strLayerCfgFile[iLayerCount] = strTag[1];
381+
if (strTag[1].length() > 0) {
382+
string cfgFilePath;
383+
#if defined(NODEFS) || defined(MEMFS)
384+
cfgFilePath = string(CWD) + strTag[1];
385+
#else
386+
cfgFilePath = strTag[1];
387+
#endif
388+
sFileSet.strLayerCfgFile[iLayerCount] = cfgFilePath;
389+
++iLayerCount;
390+
}
361391
// pSvcParam.sDependencyLayers[iLayerCount].uiDependencyId = iLayerCount;
362-
++ iLayerCount;
363392
} else if (strTag[0].compare ("PrefixNALAddingCtrl") == 0) {
364393
int ctrl_flag = atoi (strTag[1].c_str());
365394
if (ctrl_flag > 1)
@@ -801,7 +830,13 @@ int ProcessEncoding (ISVCEncoder* pPtrEnc, int argc, char** argv, bool bConfigFi
801830
// if configure file exit, reading configure file firstly
802831
if (bConfigFile) {
803832
iParsedNum = 2;
804-
cRdCfg.Openf (argv[1]);
833+
string filePath;
834+
#if defined(NODEFS) || defined(MEMFS)
835+
filePath = string(CWD) + string(argv[1]);
836+
#else
837+
filePath = string(argv[1]);
838+
#endif
839+
cRdCfg.Openf (filePath.c_str());
805840
if (!cRdCfg.ExistFile()) {
806841
fprintf (stderr, "Specified file: %s not exist, maybe invalid path or parameter settting.\n",
807842
cRdCfg.GetFileName().c_str());
@@ -1133,6 +1168,13 @@ int main (int argc, char** argv)
11331168
/* Control-C handler */
11341169
signal (SIGINT, SigIntHandler);
11351170

1171+
#if defined(NODEFS)
1172+
EM_ASM(
1173+
FS.mkdir('/workload');
1174+
FS.mount(NODEFS, { root: '.' }, '/workload');
1175+
);
1176+
#endif
1177+
11361178
iRet = CreateSVCEncHandle (&pSVCEncoder);
11371179
if (iRet) {
11381180
cout << "WelsCreateSVCEncoder() failed!!" << endl;
@@ -1167,4 +1209,4 @@ int main (int argc, char** argv)
11671209
DestroySVCEncHandle (pSVCEncoder);
11681210
PrintHelp();
11691211
return 1;
1170-
}
1212+
}

codec/console/enc/targets.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ $(H264ENC_SRCDIR)/%.$(OBJ): $(H264ENC_SRCDIR)/%.cpp
1515
h264enc$(EXEEXT): $(H264ENC_OBJS) $(H264ENC_DEPS)
1616
$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(H264ENC_OBJS) $(H264ENC_LDFLAGS) $(LDFLAGS)
1717

18+
h264enc.html: $(H264ENC_OBJS) $(H264ENC_DEPS)
19+
$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(H264ENC_OBJS) $(H264ENC_LDFLAGS) $(LDFLAGS)
20+
21+
ifeq ($(OS), wasm)
22+
binaries: h264enc.html
23+
BINARIES += h264enc.html
24+
else
1825
binaries: h264enc$(EXEEXT)
1926
BINARIES += h264enc$(EXEEXT)
27+
endif

0 commit comments

Comments
 (0)