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