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
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2021-02-24 02:59:43 +08:00
|
|
|
decoder_init(struct decoder *decoder, struct video_buffer *vb) {
|
2019-03-02 23:43:43 +08:00
|
|
|
decoder->video_buffer = vb;
|
2018-02-16 05:56:32 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
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
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
ret = avcodec_receive_frame(decoder->codec_ctx,
|
2021-02-20 04:16:57 +08:00
|
|
|
decoder->video_buffer->producer_frame);
|
2019-03-02 23:43:43 +08:00
|
|
|
if (!ret) {
|
|
|
|
// a frame was received
|
2021-02-24 02:59:43 +08:00
|
|
|
video_buffer_producer_offer_frame(decoder->video_buffer);
|
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
|
|
|
}
|