From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 41A85470F1 for ; Thu, 27 Jul 2023 15:33:14 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C682968CA4F; Thu, 27 Jul 2023 18:33:11 +0300 (EEST) Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 35E8B68C8D3 for ; Thu, 27 Jul 2023 18:33:06 +0300 (EEST) Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3a425ef874dso950501b6e.0 for ; Thu, 27 Jul 2023 08:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690471984; x=1691076784; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=+AsFujUQqCO0+cMPS2OVfEXHRJ2xE30IiEjWLZRESm4=; b=jpN+SkTg8u8oLTsW5MbFJF9gZPZ8jQuc/ItPdvgj4xLtvIsnY4GNu2fIOE5JuAoT5G MkAhmWQIoYT5rxJJK/omt2Jhv2yw1GuyhVnlnMhQa+LkEnomwCPpgUV+dgMOkk1gjl/m U/35hTORseiFEIDoTTgSrjkpmr1IO15e+Op+1vFaKGb73+EaWPaDzGIs3nA87jxHICbE 1VhmFf6oQRPnbjqw9b7Y8cL/w96jcZBrqNlFAYe5YNelGucFtDrtTyqdALSvMv8h5j/f gj8gX1UrlRAllF8iT5xOOUHV+FWTqRMO+t7GgVaA78289s7Jpf3OtfbpudiJ6u0bU4hr 68Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690471984; x=1691076784; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+AsFujUQqCO0+cMPS2OVfEXHRJ2xE30IiEjWLZRESm4=; b=CYN/UFc2fpGAnYTirjPHbVTrpd+B+Y/KoISAQd9y1IHs9TMFVjFZLJbkNh4Y5onmkB WrbwSskfhf5OQOVyyZ559WwxgzAGNjSdtQ83FrOlWesRoqwGK/sUfavWDJtFM+klkdO8 rPl/OW59C4yN7Jf05M7R3EMd/wXoHZHt+/aAO1bE4jSORTujhe6G6pwlasTPvJ/H64RK WmAUpxehWrifvPWWKxelSuuzu4jZL9DV/FMHg8mbEVc1q+AydCIm+7KgChJzbWFBJYMl VZNFZAfTUeeh/VsCY8mcCaJ7040dumlENPT2LOSaQ3vJ9+nKLRXUcfHfc1Dgr0TGwLDu MGVQ== X-Gm-Message-State: ABy/qLb3ZtTj9FHlhelzoJijgj0NOiIbIFzbW/YUeFg/ZJu0rdByzBPp EF6kWK5UveeTJQrQ5ulbC337AcCjz9w= X-Google-Smtp-Source: APBJJlGive4FHUwB6IBc2pS3MZHIcF5R/7FiEfYXGOfZ2RjggUMRRE7+3uk5JZmE2i67wxSzOUg0ig== X-Received: by 2002:a05:6808:1153:b0:3a3:b058:6c80 with SMTP id u19-20020a056808115300b003a3b0586c80mr4105010oiu.0.1690471984058; Thu, 27 Jul 2023 08:33:04 -0700 (PDT) Received: from [192.168.0.16] (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id bf33-20020a056808192100b003a37372e6b2sm731131oib.21.2023.07.27.08.33.03 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Jul 2023 08:33:03 -0700 (PDT) Message-ID: Date: Thu, 27 Jul 2023 12:33:09 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <1689971878-26226-1-git-send-email-dheitmueller@ltnglobal.com> <1689971878-26226-5-git-send-email-dheitmueller@ltnglobal.com> From: James Almer In-Reply-To: <1689971878-26226-5-git-send-email-dheitmueller@ltnglobal.com> Subject: Re: [FFmpeg-devel] [PATCH v3 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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On 7/21/2023 5:37 PM, Devin Heitmueller wrote: > 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 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 | 103 +++++++++++++++++++++++++++++++++++++++ > libavformat/mpegtsenc.c | 2 + > 5 files changed, 116 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 1b0226c..4c1b312 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..9870737 > --- /dev/null > +++ b/libavcodec/scte35ptsadjust_bsf.c > @@ -0,0 +1,103 @@ > +/* > + * 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] & (int64_t) 0x01 << 32) | > + ((int64_t) pkt->data[5] << 24) | > + ((int64_t) pkt->data[6] << 16) | > + ((int64_t) pkt->data[7] << 8) | > + ((int64_t) pkt->data[8] ); I think this is cleaner: 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 &= (int64_t) 0x1ffffffff; 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; _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".