]> git.sesse.net Git - ffmpeg/blob - libavformat/swfdec.c
Merge remote-tracking branch 'qatar/master'
[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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/intreadwrite.h"
24 #include "swf.h"
25
26 static const AVCodecTag swf_audio_codec_tags[] = {
27     { CODEC_ID_PCM_S16LE,  0x00 },
28     { CODEC_ID_ADPCM_SWF,  0x01 },
29     { CODEC_ID_MP3,        0x02 },
30     { CODEC_ID_PCM_S16LE,  0x03 },
31 //  { CODEC_ID_NELLYMOSER, 0x06 },
32     { CODEC_ID_NONE,          0 },
33 };
34
35 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
36 {
37     int tag, len;
38
39     if (url_feof(pb))
40         return AVERROR_EOF;
41
42     tag = avio_rl16(pb);
43     len = tag & 0x3f;
44     tag = tag >> 6;
45     if (len == 0x3f) {
46         len = avio_rl32(pb);
47     }
48 //    av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
49     *len_ptr = len;
50     return tag;
51 }
52
53
54 static int swf_probe(AVProbeData *p)
55 {
56     /* check file header */
57     if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
58         p->buf[2] == 'S')
59         return AVPROBE_SCORE_MAX;
60     else
61         return 0;
62 }
63
64 #if CONFIG_ZLIB
65 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
66 {
67     AVFormatContext *s = opaque;
68     SWFContext *swf = s->priv_data;
69     z_stream *z = &swf->zstream;
70     int ret;
71
72 retry:
73     if (!z->avail_in) {
74         int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
75         if (n <= 0)
76             return n;
77         z->next_in  = swf->zbuf_in;
78         z->avail_in = n;
79     }
80
81     z->next_out  = buf;
82     z->avail_out = buf_size;
83
84     ret = inflate(z, Z_NO_FLUSH);
85     if (ret < 0)
86         return AVERROR(EINVAL);
87     if (ret == Z_STREAM_END)
88         return AVERROR_EOF;
89
90     if (buf_size - z->avail_out == 0)
91         goto retry;
92
93     return buf_size - z->avail_out;
94 }
95 #endif
96
97 static int swf_read_header(AVFormatContext *s)
98 {
99     SWFContext *swf = s->priv_data;
100     AVIOContext *pb = s->pb;
101     int nbits, len, tag;
102
103     tag = avio_rb32(pb) & 0xffffff00;
104     avio_rl32(pb);
105
106     if (tag == MKBETAG('C', 'W', 'S', 0)) {
107         av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
108 #if CONFIG_ZLIB
109         swf->zbuf_in  = av_malloc(ZBUF_SIZE);
110         swf->zbuf_out = av_malloc(ZBUF_SIZE);
111         swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
112                                       zlib_refill, NULL, NULL);
113         if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
114             return AVERROR(ENOMEM);
115         swf->zpb->seekable = 0;
116         if (inflateInit(&swf->zstream) != Z_OK) {
117             av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
118             return AVERROR(EINVAL);
119         }
120         pb = swf->zpb;
121 #else
122         av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
123         return AVERROR(EIO);
124 #endif
125     } else if (tag != MKBETAG('F', 'W', 'S', 0))
126         return AVERROR(EIO);
127     /* skip rectangle size */
128     nbits = avio_r8(pb) >> 3;
129     len = (4 * nbits - 3 + 7) / 8;
130     avio_skip(pb, len);
131     swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
132     avio_rl16(pb); /* frame count */
133
134     swf->samples_per_frame = 0;
135     s->ctx_flags |= AVFMTCTX_NOHEADER;
136     return 0;
137 }
138
139 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
140 {
141     SWFContext *swf = s->priv_data;
142     AVIOContext *pb = s->pb;
143     AVStream *vst = NULL, *ast = NULL, *st = 0;
144     int tag, len, i, frame, v, res;
145
146 #if CONFIG_ZLIB
147     if (swf->zpb)
148         pb = swf->zpb;
149 #endif
150
151     for(;;) {
152         uint64_t pos = avio_tell(pb);
153         tag = get_swf_tag(pb, &len);
154         if (tag < 0)
155             return tag;
156         if (tag == TAG_VIDEOSTREAM) {
157             int ch_id = avio_rl16(pb);
158             len -= 2;
159
160             for (i=0; i<s->nb_streams; i++) {
161                 st = s->streams[i];
162                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
163                     goto skip;
164             }
165
166             avio_rl16(pb);
167             avio_rl16(pb);
168             avio_rl16(pb);
169             avio_r8(pb);
170             /* Check for FLV1 */
171             vst = avformat_new_stream(s, NULL);
172             if (!vst)
173                 return -1;
174             vst->id = ch_id;
175             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
176             vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
177             avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
178             len -= 8;
179         } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
180             /* streaming found */
181             int sample_rate_code;
182
183             for (i=0; i<s->nb_streams; i++) {
184                 st = s->streams[i];
185                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
186                     goto skip;
187             }
188
189             avio_r8(pb);
190             v = avio_r8(pb);
191             swf->samples_per_frame = avio_rl16(pb);
192             ast = avformat_new_stream(s, NULL);
193             if (!ast)
194                 return -1;
195             ast->id = -1; /* -1 to avoid clash with video stream ch_id */
196             ast->codec->channels = 1 + (v&1);
197             ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
198             ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
199             ast->need_parsing = AVSTREAM_PARSE_FULL;
200             sample_rate_code= (v>>2) & 3;
201             ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
202             avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
203             len -= 4;
204         } else if (tag == TAG_VIDEOFRAME) {
205             int ch_id = avio_rl16(pb);
206             len -= 2;
207             for(i=0; i<s->nb_streams; i++) {
208                 st = s->streams[i];
209                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
210                     frame = avio_rl16(pb);
211                     if ((res = av_get_packet(pb, pkt, len-2)) < 0)
212                         return res;
213                     pkt->pos = pos;
214                     pkt->pts = frame;
215                     pkt->stream_index = st->index;
216                     return pkt->size;
217                 }
218             }
219         } else if (tag == TAG_STREAMBLOCK) {
220             for (i = 0; i < s->nb_streams; i++) {
221                 st = s->streams[i];
222                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
223             if (st->codec->codec_id == AV_CODEC_ID_MP3) {
224                 avio_skip(pb, 4);
225                 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
226                     return res;
227             } else { // ADPCM, PCM
228                 if ((res = av_get_packet(pb, pkt, len)) < 0)
229                     return res;
230             }
231             pkt->pos = pos;
232             pkt->stream_index = st->index;
233             return pkt->size;
234                 }
235             }
236         } else if (tag == TAG_JPEG2) {
237             for (i=0; i<s->nb_streams; i++) {
238                 st = s->streams[i];
239                 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
240                     break;
241             }
242             if (i == s->nb_streams) {
243                 vst = avformat_new_stream(s, NULL);
244                 if (!vst)
245                     return -1;
246                 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
247                 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
248                 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
249                 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
250                 st = vst;
251             }
252             avio_rl16(pb); /* BITMAP_ID */
253             if ((res = av_new_packet(pkt, len-2)) < 0)
254                 return res;
255             avio_read(pb, pkt->data, 4);
256             if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
257                 AV_RB32(pkt->data) == 0xffd9ffd8) {
258                 /* old SWF files containing SOI/EOI as data start */
259                 /* files created by swink have reversed tag */
260                 pkt->size -= 4;
261                 avio_read(pb, pkt->data, pkt->size);
262             } else {
263                 avio_read(pb, pkt->data + 4, pkt->size - 4);
264             }
265             pkt->pos = pos;
266             pkt->stream_index = st->index;
267             return pkt->size;
268         }
269     skip:
270         avio_skip(pb, len);
271     }
272 }
273
274 #if CONFIG_ZLIB
275 static av_cold int swf_read_close(AVFormatContext *avctx)
276 {
277     SWFContext *s = avctx->priv_data;
278     inflateEnd(&s->zstream);
279     av_freep(&s->zbuf_in);
280     av_freep(&s->zbuf_out);
281     av_freep(&s->zpb);
282     return 0;
283 }
284 #endif
285
286 AVInputFormat ff_swf_demuxer = {
287     .name           = "swf",
288     .long_name      = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
289     .priv_data_size = sizeof(SWFContext),
290     .read_probe     = swf_probe,
291     .read_header    = swf_read_header,
292     .read_packet    = swf_read_packet,
293 #if CONFIG_ZLIB
294     .read_close     = swf_read_close,
295 #endif
296 };