From patchwork Fri Jan 28 18:52:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustav Grusell X-Patchwork-Id: 33896 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp696366iov; Fri, 28 Jan 2022 10:54:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyeNvz3inv/eNiPMpvqLkiGrPdnB2qlMkV2ykmPrVNiEX7YrY0wC6a8PHFtiAe91R+uim8t X-Received: by 2002:a17:907:6298:: with SMTP id nd24mr7856007ejc.76.1643396045699; Fri, 28 Jan 2022 10:54:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643396045; cv=none; d=google.com; s=arc-20160816; b=P0TmoCxYoAFT18jhb4G9heqZLg9fjjbWUJOK/s6TYD4VPl81PmmOXADeorh/C42FXp Li2Sh2vSFJWS1ckLN8NAd5fcRNw9MG/EiTZbBy+g3ebafZ+6lNslv23nOi88uaQrkOse zrhXsd2F7MYe51H/YXKwlWVseqaniOE7eNqC/6O0bddZYxPTeAidX+/bsHbFB1SX2MDS tUmuHdoajvDQ+2morT7vnikAq3jaVlSGD+zy9dtrLsH3Ea4UqtN8IiYNq38yVADcc9i8 F+SDN57y4cbAefz++llhTQCxDD5w/j1adzpU1DvqgQ0XT4ceDvmlj/+hW9wFuYFNsFq7 SuYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=8VQhZ13QXn4mhO6WWisXHBpaZoWPBeZ4joeL/yBT/Vs=; b=ZIkYXbBBNwbZRBTyVzn6GvScRi+7U3O+m3cNF5Qv10AL0OjJ/sLtttTrOP0AslUv9Y DCSMNwEM2QhGqZIKZWXJt9ibbxt/6+yaeUdj3pLGwJVjOZaAvEDq7dkiH5tDuQ+yh05U 9zbv8hORR9uWbg6E4bxZ7F6bBmukV0MDSlHVgfuUQlsfh7g7VUCrMDDvhcoU3Ha+yNXT 9eAVbJ61wTzbh5hM6CcwnZXZAVmXb/nTw51UWQbrheJOEGwvs2Tn9YlUut3S+pi72vyx zpL7aZ04RhxAbm/7ZfGSQXp3Gm8DWwXEKRFD9BaE8rcj10BrF6VJ0dAsVxTGSawiyYyn 9Igg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=HmyOqFgV; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 14si3116584ejj.778.2022.01.28.10.54.04; Fri, 28 Jan 2022 10:54:05 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=HmyOqFgV; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4DA0168B1C7; Fri, 28 Jan 2022 20:54:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 544F268B0FC for ; Fri, 28 Jan 2022 20:53:53 +0200 (EET) Received: by mail-lf1-f48.google.com with SMTP id bu18so13607108lfb.5 for ; Fri, 28 Jan 2022 10:53:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=U7AGIPaW0lsuqGH2JOIAjlt528WW9RVUHGV92pmnkng=; b=HmyOqFgVFtUMTlrPF8HRk/dXpOaz674q5xa5MjpuF3yyIYSZahFsq1F3Co9HdBCBf2 JiC86kIeyl9y3iuPMIe/bx1H629GzIHtlS8JklG4DTL7lbJ+ZiGcTnlvSZvlvVsPPMWH VFTV8dixYx8sIRmaTIp2IF1yn4zxe+PZrLXeEIDYRMi1HgaZxWtUwe80FNadLMu3oet9 SYZ1P5cYmqz82XfVoNynne0pPFoGDH+tnXlMRGtG663bjjHQwyrL2FyhNEiFGHc/ufAk onICZMptpjf75PF6h6iTMDvyltWC0iYdouiXTJxH8qHTEdGL+1ByYXIgEqYVWeV8YTng bX+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=U7AGIPaW0lsuqGH2JOIAjlt528WW9RVUHGV92pmnkng=; b=KnwGR2rM9/S5AK1STmc0qyQnCR/iL9gvxtlUvczpUW/xZ9d5kDoZFfcza0I135hnbS IlF5KMpanDMFw+SdkJUz8eK/sXn93+hyMasuxEdkp5pU8Sy+zkp9DVDbu2A0ycUrHFSj EDqrq0G6NwI0dV4WNX+YL1yPUXzuORKqSaF5FXlYFSLlmu0/V7uFPRtWUuV8W13da7JE jurtklevYxRqzMzBru9wWowQHdI3F8hkdxFgSwv30amzeu+V0v5z/uZTdqApPSPR9pwz yjB4Sg08ZR+jedU8MHQvQ9NM60xsgKcpBh+1SSsm29hZled90wmNgU76JXd7VN4hHPIb pRzA== X-Gm-Message-State: AOAM532Zq94wN9QENwUe0U33N3YN6IMEK7qe9ESpxvtvFtHikoUGtzI0 GCeQbr1QmhK/LVQG3hBrqrnkQi2p/Xs= X-Received: by 2002:ac2:5201:: with SMTP id a1mr6791556lfl.146.1643396032280; Fri, 28 Jan 2022 10:53:52 -0800 (PST) Received: from localhost.localdomain (c80-216-190-179.bredband.tele2.se. [80.216.190.179]) by smtp.googlemail.com with ESMTPSA id c9sm2062372lfs.169.2022.01.28.10.53.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 10:53:51 -0800 (PST) From: Gustav Grusell To: ffmpeg-devel@ffmpeg.org Date: Fri, 28 Jan 2022 19:52:54 +0100 Message-Id: <20220128185254.1662873-1-gustav.grusell@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3] avformat/hls Implement support for using AVSEEK_FLAG_BACKWARD when seeking 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: Gustav Grusell Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: rfxJgXiVVeDv Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if seeking in videostream and AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485. Signed-off-by: Gustav Grusell --- libavformat/hls.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 4568e72cb2..44afdaab42 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1653,7 +1653,8 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int64_t *seq_no) + int64_t timestamp, int64_t *seq_no, + int64_t *seg_start_ts) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1668,6 +1669,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t diff = pos + pls->segments[i]->duration - timestamp; if (diff > 0) { *seq_no = pls->start_seq_no + i; + if (seg_start_ts) { + *seg_start_ts = pos; + } return 1; } pos += pls->segments[i]->duration; @@ -1691,7 +1695,7 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * playlist) and this is a complete file, find the matching segment * by counting durations. */ if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { - find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no); + find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); return seq_no; } @@ -2362,7 +2366,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; - int64_t seq_no; + int64_t seq_no, seg_start_ts; if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS); @@ -2372,8 +2376,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE, s->streams[stream_index]->time_base.den, - flags & AVSEEK_FLAG_BACKWARD ? - AV_ROUND_DOWN : AV_ROUND_UP); + AV_ROUND_DOWN); duration = s->duration == AV_NOPTS_VALUE ? 0 : s->duration; @@ -2394,9 +2397,16 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, } /* check if the timestamp is valid for the playlist with the * specified stream index */ - if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no)) + if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts)) return AVERROR(EIO); + if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + flags & AVSEEK_FLAG_BACKWARD && !(flags & AVSEEK_FLAG_ANY)) { + /* Seeking to start of segment ensures we seek to a keyframe located + * before the given timestamp. */ + seek_timestamp = seg_start_ts; + } + /* set segment now so we do not need to search again below */ seek_pls->cur_seq_no = seq_no; seek_pls->seek_stream_index = stream_subdemuxer_index; @@ -2423,7 +2433,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, if (pls != seek_pls) { /* set closest segment seq_no for playlists not handled above */ - find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no); + find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no, NULL); /* seek the playlist to the given position without taking * keyframes into account since this playlist does not have the * specified stream where we should look for the keyframes */