2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkVideEncoder_DEFINED
9 #define SkVideEncoder_DEFINED
11 #include "include/core/SkImage.h"
12 #include "include/core/SkStream.h"
15 #include "libavcodec/avcodec.h"
16 #include "libavformat/avformat.h"
17 #include "libavformat/avio.h"
18 #include "libavutil/pixdesc.h"
21 // private to the impl
22 class SkRandomAccessWStream;
25 class SkVideoEncoder {
31 * Begins a new recording. Balance this (after adding all of your frames) with a call
34 bool beginRecording(SkISize, int fps);
37 * If you have your own pixmap, call addFrame(). Note this may fail if it uses an unsupported
38 * ColorType (requires kN32_SkColorType) or AlphaType, or the dimensions don't match those set
41 bool addFrame(const SkPixmap&);
44 * As an alternative to calling addFrame(), you can call beginFrame/endFrame, and the encoder
45 * will manage allocating a surface/canvas for you.
47 * SkCanvas* canvas = encoder.beginFrame();
48 * // your drawing code here, drawing into canvas
51 SkCanvas* beginFrame();
55 * Call this after having added all of your frames. After calling this, no more frames can
56 * be added to this recording. To record a new video, call beginRecording().
58 sk_sp<SkData> endRecording();
63 bool sendFrame(AVFrame*); // frame can be null
65 double computeTimeStamp(const AVFrame*) const;
67 SwsContext* fSWScaleCtx = nullptr;
68 AVIOContext* fStreamCtx = nullptr;
69 AVFormatContext* fFormatCtx = nullptr;
70 AVCodecContext* fEncoderCtx = nullptr;
71 AVStream* fStream = nullptr; // we do not free this
72 AVFrame* fFrame = nullptr;
73 AVPacket* fPacket = nullptr;
75 SkImageInfo fInfo; // only defined between beginRecording() and endRecording()
76 std::unique_ptr<SkRandomAccessWStream> fWStream;
77 int64_t fCurrentPTS, fDeltaPTS;
79 // Lazily allocated, iff the client has called beginFrame() for a given recording session.
80 sk_sp<SkSurface> fSurface;