agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame^] | 1 | |
| 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 | |
| 9 | namespace DRing { |
| 10 | class MediaFrame; |
| 11 | class VideoFrame; |
| 12 | } |
| 13 | |
| 14 | namespace jami { |
| 15 | using MediaFrame = DRing::MediaFrame; |
| 16 | using VideoFrame = DRing::VideoFrame; |
| 17 | } |
| 18 | |
| 19 | extern "C" { |
| 20 | #if LIBAVUTIL_VERSION_MAJOR < 56 |
| 21 | AVFrameSideData* |
| 22 | av_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 | |
| 34 | AVFrame* |
| 35 | transferToMainMemory(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 | } |