Skip to content

Commit 1a36027

Browse files
Add support for decoding input with ffmpeg libs (Linux)
- search for ffmpeg at cmake time - include ffmpegs/libav headers Todo: - implement a function to convert non compliant wav to wav mono 16khz - if needed convert input buffer in main.cpp
1 parent 58210d6 commit 1a36027

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ option(WHISPER_BUILD_EXAMPLES "whisper: build examples" ${WHISPER_STANDA
5959

6060
option(WHISPER_SDL2 "whisper: support for libSDL2" OFF)
6161

62+
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
63+
option(WHISPER_FFMPEG "whisper: support building and linking with ffmpeg libs" OFF)
64+
endif()
65+
6266
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
6367
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
6468
option(WHISPER_NO_AVX512 "whisper: disable AVX512" ON)
@@ -125,6 +129,26 @@ else()
125129
set(CMAKE_CXX_STANDARD 11)
126130
endif()
127131

132+
if (WHISPER_FFMPEG)
133+
# As today (cmake 3.27), there is no official cmake support for FindFFmpeg. eg: not possible:
134+
# find_package(FFmpeg COMPONENTS AVCODEC AVFORMAT AVUTIL AVDEVICE REQUIRED)
135+
# Check with reviewers: worth to embed a FindFFmpeg.cmake script in whisper.cpp ?
136+
# eg https://github.com/snikulov/cmake-modules/blob/master/FindFFmpeg.cmake
137+
# Using PkgConfig atm:
138+
#find_package(PkgConfig REQUIRED)
139+
#pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET libavdevice libavfilter libavformat libavcodec libswresample libswscale libavutil)
140+
141+
find_package(FFmpeg)
142+
if (NOT ${FFMPEG_FOUND})
143+
message(FATAL_ERROR "Cannot find ffmpeg libs/headers")
144+
endif()
145+
message(STATUS "Found ffmpeg libs: ${FFMPEG_LIBRARIES}")
146+
message(STATUS "Found ffmpeg headers in: ${FFMPEG_INCLUDE_DIRS}")
147+
include_directories(${FFMPEG_INCLUDE_DIRS})
148+
add_compile_definitions(WHISPER_FFMPEG)
149+
set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${FFMPEG_LIBRARIES})
150+
endif()
151+
128152
# on APPLE
129153
if (APPLE)
130154
# include Accelerate framework

cmake/FindFFmpeg.cmake

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# vim: ts=2 sw=2
2+
# - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC)
3+
#
4+
# Once done this will define
5+
# FFMPEG_FOUND - System has the all required components.
6+
# FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers.
7+
# FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
8+
# FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components.
9+
#
10+
# For each of the components it will additionally set.
11+
# - AVCODEC
12+
# - AVDEVICE
13+
# - AVFORMAT
14+
# - AVFILTER
15+
# - AVUTIL
16+
# - POSTPROC
17+
# - SWSCALE
18+
# the following variables will be defined
19+
# <component>_FOUND - System has <component>
20+
# <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
21+
# <component>_LIBRARIES - Link these to use <component>
22+
# <component>_DEFINITIONS - Compiler switches required for using <component>
23+
# <component>_VERSION - The components version
24+
#
25+
# Copyright (c) 2006, Matthias Kretz, <[email protected]>
26+
# Copyright (c) 2008, Alexander Neundorf, <[email protected]>
27+
# Copyright (c) 2011, Michael Jansen, <[email protected]>
28+
#
29+
# Redistribution and use is allowed according to the terms of the BSD license.
30+
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
31+
32+
include(FindPackageHandleStandardArgs)
33+
34+
# The default components were taken from a survey over other FindFFMPEG.cmake files
35+
if (NOT FFmpeg_FIND_COMPONENTS)
36+
set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL)
37+
endif ()
38+
39+
#
40+
### Macro: set_component_found
41+
#
42+
# Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
43+
#
44+
macro(set_component_found _component )
45+
if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
46+
# message(STATUS " - ${_component} found.")
47+
set(${_component}_FOUND TRUE)
48+
else ()
49+
# message(STATUS " - ${_component} not found.")
50+
endif ()
51+
endmacro()
52+
53+
#
54+
### Macro: find_component
55+
#
56+
# Checks for the given component by invoking pkgconfig and then looking up the libraries and
57+
# include directories.
58+
#
59+
macro(find_component _component _pkgconfig _library _header)
60+
61+
if (NOT WIN32)
62+
# use pkg-config to get the directories and then use these values
63+
# in the FIND_PATH() and FIND_LIBRARY() calls
64+
find_package(PkgConfig)
65+
if (PKG_CONFIG_FOUND)
66+
pkg_check_modules(PC_${_component} ${_pkgconfig})
67+
message(STATUS "Pkgconfig found: ${PC_${_component}_INCLUDEDIR}")
68+
message(STATUS "Pkgconfig found: ${PC_${_component}_INCLUDE_DIRS}")
69+
endif ()
70+
endif (NOT WIN32)
71+
72+
73+
find_path(${_component}_INCLUDE_DIRS ${_header}
74+
HINTS
75+
${PC_${_component}_INCLUDEDIR}
76+
${PC_${_component}_INCLUDE_DIRS}
77+
PATH_SUFFIXES
78+
ffmpeg
79+
)
80+
81+
find_library(${_component}_LIBRARIES NAMES ${_library}
82+
HINTS
83+
${PC_${_component}_LIBDIR}
84+
${PC_${_component}_LIBRARY_DIRS}
85+
)
86+
87+
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
88+
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
89+
90+
set_component_found(${_component})
91+
92+
mark_as_advanced(
93+
${_component}_INCLUDE_DIRS
94+
${_component}_LIBRARIES
95+
${_component}_DEFINITIONS
96+
${_component}_VERSION)
97+
98+
endmacro()
99+
100+
101+
# Check for cached results. If there are skip the costly part.
102+
if (NOT FFMPEG_LIBRARIES)
103+
104+
# Check for all possible component.
105+
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
106+
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
107+
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
108+
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
109+
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
110+
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
111+
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
112+
find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
113+
114+
# Check if the required components were found and add their stuff to the FFMPEG_* vars.
115+
foreach (_component ${FFmpeg_FIND_COMPONENTS})
116+
if (${_component}_FOUND)
117+
# message(STATUS "Required component ${_component} present.")
118+
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARIES})
119+
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
120+
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
121+
else ()
122+
# message(STATUS "Required component ${_component} missing.")
123+
endif ()
124+
endforeach ()
125+
126+
# Build the include path with duplicates removed.
127+
if (FFMPEG_INCLUDE_DIRS)
128+
list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
129+
endif ()
130+
131+
# cache the vars.
132+
set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
133+
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
134+
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
135+
136+
mark_as_advanced(FFMPEG_INCLUDE_DIRS
137+
FFMPEG_LIBRARIES
138+
FFMPEG_DEFINITIONS)
139+
140+
endif ()
141+
142+
# Now set the noncached _FOUND vars for the components.
143+
foreach (_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWSCALE)
144+
set_component_found(${_component})
145+
endforeach ()
146+
147+
# Compile the list of required vars
148+
set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
149+
foreach (_component ${FFmpeg_FIND_COMPONENTS})
150+
list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
151+
endforeach ()
152+
153+
# Give a nice error message if some of the required vars are missing.
154+
find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})

examples/main/main.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include "whisper.h"
44
#include "grammar-parser.h"
55

6+
#ifdef WHISPER_FFMPEG
7+
#include <libavutil/frame.h>
8+
#include <libavutil/mem.h>
9+
#include <libavcodec/avcodec.h>
10+
#endif
11+
612
#include <cmath>
713
#include <fstream>
814
#include <cstdio>

0 commit comments

Comments
 (0)