1 // Created on: 2016-04-01
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2016 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef Image_VideoRecorder_HeaderFile_
17 #define Image_VideoRecorder_HeaderFile_
19 #include <Image_PixMap.hxx>
20 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
21 #include <Standard_Transient.hxx>
22 #include <TCollection_AsciiString.hxx>
24 // forward declarations
25 struct AVFormatContext;
31 //! Auxiliary structure defining video parameters.
32 //! Please refer to FFmpeg documentation for defining text values.
33 struct Image_VideoParams
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
45 //! Empty constructor.
46 Image_VideoParams() : Width (0), Height (0), FpsNum (0), FpsDen (1) {}
48 //! Setup playback FPS.
49 void SetFramerate (const Standard_Integer theNumerator,
50 const Standard_Integer theDenominator)
52 FpsNum = theNumerator;
53 FpsDen = theDenominator;
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)
65 //! Video recording tool based on FFmpeg framework.
66 class Image_VideoRecorder : public Standard_Transient
68 DEFINE_STANDARD_RTTIEXT(Image_VideoRecorder, Standard_Transient)
71 //! Empty constructor.
72 Standard_EXPORT Image_VideoRecorder();
75 Standard_EXPORT virtual ~Image_VideoRecorder();
77 //! Close the stream - stop recorder.
78 Standard_EXPORT void Close();
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);
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; }
90 //! Return current frame index.
91 int64_t FrameCount() const { return myFrameCount; }
93 //! Push new frame, should be called after Open().
94 Standard_Boolean PushFrame()
96 return writeVideoFrame (Standard_False);
101 //! Wrapper for av_strerror().
102 Standard_EXPORT TCollection_AsciiString formatAvError (const int theError) const;
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);
110 //! Open video codec.
111 Standard_EXPORT Standard_Boolean openVideoCodec (const Image_VideoParams& theParams);
113 //! Write new video frame.
114 Standard_EXPORT Standard_Boolean writeVideoFrame (const Standard_Boolean theToFlush);
118 //! AVRational alias.
121 int num; //!< numerator
122 int den; //!< denominator
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
133 Image_PixMap myImgSrcRgba; //!< input RGBA image
134 VideoRational myFrameRate; //!< video framerate
135 int64_t myFrameCount; //!< current frame index
139 DEFINE_STANDARD_HANDLE(Image_VideoRecorder, Standard_Transient)
141 #endif // Image_VideoRecorder_HeaderFile_