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     int format;
41     TextureDSPThreadContext enc;
42 } VBNContext;
43 
vbn_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)44 static int vbn_encode(AVCodecContext *avctx, AVPacket *pkt,
45                       const AVFrame *frame, int *got_packet)
46 {
47     VBNContext *ctx = avctx->priv_data;
48     PutByteContext pb0, *const pb = &pb0;
49     int ret;
50     ptrdiff_t linesize;
51     int64_t pkt_size;
52 
53     ret = av_image_check_size2(frame->width, frame->height, INT_MAX, frame->format, 0, avctx);
54     if (ret < 0)
55         return ret;
56 
57     if (ctx->format == VBN_FORMAT_DXT1 || ctx->format == VBN_FORMAT_DXT5) {
58         if (frame->width % TEXTURE_BLOCK_W || frame->height % TEXTURE_BLOCK_H) {
59             av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4\n", frame->width, frame->height);
60             return AVERROR(EINVAL);
61         }
62         if (frame->format != AV_PIX_FMT_RGBA) {
63             av_log(avctx, AV_LOG_ERROR, "DXT formats only support RGBA pixel format\n");
64             return AVERROR(EINVAL);
65         }
66         ctx->enc.raw_ratio = 16;
67         ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H);
68     }
69 
70     switch (ctx->format) {
71     case VBN_FORMAT_DXT1:
72         linesize = frame->width / 2;
73         ctx->enc.tex_funct = ctx->dxtc.dxt1_block;
74         ctx->enc.tex_ratio = 8;
75         break;
76     case VBN_FORMAT_DXT5:
77         linesize = frame->width;
78         ctx->enc.tex_funct = ctx->dxtc.dxt5_block;
79         ctx->enc.tex_ratio = 16;
80         break;
81     case VBN_FORMAT_RAW:
82         linesize = av_image_get_linesize(frame->format, frame->width, 0);
83         if (linesize < 0)
84             return linesize;
85         break;
86     default:
87         av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->format);
88         return AVERROR(EINVAL);
89     }
90 
91     pkt_size = VBN_HEADER_SIZE + linesize * frame->height;
92     if (pkt_size > INT_MAX)
93         return AVERROR(EINVAL);
94 
95     if ((ret = ff_get_encode_buffer(avctx, pkt, pkt_size, 0)) < 0)
96         return ret;
97 
98     memset(pkt->data, 0, VBN_HEADER_SIZE);
99     bytestream2_init_writer(pb, pkt->data, pkt_size);
100     bytestream2_put_le32u(pb, VBN_MAGIC);
101     bytestream2_put_le32u(pb, VBN_MAJOR);
102     bytestream2_put_le32u(pb, VBN_MINOR);
103     bytestream2_put_le32u(pb, frame->width);
104     bytestream2_put_le32u(pb, frame->height);
105     bytestream2_put_le32u(pb, frame->format == AV_PIX_FMT_RGBA ? 4 : 3);
106     bytestream2_put_le32u(pb, ctx->format);
107     bytestream2_put_le32u(pb, frame->format == AV_PIX_FMT_RGBA ? VBN_PIX_RGBA : VBN_PIX_RGB);
108     bytestream2_put_le32u(pb, 0); // mipmaps
109     bytestream2_put_le32u(pb, pkt_size - VBN_HEADER_SIZE);
110     bytestream2_seek_p(pb, 64, SEEK_SET);
111     bytestream2_put_le32u(pb, pkt_size - VBN_HEADER_SIZE);
112 
113     if (ctx->format == VBN_FORMAT_DXT1 || ctx->format == VBN_FORMAT_DXT5) {
114         ctx->enc.frame_data.in = (frame->height - 1) * frame->linesize[0] + frame->data[0];
115         ctx->enc.stride = -frame->linesize[0];
116         ctx->enc.tex_data.out = pkt->data + VBN_HEADER_SIZE;
117         avctx->execute2(avctx, ff_texturedsp_compress_thread, &ctx->enc, NULL, ctx->enc.slice_count);
118     } else {
119         uint8_t *flipped = frame->data[0] + frame->linesize[0] * (frame->height - 1);
120         av_image_copy_plane(pkt->data + VBN_HEADER_SIZE, linesize, flipped, -frame->linesize[0], linesize, frame->height);
121     }
122 
123     *got_packet = 1;
124     return 0;
125 }
126 
vbn_init(AVCodecContext *avctx)127 static av_cold int vbn_init(AVCodecContext *avctx)
128 {
129     VBNContext *ctx = avctx->priv_data;
130     ff_texturedspenc_init(&ctx->dxtc);
131     return 0;
132 }
133 
134 #define OFFSET(x) offsetof(VBNContext, x)
135 #define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
136 static const AVOption options[] = {
137     { "format",      "Texture format", OFFSET(format), AV_OPT_TYPE_INT,   { .i64 = VBN_FORMAT_DXT5 }, VBN_FORMAT_RAW, VBN_FORMAT_DXT5, FLAGS, "format" },
138         { "raw",     "RAW texture",                 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_RAW  },              0,               0, FLAGS, "format" },
139         { "dxt1",    "DXT1 texture",                0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_DXT1 },              0,               0, FLAGS, "format" },
140         { "dxt5",    "DXT5 texture",                0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_DXT5 },              0,               0, FLAGS, "format" },
141     { NULL },
142 };
143 
144 static const AVClass vbnenc_class = {
145     .class_name = "VBN encoder",
146     .item_name  = av_default_item_name,
147     .option     = options,
148     .version    = LIBAVUTIL_VERSION_INT,
149 };
150 
151 const FFCodec ff_vbn_encoder = {
152     .p.name         = "vbn",
153     .p.long_name    = NULL_IF_CONFIG_SMALL("Vizrt Binary Image"),
154     .p.type         = AVMEDIA_TYPE_VIDEO,
155     .p.id           = AV_CODEC_ID_VBN,
156     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
157     .p.priv_class   = &vbnenc_class,
158     .init           = vbn_init,
159     FF_CODEC_ENCODE_CB(vbn_encode),
160     .priv_data_size = sizeof(VBNContext),
161     .p.pix_fmts     = (const enum AVPixelFormat[]) {
162         AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE,
163     },
164     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
165                       FF_CODEC_CAP_INIT_CLEANUP,
166 };
167