-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathframes.cpp
More file actions
303 lines (265 loc) · 7.75 KB
/
Copy pathframes.cpp
File metadata and controls
303 lines (265 loc) · 7.75 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
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <libspdl/core/frames.h>
#include <libspdl/core/rational_utils.h>
#include <libspdl/core/types.h>
#include <libspdl/core/utils.h>
#include "libspdl/common/tracing.h"
#include "libspdl/core/detail/ffmpeg/compat.h"
#include "libspdl/core/detail/ffmpeg/logging.h"
#include "libspdl/core/detail/ffmpeg/wrappers.h"
#include <algorithm>
#include <cassert>
extern "C" {
#include <libavutil/frame.h>
#include <libavutil/pixdesc.h>
}
#define _IS_AUDIO (media == MediaType::Audio)
#define _IS_VIDEO (media == MediaType::Video)
#define _IS_IMAGE (media == MediaType::Image)
namespace spdl::core {
////////////////////////////////////////////////////////////////////////////////
// FFmpeg Common
////////////////////////////////////////////////////////////////////////////////
template <MediaType media>
Frames<media>::Frames(uintptr_t id, Rational time_base)
: id_(id), time_base_(time_base) {
TRACE_EVENT("decoding", "Frames::Frames", perfetto::Flow::ProcessScoped(id_));
if (time_base_.den == 0) {
SPDL_FAIL(
fmt::format(
"Invalid time base was provided. {}/{}",
time_base_.num,
time_base_.den));
}
}
template <MediaType media>
Frames<media>::Frames(Frames&& other) noexcept {
*this = std::move(other);
}
template <MediaType media>
Frames<media>& Frames<media>::operator=(Frames<media>&& other) noexcept {
using std::swap;
swap(id_, other.id_);
swap(frames_, other.frames_);
return *this;
}
template <MediaType media>
Frames<media>::~Frames() {
TRACE_EVENT(
"decoding", "Frames::~Frames", perfetto::Flow::ProcessScoped(id_));
std::for_each(frames_.begin(), frames_.end(), [](AVFrame* p) {
DEBUG_PRINT_AVFRAME_REFCOUNT(p);
av_frame_unref(p);
av_frame_free(&p);
});
}
template <MediaType media>
uint64_t Frames<media>::get_id() const {
return id_;
}
template <MediaType media>
void Frames<media>::push_back(AVFrame* frame) {
if constexpr (media == MediaType::Image) {
if (frames_.size() > 0) {
SPDL_FAIL_INTERNAL("Attempted to store multiple frames to ImageFrames");
}
}
frames_.push_back(frame);
}
template <MediaType media>
int64_t Frames<media>::get_pts(size_t index) const {
if (auto num_frames = frames_.size(); index >= num_frames) {
throw std::out_of_range(
fmt::format("{} is out of range [0, {})", index, num_frames));
}
return frames_.at(index)->pts;
}
template <MediaType media>
double Frames<media>::get_timestamp(size_t index) const {
return av_q2d(to_rational(get_pts(index), time_base_));
}
////////////////////////////////////////////////////////////////////////////////
// Common
////////////////////////////////////////////////////////////////////////////////
template <MediaType media>
const char* Frames<media>::get_media_format_name() const {
if (frames_.size() == 0) {
return "n/a";
}
return detail::get_media_format_name<media>(frames_[0]->format);
}
template <MediaType media>
OptionDict Frames<media>::get_metadata() const {
if (frames_.size() == 0) {
return {};
}
return detail::parse_dict(frames_[0]->metadata);
}
template <MediaType media>
int Frames<media>::get_num_frames() const {
if constexpr (_IS_AUDIO) {
int ret = 0;
for (auto& f : frames_) {
ret += f->nb_samples;
}
return ret;
} else {
return (int)frames_.size();
}
}
template <MediaType media>
const std::vector<AVFrame*>& Frames<media>::get_frames() const {
return frames_;
}
template <MediaType media>
FramesPtr<media> Frames<media>::clone() const {
auto other = std::make_unique<Frames<media>>(id_, time_base_);
for (const AVFrame* f : frames_) {
other->push_back(CHECK_AVALLOCATE(av_frame_clone(f)));
}
return other;
}
template <MediaType media>
Rational Frames<media>::get_time_base() const {
return time_base_;
}
////////////////////////////////////////////////////////////////////////////////
// FFmpeg - Audio
////////////////////////////////////////////////////////////////////////////////
template <MediaType media>
int Frames<media>::get_sample_rate() const
requires _IS_AUDIO
{
return frames_.size() ? frames_[0]->sample_rate : -1;
}
template <MediaType media>
int Frames<media>::get_num_channels() const
requires _IS_AUDIO
{
return frames_.size() ? GET_NUM_CHANNELS(frames_[0]) : -1;
}
////////////////////////////////////////////////////////////////////////////////
// FFmpeg - Video
////////////////////////////////////////////////////////////////////////////////
template <MediaType media>
int Frames<media>::get_num_planes() const
requires(_IS_IMAGE || _IS_VIDEO)
{
return frames_.size()
? av_pix_fmt_count_planes((AVPixelFormat)frames_[0]->format)
: -1;
}
template <MediaType media>
int Frames<media>::get_width() const
requires(_IS_IMAGE || _IS_VIDEO)
{
return frames_.size() ? frames_[0]->width : -1;
}
template <MediaType media>
int Frames<media>::get_height() const
requires(_IS_IMAGE || _IS_VIDEO)
{
return frames_.size() ? frames_[0]->height : -1;
}
//////////////////////////////////////////////////////////////////////////////
// Video specific
//////////////////////////////////////////////////////////////////////////////
namespace {
int adjust_indices(const int length, int* start, int* stop, int step) {
if (step <= 0) {
SPDL_FAIL(fmt::format("Step must be larget than 0. Found: {}", step));
}
if (*start < 0) {
*start += length;
if (*start < 0) {
*start = (step < 0) ? -1 : 0;
}
} else if (*start >= length) {
*start = (step < 0) ? length - 1 : length;
}
if (*stop < 0) {
*stop += length;
if (*stop < 0) {
*stop = (step < 0) ? -1 : 0;
}
} else if (*stop >= length) {
*stop = (step < 0) ? length - 1 : length;
}
if (step < 0) {
if (*stop < *start) {
return (*start - *stop - 1) / (-step) + 1;
}
} else {
if (*start < *stop) {
return (*stop - *start - 1) / step + 1;
}
}
return 0;
}
} // namespace
template <MediaType media>
VideoFramesPtr Frames<media>::slice(int start, int stop, int step) const
requires _IS_VIDEO
{
const auto numel = (int)frames_.size();
int len = adjust_indices(numel, &start, &stop, step);
auto out = std::make_unique<VideoFrames>(id_, time_base_);
if (!len) {
return out;
}
for (int i = start; i < stop; i += step) {
assert(0 <= i && i < numel);
out->frames_.push_back(detail::make_reference(frames_.at(i)));
}
return out;
}
template <MediaType media>
VideoFramesPtr Frames<media>::slice(const std::vector<int64_t>& index) const
requires _IS_VIDEO
{
const auto numel = (int)frames_.size();
for (const auto& i : index) {
if (i >= numel || i < -numel) {
throw std::out_of_range(
fmt::format("Index {} is outside of [0, {})", i, frames_.size()));
}
}
auto out = std::make_unique<VideoFrames>(id_, time_base_);
if (!index.size()) {
return out;
}
for (auto i : index) {
if (i < 0) {
i += numel;
}
out->frames_.push_back(detail::make_reference(frames_.at(i)));
}
return out;
}
template <MediaType media>
ImageFramesPtr Frames<media>::slice(int64_t i) const
requires _IS_VIDEO
{
const auto numel = (int)frames_.size();
if (i >= numel || i < -numel) {
throw std::out_of_range(
fmt::format("Index {} is outside of [0, {})", i, numel));
}
if (i < 0) {
i += numel;
}
assert(0 <= i && i < numel);
auto out = std::make_unique<Frames<MediaType::Image>>(id_, time_base_);
out->push_back(detail::make_reference(frames_.at(i)));
return out;
}
template class Frames<MediaType::Audio>;
template class Frames<MediaType::Video>;
template class Frames<MediaType::Image>;
} // namespace spdl::core