AVDecoder.cpp
4.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "AVDecoder.h"
extern "C" {
#include <libswscale/swscale.h>
}
CAVDecoder::CAVDecoder() :
_a_start_time_ms(INT64_MAX),
_v_start_time_ms(INT64_MAX),
_cur_a_ts_ms(INT64_MAX),
_cur_v_ts_ms(INT64_MAX),
_end_time_ms(0),
_cur_a_frame(NULL),
_cur_v_frame(NULL),
_media_role(mr_student),
_scaled_width(0),
_scaled_height(0),
_src_width(0),
_src_height(0),
_sws_ctx(NULL)
{
}
CAVDecoder::~CAVDecoder()
{
free_cur_v_frame();
free_cur_a_frame();
if (_sws_ctx) {
sws_freeContext(_sws_ctx);
_sws_ctx = NULL;
}
}
int CAVDecoder::add(media_info &info)
{
_media_role = info.m_role;
_uid = info.uid;
if (info.m_type == mt_audio) {
_a_start_time_ms = info.start_time_ms;
_a_end_time_ms = info.end_time_ms;
_audio_info.push_back(info);
_audio_decoder.add(info);
}
else {
_v_start_time_ms = info.start_time_ms;
_v_end_time_ms = info.end_time_ms;
_video_info.push_back(info);
_video_decoder.add(info);
}
if (_cur_a_ts_ms == INT64_MAX) {
_cur_a_ts_ms = info.start_time_ms;
_cur_v_ts_ms = info.start_time_ms;
}
if (_end_time_ms < info.end_time_ms) {
_end_time_ms = info.end_time_ms;
}
av_log(NULL, AV_LOG_INFO, "CAVDecoder add info:%lf, %"PRIu64", %"PRIu64", %.3f\n", _cur_a_ts_ms, _cur_v_ts_ms, _end_time_ms, info.duration);
return 0;
}
unsigned int CAVDecoder::getuid()
{
return _uid;
}
bool CAVDecoder::get_one_v_frame()
{
int64_t ts;
int ret = -1;
if (_video_info.size()) {
AVFrame * pFrame = NULL;
ret = _video_decoder.get_one_frame(&pFrame, ts);
if (ret == 0) {
if (pFrame){
free_cur_v_frame();
_cur_v_frame = pFrame;
}
_cur_v_ts_ms += VFRAME_DURATION_MS;
}
else {
_video_info.pop_front();
if (_cur_v_ts_ms < _end_time_ms) {
_cur_v_ts_ms += VFRAME_DURATION_MS;//return last v frame
ret = 0;
}
}
}
if (ret) {//no video decoded
if (_cur_v_ts_ms < _end_time_ms) {//should have as video frame
_cur_v_ts_ms += VFRAME_DURATION_MS;//return last v frame
ret = 0;
}
}
return ret == 0;
}
std::string CAVDecoder::get_cur_vfile()
{
if (_video_info.size()){
return _video_info.front().name;
}
else {
return "";
}
}
std::string CAVDecoder::get_cur_afile()
{
if (_audio_info.size()){
return _audio_info.front().name;
}
else {
return "";
}
}
void CAVDecoder::free_cur_a_frame()
{
if (_cur_a_frame) {
av_frame_free(&_cur_a_frame);
}
}
int CAVDecoder::scale_frame(AVFrame * pFrame, AVFrame * pScaledFrame, int scaled_width, int scaled_height)
{
if (pFrame->width == _src_width && pFrame->height == _src_height && scaled_width == _scaled_width && scaled_height == _scaled_height) {
return sws_scale(_sws_ctx, pFrame->data, pFrame->linesize, 0, pFrame->height,
pScaledFrame->data, pScaledFrame->linesize);
}
else {
if (_sws_ctx) {
sws_freeContext(_sws_ctx);
_sws_ctx = NULL;
}
_sws_ctx = sws_getContext(pFrame->width, pFrame->height, AV_PIX_FMT_YUV420P,
scaled_width, scaled_height, AV_PIX_FMT_YUV420P, SWS_BILINEAR,
NULL, NULL, NULL);
if (!_sws_ctx) {
return -1;
}
_src_width = pFrame->width;
_src_height = pFrame->height;
_scaled_width = scaled_width;
_scaled_height = scaled_height;
return sws_scale(_sws_ctx, pFrame->data, pFrame->linesize, 0, pFrame->height,
pScaledFrame->data, pScaledFrame->linesize);
}
}
void CAVDecoder::free_cur_v_frame()
{
if (_cur_v_frame) {
av_frame_free(&_cur_v_frame);
}
}
bool CAVDecoder::get_one_a_frame()
{
int64_t ts;
int ret = -1;
free_cur_a_frame();
if (_audio_info.size()) {
ret = _audio_decoder.get_one_frame(&_cur_a_frame, ts);
if (ret == 0) {
_cur_a_ts_ms += AFRAME_DURATION_MS;
}
else {
_audio_info.pop_front();
if (_cur_a_ts_ms < _end_time_ms) {
_cur_a_ts_ms += AFRAME_DURATION_MS;//return silence frame
ret = 0;
}
}
}
if (ret) {//no video decoded
if (_cur_a_ts_ms < _end_time_ms) {//should have a audio frame
_cur_a_ts_ms += AFRAME_DURATION_MS;//return last a frame
ret = 0;
}
}
return ret == 0;
}