From patchwork Wed Mar 30 20:32:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 35089 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1011304pzb; Wed, 30 Mar 2022 13:33:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxr9H0vkYW9FsSKXPqdO8EWfRUzqKPv54zxVhuogHLL0QXoF+rhG+oDeLotIibd38jH6WQT X-Received: by 2002:a17:907:c16:b0:6da:7d72:1357 with SMTP id ga22-20020a1709070c1600b006da7d721357mr1485428ejc.251.1648672395698; Wed, 30 Mar 2022 13:33:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648672395; cv=none; d=google.com; s=arc-20160816; b=kRBBgH9eUWadMpp2Sk7Dw+ESG9zkN4FGYuqOP5EGGAbgkvISQ8b/bLK9xaszu65OgU fG/NuXlF9PpEel7Fdn/9TOiCK0qROHfxK0RTxvK3Pzck8XdoW+yCF3pJGgIjTGuUMneu 7W4VTUznGnf9/KbFvdOH7UpMsfiHiArCVQcMy7FkIpaCSDeFJvzyMATBKO9aPKnh7m4h VVFBAhJY5yiU2ZzyFVIlryHau4HZiTZ0lhM6O3CxTUfKGaDS7G1D3ZaGQ9Emau6hx0J7 BO/kDHOMJJKN0cRKDlhHYP7lKCHBHKyIUhmhUkYgCkMWp390r6R+d6BenYzLRd/wsrCB GVmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=rwe/X7VTIi8EZN9DmN9InMr5KOA2cU8Pw5rYFiAX84U=; b=HGmxKpNITO/mE5TybuL/iIbtVQwx8WZiJkuIdt1FLOOAEb5JzOfEIz0Mrps0cwAImK w8/QLe4DxfcdTLXEMlutvZ/YH9/6FqGzMSUt5PFVy57Px7sCGpPNNrGnylXEjiiMfcg5 FbsmZ5RKa9SSTJ5ExmaLoeqp96w4TWJIIANY7BoeFwEFibBMQ2dpYVX+Mnnyc5U1SW9y bWyJvuhYzjptB3arhqZzbdmjfdZtEy/hY1bvMXbLoBUkkUZNbRnx93xzOv/Pt5pdrG0R yYMBVZZ1knmi/KRlpSaBLbfl43qyp3WQN5ohlKSeiAUAtXBcaRVpp04tUpav8ebLf6YL i6pA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e22-20020a1709067e1600b006df76385c4dsi20589032ejr.237.2022.03.30.13.33.07; Wed, 30 Mar 2022 13:33:15 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EEC5768B284; Wed, 30 Mar 2022 23:32:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62C7168B26B for ; Wed, 30 Mar 2022 23:32:24 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id E7250E67D7; Wed, 30 Mar 2022 22:32:24 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gJYZSATvYV9i; Wed, 30 Mar 2022 22:32:21 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 70A46E6D0D; Wed, 30 Mar 2022 22:32:20 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Wed, 30 Mar 2022 22:32:03 +0200 Message-Id: <20220330203205.25937-6-cus@passwd.hu> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220330203205.25937-1-cus@passwd.hu> References: <20220330203205.25937-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/8] avcodec/vbndec: add VBN decoder 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 Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: klfY3o5kxZDJ Add support for decoding Vizrt Binary Image (VBN) files. LZW-compressed data is not supported yet. Signed-off-by: Marton Balint --- 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 --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)