From patchwork Thu Jan 26 16:47:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Bouron X-Patchwork-Id: 2329 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp273240vsb; Thu, 26 Jan 2017 08:54:19 -0800 (PST) X-Received: by 10.28.165.199 with SMTP id o190mr28724826wme.113.1485449659003; Thu, 26 Jan 2017 08:54:19 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o15si2655146wrb.191.2017.01.26.08.54.18; Thu, 26 Jan 2017 08:54:18 -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; 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 1DFF668A8CC; Thu, 26 Jan 2017 18:54:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com [74.125.82.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0E00B68A840 for ; Thu, 26 Jan 2017 18:54:09 +0200 (EET) Received: by mail-wm0-f65.google.com with SMTP id c85so51507598wmi.1 for ; Thu, 26 Jan 2017 08:54:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=B0/JKqURzWEWkzV8/5S5baAKq7OLLi/wb398/ondQwU=; b=m02icuMw9lPf88BeDxBlQEZC3ZZRmbsu7cRKkfUg8g3RbRqCstRfmgWDHMT3W3ykL3 4gQIwsRcI++fSGWjizIGjnB5t9BNnyGkI3RdLivNHBl6GztN8anhyw+Mp19H8WYmCFOj mdskE2/Jqx7q90Xdzg3dGgb7dTknrr4yov8DDltCZeSDZctizBCM2iPBRaSsCY6OuAXw rDgRroX5cuRYRBu77fAGK3tMzxLVvOp6M79D2k456Lvo0l1grhcaccBPBrc7/6kThT7N SY/tNW+6i518kRo1kPq75Pda5Ci5Xrt4U+pK54QwBxenO8g/Gff0RvOz8hg/wNC+je37 J/gQ== 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=B0/JKqURzWEWkzV8/5S5baAKq7OLLi/wb398/ondQwU=; b=pPvi67DpSDnOo8SCEsbzTSQmD4+jSgjUxFJlmrVxmJQRDZvT1vRxUgYZ/Cq4vPtzRA aoPtNP3oh/78fXXsfo518ao80sLrytUlaWvgmg7Si4mD+wEtOB5OPHmyR/JSyFp4Bpx1 bQD4TzWPI0+YxgZOUmCUxdQXHiFzWeNAEtpMHuEtaX54i1A/2yppsPYaPuT2eCsWCqQp mDZfcMZ8IOEDREX2xOiAINBH3I7S7lkZVaSl36uY0WUJFJ6+AWLcF7gsiLd5tNUt3yi6 IbP+6o5AU1tyoMZq61JZpAnZWhfX8BOHl1isFbrpvOt+cELJBT+Gl/FUjeSx10edjcEM nmmQ== X-Gm-Message-State: AIkVDXJjTXvcjaxwiZwVBLgZTobLUTWsM4qjwJvRB4l8wlnlN/rSA19irRA4yo7wc+raWg== X-Received: by 10.28.47.2 with SMTP id v2mr26425512wmv.138.1485449279455; Thu, 26 Jan 2017 08:47:59 -0800 (PST) Received: from localhost.localdomain (AMontsouris-653-1-150-196.w92-140.abo.wanadoo.fr. [92.140.77.196]) by smtp.gmail.com with ESMTPSA id c133sm4564963wmd.13.2017.01.26.08.47.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Jan 2017 08:47:58 -0800 (PST) From: Matthieu Bouron To: ffmpeg-devel@ffmpeg.org Date: Thu, 26 Jan 2017 17:47:51 +0100 Message-Id: <20170126164751.27034-1-matthieu.bouron@gmail.com> X-Mailer: git-send-email 2.11.0 Subject: [FFmpeg-devel] [PATCH] lavc/mjpegdec: hint next marker position in ff_mjpeg_find_marker 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: Matthieu Bouron MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Speeds up next marker search when a SOS marker is found. ff_mjpeg_find_marker goes from 54% to 30% under perf record ffmpeg -i sample_2992x4000.jpg --- libavcodec/mjpegdec.c | 31 +++++++++++++++++++++++++------ libavcodec/mjpegdec.h | 2 +- libavcodec/mxpegdec.c | 5 +++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c index 7d17e3dfcb..29f40cb4e6 100644 --- a/libavcodec/mjpegdec.c +++ b/libavcodec/mjpegdec.c @@ -1921,13 +1921,23 @@ static int mjpeg_decode_com(MJpegDecodeContext *s) /* return the 8 bit start code value and update the search state. Return -1 if no start code found */ -static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end) +static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_hint, const uint8_t *buf_end) { const uint8_t *buf_ptr; unsigned int v, v2; int val; int skipped = 0; + if (buf_hint && (buf_end - buf_hint > 1)) { + v = *buf_hint++; + v2 = *buf_hint; + if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_hint < buf_end) { + buf_ptr = buf_hint++; + val = v2; + goto found; + } + } + buf_ptr = *pbuf_ptr; while (buf_end - buf_ptr > 1) { v = *buf_ptr++; @@ -1947,12 +1957,15 @@ found: } int ff_mjpeg_find_marker(MJpegDecodeContext *s, - const uint8_t **buf_ptr, const uint8_t *buf_end, + const uint8_t **buf_ptr, + const uint8_t **buf_hint, + const uint8_t *buf_end, const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size) { int start_code; - start_code = find_marker(buf_ptr, buf_end); + start_code = find_marker(buf_ptr, *buf_hint, buf_end); + *buf_hint = NULL; av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr); if (!s->buffer) @@ -1963,6 +1976,7 @@ int ff_mjpeg_find_marker(MJpegDecodeContext *s, const uint8_t *src = *buf_ptr; const uint8_t *ptr = src; uint8_t *dst = s->buffer; + const uint8_t *next_marker = NULL; #define copy_data_segment(skip) do { \ ptrdiff_t length = (ptr - src) - (skip); \ @@ -1999,8 +2013,10 @@ int ff_mjpeg_find_marker(MJpegDecodeContext *s, if (x < 0xd0 || x > 0xd7) { copy_data_segment(1); - if (x) + if (x) { + next_marker = ptr - 2; break; + } } } } @@ -2009,6 +2025,8 @@ int ff_mjpeg_find_marker(MJpegDecodeContext *s, } #undef copy_data_segment + if (next_marker) + *buf_hint = next_marker; *unescaped_buf_ptr = s->buffer; *unescaped_buf_size = dst - s->buffer; memset(s->buffer + *unescaped_buf_size, 0, @@ -2073,7 +2091,7 @@ int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; MJpegDecodeContext *s = avctx->priv_data; - const uint8_t *buf_end, *buf_ptr; + const uint8_t *buf_end, *buf_hint, *buf_ptr; const uint8_t *unescaped_buf_ptr; int hshift, vshift; int unescaped_buf_size; @@ -2087,10 +2105,11 @@ int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, s->adobe_transform = -1; buf_ptr = buf; + buf_hint = NULL; buf_end = buf + buf_size; while (buf_ptr < buf_end) { /* find start next marker */ - start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end, + start_code = ff_mjpeg_find_marker(s, &buf_ptr, &buf_hint, buf_end, &unescaped_buf_ptr, &unescaped_buf_size); /* EOF */ diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h index fb811294a1..c9d289187b 100644 --- a/libavcodec/mjpegdec.h +++ b/libavcodec/mjpegdec.h @@ -144,7 +144,7 @@ int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,int mb_bitmask_size, const AVFrame *reference); int ff_mjpeg_find_marker(MJpegDecodeContext *s, - const uint8_t **buf_ptr, const uint8_t *buf_end, + const uint8_t **buf_ptr, const uint8_t **buf_hint, const uint8_t *buf_end, const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size); #endif /* AVCODEC_MJPEGDEC_H */ diff --git a/libavcodec/mxpegdec.c b/libavcodec/mxpegdec.c index 2e3ebe6e70..28a479a3b5 100644 --- a/libavcodec/mxpegdec.c +++ b/libavcodec/mxpegdec.c @@ -189,18 +189,19 @@ static int mxpeg_decode_frame(AVCodecContext *avctx, int buf_size = avpkt->size; MXpegDecodeContext *s = avctx->priv_data; MJpegDecodeContext *jpg = &s->jpg; - const uint8_t *buf_end, *buf_ptr; + const uint8_t *buf_end, *buf_hint, *buf_ptr; const uint8_t *unescaped_buf_ptr; int unescaped_buf_size; int start_code; int ret; buf_ptr = buf; + buf_hint = NULL; buf_end = buf + buf_size; jpg->got_picture = 0; s->got_mxm_bitmask = 0; while (buf_ptr < buf_end) { - start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end, + start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, &buf_hint, buf_end, &unescaped_buf_ptr, &unescaped_buf_size); if (start_code < 0) goto the_end;