| 1 |
/* |
| 2 |
* Copyright (c) 2010-2022 Belledonne Communications SARL. |
| 3 |
* |
| 4 |
* This file is part of mediastreamer2 |
| 5 |
* (see https://gitlab.linphone.org/BC/public/mediastreamer2). |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "media-codec-h264-decoder.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <cstring> |
| 25 |
|
| 26 |
#include <sys/system_properties.h> |
| 27 |
|
| 28 |
#include "bctoolbox/crypto.h" |
| 29 |
|
| 30 |
#include "filter-wrapper/decoding-filter-wrapper.h" |
| 31 |
#include "h264-nal-unpacker.h" |
| 32 |
#include "h264-utils.h" |
| 33 |
#include "h26x-decoder-filter.h" |
| 34 |
|
| 35 |
using namespace std; |
| 36 |
|
| 37 |
namespace mediastreamer { |
| 38 |
|
| 39 |
MediaCodecH264Decoder::MediaCodecH264Decoder() : MediaCodecDecoder("video/avc") { |
| 40 |
DeviceInfo info = getDeviceInfo(); |
| 41 |
ms_message("MediaCodecH264Decoder: got device info: %s", info.toString().c_str()); |
| 42 |
if (find(_tvDevices.cbegin(), _tvDevices.cend(), info) != _tvDevices.cend()) { |
| 43 |
ms_message("MediaCodecH264Decoder: found exact device, enabling reset on new SPS/PPS mode"); |
| 44 |
_resetOnPsReceiving = true; |
| 45 |
} else { |
| 46 |
for (auto it = _tvDevices.cbegin(); it != _tvDevices.cend(); it++) { |
| 47 |
if (info.weakEquals(*it)) { |
| 48 |
ms_message( |
| 49 |
"MediaCodecH264Decoder: found matching manufacturer/platform, enabling reset on new SPS/PPS mode"); |
| 50 |
_resetOnPsReceiving = true; |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
MediaCodecH264Decoder::~MediaCodecH264Decoder() { |
| 58 |
if (_lastSps) freemsg(_lastSps); |
| 59 |
} |
| 60 |
|
| 61 |
bool MediaCodecH264Decoder::setParameterSets(MSQueue *parameterSet, uint64_t timestamp) { |
| 62 |
if (_resetOnPsReceiving) { |
| 63 |
for (mblk_t *m = ms_queue_peek_first(parameterSet); !ms_queue_end(parameterSet, m); |
| 64 |
m = ms_queue_next(parameterSet, m)) { |
| 65 |
MSH264NaluType type = ms_h264_nalu_get_type(m); |
| 66 |
if (type == MSH264NaluTypeSPS && isNewPps(m)) { |
| 67 |
int32_t curWidth, curHeight; |
| 68 |
AMediaFormat_getInt32(_format, "width", &curWidth); |
| 69 |
AMediaFormat_getInt32(_format, "height", &curHeight); |
| 70 |
MSVideoSize vsize = ms_h264_sps_get_video_size(m); |
| 71 |
if (vsize.width != curWidth || vsize.height != curHeight) { |
| 72 |
ms_message( |
| 73 |
"MediaCodecDecoder: restarting decoder because the video size has changed (%dx%d->%dx%d)", |
| 74 |
curWidth, curHeight, vsize.width, vsize.height); |
| 75 |
AMediaFormat_setInt32(_format, "width", vsize.width); |
| 76 |
AMediaFormat_setInt32(_format, "height", vsize.height); |
| 77 |
stopImpl(); |
| 78 |
startImpl(); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
return MediaCodecDecoder::setParameterSets(parameterSet, timestamp); |
| 84 |
} |
| 85 |
|
| 86 |
bool MediaCodecH264Decoder::DeviceInfo::operator==(const DeviceInfo &info) const { |
| 87 |
return this->manufacturer == info.manufacturer && this->model == info.model && this->platform == info.platform; |
| 88 |
} |
| 89 |
|
| 90 |
bool MediaCodecH264Decoder::DeviceInfo::weakEquals(const DeviceInfo &info) const { |
| 91 |
return this->manufacturer == info.manufacturer && this->platform == info.platform; |
| 92 |
} |
| 93 |
|
| 94 |
std::string MediaCodecH264Decoder::DeviceInfo::toString() const { |
| 95 |
ostringstream os; |
| 96 |
os << "{ '" << this->manufacturer << "', '" << this->model << "', '" << this->platform << "' }"; |
| 97 |
return os.str(); |
| 98 |
} |
| 99 |
|
| 100 |
bool MediaCodecH264Decoder::isNewPps(mblk_t *sps) { |
| 101 |
if (_lastSps == nullptr) { |
| 102 |
_lastSps = dupmsg(sps); |
| 103 |
return true; |
| 104 |
} |
| 105 |
const size_t spsSize = size_t(sps->b_wptr - sps->b_rptr); |
| 106 |
const size_t lastSpsSize = size_t(_lastSps->b_wptr - _lastSps->b_rptr); |
| 107 |
if (spsSize != lastSpsSize || memcmp(_lastSps->b_rptr, sps->b_rptr, spsSize) != 0) { |
| 108 |
freemsg(_lastSps); |
| 109 |
_lastSps = dupmsg(sps); |
| 110 |
return true; |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
MediaCodecH264Decoder::DeviceInfo MediaCodecH264Decoder::getDeviceInfo() { |
| 116 |
const size_t propSize = 256; |
| 117 |
char manufacturer[propSize]; |
| 118 |
char model[propSize]; |
| 119 |
char platform[propSize]; |
| 120 |
|
| 121 |
if (__system_property_get("ro.product.manufacturer", manufacturer) < 0) manufacturer[0] = '\0'; |
| 122 |
if (__system_property_get("ro.product.model", model) < 0) model[0] = '\0'; |
| 123 |
if (__system_property_get("ro.board.platform", platform) < 0) platform[0] = '\0'; |
| 124 |
|
| 125 |
return DeviceInfo({manufacturer, model, platform}); |
| 126 |
} |
| 127 |
|
| 128 |
class MediaCodecH264DecoderFilterImpl : public H26xDecoderFilter { |
| 129 |
public: |
| 130 |
MediaCodecH264DecoderFilterImpl(MSFilter *f) : H26xDecoderFilter(f, new MediaCodecH264Decoder()) { |
| 131 |
} |
| 132 |
~MediaCodecH264DecoderFilterImpl() { |
| 133 |
if (_sps) freemsg(_sps); |
| 134 |
if (_pps) freemsg(_pps); |
| 135 |
} |
| 136 |
|
| 137 |
void process() { |
| 138 |
if (_sps && _pps) { |
| 139 |
static_cast<H264NalUnpacker &>(*_unpacker).setOutOfBandSpsPps(_sps, _pps); |
| 140 |
_sps = nullptr; |
| 141 |
_pps = nullptr; |
| 142 |
} |
| 143 |
H26xDecoderFilter::process(); |
| 144 |
} |
| 145 |
|
| 146 |
void addFmtp(const char *fmtp) { |
| 147 |
char value[256]; |
| 148 |
size_t value_size = sizeof(value); |
| 149 |
if (fmtp_get_value(fmtp, "sprop-parameter-sets", value, value_size)) { |
| 150 |
char *b64_sps = value; |
| 151 |
char *b64_pps = strchr(value, ','); |
| 152 |
|
| 153 |
if (b64_pps) { |
| 154 |
*b64_pps = '\0'; |
| 155 |
++b64_pps; |
| 156 |
ms_message("Got sprop-parameter-sets : sps=%s , pps=%s", b64_sps, b64_pps); |
| 157 |
_sps = allocb(sizeof(value), 0); |
| 158 |
value_size = sizeof(value); |
| 159 |
bctbx_base64_decode(_sps->b_wptr, &value_size, (unsigned char *)b64_sps, strlen(b64_sps)); |
| 160 |
_sps->b_wptr += value_size; |
| 161 |
_pps = allocb(sizeof(value), 0); |
| 162 |
value_size = sizeof(value); |
| 163 |
bctbx_base64_decode(_pps->b_wptr, &value_size, (unsigned char *)b64_pps, strlen(b64_pps)); |
| 164 |
_pps->b_wptr += value_size; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
private: |
| 170 |
void updateSps(mblk_t *sps) { |
| 171 |
if (_sps) freemsg(_sps); |
| 172 |
_sps = dupb(sps); |
| 173 |
} |
| 174 |
|
| 175 |
void updatePps(mblk_t *pps) { |
| 176 |
if (_pps) freemsg(_pps); |
| 177 |
if (pps) _pps = dupb(pps); |
| 178 |
else _pps = nullptr; |
| 179 |
} |
| 180 |
|
| 181 |
bool checkSpsChange(mblk_t *sps) { |
| 182 |
bool ret = false; |
| 183 |
if (_sps) { |
| 184 |
ret = (msgdsize(sps) != msgdsize(_sps)) || (memcmp(_sps->b_rptr, sps->b_rptr, msgdsize(sps)) != 0); |
| 185 |
|
| 186 |
if (ret) { |
| 187 |
ms_message("MediaCodecDecoder: SPS changed ! %i,%i", (int)msgdsize(sps), (int)msgdsize(_sps)); |
| 188 |
updateSps(sps); |
| 189 |
updatePps(nullptr); |
| 190 |
} |
| 191 |
} else { |
| 192 |
ms_message("MediaCodecDecoder: receiving first SPS"); |
| 193 |
updateSps(sps); |
| 194 |
} |
| 195 |
return ret; |
| 196 |
} |
| 197 |
|
| 198 |
bool checkPpsChange(mblk_t *pps) { |
| 199 |
bool ret = false; |
| 200 |
if (_pps) { |
| 201 |
ret = (msgdsize(pps) != msgdsize(_pps)) || (memcmp(_pps->b_rptr, pps->b_rptr, msgdsize(pps)) != 0); |
| 202 |
|
| 203 |
if (ret) { |
| 204 |
ms_message("MediaCodecDecoder: PPS changed ! %i,%i", (int)msgdsize(pps), (int)msgdsize(_pps)); |
| 205 |
updatePps(pps); |
| 206 |
} |
| 207 |
} else { |
| 208 |
ms_message("MediaCodecDecoder: receiving first PPS"); |
| 209 |
updatePps(pps); |
| 210 |
} |
| 211 |
return ret; |
| 212 |
} |
| 213 |
|
| 214 |
mblk_t *_sps = nullptr; |
| 215 |
mblk_t *_pps = nullptr; |
| 216 |
}; |
| 217 |
|
| 218 |
const std::vector<const MediaCodecH264Decoder::DeviceInfo> MediaCodecH264Decoder::_tvDevices = { |
| 219 |
{"Amlogic", "Quad-Core Enjoy TV Box", "gxl"}, |
| 220 |
{"rockchip", "X9-LX", "rk3288"}, |
| 221 |
{"rockchip", "rk3288", "rk3288"}, |
| 222 |
{"rockchip", "rk3399", "rk3399"}, |
| 223 |
{"rockchip", "rk3399pro", "rk3399pro"}, |
| 224 |
{"rockchip", "rk3368", "rk3368"}, |
| 225 |
{"rockchip", "Sasincomm S09", "rk3126c"}, |
| 226 |
{"freescale", "Control4-imx8mm", "imx8"}}; |
| 227 |
|
| 228 |
} // namespace mediastreamer |
| 229 |
|
| 230 |
using namespace mediastreamer; |
| 231 |
|
| 232 |
MS_DECODING_FILTER_WRAPPER_METHODS_DECLARATION(MediaCodecH264Decoder); |
| 233 |
MS_DECODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(MediaCodecH264Decoder, |
| 234 |
MS_MEDIACODEC_H264_DEC_ID, |
| 235 |
"A H264 decoder based on MediaCodec API.", |
| 236 |
"H264", |
| 237 |
MS_FILTER_IS_PUMP | MS_FILTER_IS_HW_ACCELERATED); |