2017-12-12 22:12:07 +08:00
|
|
|
#include "decoder.h"
|
|
|
|
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
|
|
|
#include "events.h"
|
2019-03-02 22:16:55 +08:00
|
|
|
#include "video_buffer.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
/** Downcast packet_sink to decoder */
|
|
|
|
#define DOWNCAST(SINK) container_of(SINK, struct decoder, packet_sink)
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 18:59:31 +08:00
|
|
|
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
2019-03-02 23:43:43 +08:00
|
|
|
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!decoder->codec_ctx) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGC("Could not allocate decoder context");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-03-02 23:43:43 +08:00
|
|
|
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
|
|
|
|
LOGE("Could not open codec");
|
|
|
|
avcodec_free_context(&decoder->codec_ctx);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
decoder->frame = av_frame_alloc();
|
|
|
|
if (!decoder->frame) {
|
|
|
|
LOGE("Could not create decoder frame");
|
|
|
|
avcodec_close(decoder->codec_ctx);
|
|
|
|
avcodec_free_context(&decoder->codec_ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
2018-10-11 13:12:36 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
decoder_close(struct decoder *decoder) {
|
2021-04-11 21:01:05 +08:00
|
|
|
av_frame_free(&decoder->frame);
|
2019-03-02 23:43:43 +08:00
|
|
|
avcodec_close(decoder->codec_ctx);
|
|
|
|
avcodec_free_context(&decoder->codec_ctx);
|
|
|
|
}
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 18:59:31 +08:00
|
|
|
decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
2019-03-02 23:43:43 +08:00
|
|
|
int ret;
|
|
|
|
if ((ret = avcodec_send_packet(decoder->codec_ctx, packet)) < 0) {
|
|
|
|
LOGE("Could not send video packet: %d", ret);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2021-04-11 21:01:05 +08:00
|
|
|
ret = avcodec_receive_frame(decoder->codec_ctx, decoder->frame);
|
2019-03-02 23:43:43 +08:00
|
|
|
if (!ret) {
|
|
|
|
// a frame was received
|
2021-04-11 21:01:05 +08:00
|
|
|
bool ok = video_buffer_push(decoder->video_buffer, decoder->frame);
|
|
|
|
// A frame lost should not make the whole pipeline fail. The error, if
|
|
|
|
// any, is already logged.
|
|
|
|
(void) ok;
|
2019-03-02 23:43:43 +08:00
|
|
|
} else if (ret != AVERROR(EAGAIN)) {
|
|
|
|
LOGE("Could not receive video frame: %d", ret);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-10-11 13:12:36 +08:00
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2021-04-11 21:01:05 +08:00
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
static bool
|
|
|
|
decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
|
|
|
|
struct decoder *decoder = DOWNCAST(sink);
|
|
|
|
return decoder_open(decoder, codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decoder_packet_sink_close(struct sc_packet_sink *sink) {
|
|
|
|
struct decoder *decoder = DOWNCAST(sink);
|
|
|
|
decoder_close(decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
decoder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
|
|
|
|
struct decoder *decoder = DOWNCAST(sink);
|
|
|
|
return decoder_push(decoder, packet);
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
void
|
|
|
|
decoder_init(struct decoder *decoder, struct video_buffer *vb) {
|
|
|
|
decoder->video_buffer = vb;
|
2021-04-11 21:01:05 +08:00
|
|
|
|
|
|
|
static const struct sc_packet_sink_ops ops = {
|
|
|
|
.open = decoder_packet_sink_open,
|
|
|
|
.close = decoder_packet_sink_close,
|
|
|
|
.push = decoder_packet_sink_push,
|
|
|
|
};
|
|
|
|
|
|
|
|
decoder->packet_sink.ops = &ops;
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|