diff mbox series

[FFmpeg-devel,6/8] avcodec/vbndec: add VBN decoder

Message ID 20220330203205.25937-6-cus@passwd.hu
State New
Headers show
Series [FFmpeg-devel,1/8] fate/filter-refcmp-*: make refcmp_metadata fail on empty input | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished

Commit Message

Marton Balint March 30, 2022, 8:32 p.m. UTC
Add support for decoding Vizrt Binary Image (VBN) files.

LZW-compressed data is not supported yet.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 Changelog                |   1 +
 configure                |   1 +
 libavcodec/Makefile      |   1 +
 libavcodec/allcodecs.c   |   1 +
 libavcodec/codec_desc.c  |   7 ++
 libavcodec/codec_id.h    |   1 +
 libavcodec/vbn.h         |  50 ++++++++++
 libavcodec/vbndec.c      | 196 +++++++++++++++++++++++++++++++++++++++
 libavformat/allformats.c |   1 +
 libavformat/img2.c       |   1 +
 libavformat/img2dec.c    |  12 +++
 11 files changed, 272 insertions(+)
 create mode 100644 libavcodec/vbn.h
 create mode 100644 libavcodec/vbndec.c
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index ac614f8f05..7bad88dabc 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@  version 5.1:
 - dropped obsolete XvMC hwaccel
 - pcm-bluray encoder
 - DFPWM audio encoder/decoder and raw muxer/demuxer
+- Vizrt Binary Image decoder
 
 
 version 5.0:
diff --git a/configure b/configure
index e4d36aa639..90c99ff85e 100755
--- a/configure
+++ b/configure
@@ -2959,6 +2959,7 @@  txd_decoder_select="texturedsp"
 utvideo_decoder_select="bswapdsp llviddsp"
 utvideo_encoder_select="bswapdsp huffman llvidencdsp"
 vble_decoder_select="llviddsp"
+vbn_decoder_select="texturedsp"
 vc1_decoder_select="blockdsp h263_decoder h264qpel intrax8 mpegvideodec vc1dsp"
 vc1image_decoder_select="vc1_decoder"
 vorbis_decoder_select="mdct"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index fb8b0e824b..90700085b8 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -708,6 +708,7 @@  OBJS-$(CONFIG_V408_ENCODER)            += v408enc.o
 OBJS-$(CONFIG_V410_DECODER)            += v410dec.o
 OBJS-$(CONFIG_V410_ENCODER)            += v410enc.o
 OBJS-$(CONFIG_VB_DECODER)              += vb.o
+OBJS-$(CONFIG_VBN_DECODER)             += vbndec.o
 OBJS-$(CONFIG_VBLE_DECODER)            += vble.o
 OBJS-$(CONFIG_VC1_DECODER)             += vc1dec.o vc1_block.o vc1_loopfilter.o \
                                           vc1_mc.o vc1_pred.o vc1.o vc1data.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 22d56760ec..f0a7ea7fd4 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -347,6 +347,7 @@  extern const FFCodec ff_v408_decoder;
 extern const FFCodec ff_v410_encoder;
 extern const FFCodec ff_v410_decoder;
 extern const FFCodec ff_vb_decoder;
+extern const FFCodec ff_vbn_decoder;
 extern const FFCodec ff_vble_decoder;
 extern const FFCodec ff_vc1_decoder;
 extern const FFCodec ff_vc1_crystalhd_decoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 81f3b3c640..c08854cc93 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1863,6 +1863,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("GEM Raster image"),
         .props     = AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_VBN,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "vbn",
