| @@ -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 | +}; |