3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * Fourcc: Hap1, Hap5, HapY
28 * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
34 #include "libavutil/frame.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/opt.h"
40 #include "bytestream.h"
43 #include "texturedsp.h"
45 /* A fixed header size allows to skip a memcpy */
48 static void compress_texture(AVCodecContext *avctx, const AVFrame *f)
50 HapContext *ctx = avctx->priv_data;
51 uint8_t *out = ctx->tex_buf;
54 for (j = 0; j < avctx->height; j += 4) {
55 for (i = 0; i < avctx->width; i += 4) {
56 uint8_t *p = f->data[0] + i * 4 + j * f->linesize[0];
57 const int step = ctx->tex_fun(out, f->linesize[0], p);
63 static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
64 const AVFrame *frame, int *got_packet)
66 HapContext *ctx = avctx->priv_data;
67 size_t final_size = ctx->max_snappy;
68 int ret, comp = HAP_COMP_SNAPPY;
69 int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy) + HEADER_SIZE;
71 /* Allocate maximum size packet, shrink later. */
72 ret = ff_alloc_packet(pkt, pktsize);
76 /* DXTC compression. */
77 compress_texture(avctx, frame);
79 /* Compress with snappy too, write directly on packet buffer. */
80 ret = snappy_compress(ctx->tex_buf, ctx->tex_size,
81 pkt->data + HEADER_SIZE, &final_size);
82 if (ret != SNAPPY_OK) {
83 av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
87 /* If there is no gain from snappy, just use the raw texture. */
88 if (final_size > ctx->tex_size) {
90 av_log(avctx, AV_LOG_VERBOSE,
91 "Snappy buffer bigger than uncompressed (%lu > %lu bytes).\n",
92 final_size, ctx->tex_size);
93 memcpy(pkt->data + HEADER_SIZE, ctx->tex_buf, ctx->tex_size);
94 final_size = ctx->tex_size;
97 /* Write header at the start. */
98 AV_WL24(pkt->data, 0);
99 AV_WL32(pkt->data + 4, final_size);
100 pkt->data[3] = comp | ctx->section_type;
102 av_shrink_packet(pkt, final_size + HEADER_SIZE);
103 pkt->flags |= AV_PKT_FLAG_KEY;
108 static av_cold int hap_init(AVCodecContext *avctx)
110 HapContext *ctx = avctx->priv_data;
112 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
115 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
116 avctx->width, avctx->height);
120 if (avctx->width % 4 || avctx->height % 4) {
121 av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
122 avctx->width, avctx->height);
123 return AVERROR_INVALIDDATA;
126 ff_texturedspenc_init(&ctx->dxtc);
128 switch (ctx->section_type & 0x0F) {
129 case HAP_FMT_RGBDXT1:
131 avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
132 ctx->tex_fun = ctx->dxtc.dxt1_block;
134 case HAP_FMT_RGBADXT5:
136 avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
137 ctx->tex_fun = ctx->dxtc.dxt5_block;
139 case HAP_FMT_YCOCGDXT5:
141 avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
142 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
145 av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->section_type);
146 return AVERROR_INVALIDDATA;
149 /* Texture compression ratio is constant, so can we computer
150 * beforehand the final size of the uncompressed buffer. */
151 ctx->tex_size = FFALIGN(avctx->width, TEXTURE_BLOCK_W) *
152 FFALIGN(avctx->height, TEXTURE_BLOCK_H) * 4 / ratio;
153 ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size);
155 ctx->tex_buf = av_malloc(ctx->tex_size);
157 return AVERROR(ENOMEM);
162 static av_cold int hap_close(AVCodecContext *avctx)
164 HapContext *ctx = avctx->priv_data;
166 av_freep(&ctx->tex_buf);
171 #define OFFSET(x) offsetof(HapContext, x)
172 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
173 static const AVOption options[] = {
174 { "format", NULL, OFFSET(section_type), AV_OPT_TYPE_INT, { .i64 = HAP_FMT_RGBDXT1 }, HAP_FMT_RGBDXT1, HAP_FMT_YCOCGDXT5, FLAGS, "format" },
175 { "hap", "Hap 1 (DXT1 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBDXT1 }, 0, 0, FLAGS, "format" },
176 { "hap_alpha", "Hap Alpha (DXT5 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBADXT5 }, 0, 0, FLAGS, "format" },
177 { "hap_q", "Hap Q (DXT5-YCoCg textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_YCOCGDXT5 }, 0, 0, FLAGS, "format" },
182 static const AVClass hapenc_class = {
183 .class_name = "Hap encoder",
184 .item_name = av_default_item_name,
186 .version = LIBAVUTIL_VERSION_INT,
189 AVCodec ff_hap_encoder = {
191 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap encoder"),
192 .type = AVMEDIA_TYPE_VIDEO,
193 .id = AV_CODEC_ID_HAP,
194 .priv_data_size = sizeof(HapContext),
195 .priv_class = &hapenc_class,
197 .encode2 = hap_encode,
199 .pix_fmts = (const enum AVPixelFormat[]) {
200 AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE,
202 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
203 FF_CODEC_CAP_INIT_CLEANUP,