Skip to content

Commit e1a7988

Browse files
committed
Move decoder initialization to decoder.c
1 parent 3cbb541 commit e1a7988

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ FFMPEG_LIB_DIR = ffmpeg_build/lib
1313
# uncomment to compile with debug logs
1414
# XAV_DEBUG_LOGS = -DXAV_DEBUG=1
1515

16+
HEADERS = $(XAV_DIR)/reader.h $(XAV_DIR)/decoder.h $(XAV_DIR)/utils.h
17+
SOURCES = $(XAV_DIR)/xav_nif.c $(XAV_DIR)/reader.c $(XAV_DIR)/decoder.c $(XAV_DIR)/utils.c
18+
1619
ifeq ($(USE_BUNDLED_FFMPEG), true)
1720
CFLAGS = -fPIC -I$(ERTS_INCLUDE_DIR) -I${FFMPEG_INCLUDE_DIR} -I${XAV_DIR} -shared $(XAV_DEBUG_LOGS)
1821
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)
2124
LDFLAGS = -lavcodec -lswscale -lavutil -lavformat -lavdevice -lswresample
2225
endif
2326

24-
$(XAV_SO): Makefile $(XAV_DIR)/xav_nif.c $(XAV_DIR)/reader.h $(XAV_DIR)/reader.c $(XAV_DIR)/utils.h $(XAV_DIR)/utils.c
27+
$(XAV_SO): Makefile $(SOURCES) $(HEADERS)
2528
mkdir -p $(PRIV_DIR)
26-
$(CC) $(CFLAGS) $(XAV_DIR)/xav_nif.c $(XAV_DIR)/reader.c $(XAV_DIR)/utils.c -o $(XAV_SO) $(LDFLAGS)
29+
$(CC) $(CFLAGS) $(SOURCES) -o $(XAV_SO) $(LDFLAGS)
2730

2831
format:
2932
clang-format -i $(XAV_DIR)/*

c_src/xav/decoder.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "decoder.h"
2+
3+
int decoder_init(struct Decoder *decoder, const char *codec) {
4+
decoder->swr_ctx = NULL;
5+
6+
if (strcmp(codec, "opus") == 0) {
7+
decoder->media_type = AVMEDIA_TYPE_AUDIO;
8+
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
9+
// we will initialize out_format_name with the first frame
10+
decoder->out_format_name = NULL;
11+
} else if (strcmp(codec, "vp8") == 0) {
12+
decoder->media_type = AVMEDIA_TYPE_VIDEO;
13+
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_VP8);
14+
decoder->out_format_name = "rgb";
15+
} else {
16+
return -1;
17+
}
18+
19+
if (!decoder->codec) {
20+
return -1;
21+
}
22+
23+
decoder->c = avcodec_alloc_context3(decoder->codec);
24+
if (!decoder->c) {
25+
return -1;
26+
}
27+
28+
if (avcodec_open2(decoder->c, decoder->codec, NULL) < 0) {
29+
return -1;
30+
}
31+
32+
return 0;
33+
}
34+
35+
int decoder_decode(struct Decoder *decoder, AVPacket *pkt) {
36+
return 0;
37+
}

c_src/xav/decoder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <libavcodec/avcodec.h>
2+
#include <libswresample/swresample.h>
23

34
struct Decoder {
45
enum AVMediaType media_type;
@@ -14,3 +15,7 @@ struct Decoder {
1415
uint8_t **frame_data;
1516
int *frame_linesize;
1617
};
18+
19+
int decoder_init(struct Decoder *decoder, const char *codec);
20+
21+
int decoder_decode(struct Decoder *decoder, AVPacket *pkt);

c_src/xav/xav_nif.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ ERL_NIF_TERM new_decoder(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
119119
}
120120

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

124123
int codec_len;
125124
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[]) {
132131
return xav_nif_raise(env, "failed_to_get_atom");
133132
}
134133

135-
if (strcmp(codec, "opus") == 0) {
136-
decoder->media_type = AVMEDIA_TYPE_AUDIO;
137-
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
138-
// we will initialize out_format_name with the first frame
139-
decoder->out_format_name = NULL;
140-
} else if (strcmp(codec, "vp8") == 0) {
141-
decoder->media_type = AVMEDIA_TYPE_VIDEO;
142-
decoder->codec = avcodec_find_decoder(AV_CODEC_ID_VP8);
143-
decoder->out_format_name = "rgb";
144-
} else {
145-
return xav_nif_raise(env, "invalid_codec");
146-
}
147-
148-
if (!decoder->codec) {
149-
return xav_nif_raise(env, "decoder_not_found");
150-
}
151-
152-
decoder->c = avcodec_alloc_context3(decoder->codec);
153-
if (!decoder->c) {
154-
return xav_nif_raise(env, "failed_to_alloc_context");
155-
}
156-
157-
if (avcodec_open2(decoder->c, decoder->codec, NULL) < 0) {
158-
return xav_nif_raise(env, "failed_to_open_codec");
134+
if (decoder_init(decoder, codec) != 0) {
135+
return xav_nif_raise(env, "failed_to_init_decoder");
159136
}
160137

161138
ERL_NIF_TERM decoder_term = enif_make_resource(env, decoder);

0 commit comments

Comments
 (0)