MT#65420 add frame rescaler

Change-Id: Id3d583db9f6c575879a63ccf74d0cfc42bb83a9d
pull/2169/head
Richard Fuchs 3 months ago
parent a2c3a6bbb8
commit 4fe4cacfed

@ -30,6 +30,7 @@ CFLAGS += $(CFLAGS_LIBAVCODEC)
CFLAGS += $(CFLAGS_LIBAVFORMAT)
CFLAGS += $(CFLAGS_LIBAVUTIL)
CFLAGS += $(CFLAGS_LIBSWRESAMPLE)
CFLAGS += $(CFLAGS_LIBSWSCALE)
CFLAGS += $(CFLAGS_LIBAVFILTER)
CFLAGS += $(CFLAGS_SPANDSP)
CFLAGS += $(CFLAGS_OPUS)
@ -63,6 +64,7 @@ LDLIBS += $(LDLIBS_LIBAVCODEC)
LDLIBS += $(LDLIBS_LIBAVFORMAT)
LDLIBS += $(LDLIBS_LIBAVUTIL)
LDLIBS += $(LDLIBS_LIBSWRESAMPLE)
LDLIBS += $(LDLIBS_LIBSWSCALE)
LDLIBS += $(LDLIBS_LIBAVFILTER)
LDLIBS += $(LDLIBS_SPANDSP)
LDLIBS += $(LDLIBS_OPUS)
@ -93,6 +95,7 @@ LIBSRCS := loglib.c auxlib.c rtplib.c str.c socket.c streambuf.c ssllib.c dtmfli
bufferpool.c bencode.c netfilter_api.c
ifeq ($(with_transcoding),yes)
LIBSRCS += codeclib.c resample.c cchain.c
LIBSRCS += rescale.c
LIBSRCS += $(CODEC_SRCS)
LIBASM := mvr2s_x64_avx2.S mvr2s_x64_avx512.S mix_in_x64_avx2.S mix_in_x64_avx512bw.S mix_in_x64_sse2.S
endif

1
debian/control vendored

@ -42,6 +42,7 @@ Build-Depends:
libspandsp-dev <!pkg.ngcp-rtpengine.no-transcoding>,
libssl-dev (>= 1.0.1),
libswresample-dev <!pkg.ngcp-rtpengine.no-transcoding>,
libswscale-dev <!pkg.ngcp-rtpengine.no-transcoding>,
libsystemd-dev,
libtest2-suite-perl,
liburing-dev (>= 2.3) <!pkg.ngcp-rtpengine.nouring>,

@ -14,6 +14,7 @@ endif
SRCS := auxlib.c bencode.c bufferpool.c codeclib.c dtmflib.c
SRCS += http.c loglib.c mix_buffer.c
SRCS += netfilter_api.c oauth.c poller.c resample.c
SRCS += rescale.c
SRCS += rtplib.c s3utils.c socket.c
SRCS += ssllib.c str.c streambuf.c uring.c
SRCS += avc.c

@ -4,6 +4,7 @@
#include "str.h"
#include "loglib.h"
#include "resample.h"
#include "rescale.h"
#include "rtplib.h"
#include "dtmflib.h"
#include "fix_frame_channel_layout.compat"
@ -232,6 +233,8 @@ void decoder_close(decoder_t *dec) {
decoder_switch_dtx(dec, -1);
resample_shutdown(&dec->resampler);
rescale_shutdown(&dec->rescaler);
g_free(dec);
}

