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
52 int is_first_table = 1, had_offsets = 0, had_compressors = 0, had_sizes = 0;
63 switch (section_type) {
68 for (
i = 0;
i < section_size;
i++) {
69 ctx->chunks[
i].compressor = bytestream2_get_byte(gbc) << 4;
78 for (
i = 0;
i < section_size / 4;
i++) {
79 ctx->chunks[
i].compressed_size = bytestream2_get_le32(gbc);
88 for (
i = 0;
i < section_size / 4;
i++) {
89 ctx->chunks[
i].compressed_offset = bytestream2_get_le32(gbc);
100 if (!had_sizes || !had_compressors)
103 /* The offsets table is optional. If not present than calculate offsets by
104 * summing the sizes of preceding chunks. */
106 size_t running_size = 0;
107 for (
i = 0;
i <
ctx->chunk_count;
i++) {
108 ctx->chunks[
i].compressed_offset = running_size;
109 if (
ctx->chunks[
i].compressed_size > UINT32_MAX - running_size)
111 running_size +=
ctx->chunks[
i].compressed_size;
121 size_t running_offset = 0;
122 for (
i = 0;
i <
ctx->chunk_count;
i++) {
123 if (
ctx->chunks[
i].compressed_offset != running_offset
126 running_offset +=
ctx->chunks[
i].compressed_size;
137 const char *compressorstr;
151 "Invalid texture format %#04x.\n", section_type & 0x0F);
155 switch (section_type & 0xF0) {
160 ctx->chunks[0].compressor = section_type & 0xF0;
161 ctx->chunks[0].compressed_offset = 0;
162 ctx->chunks[0].compressed_size =
ctx->texture_section_size;
165 compressorstr =
"none";
167 compressorstr =
"snappy";
176 compressorstr =
"complex";
186 /* Check the frame is valid and read the uncompressed chunk sizes */
188 for (
i = 0;
i <
ctx->chunk_count;
i++) {
191 /* Check the compressed buffer is valid */
195 /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
199 /* Fill out uncompressed size */
206 if (uncompressed_size < 0) {
207 return uncompressed_size;
224 int chunk_nb,
int thread_nb)
238 /* Uncompress the frame */
258 int start_texture_section = 0;
262 /* check for multi texture header */
263 if (
ctx->texture_count == 2) {
267 if ((section_type & 0x0F) != 0x0D) {
268 av_log(avctx,
AV_LOG_ERROR,
"Invalid section type in 2 textures mode %#04x.\n", section_type);
271 start_texture_section = 4;
274 /* Get the output frame ready to receive data */
279 for (t = 0; t <
ctx->texture_count; t++) {
282 /* Check for section header */
289 *
ctx->dec[t].tex_ratio) {
294 start_texture_section +=
ctx->texture_section_size + 4;
296 /* Unpack the DXT texture */
299 /* Only DXTC texture compression in a contiguous block */
300 ctx->dec[t].tex_data.in =
ctx->gbc.buffer;
304 *
ctx->dec[t].tex_ratio) {
309 /* Perform the second-stage decompression */
313 memset(
ctx->tex_buf, 0,
ctx->tex_size);
316 ctx->chunk_results,
ctx->chunk_count);
318 for (
i = 0;
i <
ctx->chunk_count;
i++) {
319 if (
ctx->chunk_results[
i] < 0)
320 return ctx->chunk_results[
i];
323 ctx->dec[t].tex_data.in =
ctx->tex_buf;
326 ctx->dec[t].frame_data.out =
frame->data[0];
327 ctx->dec[t].stride =
frame->linesize[0];
333 /* Frame is ready to be output */
343 const char *texture_name;
352 /* Since codec is based on 4x4 blocks, size is aligned to 4 */
358 ctx->texture_count = 1;
359 ctx->dec[0].raw_ratio = 16;
364 case MKTAG(
'H',
'a',
'p',
'1'):
365 texture_name =
"DXT1";
366 ctx->dec[0].tex_ratio = 8;
370 case MKTAG(
'H',
'a',
'p',
'5'):
371 texture_name =
"DXT5";
372 ctx->dec[0].tex_ratio = 16;
376 case MKTAG(
'H',
'a',
'p',
'Y'):
377 texture_name =
"DXT5-YCoCg-scaled";
378 ctx->dec[0].tex_ratio = 16;
382 case MKTAG(
'H',
'a',
'p',
'A'):
383 texture_name =
"RGTC1";
384 ctx->dec[0].tex_ratio = 8;
386 ctx->dec[0].raw_ratio = 4;
389 case MKTAG(
'H',
'a',
'p',
'M'):
390 texture_name =
"DXT5-YCoCg-scaled / RGTC1";
391 ctx->dec[0].tex_ratio = 16;
392 ctx->dec[1].tex_ratio = 8;
395 ctx->dec[1].raw_ratio = 16;
396 ctx->dec[1].slice_count =
ctx->dec[0].slice_count;
398 ctx->texture_count = 2;
430 .codec_tags = (
const uint32_t []){
431 MKTAG(
'H',
'a',
'p',
'1'),
432 MKTAG(
'H',
'a',
'p',
'5'),
433 MKTAG(
'H',
'a',
'p',
'Y'),
434 MKTAG(
'H',
'a',
'p',
'A'),
435 MKTAG(
'H',
'a',
'p',
'M'),
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
const FFCodec ff_hap_decoder
static AVFormatContext * ctx
Libavcodec external API header.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
#define i(width, name, range_min, range_max)
#define FF_CODEC_TAGS_END
FFCodec.codec_tags termination value.
#define FF_CODEC_DECODE_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_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
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...
av_cold void ff_hap_free_context(HapContext *ctx)
int ff_hap_set_chunk_count(HapContext *ctx, int count, int first_in_frame)
int ff_hap_parse_section_header(GetByteContext *gbc, int *section_size, enum HapSectionType *section_type)
@ HAP_ST_DECODE_INSTRUCTIONS
@ HAP_ST_COMPRESSOR_TABLE
static av_cold int hap_init(AVCodecContext *avctx)
static int hap_parse_frame_header(AVCodecContext *avctx)
static av_cold int hap_close(AVCodecContext *avctx)
static int hap_parse_decode_instructions(HapContext *ctx, int size)
static int decompress_chunks_thread(AVCodecContext *avctx, void *arg, int chunk_nb, int thread_nb)
static int hap_can_use_tex_in_place(HapContext *ctx)
static int hap_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Multithreading API for decoders.
#define MKTAG(a, b, c, d)
Memory handling functions.
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
int64_t ff_snappy_peek_uncompressed_length(GetByteContext *gb)
Get the uncompressed length of an input buffer compressed using the Snappy algorithm.
int ff_snappy_uncompress(GetByteContext *gb, uint8_t *buf, int64_t *size)
Decompress an input buffer using Snappy algorithm.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
int coded_width
Bitstream width / height, may be different from width/height e.g.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
uint32_t compressed_offset
enum HapCompressor compressor
int(* dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
int(* dxt5ys_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
int(* rgtc1u_gray_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
int(* dxt5_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
int(* rgtc1u_alpha_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Texture block (4x4) module.
int ff_texturedsp_exec_decompress_threads(struct AVCodecContext *avctx, TextureDSPThreadContext *ctx)