Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/Image_VideoRecorder.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Created on: 2016-04-01
0002 // Created by: Anastasia BORISOVA
0003 // Copyright (c) 2016 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef Image_VideoRecorder_HeaderFile_
0017 #define Image_VideoRecorder_HeaderFile_
0018 
0019 #include <Image_PixMap.hxx>
0020 #include <TCollection_AsciiString.hxx>
0021 #include <NCollection_DataMap.hxx>
0022 #include <Standard_Transient.hxx>
0023 
0024 // forward declarations
0025 struct AVFormatContext;
0026 struct AVCodecContext;
0027 struct AVStream;
0028 struct AVCodec;
0029 struct AVFrame;
0030 struct SwsContext;
0031 
0032 // Undefine macro that clashes with name used by field of Image_VideoParams;
0033 // this macro is defined in headers of older versions of libavutil
0034 // (see definition of macro FF_API_PIX_FMT in version.h)
0035 #ifdef PixelFormat
0036   #undef PixelFormat
0037 #endif
0038 
0039 //! Auxiliary structure defining video parameters.
0040 //! Please refer to FFmpeg documentation for defining text values.
0041 struct Image_VideoParams
0042 {
0043   // clang-format off
0044   TCollection_AsciiString Format;           //!< [optional]  video format (container), if empty - will be determined from the file name
0045   TCollection_AsciiString VideoCodec;       //!< [optional]  codec identifier, if empty - default codec from file format will be used
0046   TCollection_AsciiString PixelFormat;      //!< [optional]  pixel format, if empty - default codec pixel format will be used
0047   // clang-format on
0048   int Width;  //!< [mandatory] video frame width
0049   int Height; //!< [mandatory] video frame height
0050   int FpsNum; //!< [mandatory] framerate numerator
0051   int FpsDen; //!< [mandatory] framerate denumerator
0052   NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>
0053     VideoCodecParams; //!< map of advanced video codec parameters
0054 
0055   //! Empty constructor.
0056   Image_VideoParams()
0057       : Width(0),
0058         Height(0),
0059         FpsNum(0),
0060         FpsDen(1)
0061   {
0062   }
0063 
0064   //! Setup playback FPS.
0065   void SetFramerate(const int theNumerator, const int theDenominator)
0066   {
0067     FpsNum = theNumerator;
0068     FpsDen = theDenominator;
0069   }
0070 
0071   //! Setup playback FPS.
0072   //! For fixed-fps content, timebase should be 1/framerate and timestamp increments should be
0073   //! identical to 1.
0074   void SetFramerate(const int theValue)
0075   {
0076     FpsNum = theValue;
0077     FpsDen = 1;
0078   }
0079 };
0080 
0081 //! Video recording tool based on FFmpeg framework.
0082 class Image_VideoRecorder : public Standard_Transient
0083 {
0084   DEFINE_STANDARD_RTTIEXT(Image_VideoRecorder, Standard_Transient)
0085 public:
0086   //! Empty constructor.
0087   Standard_EXPORT Image_VideoRecorder();
0088 
0089   //! Destructor.
0090   Standard_EXPORT ~Image_VideoRecorder() override;
0091 
0092   //! Close the stream - stop recorder.
0093   Standard_EXPORT void Close();
0094 
0095   //! Open output stream - initialize recorder.
0096   //! @param[in] theFileName  video filename
0097   //! @param[in] theParams    video parameters
0098   Standard_EXPORT bool Open(const char* theFileName, const Image_VideoParams& theParams);
0099 
0100   //! Access RGBA frame, should NOT be re-initialized outside.
0101   //! Note that image is expected to have upper-left origin.
0102   Image_PixMap& ChangeFrame() { return myImgSrcRgba; }
0103 
0104   //! Return current frame index.
0105   int64_t FrameCount() const { return myFrameCount; }
0106 
0107   //! Push new frame, should be called after Open().
0108   bool PushFrame() { return writeVideoFrame(false); }
0109 
0110 protected:
0111   //! Wrapper for av_strerror().
0112   Standard_EXPORT TCollection_AsciiString formatAvError(const int theError) const;
0113 
0114   //! Get codec context compatible with both old and new FFmpeg API.
0115   Standard_EXPORT AVCodecContext* getCodecContext() const;
0116 
0117   //! Append video stream.
0118   //! theParams[in]      video parameters
0119   //! theDefCodecId[in]  identifier of codec managed by FFmpeg library (AVCodecID enum)
0120   Standard_EXPORT bool addVideoStream(const Image_VideoParams& theParams, const int theDefCodecId);
0121 
0122   //! Open video codec.
0123   Standard_EXPORT bool openVideoCodec(const Image_VideoParams& theParams);
0124 
0125   //! Write new video frame.
0126   Standard_EXPORT bool writeVideoFrame(const bool theToFlush);
0127 
0128 protected:
0129   //! AVRational alias.
0130   struct VideoRational
0131   {
0132     int num; //!< numerator
0133     int den; //!< denominator
0134   };
0135 
0136 protected:
0137   AVFormatContext* myAVContext;   //!< video context
0138   AVStream*        myVideoStream; //!< video stream
0139   AVCodec*         myVideoCodec;  //!< video codec
0140   AVCodecContext*  myCodecCtx;    //!< codec context (for FFmpeg 5.0+)
0141   AVFrame*         myFrame;       //!< frame to record
0142   SwsContext*      myScaleCtx;    //!< scale context for conversion from RGBA to YUV
0143 
0144   Image_PixMap  myImgSrcRgba; //!< input RGBA image
0145   VideoRational myFrameRate;  //!< video framerate
0146   int64_t       myFrameCount; //!< current frame index
0147 };
0148 
0149 #endif // Image_VideoRecorder_HeaderFile_