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 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 },
35 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
48 // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
54 static int swf_probe(AVProbeData *p)
56 /* check file header */
57 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
59 return AVPROBE_SCORE_MAX;
65 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
67 AVFormatContext *s = opaque;
68 SWFContext *swf = s->priv_data;
69 z_stream *z = &swf->zstream;
74 int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
77 z->next_in = swf->zbuf_in;
82 z->avail_out = buf_size;
84 ret = inflate(z, Z_NO_FLUSH);
86 return AVERROR(EINVAL);
87 if (ret == Z_STREAM_END)
90 if (buf_size - z->avail_out == 0)
93 return buf_size - z->avail_out;
97 static int swf_read_header(AVFormatContext *s)
99 SWFContext *swf = s->priv_data;
100 AVIOContext *pb = s->pb;
103 tag = avio_rb32(pb) & 0xffffff00;
106 if (tag == MKBETAG('C', 'W', 'S', 0)) {
107 av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
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);
122 av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
125 } else if (tag != MKBETAG('F', 'W', 'S', 0))
127 /* skip rectangle size */
128 nbits = avio_r8(pb) >> 3;
129 len = (4 * nbits - 3 + 7) / 8;
131 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
132 avio_rl16(pb); /* frame count */
134 swf->samples_per_frame = 0;
135 s->ctx_flags |= AVFMTCTX_NOHEADER;
139 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
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;
152 uint64_t pos = avio_tell(pb);
153 tag = get_swf_tag(pb, &len);
156 if (tag == TAG_VIDEOSTREAM) {
157 int ch_id = avio_rl16(pb);
160 for (i=0; i<s->nb_streams; i++) {
162 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
171 vst = avformat_new_stream(s, NULL);
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);
179 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
180 /* streaming found */
181 int sample_rate_code;
183 for (i=0; i<s->nb_streams; i++) {
185 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
191 swf->samples_per_frame = avio_rl16(pb);
192 ast = avformat_new_stream(s, NULL);
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);
204 } else if (tag == TAG_VIDEOFRAME) {
205 int ch_id = avio_rl16(pb);
207 for(i=0; i<s->nb_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)
215 pkt->stream_index = st->index;
219 } else if (tag == TAG_STREAMBLOCK) {
220 for (i = 0; i < s->nb_streams; i++) {
222 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
223 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
225 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
227 } else { // ADPCM, PCM
228 if ((res = av_get_packet(pb, pkt, len)) < 0)
232 pkt->stream_index = st->index;
236 } else if (tag == TAG_JPEG2) {
237 for (i=0; i<s->nb_streams; i++) {
239 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
242 if (i == s->nb_streams) {
243 vst = avformat_new_stream(s, NULL);
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);
252 avio_rl16(pb); /* BITMAP_ID */
253 if ((res = av_new_packet(pkt, len-2)) < 0)
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 */
261 avio_read(pb, pkt->data, pkt->size);
263 avio_read(pb, pkt->data + 4, pkt->size - 4);
266 pkt->stream_index = st->index;
275 static av_cold int swf_read_close(AVFormatContext *avctx)
277 SWFContext *s = avctx->priv_data;
278 inflateEnd(&s->zstream);
279 av_freep(&s->zbuf_in);
280 av_freep(&s->zbuf_out);
286 AVInputFormat ff_swf_demuxer = {
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,
294 .read_close = swf_read_close,