Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable hwaccel h264 encoding on raspberry pi #998

Merged
merged 18 commits into from
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions board/raspberrypi/overlay/opt/vc/lib
1 change: 1 addition & 0 deletions board/raspberrypi2/overlay/opt/vc/lib
1 change: 1 addition & 0 deletions board/raspberrypi3/overlay/opt/vc/lib
1 change: 1 addition & 0 deletions configs/raspberrypi2_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ BR2_PACKAGE_FFMPEG=y
BR2_PACKAGE_FFMPEG_GPL=y
BR2_PACKAGE_FFMPEG_NONFREE=y
BR2_PACKAGE_FFMPEG_SWSCALE=y
BR2_PACKAGE_FFMPEG_RPI_HW_CODECS=y
BR2_PACKAGE_LIBWEBCAM=y
BR2_PACKAGE_MOTION=y
BR2_PACKAGE_STREAMEYE=y
Expand Down
1 change: 1 addition & 0 deletions configs/raspberrypi3_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ BR2_PACKAGE_FFMPEG=y
BR2_PACKAGE_FFMPEG_GPL=y
BR2_PACKAGE_FFMPEG_NONFREE=y
BR2_PACKAGE_FFMPEG_SWSCALE=y
BR2_PACKAGE_FFMPEG_RPI_HW_CODECS=y
BR2_PACKAGE_LIBWEBCAM=y
BR2_PACKAGE_MOTION=y
BR2_PACKAGE_STREAMEYE=y
Expand Down
1 change: 1 addition & 0 deletions configs/raspberrypi_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ BR2_PACKAGE_FFMPEG=y
BR2_PACKAGE_FFMPEG_GPL=y
BR2_PACKAGE_FFMPEG_NONFREE=y
BR2_PACKAGE_FFMPEG_SWSCALE=y
BR2_PACKAGE_FFMPEG_RPI_HW_CODECS=y
BR2_PACKAGE_LIBWEBCAM=y
BR2_PACKAGE_MOTION=y
BR2_PACKAGE_STREAMEYE=y
Expand Down
6 changes: 6 additions & 0 deletions package/ffmpeg/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ config BR2_PACKAGE_FFMPEG_OUTDEVS
bool "Enable output devices"
default y

config BR2_PACKAGE_FFMPEG_RPI_HW_CODECS
bool "Enable rpi hardware accelerated codecs"
depends on BR2_PACKAGE_RPI_USERLAND
help
Enable HW accelerated codecs on Raspberry pi.

config BR2_PACKAGE_FFMPEG_EXTRACONF
string "Additional parameters for ./configure"
default ""
Expand Down
15 changes: 15 additions & 0 deletions package/ffmpeg/disable-rpi-omx-input-zerocopy.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/libavcodec/omx.c b/libavcodec/omx.c
index 19b4f33836..4641dc79e2 100644
--- a/libavcodec/omx.c
+++ b/libavcodec/omx.c
@@ -644,10 +644,6 @@ static av_cold int omx_encode_init(AVCodecContext *avctx)
OMX_BUFFERHEADERTYPE *buffer;
OMX_ERRORTYPE err;

-#if CONFIG_OMX_RPI
- s->input_zerocopy = 1;
-#endif
-
s->omx_context = omx_init(avctx, s->libname, s->libprefix);
if (!s->omx_context)
return AVERROR_ENCODER_NOT_FOUND;
1 change: 1 addition & 0 deletions package/ffmpeg/ffmpeg.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Locally calculated
sha256 54ce502aca10b7e6059f19220ea2f68fa0c9c4c4d255ae13e615f08f0c94dcc5 ffmpeg-3.2.3.tar.xz
sha256 1998de1ab32616cbf2ff86efc3f1f26e76805ec5dc51e24c041c79edd8262785 ffmpeg-3.3.2.tar.xz
9 changes: 6 additions & 3 deletions package/ffmpeg/ffmpeg.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
################################################################################

