]> git.sesse.net Git - ffmpeg/blob - libavcodec/hapdec.c
Merge commit '32c8d89c036b0e75ece74aea638df587099def0b'
[ffmpeg] / 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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
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 *texture_name;
78     const char *compressorstr;
79     int ret;
80
81     switch (ctx->section_type & 0x0F) {
82     case HAP_FMT_RGBDXT1:
83         ctx->tex_rat = 8;
84         ctx->tex_fun = ctx->dxtc.dxt1_block;
85         texture_name = "DXT1";
86         break;
87     case HAP_FMT_RGBADXT5:
88         ctx->tex_rat = 16;
89         ctx->tex_fun = ctx->dxtc.dxt5_block;
90         texture_name = "DXT5";
91         break;
92     case HAP_FMT_YCOCGDXT5:
93         ctx->tex_rat = 16;
94         ctx->tex_fun = ctx->dxtc.dxt5ys_block;
95         texture_name = "DXT5-YCoCg-scaled";
96         break;
97     default:
98         av_log(avctx, AV_LOG_ERROR,
99                "Invalid format mode %02X.\n", ctx->section_type);
100         return AVERROR_INVALIDDATA;
101     }
102
103     switch (ctx->section_type & 0xF0) {
104     case HAP_COMP_NONE:
105         /* Only DXTC texture compression */
106         ctx->tex_data = gbc->buffer;
107         ctx->tex_size = length;
108         compressorstr = "none";
109         break;
110     case HAP_COMP_SNAPPY:
111         /* Uncompress the frame */
112         ret = ff_snappy_uncompress(gbc, &ctx->snappied, &snappy_size);
113         if (ret < 0) {
114              av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
115              return ret;
116         }
117
118         ctx->tex_data = ctx->snappied;
119         ctx->tex_size = snappy_size;
120         compressorstr = "snappy";
121         break;
122     case HAP_COMP_COMPLEX:
123         compressorstr = "complex";
124         avpriv_request_sample(avctx, "Complex Hap compressor");
125         return AVERROR_PATCHWELCOME;
126         break;
127     default:
128         av_log(avctx, AV_LOG_ERROR,
129                "Invalid compressor mode %02X.\n", ctx->section_type);
130         return AVERROR_INVALIDDATA;
131     }
132
133     av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
134            texture_name, compressorstr);
135
136     return 0;
137 }
138
139 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
140                                      int block_nb, int thread_nb)
141 {
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;
148
149     ctx->tex_fun(p, frame->linesize[0], d);
150     return 0;
151 }
152
153 static int hap_decode(AVCodecContext *avctx, void *data,
154                       int *got_frame, AVPacket *avpkt)
155 {
156     HapContext *ctx = avctx->priv_data;
157     ThreadFrame tframe;
158     int ret, length;
159     int blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H);
160
161     bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
162
163     /* Check for section header */
164     length = parse_section_header(avctx);
165     if (length < 0) {
166         av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
167         return length;
168     }
169
170     /* Prepare the texture buffer and decompress function */
171     ret = setup_texture(avctx, length);
172     if (ret < 0)
173         return ret;
174
175     /* Get the output frame ready to receive data */
176     tframe.f = data;
177     ret = ff_thread_get_buffer(avctx, &tframe, 0);
178     if (ret < 0)
179         return ret;
180     if (avctx->codec->update_thread_context)
181         ff_thread_finish_setup(avctx);
182
183     /* Use the decompress function on the texture, one block per thread */
184     avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, blocks);
185
186     /* Frame is ready to be output */
187     tframe.f->pict_type = AV_PICTURE_TYPE_I;
188     tframe.f->key_frame = 1;
189     *got_frame = 1;
190
191     return avpkt->size;
192 }
193
194 static av_cold int hap_init(AVCodecContext *avctx)
195 {
196     HapContext *ctx = avctx->priv_data;
197     int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
198
199     if (ret < 0) {
200         av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
201                avctx->width, avctx->height);
202         return ret;
203     }
204
205     /* Since codec is based on 4x4 blocks, size is aligned to 4 */
206     avctx->coded_width  = FFALIGN(avctx->width,  TEXTURE_BLOCK_W);
207     avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
208
209     /* Technically only one mode has alpha, but 32 bits are easier to handle */
210     avctx->pix_fmt = AV_PIX_FMT_RGBA;
211
212     ff_texturedsp_init(&ctx->dxtc);
213
214     return 0;
215 }
216
217 static av_cold int hap_close(AVCodecContext *avctx)
218 {
219     HapContext *ctx = avctx->priv_data;
220
221     av_freep(&ctx->snappied);
222
223     return 0;
224 }
225
226 AVCodec ff_hap_decoder = {
227     .name           = "hap",
228     .long_name      = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
229     .type           = AVMEDIA_TYPE_VIDEO,
230     .id             = AV_CODEC_ID_HAP,
231     .init           = hap_init,
232     .decode         = hap_decode,
233     .close          = hap_close,
234     .priv_data_size = sizeof(HapContext),
235     .capabilities   = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
236                       CODEC_CAP_DR1,
237     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
238                       FF_CODEC_CAP_INIT_CLEANUP,
239 };