agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 1 | /** |
Sébastien Blin | cb783e3 | 2021-02-12 11:34:10 -0500 | [diff] [blame] | 2 | * Copyright (C) 2020-2021 Savoir-faire Linux Inc. |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 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 | */ |
| 20 | |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 21 | #include "accel.h" |
| 22 | |
| 23 | extern "C" { |
| 24 | #if LIBAVUTIL_VERSION_MAJOR < 56 |
| 25 | AVFrameSideData* |
| 26 | av_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 | |
| 38 | AVFrame* |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 39 | transferToMainMemory(const AVFrame* framePtr, AVPixelFormat desiredFormat) |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 40 | { |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 41 | auto desc = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(framePtr->format)); |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 42 | if (desc && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 43 | return av_frame_clone(framePtr); |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Adrien Beraud | 087d559 | 2023-03-06 11:22:33 -0500 | [diff] [blame] | 46 | AVFrame* out = av_frame_alloc(); |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 47 | out->format = desiredFormat; |
| 48 | if (av_hwframe_transfer_data(out, framePtr, 0) < 0) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 49 | av_frame_unref(out); |
| 50 | av_frame_free(&out); |
| 51 | return av_frame_clone(framePtr); |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 52 | } |
| 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 | } |