FFMPEG_VERSION = 3.2.3
FFMPEG_VERSION = 3.3.2
FFMPEG_SOURCE = ffmpeg-$(FFMPEG_VERSION).tar.xz
FFMPEG_SITE = http://ffmpeg.org/releases
FFMPEG_INSTALL_STAGING = YES
Expand All @@ -26,7 +26,6 @@ FFMPEG_CONF_OPTS = \
--enable-avdevice \
--enable-avcodec \
--enable-avformat \
--disable-x11grab \
--enable-network \
--disable-gray \
--enable-swscale-alpha \
Expand All @@ -39,7 +38,6 @@ FFMPEG_CONF_OPTS = \
--disable-dxva2 \
--enable-runtime-cpudetect \
--disable-hardcoded-tables \
--disable-memalign-hack \
--disable-mipsdsp \
--disable-mipsdspr2 \
--disable-msa \
Expand Down Expand Up @@ -473,6 +471,11 @@ else ifneq ($(call qstrip,$(BR2_GCC_TARGET_ARCH)),)
FFMPEG_CONF_OPTS += --cpu=$(BR2_GCC_TARGET_ARCH)
endif

ifeq ($(BR2_PACKAGE_FFMPEG_RPI_HW_CODECS),y)
FFMPEG_DEPENDENCIES += rpi-userland
FFMPEG_CONF_OPTS += --enable-omx --enable-omx-rpi --enable-mmal --extra-cflags=-I../../staging/usr/include/IL/
endif


FFMPEG_CONF_OPTS += $(call qstrip,$(BR2_PACKAGE_FFMPEG_EXTRACONF))

Expand Down
2 changes: 1 addition & 1 deletion package/motion/motion.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
################################################################################

MOTION_VERSION = 37b3595
MOTION_VERSION = ab9e800d5984f2907f00bebabc794d1dba9682ad
MOTION_SITE = $(call github,motion-project,motion,$(MOTION_VERSION))
MOTION_AUTORECONF = YES
MOTION_CONF_OPTS = --without-pgsql --without-sdl --without-sqlite3 --without-mysql --with-ffmpeg=$(STAGING_DIR)/usr/lib \
Expand Down
145 changes: 145 additions & 0 deletions package/motion/preferred-encoder.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
diff --git a/ffmpeg.c b/ffmpeg.c
index 87b4f75..3e7787c 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -236,10 +236,21 @@ static void ffmpeg_free_context(struct ffmpeg *ffmpeg){

static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg){

+ size_t codec_name_len = strcspn(ffmpeg->codec_name, ":");
+ char *codec_name = alloca(codec_name_len + 1);
+
+ if (codec_name == NULL) {
+ MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Failed to allocate memory for codec name");
+ ffmpeg_free_context(ffmpeg);
+ return -1;
+ }
+ memcpy(codec_name, ffmpeg->codec_name, codec_name_len);
+ codec_name[codec_name_len] = 0;
+
/* Only the newer codec and containers can handle the really fast FPS */
- if (((strcmp(ffmpeg->codec_name, "msmpeg4") == 0) ||
- (strcmp(ffmpeg->codec_name, "mpeg4") == 0) ||
- (strcmp(ffmpeg->codec_name, "swf") == 0) ) && (ffmpeg->fps >50)){
+ if (((strcmp(codec_name, "msmpeg4") == 0) ||
+ (strcmp(codec_name, "mpeg4") == 0) ||
+ (strcmp(codec_name, "swf") == 0) ) && (ffmpeg->fps >50)){
MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "The frame rate specified is too high for the ffmpeg movie type specified. Choose a different ffmpeg container or lower framerate.");
ffmpeg_free_context(ffmpeg);
return -1;
@@ -250,59 +261,59 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg){
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_MPEG2VIDEO;
strncat(ffmpeg->filename, ".mpg", 4);
if (!ffmpeg->oc->oformat) {
- MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "ffmpeg_video_codec option value %s is not supported", ffmpeg->codec_name);
+ MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "ffmpeg_video_codec option value %s is not supported", codec_name);
ffmpeg_free_context(ffmpeg);
return -1;
}
return 0;
}

