2 * Flash Compatible Streaming Format demuxer
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2003 Tinic Uro
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
26 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
39 // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
45 static int swf_probe(AVProbeData *p)
47 /* check file header */
48 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
50 return AVPROBE_SCORE_MAX;
56 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
58 AVFormatContext *s = opaque;
59 SWFContext *swf = s->priv_data;
60 z_stream *z = &swf->zstream;
65 int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
68 z->next_in = swf->zbuf_in;
73 z->avail_out = buf_size;
75 ret = inflate(z, Z_NO_FLUSH);
77 return AVERROR(EINVAL);
78 if (ret == Z_STREAM_END)
81 if (buf_size - z->avail_out == 0)
84 return buf_size - z->avail_out;
88 static int swf_read_header(AVFormatContext *s)
90 SWFContext *swf = s->priv_data;
91 AVIOContext *pb = s->pb;
94 tag = avio_rb32(pb) & 0xffffff00;
97 if (tag == MKBETAG('C', 'W', 'S', 0)) {
98 av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
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);
113 av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
116 } else if (tag != MKBETAG('F', 'W', 'S', 0))
118 /* skip rectangle size */
119 nbits = avio_r8(pb) >> 3;
120 len = (4 * nbits - 3 + 7) / 8;
122 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
123 avio_rl16(pb); /* frame count */
125 swf->samples_per_frame = 0;
126 s->ctx_flags |= AVFMTCTX_NOHEADER;
130 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
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;
143 uint64_t pos = avio_tell(pb);
144 tag = get_swf_tag(pb, &len);
147 if (tag == TAG_VIDEOSTREAM) {
148 int ch_id = avio_rl16(pb);
151 for (i=0; i<s->nb_streams; i++) {
153 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
162 vst = avformat_new_stream(s, NULL);
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);
170 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
171 /* streaming found */
172 int sample_rate_code;
174 for (i=0; i<s->nb_streams; i++) {
176 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
182 swf->samples_per_frame = avio_rl16(pb);
183 ast = avformat_new_stream(s, NULL);
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);
195 } else if (tag == TAG_VIDEOFRAME) {
196 int ch_id = avio_rl16(pb);
198 for(i=0; i<s->nb_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)
206 pkt->stream_index = st->index;
210 } else if (tag == TAG_STREAMBLOCK) {
211 for (i = 0; i < s->nb_streams; i++) {
213 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
214 if (st->codec->codec_id == CODEC_ID_MP3) {
216 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
218 } else { // ADPCM, PCM
219 if ((res = av_get_packet(pb, pkt, len)) < 0)
223 pkt->stream_index = st->index;
227 } else if (tag == TAG_JPEG2) {
228 for (i=0; i<s->nb_streams; i++) {
230 if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
233 if (i == s->nb_streams) {
234 vst = avformat_new_stream(s, NULL);
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 = CODEC_ID_MJPEG;
240 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
243 avio_rl16(pb); /* BITMAP_ID */
244 if ((res = av_new_packet(pkt, len-2)) < 0)
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 */
252 avio_read(pb, pkt->data, pkt->size);
254 avio_read(pb, pkt->data + 4, pkt->size - 4);
257 pkt->stream_index = st->index;
266 static av_cold int swf_read_close(AVFormatContext *avctx)
268 SWFContext *s = avctx->priv_data;
269 inflateEnd(&s->zstream);
270 av_freep(&s->zbuf_in);
271 av_freep(&s->zbuf_out);
277 AVInputFormat ff_swf_demuxer = {
279 .long_name = NULL_IF_CONFIG_SMALL("Flash format"),
280 .priv_data_size = sizeof(SWFContext),
281 .read_probe = swf_probe,
282 .read_header = swf_read_header,
283 .read_packet = swf_read_packet,
285 .read_close = swf_read_close,