Browse code

DXA demuxer and decoder

Originally committed as revision 8405 to svn://svn.ffmpeg.org/ffmpeg/trunk

Kostya Shishkov authored on 2007/03/14 23:49:49
Showing 13 changed files
... ...
@@ -73,6 +73,7 @@ version <next>
73 73
 - WMA encoder
74 74
 - GSM-MS encoder and decoder
75 75
 - DCA decoder
76
+- DXA demuxer and decoder
76 77
 
77 78
 version 0.4.9-pre1:
78 79
 
... ...
@@ -114,6 +114,7 @@ Codecs:
114 114
   cook.c, cookdata.h                    Benjamin Larsson
115 115
   cscd.c                                Reimar Doeffinger
116 116
   dpcm.c                                Mike Melanson
117
+  dxa.c                                 Kostya Shishkov
117 118
   dv.c                                  Roman Shaposhnik
118 119
   ffv1.c                                Michael Niedermayer
119 120
   flac.c                                Alex Beregszaszi
... ...
@@ -202,6 +203,7 @@ Muxers/Demuxers:
202 202
   crc.c                                 Michael Niedermayer
203 203
   daud.c                                Reimar Doeffinger
204 204
   dc1394.c, dv.c                        Roman Shaposhnik
205
+  dxa.c                                 Kostya Shishkov
205 206
   flic.c                                Mike Melanson
206 207
   flvdec.c, flvenc.c                    Michael Niedermayer
207 208
   gxf.c                                 Reimar Doeffinger
... ...
@@ -644,6 +644,7 @@ CMDLINE_SELECT="
644 644
     static
645 645
 "
646 646
 
647
+dxa_decoder_deps="zlib"
647 648
 flashsv_decoder_deps="zlib"
648 649
 flashsv_encoder_deps="zlib"
649 650
 mpeg_xvmc_decoder_deps="xvmc"
... ...
@@ -898,6 +898,9 @@ library:
898 898
 @tab Material eXchange Format SMPTE 377M, used by D-Cinema, broadcast industry.
899 899
 @item SEQ @tab    @tab X
900 900
 @tab Tiertex .seq files used in the DOS CDROM version of the game Flashback.
901
+@item DXA @tab    @tab X
902
+@tab This format is used in non-Windows version of Feeble Files game and
903
+different game cutscenes repacked for use with ScummVM.
901 904
 @end multitable
902 905
 
903 906
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -1005,6 +1008,7 @@ following image formats are supported:
1005 1005
 @item VMware Video           @tab     @tab  X @tab Codec used in videos captured by VMware.
1006 1006
 @item Cin Video              @tab     @tab  X @tab Codec used in Delphine Software games.
1007 1007
 @item Tiertex Seq Video      @tab     @tab  X @tab Codec used in DOS CDROM FlashBack game.
1008
+@item DXA Video              @tab     @tab  X @tab Codec originally used in Feeble Files game.
1008 1009
 @end multitable
1009 1010
 
1010 1011
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -72,6 +72,7 @@ OBJS-$(CONFIG_DVDSUB_DECODER)          += dvdsubdec.o
72 72
 OBJS-$(CONFIG_DVDSUB_ENCODER)          += dvdsubenc.o
73 73
 OBJS-$(CONFIG_DVVIDEO_DECODER)         += dv.o
74 74
 OBJS-$(CONFIG_DVVIDEO_ENCODER)         += dv.o
75
+OBJS-$(CONFIG_DXA_DECODER)             += dxa.o
75 76
 OBJS-$(CONFIG_EIGHTBPS_DECODER)        += 8bps.o
76 77
 OBJS-$(CONFIG_FFV1_DECODER)            += ffv1.o
77 78
 OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1.o
... ...
@@ -67,6 +67,7 @@ void avcodec_register_all(void)
67 67
     REGISTER_DECODER(DCA, dca);
68 68
     REGISTER_DECODER(DSICINVIDEO, dsicinvideo);
69 69
     REGISTER_ENCDEC (DVVIDEO, dvvideo);
70
+    REGISTER_DECODER(DXA, dxa);
70 71
     REGISTER_DECODER(EIGHTBPS, eightbps);
71 72
     REGISTER_ENCDEC (FFV1, ffv1);
72 73
     REGISTER_ENCDEC (FFVHUFF, ffvhuff);
