From patchwork Fri Jan 27 08:47:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Bouron X-Patchwork-Id: 2333 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp139119vsb; Fri, 27 Jan 2017 00:47:31 -0800 (PST) X-Received: by 10.28.153.139 with SMTP id b133mr1920807wme.93.1485506850989; Fri, 27 Jan 2017 00:47:30 -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 d2si5114003wrc.260.2017.01.27.00.47.30; Fri, 27 Jan 2017 00:47:30 -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 6570068A979; Fri, 27 Jan 2017 10:47:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f44.google.com (mail-wm0-f44.google.com [74.125.82.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 28A8768A375 for ; Fri, 27 Jan 2017 10:47:21 +0200 (EET) Received: by mail-wm0-f44.google.com with SMTP id r144so125234898wme.1 for ; Fri, 27 Jan 2017 00:47:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=3RJ+idBRUAs+koo5YMEErAl8twEh1laVE4+kD6HlOYY=; b=ou+L9n/KFNDPT+z0PFi08sX2O0dDBpsodKVGmUOG6eZAVo8dl/bMTIg5lsdwiol0Ov UoDCTaAVlCNy7mg/yMzsPIcAmvMa4dLHaUVHNEkXZt1ruJsH8v7GLYEQwmeDACx1aaBx m9RhjH5egbIMDYzkruZXT83R9afLgV9nnDqWChIX/6m4kuw1R8X0Bu8KT4nP4+2Anipk QitFNapKZItRA9nimBjtPZUQDRcZT5kFCQBtr169wR+xs4iN6qlrupnpiIk0Y8TU9XxC WiZZrXd8003Uqon0hiZPrspPNDFODNwsz/DloEmqUF+66H6BshqA0tDGGfg439T2X7hL hoCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=3RJ+idBRUAs+koo5YMEErAl8twEh1laVE4+kD6HlOYY=; b=AE+eQQ/v0LGlVpLiSQwS9WXhs1VIym391xZNuq7ewXto6VQeC5vohOlimdBv0iGeq0 dcEGJQOS/AiHzcMl48KQwOZXjvLcoqyzN8A8VQwmNy+ANsEGUrqbeZG7YpQkERkN6TFO Zs9UGXK2x7aeoGhBwOv68jhtQjCxb9QJe9+TgM12zGlcMJdLzyjZp25E6Z0I1Lt3wi12 dLbv5dQbdOwHrGaY/0ki12KaArHEnJt+pc3dUtksFSCvi2vzi4qQC2KFFXVoQ1S9c4RT olqMYVxHXoiwKpGbeXPQNK9/rxUKlXc8sRaChjeKSwGVrl8U0DFKLuO0+9sOEdK0ONHs R05w== X-Gm-Message-State: AIkVDXLpnu83FZKoQgp1toqlyGwxLIcjV+U0/0dPiyHpDxBv/qNqDFyRKBMg3zlX8iVZtg== X-Received: by 10.28.198.65 with SMTP id w62mr2116831wmf.123.1485506841649; Fri, 27 Jan 2017 00:47:21 -0800 (PST) Received: from tsuri (AMontsouris-653-1-150-196.w92-140.abo.wanadoo.fr. [92.140.77.196]) by smtp.gmail.com with ESMTPSA id x135sm2634628wme.23.2017.01.27.00.47.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Jan 2017 00:47:20 -0800 (PST) Date: Fri, 27 Jan 2017 09:47:18 +0100 From: Matthieu Bouron To: FFmpeg development discussions and patches Message-ID: <20170127084718.GC30664@tsuri> References: <20170126164751.27034-1-matthieu.bouron@gmail.com> <20170127013040.GV4698@nb4> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20170127013040.GV4698@nb4> User-Agent: Mutt/1.7.2 (2016-11-26) Subject: Re: [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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" On Fri, Jan 27, 2017 at 02:30:40AM +0100, Michael Niedermayer wrote: > On Thu, Jan 26, 2017 at 05:47:51PM +0100, Matthieu Bouron wrote: > > 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(-) > > this breaks decoding multiple jpeg files > for example tickets//856/nint.jpg New patch attached fixing the issue. [...] From ebef63ebdc75872d52529d5b74b6d05254d4c0fd Mon Sep 17 00:00:00 2001 From: Matthieu Bouron Date: Thu, 26 Jan 2017 15:17:36 +0100 Subject: [PATCH] lavc/mjpegdec: hint next marker position in ff_mjpeg_find_marker 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..347c363da0 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; + buf_ptr = buf_hint; + if (buf_ptr && (buf_end - buf_ptr > 1)) { + v = *buf_ptr++; + v2 = *buf_ptr; + if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { + val = *buf_ptr++; + 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; -- 2.11.0