blob: 5bb32a1dee26bef5308b6e96b14fc06f0177c100 [file] [log] [blame]
agsantos66e1e4e2020-07-28 14:48:46 -04001
2
3#include <libavutil/pixdesc.h>
4#include <libavutil/hwcontext.h>
5#include <libavutil/frame.h>
6#include <libavutil/buffer.h>
7#include "pluglog.h"
8
9namespace DRing {
10class MediaFrame;
11class VideoFrame;
12}
13
14namespace jami {
15using MediaFrame = DRing::MediaFrame;
16using VideoFrame = DRing::VideoFrame;
17}
18
19extern "C" {
20#if LIBAVUTIL_VERSION_MAJOR < 56
21AVFrameSideData*
22av_frame_new_side_data_from_buf(AVFrame* frame, enum AVFrameSideDataType type, AVBufferRef* buf)
23{
24 auto side_data = av_frame_new_side_data(frame, type, 0);
25 av_buffer_unref(&side_data->buf);
26 side_data->buf = buf;
27 side_data->data = side_data->buf->data;
28 side_data->size = side_data->buf->size;
29 return side_data;
30}
31#endif
32}
33
34AVFrame*
35transferToMainMemory(AVFrame* framePtr, AVPixelFormat desiredFormat)
36{
37 AVFrame* out = nullptr;
38
39 auto desc = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(framePtr->format));
40 if (desc && not (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
41 out = framePtr;
42 return out;
43 }
44
45 out->format = desiredFormat;
46
47 if (av_hwframe_transfer_data(out, framePtr, 0) < 0) {
48 out = framePtr;
49 return out;
50 }
51
52 out->pts = framePtr->pts;
53 if (AVFrameSideData* side_data = av_frame_get_side_data(framePtr, AV_FRAME_DATA_DISPLAYMATRIX))
54 av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DISPLAYMATRIX, av_buffer_ref(side_data->buf));
55 return out;
56}