... ...
@@ -37,8 +37,8 @@ extern "C" {
37 37
 #define AV_STRINGIFY(s)         AV_TOSTRING(s)
38 38
 #define AV_TOSTRING(s) #s
39 39
 
40
-#define LIBAVCODEC_VERSION_INT  ((51<<16)+(38<<8)+0)
41
-#define LIBAVCODEC_VERSION      51.38.0
40
+#define LIBAVCODEC_VERSION_INT  ((51<<16)+(39<<8)+0)
41
+#define LIBAVCODEC_VERSION      51.39.0
42 42
 #define LIBAVCODEC_BUILD        LIBAVCODEC_VERSION_INT
43 43
 
44 44
 #define LIBAVCODEC_IDENT        "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
... ...
@@ -150,6 +150,7 @@ enum CodecID {
150 150
     CODEC_ID_TIFF,
151 151
     CODEC_ID_GIF,
152 152
     CODEC_ID_FFH264,
153
+    CODEC_ID_DXA,
153 154
 
154 155
     /* various pcm "codecs" */
155 156
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2232,6 +2233,7 @@ extern AVCodec dca_decoder;
2232 2232
 extern AVCodec dsicinaudio_decoder;
2233 2233
 extern AVCodec dsicinvideo_decoder;
2234 2234
 extern AVCodec dvvideo_decoder;
2235
+extern AVCodec dxa_decoder;
2235 2236
 extern AVCodec eightbps_decoder;
2236 2237
 extern AVCodec ffv1_decoder;
2237 2238
 extern AVCodec ffvhuff_decoder;
2238 2239
new file mode 100644
... ...
@@ -0,0 +1,333 @@
0
+/*
1
+ * Feeble Files/ScummVM DXA decoder
2
+ * Copyright (c) 2007 Konstantin Shishkov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ *
20
+ */
21
+
22
+/**
23
+ * @file dxa.c
24
+ * DXA Video decoder
25
+ */
26
+
27
+#include <stdio.h>
28
+#include <stdlib.h>
29
+
30
+#include "common.h"
31
+#include "avcodec.h"
32
+
33
+#include <zlib.h>
34
+
35
+/*
36
+ * Decoder context
37
+ */
38
+typedef struct DxaDecContext {
39
+    AVCodecContext *avctx;
40
+    AVFrame pic, prev;
41
+
42
+    int dsize;
43
+    uint8_t *decomp_buf;
44
+    uint32_t pal[256];
45
+} DxaDecContext;
46
+
47
+static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
48
+static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
49
+
50
+static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref)
51
+{
52
+    uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
53
+    int i, j, k;
54
+    int type, x, y, d, d2;
55
+    int stride = c->pic.linesize[0];
56
+    uint32_t mask;
57
+
58
+    code = src  + 12;
59
+    data = code + ((avctx->width * avctx->height) >> 4);
60
+    mv   = data + AV_RB32(src + 0);
61
+    msk  = mv   + AV_RB32(src + 4);
62
+
63
+    for(j = 0; j < avctx->height; j += 4){
64
+        for(i = 0; i < avctx->width; i += 4){
65
+            tmp  = dst + i;
66
+            tmp2 = ref + i;
67
+            type = *code++;
68
+            switch(type){
69
+            case 4: // motion compensation
70
+                x = (*mv) >> 4;    if(x & 8) x = 8 - x;
71
+                y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
72
+                tmp2 += x + y*stride;
73
+            case 0: // skip
74
+            case 5: // skip in method 12
75
+                for(y = 0; y < 4; y++){
76
+                    memcpy(tmp, tmp2, 4);
77
+                    tmp  += stride;
78
+                    tmp2 += stride;
79
+                }
80
+                break;
81
+            case 1:  // masked change
82
+            case 10: // masked change with only half of pixels changed
83
+            case 11: // cases 10-15 are for method 12 only
84
+            case 12:
85
+            case 13:
86
+            case 14:
87
+            case 15:
88
+                if(type == 1){
89
+                    mask = AV_RB16(msk);
90
+                    msk += 2;
91
+                }else{
92
+                    type -= 10;
93
+                    mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
94
+                    msk++;
95
+                }
96
+                for(y = 0; y < 4; y++){
97
+                    for(x = 0; x < 4; x++){
98
+                        tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
99
+                        mask <<= 1;
100
+                    }
101
+                    tmp  += stride;
102
+                    tmp2 += stride;
103
+                }
104
+                break;
105
+            case 2: // fill block
106
+                for(y = 0; y < 4; y++){
107
+                    memset(tmp, data[0], 4);
108
+                    tmp += stride;
109
+                }
110
+                data++;
111
+                break;
112
+            case 3: // raw block
113
+                for(y = 0; y < 4; y++){
114
+                    memcpy(tmp, data, 4);
115
+                    data += 4;
116
+                    tmp  += stride;
117
+                }
118
+                break;
119
+            case 8: // subblocks - method 13 only
120
+                mask = *msk++;
121
+                for(k = 0; k < 4; k++){
122
+                    d  = ((k & 1) << 1) + ((k & 2) * stride);
123
+                    d2 = ((k & 1) << 1) + ((k & 2) * stride);
124
+                    tmp2 = ref + i + d2;
125
+                    switch(mask & 0xC0){
126
+                    case 0x80: // motion compensation
127
+                        x = (*mv) >> 4;    if(x & 8) x = 8 - x;
128
+                        y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
129
+                        tmp2 += x + y*stride;
130
+                    case 0x00: // skip
131
+                        tmp[d + 0         ] = tmp2[0];
132
+                        tmp[d + 1         ] = tmp2[1];
133
+                        tmp[d + 0 + stride] = tmp2[0 + stride];
134
+                        tmp[d + 1 + stride] = tmp2[1 + stride];
135
+                        break;
136
+                    case 0x40: // fill
137
+                        tmp[d + 0         ] = data[0];
138
+                        tmp[d + 1         ] = data[0];
139
+                        tmp[d + 0 + stride] = data[0];
140
+                        tmp[d + 1 + stride] = data[0];
141
+                        data++;
142
+                        break;
143
+                    case 0xC0: // raw
144
+                        tmp[d + 0         ] = *data++;
145
+                        tmp[d + 1         ] = *data++;
146
+                        tmp[d + 0 + stride] = *data++;
147
+                        tmp[d + 1 + stride] = *data++;
148
+                        break;
149
+                    }
150
+                    mask <<= 2;
151
+                }
152
+                break;
153
+            case 32: // vector quantization - 2 colors
154
+                mask = AV_RB16(msk);
155
+                msk += 2;
156
+                for(y = 0; y < 4; y++){
157
+                    for(x = 0; x < 4; x++){
158
+                        tmp[x] = data[mask & 1];
159
+                        mask >>= 1;
160
+                    }
161
+                    tmp  += stride;
162
+                    tmp2 += stride;
163
+                }
164
+                data += 2;
165
+                break;
166
+            case 33: // vector quantization - 3 or 4 colors
167
+            case 34:
168
+                mask = AV_RB32(msk);
169
+                msk += 4;
170
+                for(y = 0; y < 4; y++){
171
+                    for(x = 0; x < 4; x++){
172
+                        tmp[x] = data[mask & 3];
173
+                        mask >>= 2;
174
+                    }
175
+                    tmp  += stride;
176
+                    tmp2 += stride;
177
+                }
178
+                data += type - 30;
179
+                break;
180
+            default:
181
+                av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
182
+                return -1;
183
+            }
184
+        }
185
+        dst += stride * 4;
186
+        ref += stride * 4;
187
+    }
188
+    return 0;
189
+}
190
+
191
+static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
192
+{
193
+    DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
194
+    uint8_t *outptr, *srcptr, *tmpptr;
195
+    unsigned long dsize;
196
+    int i, j, compr;
197
+    int stride;
198
+    int orig_buf_size = buf_size;
199
+    int pc = 0;
200
+
201
+    /* make the palette available on the way out */
202
+    if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
203
+        int r, g, b;
204
+
205
+        buf += 4;
206
+        for(i = 0; i < 256; i++){
207
+            r = *buf++;
208
+            g = *buf++;
209
+            b = *buf++;
210
+            c->pal[i] = (r << 16) | (g << 8) | b;
211
+        }
212
+        pc = 1;
213
+        buf_size -= 768+4;
214
+    }
215
+
216
+    if(avctx->get_buffer(avctx, &c->pic) < 0){
217
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
218
+        return -1;
219
+    }
220
+    memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
221
+    c->pic.palette_has_changed = pc;
222
+
223
+    outptr = c->pic.data[0];
224
+    srcptr = c->decomp_buf;
225
+    tmpptr = c->prev.data[0];
226
+    stride = c->pic.linesize[0];
227
+
228
+    if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
229
+        compr = -1;
230
+    else
231
+        compr = buf[4];
232
+
233
+    dsize = c->dsize;
234
+    if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
235
+        av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
236
+        return -1;
237
+    }
238
+    switch(compr){
239
+    case -1:
240
+        c->pic.key_frame = 0;
241
+        c->pic.pict_type = FF_P_TYPE;
242
+        if(c->prev.data[0])
243
+            memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
244
+        else{ // Should happen only when first frame is 'NULL'
245
+            memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
246
+            c->pic.key_frame = 1;
247
+            c->pic.pict_type = FF_I_TYPE;
248
+        }
249
+        break;
250
+    case 2:
251
+    case 3:
252
+    case 4:
253
+    case 5:
254
+        c->pic.key_frame = !(compr & 1);
255
+        c->pic.pict_type = (compr & 1) ? FF_P_TYPE : FF_I_TYPE;
256
+        for(j = 0; j < avctx->height; j++){
257
+            if(compr & 1){
258
+                for(i = 0; i < avctx->width; i++)
259
+                    outptr[i] = srcptr[i] ^ tmpptr[i];
260
+                tmpptr += stride;
261
+            }else
262
+                memcpy(outptr, srcptr, avctx->width);
263
+            outptr += stride;
264
+            srcptr += avctx->width;
265
+        }
266
+        break;
267
+    case 12: // ScummVM coding
268
+    case 13:
269
+        c->pic.key_frame = 0;
270
+        c->pic.pict_type = FF_P_TYPE;
271
+        decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
272
+        break;
273
+    default:
274
+        av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]);
275
+        return -1;
276
+    }
277
+
278
+    FFSWAP(AVFrame, c->pic, c->prev);
279
+    if(c->pic.data[0])
280
+        avctx->release_buffer(avctx, &c->pic);
281
+
282
+    *data_size = sizeof(AVFrame);
283
+    *(AVFrame*)data = c->prev;
284
+
285
+    /* always report that the buffer was completely consumed */
286
+    return orig_buf_size;
287
+}
288
+
289
+static int decode_init(AVCodecContext *avctx)
290
+{
291
+    DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
292
+
293
+    c->avctx = avctx;
294
+    avctx->pix_fmt = PIX_FMT_PAL8;
295
+
296
+    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
297
+        return -1;
298
+    }
299
+
300
+    c->dsize = avctx->width * avctx->height * 2;
301
+    if((c->decomp_buf = av_malloc(c->dsize)) == NULL) {
302
+        av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
303
+        return -1;
304
+    }
305
+
306
+    return 0;
307
+}
308
+
309
+static int decode_end(AVCodecContext *avctx)
310
+{
311
+    DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
312
+
313
+    av_freep(&c->decomp_buf);
314
+    if(c->prev.data[0])
315
+        avctx->release_buffer(avctx, &c->prev);
316
+    if(c->pic.data[0])
317
+        avctx->release_buffer(avctx, &c->pic);
318
+
319
+    return 0;
320
+}
321
+
322
+AVCodec dxa_decoder = {
323
+    "dxa",
324
+    CODEC_TYPE_VIDEO,
325
+    CODEC_ID_DXA,
326
+    sizeof(DxaDecContext),
327
+    decode_init,
328
+    NULL,
329
+    decode_end,
330
+    decode_frame
331
+};
332
+
... ...
@@ -34,6 +34,7 @@ OBJS-$(CONFIG_DSICIN_DEMUXER)            += dsicin.o
34 34
 OBJS-$(CONFIG_DV_DEMUXER)                += dv.o
