Skip to content

Commit fbc9e89

Browse files
committed
MTSDecoder
1 parent 133e6a9 commit fbc9e89

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

src/AudioTools/AudioCodecs/CodecMTS.h

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class MTSDecoder : public AudioDecoder {
194194
TRACED();
195195
int count = 0;
196196
while (parse()) {
197-
LOGI("demux: # %d", ++count);
197+
LOGI("demux: step #%d with PES #%d", ++count, (int)pes_count);
198198
}
199199
LOGI("Number of demux calls: %d", count);
200200
}
@@ -215,11 +215,15 @@ class MTSDecoder : public AudioDecoder {
215215
bool parse() {
216216
int pos = syncPos();
217217
if (pos < 0) return false;
218+
if (pos != 0) {
219+
LOGW("Sync byte not found at position 0. Skipping %d bytes", pos);
220+
buffer.clearArray(pos);
221+
}
218222
// parse data
219223
parsePacket();
220-
// assert(pos == 0);
224+
221225
// remove processed data
222-
buffer.clearArray(pos == 0 ? TS_PACKET_SIZE : pos);
226+
buffer.clearArray(TS_PACKET_SIZE);
223227
return true;
224228
}
225229

@@ -230,15 +234,21 @@ class MTSDecoder : public AudioDecoder {
230234
int pid = ((packet[1] & 0x1F) << 8) | (packet[2] & 0xFF);
231235
LOGI("PID: 0x%04X(%d)", pid, pid);
232236
bool payloadUnitStartIndicator = false;
233-
bool hasAdaptionField = false;
237+
uint8_t adaptionField = (packet[3] & 0x30) >> 4;
234238
int adaptationSize = 0;
235239
int offset = 4; // Start after TS header (4 bytes)
240+
bool to_process = true;
236241

237242
// Check for adaptation field
238-
if (packet[3] & 0x20) { // Adaptation field exists
239-
adaptationSize += packet[4] + 1;
243+
// 00 (0) → Invalid (should never happen).
244+
// 01 (1) → Payload only (no adaptation field).
245+
// 10 (2) → Adaptation field only (no payload).
246+
// 11 (3) → Adaptation field + payload.
247+
if (adaptionField == 0b11) { // Adaptation field exists
248+
adaptationSize = packet[4] + 1;
240249
offset += adaptationSize;
241-
hasAdaptionField = true;
250+
} else if (adaptionField == 0b10) { // Adopation field only
251+
to_process = false;
242252
}
243253

244254
// If PUSI is set, there's a pointer field (skip it)
@@ -248,12 +258,12 @@ class MTSDecoder : public AudioDecoder {
248258
}
249259

250260
LOGI("Payload Unit Start Indicator (PUSI): %d", payloadUnitStartIndicator);
251-
LOGI("Adaption Field Control: 0x%x / size: %d", hasAdaptionField,
261+
LOGI("Adaption Field Control: 0x%x / size: %d", adaptionField,
252262
adaptationSize);
253263

254264
// if we are at the beginning we start with a pat
255265
if (pid == 0 && payloadUnitStartIndicator) {
256-
pids.clear();
266+
// pids.clear();
257267
}
258268

259269
int payloadStart = offset;
@@ -264,9 +274,8 @@ class MTSDecoder : public AudioDecoder {
264274
parsePAT(&packet[payloadStart], len);
265275
} else if (pid == pmt_pid && packet[payloadStart] == 0x02) {
266276
parsePMT(&packet[payloadStart], len);
267-
} else if (!is_adts_missing && pids.contains(pid)) {
268-
parsePES(&packet[payloadStart], len, payloadUnitStartIndicator,
269-
hasAdaptionField);
277+
} else if (!is_adts_missing && to_process && pids.contains(pid)) {
278+
parsePES(&packet[payloadStart], len, payloadUnitStartIndicator);
270279
} else {
271280
LOGE("-> Packet ignored for %d", pid);
272281
}
@@ -328,22 +337,19 @@ class MTSDecoder : public AudioDecoder {
328337
}
329338
}
330339

331-
void parsePES(uint8_t *pes, int len, const bool isNewPayload,
332-
bool isLastPacket) {
333-
TRACEI();
340+
void parsePES(uint8_t *pes, int len, const bool isNewPayload) {
341+
LOGI("parsePES: %d", len);
334342
++pes_count;
335343
uint8_t *data = nullptr;
336344
int dataSize = 0;
337345

338346
if (isNewPayload) {
339-
// PES header is not alligned correctly
340-
if (pes[0] == 0 && pes[1] == 1) pes--;
341-
342-
// check for PES packet start code prefix
343-
assert(pes[0] == 0);
344-
assert(pes[1] == 0);
345-
assert(pes[2] == 0x1);
346347
assert(len >= 6);
348+
// PES header is not alligned correctly
349+
if (!isPESStartCodeValid(pes)) {
350+
LOGI("PES header not aligned correctly");
351+
return;
352+
}
347353

348354
int pesPacketLength =
349355
(static_cast<int>(pes[4]) << 8) | static_cast<int>(pes[5]);
@@ -376,10 +382,24 @@ class MTSDecoder : public AudioDecoder {
376382
open_pes_data_size -= dataSize;
377383

378384
LOGI("- writing %d bytes (open: %d)", dataSize, open_pes_data_size);
385+
// if (open_pes_data_size == 9) {
386+
// uint8_t *tmp = pes + len;
387+
// for (int j = 0; j <= 9; j++) {
388+
// LOGI(" 0x%02X", tmp[j]);
389+
// }
390+
// }
379391
if (p_print) p_print->write(data, dataSize);
380392
if (p_dec) p_dec->write(data, dataSize);
381393
}
382394

395+
// check for PES packet start code prefix
396+
bool isPESStartCodeValid(uint8_t *pes) {
397+
if (pes[0] != 0) return false;
398+
if (pes[1] != 0) return false;
399+
if (pes[2] != 0x1) return false;
400+
return true;
401+
}
402+
383403
const char *toStr(MTSStreamType type) {
384404
switch (type) {
385405
case MTSStreamType::AUDIO_MP3:

0 commit comments

Comments
 (0)