summaryrefslogtreecommitdiff
path: root/audio/decoder.c
diff options
context:
space:
mode:
authorbbergeron <[email protected]>2024-04-03 17:32:01 -0400
committerbbergeron <[email protected]>2024-04-03 17:32:01 -0400
commitc1cb78d574c0429aa5e3ff3a2b3886e4bc153212 (patch)
treebf68806bcbddcafafc015b28c25550ea457eeecc /audio/decoder.c
Reset Git repo and use a pseudonym to sign commits
I used to sign my commits with my real name and my personal email address, which I wanted scrubbed off the "B." pseudosphere. Re-creating a new git repository was safer than simpler than re-writing the history (although the latter could've also worked but, oh well).
Diffstat (limited to 'audio/decoder.c')
-rw-r--r--audio/decoder.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/audio/decoder.c b/audio/decoder.c
new file mode 100644
index 0000000..70eacd5
--- /dev/null
+++ b/audio/decoder.c
@@ -0,0 +1,58 @@
+#include "decoder.h"
+#include <libavutil/dict.h>
+
+int decoder_init (struct Decoder *decoder, const AVCodecParameters *codecpar, AVDictionary **opts)
+{
+ int err;
+ AVCodecContext *avctx;
+ const AVCodec *codec;
+
+ if ((codec = avcodec_find_decoder(codecpar->codec_id)) == NULL)
+ return AVERROR_DECODER_NOT_FOUND;
+
+ /* NOTE: parameter 'codec' might be redundant here due to the next, more
+ * featuref-full call to avcodec_parameters_to_context. */
+ if ((avctx = avcodec_alloc_context3(codec)) == NULL)
+ return AVERROR(ENOMEM); /* NOTE: After reading libavcodec's code, this
+ * error code *could* misrepresent the actual
+ * error, but ffmpeg won't tell us what it is
+ * anyway so oh well. */
+
+ if ((err = avcodec_parameters_to_context(avctx, codecpar)) < 0)
+ goto error;
+
+ if ((err = avcodec_open2(avctx, codec, opts)) < 0)
+ goto error;
+
+ /* Free previous context (no-op if avctx == NULL) and swap it with the new
+ * one. This allows the previous one to be left untouched in case of error,
+ * which is generally a nice thing to do in your code. */
+ avcodec_free_context(&decoder->avctx);
+ decoder->avctx = avctx;
+ return 0;
+
+error:
+ avcodec_free_context(&avctx);
+ return err;
+}
+
+int decoder_init_for_stream (struct Decoder *decoder, const AVStream *stream, AVDictionary **opts)
+{
+ return decoder_init(decoder, stream->codecpar, opts);
+}
+
+int decoder_send (struct Decoder *decoder, const AVPacket *pkt)
+{
+ return avcodec_send_packet(decoder->avctx, pkt);
+}
+
+int decoder_convert (struct Decoder *decoder, AVFrame *out)
+{
+ return avcodec_receive_frame(decoder->avctx, out);
+}
+
+void decoder_free (struct Decoder *decoder)
+{
+ if (decoder == NULL) return;
+ avcodec_free_context(&decoder->avctx);
+}