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
34 #include "libavutil/imgutils.h"
37 #include "bytestream.h"
41 #include "texturedsp.h"
45 /* The first three bytes are the size of the section past the header, or zero
46 * if the length is stored in the next long word. The fourth byte in the first
47 * long word indicates the type of the current section. */
48 static int parse_section_header(GetByteContext *gbc, int *section_size,
49 enum HapSectionType *section_type)
51 if (bytestream2_get_bytes_left(gbc) < 4)
52 return AVERROR_INVALIDDATA;
54 *section_size = bytestream2_get_le24(gbc);
55 *section_type = bytestream2_get_byte(gbc);
57 if (*section_size == 0) {
58 if (bytestream2_get_bytes_left(gbc) < 4)
59 return AVERROR_INVALIDDATA;
61 *section_size = bytestream2_get_le32(gbc);
64 if (*section_size > bytestream2_get_bytes_left(gbc))
65 return AVERROR_INVALIDDATA;
70 static int hap_parse_decode_instructions(HapContext *ctx, int size)
72 GetByteContext *gbc = &ctx->gbc;
74 enum HapSectionType section_type;
75 int is_first_table = 1, had_offsets = 0, had_compressors = 0, had_sizes = 0;
79 int stream_remaining = bytestream2_get_bytes_left(gbc);
80 ret = parse_section_header(gbc, §ion_size, §ion_type);
84 size -= stream_remaining - bytestream2_get_bytes_left(gbc);
86 switch (section_type) {
87 case HAP_ST_COMPRESSOR_TABLE:
88 ret = ff_hap_set_chunk_count(ctx, section_size, is_first_table);
91 for (i = 0; i < section_size; i++) {
92 ctx->chunks[i].compressor = bytestream2_get_byte(gbc) << 4;
97 case HAP_ST_SIZE_TABLE:
98 ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
101 for (i = 0; i < section_size / 4; i++) {
102 ctx->chunks[i].compressed_size = bytestream2_get_le32(gbc);
107 case HAP_ST_OFFSET_TABLE:
108 ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
111 for (i = 0; i < section_size / 4; i++) {
112 ctx->chunks[i].compressed_offset = bytestream2_get_le32(gbc);
120 size -= section_size;
123 if (!had_sizes || !had_compressors)
124 return AVERROR_INVALIDDATA;
126 /* The offsets table is optional. If not present than calculate offsets by
127 * summing the sizes of preceding chunks. */
129 size_t running_size = 0;
130 for (i = 0; i < ctx->chunk_count; i++) {
131 ctx->chunks[i].compressed_offset = running_size;
132 running_size += ctx->chunks[i].compressed_size;
139 static int hap_can_use_tex_in_place(HapContext *ctx)
142 size_t running_offset = 0;
143 for (i = 0; i < ctx->chunk_count; i++) {
144 if (ctx->chunks[i].compressed_offset != running_offset
145 || ctx->chunks[i].compressor != HAP_COMP_NONE)
147 running_offset += ctx->chunks[i].compressed_size;
152 static int hap_parse_frame_header(AVCodecContext *avctx)
154 HapContext *ctx = avctx->priv_data;
155 GetByteContext *gbc = &ctx->gbc;
157 enum HapSectionType section_type;
158 const char *compressorstr;
161 ret = parse_section_header(gbc, §ion_size, §ion_type);
165 if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) != HAP_FMT_RGBDXT1)
166 || (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 0x0F) != HAP_FMT_RGBADXT5)
167 || (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
168 av_log(avctx, AV_LOG_ERROR, "Invalid texture format %#04x.\n", section_type & 0x0F);
169 return AVERROR_INVALIDDATA;
172 switch (section_type & 0xF0) {
174 case HAP_COMP_SNAPPY:
175 ret = ff_hap_set_chunk_count(ctx, 1, 1);
177 ctx->chunks[0].compressor = section_type & 0xF0;
178 ctx->chunks[0].compressed_offset = 0;
179 ctx->chunks[0].compressed_size = section_size;
181 if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
182 compressorstr = "none";
184 compressorstr = "snappy";
187 case HAP_COMP_COMPLEX:
188 ret = parse_section_header(gbc, §ion_size, §ion_type);
189 if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
190 ret = AVERROR_INVALIDDATA;
192 ret = hap_parse_decode_instructions(ctx, section_size);
193 compressorstr = "complex";
196 ret = AVERROR_INVALIDDATA;
203 /* Check the frame is valid and read the uncompressed chunk sizes */
205 for (i = 0; i < ctx->chunk_count; i++) {
206 HapChunk *chunk = &ctx->chunks[i];
208 /* Check the compressed buffer is valid */
209 if (chunk->compressed_offset + chunk->compressed_size > bytestream2_get_bytes_left(gbc))
210 return AVERROR_INVALIDDATA;
212 /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
214 chunk->uncompressed_offset = ctx->tex_size;
216 /* Fill out uncompressed size */
217 if (chunk->compressor == HAP_COMP_SNAPPY) {
218 GetByteContext gbc_tmp;
219 int64_t uncompressed_size;
220 bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
221 chunk->compressed_size);
222 uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
223 if (uncompressed_size < 0) {
224 return uncompressed_size;
226 chunk->uncompressed_size = uncompressed_size;
227 } else if (chunk->compressor == HAP_COMP_NONE) {
228 chunk->uncompressed_size = chunk->compressed_size;
230 return AVERROR_INVALIDDATA;
232 ctx->tex_size += chunk->uncompressed_size;
235 av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
240 static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
241 int chunk_nb, int thread_nb)
243 HapContext *ctx = avctx->priv_data;
245 HapChunk *chunk = &ctx->chunks[chunk_nb];
247 uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
249 bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
251 if (chunk->compressor == HAP_COMP_SNAPPY) {
253 int64_t uncompressed_size = ctx->tex_size;
254 /* Uncompress the frame */
255 ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
257 av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
260 } else if (chunk->compressor == HAP_COMP_NONE) {
261 bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
267 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
268 int slice, int thread_nb)
270 HapContext *ctx = avctx->priv_data;
271 AVFrame *frame = arg;
272 const uint8_t *d = ctx->tex_data;
273 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
275 int start_slice, end_slice;
277 start_slice = slice * ctx->slice_size;
278 end_slice = FFMIN(start_slice + ctx->slice_size, avctx->coded_height);
280 start_slice /= TEXTURE_BLOCK_H;
281 end_slice /= TEXTURE_BLOCK_H;
283 for (y = start_slice; y < end_slice; y++) {
284 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
285 int off = y * w_block;
286 for (x = 0; x < w_block; x++) {
287 ctx->tex_fun(p + x * 16, frame->linesize[0],
288 d + (off + x) * ctx->tex_rat);
295 static int hap_decode(AVCodecContext *avctx, void *data,
296 int *got_frame, AVPacket *avpkt)
298 HapContext *ctx = avctx->priv_data;
301 int slices = FFMIN(avctx->thread_count,
302 avctx->coded_height / TEXTURE_BLOCK_H);
304 ctx->slice_size = avctx->coded_height / slices;
306 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
308 /* Check for section header */
309 ret = hap_parse_frame_header(avctx);
313 /* Get the output frame ready to receive data */
315 ret = ff_thread_get_buffer(avctx, &tframe, 0);
318 if (avctx->codec->update_thread_context)
319 ff_thread_finish_setup(avctx);
321 /* Unpack the DXT texture */
322 if (hap_can_use_tex_in_place(ctx)) {
323 /* Only DXTC texture compression in a contiguous block */
324 ctx->tex_data = ctx->gbc.buffer;
326 /* Perform the second-stage decompression */
327 ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
331 avctx->execute2(avctx, decompress_chunks_thread, NULL,
332 ctx->chunk_results, ctx->chunk_count);
334 for (i = 0; i < ctx->chunk_count; i++) {
335 if (ctx->chunk_results[i] < 0)
336 return ctx->chunk_results[i];
339 ctx->tex_data = ctx->tex_buf;
342 /* Use the decompress function on the texture, one block per thread */
343 avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, slices);
345 /* Frame is ready to be output */
346 tframe.f->pict_type = AV_PICTURE_TYPE_I;
347 tframe.f->key_frame = 1;
353 static av_cold int hap_init(AVCodecContext *avctx)
355 HapContext *ctx = avctx->priv_data;
356 const char *texture_name;
357 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
360 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
361 avctx->width, avctx->height);
365 /* Since codec is based on 4x4 blocks, size is aligned to 4 */
366 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
367 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
369 /* Technically only one mode has alpha, but 32 bits are easier to handle */
370 avctx->pix_fmt = AV_PIX_FMT_RGBA;
372 ff_texturedsp_init(&ctx->dxtc);
374 switch (avctx->codec_tag) {
375 case MKTAG('H','a','p','1'):
376 texture_name = "DXT1";
378 ctx->tex_fun = ctx->dxtc.dxt1_block;
380 case MKTAG('H','a','p','5'):
381 texture_name = "DXT5";
383 ctx->tex_fun = ctx->dxtc.dxt5_block;
385 case MKTAG('H','a','p','Y'):
386 texture_name = "DXT5-YCoCg-scaled";
388 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
391 return AVERROR_DECODER_NOT_FOUND;
394 av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
399 static av_cold int hap_close(AVCodecContext *avctx)
401 HapContext *ctx = avctx->priv_data;
403 ff_hap_free_context(ctx);
408 AVCodec ff_hap_decoder = {
410 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
411 .type = AVMEDIA_TYPE_VIDEO,
412 .id = AV_CODEC_ID_HAP,
414 .decode = hap_decode,
416 .priv_data_size = sizeof(HapContext),
417 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
419 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
420 FF_CODEC_CAP_INIT_CLEANUP,