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
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;
63 static int swf_read_header(AVFormatContext *s)
65 SWFContext *swf = s->priv_data;
66 AVIOContext *pb = s->pb;
69 tag = avio_rb32(pb) & 0xffffff00;
71 if (tag == MKBETAG('C', 'W', 'S', 0)) {
72 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
75 if (tag != MKBETAG('F', 'W', 'S', 0))
78 /* skip rectangle size */
79 nbits = avio_r8(pb) >> 3;
80 len = (4 * nbits - 3 + 7) / 8;
82 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
83 avio_rl16(pb); /* frame count */
85 swf->samples_per_frame = 0;
86 s->ctx_flags |= AVFMTCTX_NOHEADER;
90 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
92 SWFContext *swf = s->priv_data;
93 AVIOContext *pb = s->pb;
94 AVStream *vst = NULL, *ast = NULL, *st = 0;
95 int tag, len, i, frame, v, res;
98 uint64_t pos = avio_tell(pb);
99 tag = get_swf_tag(pb, &len);
102 if (tag == TAG_VIDEOSTREAM) {
103 int ch_id = avio_rl16(pb);
106 for (i=0; i<s->nb_streams; i++) {
108 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
117 vst = avformat_new_stream(s, NULL);
121 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
122 vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
123 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
125 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
126 /* streaming found */
127 int sample_rate_code;
129 for (i=0; i<s->nb_streams; i++) {
131 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
137 swf->samples_per_frame = avio_rl16(pb);
138 ast = avformat_new_stream(s, NULL);
141 ast->id = -1; /* -1 to avoid clash with video stream ch_id */
142 ast->codec->channels = 1 + (v&1);
143 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
144 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
145 ast->need_parsing = AVSTREAM_PARSE_FULL;
146 sample_rate_code= (v>>2) & 3;
147 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
148 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
150 } else if (tag == TAG_VIDEOFRAME) {
151 int ch_id = avio_rl16(pb);
153 for(i=0; i<s->nb_streams; i++) {
155 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
156 frame = avio_rl16(pb);
157 if ((res = av_get_packet(pb, pkt, len-2)) < 0)
161 pkt->stream_index = st->index;
165 } else if (tag == TAG_STREAMBLOCK) {
166 for (i = 0; i < s->nb_streams; i++) {
168 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
169 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
171 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
173 } else { // ADPCM, PCM
174 if ((res = av_get_packet(pb, pkt, len)) < 0)
178 pkt->stream_index = st->index;
182 } else if (tag == TAG_JPEG2) {
183 for (i=0; i<s->nb_streams; i++) {
185 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
188 if (i == s->nb_streams) {
189 vst = avformat_new_stream(s, NULL);
192 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
193 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
194 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
195 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
198 avio_rl16(pb); /* BITMAP_ID */
199 if ((res = av_new_packet(pkt, len-2)) < 0)
201 avio_read(pb, pkt->data, 4);
202 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
203 AV_RB32(pkt->data) == 0xffd9ffd8) {
204 /* old SWF files containing SOI/EOI as data start */
205 /* files created by swink have reversed tag */
207 avio_read(pb, pkt->data, pkt->size);
209 avio_read(pb, pkt->data + 4, pkt->size - 4);
212 pkt->stream_index = st->index;
220 AVInputFormat ff_swf_demuxer = {
222 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
223 .priv_data_size = sizeof(SWFContext),
224 .read_probe = swf_probe,
225 .read_header = swf_read_header,
226 .read_packet = swf_read_packet,