• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "libswscale/swscale.h"
19 }
20 
21 class SkStream;
22 
23 class SkVideoDecoder {
24 public:
25     SkVideoDecoder(GrRecordingContext* = nullptr);
26     ~SkVideoDecoder();
27 
28     void reset();
setGrContext(GrRecordingContext * rContext)29     void setGrContext(GrRecordingContext* rContext) { fRecordingContext = rContext; }
30 
31     bool loadStream(std::unique_ptr<SkStream>);
32     bool rewind();
33 
34     SkISize dimensions() const;
35     double duration() const;    // in seconds
36 
37     // Returns each image in the video, or nullptr on eof
38     sk_sp<SkImage> nextImage(double* timeStamp = nullptr);
39 
40 private:
41     sk_sp<SkImage> convertFrame(const AVFrame*);
42     double computeTimeStamp(const AVFrame*) const;
43 
44     struct ConvertedColorSpace {
45         AVColorPrimaries              fPrimaries;
46         AVColorTransferCharacteristic fTransfer;
47         // fCS is the converted skia form of the above enums
48         sk_sp<SkColorSpace> fCS;
49 
50         // Init with illegal values, so our first compare will fail, forcing us to compute
51         // the skcolorspace.
52         ConvertedColorSpace();
53 
54         void update(AVColorPrimaries, AVColorTransferCharacteristic);
55     };
56 
57     GrRecordingContext* fRecordingContext = nullptr;  // not owned by us
58 
59     std::unique_ptr<SkStream>   fStream;
60 
61     AVIOContext*        fStreamCtx = nullptr;
62     AVFormatContext*    fFormatCtx = nullptr;
63     AVCodecContext*     fDecoderCtx = nullptr;
64     int                 fStreamIndex = -1;  // fFormatCtx->stream[...]
65 
66     AVPacket            fPacket;
67     AVFrame*            fFrame = nullptr;
68     ConvertedColorSpace fCSCache;
69 
70     enum Mode {
71         kProcessing_Mode,
72         kDraining_Mode,
73         kDone_Mode,
74     };
75     Mode    fMode = kDone_Mode;
76 };
77 
78 #endif
79 
80