-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_codec.cpp
323 lines (255 loc) · 8.33 KB
/
video_codec.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// video_codec.cpp
// QVideoPlayer
//
// Created by jt on 2023/12/18.
//
#include "video_codec.hpp"
#include <spdlog/spdlog.h>
#include <sys/time.h>
#include <functional>
#include "blocking_queue.h"
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/time.h>
}
static void StaticFrameCallback(AVFramePtr frame) {
VideoCodec& codec = VideoCodec::getInstance();
codec.OnFrame(std::move(frame));
}
VideoCodec::VideoCodec() : fq_(100), afq_(100) {} // 防止屯帧
VideoCodec::~VideoCodec() {}
void VideoCodec::Register(VideoCodecListener* listener) {
listener_ = listener;
}
void VideoCodec::UnRegister(VideoCodecListener* listener) {
if (listener != listener_) {
spdlog::error("listener error");
}
listener_ = nullptr;
}
void VideoCodec::StartCodec(const std::string& file_path) {
stop_requested_ = false;
stream_time_base_ready_ = false;
codec_thread_ = std::thread(&VideoCodec::Codec, this, file_path);
getting_frame_thread_ = std::thread(&VideoCodec::ProcessFrameFromQueue, this);
getting_audio_frame_thread_ =
std::thread(&VideoCodec::ProcessAudioFrameFromQueue, this);
}
void VideoCodec::StopCodec() {
spdlog::info("StopCodec");
stop_requested_ = true;
if (codec_thread_.joinable()) {
codec_thread_.join();
}
stream_time_base_ready_ = true;
std::queue<AVFramePtr> empty_q;
empty_q.push(nullptr);
fq_.replace(empty_q);
afq_.replace(empty_q);
if (getting_frame_thread_.joinable()) {
getting_frame_thread_.join();
}
if (getting_audio_frame_thread_.joinable()) {
getting_audio_frame_thread_.join();
}
spdlog::info("StopCodec success");
}
void VideoCodec::OnFrame(AVFramePtr frame) {
fq_.push(frame);
}
void VideoCodec::OnAudioFrame(AVFramePtr frame) {
afq_.push(frame);
}
void VideoCodec::PauseCodec(bool pause) {
if (pause) {
fq_.lock();
afq_.lock();
} else {
fq_.clear();
afq_.clear();
fq_.unlock();
afq_.unlock();
}
}
void VideoCodec::Codec(const std::string& file_path) {
spdlog::info("start Codec");
const char* video_path = file_path.c_str();
AVFormatContext* pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, video_path, NULL, NULL) != 0) {
printf("avformat_open_input error\n");
return;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("avformat_find_stream_info error\n");
return;
}
int video_stream_index = -1;
int audio_stream_index = -1;
// int audio_stream_index = -1;
for (int i = 0; i < pFormatCtx->nb_streams; ++i) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
}
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_index = i;
}
}
if (video_stream_index == -1) {
spdlog::error("no video found");
listener_->OnMediaError();
return;
}
stream_time_base_ = pFormatCtx->streams[video_stream_index]->time_base;
audio_stream_time_base_ = pFormatCtx->streams[audio_stream_index]->time_base;
stream_time_base_ready_ = true;
const AVCodec* codec = avcodec_find_decoder(
pFormatCtx->streams[video_stream_index]->codecpar->codec_id);
AVCodecContext* pCodecCtx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(
pCodecCtx, pFormatCtx->streams[video_stream_index]->codecpar);
const AVCodec* audio_codec = avcodec_find_decoder(
pFormatCtx->streams[audio_stream_index]->codecpar->codec_id);
AVCodecContext* pAudioCodecCtx = avcodec_alloc_context3(audio_codec);
avcodec_parameters_to_context(
pAudioCodecCtx, pFormatCtx->streams[audio_stream_index]->codecpar);
if (avcodec_open2(pCodecCtx, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
return;
}
if (avcodec_open2(pAudioCodecCtx, audio_codec, NULL) < 0) {
fprintf(stderr, "Could not open audio codec\n");
return;
}
AVPacket pkt;
auto frame = createAVFramePtr();
uint64_t idx = 0;
struct timeval start, end;
gettimeofday(&start, NULL);
while (av_read_frame(pFormatCtx, &pkt) >= 0 && !stop_requested_) {
if (pkt.stream_index == audio_stream_index) {
if (avcodec_send_packet(pAudioCodecCtx, &pkt) == 0) {
int ret = avcodec_receive_frame(pAudioCodecCtx, frame.get());
if (ret == 0) {
auto frame_to_cb = createAVFramePtr();
if (av_frame_copy_props(frame_to_cb.get(), frame.get()) < 0) {
continue;
}
frame_to_cb->format = frame->format;
frame_to_cb->channel_layout = frame->channel_layout;
frame_to_cb->nb_samples = frame->nb_samples;
frame_to_cb->sample_rate = frame->sample_rate;
if (av_frame_get_buffer(frame_to_cb.get(), 32) < 0) {
continue;
}
if (av_frame_copy(frame_to_cb.get(), frame.get()) < 0) {
continue;
}
if (av_frame_copy_props(frame_to_cb.get(), frame.get()) < 0) {
continue;
}
OnAudioFrame(std::move(frame_to_cb));
}
}
} else if (pkt.stream_index == video_stream_index) {
if (avcodec_send_packet(pCodecCtx, &pkt) == 0) {
int ret = avcodec_receive_frame(pCodecCtx, frame.get());
if (ret == 0) {
int width = frame->width;
int height = frame->height;
if (width <= 0 || height <= 0) {
continue;
}
auto frame_to_cb = createAVFramePtr();
if (av_frame_copy_props(frame_to_cb.get(), frame.get()) < 0) {
continue;
}
frame_to_cb->format =
pFormatCtx->streams[video_stream_index]->codecpar->format;
frame_to_cb->width = frame->width;
frame_to_cb->height = frame->height;
int r = av_frame_get_buffer(frame_to_cb.get(), 32);
if (av_frame_copy(frame_to_cb.get(), frame.get()) < 0) {
continue;
}
if (av_frame_copy_props(frame_to_cb.get(), frame.get()) < 0) {
continue;
}
OnFrame(std::move(frame_to_cb)); // call back!
}
}
}
av_packet_unref(&pkt);
}
gettimeofday(&end, NULL);
double elapsedTime =
(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
printf("Elapsed time: %.2f seconds\n", elapsedTime);
// free
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);
}
void VideoCodec::ProcessFrameFromQueue() {
AVFramePtr frame;
while (!stream_time_base_ready_) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
while ((frame = fq_.pop())) {
if (frame->pts != AV_NOPTS_VALUE) {
double time = av_q2d(stream_time_base_) * frame->pts;
if (frame->pts == 0) {
listener_->OnVideoFrame(std::move(frame));
continue;
}
WaitForFrameAudio(time);
listener_->OnVideoFrame(std::move(frame));
}
frame = nullptr;
}
spdlog::info("decode ended");
}
void VideoCodec::ProcessAudioFrameFromQueue() {
AVFramePtr frame;
while (!stream_time_base_ready_) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
while ((frame = afq_.pop())) {
if (frame->pts != AV_NOPTS_VALUE) {
double time = av_q2d(audio_stream_time_base_) * frame->pts;
if (frame->pts == 0) {
listener_->OnAudioFrame(std::move(frame));
continue;
}
WaitForFrame(time);
listener_->OnAudioFrame(std::move(frame));
}
frame = nullptr;
}
spdlog::info("audio decode ended");
}
void VideoCodec::WaitForFrame(double frame_time) {
int64_t frame_time_us = static_cast<int64_t>(frame_time * 1000000.0);
if (is_first_frame_) {
first_frame_time_us_ = av_gettime();
is_first_frame_ = false;
}
int64_t now_us = av_gettime();
frame_time_us += first_frame_time_us_;
if (now_us < frame_time_us) {
int64_t wait_time_us = frame_time_us - now_us;
std::this_thread::sleep_for(std::chrono::microseconds(wait_time_us));
}
}
void VideoCodec::WaitForFrameAudio(double frame_time) {
int64_t frame_time_us = static_cast<int64_t>(frame_time * 1000000.0);
if (is_first_audio_frame_) {
first_audio_frame_time_us_ = av_gettime();
is_first_audio_frame_ = false;
}
int64_t now_us = av_gettime();
frame_time_us += first_audio_frame_time_us_;
if (now_us < frame_time_us) {
int64_t wait_time_us = frame_time_us - now_us;
std::this_thread::sleep_for(std::chrono::microseconds(wait_time_us));
}
}