blob: dc3f9f71d6424c58524d9fc17a20626d5af9f9d4 [file] [log] [blame]
agsantos5aa39652020-08-11 18:18:04 -04001/**
2 * Copyright (C) 2020 Savoir-faire Linux Inc.
3 *
4 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
agsantos66e1e4e2020-07-28 14:48:46 -040020
agsantos5aa39652020-08-11 18:18:04 -040021extern "C" {
agsantos66e1e4e2020-07-28 14:48:46 -040022#include <libavutil/pixdesc.h>
23#include <libavutil/hwcontext.h>
24#include <libavutil/frame.h>
25#include <libavutil/buffer.h>
agsantos5aa39652020-08-11 18:18:04 -040026}
agsantos66e1e4e2020-07-28 14:48:46 -040027
28namespace DRing {
29class MediaFrame;
30class VideoFrame;
agsantosac1940d2020-09-17 10:18:40 -040031} // namespace DRing
agsantos66e1e4e2020-07-28 14:48:46 -040032
33namespace jami {
34using MediaFrame = DRing::MediaFrame;
35using VideoFrame = DRing::VideoFrame;
agsantosac1940d2020-09-17 10:18:40 -040036} // namespace jami
agsantos66e1e4e2020-07-28 14:48:46 -040037
38extern "C" {
39#if LIBAVUTIL_VERSION_MAJOR < 56
40AVFrameSideData*
agsantosac1940d2020-09-17 10:18:40 -040041av_frame_new_side_data_from_buf(AVFrame* frame, enum AVFrameSideDataType type, AVBufferRef* buf)
agsantos66e1e4e2020-07-28 14:48:46 -040042{
43 auto side_data = av_frame_new_side_data(frame, type, 0);
44 av_buffer_unref(&side_data->buf);
45 side_data->buf = buf;
46 side_data->data = side_data->buf->data;
47 side_data->size = side_data->buf->size;
48 return side_data;
49}
50#endif
51}
52
53AVFrame*
agsantosac1940d2020-09-17 10:18:40 -040054transferToMainMemory(AVFrame* framePtr, AVPixelFormat desiredFormat)
agsantos66e1e4e2020-07-28 14:48:46 -040055{
agsantos5aa39652020-08-11 18:18:04 -040056 AVFrame* out;
agsantos66e1e4e2020-07-28 14:48:46 -040057
58 auto desc = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(framePtr->format));
agsantos5aa39652020-08-11 18:18:04 -040059
agsantosac1940d2020-09-17 10:18:40 -040060 if (desc && not(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
agsantos66e1e4e2020-07-28 14:48:46 -040061 out = framePtr;
62 return out;
63 }
64
65 out->format = desiredFormat;
agsantos66e1e4e2020-07-28 14:48:46 -040066 if (av_hwframe_transfer_data(out, framePtr, 0) < 0) {
67 out = framePtr;
68 return out;
69 }
70
71 out->pts = framePtr->pts;
agsantos5aa39652020-08-11 18:18:04 -040072 if (AVFrameSideData* side_data = av_frame_get_side_data(framePtr, AV_FRAME_DATA_DISPLAYMATRIX)) {
agsantosac1940d2020-09-17 10:18:40 -040073 av_frame_new_side_data_from_buf(out,
74 AV_FRAME_DATA_DISPLAYMATRIX,
75 av_buffer_ref(side_data->buf));
agsantos5aa39652020-08-11 18:18:04 -040076 }
agsantos66e1e4e2020-07-28 14:48:46 -040077 return out;
78}