Debian Codegraph @

src/videofilters/h26x/h265-nal-unpacker.cpp

raw
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-utils.h"
22
23 #include "h265-nal-unpacker.h"
24
25 using namespace std;
26
27 namespace mediastreamer {
28
29 mblk_t *H265NalUnpacker::FuAggregator::feed(mblk_t *packet) {
30 if (packet->b_wptr - packet->b_rptr < 3) {
31 ms_error("Dropping H265 FU packet smaller than 3 bytes");
32 freemsg(packet);
33 return nullptr;
34 }
35
36 H265NaluHeader naluHeader(packet->b_rptr);
37 packet->b_rptr += 2;
38
39 H265FuHeader fuHeader(packet->b_rptr++);
40 naluHeader.setType(fuHeader.getType());
41
42 if (fuHeader.getPosition() == H265FuHeader::Position::Start && isAggregating()) {
43 ms_error("receiving start FU packet while aggregating. Dropping the under construction NALu");
44 reset();
45 _m = packet;
46 return nullptr;
47 }
48
49 if (fuHeader.getPosition() != H265FuHeader::Position::Start && !isAggregating()) {
50 ms_error("receiving continuation FU packet while aggregation hasn't been started. Doping packet");
51 freemsg(packet);
52 return nullptr;
53 }
54
55 if (fuHeader.getPosition() == H265FuHeader::Position::Start) {
56 _m = naluHeader.forge();
57 }
58
59 concatb(_m, packet);
60
61 if (fuHeader.getPosition() == H265FuHeader::Position::End) {
62 return completeAggregation();
63 } else return nullptr;
64 }
65
66 void H265NalUnpacker::FuAggregator::reset() {
67 if (_m != nullptr) freemsg(_m);
68 _m = nullptr;
69 }
70
71 mblk_t *H265NalUnpacker::FuAggregator::completeAggregation() {
72 if (!isAggregating()) return nullptr;
73 mblk_t *res = _m;
74 msgpullup(res, -1);
75 _m = nullptr;
76 return res;
77 }
78
79 void H265NalUnpacker::ApSpliter::feed(mblk_t *packet) {
80 ms_queue_flush(&_q);
81
82 if (packet->b_wptr - packet->b_rptr < 2) {
83 ms_error("Dropping H265 aggregation packet smaller than 2 bytes");
84 freemsg(packet);
85 return;
86 }
87
88 const uint8_t *it;
89 for (it = packet->b_rptr + 2; it < packet->b_wptr;) {
90 if (packet->b_wptr - it < 2) break;
91 uint16_t naluSize = ntohs(*reinterpret_cast<const uint16_t *>(it));
92 it += 2;
93
94 if (it + naluSize > packet->b_wptr) break;
95 mblk_t *m = allocb(naluSize, 0);
96 memcpy(m->b_wptr, it, naluSize);
97 m->b_wptr += naluSize;
98 ms_queue_put(&_q, m);
99
100 it += naluSize;
101 }
102
103 if (it != packet->b_wptr) {
104 ms_error("Dropping H265 aggregation packet containing truncated NALus");
105 ms_queue_flush(&_q);
106 }
107
108 freemsg(packet);
109 }
110
111 NalUnpacker::PacketType H265NalUnpacker::getNaluType(const mblk_t *nalu) const {
112 H265NaluHeader header(nalu->b_rptr);
113 if (header.getType() == H265NaluType::Ap) {
114 return PacketType::AggregationPacket;
115 } else if (header.getType() == H265NaluType::Fu) {
116 return PacketType::FragmentationUnit;
117 } else {
118 return PacketType::SingleNalUnit;
119 }
120 }
121
122 } // namespace mediastreamer