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