| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 62 |
int ts_count = (int)(*payload_len / packet_size); |
| 63 |
|
| 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 |
|
| 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 |
return -1; |
| 82 |
memcpy(&payload_out[offset], &payload_in[input_offset], packet_size); |
| 83 |
input_offset += packet_size; |
| 84 |
} |
| 85 |
else { |
| 86 |
struct mpegts_header * hdr = (struct mpegts_header *)&payload_out[offset]; |
| 87 |
memset(hdr, 0, sizeof(*hdr)); |
| 88 |
hdr->syncbyte = 0x47; |
| 89 |
hdr->flags1 = htobe16(0x1FFF); |
| 90 |
SET_BIT(hdr->flags2,4); |
| 91 |
memset(&payload_out[offset + sizeof(*hdr)], 0xff, (packet_size - sizeof(*hdr))); |
| 92 |
} |
| 93 |
offset += packet_size; |
| 94 |
} |
| 95 |
|
| 96 |
return (null_count * (int)packet_size); |
| 97 |
} |