From 496052bc496a8b3e3e56ca0c4ef15a00f3dd0964 Mon Sep 17 00:00:00 2001 From: mrwish7 Date: Fri, 9 Apr 2021 15:36:47 +0100 Subject: [PATCH] Check ADTS frame len and UDP frame len match to stop invalid data output --- mpe2aac.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mpe2aac.c b/mpe2aac.c index 7a4b25c..97d9d48 100644 --- a/mpe2aac.c +++ b/mpe2aac.c @@ -306,8 +306,15 @@ void callback(mpegts_psi_t *psi) } /* when aac located successfully */ - if (len_udp > 8) - fwrite(payload, 1, len_udp, stdout); + if (len_udp > 8) { + /* get AAC frame length as reported by the ADTS packet itself */ + int aac_frame_len = ((payload[3] & 0x3) << 11) | ((payload[4] << 3) | (payload[5] >> 5)); + + /* Only output AAC frames with matching length in ADTS packet to avoid FFMPEG errors */ + if (len_udp == aac_frame_len) { + fwrite(payload, 1, len_udp, stdout); + } + } }