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/channel_layout.h"
24 #include "libavutil/intreadwrite.h"
27 static const AVCodecTag swf_audio_codec_tags[] = {
28 { AV_CODEC_ID_PCM_S16LE, 0x00 },
29 { AV_CODEC_ID_ADPCM_SWF, 0x01 },
30 { AV_CODEC_ID_MP3, 0x02 },
31 { AV_CODEC_ID_PCM_S16LE, 0x03 },
32 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
33 { AV_CODEC_ID_NONE, 0 },
36 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
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;
64 static int swf_read_header(AVFormatContext *s)
66 SWFContext *swf = s->priv_data;
67 AVIOContext *pb = s->pb;
70 tag = avio_rb32(pb) & 0xffffff00;
72 if (tag == MKBETAG('C', 'W', 'S', 0)) {
73 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
76 if (tag != MKBETAG('F', 'W', 'S', 0))
79 /* skip rectangle size */
80 nbits = avio_r8(pb) >> 3;
81 len = (4 * nbits - 3 + 7) / 8;
83 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
84 avio_rl16(pb); /* frame count */
86 swf->samples_per_frame = 0;
87 s->ctx_flags |= AVFMTCTX_NOHEADER;
91 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
93 SWFContext *swf = s->priv_data;
94 AVIOContext *pb = s->pb;
95 AVStream *vst = NULL, *ast = NULL, *st = 0;
96 int tag, len, i, frame, v, res;
99 uint64_t pos = avio_tell(pb);
100 tag = get_swf_tag(pb, &len);
104 av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
105 return AVERROR_INVALIDDATA;
107 if (tag == TAG_VIDEOSTREAM) {
108 int ch_id = avio_rl16(pb);
111 for (i=0; i<s->nb_streams; i++) {
113 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
122 vst = avformat_new_stream(s, NULL);
126 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
127 vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
128 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
130 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
131 /* streaming found */
132 int sample_rate_code;
134 for (i=0; i<s->nb_streams; i++) {
136 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
142 swf->samples_per_frame = avio_rl16(pb);
143 ast = avformat_new_stream(s, NULL);
146 ast->id = -1; /* -1 to avoid clash with video stream ch_id */
148 ast->codec->channels = 2;
149 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
151 ast->codec->channels = 1;
152 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
154 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
155 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
156 ast->need_parsing = AVSTREAM_PARSE_FULL;
157 sample_rate_code= (v>>2) & 3;
158 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
159 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
161 } else if (tag == TAG_VIDEOFRAME) {
162 int ch_id = avio_rl16(pb);
164 for(i=0; i<s->nb_streams; i++) {
166 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
167 frame = avio_rl16(pb);
171 if ((res = av_get_packet(pb, pkt, len)) < 0)
175 pkt->stream_index = st->index;
179 } else if (tag == TAG_STREAMBLOCK) {
180 for (i = 0; i < s->nb_streams; i++) {
182 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
183 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
188 if ((res = av_get_packet(pb, pkt, len)) < 0)
190 } else { // ADPCM, PCM
193 if ((res = av_get_packet(pb, pkt, len)) < 0)
197 pkt->stream_index = st->index;
201 } else if (tag == TAG_JPEG2) {
202 for (i=0; i<s->nb_streams; i++) {
204 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
207 if (i == s->nb_streams) {
208 vst = avformat_new_stream(s, NULL);
211 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
212 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
213 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
214 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
217 avio_rl16(pb); /* BITMAP_ID */
221 if ((res = av_new_packet(pkt, len)) < 0)
223 avio_read(pb, pkt->data, 4);
224 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
225 AV_RB32(pkt->data) == 0xffd9ffd8) {
226 /* old SWF files containing SOI/EOI as data start */
227 /* files created by swink have reversed tag */
229 avio_read(pb, pkt->data, pkt->size);
231 avio_read(pb, pkt->data + 4, pkt->size - 4);
234 pkt->stream_index = st->index;
243 AVInputFormat ff_swf_demuxer = {
245 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
246 .priv_data_size = sizeof(SWFContext),
247 .read_probe = swf_probe,
248 .read_header = swf_read_header,
249 .read_packet = swf_read_packet,