From patchwork Tue Oct 24 11:13:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Izen X-Patchwork-Id: 44344 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1b28:b0:15d:8365:d4b8 with SMTP id ch40csp1979195pzb; Tue, 24 Oct 2023 04:13:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnMGyKMgiVZlHaXKZ8aRxPBYp31s7z9xw+tfJQTqCDgfbfKN9OHgVvwkQzfj9nH2kFnAjG X-Received: by 2002:a50:ba8c:0:b0:530:77e6:849f with SMTP id x12-20020a50ba8c000000b0053077e6849fmr7550605ede.27.1698146008747; Tue, 24 Oct 2023 04:13:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698146008; cv=none; d=google.com; s=arc-20160816; b=gMsSTeb302hMdexZa2rY4JOxsUY3P6GbIAOkj1TVuk13OWpJu6Mfm+/upiYeCqhmgC gM2xI/hJ7y8bX/3v0AeOOKyG8JLuToNsbo+awUyMLNFUPZY2tM1p/JFf9m2QWOWDNbTo NAULM06ezE3niDqhyAzIcKD1h4efZdsX7z5pQkSQFA81G+o1ch2tuw9UXMF6j4UPDgdO 4MNRfO/bkP2UosxaaCAlRoUbAqr/8psrByRziyF8EZ1qgvPq5REWjSfMjHytzoskZGmM mDCG5nnJfdyXfBecJ4r6veZzExvrl5dtKbDnJQW0G+fmlqGDL73PaeUuxpfbei808m0T YuHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=8BIq+SUJllBo8LIDMpyuoq7fTBuZqowUwgjY8otlNdw=; fh=+bdjGe20eEUjtjncwA1dnEEVYNfJL4vyhV+sIRR4l+g=; b=0RUWhMJTbPRoQI/KgukzE2mTX+pJvBKGwJm5eCkVCGuwRwSJXkX+cZrCaPLZE81dmk aLSI0U1LMpy45Nx/H/CrB/vH+qLu8xCBePel5kOCI+5o7J9bPfM2Sx/1nDico1bSwgnV +R1VXWCoQUibNPFVcwerbwA6Qc1XoDBMTIghxCvz6p8ZVCc4TfD3k5psouPNzVYwHYvS kqb4tOKQxV4l3wsD5zFG4qh4E04DrmYAUjCxgBzZ1MWecRnF1PBhGWJFrN79jxmnMYbe 2r+3uBYwonHYB/F23DB5nsODebVCORGJpzXPsFIM3W/GbDFOIi9aqSVp8lnAvvWTOS3D Pl8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=dnk82cFG; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x17-20020a05640226d100b0053e6ea4761asi4638435edd.137.2023.10.24.04.13.28; Tue, 24 Oct 2023 04:13:28 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=dnk82cFG; 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=QUARANTINE 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 7641268018A; Tue, 24 Oct 2023 14:13:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id ECE3268B8B6 for ; Tue, 24 Oct 2023 14:13:17 +0300 (EEST) Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-66d32cc3957so10493806d6.0 for ; Tue, 24 Oct 2023 04:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698145996; x=1698750796; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=PYAuJowYRvka1T0Fmk/2K4y+hW2EzPZASf65w6tyKBw=; b=dnk82cFGZSE3tKrUYgjs2AURgvB7j2g6gLtwz2RL+3n9yHlUeuLyPJcwl6evG4D26I lLOnGmyBjdVBcq1X6tNc2eHyF6Wf7ivH9Knt4deu2MRcYcDg1KbsNc08qXua4qfHRAcL zTMeVRPhGIOjPlKQTkaKRWyZf8m9htiUIoKJbFue8Zx0mMo96bgEmkemhyLysSD/DZuF TLuTiZ90KRQPnlNsirE+5FXUtN+r7VHkamEYWDWd4U/CSM2PmtJkiyBfh1rQVMx4aohP dS/oM4nCY1mqAEIDw6oM/vTUNKlqAWao7CxaVtzAgzHCq2nB6EKaH60t+auKiWraVh/c RHsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698145996; x=1698750796; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PYAuJowYRvka1T0Fmk/2K4y+hW2EzPZASf65w6tyKBw=; b=rrLVWHSVRpiPU7M0wFRee5IF+dZrSTqLSlD8wzBwZN5F+0RSIj/QlAUhXz1XmUlfNg Rovau8x8XvRbzyOP95Oi24iAR+Gppj68VWteRzr54Y7uj7+3cS5dDHdfJjhdRsXo7y66 xHD9ngivkJjEsXT6vk5TkjwRlJMnD9cEqThnaF0GWykm2uNM8aMeeldhE4SVVWh1kX2L eb5tzzX4c8kjYK5Yu09Pvkksytmdqvtaw4Sucro2t0nbrPOQ5zy1gseH0l8101Ny8QbZ 8/qpENDTFXtABqVcvb6Vpur/2Mf/2OsU1F7vz5tbKYuGZJPdv8ONRTRz4stdp1qJFM9Z Pc+g== X-Gm-Message-State: AOJu0Yxor7Ly1lSImG3hC9fw7y9vzFxpB2hIIT2Fi+xJPLRoSC1hB98T Xrw7iETnpNk8tGI92CqDteDAZ1RKxpA3VFcm X-Received: by 2002:a05:6214:3d8d:b0:66d:1ff9:321f with SMTP id om13-20020a0562143d8d00b0066d1ff9321fmr12627815qvb.6.1698145996210; Tue, 24 Oct 2023 04:13:16 -0700 (PDT) Received: from gauss.local (c-68-56-149-176.hsd1.mi.comcast.net. [68.56.149.176]) by smtp.gmail.com with ESMTPSA id oj2-20020a056214440200b006577e289d37sm3577299qvb.2.2023.10.24.04.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:13:15 -0700 (PDT) From: Leo Izen To: ffmpeg-devel@ffmpeg.org Date: Tue, 24 Oct 2023 07:13:14 -0400 Message-ID: <20231024111314.54717-1-leo.izen@gmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/pngdec: various stylistic changes X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Leo Izen Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: djmNYTxKejTj Various parts of this file are restructured slightly for readability, such as spacing in arithmetic operations, and putting `if (ret < 0)` clauses on separate lines. Signed-off-by: Leo Izen --- libavcodec/pngdec.c | 241 +++++++++++++++++++++++--------------------- 1 file changed, 124 insertions(+), 117 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index d812ffd348..93b067a9be 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -128,7 +128,7 @@ static const uint8_t png_pass_dsp_ymask[NB_PASSES] = { /* Mask to determine which pixels to overwrite while displaying */ static const uint8_t png_pass_dsp_mask[NB_PASSES] = { - 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff + 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff, }; /* NOTE: we try to construct a good looking image at each pass. width @@ -152,7 +152,7 @@ static void png_put_interlaced_row(uint8_t *dst, int width, j = (x & 7); if ((dsp_mask << j) & 0x80) { b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1; - dst[x >> 3] &= 0xFF7F>>j; + dst[x >> 3] &= 0xff7f >> j; dst[x >> 3] |= b << (7 - j); } if ((mask << j) & 0x80) @@ -165,8 +165,8 @@ static void png_put_interlaced_row(uint8_t *dst, int width, int j2 = 2 * (x & 3); j = (x & 7); if ((dsp_mask << j) & 0x80) { - b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3; - dst[x >> 2] &= 0xFF3F>>j2; + b = (src[src_x >> 2] >> (6 - 2 * (src_x & 3))) & 3; + dst[x >> 2] &= 0xff3f >> j2; dst[x >> 2] |= b << (6 - j2); } if ((mask << j) & 0x80) @@ -176,11 +176,11 @@ static void png_put_interlaced_row(uint8_t *dst, int width, case 4: src_x = 0; for (x = 0; x < width; x++) { - int j2 = 4*(x&1); + int j2 = 4 * (x & 1); j = (x & 7); if ((dsp_mask << j) & 0x80) { - b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15; - dst[x >> 1] &= 0xFF0F>>j2; + b = (src[src_x >> 1] >> (4 - 4 * (src_x & 1))) & 15; + dst[x >> 1] &= 0xff0f >> j2; dst[x >> 1] |= b << (4 - j2); } if ((mask << j) & 0x80) @@ -191,15 +191,14 @@ static void png_put_interlaced_row(uint8_t *dst, int width, bpp = bits_per_pixel >> 3; d = dst; s = src; - for (x = 0; x < width; x++) { - j = x & 7; - if ((dsp_mask << j) & 0x80) { - memcpy(d, s, bpp); - } - d += bpp; - if ((mask << j) & 0x80) - s += bpp; - } + for (x = 0; x < width; x++) { + j = x & 7; + if ((dsp_mask << j) & 0x80) + memcpy(d, s, bpp); + d += bpp; + if ((mask << j) & 0x80) + s += bpp; + } break; } } @@ -207,8 +206,7 @@ static void png_put_interlaced_row(uint8_t *dst, int width, void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp) { - int i; - for (i = 0; i < w; i++) { + for (int i = 0; i < w; i++) { int a, b, c, p, pa, pb, pc; a = dst[i - bpp]; @@ -233,7 +231,7 @@ void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, } #define UNROLL1(bpp, op) \ - { \ + do { \ r = dst[0]; \ if (bpp >= 2) \ g = dst[1]; \ @@ -253,21 +251,21 @@ void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, continue; \ dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \ } \ - } - -#define UNROLL_FILTER(op) \ - if (bpp == 1) { \ - UNROLL1(1, op) \ - } else if (bpp == 2) { \ - UNROLL1(2, op) \ - } else if (bpp == 3) { \ - UNROLL1(3, op) \ - } else if (bpp == 4) { \ - UNROLL1(4, op) \ - } \ - for (; i < size; i++) { \ + } while (0) + +#define UNROLL_FILTER(op) do { \ + if (bpp == 1) \ + UNROLL1(1, op); \ + else if (bpp == 2) \ + UNROLL1(2, op); \ + else if (bpp == 3) \ + UNROLL1(3, op); \ + else if (bpp == 4) \ + UNROLL1(4, op); \ + \ + for (; i < size; i++) \ dst[i] = op(dst[i - bpp], src[i], last[i]); \ - } +} while (0) /* NOTE: 'dst' can be equal to 'last' */ void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, @@ -330,9 +328,8 @@ void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, #define YUV2RGB(NAME, TYPE) \ static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \ { \ - int i; \ - for (i = 0; i < size - 2; i += 3 + alpha) { \ - int g = dst [i + 1]; \ + for (int i = 0; i < size - 2; i += 3 + alpha) { \ + int g = dst[i + 1]; \ dst[i + 0] += g; \ dst[i + 2] += g; \ } \ @@ -343,11 +340,10 @@ YUV2RGB(rgb16, uint16_t) static int percent_missing(PNGDecContext *s) { - if (s->interlace_type) { + if (s->interlace_type) return 100 - 100 * s->pass / (NB_PASSES - 1); - } else { + else return 100 - 100 * s->y / s->cur_h; - } } /* process exactly one decompressed row */ @@ -367,30 +363,28 @@ static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride) last_row, s->row_size, s->bpp); /* loco lags by 1 row so that it doesn't interfere with top prediction */ if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) { - if (s->bit_depth == 16) { + if (s->bit_depth == 16) deloco_rgb16((uint16_t *)(ptr - dst_stride), s->row_size / 2, s->color_type == PNG_COLOR_TYPE_RGB_ALPHA); - } else { + else deloco_rgb8(ptr - dst_stride, s->row_size, s->color_type == PNG_COLOR_TYPE_RGB_ALPHA); - } } s->y++; if (s->y == s->cur_h) { s->pic_state |= PNG_ALLIMAGE; if (s->filter_type == PNG_FILTER_TYPE_LOCO) { - if (s->bit_depth == 16) { + if (s->bit_depth == 16) deloco_rgb16((uint16_t *)ptr, s->row_size / 2, s->color_type == PNG_COLOR_TYPE_RGB_ALPHA); - } else { + else deloco_rgb8(ptr, s->row_size, s->color_type == PNG_COLOR_TYPE_RGB_ALPHA); - } } } } else { got_line = 0; - for (;;) { + while (1) { ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp; if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) { /* if we already read one row, it is time to stop to @@ -403,17 +397,16 @@ static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride) FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size); got_line = 1; } - if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) { + if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass, s->color_type, s->last_row); - } s->y++; if (s->y == s->cur_h) { memset(s->last_row, 0, s->row_size); - for (;;) { + while (1) { if (s->pass == NB_PASSES - 1) { s->pic_state |= PNG_ALLIMAGE; - goto the_end; + return; } else { s->pass++; s->y = 0; @@ -428,7 +421,6 @@ static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride) } } } -the_end:; } } @@ -448,18 +440,18 @@ static int png_decode_idat(PNGDecContext *s, GetByteContext *gb, return AVERROR_EXTERNAL; } if (zstream->avail_out == 0) { - if (!(s->pic_state & PNG_ALLIMAGE)) { + if (!(s->pic_state & PNG_ALLIMAGE)) png_handle_row(s, dst, dst_stride); - } zstream->avail_out = s->crow_size; zstream->next_out = s->crow_buf; } if (ret == Z_STREAM_END && zstream->avail_in > 0) { av_log(s->avctx, AV_LOG_WARNING, "%d undecompressed bytes left in buffer\n", zstream->avail_in); - return 0; + break; } } + return 0; } @@ -538,7 +530,7 @@ static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compresse const char *keyword_end = memchr(keyword, 0, data_end - data); char *kw_utf8 = NULL, *txt_utf8 = NULL; const char *text; - unsigned text_len; + size_t text_len; AVBPrint bp; if (!keyword_end) @@ -551,7 +543,8 @@ static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compresse method = *(data++); if (method) return AVERROR_INVALIDDATA; - if ((ret = decode_zbuf(&bp, data, data_end, s->avctx)) < 0) + ret = decode_zbuf(&bp, data, data_end, s->avctx); + if (ret < 0) return ret; text = bp.str; text_len = bp.len; @@ -637,7 +630,7 @@ static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s, avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb); avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb); if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0) - avctx->sample_aspect_ratio = (AVRational){ 0, 1 }; + avctx->sample_aspect_ratio = av_make_q(0, 1); bytestream2_skip(gb, 1); /* unit specifier */ return 0; @@ -833,8 +826,8 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, return ret; } else { /* The picture output this time and the reference to retain coincide. */ - if ((ret = ff_thread_get_ext_buffer(avctx, &s->picture, - AV_GET_BUFFER_FLAG_REF)) < 0) + ret = ff_thread_get_ext_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF); + if (ret < 0) return ret; ret = av_frame_ref(p, s->picture.f); if (ret < 0) @@ -845,7 +838,8 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, p->flags |= AV_FRAME_FLAG_KEY; p->flags |= AV_FRAME_FLAG_INTERLACED * !!s->interlace_type; - if ((ret = populate_avctx_color_fields(avctx, p)) < 0) + ret = populate_avctx_color_fields(avctx, p); + if (ret < 0) return ret; ff_thread_finish_setup(avctx); @@ -992,7 +986,8 @@ static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb) goto fail; } - if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end, s->avctx)) < 0) + ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end, s->avctx); + if (ret < 0) return ret; av_freep(&s->iccp_data); @@ -1050,17 +1045,17 @@ static void handle_small_bpp(PNGDecContext *s, AVFrame *p) for (j = 0; j < s->height; j++) { i = s->width / 8; for (k = 7; k >= 1; k--) - if ((s->width&7) >= k) - pd[8*i + k - 1] = (pd[i]>>8-k) & 1; + if ((s->width & 7) >= k) + pd[8*i + k - 1] = (pd[i] >> (8 - k)) & 1; for (i--; i >= 0; i--) { - pd[8*i + 7]= pd[i] & 1; - pd[8*i + 6]= (pd[i]>>1) & 1; - pd[8*i + 5]= (pd[i]>>2) & 1; - pd[8*i + 4]= (pd[i]>>3) & 1; - pd[8*i + 3]= (pd[i]>>4) & 1; - pd[8*i + 2]= (pd[i]>>5) & 1; - pd[8*i + 1]= (pd[i]>>6) & 1; - pd[8*i + 0]= pd[i]>>7; + pd[8*i + 7] = pd[i] & 1; + pd[8*i + 6] = (pd[i] >> 1) & 1; + pd[8*i + 5] = (pd[i] >> 2) & 1; + pd[8*i + 4] = (pd[i] >> 3) & 1; + pd[8*i + 3] = (pd[i] >> 4) & 1; + pd[8*i + 2] = (pd[i] >> 5) & 1; + pd[8*i + 1] = (pd[i] >> 6) & 1; + pd[8*i + 0] = pd[i] >> 7; } pd += p->linesize[0]; } @@ -1070,24 +1065,24 @@ static void handle_small_bpp(PNGDecContext *s, AVFrame *p) for (j = 0; j < s->height; j++) { i = s->width / 4; if (s->color_type == PNG_COLOR_TYPE_PALETTE) { - if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3; - if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3; - if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6; + if ((s->width & 3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3; + if ((s->width & 3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3; + if ((s->width & 3) >= 1) pd[4*i + 0]= pd[i] >> 6; for (i--; i >= 0; i--) { - pd[4*i + 3]= pd[i] & 3; - pd[4*i + 2]= (pd[i]>>2) & 3; - pd[4*i + 1]= (pd[i]>>4) & 3; - pd[4*i + 0]= pd[i]>>6; + pd[4*i + 3]= pd[i] & 3; + pd[4*i + 2]= (pd[i] >> 2) & 3; + pd[4*i + 1]= (pd[i] >> 4) & 3; + pd[4*i + 0]= pd[i] >> 6; } } else { - if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55; - if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55; - if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55; + if ((s->width & 3) >= 3) pd[4*i + 2]= ((pd[i] >> 2) & 3) * 0x55; + if ((s->width & 3) >= 2) pd[4*i + 1]= ((pd[i] >> 4) & 3) * 0x55; + if ((s->width & 3) >= 1) pd[4*i + 0]= ( pd[i] >> 6 ) * 0x55; for (i--; i >= 0; i--) { - pd[4*i + 3]= ( pd[i] & 3)*0x55; - pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55; - pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55; - pd[4*i + 0]= ( pd[i]>>6 )*0x55; + pd[4*i + 3]= ( pd[i] & 3) * 0x55; + pd[4*i + 2]= ((pd[i] >> 2) & 3) * 0x55; + pd[4*i + 1]= ((pd[i] >> 4) & 3) * 0x55; + pd[4*i + 0]= ( pd[i] >> 6 ) * 0x55; } } pd += p->linesize[0]; @@ -1096,18 +1091,20 @@ static void handle_small_bpp(PNGDecContext *s, AVFrame *p) int i, j; uint8_t *pd = p->data[0]; for (j = 0; j < s->height; j++) { - i = s->width/2; + i = s->width / 2; if (s->color_type == PNG_COLOR_TYPE_PALETTE) { - if (s->width&1) pd[2*i+0]= pd[i]>>4; + if (s->width & 1) + pd[2*i+0] = pd[i] >> 4; for (i--; i >= 0; i--) { - pd[2*i + 1] = pd[i] & 15; + pd[2*i + 1] = pd[i] & 0xff; pd[2*i + 0] = pd[i] >> 4; } } else { - if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11; - for (i--; i >= 0; i--) { - pd[2*i + 1] = (pd[i] & 15) * 0x11; + if (s->width & 1) pd[2*i + 0] = (pd[i] >> 4) * 0x11; + for (i--; i >= 0; i--) { + pd[2*i + 1] = (pd[i] & 0xff) * 0x11; + pd[2*i + 0] = (pd[i] >> 4) * 0x11; } } pd += p->linesize[0]; @@ -1321,7 +1318,7 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, int decode_next_dat = 0; int i, ret; - for (;;) { + while (1) { GetByteContext gb_chunk; length = bytestream2_get_bytes_left(&s->gb); @@ -1339,8 +1336,7 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, goto exit_loop; } av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length); - if ( s->pic_state & PNG_ALLIMAGE - && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL) + if (s->pic_state & PNG_ALLIMAGE && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL) goto exit_loop; ret = AVERROR_INVALIDDATA; goto fail; @@ -1405,7 +1401,8 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, case MKTAG('f', 'c', 'T', 'L'): if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG) continue; - if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0) + ret = decode_fctl_chunk(avctx, s, &gb_chunk); + if (ret < 0) goto fail; decode_next_dat = 1; break; @@ -1421,14 +1418,19 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, case MKTAG('I', 'D', 'A', 'T'): if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat) continue; - if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0) + ret = decode_idat_chunk(avctx, s, &gb_chunk, p); + if (ret < 0) goto fail; break; case MKTAG('P', 'L', 'T', 'E'): - decode_plte_chunk(avctx, s, &gb_chunk); + ret = decode_plte_chunk(avctx, s, &gb_chunk); + if (ret < 0) + goto fail; break; case MKTAG('t', 'R', 'N', 'S'): - decode_trns_chunk(avctx, s, &gb_chunk); + ret = decode_trns_chunk(avctx, s, &gb_chunk); + if (ret < 0) + goto fail; break; case MKTAG('t', 'E', 'X', 't'): if (decode_text_chunk(s, &gb_chunk, 0) < 0) @@ -1468,7 +1470,8 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, s->have_srgb = 1; break; case MKTAG('i', 'C', 'C', 'P'): { - if ((ret = decode_iccp_chunk(s, &gb_chunk)) < 0) + ret = decode_iccp_chunk(s, &gb_chunk); + if (ret < 0) goto fail; break; } @@ -1487,22 +1490,24 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, break; } case MKTAG('s', 'B', 'I', 'T'): - if ((ret = decode_sbit_chunk(avctx, s, &gb_chunk)) < 0) + ret = decode_sbit_chunk(avctx, s, &gb_chunk); + if (ret < 0) goto fail; break; case MKTAG('g', 'A', 'M', 'A'): { AVBPrint bp; - char *gamma_str; + char *gamma_str = NULL; s->gamma = bytestream2_get_be32(&gb_chunk); av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED); av_bprintf(&bp, "%i/%i", s->gamma, 100000); ret = av_bprint_finalize(&bp, &gamma_str); - if (ret < 0) - return ret; + if (ret < 0) { + av_freep(&gamma_str); + goto fail; + } av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL); - break; } case MKTAG('I', 'E', 'N', 'D'): @@ -1540,10 +1545,10 @@ exit_loop: for (int x = s->width - 1; x >= 0; x--) { const uint8_t idx = row[x]; - row[4*x+2] = s->palette[idx] & 0xFF; - row[4*x+1] = (s->palette[idx] >> 8 ) & 0xFF; - row[4*x+0] = (s->palette[idx] >> 16) & 0xFF; - row[4*x+3] = s->palette[idx] >> 24; + row[4*x + 2] = s->palette[idx] & 0xFF; + row[4*x + 1] = (s->palette[idx] >> 8 ) & 0xFF; + row[4*x + 0] = (s->palette[idx] >> 16) & 0xFF; + row[4*x + 3] = s->palette[idx] >> 24; } } } @@ -1572,7 +1577,7 @@ exit_loop: uint8_t *rowp = &row[3 * s->width - 1]; int tcolor = AV_RL24(s->transparent_color_be); for (x = s->width; x > 0; --x) { - *pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff; + *pixel-- = AV_RL24(rowp - 2) == tcolor ? 0 : 0xff; *pixel-- = *rowp--; *pixel-- = *rowp--; *pixel-- = *rowp--; @@ -1583,11 +1588,10 @@ exit_loop: uint8_t *pixel = &row[s->bpp * (x - 1)]; memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp); - if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) { + if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) memset(&pixel[raw_bpp], 0, byte_depth); - } else { + else memset(&pixel[raw_bpp], 0xff, byte_depth); - } } } } @@ -1689,7 +1693,8 @@ static int decode_frame_png(AVCodecContext *avctx, AVFrame *p, if (ret != Z_OK) return AVERROR_EXTERNAL; - if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0) + ret = decode_frame_common(avctx, s, p, avpkt); + if (ret < 0) goto the_end; if (avctx->skip_frame == AVDISCARD_ALL) { @@ -1729,20 +1734,22 @@ static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p, if (!avctx->extradata_size) return AVERROR_INVALIDDATA; - if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK) + if (inflateReset(&s->zstream.zstream) != Z_OK) return AVERROR_EXTERNAL; bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size); - if ((ret = decode_frame_common(avctx, s, NULL, avpkt)) < 0) + ret = decode_frame_common(avctx, s, NULL, avpkt); + if (ret < 0) return ret; } /* reset state for a new frame */ - if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK) + if (inflateReset(&s->zstream.zstream) != Z_OK) return AVERROR_EXTERNAL; s->y = 0; s->pic_state = 0; bytestream2_init(&s->gb, avpkt->data, avpkt->size); - if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0) + ret = decode_frame_common(avctx, s, p, avpkt); + if (ret < 0) return ret; if (!(s->pic_state & PNG_ALLIMAGE))