From patchwork Sun Jul 1 02:08:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 9561 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:104:0:0:0:0:0 with SMTP id c4-v6csp2415957jad; Sat, 30 Jun 2018 19:09:50 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd+hnx0uf54n+ty69f9ALPaBK4Qx6QqZ3ASG5EHiMFlKV12TBXDH1FWgI8la6xhn5WtMZ5v X-Received: by 2002:a1c:31c2:: with SMTP id x185-v6mr4992422wmx.1.1530410990167; Sat, 30 Jun 2018 19:09:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530410990; cv=none; d=google.com; s=arc-20160816; b=0I2R56ZeFMNotWbsWh5HeE8oFOlNwRWnYHXvWXyDzXWMf9BsgPbs3oDwHPHqufPqOe +ShefzlUb0+HClLW8ANuPe4D/zEin166cohUk7RQ9vdRbmm5uRfuh4ERzQNsQyWzBn+i z+r9i2phx0dzX+YsxQH3C2iahEmlNqihEZShYu8THmJAByAi5J4pecO9xXU43ThxLwi4 tk8AD42UC+Y9ulL65nzj6WajXE+PH3z95Tz9u2x2kxE2JDK4cw0fRniSeFYbhwGpxqwS C24l+JIq8YHKUHhIq1bxLZipQwjB2aw698Zdc5MUbfKD9oEMjOhJAN6Sa7w1n51x/Ifc Su7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:delivered-to :arc-authentication-results; bh=ARBXbXa/HdtSmt9iCsYX/IL7yOaTAKtKEt8KpQqwqsA=; b=fRrKgp3O5WANj+k88Q7Le9D580Z81ry6cCQW6+4N7Syg5cS8/EArUAaYfpRxVEKeUB Ayy8c2DiVewhGpjhcCcPe29q8Qr4cpyTvvx2gyUD/QKPbDz5rZFuBvW5B/W8TlayugQm 8w3upaSVlyp6N+n/UN/MBuzXCfgoiw4ulRejDNUWbNZgdODY643aLqCLWZa2xlYSN0c6 sZyw423sMejUl42GymfzUF7CJK/8FnQR98T/KTbDMMDT+NTmrZztciqHwgzVIaa1b4Bn iiMLP4KShPPVvbLXDR1aOT84nfn6q5i3OoAdHZyHenftVSKWEYiSbjV58i1snriYWrsn 4Ddw== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q25-v6si4105902wmf.72.2018.06.30.19.09.49; Sat, 30 Jun 2018 19:09:50 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BA3C668A592; Sun, 1 Jul 2018 05:09:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe02-3.mx.upcmail.net (vie01a-qmta-pe02-3.mx.upcmail.net [62.179.121.183]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E75DD68A557 for ; Sun, 1 Jul 2018 05:09:36 +0300 (EEST) Received: from [172.31.218.49] (helo=vie01a-dmta-pe07-1.mx.upcmail.net) by vie01a-pqmta-pe02.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1fZRo2-0004lk-RU for ffmpeg-devel@ffmpeg.org; Sun, 01 Jul 2018 04:09:38 +0200 Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe07.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1fZRnx-0000Zi-7x for ffmpeg-devel@ffmpeg.org; Sun, 01 Jul 2018 04:09:33 +0200 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id 5E9R1y00y0S5wYM01E9S5R; Sun, 01 Jul 2018 04:09:27 +0200 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Sun, 1 Jul 2018 04:08:38 +0200 Message-Id: <20180701020838.26740-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.18.0 Subject: [FFmpeg-devel] [PATCH] avcodec/dvdsubdec: Check for fully transparent rectangles earlier 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Testcase with large transparent rectangles changes from 67 sec to 3 sec decode time Fixes: Timeout Fixes: 8728/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_DVDSUB_fuzzer-5190088756559872 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/dvdsubdec.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/libavcodec/dvdsubdec.c b/libavcodec/dvdsubdec.c index e18113c20c..f1ac9e353e 100644 --- a/libavcodec/dvdsubdec.c +++ b/libavcodec/dvdsubdec.c @@ -42,6 +42,7 @@ typedef struct DVDSubContext uint8_t buf[0x10000]; int buf_size; int forced_subs_only; + uint8_t used_color[256]; #ifdef DEBUG int sub_id; #endif @@ -100,7 +101,7 @@ static int decode_run_8bit(GetBitContext *gb, int *color) return len; } -static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, +static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256], const uint8_t *buf, int start, int buf_size, int is_8bit) { GetBitContext gb; @@ -129,6 +130,7 @@ static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, len = decode_run_2bit(&gb, &color); len = FFMIN(len, w - x); memset(d + x, color, len); + used_color[color] = 1; x += len; if (x >= w) { y++; @@ -370,7 +372,7 @@ static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, h = 0; if (w > 0 && h > 1) { reset_rects(sub_header); - + memset(ctx->used_color, 0, sizeof(ctx->used_color)); sub_header->rects = av_mallocz(sizeof(*sub_header->rects)); if (!sub_header->rects) goto fail; @@ -381,10 +383,10 @@ static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h); if (!bitmap) goto fail; - if (decode_rle(bitmap, w * 2, w, (h + 1) / 2, + if (decode_rle(bitmap, w * 2, w, (h + 1) / 2, ctx->used_color, buf, offset1, buf_size, is_8bit) < 0) goto fail; - if (decode_rle(bitmap + w, w * 2, w, h / 2, + if (decode_rle(bitmap + w, w * 2, w, h / 2, ctx->used_color, buf, offset2, buf_size, is_8bit) < 0) goto fail; sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE); @@ -448,19 +450,24 @@ static int is_transp(const uint8_t *buf, int pitch, int n, } /* return 0 if empty rectangle, 1 if non empty */ -static int find_smallest_bounding_rectangle(AVSubtitle *s) +static int find_smallest_bounding_rectangle(DVDSubContext *ctx, AVSubtitle *s) { uint8_t transp_color[256] = { 0 }; int y1, y2, x1, x2, y, w, h, i; uint8_t *bitmap; + int transparent = 1; if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0) return 0; for(i = 0; i < s->rects[0]->nb_colors; i++) { - if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0) + if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0) { transp_color[i] = 1; + } else if (ctx->used_color[i]) + transparent = 0; } + if (transparent) + return 0; y1 = 0; while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0], 1, s->rects[0]->w, transp_color)) @@ -595,7 +602,7 @@ static int dvdsub_decode(AVCodecContext *avctx, return buf_size; } - if (!is_menu && find_smallest_bounding_rectangle(sub) == 0) + if (!is_menu && find_smallest_bounding_rectangle(ctx, sub) == 0) goto no_subtitle; if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))