blob: ca887b5d72907f897e3d9af5ca3dc3f334f750c4 [file] [log] [blame]
Mike Reedc0ee21f2019-05-28 16:11:03 -04001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Klein4b432fa2019-06-06 11:44:05 -05008#include "experimental/ffmpeg/SkVideoEncoder.h"
9#include "include/core/SkCanvas.h"
Mike Reedf755bc72019-09-20 11:37:36 -040010#include "include/core/SkGraphics.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050011#include "include/core/SkStream.h"
12#include "include/core/SkSurface.h"
13#include "include/core/SkTime.h"
Mike Reedc0ee21f2019-05-28 16:11:03 -040014#include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050015#include "modules/skresources/include/SkResources.h"
Mike Reed5093e232019-05-30 10:10:50 -040016#include "src/utils/SkOSPath.h"
Mike Reed5f12eaa2019-09-24 12:01:58 -040017
Mike Reedc0ee21f2019-05-28 16:11:03 -040018#include "tools/flags/CommandLineFlags.h"
Mike Reed5f12eaa2019-09-24 12:01:58 -040019#include "tools/gpu/GrContextFactory.h"
20
21#include "include/gpu/GrContextOptions.h"
Mike Reedc0ee21f2019-05-28 16:11:03 -040022
23static DEFINE_string2(input, i, "", "skottie animation to render");
24static DEFINE_string2(output, o, "", "mp4 file to create");
Mike Reed5093e232019-05-30 10:10:50 -040025static DEFINE_string2(assetPath, a, "", "path to assets needed for json file");
Mike Reedc0ee21f2019-05-28 16:11:03 -040026static DEFINE_int_2(fps, f, 25, "fps");
27static DEFINE_bool2(verbose, v, false, "verbose mode");
Mike Reed5093e232019-05-30 10:10:50 -040028static DEFINE_bool2(loop, l, false, "loop mode for profiling");
Mike Reed2d241f52019-09-16 15:14:21 -040029static DEFINE_int(set_dst_width, 0, "set destination width (height will be computed)");
Mike Reed5f12eaa2019-09-24 12:01:58 -040030static DEFINE_bool2(gpu, g, false, "use GPU for rendering");
Mike Reed4b203ad2019-06-17 17:45:01 -040031
Florin Malitadd5ba942019-11-24 15:42:24 -050032static void produce_frame(SkSurface* surf, skottie::Animation* anim, double frame) {
33 anim->seekFrame(frame);
Mike Reed4b203ad2019-06-17 17:45:01 -040034 surf->getCanvas()->clear(SK_ColorWHITE);
35 anim->render(surf->getCanvas());
36}
37
Mike Reed5f12eaa2019-09-24 12:01:58 -040038struct AsyncRec {
39 SkImageInfo info;
40 SkVideoEncoder* encoder;
41};
42
Mike Reedc0ee21f2019-05-28 16:11:03 -040043int main(int argc, char** argv) {
Mike Reedf755bc72019-09-20 11:37:36 -040044 SkGraphics::Init();
45
Mike Reedc0ee21f2019-05-28 16:11:03 -040046 CommandLineFlags::SetUsage("Converts skottie to a mp4");
47 CommandLineFlags::Parse(argc, argv);
48
Mike Reed5093e232019-05-30 10:10:50 -040049 if (FLAGS_input.count() == 0) {
50 SkDebugf("-i input_file.json argument required\n");
Mike Reedc0ee21f2019-05-28 16:11:03 -040051 return -1;
52 }
53
Mike Reed5f12eaa2019-09-24 12:01:58 -040054 auto contextType = sk_gpu_test::GrContextFactory::kGL_ContextType;
55 GrContextOptions grCtxOptions;
56 sk_gpu_test::GrContextFactory factory(grCtxOptions);
57
Mike Reed5093e232019-05-30 10:10:50 -040058 SkString assetPath;
59 if (FLAGS_assetPath.count() > 0) {
60 assetPath.set(FLAGS_assetPath[0]);
61 } else {
62 assetPath = SkOSPath::Dirname(FLAGS_input[0]);
63 }
64 SkDebugf("assetPath %s\n", assetPath.c_str());
65
66 auto animation = skottie::Animation::Builder()
Brian Osman849f4d62019-11-26 08:58:26 -050067 .setResourceProvider(skresources::FileResourceProvider::Make(assetPath))
Mike Reed5093e232019-05-30 10:10:50 -040068 .makeFromFile(FLAGS_input[0]);
69 if (!animation) {
70 SkDebugf("failed to load %s\n", FLAGS_input[0]);
71 return -1;
72 }
73
Mike Reedc0ee21f2019-05-28 16:11:03 -040074 SkISize dim = animation->size().toRound();
75 double duration = animation->duration();
Florin Malitadd5ba942019-11-24 15:42:24 -050076 int fps = SkTPin(FLAGS_fps, 1, 240);
77 double fps_scale = animation->fps() / fps;
Mike Reedc0ee21f2019-05-28 16:11:03 -040078
Mike Reed2d241f52019-09-16 15:14:21 -040079 float scale = 1;
80 if (FLAGS_set_dst_width > 0) {
81 scale = FLAGS_set_dst_width / (float)dim.width();
82 dim = { FLAGS_set_dst_width, SkScalarRoundToInt(scale * dim.height()) };
83 }
84
Mike Reed4b203ad2019-06-17 17:45:01 -040085 const int frames = SkScalarRoundToInt(duration * fps);
86 const double frame_duration = 1.0 / fps;
87
Mike Reedc0ee21f2019-05-28 16:11:03 -040088 if (FLAGS_verbose) {
Mike Reed2d241f52019-09-16 15:14:21 -040089 SkDebugf("Size %dx%d duration %g, fps %d, frame_duration %g\n",
Mike Reed4b203ad2019-06-17 17:45:01 -040090 dim.width(), dim.height(), duration, fps, frame_duration);
Mike Reedc0ee21f2019-05-28 16:11:03 -040091 }
92
Mike Reedc0ee21f2019-05-28 16:11:03 -040093 SkVideoEncoder encoder;
Mike Reedc0ee21f2019-05-28 16:11:03 -040094
Mike Reed5f12eaa2019-09-24 12:01:58 -040095 GrContext* context = nullptr;
96 sk_sp<SkSurface> surf;
Mike Reed4b203ad2019-06-17 17:45:01 -040097 sk_sp<SkData> data;
98
Florin Malita10e52572019-10-09 16:03:35 -040099 const auto info = SkImageInfo::MakeN32Premul(dim);
Mike Reed4b203ad2019-06-17 17:45:01 -0400100 do {
101 double loop_start = SkTime::GetSecs();
102
Florin Malita10e52572019-10-09 16:03:35 -0400103 if (!encoder.beginRecording(dim, fps)) {
104 SkDEBUGF("Invalid video stream configuration.\n");
105 return -1;
106 }
Mike Reed5f12eaa2019-09-24 12:01:58 -0400107
Mike Reed4b203ad2019-06-17 17:45:01 -0400108 // lazily allocate the surfaces
109 if (!surf) {
Mike Reed5f12eaa2019-09-24 12:01:58 -0400110 if (FLAGS_gpu) {
111 context = factory.getContextInfo(contextType).grContext();
112 surf = SkSurface::MakeRenderTarget(context,
113 SkBudgeted::kNo,
114 info,
115 0,
116 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
117 nullptr);
118 if (!surf) {
119 context = nullptr;
120 }
121 }
122 if (!surf) {
123 surf = SkSurface::MakeRaster(info);
124 }
125 surf->getCanvas()->scale(scale, scale);
Mike Reed4b203ad2019-06-17 17:45:01 -0400126 }
127
Mike Reedc0ee21f2019-05-28 16:11:03 -0400128 for (int i = 0; i <= frames; ++i) {
Florin Malitadd5ba942019-11-24 15:42:24 -0500129 const double frame = i * fps_scale;
Mike Reed4b203ad2019-06-17 17:45:01 -0400130 if (FLAGS_verbose) {
Florin Malitadd5ba942019-11-24 15:42:24 -0500131 SkDebugf("rendering frame %g\n", frame);
Mike Reed4b203ad2019-06-17 17:45:01 -0400132 }
133
Florin Malitadd5ba942019-11-24 15:42:24 -0500134 produce_frame(surf.get(), animation.get(), frame);
Mike Reed4b203ad2019-06-17 17:45:01 -0400135
Mike Reed5f12eaa2019-09-24 12:01:58 -0400136 AsyncRec asyncRec = { info, &encoder };
137 if (context) {
Florin Malitaf22dda92019-10-28 18:47:32 -0400138 auto read_pixels_cb = [](SkSurface::ReadPixelsContext ctx,
139 std::unique_ptr<const SkSurface::AsyncReadResult> result) {
140 if (result && result->count() == 1) {
141 AsyncRec* rec = reinterpret_cast<AsyncRec*>(ctx);
142 rec->encoder->addFrame({rec->info, result->data(0), result->rowBytes(0)});
143 }
144 };
Mike Reed5f12eaa2019-09-24 12:01:58 -0400145 surf->asyncRescaleAndReadPixels(info, {0, 0, info.width(), info.height()},
Florin Malitaf22dda92019-10-28 18:47:32 -0400146 SkSurface::RescaleGamma::kSrc,
147 kNone_SkFilterQuality,
148 read_pixels_cb, &asyncRec);
Mike Reed5f12eaa2019-09-24 12:01:58 -0400149 } else {
150 SkPixmap pm;
151 SkAssertResult(surf->peekPixels(&pm));
152 encoder.addFrame(pm);
153 }
Mike Reedc0ee21f2019-05-28 16:11:03 -0400154 }
Mike Reed4b203ad2019-06-17 17:45:01 -0400155 data = encoder.endRecording();
156
157 if (FLAGS_loop) {
158 double loop_dur = SkTime::GetSecs() - loop_start;
159 SkDebugf("recording secs %g, frames %d, recording fps %d\n",
160 loop_dur, frames, (int)(frames / loop_dur));
Mike Reed5093e232019-05-30 10:10:50 -0400161 }
Mike Reed4b203ad2019-06-17 17:45:01 -0400162 } while (FLAGS_loop);
Mike Reedc0ee21f2019-05-28 16:11:03 -0400163
Mike Reed5093e232019-05-30 10:10:50 -0400164 if (FLAGS_output.count() == 0) {
165 SkDebugf("missing -o output_file.mp4 argument\n");
166 return 0;
167 }
168
Mike Reedc0ee21f2019-05-28 16:11:03 -0400169 SkFILEWStream ostream(FLAGS_output[0]);
170 if (!ostream.isValid()) {
171 SkDebugf("Can't create output file %s\n", FLAGS_output[0]);
172 return -1;
173 }
174 ostream.write(data->data(), data->size());
Mike Reed5093e232019-05-30 10:10:50 -0400175 return 0;
Mike Reedc0ee21f2019-05-28 16:11:03 -0400176}