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 if ((avctx->codec_tag == MKTAG('H','a','p','1') && (ctx->section_type & 0x0F) != HAP_FMT_RGBDXT1)
82 || (avctx->codec_tag == MKTAG('H','a','p','5') && (ctx->section_type & 0x0F) != HAP_FMT_RGBADXT5)
83 || (avctx->codec_tag == MKTAG('H','a','p','Y') && (ctx->section_type & 0x0F) != HAP_FMT_YCOCGDXT5))
84 return AVERROR_INVALIDDATA;
86 switch (ctx->section_type & 0x0F) {
88 texture_name = "DXT1";
90 case HAP_FMT_RGBADXT5:
91 texture_name = "DXT5";
93 case HAP_FMT_YCOCGDXT5:
94 texture_name = "DXT5-YCoCg-scaled";
97 av_log(avctx, AV_LOG_ERROR,
98 "Invalid format mode %02X.\n", ctx->section_type);
99 return AVERROR_INVALIDDATA;
102 switch (ctx->section_type & 0xF0) {
104 /* Only DXTC texture compression */
105 ctx->tex_data = gbc->buffer;
106 ctx->tex_size = length;
107 compressorstr = "none";
109 case HAP_COMP_SNAPPY:
110 snappy_size = ff_snappy_peek_uncompressed_length(gbc);
111 ret = av_reallocp(&ctx->snappied, snappy_size);
115 /* Uncompress the frame */
116 ret = ff_snappy_uncompress(gbc, ctx->snappied, &snappy_size);
118 av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
122 ctx->tex_data = ctx->snappied;
123 ctx->tex_size = snappy_size;
124 compressorstr = "snappy";
126 case HAP_COMP_COMPLEX:
127 compressorstr = "complex";
128 avpriv_request_sample(avctx, "Complex Hap compressor");
129 return AVERROR_PATCHWELCOME;
132 av_log(avctx, AV_LOG_ERROR,
133 "Invalid compressor mode %02X.\n", ctx->section_type);
134 return AVERROR_INVALIDDATA;
137 av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
138 texture_name, compressorstr);
143 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
144 int block_nb, int thread_nb)
146 HapContext *ctx = avctx->priv_data;
147 AVFrame *frame = arg;
148 int x = (TEXTURE_BLOCK_W * block_nb) % avctx->coded_width;
149 int y = TEXTURE_BLOCK_H * (TEXTURE_BLOCK_W * block_nb / avctx->coded_width);
150 uint8_t *p = frame->data[0] + x * 4 + y * frame->linesize[0];
151 const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_rat;
153 ctx->tex_fun(p, frame->linesize[0], d);
157 static int hap_decode(AVCodecContext *avctx, void *data,
158 int *got_frame, AVPacket *avpkt)
160 HapContext *ctx = avctx->priv_data;
163 int blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H);
165 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
167 /* Check for section header */
168 length = parse_section_header(avctx);
170 av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
174 /* Prepare the texture buffer and decompress function */
175 ret = setup_texture(avctx, length);
179 /* Get the output frame ready to receive data */
181 ret = ff_thread_get_buffer(avctx, &tframe, 0);
184 if (avctx->codec->update_thread_context)
185 ff_thread_finish_setup(avctx);
187 /* Use the decompress function on the texture, one block per thread */
188 avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, blocks);
190 /* Frame is ready to be output */
191 tframe.f->pict_type = AV_PICTURE_TYPE_I;
192 tframe.f->key_frame = 1;
198 static av_cold int hap_init(AVCodecContext *avctx)
200 HapContext *ctx = avctx->priv_data;
201 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
204 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
205 avctx->width, avctx->height);
209 /* Since codec is based on 4x4 blocks, size is aligned to 4 */
210 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
211 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
213 /* Technically only one mode has alpha, but 32 bits are easier to handle */
214 avctx->pix_fmt = AV_PIX_FMT_RGBA;
216 ff_texturedsp_init(&ctx->dxtc);
218 switch (avctx->codec_tag) {
219 case MKTAG('H','a','p','1'):
221 ctx->tex_fun = ctx->dxtc.dxt1_block;
223 case MKTAG('H','a','p','5'):
225 ctx->tex_fun = ctx->dxtc.dxt5_block;
227 case MKTAG('H','a','p','Y'):
229 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
232 return AVERROR_DECODER_NOT_FOUND;
237 static av_cold int hap_close(AVCodecContext *avctx)
239 HapContext *ctx = avctx->priv_data;
241 av_freep(&ctx->snappied);
246 AVCodec ff_hap_decoder = {
248 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
249 .type = AVMEDIA_TYPE_VIDEO,
250 .id = AV_CODEC_ID_HAP,
252 .decode = hap_decode,
254 .priv_data_size = sizeof(HapContext),
255 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
257 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
258 FF_CODEC_CAP_INIT_CLEANUP,