From patchwork Tue Sep 5 23:57:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilia X-Patchwork-Id: 5000 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.15.201 with SMTP id 70csp4662568jao; Tue, 5 Sep 2017 17:05:23 -0700 (PDT) X-Received: by 10.28.224.195 with SMTP id x186mr346974wmg.167.1504656323623; Tue, 05 Sep 2017 17:05:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504656323; cv=none; d=google.com; s=arc-20160816; b=DZO1UuvLf4xMHX2v3pbOgVd5gGl8RbiNap90y1x7TwwAln/k9D43fo4S2jPYdb80cQ s0Q/arDnfumiUKisHNIWAayklulp6gNq2t22gR/XLx56Ou9nSFtHPJ6XygI9gbSFWPqN 8gmf+Hupmup68zpvgNP88W/TKGsvFQtXqTUISm1znM5D0VBHz5IJhW2dktGO5c55uI1j +H28LoAdORS/+rcWpuTfVYsXWFF6UMJGr5tbA/n73CFW406GNua1024gGwCl9XZ7XY1l sMCccdQ/cY7AyrZf7r65EymGYMyqBf/L1XNo8QpUUhbLlWWXNp2e14eYxcYxVyvREpAj 2yyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=bGwW0cLUKcRt4gR2cq29ZcNusc8/3h/HwOnULINLp24=; b=PdgHFjhzCwvVi11vQ3eMqNS4RqTJcEtMKRGFIexdv7Elf0DUfuC6voK4ko2WRArGTV RqC0Ek9VBxMO9LxifJ64cOKlgPE0W4tiXLCeWgjsjZCoIWri77plQ+vm9VQJrBTmbG17 WqxH+MfvIg+wkTiRwhqf7nNY/z6ynjzLsR7q/c/rBfOCcwiWerV/SLA6hHApr3n3q/9l oonY1trXuqL3PUBczSp59ifRy2+p4iEVzCzqZg5fnfpNUPp+TSr0F8WHrGrjzKQp7y3f tL6318gMuOx46Pbv59DizQvPh7XOLKUcIcT4uDQQVDB21FUs4Sm9LKk1FVifeHZz/88u qhog== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=kcJi0+S2; 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=NONE 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 y40si1237812wrd.465.2017.09.05.17.05.23; Tue, 05 Sep 2017 17:05:23 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=kcJi0+S2; 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=NONE 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 E76DB68A49B; Wed, 6 Sep 2017 03:05:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf0-f66.google.com (mail-lf0-f66.google.com [209.85.215.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C4B2268A474 for ; Wed, 6 Sep 2017 03:05:12 +0300 (EEST) Received: by mail-lf0-f66.google.com with SMTP id q132so1666266lfe.4 for ; Tue, 05 Sep 2017 17:05:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=RfQqzZqRYxOP75naYx9+EgSiQUHNCPSvdtR0NkNpBP0=; b=kcJi0+S2vmQNVJwohGm0YFbIth6UQGDXx0Zz/Ql+kSPCNjzj9f1MDFU/na3+80mWL2 QA3U5nJniVWxrgYcRb8WhI4cAyXj9K/tH4/fRmwBCgTdKozlUY87iC3e7+hpvGgLDeq8 1cIGI6cZcAxPVTDjaLrCJ4Cw1G4sjUfJ8s3VeiL8WOcKbGsdW85q7UbKZUYoMlqFsV3Q TqlcUhS7mjr2g006UV/ucJupyi9k1EANywxtLUYegVvDxJBd4y5NQ3HvcByy+ECd6TVx Xm6klkaF+cNTHwAl5Bdg51Ez41LtziDcGIMxnYLt6ZmYDZR3Urjo40YMnODHJGTIJpBd 5lrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=RfQqzZqRYxOP75naYx9+EgSiQUHNCPSvdtR0NkNpBP0=; b=X4FAB6FvLLTKGMBq3UFvepJI6x++PeXuqYQDT8mGqjhTtuXHOGbn0LuSD1PJVKi3PF uGprfl70BIggEQ37w3gcFb0cd2E6VPcZWAs+EgQ8MmFLuYA6l7mnToGVzXmK1OtISIYN DtOsEvBGFz4siCzo2kqmxhj92A8c0MmVw1vKK3TBEl3FimsXItnBnftEjm1kuyG8nUPK reJLHrPyfuLrwbNX4uyYMn9vbprZE+y4RqbbVvzs7V5xivjMb57eS/F5Ir3ydJ/DZFWW LC+ccdDS8WcwR3/8wklMbVaBWABJMnLuB+nsj2UedLcrQ4D+mE173DTcbBR44h1YJWJd g5sA== X-Gm-Message-State: AHPjjUiEkFK92MhpgwHYgjiEsVyicGaiRCqRs5a50nh9e96aRlkxYoLk R+cckqi5olRTU/S4 X-Google-Smtp-Source: ADKCNb7MswNWS01KxkdgK92GQa8W1KgVxuG+NTVDKYLQAsmXoXGOvrTWDwm5FbDk14seix0Yr5SUAw== X-Received: by 10.25.15.98 with SMTP id e95mr204373lfi.141.1504655885222; Tue, 05 Sep 2017 16:58:05 -0700 (PDT) Received: from localhost.localdomain ([95.191.192.76]) by smtp.gmail.com with ESMTPSA id l187sm335481lfg.25.2017.09.05.16.58.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Sep 2017 16:58:04 -0700 (PDT) From: Ilia Valiakhmetov To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Sep 2017 06:57:25 +0700 Message-Id: <20170905235725.1240-1-zakne0ne@gmail.com> X-Mailer: git-send-email 2.8.3 Subject: [FFmpeg-devel] [PATCHv2 1/2] avcodec: add execute3() api to utilize the main function of avpriv_slicethread_create(). X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: Ilia Valiakhmetov MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Ilia Valiakhmetov --- libavcodec/avcodec.h | 7 ++++++- libavcodec/options.c | 1 + libavcodec/pthread_slice.c | 26 ++++++++++++++++++++++++-- libavcodec/utils.c | 14 ++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 6555550..712f40c 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -1089,6 +1089,10 @@ typedef struct RcOverride{ */ #define AV_CODEC_CAP_AVOID_PROBING (1 << 17) /** + * Codec initializes slice-based threading with a main function + */ +#define AV_CODEC_SLICE_THREAD_HAS_MF (1 << 18) +/** * Codec is intra only. */ #define AV_CODEC_CAP_INTRA_ONLY 0x40000000 @@ -3233,7 +3237,7 @@ typedef struct AVCodecContext { * - decoding: Set by libavcodec, user can override. */ int (*execute2)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count); - + int (*execute3)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), int (*m_func)(struct AVCodecContext *c3), void *arg2, int *ret, int count); /** * noise vs. sse weight for the nsse comparison function * - encoding: Set by user. @@ -5774,6 +5778,7 @@ const char *avcodec_profile_name(enum AVCodecID codec_id, int profile); int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size); int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count); +int avcodec_default_execute3(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), int (*m_func)(struct AVCodecContext *c3), void *arg, int *ret, int count); //FIXME func typedef /** diff --git a/libavcodec/options.c b/libavcodec/options.c index 82e1217..6d63bdb 100644 --- a/libavcodec/options.c +++ b/libavcodec/options.c @@ -117,6 +117,7 @@ static int init_context_defaults(AVCodecContext *s, const AVCodec *codec) s->get_format = avcodec_default_get_format; s->execute = avcodec_default_execute; s->execute2 = avcodec_default_execute2; + s->execute3 = avcodec_default_execute3; s->sample_aspect_ratio = (AVRational){0,1}; s->pix_fmt = AV_PIX_FMT_NONE; s->sw_pix_fmt = AV_PIX_FMT_NONE; diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c index c781d35..3aff816 100644 --- a/libavcodec/pthread_slice.c +++ b/libavcodec/pthread_slice.c @@ -38,11 +38,13 @@ typedef int (action_func)(AVCodecContext *c, void *arg); typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr); +typedef int (main_func)(AVCodecContext *c); typedef struct SliceThreadContext { AVSliceThread *thread; action_func *func; action_func2 *func2; + main_func *m_func; void *args; int *rets; int job_size; @@ -54,6 +56,12 @@ typedef struct SliceThreadContext { pthread_mutex_t *progress_mutex; } SliceThreadContext; +static void main_function(void *priv) { + AVCodecContext *avctx = priv; + SliceThreadContext *c = avctx->internal->thread_ctx; + c->m_func(avctx); +} + static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads) { AVCodecContext *avctx = priv; @@ -99,7 +107,8 @@ static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, i c->func = func; c->rets = ret; - avpriv_slicethread_execute(c->thread, job_count, 0); + avpriv_slicethread_execute(c->thread, job_count, !!c->m_func); + return 0; } @@ -110,10 +119,20 @@ static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg return thread_execute(avctx, NULL, arg, ret, job_count, 0); } +static int thread_execute3(AVCodecContext *avctx, action_func2* func2, main_func* m_func, void *arg, int *ret, int job_count) +{ + SliceThreadContext *c = avctx->internal->thread_ctx; + c->func2 = func2; + c->m_func = m_func; + return thread_execute(avctx, NULL, arg, ret, job_count, 0); +} + + int ff_slice_thread_init(AVCodecContext *avctx) { SliceThreadContext *c; int thread_count = avctx->thread_count; + static void (*m_f)(void *); #if HAVE_W32THREADS w32thread_init(); @@ -142,7 +161,9 @@ int ff_slice_thread_init(AVCodecContext *avctx) } avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c)); - if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, NULL, thread_count)) <= 1) { + m_f = avctx->codec->capabilities & AV_CODEC_SLICE_THREAD_HAS_MF ? &main_function : NULL; + + if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, m_f, thread_count)) <= 1) { if (c) avpriv_slicethread_free(&c->thread); av_freep(&avctx->internal->thread_ctx); @@ -154,6 +175,7 @@ int ff_slice_thread_init(AVCodecContext *avctx) avctx->execute = thread_execute; avctx->execute2 = thread_execute2; + avctx->execute3 = thread_execute3; return 0; } diff --git a/libavcodec/utils.c b/libavcodec/utils.c index baf4992..150e145 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -558,6 +558,20 @@ int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, return 0; } +int avcodec_default_execute3(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), int (*m_func)(struct AVCodecContext *c3), void *arg, int *ret, int count) +{ + int i; + + for (i = 0; i < count; i++) { + int r = func(c, arg, i, 0); + if (ret) + ret[i] = r; + } + m_func(c); + emms_c(); + return 0; +} + enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc) {