@@ -194,7 +194,7 @@ class MTSDecoder : public AudioDecoder {
194
194
TRACED ();
195
195
int count = 0 ;
196
196
while (parse ()) {
197
- LOGI (" demux: # %d" , ++count);
197
+ LOGI (" demux: step #%d with PES # %d" , ++count, ( int )pes_count );
198
198
}
199
199
LOGI (" Number of demux calls: %d" , count);
200
200
}
@@ -215,11 +215,15 @@ class MTSDecoder : public AudioDecoder {
215
215
bool parse () {
216
216
int pos = syncPos ();
217
217
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
+ }
218
222
// parse data
219
223
parsePacket ();
220
- // assert(pos == 0);
224
+
221
225
// remove processed data
222
- buffer.clearArray (pos == 0 ? TS_PACKET_SIZE : pos );
226
+ buffer.clearArray (TS_PACKET_SIZE);
223
227
return true ;
224
228
}
225
229
@@ -230,15 +234,21 @@ class MTSDecoder : public AudioDecoder {
230
234
int pid = ((packet[1 ] & 0x1F ) << 8 ) | (packet[2 ] & 0xFF );
231
235
LOGI (" PID: 0x%04X(%d)" , pid, pid);
232
236
bool payloadUnitStartIndicator = false ;
233
- bool hasAdaptionField = false ;
237
+ uint8_t adaptionField = (packet[ 3 ] & 0x30 ) >> 4 ;
234
238
int adaptationSize = 0 ;
235
239
int offset = 4 ; // Start after TS header (4 bytes)
240
+ bool to_process = true ;
236
241
237
242
// 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 ;
240
249
offset += adaptationSize;
241
- hasAdaptionField = true ;
250
+ } else if (adaptionField == 0b10 ) { // Adopation field only
251
+ to_process = false ;
242
252
}
243
253
244
254
// If PUSI is set, there's a pointer field (skip it)
@@ -248,12 +258,12 @@ class MTSDecoder : public AudioDecoder {
248
258
}
249
259
250
260
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 ,
252
262
adaptationSize);
253
263
254
264
// if we are at the beginning we start with a pat
255
265
if (pid == 0 && payloadUnitStartIndicator) {
256
- pids.clear ();
266
+ // pids.clear();
257
267
}
258
268
259
269
int payloadStart = offset;
@@ -264,9 +274,8 @@ class MTSDecoder : public AudioDecoder {
264
274
parsePAT (&packet[payloadStart], len);
265
275
} else if (pid == pmt_pid && packet[payloadStart] == 0x02 ) {
266
276
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);
270
279
} else {
271
280
LOGE (" -> Packet ignored for %d" , pid);
272
281
}
@@ -328,22 +337,19 @@ class MTSDecoder : public AudioDecoder {
328
337
}
329
338
}
330
339
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);
334
342
++pes_count;
335
343
uint8_t *data = nullptr ;
336
344
int dataSize = 0 ;
337
345
338
346
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 );
346
347
assert (len >= 6 );
348
+ // PES header is not alligned correctly
349
+ if (!isPESStartCodeValid (pes)) {
350
+ LOGI (" PES header not aligned correctly" );
351
+ return ;
352
+ }
347
353
348
354
int pesPacketLength =
349
355
(static_cast <int >(pes[4 ]) << 8 ) | static_cast <int >(pes[5 ]);
@@ -376,10 +382,24 @@ class MTSDecoder : public AudioDecoder {
376
382
open_pes_data_size -= dataSize;
377
383
378
384
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
+ // }
379
391
if (p_print) p_print->write (data, dataSize);
380
392
if (p_dec) p_dec->write (data, dataSize);
381
393
}
382
394
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
+
383
403
const char *toStr (MTSStreamType type) {
384
404
switch (type) {
385
405
case MTSStreamType::AUDIO_MP3:
0 commit comments