Line | Branch | Exec | Source |
---|---|---|---|
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 | TextureDSPEncContext dxtc; | ||
40 | int format; | ||
41 | TextureDSPThreadContext enc; | ||
42 | } VBNContext; | ||
43 | |||
44 | 4 | static int vbn_encode(AVCodecContext *avctx, AVPacket *pkt, | |
45 | const AVFrame *frame, int *got_packet) | ||
46 | { | ||
47 | 4 | VBNContext *ctx = avctx->priv_data; | |
48 | 4 | PutByteContext pb0, *const pb = &pb0; | |
49 | int ret; | ||
50 | ptrdiff_t linesize; | ||
51 | int64_t pkt_size; | ||
52 | |||
53 | 4 | ret = av_image_check_size2(frame->width, frame->height, INT_MAX, frame->format, 0, avctx); | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
55 | ✗ | return ret; | |
56 | |||
57 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
4 | if (ctx->format == VBN_FORMAT_DXT1 || ctx->format == VBN_FORMAT_DXT5) { |
58 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | 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 | 2 | ctx->enc.raw_ratio = 16; | |
67 | 2 | ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H); | |
68 | } | ||
69 | |||
70 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | switch (ctx->format) { |
71 | 1 | case VBN_FORMAT_DXT1: | |
72 | 1 | linesize = frame->width / 2; | |
73 | 1 | ctx->enc.tex_funct = ctx->dxtc.dxt1_block; | |
74 | 1 | ctx->enc.tex_ratio = 8; | |
75 | 1 | break; | |
76 | 1 | case VBN_FORMAT_DXT5: | |
77 | 1 | linesize = frame->width; | |
78 | 1 | ctx->enc.tex_funct = ctx->dxtc.dxt5_block; | |
79 | 1 | ctx->enc.tex_ratio = 16; | |
80 | 1 | break; | |
81 | 2 | case VBN_FORMAT_RAW: | |
82 | 2 | linesize = av_image_get_linesize(frame->format, frame->width, 0); | |
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (linesize < 0) |
84 | ✗ | return linesize; | |
85 | 2 | break; | |
86 | ✗ | default: | |
87 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->format); | |
88 | ✗ | return AVERROR(EINVAL); | |
89 | } | ||
90 | |||
91 | 4 | pkt_size = VBN_HEADER_SIZE + linesize * frame->height; | |
92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (pkt_size > INT_MAX) |
93 | ✗ | return AVERROR(EINVAL); | |
94 | |||
95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_get_encode_buffer(avctx, pkt, pkt_size, 0)) < 0) |
96 | ✗ | return ret; | |
97 | |||
98 | 4 | memset(pkt->data, 0, VBN_HEADER_SIZE); | |
99 | 4 | bytestream2_init_writer(pb, pkt->data, pkt_size); | |
100 | 4 | bytestream2_put_le32u(pb, VBN_MAGIC); | |
101 | 4 | bytestream2_put_le32u(pb, VBN_MAJOR); | |
102 | 4 | bytestream2_put_le32u(pb, VBN_MINOR); | |
103 | 4 | bytestream2_put_le32u(pb, frame->width); | |
104 | 4 | bytestream2_put_le32u(pb, frame->height); | |
105 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | bytestream2_put_le32u(pb, frame->format == AV_PIX_FMT_RGBA ? 4 : 3); |
106 | 4 | bytestream2_put_le32u(pb, ctx->format); | |
107 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | bytestream2_put_le32u(pb, frame->format == AV_PIX_FMT_RGBA ? VBN_PIX_RGBA : VBN_PIX_RGB); |
108 | 4 | bytestream2_put_le32u(pb, 0); // mipmaps | |
109 | 4 | bytestream2_put_le32u(pb, pkt_size - VBN_HEADER_SIZE); | |
110 | 4 | bytestream2_seek_p(pb, 64, SEEK_SET); | |
111 | 4 | bytestream2_put_le32u(pb, pkt_size - VBN_HEADER_SIZE); | |
112 | |||
113 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
4 | if (ctx->format == VBN_FORMAT_DXT1 || ctx->format == VBN_FORMAT_DXT5) { |
114 | 2 | ctx->enc.frame_data.in = (frame->height - 1) * frame->linesize[0] + frame->data[0]; | |
115 | 2 | ctx->enc.stride = -frame->linesize[0]; | |
116 | 2 | ctx->enc.tex_data.out = pkt->data + VBN_HEADER_SIZE; | |
117 | 2 | ctx->enc.width = avctx->width; | |
118 | 2 | ctx->enc.height = avctx->height; | |
119 | 2 | ff_texturedsp_exec_compress_threads(avctx, &ctx->enc); | |
120 | } else { | ||
121 | 2 | const uint8_t *flipped = frame->data[0] + frame->linesize[0] * (frame->height - 1); | |
122 | 2 | av_image_copy_plane(pkt->data + VBN_HEADER_SIZE, linesize, flipped, -frame->linesize[0], linesize, frame->height); | |
123 | } | ||
124 | |||
125 | 4 | *got_packet = 1; | |
126 | 4 | return 0; | |
127 | } | ||
128 | |||
129 | 4 | static av_cold int vbn_init(AVCodecContext *avctx) | |
130 | { | ||
131 | 4 | VBNContext *ctx = avctx->priv_data; | |
132 | 4 | ff_texturedspenc_init(&ctx->dxtc); | |
133 | 4 | return 0; | |
134 | } | ||
135 | |||
136 | #define OFFSET(x) offsetof(VBNContext, x) | ||
137 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
138 | static 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 | |||
146 | static 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 | |||
153 | const FFCodec ff_vbn_encoder = { | ||
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 | | ||
159 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
160 | .p.priv_class = &vbnenc_class, | ||
161 | .init = vbn_init, | ||
162 | FF_CODEC_ENCODE_CB(vbn_encode), | ||
163 | .priv_data_size = sizeof(VBNContext), | ||
164 | .p.pix_fmts = (const enum AVPixelFormat[]) { | ||
165 | AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE, | ||
166 | }, | ||
167 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
168 | }; | ||
169 |