From patchwork Wed Aug 3 13:58:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37116 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1649:b0:8b:613a:194d with SMTP id no9csp459625pzb; Wed, 3 Aug 2022 07:02:17 -0700 (PDT) X-Google-Smtp-Source: AA6agR4kEJeFjHqPvLo7YwzRcx8kXnSMxp0ylJV7Ee6Nvom2MJSZyoLqSUFcJ2HTORLYZeq2FguR X-Received: by 2002:a17:906:98d5:b0:730:4f6b:ce28 with SMTP id zd21-20020a17090698d500b007304f6bce28mr15357516ejb.736.1659535337746; Wed, 03 Aug 2022 07:02:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659535337; cv=none; d=google.com; s=arc-20160816; b=RS6psK7yG1bstZvEvX+QiQCOV19DQdi/ePZ8czFP4FtJmpA2jm5DTnGVlMNJj7FVud 1726Sa7GoH9o21yRrHD31yU9Ff7dkLC8f9wlks1VV0YVbZEz38KrA5eSfNs0YsxfWtZJ OC9u6AFZnktXSpF75/3/rIY4YR+iz/vPSU9i9YlealQkZ8yX4sKgDY2BEOkwdzlSs6bP Zx38J0Pd7//u7B16EPlQiwRQc1bTQTaRaz4phTvVQdmo6aNbyV9rCi63hgVZ0pD3Xiko 1mfBftqidto8LroHfyxh2VVaOLLw0w4gHhxWOY6wPITp5ArleGlKAY8qmodzf0O0BzEV LJeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding: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:delivered-to; bh=vktvzHkyTK0g61gi3ijlNTEh8NZscUDVQbziBjBF+2Q=; b=FQZ4kwGtPI1HglXL9O3bkTvoJc7M34eUA+XRUqSU+/1LgWe7/JXTqe/IgfWjbRlEwR Sa6QofjFKY5kej7ckUyEDHLdxU4znU3GXzQ/k4R9zqs3LsUZaPXW0ss7li1rIOApcC3B 9IczpSv/B4sNnlDx5JJU0aDO8qW5HHUCPjW2g8olA9fMvOc77UYMiT/TO2FLAAHNmUyo 7t5LvqyfTgDUiRwd6dol8+D8Q8G42T6wXeglP62v6ZKvV82mo0hP3YMRXfcKoU4Lo8zm /IxEvZAa2Uxm4RuUP0dgrN7v6W2kZpGYH54Zoh6Oicspomqp1dKBVd6zIoMkrVzEYAIt 4TNQ== ARC-Authentication-Results: i=1; mx.google.com; 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 hq12-20020a1709073f0c00b007309e910729si3988721ejc.580.2022.08.03.07.02.17; Wed, 03 Aug 2022 07:02:17 -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; 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 5DAB668B9B5; Wed, 3 Aug 2022 16:59:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E792168B89E for ; Wed, 3 Aug 2022 16:59:06 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 88024240506 for ; Wed, 3 Aug 2022 15:59:06 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id wYnIzWpj-yDd for ; Wed, 3 Aug 2022 15:59:05 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 2320224068C for ; Wed, 3 Aug 2022 15:58:55 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 438C43A06A9; Wed, 3 Aug 2022 15:58:52 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Aug 2022 15:58:40 +0200 Message-Id: <20220803135844.16662-21-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220803135844.16662-1-anton@khirnov.net> References: <20220803135844.16662-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 21/25] fftools/ffmpeg: move seek_to_start() to ffmpeg_demux.c 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: SIf2GF821fH9 Reduces the diff in the following commit. --- fftools/ffmpeg.c | 79 ------------------------------------------ fftools/ffmpeg.h | 1 + fftools/ffmpeg_demux.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 79 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 497a847101..90e25973d3 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3628,85 +3628,6 @@ static void reset_eagain(void) output_streams[i]->unavailable = 0; } -// set duration to max(tmp, duration) in a proper time base and return duration's time_base -static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base, - AVRational time_base) -{ - int ret; - - if (!*duration) { - *duration = tmp; - return tmp_time_base; - } - - ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base); - if (ret < 0) { - *duration = tmp; - return tmp_time_base; - } - - return time_base; -} - -static int seek_to_start(InputFile *ifile, AVFormatContext *is) -{ - InputStream *ist; - AVCodecContext *avctx; - int i, ret, has_audio = 0; - int64_t duration = 0; - - ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0); - if (ret < 0) - return ret; - - for (i = 0; i < ifile->nb_streams; i++) { - ist = input_streams[ifile->ist_index + i]; - avctx = ist->dec_ctx; - - /* duration is the length of the last frame in a stream - * when audio stream is present we don't care about - * last video frame length because it's not defined exactly */ - if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) - has_audio = 1; - } - - for (i = 0; i < ifile->nb_streams; i++) { - ist = input_streams[ifile->ist_index + i]; - avctx = ist->dec_ctx; - - if (has_audio) { - if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) { - AVRational sample_rate = {1, avctx->sample_rate}; - - duration = av_rescale_q(ist->nb_samples, sample_rate, ist->st->time_base); - } else { - continue; - } - } else { - if (ist->framerate.num) { - duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base); - } else if (ist->st->avg_frame_rate.num) { - duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base); - } else { - duration = 1; - } - } - if (!ifile->duration) - ifile->time_base = ist->st->time_base; - /* the total duration of the stream, max_pts - min_pts is - * the duration of the stream without the last frame */ - if (ist->max_pts > ist->min_pts && ist->max_pts - (uint64_t)ist->min_pts < INT64_MAX - duration) - duration += ist->max_pts - ist->min_pts; - ifile->time_base = duration_max(duration, &ifile->duration, ist->st->time_base, - ifile->time_base); - } - - if (ifile->loop > 0) - ifile->loop--; - - return ret; -} - /* * Return * - 0 -- one packet was read and processed diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index f983d077d9..2a9c34eb93 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -715,5 +715,6 @@ int init_input_threads(void); int init_input_thread(int i); void free_input_threads(void); void free_input_thread(int i); +int seek_to_start(InputFile *ifile, AVFormatContext *is); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index 1e95be349c..ff9313b040 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -42,6 +42,85 @@ static void report_new_stream(InputFile *file, AVPacket *pkt) file->nb_streams_warn = pkt->stream_index + 1; } +// set duration to max(tmp, duration) in a proper time base and return duration's time_base +static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base, + AVRational time_base) +{ + int ret; + + if (!*duration) { + *duration = tmp; + return tmp_time_base; + } + + ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base); + if (ret < 0) { + *duration = tmp; + return tmp_time_base; + } + + return time_base; +} + +int seek_to_start(InputFile *ifile, AVFormatContext *is) +{ + InputStream *ist; + AVCodecContext *avctx; + int i, ret, has_audio = 0; + int64_t duration = 0; + + ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0); + if (ret < 0) + return ret; + + for (i = 0; i < ifile->nb_streams; i++) { + ist = input_streams[ifile->ist_index + i]; + avctx = ist->dec_ctx; + + /* duration is the length of the last frame in a stream + * when audio stream is present we don't care about + * last video frame length because it's not defined exactly */ + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) + has_audio = 1; + } + + for (i = 0; i < ifile->nb_streams; i++) { + ist = input_streams[ifile->ist_index + i]; + avctx = ist->dec_ctx; + + if (has_audio) { + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) { + AVRational sample_rate = {1, avctx->sample_rate}; + + duration = av_rescale_q(ist->nb_samples, sample_rate, ist->st->time_base); + } else { + continue; + } + } else { + if (ist->framerate.num) { + duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base); + } else if (ist->st->avg_frame_rate.num) { + duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base); + } else { + duration = 1; + } + } + if (!ifile->duration) + ifile->time_base = ist->st->time_base; + /* the total duration of the stream, max_pts - min_pts is + * the duration of the stream without the last frame */ + if (ist->max_pts > ist->min_pts && ist->max_pts - (uint64_t)ist->min_pts < INT64_MAX - duration) + duration += ist->max_pts - ist->min_pts; + ifile->time_base = duration_max(duration, &ifile->duration, ist->st->time_base, + ifile->time_base); + } + + if (ifile->loop > 0) + ifile->loop--; + + return ret; +} + static void *input_thread(void *arg) { InputFile *f = arg;