Debian Codegraph @

src/videofilters/h26x/h265-nal-packer.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-nal-packer.h"
22
23 using namespace std;
24
25 namespace mediastreamer {
26
27 H265NalPacker::NaluAggregator::~NaluAggregator() {
28 if (_ap) freemsg(_ap);
29 }
30
31 mblk_t *H265NalPacker::NaluAggregator::feed(mblk_t *nalu) {
32 H265NaluHeader header(nalu->b_rptr);
33 if (_ap == nullptr) {
34 placeFirstNalu(nalu);
35 } else {
36 if (H265NaluHeader::length + _size + 2 + msgdsize(nalu) > _maxSize) {
37 mblk_t *m = completeAggregation();
38 placeFirstNalu(nalu);
39 return m;
40 } else {
41 aggregate(nalu);
42 }
43 }
44 return nullptr;
45 }
46
47 void H265NalPacker::NaluAggregator::reset() {
48 if (_ap) freemsg(_ap);
49 }
50
51 mblk_t *H265NalPacker::NaluAggregator::completeAggregation() {
52 mblk_t *m;
53
54 if (_ap == nullptr) return nullptr;
55
56 if (_apHeader.getType() != H265NaluType::Ap) {
57 m = _ap;
58 } else {
59 m = _apHeader.forge();
60 concatb(m, _ap);
61 msgpullup(m, -1);
62 }
63 _ap = nullptr;
64 return m;
65 }
66
67 void H265NalPacker::NaluAggregator::placeFirstNalu(mblk_t *nalu) {
68 H265NaluHeader header(nalu->b_rptr);
69 _ap = nalu;
70 _apHeader = header;
71 _size = msgdsize(nalu);
72 }
73
74 void H265NalPacker::NaluAggregator::aggregate(mblk_t *nalu) {
75 H265NaluHeader header(nalu->b_rptr);
76 _apHeader.setFBit(_apHeader.getFBit() || header.getFBit());
77 _apHeader.setType(H265NaluType::Ap);
78 _apHeader.setLayerId(min(_apHeader.getLayerId(), header.getLayerId()));
79 _apHeader.setTid(min(_apHeader.getTid(), header.getTid()));
80
81 mblk_t *size = allocb(2, 0);
82 uint16_t *sizePtr = reinterpret_cast<uint16_t *>(size->b_wptr);
83 *sizePtr = htons(uint16_t(msgdsize(nalu)));
84 size->b_wptr += 2;
85
86 _size += (msgdsize(size) + msgdsize(nalu)); // warning: this line must remain before 'concatb()' invocations.
87
88 concatb(_ap, size);
89 concatb(_ap, nalu);
90 }
91
92 void H265NalPacker::NaluSpliter::feed(mblk_t *nalu) {
93 if (msgdsize(nalu) <= _maxSize) return;
94
95 H265NaluHeader naluHeader(nalu->b_rptr);
96 nalu->b_rptr += H265NaluHeader::length;
97
98 H265FuHeader fuHeader;
99 fuHeader.setType(naluHeader.getType());
100 naluHeader.setType(H265NaluType::Fu);
101
102 const size_t maxFuPayloadSize = _maxSize - H265NaluHeader::length - H265FuHeader::length;
103 while (msgdsize(nalu) > maxFuPayloadSize) {
104 ms_queue_put(&_q, makeFu(naluHeader, fuHeader, nalu->b_rptr, maxFuPayloadSize));
105 fuHeader.setPosition(H265FuHeader::Position::Middle);
106 nalu->b_rptr += maxFuPayloadSize;
107 }
108 fuHeader.setPosition(H265FuHeader::Position::End);
109 ms_queue_put(&_q, makeFu(naluHeader, fuHeader, nalu->b_rptr, msgdsize(nalu)));
110
111 freemsg(nalu);
112 }
113
114 mblk_t *H265NalPacker::NaluSpliter::makeFu(const H265NaluHeader &naluHeader,
115 const H265FuHeader &fuHeader,
116 const uint8_t *payload,
117 size_t length) {
118 mblk_t *naluHeaderBuf = naluHeader.forge();
119 mblk_t *fuHeaderBuf = fuHeader.forge();
120 mblk_t *payloadBuf = allocb(length, 0);
121 memcpy(payloadBuf->b_wptr, payload, length);
122 payloadBuf->b_wptr += length;
123
124 mblk_t *fu = naluHeaderBuf;
125 concatb(fu, fuHeaderBuf);
126 concatb(fu, payloadBuf);
127 msgpullup(fu, -1);
128 return fu;
129 }
130
131 } // namespace mediastreamer