-
Notifications
You must be signed in to change notification settings - Fork 46
/
player.cpp
179 lines (153 loc) · 4.48 KB
/
player.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
#include "player.h"
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/time.h>
#include <libavutil/imgutils.h>
}
const size_t Player::queue_size_{5};
Player::Player(const std::string &file_name) :
demuxer_{std::make_unique<Demuxer>(file_name)},
video_decoder_{std::make_unique<VideoDecoder>(
demuxer_->video_codec_parameters())},
format_converter_{std::make_unique<FormatConverter>(
video_decoder_->width(), video_decoder_->height(),
video_decoder_->pixel_format(), AV_PIX_FMT_YUV420P)},
display_{std::make_unique<Display>(
video_decoder_->width(), video_decoder_->height())},
timer_{std::make_unique<Timer>()},
packet_queue_{std::make_unique<PacketQueue>(queue_size_)},
frame_queue_{std::make_unique<FrameQueue>(queue_size_)} {
}
void Player::operator()() {
stages_.emplace_back(&Player::demultiplex, this);
stages_.emplace_back(&Player::decode_video, this);
video();
for (auto &stage : stages_) {
stage.join();
}
if (exception_) {
std::rethrow_exception(exception_);
}
}
void Player::demultiplex() {
try {
for (;;) {
// Create AVPacket
std::unique_ptr<AVPacket, std::function<void(AVPacket*)>> packet{
av_packet_alloc(),
[](AVPacket* p){ av_packet_free(&p); }};
packet->data = nullptr;
// Read frame into AVPacket
if (!(*demuxer_)(*packet)) {
packet_queue_->finished();
break;
}
// Move into queue if first video stream
if (packet->stream_index == demuxer_->video_stream_index()) {
if (!packet_queue_->push(std::move(packet))) {
break;
}
}
}
} catch (...) {
exception_ = std::current_exception();
frame_queue_->quit();
packet_queue_->quit();
}
}
void Player::decode_video() {
try {
const AVRational microseconds = {1, 1000000};
for (;;) {
// Create AVFrame and AVQueue
std::unique_ptr<AVFrame, std::function<void(AVFrame*)>>
frame_decoded{
av_frame_alloc(), [](AVFrame* f){ av_frame_free(&f); }};
std::unique_ptr<AVPacket, std::function<void(AVPacket*)>> packet{
nullptr, [](AVPacket* p){ av_packet_unref(p); delete p; }};
// Read packet from queue
if (!packet_queue_->pop(packet)) {
frame_queue_->finished();
break;
}
// If the packet didn't send, receive more frames and try again
bool sent = false;
while (!sent) {
sent = video_decoder_->send(packet.get());
// If a whole frame has been decoded,
// adjust time stamps and add to queue
while (video_decoder_->receive(frame_decoded.get())) {
frame_decoded->pts = av_rescale_q(
frame_decoded->pkt_dts,
demuxer_->time_base(),
microseconds);
std::unique_ptr<AVFrame, std::function<void(AVFrame*)>>
frame_converted{
av_frame_alloc(),
[](AVFrame* f){ av_free(f->data[0]); }};
if (av_frame_copy_props(frame_converted.get(),
frame_decoded.get()) < 0) {
throw std::runtime_error("Copying frame properties");
}
if (av_image_alloc(
frame_converted->data, frame_converted->linesize,
video_decoder_->width(), video_decoder_->height(),
video_decoder_->pixel_format(), 1) < 0) {
throw std::runtime_error("Allocating picture");
}
(*format_converter_)(
frame_decoded.get(), frame_converted.get());
if (!frame_queue_->push(std::move(frame_converted))) {
break;
}
}
}
}
} catch (...) {
exception_ = std::current_exception();
frame_queue_->quit();
packet_queue_->quit();
}
}
void Player::video() {
try {
int64_t last_pts = 0;
for (uint64_t frame_number = 0;; ++frame_number) {
display_->input();
if (display_->get_quit()) {
break;
} else if (display_->get_play()) {
std::unique_ptr<AVFrame, std::function<void(AVFrame*)>> frame{
nullptr, [](AVFrame* f){ av_frame_free(&f); }};
if (!frame_queue_->pop(frame)) {
break;
}
if (frame_number) {
const int64_t frame_delay = frame->pts - last_pts;
last_pts = frame->pts;
timer_->wait(frame_delay);
} else {
last_pts = frame->pts;
timer_->update();
}
display_->refresh(
{frame->data[0], frame->data[1], frame->data[2]},
{static_cast<size_t>(frame->linesize[0]),
static_cast<size_t>(frame->linesize[1]),
static_cast<size_t>(frame->linesize[2])});
} else {
std::chrono::milliseconds sleep(10);
std::this_thread::sleep_for(sleep);
timer_->update();
}
}
} catch (...) {
exception_ = std::current_exception();
}
frame_queue_->quit();
packet_queue_->quit();
}