소스 검색

Support cmake for ffmpeg-examples. (#151)

Winlin 2 년 전
부모
커밋
7ecf28c563
7개의 변경된 파일61개의 추가작업 그리고 3개의 파일을 삭제
  1. 1 1
      .github/workflows/linux.yaml
  2. 6 0
      .gitignore
  3. 6 0
      CMakeLists.txt
  4. 16 0
      ffmpeg-examples/CMakeLists.txt
  5. 7 2
      ffmpeg-examples/Makefile
  6. 20 0
      ffmpeg-examples/README.md
  7. 5 0
      ffmpeg-examples/sherpa-ncnn-ffmpeg.cc

+ 1 - 1
.github/workflows/linux.yaml

@@ -65,7 +65,7 @@ jobs:
         run: |
           mkdir build
           cd build
-          cmake -D CMAKE_BUILD_TYPE=Release ..
+          cmake -D CMAKE_BUILD_TYPE=Release -DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=ON ..
 
       - name: Build sherpa for ubuntu
         run: |

+ 6 - 0
.gitignore

@@ -110,3 +110,9 @@ encoder-scale-table.txt
 joiner-scale-table.txt
 *.tar.gz
 generate-int8-*.sh
+
+# For JetBrains IDE Clion.
+.idea
+cmake-build-release
+cmake-build-debug
+

+ 6 - 0
CMakeLists.txt

@@ -37,6 +37,7 @@ option(SHERPA_NCNN_ENABLE_BINARY "Whether to build the binary sherpa-ncnn" ON)
 option(SHERPA_NCNN_ENABLE_TEST "Whether to build tests" OFF)
 option(SHERPA_NCNN_ENABLE_C_API "Whether to build C API" ON)
 option(SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE "Whether to generate-int8-scale-table" ON)
+option(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES "Whether to enable ffmpeg-examples" OFF)
 
 if(DEFINED ANDROID_ABI)
   message(STATUS "Set SHERPA_NCNN_ENABLE_JNI to ON for Android")
@@ -57,6 +58,7 @@ message(STATUS "SHERPA_NCNN_ENABLE_BINARY ${SHERPA_NCNN_ENABLE_BINARY}")
 message(STATUS "SHERPA_NCNN_ENABLE_TEST ${SHERPA_NCNN_ENABLE_TEST}")
 message(STATUS "SHERPA_NCNN_ENABLE_C_API ${SHERPA_NCNN_ENABLE_C_API}")
 message(STATUS "SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE ${SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE}")
+message(STATUS "SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES ${SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES}")
 
 if(NOT CMAKE_BUILD_TYPE)
   message(STATUS "No CMAKE_BUILD_TYPE given, default to Release")
@@ -115,6 +117,10 @@ endif()
 
 add_subdirectory(sherpa-ncnn)
 
+if(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES)
+  add_subdirectory(ffmpeg-examples)
+endif()
+
 if(SHERPA_NCNN_ENABLE_C_API AND SHERPA_NCNN_ENABLE_BINARY)
   add_subdirectory(c-api-examples)
 endif()

+ 16 - 0
ffmpeg-examples/CMakeLists.txt

@@ -0,0 +1,16 @@
+
+include_directories(${CMAKE_SOURCE_DIR})
+add_executable(sherpa-ncnn-ffmpeg sherpa-ncnn-ffmpeg.cc)
+
+# Link libraries from sherpa-ncnn.
+target_link_libraries(sherpa-ncnn-ffmpeg sherpa-ncnn-c-api)
+
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(AVCODEC REQUIRED libavcodec)
+include_directories(${AVCODEC_INCLUDE_DIRS})
+target_link_directories(sherpa-ncnn-ffmpeg PRIVATE ${AVCODEC_LIBRARY_DIRS})
+target_link_libraries(sherpa-ncnn-ffmpeg ${AVCODEC_LIBRARIES})
+# All libraries of FFmpeg shares the same include and library directory.
+# Note that ${AVCODEC_LIBRARIES} equals to avcodec, but we add it for consistence.
+target_link_libraries(sherpa-ncnn-ffmpeg avcodec avformat avfilter avutil swresample swscale avdevice)
+

+ 7 - 2
ffmpeg-examples/Makefile

@@ -14,9 +14,14 @@ ifeq ($(GDB), TRUE)
 	OPTFLAG += -g
 endif
 
-CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS)) -I.. -Wall -std=c++11 -fopenmp ${OPTFLAG}
+CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS)) -I.. -Wall -std=c++11 ${OPTFLAG}
 LDLIBS := $(shell pkg-config --libs $(SHARED_LIBS)) -L../build/lib -lsherpa-ncnn-c-api -lsherpa-ncnn-core -lkaldi-native-fbank-core -lncnn -lportaudio -lpthread -lm
 
+# Set the flag if not macOS, or it fails with "DSO missing from command line".
+ifneq ($(shell uname -s), Darwin)
+  CFLAGS += -fopenmp
+endif
+
 #Get libavutil version and extract major, minor and micro
 LIBAVUTIL_VERSION := $(shell pkg-config --modversion libavutil)
 LIBAVUTIL_MAJOR := $(shell echo "$(LIBAVUTIL_VERSION)" | awk -F. '{print $$1}')
@@ -41,7 +46,7 @@ all: $(EXAMPLES)
 $(EXAMPLES): $(OBJS)
 	$(CC) $(addsuffix .o,$@) $(CFLAGS) $(LDLIBS) -o $@
 
-%.o : %.c
+%.o : %.cc
 	${CC} ${CFLAGS} -c -o $@ $<
 
 clean:

+ 20 - 0
ffmpeg-examples/README.md

@@ -1,3 +1,23 @@
+# ffmpeg-examples
+
+Enable sherpa-ncnn to use any url/file input that FFmpeg supports.
+
+## Usage
+
+This example is disabled by default, please enable it by:
+
+```bash
+cd sherpa-ncnn
+mkdir -p build
+cd build
+cmake -DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=ON ..
+make -j10
+```
+
+Please install ffmpeg first:
+
+* macOS: `brew install ffmpeg`
+
 # Fixes for errors
 
 To fix the following error:

+ 5 - 0
ffmpeg-examples/sherpa-ncnn-ffmpeg.c → ffmpeg-examples/sherpa-ncnn-ffmpeg.cc

@@ -55,14 +55,19 @@
  */
 
 #include <unistd.h>
+#ifdef __cplusplus
 extern "C" {
+#endif
+#include <libavutil/samplefmt.h>
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
 #include <libavutil/channel_layout.h>
 #include <libavutil/opt.h>
+#ifdef __cplusplus
 }
+#endif
 
 static const char *filter_descr = "aresample=16000,aformat=sample_fmts=s16:channel_layouts=mono";