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