| 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 "h264-utils.h" |
| 22 |
|
| 23 |
#include "h264-nal-unpacker.h" |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
|
| 27 |
namespace mediastreamer { |
| 28 |
|
| 29 |
// ======================= |
| 30 |
// H264FUAAggregator class |
| 31 |
// ======================= |
| 32 |
mblk_t *H264FuaAggregator::feed(mblk_t *im) { |
| 33 |
mblk_t *om = nullptr; |
| 34 |
uint8_t fu_header; |
| 35 |
uint8_t nri, type; |
| 36 |
bool_t start, end; |
| 37 |
bool_t marker = mblk_get_marker_info(im); |
| 38 |
|
| 39 |
fu_header = im->b_rptr[1]; |
| 40 |
type = fu_header & 0x17; |
| 41 |
start = fu_header >> 7; |
| 42 |
end = (fu_header >> 6) & 0x1; |
| 43 |
if (start) { |
| 44 |
mblk_t *new_header; |
| 45 |
nri = ms_h264_nalu_get_nri(im); |
| 46 |
if (_m != nullptr) { |
| 47 |
ms_error("receiving FU-A start while previous FU-A is not " |
| 48 |
"finished"); |
| 49 |
freemsg(_m); |
| 50 |
_m = nullptr; |
| 51 |
} |
| 52 |
im->b_rptr += 2; /*skip the nal header and the fu header*/ |
| 53 |
new_header = allocb(1, 0); /* allocate small fragment to put the correct nal header, this is to avoid to write |
| 54 |
on the buffer which can break processing of other users of the buffers */ |
| 55 |
H264Tools::nalHeaderInit(new_header->b_wptr, nri, type); |
| 56 |
new_header->b_wptr++; |
| 57 |
mblk_meta_copy(im, new_header); |
| 58 |
concatb(new_header, im); |
| 59 |
_m = new_header; |
| 60 |
} else { |
| 61 |
if (_m != nullptr) { |
| 62 |
im->b_rptr += 2; |
| 63 |
concatb(_m, im); |
| 64 |
} else { |
| 65 |
ms_error("Receiving continuation FU packet but no start."); |
| 66 |
freemsg(im); |
| 67 |
} |
| 68 |
} |
| 69 |
if (end && _m) { |
| 70 |
msgpullup(_m, -1); |
| 71 |
om = _m; |
| 72 |
mblk_set_marker_info(om, marker); /*set the marker bit of this aggregated NAL as the last fragment received.*/ |
| 73 |
_m = nullptr; |
| 74 |
} |
| 75 |
return om; |
| 76 |
} |
| 77 |
|
| 78 |
void H264FuaAggregator::reset() { |
| 79 |
if (_m) { |
| 80 |
freemsg(_m); |
| 81 |
_m = nullptr; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
mblk_t *H264FuaAggregator::completeAggregation() { |
| 86 |
mblk_t *res = _m; |
| 87 |
_m = nullptr; |
| 88 |
return res; |
| 89 |
} |
| 90 |
|
| 91 |
// ===================== |
| 92 |
// H264StapASlicer class |
| 93 |
// ===================== |
| 94 |
|
| 95 |
void H264StapaSpliter::feed(mblk_t *im) { |
| 96 |
uint16_t sz; |
| 97 |
for (uint8_t *p = im->b_rptr + 1; p < im->b_wptr;) { |
| 98 |
memcpy(&sz, p, 2); |
| 99 |
sz = ntohs(sz); |
| 100 |
mblk_t *nal = dupb(im); |
| 101 |
p += 2; |
| 102 |
nal->b_rptr = p; |
| 103 |
p += sz; |
| 104 |
nal->b_wptr = p; |
| 105 |
if (p > im->b_wptr) { |
| 106 |
ms_error("Malformed STAP-A packet"); |
| 107 |
freemsg(nal); |
| 108 |
break; |
| 109 |
} |
| 110 |
ms_queue_put(&_q, nal); |
| 111 |
} |
| 112 |
freemsg(im); |
| 113 |
} |
| 114 |
|
| 115 |
//================================================== |
| 116 |
// Rfc3984Unpacker |
| 117 |
//================================================== |
| 118 |
|
| 119 |
// Public methods |
| 120 |
// -------------- |
| 121 |
|
| 122 |
H264NalUnpacker::~H264NalUnpacker() { |
| 123 |
if (_sps != nullptr) freemsg(_sps); |
| 124 |
if (_pps != nullptr) freemsg(_pps); |
| 125 |
} |
| 126 |
|
| 127 |
void H264NalUnpacker::setOutOfBandSpsPps(mblk_t *sps, mblk_t *pps) { |
| 128 |
if (_sps) freemsg(_sps); |
| 129 |
if (_pps) freemsg(_pps); |
| 130 |
_sps = sps; |
| 131 |
_pps = pps; |
| 132 |
} |
| 133 |
|
| 134 |
// Private methods |
| 135 |
// --------------- |
| 136 |
NalUnpacker::PacketType H264NalUnpacker::getNaluType(const mblk_t *nalu) const { |
| 137 |
switch (ms_h264_nalu_get_type(nalu)) { |
| 138 |
case MSH264NaluTypeFUA: |
| 139 |
return PacketType::FragmentationUnit; |
| 140 |
case MSH264NaluTypeSTAPA: |
| 141 |
return PacketType::AggregationPacket; |
| 142 |
default: |
| 143 |
return PacketType::SingleNalUnit; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
H264NalUnpacker::Status H264NalUnpacker::outputFrame(MSQueue *out, const Status &flags) { |
| 148 |
if (_status.isKeyFrame && _sps && _pps) { |
| 149 |
/*prepend out of band provided sps and pps*/ |
| 150 |
ms_queue_put(out, _sps); |
| 151 |
ms_queue_put(out, _pps); |
| 152 |
_sps = NULL; |
| 153 |
_pps = NULL; |
| 154 |
} |
| 155 |
return NalUnpacker::outputFrame(out, flags); |
| 156 |
} |
| 157 |
|
| 158 |
} // namespace mediastreamer |