1 /* 2 * Copyright 2019 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 8 #ifndef SkVideDecoder_DEFINED 9 #define SkVideDecoder_DEFINED 10 11 #include "include/core/SkImage.h" 12 13 extern "C" { 14 #include "libavcodec/avcodec.h" 15 #include "libavformat/avformat.h" 16 #include "libavformat/avio.h" 17 #include "libavutil/pixdesc.h" 18 } 19 20 class SkVideoDecoder { 21 public: 22 SkVideoDecoder(GrContext* gr = nullptr); 23 ~SkVideoDecoder(); 24 25 void reset(); setGrContext(GrContext * gr)26 void setGrContext(GrContext* gr) { fGr = gr; } 27 28 bool loadStream(std::unique_ptr<SkStream>); 29 bool rewind(); 30 31 SkISize dimensions() const; 32 double duration() const; // in seconds 33 34 // Returns each image in the video, or nullptr on eof 35 sk_sp<SkImage> nextImage(double* timeStamp = nullptr); 36 37 private: 38 sk_sp<SkImage> convertFrame(const AVFrame*); 39 double computeTimeStamp(const AVFrame*) const; 40 41 struct ConvertedColorSpace { 42 AVColorPrimaries fPrimaries; 43 AVColorTransferCharacteristic fTransfer; 44 // fCS is the converted skia form of the above enums 45 sk_sp<SkColorSpace> fCS; 46 47 // Init with illegal values, so our first compare will fail, forcing us to compute 48 // the skcolorspace. 49 ConvertedColorSpace(); 50 51 void update(AVColorPrimaries, AVColorTransferCharacteristic); 52 }; 53 54 GrContext* fGr = nullptr; // not owned by us 55 56 std::unique_ptr<SkStream> fStream; 57 58 AVIOContext* fStreamCtx = nullptr; 59 AVFormatContext* fFormatCtx = nullptr; 60 AVCodecContext* fDecoderCtx = nullptr; 61 int fStreamIndex = -1; // fFormatCtx->stream[...] 62 63 AVPacket fPacket; 64 AVFrame* fFrame = nullptr; 65 ConvertedColorSpace fCSCache; 66 67 enum Mode { 68 kProcessing_Mode, 69 kDraining_Mode, 70 kDone_Mode, 71 }; 72 Mode fMode = kDone_Mode; 73 }; 74 75 #endif 76 77