+        .long_name = NULL_IF_CONFIG_SMALL("Vizrt Binary Image"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
 
     /* various PCM "codecs" */
     {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index 3ffb9bd22e..43c72ce8e4 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -308,6 +308,7 @@  enum AVCodecID {
     AV_CODEC_ID_SIMBIOSIS_IMX,
     AV_CODEC_ID_SGA_VIDEO,
     AV_CODEC_ID_GEM,
+    AV_CODEC_ID_VBN,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/vbn.h b/libavcodec/vbn.h
new file mode 100644
index 0000000000..8660786de8
--- /dev/null
+++ b/libavcodec/vbn.h
@@ -0,0 +1,50 @@ 
+/*
+ * VBN format definitions
+ *
+ * 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
+ * VBN format definitions.
+ */
+
+#ifndef AVCODEC_VBN_H
+#define AVCODEC_VBN_H
+
+#define VBN_MAGIC          0x900df11e
+#define VBN_MAJOR                   3
+#define VBN_MINOR                   4
+
+#define VBN_HEADER_SIZE           192
+
+#define VBN_FORMAT_RAW              0
+#define VBN_FORMAT_LZ               1
+#define VBN_FORMAT_DXT1             2
+#define VBN_FORMAT_DXT5             3
+
+#define VBN_COMPRESSION_NONE        0
+#define VBN_COMPRESSION_LZW     0x100
+
+#define VBN_PIX_ALPHA               0
+#define VBN_PIX_LUMINANCE           1
+#define VBN_PIX_LUMINANCE_ALPHA     2
+#define VBN_PIX_RGB                 3
+#define VBN_PIX_RGBA                5
+#define VBN_PIX_INDEX               6
+
+#endif /* AVCODEC_VBN_H */
diff --git a/libavcodec/vbndec.c b/libavcodec/vbndec.c
new file mode 100644
index 0000000000..1119059c55
--- /dev/null
+++ b/libavcodec/vbndec.c
@@ -0,0 +1,196 @@ 
+/*
+ * Vizrt Binary Image decoder
+ *
+ * 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
+ * Vizrt Binary Image decoder
+ */
+
+#include "avcodec.h"
+#include "bytestream.h"
+#include "codec_internal.h"
+#include "internal.h"
+#include "texturedsp.h"
+#include "vbn.h"
+#include "libavutil/imgutils.h"
+
+typedef struct VBNContext {
+    TextureDSPContext texdsp;
+    TextureDSPThreadContext dec;
+    GetByteContext gb;
+} VBNContext;
+
+static av_cold int vbn_init(AVCodecContext *avctx)
+{
+    VBNContext *ctx = avctx->priv_data;
+    ff_texturedsp_init(&ctx->texdsp);
+    return 0;
+}
+
+static int decompress(AVCodecContext *avctx, int compression, uint8_t **outbuf)
+{
+    VBNContext *ctx = avctx->priv_data;
+    GetByteContext *gb = &ctx->gb;
+
+    if (compression == VBN_COMPRESSION_NONE) // outbuf is left NULL because gb->buf can be used directly
+        return bytestream2_get_bytes_left(gb);
+
+    av_log(avctx, AV_LOG_ERROR, "Unsupported VBN compression: 0x%08x\n", compression);
+    return AVERROR_PATCHWELCOME;
+}
+
+static int vbn_decode_frame(AVCodecContext *avctx,
+                            void *data, int *got_frame,
+                            AVPacket *avpkt)
+{
+    VBNContext *ctx    = avctx->priv_data;
+    GetByteContext *gb = &ctx->gb;
+    AVFrame *frame     = data;
+    uint8_t *image_buf = NULL;
+    int      image_len;
+    int width, height, components, format, compression, pix_fmt, linesize, data_size;
+    int ret;
+
+    bytestream2_init(gb, avpkt->data, avpkt->size);
+
+    if (bytestream2_get_bytes_left(gb) < VBN_HEADER_SIZE) {
+        av_log(avctx, AV_LOG_ERROR, "VBN header truncated\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (bytestream2_get_le32(gb) != VBN_MAGIC ||
+        bytestream2_get_le32(gb) != VBN_MAJOR ||
+        bytestream2_get_le32(gb) != VBN_MINOR) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid VBN header\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    width       = bytestream2_get_le32(gb);
+    height      = bytestream2_get_le32(gb);
+    components  = bytestream2_get_le32(gb);
+    format      = bytestream2_get_le32(gb);
+    pix_fmt     = bytestream2_get_le32(gb);
+    bytestream2_get_le32(gb); // mipmaps
+    data_size   = bytestream2_get_le32(gb);
+    bytestream2_seek(gb, VBN_HEADER_SIZE, SEEK_SET);
+
+    compression = format & 0xffffff00;
+    format      = format & 0xff;
+
+    if (data_size != bytestream2_get_bytes_left(gb)) {
+        av_log(avctx, AV_LOG_ERROR, "Truncated packet\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (pix_fmt != VBN_PIX_RGBA && pix_fmt != VBN_PIX_RGB) {
+        av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: 0x%08x\n", pix_fmt);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    ret = ff_set_dimensions(avctx, width, height);
+    if (ret < 0)
+        return ret;
+
+    if (format == VBN_FORMAT_RAW) {
+        if (pix_fmt == VBN_PIX_RGB && components == 3) {
+             avctx->pix_fmt = AV_PIX_FMT_RGB24;
+             linesize = avctx->width * 3;
+        } else if (pix_fmt == VBN_PIX_RGBA && components == 4) {
+             avctx->pix_fmt = AV_PIX_FMT_RGBA;
+             linesize = avctx->width * 4;
+        } else {
+            av_log(avctx, AV_LOG_ERROR, "Unsupported number of components: %d\n", components);
+            return AVERROR_PATCHWELCOME;
+        }
+    } else if (format == VBN_FORMAT_DXT1 || format == VBN_FORMAT_DXT5)  {
+        if (avctx->width % TEXTURE_BLOCK_W || avctx->height % TEXTURE_BLOCK_H) {
+            av_log(avctx, AV_LOG_ERROR, "DXTx compression only supports 4 pixel aligned resolutions\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        avctx->pix_fmt = AV_PIX_FMT_RGBA;
+        if (format == VBN_FORMAT_DXT1) {
+            ctx->dec.tex_funct = ctx->texdsp.dxt1_block;
+            ctx->dec.tex_ratio = 8;
+            linesize = avctx->coded_width / 2;
+        } else {
+            ctx->dec.tex_funct = ctx->texdsp.dxt5_block;
+            ctx->dec.tex_ratio = 16;
+            linesize = avctx->coded_width;
+        }
+    } else {
+        av_log(avctx, AV_LOG_ERROR, "Unsupported VBN format: 0x%02x\n", format);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    image_len = decompress(avctx, compression, &image_buf);
+    if (image_len < 0)
+        return image_len;
+
+    if (image_len < linesize * avctx->coded_height) {
+        av_log(avctx, AV_LOG_ERROR, "Insufficent data\n");
+        ret = AVERROR_INVALIDDATA;
+        goto out;
+    }
+
+    ret = ff_get_buffer(avctx, frame, 0);
+    if (ret < 0)
+        goto out;
+
+    frame->pict_type = AV_PICTURE_TYPE_I;
+    frame->key_frame = 1;
+
+    if (format == VBN_FORMAT_RAW) {
+        uint8_t *flipped = frame->data[0] + frame->linesize[0] * (frame->height - 1);
+        av_image_copy_plane(flipped, -frame->linesize[0], image_buf ? image_buf : gb->buffer, linesize, linesize, frame->height);
+    } else {
+        ctx->dec.slice_count = av_clip(avctx->thread_count, 1, avctx->coded_height / TEXTURE_BLOCK_H);
+        ctx->dec.tex_data.in = image_buf ? image_buf : gb->buffer;
+        ctx->dec.raw_ratio = 16;
+        ctx->dec.frame_data.out = frame->data[0] + frame->linesize[0] * (frame->height - 1);
+        ctx->dec.stride = -frame->linesize[0];
+        avctx->execute2(avctx, ff_texturedsp_decompress_thread, &ctx->dec, NULL, ctx->dec.slice_count);
+    }
+
+    *got_frame = 1;
+    ret = avpkt->size;
+
+out:
+    av_freep(&image_buf);
+    return ret;
+}
+
+static av_cold int vbn_close(AVCodecContext *avctx)
+{
+    return 0;
+}
+
+const FFCodec ff_vbn_decoder = {
+    .p.name         = "vbn",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("Vizrt Binary Image"),
+    .p.type         = AVMEDIA_TYPE_VIDEO,
+    .p.id           = AV_CODEC_ID_VBN,
+    .init           = vbn_init,
+    .decode         = vbn_decode_frame,
+    .close          = vbn_close,
+    .priv_data_size = sizeof(VBNContext),
+    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE
+};
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 587ad59b3c..7c1d0ac38f 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -526,6 +526,7 @@  extern const AVInputFormat  ff_image_sgi_pipe_demuxer;
 extern const AVInputFormat  ff_image_svg_pipe_demuxer;
 extern const AVInputFormat  ff_image_sunrast_pipe_demuxer;
 extern const AVInputFormat  ff_image_tiff_pipe_demuxer;
+extern const AVInputFormat  ff_image_vbn_pipe_demuxer;
 extern const AVInputFormat  ff_image_webp_pipe_demuxer;
 extern const AVInputFormat  ff_image_xbm_pipe_demuxer;
 extern const AVInputFormat  ff_image_xpm_pipe_demuxer;
diff --git a/libavformat/img2.c b/libavformat/img2.c
index 4153102c92..fe2ca7bfff 100644
--- a/libavformat/img2.c
+++ b/libavformat/img2.c
@@ -87,6 +87,7 @@  const IdStrMap ff_img_tags[] = {
     { AV_CODEC_ID_GEM,        "img"      },
     { AV_CODEC_ID_GEM,        "ximg"     },
     { AV_CODEC_ID_GEM,        "timg"     },
+    { AV_CODEC_ID_VBN,        "vbn"      },
     { AV_CODEC_ID_NONE,       NULL       }
 };
 
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index b9c06c5b54..551b9d508e 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -37,6 +37,7 @@ 
 #include "internal.h"
 #include "img2.h"
 #include "libavcodec/mjpeg.h"
+#include "libavcodec/vbn.h"
 #include "libavcodec/xwd.h"
 #include "subtitles.h"
 
@@ -1131,6 +1132,16 @@  static int gem_probe(const AVProbeData *p)
     return 0;
 }
 
+static int vbn_probe(const AVProbeData *p)
+{
+    const uint8_t *b = p->buf;
+    if (AV_RL32(b    ) == VBN_MAGIC &&
+        AV_RL32(b + 4) == VBN_MAJOR &&
+        AV_RL32(b + 8) == VBN_MINOR)
+        return AVPROBE_SCORE_MAX - 1;
+    return 0;
+}
+
 #define IMAGEAUTO_DEMUXER_0(imgname, codecid)
 #define IMAGEAUTO_DEMUXER_1(imgname, codecid)\
 const AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
@@ -1181,6 +1192,7 @@  IMAGEAUTO_DEMUXER(sgi,       SGI)
 IMAGEAUTO_DEMUXER(sunrast,   SUNRAST)
 IMAGEAUTO_DEMUXER(svg,       SVG)
 IMAGEAUTO_DEMUXER(tiff,      TIFF)
+IMAGEAUTO_DEMUXER(vbn,       VBN)
 IMAGEAUTO_DEMUXER(webp,      WEBP)
 IMAGEAUTO_DEMUXER(xbm,       XBM)
 IMAGEAUTO_DEMUXER(xpm,       XPM)