FFmpeg
Loading...
Searching...
No Matches
hapenc.c
Go to the documentation of this file.
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/mem.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
50 /* Short header: four bytes with a 24 bit size value */
52 /* Long header: eight bytes with a 32 bit size value */
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)
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 ctx->enc.width = avctx->width;
67 ctx->enc.height = avctx->height;
69
70 return 0;
71}
72
73/* section_length does not include the header */
75 enum HapHeaderLength header_length,
76 int section_length,
77 enum HapSectionType section_type)
78{
79 /* The first three bytes are the length of the section (not including the
80 * header) or zero if using an eight-byte header.
81 * For an eight-byte header, the length is in the last four bytes.
82 * The fourth byte stores the section type. */
83 bytestream2_put_le24(pbc, header_length == HAP_HDR_LONG ? 0 : section_length);
84 bytestream2_put_byte(pbc, section_type);
85
86 if (header_length == HAP_HDR_LONG) {
87 bytestream2_put_le32(pbc, section_length);
88 }
89}
90
91static int hap_compress_frame(AVCodecContext *avctx, uint8_t *dst)
92{
93 HapContext *ctx = avctx->priv_data;
94 int i, final_size = 0;
95
96 for (i = 0; i < ctx->chunk_count; i++) {
97 HapChunk *chunk = &ctx->chunks[i];
98 uint8_t *chunk_src, *chunk_dst;
99 int ret;
100
101 if (i == 0) {
102 chunk->compressed_offset = 0;
103 } else {
104 chunk->compressed_offset = ctx->chunks[i-1].compressed_offset
105 + ctx->chunks[i-1].compressed_size;
106 }
107 chunk->uncompressed_size = ctx->tex_size / ctx->chunk_count;
108 chunk->uncompressed_offset = i * chunk->uncompressed_size;
109 chunk->compressed_size = ctx->max_snappy;
110 chunk_src = ctx->tex_buf + chunk->uncompressed_offset;
111 chunk_dst = dst + chunk->compressed_offset;
112
113 /* Compress with snappy too, write directly on packet buffer. */
114 ret = snappy_compress(chunk_src, chunk->uncompressed_size,
115 chunk_dst, &chunk->compressed_size);
116 if (ret != SNAPPY_OK) {
117 av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
118 return AVERROR_BUG;
119 }
120
121 /* If there is no gain from snappy, just use the raw texture. */
122 if (chunk->compressed_size >= chunk->uncompressed_size) {
123 av_log(avctx, AV_LOG_VERBOSE,
124 "Snappy buffer bigger than uncompressed (%zu >= %zu bytes).\n",
125 chunk->compressed_size, chunk->uncompressed_size);
126 memcpy(chunk_dst, chunk_src, chunk->uncompressed_size);
127 chunk->compressor = HAP_COMP_NONE;
128 chunk->compressed_size = chunk->uncompressed_size;
129 } else {
131 }
132
133 final_size += chunk->compressed_size;
134 }
135
136 return final_size;
137}
138
140{
141 /* Second-Stage Compressor Table (one byte per entry)
142 * + Chunk Size Table (four bytes per entry)
143 * + headers for both sections (short versions)
144 * = chunk_count + (4 * chunk_count) + 4 + 4 */
145 return (5 * ctx->chunk_count) + 8;
146}
147
149{
150 /* Top section header (long version) */
151 int length = HAP_HDR_LONG;
152
153 if (ctx->chunk_count > 1) {
154 /* Decode Instructions header (short) + Decode Instructions Container */
156 }
157
158 return length;
159}
160
161static void hap_write_frame_header(HapContext *ctx, uint8_t *dst, int frame_length)
162{
163 PutByteContext pbc;
164 int i;
165
166 bytestream2_init_writer(&pbc, dst, frame_length);
167 if (ctx->chunk_count == 1) {
168 /* Write a simple header */
169 hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
170 ctx->chunks[0].compressor | ctx->opt_tex_fmt);
171 } else {
172 /* Write a complex header with Decode Instructions Container */
173 hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
174 HAP_COMP_COMPLEX | ctx->opt_tex_fmt);
177 hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count,
179
180 for (i = 0; i < ctx->chunk_count; i++) {
181 bytestream2_put_byte(&pbc, ctx->chunks[i].compressor >> 4);
182 }
183
184 hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count * 4,
186
187 for (i = 0; i < ctx->chunk_count; i++) {
188 bytestream2_put_le32(&pbc, ctx->chunks[i].compressed_size);
189 }
190 }
191}
192
194 const AVFrame *frame, int *got_packet)
195{
196 HapContext *ctx = avctx->priv_data;
197 int header_length = hap_header_length(ctx);
198 int final_data_size, ret;
199 int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy * ctx->chunk_count) + header_length;
200
201 /* Allocate maximum size packet, shrink later. */
202 ret = ff_alloc_packet(avctx, pkt, pktsize);
203 if (ret < 0)
204 return ret;
205
206 if (ctx->opt_compressor == HAP_COMP_NONE) {
207 /* DXTC compression directly to the packet buffer. */
208 ret = compress_texture(avctx, pkt->data + header_length, pkt->size - header_length, frame);
209 if (ret < 0)
210 return ret;
211
212 ctx->chunks[0].compressor = HAP_COMP_NONE;
213 final_data_size = ctx->tex_size;
214 } else {
215 /* DXTC compression. */
216 ret = compress_texture(avctx, ctx->tex_buf, ctx->tex_size, frame);
217 if (ret < 0)
218 return ret;
219
220 /* Compress (using Snappy) the frame */
221 final_data_size = hap_compress_frame(avctx, pkt->data + header_length);
222 if (final_data_size < 0)
223 return final_data_size;
224 }
225
226 /* Write header at the start. */
227 hap_write_frame_header(ctx, pkt->data, final_data_size + header_length);
228
229 av_shrink_packet(pkt, final_data_size + header_length);
230 *got_packet = 1;
231 return 0;
232}
233
235{
236 HapContext *ctx = avctx->priv_data;
238 int corrected_chunk_count;
239 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
240
241 if (ret < 0) {
242 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
243 avctx->width, avctx->height);
244 return ret;
245 }
246
247 if (avctx->width % 4 || avctx->height % 4) {
248 av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
249 avctx->width, avctx->height);
250 return AVERROR_INVALIDDATA;
251 }
252
254
255 switch (ctx->opt_tex_fmt) {
256 case HAP_FMT_RGBDXT1:
257 ctx->enc.tex_ratio = 8;
258 avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
259 avctx->bits_per_coded_sample = 24;
260 ctx->enc.tex_funct = dxtc.dxt1_block;
261 break;
262 case HAP_FMT_RGBADXT5:
263 ctx->enc.tex_ratio = 16;
264 avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
265 avctx->bits_per_coded_sample = 32;
266 ctx->enc.tex_funct = dxtc.dxt5_block;
267 break;
269 ctx->enc.tex_ratio = 16;
270 avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
271 avctx->bits_per_coded_sample = 24;
272 ctx->enc.tex_funct = dxtc.dxt5ys_block;
273 break;
274 default:
275 av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->opt_tex_fmt);
276 return AVERROR_INVALIDDATA;
277 }
278 ctx->enc.raw_ratio = 16;
279 ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H);
280
281 /* Texture compression ratio is constant, so can we computer
282 * beforehand the final size of the uncompressed buffer. */
283 ctx->tex_size = avctx->width / TEXTURE_BLOCK_W *
284 avctx->height / TEXTURE_BLOCK_H * ctx->enc.tex_ratio;
285
286 switch (ctx->opt_compressor) {
287 case HAP_COMP_NONE:
288 /* No benefit chunking uncompressed data */
289 corrected_chunk_count = 1;
290
291 ctx->max_snappy = ctx->tex_size;
292 ctx->tex_buf = NULL;
293 break;
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 / ctx->enc.tex_ratio) % corrected_chunk_count != 0) {
298 corrected_chunk_count--;
299 }
300
301 ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size / corrected_chunk_count);
302 ctx->tex_buf = av_malloc(ctx->tex_size);
303 if (!ctx->tex_buf) {
304 return AVERROR(ENOMEM);
305 }
306 break;
307 default:
308 av_log(avctx, AV_LOG_ERROR, "Invalid compressor %02X\n", ctx->opt_compressor);
309 return AVERROR_INVALIDDATA;
310 }
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);
314 }
315 ret = ff_hap_set_chunk_count(ctx, corrected_chunk_count, 1);
316 if (ret != 0)
317 return ret;
318
319 return 0;
320}
321
323{
324 HapContext *ctx = avctx->priv_data;
325
327
328 return 0;
329}
330
331#define OFFSET(x) offsetof(HapContext, x)
332#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
333static 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, .unit = "format" },
335 { "hap", "Hap 1 (DXT1 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBDXT1 }, 0, 0, FLAGS, .unit = "format" },
336 { "hap_alpha", "Hap Alpha (DXT5 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBADXT5 }, 0, 0, FLAGS, .unit = "format" },
337 { "hap_q", "Hap Q (DXT5-YCoCg textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_YCOCGDXT5 }, 0, 0, FLAGS, .unit = "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, .unit = "compressor" },
340 { "none", "None", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_COMP_NONE }, 0, 0, FLAGS, .unit = "compressor" },
341 { "snappy", "Snappy", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_COMP_SNAPPY }, 0, 0, FLAGS, .unit = "compressor" },
342 { NULL },
343};
344
345static const AVClass hapenc_class = {
346 .class_name = "Hap encoder",
347 .item_name = av_default_item_name,
348 .option = options,
349 .version = LIBAVUTIL_VERSION_INT,
350};
351
353 .p.name = "hap",
354 CODEC_LONG_NAME("Vidvox Hap"),
355 .p.type = AVMEDIA_TYPE_VIDEO,
356 .p.id = AV_CODEC_ID_HAP,
357 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
359 .priv_data_size = sizeof(HapContext),
360 .p.priv_class = &hapenc_class,
361 .init = hap_init,
363 .close = hap_close,
365 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
366};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_hap_encoder
Definition hapenc.c:352
static FILE * out
static AVFormatContext * ctx
Libavcodec external API header.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c: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
static AVPacket * pkt
static AVFrame * frame
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
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_HAP
Definition codec_id.h:238
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition error.h:53
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#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
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
av_cold void ff_hap_free_context(HapContext *ctx)
Definition hap.c:51
int ff_hap_set_chunk_count(HapContext *ctx, int count, int first_in_frame)
Definition hap.c:29
@ HAP_FMT_RGBDXT1
Definition hap.h:33
@ HAP_FMT_YCOCGDXT5
Definition hap.h:35
@ HAP_FMT_RGBADXT5
Definition hap.h:34
@ HAP_COMP_NONE
Definition hap.h:40
@ HAP_COMP_SNAPPY
Definition hap.h:41
@ HAP_COMP_COMPLEX
Definition hap.h:42
HapSectionType
Definition hap.h:45
@ HAP_ST_SIZE_TABLE
Definition hap.h:48
@ HAP_ST_DECODE_INSTRUCTIONS
Definition hap.h:46
@ HAP_ST_COMPRESSOR_TABLE
Definition hap.h:47
static av_cold int hap_init(AVCodecContext *avctx)
Definition hapdec.c:339
static av_cold int hap_close(AVCodecContext *avctx)
Definition hapdec.c:409
static int hap_header_length(HapContext *ctx)
Definition hapenc.c:148
#define HAP_MAX_CHUNKS
Definition hapenc.c:47
static int hap_decode_instructions_length(HapContext *ctx)
Definition hapenc.c:139
static int hap_compress_frame(AVCodecContext *avctx, uint8_t *dst)
Definition hapenc.c:91
HapHeaderLength
Definition hapenc.c:49
@ HAP_HDR_LONG
Definition hapenc.c:53
@ HAP_HDR_SHORT
Definition hapenc.c:51
static void hap_write_section_header(PutByteContext *pbc, enum HapHeaderLength header_length, int section_length, enum HapSectionType section_type)
Definition hapenc.c:74
static av_cold int hap_init(AVCodecContext *avctx)
Definition hapenc.c:234
static int compress_texture(AVCodecContext *avctx, uint8_t *out, int out_length, const AVFrame *f)
Definition hapenc.c:56
static av_cold int hap_close(AVCodecContext *avctx)
Definition hapenc.c:322
static int hap_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition hapenc.c:193
#define OFFSET(x)
Definition hapenc.c:331
static const AVClass hapenc_class
Definition hapenc.c:345
static void hap_write_frame_header(HapContext *ctx, uint8_t *dst, int frame_length)
Definition hapenc.c:161
misc image utilities
#define av_cold
Definition attributes.h:117
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
@ 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
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
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
Definition hap.h:52
size_t compressed_size
Definition hap.h:55
uint32_t compressed_offset
Definition hap.h:54
int uncompressed_offset
Definition hap.h:56
size_t uncompressed_size
Definition hap.h:57
enum HapCompressor compressor
Definition hap.h:53
int(* dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition texturedsp.h:64
int(* dxt5ys_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition texturedsp.h:66
int(* dxt5_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition texturedsp.h:65
#define av_log(a,...)
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)