summaryrefslogtreecommitdiff
path: root/libavcodec
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2022-05-31 12:33:54 +0200
committerPaul B Mahol <onemda@gmail.com>2022-06-05 13:06:54 +0200
commit973fab565378cbdd0712977152a66f5b17938d51 (patch)
tree720f0b9c6e8f3cf64c4134d0ae78a5e30d97a577 /libavcodec
parentc6364b711bad1fe2fbd90e5b2798f87080ddf5ea (diff)
avcodec: add QOI decoder and demuxer and parser and encoder and muxer
Diffstat (limited to 'libavcodec')
-rw-r--r--libavcodec/Makefile3
-rw-r--r--libavcodec/allcodecs.c2
-rw-r--r--libavcodec/codec_desc.c7
-rw-r--r--libavcodec/codec_id.h1
-rw-r--r--libavcodec/parsers.c1
-rw-r--r--libavcodec/qoi.h35
-rw-r--r--libavcodec/qoi_parser.c77
-rw-r--r--libavcodec/qoidec.c126
-rw-r--r--libavcodec/qoienc.c140
-rw-r--r--libavcodec/version.h4
10 files changed, 394 insertions, 2 deletions
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 38425d2f22..3b8f7b5e01 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -593,6 +593,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
@@ -1151,6 +1153,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 c47133aa18..f0b01051b0 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -269,6 +269,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 e7f0f6a8d4..e2c1c67f5e 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1879,6 +1879,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
AV_CODEC_PROP_LOSSLESS,
.mime_types= MT("image/jxl"),
},
+ {
+ .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 8b317fa121..93856a16f2 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -310,6 +310,7 @@ enum AVCodecID {
AV_CODEC_ID_GEM,
AV_CODEC_ID_VBN,
AV_CODEC_ID_JPEGXL,
+ 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 <stdlib.h>
+
+#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..bd137674a4
--- /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 * (channels + 1LL) + 14LL + 8LL;
+ 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, avctx->color_trc == AVCOL_TRC_LINEAR);
+
+ 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/libavcodec/version.h b/libavcodec/version.h
index d6f1440d54..2a08e42d7e 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,8 +29,8 @@
#include "version_major.h"
-#define LIBAVCODEC_VERSION_MINOR 32
-#define LIBAVCODEC_VERSION_MICRO 102
+#define LIBAVCODEC_VERSION_MINOR 33
+#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \