From 41b8fd3a16a0c8f2de71a5338fb4f086bd857124 Mon Sep 17 00:00:00 2001 From: Jose Da Silva Date: Mon, 18 Jan 2021 21:42:57 -0800 Subject: [PATCH] avcodec/xbmenc: Do not add last comma into output There is a minor bug in xbm encode which adds a trailing comma at the end of data. This isn't a big problem, but it would be nicer to be more technically true to an array of data (by not including the last comma). This bug fixes the output from something like this (having 4 values): static unsigned char image_bits[] = { 0x00, 0x11, 0x22, } to C code that looks like this instead (having 3 values): static unsigned char image_bits[] = { 0x00, 0x11, 0x22 } which is the intended results. Subject: [PATCH 1/3] avcodec/xbmenc: Do not add last comma into output array xbm outputs c arrays of data. Including a comma at the end means there is another value to be added. This bug fix changes something like this: static unsigned char image_bits[] = { 0x00, 0x11, 0x22, } to C code like this: static unsigned char image_bits[] = { 0x00, 0x11, 0x22 } Signed-off-by: Joe Da Silva --- libavcodec/xbmenc.c | 12 ++++++++---- tests/ref/lavf/xbm | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/libavcodec/xbmenc.c b/libavcodec/xbmenc.c index b25615f2a4..3fc0e3185a 100644 --- a/libavcodec/xbmenc.c +++ b/libavcodec/xbmenc.c @@ -27,11 +27,12 @@ static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet) { - int i, j, ret, size, linesize; + int i, j, commas, ret, size, linesize; uint8_t *ptr, *buf; linesize = (avctx->width + 7) / 8; - size = avctx->height * (linesize * 7 + 2) + 110; + commas = avctx->height * linesize; + size = avctx->height * (linesize * 7 + 2) + 109; if ((ret = ff_alloc_packet2(avctx, pkt, size, 0)) < 0) return ret; @@ -42,8 +43,11 @@ static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, buf += snprintf(buf, 33, "#define image_height %u\n", avctx->height); buf += snprintf(buf, 40, "static unsigned char image_bits[] = {\n"); for (i = 0; i < avctx->height; i++) { - for (j = 0; j < linesize; j++) - buf += snprintf(buf, 7, " 0x%02X,", ff_reverse[*ptr++]); + for (j = 0; j < linesize; j++) { + buf += snprintf(buf, 6, " 0x%02X", ff_reverse[*ptr++]); + if (--commas > 0) + buf += snprintf(buf, 2, ","); + } ptr += p->linesize[0] - linesize; buf += snprintf(buf, 2, "\n"); } diff --git a/tests/ref/lavf/xbm b/tests/ref/lavf/xbm index bc157834ff..e54d6bc226 100644 --- a/tests/ref/lavf/xbm +++ b/tests/ref/lavf/xbm @@ -1,3 +1,3 @@ -0629055fd82366317c651a0af4bb82d7 *tests/data/images/xbm/02.xbm +83ed197cc88f382d9253365ffef70ec5 *tests/data/images/xbm/02.xbm tests/data/images/xbm/%02d.xbm CRC=0xc9a20204 -76411 tests/data/images/xbm/02.xbm +76410 tests/data/images/xbm/02.xbm