From patchwork Mon Dec 24 00:14:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 11534 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 94B3D44DDB6 for ; Mon, 24 Dec 2018 02:18:52 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4D11868ABD8; Mon, 24 Dec 2018 02:18:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-dmta-pe08-3.mx.upcmail.net (vie01a-dmta-pe08-3.mx.upcmail.net [84.116.36.22]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2E9ED68AB7A for ; Mon, 24 Dec 2018 02:18:43 +0200 (EET) Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe08.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1gbDxH-0005Wu-0L for ffmpeg-devel@ffmpeg.org; Mon, 24 Dec 2018 01:18:47 +0100 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id bDtrgCCrS2WSsbDtrgv97X; Mon, 24 Dec 2018 01:15:15 +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=xE7fejmD9a1CE8vIgZEA:9 a=pHzHmUro8NiASowvMSCR:22 a=nt3jZW36AmriUCFCBwmW:22 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 24 Dec 2018 01:14:50 +0100 Message-Id: <20181224001451.8853-2-michael@niedermayer.cc> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181224001451.8853-1-michael@niedermayer.cc> References: <20181224001451.8853-1-michael@niedermayer.cc> MIME-Version: 1.0 X-CMAE-Envelope: MS4wfE2gmiQyQ/9xJRB2YP263gCeeWvLEGKXIVFgwn35JoOgSGWKip3I/MNeQOu4KTM+tGZP92/ID4JuK9YUwTQaSaMrcmpj3I5v6QoOsXNm6ySp/EYTWQF9 XsjwTTZb0Qz897M2LjCzmf2dJRoRL2xOM6hdL8n4qXtlix8TSZHtipUU Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/lagarith: Optimize case with singleton probability distribution 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" Fixes: Timeout Fixes: 10554/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_LAGARITH_fuzzer-5739938067251200 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/lagarith.c | 36 ++++++++++++++++++++++++++++++++++++ libavcodec/lagarithrac.h | 1 + 2 files changed, 37 insertions(+) diff --git a/libavcodec/lagarith.c b/libavcodec/lagarith.c index 59169be5de..0b222e1acd 100644 --- a/libavcodec/lagarith.c +++ b/libavcodec/lagarith.c @@ -175,6 +175,7 @@ static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb) if (nnz == 1 && (show_bits_long(gb, 32) & 0xFFFFFF)) { return AVERROR_INVALIDDATA; } + rac->nnz = nnz; /* Scale probabilities so cumulative probability is an even power of 2. */ scale_factor = av_log2(cumul_prob); @@ -332,6 +333,41 @@ static int lag_decode_line(LagarithContext *l, lag_rac *rac, if (!esc_count) esc_count = -1; + if (rac->nnz == 1) { + int v = -1;; +handle_zeros1: + if (l->zeros_rem) { + int count = FFMIN(l->zeros_rem, width - i); + memset(dst + i, 0, count); + i += count; + l->zeros_rem -= count; + } + + while (i < width) { + if (v < 0) + v = lag_get_rac(rac); + + dst[i] =v; + ret++; + + if (v) + l->zeros = 0; + else + l->zeros++; + + i++; + if (l->zeros == esc_count) { + ret++; + + l->zeros = 0; + + l->zeros_rem = lag_calc_zero_run(v); + goto handle_zeros1; + } + } + return ret; + } + /* Output any zeros remaining from the previous run */ handle_zeros: if (l->zeros_rem) { diff --git a/libavcodec/lagarithrac.h b/libavcodec/lagarithrac.h index ee836d01db..9f37f3939c 100644 --- a/libavcodec/lagarithrac.h +++ b/libavcodec/lagarithrac.h @@ -47,6 +47,7 @@ typedef struct lag_rac { const uint8_t *bytestream; /**< Current position in input bytestream. */ const uint8_t *bytestream_end; /**< End position of input bytestream. */ + int nnz; int overread; #define MAX_OVERREAD 4