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 Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 14 | #include "modules/skottie/include/Skottie.h" |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 15 | #include "modules/skottie/utils/SkottieUtils.h" |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 16 | #include "src/utils/SkOSPath.h" |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 17 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 18 | #include "tools/flags/CommandLineFlags.h" |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 19 | #include "tools/gpu/GrContextFactory.h" |
| 20 | |
| 21 | #include "include/gpu/GrContextOptions.h" |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 22 | |
| 23 | static DEFINE_string2(input, i, "", "skottie animation to render"); |
| 24 | static DEFINE_string2(output, o, "", "mp4 file to create"); |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 25 | static DEFINE_string2(assetPath, a, "", "path to assets needed for json file"); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 26 | static DEFINE_int_2(fps, f, 25, "fps"); |
| 27 | static DEFINE_bool2(verbose, v, false, "verbose mode"); |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 28 | static DEFINE_bool2(loop, l, false, "loop mode for profiling"); |
Mike Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 29 | 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] | 30 | static DEFINE_bool2(gpu, g, false, "use GPU for rendering"); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 31 | |
| 32 | static 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 Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 38 | struct AsyncRec { |
| 39 | SkImageInfo info; |
| 40 | SkVideoEncoder* encoder; |
| 41 | }; |
| 42 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 43 | int main(int argc, char** argv) { |
Mike Reed | f755bc7 | 2019-09-20 11:37:36 -0400 | [diff] [blame] | 44 | SkGraphics::Init(); |
| 45 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 46 | CommandLineFlags::SetUsage("Converts skottie to a mp4"); |
| 47 | CommandLineFlags::Parse(argc, argv); |
| 48 | |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 49 | if (FLAGS_input.count() == 0) { |
| 50 | SkDebugf("-i input_file.json argument required\n"); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 51 | return -1; |
| 52 | } |
| 53 | |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 54 | auto contextType = sk_gpu_test::GrContextFactory::kGL_ContextType; |
| 55 | GrContextOptions grCtxOptions; |
| 56 | sk_gpu_test::GrContextFactory factory(grCtxOptions); |
| 57 | |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 58 | 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 Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 74 | 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 Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 83 | 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 Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 89 | const int frames = SkScalarRoundToInt(duration * fps); |
| 90 | const double frame_duration = 1.0 / fps; |
| 91 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 92 | if (FLAGS_verbose) { |
Mike Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 93 | SkDebugf("Size %dx%d duration %g, fps %d, frame_duration %g\n", |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 94 | dim.width(), dim.height(), duration, fps, frame_duration); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 97 | SkVideoEncoder encoder; |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 98 | |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 99 | GrContext* context = nullptr; |
| 100 | sk_sp<SkSurface> surf; |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 101 | sk_sp<SkData> data; |
| 102 | |
Florin Malita | 10e5257 | 2019-10-09 16:03:35 -0400 | [diff] [blame] | 103 | const auto info = SkImageInfo::MakeN32Premul(dim); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 104 | do { |
| 105 | double loop_start = SkTime::GetSecs(); |
| 106 | |
Florin Malita | 10e5257 | 2019-10-09 16:03:35 -0400 | [diff] [blame] | 107 | if (!encoder.beginRecording(dim, fps)) { |
| 108 | SkDEBUGF("Invalid video stream configuration.\n"); |
| 109 | return -1; |
| 110 | } |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 111 | |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 112 | // lazily allocate the surfaces |
| 113 | if (!surf) { |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 114 | 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 Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 132 | for (int i = 0; i <= frames; ++i) { |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 133 | 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 Reed | 2d241f5 | 2019-09-16 15:14:21 -0400 | [diff] [blame] | 141 | produce_frame(surf.get(), animation.get(), frame_time); |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 142 | |
Mike Reed | 5f12eaa | 2019-09-24 12:01:58 -0400 | [diff] [blame] | 143 | 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 Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 156 | } |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 157 | 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 Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 163 | } |
Mike Reed | 4b203ad | 2019-06-17 17:45:01 -0400 | [diff] [blame] | 164 | } while (FLAGS_loop); |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 165 | |
Mike Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 166 | if (FLAGS_output.count() == 0) { |
| 167 | SkDebugf("missing -o output_file.mp4 argument\n"); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 171 | 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 Reed | 5093e23 | 2019-05-30 10:10:50 -0400 | [diff] [blame] | 177 | return 0; |
Mike Reed | c0ee21f | 2019-05-28 16:11:03 -0400 | [diff] [blame] | 178 | } |