-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoDecoder.cpp
More file actions
139 lines (118 loc) · 3.78 KB
/
Copy pathVideoDecoder.cpp
File metadata and controls
139 lines (118 loc) · 3.78 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "VideoDecoder.hpp"
#include <assert.h>
VideoDecoder::VideoDecoder(std::string inpath)
{
mInpath = inpath;
}
VideoDecoder::~VideoDecoder()
{
if (av_format_ctx)
avformat_free_context(av_format_ctx);
if (av_codec_ctx)
avcodec_free_context(&av_codec_ctx);
if (curr_frame)
av_frame_free(&curr_frame);
if (curr_packet)
av_packet_free(&curr_packet);
sws_freeContext(sw_context);
}
bool VideoDecoder::init()
{
av_format_ctx = avformat_alloc_context();
if (avformat_open_input(&av_format_ctx, mInpath.c_str(), NULL, NULL) != 0)
{
avformat_free_context(av_format_ctx);
mLogger.logError("Failed opening input file");
return false;
}
if (av_format_ctx->nb_streams == 0)
{
avformat_free_context(av_format_ctx);
mLogger.logError("No streams in input file");
return false;
}
AVCodecParameters *avcodecparams;
const AVCodec *avcodec;
for (unsigned int i = 0; i < av_format_ctx->nb_streams; i++)
{
auto stream = av_format_ctx->streams[i];
avcodecparams = av_format_ctx->streams[i]->codecpar;
avcodec = avcodec_find_decoder(avcodecparams->codec_id);
if (!avcodec)
continue;
if (avcodecparams->codec_type == AVMEDIA_TYPE_VIDEO)
{
mLogger.logInfo("Found video stream");
setStreamInfo(stream);
info.streamIndex = i;
break;
}
}
av_codec_ctx = avcodec_alloc_context3(avcodec);
avcodec_parameters_to_context(av_codec_ctx, avcodecparams);
avcodec_open2(av_codec_ctx, avcodec, NULL);
return true;
}
void VideoDecoder::setTargetFrameSize(int w, int h)
{
targetw = w;
targeth = h;
}
bool VideoDecoder::decode(uint8_t *buffer)
{
curr_frame = av_frame_alloc();
curr_packet = av_packet_alloc();
int result;
int cont;
while (true)
{
cont = av_read_frame(av_format_ctx, curr_packet);
if (cont < 0)
return false;
if (curr_packet->stream_index != info.streamIndex)
{
av_packet_unref(curr_packet);
continue;
}
avcodec_send_packet(av_codec_ctx, curr_packet);
av_packet_unref(curr_packet);
// after we send packets pixel format is available so now we can create swscontext
if (!sw_context)
sw_context = sws_getContext(info.videoWidth, info.videoHeight, this->av_codec_ctx->pix_fmt,
targetw, targeth, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);
result = avcodec_receive_frame(av_codec_ctx, curr_frame);
if (result == 0)
{
// tmp buffers
uint8_t data1[targetw * targeth / 2];
uint8_t data2[targetw * targeth / 2];
uint8_t *dstdata[4] = {buffer, data1, data2, NULL};
int dstlinesize[4] = {targetw, targetw / 2, targetw / 2, 0};
sws_scale(sw_context, curr_frame->data, curr_frame->linesize, 0, curr_frame->height, dstdata, dstlinesize);
av_frame_unref(curr_frame);
return true;
}
if (result == AVERROR(EOF) || result == AVERROR(EAGAIN))
continue;
else if (result != 0)
return false;
}
}
StreamInfo VideoDecoder::getStreamInfo()
{
return info;
}
void VideoDecoder::setStreamInfo(AVStream *stream)
{
float duration =
stream->duration *
(float)stream->time_base.num /
(float)stream->time_base.den;
float frameRate = (float)stream->r_frame_rate.num / (float)stream->r_frame_rate.den;
info = {
.duration = duration,
.framerate = frameRate,
.frames = stream->nb_frames,
.videoWidth = stream->codecpar->width,
.videoHeight = stream->codecpar->height};
}