Skip to content

Commit

Permalink
Bring back support for older ffmpegs
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Aug 2, 2024
1 parent 486121e commit a720a54
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ XAV_SO = $(PRIV_DIR)/libxav.so
# uncomment to compile with debug logs
# XAV_DEBUG_LOGS = -DXAV_DEBUG=1

HEADERS = $(XAV_DIR)/reader.h $(XAV_DIR)/decoder.h $(XAV_DIR)/converter.h $(XAV_DIR)/utils.h
HEADERS = $(XAV_DIR)/reader.h $(XAV_DIR)/decoder.h $(XAV_DIR)/converter.h $(XAV_DIR)/channel_layout.h $(XAV_DIR)/utils.h
SOURCES = $(XAV_DIR)/xav_nif.c $(XAV_DIR)/reader.c $(XAV_DIR)/decoder.c $(XAV_DIR)/converter.c $(XAV_DIR)/utils.c

CFLAGS = $(XAV_DEBUG_LOGS) -fPIC -shared
Expand Down
12 changes: 12 additions & 0 deletions c_src/xav/channel_layout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CHANNEL_LAYOUT_H
#define CHANNEL_LAYOUT_H
#include <libavutil/channel_layout.h>

struct ChannelLayout {
#if LIBAVUTIL_VERSION_MAJOR >= 58
AVChannelLayout layout;
#else
uint64_t layout;
#endif
};
#endif
25 changes: 19 additions & 6 deletions c_src/xav/converter.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
#include <libswresample/swresample.h>
#include <stdint.h>

#include "channel_layout.h"
#include "utils.h"

