FFmpeg
Loading...
Searching...
No Matches
vbnenc.c
Go to the documentation of this file.
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
37typedef struct VBNContext {
38 AVClass *class;
40 int format;
43
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 ctx->enc.width = avctx->width;
118 ctx->enc.height = avctx->height;
120 } else {
121 const uint8_t *flipped = frame->data[0] + frame->linesize[0] * (frame->height - 1);
122 av_image_copy_plane(pkt->data + VBN_HEADER_SIZE, linesize, flipped, -frame->linesize[0], linesize, frame->height);
123 }
124
125 *got_packet = 1;
126 return 0;
127}
128
130{
131 VBNContext *ctx = avctx->priv_data;
133 return 0;
134}
135
136#define OFFSET(x) offsetof(VBNContext, x)
137#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
138static const AVOption options[] = {
139 { "format", "Texture format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = VBN_FORMAT_DXT5 }, VBN_FORMAT_RAW, VBN_FORMAT_DXT5, FLAGS, .unit = "format" },
140 { "raw", "RAW texture", 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_RAW }, 0, 0, FLAGS, .unit = "format" },
141 { "dxt1", "DXT1 texture", 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_DXT1 }, 0, 0, FLAGS, .unit = "format" },
142 { "dxt5", "DXT5 texture", 0, AV_OPT_TYPE_CONST, { .i64 = VBN_FORMAT_DXT5 }, 0, 0, FLAGS, .unit = "format" },
143 { NULL },
144};
145
146static const AVClass vbnenc_class = {
147 .class_name = "VBN encoder",
148 .item_name = av_default_item_name,
149 .option = options,
150 .version = LIBAVUTIL_VERSION_INT,
151};
152
154 .p.name = "vbn",
155 CODEC_LONG_NAME("Vizrt Binary Image"),
156 .p.type = AVMEDIA_TYPE_VIDEO,
157 .p.id = AV_CODEC_ID_VBN,
158 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
160 .p.priv_class = &vbnenc_class,
161 .init = vbn_init,
163 .priv_data_size = sizeof(VBNContext),
165 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
166};
static const char *const format[]
Definition af_aiir.c:444
const FFCodec ff_vbn_encoder
Definition vbnenc.c:153
Libavcodec external API header.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition bytestream.h:236
#define FLAGS
Definition cmdutils.c:598
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static AVFrame * frame
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
reference-counted frame API
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
@ AV_CODEC_ID_VBN
Definition codec_id.h:307
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition imgutils.c:76
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition imgutils.c:289
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
misc image utilities
#define av_cold
Definition attributes.h:117
AVOptions.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int format
Definition vbnenc.c:40
TextureDSPThreadContext enc
Definition vbnenc.c:41
TextureDSPEncContext dxtc
Definition vbnenc.c:39
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
Texture block (4x4) module.
#define TEXTURE_BLOCK_W
Definition texturedsp.h:42
void ff_texturedspenc_init(TextureDSPEncContext *c)
#define TEXTURE_BLOCK_H
Definition texturedsp.h:43
int ff_texturedsp_exec_compress_threads(struct AVCodecContext *avctx, TextureDSPThreadContext *ctx)
VBN format definitions.
#define VBN_FORMAT_RAW
Definition vbn.h:35
#define VBN_FORMAT_DXT1
Definition vbn.h:37
#define VBN_MAJOR
Definition vbn.h:30
#define VBN_PIX_RGB
Definition vbn.h:46
#define VBN_HEADER_SIZE
Definition vbn.h:33
#define VBN_MINOR
Definition vbn.h:31
#define VBN_PIX_RGBA
Definition vbn.h:47
#define VBN_FORMAT_DXT5
Definition vbn.h:38
#define VBN_MAGIC
Definition vbn.h:29
static av_cold int vbn_init(AVCodecContext *avctx)
Definition vbndec.c:40
static const AVClass vbnenc_class
Definition vbnenc.c:146
static int vbn_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition vbnenc.c:44
static av_cold int vbn_init(AVCodecContext *avctx)
Definition vbnenc.c:129
#define OFFSET(x)
Definition vbnenc.c:136