From 88ea5bdca85fc0954d1dd64309a308199c1af4a8 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sat, 20 Jan 2024 18:45:29 -0500 Subject: wip Signed-off-by: Andrew Opalach --- src/codec/libav/decoder.c | 2 ++ src/codec/libav/demuxer.c | 4 +--- src/codec/libav/resampler.c | 55 ++++++++++++++++++++++----------------------- src/codec/libav/resampler.h | 6 ++--- src/codec/meson.build | 23 +++++++++++++------ 5 files changed, 49 insertions(+), 41 deletions(-) (limited to 'src/codec') diff --git a/src/codec/libav/decoder.c b/src/codec/libav/decoder.c index ebdcb03..3a6ac06 100644 --- a/src/codec/libav/decoder.c +++ b/src/codec/libav/decoder.c @@ -38,11 +38,13 @@ static bool lav_decoder_init(struct camu_decoder *dec, struct camu_renderer *ren goto err; } + /* s32 cpus = av_cpu_count(); cpus = (cpus > 16) ? 4 : cpus / 4; if (cpus == 0) cpus = 1; av->codec_context->thread_count = cpus; al_log_debug("lav_decoder", "Using %i threads for decoder.", cpus); + */ (void)renderer; /* diff --git a/src/codec/libav/demuxer.c b/src/codec/libav/demuxer.c index 18b9020..fb05ec8 100644 --- a/src/codec/libav/demuxer.c +++ b/src/codec/libav/demuxer.c @@ -83,9 +83,7 @@ static bool lav_demuxer_init(struct camu_demuxer *demux, struct cch_handle *hand } al_log_info("lav_demux", "Setting stream #%i to a duration of %.3fs.", i, stream->duration * av_q2d(stream->time_base)); } - if (stream->start_time < 0) { - al_log_warn("lav_demux", "Stream has a negative start_time (%ld).", stream->start_time); - } + if (stream->start_time < 0) stream->start_time = 0; s64 duration = av_rescale_q(stream->duration, stream->time_base, AV_TIME_BASE_Q); al_assert(duration >= 0); if ((u64)duration > av->duration) av->duration = (u64)duration; diff --git a/src/codec/libav/resampler.c b/src/codec/libav/resampler.c index ba700f4..229c68c 100644 --- a/src/codec/libav/resampler.c +++ b/src/codec/libav/resampler.c @@ -8,40 +8,38 @@ #include "resampler.h" -static bool in_req_match(struct camu_lav_resample_fmt *fmt) +static bool in_req_matches(struct camu_lav_resample_fmt *fmt) { - return (av_channel_layout_compare(&fmt->in_channel_layout, &fmt->req_channel_layout) == 0) && - (fmt->in_format == fmt->req_format) && (fmt->in_sample_rate == fmt->req_sample_rate); + return (fmt->in_format == fmt->req_format) && + (av_channel_layout_compare(&fmt->in_channel_layout, &fmt->req_channel_layout) == 0) && + (fmt->in_sample_rate == fmt->req_sample_rate); } bool camu_lav_resampler_init(struct camu_lav_resampler *resamp, struct camu_lav_resample_fmt *fmt) { al_memset(resamp, 0, sizeof(struct camu_lav_resampler)); - fmt->resampler_needed = !in_req_match(fmt); + fmt->resampler_needed = !in_req_matches(fmt); + if (!fmt->resampler_needed) return true; camu_lav_resample_fmt_copy(&resamp->fmt, fmt); - if (!fmt->resampler_needed) { - return true; - } - - resamp->resample_context = swr_alloc(); - - SwrContext *ctx = resamp->resample_context; - - if (!ctx) { - al_log_error("lav_resampler", "Failed to create resampler context."); + SwrContext *ctx = NULL; + s32 ret = swr_alloc_set_opts2(&ctx, + &fmt->req_channel_layout, + fmt->req_format, + fmt->req_sample_rate, + &fmt->in_channel_layout, + fmt->in_format, + fmt->in_sample_rate, + 0, + NULL); + + if (ret != 0) { + al_log_error("lav_resampler", "Failed to configure resampler context."); return false; } - av_opt_set_chlayout(ctx, "in_chlayout", &fmt->in_channel_layout, 0); - av_opt_set_chlayout(ctx, "out_chlayout", &fmt->req_channel_layout, 0); - av_opt_set_int(ctx, "in_sample_rate", fmt->in_sample_rate, 0); - av_opt_set_int(ctx, "out_sample_rate", fmt->req_sample_rate, 0); - av_opt_set_sample_fmt(ctx, "in_sample_fmt", fmt->in_format, 0); - av_opt_set_sample_fmt(ctx, "out_sample_fmt", fmt->req_format, 0); - // Slower but more accurate resampler. //av_opt_set_int(ctx, "resampler", SWR_ENGINE_SOXR, 0); @@ -51,13 +49,16 @@ bool camu_lav_resampler_init(struct camu_lav_resampler *resamp, struct camu_lav_ return false; } + resamp->resample_context = ctx; + return true; } s32 camu_lav_resampler_sample_count(struct camu_lav_resampler *resamp, s32 in_samples) { - al_assert(resamp->fmt.resampler_needed); return swr_get_out_samples(resamp->resample_context, in_samples); + //return av_rescale_rnd(swr_get_delay(resamp->resample_context, resamp->fmt.req_sample_rate) + + // in_samples, resamp->fmt.in_sample_rate, resamp->fmt.req_sample_rate, AV_ROUND_UP); } static inline void free_sample_data(struct camu_lav_resampler *resamp) @@ -70,15 +71,14 @@ static inline void alloc_sample_data(struct camu_lav_resampler *resamp, s32 samp if (resamp->sample_count >= sample_count) return; free_sample_data(resamp); s32 nb_channels = resamp->fmt.req_channel_layout.nb_channels; - av_samples_alloc(resamp->data, NULL, nb_channels, sample_count, resamp->fmt.req_format, 0); + av_samples_alloc(resamp->data, NULL, nb_channels, sample_count, resamp->fmt.req_format, 32); resamp->sample_count = sample_count; } s32 camu_lav_resampler_convert(struct camu_lav_resampler *resamp, const u8 **in_data, s32 in_samples) { - al_assert(resamp->fmt.resampler_needed); - s32 max_resample_count = in_samples ? - camu_lav_resampler_sample_count(resamp, in_samples) : resamp->sample_count; + al_assert(in_samples > 0); + s32 max_resample_count = camu_lav_resampler_sample_count(resamp, in_samples); alloc_sample_data(resamp, max_resample_count); s32 resample_count = swr_convert(resamp->resample_context, resamp->data, max_resample_count, in_data, in_samples); @@ -95,7 +95,6 @@ u8 **camu_lav_resampler_get_data(struct camu_lav_resampler *resamp) void camu_lav_resampler_close(struct camu_lav_resampler *resamp) { - al_assert(resamp->fmt.resampler_needed); if (resamp->resample_context) { swr_close(resamp->resample_context); swr_free(&resamp->resample_context); @@ -105,13 +104,13 @@ void camu_lav_resampler_close(struct camu_lav_resampler *resamp) void camu_lav_resample_fmt_copy(struct camu_lav_resample_fmt *dest, struct camu_lav_resample_fmt *src) { + dest->resampler_needed = src->resampler_needed; dest->in_format = src->in_format; av_channel_layout_copy(&dest->in_channel_layout, &src->in_channel_layout); dest->in_sample_rate = src->in_sample_rate; dest->req_format = src->req_format; av_channel_layout_copy(&dest->req_channel_layout, &src->req_channel_layout); dest->req_sample_rate = src->req_sample_rate; - dest->resampler_needed = src->resampler_needed; } size_t camu_lav_resample_fmt_bytes_per_sample(struct camu_lav_resample_fmt *fmt) diff --git a/src/codec/libav/resampler.h b/src/codec/libav/resampler.h index 703d9a7..92525b2 100644 --- a/src/codec/libav/resampler.h +++ b/src/codec/libav/resampler.h @@ -5,15 +5,15 @@ #include struct camu_lav_resample_fmt { + bool resampler_needed; + enum AVSampleFormat in_format; + enum AVSampleFormat req_format; s32 in_channel_count; s32 req_channel_count; AVChannelLayout in_channel_layout; AVChannelLayout req_channel_layout; s32 in_sample_rate; s32 req_sample_rate; - enum AVSampleFormat in_format; - enum AVSampleFormat req_format; - bool resampler_needed; }; struct camu_lav_resampler { diff --git a/src/codec/meson.build b/src/codec/meson.build index 0946a17..20524a4 100644 --- a/src/codec/meson.build +++ b/src/codec/meson.build @@ -11,13 +11,17 @@ av_server_src = [ av_client_src = [ av_src, 'libav/decoder.c', - 'libav/resampler.c', - 'libav/scaler.c' + 'libav/resampler.c' ] -av_inc = [] av_ldflags = [] +av_inc = [] av_server_deps = [] av_client_deps = [] +av_client_use_scaler = false + +if av_client_use_scaler + av_client_src += ['libav/scaler.c'] +endif libavutil = dependency('libavutil', required: false) libavformat = dependency('libavformat', required: false) @@ -32,9 +36,11 @@ if not (libavutil.found() and libavformat.found() and libavcodec.found() and lib libavcodec = av_proj.get_variable('avcodec') libswresample = av_proj.get_variable('swresample') libswscale = av_proj.get_variable('swscale') - av_inc += [av_proj.get_variable('inc')] - av_ldflags += ['-L' + av_proj.get_variable('libdir')] - av_client_deps += [dependency('zlib')] + av_ldflags += ['-L' + av_proj.get_variable('av_lib_dir')] + # Only used to set include dirs for libav. + av_inc_dep = av_proj.get_variable('av_inc_dep') + av_server_deps += [av_inc_dep] + av_client_deps += [av_inc_dep, dependency('zlib')] soxr = compiler.find_library('soxr', required: false) if soxr.found() av_client_deps += [soxr] @@ -45,7 +51,10 @@ endif if libavutil.found() and libavformat.found() and libavcodec.found() and libswresample.found() and libswscale.found() av_server_deps += [libavutil, libavformat, libavcodec] - av_client_deps += [libavutil, libavformat, libavcodec, libswresample, libswscale] + av_client_deps += [libavutil, libavformat, libavcodec, libswresample] + if av_client_use_scaler + av_client_deps += [libswscale] + endif av_server = declare_dependency(sources: av_server_src, dependencies: av_server_deps, include_directories: av_inc, link_args: av_ldflags) av_client = declare_dependency(sources: av_client_src, dependencies: av_client_deps, -- cgit v1.2.3-101-g0448