- if (strcmp(ffmpeg->codec_name, "mpeg4") == 0) {
+ if (strcmp(codec_name, "mpeg4") == 0) {
ffmpeg->oc->oformat = av_guess_format("avi", NULL, NULL);
strncat(ffmpeg->filename, ".avi", 4);
}

- if (strcmp(ffmpeg->codec_name, "msmpeg4") == 0) {
+ if (strcmp(codec_name, "msmpeg4") == 0) {
ffmpeg->oc->oformat = av_guess_format("avi", NULL, NULL);
strncat(ffmpeg->filename, ".avi", 4);
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_MSMPEG4V2;
}

- if (strcmp(ffmpeg->codec_name, "swf") == 0) {
+ if (strcmp(codec_name, "swf") == 0) {
ffmpeg->oc->oformat = av_guess_format("swf", NULL, NULL);
strncat(ffmpeg->filename, ".swf", 4);
}

- if (strcmp(ffmpeg->codec_name, "flv") == 0) {
+ if (strcmp(codec_name, "flv") == 0) {
ffmpeg->oc->oformat = av_guess_format("flv", NULL, NULL);
strncat(ffmpeg->filename, ".flv", 4);
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_FLV1;
}

- if (strcmp(ffmpeg->codec_name, "ffv1") == 0) {
+ if (strcmp(codec_name, "ffv1") == 0) {
ffmpeg->oc->oformat = av_guess_format("avi", NULL, NULL);
strncat(ffmpeg->filename, ".avi", 4);
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_FFV1;
}

- if (strcmp(ffmpeg->codec_name, "mov") == 0) {
+ if (strcmp(codec_name, "mov") == 0) {
ffmpeg->oc->oformat = av_guess_format("mov", NULL, NULL);
strncat(ffmpeg->filename, ".mov", 4);
}

- if (strcmp(ffmpeg->codec_name, "mp4") == 0) {
+ if (strcmp(codec_name, "mp4") == 0) {
ffmpeg->oc->oformat = av_guess_format("mp4", NULL, NULL);
strncat(ffmpeg->filename, ".mp4", 4);
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_H264;
}

- if (strcmp(ffmpeg->codec_name, "mkv") == 0) {
+ if (strcmp(codec_name, "mkv") == 0) {
ffmpeg->oc->oformat = av_guess_format("matroska", NULL, NULL);
strncat(ffmpeg->filename, ".mkv", 4);
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_H264;
}

- if (strcmp(ffmpeg->codec_name, "hevc") == 0) {
+ if (strcmp(codec_name, "hevc") == 0) {
ffmpeg->oc->oformat = av_guess_format("mp4", NULL, NULL);
strncat(ffmpeg->filename, ".mp4", 4);
if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_HEVC;
@@ -310,7 +321,7 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg){

//Check for valid results
if (!ffmpeg->oc->oformat) {
- MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "codec option value %s is not supported", ffmpeg->codec_name);
+ MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "codec option value %s is not supported", codec_name);
ffmpeg_free_context(ffmpeg);
return -1;
}
@@ -484,13 +495,23 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg){
int retcd;
char errstr[128];
int chkrate;
+ size_t codec_name_len = strcspn(ffmpeg->codec_name, ":");

- ffmpeg->codec = avcodec_find_encoder(ffmpeg->oc->oformat->video_codec);
+ ffmpeg->codec = NULL;
+ if (ffmpeg->codec_name[codec_name_len]) {
+ ffmpeg->codec = avcodec_find_encoder_by_name(&ffmpeg->codec_name[codec_name_len+1]);
+ if (!ffmpeg->codec)
+ MOTION_LOG(WRN, TYPE_ENCODER, NO_ERRNO, "Preferred codec %s not found", &ffmpeg->codec_name[codec_name_len+1]);
+ }
+ if (!ffmpeg->codec)
+ ffmpeg->codec = avcodec_find_encoder(ffmpeg->oc->oformat->video_codec);
if (!ffmpeg->codec) {
MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Codec %s not found", ffmpeg->codec_name);
ffmpeg_free_context(ffmpeg);
return -1;
}
+ if (ffmpeg->codec_name[codec_name_len])
+ MOTION_LOG(NTC, TYPE_ENCODER, NO_ERRNO, "Using codec %s", ffmpeg->codec->name);

#if (LIBAVFORMAT_VERSION_MAJOR >= 58) || ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR >= 41))
//If we provide the codec to this, it results in a memory leak. ffmpeg ticket: 5714
diff --git a/ffmpeg.h b/ffmpeg.h
index 78a2785..95383a7 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -5,6 +5,7 @@
#include <stdarg.h>
#include <sys/time.h>
#include <stdint.h>
+#include <alloca.h>
#include "config.h"

enum TIMELAPSE_TYPE {
17 changes: 17 additions & 0 deletions package/motion/temp-fix-for-extpipe-subdir-creation.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/event.c b/event.c
index f67214e..44d8096 100644
--- a/event.c
+++ b/event.c
@@ -572,6 +572,12 @@ static void event_create_extpipe(struct context *cnt,
}
}

+ /* Temporary solution for https://github.com/Motion-Project/motion/issues/360#issuecomment-320849158
+ Always create any path specified as file name */
+ if (create_path(cnt->extpipefilename) == -1)
+ return ;
+ /* end of fix */
+
mystrftime(cnt, stamp, sizeof(stamp), cnt->conf.extpipe, currenttime_tv, cnt->extpipefilename, 0);

MOTION_LOG(NTC, TYPE_EVENTS, NO_ERRNO, "pipe: %s", stamp);
42 changes: 42 additions & 0 deletions package/motion/tune-h264-encode-quality.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
diff --git a/ffmpeg.c b/ffmpeg.c
index a4d3757..368c855 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -451,7 +451,7 @@ static int ffmpeg_set_pts(struct ffmpeg *ffmpeg, const struct timeval *tv1){

static int ffmpeg_set_quality(struct ffmpeg *ffmpeg){

- char crf[4];
+ int bit_rate;

ffmpeg->opts = 0;
if (ffmpeg->vbr > 100) ffmpeg->vbr = 100;
@@ -459,13 +459,24 @@ static int ffmpeg_set_quality(struct ffmpeg *ffmpeg){
ffmpeg->ctx_codec->codec_id == MY_CODEC_ID_HEVC){
if (ffmpeg->vbr > 0) {
ffmpeg->vbr = (int)(( (100-ffmpeg->vbr) * 51)/100);
+ //bit_rate = ffmpeg->width * ffmpeg->height * ffmpeg->fps * quality_factor
+ bit_rate = (ffmpeg->width * ffmpeg->height * ffmpeg->fps * ffmpeg->vbr) >> 8;
} else {
+ bit_rate = ffmpeg->bps;
ffmpeg->vbr = 28;
}
- snprintf(crf, 4, "%d",ffmpeg->vbr);
- av_dict_set(&ffmpeg->opts, "preset", "ultrafast", 0);
- av_dict_set(&ffmpeg->opts, "tune", "zerolatency", 0);
- av_dict_set(&ffmpeg->opts, "crf", crf, 0);
+ av_dict_set(&ffmpeg->opts, "preset", "ultrafast", 0);
+ av_dict_set(&ffmpeg->opts, "tune", "zerolatency", 0);
+ av_dict_set_int(&ffmpeg->opts, "crf", ffmpeg->vbr, 0);
+ if ((strcmp(ffmpeg->codec->name, "h264_omx") == 0) || (strcmp(ffmpeg->codec->name, "mpeg4_omx") == 0)) {
+ // Clip bit rate to min and max
+ if (bit_rate < 40000)
+ bit_rate = 40000;
+ else if (bit_rate > 3000000)
+ bit_rate = 3000000;
+ ffmpeg->ctx_codec->profile = FF_PROFILE_H264_HIGH;
+ ffmpeg->ctx_codec->bit_rate = bit_rate;
+ }
} else {
/* The selection of 8000 in the else is a subjective number based upon viewing output files */
if (ffmpeg->vbr > 0){
Loading