From patchwork Sat Feb 20 18:50:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derek Buitenhuis X-Patchwork-Id: 25846 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 C722644A0BF for ; Sat, 20 Feb 2021 20:58:00 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A9B3D68A67C; Sat, 20 Feb 2021 20:58:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6E1ED68A3B2 for ; Sat, 20 Feb 2021 20:57:54 +0200 (EET) Received: by mail-ej1-f44.google.com with SMTP id e13so18968259ejl.8 for ; Sat, 20 Feb 2021 10:57:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=M2WSdBKTzyjz/YHBzy64okROBGsPm+Ok4x/Zke4xsRg=; b=PE6PdiqeG8bXPUK6mBOrakRILR8rr3s2bKsBp7nbScKfy2iJQhIR6nx3stey1JkvBg omfIPrxkNkCCXTF/0i39lAFUDN6egInssfpqzqJyHsTh37ufxysVtIROkK7ft+dmA+ov oI/WSqRZuO7pqyz9aAcMTp3KmL3OnxgDi3+So48aMT8KgxBabxMVbj3xXY93OJmiWsYc U/0HqVAwdHwgibuVmqaA3gN7C0PWRJ/8tlsP8dSiNeQ1G4uL1PeLcgWW92CwZiwBhCGW BVPnogKdsAmNoqY8wF37A0vpGB3lJWKcUROVwSaWZl5EyGGexZfTexVdmvYSghMckNUU YCaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=M2WSdBKTzyjz/YHBzy64okROBGsPm+Ok4x/Zke4xsRg=; b=rfvKTkwmmFTseMk6I3IhlgF3rwkPUiNm8N+eikKjp/Yd1sJZSO1Y4A26WW2D2YY3wX ddYizYo/6EXEezVMnYjHdmNipFJK8AWLxFjp0rWsSbU8bMopuEn0P/gMqCeDyRh4ktJe QwhXYcpDswpKsZguBzAPn288Cj6s8xD1iaAlQpJsvb8sGFVXR1jVc5WV8pOxhnYEW3wR ylsZ5FaBnURZ3NK2ate6SQxXINoKsU8hQsZU0htYzTlMU4VNgQmMk9F/JcjA21Xl48Sq mAf5uVLQ+Y82rOAl/rj6WiwE77X7bQlEx829yK5yyKBXJVozqkcfM3LUKB4sEtAV8kEn G7Ig== X-Gm-Message-State: AOAM532+Gnl2N1U0RwA6BIG+TC7XcqBPqS/B8OWdf9dk+iC0mdjZF9wr aEgefFtELQkJAqStTp3wzdgGNCie43g= X-Google-Smtp-Source: ABdhPJxZE2kVyPoomXjJ1lEOZSelfahxASEX3Tjm6l+BGaQnLGJ31NiArs7fbOdOsLH2hrABhD7Zxw== X-Received: by 2002:adf:f7ca:: with SMTP id a10mr14828120wrq.25.1613847065627; Sat, 20 Feb 2021 10:51:05 -0800 (PST) Received: from localhost.localdomain ([82.129.110.36]) by smtp.gmail.com with ESMTPSA id v17sm19591860wru.85.2021.02.20.10.51.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Feb 2021 10:51:05 -0800 (PST) From: Derek Buitenhuis To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Feb 2021 18:50:50 +0000 Message-Id: <20210220185050.508373-3-derek.buitenhuis@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210220185050.508373-1-derek.buitenhuis@gmail.com> References: <20210220185050.508373-1-derek.buitenhuis@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2 v2] avcodec/gifenc: Only write frame palette entries that actually used 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" GIF palette entries are not compressed, and writing 256 entries, which can be up to every frame, uses a significant amount of space, especially in extreme cases, where palettes can be very small. Example, first six seconds of Tears of Steel, palette generated with libimagequant, 320x240 resolution, and with transparency optimization + per frame palette: * Before patch: 186765 bytes * After patch: 77895 bytes Signed-off-by: Derek Buitenhuis --- The global palette, if used, is no longer ever shrunk in v2 of this patch. This is because just because any given frame doesn't reference an index, doesn't mean a future frame won't. --- libavcodec/gif.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/libavcodec/gif.c b/libavcodec/gif.c index 8c07ee2769..420a1f523e 100644 --- a/libavcodec/gif.c +++ b/libavcodec/gif.c @@ -63,6 +63,44 @@ enum { GF_TRANSDIFF = 1<<1, }; +static void shrink_palette(const uint32_t *src, uint8_t *map, + uint32_t *dst, size_t *palette_count) +{ + size_t colors_seen = 0; + + for (size_t i = 0; i < AVPALETTE_COUNT; i++) { + int seen = 0; + for (size_t c = 0; c < colors_seen; c++) { + if (src[i] == dst[c]) { + seen = 1; + break; + } + } + if (!seen) { + dst[colors_seen] = src[i]; + map[i] = colors_seen; + colors_seen++; + } + } + + *palette_count = colors_seen; +} + +static void remap_frame_to_palette(const uint8_t *src, int src_linesize, + uint8_t *dst, int dst_linesize, + int w, int h, uint8_t *map) +{ + uint8_t *sptr = (uint8_t *) src; + uint8_t *dptr = dst; + + for (int i = 0; i < h; i++) { + for (int j = 0; j < w; j++) + dptr[j] = map[sptr[j]]; + dptr += dst_linesize; + sptr += src_linesize; + } +} + static int is_image_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize) { @@ -267,6 +305,18 @@ static int gif_image_write_image(AVCodecContext *avctx, int x_start = 0, y_start = 0, trans = s->transparent_index; int bcid = -1, honor_transparency = (s->flags & GF_TRANSDIFF) && s->last_frame && !palette; const uint8_t *ptr; + uint32_t shrunk_palette[AVPALETTE_COUNT]; + uint8_t map[AVPALETTE_COUNT] = { 0 }; + size_t shrunk_palette_count = 0; + uint8_t *shrunk_buf = NULL; + + /* + * We memset to 0xff instead of 0x00 so that the transparency detection + * doesn't pick anything after the palette entries as the transparency + * index, and because GIF89a requires us to always write a power-of-2 + * number of palette entries. + */ + memset(shrunk_palette, 0xff, AVPALETTE_SIZE); if (!s->image && is_image_translucent(avctx, buf, linesize)) { gif_crop_translucent(avctx, buf, linesize, &width, &height, &x_start, &y_start); @@ -335,9 +385,14 @@ static int gif_image_write_image(AVCodecContext *avctx, if (palette || !s->use_global_palette) { const uint32_t *pal = palette ? palette : s->palette; + unsigned pow2_count; unsigned i; - bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */ - for (i = 0; i < AVPALETTE_COUNT; i++) { + + shrink_palette(pal, map, shrunk_palette, &shrunk_palette_count); + pow2_count = av_log2(shrunk_palette_count - 1); + + bytestream_put_byte(bytestream, 1<<7 | pow2_count); /* flags */ + for (i = 0; i < 1 << (pow2_count + 1); i++) { const uint32_t v = pal[i]; bytestream_put_be24(bytestream, v); } @@ -350,7 +405,17 @@ static int gif_image_write_image(AVCodecContext *avctx, ff_lzw_encode_init(s->lzw, s->buf, s->buf_size, 12, FF_LZW_GIF, 1); - ptr = buf + y_start*linesize + x_start; + if (shrunk_palette_count) { + shrunk_buf = av_malloc(avctx->height * linesize); + if (!shrunk_buf) { + av_log(avctx, AV_LOG_ERROR, "Could not allocated remapped frame buffer.\n"); + return AVERROR(ENOMEM); + } + remap_frame_to_palette(buf, linesize, shrunk_buf, linesize, avctx->width, avctx->height, map); + ptr = shrunk_buf + y_start*linesize + x_start; + } else { + ptr = buf + y_start*linesize + x_start; + } if (honor_transparency) { const int ref_linesize = s->last_frame->linesize[0]; const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start; @@ -383,6 +448,7 @@ static int gif_image_write_image(AVCodecContext *avctx, len -= size; } bytestream_put_byte(bytestream, 0x00); /* end of image block */ + av_freep(&shrunk_buf); return 0; }