From patchwork Wed May 9 21:35:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 8895 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp323128jad; Wed, 9 May 2018 14:35:22 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqmyLr4hbIDURmc0SQzGX4BUxnZUvpsq+nmJJ00CHjDnF2+9bDJeByca0APFOkHTNoqXxbi X-Received: by 2002:adf:a512:: with SMTP id i18-v6mr5962146wrb.155.1525901722210; Wed, 09 May 2018 14:35:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525901722; cv=none; d=google.com; s=arc-20160816; b=Qb5kRLk6aC/skVQsjOBrbLGCe/DId2w/Dr1djsVmySKr5bl++eXf9x5gvEjrWPp5kk 0Zv1wJtzH8p2EZSymb6n6xmZYOlNibtVhPEuyrNxtEBMYKQBCNFVutZj/ync0rFxDFKu xsrSgPrue8JSYxal4FDMm+7AbEgCaW5DKR6J/iG185j2f+0mls8lbV3kbUCwx4Ax6ENj sV20rM7xdZ2ifDoPMqbSVD29vqIedw/E0UMtYTy7+yBOVkxUkVEnA4n1qPQeFPKWxkLm fOroPx21MKziU6vK2OsEzJI7NKXDXPdeq82Fq3CMIZn/Z65VsH5Tr0UZexQINAC+9rY9 pRKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=p+K2B86O/GIFMnHC6u8o6aKaukQLwD3WsJg8kZ6jJdc=; b=y2J2pkxYD+e+VpIaKM7ovHJ07kcCrtPpjjJGktLUQrnjlpd1NV1Pw/2D/45Bxpt+Bd CM0kEGCcfyYUKrLEoQnrK2V+2WCuq20EOSGrHDJXGN7mLYrG8lIX2ZghB+quwzMkn2yf sBHKj2gEkk+BOSgrdG1dkFAkU+vlMMmv6bQj8hUKg8pCW0TPtSdjmrHeYzIV1K7K23G5 yr0S78bK7GwGwXGhy185osMgS3oFElAnRvamWV3xm0C9eXZKtZY66UrnJ5xuzMQizGe7 fGjdk/xKZfeUPbPzsJd7AWb27b7Pn3YWAIjIMceH+HvyA8hzjbHEdlJB60ErCQ8m+oPw MCXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=NV0UIVok; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 30-v6si2488436wrd.224.2018.05.09.14.35.21; Wed, 09 May 2018 14:35:22 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=NV0UIVok; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2C420689ADD; Thu, 10 May 2018 00:34:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl0-f68.google.com (mail-pl0-f68.google.com [209.85.160.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E310C6898DA for ; Thu, 10 May 2018 00:34:36 +0300 (EEST) Received: by mail-pl0-f68.google.com with SMTP id c19-v6so2085pls.6 for ; Wed, 09 May 2018 14:35:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tmm1-net.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id; bh=I/0MunQh7otwFDV193UNU0ecv0cHoqiGSy6xTRbIekc=; b=NV0UIVokyKoR8bN8BpsqgyNat+eqEa/8JmB+mW3iaZpGxvyLX+MVJbUL3n4Mh4pryK vhbBH55JVkSzpjiDWJYihtPs9YaXEu26DdRF7WhSdRE5C0A0i5MDC5RV94nO3olngHQQ EJtVHdUdZAyi/1rGE60+a22worLk29VQ0p5nnFnfUABSZ5kjft2HHtT140rhx+NpHMZl xjo9BHb94a2MXWAw9rKE6uTBVKOGtYHqzo4VI7Z7PLXoTkxhIIkOazhd43oCfnGJQTF9 For4AJK1pXm056g+auYJoGpX0M8zNtoCFPDyPZIZb0dYS2uhFKL3OsprtyvKaTzLDKon Qdfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=I/0MunQh7otwFDV193UNU0ecv0cHoqiGSy6xTRbIekc=; b=aiS+OpcXhzIAopXEWXWfnEWbZLWZI5XSHuxYnVc+PibFHieCqL8Sj5V8r5wNw0O4fE M040BeVGLQX9sd37FHYsmhgKNtHr20fR7OarXxrzwU4+TWr6LR0hqoUaGbmao9sKjEhu 11snXz5EMw/kWsmF9kI+8IRCnP5z/b7/Pm1LCe6zpqe/1p0JL7TTteJMnr7VQUy6uvpY WfIPCs79J2RJvXgnl89INYyhBUnsPdJ5rEf2RHQ7Xiiv3Kn1gH8WoukL0Njqy8r3Bkud 4cVheo3ksYyjPaqJhZP2o/B7p6FXobkjPZuWbcm4SAjzwsGfxW0AA9gJgUwRfUMHHAIc ydVw== X-Gm-Message-State: ALQs6tAtTZB1prHpqT5SzaPsvqbQJkL6JF8QaeiqKoLP8iZ8MxY/0j/U wybncccuYXsnYZizTLog4obc5rPK X-Received: by 2002:a17:902:31a4:: with SMTP id x33-v6mr47120704plb.355.1525901710514; Wed, 09 May 2018 14:35:10 -0700 (PDT) Received: from localhost.localdomain (c-69-181-54-242.hsd1.ca.comcast.net. [69.181.54.242]) by smtp.gmail.com with ESMTPSA id c7sm68549598pfg.81.2018.05.09.14.35.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 09 May 2018 14:35:09 -0700 (PDT) From: Aman Gupta To: ffmpeg-devel@ffmpeg.org Date: Wed, 9 May 2018 14:35:00 -0700 Message-Id: <20180509213501.49966-1-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.14.2 Subject: [FFmpeg-devel] [PATCH 1/2] avformat/mpegts: parse sections with multiple tables 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: michael@niedermayer.cc, cus@passwd.hu, Aman Gupta , jeebjp@gmail.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Aman Gupta Fixes PMT parsing in some mpegts streams which contain multiple tables within the PMT pid. Previously, the parser assumed only one table was present in each packet, and discarded the rest of the section data after attempting to parse the first table. A similar issue was documented in the BeyondTV software[1], which helped me diagnose the same bug in the ffmpeg mpegts demuxer. I also tried DVBInspector, libdvbpsi's dvbinfo, and tstools' tsinfo to help debug. The former two properly read PMTs with multiple tables, whereas the last has the same bug as ffmpeg. I've created a minimal sample[2] which contains the combined PMT. Here's what ffmpeg probe shows before and after this patch: Before: Input #0, mpegts, from 'combined-pmt-tids.ts': Duration: 00:00:01.08, start: 4932.966167, bitrate: 741 kb/s Program 1 No Program Stream #0:0[0xf9d]: Audio: ac3, 48000 Hz, mono, fltp, 96 kb/s Stream #0:1[0xf9b]: Audio: mp3, 0 channels, fltp Stream #0:2[0xf9c]: Unknown: none After: Input #0, mpegts, from 'combined-pmt-tids.ts': Duration: 00:00:01.11, start: 4932.966167, bitrate: 718 kb/s Program 1 Stream #0:0[0xf9b]: Video: mpeg2video ([2][0][0][0] / 0x0002), none(tv, top first), 29.97 fps, 29.97 tbr, 90k tbn, 90k tbc Stream #0:1[0xf9c](eng): Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 384 kb/s Stream #0:2[0xf9d](spa): Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, mono, fltp, 96 kb/s With the patch, the PMT is parsed correctly so the streams are created in the correct order, are associated with "Program 1", and their codecs are set correctly. [1] http://forums.snapstream.com/vb/showpost.php?p=343816&postcount=201 [2] https://s3.amazonaws.com/tmm1/combined-pmt-tids.ts Signed-off-by: Aman Gupta --- libavformat/mpegts.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 0d1dda1c36..3c9f9421cb 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -391,7 +391,8 @@ static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1, const uint8_t *buf, int buf_size, int is_start) { MpegTSSectionFilter *tss = &tss1->u.section_filter; - int len; + uint8_t *cur_section_buf = NULL; + int len, offset; if (is_start) { memcpy(tss->section_buf, buf, buf_size); @@ -408,23 +409,26 @@ static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1, tss->section_index += len; } + offset = 0; + cur_section_buf = tss->section_buf; + while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && cur_section_buf[0] != 0xff) { /* compute section length if possible */ - if (tss->section_h_size == -1 && tss->section_index >= 3) { - len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3; + if (tss->section_h_size == -1 && tss->section_index - offset >= 3) { + len = (AV_RB16(cur_section_buf + 1) & 0xfff) + 3; if (len > MAX_SECTION_SIZE) return; tss->section_h_size = len; } if (tss->section_h_size != -1 && - tss->section_index >= tss->section_h_size) { + tss->section_index >= offset + tss->section_h_size) { int crc_valid = 1; tss->end_of_section_reached = 1; if (tss->check_crc) { - crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size); + crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, cur_section_buf, tss->section_h_size); if (tss->section_h_size >= 4) - tss->crc = AV_RB32(tss->section_buf + tss->section_h_size - 4); + tss->crc = AV_RB32(cur_section_buf + tss->section_h_size - 4); if (crc_valid) { ts->crc_validity[ tss1->pid ] = 100; @@ -434,10 +438,18 @@ static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1, crc_valid = 2; } if (crc_valid) { - tss->section_cb(tss1, tss->section_buf, tss->section_h_size); + tss->section_cb(tss1, cur_section_buf, tss->section_h_size); if (crc_valid != 1) tss->last_ver = -1; } + + cur_section_buf += tss->section_h_size; + offset += tss->section_h_size; + tss->section_h_size = -1; + } else { + tss->end_of_section_reached = 0; + break; + } } }