Skip to content

Commit

Permalink
add H264 software encoder (bluenviron/mediamtx#2581) (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Aug 20, 2024
1 parent 5e5eb1c commit 3b15ba9
Show file tree
Hide file tree
Showing 11 changed files with 641 additions and 318 deletions.
54 changes: 41 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ all: \
$(OUT_DIR)/libcamera.so.9.9 \
$(OUT_DIR)/libcamera-base.so.9.9

folder:
mkdir -p $(OUT_DIR)

#################################################
# openssl

Expand Down Expand Up @@ -69,17 +66,21 @@ $(LIBCAMERA_TARGET): deps/libcamera $(OPENSSL_TARGET)
-Dudev=disabled \
&& ninja -C build install

$(OUT_DIR)/ipa_conf: folder $(LIBCAMERA_TARGET)
cp -r prefix/share/libcamera/ipa $@
$(OUT_DIR)/ipa_conf: $(LIBCAMERA_TARGET)
mkdir -p $(OUT_DIR)
cp -r $(PWD)/prefix/share/libcamera/ipa $@

$(OUT_DIR)/ipa_module: folder $(LIBCAMERA_TARGET)
cp -r prefix/lib/libcamera $@
$(OUT_DIR)/ipa_module: $(LIBCAMERA_TARGET)
mkdir -p $(OUT_DIR)
cp -r $(PWD)/prefix/lib/libcamera $@

$(OUT_DIR)/libcamera.so.9.9: folder $(LIBCAMERA_TARGET)
cp prefix/lib/libcamera.so.9.9 $@
$(OUT_DIR)/libcamera.so.9.9: $(LIBCAMERA_TARGET)
mkdir -p $(OUT_DIR)
cp $(PWD)/prefix/lib/libcamera.so.9.9 $@

$(OUT_DIR)/libcamera-base.so.9.9: folder $(LIBCAMERA_TARGET)
cp prefix/lib/libcamera-base.so.9.9 $@
$(OUT_DIR)/libcamera-base.so.9.9: $(LIBCAMERA_TARGET)
mkdir -p $(OUT_DIR)
cp $(PWD)/prefix/lib/libcamera-base.so.9.9 $@

#################################################
# libfreetype
Expand All @@ -106,6 +107,28 @@ $(FREETYPE_TARGET): deps/freetype
&& make -C build -j$$(nproc) \
&& make -C build install

#################################################
# x264

X264_REPO = https://code.videolan.org/videolan/x264
X264_COMMIT = 31e19f92f00c7003fa115047ce50978bc98c3a0d

X264_TARGET = prefix/lib/libx264.a

deps/x264:
git clone $(X264_REPO) $@
cd $@ && git checkout $(X264_COMMIT)

$(X264_TARGET): deps/x264
cd $< \
&& ./configure \
--prefix=$(PWD)/prefix \
--enable-static \
--enable-strip \
--disable-cli \
&& make -j$(nproc) \
&& make install

#################################################
# text font

Expand Down Expand Up @@ -133,7 +156,8 @@ CFLAGS = \
-Wextra \
-Wno-unused-parameter \
-Wno-unused-result \
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --cflags freetype2)
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --cflags freetype2) \
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --cflags x264)

CXXFLAGS = \
-Ofast \
Expand All @@ -148,13 +172,16 @@ CXXFLAGS = \
LDFLAGS = \
-s \
-pthread \
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --libs libcamera) \
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --libs freetype2) \
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --libs libcamera)
$$(PKG_CONFIG_PATH=$(PWD)/prefix/lib/pkgconfig pkg-config --libs x264)

OBJS = \
base64.o \
camera.o \
encoder.o \
encoder_hard_h264.o \
encoder_soft_h264.o \
main.o \
parameters.o \
pipe.o \
Expand All @@ -165,6 +192,7 @@ OBJS = \
DEPENDENCIES = \
$(LIBCAMERA_TARGET) \
$(FREETYPE_TARGET) \
$(X264_TARGET) \
$(TEXT_FONT_TARGET)

%.o: %.c $(DEPENDENCIES)
Expand Down
12 changes: 11 additions & 1 deletion camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ struct CameraPriv {
std::mutex ctrls_mutex;
std::unique_ptr<ControlList> ctrls;
std::map<FrameBuffer *, uint8_t *> mapped_buffers;
bool ts_initialized;
uint64_t ts_start;
};

static int get_v4l2_colorspace(std::optional<ColorSpace> const &cs) {
Expand Down Expand Up @@ -281,13 +283,21 @@ static void on_request_complete(Request *request) {

FrameBuffer *buffer = request->buffers().at(camp->video_stream);

uint64_t ts = buffer->metadata().timestamp / 1000;

if (!camp->ts_initialized) {
camp->ts_initialized = true;
camp->ts_start = ts;
}
ts -= camp->ts_start;

camp->frame_cb(
camp->mapped_buffers.at(buffer),
camp->video_stream->configuration().stride,
camp->video_stream->configuration().size.height,
buffer->planes()[0].fd.get(),
buffer_size(buffer->planes()),
buffer->metadata().timestamp / 1000);
ts);

request->reuse(Request::ReuseFlag::ReuseBuffers);

Expand Down
Loading

0 comments on commit 3b15ba9

Please sign in to comment.