| 1 | /* |
| 2 | Copyright (C) 2012-2021 Harald Sitter <sitter@kde.org> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "videomemorystream.h" |
| 19 | |
| 20 | #include <vlc/plugins/vlc_picture.h> |
| 21 | |
| 22 | #include "mediaplayer.h" |
| 23 | #include "utils/debug.h" |
| 24 | |
| 25 | namespace Phonon { |
| 26 | namespace VLC { |
| 27 | |
| 28 | static inline VideoMemoryStream *p_this(void *opaque) { return static_cast<VideoMemoryStream *>(opaque); } |
| 29 | static inline VideoMemoryStream *p_this(void **opaque) { return static_cast<VideoMemoryStream *>(*opaque); } |
| 30 | #define P_THIS p_this(opaque) |
| 31 | |
| 32 | VideoMemoryStream::VideoMemoryStream() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | VideoMemoryStream::~VideoMemoryStream() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | unsigned VideoMemoryStream::setPitchAndLines(uint32_t fourcc, |
| 41 | unsigned width, unsigned height, |
| 42 | unsigned *pitches, unsigned *lines) |
| 43 | { |
| 44 | // Fairly unclear what the last two arguments do, they seem to make no diff for the planes though, so I guess they can be anything in our case. |
| 45 | const auto picture = picture_New(i_chroma: fourcc, i_width: width, i_height: height, i_sar_num: 0, i_sar_den: 1); |
| 46 | |
| 47 | unsigned bufferSize = 0; |
| 48 | |
| 49 | for (auto i = 0; i < picture->i_planes; ++i) { |
| 50 | const auto plane = picture->p[i]; |
| 51 | pitches[i] = plane.i_visible_pitch; |
| 52 | lines[i] = plane.i_visible_lines; |
| 53 | bufferSize += (pitches[i] * lines[i]); |
| 54 | } |
| 55 | |
| 56 | return bufferSize; |
| 57 | } |
| 58 | |
| 59 | void VideoMemoryStream::setCallbacks(MediaPlayer *player) |
| 60 | { |
| 61 | libvlc_video_set_callbacks(mp: player->libvlc_media_player(), |
| 62 | lock: lockCallbackInternal, |
| 63 | unlock: unlockCallbackInternal, |
| 64 | display: displayCallbackInternal, |
| 65 | opaque: this); |
| 66 | libvlc_video_set_format_callbacks(mp: player->libvlc_media_player(), |
| 67 | setup: formatCallbackInternal, |
| 68 | cleanup: formatCleanUpCallbackInternal); |
| 69 | } |
| 70 | |
| 71 | void VideoMemoryStream::unsetCallbacks(MediaPlayer *player) |
| 72 | { |
| 73 | libvlc_video_set_callbacks(mp: player->libvlc_media_player(), |
| 74 | lock: 0, |
| 75 | unlock: 0, |
| 76 | display: 0, |
| 77 | opaque: 0); |
| 78 | libvlc_video_set_format_callbacks(mp: player->libvlc_media_player(), |
| 79 | setup: 0, |
| 80 | cleanup: 0); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void *VideoMemoryStream::lockCallbackInternal(void *opaque, void **planes) |
| 85 | { |
| 86 | return P_THIS->lockCallback(planes); |
| 87 | } |
| 88 | |
| 89 | void VideoMemoryStream::unlockCallbackInternal(void *opaque, void *picture, void *const*planes) |
| 90 | { |
| 91 | P_THIS->unlockCallback(picture, planes); |
| 92 | } |
| 93 | |
| 94 | void VideoMemoryStream::displayCallbackInternal(void *opaque, void *picture) |
| 95 | { |
| 96 | P_THIS->displayCallback(picture); |
| 97 | } |
| 98 | |
| 99 | unsigned VideoMemoryStream::formatCallbackInternal(void **opaque, char *chroma, |
| 100 | unsigned *width, unsigned *height, |
| 101 | unsigned *pitches, unsigned *lines) |
| 102 | { |
| 103 | auto ret = P_THIS->formatCallback(chroma, width, height, pitches, lines); |
| 104 | |
| 105 | if (Debug::debugEnabled()) { |
| 106 | QStringList pitchValues; |
| 107 | QStringList lineValues; |
| 108 | unsigned *pitch = pitches; |
| 109 | unsigned *line = lines; |
| 110 | for (; *pitch != 0; ++pitch) { |
| 111 | Q_ASSERT(lines != 0); // pitch and line tables ought to be the same size |
| 112 | pitchValues << QString::number(*pitch); |
| 113 | lineValues << QString::number(*line); |
| 114 | } |
| 115 | const QString separator = QStringLiteral(", " ); |
| 116 | debug() << "vmem-format[chroma:" << chroma << "w:" << *width << "h:" << *height |
| 117 | << "pitches:" << pitchValues.join(sep: separator) << "lines:" << lineValues.join(sep: separator) |
| 118 | << "size:" << ret << "]" ; |
| 119 | } |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | void VideoMemoryStream::formatCleanUpCallbackInternal(void *opaque) |
| 125 | { |
| 126 | P_THIS->formatCleanUpCallback(); |
| 127 | } |
| 128 | |
| 129 | } // namespace VLC |
| 130 | } // namespace Phonon |
| 131 | |