Skip to content

Commit 23233ed

Browse files
committed
MTSDecoderTSDemux
1 parent c3cbff6 commit 23233ed

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/AudioTools/AudioCodecs/CodecADTS.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ class ADTSParser {
121121
LOGW(error_fmt, "freq", header.sampling_freq_idx);
122122
is_valid = false;
123123
}
124-
if (header.channel_cfg == 0 || header.channel_cfg > 2) {
124+
// valid value 0-7
125+
//if (header.channel_cfg == 0 || header.channel_cfg > 7) {
126+
if (header.channel_cfg > 7) {
125127
LOGW(error_fmt, "channels", header.channel_cfg);
126128
is_valid = false;
127129
}

src/AudioTools/AudioCodecs/CodecMTS.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class MTSDecoder : public AudioDecoder {
306306
} else if (!is_adts_missing && to_process && pids.contains(pid)) {
307307
parsePES(&packet[payloadStart], len, payloadUnitStartIndicator);
308308
} else {
309-
LOGE("-> Packet ignored for %d", pid);
309+
LOGE("-> Packet ignored for PID 0x%x", pid);
310310
}
311311
}
312312

@@ -374,9 +374,13 @@ class MTSDecoder : public AudioDecoder {
374374

375375
if (isNewPayload) {
376376
assert(len >= 6);
377-
// PES header is not alligned correctly
378377
if (!isPESStartCodeValid(pes)) {
379-
LOGI("PES header not aligned correctly");
378+
LOGI("PES header not aligned correctly - adjusting");
379+
pes--;
380+
}
381+
// PES header is not alligned correctly
382+
if (!isPESStartCodeValid(pes)) {
383+
LOGE("PES header not aligned correctly");
380384
return;
381385
}
382386

@@ -410,8 +414,11 @@ class MTSDecoder : public AudioDecoder {
410414
}
411415
open_pes_data_size -= dataSize;
412416

413-
LOGI("- writing %d bytes (open: %d)", dataSize, open_pes_data_size);
417+
if (open_pes_data_size < 0){
418+
return;
419+
}
414420

421+
LOGI("- writing %d bytes (open: %d)", dataSize, open_pes_data_size);
415422
if (p_print) {
416423
size_t result = writeData<uint8_t>(p_print, data, dataSize);
417424
assert(result==dataSize);

src/AudioTools/AudioCodecs/CodecTSDemux.h

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct AllocSize {
4848
class MTSDecoderTSDemux : public AudioDecoder {
4949
public:
5050
MTSDecoderTSDemux() { self = this; };
51+
MTSDecoderTSDemux(AudioDecoder &dec) { p_dec = &dec; };
5152

5253
bool begin() override {
5354
TRACED();
@@ -56,6 +57,8 @@ class MTSDecoderTSDemux : public AudioDecoder {
5657
end();
5758
}
5859

60+
if (p_dec) p_dec->begin();
61+
5962
is_active = true;
6063

6164
// create the pids we plan on printing
@@ -94,6 +97,8 @@ class MTSDecoderTSDemux : public AudioDecoder {
9497

9598
void end() override {
9699
TRACED();
100+
if (p_dec) p_dec->begin();
101+
97102
// finally end the demux process which will flush any remaining PES data.
98103
tsd_demux_end(&ctx);
99104

@@ -109,7 +114,7 @@ class MTSDecoderTSDemux : public AudioDecoder {
109114

110115
size_t write(const uint8_t *data, size_t len) override {
111116
if (!is_active) return 0;
112-
LOGD("MTSDecoder::write: %d", (int)len);
117+
LOGD("MTSDecoderTSDemux::write: %d", (int)len);
113118
size_t result = buffer.writeArray((uint8_t *)data, len);
114119
// demux
115120
demux(underflowLimit);
@@ -141,8 +146,35 @@ class MTSDecoderTSDemux : public AudioDecoder {
141146
/// Activate logging for memory allocations
142147
void setMemoryAllocationLogging(bool flag) { is_alloc_active = flag; }
143148

149+
/// Defines where the decoded result is written to
150+
void setOutput(AudioStream &out_stream) override {
151+
if (p_dec) {
152+
p_dec->setOutput(out_stream);
153+
} else {
154+
AudioDecoder::setOutput(out_stream);
155+
}
156+
}
157+
158+
/// Defines where the decoded result is written to
159+
void setOutput(AudioOutput &out_stream) override {
160+
if (p_dec) {
161+
p_dec->setOutput(out_stream);
162+
} else {
163+
AudioDecoder::setOutput(out_stream);
164+
}
165+
}
166+
167+
/// Defines where the decoded result is written to
168+
void setOutput(Print &out_stream) override {
169+
if (p_dec) {
170+
p_dec->setOutput(out_stream);
171+
} else {
172+
AudioDecoder::setOutput(out_stream);
173+
}
174+
}
175+
144176
protected:
145-
static MTSDecoder *self;
177+
static MTSDecoderTSDemux *self;
146178
int underflowLimit = MTS_UNDERFLOW_LIMIT;
147179
bool is_active = false;
148180
bool is_write_active = false;
@@ -152,6 +184,7 @@ class MTSDecoderTSDemux : public AudioDecoder {
152184
SingleBuffer<uint8_t> buffer{MTS_WRITE_BUFFER_SIZE};
153185
Vector<TSDPMTStreamType> stream_types;
154186
Vector<AllocSize> alloc_vector;
187+
AudioDecoder *p_dec = nullptr;
155188

156189
void set_write_active(bool flag) {
157190
// LOGD("is_write_active: %s", flag ? "true":"false");
@@ -240,8 +273,8 @@ class MTSDecoderTSDemux : public AudioDecoder {
240273
static void event_cb(TSDemuxContext *ctx, uint16_t pid, TSDEventId event_id,
241274
void *data) {
242275
TRACED();
243-
if (MTSDecoder::self != nullptr) {
244-
MTSDecoder::self->event_cb_local(ctx, pid, event_id, data);
276+
if (MTSDecoderTSDemux::self != nullptr) {
277+
MTSDecoderTSDemux::self->event_cb_local(ctx, pid, event_id, data);
245278
}
246279
}
247280

@@ -288,6 +321,16 @@ class MTSDecoderTSDemux : public AudioDecoder {
288321
TRACEE();
289322
}
290323
}
324+
if (p_dec != nullptr) {
325+
// size_t eff = p_print->write(pes->data_bytes,
326+
// pes->data_bytes_length);
327+
size_t eff = writeDataT<uint8_t,AudioDecoder>(
328+
p_dec, (uint8_t *)pes->data_bytes, pes->data_bytes_length);
329+
if (eff != pes->data_bytes_length) {
330+
// we should not get here
331+
TRACEE();
332+
}
333+
}
291334
}
292335
}
293336

@@ -974,6 +1017,6 @@ class MTSDecoderTSDemux : public AudioDecoder {
9741017
// }
9751018
};
9761019
// init static variable
977-
MTSDecoder *MTSDecoder::self = nullptr;
1020+
MTSDecoderTSDemux *MTSDecoderTSDemux::self = nullptr;
9781021

9791022
} // namespace audio_tools

0 commit comments

Comments
 (0)