aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2013-04-22 13:51:30 +0200
committerAnton Khirnov <anton@khirnov.net>2013-11-04 11:02:12 +0100
commit8914dc15aaa9fef13ee1eb8da1aa74bd1b7d4911 (patch)
tree0195b1db502fc359938500d00d8f91c04ec93177
parentb86d226edd4ca4d16a311482d87020e72d0d354c (diff)
libav decoder plugin: allocate LibavDecContext on stack.
-rw-r--r--src/decoder/libav_decoder_plugin.c69
1 files changed, 33 insertions, 36 deletions
diff --git a/src/decoder/libav_decoder_plugin.c b/src/decoder/libav_decoder_plugin.c
index 006d4514..c6535150 100644
--- a/src/decoder/libav_decoder_plugin.c
+++ b/src/decoder/libav_decoder_plugin.c
@@ -107,23 +107,16 @@ mpd_libav_stream_seek(void *opaque, int64_t pos, int whence)
return stream->input->offset;
}
-static LibavDecContext *
-mpd_libav_stream_open(struct decoder *decoder, struct input_stream *input)
+static int mpd_libav_stream_open(LibavDecContext *s, struct input_stream *input)
{
- LibavDecContext *stream = g_new(LibavDecContext, 1);
- stream->decoder = decoder;
- stream->input = input;
- stream->io = avio_alloc_context(stream->buffer, sizeof(stream->buffer),
- false, stream,
- mpd_libav_stream_read, NULL,
- input->seekable
- ? mpd_libav_stream_seek : NULL);
- if (stream->io == NULL) {
- g_free(stream);
- return NULL;
- }
+ s->input = input;
+ s->io = avio_alloc_context(s->buffer, sizeof(s->buffer), 0,
+ s, mpd_libav_stream_read, NULL,
+ input->seekable ? mpd_libav_stream_seek : NULL);
+ if (!s->io)
+ return AVERROR(ENOMEM);
- return stream;
+ return 0;
}
/**
@@ -149,7 +142,6 @@ static void
mpd_libav_stream_close(LibavDecContext *stream)
{
av_free(stream->io);
- g_free(stream);
}
static bool
@@ -328,29 +320,32 @@ libav_probe(struct decoder *decoder, struct input_stream *is)
return format;
}
-static void
-libav_decode(struct decoder *decoder, struct input_stream *input)
+static void libav_decode(struct decoder *decoder, struct input_stream *input)
{
- AVInputFormat *input_format = libav_probe(decoder, input);
+ LibavDecContext s = { .decoder = decoder };
+ AVInputFormat *input_format;
+ int ret;
+
+ input_format = libav_probe(decoder, input);
if (input_format == NULL)
return;
g_debug("detected input format '%s' (%s)",
input_format->name, input_format->long_name);
- LibavDecContext *stream =
- mpd_libav_stream_open(decoder, input);
- if (stream == NULL) {
+ s.decoder = decoder;
+ ret = mpd_libav_stream_open(&s, input);
+ if (ret < 0) {
g_warning("Failed to open stream");
return;
}
//ffmpeg works with ours "fileops" helper
AVFormatContext *format_context = NULL;
- if (mpd_libav_open_input(&format_context, stream->io, input->uri,
+ if (mpd_libav_open_input(&format_context, s.io, input->uri,
input_format) != 0) {
g_warning("Open failed\n");
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return;
}
@@ -359,7 +354,7 @@ libav_decode(struct decoder *decoder, struct input_stream *input)
if (find_result < 0) {
g_warning("Couldn't find stream info\n");
avformat_close_input(&format_context);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return;
}
@@ -367,7 +362,7 @@ libav_decode(struct decoder *decoder, struct input_stream *input)
if (audio_stream == -1) {
g_warning("No audio stream inside\n");
avformat_close_input(&format_context);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return;
}
@@ -382,7 +377,7 @@ libav_decode(struct decoder *decoder, struct input_stream *input)
if (!codec) {
g_warning("Unsupported audio codec\n");
avformat_close_input(&format_context);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return;
}
@@ -395,7 +390,7 @@ libav_decode(struct decoder *decoder, struct input_stream *input)
g_warning("%s", error->message);
g_error_free(error);
avformat_close_input(&format_context);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return;
}
@@ -408,7 +403,7 @@ libav_decode(struct decoder *decoder, struct input_stream *input)
if (open_result < 0) {
g_warning("Could not open codec\n");
avformat_close_input(&format_context);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return;
}
@@ -452,7 +447,7 @@ libav_decode(struct decoder *decoder, struct input_stream *input)
avcodec_close(codec_context);
avformat_close_input(&format_context);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
}
//no tag reading in ffmpeg, check if playable
@@ -460,18 +455,20 @@ static bool
libav_scan_stream(struct input_stream *is,
const struct tag_handler *handler, void *handler_ctx)
{
+ LibavDecContext s = { NULL };
+ int ret;
AVInputFormat *input_format = libav_probe(NULL, is);
if (input_format == NULL)
return false;
- LibavDecContext *stream = mpd_libav_stream_open(NULL, is);
- if (stream == NULL)
+ ret = mpd_libav_stream_open(&s, is);
+ if (ret < 0)
return false;
AVFormatContext *f = NULL;
- if (mpd_libav_open_input(&f, stream->io, is->uri,
+ if (mpd_libav_open_input(&f, s.io, is->uri,
input_format) != 0) {
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return false;
}
@@ -479,7 +476,7 @@ libav_scan_stream(struct input_stream *is,
avformat_find_stream_info(f, NULL);
if (find_result < 0) {
avformat_close_input(&f);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return false;
}
@@ -495,7 +492,7 @@ libav_scan_stream(struct input_stream *is,
handler, handler_ctx);
avformat_close_input(&f);
- mpd_libav_stream_close(stream);
+ mpd_libav_stream_close(&s);
return true;
}