35 35
 OBJS-$(CONFIG_DV_MUXER)                  += dvenc.o
36 36
 OBJS-$(CONFIG_DV1394_DEMUXER)            += dv1394.o
37
+OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o
37 38
 OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
38 39
 OBJS-$(CONFIG_FFM_DEMUXER)               += ffm.o
39 40
 OBJS-$(CONFIG_FFM_MUXER)                 += ffm.o
... ...
@@ -65,6 +65,7 @@ void av_register_all(void)
65 65
     REGISTER_DEMUXER (DTS, dts);
66 66
     REGISTER_MUXDEMUX(DV, dv);
67 67
     REGISTER_DEMUXER (DV1394, dv1394);
68
+    REGISTER_DEMUXER (DXA, dxa);
68 69
     REGISTER_DEMUXER (EA, ea);
69 70
     REGISTER_MUXDEMUX(FFM, ffm);
70 71
     REGISTER_MUXDEMUX(FLAC, flac);
... ...
@@ -47,6 +47,7 @@ extern AVInputFormat dsicin_demuxer;
47 47
 extern AVInputFormat dv1394_demuxer;
48 48
 extern AVInputFormat dv_demuxer;
49 49
 extern AVOutputFormat dv_muxer;
50
+extern AVInputFormat dxa_demuxer;
50 51
 extern AVInputFormat ea_demuxer;
