46dda0400d457fc1988f81ef1d561f9f51d9b66a
[libav.git] / libavcodec / hapdec.c
1 /*
2 * Vidvox Hap decoder
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Hap decoder
25 *
26 * Fourcc: Hap1, Hap5, HapY
27 *
28 * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
29 */
30
31 #include <stdint.h>
32
33 #include "libavutil/imgutils.h"
34
35 #include "avcodec.h"
36 #include "bytestream.h"
37 #include "hap.h"
38 #include "internal.h"
39 #include "snappy.h"
40 #include "texturedsp.h"
41 #include "thread.h"
42
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)
47 {
48 HapContext *ctx = avctx->priv_data;
49 GetByteContext *gbc = &ctx->gbc;
50 int length;
51
52 if (bytestream2_get_bytes_left(gbc) < 4)
53 return AVERROR_INVALIDDATA;
54
55 length = bytestream2_get_le24(gbc);
56
57 ctx->section_type = bytestream2_get_byte(gbc);
58
59 if (length == 0) {
60 if (bytestream2_get_bytes_left(gbc) < 4)
61 return AVERROR_INVALIDDATA;
62 length = bytestream2_get_le32(gbc);
63 }
64
65 if (length > bytestream2_get_bytes_left(gbc) || length == 0)
66 return AVERROR_INVALIDDATA;
67
68 return length;
69 }
70
71 /* Prepare the texture to be decompressed */
72 static int setup_texture(AVCodecContext *avctx, size_t length)
73 {
74 HapContext *ctx = avctx->priv_data;
75 GetByteContext *gbc = &ctx->gbc;
76 int64_t snappy_size;
77 const char *compressorstr;
78 int ret;
79
80 if ((avctx->codec_tag == MKTAG('H','a','p','1') && (ctx->section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
81 (avctx->codec_tag == MKTAG('H','a','p','5') && (ctx->section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
82 (avctx->codec_tag == MKTAG('H','a','p','Y') && (ctx->section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
83 av_log(avctx, AV_LOG_ERROR,
84 "Invalid texture format %#04x.\n", ctx->section_type & 0x0F);
85 return AVERROR_INVALIDDATA;
86 }
87
88 switch (ctx->section_type & 0xF0) {
89 case HAP_COMP_NONE:
90 /* Only DXTC texture compression */
91 ctx->tex_data = gbc->buffer;
92 ctx->tex_size = length;
93 compressorstr = "none";
94 break;
95 case HAP_COMP_SNAPPY:
96 snappy_size = ff_snappy_peek_uncompressed_length(gbc);
97 ret = av_reallocp(&ctx->snappied, snappy_size);
98 if (ret < 0) {
99 return ret;
100 }
101 /* Uncompress the frame */
102 ret = ff_snappy_uncompress(gbc, ctx->snappied, &snappy_size);
103 if (ret < 0) {
104 av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
105 return ret;
106 }
107
108 ctx->tex_data = ctx->snappied;
109 ctx->tex_size = snappy_size;
110 compressorstr = "snappy";
111 break;
112 case HAP_COMP_COMPLEX:
113 compressorstr = "complex";
114 avpriv_request_sample(avctx, "Complex Hap compressor");
115 return AVERROR_PATCHWELCOME;
116 break;
117 default:
118 av_log(avctx, AV_LOG_ERROR,
119 "Invalid compressor mode %02X.\n", ctx->section_type);
120 return AVERROR_INVALIDDATA;
121 }
122
123 av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
124
125 return 0;
126 }
127
128 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
129 int slice, int thread_nb)
130 {
131 HapContext *ctx = avctx->priv_data;
132 AVFrame *frame = arg;
133 const uint8_t *d = ctx->tex_data;
134 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
135 int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
136 int x, y;
137 int start_slice, end_slice;
138 int base_blocks_per_slice = h_block / ctx->slice_count;
139 int remainder_blocks = h_block % ctx->slice_count;
140
141 /* When the frame height (in blocks) doesn't divide evenly between the
142 * number of slices, spread the remaining blocks evenly between the first
143 * operations */
144 start_slice = slice * base_blocks_per_slice;
145 /* Add any extra blocks (one per slice) that have been added before this slice */
146 start_slice += FFMIN(slice, remainder_blocks);
147
148 end_slice = start_slice + base_blocks_per_slice;
149 /* Add an extra block if there are still remainder blocks to be accounted for */
150 if (slice < remainder_blocks)
151 end_slice++;
152
153 for (y = start_slice; y < end_slice; y++) {
154 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
155 int off = y * w_block;
156 for (x = 0; x < w_block; x++) {
157 ctx->tex_fun(p + x * 16, frame->linesize[0],
158 d + (off + x) * ctx->tex_rat);
159 }
160 }
161
162 return 0;
163 }
164
165 static int hap_decode(AVCodecContext *avctx, void *data,
166 int *got_frame, AVPacket *avpkt)
167 {
168 HapContext *ctx = avctx->priv_data;
169 ThreadFrame tframe;
170 int ret, length;
171
172 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
173
174 /* Check for section header */
175 length = parse_section_header(avctx);
176 if (length < 0) {
177 av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
178 return length;
179 }
180
181 /* Prepare the texture buffer and decompress function */
182 ret = setup_texture(avctx, length);
183 if (ret < 0)
184 return ret;
185
186 /* Get the output frame ready to receive data */
187 tframe.f = data;
188 ret = ff_thread_get_buffer(avctx, &tframe, 0);
189 if (ret < 0)
190 return ret;
191 ff_thread_finish_setup(avctx);
192
193 /* Use the decompress function on the texture, one block per thread */
194 avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
195
196 /* Frame is ready to be output */
197 tframe.f->pict_type = AV_PICTURE_TYPE_I;
198 tframe.f->key_frame = 1;
199 *got_frame = 1;
200
201 return avpkt->size;
202 }
203
204 static av_cold int hap_init(AVCodecContext *avctx)
205 {
206 HapContext *ctx = avctx->priv_data;
207 const char *texture_name;
208 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
209
210 if (ret < 0) {
211 av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
212 avctx->width, avctx->height);
213 return ret;
214 }
215
216 /* Since codec is based on 4x4 blocks, size is aligned to 4 */
217 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
218 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
219
220 /* Technically only one mode has alpha, but 32 bits are easier to handle */
221 avctx->pix_fmt = AV_PIX_FMT_RGBA;
222
223 ff_texturedsp_init(&ctx->dxtc);
224
225 switch (avctx->codec_tag) {
226 case MKTAG('H','a','p','1'):
227 texture_name = "DXT1";
228 ctx->tex_rat = 8;
229 ctx->tex_fun = ctx->dxtc.dxt1_block;
230 break;
231 case MKTAG('H','a','p','5'):
232 texture_name = "DXT5";
233 ctx->tex_rat = 16;
234 ctx->tex_fun = ctx->dxtc.dxt5_block;
235 break;
236 case MKTAG('H','a','p','Y'):
237 texture_name = "DXT5-YCoCg-scaled";
238 ctx->tex_rat = 16;
239 ctx->tex_fun = ctx->dxtc.dxt5ys_block;
240 break;
241 default:
242 return AVERROR_DECODER_NOT_FOUND;
243 }
244
245 av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
246
247 ctx->slice_count = av_clip(avctx->thread_count, 1,
248 avctx->coded_height / TEXTURE_BLOCK_H);
249
250 return 0;
251 }
252
253 static av_cold int hap_close(AVCodecContext *avctx)
254 {
255 HapContext *ctx = avctx->priv_data;
256
257 av_freep(&ctx->snappied);
258
259 return 0;
260 }
261
262 AVCodec ff_hap_decoder = {
263 .name = "hap",
264 .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
265 .type = AVMEDIA_TYPE_VIDEO,
266 .id = AV_CODEC_ID_HAP,
267 .init = hap_init,
268 .decode = hap_decode,
269 .close = hap_close,
270 .priv_data_size = sizeof(HapContext),
271 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
272 AV_CODEC_CAP_DR1,
273 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
274 FF_CODEC_CAP_INIT_CLEANUP,
275 };