blob: e12ad3c2caafc5ed008d165f5afc144a6578fd11 [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"
Mike Reed5093e232019-05-30 10:10:50 -040015#include "modules/skottie/utils/SkottieUtils.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
32static void produce_frame(SkSurface* surf, skottie::Animation* anim, double frame_time) {
33 anim->seekFrameTime(frame_time);
34 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()
67 .setResourceProvider(skottie_utils::FileResourceProvider::Make(assetPath))
68 .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();
76 int fps = FLAGS_fps;
77 if (fps < 1) {
78 fps = 1;
79 } else if (fps > 240) {
80 fps = 240;
81 }
82
Mike Reed2d241f52019-09-16 15:14:21 -040083 float scale = 1;
84 if (FLAGS_set_dst_width > 0) {
85 scale = FLAGS_set_dst_width / (float)dim.width();
86 dim = { FLAGS_set_dst_width, SkScalarRoundToInt(scale * dim.height()) };
87 }
88
Mike Reed4b203ad2019-06-17 17:45:01 -040089 const int frames = SkScalarRoundToInt(duration * fps);
90 const double frame_duration = 1.0 / fps;
91
Mike Reedc0ee21f2019-05-28 16:11:03 -040092 if (FLAGS_verbose) {
Mike Reed2d241f52019-09-16 15:14:21 -040093 SkDebugf("Size %dx%d duration %g, fps %d, frame_duration %g\n",
Mike Reed4b203ad2019-06-17 17:45:01 -040094 dim.width(), dim.height(), duration, fps, frame_duration);
Mike Reedc0ee21f2019-05-28 16:11:03 -040095 }
96
Mike Reedc0ee21f2019-05-28 16:11:03 -040097 SkVideoEncoder encoder;
Mike Reedc0ee21f2019-05-28 16:11:03 -040098
Mike Reed5f12eaa2019-09-24 12:01:58 -040099 GrContext* context = nullptr;
100 sk_sp<SkSurface> surf;
Mike Reed4b203ad2019-06-17 17:45:01 -0400101 sk_sp<SkData> data;
102
Florin Malita10e52572019-10-09 16:03:35 -0400103 const auto info = SkImageInfo::MakeN32Premul(dim);
Mike Reed4b203ad2019-06-17 17:45:01 -0400104 do {
105 double loop_start = SkTime::GetSecs();
106
Florin Malita10e52572019-10-09 16:03:35 -0400107 if (!encoder.beginRecording(dim, fps)) {
108 SkDEBUGF("Invalid video stream configuration.\n");
109 return -1;
110 }
Mike Reed5f12eaa2019-09-24 12:01:58 -0400111
Mike Reed4b203ad2019-06-17 17:45:01 -0400112 // lazily allocate the surfaces
113 if (!surf) {
Mike Reed5f12eaa2019-09-24 12:01:58 -0400114 if (FLAGS_gpu) {
115 context = factory.getContextInfo(contextType).grContext();
116 surf = SkSurface::MakeRenderTarget(context,
117 SkBudgeted::kNo,
118 info,
119 0,
120 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
121 nullptr);
122 if (!surf) {
123 context = nullptr;
124 }
125 }
126 if (!surf) {
127 surf = SkSurface::MakeRaster(info);
128 }
129 surf->getCanvas()->scale(scale, scale);
Mike Reed4b203ad2019-06-17 17:45:01 -0400130 }
131
Mike Reedc0ee21f2019-05-28 16:11:03 -0400132 for (int i = 0; i <= frames; ++i) {
Mike Reed4b203ad2019-06-17 17:45:01 -0400133 double ts = i * 1.0 / fps;
134 if (FLAGS_verbose) {
135 SkDebugf("rendering frame %d ts %g\n", i, ts);
136 }
137
138 double normal_time = i * 1.0 / frames;
139 double frame_time = normal_time * duration;
140
Mike Reed2d241f52019-09-16 15:14:21 -0400141 produce_frame(surf.get(), animation.get(), frame_time);
Mike Reed4b203ad2019-06-17 17:45:01 -0400142
Mike Reed5f12eaa2019-09-24 12:01:58 -0400143 AsyncRec asyncRec = { info, &encoder };
144 if (context) {
145 surf->asyncRescaleAndReadPixels(info, {0, 0, info.width(), info.height()},
146 SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality,
147 [](void* ctx, const void* data, size_t rb) {
148 AsyncRec* rec = (AsyncRec*)ctx;
149 rec->encoder->addFrame({rec->info, data, rb});
150 }, &asyncRec);
151 } else {
152 SkPixmap pm;
153 SkAssertResult(surf->peekPixels(&pm));
154 encoder.addFrame(pm);
155 }
Mike Reedc0ee21f2019-05-28 16:11:03 -0400156 }
Mike Reed4b203ad2019-06-17 17:45:01 -0400157 data = encoder.endRecording();
158
159 if (FLAGS_loop) {
160 double loop_dur = SkTime::GetSecs() - loop_start;
161 SkDebugf("recording secs %g, frames %d, recording fps %d\n",
162 loop_dur, frames, (int)(frames / loop_dur));
Mike Reed5093e232019-05-30 10:10:50 -0400163 }
Mike Reed4b203ad2019-06-17 17:45:01 -0400164 } while (FLAGS_loop);
Mike Reedc0ee21f2019-05-28 16:11:03 -0400165
Mike Reed5093e232019-05-30 10:10:50 -0400166 if (FLAGS_output.count() == 0) {
167 SkDebugf("missing -o output_file.mp4 argument\n");
168 return 0;
169 }
170
Mike Reedc0ee21f2019-05-28 16:11:03 -0400171 SkFILEWStream ostream(FLAGS_output[0]);
172 if (!ostream.isValid()) {
173 SkDebugf("Can't create output file %s\n", FLAGS_output[0]);
174 return -1;
175 }
176 ostream.write(data->data(), data->size());
Mike Reed5093e232019-05-30 10:10:50 -0400177 return 0;
Mike Reedc0ee21f2019-05-28 16:11:03 -0400178}