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 // activate some C99 macros like UINT64_C in "stdint.h" which used by FFmpeg
17 #ifndef __STDC_CONSTANT_MACROS
18 #define __STDC_CONSTANT_MACROS
21 #include <Image_VideoRecorder.hxx>
23 #include <Message.hxx>
24 #include <Message_Messenger.hxx>
28 // Suppress deprecation warnings - it is difficult to provide compatibility with old and new API at once
29 // since new APIs are introduced too often.
30 // Should be disabled from time to time to clean up usage of old APIs.
31 #if (defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
32 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
34 Standard_DISABLE_DEPRECATION_WARNINGS
40 // suppress some common warnings in FFmpeg headers
41 #pragma warning(disable : 4244)
44 #include <libavcodec/avcodec.h>
45 #include <libavformat/avformat.h>
46 #include <libswscale/swscale.h>
47 #include <libavutil/imgutils.h>
50 #pragma warning(default : 4244)
54 // Undefine macro that clashes with name used by field of Image_VideoParams;
55 // this macro is defined in headers of older versions of libavutil
56 // (see definition of macro FF_API_PIX_FMT in version.h)
63 IMPLEMENT_STANDARD_RTTIEXT(Image_VideoRecorder, Standard_Transient)
65 //=============================================================================
66 //function : Constructor
68 //=============================================================================
69 Image_VideoRecorder::Image_VideoRecorder()
81 // initialize libavcodec, and register all codecs and formats, should be done once
86 //=============================================================================
87 //function : ~Image_VideoRecorder
89 //=============================================================================
90 Image_VideoRecorder::~Image_VideoRecorder()
95 //=============================================================================
96 //function : formatAvError
98 //=============================================================================
99 TCollection_AsciiString Image_VideoRecorder::formatAvError (const int theError) const
102 char anErrBuf[AV_ERROR_MAX_STRING_SIZE] = {};
103 av_strerror (theError, anErrBuf, AV_ERROR_MAX_STRING_SIZE);
106 return TCollection_AsciiString(theError);
110 //=============================================================================
113 //=============================================================================
114 void Image_VideoRecorder::Close()
117 if (myScaleCtx != NULL)
119 sws_freeContext (myScaleCtx);
123 if (myAVContext == NULL)
129 // Write the trailer, if any. The trailer must be written before you close the CodecContexts open when you wrote the header;
130 // otherwise av_write_trailer() may try to use memory that was freed on av_codec_close().
131 if (myFrameCount != 0)
133 av_write_trailer (myAVContext);
138 if (myVideoStream != NULL)
140 avcodec_close (myVideoStream->codec);
141 myVideoStream = NULL;
145 av_free (myFrame->data[0]);
146 av_frame_free (&myFrame);
150 if (!(myAVContext->oformat->flags & AVFMT_NOFILE))
152 // close the output file
153 avio_close (myAVContext->pb);
157 avformat_free_context (myAVContext);
162 //=============================================================================
165 //=============================================================================
166 Standard_Boolean Image_VideoRecorder::Open (const char* theFileName,
167 const Image_VideoParams& theParams)
171 if (theParams.Width <= 0
172 || theParams.Height <= 0)
174 return Standard_False;
177 // allocate the output media context
178 avformat_alloc_output_context2 (&myAVContext, NULL, theParams.Format.IsEmpty() ? NULL : theParams.Format.ToCString(), theFileName);
179 if (myAVContext == NULL)
181 ::Message::DefaultMessenger()->Send ("ViewerTest_VideoRecorder, could not deduce output format from file extension", Message_Fail);
182 return Standard_False;
185 // add the audio stream using the default format codecs and initialize the codecs
186 if (!addVideoStream (theParams, myAVContext->oformat->video_codec))
189 return Standard_False;
192 // open video codec and allocate the necessary encode buffers
193 if (!openVideoCodec (theParams))
196 return Standard_False;
200 av_dump_format (myAVContext, 0, theFileName, 1);
203 // open the output file, if needed
204 if ((myAVContext->oformat->flags & AVFMT_NOFILE) == 0)
206 const int aResAv = avio_open (&myAVContext->pb, theFileName, AVIO_FLAG_WRITE);
209 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: could not open '") + theFileName + "', " + formatAvError (aResAv);
210 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
212 return Standard_False;
216 // write the stream header, if any
217 const int aResAv = avformat_write_header (myAVContext, NULL);
220 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not open output file '") + theFileName + "', " + formatAvError (aResAv);
221 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
223 return Standard_False;
229 return Standard_True;
232 //=============================================================================
233 //function : addVideoStream
235 //=============================================================================
236 Standard_Boolean Image_VideoRecorder::addVideoStream (const Image_VideoParams& theParams,
237 const Standard_Integer theDefCodecId)
239 myFrameRate.num = theParams.FpsNum;
240 myFrameRate.den = theParams.FpsDen;
244 TCollection_AsciiString aCodecName;
245 if (!theParams.VideoCodec.IsEmpty())
247 myVideoCodec = avcodec_find_encoder_by_name (theParams.VideoCodec.ToCString());
248 aCodecName = theParams.VideoCodec;
252 myVideoCodec = avcodec_find_encoder ((AVCodecID )theDefCodecId);
253 aCodecName = avcodec_get_name ((AVCodecID )theDefCodecId);
255 if (myVideoCodec == NULL)
257 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not find encoder for ") + aCodecName;
258 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
259 return Standard_False;
262 const AVCodecID aCodecId = myVideoCodec->id;
263 myVideoStream = avformat_new_stream (myAVContext, myVideoCodec);
264 if (myVideoStream == NULL)
266 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not allocate stream!");
267 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
268 return Standard_False;
270 myVideoStream->id = myAVContext->nb_streams - 1;
272 AVCodecContext* aCodecCtx = myVideoStream->codec;
273 aCodecCtx->codec_id = aCodecId;
274 // resolution must be a multiple of two
275 aCodecCtx->width = theParams.Width;
276 aCodecCtx->height = theParams.Height;
277 // Timebase is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
278 // For fixed-fps content, timebase should be 1/framerate and timestamp increments should be identical to 1.
279 aCodecCtx->time_base.den = myFrameRate.num;
280 aCodecCtx->time_base.num = myFrameRate.den;
281 aCodecCtx->gop_size = 12; // emit one intra frame every twelve frames at most
283 // some formats want stream headers to be separate
284 if (myAVContext->oformat->flags & AVFMT_GLOBALHEADER)
286 aCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
288 return Standard_True;
291 (void )theDefCodecId;
292 return Standard_False;
296 //=============================================================================
297 //function : openVideoCodec
299 //=============================================================================
300 Standard_Boolean Image_VideoRecorder::openVideoCodec (const Image_VideoParams& theParams)
303 AVDictionary* anOptions = NULL;
304 AVCodecContext* aCodecCtx = myVideoStream->codec;
306 // setup default values
307 aCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
308 //av_dict_set (&anOptions, "threads", "auto", 0);
309 if (aCodecCtx->codec == avcodec_find_encoder_by_name ("mpeg2video"))
311 // just for testing, we also add B frames
312 aCodecCtx->max_b_frames = 2;
313 aCodecCtx->bit_rate = 6000000;
315 else if (aCodecCtx->codec == avcodec_find_encoder_by_name ("mpeg4"))
319 else if (aCodecCtx->codec == avcodec_find_encoder_by_name ("mjpeg"))
321 aCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
322 aCodecCtx->qmin = aCodecCtx->qmax = 5;
324 else if (aCodecCtx->codec == avcodec_find_encoder_by_name ("huffyuv"))
326 aCodecCtx->pix_fmt = AV_PIX_FMT_RGB24;
328 else if (aCodecCtx->codec == avcodec_find_encoder_by_name ("png"))
330 aCodecCtx->pix_fmt = AV_PIX_FMT_RGB24;
331 aCodecCtx->compression_level = 9; // 0..9
333 else if (aCodecCtx->codec == avcodec_find_encoder_by_name ("h264")
334 || aCodecCtx->codec == avcodec_find_encoder_by_name ("libx264"))
336 // use CRF (Constant Rate Factor) as best single-pass compressing method
337 av_dict_set (&anOptions, "crf", "20", 0); // quality 18-28, 23 is default (normal), 18 is almost lossless
338 av_dict_set (&anOptions, "preset", "slow", 0); // good compression (see also "veryslow", "ultrafast")
341 //av_dict_set (&anOptions, "qp", "0", 0); // instead of crf
342 //av_dict_set (&anOptions, "preset", "ultrafast", 0);
344 // compatibility with devices
345 //av_dict_set (&anOptions, "profile", "baseline", 0);
346 //av_dict_set (&anOptions, "level", "3.0", 0);
348 else if (aCodecCtx->codec == avcodec_find_encoder_by_name ("vp8")
349 || aCodecCtx->codec == avcodec_find_encoder_by_name ("vp9"))
351 av_dict_set (&anOptions, "crf", "20", 0); // quality 4-63, 10 is normal
354 // override defaults with specified options
355 if (!theParams.PixelFormat.IsEmpty())
357 const AVPixelFormat aPixFormat = av_get_pix_fmt (theParams.PixelFormat.ToCString());
358 if (aPixFormat == AV_PIX_FMT_NONE)
360 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: unknown pixel format has been specified '") + theParams.PixelFormat + "'";
361 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
362 return Standard_False;
365 aCodecCtx->pix_fmt = aPixFormat;
366 for (Resource_DataMapOfAsciiStringAsciiString::Iterator aParamIter (theParams.VideoCodecParams);
367 aParamIter.More(); aParamIter.Next())
369 av_dict_set (&anOptions, aParamIter.Key().ToCString(), aParamIter.Value().ToCString(), 0);
374 int aResAv = avcodec_open2 (aCodecCtx, myVideoCodec, &anOptions);
375 if (anOptions != NULL)
377 av_dict_free (&anOptions);
381 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not open video codec, ") + formatAvError (aResAv);
382 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
383 return Standard_False;
386 // allocate and init a re-usable frame
387 myFrame = av_frame_alloc();
390 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not allocate video frame!");
391 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
392 return Standard_False;
395 // allocate the encoded raw picture
396 aResAv = av_image_alloc (myFrame->data, myFrame->linesize,
397 aCodecCtx->width, aCodecCtx->height, aCodecCtx->pix_fmt, 1);
400 memset (myFrame->data, 0, sizeof(myFrame->data));
401 memset (myFrame->linesize, 0, sizeof(myFrame->linesize));
402 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not allocate picture ")
403 + aCodecCtx->width+ "x" + aCodecCtx->height + ", " + formatAvError (aResAv);
404 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
405 return Standard_False;
407 // copy data and linesize picture pointers to frame
408 myFrame->format = aCodecCtx->pix_fmt;
409 myFrame->width = aCodecCtx->width;
410 myFrame->height = aCodecCtx->height;
412 const Standard_Size aStride = aCodecCtx->width + 16 - (aCodecCtx->width % 16);
413 if (!myImgSrcRgba.InitZero (Image_PixMap::ImgRGBA, aCodecCtx->width, aCodecCtx->height, aStride))
415 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not allocate RGBA32 picture ")
416 + aCodecCtx->width+ "x" + aCodecCtx->height;
417 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
418 return Standard_False;
421 myScaleCtx = sws_getContext (aCodecCtx->width, aCodecCtx->height, AV_PIX_FMT_RGBA,
422 aCodecCtx->width, aCodecCtx->height, aCodecCtx->pix_fmt,
423 SWS_BICUBIC, NULL, NULL, NULL);
424 if (myScaleCtx == NULL)
426 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not initialize the conversion context!");
427 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
428 return Standard_False;
430 return Standard_True;
433 return Standard_False;
437 //=============================================================================
438 //function : writeVideoFrame
440 //=============================================================================
441 Standard_Boolean Image_VideoRecorder::writeVideoFrame (const Standard_Boolean theToFlush)
445 AVCodecContext* aCodecCtx = myVideoStream->codec;
448 uint8_t* aSrcData[4] = { (uint8_t*)myImgSrcRgba.ChangeData(), NULL, NULL, NULL };
449 int aSrcLinesize[4] = { (int )myImgSrcRgba.SizeRowBytes(), 0, 0, 0 };
450 sws_scale (myScaleCtx,
451 aSrcData, aSrcLinesize,
452 0, aCodecCtx->height,
453 myFrame->data, myFrame->linesize);
457 memset (&aPacket, 0, sizeof(aPacket));
458 av_init_packet (&aPacket);
461 myFrame->pts = myFrameCount;
463 aResAv = avcodec_encode_video2 (aCodecCtx, &aPacket, theToFlush ? NULL : myFrame, &isGotPacket);
466 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not encode video frame, ") + formatAvError (aResAv);
467 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
468 return Standard_False;
471 // if size is zero, it means the image was buffered
474 const AVRational& aTimeBase = aCodecCtx->time_base;
476 // rescale output packet timestamp values from codec to stream timebase
477 aPacket.pts = av_rescale_q_rnd (aPacket.pts, aTimeBase, myVideoStream->time_base, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
478 aPacket.dts = av_rescale_q_rnd (aPacket.dts, aTimeBase, myVideoStream->time_base, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
479 aPacket.duration = av_rescale_q (aPacket.duration, aTimeBase, myVideoStream->time_base);
480 aPacket.stream_index = myVideoStream->index;
482 // write the compressed frame to the media file
483 aResAv = av_interleaved_write_frame (myAVContext, &aPacket);
493 const TCollection_AsciiString aMsg = TCollection_AsciiString ("Error: can not write video frame, ") + formatAvError (aResAv);
494 ::Message::DefaultMessenger()->Send (aMsg, Message_Fail);
495 return Standard_False;
499 return Standard_True;
502 return Standard_False;