int converter_init(struct Converter *c, AVChannelLayout in_chlayout, int in_sample_rate,
enum AVSampleFormat in_sample_fmt, AVChannelLayout out_chlayout,
int converter_init(struct Converter *c, struct ChannelLayout in_chlayout, int in_sample_rate,
enum AVSampleFormat in_sample_fmt, struct ChannelLayout out_chlayout,
int out_sample_rate, enum AVSampleFormat out_sample_fmt) {
c->swr_ctx = swr_alloc();
c->in_sample_rate = in_sample_rate;
c->out_sample_rate = out_sample_rate;
c->out_chlayout = out_chlayout;
c->out_sample_fmt = out_sample_fmt;

av_opt_set_chlayout(c->swr_ctx, "in_chlayout", &in_chlayout, 0);
av_opt_set_chlayout(c->swr_ctx, "out_chlayout", &out_chlayout, 0);
#if LIBAVUTIL_VERSION_MAJOR >= 58
av_opt_set_chlayout(c->swr_ctx, "in_chlayout", &in_chlayout.layout, 0);
av_opt_set_chlayout(c->swr_ctx, "out_chlayout", &out_chlayout.layout, 0);
#else
av_opt_set_channel_layout(c->swr_ctx, "in_channel_layout", &in_chlayout.layout, 0);
av_opt_set_channel_layout(c->swr_ctx, "out_channel_layout", &out_chlayout.layout, 0);
#endif

av_opt_set_int(c->swr_ctx, "in_sample_rate", in_sample_rate, 0);
av_opt_set_int(c->swr_ctx, "out_sample_rate", out_sample_rate, 0);
Expand All @@ -30,6 +36,13 @@ int converter_init(struct Converter *c, AVChannelLayout in_chlayout, int in_samp

int converter_convert(struct Converter *c, AVFrame *src_frame, uint8_t ***out_data,
int *out_samples, int *out_size) {

#if LIBAVUTIL_VERSION_MAJOR >= 58
int out_nb_channels = c->out_chlayout.layout.nb_channels;
#else
int out_nb_channels = av_get_channel_layout_nb_channels(c->out_chlayout.layout);
#endif

uint8_t **out_data_tmp = NULL;
int max_out_nb_samples = swr_get_out_samples(c->swr_ctx, src_frame->nb_samples);
int out_bytes_per_sample = av_get_bytes_per_sample(c->out_sample_fmt);
Expand All @@ -38,7 +51,7 @@ int converter_convert(struct Converter *c, AVFrame *src_frame, uint8_t ***out_da
// to use fast/aligned SIMD routines - this is what align option is used for.
// See https://stackoverflow.com/questions/35678041/what-is-linesize-alignment-meaning
// Because we return the binary straight to the Erlang, we can disable it.
int ret = av_samples_alloc_array_and_samples(&out_data_tmp, NULL, c->out_chlayout.nb_channels,
int ret = av_samples_alloc_array_and_samples(&out_data_tmp, NULL, out_nb_channels,
max_out_nb_samples, c->out_sample_fmt, 1);

if (ret < 0) {
Expand All @@ -58,7 +71,7 @@ int converter_convert(struct Converter *c, AVFrame *src_frame, uint8_t ***out_da

XAV_LOG_DEBUG("Converted %d samples per channel", *out_samples);

*out_size = *out_samples * out_bytes_per_sample * c->out_chlayout.nb_channels;
*out_size = *out_samples * out_bytes_per_sample * out_nb_channels;

return 0;
}
Expand Down
8 changes: 5 additions & 3 deletions c_src/xav/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
#include <libswresample/swresample.h>
#include <stdint.h>

#include "channel_layout.h"

struct Converter {
SwrContext *swr_ctx;
int64_t in_sample_rate;
int64_t out_sample_rate;
AVChannelLayout out_chlayout;
struct ChannelLayout out_chlayout;
enum AVSampleFormat out_sample_fmt;
};

int converter_init(struct Converter *c, AVChannelLayout in_chlayout, int in_sample_rate,
enum AVSampleFormat in_sample_fmt, AVChannelLayout out_chlaout,
int converter_init(struct Converter *c, struct ChannelLayout in_chlayout, int in_sample_rate,
enum AVSampleFormat in_sample_fmt, struct ChannelLayout out_chlaout,
int out_sample_rate, enum AVSampleFormat out_sample_fmt);
int converter_convert(struct Converter *c, AVFrame *src_frame, uint8_t ***out_data,
int *out_samples, int *out_size);
Expand Down
44 changes: 30 additions & 14 deletions c_src/xav/decoder.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "decoder.h"
#include "utils.h"

static int init_converter(struct Decoder *decoder);

int decoder_init(struct Decoder *decoder, const char *codec) {
decoder->swr_ctx = NULL;
decoder->converter = NULL;
decoder->out_data = NULL;

if (strcmp(codec, "opus") == 0) {
Expand Down Expand Up @@ -31,19 +34,6 @@ int decoder_init(struct Decoder *decoder, const char *codec) {
return -1;
}

if (decoder->media_type == AVMEDIA_TYPE_AUDIO) {
AVChannelLayout out_chlayout = decoder->c->ch_layout;
int out_sample_rate = decoder->c->sample_rate;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_FLT;

int ret = converter_init(&decoder->converter, decoder->c->ch_layout, decoder->c->sample_rate,
decoder->c->sample_fmt, out_chlayout, out_sample_rate, out_sample_fmt);

if (ret < 0) {
return ret;
}
}

return 0;
}

Expand Down Expand Up @@ -74,7 +64,15 @@ int decoder_decode(struct Decoder *decoder, AVPacket *pkt, AVFrame *frame) {
decoder->frame_linesize = frame->linesize;
}
} else if (decoder->media_type == AVMEDIA_TYPE_AUDIO) {
return converter_convert(&decoder->converter, frame, &decoder->out_data, &decoder->out_samples,

if (decoder->converter == NULL) {
ret = init_converter(decoder);
if (ret < 0) {
return ret;
}
}

return converter_convert(decoder->converter, frame, &decoder->out_data, &decoder->out_samples,
&decoder->out_size);
}

Expand All @@ -89,4 +87,22 @@ void decoder_free(struct Decoder *decoder) {
if (decoder->c != NULL) {
avcodec_free_context(&decoder->c);
}
}

static int init_converter(struct Decoder *decoder) {
decoder->converter = (struct Converter *)calloc(1, sizeof(struct Converter));
int out_sample_rate = decoder->c->sample_rate;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_FLT;

struct ChannelLayout in_chlayout, out_chlayout;
#if LIBAVUTIL_VERSION_MAJOR >= 58
in_chlayout.layout = decoder->c->ch_layout;
out_chlayout.layout = decoder->c->ch_layout;
#else
in_chlayout.layout = decoder->c->channel_layout;
out_chlayout.layout = decoder->c->channel_layout;
#endif

return converter_init(decoder->converter, in_chlayout, decoder->c->sample_rate,
decoder->c->sample_fmt, out_chlayout, out_sample_rate, out_sample_fmt);
}
2 changes: 1 addition & 1 deletion c_src/xav/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Decoder {
uint8_t **frame_data;
int *frame_linesize;

struct Converter converter;
struct Converter *converter;
// Buffer where audio samples are written after conversion.
// We always convet to packed format, so only out_data[0] is set.
uint8_t **out_data;
Expand Down
12 changes: 10 additions & 2 deletions c_src/xav/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ int reader_init(struct Reader *reader, unsigned char *path, size_t path_size, in
}

if (reader->media_type == AVMEDIA_TYPE_AUDIO) {
AVChannelLayout out_chlayout = AV_CHANNEL_LAYOUT_MONO;
int out_sample_rate = 16000;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_FLT;

int ret = converter_init(&reader->converter, reader->c->ch_layout, reader->c->sample_rate,
struct ChannelLayout in_chlayout, out_chlayout;
#if LIBAVUTIL_VERSION_MAJOR >= 58
in_chlayout.layout = reader->c->ch_layout;
av_channel_layout_from_mask(&out_chlayout.layout, AV_CH_LAYOUT_MONO);
#else
in_chlayout.layout = reader->c->channel_layout;
out_chlayout.layout = AV_CH_LAYOUT_MONO;
#endif

int ret = converter_init(&reader->converter, in_chlayout, reader->c->sample_rate,
reader->c->sample_fmt, out_chlayout, out_sample_rate, out_sample_fmt);

if (ret < 0) {
Expand Down
2 changes: 1 addition & 1 deletion c_src/xav/xav_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ ERL_NIF_TERM decode(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
decoder->frame_linesize, "rgb");

} else if (decoder->media_type == AVMEDIA_TYPE_AUDIO) {
const char *out_format = av_get_sample_fmt_name(decoder->converter.out_sample_fmt);
const char *out_format = av_get_sample_fmt_name(decoder->converter->out_sample_fmt);

frame_term = xav_nif_audio_frame_to_term(env, decoder->out_data, decoder->out_samples,
decoder->out_size, out_format, frame->pts);
Expand Down

0 comments on commit a720a54

Please sign in to comment.