2 * Flash Compatible Streaming Format demuxer
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2003 Tinic Uro
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/intreadwrite.h"
33 static const AVCodecTag swf_audio_codec_tags[] = {
34 { AV_CODEC_ID_PCM_S16LE, 0x00 },
35 { AV_CODEC_ID_ADPCM_SWF, 0x01 },
36 { AV_CODEC_ID_MP3, 0x02 },
37 { AV_CODEC_ID_PCM_S16LE, 0x03 },
38 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
39 { AV_CODEC_ID_NONE, 0 },
42 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
60 static int swf_probe(AVProbeData *p)
62 /* check file header */
63 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
65 return AVPROBE_SCORE_MAX;
71 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
73 AVFormatContext *s = opaque;
74 SWFContext *swf = s->priv_data;
75 z_stream *z = &swf->zstream;
80 int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
83 z->next_in = swf->zbuf_in;
88 z->avail_out = buf_size;
90 ret = inflate(z, Z_NO_FLUSH);
91 if (ret != Z_OK && ret != Z_STREAM_END) {
92 av_log(s, AV_LOG_ERROR, "Inflate error: %d\n", ret);
93 return AVERROR_UNKNOWN;
96 if (buf_size - z->avail_out == 0)
99 return buf_size - z->avail_out;
103 static int swf_read_header(AVFormatContext *s)
105 SWFContext *swf = s->priv_data;
106 AVIOContext *pb = s->pb;
109 tag = avio_rb32(pb) & 0xffffff00;
112 if (tag == MKBETAG('C', 'W', 'S', 0)) {
113 av_log(s, AV_LOG_INFO, "Compressed SWF file detected\n");
115 if (inflateInit(&swf->zstream) != Z_OK) {
116 av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
117 return AVERROR(EINVAL);
119 swf->zbuf_in = av_malloc(ZBUF_SIZE);
120 swf->zbuf_out = av_malloc(ZBUF_SIZE);
121 swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
122 zlib_refill, NULL, NULL);
123 if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb) {
124 av_freep(&swf->zbuf_in);
125 av_freep(&swf->zbuf_out);
127 return AVERROR(ENOMEM);
129 swf->zpb->seekable = 0;
132 av_log(s, AV_LOG_ERROR, "missing zlib support, unable to open\n");
135 } else if (tag != MKBETAG('F', 'W', 'S', 0))
137 /* skip rectangle size */
138 nbits = avio_r8(pb) >> 3;
139 len = (4 * nbits - 3 + 7) / 8;
141 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
142 avio_rl16(pb); /* frame count */
144 swf->samples_per_frame = 0;
145 s->ctx_flags |= AVFMTCTX_NOHEADER;
149 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
151 SWFContext *swf = s->priv_data;
152 AVIOContext *pb = s->pb;
153 AVStream *vst = NULL, *ast = NULL, *st = 0;
154 int tag, len, i, frame, v, res;
162 uint64_t pos = avio_tell(pb);
163 tag = get_swf_tag(pb, &len);
167 av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
168 return AVERROR_INVALIDDATA;
170 if (tag == TAG_VIDEOSTREAM) {
171 int ch_id = avio_rl16(pb);
174 for (i=0; i<s->nb_streams; i++) {
176 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
185 vst = avformat_new_stream(s, NULL);
189 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
190 vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
191 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
193 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
194 /* streaming found */
195 int sample_rate_code;
197 for (i=0; i<s->nb_streams; i++) {
199 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
205 swf->samples_per_frame = avio_rl16(pb);
206 ast = avformat_new_stream(s, NULL);
209 ast->id = -1; /* -1 to avoid clash with video stream ch_id */
211 ast->codec->channels = 2;
212 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
214 ast->codec->channels = 1;
215 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
217 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
218 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
219 ast->need_parsing = AVSTREAM_PARSE_FULL;
220 sample_rate_code= (v>>2) & 3;
221 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
222 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
224 } else if (tag == TAG_VIDEOFRAME) {
225 int ch_id = avio_rl16(pb);
227 for(i=0; i<s->nb_streams; i++) {
229 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
230 frame = avio_rl16(pb);
234 if ((res = av_get_packet(pb, pkt, len)) < 0)
238 pkt->stream_index = st->index;
242 } else if (tag == TAG_STREAMBLOCK) {
243 for (i = 0; i < s->nb_streams; i++) {
245 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
246 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
251 if ((res = av_get_packet(pb, pkt, len)) < 0)
253 } else { // ADPCM, PCM
256 if ((res = av_get_packet(pb, pkt, len)) < 0)
260 pkt->stream_index = st->index;
264 } else if (tag == TAG_JPEG2) {
265 for (i=0; i<s->nb_streams; i++) {
267 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
270 if (i == s->nb_streams) {
271 vst = avformat_new_stream(s, NULL);
274 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
275 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
276 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
277 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
280 avio_rl16(pb); /* BITMAP_ID */
284 if ((res = av_new_packet(pkt, len)) < 0)
286 avio_read(pb, pkt->data, 4);
287 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
288 AV_RB32(pkt->data) == 0xffd9ffd8) {
289 /* old SWF files containing SOI/EOI as data start */
290 /* files created by swink have reversed tag */
292 avio_read(pb, pkt->data, pkt->size);
294 avio_read(pb, pkt->data + 4, pkt->size - 4);
297 pkt->stream_index = st->index;
307 static av_cold int swf_read_close(AVFormatContext *avctx)
309 SWFContext *s = avctx->priv_data;
310 inflateEnd(&s->zstream);
311 av_freep(&s->zbuf_in);
312 av_freep(&s->zbuf_out);
318 AVInputFormat ff_swf_demuxer = {
320 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
321 .priv_data_size = sizeof(SWFContext),
322 .read_probe = swf_probe,
323 .read_header = swf_read_header,
324 .read_packet = swf_read_packet,
326 .read_close = swf_read_close,