Skip to content

Commit

Permalink
Remove unnecessary buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
xtne6f committed Oct 21, 2023
1 parent c8568cd commit d600cb4
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions tsmemseg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,20 @@ void ProcessSegmentation(FILE *fp, bool enableFragmentation, uint32_t targetDura
bool firstAudioPacketArrived = false;
bool isFirstKey = true;
PAT pat = {};
uint8_t buf[188 * 16];
int countForOnRead = 0;
uint8_t buf[188];
size_t bufCount = 0;
size_t nRead;

while ((nRead = fread(buf + bufCount, 1, sizeof(buf) - bufCount, fp)) != 0) {
bufCount += nRead;
if (bufCount < sizeof(buf)) {
continue;
}
bufCount = 0;

if (onRead) {
if (onRead && ++countForOnRead == 16) {
countForOnRead = 0;
int64_t ptsDiff = (0x200000000 + pts - lastSegPts) & 0x1ffffffff;
if (ptsDiff >= 0x100000000) {
// PTS went back.
Expand All @@ -460,12 +466,14 @@ void ProcessSegmentation(FILE *fp, bool enableFragmentation, uint32_t targetDura
}
}

for (const uint8_t *packet = buf; packet < buf + sizeof(buf) && packet + 188 <= buf + bufCount; packet += 188) {
if (extract_ts_header_sync(packet) != 0x47) {
// Resynchronization is not implemented.
++syncError;
continue;
}
if (extract_ts_header_sync(buf) != 0x47) {
// Resynchronization is not implemented.
++syncError;
continue;
}

{
const uint8_t *packet = buf;
int unitStart = extract_ts_header_unit_start(packet);
int pid = extract_ts_header_pid(packet);
int counter = extract_ts_header_counter(packet);
Expand Down Expand Up @@ -634,11 +642,6 @@ void ProcessSegmentation(FILE *fp, bool enableFragmentation, uint32_t targetDura
}
packets.insert(packets.end(), packet, packet + 188);
}

if (bufCount >= 188 && bufCount % 188 != 0) {
std::copy(buf + bufCount / 188 * 188, buf + bufCount, buf);
}
bufCount %= 188;
}
}
}
Expand Down

0 comments on commit d600cb4

Please sign in to comment.