0030612: Visualization - provide texture map with video as image source
[occt.git] / src / Media / Media_FormatContext.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _Media_FormatContext_HeaderFile
16 #define _Media_FormatContext_HeaderFile
17
18 #include <Media_Packet.hxx>
19
20 #include <TCollection_AsciiString.hxx>
21
22 struct AVCodecContext;
23 struct AVFormatContext;
24 struct AVStream;
25 struct AVRational;
26
27 //! AVFormatContext wrapper - the media input/output stream holder.
28 class Media_FormatContext : public Standard_Transient
29 {
30   DEFINE_STANDARD_RTTIEXT(Media_FormatContext, Standard_Transient)
31 public:
32   //! Returns string description for AVError code.
33   Standard_EXPORT static TCollection_AsciiString FormatAVErrorDescription (int theErrCodeAV);
34
35   //! Convert time units into seconds for context.
36   //! @param theTimeUnits value to convert
37   //! @return converted time units in seconds
38   Standard_EXPORT static double FormatUnitsToSeconds (int64_t theTimeUnits);
39
40   //! Convert time units into seconds. Returns zero for invalid value.
41   //! @param theTimeBase  the timebase
42   //! @param theTimeUnits value to convert
43   //! @return converted time units in seconds
44   Standard_EXPORT static double UnitsToSeconds (const AVRational& theTimeBase,
45                                                 int64_t theTimeUnits);
46
47   //! Convert time units into seconds using stream base.
48   //! @param theStream    the stream;
49   //! @param theTimeUnits value to convert;
50   //! @return converted time units in seconds.
51   Standard_EXPORT static double StreamUnitsToSeconds (const AVStream& theStream,
52                                                       int64_t theTimeUnits);
53
54   //! Convert seconds into time units for context.
55   //! @param theTimeSeconds value to convert
56   //! @return time units
57   Standard_EXPORT static int64_t SecondsToUnits (double theTimeSeconds);
58
59   //! Convert seconds into time units.
60   //! @param theTimeBase    the timebase
61   //! @param theTimeSeconds value to convert
62   //! @return time units
63   Standard_EXPORT static int64_t SecondsToUnits (const AVRational& theTimeBase,
64                                                  double theTimeSeconds);
65
66   //! Convert seconds into time units for stream.
67   //! @param theStream      the stream
68   //! @param theTimeSeconds value to convert
69   //! @return time units
70   Standard_EXPORT static int64_t StreamSecondsToUnits (const AVStream& theStream,
71                                                        double theTimeSeconds);
72
73   //! Time formatter.
74   Standard_EXPORT static TCollection_AsciiString FormatTime (double theSeconds);
75
76   //! Time progress / duration formatter.
77   Standard_EXPORT static TCollection_AsciiString FormatTimeProgress (double theProgress,
78                                                                      double theDuration);
79
80 public:
81
82   //! Constructor.
83   Standard_EXPORT Media_FormatContext();
84
85   //! Destructor.
86   Standard_EXPORT virtual ~Media_FormatContext();
87
88   //! Return context.
89   AVFormatContext* Context() const { return myFormatCtx; }
90
91   //! Open input.
92   Standard_EXPORT bool OpenInput (const TCollection_AsciiString& theInput);
93
94   //! Close input.
95   Standard_EXPORT void Close();
96
97   //! Return amount of streams.
98   Standard_EXPORT unsigned int NbSteams() const;
99
100   //! Return stream.
101   Standard_EXPORT const AVStream& Stream (unsigned int theIndex) const;
102
103   //! Format stream info.
104   Standard_EXPORT TCollection_AsciiString StreamInfo (unsigned int theIndex,
105                                                       AVCodecContext* theCodecCtx = NULL) const;
106
107   //! Return PTS start base in seconds.
108   double PtsStartBase() const { return myPtsStartBase; }
109
110   //! Return duration in seconds.
111   double Duration() const { return myDuration; }
112
113   //! av_read_frame() wrapper.
114   Standard_EXPORT bool ReadPacket (const Handle(Media_Packet)& thePacket);
115
116   //! Seek stream to specified position.
117   Standard_EXPORT bool SeekStream (unsigned int theStreamId,
118                                    double theSeekPts,
119                                    bool toSeekBack);
120
121   //! Seek context to specified position.
122   Standard_EXPORT bool Seek (double theSeekPts,
123                              bool   toSeekBack);
124
125 protected:
126
127   AVFormatContext* myFormatCtx;    //!< format context
128   double           myPtsStartBase; //!< start time
129   double           myDuration;     //!< duration
130
131 };
132
133 #endif // _Media_FormatContext_HeaderFile