From patchwork Mon Dec 28 22:49:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24666 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 7A1E444866D for ; Tue, 29 Dec 2020 00:49:43 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 401ED68A4C5; Tue, 29 Dec 2020 00:49:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4D85D680ACD for ; Tue, 29 Dec 2020 00:49:36 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id C5585E4BDB; Mon, 28 Dec 2020 23:49:35 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mlspuE7W02Db; Mon, 28 Dec 2020 23:49:33 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 5C68FE49CD; Mon, 28 Dec 2020 23:49:33 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Dec 2020 23:49:24 +0100 Message-Id: <20201228224929.21299-1-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/6] avformat/utils: do not overwrite already existing program with defaults in av_new_program X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" av_new_program returns the existing program if that already exists, in that case it makes no sense to overwrite existing attributes. Signed-off-by: Marton Balint --- libavformat/utils.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index 1a87d9422a..270ed8d4f2 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -4568,14 +4568,12 @@ AVProgram *av_new_program(AVFormatContext *ac, int id) dynarray_add(&ac->programs, &ac->nb_programs, program); program->discard = AVDISCARD_NONE; program->pmt_version = -1; + program->id = id; + program->pts_wrap_reference = AV_NOPTS_VALUE; + program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE; + program->start_time = + program->end_time = AV_NOPTS_VALUE; } - program->id = id; - program->pts_wrap_reference = AV_NOPTS_VALUE; - program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE; - - program->start_time = - program->end_time = AV_NOPTS_VALUE; - return program; } From patchwork Mon Dec 28 22:49:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24667 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2689D44866D for ; Tue, 29 Dec 2020 00:49:46 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1047968A57E; Tue, 29 Dec 2020 00:49:46 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1C1436806B3 for ; Tue, 29 Dec 2020 00:49:38 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 04A1EE4BF7; Mon, 28 Dec 2020 23:49:38 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id cSmW6SWMKikH; Mon, 28 Dec 2020 23:49:35 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id AEA15E4E26; Mon, 28 Dec 2020 23:49:35 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Dec 2020 23:49:25 +0100 Message-Id: <20201228224929.21299-2-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201228224929.21299-1-cus@passwd.hu> References: <20201228224929.21299-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/6] avformat/mpegts: never discard PAT pid X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" PID 0 was removed from the pid list when then PMT was parsed, it is better to explictly avoid it from being discarded instead of keeing it in the list of every program. Signed-off-by: Marton Balint --- libavformat/mpegts.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index efff5130de..3712dad1c8 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -380,6 +380,9 @@ static int discard_pid(MpegTSContext *ts, unsigned int pid) int used = 0, discarded = 0; struct Program *p; + if (pid == PAT_PID) + return 0; + /* If none of the programs have .discard=AVDISCARD_ALL then there's * no way we have to discard this packet */ for (k = 0; k < ts->stream->nb_programs; k++) @@ -2543,7 +2546,6 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!ts->pids[pmt_pid]) mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1); add_pat_entry(ts, sid); - add_pid_to_pmt(ts, sid, 0); // add pat pid to program add_pid_to_pmt(ts, sid, pmt_pid); } } From patchwork Mon Dec 28 22:49:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24668 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9388444866D for ; Tue, 29 Dec 2020 00:49:47 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 752EF68A5BA; Tue, 29 Dec 2020 00:49:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 70B77680ACD for ; Tue, 29 Dec 2020 00:49:39 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 5388AE4E26; Mon, 28 Dec 2020 23:49:39 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DrXmAQ27CdHN; Mon, 28 Dec 2020 23:49:38 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 96C09E4E2C; Mon, 28 Dec 2020 23:49:37 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Dec 2020 23:49:26 +0100 Message-Id: <20201228224929.21299-3-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201228224929.21299-1-cus@passwd.hu> References: <20201228224929.21299-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/6] avformat/mpegts: rework clearing and adding pid to program X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" And use better function names. Signed-off-by: Marton Balint --- libavformat/mpegts.c | 59 +++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 3712dad1c8..1b990f7a66 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -285,16 +285,12 @@ static void clear_avprogram(MpegTSContext *ts, unsigned int programid) prg->nb_stream_indexes = 0; } -static void clear_program(MpegTSContext *ts, unsigned int programid) +static void clear_program(struct Program *p) { - int i; - - clear_avprogram(ts, programid); - for (i = 0; i < ts->nb_prg; i++) - if (ts->prg[i].id == programid) { - ts->prg[i].nb_pids = 0; - ts->prg[i].pmt_found = 0; - } + if (!p) + return; + p->nb_pids = 0; + p->pmt_found = 0; } static void clear_programs(MpegTSContext *ts) @@ -303,24 +299,22 @@ static void clear_programs(MpegTSContext *ts) ts->nb_prg = 0; } -static void add_pat_entry(MpegTSContext *ts, unsigned int programid) +static struct Program * add_program(MpegTSContext *ts, unsigned int programid) { struct Program *p; if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) { ts->nb_prg = 0; - return; + return NULL; } p = &ts->prg[ts->nb_prg]; p->id = programid; - p->nb_pids = 0; - p->pmt_found = 0; + clear_program(p); ts->nb_prg++; + return p; } -static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, - unsigned int pid) +static void add_pid_to_program(struct Program *p, unsigned int pid) { - struct Program *p = get_program(ts, programid); int i; if (!p) return; @@ -335,15 +329,6 @@ static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, p->pids[p->nb_pids++] = pid; } -static void set_pmt_found(MpegTSContext *ts, unsigned int programid) -{ - struct Program *p = get_program(ts, programid); - if (!p) - return; - - p->pmt_found = 1; -} - static void update_av_program_info(AVFormatContext *s, unsigned int programid, unsigned int pid, int version) { @@ -2290,6 +2275,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len int desc_list_len; uint32_t prog_reg_desc = 0; /* registration descriptor */ int stream_identifier = -1; + struct Program *prg; int mp4_descr_count = 0; Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } }; @@ -2313,16 +2299,20 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!ts->scan_all_pmts && ts->skip_changes) return; - if (ts->skip_unknown_pmt && !get_program(ts, h->id)) + prg = get_program(ts, h->id); + + if (ts->skip_unknown_pmt && !prg) return; - if (!ts->skip_clear) - clear_program(ts, h->id); + if (!ts->skip_clear) { + clear_avprogram(ts, h->id); + clear_program(prg); + } pcr_pid = get16(&p, p_end); if (pcr_pid < 0) return; pcr_pid &= 0x1fff; - add_pid_to_pmt(ts, h->id, pcr_pid); + add_pid_to_program(prg, pcr_pid); update_av_program_info(ts->stream, h->id, pcr_pid, h->version); av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid); @@ -2362,8 +2352,8 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!ts->pkt) ts->stop_parse = 2; - set_pmt_found(ts, h->id); - + if (prg) + prg->pmt_found = 1; for (i = 0; ; i++) { st = 0; @@ -2453,7 +2443,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (pes && !pes->stream_type) mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc); - add_pid_to_pmt(ts, h->id, pid); + add_pid_to_program(prg, pid); av_program_add_stream_index(ts->stream, h->id, st->index); @@ -2532,6 +2522,7 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len /* NIT info */ } else { MpegTSFilter *fil = ts->pids[pmt_pid]; + struct Program *prg; program = av_new_program(ts->stream, sid); if (program) { program->program_num = sid; @@ -2545,8 +2536,8 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!ts->pids[pmt_pid]) mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1); - add_pat_entry(ts, sid); - add_pid_to_pmt(ts, sid, pmt_pid); + prg = add_program(ts, sid); + add_pid_to_program(prg, pmt_pid); } } From patchwork Mon Dec 28 22:49:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24669 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 356F744866D for ; Tue, 29 Dec 2020 00:49:49 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1DA3068A5CC; Tue, 29 Dec 2020 00:49:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 537B068A588 for ; Tue, 29 Dec 2020 00:49:42 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 28871E4BDB; Mon, 28 Dec 2020 23:49:42 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 18smSSP7TeGq; Mon, 28 Dec 2020 23:49:40 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 6D1A5E4E29; Mon, 28 Dec 2020 23:49:40 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Dec 2020 23:49:27 +0100 Message-Id: <20201228224929.21299-4-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201228224929.21299-1-cus@passwd.hu> References: <20201228224929.21299-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/6] avformat/mpegts: only clear programs which no longer exist or have a new PMT X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Otherwise there can be a small period when the programs only contain the PMT pid. Also make sure skip_clear only affects AVProgram clear, and that pmt_pid is always kept as the first entry of the PID list of the programs. Also reject PMTs for programs on the wrong PID. Signed-off-by: Marton Balint --- libavformat/mpegts.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 1b990f7a66..ee69e2ca29 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -20,6 +20,7 @@ */ #include "libavutil/buffer.h" +#include "libavutil/common.h" #include "libavutil/crc.h" #include "libavutil/internal.h" #include "libavutil/intreadwrite.h" @@ -301,7 +302,9 @@ static void clear_programs(MpegTSContext *ts) static struct Program * add_program(MpegTSContext *ts, unsigned int programid) { - struct Program *p; + struct Program *p = get_program(ts, programid); + if (p) + return p; if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) { ts->nb_prg = 0; return NULL; @@ -2303,10 +2306,12 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (ts->skip_unknown_pmt && !prg) return; - if (!ts->skip_clear) { + if (prg && prg->nb_pids && prg->pids[0] != ts->current_pid) + return; + if (!ts->skip_clear) clear_avprogram(ts, h->id); - clear_program(prg); - } + clear_program(prg); + add_pid_to_program(prg, ts->current_pid); pcr_pid = get16(&p, p_end); if (pcr_pid < 0) @@ -2485,6 +2490,7 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len SectionHeader h1, *h = &h1; const uint8_t *p, *p_end; int sid, pmt_pid; + int nb_prg = 0; AVProgram *program; av_log(ts->stream, AV_LOG_TRACE, "PAT:\n"); @@ -2503,7 +2509,6 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len return; ts->stream->ts_id = h->id; - clear_programs(ts); for (;;) { sid = get16(&p, p_end); if (sid < 0) @@ -2537,9 +2542,19 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!ts->pids[pmt_pid]) mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1); prg = add_program(ts, sid); - add_pid_to_program(prg, pmt_pid); + if (prg) { + unsigned prg_idx = prg - ts->prg; + if (prg->nb_pids && prg->pids[0] != pmt_pid) + clear_program(prg); + add_pid_to_program(prg, pmt_pid); + if (prg_idx > nb_prg) + FFSWAP(struct Program, ts->prg[nb_prg], ts->prg[prg_idx]); + if (prg_idx >= nb_prg) + nb_prg++; + } } } + ts->nb_prg = nb_prg; if (sid < 0) { int i,j; From patchwork Mon Dec 28 22:49:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24670 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 54C5D44866D for ; Tue, 29 Dec 2020 00:49:52 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 293E868A5E3; Tue, 29 Dec 2020 00:49:52 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18A7268A6B6 for ; Tue, 29 Dec 2020 00:49:45 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 041E8E4BF7; Mon, 28 Dec 2020 23:49:45 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2WjM1_fV8qMY; Mon, 28 Dec 2020 23:49:43 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id D230FE4E29; Mon, 28 Dec 2020 23:49:42 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Dec 2020 23:49:28 +0100 Message-Id: <20201228224929.21299-5-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201228224929.21299-1-cus@passwd.hu> References: <20201228224929.21299-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/6] avformat/mpegts: use stream index based lookup with merge_pmt_versions if stream identifier matches multiple streams X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Also make sure we are checking the old state of the streams because otherwise some streams might already have the newly parsed stream identifiers which corrupts matching. Fixes streams having the same identifier mixed up on pmt version change. Fixes ticket #9006. Signed-off-by: Marton Balint --- libavformat/mpegts.c | 57 +++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index ee69e2ca29..9a3fd56ece 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -107,11 +107,19 @@ struct MpegTSFilter { } u; }; -#define MAX_PIDS_PER_PROGRAM 64 +struct Stream { + int idx; + int stream_identifier; +}; + +#define MAX_STREAMS_PER_PROGRAM 128 +#define MAX_PIDS_PER_PROGRAM (MAX_STREAMS_PER_PROGRAM + 2) struct Program { unsigned int id; // program id/service id unsigned int nb_pids; unsigned int pids[MAX_PIDS_PER_PROGRAM]; + unsigned int nb_streams; + struct Stream streams[MAX_STREAMS_PER_PROGRAM]; /** have we found pmt for this program */ int pmt_found; @@ -291,6 +299,7 @@ static void clear_program(struct Program *p) if (!p) return; p->nb_pids = 0; + p->nb_streams = 0; p->pmt_found = 0; } @@ -2193,27 +2202,28 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type } static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid, - int stream_identifier, int pmt_stream_idx) + int stream_identifier, int pmt_stream_idx, struct Program *p) { AVFormatContext *s = ts->stream; int i; AVStream *found = NULL; - for (i = 0; i < s->nb_streams; i++) { - AVStream *st = s->streams[i]; - if (st->program_num != programid) - continue; - if (stream_identifier != -1) { /* match based on "stream identifier descriptor" if present */ - if (st->stream_identifier == stream_identifier+1) { - found = st; - break; + if (stream_identifier) { /* match based on "stream identifier descriptor" if present */ + for (i = 0; i < p->nb_streams; i++) { + if (p->streams[i].stream_identifier == stream_identifier) { + if (found) { /* fallback to idx based guess if multiple streams have the same identifier */ + found = NULL; + break; + } else { + found = s->streams[p->streams[i].idx]; + } } - } else if (st->pmt_stream_idx == pmt_stream_idx) { /* match based on position within the PMT */ - found = st; - break; } } + if (!found && pmt_stream_idx < p->nb_streams) + found = s->streams[p->streams[pmt_stream_idx].idx]; + if (found) { av_log(ts->stream, AV_LOG_VERBOSE, "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n", @@ -2270,6 +2280,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len { MpegTSContext *ts = filter->u.section_filter.opaque; MpegTSSectionFilter *tssf = &filter->u.section_filter; + struct Program old_program; SectionHeader h1, *h = &h1; PESContext *pes; AVStream *st; @@ -2303,6 +2314,10 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len return; prg = get_program(ts, h->id); + if (prg) + old_program = *prg; + else + clear_program(&old_program); if (ts->skip_unknown_pmt && !prg) return; @@ -2360,7 +2375,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (prg) prg->pmt_found = 1; - for (i = 0; ; i++) { + for (i = 0; i < MAX_STREAMS_PER_PROGRAM; i++) { st = 0; pes = NULL; stream_type = get8(&p, p_end); @@ -2373,14 +2388,13 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (pid == ts->current_pid) goto out; - if (ts->merge_pmt_versions) - stream_identifier = parse_stream_identifier_desc(p, p_end); + stream_identifier = parse_stream_identifier_desc(p, p_end) + 1; /* now create stream */ if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) { pes = ts->pids[pid]->u.pes_filter.opaque; if (ts->merge_pmt_versions && !pes->st) { - st = find_matching_stream(ts, pid, h->id, stream_identifier, i); + st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program); if (st) { pes->st = st; pes->stream_type = stream_type; @@ -2402,7 +2416,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably pes = add_pes_stream(ts, pid, pcr_pid); if (ts->merge_pmt_versions && pes && !pes->st) { - st = find_matching_stream(ts, pid, h->id, stream_identifier, i); + st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program); if (st) { pes->st = st; pes->stream_type = stream_type; @@ -2424,7 +2438,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len st = ts->stream->streams[idx]; } if (ts->merge_pmt_versions && !st) { - st = find_matching_stream(ts, pid, h->id, stream_identifier, i); + st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program); } if (!st) { st = avformat_new_stream(ts->stream, NULL); @@ -2449,6 +2463,11 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc); add_pid_to_program(prg, pid); + if (prg) { + prg->streams[i].idx = st->index; + prg->streams[i].stream_identifier = stream_identifier; + prg->nb_streams++; + } av_program_add_stream_index(ts->stream, h->id, st->index); From patchwork Mon Dec 28 22:49:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24671 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 72CE444866D for ; Tue, 29 Dec 2020 00:49:55 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 610C968A5D7; Tue, 29 Dec 2020 00:49:55 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6E68368A708 for ; Tue, 29 Dec 2020 00:49:47 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 5C67BE4BDB; Mon, 28 Dec 2020 23:49:47 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id A5YvDS2t9ibH; Mon, 28 Dec 2020 23:49:45 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 7A6B3E4E26; Mon, 28 Dec 2020 23:49:45 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Dec 2020 23:49:29 +0100 Message-Id: <20201228224929.21299-6-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201228224929.21299-1-cus@passwd.hu> References: <20201228224929.21299-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/6] avformat: deprecate some mpegts details from AVStream X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" These fields were added to support -merge_pmt_versions, but the mpegts demuxer is also keeping track its programs internally, so that should be a better place to handle it. Also it is not a very good API to provide fields like program_num or pmt_stream_idx in an AVStream, because a single stream can be part of multiple programs, multiple PMTs, so the stream attributes can refer to any program the stream is part of. Considering that these were added only 2 years ago, and their usefulness was limited in the first place, I think the best approach is to deprecate them. If users should come forward wanting such functionality, then adding it to AVProgram seems the better place to be able to better support having the same stream in multiple programs. Signed-off-by: Marton Balint --- doc/APIchanges | 3 +++ libavformat/avformat.h | 5 +++++ libavformat/mpegts.c | 12 ++++++++++++ libavformat/version.h | 5 ++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/APIchanges b/doc/APIchanges index 3fb9e12525..b901133f63 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2017-10-21 API changes, most recent first: +2021-01-xx - xxxxxxxxxx - lavf 58.66.100 - avformat.h + Deprecate AVStream program_num, pmt_version, pmt_stream_idx. + 2020-12-03 - xxxxxxxxxx - lavu 56.62.100 - timecode.h Add av_timecode_init_from_components. diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 4865c56cc3..89a53056f2 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1096,12 +1096,17 @@ typedef struct AVStream { */ int stream_identifier; +#if FF_API_MPEGTS_AVSTREAM /** * Details of the MPEG-TS program which created this stream. */ + attribute_deprecated int program_num; + attribute_deprecated int pmt_version; + attribute_deprecated int pmt_stream_idx; +#endif /** * An opaque field for libavformat internal usage. diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 9a3fd56ece..6d9bdbc485 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -2406,9 +2406,13 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!pes->st) goto out; pes->st->id = pes->pid; +#if FF_API_MPEGTS_AVSTREAM +FF_DISABLE_DEPRECATION_WARNINGS pes->st->program_num = h->id; pes->st->pmt_version = h->version; pes->st->pmt_stream_idx = i; +FF_ENABLE_DEPRECATION_WARNINGS +#endif } st = pes->st; } else if (is_pes_stream(stream_type, prog_reg_desc)) { @@ -2428,9 +2432,13 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!st) goto out; st->id = pes->pid; +#if FF_API_MPEGTS_AVSTREAM +FF_DISABLE_DEPRECATION_WARNINGS st->program_num = h->id; st->pmt_version = h->version; st->pmt_stream_idx = i; +FF_ENABLE_DEPRECATION_WARNINGS +#endif } } else { int idx = ff_find_stream_index(ts->stream, pid); @@ -2445,9 +2453,13 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len if (!st) goto out; st->id = pid; +#if FF_API_MPEGTS_AVSTREAM +FF_DISABLE_DEPRECATION_WARNINGS st->program_num = h->id; st->pmt_version = h->version; st->pmt_stream_idx = i; +FF_ENABLE_DEPRECATION_WARNINGS +#endif st->codecpar->codec_type = AVMEDIA_TYPE_DATA; if (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) { mpegts_find_stream_type(st, stream_type, SCTE_types); diff --git a/libavformat/version.h b/libavformat/version.h index b43193bcb1..c211c13477 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 -#define LIBAVFORMAT_VERSION_MINOR 65 +#define LIBAVFORMAT_VERSION_MINOR 66 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ @@ -106,6 +106,9 @@ #ifndef FF_API_AVIOFORMAT #define FF_API_AVIOFORMAT (LIBAVFORMAT_VERSION_MAJOR < 59) #endif +#ifndef FF_API_MPEGTS_AVSTREAM +#define FF_API_MPEGTS_AVSTREAM (LIBAVFORMAT_VERSION_MAJOR < 59) +#endif #ifndef FF_API_R_FRAME_RATE