Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 8 | #include "experimental/ffmpeg/SkVideoEncoder.h" |
| 9 | #include "include/core/SkCanvas.h" |
Mike Reed | f755bc7 | 2019-09-20 11:37:36 -0400 | [diff] [blame] | 10 | #include "include/core/SkGraphics.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 11 | #include "include/core/SkStream.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "include/core/SkTime.h" |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame] | 14 | #include "include/private/SkTPin.h" |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 15 | #include "modules/skottie/include/Skottie.h" |
Brian Osman | 849f4d6 | 2019-11-26 08:58:26 -0500 | [diff] [blame] | 16 | #include "modules/skresources/include/SkResources.h" |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 17 | #include "src/utils/SkOSPath.h" |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 18 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 19 | #include "tools/flags/CommandLineFlags.h" |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 20 | #include "tools/gpu/GrContextFactory.h" |
| 21 | |
| 22 | #include "include/gpu/GrContextOptions.h" |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 23 | |
| 24 | static DEFINE_string2(input, i, "", "skottie animation to render"); |
| 25 | static DEFINE_string2(output, o, "", "mp4 file to create"); |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 26 | static DEFINE_string2(assetPath, a, "", "path to assets needed for json file"); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 27 | static DEFINE_int_2(fps, f, 25, "fps"); |
| 28 | static DEFINE_bool2(verbose, v, false, "verbose mode"); |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 29 | static DEFINE_bool2(loop, l, false, "loop mode for profiling"); |
Mike Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 30 | static DEFINE_int(set_dst_width, 0, "set destination width (height will be computed)"); |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 31 | static DEFINE_bool2(gpu, g, false, "use GPU for rendering"); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 32 | |
Florin Malita | dd5ba94 | 2019-11-24 15:42:24 -0500 | [diff] [blame] | 33 | static void produce_frame(SkSurface* surf, skottie::Animation* anim, double frame) { |
| 34 | anim->seekFrame(frame); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 35 | surf->getCanvas()->clear(SK_ColorWHITE); |
| 36 | anim->render(surf->getCanvas()); |
| 37 | } |
| 38 | |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 39 | struct AsyncRec { |
| 40 | SkImageInfo info; |
| 41 | SkVideoEncoder* encoder; |
| 42 | }; |
| 43 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 44 | int main(int argc, char** argv) { |
Mike Reed | f755bc7 | 2019-09-20 11:37:36 -0400 | [diff] [blame] | 45 | SkGraphics::Init(); |
| 46 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 47 | CommandLineFlags::SetUsage("Converts skottie to a mp4"); |
| 48 | CommandLineFlags::Parse(argc, argv); |
| 49 | |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 50 | if (FLAGS_input.count() == 0) { |
| 51 | SkDebugf("-i input_file.json argument required\n"); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 52 | return -1; |
| 53 | } |
| 54 | |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 55 | auto contextType = sk_gpu_test::GrContextFactory::kGL_ContextType; |
| 56 | GrContextOptions grCtxOptions; |
| 57 | sk_gpu_test::GrContextFactory factory(grCtxOptions); |
| 58 | |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 59 | SkString assetPath; |
| 60 | if (FLAGS_assetPath.count() > 0) { |
| 61 | assetPath.set(FLAGS_assetPath[0]); |
| 62 | } else { |
| 63 | assetPath = SkOSPath::Dirname(FLAGS_input[0]); |
| 64 | } |
| 65 | SkDebugf("assetPath %s\n", assetPath.c_str()); |
| 66 | |
| 67 | auto animation = skottie::Animation::Builder() |
Brian Osman | 849f4d6 | 2019-11-26 08:58:26 -0500 | [diff] [blame] | 68 | .setResourceProvider(skresources::FileResourceProvider::Make(assetPath)) |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 69 | .makeFromFile(FLAGS_input[0]); |
| 70 | if (!animation) { |
| 71 | SkDebugf("failed to load %s\n", FLAGS_input[0]); |
| 72 | return -1; |
| 73 | } |
| 74 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 75 | SkISize dim = animation->size().toRound(); |
| 76 | double duration = animation->duration(); |
Florin Malita | dd5ba94 | 2019-11-24 15:42:24 -0500 | [diff] [blame] | 77 | int fps = SkTPin(FLAGS_fps, 1, 240); |
| 78 | double fps_scale = animation->fps() / fps; |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 79 | |
Mike Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 80 | float scale = 1; |
| 81 | if (FLAGS_set_dst_width > 0) { |
| 82 | scale = FLAGS_set_dst_width / (float)dim.width(); |
| 83 | dim = { FLAGS_set_dst_width, SkScalarRoundToInt(scale * dim.height()) }; |
| 84 | } |
| 85 | |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 86 | const int frames = SkScalarRoundToInt(duration * fps); |
| 87 | const double frame_duration = 1.0 / fps; |
| 88 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 89 | if (FLAGS_verbose) { |
Mike Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 90 | SkDebugf("Size %dx%d duration %g, fps %d, frame_duration %g\n", |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 91 | dim.width(), dim.height(), duration, fps, frame_duration); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 94 | SkVideoEncoder encoder; |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 95 | |
Florin Malita | 2e4dc86 | 2021-09-09 10:35:33 -0400 | [diff] [blame] | 96 | GrDirectContext* grctx = nullptr; |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 97 | sk_sp<SkSurface> surf; |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 98 | sk_sp<SkData> data; |
| 99 | |
Florin Malita | 10e5257 | 2019-10-09 16:03:35 -0400 | [diff] [blame] | 100 | const auto info = SkImageInfo::MakeN32Premul(dim); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 101 | do { |
| 102 | double loop_start = SkTime::GetSecs(); |
| 103 | |
Florin Malita | 10e5257 | 2019-10-09 16:03:35 -0400 | [diff] [blame] | 104 | if (!encoder.beginRecording(dim, fps)) { |
| 105 | SkDEBUGF("Invalid video stream configuration.\n"); |
| 106 | return -1; |
| 107 | } |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 108 | |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 109 | // lazily allocate the surfaces |
| 110 | if (!surf) { |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 111 | if (FLAGS_gpu) { |
Florin Malita | 2e4dc86 | 2021-09-09 10:35:33 -0400 | [diff] [blame] | 112 | grctx = factory.getContextInfo(contextType).directContext(); |
| 113 | surf = SkSurface::MakeRenderTarget(grctx, |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 114 | SkBudgeted::kNo, |
| 115 | info, |
| 116 | 0, |
| 117 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 118 | nullptr); |
| 119 | if (!surf) { |
Florin Malita | 2e4dc86 | 2021-09-09 10:35:33 -0400 | [diff] [blame] | 120 | grctx = nullptr; |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | if (!surf) { |
| 124 | surf = SkSurface::MakeRaster(info); |
| 125 | } |
| 126 | surf->getCanvas()->scale(scale, scale); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 129 | for (int i = 0; i <= frames; ++i) { |
Florin Malita | dd5ba94 | 2019-11-24 15:42:24 -0500 | [diff] [blame] | 130 | const double frame = i * fps_scale; |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 131 | if (FLAGS_verbose) { |
Florin Malita | dd5ba94 | 2019-11-24 15:42:24 -0500 | [diff] [blame] | 132 | SkDebugf("rendering frame %g\n", frame); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Florin Malita | dd5ba94 | 2019-11-24 15:42:24 -0500 | [diff] [blame] | 135 | produce_frame(surf.get(), animation.get(), frame); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 136 | |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 137 | AsyncRec asyncRec = { info, &encoder }; |
Florin Malita | 2e4dc86 | 2021-09-09 10:35:33 -0400 | [diff] [blame] | 138 | if (grctx) { |
Florin Malita | f22dda9 | 2019-10-28 18:47:32 -0400 | [diff] [blame] | 139 | auto read_pixels_cb = [](SkSurface::ReadPixelsContext ctx, |
| 140 | std::unique_ptr<const SkSurface::AsyncReadResult> result) { |
| 141 | if (result && result->count() == 1) { |
| 142 | AsyncRec* rec = reinterpret_cast<AsyncRec*>(ctx); |
| 143 | rec->encoder->addFrame({rec->info, result->data(0), result->rowBytes(0)}); |
| 144 | } |
| 145 | }; |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 146 | surf->asyncRescaleAndReadPixels(info, {0, 0, info.width(), info.height()}, |
Florin Malita | f22dda9 | 2019-10-28 18:47:32 -0400 | [diff] [blame] | 147 | SkSurface::RescaleGamma::kSrc, |
Florin Malita | 6356cb1 | 2021-01-05 12:56:34 -0500 | [diff] [blame] | 148 | SkImage::RescaleMode::kNearest, |
Florin Malita | f22dda9 | 2019-10-28 18:47:32 -0400 | [diff] [blame] | 149 | read_pixels_cb, &asyncRec); |
Florin Malita | 2e4dc86 | 2021-09-09 10:35:33 -0400 | [diff] [blame] | 150 | grctx->submit(); |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 151 | } else { |
| 152 | SkPixmap pm; |
| 153 | SkAssertResult(surf->peekPixels(&pm)); |
| 154 | encoder.addFrame(pm); |
| 155 | } |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 156 | } |
Florin Malita | 2e4dc86 | 2021-09-09 10:35:33 -0400 | [diff] [blame] | 157 | |
| 158 | if (grctx) { |
| 159 | // ensure all pending reads are completed |
| 160 | grctx->flushAndSubmit(true); |
| 161 | } |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 162 | data = encoder.endRecording(); |
| 163 | |
| 164 | if (FLAGS_loop) { |
| 165 | double loop_dur = SkTime::GetSecs() - loop_start; |
| 166 | SkDebugf("recording secs %g, frames %d, recording fps %d\n", |
| 167 | loop_dur, frames, (int)(frames / loop_dur)); |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 168 | } |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 169 | } while (FLAGS_loop); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 170 | |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 171 | if (FLAGS_output.count() == 0) { |
| 172 | SkDebugf("missing -o output_file.mp4 argument\n"); |
| 173 | return 0; |
| 174 | } |
| 175 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 176 | SkFILEWStream ostream(FLAGS_output[0]); |
| 177 | if (!ostream.isValid()) { |
| 178 | SkDebugf("Can't create output file %s\n", FLAGS_output[0]); |
| 179 | return -1; |
| 180 | } |
| 181 | ostream.write(data->data(), data->size()); |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 182 | return 0; |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 183 | } |