Build: Promote libswscale to a required dependency

FFmpeg is an integral component of ZM. Promote the appropriate libraries to required dependencies.
This reduces the possible build configurations greatly and thus maintenance burden.
This commit is contained in:
Peter Keresztes Schmidt 2021-06-05 14:12:20 +02:00
parent a9ad5c5eee
commit 762476ec76
11 changed files with 6 additions and 64 deletions

View File

@ -442,7 +442,8 @@ find_package(FFMPEG REQUIRED
COMPONENTS COMPONENTS
avcodec avcodec
avformat avformat
avutil) avutil
swscale)
set(CMAKE_REQUIRED_INCLUDES ${FFMPEG_avutil_INCLUDE_DIRS}) set(CMAKE_REQUIRED_INCLUDES ${FFMPEG_avutil_INCLUDE_DIRS})
check_include_file("libavutil/hwcontext.h" HAVE_LIBAVUTIL_HWCONTEXT_H) check_include_file("libavutil/hwcontext.h" HAVE_LIBAVUTIL_HWCONTEXT_H)
@ -457,16 +458,6 @@ else()
set(optlibsnotfound "${optlibsnotfound} AVDevice") set(optlibsnotfound "${optlibsnotfound} AVDevice")
endif() endif()
find_package(FFMPEG COMPONENTS swscale)
if(FFMPEG_swscale_FOUND)
set(HAVE_LIBSWSCALE 1)
set(HAVE_LIBSWSCALE_SWSCALE_H 1)
list(APPEND ZM_BIN_LIBS "${FFMPEG_swscale_LIBRARIES}")
set(optlibsfound "${optlibsfound} SWScale")
else()
set(optlibsnotfound "${optlibsnotfound} SWScale")
endif()
find_package(FFMPEG COMPONENTS swresample) find_package(FFMPEG COMPONENTS swresample)
if(FFMPEG_swresample_FOUND) if(FFMPEG_swresample_FOUND)
set(HAVE_LIBSWRESAMPLE 1) set(HAVE_LIBSWRESAMPLE 1)

View File

