From a82e87618fc04fe146ccd2da5f50154c4461e3f6 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Fri, 2 May 2014 01:35:28 +0200 Subject: [PATCH 1/6] mpegts: remove uneeded buf_size check It is already ensured by the loop condition Signed-off-by: Marton Balint --- libavformat/mpegts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index fe51d7316e..20bed02261 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -1107,7 +1107,7 @@ skip: } break; case MPEGTS_PAYLOAD: - if (buf_size > 0 && pes->buffer) { + if (pes->buffer) { if (pes->data_index > 0 && pes->data_index + buf_size > pes->total_size) { new_pes_packet(pes, ts->pkt); From 9ba8debce9946b21c20791b0d234443f71e471a0 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Fri, 2 May 2014 01:44:19 +0200 Subject: [PATCH 2/6] mpegts: only emit new packets if data buffer exists I don't think this can acutally happen in the current code, but better safe than sorry. Fixes Coverity CID 732217. Signed-off-by: Marton Balint --- libavformat/mpegts.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 20bed02261..001f93bb81 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -1125,8 +1125,6 @@ skip: } memcpy(pes->buffer->data + pes->data_index, p, buf_size); pes->data_index += buf_size; - } - buf_size = 0; /* emit complete packets with known packet size * decreases demuxer delay for infrequent packets like subtitles from * a couple of seconds to milliseconds for properly muxed files. @@ -1137,6 +1135,8 @@ skip: ts->stop_parse = 1; new_pes_packet(pes, ts->pkt); } + } + buf_size = 0; break; case MPEGTS_SKIP: buf_size = 0; From e2752de344588ccea364d30f3f81cd73100b1053 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Fri, 2 May 2014 01:44:58 +0200 Subject: [PATCH 3/6] mpegts: fix indentation after last commit Signed-off-by: Marton Balint --- libavformat/mpegts.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 001f93bb81..75553b6e95 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -1125,16 +1125,16 @@ skip: } memcpy(pes->buffer->data + pes->data_index, p, buf_size); pes->data_index += buf_size; - /* emit complete packets with known packet size - * decreases demuxer delay for infrequent packets like subtitles from - * a couple of seconds to milliseconds for properly muxed files. - * total_size is the number of bytes following pes_packet_length - * in the pes header, i.e. not counting the first PES_START_SIZE bytes */ - if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD && - pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) { - ts->stop_parse = 1; - new_pes_packet(pes, ts->pkt); - } + /* emit complete packets with known packet size + * decreases demuxer delay for infrequent packets like subtitles from + * a couple of seconds to milliseconds for properly muxed files. + * total_size is the number of bytes following pes_packet_length + * in the pes header, i.e. not counting the first PES_START_SIZE bytes */ + if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD && + pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) { + ts->stop_parse = 1; + new_pes_packet(pes, ts->pkt); + } } buf_size = 0; break; From d7ca914988439d267382b8b403a7e698f9a0c8ab Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Fri, 2 May 2014 16:51:58 +0200 Subject: [PATCH 4/6] mpegts: factorize pes packet state reset function Signed-off-by: Marton Balint --- libavformat/mpegts.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 75553b6e95..1bd697a71f 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -787,6 +787,15 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes, return 0; } +static void reset_pes_packet_state(PESContext *pes) +{ + pes->pts = AV_NOPTS_VALUE; + pes->dts = AV_NOPTS_VALUE; + pes->buffer = NULL; + pes->data_index = 0; + pes->flags = 0; +} + static void new_pes_packet(PESContext *pes, AVPacket *pkt) { av_init_packet(pkt); @@ -814,12 +823,7 @@ static void new_pes_packet(PESContext *pes, AVPacket *pkt) pkt->pos = pes->ts_packet_pos; pkt->flags = pes->flags; - /* reset pts values */ - pes->pts = AV_NOPTS_VALUE; - pes->dts = AV_NOPTS_VALUE; - pes->buffer = NULL; - pes->data_index = 0; - pes->flags = 0; + reset_pes_packet_state(pes); } static uint64_t get_ts64(GetBitContext *gb, int bits) From a78274329c0b1ea9964b4fa1bf6f4683e797e453 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Fri, 2 May 2014 17:13:27 +0200 Subject: [PATCH 5/6] mpegts: unref buffer in reset_pes_packet_state Signed-off-by: Marton Balint --- libavformat/mpegts.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 1bd697a71f..7ed7bb4b2f 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -791,9 +791,9 @@ static void reset_pes_packet_state(PESContext *pes) { pes->pts = AV_NOPTS_VALUE; pes->dts = AV_NOPTS_VALUE; - pes->buffer = NULL; pes->data_index = 0; pes->flags = 0; + av_buffer_unref(&pes->buffer); } static void new_pes_packet(PESContext *pes, AVPacket *pkt) @@ -823,6 +823,7 @@ static void new_pes_packet(PESContext *pes, AVPacket *pkt) pkt->pos = pes->ts_packet_pos; pkt->flags = pes->flags; + pes->buffer = NULL; reset_pes_packet_state(pes); } From 6c5375100c4fdb2f9b1a397b9969e580b2c4fdb0 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Fri, 2 May 2014 17:17:01 +0200 Subject: [PATCH 6/6] mpegts: always reset pes packet state on new packet Fixes ticket #3584. Signed-off-by: Marton Balint --- libavformat/mpegts.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 7ed7bb4b2f..0a5ac68961 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -918,9 +918,10 @@ static int mpegts_push_data(MpegTSFilter *filter, if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { new_pes_packet(pes, ts->pkt); ts->stop_parse = 1; + } else { + reset_pes_packet_state(pes); } pes->state = MPEGTS_HEADER; - pes->data_index = 0; pes->ts_packet_pos = pos; } p = buf;