-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathvideo_decoder.cpp
56 lines (48 loc) · 1.35 KB
/
video_decoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "video_decoder.h"
#include "ffmpeg.h"
VideoDecoder::VideoDecoder(AVCodecParameters* codec_parameters) {
const auto codec = avcodec_find_decoder(codec_parameters->codec_id);
if (!codec) {
throw ffmpeg::Error{"Unsupported video codec"};
}
codec_context_ = avcodec_alloc_context3(codec);
if (!codec_context_) {
throw ffmpeg::Error{"Couldn't allocate video codec context"};
}
ffmpeg::check(avcodec_parameters_to_context(
codec_context_, codec_parameters));
ffmpeg::check(avcodec_open2(codec_context_, codec, nullptr));
}
VideoDecoder::~VideoDecoder() {
avcodec_free_context(&codec_context_);
}
bool VideoDecoder::send(AVPacket* packet) {
auto ret = avcodec_send_packet(codec_context_, packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return false;
} else {
ffmpeg::check(ret);
return true;
}
}
bool VideoDecoder::receive(AVFrame* frame) {
auto ret = avcodec_receive_frame(codec_context_, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return false;
} else {
ffmpeg::check(ret);
return true;
}
}
unsigned VideoDecoder::width() const {
return codec_context_->width;
}
unsigned VideoDecoder::height() const {
return codec_context_->height;
}
AVPixelFormat VideoDecoder::pixel_format() const {
return codec_context_->pix_fmt;
}
AVRational VideoDecoder::time_base() const {
return codec_context_->time_base;
}