| 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 "h265-nal-packer.h" |
| 22 |
#include "h265-nal-unpacker.h" |
| 23 |
|
| 24 |
#include "h265-utils.h" |
| 25 |
|
| 26 |
using namespace std; |
| 27 |
|
| 28 |
namespace mediastreamer { |
| 29 |
|
| 30 |
H265NaluType::H265NaluType(uint8_t value) { |
| 31 |
if (0xc0 & value) throw out_of_range("H265 NALu type higher than 63"); |
| 32 |
_value = value; |
| 33 |
} |
| 34 |
|
| 35 |
const H265NaluType H265NaluType::IdrWRadl = 19; |
| 36 |
const H265NaluType H265NaluType::IdrNLp = 20; |
| 37 |
const H265NaluType H265NaluType::CraNut = 21; |
| 38 |
const H265NaluType H265NaluType::Vps = 32; |
| 39 |
const H265NaluType H265NaluType::Sps = 33; |
| 40 |
const H265NaluType H265NaluType::Pps = 34; |
| 41 |
const H265NaluType H265NaluType::Ap = 48; |
| 42 |
const H265NaluType H265NaluType::Fu = 49; |
| 43 |
|
| 44 |
void H265NaluHeader::setLayerId(uint8_t layerId) { |
| 45 |
if (layerId & 0xc0) throw out_of_range("H265 layer ID wider than 6 bits"); |
| 46 |
_layerId = layerId; |
| 47 |
} |
| 48 |
|
| 49 |
void H265NaluHeader::setTid(uint8_t tid) { |
| 50 |
if (tid & 0xf8) throw out_of_range("H265 layer ID wider than 3 bits"); |
| 51 |
_tid = tid; |
| 52 |
} |
| 53 |
|
| 54 |
bool H265NaluHeader::operator==(const H265NaluHeader &h2) const { |
| 55 |
return _fBit == h2._fBit && _type == h2._type && _tid == h2._tid && _layerId == h2._layerId; |
| 56 |
} |
| 57 |
|
| 58 |
void H265NaluHeader::parse(const uint8_t *header) { |
| 59 |
uint16_t header2 = ntohs(*reinterpret_cast<const uint16_t *>(header)); |
| 60 |
_tid = header2 & 0x07; |
| 61 |
header2 >>= 3; |
| 62 |
_layerId = header2 & 0x3f; |
| 63 |
header2 >>= 6; |
| 64 |
_type = header2 & 0x3f; |
| 65 |
header2 >>= 6; |
| 66 |
_fBit = (header2 != 0); |
| 67 |
} |
| 68 |
|
| 69 |
mblk_t *H265NaluHeader::forge() const { |
| 70 |
uint16_t header = _fBit ? 1 : 0; |
| 71 |
header <<= 6; |
| 72 |
header |= _type; |
| 73 |
header <<= 6; |
| 74 |
header |= _layerId; |
| 75 |
header <<= 3; |
| 76 |
header |= _tid; |
| 77 |
header = htons(header); |
| 78 |
|
| 79 |
mblk_t *newHeader = allocb(2, 0); |
| 80 |
*reinterpret_cast<uint16_t *>(newHeader->b_wptr) = header; |
| 81 |
newHeader->b_wptr += 2; |
| 82 |
return newHeader; |
| 83 |
} |
| 84 |
|
| 85 |
void H265FuHeader::parse(const uint8_t *header) { |
| 86 |
uint8_t header2 = *header; |
| 87 |
_type = header2 & 0x3f; |
| 88 |
header2 >>= 6; |
| 89 |
bool end = ((header2 & 0x01) != 0); |
| 90 |
header2 >>= 1; |
| 91 |
bool start = ((header2 & 0x01) != 0); |
| 92 |
|
| 93 |
if (start && end) throw invalid_argument("parsing an FU header with both start and end flags enabled"); |
| 94 |
|
| 95 |
if (start) { |
| 96 |
_pos = Position::Start; |
| 97 |
} else if (end) { |
| 98 |
_pos = Position::End; |
| 99 |
} else { |
| 100 |
_pos = Position::Middle; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
mblk_t *H265FuHeader::forge() const { |
| 105 |
uint8_t header = (_pos == Position::Start ? 1 : 0); |
| 106 |
header <<= 1; |
| 107 |
header |= (_pos == Position::End ? 1 : 0); |
| 108 |
header <<= 6; |
| 109 |
header |= _type; |
| 110 |
|
| 111 |
mblk_t *newHeader = allocb(1, 0); |
| 112 |
*newHeader->b_wptr++ = header; |
| 113 |
return newHeader; |
| 114 |
} |
| 115 |
|
| 116 |
void H265ParameterSetsInserter::process(MSQueue *in, MSQueue *out) { |
| 117 |
H265NaluHeader header; |
| 118 |
bool isKeyFrame = false; |
| 119 |
while (mblk_t *m = ms_queue_get(in)) { |
| 120 |
header.parse(m->b_rptr); |
| 121 |
if (header.getType() == H265NaluType::Vps) { |
| 122 |
replaceParameterSet(_vps, m); |
| 123 |
} else if (header.getType() == H265NaluType::Sps) { |
| 124 |
replaceParameterSet(_sps, m); |
| 125 |
} else if (header.getType() == H265NaluType::Pps) { |
| 126 |
replaceParameterSet(_pps, m); |
| 127 |
} else { |
| 128 |
if (header.getType().isKeyFramePart()) { |
| 129 |
ms_message("H265ParameterSetsInserter: I-frame detected"); |
| 130 |
isKeyFrame = true; |
| 131 |
} |
| 132 |
ms_queue_put(out, m); |
| 133 |
} |
| 134 |
} |
| 135 |
if (isKeyFrame) { |
| 136 |
mblk_t *insPoint = ms_queue_peek_first(out); |
| 137 |
ms_queue_insert(out, insPoint, dupmsg(_vps)); |
| 138 |
ms_queue_insert(out, insPoint, dupmsg(_sps)); |
| 139 |
ms_queue_insert(out, insPoint, dupmsg(_pps)); |
| 140 |
ms_message("H265ParameterSetsInserter: parameter sets inserted"); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void H265ParameterSetsInserter::flush() { |
| 145 |
replaceParameterSet(_vps, nullptr); |
| 146 |
replaceParameterSet(_sps, nullptr); |
| 147 |
replaceParameterSet(_pps, nullptr); |
| 148 |
} |
| 149 |
|
| 150 |
H26xNaluHeader *H265ToolFactory::createNaluHeader() const { |
| 151 |
return new H265NaluHeader(); |
| 152 |
} |
| 153 |
|
| 154 |
NalPacker *H265ToolFactory::createNalPacker(size_t maxPayloadType) const { |
| 155 |
return new H265NalPacker(maxPayloadType); |
| 156 |
} |
| 157 |
|
| 158 |
NalUnpacker *H265ToolFactory::createNalUnpacker() const { |
| 159 |
return new H265NalUnpacker(); |
| 160 |
} |
| 161 |
|
| 162 |
H26xParameterSetsInserter *H265ToolFactory::createParameterSetsInserter() const { |
| 163 |
return new H265ParameterSetsInserter(); |
| 164 |
} |
| 165 |
|
| 166 |
H26xParameterSetsStore *H265ToolFactory::createParameterSetsStore() const { |
| 167 |
return new H265ParameterSetsStore(); |
| 168 |
} |
| 169 |
|
| 170 |
} // namespace mediastreamer |