History

avcodec/vbnenc: add VBN encoder

Marton Balint committed 4 years ago

Commit ee50cc11 parent 013d77410 files · +200 −6Browse files

Unified · Split

10 files
  1. Changelog +1 −1
  2. configure +1 −0
  3. doc/encoders.texi +16 −0
  4. doc/general_contents.texi +2 −0
  5. libavcodec/Makefile +1 −0
  6. libavcodec/allcodecs.c +1 −0
  7. libavcodec/vbnenc.c +173 −0
  8. libavcodec/version.h +2 −2
  9. libavformat/img2enc.c +1 −1
  10. libavformat/version.h +2 −2
Changelog+1−1
@@ -7,7 +7,7 @@ version 5.1: · 6 unmodified lines
77 - pcm-bluray encoder
88 - DFPWM audio encoder/decoder and raw muxer/demuxer
99 - SITI filter
10−- Vizrt Binary Image decoder
10+- Vizrt Binary Image encoder/decoder
1111
1212
1313 version 5.0:
configure+1−0
@@ -2960,6 +2960,7 @@ utvideo_decoder_select="bswapdsp llviddsp" · 2959 unmodified lines
29602960 utvideo_encoder_select="bswapdsp huffman llvidencdsp"
29612961 vble_decoder_select="llviddsp"
29622962 vbn_decoder_select="texturedsp"
2963+vbn_encoder_select="texturedspenc"
29632964 vc1_decoder_select="blockdsp h263_decoder h264qpel intrax8 mpegvideodec vc1dsp"
29642965 vc1image_decoder_select="vc1_decoder"
29652966 vorbis_decoder_select="mdct"
doc/encoders.texi+16−0
@@ -3773,6 +3773,22 @@ required to produce a stream usable with all decoders. · 3772 unmodified lines
37733773
37743774 @end table
37753775
3776+@section vbn
3777+
3778+Vizrt Binary Image encoder.
3779+
3780+This format is used by the broadcast vendor Vizrt for quick texture streaming.
3781+Advanced features of the format such as LZW compression of texture data or
3782+generation of mipmaps are not supported.
3783+
3784+@subsection Options
3785+
3786+@table @option
3787+@item format @var{string}
3788+Sets the texture compression used by the VBN file. Can be @var{dxt1},
3789+@var{dxt5} or @var{raw}. Default is @var{dxt5}.
3790+@end table
3791+
37763792 @section vc2
37773793
37783794 SMPTE VC-2 (previously BBC Dirac Pro). This codec was primarily aimed at
doc/general_contents.texi+2−0
@@ -786,6 +786,8 @@ following image formats are supported: · 785 unmodified lines
786786 @tab YUV, JPEG and some extension is not supported yet.
787787 @item Truevision Targa @tab X @tab X
788788 @tab Targa (.TGA) image format
789+@item VBN @tab X @tab X
790+ @tab Vizrt Binary Image format
789791 @item WebP @tab E @tab X
790792 @tab WebP image format, encoding supported through external library libwebp
791793 @item XBM @tab X @tab X
libavcodec/Makefile+1−0
@@ -709,6 +709,7 @@ OBJS-$(CONFIG_V410_DECODER) += v410dec.o · 708 unmodified lines
709709 OBJS-$(CONFIG_V410_ENCODER) += v410enc.o
710710 OBJS-$(CONFIG_VB_DECODER) += vb.o
711711 OBJS-$(CONFIG_VBN_DECODER) += vbndec.o
712+OBJS-$(CONFIG_VBN_ENCODER) += vbnenc.o
712713 OBJS-$(CONFIG_VBLE_DECODER) += vble.o
713714 OBJS-$(CONFIG_VC1_DECODER) += vc1dec.o vc1_block.o vc1_loopfilter.o \
714715 vc1_mc.o vc1_pred.o vc1.o vc1data.o \
libavcodec/allcodecs.c+1−0
@@ -347,6 +347,7 @@ extern const FFCodec ff_v408_decoder; · 346 unmodified lines
347347 extern const FFCodec ff_v410_encoder;
348348 extern const FFCodec ff_v410_decoder;
349349 extern const FFCodec ff_vb_decoder;
350+extern const FFCodec ff_vbn_encoder;
350351 extern const FFCodec ff_vbn_decoder;
351352 extern const FFCodec ff_vble_decoder;
352353 extern const FFCodec ff_vc1_decoder;
libavcodec/vbnenc.c+173−0
@@ -0,0 +1,173 @@
1+/*
2+ * Vizrt Binary Image encoder
3+ *
4+ * This file is part of FFmpeg.
5+ *
6+ * FFmpeg is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * FFmpeg is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with FFmpeg; if not, write to the Free Software
18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+ */
20+
21+/**
22+ * @file
23+ * Vizrt Binary Image encoder
24+ */
25+
26+#include "avcodec.h"
27+#include "bytestream.h"
28+#include "codec_internal.h"
29+#include "encode.h"
30+#include "texturedsp.h"
31+#include "vbn.h"
32+
33+#include "libavutil/imgutils.h"
34+#include "libavutil/frame.h"
35+#include "libavutil/opt.h"
36+
37+typedef struct VBNContext {
38+ AVClass *class;
39+ TextureDSPContext dxtc;
40+ PutByteContext pb;
41+ int format;
42+ TextureDSPThreadContext enc;
43+} VBNContext;
44+
45+static int vbn_encode(AVCodecContext *avctx, AVPacket *pkt,
46+ const AVFrame *frame, int *got_packet)
47+{
48+ VBNContext *ctx = avctx->priv_data;
49+ PutByteContext *pb = &ctx->pb;
50+ int ret;
51+ ptrdiff_t linesize;
52+ int64_t pkt_size;
53+
54+ ret = av_image_check_size2(frame->width, frame->height, INT_MAX, frame->format, 0, avctx);
55+ if (ret < 0)
56+ return ret;
57+
58+ if (ctx->format == VBN_FORMAT_DXT1 || ctx->format == VBN_FORMAT_DXT5) {
59+ if (frame->width % TEXTURE_BLOCK_W || frame->height % TEXTURE_BLOCK_H) {
60+ av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4\n", frame->width, frame->height);
61+ return AVERROR(EINVAL);
62+ }
63+ if (frame->format != AV_PIX_FMT_RGBA) {
64+ av_log(avctx, AV_LOG_ERROR, "DXT formats only support RGBA pixel format\n");
65+ return AVERROR(EINVAL);
66+ }
67+ ctx->enc.raw_ratio = 16;
68+ ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H);
69+ }
70+
71+ switch (ctx->format) {
72+ case VBN_FORMAT_DXT1:
73+ linesize = frame->width / 2;
74+ ctx->enc.tex_funct = ctx->dxtc.dxt1_block;
75+ ctx->enc.tex_ratio = 8;
76+ break;
77+ case VBN_FORMAT_DXT5:
78+ linesize = frame->width;
79+ ctx->enc.tex_funct = ctx->dxtc.dxt5_block;
80+ ctx->enc.tex_ratio = 16;
81+ break;
82+ case VBN_FORMAT_RAW:
83+ linesize = av_image_get_linesize(frame->format, frame->width, 0);
84+ if (linesize < 0)
85+ return linesize;
86+ break;
87+ default:
88+ av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->format);
89+ return AVERROR(EINVAL);
90+ }
91+
92+ pkt_size = VBN_HEADER_SIZE + linesize * frame->height;
93+ if (pkt_size > INT_MAX)
94+ return AVERROR(EINVAL);
95+
96+ if ((ret = ff_get_encode_buffer(avctx, pkt, pkt_size, 0)) < 0)
97+ return ret;
98+
99+ memset(pkt->data, 0, VBN_HEADER_SIZE);
100+ bytestream2_init_writer(pb, pkt->data, pkt_size);
101+ bytestream2_put_le32(pb, VBN_MAGIC);
102+ bytestream2_put_le32(pb, VBN_MAJOR);
103+ bytestream2_put_le32(pb, VBN_MINOR);
104+ bytestream2_put_le32(pb, frame->width);
105+ bytestream2_put_le32(pb, frame->height);
106+ bytestream2_put_le32(pb, frame->format == AV_PIX_FMT_RGBA ? 4 : 3);
107+ bytestream2_put_le32(pb, ctx->format);
108+ bytestream2_put_le32(pb, frame->format == AV_PIX_FMT_RGBA ? VBN_PIX_RGBA : VBN_PIX_RGB);
109+ bytestream2_put_le32(pb, 0); // mipmaps
110+ bytestream2_put_le32(pb, pkt_size - VBN_HEADER_SIZE);
111+ bytestream2_seek_p(pb, 64, SEEK_SET);
112+ bytestream2_put_le32(pb, pkt_size - VBN_HEADER_SIZE);
113+
114+ if (ctx->format == VBN_FORMAT_DXT1 || ctx->format == VBN_FORMAT_DXT5) {
115+ ctx->enc.frame_data.in = (frame->height - 1) * frame->linesize[0] + frame->data[0];
116+ ctx->enc.stride = -frame->linesize[0];
117+ ctx->enc.tex_data.out = pkt->data + VBN_HEADER_SIZE;
118+ avctx->execute2(avctx, ff_texturedsp_compress_thread, &ctx->enc, NULL, ctx->enc.slice_count);
119+ } else {
120+ uint8_t *flipped = frame->data[0] + frame->linesize[0] * (frame->height - 1);
121+ av_image_copy_plane(pkt->data + VBN_HEADER_SIZE, linesize, flipped, -frame->linesize[0], linesize, frame->height);
122+ }
123+
124+ *got_packet = 1;
125+ return 0;
126+}
127+
128+static av_cold int vbn_init(AVCodecContext *avctx)
129+{
130+ VBNContext *ctx = avctx->priv_data;
131+ ff_texturedspenc_init(&ctx->dxtc);
132+ return 0;
133+}
134+
135+static av_cold int vbn_close(AVCodecContext *avctx)
136+{
137+ return 0;
138+}
139+
140+#define OFFSET(x) offsetof(VBNContext, x)
141+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
142+static const AVOption options[] = {
143+ { "format", "Texture format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = VBN_FORMAT_DXT5 }, VBN_FORMAT_RAW, VBN_FORMAT_DXT5, FLAGS, "format" },
144+ { "raw", "RAW texture", 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_RAW }, 0, 0, FLAGS, "format" },
145+ { "dxt1", "DXT1 texture", 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_DXT1 }, 0, 0, FLAGS, "format" },
146+ { "dxt5", "DXT5 texture", 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_DXT5 }, 0, 0, FLAGS, "format" },
147+ { NULL },
148+};
149+
150+static const AVClass vbnenc_class = {
151+ .class_name = "VBN encoder",
152+ .item_name = av_default_item_name,
153+ .option = options,
154+ .version = LIBAVUTIL_VERSION_INT,
155+};
156+
157+const FFCodec ff_vbn_encoder = {
158+ .p.name = "vbn",
159+ .p.long_name = NULL_IF_CONFIG_SMALL("Vizrt Binary Image"),
160+ .p.type = AVMEDIA_TYPE_VIDEO,
161+ .p.id = AV_CODEC_ID_VBN,
162+ .p.capabilities = AV_CODEC_CAP_SLICE_THREADS,
163+ .p.priv_class = &vbnenc_class,
164+ .init = vbn_init,
165+ FF_CODEC_ENCODE_CB(vbn_encode),
166+ .close = vbn_close,
167+ .priv_data_size = sizeof(VBNContext),
168+ .p.pix_fmts = (const enum AVPixelFormat[]) {
169+ AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE,
170+ },
171+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
172+ FF_CODEC_CAP_INIT_CLEANUP,
173+};
libavcodec/version.h+2−2
@@ -29,8 +29,8 @@ · 28 unmodified lines
2929
3030 #include "version_major.h"
3131
32−#define LIBAVCODEC_VERSION_MINOR 25
33−#define LIBAVCODEC_VERSION_MICRO 101
32+#define LIBAVCODEC_VERSION_MINOR 26
33+#define LIBAVCODEC_VERSION_MICRO 100
3434
3535 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3636 LIBAVCODEC_VERSION_MINOR, \
libavformat/img2enc.c+1−1
@@ -265,7 +265,7 @@ const AVOutputFormat ff_image2_muxer = { · 264 unmodified lines
265265 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
266266 .extensions = "bmp,dpx,exr,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,png,"
267267 "ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,im24,"
268− "sunras,xbm,xface,pix,y",
268+ "sunras,vbn,xbm,xface,pix,y",
269269 .priv_data_size = sizeof(VideoMuxData),
270270 .video_codec = AV_CODEC_ID_MJPEG,
271271 .write_header = write_header,
libavformat/version.h+2−2
@@ -31,8 +31,8 @@ · 30 unmodified lines
3131
3232 #include "version_major.h"
3333
34−#define LIBAVFORMAT_VERSION_MINOR 20
35−#define LIBAVFORMAT_VERSION_MICRO 101
34+#define LIBAVFORMAT_VERSION_MINOR 21
35+#define LIBAVFORMAT_VERSION_MICRO 100
3636
3737 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
3838 LIBAVFORMAT_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t