3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 * Copyright (C) 2015 Tom Butterworth <bangnoise@gmail.com>
6 * HapQA and HAPAlphaOnly added by Jokyo Images
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 * Fourcc: Hap1, Hap5, HapY, HapA, HapM
31 * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
36 #include "libavutil/imgutils.h"
39 #include "bytestream.h"
43 #include "texturedsp.h"
46 static int hap_parse_decode_instructions(HapContext *ctx, int size)
48 GetByteContext *gbc = &ctx->gbc;
50 enum HapSectionType section_type;
51 int is_first_table = 1, had_offsets = 0, had_compressors = 0, had_sizes = 0;
55 int stream_remaining = bytestream2_get_bytes_left(gbc);
56 ret = ff_hap_parse_section_header(gbc, §ion_size, §ion_type);
60 size -= stream_remaining - bytestream2_get_bytes_left(gbc);
62 switch (section_type) {
63 case HAP_ST_COMPRESSOR_TABLE:
64 ret = ff_hap_set_chunk_count(ctx, section_size, is_first_table);
67 for (i = 0; i < section_size; i++) {
68 ctx->chunks[i].compressor = bytestream2_get_byte(gbc) << 4;
73 case HAP_ST_SIZE_TABLE:
74 ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
77 for (i = 0; i < section_size / 4; i++) {
78 ctx->chunks[i].compressed_size = bytestream2_get_le32(gbc);
83 case HAP_ST_OFFSET_TABLE:
84 ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
87 for (i = 0; i < section_size / 4; i++) {
88 ctx->chunks[i].compressed_offset = bytestream2_get_le32(gbc);
99 if (!had_sizes || !had_compressors)
100 return AVERROR_INVALIDDATA;
102 /* The offsets table is optional. If not present than calculate offsets by
103 * summing the sizes of preceding chunks. */
105 size_t running_size = 0;
106 for (i = 0; i < ctx->chunk_count; i++) {
107 ctx->chunks[i].compressed_offset = running_size;
108 if (ctx->chunks[i].compressed_size > UINT32_MAX - running_size)
109 return AVERROR_INVALIDDATA;
110 running_size += ctx->chunks[i].compressed_size;
117 static int hap_can_use_tex_in_place(HapContext *ctx)
120 size_t running_offset = 0;
121 for (i = 0; i < ctx->chunk_count; i++) {
122 if (ctx->chunks[i].compressed_offset != running_offset
123 || ctx->chunks[i].compressor != HAP_COMP_NONE)
125 running_offset += ctx->chunks[i].compressed_size;
130 static int hap_parse_frame_header(AVCodecContext *avctx)
132 HapContext *ctx = avctx->priv_data;
133 GetByteContext *gbc = &ctx->gbc;
135 enum HapSectionType section_type;
136 const char *compressorstr;
139 ret = ff_hap_parse_section_header(gbc, &ctx->texture_section_size, §ion_type);
143 if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
144 (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
145 (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 0x0F) != HAP_FMT_YCOCGDXT5) ||
146 (avctx->codec_tag == MKTAG('H','a','p','A') && (section_type & 0x0F) != HAP_FMT_RGTC1) ||
147 ((avctx->codec_tag == MKTAG('H','a','p','M') && (section_type & 0x0F) != HAP_FMT_RGTC1) &&
148 (section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
149 av_log(avctx, AV_LOG_ERROR,
150 "Invalid texture format %#04x.\n", section_type & 0x0F);
151 return AVERROR_INVALIDDATA;
154 switch (section_type & 0xF0) {
156 case HAP_COMP_SNAPPY:
157 ret = ff_hap_set_chunk_count(ctx, 1, 1);
159 ctx->chunks[0].compressor = section_type & 0xF0;
160 ctx->chunks[0].compressed_offset = 0;
161 ctx->chunks[0].compressed_size = ctx->texture_section_size;
163 if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
164 compressorstr = "none";
166 compressorstr = "snappy";
169 case HAP_COMP_COMPLEX:
170 ret = ff_hap_parse_section_header(gbc, §ion_size, §ion_type);
171 if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
172 ret = AVERROR_INVALIDDATA;
174 ret = hap_parse_decode_instructions(ctx, section_size);
175 compressorstr = "complex";
178 ret = AVERROR_INVALIDDATA;
185 /* Check the frame is valid and read the uncompressed chunk sizes */
187 for (i = 0; i < ctx->chunk_count; i++) {
188 HapChunk *chunk = &ctx->chunks[i];
190 /* Check the compressed buffer is valid */
191 if (chunk->compressed_offset + (uint64_t)chunk->compressed_size > bytestream2_get_bytes_left(gbc))
192 return AVERROR_INVALIDDATA;
194 /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
196 chunk->uncompressed_offset = ctx->tex_size;
198 /* Fill out uncompressed size */
199 if (chunk->compressor == HAP_COMP_SNAPPY) {
200 GetByteContext gbc_tmp;
201 int64_t uncompressed_size;
202 bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
203 chunk->compressed_size);
204 uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
205 if (uncompressed_size < 0) {
206 return uncompressed_size;
208 chunk->uncompressed_size = uncompressed_size;
209 } else if (chunk->compressor == HAP_COMP_NONE) {
210 chunk->uncompressed_size = chunk->compressed_size;
212 return AVERROR_INVALIDDATA;
214 ctx->tex_size += chunk->uncompressed_size;
217 av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
222 static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
223 int chunk_nb, int thread_nb)
225 HapContext *ctx = avctx->priv_data;
227 HapChunk *chunk = &ctx->chunks[chunk_nb];
229 uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
231 bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
233 if (chunk->compressor == HAP_COMP_SNAPPY) {
235 int64_t uncompressed_size = ctx->tex_size;
237 /* Uncompress the frame */
238 ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
240 av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
243 } else if (chunk->compressor == HAP_COMP_NONE) {
244 bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
250 static int decompress_texture_thread_internal(AVCodecContext *avctx, void *arg,
251 int slice, int thread_nb, int texture_num)
253 HapContext *ctx = avctx->priv_data;
254 AVFrame *frame = arg;
255 const uint8_t *d = ctx->tex_data;
256 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
257 int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
259 int start_slice, end_slice;
260 int base_blocks_per_slice = h_block / ctx->slice_count;
261 int remainder_blocks = h_block % ctx->slice_count;
263 /* When the frame height (in blocks) doesn't divide evenly between the
264 * number of slices, spread the remaining blocks evenly between the first
266 start_slice = slice * base_blocks_per_slice;
267 /* Add any extra blocks (one per slice) that have been added before this slice */
268 start_slice += FFMIN(slice, remainder_blocks);
270 end_slice = start_slice + base_blocks_per_slice;
271 /* Add an extra block if there are still remainder blocks to be accounted for */
272 if (slice < remainder_blocks)
275 for (y = start_slice; y < end_slice; y++) {
276 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
277 int off = y * w_block;
278 for (x = 0; x < w_block; x++) {
279 if (texture_num == 0) {
280 ctx->tex_fun(p + x * 4 * ctx->uncompress_pix_size, frame->linesize[0],
281 d + (off + x) * ctx->tex_rat);
283 ctx->tex_fun2(p + x * 4 * ctx->uncompress_pix_size, frame->linesize[0],
284 d + (off + x) * ctx->tex_rat2);
292 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
293 int slice, int thread_nb)
295 return decompress_texture_thread_internal(avctx, arg, slice, thread_nb, 0);
298 static int decompress_texture2_thread(AVCodecContext *avctx, void *arg,
299 int slice, int thread_nb)
301 return decompress_texture_thread_internal(avctx, arg, slice, thread_nb, 1);
304 static int hap_decode(AVCodecContext *avctx, void *data,
305 int *got_frame, AVPacket *avpkt)
307 HapContext *ctx = avctx->priv_data;
311 enum HapSectionType section_type;
312 int start_texture_section = 0;
313 int tex_rat[2] = {0, 0};
315 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
317 tex_rat[0] = ctx->tex_rat;
319 /* check for multi texture header */
320 if (ctx->texture_count == 2) {
321 ret = ff_hap_parse_section_header(&ctx->gbc, §ion_size, §ion_type);
324 if ((section_type & 0x0F) != 0x0D) {
325 av_log(avctx, AV_LOG_ERROR, "Invalid section type in 2 textures mode %#04x.\n", section_type);
326 return AVERROR_INVALIDDATA;
328 start_texture_section = 4;
329 tex_rat[1] = ctx->tex_rat2;
332 /* Get the output frame ready to receive data */
334 ret = ff_thread_get_buffer(avctx, &tframe, 0);
338 for (t = 0; t < ctx->texture_count; t++) {
339 bytestream2_seek(&ctx->gbc, start_texture_section, SEEK_SET);
341 /* Check for section header */
342 ret = hap_parse_frame_header(avctx);
346 if (ctx->tex_size != (avctx->coded_width / TEXTURE_BLOCK_W)
347 *(avctx->coded_height / TEXTURE_BLOCK_H)
349 av_log(avctx, AV_LOG_ERROR, "uncompressed size mismatches\n");
350 return AVERROR_INVALIDDATA;
353 start_texture_section += ctx->texture_section_size + 4;
355 if (avctx->codec->update_thread_context)
356 ff_thread_finish_setup(avctx);
358 /* Unpack the DXT texture */
359 if (hap_can_use_tex_in_place(ctx)) {
361 /* Only DXTC texture compression in a contiguous block */
362 ctx->tex_data = ctx->gbc.buffer;
363 tex_size = FFMIN(ctx->texture_section_size, bytestream2_get_bytes_left(&ctx->gbc));
364 if (tex_size < (avctx->coded_width / TEXTURE_BLOCK_W)
365 *(avctx->coded_height / TEXTURE_BLOCK_H)
367 av_log(avctx, AV_LOG_ERROR, "Insufficient data\n");
368 return AVERROR_INVALIDDATA;
371 /* Perform the second-stage decompression */
372 ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
376 avctx->execute2(avctx, decompress_chunks_thread, NULL,
377 ctx->chunk_results, ctx->chunk_count);
379 for (i = 0; i < ctx->chunk_count; i++) {
380 if (ctx->chunk_results[i] < 0)
381 return ctx->chunk_results[i];
384 ctx->tex_data = ctx->tex_buf;
387 /* Use the decompress function on the texture, one block per thread */
389 avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
392 avctx->execute2(avctx, decompress_texture2_thread, tframe.f, NULL, ctx->slice_count);
396 /* Frame is ready to be output */
397 tframe.f->pict_type = AV_PICTURE_TYPE_I;
398 tframe.f->key_frame = 1;
404 static av_cold int hap_init(AVCodecContext *avctx)
406 HapContext *ctx = avctx->priv_data;
407 const char *texture_name;
408 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
411 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
412 avctx->width, avctx->height);
416 /* Since codec is based on 4x4 blocks, size is aligned to 4 */
417 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
418 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
420 ff_texturedsp_init(&ctx->dxtc);
422 ctx->texture_count = 1;
423 ctx->uncompress_pix_size = 4;
425 switch (avctx->codec_tag) {
426 case MKTAG('H','a','p','1'):
427 texture_name = "DXT1";
429 ctx->tex_fun = ctx->dxtc.dxt1_block;
430 avctx->pix_fmt = AV_PIX_FMT_RGB0;
432 case MKTAG('H','a','p','5'):
433 texture_name = "DXT5";
435 ctx->tex_fun = ctx->dxtc.dxt5_block;
436 avctx->pix_fmt = AV_PIX_FMT_RGBA;
438 case MKTAG('H','a','p','Y'):
439 texture_name = "DXT5-YCoCg-scaled";
441 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
442 avctx->pix_fmt = AV_PIX_FMT_RGB0;
444 case MKTAG('H','a','p','A'):
445 texture_name = "RGTC1";
447 ctx->tex_fun = ctx->dxtc.rgtc1u_gray_block;
448 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
449 ctx->uncompress_pix_size = 1;
451 case MKTAG('H','a','p','M'):
452 texture_name = "DXT5-YCoCg-scaled / RGTC1";
455 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
456 ctx->tex_fun2 = ctx->dxtc.rgtc1u_alpha_block;
457 avctx->pix_fmt = AV_PIX_FMT_RGBA;
458 ctx->texture_count = 2;
461 return AVERROR_DECODER_NOT_FOUND;
464 av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
466 ctx->slice_count = av_clip(avctx->thread_count, 1,
467 avctx->coded_height / TEXTURE_BLOCK_H);
472 static av_cold int hap_close(AVCodecContext *avctx)
474 HapContext *ctx = avctx->priv_data;
476 ff_hap_free_context(ctx);
481 const AVCodec ff_hap_decoder = {
483 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap"),
484 .type = AVMEDIA_TYPE_VIDEO,
485 .id = AV_CODEC_ID_HAP,
487 .decode = hap_decode,
489 .priv_data_size = sizeof(HapContext),
490 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
492 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
493 FF_CODEC_CAP_INIT_CLEANUP,
494 .codec_tags = (const uint32_t []){
495 MKTAG('H','a','p','1'),
496 MKTAG('H','a','p','5'),
497 MKTAG('H','a','p','Y'),
498 MKTAG('H','a','p','A'),
499 MKTAG('H','a','p','M'),