Skip to content

Commit

Permalink
Bring back support for FFmpeg 5.x and 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Jul 27, 2024
1 parent f609bfe commit 679934f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ See an interview with FFmpeg enthusiast: https://youtu.be/9kaIXkImCAM

## Installation

Make sure you have installed FFMpeg (ver. 7.0) development packages on your system
Make sure you have installed FFMpeg (ver. 5.x - 7.x) development packages on your system
(see [here](INSTALL.md) for installation one-liners) and add Xav to the list of your dependencies:

```elixir
Expand Down
14 changes: 14 additions & 0 deletions c_src/xav/reader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "reader.h"
#include "utils.h"
#include <libavutil/version.h>

int reader_init(struct Reader *reader, char *path, size_t path_size, int device_flag,
enum AVMediaType media_type) {
Expand Down Expand Up @@ -70,8 +71,15 @@ int reader_init(struct Reader *reader, char *path, size_t path_size, int device_
if (reader->media_type == AVMEDIA_TYPE_AUDIO) {
reader->swr_ctx = swr_alloc();
enum AVSampleFormat out_sample_fmt = av_get_alt_sample_fmt(reader->c->sample_fmt, 0);

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

av_opt_set_int(reader->swr_ctx, "in_sample_rate", reader->c->sample_rate, 0);
av_opt_set_int(reader->swr_ctx, "out_sample_rate", reader->c->sample_rate, 0);
av_opt_set_sample_fmt(reader->swr_ctx, "in_sample_fmt", reader->c->sample_fmt, 0);
Expand Down Expand Up @@ -202,8 +210,14 @@ int reader_next_frame(struct Reader *reader) {
reader->frame_linesize = reader->frame->linesize;
} else if (reader->media_type == AVMEDIA_TYPE_AUDIO &&
av_sample_fmt_is_planar(reader->frame->format) == 1) {

// convert to interleaved
#if LIBAVUTIL_VERSION_MAJOR >= 58
int channels = reader->frame->ch_layout.nb_channels;
#else
int channels = reader->frame->channels;
#endif

int samples_per_channel = reader->frame->nb_samples;

// reader->frame_data = (uint8_t**)malloc(sizeof(uint8_t *));
Expand Down
18 changes: 18 additions & 0 deletions c_src/xav/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ void print_supported_pix_fmts(AVCodec *codec) {
int init_swr_ctx_from_frame(SwrContext **swr_ctx, AVFrame *frame) {
*swr_ctx = swr_alloc();
enum AVSampleFormat out_sample_fmt = av_get_alt_sample_fmt(frame->format, 0);

#if LIBAVUTIL_VERSION_MAJOR >= 58
av_opt_set_chlayout(*swr_ctx, "in_chlayout", &frame->ch_layout, 0);
av_opt_set_chlayout(*swr_ctx, "out_chlayout", &frame->ch_layout, 0);
#else
av_opt_set_channel_layout(*swr_ctx, "in_channel_layout", frame->channel_layout, 0);
av_opt_set_channel_layout(*swr_ctx, "out_channel_layout", frame->channel_layout, 0);
#endif

av_opt_set_int(*swr_ctx, "in_sample_rate", frame->sample_rate, 0);
av_opt_set_int(*swr_ctx, "out_sample_rate", frame->sample_rate, 0);
av_opt_set_sample_fmt(*swr_ctx, "in_sample_fmt", frame->format, 0);
Expand All @@ -37,7 +44,12 @@ void convert_to_rgb(AVFrame *src_frame, uint8_t *dst_data[], int dst_linesize[])

int convert_to_interleaved(SwrContext *swr_ctx, AVFrame *src_frame, uint8_t **dst_data,
int *dst_linesize) {
#if LIBAVUTIL_VERSION_MAJOR >= 58
int channels = src_frame->ch_layout.nb_channels;
#else
int channels = src_frame->channels;
#endif

int samples_per_channel = src_frame->nb_samples;

int ret =
Expand Down Expand Up @@ -75,8 +87,14 @@ ERL_NIF_TERM xav_nif_audio_frame_to_term(ErlNifEnv *env, AVFrame *frame, unsigne
char *format_name) {
ERL_NIF_TERM data_term;

#if LIBAVUTIL_VERSION_MAJOR >= 58
size_t unpadded_linesize =
frame->nb_samples * av_get_bytes_per_sample(frame->format) * frame->ch_layout.nb_channels;
#else
size_t unpadded_linesize =
frame->nb_samples * av_get_bytes_per_sample(frame->format) * frame->channels;
#endif

unsigned char *ptr = enif_make_new_binary(env, unpadded_linesize, &data_term);
memcpy(ptr, data[0], unpadded_linesize);

Expand Down

0 comments on commit 679934f

Please sign in to comment.