51 52
 extern AVInputFormat ffm_demuxer;
52 53
 extern AVOutputFormat ffm_muxer;
... ...
@@ -25,8 +25,8 @@
25 25
 extern "C" {
26 26
 #endif
27 27
 
28
-#define LIBAVFORMAT_VERSION_INT ((51<<16)+(10<<8)+0)
29
-#define LIBAVFORMAT_VERSION     51.10.0
28
+#define LIBAVFORMAT_VERSION_INT ((51<<16)+(11<<8)+0)
29
+#define LIBAVFORMAT_VERSION     51.11.0
30 30
 #define LIBAVFORMAT_BUILD       LIBAVFORMAT_VERSION_INT
31 31
 
32 32
 #define LIBAVFORMAT_IDENT       "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
33 33
new file mode 100644
... ...
@@ -0,0 +1,214 @@
0
+/*
1
+ * DXA demuxer
2
+ * Copyright (c) 2007 Konstantin Shishkov.
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "riff.h"
23
+
24
+#define DXA_EXTRA_SIZE  9
25
+
26
+typedef struct{
27
+    int frames;
28
+    int has_sound;
29
+    int bpc;
30
+    uint32_t bytes_left;
31
+    int64_t wavpos, vidpos;
32
+    int readvid;
33
+}DXAContext;
34
+
35
+static int dxa_probe(AVProbeData *p)
36
+{
37
+    /* check file header */
38
+    if (p->buf_size <= 4)
39
+        return 0;
40
+    if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
41
+        p->buf[2] == 'X' && p->buf[3] == 'A')
42
+        return AVPROBE_SCORE_MAX;
43
+    else
44
+        return 0;
45
+}
46
+
47
+static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
48
+{
49
+    ByteIOContext *pb = &s->pb;
50
+    DXAContext *c = s->priv_data;
51
+    AVStream *st, *ast;
52
+    uint32_t tag;
53
+    int32_t fps;
54
+    int w, h;
55
+    int num, den;
56
+    int flags;
57
+
58
+    tag = get_le32(pb);
59
+    if (tag != MKTAG('D', 'E', 'X', 'A'))
60
+        return -1;
61
+    flags = get_byte(pb);
62
+    c->frames = get_be16(pb);
63
+    if(!c->frames){
64
+        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
65
+        return -1;
66
+    }
67
+
68
+    fps = get_be32(pb);
69
+    if(fps > 0){
70
+        den = 1000;
71
+        num = fps;
72
+    }else if (fps < 0){
73
+        den = 100000;
74
+        num = -fps;
75
+    }else{
76
+        den = 10;
77
+        num = 1;
78
+    }
79
+    w = get_be16(pb);
80
+    h = get_be16(pb);
81
+    c->has_sound = 0;
82
+
83
+    st = av_new_stream(s, 0);
84
+    if (!st)
85
+        return -1;
86
+
87
+    // Parse WAV data header
88
+    if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
89
+        uint32_t size, fsize;
90
+        c->has_sound = 1;
91
+        size = get_be32(pb);
92
+        c->vidpos = url_ftell(pb) + size;
93
+        url_fskip(pb, 16);
94
+        fsize = get_le32(pb);
95
+
96
+        ast = av_new_stream(s, 0);
97
+        if (!ast)
98
+            return -1;
99
+        get_wav_header(pb, ast->codec, fsize);
100
+        // find 'data' chunk
101
+        while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
102
+            tag = get_le32(pb);
103
+            fsize = get_le32(pb);
104
+            if(tag == MKTAG('d', 'a', 't', 'a')) break;
105
+            url_fskip(pb, fsize);
106
+        }
107
+        c->bpc = (fsize + c->frames - 1) / c->frames;
108
+        if(ast->codec->block_align)
109
+            c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
110
+        c->bytes_left = fsize;
111
+        c->wavpos = url_ftell(pb);
112
+        url_fseek(pb, c->vidpos, SEEK_SET);
113
+    }
114
+
115
+    /* now we are ready: build format streams */
116
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
117
+    st->codec->codec_id   = CODEC_ID_DXA;
118
+    st->codec->width      = w;
119
+    st->codec->height     = h;
120
+    av_reduce(&den, &num, den, num, (1UL<<31)-1);
121
+    av_set_pts_info(st, 33, num, den);
122
+    /* flags & 0x80 means that image is interlaced,
123
+     * flags & 0x40 means that image has double height
124
+     * either way set true height
125
+     */
126
+    if(flags & 0xC0){
127
+        st->codec->height >>= 1;
128
+    }
129
+    c->readvid = !c->has_sound;
130
+    c->vidpos  = url_ftell(pb);
131
+    s->start_time = 0;
132
+    s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
133
+    av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
134
+
135
+    return 0;
136
+}
137
+
138
+static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
139
+{
140
+    DXAContext *c = s->priv_data;
141
+    int ret;
142
+    uint32_t size;
143
+    uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
144
+    int pal_size = 0;
145
+
146
+    if(!c->readvid && c->has_sound && c->bytes_left){
147
+        c->readvid = 1;
148
+        url_fseek(&s->pb, c->wavpos, SEEK_SET);
149
+        size = FFMIN(c->bytes_left, c->bpc);
150
+        ret = av_get_packet(&s->pb, pkt, size);
151
+        pkt->stream_index = 1;
152
+        if(ret != size)
153
+            return AVERROR_IO;
154
+        c->bytes_left -= size;
155
+        c->wavpos = url_ftell(&s->pb);
156
+        return 0;
157
+    }
158
+    url_fseek(&s->pb, c->vidpos, SEEK_SET);
159
+    while(!url_feof(&s->pb) && c->frames){
160
+        get_buffer(&s->pb, buf, 4);
161
+        switch(AV_RL32(buf)){
162
+        case MKTAG('N', 'U', 'L', 'L'):
163
+            if(av_new_packet(pkt, 4 + pal_size) < 0)
164
+                return AVERROR_NOMEM;
165
+            pkt->stream_index = 0;
166
+            if(pal_size) memcpy(pkt->data, pal, pal_size);
167
+            memcpy(pkt->data + pal_size, buf, 4);
168
+            c->frames--;
169
+            c->vidpos = url_ftell(&s->pb);
170
+            c->readvid = 0;
171
+            return 0;
172
+        case MKTAG('C', 'M', 'A', 'P'):
173
+            pal_size = 768+4;
174
+            memcpy(pal, buf, 4);
175
+            get_buffer(&s->pb, pal + 4, 768);
176
+            break;
177
+        case MKTAG('F', 'R', 'A', 'M'):
178
+            get_buffer(&s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
179
+            size = AV_RB32(buf + 5);
180
+            if(size > 0xFFFFFF){
181
+                av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
182
+                return -1;
183
+            }
184
+            if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
185
+                return AVERROR_NOMEM;
186
+            memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
187
+            ret = get_buffer(&s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
188
+            if(ret != size){
189
+                av_free_packet(pkt);
190
+                return AVERROR_IO;
191
+            }
192
+            if(pal_size) memcpy(pkt->data, pal, pal_size);
193
+            pkt->stream_index = 0;
194
+            c->frames--;
195
+            c->vidpos = url_ftell(&s->pb);
196
+            c->readvid = 0;
197
+            return 0;
198
+        default:
199
+            av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
200
+            return -1;
201
+        }
202
+    }
203
+    return AVERROR(EIO);
204
+}
205
+
206
+AVInputFormat dxa_demuxer = {
207
+    "dxa",
208
+    "dxa",
209
+    sizeof(DXAContext),
210
+    dxa_probe,
211
+    dxa_read_header,
212
+    dxa_read_packet,
213
+};