]> git.sesse.net Git - ffmpeg/blob - libavcodec/dxv.c
lavc: mark the old audio/video encoding API as deprecated
[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_decompress_raw(AVCodecContext *avctx)
318 {
319     DXVContext *ctx = avctx->priv_data;
320     GetByteContext *gbc = &ctx->gbc;
321
322     bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
323     return 0;
324 }
325
326 static int dxv_decode(AVCodecContext *avctx, void *data,
327                       int *got_frame, AVPacket *avpkt)
328 {
329     DXVContext *ctx = avctx->priv_data;
330     ThreadFrame tframe;
331     GetByteContext *gbc = &ctx->gbc;
332     int (*decompress_tex)(AVCodecContext *avctx);
333     const char *msgcomp, *msgtext;
334     uint32_t tag;
335     int version_major, version_minor = 0;
336     int size = 0, old_type = 0;
337     int ret;
338
339     bytestream2_init(gbc, avpkt->data, avpkt->size);
340
341     tag = bytestream2_get_le32(gbc);
342     switch (tag) {
343     case MKBETAG('D', 'X', 'T', '1'):
344         decompress_tex = dxv_decompress_dxt1;
345         ctx->tex_funct = ctx->texdsp.dxt1_block;
346         ctx->tex_rat   = 8;
347         ctx->tex_step  = 8;
348         msgcomp = "DXTR1";
349         msgtext = "DXT1";
350         break;
351     case MKBETAG('D', 'X', 'T', '5'):
352         decompress_tex = dxv_decompress_dxt5;
353         ctx->tex_funct = ctx->texdsp.dxt5_block;
354         ctx->tex_rat   = 4;
355         ctx->tex_step  = 16;
356         msgcomp = "DXTR5";
357         msgtext = "DXT5";
358         break;
359     case MKBETAG('Y', 'C', 'G', '6'):
360     case MKBETAG('Y', 'G', '1', '0'):
361         avpriv_report_missing_feature(avctx, "Tag 0x%08X", tag);
362         return AVERROR_PATCHWELCOME;
363     default:
364         /* Old version does not have a real header, just size and type. */
365         size = tag & 0x00FFFFFF;
366         old_type = tag >> 24;
367         version_major = (old_type & 0x0F) - 1;
368
369         if (old_type & 0x80) {
370             msgcomp = "RAW";
371             decompress_tex = dxv_decompress_raw;
372         } else {
373             msgcomp = "LZF";
374             decompress_tex = dxv_decompress_lzf;
375         }
376
377         if (old_type & 0x40) {
378             msgtext = "DXT5";
379
380             ctx->tex_funct = ctx->texdsp.dxt5_block;
381             ctx->tex_step  = 16;
382         } else if (old_type & 0x20 || version_major == 1) {
383             msgtext = "DXT1";
384
385             ctx->tex_funct = ctx->texdsp.dxt1_block;
386             ctx->tex_step  = 8;
387         } else {
388             av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08X)\n.", tag);
389             return AVERROR_INVALIDDATA;
390         }
391         ctx->tex_rat = 1;
392         break;
393     }
394
395     /* New header is 12 bytes long. */
396     if (!old_type) {
397         version_major = bytestream2_get_byte(gbc) - 1;
398         version_minor = bytestream2_get_byte(gbc);
399
400         /* Encoder copies texture data when compression is not advantageous. */
401         if (bytestream2_get_byte(gbc)) {
402             msgcomp = "RAW";
403             ctx->tex_rat = 1;
404             decompress_tex = dxv_decompress_raw;
405         }
406
407         bytestream2_skip(gbc, 1); // unknown
408         size = bytestream2_get_le32(gbc);
409     }
410     av_log(avctx, AV_LOG_DEBUG,
411            "%s compression with %s texture (version %d.%d)\n",
412            msgcomp, msgtext, version_major, version_minor);
413
414     if (size != bytestream2_get_bytes_left(gbc)) {
415         av_log(avctx, AV_LOG_ERROR,
416                "Incomplete or invalid file (header %d, left %d).\n",
417                size, bytestream2_get_bytes_left(gbc));
418         return AVERROR_INVALIDDATA;
419     }
420
421     ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
422     ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
423     if (ret < 0)
424         return ret;
425
426     /* Decompress texture out of the intermediate compression. */
427     ret = decompress_tex(avctx);
428     if (ret < 0)
429         return ret;
430
431     tframe.f = data;
432     ret = ff_thread_get_buffer(avctx, &tframe, 0);
433     if (ret < 0)
434         return ret;
435     ff_thread_finish_setup(avctx);
436
437     /* Now decompress the texture with the standard functions. */
438     avctx->execute2(avctx, decompress_texture_thread,
439                     tframe.f, NULL, ctx->slice_count);
440
441     /* Frame is ready to be output. */
442     tframe.f->pict_type = AV_PICTURE_TYPE_I;
443     tframe.f->key_frame = 1;
444     *got_frame = 1;
445
446     return avpkt->size;
447 }
448
449 static int dxv_init(AVCodecContext *avctx)
450 {
451     DXVContext *ctx = avctx->priv_data;
452     int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
453
454     if (ret < 0) {
455         av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
456                avctx->width, avctx->height);
457         return ret;
458     }
459
460     /* Codec requires 16x16 alignment. */
461     avctx->coded_width  = FFALIGN(avctx->width,  16);
462     avctx->coded_height = FFALIGN(avctx->height, 16);
463
464     ff_texturedsp_init(&ctx->texdsp);
465     avctx->pix_fmt = AV_PIX_FMT_RGBA;
466
467     ctx->slice_count = av_clip(avctx->thread_count, 1,
468                                avctx->coded_height / TEXTURE_BLOCK_H);
469
470     return 0;
471 }
472
473 static int dxv_close(AVCodecContext *avctx)
474 {
475     DXVContext *ctx = avctx->priv_data;
476
477     av_freep(&ctx->tex_data);
478
479     return 0;
480 }
481
482 AVCodec ff_dxv_decoder = {
483     .name           = "dxv",
484     .long_name      = NULL_IF_CONFIG_SMALL("Resolume DXV"),
485     .type           = AVMEDIA_TYPE_VIDEO,
486     .id             = AV_CODEC_ID_DXV,
487     .init           = dxv_init,
488     .decode         = dxv_decode,
489     .close          = dxv_close,
490     .priv_data_size = sizeof(DXVContext),
491     .capabilities   = AV_CODEC_CAP_DR1 |
492                       AV_CODEC_CAP_SLICE_THREADS |
493                       AV_CODEC_CAP_FRAME_THREADS,
494     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
495                       FF_CODEC_CAP_INIT_CLEANUP,
496 };