Implement libswresample support as an alternative to libavresample, which is deprecated

This commit is contained in:
Isaac Connor 2018-12-20 14:06:56 -05:00
parent 88e148068e
commit 6ee72aa2d9
5 changed files with 133 additions and 61 deletions

View File

@ -607,7 +607,23 @@ if(NOT ZM_NO_FFMPEG)
set(optlibsnotfound "${optlibsnotfound} SWScale")
endif(SWSCALE_LIBRARIES)
# rescale (using find_library and find_path)
# SWresample (using find_library and find_path)
find_library(SWRESAMPLE_LIBRARIES swresample)
if(SWRESAMPLE_LIBRARIES)
set(HAVE_LIBSWRESAMPLE 1)
list(APPEND ZM_BIN_LIBS "${SWRESAMPLE_LIBRARIES}")
find_path(SWRESAMPLE_INCLUDE_DIR "libswresample/swresample.h" /usr/include/ffmpeg)
if(SWRESAMPLE_INCLUDE_DIR)
include_directories("${SWRESAMPLE_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${SWRESAMPLE_INCLUDE_DIR}")
endif(SWRESAMPLE_INCLUDE_DIR)
mark_as_advanced(FORCE SWRESAMPLE_LIBRARIES SWRESAMPLE_INCLUDE_DIR)
check_include_file("libswresample/swresample.h" HAVE_LIBSWRESAMPLE_SWRESAMPLE_H)
set(optlibsfound "${optlibsfound} SWResample")
else(SWRESAMPLE_LIBRARIES)
set(optlibsnotfound "${optlibsnotfound} SWResample")
# AVresample (using find_library and find_path)
find_library(AVRESAMPLE_LIBRARIES avresample)
if(AVRESAMPLE_LIBRARIES)
set(HAVE_LIBAVRESAMPLE 1)
@ -624,6 +640,8 @@ if(NOT ZM_NO_FFMPEG)
set(optlibsnotfound "${optlibsnotfound} AVResample")
endif(AVRESAMPLE_LIBRARIES)
endif(SWRESAMPLE_LIBRARIES)
# Find the path to the ffmpeg executable
find_program(FFMPEG_EXECUTABLE
NAMES ffmpeg avconv

View File

@ -10,6 +10,7 @@ Build-Depends: debhelper (>= 9), dh-systemd, python-sphinx | python3-sphinx, apa
,libavcodec-dev (>= 6:10~)
,libavformat-dev (>= 6:10~)
,libavutil-dev (>= 6:10~)
,libswresample-dev
,libswscale-dev (>= 6:10~)
,ffmpeg | libav-tools
,net-tools
@ -41,7 +42,9 @@ Package: zoneminder
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
,javascript-common
,libmp4v2-2, libx264-142|libx264-148|libx264-152, libswscale-ffmpeg3|libswscale4|libswscale3|libswscale5
,libmp4v2-2, libx264-142|libx264-148|libx264-152
,libswscale-ffmpeg3|libswscale4|libswscale3|libswscale5
,libswresample2|libswresample3|libswresample24
,ffmpeg | libav-tools
,libdate-manip-perl, libmime-lite-perl, libmime-tools-perl
,libdbd-mysql-perl

View File

@ -205,7 +205,7 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
audio_out_stream = NULL;
in_frame = NULL;
out_frame = NULL;
#ifdef HAVE_LIBAVRESAMPLE
#if defined(HAVE_LIBSWRESAMPLE) || defined(HAVE_LIBAVRESAMPLE)
resample_ctx = NULL;
#endif
@ -241,8 +241,6 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
Error("Unable to create audio out stream");
audio_out_stream = NULL;
} else {
Debug(2, "setting parameters");
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
audio_out_ctx = avcodec_alloc_context3(audio_out_codec);
// Copy params from instream to ctx
@ -466,10 +464,16 @@ VideoStore::~VideoStore() {
avcodec_free_context(&audio_out_ctx);
#endif
audio_out_ctx = NULL;
#ifdef HAVE_LIBAVRESAMPLE
#if defined(HAVE_LIBAVRESAMPLE) || defined(HAVE_LIBSWRESAMPLE)
if ( resample_ctx ) {
#if defined(HAVE_LIBSWRESAMPLE)
swr_free(&resample_ctx);
#else
#if defined(HAVE_LIBAVRESAMPLE)
avresample_close(resample_ctx);
avresample_free(&resample_ctx);
#endif
#endif
}
if ( in_frame ) {
av_frame_free(&in_frame);
@ -491,7 +495,12 @@ VideoStore::~VideoStore() {
} // VideoStore::~VideoStore()
bool VideoStore::setup_resampler() {
#ifdef HAVE_LIBAVRESAMPLE
#if !defined(HAVE_LIBSWRESAMPLE) && !defined(HAVE_LIBAVRESAMPLE)
Error(
"Not built with resample library. "
"Cannot do audio conversion to AAC");
return false;
#else
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// Newer ffmpeg wants to keep everything separate... so have to lookup our own
@ -499,8 +508,7 @@ bool VideoStore::setup_resampler() {
audio_in_codec =
avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
#else
audio_in_codec =
avcodec_find_decoder(audio_in_ctx->codec_id);
audio_in_codec = avcodec_find_decoder(audio_in_ctx->codec_id);
#endif
ret = avcodec_open2(audio_in_ctx, audio_in_codec, NULL);
if ( ret < 0 ) {
@ -531,6 +539,9 @@ bool VideoStore::setup_resampler() {
audio_out_stream = avformat_new_stream(oc, NULL);
audio_out_ctx = audio_out_stream->codec;
#endif
// Some formats (i.e. WAV) do not produce the proper channel layout
if ( audio_in_ctx->channel_layout == 0 )
audio_in_ctx->channel_layout = av_get_channel_layout("mono");
/* put sample parameters */
audio_out_ctx->bit_rate = audio_in_ctx->bit_rate;
@ -622,34 +633,49 @@ bool VideoStore::setup_resampler() {
return false;
}
#if defined(HAVE_LIBSWRESAMPLE)
resample_ctx = swr_alloc_set_opts(NULL,
av_get_default_channel_layout(audio_out_ctx->channels),
audio_out_ctx->sample_fmt,
audio_out_ctx->sample_rate,
av_get_default_channel_layout(audio_in_ctx->channels),
audio_in_ctx->sample_fmt,
audio_in_ctx->sample_rate,
0, NULL);
if ( !resample_ctx ) {
Error("Could not allocate resample context");
av_frame_free(&in_frame);
av_frame_free(&out_frame);
return false;
}
if ( (ret = swr_init(resample_ctx)) < 0 ) {
Error("Could not open resampler");
av_frame_free(&in_frame);
av_frame_free(&out_frame);
swr_free(&resample_ctx);
return false;
}
#else
#if defined(HAVE_LIBAVRESAMPLE)
// Setup the audio resampler
resample_ctx = avresample_alloc_context();
if ( !resample_ctx ) {
Error("Could not allocate resample ctx");
av_frame_free(&in_frame);
av_frame_free(&out_frame);
return false;
}
// Some formats (i.e. WAV) do not produce the proper channel layout
uint64_t layout = av_get_channel_layout("mono");
if ( audio_in_ctx->channel_layout == 0 ) {
av_opt_set_int(resample_ctx, "in_channel_layout", layout, 0);
Debug(1, "Bad in channel layout. Need to set it to mono (%d).", layout);
} else {
av_opt_set_int(resample_ctx, "in_channel_layout",
audio_in_ctx->channel_layout, 0);
layout = audio_in_ctx->channel_layout;
}
av_opt_set_int(resample_ctx, "in_sample_fmt",
audio_in_ctx->sample_fmt, 0);
av_opt_set_int(resample_ctx, "in_sample_rate",
audio_in_ctx->sample_rate, 0);
av_opt_set_int(resample_ctx, "in_channels", audio_in_ctx->channels,
0);
// av_opt_set_int( resample_ctx, "out_channel_layout",
// audio_out_ctx->channel_layout, 0);
av_opt_set_int(resample_ctx, "in_channels",
audio_in_ctx->channels, 0);
av_opt_set_int(resample_ctx, "out_channel_layout",
layout, 0);
audio_in_ctx->channel_layout, 0);
av_opt_set_int(resample_ctx, "out_sample_fmt",
audio_out_ctx->sample_fmt, 0);
av_opt_set_int(resample_ctx, "out_sample_rate",
@ -664,6 +690,8 @@ bool VideoStore::setup_resampler() {
} else {
Debug(2, "Success opening resampler");
}
#endif
#endif
out_frame->nb_samples = audio_out_ctx->frame_size;
out_frame->format = audio_out_ctx->sample_fmt;
@ -694,11 +722,6 @@ bool VideoStore::setup_resampler() {
}
return true;
#else
Error(
"Not built with libavresample library. Cannot do audio conversion to "
"AAC");
return false;
#endif
} // end bool VideoStore::setup_resampler()
@ -858,7 +881,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
if ( audio_out_codec ) {
Debug(3, "Have audio codec");
#ifdef HAVE_LIBAVRESAMPLE
#if defined(HAVE_LIBSWRESAMPLE) || defined(HAVE_LIBAVRESAMPLE)
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
ret = avcodec_send_packet(audio_in_ctx, ipkt);
@ -902,6 +925,19 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
// Resample the in into the audioSampleBuffer until we proceed the whole
// decoded data
#if defined(HAVE_LIBSWRESAMPLE)
Debug(2, "Converting %d to %d samples", in_frame->nb_samples, out_frame->nb_samples);
if ((ret = swr_convert(resample_ctx,
out_frame->data, frame_size,
(const uint8_t**)in_frame->data,
in_frame->nb_samples)) < 0) {
Error("Could not resample frame (error '%s')\n",
av_make_error_string(ret).c_str());
av_frame_unref(in_frame);
return 0;
}
#else
#if defined(HAVE_LIBAVRESAMPLE)
if ((ret =
avresample_convert(resample_ctx, NULL, 0, 0, in_frame->data,
0, in_frame->nb_samples)) < 0) {
@ -910,8 +946,11 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
av_frame_unref(in_frame);
return 0;
}
#endif
#endif
av_frame_unref(in_frame);
#if defined(HAVE_LIBAVRESAMPLE)
int samples_available = avresample_available(resample_ctx);
if (samples_available < frame_size) {
@ -926,6 +965,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
Warning("Error reading resampled audio: ");
return 0;
}
#endif
Debug(2,
"Frame: samples(%d), format(%d), sample_rate(%d), channel layout(%d)",
out_frame->nb_samples, out_frame->format,
@ -972,7 +1012,8 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
return 0;
}
#endif
#else
Error("Have audio codec but no resampler?!");
#endif
} else {
av_init_packet(&opkt);

View File

@ -3,9 +3,13 @@
#include "zm_ffmpeg.h"
extern "C" {
#ifdef HAVE_LIBSWRESAMPLE
#include "libswresample/swresample.h"
#else
#ifdef HAVE_LIBAVRESAMPLE
#include "libavresample/avresample.h"
#endif
#endif
}
#if HAVE_LIBAVCODEC
@ -38,8 +42,12 @@ private:
// The following are used when encoding the audio stream to AAC
AVCodec *audio_out_codec;
AVCodecContext *audio_out_ctx;
#ifdef HAVE_LIBSWRESAMPLE
SwrContext *resample_ctx;
#else
#ifdef HAVE_LIBAVRESAMPLE
AVAudioResampleContext* resample_ctx;
#endif
#endif
uint8_t *converted_in_samples;

View File

@ -54,6 +54,8 @@
#cmakedefine HAVE_LIBAVUTIL_HWCONTEXT_H 0
#cmakedefine HAVE_LIBSWSCALE 1
#cmakedefine HAVE_LIBSWSCALE_SWSCALE_H 1
#cmakedefine HAVE_LIBSWRESAMPLE 1
#cmakedefine HAVE_LIBSWRESAMPLE_SWRESAMPLE_H 1
#cmakedefine HAVE_LIBAVRESAMPLE 1
#cmakedefine HAVE_LIBAVRESAMPLE_AVRESAMPLE_H 1
#cmakedefine HAVE_LIBVLC 1