From patchwork Thu Dec 27 10:40:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 11564 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 4A12044C2DC for ; Thu, 27 Dec 2018 12:42:09 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EF44B68AC45; Thu, 27 Dec 2018 12:42:05 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe01-2.mx.upcmail.net (vie01a-qmta-pe01-2.mx.upcmail.net [62.179.121.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9E4E368AA1F for ; Thu, 27 Dec 2018 12:41:59 +0200 (EET) Received: from [172.31.218.51] (helo=vie01a-dmta-pe07-3.mx.upcmail.net) by vie01a-pqmta-pe01.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1gcT76-00089N-W5 for ffmpeg-devel@ffmpeg.org; Thu, 27 Dec 2018 11:42:05 +0100 Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe07.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1gcT71-0002hU-0P for ffmpeg-devel@ffmpeg.org; Thu, 27 Dec 2018 11:41:59 +0100 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id cT63gjjw72WSscT63gQLv6; Thu, 27 Dec 2018 11:40:59 +0100 X-Env-Mailfrom: michael@niedermayer.cc X-Env-Rcptto: ffmpeg-devel@ffmpeg.org X-SourceIP: 213.47.41.20 X-CNFS-Analysis: v=2.3 cv=E7kcWpVl c=1 sm=1 tr=0 a=I1eytVlZLDX1BM2VTtTtSw==:117 a=I1eytVlZLDX1BM2VTtTtSw==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=GEAsPZ9sns4A:10 a=ZZnuYtJkoWoA:10 a=nZOtpAppAAAA:20 a=pnGqd7G2VgqaZ4NMCUcA:9 a=pHzHmUro8NiASowvMSCR:22 a=nt3jZW36AmriUCFCBwmW:22 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 27 Dec 2018 11:40:56 +0100 Message-Id: <20181227104058.22799-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-CMAE-Envelope: MS4wfFpDuy1Gevig9m49/aXSxBeUDUlmTUqcPbO8pdUWGj6C4as2oof/8ws36CimFMuMFng8zDKxsnSYx5YRhI4FXu9q4splAKSHjSxY7Hfw4A40kbzexRZ5 7bYWSRB2tNA0YgHP711PU4jeyz+gjkerGJMNiZjKJ/RRrZAcprf8oLEg Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/scpr: Use local variables in decode_run_i() 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" This improves the speed of decode_run_i() Before: clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_SCPR_fuzzer-5656821117747200 in 17420 ms After: clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_SCPR_fuzzer-5656821117747200 in 14018 ms Improves: 11270/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_SCPR_fuzzer-5656821117747200 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/scpr.h | 125 +++++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 58 deletions(-) diff --git a/libavcodec/scpr.h b/libavcodec/scpr.h index a013d7e600..bb9b4d6a9b 100644 --- a/libavcodec/scpr.h +++ b/libavcodec/scpr.h @@ -78,58 +78,62 @@ typedef struct SCPRContext { } SCPRContext; static int decode_run_i(AVCodecContext *avctx, uint32_t ptype, int run, - int *x, int *y, uint32_t clr, uint32_t *dst, - int linesize, uint32_t *lx, uint32_t *ly, + int *px, int *py, uint32_t clr, uint32_t *dst, + int linesize, uint32_t *plx, uint32_t *ply, uint32_t backstep, int off, int *cx, int *cx1) { uint32_t r, g, b; int z; + int x = *px, + y = *py; + uint32_t lx = *plx, + ly = *ply; switch (ptype) { case 0: while (run-- > 0) { - if (*y >= avctx->height) + if (y >= avctx->height) return AVERROR_INVALIDDATA; - dst[*y * linesize + *x] = clr; - *lx = *x; - *ly = *y; - (*x)++; - if (*x >= avctx->width) { - *x = 0; - (*y)++; + dst[y * linesize + x] = clr; + lx = x; + ly = y; + (x)++; + if (x >= avctx->width) { + x = 0; + (y)++; } } break; case 1: while (run-- > 0) { - if (*y >= avctx->height) + if (y >= avctx->height) return AVERROR_INVALIDDATA; - dst[*y * linesize + *x] = dst[*ly * linesize + *lx]; - *lx = *x; - *ly = *y; - (*x)++; - if (*x >= avctx->width) { - *x = 0; - (*y)++; + dst[y * linesize + x] = dst[ly * linesize + lx]; + lx = x; + ly = y; + (x)++; + if (x >= avctx->width) { + x = 0; + (y)++; } } - clr = dst[*ly * linesize + *lx]; + clr = dst[ly * linesize + lx]; break; case 2: while (run-- > 0) { - if (*y < 1 || *y >= avctx->height) + if (y < 1 || y >= avctx->height) return AVERROR_INVALIDDATA; - clr = dst[*y * linesize + *x + off + 1]; - dst[*y * linesize + *x] = clr; - *lx = *x; - *ly = *y; - (*x)++; - if (*x >= avctx->width) { - *x = 0; - (*y)++; + clr = dst[y * linesize + x + off + 1]; + dst[y * linesize + x] = clr; + lx = x; + ly = y; + (x)++; + if (x >= avctx->width) { + x = 0; + (y)++; } } break; @@ -137,61 +141,66 @@ static int decode_run_i(AVCodecContext *avctx, uint32_t ptype, int run, while (run-- > 0) { uint8_t *odst = (uint8_t *)dst; - if (*y < 1 || *y >= avctx->height || - (*y == 1 && *x == 0)) + if (y < 1 || y >= avctx->height || + (y == 1 && x == 0)) return AVERROR_INVALIDDATA; - if (*x == 0) { + if (x == 0) { z = backstep; } else { z = 0; } - r = odst[(*ly * linesize + *lx) * 4] + - odst[((*y * linesize + *x) + off) * 4 + 4] - - odst[((*y * linesize + *x) + off - z) * 4]; - g = odst[(*ly * linesize + *lx) * 4 + 1] + - odst[((*y * linesize + *x) + off) * 4 + 5] - - odst[((*y * linesize + *x) + off - z) * 4 + 1]; - b = odst[(*ly * linesize + *lx) * 4 + 2] + - odst[((*y * linesize + *x) + off) * 4 + 6] - - odst[((*y * linesize + *x) + off - z) * 4 + 2]; + r = odst[(ly * linesize + lx) * 4] + + odst[((y * linesize + x) + off) * 4 + 4] - + odst[((y * linesize + x) + off - z) * 4]; + g = odst[(ly * linesize + lx) * 4 + 1] + + odst[((y * linesize + x) + off) * 4 + 5] - + odst[((y * linesize + x) + off - z) * 4 + 1]; + b = odst[(ly * linesize + lx) * 4 + 2] + + odst[((y * linesize + x) + off) * 4 + 6] - + odst[((y * linesize + x) + off - z) * 4 + 2]; clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF); - dst[*y * linesize + *x] = clr; - *lx = *x; - *ly = *y; - (*x)++; - if (*x >= avctx->width) { - *x = 0; - (*y)++; + dst[y * linesize + x] = clr; + lx = x; + ly = y; + (x)++; + if (x >= avctx->width) { + x = 0; + (y)++; } } break; case 5: while (run-- > 0) { - if (*y < 1 || *y >= avctx->height || - (*y == 1 && *x == 0)) + if (y < 1 || y >= avctx->height || + (y == 1 && x == 0)) return AVERROR_INVALIDDATA; - if (*x == 0) { + if (x == 0) { z = backstep; } else { z = 0; } - clr = dst[*y * linesize + *x + off - z]; - dst[*y * linesize + *x] = clr; - *lx = *x; - *ly = *y; - (*x)++; - if (*x >= avctx->width) { - *x = 0; - (*y)++; + clr = dst[y * linesize + x + off - z]; + dst[y * linesize + x] = clr; + lx = x; + ly = y; + (x)++; + if (x >= avctx->width) { + x = 0; + (y)++; } } break; } + *px = x; + *py = y; + *plx= lx; + *ply= ly; + if (avctx->bits_per_coded_sample == 16) { *cx1 = (clr & 0x3F00) >> 2; *cx = (clr & 0x3FFFFF) >> 16;