@ -80,6 +80,7 @@ target_link_libraries(zm
FFMPEG::avcodec FFMPEG::avcodec
FFMPEG::avformat FFMPEG::avformat
FFMPEG::avutil FFMPEG::avutil
FFMPEG::swscale
libbcrypt::bcrypt libbcrypt::bcrypt
RtspServer::RtspServer RtspServer::RtspServer
martinmoene::span-lite martinmoene::span-lite

View File

@ -155,7 +155,6 @@ extern "C" {
#endif /* HAVE_LIBAVDEVICE_AVDEVICE_H */ #endif /* HAVE_LIBAVDEVICE_AVDEVICE_H */
// SWSCALE // SWSCALE
#if HAVE_LIBSWSCALE_SWSCALE_H
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
/* LIBSWSCALE_VERSION_CHECK checks for the right version of libav and FFmpeg /* LIBSWSCALE_VERSION_CHECK checks for the right version of libav and FFmpeg
@ -166,8 +165,6 @@ extern "C" {
( (LIBSWSCALE_VERSION_MICRO < 100 && LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(a, b, c) ) || \ ( (LIBSWSCALE_VERSION_MICRO < 100 && LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(a, b, c) ) || \
(LIBSWSCALE_VERSION_MICRO >= 100 && LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(a, d, e) ) ) (LIBSWSCALE_VERSION_MICRO >= 100 && LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(a, d, e) ) )
#endif /* HAVE_LIBSWSCALE_SWSCALE_H */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -144,9 +144,7 @@ FfmpegCamera::FfmpegCamera(
#endif #endif
#endif #endif
#if HAVE_LIBSWSCALE
mConvertContext = nullptr; mConvertContext = nullptr;
#endif
/* Has to be located inside the constructor so other components such as zma /* Has to be located inside the constructor so other components such as zma
* will receive correct colours and subpixel order */ * will receive correct colours and subpixel order */
if ( colours == ZM_COLOUR_RGB32 ) { if ( colours == ZM_COLOUR_RGB32 ) {

View File

@ -62,9 +62,7 @@ class FfmpegCamera : public Camera {
int Close() override; int Close() override;
bool mCanCapture; bool mCanCapture;
#if HAVE_LIBSWSCALE
struct SwsContext *mConvertContext; struct SwsContext *mConvertContext;
#endif
int error_count; int error_count;

View File

@ -45,7 +45,6 @@ static int vidioctl(int fd, int request, void *arg) {
return result; return result;
} }
#if HAVE_LIBSWSCALE
static _AVPIXELFORMAT getFfPixFormatFromV4lPalette(int v4l_version, int palette) { static _AVPIXELFORMAT getFfPixFormatFromV4lPalette(int v4l_version, int palette) {
_AVPIXELFORMAT pixFormat = AV_PIX_FMT_NONE; _AVPIXELFORMAT pixFormat = AV_PIX_FMT_NONE;
@ -176,7 +175,6 @@ static _AVPIXELFORMAT getFfPixFormatFromV4lPalette(int v4l_version, int palette)
return pixFormat; return pixFormat;
} // end getFfPixFormatFromV4lPalette } // end getFfPixFormatFromV4lPalette
#endif // HAVE_LIBSWSCALE
static char palette_desc[32]; static char palette_desc[32];
/* Automatic format selection preferred formats */ /* Automatic format selection preferred formats */
@ -222,9 +220,7 @@ int LocalCamera::vid_fd = -1;
int LocalCamera::v4l_version = 0; int LocalCamera::v4l_version = 0;
LocalCamera::V4L2Data LocalCamera::v4l2_data; LocalCamera::V4L2Data LocalCamera::v4l2_data;
#if HAVE_LIBSWSCALE
AVFrame **LocalCamera::capturePictures = nullptr; AVFrame **LocalCamera::capturePictures = nullptr;
#endif // HAVE_LIBSWSCALE
LocalCamera *LocalCamera::last_camera = nullptr; LocalCamera *LocalCamera::last_camera = nullptr;
@ -326,11 +322,9 @@ LocalCamera::LocalCamera(
Warning("Different capture sizes defined for monitors sharing same device, results may be unpredictable or completely wrong"); Warning("Different capture sizes defined for monitors sharing same device, results may be unpredictable or completely wrong");
} }
#if HAVE_LIBSWSCALE
/* Get ffmpeg pixel format based on capture palette and endianness */ /* Get ffmpeg pixel format based on capture palette and endianness */
capturePixFormat = getFfPixFormatFromV4lPalette( v4l_version, palette ); capturePixFormat = getFfPixFormatFromV4lPalette( v4l_version, palette );
imagePixFormat = AV_PIX_FMT_NONE; imagePixFormat = AV_PIX_FMT_NONE;
#endif // HAVE_LIBSWSCALE
} }
/* V4L2 format matching */ /* V4L2 format matching */
@ -360,16 +354,11 @@ LocalCamera::LocalCamera(
/* Unable to find a solution for the selected palette and target colourspace. Conversion required. Notify the user of performance penalty */ /* Unable to find a solution for the selected palette and target colourspace. Conversion required. Notify the user of performance penalty */
} else { } else {
if ( capture ) { if ( capture ) {
#if HAVE_LIBSWSCALE
Info( Info(
"No direct match for the selected palette (%d) and target colorspace (%02u). Format conversion is required, performance penalty expected", "No direct match for the selected palette (%d) and target colorspace (%02u). Format conversion is required, performance penalty expected",
capturePixFormat, capturePixFormat,
colours); colours);
#else
Info("No direct match for the selected palette and target colorspace. Format conversion is required, performance penalty expected");
#endif
} }
#if HAVE_LIBSWSCALE
/* Try using swscale for the conversion */ /* Try using swscale for the conversion */
conversion_type = 1; conversion_type = 1;
Debug(2, "Using swscale for image conversion"); Debug(2, "Using swscale for image conversion");
@ -397,10 +386,6 @@ LocalCamera::LocalCamera(
} }
#endif #endif
} }
#else
/* Don't have swscale, see what we can do */
conversion_type = 2;
#endif
/* Our YUYV->Grayscale conversion is a lot faster than swscale's */ /* Our YUYV->Grayscale conversion is a lot faster than swscale's */
if ( colours == ZM_COLOUR_GRAY8 && palette == V4L2_PIX_FMT_YUYV ) { if ( colours == ZM_COLOUR_GRAY8 && palette == V4L2_PIX_FMT_YUYV ) {
conversion_type = 2; conversion_type = 2;
@ -458,7 +443,6 @@ LocalCamera::LocalCamera(
last_camera = this; last_camera = this;
Debug(3, "Selected subpixelorder: %u", subpixelorder); Debug(3, "Selected subpixelorder: %u", subpixelorder);
#if HAVE_LIBSWSCALE
/* Initialize swscale stuff */ /* Initialize swscale stuff */
if ( capture and (conversion_type == 1) ) { if ( capture and (conversion_type == 1) ) {
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101) #if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
@ -490,7 +474,6 @@ LocalCamera::LocalCamera(
tmpPicture = nullptr; tmpPicture = nullptr;
imgConversionContext = nullptr; imgConversionContext = nullptr;
} // end if capture and conversion_tye == swscale } // end if capture and conversion_tye == swscale
#endif
if ( capture and device_prime ) if ( capture and device_prime )
Initialise(); Initialise();
} // end LocalCamera::LocalCamera } // end LocalCamera::LocalCamera
@ -499,7 +482,6 @@ LocalCamera::~LocalCamera() {
if ( device_prime && capture ) if ( device_prime && capture )
Terminate(); Terminate();
#if HAVE_LIBSWSCALE
/* Clean up swscale stuff */ /* Clean up swscale stuff */
if ( capture && (conversion_type == 1) ) { if ( capture && (conversion_type == 1) ) {
sws_freeContext(imgConversionContext); sws_freeContext(imgConversionContext);
@ -507,8 +489,6 @@ LocalCamera::~LocalCamera() {
av_frame_free(&tmpPicture); av_frame_free(&tmpPicture);
} }
#endif
} // end LocalCamera::~LocalCamera } // end LocalCamera::~LocalCamera
int LocalCamera::Close() { int LocalCamera::Close() {
@ -696,9 +676,8 @@ void LocalCamera::Initialise() {
channel_count, v4l_multi_buffer, v4l2_data.reqbufs.count); channel_count, v4l_multi_buffer, v4l2_data.reqbufs.count);
v4l2_data.buffers = new V4L2MappedBuffer[v4l2_data.reqbufs.count]; v4l2_data.buffers = new V4L2MappedBuffer[v4l2_data.reqbufs.count];
#if HAVE_LIBSWSCALE
capturePictures = new AVFrame *[v4l2_data.reqbufs.count]; capturePictures = new AVFrame *[v4l2_data.reqbufs.count];
#endif // HAVE_LIBSWSCALE
for ( unsigned int i = 0; i < v4l2_data.reqbufs.count; i++ ) { for ( unsigned int i = 0; i < v4l2_data.reqbufs.count; i++ ) {
struct v4l2_buffer vid_buf; struct v4l2_buffer vid_buf;
@ -720,7 +699,6 @@ void LocalCamera::Initialise() {
Fatal("Can't map video buffer %u (%u bytes) to memory: %s(%d)", Fatal("Can't map video buffer %u (%u bytes) to memory: %s(%d)",
i, vid_buf.length, strerror(errno), errno); i, vid_buf.length, strerror(errno), errno);
#if HAVE_LIBSWSCALE
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101) #if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
capturePictures[i] = av_frame_alloc(); capturePictures[i] = av_frame_alloc();
#else #else
@ -746,7 +724,6 @@ void LocalCamera::Initialise() {
v4l2_data.fmt.fmt.pix.height v4l2_data.fmt.fmt.pix.height
); );
#endif #endif
#endif // HAVE_LIBSWSCALE
} // end foreach request buf } // end foreach request buf
Debug(3, "Configuring video source"); Debug(3, "Configuring video source");
@ -792,13 +769,11 @@ void LocalCamera::Terminate() {
Debug(3, "Unmapping video buffers"); Debug(3, "Unmapping video buffers");
for ( unsigned int i = 0; i < v4l2_data.reqbufs.count; i++ ) { for ( unsigned int i = 0; i < v4l2_data.reqbufs.count; i++ ) {
#if HAVE_LIBSWSCALE
/* Free capture pictures */ /* Free capture pictures */
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101) #if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
av_frame_free(&capturePictures[i]); av_frame_free(&capturePictures[i]);
#else #else
av_freep(&capturePictures[i]); av_freep(&capturePictures[i]);
#endif
#endif #endif
if ( munmap(v4l2_data.buffers[i].start, v4l2_data.buffers[i].length) < 0 ) if ( munmap(v4l2_data.buffers[i].start, v4l2_data.buffers[i].length) < 0 )
Error("Failed to munmap buffer %d: %s", i, strerror(errno)); Error("Failed to munmap buffer %d: %s", i, strerror(errno));
@ -1484,7 +1459,6 @@ int LocalCamera::Capture(std::shared_ptr<ZMPacket> &zm_packet) {
Error("Failed requesting writeable buffer for the captured image."); Error("Failed requesting writeable buffer for the captured image.");
return -1; return -1;
} }
#if HAVE_LIBSWSCALE
if (conversion_type == 1) { if (conversion_type == 1) {
Debug(9, "Calling sws_scale to perform the conversion"); Debug(9, "Calling sws_scale to perform the conversion");
/* Use swscale to convert the image directly into the shared memory */ /* Use swscale to convert the image directly into the shared memory */
@ -1505,9 +1479,7 @@ int LocalCamera::Capture(std::shared_ptr<ZMPacket> &zm_packet) {
tmpPicture->data, tmpPicture->data,
tmpPicture->linesize tmpPicture->linesize
); );
} else } else if (conversion_type == 2) {
#endif
if ( conversion_type == 2 ) {
Debug(9, "Calling the conversion function"); Debug(9, "Calling the conversion function");
/* Call the image conversion function and convert directly into the shared memory */ /* Call the image conversion function and convert directly into the shared memory */
(*conversion_fptr)(buffer, directbuffer, pixels); (*conversion_fptr)(buffer, directbuffer, pixels);
@ -1517,7 +1489,6 @@ int LocalCamera::Capture(std::shared_ptr<ZMPacket> &zm_packet) {
/* JPEG decoding */ /* JPEG decoding */
zm_packet->image->DecodeJpeg(buffer, buffer_bytesused, colours, subpixelorder); zm_packet->image->DecodeJpeg(buffer, buffer_bytesused, colours, subpixelorder);
} }
} else { } else {
Debug(3, "No format conversion performed. Assigning the image"); Debug(3, "No format conversion performed. Assigning the image");

View File

@ -73,13 +73,11 @@ protected:
static V4L2Data v4l2_data; static V4L2Data v4l2_data;
#if HAVE_LIBSWSCALE
static AVFrame **capturePictures; static AVFrame **capturePictures;
_AVPIXELFORMAT imagePixFormat; _AVPIXELFORMAT imagePixFormat;
_AVPIXELFORMAT capturePixFormat; _AVPIXELFORMAT capturePixFormat;
struct SwsContext *imgConversionContext; struct SwsContext *imgConversionContext;
AVFrame *tmpPicture; AVFrame *tmpPicture;
#endif // HAVE_LIBSWSCALE
static LocalCamera *last_camera; static LocalCamera *last_camera;

View File

@ -594,20 +594,14 @@ double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _a
double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp ) { double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp ) {
if ( codec_context->pix_fmt != pf ) { if ( codec_context->pix_fmt != pf ) {
#ifdef HAVE_LIBSWSCALE
static struct SwsContext *img_convert_ctx = nullptr; static struct SwsContext *img_convert_ctx = nullptr;
#endif // HAVE_LIBSWSCALE
memcpy( tmp_opicture->data[0], buffer, buffer_size ); memcpy( tmp_opicture->data[0], buffer, buffer_size );
#ifdef HAVE_LIBSWSCALE
if ( !img_convert_ctx ) { if ( !img_convert_ctx ) {
img_convert_ctx = sws_getCachedContext( nullptr, codec_context->width, codec_context->height, pf, codec_context->width, codec_context->height, codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr ); img_convert_ctx = sws_getCachedContext( nullptr, codec_context->width, codec_context->height, pf, codec_context->width, codec_context->height, codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr );
if ( !img_convert_ctx ) if ( !img_convert_ctx )
Panic( "Unable to initialise image scaling context" ); Panic( "Unable to initialise image scaling context" );
} }
sws_scale( img_convert_ctx, tmp_opicture->data, tmp_opicture->linesize, 0, codec_context->height, opicture->data, opicture->linesize ); sws_scale( img_convert_ctx, tmp_opicture->data, tmp_opicture->linesize, 0, codec_context->height, opicture->data, opicture->linesize );
#else // HAVE_LIBSWSCALE
Fatal( "swscale is required for MPEG mode" );
#endif // HAVE_LIBSWSCALE
} else { } else {
memcpy( opicture->data[0], buffer, buffer_size ); memcpy( opicture->data[0], buffer, buffer_size );
} }

View File

@ -22,7 +22,6 @@
#include "zm_image.h" #include "zm_image.h"
#include "zm_logger.h" #include "zm_logger.h"
#if HAVE_LIBSWSCALE
SWScale::SWScale() : gotdefaults(false), swscale_ctx(nullptr), input_avframe(nullptr), output_avframe(nullptr) { SWScale::SWScale() : gotdefaults(false), swscale_ctx(nullptr), input_avframe(nullptr), output_avframe(nullptr) {
Debug(4, "SWScale object created"); Debug(4, "SWScale object created");
} }
@ -291,4 +290,3 @@ size_t SWScale::GetBufferSize(enum _AVPIXELFORMAT pf, unsigned int width, unsign
return outsize = avpicture_get_size(pf, width,height); return outsize = avpicture_get_size(pf, width,height);
#endif #endif
} }
#endif // HAVE_LIBSWSCALE

View File

@ -7,7 +7,6 @@
class Image; class Image;
/* SWScale wrapper class to make our life easier and reduce code reuse */ /* SWScale wrapper class to make our life easier and reduce code reuse */
#if HAVE_LIBSWSCALE
class SWScale { class SWScale {
public: public:
SWScale(); SWScale();
@ -32,6 +31,5 @@ class SWScale {
unsigned int default_width; unsigned int default_width;
unsigned int default_height; unsigned int default_height;
}; };
#endif // HAVE_LIBSWSCALE
#endif #endif // ZM_SWSCALE_H

View File

@ -37,8 +37,6 @@
#cmakedefine HAVE_LIBAVDEVICE 1 #cmakedefine HAVE_LIBAVDEVICE 1
#cmakedefine HAVE_LIBAVDEVICE_AVDEVICE_H 1 #cmakedefine HAVE_LIBAVDEVICE_AVDEVICE_H 1
#cmakedefine HAVE_LIBAVUTIL_HWCONTEXT_H 1 #cmakedefine HAVE_LIBAVUTIL_HWCONTEXT_H 1
#cmakedefine HAVE_LIBSWSCALE 1
#cmakedefine HAVE_LIBSWSCALE_SWSCALE_H 1
#cmakedefine HAVE_LIBSWRESAMPLE 1 #cmakedefine HAVE_LIBSWRESAMPLE 1
#cmakedefine HAVE_LIBSWRESAMPLE_SWRESAMPLE_H 1 #cmakedefine HAVE_LIBSWRESAMPLE_SWRESAMPLE_H 1
#cmakedefine HAVE_LIBVLC 1 #cmakedefine HAVE_LIBVLC 1