From patchwork Mon Mar 27 14:09:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ronald S. Bultje" X-Patchwork-Id: 3127 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp187108vss; Mon, 27 Mar 2017 07:16:33 -0700 (PDT) X-Received: by 10.223.139.83 with SMTP id v19mr19662191wra.142.1490624193580; Mon, 27 Mar 2017 07:16:33 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h14si936679wrb.231.2017.03.27.07.16.33; Mon, 27 Mar 2017 07:16:33 -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; 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 3B2CE68834C; Mon, 27 Mar 2017 17:16:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 426B06808CA for ; Mon, 27 Mar 2017 17:16:02 +0300 (EEST) Received: by mail-qk0-f195.google.com with SMTP id f11so5071039qkb.2 for ; Mon, 27 Mar 2017 07:16:24 -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=qSG+BGF4HzHbrO/O7AvpipNpgKq1x4/wyU1bYOG/qYc=; b=T8wHlyB+jx6isJGktPleviwpVkzuJ4PN2JUPJshnM0Tn3+8UCXzaLH0sr1CSjoEsu0 Zi0IFrDzen87/ISbZWuNz4gTsdyuWfV8pt5esCpEkvJTsb6KqSMW+S3GJCYhbZEH+1Ht q0/ewCqrsB1EHk1w3nHDw7snqLdkiDWpsH1Yr6cBFIcGKC9LlpqsbHfwyhfy6ESCQ0tC 9My/hlp6CLE+khhIG3T0fDqskFwi9MHVFZg9f483B9tRw+fFs0b+MmjSs8hF/8D5tZ88 qiWDZQpSwtNhxtRbv58uXVZOFNy6AhTiTio7irFUbKoeX8/b8uoVsYdDZyfi2dSwBtsj wnUg== 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=qSG+BGF4HzHbrO/O7AvpipNpgKq1x4/wyU1bYOG/qYc=; b=gnZPm9DEtPENpOwiV+URAgTwMe1M7gmdcHNRGB6nfvaxnNNGHrO3XmgPzQdXILa1dk OQa3KN2ct/E7Ljqi1B36OHxXIn7xR4VTCkB+XODzOpszYS/epxb/st25ByP5/TnqLm+6 zOMkceR6G5tYybbtBUvGvlHq1UywaUE5RO/12769yp/9jWSi/UVcS208RZYp7N6pwpri 475lw5i8+rD94fv3i0GqH0lLHZ5qBAss4Wg3RkaE8ylirVsMKJARhgS+F2zaEUEolZe1 57WGNiOFRVlZO9BzP5ENn3HHhPtL7LSTIfuj9DGF54tzQCfNeFk9Vi37DvxP6Pp0QgVW dLJA== X-Gm-Message-State: AFeK/H36aQWwv5dpRmiYD0dbn32JqGzTeEGiAHgDaHg1Uay+0sJ0x55lHOYUoLyrtoHSSw== X-Received: by 10.55.192.206 with SMTP id v75mr2763080qkv.178.1490623749064; Mon, 27 Mar 2017 07:09:09 -0700 (PDT) Received: from localhost.localdomain ([65.206.95.146]) by smtp.gmail.com with ESMTPSA id v26sm429947qtc.13.2017.03.27.07.09.08 (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 27 Mar 2017 07:09:08 -0700 (PDT) From: "Ronald S. Bultje" To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Mar 2017 10:09:05 -0400 Message-Id: <1490623745-42356-1-git-send-email-rsbultje@gmail.com> X-Mailer: git-send-email 2.8.1 Subject: [FFmpeg-devel] [PATCH] dirac: make initialization of arithmetic coder tables threadsafe. 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: "Ronald S. Bultje" MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavcodec/dirac_arith.c | 17 +++++++++++------ libavcodec/dirac_arith.h | 1 + libavcodec/diracdec.c | 9 ++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/libavcodec/dirac_arith.c b/libavcodec/dirac_arith.c index bf91392..49c0909 100644 --- a/libavcodec/dirac_arith.c +++ b/libavcodec/dirac_arith.c @@ -83,9 +83,19 @@ const uint8_t ff_dirac_next_ctx[DIRAC_CTX_COUNT] = { int16_t ff_dirac_prob_branchless[256][2]; -void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length) +av_cold void ff_dirac_init_arith_tables(void) { int i; + + for (i = 0; i < 256; i++) { + ff_dirac_prob_branchless[i][0] = ff_dirac_prob[255-i]; + ff_dirac_prob_branchless[i][1] = -ff_dirac_prob[i]; + } +} + +void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length) +{ + int i, ret; align_get_bits(gb); length = FFMIN(length, get_bits_left(gb)/8); @@ -106,11 +116,6 @@ void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length) c->counter = -16; c->range = 0xffff; - for (i = 0; i < 256; i++) { - ff_dirac_prob_branchless[i][0] = ff_dirac_prob[255-i]; - ff_dirac_prob_branchless[i][1] = -ff_dirac_prob[i]; - } - for (i = 0; i < DIRAC_CTX_COUNT; i++) c->contexts[i] = 0x8000; } diff --git a/libavcodec/dirac_arith.h b/libavcodec/dirac_arith.h index 003430a..24a7ca3 100644 --- a/libavcodec/dirac_arith.h +++ b/libavcodec/dirac_arith.h @@ -190,6 +190,7 @@ static inline int dirac_get_arith_int(DiracArith *c, int follow_ctx, int data_ct return ret; } +void ff_dirac_init_arith_tables(void); void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length); #endif /* AVCODEC_DIRAC_ARITH_H */ diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c index e0604af..202ae94 100644 --- a/libavcodec/diracdec.c +++ b/libavcodec/diracdec.c @@ -26,6 +26,7 @@ * @author Marco Gerards , David Conrad, Jordi Ortiz */ +#include "libavutil/thread.h" #include "avcodec.h" #include "get_bits.h" #include "bytestream.h" @@ -379,10 +380,12 @@ static void free_sequence_buffers(DiracContext *s) av_freep(&s->mcscratch); } +static AVOnce dirac_arith_init = AV_ONCE_INIT; + static av_cold int dirac_decode_init(AVCodecContext *avctx) { DiracContext *s = avctx->priv_data; - int i; + int i, ret; s->avctx = avctx; s->frame_number = -1; @@ -404,6 +407,9 @@ static av_cold int dirac_decode_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } } + ret = ff_thread_once(&dirac_arith_init, ff_dirac_init_arith_tables); + if (ret != 0) + return AVERROR_UNKNOWN; return 0; } @@ -2299,5 +2305,6 @@ AVCodec ff_dirac_decoder = { .close = dirac_decode_end, .decode = dirac_decode_frame, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, .flush = dirac_decode_flush, };