]> git.sesse.net Git - ffmpeg/blob - libavformat/swfdec.c
swfdec: support compressed swf
[ffmpeg] / libavformat / swfdec.c
1 /*
2  * Flash Compatible Streaming Format demuxer
3  * Copyright (c) 2000 Fabrice Bellard
4  * Copyright (c) 2003 Tinic Uro
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "config.h"
24
25 #if CONFIG_ZLIB
26 #include <zlib.h>
27 #endif
28
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/intreadwrite.h"
31 #include "swf.h"
32
33 static const AVCodecTag swf_audio_codec_tags[] = {
34     { AV_CODEC_ID_PCM_S16LE,  0x00 },
35     { AV_CODEC_ID_ADPCM_SWF,  0x01 },
36     { AV_CODEC_ID_MP3,        0x02 },
37     { AV_CODEC_ID_PCM_S16LE,  0x03 },
38 //  { AV_CODEC_ID_NELLYMOSER, 0x06 },
39     { AV_CODEC_ID_NONE,          0 },
40 };
41
42 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
43 {
44     int tag, len;
45
46     if (pb->eof_reached)
47         return -1;
48
49     tag = avio_rl16(pb);
50     len = tag & 0x3f;
51     tag = tag >> 6;
52     if (len == 0x3f) {
53         len = avio_rl32(pb);
54     }
55     *len_ptr = len;
56     return tag;
57 }
58
59
60 static int swf_probe(AVProbeData *p)
61 {
62     /* check file header */
63     if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
64         p->buf[2] == 'S')
65         return AVPROBE_SCORE_MAX;
66     else
67         return 0;
68 }
69
70 #if CONFIG_ZLIB
71 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
72 {
73     AVFormatContext *s = opaque;
74     SWFContext *swf = s->priv_data;
75     z_stream *z = &swf->zstream;
76     int ret;
77
78 retry:
79     if (!z->avail_in) {
80         int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
81         if (n < 0)
82             return n;
83         z->next_in  = swf->zbuf_in;
84         z->avail_in = n;
85     }
86
87     z->next_out  = buf;
88     z->avail_out = buf_size;
89
90     ret = inflate(z, Z_NO_FLUSH);
91     if (ret != Z_OK && ret != Z_STREAM_END) {
92         av_log(s, AV_LOG_ERROR, "Inflate error: %d\n", ret);
93         return AVERROR_UNKNOWN;
94     }
95
96     if (buf_size - z->avail_out == 0)
97         goto retry;
98
99     return buf_size - z->avail_out;
100 }
101 #endif
102
103 static int swf_read_header(AVFormatContext *s)
104 {
105     SWFContext *swf = s->priv_data;
106     AVIOContext *pb = s->pb;
107     int nbits, len, tag;
108
109     tag = avio_rb32(pb) & 0xffffff00;
110     avio_rl32(pb);
111
112     if (tag == MKBETAG('C', 'W', 'S', 0)) {
113         av_log(s, AV_LOG_INFO, "Compressed SWF file detected\n");
114 #if CONFIG_ZLIB
115         if (inflateInit(&swf->zstream) != Z_OK) {
116             av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
117             return AVERROR(EINVAL);
118         }
119         swf->zbuf_in  = av_malloc(ZBUF_SIZE);
120         swf->zbuf_out = av_malloc(ZBUF_SIZE);
121         swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
122                                       zlib_refill, NULL, NULL);
123         if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb) {
124             av_freep(&swf->zbuf_in);
125             av_freep(&swf->zbuf_out);
126             av_freep(&swf->zpb);
127             return AVERROR(ENOMEM);
128         }
129         swf->zpb->seekable = 0;
130         pb = swf->zpb;
131 #else
132         av_log(s, AV_LOG_ERROR, "missing zlib support, unable to open\n");
133         return AVERROR(EIO);
134 #endif
135     } else if (tag != MKBETAG('F', 'W', 'S', 0))
136         return AVERROR(EIO);
137     /* skip rectangle size */
138     nbits = avio_r8(pb) >> 3;
139     len = (4 * nbits - 3 + 7) / 8;
140     avio_skip(pb, len);
141     swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
142     avio_rl16(pb); /* frame count */
143
144     swf->samples_per_frame = 0;
145     s->ctx_flags |= AVFMTCTX_NOHEADER;
146     return 0;
147 }
148
149 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
150 {
151     SWFContext *swf = s->priv_data;
152     AVIOContext *pb = s->pb;
153     AVStream *vst = NULL, *ast = NULL, *st = 0;
154     int tag, len, i, frame, v, res;
155
156 #if CONFIG_ZLIB
157     if (swf->zpb)
158         pb = swf->zpb;
159 #endif
160
161     for(;;) {
162         uint64_t pos = avio_tell(pb);
163         tag = get_swf_tag(pb, &len);
164         if (tag < 0)
165             return AVERROR(EIO);
166         if (len < 0) {
167             av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
168             return AVERROR_INVALIDDATA;
169         }
170         if (tag == TAG_VIDEOSTREAM) {
171             int ch_id = avio_rl16(pb);
172             len -= 2;
173
174             for (i=0; i<s->nb_streams; i++) {
175                 st = s->streams[i];
176                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
177                     goto skip;
178             }
179
180             avio_rl16(pb);
181             avio_rl16(pb);
182             avio_rl16(pb);
183             avio_r8(pb);
184             /* Check for FLV1 */
185             vst = avformat_new_stream(s, NULL);
186             if (!vst)
187                 return -1;
188             vst->id = ch_id;
189             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
190             vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
191             avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
192             len -= 8;
193         } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
194             /* streaming found */
195             int sample_rate_code;
196
197             for (i=0; i<s->nb_streams; i++) {
198                 st = s->streams[i];
199                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
200                     goto skip;
201             }
202
203             avio_r8(pb);
204             v = avio_r8(pb);
205             swf->samples_per_frame = avio_rl16(pb);
206             ast = avformat_new_stream(s, NULL);
207             if (!ast)
208                 return -1;
209             ast->id = -1; /* -1 to avoid clash with video stream ch_id */
210             if (v & 1) {
211                 ast->codec->channels       = 2;
212                 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
213             } else {
214                 ast->codec->channels       = 1;
215                 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
216             }
217             ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
218             ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
219             ast->need_parsing = AVSTREAM_PARSE_FULL;
220             sample_rate_code= (v>>2) & 3;
221             ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
222             avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
223             len -= 4;
224         } else if (tag == TAG_VIDEOFRAME) {
225             int ch_id = avio_rl16(pb);
226             len -= 2;
227             for(i=0; i<s->nb_streams; i++) {
228                 st = s->streams[i];
229                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
230                     frame = avio_rl16(pb);
231                     len -= 2;
232                     if (len <= 0)
233                         goto skip;
234                     if ((res = av_get_packet(pb, pkt, len)) < 0)
235                         return res;
236                     pkt->pos = pos;
237                     pkt->pts = frame;
238                     pkt->stream_index = st->index;
239                     return pkt->size;
240                 }
241             }
242         } else if (tag == TAG_STREAMBLOCK) {
243             for (i = 0; i < s->nb_streams; i++) {
244                 st = s->streams[i];
245                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
246                     if (st->codec->codec_id == AV_CODEC_ID_MP3) {
247                         avio_skip(pb, 4);
248                         len -= 4;
249                         if (len <= 0)
250                             goto skip;
251                         if ((res = av_get_packet(pb, pkt, len)) < 0)
252                             return res;
253                     } else { // ADPCM, PCM
254                         if (len <= 0)
255                             goto skip;
256                         if ((res = av_get_packet(pb, pkt, len)) < 0)
257                             return res;
258                     }
259                     pkt->pos          = pos;
260                     pkt->stream_index = st->index;
261                     return pkt->size;
262                 }
263             }
264         } else if (tag == TAG_JPEG2) {
265             for (i=0; i<s->nb_streams; i++) {
266                 st = s->streams[i];
267                 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
268                     break;
269             }
270             if (i == s->nb_streams) {
271                 vst = avformat_new_stream(s, NULL);
272                 if (!vst)
273                     return -1;
274                 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
275                 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
276                 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
277                 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
278                 st = vst;
279             }
280             avio_rl16(pb); /* BITMAP_ID */
281             len -= 2;
282             if (len < 4)
283                 goto skip;
284             if ((res = av_new_packet(pkt, len)) < 0)
285                 return res;
286             avio_read(pb, pkt->data, 4);
287             if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
288                 AV_RB32(pkt->data) == 0xffd9ffd8) {
289                 /* old SWF files containing SOI/EOI as data start */
290                 /* files created by swink have reversed tag */
291                 pkt->size -= 4;
292                 avio_read(pb, pkt->data, pkt->size);
293             } else {
294                 avio_read(pb, pkt->data + 4, pkt->size - 4);
295             }
296             pkt->pos = pos;
297             pkt->stream_index = st->index;
298             return pkt->size;
299         }
300     skip:
301         len = FFMAX(0, len);
302         avio_skip(pb, len);
303     }
304 }
305
306 #if CONFIG_ZLIB
307 static av_cold int swf_read_close(AVFormatContext *avctx)
308 {
309     SWFContext *s = avctx->priv_data;
310     inflateEnd(&s->zstream);
311     av_freep(&s->zbuf_in);
312     av_freep(&s->zbuf_out);
313     av_freep(&s->zpb);
314     return 0;
315 }
316 #endif
317
318 AVInputFormat ff_swf_demuxer = {
319     .name           = "swf",
320     .long_name      = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
321     .priv_data_size = sizeof(SWFContext),
322     .read_probe     = swf_probe,
323     .read_header    = swf_read_header,
324     .read_packet    = swf_read_packet,
325 #if CONFIG_ZLIB
326     .read_close     = swf_read_close,
327 #endif
328 };