From patchwork Thu Jun 2 16:01:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 36058 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6914:b0:82:6b11:2509 with SMTP id q20csp4120913pzj; Thu, 2 Jun 2022 08:58:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqOm5yd0bpFVCYOWm7muAfu153NA6V95Gi5WMc10bOZrFQNQ8B0YhLl1Igv+v/xInzPHyL X-Received: by 2002:a05:6402:510a:b0:42b:66c3:d4fd with SMTP id m10-20020a056402510a00b0042b66c3d4fdmr6272234edd.260.1654185537205; Thu, 02 Jun 2022 08:58:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654185537; cv=none; d=google.com; s=arc-20160816; b=BFgOTwZdkNG/l7DdzrbDXAk+Y5J/WtaIjLGr2t+6HXJwLQh04FPAJDS7Sb4hB5/tQp BUX2yu+d2eISH805ctk7HmEXITWYkkX3RKb1h3IZ1LHObbWt6Y2qYybSp3+jy6fDbXEI bw/8Qx927t5WoKqtrH2VYbCZcM7Q5gsuCyS4bUZ4NgG1ow63xQAotNpKZDn8qBfoEz/8 q+cUWlaYM0GZFv8JBQysu4GJXfmyMh/+moFIzuUCcXZd8zpOYjiSjSj5vrT9PAvirW75 hRTP5SZu/CCcS48ig9bx+MSXT1jUY022LjW45C4OPvzkjE9uruUcXhokJWv08ZIU0keM EzGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=akNCsviQ2JOdkhwVN5OIRx/CvGGof5V7lOtaIEK0Bj4=; b=PykmmIUVWMPiJ+d8XZ8GilywbGZlZPY8j9DihLzfIBCdJNjiO4W/axhGtO4sLRTsKd vR9JcgJDnaH81REvAefJg1V+MxYqPnGCV3GFd1yooFxtQ0Jx8PDMw1leCrAz4Ty/+T45 SrqSDQTacylIkVn9fIPFnTcs5FM6Xn5tTTlKrjcrxZEq2T9r+EQiM0sHjbgKBlk81qJp 8Jj7F8zbYe7TfLdcar9vNaKU3de/jkEqmPdBUWmAirV5yyCqL5BOWxJeOwfoHzhH6JFZ heu6hgYSicLabuPuOjfh/ExQ0SEpihgvxRqa38PKcE5E4qUu/Kc58yT4Lw5MyyFlq29O Rd5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=NHUXL1XW; 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 gn33-20020a1709070d2100b006ff786787aasi5739850ejc.545.2022.06.02.08.58.55; Thu, 02 Jun 2022 08:58:57 -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=20210112 header.b=NHUXL1XW; 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 97FCA68B7AA; Thu, 2 Jun 2022 18:58:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2C6EA68B546 for ; Thu, 2 Jun 2022 18:58:45 +0300 (EEST) Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-30fdbe7467cso20761777b3.1 for ; Thu, 02 Jun 2022 08:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=r/J3eElDTlwQgPue7AlG5HGH69WQNK3xzOBuBWYeOJw=; b=NHUXL1XWuTmkhKQbi/7dCpXz5+Y3eTk3if9j0xeaT1Mcx6hFfqTEDaN9maXudfcxA5 cyHZrhKYBCng7HsOmfpsnmZSo4Cev31zHW+i3fz3Ji/mBp7g8FPOQ5ifaaH8GmfrbIQs 3QuPCv+R7K7yVZHWpKFq8WALoK9+KHGhIBnbodh5VewpeJwcQJ5oQN3aV4y16/pjgcLO f+YzrJDSmONLOH1GcyySr947FKGehymkYyQKH7BRpit5QySxdLzy42d5+DCztQ4l857v ByHGWyfysh2+BHBPC5Z8BZXz0nsf3FFv8LIDfkbbrWt/Ls2aJKSxPQO8R2gB+wISZrE+ 5zQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=r/J3eElDTlwQgPue7AlG5HGH69WQNK3xzOBuBWYeOJw=; b=UjuBKlXLXc34B8as+DotNu7Vg1l2k5RW9AWKwS1Hz8Ny3ix3joZKoItbNus+EmbOaD Bkvkc4rtunBd8wlf7K9RWBoUWMg2sWxGjeeD9JEfhKXBWH87uf1UTGWjUMT4lFE8cKVd YljpKJemZIj89GhNIaQmsYaCO+9pDaYHNSeHdRTZBxz6YE/cOewYyJDg70THFx6t4vpT A5wVO397nihl6znr2nl5xht8xvl3d9Rz0id9/lHsMBdZPZ7SPnB9scqOy/w7aH4dx61z /BNxDFsXCNNeP+MYOphetuTNKdatf+Q4aX3ySakt2Y01zZzCXOine+R21X34usCjH/PM +xaw== X-Gm-Message-State: AOAM530p8JYvYNkL/fCiCgQJl/OQN8RgrP9cdolznz2oqEOJJTJpxg1l 60g20o/yLO8VCiMty524S0OmOVwypR3ZgxNBQo4JbMiMmcQ= X-Received: by 2002:a0d:d7c7:0:b0:30c:754d:6d8e with SMTP id z190-20020a0dd7c7000000b0030c754d6d8emr6204099ywd.516.1654185523289; Thu, 02 Jun 2022 08:58:43 -0700 (PDT) MIME-Version: 1.0 From: Paul B Mahol Date: Thu, 2 Jun 2022 18:01:39 +0200 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: [FFmpeg-devel] [PATCH] avcodec: add QOI decoder and demuxer and parser and encoder and muxer 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: qYPIswCbIfjo Patch attached. From 885ef4eaa070ff2b6517842997724b93f9663486 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 31 May 2022 12:33:54 +0200 Subject: [PATCH] avcodec: add QOI decoder and demuxer and parser and encoder and muxer Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 3 + libavcodec/allcodecs.c | 2 + libavcodec/codec_desc.c | 7 ++ libavcodec/codec_id.h | 1 + libavcodec/parsers.c | 1 + libavcodec/qoi.h | 35 ++++++++++ libavcodec/qoi_parser.c | 77 +++++++++++++++++++++ libavcodec/qoidec.c | 126 +++++++++++++++++++++++++++++++++++ libavcodec/qoienc.c | 140 +++++++++++++++++++++++++++++++++++++++ libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/img2.c | 1 + libavformat/img2dec.c | 18 +++++ libavformat/img2enc.c | 2 +- 14 files changed, 414 insertions(+), 1 deletion(-) create mode 100644 libavcodec/qoi.h create mode 100644 libavcodec/qoi_parser.c create mode 100644 libavcodec/qoidec.c create mode 100644 libavcodec/qoienc.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index e6eb8c0854..baf295a58b 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -594,6 +594,8 @@ OBJS-$(CONFIG_QCELP_DECODER) += qcelpdec.o \ OBJS-$(CONFIG_QDM2_DECODER) += qdm2.o OBJS-$(CONFIG_QDMC_DECODER) += qdmc.o OBJS-$(CONFIG_QDRAW_DECODER) += qdrw.o +OBJS-$(CONFIG_QOI_DECODER) += qoidec.o +OBJS-$(CONFIG_QOI_ENCODER) += qoienc.o OBJS-$(CONFIG_QPEG_DECODER) += qpeg.o OBJS-$(CONFIG_QTRLE_DECODER) += qtrle.o OBJS-$(CONFIG_QTRLE_ENCODER) += qtrleenc.o @@ -1152,6 +1154,7 @@ OBJS-$(CONFIG_OPUS_PARSER) += opus_parser.o opus.o opustab.o \ opus_rc.o vorbis_data.o OBJS-$(CONFIG_PNG_PARSER) += png_parser.o OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o +OBJS-$(CONFIG_QOI_PARSER) += qoi_parser.o OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o OBJS-$(CONFIG_RV40_PARSER) += rv34_parser.o OBJS-$(CONFIG_SBC_PARSER) += sbc_parser.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 3ae41827a2..d95aa35ea2 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -270,6 +270,8 @@ extern const FFCodec ff_prosumer_decoder; extern const FFCodec ff_psd_decoder; extern const FFCodec ff_ptx_decoder; extern const FFCodec ff_qdraw_decoder; +extern const FFCodec ff_qoi_encoder; +extern const FFCodec ff_qoi_decoder; extern const FFCodec ff_qpeg_decoder; extern const FFCodec ff_qtrle_encoder; extern const FFCodec ff_qtrle_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 3bcf22e6e7..dd877ca0bd 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1886,6 +1886,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("Bink video 2"), .props = AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_QOI, + .type = AVMEDIA_TYPE_VIDEO, + .name = "qoi", + .long_name = NULL_IF_CONFIG_SMALL("QOI (Quite OK Image)"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index 03234b7543..a716dc87c3 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -311,6 +311,7 @@ enum AVCodecID { AV_CODEC_ID_VBN, AV_CODEC_ID_JPEGXL, AV_CODEC_ID_BINKVIDEO2, + AV_CODEC_ID_QOI, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c index 6b40c18d80..b59388835d 100644 --- a/libavcodec/parsers.c +++ b/libavcodec/parsers.c @@ -60,6 +60,7 @@ extern const AVCodecParser ff_mpegvideo_parser; extern const AVCodecParser ff_opus_parser; extern const AVCodecParser ff_png_parser; extern const AVCodecParser ff_pnm_parser; +extern const AVCodecParser ff_qoi_parser; extern const AVCodecParser ff_rv30_parser; extern const AVCodecParser ff_rv40_parser; extern const AVCodecParser ff_sbc_parser; diff --git a/libavcodec/qoi.h b/libavcodec/qoi.h new file mode 100644 index 0000000000..3efab1e55b --- /dev/null +++ b/libavcodec/qoi.h @@ -0,0 +1,35 @@ +/* + * QOI image format + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_QOI_H +#define AVCODEC_QOI_H + +#define QOI_OP_INDEX 0x00 /* 00xxxxxx */ +#define QOI_OP_DIFF 0x40 /* 01xxxxxx */ +#define QOI_OP_LUMA 0x80 /* 10xxxxxx */ +#define QOI_OP_RUN 0xc0 /* 11xxxxxx */ +#define QOI_OP_RGB 0xfe /* 11111110 */ +#define QOI_OP_RGBA 0xff /* 11111111 */ + +#define QOI_MASK_2 0xc0 /* 11000000 */ + +#define QOI_COLOR_HASH(px) (px[0]*3 + px[1]*5 + px[2]*7 + px[3]*11) + +#endif /* AVCODEC_QOI_H */ diff --git a/libavcodec/qoi_parser.c b/libavcodec/qoi_parser.c new file mode 100644 index 0000000000..e5af11e948 --- /dev/null +++ b/libavcodec/qoi_parser.c @@ -0,0 +1,77 @@ +/* + * QOI parser + * Copyright (c) 2022 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * QOI parser + */ + +#include "parser.h" + +typedef struct QOIParseContext { + ParseContext pc; +} QOIParseContext; + +static int qoi_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + QOIParseContext *ipc = s->priv_data; + uint64_t state = ipc->pc.state64; + int next = END_NOT_FOUND, i = 0; + + s->pict_type = AV_PICTURE_TYPE_NONE; + s->duration = 1; + + *poutbuf_size = 0; + *poutbuf = NULL; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { + next = buf_size; + } else { + for (; i < buf_size; i++) { + state = (state << 8) | buf[i]; + if (state == 0x01LL) { + next = i + 1; + break; + } + } + + ipc->pc.state64 = state; + if (ff_combine_frame(&ipc->pc, next, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + + return next; +} + +const AVCodecParser ff_qoi_parser = { + .codec_ids = { AV_CODEC_ID_QOI }, + .priv_data_size = sizeof(QOIParseContext), + .parser_parse = qoi_parse, + .parser_close = ff_parse_close, +}; diff --git a/libavcodec/qoidec.c b/libavcodec/qoidec.c new file mode 100644 index 0000000000..8a119f7606 --- /dev/null +++ b/libavcodec/qoidec.c @@ -0,0 +1,126 @@ +/* + * QOI image format + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "libavutil/imgutils.h" +#include "avcodec.h" +#include "internal.h" +#include "bytestream.h" +#include "codec_internal.h" +#include "thread.h" +#include "qoi.h" + +static int qoi_decode_frame(AVCodecContext *avctx, AVFrame *p, + int *got_frame, AVPacket *avpkt) +{ + const uint8_t *buf = avpkt->data; + int ret, buf_size = avpkt->size; + int width, height, channels, space, run = 0; + uint8_t index[64][4] = { 0 }; + uint8_t px[4] = { 0, 0, 0, 255 }; + GetByteContext gb; + uint8_t *dst; + uint64_t len; + + if (buf_size < 20) + return AVERROR_INVALIDDATA; + + bytestream2_init(&gb, buf, buf_size); + bytestream2_skip(&gb, 4); + width = bytestream2_get_be32(&gb); + height = bytestream2_get_be32(&gb); + channels = bytestream2_get_byte(&gb); + space = bytestream2_get_byte(&gb); + switch (space) { + case 0: break; + case 1: avctx->color_trc = AVCOL_TRC_LINEAR; break; + default: return AVERROR_INVALIDDATA; + } + + if ((ret = ff_set_dimensions(avctx, width, height)) < 0) + return ret; + + switch (channels) { + case 3: avctx->pix_fmt = AV_PIX_FMT_RGB24; break; + case 4: avctx->pix_fmt = AV_PIX_FMT_RGBA; break; + default: return AVERROR_INVALIDDATA; + } + + if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0) + return ret; + + dst = p->data[0]; + len = width * height * channels; + for (int n = 0, off_x = 0, off_y = 0; n < len; n += channels, off_x++) { + if (off_x >= width) { + off_x = 0; + off_y++; + dst += p->linesize[0]; + } + if (run > 0) { + run--; + } else if (bytestream2_get_bytes_left(&gb) > 4) { + int chunk = bytestream2_get_byteu(&gb); + + if (chunk == QOI_OP_RGB) { + bytestream2_get_bufferu(&gb, px, 3); + } else if (chunk == QOI_OP_RGBA) { + bytestream2_get_bufferu(&gb, px, 4); + } else if ((chunk & QOI_MASK_2) == QOI_OP_INDEX) { + memcpy(px, index[chunk], 4); + } else if ((chunk & QOI_MASK_2) == QOI_OP_DIFF) { + px[0] += ((chunk >> 4) & 0x03) - 2; + px[1] += ((chunk >> 2) & 0x03) - 2; + px[2] += ( chunk & 0x03) - 2; + } else if ((chunk & QOI_MASK_2) == QOI_OP_LUMA) { + int b2 = bytestream2_get_byteu(&gb); + int vg = (chunk & 0x3f) - 32; + px[0] += vg - 8 + ((b2 >> 4) & 0x0f); + px[1] += vg; + px[2] += vg - 8 + (b2 & 0x0f); + } else if ((chunk & QOI_MASK_2) == QOI_OP_RUN) { + run = chunk & 0x3f; + } + + memcpy(index[QOI_COLOR_HASH(px) & 63], px, 4); + } else { + break; + } + + memcpy(&dst[off_x * channels], px, channels); + } + + p->key_frame = 1; + p->pict_type = AV_PICTURE_TYPE_I; + + *got_frame = 1; + + return buf_size; +} + +const FFCodec ff_qoi_decoder = { + .p.name = "qoi", + .p.long_name = NULL_IF_CONFIG_SMALL("QOI (Quite OK Image format) image"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_QOI, + .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, + FF_CODEC_DECODE_CB(qoi_decode_frame), +}; diff --git a/libavcodec/qoienc.c b/libavcodec/qoienc.c new file mode 100644 index 0000000000..9832c5c767 --- /dev/null +++ b/libavcodec/qoienc.c @@ -0,0 +1,140 @@ +/* + * QOI image format encoder + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/imgutils.h" +#include "avcodec.h" +#include "bytestream.h" +#include "codec_internal.h" +#include "encode.h" +#include "qoi.h" + +static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *pict, int *got_packet) +{ + const int channels = 3 + (avctx->pix_fmt == AV_PIX_FMT_RGBA); + uint8_t px_prev[4] = { 0, 0, 0, 255 }; + uint8_t px[4] = { 0, 0, 0, 255 }; + uint8_t index[64][4] = { 0 }; + int64_t packet_size; + uint8_t *buf, *src; + int ret, run = 0; + + packet_size = avctx->width * avctx->height * 16LL; + if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0) + return ret; + + buf = pkt->data; + src = pict->data[0]; + + bytestream_put_buffer(&buf, "qoif", 4); + bytestream_put_be32(&buf, avctx->width); + bytestream_put_be32(&buf, avctx->height); + bytestream_put_byte(&buf, channels); + bytestream_put_byte(&buf, 0); + + for (int y = 0; y < avctx->height; y++) { + for (int x = 0; x < avctx->width; x++) { + memcpy(px, src + x * channels, channels); + + if (!memcmp(px, px_prev, 4)) { + run++; + if (run == 62) { + bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1)); + run = 0; + } + } else { + int index_pos; + + if (run > 0) { + bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1)); + run = 0; + } + + index_pos = QOI_COLOR_HASH(px) & 63; + + if (!memcmp(index[index_pos], px, 4)) { + bytestream_put_byte(&buf, QOI_OP_INDEX | index_pos); + } else { + memcpy(index[index_pos], px, 4); + + if (px[3] == px_prev[3]) { + int8_t vr = px[0] - px_prev[0]; + int8_t vg = px[1] - px_prev[1]; + int8_t vb = px[2] - px_prev[2]; + + int8_t vg_r = vr - vg; + int8_t vg_b = vb - vg; + + if (vr > -3 && vr < 2 && + vg > -3 && vg < 2 && + vb > -3 && vb < 2) { + bytestream_put_byte(&buf, QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2)); + } else if (vg_r > -9 && vg_r < 8 && + vg > -33 && vg < 32 && + vg_b > -9 && vg_b < 8) { + bytestream_put_byte(&buf, QOI_OP_LUMA | (vg + 32)); + bytestream_put_byte(&buf, (vg_r + 8) << 4 | (vg_b + 8)); + } else { + bytestream_put_byte(&buf, QOI_OP_RGB); + bytestream_put_byte(&buf, px[0]); + bytestream_put_byte(&buf, px[1]); + bytestream_put_byte(&buf, px[2]); + } + } else { + bytestream_put_byte(&buf, QOI_OP_RGBA); + bytestream_put_byte(&buf, px[0]); + bytestream_put_byte(&buf, px[1]); + bytestream_put_byte(&buf, px[2]); + bytestream_put_byte(&buf, px[3]); + } + } + } + + memcpy(px_prev, px, 4); + } + + src += pict->linesize[0]; + } + + if (run) + bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1)); + + bytestream_put_be64(&buf, 0x01); + + pkt->size = buf - pkt->data; + + *got_packet = 1; + + return 0; +} + +const FFCodec ff_qoi_encoder = { + .p.name = "qoi", + .p.long_name = NULL_IF_CONFIG_SMALL("QOI (Quite OK Image format) image"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_QOI, + .p.capabilities = AV_CODEC_CAP_FRAME_THREADS, + FF_CODEC_ENCODE_CB(qoi_encode_frame), + .p.pix_fmts = (const enum AVPixelFormat[]){ + AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24, + AV_PIX_FMT_NONE + }, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, +}; diff --git a/libavformat/Makefile b/libavformat/Makefile index 8e612b6cc7..1416bf31bd 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -289,6 +289,7 @@ OBJS-$(CONFIG_IMAGE_PNG_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_PPM_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_PSD_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_QDRAW_PIPE_DEMUXER) += img2dec.o img2.o +OBJS-$(CONFIG_IMAGE_QOI_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_SGI_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_SVG_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_SUNRAST_PIPE_DEMUXER) += img2dec.o img2.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 1802536633..8b84b52c64 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -524,6 +524,7 @@ extern const AVInputFormat ff_image_png_pipe_demuxer; extern const AVInputFormat ff_image_ppm_pipe_demuxer; extern const AVInputFormat ff_image_psd_pipe_demuxer; extern const AVInputFormat ff_image_qdraw_pipe_demuxer; +extern const AVInputFormat ff_image_qoi_pipe_demuxer; extern const AVInputFormat ff_image_sgi_pipe_demuxer; extern const AVInputFormat ff_image_svg_pipe_demuxer; extern const AVInputFormat ff_image_sunrast_pipe_demuxer; diff --git a/libavformat/img2.c b/libavformat/img2.c index 566ef873ca..68cb7de2c1 100644 --- a/libavformat/img2.c +++ b/libavformat/img2.c @@ -89,6 +89,7 @@ const IdStrMap ff_img_tags[] = { { AV_CODEC_ID_GEM, "timg" }, { AV_CODEC_ID_VBN, "vbn" }, { AV_CODEC_ID_JPEGXL, "jxl" }, + { AV_CODEC_ID_QOI, "qoi" }, { AV_CODEC_ID_NONE, NULL } }; diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index 5f9d1f094f..e4912cb487 100644 --- a/libavformat/img2dec.c +++ b/libavformat/img2dec.c @@ -1131,6 +1131,23 @@ static int photocd_probe(const AVProbeData *p) return AVPROBE_SCORE_MAX - 1; } +static int qoi_probe(const AVProbeData *p) +{ + if (memcmp(p->buf, "qoif", 4)) + return 0; + + if (AV_RB32(p->buf + 4) == 0 || AV_RB32(p->buf + 8) == 0) + return 0; + + if (p->buf[12] != 3 && p->buf[12] != 4) + return 0; + + if (p->buf[13] > 1) + return 0; + + return AVPROBE_SCORE_MAX - 1; +} + static int gem_probe(const AVProbeData *p) { const uint8_t *b = p->buf; @@ -1208,6 +1225,7 @@ IMAGEAUTO_DEMUXER(png, PNG) IMAGEAUTO_DEMUXER(ppm, PPM) IMAGEAUTO_DEMUXER(psd, PSD) IMAGEAUTO_DEMUXER(qdraw, QDRAW) +IMAGEAUTO_DEMUXER(qoi, QOI) IMAGEAUTO_DEMUXER(sgi, SGI) IMAGEAUTO_DEMUXER(sunrast, SUNRAST) IMAGEAUTO_DEMUXER(svg, SVG) diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c index 0015297ec2..b3a0801ec9 100644 --- a/libavformat/img2enc.c +++ b/libavformat/img2enc.c @@ -267,7 +267,7 @@ const AVOutputFormat ff_image2_muxer = { .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"), .extensions = "bmp,dpx,exr,jls,jpeg,jpg,jxl,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv," "png,ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8," - "im24,sunras,vbn,xbm,xface,pix,y,avif", + "im24,sunras,vbn,xbm,xface,pix,y,avif,qoi", .priv_data_size = sizeof(VideoMuxData), .video_codec = AV_CODEC_ID_MJPEG, .write_header = write_header, -- 2.36.1