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)
53 static int swf_probe(AVProbeData *p)
55 /* check file header */
56 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
58 return AVPROBE_SCORE_MAX;
64 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
66 AVFormatContext *s = opaque;
67 SWFContext *swf = s->priv_data;
68 z_stream *z = &swf->zstream;
73 int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
76 z->next_in = swf->zbuf_in;
81 z->avail_out = buf_size;
83 ret = inflate(z, Z_NO_FLUSH);
85 return AVERROR(EINVAL);
86 if (ret == Z_STREAM_END)
89 if (buf_size - z->avail_out == 0)
92 return buf_size - z->avail_out;
96 static int swf_read_header(AVFormatContext *s)
98 SWFContext *swf = s->priv_data;
99 AVIOContext *pb = s->pb;
102 tag = avio_rb32(pb) & 0xffffff00;
105 if (tag == MKBETAG('C', 'W', 'S', 0)) {
106 av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
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);
121 av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
124 } else if (tag != MKBETAG('F', 'W', 'S', 0))
126 /* skip rectangle size */
127 nbits = avio_r8(pb) >> 3;
128 len = (4 * nbits - 3 + 7) / 8;
130 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
131 avio_rl16(pb); /* frame count */
133 swf->samples_per_frame = 0;
134 s->ctx_flags |= AVFMTCTX_NOHEADER;
138 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
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;
151 uint64_t pos = avio_tell(pb);
152 tag = get_swf_tag(pb, &len);
155 if (tag == TAG_VIDEOSTREAM) {
156 int ch_id = avio_rl16(pb);
159 for (i=0; i<s->nb_streams; i++) {
161 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
170 vst = avformat_new_stream(s, NULL);
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);
178 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
179 /* streaming found */
180 int sample_rate_code;
182 for (i=0; i<s->nb_streams; i++) {
184 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
190 swf->samples_per_frame = avio_rl16(pb);
191 ast = avformat_new_stream(s, NULL);
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);
203 } else if (tag == TAG_VIDEOFRAME) {
204 int ch_id = avio_rl16(pb);
206 for(i=0; i<s->nb_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)
214 pkt->stream_index = st->index;
218 } else if (tag == TAG_STREAMBLOCK) {
219 for (i = 0; i < s->nb_streams; i++) {
221 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
222 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
224 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
226 } else { // ADPCM, PCM
227 if ((res = av_get_packet(pb, pkt, len)) < 0)
231 pkt->stream_index = st->index;
235 } else if (tag == TAG_JPEG2) {
236 for (i=0; i<s->nb_streams; i++) {
238 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
241 if (i == s->nb_streams) {
242 vst = avformat_new_stream(s, NULL);
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);
251 avio_rl16(pb); /* BITMAP_ID */
252 if ((res = av_new_packet(pkt, len-2)) < 0)
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 */
260 avio_read(pb, pkt->data, pkt->size);
262 avio_read(pb, pkt->data + 4, pkt->size - 4);
265 pkt->stream_index = st->index;
274 static av_cold int swf_read_close(AVFormatContext *avctx)
276 SWFContext *s = avctx->priv_data;
277 inflateEnd(&s->zstream);
278 av_freep(&s->zbuf_in);
279 av_freep(&s->zbuf_out);
285 AVInputFormat ff_swf_demuxer = {
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,
293 .read_close = swf_read_close,