3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * Fourcc: Hap1, Hap5, HapY
28 * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
33 #include "libavutil/imgutils.h"
36 #include "bytestream.h"
40 #include "texturedsp.h"
43 /* The first three bytes are the size of the section past the header, or zero
44 * if the length is stored in the next long word. The fourth byte in the first
45 * long word indicates the type of the current section. */
46 static int parse_section_header(AVCodecContext *avctx)
48 HapContext *ctx = avctx->priv_data;
49 GetByteContext *gbc = &ctx->gbc;
52 if (bytestream2_get_bytes_left(gbc) < 4)
53 return AVERROR_INVALIDDATA;
55 length = bytestream2_get_le24(gbc);
57 ctx->section_type = bytestream2_get_byte(gbc);
60 if (bytestream2_get_bytes_left(gbc) < 4)
61 return AVERROR_INVALIDDATA;
62 length = bytestream2_get_le32(gbc);
65 if (length > bytestream2_get_bytes_left(gbc) || length == 0)
66 return AVERROR_INVALIDDATA;
71 /* Prepare the texture to be decompressed */
72 static int setup_texture(AVCodecContext *avctx, size_t length)
74 HapContext *ctx = avctx->priv_data;
75 GetByteContext *gbc = &ctx->gbc;
77 const char *texture_name;
78 const char *compressorstr;
81 switch (ctx->section_type & 0x0F) {
84 ctx->tex_fun = ctx->dxtc.dxt1_block;
85 texture_name = "DXT1";
87 case HAP_FMT_RGBADXT5:
89 ctx->tex_fun = ctx->dxtc.dxt5_block;
90 texture_name = "DXT5";
92 case HAP_FMT_YCOCGDXT5:
94 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
95 texture_name = "DXT5-YCoCg-scaled";
98 av_log(avctx, AV_LOG_ERROR,
99 "Invalid format mode %02X.\n", ctx->section_type);
100 return AVERROR_INVALIDDATA;
103 switch (ctx->section_type & 0xF0) {
105 /* Only DXTC texture compression */
106 ctx->tex_data = gbc->buffer;
107 ctx->tex_size = length;
108 compressorstr = "none";
110 case HAP_COMP_SNAPPY:
111 /* Uncompress the frame */
112 ret = ff_snappy_uncompress(gbc, &ctx->snappied, &snappy_size);
114 av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
118 ctx->tex_data = ctx->snappied;
119 ctx->tex_size = snappy_size;
120 compressorstr = "snappy";
122 case HAP_COMP_COMPLEX:
123 compressorstr = "complex";
124 avpriv_request_sample(avctx, "Complex Hap compressor");
125 return AVERROR_PATCHWELCOME;
128 av_log(avctx, AV_LOG_ERROR,
129 "Invalid compressor mode %02X.\n", ctx->section_type);
130 return AVERROR_INVALIDDATA;
133 av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
134 texture_name, compressorstr);
139 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
140 int block_nb, int thread_nb)
142 HapContext *ctx = avctx->priv_data;
143 AVFrame *frame = arg;
144 int x = (TEXTURE_BLOCK_W * block_nb) % avctx->coded_width;
145 int y = TEXTURE_BLOCK_H * (TEXTURE_BLOCK_W * block_nb / avctx->coded_width);
146 uint8_t *p = frame->data[0] + x * 4 + y * frame->linesize[0];
147 const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_rat;
149 ctx->tex_fun(p, frame->linesize[0], d);
153 static int hap_decode(AVCodecContext *avctx, void *data,
154 int *got_frame, AVPacket *avpkt)
156 HapContext *ctx = avctx->priv_data;
159 int blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H);
161 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
163 /* Check for section header */
164 length = parse_section_header(avctx);
166 av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
170 /* Prepare the texture buffer and decompress function */
171 ret = setup_texture(avctx, length);
175 /* Get the output frame ready to receive data */
177 ret = ff_thread_get_buffer(avctx, &tframe, 0);
180 ff_thread_finish_setup(avctx);
182 /* Use the decompress function on the texture, one block per thread */
183 avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, blocks);
185 /* Frame is ready to be output */
186 tframe.f->pict_type = AV_PICTURE_TYPE_I;
187 tframe.f->key_frame = 1;
193 static av_cold int hap_init(AVCodecContext *avctx)
195 HapContext *ctx = avctx->priv_data;
196 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
199 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
200 avctx->width, avctx->height);
204 /* Since codec is based on 4x4 blocks, size is aligned to 4 */
205 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
206 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
208 /* Technically only one mode has alpha, but 32 bits are easier to handle */
209 avctx->pix_fmt = AV_PIX_FMT_RGBA;
211 ff_texturedsp_init(&ctx->dxtc);
216 static av_cold int hap_close(AVCodecContext *avctx)
218 HapContext *ctx = avctx->priv_data;
220 av_freep(&ctx->snappied);
225 AVCodec ff_hap_decoder = {
227 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
228 .type = AVMEDIA_TYPE_VIDEO,
229 .id = AV_CODEC_ID_HAP,
231 .decode = hap_decode,
233 .priv_data_size = sizeof(HapContext),
234 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
236 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
237 FF_CODEC_CAP_INIT_CLEANUP,