blob: 36e764d0e0a2bd307283ed0d581be06d86dee733 [file] [log] [blame]
agsantos1e7736c2020-10-28 14:39:13 -04001/**
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantos1e7736c2020-10-28 14:39:13 -04003 *
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 */
20
agsantos1e7736c2020-10-28 14:39:13 -040021#include "accel.h"
22
23extern "C" {
24#if LIBAVUTIL_VERSION_MAJOR < 56
25AVFrameSideData*
26av_frame_new_side_data_from_buf(AVFrame* frame, enum AVFrameSideDataType type, AVBufferRef* buf)
27{
28 auto side_data = av_frame_new_side_data(frame, type, 0);
29 av_buffer_unref(&side_data->buf);
30 side_data->buf = buf;
31 side_data->data = side_data->buf->data;
32 side_data->size = side_data->buf->size;
33 return side_data;
34}
35#endif
36}
37
38AVFrame*
agsantosc9181b42020-11-26 12:03:04 -050039transferToMainMemory(const AVFrame* framePtr, AVPixelFormat desiredFormat)
agsantos1e7736c2020-10-28 14:39:13 -040040{
agsantos1e7736c2020-10-28 14:39:13 -040041 auto desc = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(framePtr->format));
agsantos1e7736c2020-10-28 14:39:13 -040042 if (desc && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
agsantosc9181b42020-11-26 12:03:04 -050043 return av_frame_clone(framePtr);
agsantos1e7736c2020-10-28 14:39:13 -040044 }
45
Adrien Beraud087d5592023-03-06 11:22:33 -050046 AVFrame* out = av_frame_alloc();
agsantos1e7736c2020-10-28 14:39:13 -040047 out->format = desiredFormat;
48 if (av_hwframe_transfer_data(out, framePtr, 0) < 0) {
agsantosc9181b42020-11-26 12:03:04 -050049 av_frame_unref(out);
50 av_frame_free(&out);
51 return av_frame_clone(framePtr);
agsantos1e7736c2020-10-28 14:39:13 -040052 }
53
54 out->pts = framePtr->pts;
55 if (AVFrameSideData* side_data = av_frame_get_side_data(framePtr, AV_FRAME_DATA_DISPLAYMATRIX)) {
56 av_frame_new_side_data_from_buf(out,
57 AV_FRAME_DATA_DISPLAYMATRIX,
58 av_buffer_ref(side_data->buf));
59 }
60 return out;
61}