3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 * Copyright (C) 2015 Tom Butterworth <bangnoise@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * Fourcc: Hap1, Hap5, HapY
29 * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
35 #include "libavutil/frame.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/opt.h"
41 #include "bytestream.h"
44 #include "texturedsp.h"
46 #define HAP_MAX_CHUNKS 64
48 enum HapHeaderLength {
49 /* Short header: four bytes with a 24 bit size value */
51 /* Long header: eight bytes with a 32 bit size value */
55 static int compress_texture(AVCodecContext *avctx, uint8_t *out, int out_length, const AVFrame *f)
57 HapContext *ctx = avctx->priv_data;
60 if (ctx->tex_size > out_length)
61 return AVERROR_BUFFER_TOO_SMALL;
63 for (j = 0; j < avctx->height; j += 4) {
64 for (i = 0; i < avctx->width; i += 4) {
65 uint8_t *p = f->data[0] + i * 4 + j * f->linesize[0];
66 const int step = ctx->tex_fun(out, f->linesize[0], p);
74 /* section_length does not include the header */
75 static void hap_write_section_header(PutByteContext *pbc,
76 enum HapHeaderLength header_length,
78 enum HapSectionType section_type)
80 /* The first three bytes are the length of the section (not including the
81 * header) or zero if using an eight-byte header.
82 * For an eight-byte header, the length is in the last four bytes.
83 * The fourth byte stores the section type. */
84 bytestream2_put_le24(pbc, header_length == HAP_HDR_LONG ? 0 : section_length);
85 bytestream2_put_byte(pbc, section_type);
87 if (header_length == HAP_HDR_LONG) {
88 bytestream2_put_le32(pbc, section_length);
92 static int hap_compress_frame(AVCodecContext *avctx, uint8_t *dst)
94 HapContext *ctx = avctx->priv_data;
95 int i, final_size = 0;
97 for (i = 0; i < ctx->chunk_count; i++) {
98 HapChunk *chunk = &ctx->chunks[i];
99 uint8_t *chunk_src, *chunk_dst;
103 chunk->compressed_offset = 0;
105 chunk->compressed_offset = ctx->chunks[i-1].compressed_offset
106 + ctx->chunks[i-1].compressed_size;
108 chunk->uncompressed_size = ctx->tex_size / ctx->chunk_count;
109 chunk->uncompressed_offset = i * chunk->uncompressed_size;
110 chunk->compressed_size = ctx->max_snappy;
111 chunk_src = ctx->tex_buf + chunk->uncompressed_offset;
112 chunk_dst = dst + chunk->compressed_offset;
114 /* Compress with snappy too, write directly on packet buffer. */
115 ret = snappy_compress(chunk_src, chunk->uncompressed_size,
116 chunk_dst, &chunk->compressed_size);
117 if (ret != SNAPPY_OK) {
118 av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
122 /* If there is no gain from snappy, just use the raw texture. */
123 if (chunk->compressed_size >= chunk->uncompressed_size) {
124 av_log(avctx, AV_LOG_VERBOSE,
125 "Snappy buffer bigger than uncompressed (%"SIZE_SPECIFIER" >= %"SIZE_SPECIFIER" bytes).\n",
126 chunk->compressed_size, chunk->uncompressed_size);
127 memcpy(chunk_dst, chunk_src, chunk->uncompressed_size);
128 chunk->compressor = HAP_COMP_NONE;
129 chunk->compressed_size = chunk->uncompressed_size;
131 chunk->compressor = HAP_COMP_SNAPPY;
134 final_size += chunk->compressed_size;
140 static int hap_decode_instructions_length(HapContext *ctx)
142 /* Second-Stage Compressor Table (one byte per entry)
143 * + Chunk Size Table (four bytes per entry)
144 * + headers for both sections (short versions)
145 * = chunk_count + (4 * chunk_count) + 4 + 4 */
146 return (5 * ctx->chunk_count) + 8;
149 static int hap_header_length(HapContext *ctx)
151 /* Top section header (long version) */
152 int length = HAP_HDR_LONG;
154 if (ctx->chunk_count > 1) {
155 /* Decode Instructions header (short) + Decode Instructions Container */
156 length += HAP_HDR_SHORT + hap_decode_instructions_length(ctx);
162 static void hap_write_frame_header(HapContext *ctx, uint8_t *dst, int frame_length)
167 bytestream2_init_writer(&pbc, dst, frame_length);
168 if (ctx->chunk_count == 1) {
169 /* Write a simple header */
170 hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
171 ctx->chunks[0].compressor | ctx->opt_tex_fmt);
173 /* Write a complex header with Decode Instructions Container */
174 hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
175 HAP_COMP_COMPLEX | ctx->opt_tex_fmt);
176 hap_write_section_header(&pbc, HAP_HDR_SHORT, hap_decode_instructions_length(ctx),
177 HAP_ST_DECODE_INSTRUCTIONS);
178 hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count,
179 HAP_ST_COMPRESSOR_TABLE);
181 for (i = 0; i < ctx->chunk_count; i++) {
182 bytestream2_put_byte(&pbc, ctx->chunks[i].compressor >> 4);
185 hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count * 4,
188 for (i = 0; i < ctx->chunk_count; i++) {
189 bytestream2_put_le32(&pbc, ctx->chunks[i].compressed_size);
194 static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
195 const AVFrame *frame, int *got_packet)
197 HapContext *ctx = avctx->priv_data;
198 int header_length = hap_header_length(ctx);
199 int final_data_size, ret;
200 int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy * ctx->chunk_count) + header_length;
202 /* Allocate maximum size packet, shrink later. */
203 ret = ff_alloc_packet2(avctx, pkt, pktsize, header_length);
207 if (ctx->opt_compressor == HAP_COMP_NONE) {
208 /* DXTC compression directly to the packet buffer. */
209 ret = compress_texture(avctx, pkt->data + header_length, pkt->size - header_length, frame);
213 ctx->chunks[0].compressor = HAP_COMP_NONE;
214 final_data_size = ctx->tex_size;
216 /* DXTC compression. */
217 ret = compress_texture(avctx, ctx->tex_buf, ctx->tex_size, frame);
221 /* Compress (using Snappy) the frame */
222 final_data_size = hap_compress_frame(avctx, pkt->data + header_length);
223 if (final_data_size < 0)
224 return final_data_size;
227 /* Write header at the start. */
228 hap_write_frame_header(ctx, pkt->data, final_data_size + header_length);
230 av_shrink_packet(pkt, final_data_size + header_length);
231 pkt->flags |= AV_PKT_FLAG_KEY;
236 static av_cold int hap_init(AVCodecContext *avctx)
238 HapContext *ctx = avctx->priv_data;
240 int corrected_chunk_count;
241 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
244 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
245 avctx->width, avctx->height);
249 if (avctx->width % 4 || avctx->height % 4) {
250 av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
251 avctx->width, avctx->height);
252 return AVERROR_INVALIDDATA;
255 ff_texturedspenc_init(&ctx->dxtc);
257 switch (ctx->opt_tex_fmt) {
258 case HAP_FMT_RGBDXT1:
260 avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
261 avctx->bits_per_coded_sample = 24;
262 ctx->tex_fun = ctx->dxtc.dxt1_block;
264 case HAP_FMT_RGBADXT5:
266 avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
267 avctx->bits_per_coded_sample = 32;
268 ctx->tex_fun = ctx->dxtc.dxt5_block;
270 case HAP_FMT_YCOCGDXT5:
272 avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
273 avctx->bits_per_coded_sample = 24;
274 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
277 av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->opt_tex_fmt);
278 return AVERROR_INVALIDDATA;
281 /* Texture compression ratio is constant, so can we computer
282 * beforehand the final size of the uncompressed buffer. */
283 ctx->tex_size = FFALIGN(avctx->width, TEXTURE_BLOCK_W) *
284 FFALIGN(avctx->height, TEXTURE_BLOCK_H) * 4 / ratio;
286 switch (ctx->opt_compressor) {
288 /* No benefit chunking uncompressed data */
289 corrected_chunk_count = 1;
291 ctx->max_snappy = ctx->tex_size;
294 case HAP_COMP_SNAPPY:
295 /* Round the chunk count to divide evenly on DXT block edges */
296 corrected_chunk_count = av_clip(ctx->opt_chunk_count, 1, HAP_MAX_CHUNKS);
297 while ((ctx->tex_size / (64 / ratio)) % corrected_chunk_count != 0) {
298 corrected_chunk_count--;
301 ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size / corrected_chunk_count);
302 ctx->tex_buf = av_malloc(ctx->tex_size);
304 return AVERROR(ENOMEM);
308 av_log(avctx, AV_LOG_ERROR, "Invalid compresor %02X\n", ctx->opt_compressor);
309 return AVERROR_INVALIDDATA;
311 if (corrected_chunk_count != ctx->opt_chunk_count) {
312 av_log(avctx, AV_LOG_INFO, "%d chunks requested but %d used.\n",
313 ctx->opt_chunk_count, corrected_chunk_count);
315 ret = ff_hap_set_chunk_count(ctx, corrected_chunk_count, 1);
322 static av_cold int hap_close(AVCodecContext *avctx)
324 HapContext *ctx = avctx->priv_data;
326 ff_hap_free_context(ctx);
331 #define OFFSET(x) offsetof(HapContext, x)
332 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
333 static const AVOption options[] = {
334 { "format", NULL, OFFSET(opt_tex_fmt), AV_OPT_TYPE_INT, { .i64 = HAP_FMT_RGBDXT1 }, HAP_FMT_RGBDXT1, HAP_FMT_YCOCGDXT5, FLAGS, "format" },
335 { "hap", "Hap 1 (DXT1 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBDXT1 }, 0, 0, FLAGS, "format" },
336 { "hap_alpha", "Hap Alpha (DXT5 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBADXT5 }, 0, 0, FLAGS, "format" },
337 { "hap_q", "Hap Q (DXT5-YCoCg textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_YCOCGDXT5 }, 0, 0, FLAGS, "format" },
338 { "chunks", "chunk count", OFFSET(opt_chunk_count), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, HAP_MAX_CHUNKS, FLAGS, },
339 { "compressor", "second-stage compressor", OFFSET(opt_compressor), AV_OPT_TYPE_INT, { .i64 = HAP_COMP_SNAPPY }, HAP_COMP_NONE, HAP_COMP_SNAPPY, FLAGS, "compressor" },
340 { "none", "None", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_COMP_NONE }, 0, 0, FLAGS, "compressor" },
341 { "snappy", "Snappy", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_COMP_SNAPPY }, 0, 0, FLAGS, "compressor" },
345 static const AVClass hapenc_class = {
346 .class_name = "Hap encoder",
347 .item_name = av_default_item_name,
349 .version = LIBAVUTIL_VERSION_INT,
352 AVCodec ff_hap_encoder = {
354 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap"),
355 .type = AVMEDIA_TYPE_VIDEO,
356 .id = AV_CODEC_ID_HAP,
357 .priv_data_size = sizeof(HapContext),
358 .priv_class = &hapenc_class,
360 .encode2 = hap_encode,
362 .pix_fmts = (const enum AVPixelFormat[]) {
363 AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE,
365 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
366 FF_CODEC_CAP_INIT_CLEANUP,