1 //  Copyright (c) 2007-2017 Fredrik Mellbin
2 //
3 //  Permission is hereby granted, free of charge, to any person obtaining a copy
4 //  of this software and associated documentation files (the "Software"), to deal
5 //  in the Software without restriction, including without limitation the rights
6 //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 //  copies of the Software, and to permit persons to whom the Software is
8 //  furnished to do so, subject to the following conditions:
9 //
10 //  The above copyright notice and this permission notice shall be included in
11 //  all copies or substantial portions of the Software.
12 //
13 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 //  THE SOFTWARE.
20 
21 #ifndef FFVIDEOSOURCE_H
22 #define FFVIDEOSOURCE_H
23 
24 extern "C" {
25 #include <libavformat/avformat.h>
26 #include <libavcodec/avcodec.h>
27 #include <libswscale/swscale.h>
28 #include <libavutil/imgutils.h>
29 #include <libavutil/stereo3d.h>
30 #include <libavutil/display.h>
31 #include <libavutil/mastering_display_metadata.h>
32 }
33 
34 #include <vector>
35 
36 #include "track.h"
37 #include "utils.h"
38 
39 struct FFMS_VideoSource {
40 private:
41     SwsContext *SWS = nullptr;
42 
43     int Delay = 0;
44     int DelayCounter = 0;
45     int InitialDecode = 1;
46     bool PAFFAdjusted = false;
47 
48     int LastFrameHeight = -1;
49     int LastFrameWidth = -1;
50     AVPixelFormat LastFramePixelFormat = AV_PIX_FMT_NONE;
51 
52     int TargetHeight = -1;
53     int TargetWidth = -1;
54     std::vector<AVPixelFormat> TargetPixelFormats;
55     int TargetResizer = 0;
56 
57     AVPixelFormat OutputFormat = AV_PIX_FMT_NONE;
58     AVColorRange OutputColorRange = AVCOL_RANGE_UNSPECIFIED;
59     AVColorSpace OutputColorSpace = AVCOL_SPC_UNSPECIFIED;
60     bool OutputColorRangeSet = false;
61     bool OutputColorSpaceSet = false;
62 
63     int OutputColorPrimaries = -1;
64     int OutputTransferCharateristics = -1;
65     int OutputChromaLocation = -1;
66 
67     bool InputFormatOverridden = false;
68     AVPixelFormat InputFormat = AV_PIX_FMT_NONE;
69     AVColorRange InputColorRange = AVCOL_RANGE_UNSPECIFIED;
70     AVColorSpace InputColorSpace = AVCOL_SPC_UNSPECIFIED;
71 
72     uint8_t *SWSFrameData[4] = {};
73     int SWSFrameLinesize[4] = {};
74 
75     void DetectInputFormat();
76     bool HasPendingDelayedFrames();
77 
78     FFMS_VideoProperties VP = {};
79     FFMS_Frame LocalFrame = {};
80     AVFrame *DecodeFrame = nullptr;
81     AVFrame *LastDecodedFrame = nullptr;
82     int LastFrameNum = 0;
83     FFMS_Index &Index;
84     FFMS_Track Frames;
85     int VideoTrack;
86     int CurrentFrame = 1;
87     int DecodingThreads;
88     AVCodecContext *CodecContext = nullptr;
89     AVFormatContext *FormatContext = nullptr;
90     int SeekMode;
91     bool SeekByPos = false;
92     int PosOffset = 0;
93 
94     void ReAdjustOutputFormat(AVFrame *Frame);
95     FFMS_Frame *OutputFrame(AVFrame *Frame);
96     void SetVideoProperties();
97     bool DecodePacket(AVPacket *Packet);
98     void DecodeNextFrame(int64_t &PTS, int64_t &Pos);
99     bool SeekTo(int n, int SeekOffset);
100     int Seek(int n);
101     int ReadFrame(AVPacket *pkt);
102     void Free();
103     static void SanityCheckFrameForData(AVFrame *Frame);
104 public:
105     FFMS_VideoSource(const char *SourceFile, FFMS_Index &Index, int Track, int Threads, int SeekMode);
106     ~FFMS_VideoSource();
GetVideoPropertiesFFMS_VideoSource107     const FFMS_VideoProperties& GetVideoProperties() { return VP; }
GetTrackFFMS_VideoSource108     FFMS_Track *GetTrack() { return &Frames; }
109     FFMS_Frame *GetFrame(int n);
110     void GetFrameCheck(int n);
111     FFMS_Frame *GetFrameByTime(double Time);
112     void SetOutputFormat(const AVPixelFormat *TargetFormats, int Width, int Height, int Resizer);
113     void ResetOutputFormat();
114     void SetInputFormat(int ColorSpace, int ColorRange, AVPixelFormat Format);
115     void ResetInputFormat();
116 };
117 
118 #endif
119