Repository navigation
Expand file tree
/
Copy pathnvdec_source.cpp
More file actions
241 lines (210 loc) · 7.5 KB
/
Copy pathnvdec_source.cpp
File metadata and controls
241 lines (210 loc) · 7.5 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
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
#include "vrt/nvdec_source.h"
#include <cuda.h>
#include <cstdlib>
#include <stdexcept>
#include <utility>
#include "vrt/cuda_check.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_cuda.h>
#include <libavutil/pixdesc.h>
}
namespace vrt {
namespace {
std::string av_error(int err) {
char buf[AV_ERROR_MAX_STRING_SIZE] = {};
av_strerror(err, buf, sizeof buf);
return buf;
}
void check(int err, const std::string& what) {
if (err < 0) throw std::runtime_error(what + ": " + av_error(err));
}
AVPixelFormat pick_cuda_format(AVCodecContext*, const AVPixelFormat* formats) {
for (const AVPixelFormat* f = formats; *f != AV_PIX_FMT_NONE; ++f)
if (*f == AV_PIX_FMT_CUDA) return *f;
return AV_PIX_FMT_NONE; // decoder can't use NVDEC for this stream: fail loudly
}
YuvMatrix matrix_for(const AVFrame* f) {
switch (f->colorspace) {
case AVCOL_SPC_BT709:
return YuvMatrix::kBt709;
case AVCOL_SPC_BT470BG:
case AVCOL_SPC_SMPTE170M:
return YuvMatrix::kBt601;
default:
return f->height >= 720 ? YuvMatrix::kBt709 : YuvMatrix::kBt601; // same rule as the CPU path
}
}
} // namespace
GpuFrame::~GpuFrame() { release(); }
GpuFrame::GpuFrame(GpuFrame&& o) noexcept { *this = std::move(o); }
GpuFrame& GpuFrame::operator=(GpuFrame&& o) noexcept {
if (this != &o) {
release();
luma = o.luma;
chroma = o.chroma;
luma_pitch = o.luma_pitch;
chroma_pitch = o.chroma_pitch;
width = o.width;
height = o.height;
matrix = o.matrix;
full_range = o.full_range;
frame_ = std::exchange(o.frame_, nullptr);
}
return *this;
}
void GpuFrame::release() {
if (frame_) av_frame_free(&frame_);
luma = chroma = nullptr;
}
struct NvdecSource::Impl {
AVFormatContext* fmt = nullptr;
AVCodecContext* dec = nullptr;
AVBufferRef* device = nullptr;
AVPacket* pkt = nullptr;
int stream = -1;
bool draining = false;
bool pending = false; // pkt holds a packet the decoder refused (EAGAIN)
~Impl() {
av_packet_free(&pkt);
avcodec_free_context(&dec);
av_buffer_unref(&device);
avformat_close_input(&fmt);
}
};
NvdecSource::NvdecSource(const std::string& path, cudaStream_t stream, int in_flight)
: impl_(std::make_unique<Impl>()) {
Impl& s = *impl_;
// CUDA device context that uses the runtime's primary context and our stream.
VRT_CUDA_CHECK(cudaFree(nullptr)); // make sure the runtime has made the primary context current
CUcontext ctx = nullptr;
if (cuCtxGetCurrent(&ctx) != CUDA_SUCCESS || !ctx)
throw std::runtime_error("no current CUDA context");
s.device = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
if (!s.device) throw std::runtime_error("av_hwdevice_ctx_alloc failed");
auto* cuda = static_cast<AVCUDADeviceContext*>(
reinterpret_cast<AVHWDeviceContext*>(s.device->data)->hwctx);
cuda->cuda_ctx = ctx;
cuda->stream = stream;
check(av_hwdevice_ctx_init(s.device), "init CUDA device context");
check(avformat_open_input(&s.fmt, path.c_str(), nullptr, nullptr), "open " + path);
check(avformat_find_stream_info(s.fmt, nullptr), "find stream info");
const AVCodec* codec = nullptr;
s.stream = av_find_best_stream(s.fmt, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
check(s.stream, "no video stream in " + path);
const AVStream* st = s.fmt->streams[s.stream];
// VRT_DECODER=cuvid uses FFmpeg's <codec>_cuvid decoder (NVIDIA's parser and its own surface
// queue) instead of the native decoder with the nvdec hwaccel.
if (const char* which = std::getenv("VRT_DECODER"); which && std::string(which) == "cuvid") {
const AVCodec* cuvid =
avcodec_find_decoder_by_name((std::string(codec->name) + "_cuvid").c_str());
if (!cuvid) throw std::runtime_error(std::string("no ") + codec->name + "_cuvid decoder");
codec = cuvid;
}
s.dec = avcodec_alloc_context3(codec);
if (!s.dec) throw std::runtime_error("avcodec_alloc_context3 failed");
check(avcodec_parameters_to_context(s.dec, st->codecpar), "codec parameters");
s.dec->hw_device_ctx = av_buffer_ref(s.device);
s.dec->get_format = pick_cuda_format;
s.dec->extra_hw_frames = in_flight + 1;
check(avcodec_open2(s.dec, codec, nullptr), "open decoder with NVDEC");
s.pkt = av_packet_alloc();
if (!s.pkt) throw std::runtime_error("av_packet_alloc failed");
info_.codec = std::string(codec->name) + " (nvdec)";
info_.width = st->codecpar->width;
info_.height = st->codecpar->height;
info_.fps = st->avg_frame_rate.den ? av_q2d(st->avg_frame_rate) : 0.0;
info_.frames = st->nb_frames;
const char* cs = av_color_space_name(st->codecpar->color_space);
const char* cr = av_color_range_name(st->codecpar->color_range);
info_.color_space = cs ? cs : "unknown";
info_.color_range = cr ? cr : "unknown";
}
NvdecSource::~NvdecSource() = default;
bool NvdecSource::send() {
Impl& s = *impl_;
if (s.draining) return false;
while (true) {
if (!s.pending) {
const int rd = av_read_frame(s.fmt, s.pkt);
if (rd == AVERROR_EOF) {
s.draining = true;
check(avcodec_send_packet(s.dec, nullptr), "flush decoder");
return true;
}
check(rd, "av_read_frame");
if (s.pkt->stream_index != s.stream) {
av_packet_unref(s.pkt);
continue;
}
}
const int r = avcodec_send_packet(s.dec, s.pkt);
if (r == AVERROR(EAGAIN)) {
s.pending = true; // decoder full: keep the packet, the caller must receive() first
return true;
}
check(r, "send packet");
s.pending = false;
av_packet_unref(s.pkt);
return true;
}
}
NvdecSource::Recv NvdecSource::receive(GpuFrame& out) {
Impl& s = *impl_;
AVFrame* frame = av_frame_alloc();
if (!frame) throw std::runtime_error("av_frame_alloc failed");
const int r = avcodec_receive_frame(s.dec, frame);
if (r != 0) {
av_frame_free(&frame);
if (r == AVERROR(EAGAIN)) return s.draining ? Recv::kEnd : Recv::kNeedInput;
if (r == AVERROR_EOF) return Recv::kEnd;
check(r, "avcodec_receive_frame");
}
if (frame->format != AV_PIX_FMT_CUDA) {
av_frame_free(&frame);
throw std::runtime_error("decoder returned a non-CUDA frame (NVDEC not used)");
}
const auto* frames_ctx = reinterpret_cast<AVHWFramesContext*>(frame->hw_frames_ctx->data);
if (frames_ctx->sw_format != AV_PIX_FMT_NV12) {
const char* name = av_get_pix_fmt_name(frames_ctx->sw_format);
av_frame_free(&frame);
throw std::runtime_error(std::string("expected NV12 surfaces, got ") + (name ? name : "?"));
}
out.release();
out.frame_ = frame;
out.luma = frame->data[0];
out.chroma = frame->data[1];
out.luma_pitch = frame->linesize[0];
out.chroma_pitch = frame->linesize[1];
out.width = frame->width;
out.height = frame->height;
out.matrix = matrix_for(frame);
out.full_range = frame->color_range == AVCOL_RANGE_JPEG;
return Recv::kFrame;
}
void NvdecSource::rewind() {
Impl& s = *impl_;
const AVStream* st = s.fmt->streams[s.stream];
const std::int64_t start = st->start_time == AV_NOPTS_VALUE ? 0 : st->start_time;
check(av_seek_frame(s.fmt, s.stream, start, AVSEEK_FLAG_BACKWARD), "seek to start");
avcodec_flush_buffers(s.dec); // also clears the end-of-stream state after a flush packet
av_packet_unref(s.pkt);
s.pending = false;
s.draining = false;
}
bool NvdecSource::read(GpuFrame& out) {
while (true) {
switch (receive(out)) {
case Recv::kFrame:
return true;
case Recv::kEnd:
return false;
case Recv::kNeedInput:
send();
break;
}
}
}
} // namespace vrt