xref: /third_party/ffmpeg/libavcodec/hapenc.c (revision cabdff1a)
1/*
2 * Vidvox Hap encoder
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 * Copyright (C) 2015 Tom Butterworth <bangnoise@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23/**
24 * @file
25 * Hap encoder
26 *
27 * Fourcc: Hap1, Hap5, HapY
28 *
29 * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
30 */
31
32#include <stdint.h>
33#include "snappy-c.h"
34
35#include "libavutil/frame.h"
36#include "libavutil/imgutils.h"
37#include "libavutil/intreadwrite.h"
38#include "libavutil/opt.h"
39
40#include "avcodec.h"
41#include "bytestream.h"
42#include "codec_internal.h"
43#include "encode.h"
44#include "hap.h"
45#include "texturedsp.h"
46
47#define HAP_MAX_CHUNKS 64
48
49enum HapHeaderLength {
50    /* Short header: four bytes with a 24 bit size value */
51    HAP_HDR_SHORT = 4,
52    /* Long header: eight bytes with a 32 bit size value */
53    HAP_HDR_LONG = 8,
54};
55
56static int compress_texture(AVCodecContext *avctx, uint8_t *out, int out_length, const AVFrame *f)
57{
58    HapContext *ctx = avctx->priv_data;
59
60    if (ctx->tex_size > out_length)
61        return AVERROR_BUFFER_TOO_SMALL;
62
63    ctx->enc.tex_data.out = out;
64    ctx->enc.frame_data.in = f->data[0];
65    ctx->enc.stride = f->linesize[0];
66    avctx->execute2(avctx, ff_texturedsp_compress_thread, &ctx->enc, NULL, ctx->enc.slice_count);
67
68    return 0;
69}
70
71/* section_length does not include the header */
72static void hap_write_section_header(PutByteContext *pbc,
73                                     enum HapHeaderLength header_length,
74                                     int section_length,
75                                     enum HapSectionType section_type)
76{
77    /* The first three bytes are the length of the section (not including the
78     * header) or zero if using an eight-byte header.
79     * For an eight-byte header, the length is in the last four bytes.
80     * The fourth byte stores the section type. */
81    bytestream2_put_le24(pbc, header_length == HAP_HDR_LONG ? 0 : section_length);
82    bytestream2_put_byte(pbc, section_type);
83
84    if (header_length == HAP_HDR_LONG) {
85        bytestream2_put_le32(pbc, section_length);
86    }
87}
88
89static int hap_compress_frame(AVCodecContext *avctx, uint8_t *dst)
90{
91    HapContext *ctx = avctx->priv_data;
92    int i, final_size = 0;
93
94    for (i = 0; i < ctx->chunk_count; i++) {
95        HapChunk *chunk = &ctx->chunks[i];
96        uint8_t *chunk_src, *chunk_dst;
97        int ret;
98
99        if (i == 0) {
100            chunk->compressed_offset = 0;
101        } else {
102            chunk->compressed_offset = ctx->chunks[i-1].compressed_offset
103                                       + ctx->chunks[i-1].compressed_size;
104        }
105        chunk->uncompressed_size = ctx->tex_size / ctx->chunk_count;
106        chunk->uncompressed_offset = i * chunk->uncompressed_size;
107        chunk->compressed_size = ctx->max_snappy;
108        chunk_src = ctx->tex_buf + chunk->uncompressed_offset;
109        chunk_dst = dst + chunk->compressed_offset;
110
111        /* Compress with snappy too, write directly on packet buffer. */
112        ret = snappy_compress(chunk_src, chunk->uncompressed_size,
113                              chunk_dst, &chunk->compressed_size);
114        if (ret != SNAPPY_OK) {
115            av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
116            return AVERROR_BUG;
117        }
118
119        /* If there is no gain from snappy, just use the raw texture. */
120        if (chunk->compressed_size >= chunk->uncompressed_size) {
121            av_log(avctx, AV_LOG_VERBOSE,
122                   "Snappy buffer bigger than uncompressed (%"SIZE_SPECIFIER" >= %"SIZE_SPECIFIER" bytes).\n",
123                   chunk->compressed_size, chunk->uncompressed_size);
124            memcpy(chunk_dst, chunk_src, chunk->uncompressed_size);
125            chunk->compressor = HAP_COMP_NONE;
126            chunk->compressed_size = chunk->uncompressed_size;
127        } else {
128            chunk->compressor = HAP_COMP_SNAPPY;
129        }
130
131        final_size += chunk->compressed_size;
132    }
133
134    return final_size;
135}
136
137static int hap_decode_instructions_length(HapContext *ctx)
138{
139    /*    Second-Stage Compressor Table (one byte per entry)
140     *  + Chunk Size Table (four bytes per entry)
141     *  + headers for both sections (short versions)
142     *  = chunk_count + (4 * chunk_count) + 4 + 4 */
143    return (5 * ctx->chunk_count) + 8;
144}
145
146static int hap_header_length(HapContext *ctx)
147{
148    /* Top section header (long version) */
149    int length = HAP_HDR_LONG;
150
151    if (ctx->chunk_count > 1) {
152        /* Decode Instructions header (short) + Decode Instructions Container */
153        length += HAP_HDR_SHORT + hap_decode_instructions_length(ctx);
154    }
155
156    return length;
157}
158
159static void hap_write_frame_header(HapContext *ctx, uint8_t *dst, int frame_length)
160{
161    PutByteContext pbc;
162    int i;
163
164    bytestream2_init_writer(&pbc, dst, frame_length);
165    if (ctx->chunk_count == 1) {
166        /* Write a simple header */
167        hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
168                                 ctx->chunks[0].compressor | ctx->opt_tex_fmt);
169    } else {
170        /* Write a complex header with Decode Instructions Container */
171        hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
172                                 HAP_COMP_COMPLEX | ctx->opt_tex_fmt);
173        hap_write_section_header(&pbc, HAP_HDR_SHORT, hap_decode_instructions_length(ctx),
174                                 HAP_ST_DECODE_INSTRUCTIONS);
175        hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count,
176                                 HAP_ST_COMPRESSOR_TABLE);
177
178        for (i = 0; i < ctx->chunk_count; i++) {
179            bytestream2_put_byte(&pbc, ctx->chunks[i].compressor >> 4);
180        }
181
182        hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count * 4,
183                                 HAP_ST_SIZE_TABLE);
184
185        for (i = 0; i < ctx->chunk_count; i++) {
186            bytestream2_put_le32(&pbc, ctx->chunks[i].compressed_size);
187        }
188    }
189}
190
191static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
192                      const AVFrame *frame, int *got_packet)
193{
194    HapContext *ctx = avctx->priv_data;
195    int header_length = hap_header_length(ctx);
196    int final_data_size, ret;
197    int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy * ctx->chunk_count) + header_length;
198
199    /* Allocate maximum size packet, shrink later. */
200    ret = ff_alloc_packet(avctx, pkt, pktsize);
201    if (ret < 0)
202        return ret;
203
204    if (ctx->opt_compressor == HAP_COMP_NONE) {
205        /* DXTC compression directly to the packet buffer. */
206        ret = compress_texture(avctx, pkt->data + header_length, pkt->size - header_length, frame);
207        if (ret < 0)
208            return ret;
209
210        ctx->chunks[0].compressor = HAP_COMP_NONE;
211        final_data_size = ctx->tex_size;
212    } else {
213        /* DXTC compression. */
214        ret = compress_texture(avctx, ctx->tex_buf, ctx->tex_size, frame);
215        if (ret < 0)
216            return ret;
217
218        /* Compress (using Snappy) the frame */
219        final_data_size = hap_compress_frame(avctx, pkt->data + header_length);
220        if (final_data_size < 0)
221            return final_data_size;
222    }
223
224    /* Write header at the start. */
225    hap_write_frame_header(ctx, pkt->data, final_data_size + header_length);
226
227    av_shrink_packet(pkt, final_data_size + header_length);
228    *got_packet = 1;
229    return 0;
230}
231
232static av_cold int hap_init(AVCodecContext *avctx)
233{
234    HapContext *ctx = avctx->priv_data;
235    int corrected_chunk_count;
236    int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
237
238    if (ret < 0) {
239        av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
240               avctx->width, avctx->height);
241        return ret;
242    }
243
244    if (avctx->width % 4 || avctx->height % 4) {
245        av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
246               avctx->width, avctx->height);
247        return AVERROR_INVALIDDATA;
248    }
249
250    ff_texturedspenc_init(&ctx->dxtc);
251
252    switch (ctx->opt_tex_fmt) {
253    case HAP_FMT_RGBDXT1:
254        ctx->enc.tex_ratio = 8;
255        avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
256        avctx->bits_per_coded_sample = 24;
257        ctx->enc.tex_funct = ctx->dxtc.dxt1_block;
258        break;
259    case HAP_FMT_RGBADXT5:
260        ctx->enc.tex_ratio = 16;
261        avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
262        avctx->bits_per_coded_sample = 32;
263        ctx->enc.tex_funct = ctx->dxtc.dxt5_block;
264        break;
265    case HAP_FMT_YCOCGDXT5:
266        ctx->enc.tex_ratio = 16;
267        avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
268        avctx->bits_per_coded_sample = 24;
269        ctx->enc.tex_funct = ctx->dxtc.dxt5ys_block;
270        break;
271    default:
272        av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->opt_tex_fmt);
273        return AVERROR_INVALIDDATA;
274    }
275    ctx->enc.raw_ratio = 16;
276    ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H);
277
278    /* Texture compression ratio is constant, so can we computer
279     * beforehand the final size of the uncompressed buffer. */
280    ctx->tex_size   = avctx->width  / TEXTURE_BLOCK_W *
281                      avctx->height / TEXTURE_BLOCK_H * ctx->enc.tex_ratio;
282
283    switch (ctx->opt_compressor) {
284    case HAP_COMP_NONE:
285        /* No benefit chunking uncompressed data */
286        corrected_chunk_count = 1;
287
288        ctx->max_snappy = ctx->tex_size;
289        ctx->tex_buf = NULL;
290        break;
291    case HAP_COMP_SNAPPY:
292        /* Round the chunk count to divide evenly on DXT block edges */
293        corrected_chunk_count = av_clip(ctx->opt_chunk_count, 1, HAP_MAX_CHUNKS);
294        while ((ctx->tex_size / ctx->enc.tex_ratio) % corrected_chunk_count != 0) {
295            corrected_chunk_count--;
296        }
297
298        ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size / corrected_chunk_count);
299        ctx->tex_buf = av_malloc(ctx->tex_size);
300        if (!ctx->tex_buf) {
301            return AVERROR(ENOMEM);
302        }
303        break;
304    default:
305        av_log(avctx, AV_LOG_ERROR, "Invalid compresor %02X\n", ctx->opt_compressor);
306        return AVERROR_INVALIDDATA;
307    }
308    if (corrected_chunk_count != ctx->opt_chunk_count) {
309        av_log(avctx, AV_LOG_INFO, "%d chunks requested but %d used.\n",
310                                    ctx->opt_chunk_count, corrected_chunk_count);
311    }
312    ret = ff_hap_set_chunk_count(ctx, corrected_chunk_count, 1);
313    if (ret != 0)
314        return ret;
315
316    return 0;
317}
318
319static av_cold int hap_close(AVCodecContext *avctx)
320{
321    HapContext *ctx = avctx->priv_data;
322
323    ff_hap_free_context(ctx);
324
325    return 0;
326}
327
328#define OFFSET(x) offsetof(HapContext, x)
329#define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
330static const AVOption options[] = {
331    { "format", NULL, OFFSET(opt_tex_fmt), AV_OPT_TYPE_INT, { .i64 = HAP_FMT_RGBDXT1 }, HAP_FMT_RGBDXT1, HAP_FMT_YCOCGDXT5, FLAGS, "format" },
332        { "hap",       "Hap 1 (DXT1 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBDXT1   }, 0, 0, FLAGS, "format" },
333        { "hap_alpha", "Hap Alpha (DXT5 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBADXT5  }, 0, 0, FLAGS, "format" },
334        { "hap_q",     "Hap Q (DXT5-YCoCg textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_YCOCGDXT5 }, 0, 0, FLAGS, "format" },
335    { "chunks", "chunk count", OFFSET(opt_chunk_count), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, HAP_MAX_CHUNKS, FLAGS, },
336    { "compressor", "second-stage compressor", OFFSET(opt_compressor), AV_OPT_TYPE_INT, { .i64 = HAP_COMP_SNAPPY }, HAP_COMP_NONE, HAP_COMP_SNAPPY, FLAGS, "compressor" },
337        { "none",       "None", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_COMP_NONE }, 0, 0, FLAGS, "compressor" },
338        { "snappy",     "Snappy", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_COMP_SNAPPY }, 0, 0, FLAGS, "compressor" },
339    { NULL },
340};
341
342static const AVClass hapenc_class = {
343    .class_name = "Hap encoder",
344    .item_name  = av_default_item_name,
345    .option     = options,
346    .version    = LIBAVUTIL_VERSION_INT,
347};
348
349const FFCodec ff_hap_encoder = {
350    .p.name         = "hap",
351    .p.long_name    = NULL_IF_CONFIG_SMALL("Vidvox Hap"),
352    .p.type         = AVMEDIA_TYPE_VIDEO,
353    .p.id           = AV_CODEC_ID_HAP,
354    .priv_data_size = sizeof(HapContext),
355    .p.priv_class   = &hapenc_class,
356    .p.capabilities = AV_CODEC_CAP_SLICE_THREADS,
357    .init           = hap_init,
358    FF_CODEC_ENCODE_CB(hap_encode),
359    .close          = hap_close,
360    .p.pix_fmts     = (const enum AVPixelFormat[]) {
361        AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE,
362    },
363    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
364                      FF_CODEC_CAP_INIT_CLEANUP,
365};
366