2019-05-28 20:11:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-06-06 16:44:05 +00:00
|
|
|
#include "experimental/ffmpeg/SkVideoEncoder.h"
|
2019-06-03 15:04:20 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-09-20 15:37:36 +00:00
|
|
|
#include "include/core/SkGraphics.h"
|
2019-05-28 20:11:03 +00:00
|
|
|
#include "include/core/SkStream.h"
|
2019-06-06 16:44:05 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-28 20:11:03 +00:00
|
|
|
#include "include/core/SkTime.h"
|
2019-06-06 16:44:05 +00:00
|
|
|
#include "modules/skottie/include/Skottie.h"
|
2019-11-26 13:58:26 +00:00
|
|
|
#include "modules/skresources/include/SkResources.h"
|
2019-05-30 14:10:50 +00:00
|
|
|
#include "src/utils/SkOSPath.h"
|
2019-09-24 16:01:58 +00:00
|
|
|
|
2019-05-28 20:11:03 +00:00
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
2019-09-24 16:01:58 +00:00
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
|
|
|
|
|
|
|
#include "include/gpu/GrContextOptions.h"
|
2019-05-28 20:11:03 +00:00
|
|
|
|
|
|
|
static DEFINE_string2(input, i, "", "skottie animation to render");
|
|
|
|
static DEFINE_string2(output, o, "", "mp4 file to create");
|
2019-05-30 14:10:50 +00:00
|
|
|
static DEFINE_string2(assetPath, a, "", "path to assets needed for json file");
|
2019-05-28 20:11:03 +00:00
|
|
|
static DEFINE_int_2(fps, f, 25, "fps");
|
|
|
|
static DEFINE_bool2(verbose, v, false, "verbose mode");
|
2019-05-30 14:10:50 +00:00
|
|
|
static DEFINE_bool2(loop, l, false, "loop mode for profiling");
|
2019-09-16 19:14:21 +00:00
|
|
|
static DEFINE_int(set_dst_width, 0, "set destination width (height will be computed)");
|
2019-09-24 16:01:58 +00:00
|
|
|
static DEFINE_bool2(gpu, g, false, "use GPU for rendering");
|
2019-06-17 21:45:01 +00:00
|
|
|
|
2019-11-24 20:42:24 +00:00
|
|
|
static void produce_frame(SkSurface* surf, skottie::Animation* anim, double frame) {
|
|
|
|
anim->seekFrame(frame);
|
2019-06-17 21:45:01 +00:00
|
|
|
surf->getCanvas()->clear(SK_ColorWHITE);
|
|
|
|
anim->render(surf->getCanvas());
|
|
|
|
}
|
|
|
|
|
2019-09-24 16:01:58 +00:00
|
|
|
struct AsyncRec {
|
|
|
|
SkImageInfo info;
|
|
|
|
SkVideoEncoder* encoder;
|
|
|
|
};
|
|
|
|
|
2019-05-28 20:11:03 +00:00
|
|
|
int main(int argc, char** argv) {
|
2019-09-20 15:37:36 +00:00
|
|
|
SkGraphics::Init();
|
|
|
|
|
2019-05-28 20:11:03 +00:00
|
|
|
CommandLineFlags::SetUsage("Converts skottie to a mp4");
|
|
|
|
CommandLineFlags::Parse(argc, argv);
|
|
|
|
|
2019-05-30 14:10:50 +00:00
|
|
|
if (FLAGS_input.count() == 0) {
|
|
|
|
SkDebugf("-i input_file.json argument required\n");
|
2019-05-28 20:11:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-05-30 14:10:50 +00:00
|
|
|
|
2019-09-24 16:01:58 +00:00
|
|
|
auto contextType = sk_gpu_test::GrContextFactory::kGL_ContextType;
|
|
|
|
GrContextOptions grCtxOptions;
|
|
|
|
sk_gpu_test::GrContextFactory factory(grCtxOptions);
|
|
|
|
|
2019-05-30 14:10:50 +00:00
|
|
|
SkString assetPath;
|
|
|
|
if (FLAGS_assetPath.count() > 0) {
|
|
|
|
assetPath.set(FLAGS_assetPath[0]);
|
|
|
|
} else {
|
|
|
|
assetPath = SkOSPath::Dirname(FLAGS_input[0]);
|
|
|
|
}
|
|
|
|
SkDebugf("assetPath %s\n", assetPath.c_str());
|
|
|
|
|
|
|
|
auto animation = skottie::Animation::Builder()
|
2019-11-26 13:58:26 +00:00
|
|
|
.setResourceProvider(skresources::FileResourceProvider::Make(assetPath))
|
2019-05-30 14:10:50 +00:00
|
|
|
.makeFromFile(FLAGS_input[0]);
|
|
|
|
if (!animation) {
|
|
|
|
SkDebugf("failed to load %s\n", FLAGS_input[0]);
|
2019-05-28 20:11:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize dim = animation->size().toRound();
|
|
|
|
double duration = animation->duration();
|
2019-11-24 20:42:24 +00:00
|
|
|
int fps = SkTPin(FLAGS_fps, 1, 240);
|
|
|
|
double fps_scale = animation->fps() / fps;
|
2019-05-28 20:11:03 +00:00
|
|
|
|
2019-09-16 19:14:21 +00:00
|
|
|
float scale = 1;
|
|
|
|
if (FLAGS_set_dst_width > 0) {
|
|
|
|
scale = FLAGS_set_dst_width / (float)dim.width();
|
|
|
|
dim = { FLAGS_set_dst_width, SkScalarRoundToInt(scale * dim.height()) };
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:45:01 +00:00
|
|
|
const int frames = SkScalarRoundToInt(duration * fps);
|
|
|
|
const double frame_duration = 1.0 / fps;
|
|
|
|
|
2019-05-28 20:11:03 +00:00
|
|
|
if (FLAGS_verbose) {
|
2019-09-16 19:14:21 +00:00
|
|
|
SkDebugf("Size %dx%d duration %g, fps %d, frame_duration %g\n",
|
2019-06-17 21:45:01 +00:00
|
|
|
dim.width(), dim.height(), duration, fps, frame_duration);
|
2019-05-28 20:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkVideoEncoder encoder;
|
|
|
|
|
2019-09-24 16:01:58 +00:00
|
|
|
GrContext* context = nullptr;
|
|
|
|
sk_sp<SkSurface> surf;
|
2019-06-17 21:45:01 +00:00
|
|
|
sk_sp<SkData> data;
|
2019-05-28 20:11:03 +00:00
|
|
|
|
2019-10-09 20:03:35 +00:00
|
|
|
const auto info = SkImageInfo::MakeN32Premul(dim);
|
2019-06-17 21:45:01 +00:00
|
|
|
do {
|
|
|
|
double loop_start = SkTime::GetSecs();
|
2019-05-28 20:11:03 +00:00
|
|
|
|
2019-10-09 20:03:35 +00:00
|
|
|
if (!encoder.beginRecording(dim, fps)) {
|
|
|
|
SkDEBUGF("Invalid video stream configuration.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-09-24 16:01:58 +00:00
|
|
|
|
2019-06-17 21:45:01 +00:00
|
|
|
// lazily allocate the surfaces
|
|
|
|
if (!surf) {
|
2019-09-24 16:01:58 +00:00
|
|
|
if (FLAGS_gpu) {
|
2020-07-07 03:18:53 +00:00
|
|
|
context = factory.getContextInfo(contextType).directContext();
|
2019-09-24 16:01:58 +00:00
|
|
|
surf = SkSurface::MakeRenderTarget(context,
|
|
|
|
SkBudgeted::kNo,
|
|
|
|
info,
|
|
|
|
0,
|
|
|
|
GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
|
|
|
|
nullptr);
|
|
|
|
if (!surf) {
|
|
|
|
context = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!surf) {
|
|
|
|
surf = SkSurface::MakeRaster(info);
|
|
|
|
}
|
|
|
|
surf->getCanvas()->scale(scale, scale);
|
2019-06-17 21:45:01 +00:00
|
|
|
}
|
2019-06-03 15:04:20 +00:00
|
|
|
|
2019-06-17 21:45:01 +00:00
|
|
|
for (int i = 0; i <= frames; ++i) {
|
2019-11-24 20:42:24 +00:00
|
|
|
const double frame = i * fps_scale;
|
2019-06-17 21:45:01 +00:00
|
|
|
if (FLAGS_verbose) {
|
2019-11-24 20:42:24 +00:00
|
|
|
SkDebugf("rendering frame %g\n", frame);
|
2019-06-17 21:45:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:42:24 +00:00
|
|
|
produce_frame(surf.get(), animation.get(), frame);
|
2019-06-17 21:45:01 +00:00
|
|
|
|
2019-09-24 16:01:58 +00:00
|
|
|
AsyncRec asyncRec = { info, &encoder };
|
|
|
|
if (context) {
|
2019-10-28 22:47:32 +00:00
|
|
|
auto read_pixels_cb = [](SkSurface::ReadPixelsContext ctx,
|
|
|
|
std::unique_ptr<const SkSurface::AsyncReadResult> result) {
|
|
|
|
if (result && result->count() == 1) {
|
|
|
|
AsyncRec* rec = reinterpret_cast<AsyncRec*>(ctx);
|
|
|
|
rec->encoder->addFrame({rec->info, result->data(0), result->rowBytes(0)});
|
|
|
|
}
|
|
|
|
};
|
2019-09-24 16:01:58 +00:00
|
|
|
surf->asyncRescaleAndReadPixels(info, {0, 0, info.width(), info.height()},
|
2019-10-28 22:47:32 +00:00
|
|
|
SkSurface::RescaleGamma::kSrc,
|
|
|
|
kNone_SkFilterQuality,
|
|
|
|
read_pixels_cb, &asyncRec);
|
2020-09-22 12:51:43 +00:00
|
|
|
context->submit();
|
2019-09-24 16:01:58 +00:00
|
|
|
} else {
|
|
|
|
SkPixmap pm;
|
|
|
|
SkAssertResult(surf->peekPixels(&pm));
|
|
|
|
encoder.addFrame(pm);
|
|
|
|
}
|
2019-05-28 20:11:03 +00:00
|
|
|
}
|
2019-06-17 21:45:01 +00:00
|
|
|
data = encoder.endRecording();
|
2019-06-03 15:04:20 +00:00
|
|
|
|
2019-06-17 21:45:01 +00:00
|
|
|
if (FLAGS_loop) {
|
|
|
|
double loop_dur = SkTime::GetSecs() - loop_start;
|
|
|
|
SkDebugf("recording secs %g, frames %d, recording fps %d\n",
|
|
|
|
loop_dur, frames, (int)(frames / loop_dur));
|
|
|
|
}
|
|
|
|
} while (FLAGS_loop);
|
2019-05-28 20:11:03 +00:00
|
|
|
|
2019-05-30 14:10:50 +00:00
|
|
|
if (FLAGS_output.count() == 0) {
|
|
|
|
SkDebugf("missing -o output_file.mp4 argument\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:11:03 +00:00
|
|
|
SkFILEWStream ostream(FLAGS_output[0]);
|
|
|
|
if (!ostream.isValid()) {
|
|
|
|
SkDebugf("Can't create output file %s\n", FLAGS_output[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ostream.write(data->data(), data->size());
|
2019-05-30 14:10:50 +00:00
|
|
|
return 0;
|
2019-05-28 20:11:03 +00:00
|
|
|
}
|