Skip to content

Commit

Permalink
Move decoder initialization to decoder.c
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Jan 20, 2024
1 parent 3cbb541 commit e1a7988
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ FFMPEG_LIB_DIR = ffmpeg_build/lib
# uncomment to compile with debug logs
# XAV_DEBUG_LOGS = -DXAV_DEBUG=1

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

ifeq ($(USE_BUNDLED_FFMPEG), true)
CFLAGS = -fPIC -I$(ERTS_INCLUDE_DIR) -I${FFMPEG_INCLUDE_DIR} -I${XAV_DIR} -shared $(XAV_DEBUG_LOGS)
LDFLAGS = -L$(FFMPEG_LIB_DIR) -Wl,--whole-archive -lavcodec -lswscale -lavutil -lavformat -Wl,--no-whole-archive
Expand All @@ -21,9 +24,9 @@ CFLAGS = -fPIC -I$(ERTS_INCLUDE_DIR) -I${XAV_DIR} -shared $(XAV_DEBUG_LOGS)
LDFLAGS = -lavcodec -lswscale -lavutil -lavformat -lavdevice -lswresample
endif

$(XAV_SO): Makefile $(XAV_DIR)/xav_nif.c $(XAV_DIR)/reader.h $(XAV_DIR)/reader.c $(XAV_DIR)/utils.h $(XAV_DIR)/utils.c
$(XAV_SO): Makefile $(SOURCES) $(HEADERS)
mkdir -p $(PRIV_DIR)
$(CC) $(CFLAGS) $(XAV_DIR)/xav_nif.c $(XAV_DIR)/reader.c $(XAV_DIR)/utils.c -o $(XAV_SO) $(LDFLAGS)
$(CC) $(CFLAGS) $(SOURCES) -o $(XAV_SO) $(LDFLAGS)

format:
clang-format -i $(XAV_DIR)/*
Expand Down
37 changes: 37 additions & 0 deletions c_src/xav/decoder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "decoder.h"

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

if (strcmp(codec, "opus") == 0) {
decoder->media_type = AVMEDIA_TYPE_AUDIO;
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
// we will initialize out_format_name with the first frame
decoder->out_format_name = NULL;
} else if (strcmp(codec, "vp8") == 0) {
decoder->media_type = AVMEDIA_TYPE_VIDEO;
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_VP8);
decoder->out_format_name = "rgb";
} else {
return -1;
}

if (!decoder->codec) {
return -1;
}

decoder->c = avcodec_alloc_context3(decoder->codec);
if (!decoder->c) {
return -1;
}

if (avcodec_open2(decoder->c, decoder->codec, NULL) < 0) {
return -1;
}

return 0;
}

int decoder_decode(struct Decoder *decoder, AVPacket *pkt) {
return 0;
}
5 changes: 5 additions & 0 deletions c_src/xav/decoder.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>

struct Decoder {
enum AVMediaType media_type;
Expand All @@ -14,3 +15,7 @@ struct Decoder {
uint8_t **frame_data;
int *frame_linesize;
};

int decoder_init(struct Decoder *decoder, const char *codec);

int decoder_decode(struct Decoder *decoder, AVPacket *pkt);
27 changes: 2 additions & 25 deletions c_src/xav/xav_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ ERL_NIF_TERM new_decoder(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
}

struct Decoder *decoder = enif_alloc_resource(decoder_resource_type, sizeof(struct Decoder));
decoder->swr_ctx = NULL;

int codec_len;
if (!enif_get_atom_length(env, argv[0], &codec_len, ERL_NIF_UTF8)) {
Expand All @@ -132,30 +131,8 @@ ERL_NIF_TERM new_decoder(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
return xav_nif_raise(env, "failed_to_get_atom");
}

if (strcmp(codec, "opus") == 0) {
decoder->media_type = AVMEDIA_TYPE_AUDIO;
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
// we will initialize out_format_name with the first frame
decoder->out_format_name = NULL;
} else if (strcmp(codec, "vp8") == 0) {
decoder->media_type = AVMEDIA_TYPE_VIDEO;
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_VP8);
decoder->out_format_name = "rgb";
} else {
return xav_nif_raise(env, "invalid_codec");
}

if (!decoder->codec) {
return xav_nif_raise(env, "decoder_not_found");
}

decoder->c = avcodec_alloc_context3(decoder->codec);
if (!decoder->c) {
return xav_nif_raise(env, "failed_to_alloc_context");
}

if (avcodec_open2(decoder->c, decoder->codec, NULL) < 0) {
return xav_nif_raise(env, "failed_to_open_codec");
if (decoder_init(decoder, codec) != 0) {
return xav_nif_raise(env, "failed_to_init_decoder");
}

ERL_NIF_TERM decoder_term = enif_make_resource(env, decoder);
Expand Down

0 comments on commit e1a7988

Please sign in to comment.