Debian Codegraph @

src/mpegts.c

raw symbols SCIP
1 /* librist. Copyright © 2020 SipRadius LLC. All right reserved.
2 * Author: Gijs Peskens <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "mpegts.h"
9 #include "udp-private.h"
10 #include "endian-shim.h"
11
12 int suppress_null_packets(const uint8_t payload_in[],uint8_t payload_out[], size_t *payload_len, struct rist_rtp_hdr_ext *header_ext) {
13 size_t packet_size = 188;
14 if (RIST_UNLIKELY(*payload_len % packet_size !=0)) {
15 packet_size = 204;
16 if (RIST_UNLIKELY(*payload_len % packet_size != 0)) {
17 return -1;
18 }
19 SET_BIT(header_ext->npd_bits, 7);
20 }
21 size_t count = *payload_len / packet_size;
22 if (RIST_UNLIKELY(count > 7))
23 return -1;
24 SET_BIT(header_ext->flags, 7);
25 size_t offset = 0;
26 int suppressed = 0;
27 for (int i = 0; i <= (int)count-1; i++) {
28 struct mpegts_header *hdr = (struct mpegts_header *)&payload_in[offset];
29 if (RIST_UNLIKELY(hdr->syncbyte != 0x47))
30 goto fail;
31 if (be16toh(hdr->flags1) == 0x1FFF) {
32 *payload_len -= packet_size;
33 SET_BIT(header_ext->npd_bits, (6 - i));
34 suppressed++;
35 }
36 offset += packet_size;
37 }
38
39 if (suppressed == 0)
40 return 0;
41
42 offset = 0;
43 size_t output_offset = 0;
44 for (int i = 0; i <= (int)count-1; i++) {
45 if (CHECK_BIT(header_ext->npd_bits, (6 - i)) == 0) {
46 memcpy(&payload_out[output_offset], &payload_in[offset], packet_size);
47 output_offset += packet_size;
48 }
49 offset += packet_size;
50 }
51
52 return (suppressed * (int)packet_size);
53 fail:
54 UNSET_BIT(header_ext->flags, 7);
55 return -1;
56 }
57
58 int expand_null_packets(uint8_t payload_in[], uint8_t payload_out[], size_t *payload_len, uint8_t npd_bits) {
59 size_t packet_size = CHECK_BIT(npd_bits, 7) == 0? 188: 204;
60
61 // Non-null data
62 int ts_count = (int)(*payload_len / packet_size);
63 // Null packets defined in header
64 int null_count = CHECK_BIT(npd_bits, 6) + CHECK_BIT(npd_bits, 5) + CHECK_BIT(npd_bits, 4) + CHECK_BIT(npd_bits, 3) + CHECK_BIT(npd_bits, 2) + CHECK_BIT(npd_bits, 1) + CHECK_BIT(npd_bits, 0);
65
66 if (null_count == 0)
67 return 0;
68
69 // npd_bits only encodes 7 positions; mirror suppress_null_packets()'s upper bound
70 if ((ts_count + null_count) > 7)
71 return 0;
72
73 size_t orig_payload_len = *payload_len;
74 size_t offset = 0;
75 ts_count += null_count;
76 *payload_len = ts_count * packet_size;
77 size_t input_offset = 0;
78 for (int i = 0; i <= (int)ts_count-1; i++) {
79 if (CHECK_BIT(npd_bits, (6 - i)) == 0) {
80 if (input_offset + packet_size > orig_payload_len) {
81 /* Failure mid-loop: restore the caller's length so the
82 * error return can't be mistaken for a successful
83 * expansion delivering unwritten scratch content. */
84 *payload_len = orig_payload_len;
85 return -1;
86 }
87 memcpy(&payload_out[offset], &payload_in[input_offset], packet_size);
88 input_offset += packet_size;
89 }
90 else {
91 struct mpegts_header * hdr = (struct mpegts_header *)&payload_out[offset];
92 memset(hdr, 0, sizeof(*hdr));
93 hdr->syncbyte = 0x47;
94 hdr->flags1 = htobe16(0x1FFF);
95 SET_BIT(hdr->flags2,4);
96 memset(&payload_out[offset + sizeof(*hdr)], 0xff, (packet_size - sizeof(*hdr)));
97 }
98 offset += packet_size;
99 }
100
101 return (null_count * (int)packet_size);
102 }