]> 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     { 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 AVERROR(ENOMEM);
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 AVERROR(ENOMEM);
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_DEFINESOUND) {
204             /* audio stream */
205             int sample_rate_code;
206             int ch_id = avio_rl16(pb);
207
208             for (i=0; i<s->nb_streams; i++) {
209                 st = s->streams[i];
210                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == ch_id)
211                     goto skip;
212             }
213
214             // FIXME: 8-bit uncompressed PCM audio will be interpreted as 16-bit
215             // FIXME: The entire audio stream is stored in a single chunk/tag. Normally,
216             // these are smaller audio streams in DEFINESOUND tags, but it's technically
217             // possible they could be huge. Break it up into multiple packets if it's big.
218             v = avio_r8(pb);
219             ast = avformat_new_stream(s, NULL);
220             if (!ast)
221                 return AVERROR(ENOMEM);
222             ast->id = ch_id;
223             ast->codec->channels = 1 + (v&1);
224             ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
225             ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
226             ast->need_parsing = AVSTREAM_PARSE_FULL;
227             sample_rate_code= (v>>2) & 3;
228             ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
229             avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
230             ast->duration = avio_rl32(pb); // number of samples
231             if (((v>>4) & 15) == 2) { // MP3 sound data record
232                 ast->skip_samples = avio_rl16(pb);
233                 len -= 2;
234             }
235             len -= 7;
236             if ((res = av_get_packet(pb, pkt, len)) < 0)
237                 return res;
238             pkt->pos = pos;
239             pkt->stream_index = ast->index;
240             return pkt->size;
241         } else if (tag == TAG_VIDEOFRAME) {
242             int ch_id = avio_rl16(pb);
243             len -= 2;
244             for(i=0; i<s->nb_streams; i++) {
245                 st = s->streams[i];
246                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
247                     frame = avio_rl16(pb);
248                     if ((res = av_get_packet(pb, pkt, len-2)) < 0)
249                         return res;
250                     pkt->pos = pos;
251                     pkt->pts = frame;
252                     pkt->stream_index = st->index;
253                     return pkt->size;
254                 }
255             }
256         } else if (tag == TAG_STREAMBLOCK) {
257             for (i = 0; i < s->nb_streams; i++) {
258                 st = s->streams[i];
259                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
260             if (st->codec->codec_id == AV_CODEC_ID_MP3) {
261                 avio_skip(pb, 4);
262                 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
263                     return res;
264             } else { // ADPCM, PCM
265                 if ((res = av_get_packet(pb, pkt, len)) < 0)
266                     return res;
267             }
268             pkt->pos = pos;
269             pkt->stream_index = st->index;
270             return pkt->size;
271                 }
272             }
273         } else if (tag == TAG_JPEG2) {
274             for (i=0; i<s->nb_streams; i++) {
275                 st = s->streams[i];
276                 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
277                     break;
278             }
279             if (i == s->nb_streams) {
280                 vst = avformat_new_stream(s, NULL);
281                 if (!vst)
282                     return AVERROR(ENOMEM);
283                 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
284                 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
285                 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
286                 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
287                 st = vst;
288             }
289             avio_rl16(pb); /* BITMAP_ID */
290             if ((res = av_new_packet(pkt, len-2)) < 0)
291                 return res;
292             avio_read(pb, pkt->data, 4);
293             if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
294                 AV_RB32(pkt->data) == 0xffd9ffd8) {
295                 /* old SWF files containing SOI/EOI as data start */
296                 /* files created by swink have reversed tag */
297                 pkt->size -= 4;
298                 avio_read(pb, pkt->data, pkt->size);
299             } else {
300                 avio_read(pb, pkt->data + 4, pkt->size - 4);
301             }
302             pkt->pos = pos;
303             pkt->stream_index = st->index;
304             return pkt->size;
305         }
306     skip:
307         avio_skip(pb, len);
308     }
309 }
310
311 #if CONFIG_ZLIB
312 static av_cold int swf_read_close(AVFormatContext *avctx)
313 {
314     SWFContext *s = avctx->priv_data;
315     inflateEnd(&s->zstream);
316     av_freep(&s->zbuf_in);
317     av_freep(&s->zbuf_out);
318     av_freep(&s->zpb);
319     return 0;
320 }
321 #endif
322
323 AVInputFormat ff_swf_demuxer = {
324     .name           = "swf",
325     .long_name      = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
326     .priv_data_size = sizeof(SWFContext),
327     .read_probe     = swf_probe,
328     .read_header    = swf_read_header,
329     .read_packet    = swf_read_packet,
330 #if CONFIG_ZLIB
331     .read_close     = swf_read_close,
332 #endif
333 };