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 B9A7144B86 for ; Sun, 5 Feb 2023 23:33:29 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BDA1868BD3D; Mon, 6 Feb 2023 01:33:26 +0200 (EET) Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8CBC468BD25 for ; Mon, 6 Feb 2023 01:33:20 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 2BC46E8734 for ; Mon, 6 Feb 2023 00:33:19 +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 ZUAdjzljmuDO for ; Mon, 6 Feb 2023 00:33:16 +0100 (CET) Received: from iq (iq [217.27.212.140]) by iq.passwd.hu (Postfix) with ESMTPS id 51F75E8733 for ; Mon, 6 Feb 2023 00:33:16 +0100 (CET) Date: Mon, 6 Feb 2023 00:33:16 +0100 (CET) From: Marton Balint To: FFmpeg development discussions and patches In-Reply-To: <20230131124512.281726-1-jpcoiner@gmail.com> Message-ID: References: <20230131124512.281726-1-jpcoiner@gmail.com> MIME-Version: 1.0 Subject: Re: [FFmpeg-devel] [PATCH] libavformat/mpegtsenc.c -- correctly re-emit extradata ahead of IDR pictures 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 Tue, 31 Jan 2023, John Coiner wrote: > This replaces and obsoletes the earlier version of this patch, which had a bug. Thanks to Marton Balint for spotting it. > > This fixes https://trac.ffmpeg.org/ticket/10148, it ensures that each IDR frame is prefixed with SPS/PPS. > > Tested manually, with inputs containing AUDs (x264's aud=1 mode) and also without (aud=0) to confirm it still inserts AUDs when necessary, and not otherwise. Your code duplicates AUD-s if they are already present and sps is also added: if original packet had [AUD][IDR] it will become [AUD][SPS][PPS][AUD][IDR]. Regards, Marton > > --- > libavformat/mpegtsenc.c | 21 +++++++++++++++------ > 1 file changed, 15 insertions(+), 6 deletions(-) > > diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c > index 48d39e6a7d..64948f4d75 100644 > --- a/libavformat/mpegtsenc.c > +++ b/libavformat/mpegtsenc.c > @@ -1877,6 +1877,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) > > if (st->codecpar->codec_id == AV_CODEC_ID_H264) { > const uint8_t *p = buf, *buf_end = p + size; > + uint8_t found_aud = 0; > uint32_t state = -1; > int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0; > int ret = ff_check_h264_startcode(s, st, pkt); > @@ -1886,17 +1887,25 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) > if (extradd && AV_RB24(st->codecpar->extradata) > 1) > extradd = 0; > > + // Ensure that all pictures are prefixed with an AUD, and that > + // IDR pictures are also prefixed with SPS and PPS. SPS and PPS > + // are assumed to be available in 'extradata' if not found in-band. > do { > p = avpriv_find_start_code(p, buf_end, &state); > av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", state & 0x1f); > - if ((state & 0x1f) == 7) > + if ((state & 0x1f) == 7) // SPS NAL > extradd = 0; > - } while (p < buf_end && (state & 0x1f) != 9 && > - (state & 0x1f) != 5 && (state & 0x1f) != 1); > - > - if ((state & 0x1f) != 5) > + if ((state & 0x1f) == 9) // AUD > + found_aud = 1; > + } while (p < buf_end > + && (state & 0x1f) != 5 // IDR picture > + && (state & 0x1f) != 1 // non-IDR picture > + && (extradd > 0 || !found_aud)); > + if ((state & 0x1f) != 5) { > + // Did not find IDR picture; do not emit extradata. > extradd = 0; > - if ((state & 0x1f) != 9) { // AUD NAL > + } > + if (extradd > 0 || !found_aud) { > data = av_malloc(pkt->size + 6 + extradd); > if (!data) > return AVERROR(ENOMEM); > -- > 2.39.1.456.gfc5497dd1b-goog > > _______________________________________________ > 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". > _______________________________________________ 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".