/
j_ffmpeg_av_decoder.h
245 lines (206 loc) · 6.9 KB
/
j_ffmpeg_av_decoder.h
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
//
// Created by user on 11/7/23.
//
#ifndef FFMPEG_VIDEO_PLAYER_J_FFMPEG_AV_DECODER_H
#define FFMPEG_VIDEO_PLAYER_J_FFMPEG_AV_DECODER_H
#include "j_video_player/ffmpeg_utils/ffmpeg_codec.h"
#include "j_video_player/ffmpeg_utils/ffmpeg_demuxer.h"
#include "j_video_player/ffmpeg_utils/ffmpeg_headers.h"
#include "j_video_player/modules/j_i_audio_decoder.h"
#include "j_video_player/modules/j_i_video_decoder.h"
#include "j_video_player/utils/scope_guard.h"
namespace j_video_player {
template <int mediaType = AVMEDIA_TYPE_VIDEO>
class FFmpegAVDecoder : public IVideoDecoder, public IAudioDecoder {
public:
~FFmpegAVDecoder() override { cleanUp(); }
int open(const std::string &file_path) override {
int ret = initDemuxer(file_path);
RETURN_IF_ERROR(ret);
ret = initCodec();
RETURN_IF_ERROR(ret);
return ret;
}
bool isValid() override {
if (demux_ == nullptr || codec_ == nullptr) {
return false;
}
return demux_->isValid() && codec_->isValid();
}
void close() override { cleanUp(); }
std::shared_ptr<Frame> decodeNextFrame() override {
if (!isValid()) {
LOGE("decoder is invalid");
return nullptr;
}
for (;;) {
int ret = 0;
// there may remains frames, let's try to get frame from codec first,
{
auto video_frame = std::make_shared<Frame>(time_base_);
ret = codec_->receiveFrame(video_frame->f);
if (ret == 0) {
updatePosition(video_frame);
return video_frame;
}
}
AVPacket *pkt = nullptr;
std::tie(ret, pkt) = demux_->readPacket();
ON_SCOPE_EXIT([&pkt] { av_packet_unref(pkt); });
if (pkt == nullptr) {
return nullptr;
}
if (ret != 0) {
LOGE("readPacket failed: %s\n", av_err2str(ret));
return nullptr;
}
// skip this packet if is not target stream
if (stream_index_ != pkt->stream_index) {
continue;
}
ret = codec_->sendPacketToCodec(pkt);
if (ret == AVERROR_EOF) {
LOGE("sendPacketToCodec AVERROR_EOF\n");
return nullptr;
}
auto video_frame = std::make_shared<Frame>(time_base_);
ret = codec_->receiveFrame(video_frame->f);
if (ret == 0) {
updatePosition(video_frame);
return video_frame;
} else if (ret == AVERROR(EAGAIN)) {
continue;
} else {
LOGE("codec_->receiveFrame failed");
return nullptr;
}
}
}
std::shared_ptr<Frame> seekFrameQuick(int64_t timestamp) override {
if (!isValid()) {
LOGE("seek failed");
return nullptr;
}
auto ret = seekDemuxerAndFlushCodecBuffer(timestamp, position_);
if (ret != 0) {
return nullptr;
}
return decodeNextFrame();
}
std::shared_ptr<Frame> seekFramePrecise(int64_t timestamp) override {
if (!isValid()) {
LOGE("seek failed");
return nullptr;
}
auto ret = seekDemuxerAndFlushCodecBuffer(timestamp, position_);
if (ret != 0) {
return nullptr;
}
for (;;) {
auto frame = decodeNextFrame();
if (frame == nullptr) {
return nullptr;
}
auto frame_pts = av_rescale_q(frame->f->pts, time_base_, AV_TIME_BASE_Q);
if (frame_pts >= timestamp) {
return frame;
}
}
}
int64_t getPosition() override { return position_; }
MediaFileInfo getMediaFileInfo() override {
if(!isValid()) {
LOGE("getMediaFileInfo failed, decoder is invalid");
return {};
}
MediaFileInfo info;
info.file_path = demux_->getFormatContext()->url;
info.width = codec_->getCodecContext()->width;
info.height = codec_->getCodecContext()->height;
info.duration = demux_->getFormatContext()->duration;
info.bit_rate = demux_->getFormatContext()->bit_rate;
auto video_index = demux_->getVideoStreamIndex();
auto *video_stream = demux_->getStream(video_index);
if (video_stream != nullptr) {
info.fps = av_q2d(video_stream->avg_frame_rate);
info.pixel_format = video_stream->codecpar->format;
info.video_stream_timebase = video_stream->time_base;
}
auto audio_index = demux_->getAudioStreamIndex();
auto *audio_stream = demux_->getStream(audio_index);
if (audio_stream != nullptr) {
info.sample_rate = audio_stream->codecpar->sample_rate;
info.channels = audio_stream->codecpar->channels;
info.sample_format = audio_stream->codecpar->format;
info.channel_layout = audio_stream->codecpar->channel_layout;
info.audio_stream_timebase = audio_stream->time_base;
}
return info;
}
private:
int initDemuxer(const std::string &file_path) {
demux_ = std::make_unique<ffmpeg_utils::FFmpegDmuxer>();
int ret = demux_->openFile(file_path);
RETURN_IF_ERROR_LOG(ret, "demux_->openFile failed\n");
return ret;
}
int initCodec() {
codec_ = std::make_unique<ffmpeg_utils::FFmpegCodec>();
if (mediaType == AVMEDIA_TYPE_VIDEO) {
stream_index_ = demux_->getVideoStreamIndex();
} else if (mediaType == AVMEDIA_TYPE_AUDIO) {
stream_index_ = demux_->getAudioStreamIndex();
} else {
abort();
}
time_base_ = demux_->getStream(stream_index_)->time_base;
auto *av_stream = demux_->getStream(stream_index_);
if (av_stream == nullptr) {
printf("av_stream is nullptr\n");
return -1;
}
int ret =
codec_->prepare(av_stream->codecpar->codec_id, av_stream->codecpar);
RETURN_IF_ERROR_LOG(ret, "codec_->prepare failed\n");
return ret;
}
void cleanUp() {
demux_ = nullptr;
codec_ = nullptr;
position_ = AV_NOPTS_VALUE;
}
void updatePosition(const std::shared_ptr<Frame> &frame) {
if (frame->f->pts != AV_NOPTS_VALUE) {
position_ = av_rescale_q(frame->f->pts,
demux_->getStream(stream_index_)->time_base,
AV_TIME_BASE_Q);
}
}
int seekToTargetPosition(int64_t seek_pos, int64_t seek_rel) {
auto seek_flags = 0;
if (seek_rel < 0) {
seek_flags = AVSEEK_FLAG_BACKWARD;
}
auto min_ts = (seek_rel > 0) ? (seek_pos - seek_rel + 2) : (INT64_MIN);
auto max_ts = (seek_rel < 0) ? (seek_pos - seek_rel - 2) : (seek_pos);
return demux_->seek(min_ts, seek_pos, max_ts, seek_flags);
}
int seekDemuxerAndFlushCodecBuffer(int64_t timestamp, int64_t target_pos) {
auto ret = seekToTargetPosition(timestamp, timestamp - target_pos);
if (ret != 0) {
LOGE("seekToTargetPosition failed");
return -1;
}
codec_->flush_buffers();
return 0;
}
std::unique_ptr<ffmpeg_utils::FFmpegDmuxer> demux_{nullptr};
std::unique_ptr<ffmpeg_utils::FFmpegCodec> codec_{nullptr};
int stream_index_{-1};
AVRational time_base_;
std::atomic<int64_t> position_{AV_NOPTS_VALUE};
};
using FFmpegVideoDecoder = FFmpegAVDecoder<AVMEDIA_TYPE_VIDEO>;
using FFmpegAudioDecoder = FFmpegAVDecoder<AVMEDIA_TYPE_AUDIO>;
} // namespace j_video_player
#endif // FFMPEG_VIDEO_PLAYER_J_FFMPEG_AV_DECODER_H