@ -58,6 +58,7 @@ typedef GString *format_print_f(const struct rtp_payload_type *);
#include <stdbool.h>
#include <libswresample/swresample.h>
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavutil/audio_fifo.h>
#ifdef HAVE_BCG729
#include <bcg729/encoder.h>
@ -85,6 +86,7 @@ struct decoder_s;
struct encoder_s;
struct format_s;
struct resample_s;
struct rescale_s;
struct seq_packet_s;
struct rtp_payload_type;
union codec_options_u;
@ -97,6 +99,7 @@ typedef struct decoder_s decoder_t;
typedef struct encoder_s encoder_t;
typedef struct format_s format_t;
typedef struct resample_s resample_t;
typedef struct rescale_s rescale_t;
typedef struct seq_packet_s seq_packet_t;
typedef union codec_options_u codec_options_t;
typedef struct encoder_callback_s encoder_callback_t;
@ -272,6 +275,18 @@ struct resample_s {
bool no_filter;
};
struct rescale_s {
AVFilterGraph *filter;
AVFilterContext *src,
*scale,
*pad,
*format,
*sink;
format_t from_format;
format_t to_format;
format_t aspect_format;
};
enum codec_event {
CE_AMR_CMR_RECV,
CE_AMR_SEND_CMR,
@ -305,6 +320,7 @@ struct decoder_s {
dest_format;
resample_t resampler;
rescale_t rescaler;
union {
struct {

@ -0,0 +1,206 @@
#include "rescale.h"
#include <glib.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersrc.h>
#include <libavfilter/buffersink.h>
#include <libavutil/pixdesc.h>
#include <inttypes.h>
#include <libavutil/frame.h>
#include "loglib.h"
#include "codeclib.h"
static const char *rescale_init(rescale_t *rescale, const format_t *to_format, const format_t *from_format)
{
avfilter_graph_free(&rescale->filter);
rescale->filter = avfilter_graph_alloc();
if (!rescale->filter)
return "failed to alloc filter graph";
rescale->filter->nb_threads = 4;
rescale->filter->thread_type = 0;
rescale->to_format = *to_format;
rescale->from_format = *from_format;
format_t aspect_format = *to_format;
const AVFilter *filter = avfilter_get_by_name("buffer");
if (!filter)
return "buffer filter not available";
char opts[64];
snprintf(opts, sizeof(opts), "width=%d:height=%d:pix_fmt=%s:time_base=1/1",
from_format->width, from_format->height,
av_get_pix_fmt_name(from_format->pix_fmt));
if (avfilter_graph_create_filter(&rescale->src, filter, NULL,
opts, NULL, rescale->filter))
return "failed to configure buffer filter";
AVFilterContext *link = rescale->src;
if (from_format->height != to_format->height || from_format->width != to_format->width) {
filter = avfilter_get_by_name("scale");
if (!filter)
return "scale filter not available";
// maintain aspect ratio
int asp_height = to_format->width * from_format->height / from_format->width;
int diff = asp_height - to_format->height;
if (diff > 0) {
// height too large: use requested height and reduce width to fit
aspect_format.height = to_format->height;
aspect_format.width = to_format->height * from_format->height / from_format->width;
}
else if (diff < -3) {
// height too small: use aspect-adjusted height
aspect_format.height = asp_height;
}
// else: close enough: use exact requested height
snprintf(opts, sizeof(opts), "w=%d:h=%d", aspect_format.width, aspect_format.height);
if (avfilter_graph_create_filter(&rescale->scale, filter, NULL,
opts, NULL, rescale->filter))
return "failed to configure scale filter";
if (avfilter_link(link, 0, rescale->scale, 0))
return "failed to link scale filter";
link = rescale->scale;
}
rescale->aspect_format = aspect_format;
if (memcmp(&aspect_format, to_format, sizeof(aspect_format))) {
filter = avfilter_get_by_name("pad");
if (!filter)
return "pad filter not available";
snprintf(opts, sizeof(opts), "w=%d:h=%d:x=%d:y=%d",
to_format->width, to_format->height,
(to_format->width - aspect_format.width) / 2,
(to_format->height - aspect_format.height) / 2);
if (avfilter_graph_create_filter(&rescale->pad, filter, NULL,
opts, NULL, rescale->filter))
return "failed to configure pad filter";
if (avfilter_link(link, 0, rescale->pad, 0))
return "failed to link pad filter";
link = rescale->pad;
}
if (to_format->pix_fmt != from_format->format) {
filter = avfilter_get_by_name("format");
if (!filter)
return "format filter not available";
snprintf(opts, sizeof(opts), "pix_fmts=%s", av_get_pix_fmt_name(to_format->pix_fmt));
if (avfilter_graph_create_filter(&rescale->format, filter, NULL,
opts, NULL, rescale->filter))
return "failed to configure scale filter";
if (avfilter_link(link, 0, rescale->format, 0))
return "failed to link format filter";
link = rescale->format;
}
filter = avfilter_get_by_name("buffersink");
if (!filter)
return "buffersink filter not available";
if (avfilter_graph_create_filter(&rescale->sink, filter, NULL,
NULL, NULL, rescale->filter))
return "failed to configure buffersink filter";
if (avfilter_link(link, 0, rescale->sink, 0))
return "failed to link buffersink filter";
link = rescale->sink;
if (avfilter_graph_config(rescale->filter, NULL))
return "failed to config filter";
return NULL;
}
AVFrame *rescale_frame(rescale_t *rescale, AVFrame *frame, const format_t *to_format) {
const char *err;
int errcode = 0;
if (frame->format != to_format->pix_fmt)
goto resample;
if (frame->width != to_format->width)
goto resample;
if (frame->height != to_format->height)
goto resample;
return frame;
resample:;
format_t from_format = {
.pix_fmt = frame->format,
.width = frame->width,
.height = frame->height,
};
if (G_UNLIKELY(!rescale->filter
|| memcmp(&rescale->to_format, to_format, sizeof(*to_format))
|| memcmp(&rescale->from_format, &from_format, sizeof(from_format))))
{
err = rescale_init(rescale, to_format, &from_format);
if (err)
goto err;
}
int ret = av_buffersrc_add_frame(rescale->src, frame);
err = "failed to add frame to filter";
if (ret)
goto err;
AVFrame *swr_frame = av_frame_alloc();
err = "failed to alloc rescaling frame";
if (!swr_frame)
goto err;
ret = av_buffersink_get_frame(rescale->sink, swr_frame);
err = "failed to retrieve frame from filter";
if (ret)
goto err;
return swr_frame;
err:
if (errcode)
ilog(LOG_ERR | LOG_FLAG_LIMIT, "Error rescaling: %s (%s)", err, av_error(errcode));
else
ilog(LOG_ERR | LOG_FLAG_LIMIT, "Error rescaling: %s", err);
rescale_shutdown(rescale);
return NULL;
}
void rescale_shutdown(rescale_t *rescale) {
if (rescale->src)
avfilter_free(rescale->src);
rescale->src = NULL;
if (rescale->pad)
avfilter_free(rescale->pad);
rescale->pad = NULL;
if (rescale->scale)
avfilter_free(rescale->scale);
rescale->scale = NULL;
if (rescale->format)
avfilter_free(rescale->format);
rescale->format = NULL;
if (rescale->sink)
avfilter_free(rescale->sink);
rescale->sink = NULL;
avfilter_graph_free(&rescale->filter);
rescale->to_format.pix_fmt = -1;
rescale->from_format.pix_fmt = -1;
}

@ -0,0 +1,13 @@
#ifndef _RESCALE_H_
#define _RESCALE_H_
#include "codeclib.h"
#include <libavutil/frame.h>
AVFrame *rescale_frame(rescale_t *rescale, AVFrame *frame, const format_t *to_format);
void rescale_shutdown(rescale_t *rescale);
#endif

@ -19,6 +19,7 @@ CFLAGS += $(CFLAGS_LIBAVCODEC)
CFLAGS += $(CFLAGS_LIBAVFORMAT)
CFLAGS += $(CFLAGS_LIBAVUTIL)
CFLAGS += $(CFLAGS_LIBSWRESAMPLE)
CFLAGS += $(CFLAGS_LIBSWSCALE)
CFLAGS += $(CFLAGS_LIBAVFILTER)
CFLAGS += $(CFLAGS_SPANDSP)
CFLAGS += $(CFLAGS_OPUS)
@ -32,6 +33,7 @@ LDLIBS += $(LDLIBS_LIBAVCODEC)
LDLIBS += $(LDLIBS_LIBAVFORMAT)
LDLIBS += $(LDLIBS_LIBAVUTIL)
LDLIBS += $(LDLIBS_LIBSWRESAMPLE)
LDLIBS += $(LDLIBS_LIBSWSCALE)
LDLIBS += $(LDLIBS_LIBAVFILTER)
LDLIBS += $(LDLIBS_SPANDSP)
LDLIBS += $(LDLIBS_OPUS)
@ -47,6 +49,7 @@ LDLIBS += $(LDLIBS_CODEC_CHAIN)
SRCS := main.c log.c
LIBSRCS := codeclib.c loglib.c auxlib.c resample.c str.c dtmflib.c rtplib.c poller.c ssllib.c bufferpool.c \
bencode.c uring.c cchain.c socket.c
LIBSRCS += rescale.c
LIBSRCS += $(CODEC_SRCS)
LIBASM := mvr2s_x64_avx2.S mvr2s_x64_avx512.S

@ -14,6 +14,7 @@ CFLAGS += $(CFLAGS_LIBAVCODEC)
CFLAGS += $(CFLAGS_LIBAVFORMAT)
CFLAGS += $(CFLAGS_LIBAVUTIL)
CFLAGS += $(CFLAGS_LIBSWRESAMPLE)
CFLAGS += $(CFLAGS_LIBSWSCALE)
CFLAGS += $(CFLAGS_LIBAVFILTER)
CFLAGS += $(CFLAGS_OPUS)
CFLAGS += $(CFLAGS_MYSQL)
@ -28,6 +29,7 @@ LDLIBS += $(LDLIBS_LIBAVCODEC)
LDLIBS += $(LDLIBS_LIBAVFORMAT)
LDLIBS += $(LDLIBS_LIBAVUTIL)
LDLIBS += $(LDLIBS_LIBSWRESAMPLE)
LDLIBS += $(LDLIBS_LIBSWSCALE)
LDLIBS += $(LDLIBS_LIBAVFILTER)
LDLIBS += $(LDLIBS_OPUS)
LDLIBS += $(LDLIBS_MYSQL)
@ -43,6 +45,7 @@ SRCS := epoll.c garbage.c inotify.c main.c metafile.c stream.c recaux.c packet.c
gcs.c
LIBSRCS := loglib.c auxlib.c rtplib.c codeclib.c resample.c str.c socket.c streambuf.c ssllib.c \
dtmflib.c bufferpool.c bencode.c http.c s3utils.c oauth.c cchain.c
LIBSRCS += rescale.c
LIBSRCS += $(CODEC_SRCS)
LIBASM := mvr2s_x64_avx2.S mvr2s_x64_avx512.S mix_in_x64_avx2.S mix_in_x64_avx512bw.S mix_in_x64_sse2.S

@ -27,6 +27,7 @@ CFLAGS += $(CFLAGS_LIBAVCODEC)
CFLAGS += $(CFLAGS_LIBAVFORMAT)
CFLAGS += $(CFLAGS_LIBAVUTIL)
CFLAGS += $(CFLAGS_LIBSWRESAMPLE)
CFLAGS += $(CFLAGS_LIBSWSCALE)
CFLAGS += $(CFLAGS_LIBAVFILTER)
CFLAGS += $(CFLAGS_SPANDSP)
CFLAGS += $(CFLAGS_OPUS)
@ -55,6 +56,7 @@ LDLIBS += $(LDLIBS_LIBAVCODEC)
LDLIBS += $(LDLIBS_LIBAVFORMAT)
LDLIBS += $(LDLIBS_LIBAVUTIL)
LDLIBS += $(LDLIBS_LIBSWRESAMPLE)
LDLIBS += $(LDLIBS_LIBSWSCALE)
LDLIBS += $(LDLIBS_LIBAVFILTER)
LDLIBS += $(LDLIBS_SPANDSP)
LDLIBS += $(LDLIBS_OPUS)
@ -322,6 +324,7 @@ test-mix-buffer: test-mix-buffer.o \
../lib/mvr2s_x64_avx512.o \
../lib/poller.o \
../lib/resample.o \
../lib/rescale.o \
../lib/socket.o \
../lib/uring.o \
log.o
@ -347,6 +350,7 @@ test-amr-decode: test-amr-decode.o \
../lib/mvr2s_x64_avx2.o \
../lib/mvr2s_x64_avx512.o \
../lib/resample.o \
../lib/rescale.o \
log.o
test-amr-encode: test-amr-encode.o \
@ -360,6 +364,7 @@ test-amr-encode: test-amr-encode.o \
../lib/mvr2s_x64_avx2.o \
../lib/mvr2s_x64_avx512.o \
../lib/resample.o \
../lib/rescale.o \
log.o
test-dtmf-detect: test-dtmf-detect.o
@ -427,6 +432,7 @@ test-stats: test-stats.o \
../lib/mvr2s_x64_avx512.o \
../lib/poller.o \
../lib/resample.o \
../lib/rescale.o \
../lib/socket.o \
../lib/streambuf.o \
../lib/uring.o \
@ -494,6 +500,7 @@ test-transcode: test-transcode.o \
../lib/poller.o \
../lib/pseudo.o \
../lib/resample.o \
../lib/rescale.o \
../lib/socket.o \
../lib/streambuf.o \
../lib/uring.o \
@ -507,6 +514,7 @@ test-sequencer: test-sequencer.o \
../lib/codeclib.o \
../lib/dtmflib.o \
../lib/resample.o \
../lib/rescale.o \
log.o
test-resample: test-resample.o \
@ -519,6 +527,7 @@ test-resample: test-resample.o \
../lib/mvr2s_x64_avx2.o \
../lib/mvr2s_x64_avx512.o \
../lib/resample.o \
../lib/rescale.o \
log.o
test-payload-tracker: test-payload-tracker.o \
@ -537,6 +546,7 @@ test-payload-tracker: test-payload-tracker.o \
../lib/mvr2s_x64_avx512.o \
../lib/poller.o \
../lib/resample.o \
../lib/rescale.o \
../lib/socket.o \
../lib/uring.o \
log.o

@ -90,6 +90,7 @@ if [ "${with_transcoding:-yes}" = yes ]; then
gen-pkgconf-flags LIBAVFORMAT libavformat
gen-pkgconf-flags LIBAVUTIL libavutil
gen-pkgconf-flags LIBSWRESAMPLE libswresample
gen-pkgconf-flags LIBSWSCALE libswscale
gen-pkgconf-flags SPANDSP spandsp
fi

Loading…
Cancel
Save