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