agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 1 | /** |
| 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 | */ |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 20 | |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 21 | extern "C" { |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 22 | #include <libavutil/pixdesc.h> |
| 23 | #include <libavutil/hwcontext.h> |
| 24 | #include <libavutil/frame.h> |
| 25 | #include <libavutil/buffer.h> |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 26 | } |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 27 | |
| 28 | namespace DRing { |
| 29 | class MediaFrame; |
| 30 | class VideoFrame; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 31 | } // namespace DRing |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 32 | |
| 33 | namespace jami { |
| 34 | using MediaFrame = DRing::MediaFrame; |
| 35 | using VideoFrame = DRing::VideoFrame; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 36 | } // namespace jami |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 37 | |
| 38 | extern "C" { |
| 39 | #if LIBAVUTIL_VERSION_MAJOR < 56 |
| 40 | AVFrameSideData* |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 41 | av_frame_new_side_data_from_buf(AVFrame* frame, enum AVFrameSideDataType type, AVBufferRef* buf) |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 42 | { |
| 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 | |
| 53 | AVFrame* |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 54 | transferToMainMemory(AVFrame* framePtr, AVPixelFormat desiredFormat) |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 55 | { |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 56 | AVFrame* out; |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 57 | |
| 58 | auto desc = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(framePtr->format)); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 59 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 60 | if (desc && not(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 61 | out = framePtr; |
| 62 | return out; |
| 63 | } |
| 64 | |
| 65 | out->format = desiredFormat; |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 66 | if (av_hwframe_transfer_data(out, framePtr, 0) < 0) { |
| 67 | out = framePtr; |
| 68 | return out; |
| 69 | } |
| 70 | |
| 71 | out->pts = framePtr->pts; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 72 | if (AVFrameSideData* side_data = av_frame_get_side_data(framePtr, AV_FRAME_DATA_DISPLAYMATRIX)) { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 73 | av_frame_new_side_data_from_buf(out, |
| 74 | AV_FRAME_DATA_DISPLAYMATRIX, |
| 75 | av_buffer_ref(side_data->buf)); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 76 | } |
agsantos | 66e1e4e | 2020-07-28 14:48:46 -0400 | [diff] [blame] | 77 | return out; |
| 78 | } |