1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2019 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 // activate some C99 macros like UINT64_C in "stdint.h" which used by FFmpeg
16 #ifndef __STDC_CONSTANT_MACROS
17 #define __STDC_CONSTANT_MACROS
20 #include <Media_Frame.hxx>
22 #include <Media_Scaler.hxx>
25 #include <Standard_WarningsDisable.hxx>
28 #include <libavcodec/avcodec.h>
29 #include <libavutil/imgutils.h>
31 #include <Standard_WarningsRestore.hxx>
34 IMPLEMENT_STANDARD_RTTIEXT(Media_Frame, Standard_Transient)
36 // =======================================================================
37 // function : FormatFFmpeg2Occt
39 // =======================================================================
40 Image_Format Media_Frame::FormatFFmpeg2Occt (int theFormat)
46 return Image_Format_RGBA;
48 return Image_Format_BGRA;
50 return Image_Format_RGB32;
52 return Image_Format_BGR32;
53 case AV_PIX_FMT_RGB24:
54 return Image_Format_RGB;
55 case AV_PIX_FMT_BGR24:
56 return Image_Format_BGR;
57 case AV_PIX_FMT_GRAY8:
58 return Image_Format_Gray;
60 return Image_Format_UNKNOWN;
64 return Image_Format_UNKNOWN;
68 // =======================================================================
69 // function : FormatOcct2FFmpeg
71 // =======================================================================
72 int Media_Frame::FormatOcct2FFmpeg (Image_Format theFormat)
77 case Image_Format_RGBA:
78 return AV_PIX_FMT_RGBA;
79 case Image_Format_BGRA:
80 return AV_PIX_FMT_BGRA;
81 case Image_Format_RGB32:
82 return AV_PIX_FMT_RGB0;
83 case Image_Format_BGR32:
84 return AV_PIX_FMT_BGR0;
85 case Image_Format_RGB:
86 return AV_PIX_FMT_RGB24;
87 case Image_Format_BGR:
88 return AV_PIX_FMT_BGR24;
89 case Image_Format_Gray:
90 return AV_PIX_FMT_GRAY8;
91 case Image_Format_Alpha:
92 return AV_PIX_FMT_GRAY8;
93 case Image_Format_GrayF:
94 case Image_Format_AlphaF:
95 case Image_Format_RGF:
96 case Image_Format_RGBAF:
97 case Image_Format_RGBF:
98 case Image_Format_BGRAF:
99 case Image_Format_BGRF:
100 case Image_Format_UNKNOWN:
101 return AV_PIX_FMT_NONE; // unsupported
103 return AV_PIX_FMT_NONE;
110 // =======================================================================
111 // function : Media_Frame
113 // =======================================================================
114 Media_Frame::Media_Frame()
121 myFrame = av_frame_alloc();
126 // =======================================================================
127 // function : ~Media_Frame
129 // =======================================================================
130 Media_Frame::~Media_Frame()
133 av_frame_free (&myFrame);
137 // =======================================================================
140 // =======================================================================
141 void Media_Frame::Unref()
144 av_frame_unref (myFrame);
148 // =======================================================================
149 // function : IsFullRangeYUV
151 // =======================================================================
152 bool Media_Frame::IsFullRangeYUV() const
155 return Format() == AV_PIX_FMT_YUVJ420P
156 || myFrame->color_range == AVCOL_RANGE_JPEG;
162 // =======================================================================
165 // =======================================================================
166 void Media_Frame::Swap (const Handle(Media_Frame)& theFrame1,
167 const Handle(Media_Frame)& theFrame2)
169 std::swap (theFrame1->myFrame, theFrame2->myFrame);
172 // =======================================================================
173 // function : IsEmpty
175 // =======================================================================
176 bool Media_Frame::IsEmpty() const
179 return myFrame->format == -1; // AV_PIX_FMT_NONE
185 // =======================================================================
188 // =======================================================================
189 int Media_Frame::SizeX() const
192 return myFrame->width;
198 // =======================================================================
201 // =======================================================================
202 int Media_Frame::SizeY() const
205 return myFrame->height;
211 // =======================================================================
214 // =======================================================================
215 int Media_Frame::Format() const
218 return myFrame->format;
224 // =======================================================================
227 // =======================================================================
228 uint8_t* Media_Frame::Plane (int thePlaneId) const
231 return myFrame->data[thePlaneId];
238 // =======================================================================
239 // function : LineSize
241 // =======================================================================
242 int Media_Frame::LineSize (int thePlaneId) const
245 return myFrame->linesize[thePlaneId];
252 // =======================================================================
253 // function : BestEffortTimestamp
255 // =======================================================================
256 int64_t Media_Frame::BestEffortTimestamp() const
259 return myFrame->best_effort_timestamp;
265 // =======================================================================
266 // function : InitWrapper
268 // =======================================================================
269 bool Media_Frame::InitWrapper (const Handle(Image_PixMap)& thePixMap)
272 if (thePixMap.IsNull())
278 myFrame->format = FormatOcct2FFmpeg (thePixMap->Format());
279 if (myFrame->format == AV_PIX_FMT_NONE)
284 myFrame->width = (int )thePixMap->SizeX();
285 myFrame->height = (int )thePixMap->SizeY();
286 myFrame->data[0] = (uint8_t* )thePixMap->ChangeData();
287 myFrame->linesize[0] = (int )thePixMap->SizeRowBytes();
288 for (int aPlaneIter = 1; aPlaneIter < AV_NUM_DATA_POINTERS; ++aPlaneIter)
290 myFrame->data [aPlaneIter] = NULL;
291 myFrame->linesize[aPlaneIter] = 0;