]> git.sesse.net Git - ffmpeg/blob - libavcodec/dxv.c
mmal: Remove setting extradata on input format
[ffmpeg] / libavcodec / dxv.c
1 /*
2  * Resolume DXV 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 #include <stdint.h>
23
24 #include "libavutil/imgutils.h"
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "lzf.h"
30 #include "texturedsp.h"
31 #include "thread.h"
32
33 typedef struct DXVContext {
34     TextureDSPContext texdsp;
35     GetByteContext gbc;
36
37     uint8_t *tex_data;  // Compressed texture
38     int tex_rat;        // Compression ratio
39     int tex_step;       // Distance between blocks
40     int64_t tex_size;   // Texture size
41
42     /* Optimal number of slices for parallel decoding */
43     int slice_count;
44
45     /* Pointer to the selected decompression function */
46     int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
47 } DXVContext;
48
49 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
50                                      int slice, int thread_nb)
51 {
52     DXVContext *ctx = avctx->priv_data;
53     AVFrame *frame = arg;
54     const uint8_t *d = ctx->tex_data;
55     int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
56     int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
57     int x, y;
58     int start_slice, end_slice;
59     int base_blocks_per_slice = h_block / ctx->slice_count;
60     int remainder_blocks = h_block % ctx->slice_count;
61
62     /* When the frame height (in blocks) doesn't divide evenly between the
63      * number of slices, spread the remaining blocks evenly between the first
64      * operations */
65     start_slice = slice * base_blocks_per_slice;
66     /* Add any extra blocks (one per slice) that have been added
67      * before this slice */
68     start_slice += FFMIN(slice, remainder_blocks);
69
70     end_slice = start_slice + base_blocks_per_slice;
71     /* Add an extra block if there are remainder blocks to be accounted for */
72     if (slice < remainder_blocks)
73         end_slice++;
74
75     for (y = start_slice; y < end_slice; y++) {
76         uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
77         int off  = y * w_block;
78         for (x = 0; x < w_block; x++) {
79             ctx->tex_funct(p + x * 16, frame->linesize[0],
80                            d + (off + x) * ctx->tex_step);
81         }
82     }
83
84     return 0;
85 }
86
87 /* This scheme addresses already decoded elements depending on 2-bit status:
88  *   0 -> copy new element
89  *   1 -> copy one element from position -x
90  *   2 -> copy one element from position -(get_byte() + 2) * x
91  *   3 -> copy one element from position -(get_16le() + 0x102) * x
92  * x is always 2 for dxt1 and 4 for dxt5. */
93 #define CHECKPOINT(x)                                                         \
94     do {                                                                      \
95         if (state == 0) {                                                     \
96             value = bytestream2_get_le32(gbc);                                \
97             state = 16;                                                       \
98         }                                                                     \
99         op = value & 0x3;                                                     \
100         value >>= 2;                                                          \
101         state--;                                                              \
102         switch (op) {                                                         \
103         case 1:                                                               \
104             idx = x;                                                          \
105             break;                                                            \
106         case 2:                                                               \
107             idx = (bytestream2_get_byte(gbc) + 2) * x;                        \
108             break;                                                            \
109         case 3:                                                               \
110             idx = (bytestream2_get_le16(gbc) + 0x102) * x;                    \
111             break;                                                            \
112         }                                                                     \
113     } while(0)
114
115 static int dxv_decompress_dxt1(AVCodecContext *avctx)
116 {
117     DXVContext *ctx = avctx->priv_data;
118     GetByteContext *gbc = &ctx->gbc;
119     uint32_t value, prev, op;
120     int idx = 0, state = 0;
121     int pos = 2;
122
123     /* Copy the first two elements */
124     AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
125     AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
126
127     /* Process input until the whole texture has been filled */
128     while (pos < ctx->tex_size / 4) {
129         CHECKPOINT(2);
130
131         /* Copy two elements from a previous offset or from the input buffer */
132         if (op) {
133             prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
134             AV_WL32(ctx->tex_data + 4 * pos, prev);
135             pos++;
136
137             prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
138             AV_WL32(ctx->tex_data + 4 * pos, prev);
139             pos++;
140         } else {
141             CHECKPOINT(2);
142
143             if (op)
144                 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
145             else
146                 prev = bytestream2_get_le32(gbc);
147             AV_WL32(ctx->tex_data + 4 * pos, prev);
148             pos++;
149
150             CHECKPOINT(2);
151
152             if (op)
153                 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
154             else
155                 prev = bytestream2_get_le32(gbc);
156             AV_WL32(ctx->tex_data + 4 * pos, prev);
157             pos++;
158         }
159     }
160
161     return 0;
162 }
163
164 static int dxv_decompress_dxt5(AVCodecContext *avctx)
165 {
166     DXVContext *ctx = avctx->priv_data;
167     GetByteContext *gbc = &ctx->gbc;
168     uint32_t value, op;
169     int idx, prev, state = 0;
170     int pos = 4;
171     int run = 0;
172     int probe, check;
173
174     /* Copy the first four elements */
175     AV_WL32(ctx->tex_data +  0, bytestream2_get_le32(gbc));
176     AV_WL32(ctx->tex_data +  4, bytestream2_get_le32(gbc));
177     AV_WL32(ctx->tex_data +  8, bytestream2_get_le32(gbc));
178     AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
179
180     /* Process input until the whole texture has been filled */
181     while (pos < ctx->tex_size / 4) {
182         if (run) {
183             run--;
184
185             prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
186             AV_WL32(ctx->tex_data + 4 * pos, prev);
187             pos++;
188             prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
189             AV_WL32(ctx->tex_data + 4 * pos, prev);
190             pos++;
191         } else {
192             if (state == 0) {
193                 value = bytestream2_get_le32(gbc);
194                 state = 16;
195             }
196             op = value & 0x3;
197             value >>= 2;
198             state--;
199
200             switch (op) {
201             case 0:
202                 /* Long copy */
203                 check = bytestream2_get_byte(gbc) + 1;
204                 if (check == 256) {
205                     do {
206                         probe = bytestream2_get_le16(gbc);
207                         check += probe;
208                     } while (probe == 0xFFFF);
209                 }
210                 while (check && pos < ctx->tex_size / 4) {
211                     prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
212                     AV_WL32(ctx->tex_data + 4 * pos, prev);
213                     pos++;
214
215                     prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
216                     AV_WL32(ctx->tex_data + 4 * pos, prev);
217                     pos++;
218
219                     prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
220                     AV_WL32(ctx->tex_data + 4 * pos, prev);
221                     pos++;
222
223                     prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
224                     AV_WL32(ctx->tex_data + 4 * pos, prev);
225                     pos++;
226
227                     check--;
228                 }
229
230                 /* Restart (or exit) the loop */
231                 continue;
232                 break;
233             case 1:
234                 /* Load new run value */
235                 run = bytestream2_get_byte(gbc);
236                 if (run == 255) {
237                     do {
238                         probe = bytestream2_get_le16(gbc);
239                         run += probe;
240                     } while (probe == 0xFFFF);
241                 }
242
243                 /* Copy two dwords from previous data */
244                 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
245                 AV_WL32(ctx->tex_data + 4 * pos, prev);
246                 pos++;
247
248                 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
249                 AV_WL32(ctx->tex_data + 4 * pos, prev);
250                 pos++;
251                 break;
252             case 2:
253                 /* Copy two dwords from a previous index */
254                 idx = 8 + bytestream2_get_le16(gbc);
255                 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
256                 AV_WL32(ctx->tex_data + 4 * pos, prev);
257                 pos++;
258
259                 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
260                 AV_WL32(ctx->tex_data + 4 * pos, prev);
261                 pos++;
262                 break;
263             case 3:
264                 /* Copy two dwords from input */
265                 prev = bytestream2_get_le32(gbc);
266                 AV_WL32(ctx->tex_data + 4 * pos, prev);
267                 pos++;
268
269                 prev = bytestream2_get_le32(gbc);
270                 AV_WL32(ctx->tex_data + 4 * pos, prev);
271                 pos++;
272                 break;
273             }
274         }
275
276         CHECKPOINT(4);
277
278         /* Copy two elements from a previous offset or from the input buffer */
279         if (op) {
280             prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
281             AV_WL32(ctx->tex_data + 4 * pos, prev);
282             pos++;
283
284             prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
285             AV_WL32(ctx->tex_data + 4 * pos, prev);
286             pos++;
287         } else {
288             CHECKPOINT(4);
289
290             if (op)
291                 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
292             else
293                 prev = bytestream2_get_le32(gbc);
294             AV_WL32(ctx->tex_data + 4 * pos, prev);
295             pos++;
296
297             CHECKPOINT(4);
298
299             if (op)
300                 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
301             else
302                 prev = bytestream2_get_le32(gbc);
303             AV_WL32(ctx->tex_data + 4 * pos, prev);
304             pos++;
305         }
306     }
307
308     return 0;
309 }
310
311 static int dxv_decompress_lzf(AVCodecContext *avctx)
312 {
313     DXVContext *ctx = avctx->priv_data;
314     return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
315 }
316
317 static int dxv_decode(AVCodecContext *avctx, void *data,
318                       int *got_frame, AVPacket *avpkt)
319 {
320     DXVContext *ctx = avctx->priv_data;
321     ThreadFrame tframe;
322     GetByteContext *gbc = &ctx->gbc;
323     int (*decompress_tex)(AVCodecContext *avctx);
324     uint32_t tag;
325     int channels, size = 0, old_type = 0;
326     int ret;
327
328     bytestream2_init(gbc, avpkt->data, avpkt->size);
329
330     tag = bytestream2_get_le32(gbc);
331     switch (tag) {
332     case MKBETAG('D', 'X', 'T', '1'):
333         decompress_tex = dxv_decompress_dxt1;
334         ctx->tex_funct = ctx->texdsp.dxt1_block;
335         ctx->tex_rat   = 8;
336         ctx->tex_step  = 8;
337         av_log(avctx, AV_LOG_DEBUG, "DXTR1 compression and DXT1 texture ");
338         break;
339     case MKBETAG('D', 'X', 'T', '5'):
340         decompress_tex = dxv_decompress_dxt5;
341         ctx->tex_funct = ctx->texdsp.dxt5_block;
342         ctx->tex_rat   = 4;
343         ctx->tex_step  = 16;
344         av_log(avctx, AV_LOG_DEBUG, "DXTR5 compression and DXT5 texture ");
345         break;
346     case MKBETAG('Y', 'C', 'G', '6'):
347     case MKBETAG('Y', 'G', '1', '0'):
348         avpriv_report_missing_feature(avctx, "Tag 0x%08X", tag);
349         return AVERROR_PATCHWELCOME;
350     default:
351         /* Old version does not have a real header, just size and type. */
352         size = tag & 0x00FFFFFF;
353         old_type = tag >> 24;
354         channels = old_type & 0x0F;
355         if (old_type & 0x40) {
356             av_log(avctx, AV_LOG_DEBUG, "LZF compression and DXT5 texture ");
357             ctx->tex_funct = ctx->texdsp.dxt5_block;
358             ctx->tex_step  = 16;
359         } else if (old_type & 0x20) {
360             av_log(avctx, AV_LOG_DEBUG, "LZF compression and DXT1 texture ");
361             ctx->tex_funct = ctx->texdsp.dxt1_block;
362             ctx->tex_step  = 8;
363         } else {
364             av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08X)\n.", tag);
365             return AVERROR_INVALIDDATA;
366         }
367         decompress_tex = dxv_decompress_lzf;
368         ctx->tex_rat = 1;
369         break;
370     }
371
372     /* New header is 12 bytes long. */
373     if (!old_type) {
374         channels = bytestream2_get_byte(gbc);
375         bytestream2_skip(gbc, 3); // unknown
376         size = bytestream2_get_le32(gbc);
377     }
378     av_log(avctx, AV_LOG_DEBUG, "(%d channels)\n", channels);
379
380     if (size != bytestream2_get_bytes_left(gbc)) {
381         av_log(avctx, AV_LOG_ERROR, "Incomplete or invalid file (%u > %u)\n.",
382                size, bytestream2_get_bytes_left(gbc));
383         return AVERROR_INVALIDDATA;
384     }
385
386     ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
387     ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
388     if (ret < 0)
389         return ret;
390
391     /* Decompress texture out of the intermediate compression. */
392     ret = decompress_tex(avctx);
393     if (ret < 0)
394         return ret;
395
396     tframe.f = data;
397     ret = ff_thread_get_buffer(avctx, &tframe, 0);
398     if (ret < 0)
399         return ret;
400     ff_thread_finish_setup(avctx);
401
402     /* Now decompress the texture with the standard functions. */
403     avctx->execute2(avctx, decompress_texture_thread,
404                     tframe.f, NULL, ctx->slice_count);
405
406     /* Frame is ready to be output. */
407     tframe.f->pict_type = AV_PICTURE_TYPE_I;
408     tframe.f->key_frame = 1;
409     *got_frame = 1;
410
411     return avpkt->size;
412 }
413
414 static int dxv_init(AVCodecContext *avctx)
415 {
416     DXVContext *ctx = avctx->priv_data;
417     int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
418
419     if (ret < 0) {
420         av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
421                avctx->width, avctx->height);
422         return ret;
423     }
424
425     /* Codec requires 16x16 alignment. */
426     avctx->coded_width  = FFALIGN(avctx->width,  16);
427     avctx->coded_height = FFALIGN(avctx->height, 16);
428
429     ff_texturedsp_init(&ctx->texdsp);
430     avctx->pix_fmt = AV_PIX_FMT_RGBA;
431
432     ctx->slice_count = av_clip(avctx->thread_count, 1,
433                                avctx->coded_height / TEXTURE_BLOCK_H);
434
435     return 0;
436 }
437
438 static int dxv_close(AVCodecContext *avctx)
439 {
440     DXVContext *ctx = avctx->priv_data;
441
442     av_freep(&ctx->tex_data);
443
444     return 0;
445 }
446
447 AVCodec ff_dxv_decoder = {
448     .name           = "dxv",
449     .long_name      = NULL_IF_CONFIG_SMALL("Resolume DXV"),
450     .type           = AVMEDIA_TYPE_VIDEO,
451     .id             = AV_CODEC_ID_DXV,
452     .init           = dxv_init,
453     .decode         = dxv_decode,
454     .close          = dxv_close,
455     .priv_data_size = sizeof(DXVContext),
456     .capabilities   = AV_CODEC_CAP_DR1 |
457                       AV_CODEC_CAP_SLICE_THREADS |
458                       AV_CODEC_CAP_FRAME_THREADS,
459     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
460                       FF_CODEC_CAP_INIT_CLEANUP,
461 };