From e1a798828d0e9276adfdfd9dfd9f12998ccc48b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=9Aled=C5=BA?= Date: Sat, 20 Jan 2024 16:28:53 +0100 Subject: [PATCH] Move decoder initialization to decoder.c --- Makefile | 7 +++++-- c_src/xav/decoder.c | 37 +++++++++++++++++++++++++++++++++++++ c_src/xav/decoder.h | 5 +++++ c_src/xav/xav_nif.c | 27 ++------------------------- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index cd93ed1..fdab0e2 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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)/* diff --git a/c_src/xav/decoder.c b/c_src/xav/decoder.c index e69de29..c186811 100644 --- a/c_src/xav/decoder.c +++ b/c_src/xav/decoder.c @@ -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; +} \ No newline at end of file diff --git a/c_src/xav/decoder.h b/c_src/xav/decoder.h index 39c78a7..11856ee 100644 --- a/c_src/xav/decoder.h +++ b/c_src/xav/decoder.h @@ -1,4 +1,5 @@ #include +#include struct Decoder { enum AVMediaType media_type; @@ -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); \ No newline at end of file diff --git a/c_src/xav/xav_nif.c b/c_src/xav/xav_nif.c index c71856c..49779df 100644 --- a/c_src/xav/xav_nif.c +++ b/c_src/xav/xav_nif.c @@ -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)) { @@ -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);