+ switch (section_type & 0xF0) {
+ case HAP_COMP_NONE:
+ case HAP_COMP_SNAPPY:
+ ret = ff_hap_set_chunk_count(ctx, 1, 1);
+ if (ret == 0) {
+ ctx->chunks[0].compressor = section_type & 0xF0;
+ ctx->chunks[0].compressed_offset = 0;
+ ctx->chunks[0].compressed_size = section_size;
+ }
+ if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
+ compressorstr = "none";
+ } else {
+ compressorstr = "snappy";
+ }
+ break;
+ case HAP_COMP_COMPLEX:
+ ret = parse_section_header(gbc, §ion_size, §ion_type);
+ if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
+ ret = AVERROR_INVALIDDATA;
+ if (ret == 0)
+ ret = hap_parse_decode_instructions(ctx, section_size);
+ compressorstr = "complex";
+ break;
+ default:
+ ret = AVERROR_INVALIDDATA;
+ break;
+ }
+
+ if (ret != 0)
+ return ret;
+
+ /* Check the frame is valid and read the uncompressed chunk sizes */
+ ctx->tex_size = 0;
+ for (i = 0; i < ctx->chunk_count; i++) {
+ HapChunk *chunk = &ctx->chunks[i];
+
+ /* Check the compressed buffer is valid */
+ if (chunk->compressed_offset + chunk->compressed_size > bytestream2_get_bytes_left(gbc))
+ return AVERROR_INVALIDDATA;
+
+ /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
+ * size thus far */
+ chunk->uncompressed_offset = ctx->tex_size;
+
+ /* Fill out uncompressed size */
+ if (chunk->compressor == HAP_COMP_SNAPPY) {
+ GetByteContext gbc_tmp;
+ int64_t uncompressed_size;
+ bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
+ chunk->compressed_size);
+ uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
+ if (uncompressed_size < 0) {
+ return uncompressed_size;
+ }
+ chunk->uncompressed_size = uncompressed_size;
+ } else if (chunk->compressor == HAP_COMP_NONE) {
+ chunk->uncompressed_size = chunk->compressed_size;
+ } else {
+ return AVERROR_INVALIDDATA;
+ }
+ ctx->tex_size += chunk->uncompressed_size;
+ }
+
+ av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
+
+ return ret;
+}
+
+static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
+ int chunk_nb, int thread_nb)
+{
+ HapContext *ctx = avctx->priv_data;
+
+ HapChunk *chunk = &ctx->chunks[chunk_nb];
+ GetByteContext gbc;
+ uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
+
+ bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
+
+ if (chunk->compressor == HAP_COMP_SNAPPY) {
+ int ret;
+ int64_t uncompressed_size = ctx->tex_size;
+