Debian Codegraph @

src/videofilters/av1/obu/obu-packer.cpp

raw
1 /*
2 * Copyright (c) 2010-2023 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 "obu-packer.h"
22
23 #include <algorithm>
24 #include <stdexcept>
25
26 using namespace std;
27
28 namespace mediastreamer {
29
30 ObuPacker::ObuPacker(size_t maxPayloadSize) : mMaxPayloadSize(maxPayloadSize) {
31 }
32
33 void ObuPacker::setMaxPayloadSize(size_t size) {
34 mMaxPayloadSize = size;
35 }
36
37 void ObuPacker::enableAggregation(bool enable) {
38 mAggregationEnabled = enable;
39 }
40
41 void ObuPacker::pack(MSQueue *input, MSQueue *output, uint32_t ts) {
42 while (mblk_t *m = ms_queue_get(input)) {
43 int size = msgdsize(m);
44
45 vector<ParsedObu> obus;
46
47 int currentPos = 0;
48 while (currentPos < size) {
49 try {
50 auto obu = parseNextObu(m->b_rptr + currentPos, size - currentPos);
51
52 // TD Obus are dropped
53 if (obu.type != OBP_OBU_TEMPORAL_DELIMITER) {
54 obus.push_back(obu);
55 }
56
57 currentPos += obu.size;
58 } catch (runtime_error &e) {
59 ms_error("ObuPacker: Failed to parse OBU header: %s", e.what());
60 obus.clear();
61 break;
62 }
63 }
64
65 if (!obus.empty()) sendObus(obus, output, ts);
66
67 freemsg(m);
68 }
69 }
70
71 ObuPacker::ParsedObu ObuPacker::parseNextObu(uint8_t *buf, size_t size) {
72 char errBuf[1024];
73 ptrdiff_t offset;
74 size_t obuSize;
75 int temporalId, spatialId;
76 OBPOBUType obuType;
77 OBPError err = {&errBuf[0], 1024};
78
79 if (obp_get_next_obu(buf, size, &obuType, &offset, &obuSize, &temporalId, &spatialId, &err) < 0) {
80 throw runtime_error(err.error);
81 }
82 // else {
83 // ms_message("ObuPacker [%p]: type = %d, size = %d, temporal = %d, spatial = %d", this, obuType,
84 // (int)(obuSize + offset), temporalId, spatialId);
85 // }
86
87 return {obuType, buf, obuSize + offset};
88 }
89
90 void ObuPacker::sendObus(vector<ParsedObu> &obus, MSQueue *output, uint32_t ts) {
91 for (size_t i = 0, e = obus.size(); i != e; ++i) {
92 const auto &obu = obus[i];
93
94 if (obu.size + 1 < mMaxPayloadSize) {
95 mblk_t *m = makePacket(obu.start, obu.size + 1, false, false, (i == 0), (i == (e - 1)), ts);
96 ms_queue_put(output, m);
97 } else {
98 size_t remainingSize = obu.size;
99 bool firstPacket = true;
100
101 do {
102 size_t minSize = std::min<size_t>(remainingSize + 1, mMaxPayloadSize);
103
104 mblk_t *m = makePacket(obu.start + obu.size - remainingSize, minSize, !firstPacket,
105 (remainingSize + 1 > mMaxPayloadSize), (firstPacket && i == 0),
106 (i == (e - 1) && minSize < mMaxPayloadSize), ts);
107 ms_queue_put(output, m);
108
109 if (firstPacket) firstPacket = false;
110
111 remainingSize -= minSize - 1;
112 } while (remainingSize > 0);
113 }
114 }
115 }
116
117 mblk_t *ObuPacker::makePacket(uint8_t *buf, size_t size, bool Z, bool Y, bool N, bool marker, uint32_t ts) {
118 mblk_t *m = allocb(size, -1);
119
120 memcpy(m->b_rptr + 1, buf, size - 1);
121 m->b_wptr += size;
122
123 uint8_t *agHdr = m->b_rptr;
124 *agHdr = 0;
125
126 *agHdr |= (Z ? 0x1 : 0x0) << 7; // Z
127 *agHdr |= (Y ? 0x1 : 0x0) << 6; // Y
128 *agHdr |= 0x1 << 4; // W
129 *agHdr |= (N ? 0x1 : 0x0) << 3; // N
130
131 mblk_set_timestamp_info(m, ts);
132 mblk_set_marker_info(m, marker ? 1 : 0);
133
134 return m;
135 }
136
137 } // namespace mediastreamer