From patchwork Wed Aug 3 13:58:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37118 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1649:b0:8b:613a:194d with SMTP id no9csp459980pzb; Wed, 3 Aug 2022 07:02:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1svMXoggpfSWNiBuIv501PGuW8mUpUJJyxd+H3zuLhxTkYwMA1AapytHTDqJlOaPeC0+c1B X-Received: by 2002:a17:906:5d16:b0:72f:9f40:d1eb with SMTP id g22-20020a1709065d1600b0072f9f40d1ebmr20082032ejt.403.1659535365581; Wed, 03 Aug 2022 07:02:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659535365; cv=none; d=google.com; s=arc-20160816; b=AEgPjfGa/ddC6iovKFABSaBsfVyfLfdkcSymXVhVV0Q3LQKhvDqanDdzH5NbAif5mr JCVDmnbYz+fDfYVbxjFk0SYm4BWGSJELD9xdx0Mz+v1cBZ0erLviaH2dIed//qnhtzIP 5BxByxQU1mbqaY0+bNrpzBUengLUpyDfOog7j64H8nOhrfgWVsvDIDPe5/hhsQv5seHk Yy549ZDYPV8ru8dbiF+rbybHIwfW8q858MuodgFzjt3rh53jipIfijX7bZmLww0f1WCx tRz5cq9O0O5/Fb+Z97VI5CPx9XyKRnfDesPqfU3VVNIrZigF+YBiI2qOLlujDQ/GvdTQ j+mQ== 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=jztfJDe50aB0ly+H5/kYKbIXRikakgUB8oCHS8bXvsg=; b=vJfxKxHRSFSv+9cCVhuWuNio4BrnC3PbUxZdzKOETH5zmF01UY1Ds9PVsXu5c1vmyw 1CcFKHROMYC1pIVyehhOvCYPzsIzuMkmQ05F3LUshWxZr36LOI081b3FGfvjsLWX7fZ9 xOxBGtGY0wItWeIyyvrUpmC9kcIdUzp+CAjWXeDCoJUuyfENePmtZiJS3BH/pEfaigup MpGwvqjBsa/n062OPCwgiSE5c2bsyhJBwGV9N4YO3KLqkj6veeKY00UqG1/KJdm4CdOj 7kUzi6/E+aNzVj9X4lqaJ9dhEqEGEEueLS9f3YdVDOO4PUL8q+ctHjJipiEj7XMV3UzH BaHQ== 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 s4-20020a17090699c400b00730ab8060e3si2705649ejn.979.2022.08.03.07.02.38; Wed, 03 Aug 2022 07:02:45 -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 6D30368B9C7; Wed, 3 Aug 2022 16:59:23 +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 6924C68B895 for ; Wed, 3 Aug 2022 16:59:02 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 5550924055A for ; Wed, 3 Aug 2022 15:59:00 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id X3ghKctsAsDE for ; Wed, 3 Aug 2022 15:58:59 +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 DF63E24052D for ; Wed, 3 Aug 2022 15:58:54 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 2D1253A04EA; Wed, 3 Aug 2022 15:58:52 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Aug 2022 15:58:35 +0200 Message-Id: <20220803135844.16662-16-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 16/25] fftools/ffmpeg: move the input thread into its own file 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: 0+vVhW7+fk+t It will contain more demuxing-specific code in the future. --- fftools/Makefile | 1 + fftools/ffmpeg.c | 110 --------------------------------- fftools/ffmpeg.h | 5 ++ fftools/ffmpeg_demux.c | 136 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 110 deletions(-) create mode 100644 fftools/ffmpeg_demux.c diff --git a/fftools/Makefile b/fftools/Makefile index 6285e6eacb..5907f5c57e 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -10,6 +10,7 @@ ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF)) ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF)) OBJS-ffmpeg += \ + fftools/ffmpeg_demux.o \ fftools/ffmpeg_filter.o \ fftools/ffmpeg_hw.o \ fftools/ffmpeg_mux.o \ diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a377e776c7..0a94b0d05a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -161,8 +161,6 @@ static struct termios oldtty; static int restore_tty; #endif -static void free_input_threads(void); - /* sub2video hack: Convert subtitles to video with alpha to insert them in filter graphs. This is a temporary solution until libavfilter gets real subtitles support. @@ -3636,114 +3634,6 @@ static int check_keyboard_interaction(int64_t cur_time) return 0; } -static void *input_thread(void *arg) -{ - InputFile *f = arg; - AVPacket *pkt = f->pkt, *queue_pkt; - unsigned flags = f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0; - int ret = 0; - - while (1) { - ret = av_read_frame(f->ctx, pkt); - - if (ret == AVERROR(EAGAIN)) { - av_usleep(10000); - continue; - } - if (ret < 0) { - av_thread_message_queue_set_err_recv(f->in_thread_queue, ret); - break; - } - queue_pkt = av_packet_alloc(); - if (!queue_pkt) { - av_packet_unref(pkt); - av_thread_message_queue_set_err_recv(f->in_thread_queue, AVERROR(ENOMEM)); - break; - } - av_packet_move_ref(queue_pkt, pkt); - ret = av_thread_message_queue_send(f->in_thread_queue, &queue_pkt, flags); - if (flags && ret == AVERROR(EAGAIN)) { - flags = 0; - ret = av_thread_message_queue_send(f->in_thread_queue, &queue_pkt, flags); - av_log(f->ctx, AV_LOG_WARNING, - "Thread message queue blocking; consider raising the " - "thread_queue_size option (current value: %d)\n", - f->thread_queue_size); - } - if (ret < 0) { - if (ret != AVERROR_EOF) - av_log(f->ctx, AV_LOG_ERROR, - "Unable to send packet to main thread: %s\n", - av_err2str(ret)); - av_packet_free(&queue_pkt); - av_thread_message_queue_set_err_recv(f->in_thread_queue, ret); - break; - } - } - - return NULL; -} - -static void free_input_thread(int i) -{ - InputFile *f = input_files[i]; - AVPacket *pkt; - - if (!f || !f->in_thread_queue) - return; - av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF); - while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0) - av_packet_free(&pkt); - - pthread_join(f->thread, NULL); - av_thread_message_queue_free(&f->in_thread_queue); -} - -static void free_input_threads(void) -{ - int i; - - for (i = 0; i < nb_input_files; i++) - free_input_thread(i); -} - -static int init_input_thread(int i) -{ - int ret; - InputFile *f = input_files[i]; - - if (f->thread_queue_size <= 0) - f->thread_queue_size = (nb_input_files > 1 ? 8 : 1); - - if (f->ctx->pb ? !f->ctx->pb->seekable : - strcmp(f->ctx->iformat->name, "lavfi")) - f->non_blocking = 1; - ret = av_thread_message_queue_alloc(&f->in_thread_queue, - f->thread_queue_size, sizeof(f->pkt)); - if (ret < 0) - return ret; - - if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) { - av_log(NULL, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret)); - av_thread_message_queue_free(&f->in_thread_queue); - return AVERROR(ret); - } - - return 0; -} - -static int init_input_threads(void) -{ - int i, ret; - - for (i = 0; i < nb_input_files; i++) { - ret = init_input_thread(i); - if (ret < 0) - return ret; - } - return 0; -} - static int get_input_packet(InputFile *f, AVPacket **pkt) { if (f->readrate || f->rate_emu) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index fc082bfbea..81356fd566 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -710,4 +710,9 @@ int64_t of_filesize(OutputFile *of); AVChapter * const * of_get_chapters(OutputFile *of, unsigned int *nb_chapters); +int init_input_threads(void); +int init_input_thread(int i); +void free_input_threads(void); +void free_input_thread(int i); + #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c new file mode 100644 index 0000000000..2629af5950 --- /dev/null +++ b/fftools/ffmpeg_demux.c @@ -0,0 +1,136 @@ +/* + * 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 "ffmpeg.h" + +#include "libavutil/error.h" +#include "libavutil/time.h" +#include "libavutil/thread.h" +#include "libavutil/threadmessage.h" + +#include "libavcodec/packet.h" + +#include "libavformat/avformat.h" + +static void *input_thread(void *arg) +{ + InputFile *f = arg; + AVPacket *pkt = f->pkt, *queue_pkt; + unsigned flags = f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0; + int ret = 0; + + while (1) { + ret = av_read_frame(f->ctx, pkt); + + if (ret == AVERROR(EAGAIN)) { + av_usleep(10000); + continue; + } + if (ret < 0) { + av_thread_message_queue_set_err_recv(f->in_thread_queue, ret); + break; + } + queue_pkt = av_packet_alloc(); + if (!queue_pkt) { + av_packet_unref(pkt); + av_thread_message_queue_set_err_recv(f->in_thread_queue, AVERROR(ENOMEM)); + break; + } + av_packet_move_ref(queue_pkt, pkt); + ret = av_thread_message_queue_send(f->in_thread_queue, &queue_pkt, flags); + if (flags && ret == AVERROR(EAGAIN)) { + flags = 0; + ret = av_thread_message_queue_send(f->in_thread_queue, &queue_pkt, flags); + av_log(f->ctx, AV_LOG_WARNING, + "Thread message queue blocking; consider raising the " + "thread_queue_size option (current value: %d)\n", + f->thread_queue_size); + } + if (ret < 0) { + if (ret != AVERROR_EOF) + av_log(f->ctx, AV_LOG_ERROR, + "Unable to send packet to main thread: %s\n", + av_err2str(ret)); + av_packet_free(&queue_pkt); + av_thread_message_queue_set_err_recv(f->in_thread_queue, ret); + break; + } + } + + return NULL; +} + +void free_input_thread(int i) +{ + InputFile *f = input_files[i]; + AVPacket *pkt; + + if (!f || !f->in_thread_queue) + return; + av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF); + while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0) + av_packet_free(&pkt); + + pthread_join(f->thread, NULL); + av_thread_message_queue_free(&f->in_thread_queue); +} + +void free_input_threads(void) +{ + int i; + + for (i = 0; i < nb_input_files; i++) + free_input_thread(i); +} + +int init_input_thread(int i) +{ + int ret; + InputFile *f = input_files[i]; + + if (f->thread_queue_size <= 0) + f->thread_queue_size = (nb_input_files > 1 ? 8 : 1); + + if (f->ctx->pb ? !f->ctx->pb->seekable : + strcmp(f->ctx->iformat->name, "lavfi")) + f->non_blocking = 1; + ret = av_thread_message_queue_alloc(&f->in_thread_queue, + f->thread_queue_size, sizeof(f->pkt)); + if (ret < 0) + return ret; + + if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) { + av_log(NULL, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret)); + av_thread_message_queue_free(&f->in_thread_queue); + return AVERROR(ret); + } + + return 0; +} + +int init_input_threads(void) +{ + int i, ret; + + for (i = 0; i < nb_input_files; i++) { + ret = init_input_thread(i); + if (ret < 0) + return ret; + } + return 0; +}