From patchwork Mon Jul 31 13:38:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Devin Heitmueller X-Patchwork-Id: 43049 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba09:b0:130:ccc6:6c4b with SMTP id fa9csp119513pzb; Mon, 31 Jul 2023 06:38:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlFK2H1B8QEdaD0pc53SNKXHAzOj2PTV2N4SuWpR7zZFbjbCP6cYc+Ql1i6Y9LB75vW+d8J5 X-Received: by 2002:a05:6402:1055:b0:522:4d1b:3acf with SMTP id e21-20020a056402105500b005224d1b3acfmr8084356edu.37.1690810713200; Mon, 31 Jul 2023 06:38:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690810713; cv=none; d=google.com; s=arc-20160816; b=yxECnU655Hwa49C/PZp0ZIbk+YgUMP3np257dAV7KHUyLc9d4OJ86nW9jMpC3/Nvhg Gl57chy0sqMQ56LHNFLhDNg0U2cqz/weBnHNrQD5HrkO0liKqQYa1bFjXB4r8kWunH1y L0zvVqbWZpmTjZHtYu7jllzDs7varrA/46bZE6Trivgy0kpNPF94jkYIEwo62wxiI6fp UZ+vBepmH/eOG5ri2u/gB7ZrnTvDFD1vNW7FT90od3yfauR1TalINwDPE0E5SzSNwYI+ rN0tIzT0wHY2uwyxwnsNODdbtA+TrmOkUJZUjQY0n5NFQ5sJWD2rzZOJphr9l6Wd+SiO noDg== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=bQzyDAHUAaQhAx3Xhwjtv8NOaXxM0TI0NcVMieaivcg=; fh=DKzdJpyueDOox6ShoNYUi750QZs6R2naOS9+hcsrM90=; b=0mp6Sydtf8h+JY/9zAs5gNdmvykfzhKO0ch95o+P7eIMDVztSuT1cHKsKrEEumiu0I rF1Ml/U154Usmqmdw9kuJu6EWcy60imSkYfjcVnB6TbwV9/i+dPAHXKUQhb8yTr2KIS4 T9Fnog+41kSKT/0dF/TmXTfKVix+o217V9qNUL/8/gaE2GXei9mw0apPDuYVmCtn19uC wEhQCBaSdCWxMMO1I4E44KjhoYMj6cXEqn86BO34fPC4ZnNAQw9vEJFg4tfoyHLLfmS6 yo2RQ8WTNDJGWCMFGCXcgF5QwA0pZNRBp10087POh+DmtSwmD9WZMspU47Px4cRLBw3F yyDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b="ck/aQfM4"; 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 w8-20020aa7cb48000000b005227bab0331si3066928edt.139.2023.07.31.06.38.32; Mon, 31 Jul 2023 06:38:33 -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=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b="ck/aQfM4"; 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 03B5168C158; Mon, 31 Jul 2023 16:38:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3DEBC68BEE5 for ; Mon, 31 Jul 2023 16:38:13 +0300 (EEST) Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-76754b9eac0so377047185a.0 for ; Mon, 31 Jul 2023 06:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ltnglobal-com.20221208.gappssmtp.com; s=20221208; t=1690810691; x=1691415491; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=NIa8U61D4HjMO2dR5ME9+bwUAki8APBvAR/q5qBzgps=; b=ck/aQfM40XhoGib1/Ye662bna0mHRjQ57ixjd1ekcgJQRUBcdA9Uht80ipQik2bc41 TK3+jAXvaCz96Q/LyKVjq5wqHhnphlSyX0C9tyl784ird5zW0ZxujdE0mbt3zcdfzNLV Tr49CQTGQQvhiHPqYNkURIaRWDlmp4D+Y4/7LHsKzpMajdUO5tQdJEmPGRGtvW8E0viw nCOHBTIAzIbLAw/SHQIm7HXL1NovjGF7xfjcOOWskiv3WwVHyJjS2xL57YCejlWuT/Sf qvkrrYfbkdQIoQFw04NbNuAl/7OErQKH3dUpqtWqaB1huIDI0mLvMbuuSniZP816JRE5 30KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690810691; x=1691415491; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NIa8U61D4HjMO2dR5ME9+bwUAki8APBvAR/q5qBzgps=; b=Ol1ANP+mWnHTIvDzAOsDSJf2/IYBamyJzmKeeDL1zzREoap503lSi7NJHGnV3KnFws wTRunsuUV7OlM1Ob4w9Ik42WDwXASg09UQjoA51w+bIHBUSzBdt52OATpmokbQG9Z8uR FjJ0nx/Fu6o+Ldbc9sYgKWyn3v1ijujZAKxNAawUJ451FKnxCfsjjBb5ewghjwCf1e4y sKYQ8+pcqu2XmatFoyot/wXIY+50VjU66JC3upDGaYU7yS0X5kJM86oz+VKLTt3Yvyit L1NQpA/qR3xF/snNBmrl/smIazMbuoCXEGpbZrIcc09sEuEphB5xIuTIcGQdDjkcXjHB Ja7g== X-Gm-Message-State: ABy/qLazVHkzr581qSZGo+SPqfWAYjGzjR1UO0REHFJvhuFT0oe4LdMh Pval/4OjCdtolYaWPfDPHE8kzprmQXxLQt87WIU= X-Received: by 2002:a05:620a:1914:b0:76c:add6:f84a with SMTP id bj20-20020a05620a191400b0076cadd6f84amr4196052qkb.35.1690810691322; Mon, 31 Jul 2023 06:38:11 -0700 (PDT) Received: from ltnt-nyc-580testdevin.livetimenet.com (pool-71-105-132-214.nycmny.fios.verizon.net. [71.105.132.214]) by smtp.gmail.com with ESMTPSA id o9-20020a05620a110900b0076c84240467sm2504235qkk.52.2023.07.31.06.38.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2023 06:38:10 -0700 (PDT) From: Devin Heitmueller X-Google-Original-From: Devin Heitmueller To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Jul 2023 09:38:03 -0400 Message-Id: <1690810686-4723-2-git-send-email-dheitmueller@ltnglobal.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> References: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> Subject: [FFmpeg-devel] [PATCH v4 1/4] avcodec: Add new side data type to contain original PTS value X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Devin Heitmueller MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ZPdTZsvHPlZ8 In order to properly process SCTE-35 packets, we need the original PTS value from the demux (i.e. not mangled by the application or reclocked for the output). This allows us to set the pts_adjustment field in an BSF on the output side. Introduce a new side data type to store the original PTS. Signed-off-by: Devin Heitmueller --- libavcodec/defs.h | 12 ++++++++++++ libavcodec/packet.h | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/libavcodec/defs.h b/libavcodec/defs.h index fbe3254..ff50d25 100644 --- a/libavcodec/defs.h +++ b/libavcodec/defs.h @@ -28,6 +28,7 @@ #include #include +#include "libavutil/rational.h" /** * @ingroup lavc_decoding @@ -119,6 +120,17 @@ typedef struct AVPanScan { } AVPanScan; /** + * Original Transport Timestamp. Provides the original timestamp + * of the packet as specified by the libavformat source This allows that + * data to be used in calculations even if the clocks have been + * rebased or otherwise modified. + */ +typedef struct AVTransportTimestamp { + int64_t pts; + AVRational time_base; +} AVTransportTimestamp; + +/** * This structure describes the bitrate properties of an encoded bitstream. It * roughly corresponds to a subset the VBV parameters for MPEG-2 or HRD * parameters for H.264/HEVC. diff --git a/libavcodec/packet.h b/libavcodec/packet.h index f28e7e7..8b650a8 100644 --- a/libavcodec/packet.h +++ b/libavcodec/packet.h @@ -300,6 +300,17 @@ enum AVPacketSideDataType { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, /** + * Provides the original PTS when passed through the demux. This can + * be used to offset any subsequent changes made by the caller to + * adjust PTS values (such as pts_offset). We need this for SCTE-35, + * since by the time the packets reach the output the PTS values have + * already been re-written, and we cannot calculate pre-roll values + * using the PTS values embedded in the packet content + * Format for this data can be found in AVTransportTimestamp struct + */ + AV_PKT_DATA_TRANSPORT_TIMESTAMP, + + /** * The number of side data types. * This is not part of the public API/ABI in the sense that it may * change when new side data types are added. From patchwork Mon Jul 31 13:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Devin Heitmueller X-Patchwork-Id: 43050 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba09:b0:130:ccc6:6c4b with SMTP id fa9csp119600pzb; Mon, 31 Jul 2023 06:38:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlFOS2Sp1KMiR7fK8ZsWgSTeztwVdpPLhC20rNNZRFFUwhahJb1bN2rsFivoVM/Cq94yqvNm X-Received: by 2002:a17:906:5a4a:b0:993:e752:1a6f with SMTP id my10-20020a1709065a4a00b00993e7521a6fmr6942988ejc.6.1690810722151; Mon, 31 Jul 2023 06:38:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690810722; cv=none; d=google.com; s=arc-20160816; b=wwqw5YYWv/kA2m6syXOZYjn9LjAOXU7GzHYc6sG/xfpsS+JtEtgC2/offIRln9VUwM mtOJ5T/NkQTF9gPrI+rbDpXTfY9wXwQ6gEVt4Ek48SY4Syds4Pf/WHLd2CWaXlwJaqou ErGufQXLRgJL3Fze+dOoVDhRn/GN0Dz7KfzWpg+zuWoPwcISZeWN0+0IQkSY+zz211Kv MIuJH66phGVgQhKxfNpB70SdRwbZrII8vC6A3MDA8q2VORVEWGWmIMCznX8SP0PRqmdi hwkAtuOwPaUoG/iLr+dV+Q9P8wRfa8Jf9zeyOQv+pZWTwYT0kmYaBxjAnAKH2xyGQvkd 10+w== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=c4t+ob2p3gRxwCmvJ+LdvWUSb1Q5ksH4qlTAdf4gN90=; fh=DKzdJpyueDOox6ShoNYUi750QZs6R2naOS9+hcsrM90=; b=p9dK9Bzlyx0qNKrnGYjLlcVQV4MMg9zV1GjRJK8v3I6wCzUeVTj4BdYL2sJwkv2nQQ d/WzipkYtieVBX9HuFZQ2pykLa9R1DpH98d/fbRlAdbp9uPcqpoV2Nl5JaNKxQK5XeiG 3wq3s3wmS1nh6ZcMric/E9EeVw1cYqgFEBohRFB0EDNvUExDgjY4NRt8HR7WsvB2rvxX JDlEuLsFwzW+uq5J50ye05bPwinVBIGX8uN0ci8xVT6PWUKV0lREsb9NQtAnpg4SduFX amohpo/aT//vrMatBslit96NToS5NXdF5ecNsv5J/0lVBnR2f7DjzE21VyNU+cphYacU k2GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b=qeSQeGhL; 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 m15-20020a17090607cf00b0099b56bacec3si6690168ejc.955.2023.07.31.06.38.41; Mon, 31 Jul 2023 06:38:42 -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=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b=qeSQeGhL; 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 0D1CB68C1B5; Mon, 31 Jul 2023 16:38:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4708568BFD6 for ; Mon, 31 Jul 2023 16:38:14 +0300 (EEST) Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-7672303c831so375151685a.2 for ; Mon, 31 Jul 2023 06:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ltnglobal-com.20221208.gappssmtp.com; s=20221208; t=1690810692; x=1691415492; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=uFaQbwVUGmommYou1n30PZ0n+bW4u9ZL3194lhxhDy8=; b=qeSQeGhLYPKTginT4HPOVyOgGuN58FLS3IuEXgpbEPjFKJ/+mMSrLWLfjJKycR1xGI +q/JA8WS7UAABLuvaohd6AUgkiZs7POwb2AidfyS7Uxa4lsZ5UG5BQ5ibCvnERxq9Ewa 460KuH2yVS2z6HCN6wz56uPQCyiosInl0dVaQ627jY+abTJRKE5kb7UkM5S/svfhrVDR LfdEw7Tc5SyTn7IeUrZ5oRmBiYI8/nIpf3nNRAZj0ANp7KKqAqRtOGkSA7thPXympyrm 2Gm4ou4iwaHMeJCu9Fn3961MSSuFvwK5XbuOktDXF+sP5vN+rSIa8Qff5bNpycCanAbJ 4ORA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690810692; x=1691415492; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uFaQbwVUGmommYou1n30PZ0n+bW4u9ZL3194lhxhDy8=; b=hw0zORE9xfljFAoF33/G2IBq2hCxqzeGXyjq0jElWTdYmdarPyBE0HgZ1fFSL8cfeP BvNbz/Or5O8ub9ishgrGaEyZlc+3qp4zCOdMcQHaVEsZPZk30zw85FhLaZxbm2kHes7f CVHz7u5K2xhVyg67ConZ4eI/NuI1SAM8k8wDt7fAwk/xnrZVIhGmIdBnZ0uNiVO+rwHA 3XZGwAV94iiAlY5e29FhOf2FkvsCHoV4i4wuYoASmuunhb13eNsCeNc3+64Cgjrmu8f9 1QqdSPbhSPkBvd4LZfc5wd9vfkt4s18TUslLcovACR0OEuz+uTnFlOSfRCoWIcM0Z4Sf WuOA== X-Gm-Message-State: ABy/qLbAByFGQDs2SDmpXqDkqVuPwzcJyjS3fp3Fby84JLNK62INAzth aiMZs8SBUjjXgt/MosZIZqywOu8kgh2wB4kC9wg= X-Received: by 2002:a37:b801:0:b0:767:e97b:38b4 with SMTP id i1-20020a37b801000000b00767e97b38b4mr10570055qkf.4.1690810692606; Mon, 31 Jul 2023 06:38:12 -0700 (PDT) Received: from ltnt-nyc-580testdevin.livetimenet.com (pool-71-105-132-214.nycmny.fios.verizon.net. [71.105.132.214]) by smtp.gmail.com with ESMTPSA id o9-20020a05620a110900b0076c84240467sm2504235qkk.52.2023.07.31.06.38.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2023 06:38:11 -0700 (PDT) From: Devin Heitmueller X-Google-Original-From: Devin Heitmueller To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Jul 2023 09:38:04 -0400 Message-Id: <1690810686-4723-3-git-send-email-dheitmueller@ltnglobal.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> References: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> Subject: [FFmpeg-devel] [PATCH v4 2/4] mpegts: Stash original PTS for SCTE-35 sections for processing later X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Devin Heitmueller MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: FY06KhLGIX05 We need the original PTS value in order to do subsequent processing, so set it as packet side data. Signed-off-by: Devin Heitmueller --- libavformat/mpegts.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 0b3edda..a1b2420 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -1783,8 +1783,17 @@ static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section, prg = av_find_program_from_stream(ts->stream, NULL, idx); if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) { MpegTSFilter *f = ts->pids[prg->pcr_pid]; - if (f && f->last_pcr != -1) + if (f && f->last_pcr != -1) { + AVTransportTimestamp *transport_ts; ts->pkt->pts = ts->pkt->dts = f->last_pcr/300; + transport_ts = (AVTransportTimestamp *) av_packet_new_side_data(ts->pkt, + AV_PKT_DATA_TRANSPORT_TIMESTAMP, + sizeof(AVTransportTimestamp)); + if (transport_ts) { + transport_ts->pts = ts->pkt->pts; + transport_ts->time_base = av_make_q(1, 90000); + } + } } ts->stop_parse = 1; From patchwork Mon Jul 31 13:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Devin Heitmueller X-Patchwork-Id: 43051 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba09:b0:130:ccc6:6c4b with SMTP id fa9csp119680pzb; Mon, 31 Jul 2023 06:38:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlHr+MO5KnymWOEoXNgS2snpq5chhkIqPy70D8sWRDfFSZeKHPMPmSltBtIGRqTatpopTu6m X-Received: by 2002:a17:906:77d5:b0:993:d617:bdc5 with SMTP id m21-20020a17090677d500b00993d617bdc5mr6310114ejn.37.1690810730458; Mon, 31 Jul 2023 06:38:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690810730; cv=none; d=google.com; s=arc-20160816; b=OuRFuPyGWDkd9MgzAff9IaYEgxKzx83cQMUEZtUCkVSwYIbWJEwd27Y7XSjVR9iXvc E7fvM+H2gYNi9BJKzaiwN8j2ymalrQ2EpE24lEvAYAddkGIoRdiLamWTvniLorAN/mMT RimZBHFejYWQSb41l76MxdkVLqrAuf6IB6FNZhH4W3IqfAGwxZmOJfjoWKWxzoc6QCfI 8JX5HewaHzxl8uUxUPrvp2eIfyqBwIBGpFbxIxMoQ3ooPsiKYbJwu4KQwXAO4V9lT46T 9hmGeIMkK+pQvbxRNJmR5KnWLIEBgrKrpvlIWHcIGNgcc6mEEx7ViR8JWZJNHCbuO57h 4hIA== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=avgFnl6HNAVzdvvAjCF+yXZAt4n/dTgq+Y6281Jhzsw=; fh=DKzdJpyueDOox6ShoNYUi750QZs6R2naOS9+hcsrM90=; b=Xk8mDwmjlwBrM+SFMTiPy/RjwZLwGfvNiy8kl7O4ICAVBiAG3KYLip7ZxMVHYzxgFn F9HPpayzcW5gk8KTBQKT14DSibF9V1dgN+pEWp95UshlplzmbmGZ8KEdzWtnL8CWHzpt aPeAjaR9q+o7DM/WZZjAeQkMPABABA3U88ioq5a5NmWF1r8aREDvFrTOs/kdhkmuntt3 UEfZ+uGK8gQG1N8eNIzHPICN5QnZOHqn9kSI3aQyKa0oEe/2Gsi9UtvAW43eE4ImZSMs H0EtOLBxx8D6FQN80Pt0pFY11Pv4cdTxWfe0cL1RTSAsPPIXSm0SwtReXzNr4GtLmTeX 8foA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b=NrrnCS6b; 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 f9-20020a170906138900b009828e8bec17si7171082ejc.531.2023.07.31.06.38.50; Mon, 31 Jul 2023 06:38:50 -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=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b=NrrnCS6b; 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 0992C68C36E; Mon, 31 Jul 2023 16:38:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4426A68BFF8 for ; Mon, 31 Jul 2023 16:38:15 +0300 (EEST) Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-765a7768f1dso411567885a.0 for ; Mon, 31 Jul 2023 06:38:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ltnglobal-com.20221208.gappssmtp.com; s=20221208; t=1690810693; x=1691415493; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=mgn1IXm5zp+Oc2RiyejXeVRTpXXr4BQSkba+CMTiD5g=; b=NrrnCS6bKEeg6xbEr+aeZhM/pKNWw8wcCh0yncxwoAs48541hPa90Ox+OTgQ6CMdOl hfbD+s1XKIkGxjpH0MyrSj+qG8nl2XoCsmGUYKihCaC5COTSuIUK7KFYzXCgdDmN/YNk 6UUEPEigxfF1Cgj5TLRa4qE3EPGktwg4BBiMRl8nbKQT2i4vkuiUXfUrkIHiLfComN6w IwNHkuckPW24k39tIZ62rLxvwBBbUXz0JbZd1d1yrlSWFibCapZ+v22YCKs74zCy1+jZ T6797DpMM7nnKf+g1LZ3OQ6nUy0ETZfadUbO3w0lwE52l0G9Z+ruH75HyhKyGPcNJEAB xuMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690810693; x=1691415493; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mgn1IXm5zp+Oc2RiyejXeVRTpXXr4BQSkba+CMTiD5g=; b=BWIcgZ6y0aBCV4FTE0Yz9PjSyqkv62D6bqu7bsteo7B7MjZukoh3WeEKQsGmIaf/JT CtQkdevYrK9dc5WFibxuRMVULl3T3Da1BjRWdJcudbPy1VXm4N4Vv/gMZ70mDKL/NdXa a4t8lIN06+huTIWL9VHl6n0oKyjluSGNPXGCAMnVz242hwnNYjySFOGDmlEhkicnb20z QBo55FHbAzVDK91FiLNoQ5TkE9GtIssHlHdVjyJdyNG6hF7zClG1MoG9/x0afnudjdnG 3Dk+886q6Eg7098wmJIE/grm+TOjZvGswzG5a19gDxpVAl8bwuCr4B45o5D3J60EjlSb jBmQ== X-Gm-Message-State: ABy/qLZaZXDzZ2DzyCmE2f5I5cMc6Q+JK+2SI28me4aXj8HNi4pv/I1b Z9tvipT5JYql14UewWJsTObH39WFrTygG1k2zvQ= X-Received: by 2002:a05:620a:2551:b0:765:8063:c369 with SMTP id s17-20020a05620a255100b007658063c369mr12144136qko.74.1690810693581; Mon, 31 Jul 2023 06:38:13 -0700 (PDT) Received: from ltnt-nyc-580testdevin.livetimenet.com (pool-71-105-132-214.nycmny.fios.verizon.net. [71.105.132.214]) by smtp.gmail.com with ESMTPSA id o9-20020a05620a110900b0076c84240467sm2504235qkk.52.2023.07.31.06.38.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2023 06:38:12 -0700 (PDT) From: Devin Heitmueller X-Google-Original-From: Devin Heitmueller To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Jul 2023 09:38:05 -0400 Message-Id: <1690810686-4723-4-git-send-email-dheitmueller@ltnglobal.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> References: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> Subject: [FFmpeg-devel] [PATCH v4 3/4] mpegtsenc: Add support for output of SCTE-35 streams over TS X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Devin Heitmueller MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 7j2Pq2r4/pwT Introduce the ability to pass through SCTE-35 packets when creating MPEG transport streams. Note that this patch makes no effort to change the PTS values in the SCTE-35 packets, and thus only works when not reclocking the stream (i.e. using -copyts). A subsequent patch includes a BSF to recompute the PTS values. Signed-off-by: Devin Heitmueller --- libavformat/mpegts.h | 1 + libavformat/mpegtsenc.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++--- libavformat/mux.c | 6 ++-- 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h index a48f14e..a7aaaba 100644 --- a/libavformat/mpegts.h +++ b/libavformat/mpegts.h @@ -137,6 +137,7 @@ #define STREAM_TYPE_AUDIO_AC3 0x81 #define STREAM_TYPE_AUDIO_DTS 0x82 #define STREAM_TYPE_AUDIO_TRUEHD 0x83 +#define STREAM_TYPE_SCTE_35 0x86 #define STREAM_TYPE_AUDIO_EAC3 0x87 /* ISO/IEC 13818-1 Table 2-22 */ diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index 700fc54..c6cd1fd 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -425,6 +425,9 @@ static int get_dvb_stream_type(AVFormatContext *s, AVStream *st) case AV_CODEC_ID_SMPTE_2038: stream_type = STREAM_TYPE_PRIVATE_DATA; break; + case AV_CODEC_ID_SCTE_35: + stream_type = STREAM_TYPE_SCTE_35; + break; case AV_CODEC_ID_DVB_SUBTITLE: case AV_CODEC_ID_DVB_TELETEXT: case AV_CODEC_ID_ARIB_CAPTION: @@ -522,6 +525,16 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service) *q++ = 0xfc; // private_data_byte } + /* If there is an SCTE-35 stream, we need a registration descriptor + at the program level (SCTE 35 2016 Sec 8.1) */ + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) { + put_registration_descriptor(&q, MKTAG('C', 'U', 'E', 'I')); + break; + } + } + val = 0xf000 | (q - program_info_length_ptr - 2); program_info_length_ptr[0] = val >> 8; program_info_length_ptr[1] = val; @@ -533,6 +546,14 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service) const char default_language[] = "und"; const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language; enum AVCodecID codec_id = st->codecpar->codec_id; + uint16_t pid; + + if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) { + MpegTSSection *sect = st->priv_data; + pid = sect->pid; + } else { + pid = ts_st->pid; + } if (s->nb_programs) { int k, found = 0; @@ -556,7 +577,7 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service) stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st); *q++ = stream_type; - put16(&q, 0xe000 | ts_st->pid); + put16(&q, 0xe000 | pid); desc_length_ptr = q; q += 2; /* patched after */ @@ -819,6 +840,10 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service) putbuf(&q, tag, strlen(tag)); *q++ = 0; /* metadata service ID */ *q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */ + } else if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) { + *q++ = 0x8a; /* Cue Identifier Descriptor */ + *q++ = 0x01; /* length */ + *q++ = 0x01; /* Cue Stream Type (see Sec 8.2) */ } break; } @@ -1159,6 +1184,33 @@ static int mpegts_init(AVFormatContext *s) AVStream *st = s->streams[i]; MpegTSWriteStream *ts_st; + if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) { + struct MpegTSSection *sect; + sect = av_mallocz(sizeof(MpegTSSection)); + if (!sect) { + ret = AVERROR(ENOMEM); + continue; + } + + if (st->id < 16) { + sect->pid = ts->start_pid + i; + } else if (st->id < 0x1FFF) { + sect->pid = st->id; + } else { + av_log(s, AV_LOG_ERROR, + "Invalid stream id %d, must be less than 8191\n", st->id); + ret = AVERROR(EINVAL); + continue; + } + + sect->write_packet = section_write_packet; + sect->opaque = s; + sect->cc = 15; + sect->discontinuity= ts->flags & MPEGTS_FLAG_DISCONT; + st->priv_data = sect; + continue; + } + ts_st = av_mallocz(sizeof(MpegTSWriteStream)); if (!ts_st) { return AVERROR(ENOMEM); @@ -1877,6 +1929,19 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) dts += delay; } + if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) { + MpegTSSection *s = st->priv_data; + uint8_t data[SECTION_LENGTH]; + + if (size > SECTION_LENGTH) { + av_log(s, AV_LOG_ERROR, "SCTE-35 section too long\n"); + return AVERROR_INVALIDDATA; + } + memcpy(data, buf, size); + mpegts_write_section(s, data, size); + return 0; + } + if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) { av_log(s, AV_LOG_ERROR, "first pts and dts value must be set\n"); return AVERROR_INVALIDDATA; @@ -2149,7 +2214,8 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) return 0; } - if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size || + if (st->codecpar->codec_id != AV_CODEC_ID_SCTE_35 && + ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size || (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE && dts - ts_st->payload_dts >= max_audio_delay) || ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) { @@ -2194,7 +2260,7 @@ static void mpegts_write_flush(AVFormatContext *s) for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; MpegTSWriteStream *ts_st = st->priv_data; - if (ts_st->payload_size > 0) { + if (st->codecpar->codec_id != AV_CODEC_ID_SCTE_35 && ts_st->payload_size > 0) { mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size, ts_st->payload_pts, ts_st->payload_dts, ts_st->payload_flags & AV_PKT_FLAG_KEY, -1); @@ -2237,7 +2303,7 @@ static void mpegts_deinit(AVFormatContext *s) for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; MpegTSWriteStream *ts_st = st->priv_data; - if (ts_st) { + if (ts_st && st->codecpar->codec_id != AV_CODEC_ID_SCTE_35) { av_freep(&ts_st->dvb_ac3_desc); av_freep(&ts_st->payload); if (ts_st->amux) { diff --git a/libavformat/mux.c b/libavformat/mux.c index 415bd39..55bec25 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -307,7 +307,8 @@ FF_ENABLE_DEPRECATION_WARNINGS } if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT && - par->codec_id != AV_CODEC_ID_SMPTE_2038) + par->codec_id != AV_CODEC_ID_SMPTE_2038 && + par->codec_id != AV_CODEC_ID_SCTE_35) si->nb_interleaved_streams++; } si->interleave_packet = of->interleave_packet; @@ -946,7 +947,8 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *pkt, } else if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT && par->codec_id != AV_CODEC_ID_VP8 && par->codec_id != AV_CODEC_ID_VP9 && - par->codec_id != AV_CODEC_ID_SMPTE_2038) { + par->codec_id != AV_CODEC_ID_SMPTE_2038 && + par->codec_id != AV_CODEC_ID_SCTE_35) { ++noninterleaved_count; } } From patchwork Mon Jul 31 13:38:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Devin Heitmueller X-Patchwork-Id: 43052 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba09:b0:130:ccc6:6c4b with SMTP id fa9csp119783pzb; Mon, 31 Jul 2023 06:39:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlEiM5Ovy3ljwyCnDf5qaafwMn8VNrK5xMCdkoVHvPxuf8BdXM/qpY34nsOp+0ZIk9yENh4b X-Received: by 2002:a17:906:30c5:b0:992:a80e:e5bd with SMTP id b5-20020a17090630c500b00992a80ee5bdmr6533112ejb.48.1690810740304; Mon, 31 Jul 2023 06:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690810740; cv=none; d=google.com; s=arc-20160816; b=q8jXzZCkoKPH5CzKe41JlI8mQvrALcheQUjyfFe7fzPeYIw+tlPkIYJp7MS4142O9c yfef1mxaeYYqe6FoxSlxow694YlDKr0tZFhvumyNEGLIVlIY6GVII9i6KpkhudmqVMoU ijR4Zq6kFvUkT9r1d4VRzBU2/QGQ/VrxlcBiQ3PJ0YMKC74Nomj2PBKSClFHJc2DjQym Qm/PdAzPmxVXLgi6WkxRkYACjywtp8oDA89ifFxBDpee+YTXFIElsKaHxXrzexf+mjMd terZcOjBnigQbZCbfOolfKzDbxvQv2ge3G/ZwrizJ/BM6fV5DbJqyCXHIPUnneulvRlb FAlQ== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=9inipeD4toglyb+6pF2N7HmO2W2b42U///4lZpjRHw0=; fh=DKzdJpyueDOox6ShoNYUi750QZs6R2naOS9+hcsrM90=; b=oBF8tLSn+MLnbA4mpCV5dnKGbIJINeQIhLTmM2Ua9r0KUwF3NR90Etp+WJcMt2AHB+ v5+0E3YYwoVO3dXztF/J/LuOMXwbc4d7W1effY1XBAiZJ9MhvG4W2uVLhqlrjvcRXcgh Q7KyBkdOl+RSGD343zLL1uloHfcYpS2zXWoXj+d2fG+qmw0FJpzJyhCkqW2O7n62QEjp ejCcVA9OYbZ4pA+Enwj0KjpVaO6VeGmJByvx4H6TZ7VkGaea7jBz1APGjApnbMdDHzsM 8yf88ahPAzjPSK56NBR5GaMu/YZf37ujccwiw0AEvSiVDXTaFVj7XWNfrOkZ1WK2YeZA cGGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b=W9dEzoad; 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 q22-20020a17090622d600b00993470737basi5934592eja.986.2023.07.31.06.38.59; Mon, 31 Jul 2023 06:39:00 -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=@ltnglobal-com.20221208.gappssmtp.com header.s=20221208 header.b=W9dEzoad; 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 24F8068C24A; Mon, 31 Jul 2023 16:38:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 68CBC68C19C for ; Mon, 31 Jul 2023 16:38:16 +0300 (EEST) Received: by mail-qk1-f181.google.com with SMTP id af79cd13be357-76595a7b111so310466785a.2 for ; Mon, 31 Jul 2023 06:38:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ltnglobal-com.20221208.gappssmtp.com; s=20221208; t=1690810694; x=1691415494; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vYf4ig1MTZLtcEEhJLyoI2e39wcVu8GMKZrvgIkSuDA=; b=W9dEzoad5rYBAP/Pm1zF/BzQM0oHtuc/jO7ZdFBB/q/gX888lYS9ZAVf/X8ESYu6Nb 1HiIbonSIqtpbeY3KPJcLkx0P0t3cKaz8vNtk7cwayUTSDG5cUou5XbtY6cEf4nSMLdk RT5o/YAl93DcqIq4DwoEPPF5W2yeQJEQ53RIkit0dErfngKHIAGnZuHSuzcj0172aCNO 6ZJKdTUtiwYkSK3D0uG41HR0zzAvPFl6V8zOYOhWJD2Jl03ZWMMYZ6zLxQJsoFd6jA1c XNhOIA5fqwLqoaNFWLDivUGokGpCy3IrM/C79deeiAUCGpod8VteSIcErYxUJBgGwCHv sHjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690810694; x=1691415494; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vYf4ig1MTZLtcEEhJLyoI2e39wcVu8GMKZrvgIkSuDA=; b=L1Q85U8EYB8hkN4ggeLf6wAEULPqxwu/LCgW2ak4bhuFrgzWouaQA4SCX3/lSQGIdp wqLOsQvKcfg7pUMsT3EGd4OAFjlAX/hpW8FLDr47Uc6J784yyuOIF4w7RBGT9YB2e/VT AMB1A5ZzLaqhuNgRMVxoTDmPIzsGYMZfpTlECcJf5rvFrA7mQy4NmxkMF1ZNztXjBsPG kSubAs5Qz4V1EqTPb3QLdLJyDfsk/EVHd7vBZK97hphEAAOumZwFGXVELH00uOgnKUOM VEIuFD1bCaDKcMj/GP9OW0OR1TByndt8c5SP23qSnagjAJ8MhYsocXXaDrejvUMNp4BC pWaA== X-Gm-Message-State: ABy/qLYibsXb6UYSIobhrkm2wXQlroI3mOIw7n7FSh1lQ4fs5lFqbfZP 7KqjFoD9OxKOG0AGrV211e8df+65Nn64U9kXKaY= X-Received: by 2002:a05:620a:4727:b0:75e:bf51:29a6 with SMTP id bs39-20020a05620a472700b0075ebf5129a6mr11218881qkb.65.1690810694594; Mon, 31 Jul 2023 06:38:14 -0700 (PDT) Received: from ltnt-nyc-580testdevin.livetimenet.com (pool-71-105-132-214.nycmny.fios.verizon.net. [71.105.132.214]) by smtp.gmail.com with ESMTPSA id o9-20020a05620a110900b0076c84240467sm2504235qkk.52.2023.07.31.06.38.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2023 06:38:13 -0700 (PDT) From: Devin Heitmueller X-Google-Original-From: Devin Heitmueller To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Jul 2023 09:38:06 -0400 Message-Id: <1690810686-4723-5-git-send-email-dheitmueller@ltnglobal.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> References: <1690810686-4723-1-git-send-email-dheitmueller@ltnglobal.com> Subject: [FFmpeg-devel] [PATCH v4 4/4] bsf: Add new bitstream filter to set SCTE-35 pts_adjustment when reclocking X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Devin Heitmueller MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Fb+uT+SxuXu6 Because SCTE-35 messages are represented in TS streams as sections rather than PES packets, we cannot rely on ffmpeg's standard mechanisms to adjust PTS values if reclocking the stream. This filter will leverage the SCTE-35 pts_adjust field to compensate for any change in the PTS values in the stream. See SCTE-35 2019 Sec 9.6 for information about the use of the pts_adjust field. This filter also tweaks the mpegtsenc mux to automatically add it so the user doesn't have to include it manually. Thanks to Andreas Rheinhardt and James Almer for providing feedback/suggestions on improving the patch. Signed-off-by: Devin Heitmueller --- doc/bitstream_filters.texi | 9 ++++ libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/scte35ptsadjust_bsf.c | 100 +++++++++++++++++++++++++++++++++++++++ libavformat/mpegtsenc.c | 2 + 5 files changed, 113 insertions(+) create mode 100644 libavcodec/scte35ptsadjust_bsf.c diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index c63c203..068b0c9 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -797,6 +797,15 @@ Remove extradata from all frames. @end table @end table +@section scte35ptsadjust +This bitstream filter makes use of side data injected by the MPEG-TS demux +in order to rewrite the PTS adjust field within SCTE-35 packets. This +ensures the pts_adjust field contains a valid value if the caller changes +the timebase of the stream. + +The bitstream filter is added automatically by the mpegtsenc mux, and no +action is required on the part of the user. + @section setts Set PTS and DTS in packets. diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3c16b51..efe0ae9 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1253,6 +1253,7 @@ OBJS-$(CONFIG_PCM_RECHUNK_BSF) += pcm_rechunk_bsf.o OBJS-$(CONFIG_PGS_FRAME_MERGE_BSF) += pgs_frame_merge_bsf.o OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o av1_parse.o +OBJS-$(CONFIG_SCTE35PTSADJUST_BSF) += scte35ptsadjust_bsf.o OBJS-$(CONFIG_SETTS_BSF) += setts_bsf.o OBJS-$(CONFIG_TEXT2MOVSUB_BSF) += movsub_bsf.o OBJS-$(CONFIG_TRACE_HEADERS_BSF) += trace_headers_bsf.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index 1e9a676..60ed164 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -57,6 +57,7 @@ extern const FFBitStreamFilter ff_pcm_rechunk_bsf; extern const FFBitStreamFilter ff_pgs_frame_merge_bsf; extern const FFBitStreamFilter ff_prores_metadata_bsf; extern const FFBitStreamFilter ff_remove_extradata_bsf; +extern const FFBitStreamFilter ff_scte35ptsadjust_bsf; extern const FFBitStreamFilter ff_setts_bsf; extern const FFBitStreamFilter ff_text2movsub_bsf; extern const FFBitStreamFilter ff_trace_headers_bsf; diff --git a/libavcodec/scte35ptsadjust_bsf.c b/libavcodec/scte35ptsadjust_bsf.c new file mode 100644 index 0000000..b8bf48d --- /dev/null +++ b/libavcodec/scte35ptsadjust_bsf.c @@ -0,0 +1,100 @@ +/* + * SCTE-35 PTS fixup bitstream filter + * Copyright (c) 2023 LTN Global Communications, Inc. + * + * Author: Devin Heitmueller + * + * Because SCTE-35 messages are represented in TS streams as sections + * rather than PES packets, we cannot rely on ffmpeg's standard + * mechanisms to adjust PTS values if reclocking the stream. + * This filter will leverage the SCTE-35 pts_adjust field to + * compensate for any change in the PTS values in the stream. + * + * See SCTE-35 2019 Sec 9.6 for information about the use of + * the pts_adjust field. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "bsf.h" +#include "bsf_internal.h" +#include "defs.h" +#include "libavutil/intreadwrite.h" + +static int scte35ptsadjust_filter(AVBSFContext *ctx, AVPacket *pkt) +{ + const AVTransportTimestamp *transport_ts; + int64_t cur_pts_adjust; + int ret = 0; + + ret = ff_bsf_get_packet_ref(ctx, pkt); + if (ret < 0) + return ret; + + /* Retrieve the original PTS, which will be used to calculate the pts_adjust */ + transport_ts = (AVTransportTimestamp *) av_packet_get_side_data(pkt, AV_PKT_DATA_TRANSPORT_TIMESTAMP, NULL); + if (transport_ts == NULL) { + /* No original PTS specified, so just pass the packet through */ + return 0; + } + + /* The pts_adjust field is logically buf[4]-buf[8] of the payload */ + if (pkt->size < 9) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + /* Extract the current pts_adjust value from the packet */ + cur_pts_adjust = ((int64_t)(pkt->data[4] & 1) << 32 ) | + AV_RB32(pkt->data + 5); + + av_log(ctx, AV_LOG_DEBUG, "pts=%" PRId64 "(%d/%d) orig_pts=%" PRId64 "(%d/%d) pts_adjust=%" PRId64 "\n", + pkt->pts, pkt->time_base.num, pkt->time_base.den, + transport_ts->pts, transport_ts->time_base.num, transport_ts->time_base.den, cur_pts_adjust); + + /* Compute the new PTS adjust value */ + cur_pts_adjust -= av_rescale_q(transport_ts->pts, transport_ts->time_base, (AVRational){1, 90000}); + cur_pts_adjust += av_rescale_q(pkt->pts, pkt->time_base, (AVRational){1, 90000}); + cur_pts_adjust &= 0x1FFFFFFFFLL; + + av_log(ctx, AV_LOG_DEBUG, "new pts_adjust=%" PRId64 "\n", cur_pts_adjust); + + ret = av_packet_make_writable(pkt); + if (ret < 0) + goto fail; + + /* Insert the updated pts_adjust value */ + pkt->data[4] &= 0xfe; /* Preserve top 7 unrelated bits */ + pkt->data[4] |= cur_pts_adjust >> 32; + AV_WB32(pkt->data + 5, cur_pts_adjust); + +fail: + if (ret < 0) + av_packet_unref(pkt); + + return ret; +} + +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_SCTE_35, AV_CODEC_ID_NONE, +}; + +const FFBitStreamFilter ff_scte35ptsadjust_bsf = { + .p.name = "scte35ptsadjust", + .p.codec_ids = codec_ids, + .filter = scte35ptsadjust_filter, +}; diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index c6cd1fd..48d7833 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -2337,6 +2337,8 @@ static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st, (st->codecpar->extradata_size > 0 && st->codecpar->extradata[0] == 1))) ret = ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL); + } else if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) { + ret = ff_stream_add_bitstream_filter(st, "scte35ptsadjust", NULL); } return ret;