Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:05

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 <Resource_DataMapOfAsciiStringAsciiString.hxx>
0021 #include <Standard_Transient.hxx>
0022 #include <TCollection_AsciiString.hxx>
0023 
0024 // forward declarations
0025 struct AVFormatContext;
0026 struct AVStream;
0027 struct AVCodec;
0028 struct AVFrame;
0029 struct SwsContext;
0030 
0031 // Undefine macro that clashes with name used by field of Image_VideoParams;
0032 // this macro is defined in headers of older versions of libavutil
0033 // (see definition of macro FF_API_PIX_FMT in version.h)
0034 #ifdef PixelFormat
0035 #undef PixelFormat
0036 #endif
0037 
0038 //! Auxiliary structure defining video parameters.
0039 //! Please refer to FFmpeg documentation for defining text values.
0040 struct Image_VideoParams
0041 {
0042   TCollection_AsciiString Format;           //!< [optional]  video format (container), if empty - will be determined from the file name
0043   TCollection_AsciiString VideoCodec;       //!< [optional]  codec identifier, if empty - default codec from file format will be used
0044   TCollection_AsciiString PixelFormat;      //!< [optional]  pixel format, if empty - default codec pixel format will be used
0045   Standard_Integer        Width;            //!< [mandatory] video frame width
0046   Standard_Integer        Height;           //!< [mandatory] video frame height
0047   Standard_Integer        FpsNum;           //!< [mandatory] framerate numerator
0048   Standard_Integer        FpsDen;           //!< [mandatory] framerate denumerator
0049   Resource_DataMapOfAsciiStringAsciiString
0050                           VideoCodecParams; //!< map of advanced video codec parameters
0051 
0052   //! Empty constructor.
0053   Image_VideoParams() : Width (0), Height (0), FpsNum (0), FpsDen (1) {}
0054 
0055   //! Setup playback FPS.
0056   void SetFramerate (const Standard_Integer theNumerator,
0057                      const Standard_Integer theDenominator)
0058   {
0059     FpsNum = theNumerator;
0060     FpsDen = theDenominator;
0061   }
0062 
0063   //! Setup playback FPS.
0064   //! For fixed-fps content, timebase should be 1/framerate and timestamp increments should be identical to 1.
0065   void SetFramerate (const Standard_Integer theValue)
0066   {
0067     FpsNum = theValue;
0068     FpsDen = 1;
0069   }
0070 };
0071 
0072 //! Video recording tool based on FFmpeg framework.
0073 class Image_VideoRecorder : public Standard_Transient
0074 {
0075   DEFINE_STANDARD_RTTIEXT(Image_VideoRecorder, Standard_Transient)
0076 public:
0077 
0078   //! Empty constructor.
0079   Standard_EXPORT Image_VideoRecorder();
0080 
0081   //! Destructor.
0082   Standard_EXPORT virtual ~Image_VideoRecorder();
0083 
0084   //! Close the stream - stop recorder.
0085   Standard_EXPORT void Close();
0086 
0087   //! Open output stream - initialize recorder.
0088   //! @param theFileName [in] video filename
0089   //! @param theParams   [in] video parameters
0090   Standard_EXPORT Standard_Boolean Open (const char* theFileName,
0091                                          const Image_VideoParams& theParams);
0092 
0093   //! Access RGBA frame, should NOT be re-initialized outside.
0094   //! Note that image is expected to have upper-left origin.
0095   Image_PixMap& ChangeFrame() { return myImgSrcRgba; }
0096 
0097   //! Return current frame index.
0098   int64_t FrameCount() const { return myFrameCount; }
0099 
0100   //! Push new frame, should be called after Open().
0101   Standard_Boolean PushFrame()
0102   {
0103     return writeVideoFrame (Standard_False);
0104   }
0105 
0106 protected:
0107 
0108   //! Wrapper for av_strerror().
0109   Standard_EXPORT TCollection_AsciiString formatAvError (const int theError) const;
0110 
0111   //! Append video stream.
0112   //! theParams     [in] video parameters
0113   //! theDefCodecId [in] identifier of codec managed by FFmpeg library (AVCodecID enum)
0114   Standard_EXPORT Standard_Boolean addVideoStream (const Image_VideoParams& theParams,
0115                                                    const Standard_Integer   theDefCodecId);
0116 
0117   //! Open video codec.
0118   Standard_EXPORT Standard_Boolean openVideoCodec (const Image_VideoParams& theParams);
0119 
0120   //! Write new video frame.
0121   Standard_EXPORT Standard_Boolean writeVideoFrame (const Standard_Boolean theToFlush);
0122 
0123 protected:
0124 
0125   //! AVRational alias.
0126   struct VideoRational
0127   {
0128     int num; //!< numerator
0129     int den; //!< denominator
0130   };
0131 
0132 protected:
0133 
0134   AVFormatContext* myAVContext;   //!< video context
0135   AVStream*        myVideoStream; //!< video stream
0136   AVCodec*         myVideoCodec;  //!< video codec
0137   AVFrame*         myFrame;       //!< frame to record
0138   SwsContext*      myScaleCtx;    //!< scale context for conversion from RGBA to YUV
0139 
0140   Image_PixMap     myImgSrcRgba;  //!< input RGBA image
0141   VideoRational    myFrameRate;   //!< video framerate
0142   int64_t          myFrameCount;  //!< current frame index
0143 
0144 };
0145 
0146 DEFINE_STANDARD_HANDLE(Image_VideoRecorder, Standard_Transient)
0147 
0148 #endif // Image_VideoRecorder_HeaderFile_