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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#include "director.h"
#include <dirent.h>
#include <iostream>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
namespace ffmpeg {
class format {
public:
format(std::string videoPath)
{
if (avformat_open_input(&ptr_, videoPath.c_str(), nullptr, nullptr))
{
throw ffmpeg_error("Could not open video " + videoPath);
}
if (avformat_find_stream_info(ptr_, nullptr))
{
throw ffmpeg_error("Could not read stream");
}
}
~format()
{
avformat_close_input(&ptr_);
}
format(const format& other) = delete;
AVFormatContext* ptr()
{
return ptr_;
}
private:
AVFormatContext* ptr_ = nullptr;
};
class codec {
public:
codec(
format& fmt,
AVStream* st)
{
AVCodec* dec = avcodec_find_decoder(st->codecpar->codec_id);
if (!dec)
{
throw ffmpeg_error("Failed to find codec");
}
ptr_ = avcodec_alloc_context3(dec);
if (!ptr_)
{
throw ffmpeg_error("Failed to allocate codec context");
}
if (avcodec_parameters_to_context(ptr_, st->codecpar) < 0)
{
throw ffmpeg_error("Failed to copy codec parameters to decoder");
}
// Init the decoders, with or without reference counting
AVDictionary* opts = nullptr;
av_dict_set(&opts, "refcounted_frames", "0", 0);
if (avcodec_open2(ptr_, dec, &opts) < 0)
{
throw ffmpeg_error("Failed to open codec");
}
}
codec(const codec& other) = delete;
~codec()
{
avcodec_free_context(&ptr_);
}
int getWidth() const
{
return ptr_->width;
}
int getHeight() const
{
return ptr_->height;
}
enum AVPixelFormat getPixelFormat() const
{
return ptr_->pix_fmt;
}
AVCodecContext* ptr()
{
return ptr_;
}
private:
AVCodecContext* ptr_ = nullptr;
};
class packet {
public:
packet()
{
ptr_ = av_packet_alloc();
}
~packet()
{
av_packet_free(&ptr_);
}
packet(const packet& other) = delete;
int getStreamIndex() const
{
return ptr_->stream_index;
}
AVPacket* ptr()
{
return ptr_;
}
int getFlags() const
{
return ptr_->flags;
}
private:
AVPacket* ptr_ = nullptr;
};
class frame {
public:
frame()
{
ptr_ = av_frame_alloc();
}
~frame()
{
av_frame_free(&ptr_);
}
frame(const frame& other) = delete;
uint8_t** getData()
{
return ptr_->data;
}
int* getLinesize()
{
return ptr_->linesize;
}
AVFrame* ptr()
{
return ptr_;
}
private:
AVFrame* ptr_ = nullptr;
};
class sws {
public:
sws(
int srcW,
int srcH,
enum AVPixelFormat srcFormat,
int dstW,
int dstH,
enum AVPixelFormat dstFormat,
int flags,
SwsFilter* srcFilter,
SwsFilter* dstFilter,
const double* param)
{
ptr_ = sws_getContext(
srcW,
srcH,
srcFormat,
dstW,
dstH,
dstFormat,
flags,
srcFilter,
dstFilter,
param);
if (ptr_ == NULL)
{
throw ffmpeg_error("Could not allocate sws context");
}
}
~sws()
{
sws_freeContext(ptr_);
}
sws(const sws& other) = delete;
void scale(
const uint8_t* const srcSlice[],
const int srcStride[],
int srcSliceY,
int srcSliceH,
uint8_t* const dst[],
const int dstStride[])
{
sws_scale(
ptr_,
srcSlice,
srcStride,
srcSliceY,
srcSliceH,
dst,
dstStride);
}
private:
SwsContext* ptr_;
};
}
director::director(std::string videosPath) : videosPath_(videosPath)
{
DIR* videodir;
struct dirent* ent;
if ((videodir = opendir(videosPath.c_str())) == nullptr)
{
throw std::invalid_argument("Couldn't find videos");
}
while ((ent = readdir(videodir)) != nullptr)
{
std::string dname(ent->d_name);
if (dname.find(".mp4") != std::string::npos)
{
videos_.push_back(dname);
}
}
closedir(videodir);
}
Magick::Image director::generate(std::mt19937& rng) const
{
std::uniform_int_distribution<size_t> videoDist(0, videos_.size() - 1);
std::string video = videosPath_ + "/" + videos_[videoDist(rng)];
ffmpeg::format fmt(video);
int streamIdx =
av_find_best_stream(fmt.ptr(), AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (streamIdx < 0)
{
throw ffmpeg_error("Could not find stream");
}
AVStream* stream = fmt.ptr()->streams[streamIdx];
ffmpeg::codec cdc(fmt, stream);
size_t codecw = cdc.getWidth();
size_t codech = cdc.getHeight();
std::uniform_int_distribution<int64_t> frameDist(0, stream->duration - 1);
int64_t seek = frameDist(rng);
std::cout << seek << std::endl;
if (av_seek_frame(fmt.ptr(), streamIdx, seek, 0))
{
throw ffmpeg_error("Could not seek");
}
ffmpeg::frame frame;
ffmpeg::frame converted;
av_image_alloc(
converted.getData(),
converted.getLinesize(),
codecw,
codech,
AV_PIX_FMT_RGB24,
1);
ffmpeg::packet pkt;
do
{
if (av_read_frame(fmt.ptr(), pkt.ptr()) < 0)
{
throw ffmpeg_error("Could not read frame");
}
} while ((pkt.getStreamIndex() != streamIdx)
|| !(pkt.getFlags() & AV_PKT_FLAG_KEY));
int ret;
do {
if (avcodec_send_packet(cdc.ptr(), pkt.ptr()) < 0)
{
throw ffmpeg_error("Could not send packet");
}
ret = avcodec_receive_frame(cdc.ptr(), frame.ptr());
} while (ret == AVERROR(EAGAIN));
if (ret < 0)
{
throw ffmpeg_error("Could not decode frame");
}
ffmpeg::sws scaler(
cdc.getWidth(),
cdc.getHeight(),
cdc.getPixelFormat(),
cdc.getWidth(),
cdc.getHeight(),
AV_PIX_FMT_RGB24,
0, nullptr, nullptr, 0);
scaler.scale(
frame.getData(),
frame.getLinesize(),
0,
codech,
converted.getData(),
converted.getLinesize());
size_t buffer_size = av_image_get_buffer_size(
AV_PIX_FMT_RGB24, cdc.getWidth(), cdc.getHeight(), 1);
std::vector<uint8_t> buffer(buffer_size);
av_image_copy_to_buffer(
buffer.data(),
buffer_size,
converted.getData(),
converted.getLinesize(),
AV_PIX_FMT_RGB24,
cdc.getWidth(),
cdc.getHeight(),
1);
size_t width = 1024;
size_t height = codech * width / codecw;
Magick::Image image;
image.read(codecw, codech, "RGB", Magick::CharPixel, buffer.data());
image.zoom(Magick::Geometry(width, height));
return image;
}
|