Skip to content

Commit 87045f2

Browse files
committed
Initial Commit
0 parents  commit 87045f2

8 files changed

Lines changed: 852 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(LagrangeCodec)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3")
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
7+
set(CMAKE_C_COMPILE gcc)
8+
set(CMAKE_CXX_COMPILER g++)
9+
10+
include(ProcessorCount)
11+
include(FetchContent)
12+
13+
set(ENV{http_proxy} "http://127.0.0.1:7890")
14+
set(ENV{https_proxy} "http://127.0.0.1:7890")
15+
16+
# Download ffmpeg source code
17+
18+
FetchContent_Declare(
19+
ffmpeg_fetch
20+
GIT_REPOSITORY "https://github.com/FFmpeg/FFmpeg.git"
21+
GIT_TAG "n4.1"
22+
GIT_SHALLOW TRUE
23+
GIT_PROGRESS TRUE
24+
)
25+
26+
FetchContent_MakeAvailable(ffmpeg_fetch)
27+
message(STATUS "FFmpeg source code will be downloaded to: ${ffmpeg_fetch_SOURCE_DIR}")
28+
29+
# Download silk source code
30+
31+
FetchContent_Declare(
32+
silk_fetch
33+
GIT_REPOSITORY "https://github.com/ploverlake/silk.git"
34+
GIT_SHALLOW TRUE
35+
GIT_PROGRESS TRUE
36+
)
37+
38+
FetchContent_MakeAvailable(silk_fetch)
39+
message(STATUS "SILK source code will be downloaded to: ${silk_fetch_SOURCE_DIR}")
40+
41+
# Prepare FFmpeg build
42+
43+
find_program(MAKE_EXECUTABLE
44+
NAMES gmake make nmake
45+
DOC "make executable"
46+
REQUIRED
47+
)
48+
49+
find_package(PkgConfig)
50+
51+
if(PKG_CONFIG_FOUND)
52+
list(JOIN PKG_CONFIG_ARGN " " pkg_config_flags)
53+
set(pkgconfig_opts
54+
"--pkg-config=${PKG_CONFIG_EXECUTABLE}"
55+
"--pkg-config-flags=${pkg_config_flags}")
56+
else()
57+
unset(pkgconfig_opts)
58+
endif()
59+
60+
set(shared_opts
61+
--disable-shared
62+
--enable-static
63+
)
64+
65+
set (size_opts --enable-small)
66+
set (postfix_opts
67+
"--progs-suffix=${CMAKE_${BT}_POSTFIX}"
68+
"--build-suffix=${CMAKE_${BT}_POSTFIX}")
69+
set(pic_opts --enable-pic)
70+
71+
# Configure FFmpeg build
72+
73+
ProcessorCount(numCores)
74+
75+
set(ffmpeg_build_dir ${CMAKE_BINARY_DIR}/ffmpeg_build)
76+
set(ffmpeg_install_dir ${CMAKE_BINARY_DIR}/ffmpeg_install)
77+
file(MAKE_DIRECTORY ${ffmpeg_build_dir})
78+
file(MAKE_DIRECTORY ${ffmpeg_install_dir})
79+
80+
set(ffmpeg_configure_cmd
81+
${ffmpeg_fetch_SOURCE_DIR}/configure
82+
83+
--disable-programs
84+
85+
${shared_opts}
86+
${pkgconfig_opts}
87+
${size_opts}
88+
${pic_opts}
89+
${lto_opts}
90+
${postfix_opts}
91+
92+
"--strip=${CMAKE_STRIP}"
93+
"--extra-cflags=${CMAKE_C_FLAGS_${BT}}"
94+
"--optflags=${CMAKE_C_FLAGS_RELEASE}"
95+
"--cxx=${CMAKE_CXX_COMPILER}"
96+
"--extra-cxxflags=${CMAKE_CXX_FLAGS_${BT}}"
97+
"--objcc=${CMAKE_OBJC_COMPILER}"
98+
"--extra-objcflags=${CMAKE_OBJC_FLAGS_${BT}}"
99+
)
100+
101+
set(ffmpeg_make_cmd
102+
${MAKE_EXECUTABLE}
103+
-j${numCores}
104+
)
105+
106+
# Build FFmpeg
107+
108+
execute_process(
109+
COMMAND ${ffmpeg_configure_cmd}
110+
WORKING_DIRECTORY ${ffmpeg_build_dir}
111+
)
112+
113+
execute_process(
114+
COMMAND ${ffmpeg_make_cmd}
115+
WORKING_DIRECTORY ${ffmpeg_build_dir}
116+
)
117+
118+
include_directories(
119+
${ffmpeg_fetch_SOURCE_DIR}
120+
${ffmpeg_build_dir}
121+
)
122+
123+
# Including SILK
124+
125+
include_directories(
126+
${silk_fetch_SOURCE_DIR}/src/SILK_SDK_SRC_ARM_v1.0.9/interface/
127+
${silk_fetch_SOURCE_DIR}/src/SILK_SDK_SRC_ARM_v1.0.9/src/
128+
)
129+
130+
file(GLOB SILK_SRC
131+
${silk_fetch_SOURCE_DIR}/src/SILK_SDK_SRC_ARM_v1.0.9/src/*.c
132+
)
133+
134+
# Shared Library Definition
135+
136+
include_directories(
137+
${CMAKE_CURRENT_SOURCE_DIR}
138+
)
139+
140+
file(GLOB SOURCE_FILES "./src/*.cpp")
141+
142+
add_library(${PROJECT_NAME} SHARED
143+
${SOURCE_FILES}
144+
${SILK_SRC}
145+
)
146+
147+
target_link_libraries(${PROJECT_NAME}
148+
${ffmpeg_build_dir}/libavcodec/libavcodec.a
149+
${ffmpeg_build_dir}/libavformat/libavformat.a
150+
${ffmpeg_build_dir}/libavutil/libavutil.a
151+
${ffmpeg_build_dir}/libswresample/libswresample.a
152+
${ffmpeg_build_dir}/libswscale/libswscale.a
153+
)
154+
155+
if (APPLE)
156+
find_library(VIDEO_TOOLBOX VideoToolbox)
157+
find_library(CORE_FOUNDATION CoreFoundation)
158+
find_library(CORE_MEDIA CoreMedia)
159+
find_library(BZ2 bz2)
160+
find_library(CORE_VIDEO CoreVideo)
161+
find_library(ZLIB z)
162+
find_library(AUDIO_TOOLBOX AudioToolbox)
163+
find_library(ICONV iconv)
164+
165+
target_link_libraries(${PROJECT_NAME}
166+
${VIDEO_TOOLBOX}
167+
${CORE_FOUNDATION}
168+
${CORE_MEDIA}
169+
${BZ2}
170+
${CORE_VIDEO}
171+
${SECURITY}
172+
${ZLIB}
173+
${AUDIO_TOOLBOX}
174+
${ICONV}
175+
)
176+
elseif (WIN32)
177+
elseif (LINUX)
178+
endif ()

include/audio.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Created by Wenxuan Lin on 2025-02-23.
3+
//
4+
5+
6+
7+
#ifndef AUDIO_CPP_H
8+
#define AUDIO_CPP_H
9+
10+
#include "include/common.h"
11+
12+
constexpr int SILKV3_SAMPLE_RATE = 24000;
13+
14+
EXPORT int audio_to_pcm(uint8_t* audio_data, int data_len, cb_codec callback, void *userdata);
15+
16+
#endif //AUDIO_CPP_H

include/common.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Created by Wenxuan Lin on 2025-02-23.
3+
//
4+
5+
#ifndef COMMON_H
6+
#define COMMON_H
7+
8+
#include <cstdint>
9+
10+
extern "C" {
11+
#include <libavformat/avio.h>
12+
#include <libavformat/avformat.h>
13+
}
14+
15+
typedef void (cb_codec)(void* userdata, uint8_t* p, int len);
16+
17+
#ifdef _WIN32
18+
#define __dllexport __declspec(dllexport)
19+
#else
20+
#define __dllexport
21+
#endif
22+
23+
#define EXPORT extern "C" __dllexport
24+
25+
// REMEMBER TO FREE THE BUFFER AFTER USE BY av_free(format_context->pb->buffer);
26+
inline int create_format_context(uint8_t* data, int data_len, AVFormatContext** format_context) {
27+
AVIOContext* avio_ctx = nullptr; // Create a custom I/O context with the raw audio data buffer
28+
uint8_t* avio_buffer = nullptr;
29+
30+
avio_buffer = static_cast<uint8_t*>(av_malloc(data_len)); // Allocate buffer for AVIOContext
31+
if (!avio_buffer) {
32+
fprintf(stderr, "ERROR: failed to allocate memory for AVIOContext\n");
33+
return -1;
34+
}
35+
memcpy(avio_buffer, data, data_len);
36+
37+
// Allocate the AVIOContext with the custom buffer
38+
avio_ctx = avio_alloc_context(avio_buffer, data_len, 0, nullptr, nullptr, nullptr, nullptr);
39+
if (!avio_ctx) {
40+
fprintf(stderr, "ERROR: failed to create AVIOContext\n");
41+
return -1;
42+
}
43+
44+
// Create format context and set the I/O context
45+
*format_context = avformat_alloc_context();
46+
(*format_context)->pb = avio_ctx;
47+
48+
return 0;
49+
}
50+
51+
#endif //COMMON_H

include/silk.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by Wenxuan Lin on 2025-02-23.
3+
//
4+
5+
#ifndef SILK_H
6+
#define SILK_H
7+
8+
#include "include/common.h"
9+
10+
#include <SKP_Silk_typedef.h>
11+
12+
#define MAX_INPUT_FRAMES 5
13+
#define MAX_FRAME_LENGTH 480
14+
#define FRAME_LENGTH_MS 20
15+
#define MAX_API_FS_KHZ 48
16+
#define MAX_LBRR_DELAY 2
17+
#define MAX_BYTES_PER_FRAME 250 // Equals peak bitrate of 100 kbps
18+
19+
#ifdef _SYSTEM_IS_BIG_ENDIAN
20+
inline void swap_endian(SKP_int16 vec[], SKP_int len) {
21+
SKP_int16 tmp;
22+
23+
for (SKP_int i = 0; i < len; i++) {
24+
tmp = vec[i];
25+
SKP_uint8 *p1 = reinterpret_cast<unsigned char *>(&vec[i]);
26+
SKP_uint8 *p2 = reinterpret_cast<unsigned char *>(&tmp);
27+
p1[0] = p2[1]; p1[1] = p2[0];
28+
}
29+
}
30+
#endif
31+
32+
EXPORT int silk_decode(uint8_t* silk_data, int len, cb_codec callback, void* userdata);
33+
34+
EXPORT int silk_encode(uint8_t* pcm_data, int len, cb_codec callback, void* userdata);
35+
36+
#endif //SILK_H

include/video.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by Wenxuan Lin on 2025-02-23.
3+
//
4+
5+
#ifndef VIDEO_H
6+
#define VIDEO_H
7+
8+
#include "common.h"
9+
10+
#include <cstdint>
11+
12+
struct VideoInfo {
13+
int width;
14+
int height;
15+
int64_t duration;
16+
};
17+
18+
EXPORT int video_first_frame(uint8_t* video_data, int data_len, uint8_t*& out, int& out_len);
19+
20+
EXPORT int video_get_size(uint8_t* video_data, int data_len, VideoInfo& info);
21+
22+
#endif //VIDEO_H

0 commit comments

Comments
 (0)