0025382: Visualization, TKOpenGl - improved video recording capability
[occt.git] / src / Image / Image_VideoRecorder.hxx
1 // Created on: 2016-04-01
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2016 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef Image_VideoRecorder_HeaderFile_
17 #define Image_VideoRecorder_HeaderFile_
18
19 #include <Image_PixMap.hxx>
20 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
21 #include <Standard_Transient.hxx>
22 #include <TCollection_AsciiString.hxx>
23
24 // forward declarations
25 struct AVFormatContext;
26 struct AVStream;
27 struct AVCodec;
28 struct AVFrame;
29 struct SwsContext;
30
31 //! Auxiliary structure defining video parameters.
32 //! Please refer to FFmpeg documentation for defining text values.
33 struct Image_VideoParams
34 {
35   TCollection_AsciiString Format;           //!< [optional]  video format (container), if empty - will be determined from the file name
36   TCollection_AsciiString VideoCodec;       //!< [optional]  codec identifier, if empty - default codec from file format will be used
37   TCollection_AsciiString PixelFormat;      //!< [optional]  pixel format, if empty - default codec pixel format will be used
38   Standard_Integer        Width;            //!< [mandatory] video frame width
39   Standard_Integer        Height;           //!< [mandatory] video frame height
40   Standard_Integer        FpsNum;           //!< [mandatory] framerate numerator
41   Standard_Integer        FpsDen;           //!< [mandatory] framerate denumerator
42   Resource_DataMapOfAsciiStringAsciiString
43                           VideoCodecParams; //!< map of advanced video codec parameters
44
45   //! Empty constructor.
46   Image_VideoParams() : Width (0), Height (0), FpsNum (0), FpsDen (1) {}
47
48   //! Setup playback FPS.
49   void SetFramerate (const Standard_Integer theNumerator,
50                      const Standard_Integer theDenominator)
51   {
52     FpsNum = theNumerator;
53     FpsDen = theDenominator;
54   }
55
56   //! Setup playback FPS.
57   //! For fixed-fps content, timebase should be 1/framerate and timestamp increments should be identical to 1.
58   void SetFramerate (const Standard_Integer theValue)
59   {
60     FpsNum = theValue;
61     FpsDen = 1;
62   }
63 };
64
65 //! Video recording tool based on FFmpeg framework.
66 class Image_VideoRecorder : public Standard_Transient
67 {
68   DEFINE_STANDARD_RTTIEXT(Image_VideoRecorder, Standard_Transient)
69 public:
70
71   //! Empty constructor.
72   Standard_EXPORT Image_VideoRecorder();
73
74   //! Destructor.
75   Standard_EXPORT virtual ~Image_VideoRecorder();
76
77   //! Close the stream - stop recorder.
78   Standard_EXPORT void Close();
79
80   //! Open output stream - initialize recorder.
81   //! @param theFileName [in] video filename
82   //! @param theParams   [in] video parameters
83   Standard_EXPORT Standard_Boolean Open (const char* theFileName,
84                                          const Image_VideoParams& theParams);
85
86   //! Access RGBA frame, should NOT be re-initialized outside.
87   //! Note that image is expected to have upper-left origin.
88   Image_PixMap& ChangeFrame() { return myImgSrcRgba; }
89
90   //! Return current frame index.
91   int64_t FrameCount() const { return myFrameCount; }
92
93   //! Push new frame, should be called after Open().
94   Standard_Boolean PushFrame()
95   {
96     return writeVideoFrame (Standard_False);
97   }
98
99 protected:
100
101   //! Wrapper for av_strerror().
102   Standard_EXPORT TCollection_AsciiString formatAvError (const int theError) const;
103
104   //! Append video stream.
105   //! theParams     [in] video parameters
106   //! theDefCodecId [in] identifier of codec managed by FFmpeg library (AVCodecID enum)
107   Standard_EXPORT Standard_Boolean addVideoStream (const Image_VideoParams& theParams,
108                                                    const Standard_Integer   theDefCodecId);
109
110   //! Open video codec.
111   Standard_EXPORT Standard_Boolean openVideoCodec (const Image_VideoParams& theParams);
112
113   //! Write new video frame.
114   Standard_EXPORT Standard_Boolean writeVideoFrame (const Standard_Boolean theToFlush);
115
116 protected:
117
118   //! AVRational alias.
119   struct VideoRational
120   {
121     int num; //!< numerator
122     int den; //!< denominator
123   };
124
125 protected:
126
127   AVFormatContext* myAVContext;   //!< video context
128   AVStream*        myVideoStream; //!< video stream
129   AVCodec*         myVideoCodec;  //!< video codec
130   AVFrame*         myFrame;       //!< frame to record
131   SwsContext*      myScaleCtx;    //!< scale context for conversion from RGBA to YUV
132
133   Image_PixMap     myImgSrcRgba;  //!< input RGBA image
134   VideoRational    myFrameRate;   //!< video framerate
135   int64_t          myFrameCount;  //!< current frame index
136
137 };
138
139 DEFINE_STANDARD_HANDLE(Image_VideoRecorder, Standard_Transient)
140
141 #endif // Image_VideoRecorder_HeaderFile_