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 int get_swf_tag(AVIOContext *pb, int *len_ptr)
39 // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
45 static int swf_probe(AVProbeData *p)
47 /* check file header */
48 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
50 return AVPROBE_SCORE_MAX;
55 static int swf_read_header(AVFormatContext *s)
57 SWFContext *swf = s->priv_data;
58 AVIOContext *pb = s->pb;
61 tag = avio_rb32(pb) & 0xffffff00;
63 if (tag == MKBETAG('C', 'W', 'S', 0)) {
64 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
67 if (tag != MKBETAG('F', 'W', 'S', 0))
70 /* skip rectangle size */
71 nbits = avio_r8(pb) >> 3;
72 len = (4 * nbits - 3 + 7) / 8;
74 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
75 avio_rl16(pb); /* frame count */
77 swf->samples_per_frame = 0;
78 s->ctx_flags |= AVFMTCTX_NOHEADER;
82 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
84 SWFContext *swf = s->priv_data;
85 AVIOContext *pb = s->pb;
86 AVStream *vst = NULL, *ast = NULL, *st = 0;
87 int tag, len, i, frame, v, res;
90 uint64_t pos = avio_tell(pb);
91 tag = get_swf_tag(pb, &len);
94 if (tag == TAG_VIDEOSTREAM) {
95 int ch_id = avio_rl16(pb);
98 for (i=0; i<s->nb_streams; i++) {
100 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
109 vst = avformat_new_stream(s, NULL);
113 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
114 vst->codec->codec_id = ff_codec_get_id(swf_codec_tags, avio_r8(pb));
115 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
117 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
118 /* streaming found */
119 int sample_rate_code;
121 for (i=0; i<s->nb_streams; i++) {
123 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
129 swf->samples_per_frame = avio_rl16(pb);
130 ast = avformat_new_stream(s, NULL);
133 ast->id = -1; /* -1 to avoid clash with video stream ch_id */
134 ast->codec->channels = 1 + (v&1);
135 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
136 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
137 ast->need_parsing = AVSTREAM_PARSE_FULL;
138 sample_rate_code= (v>>2) & 3;
139 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
140 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
142 } else if (tag == TAG_VIDEOFRAME) {
143 int ch_id = avio_rl16(pb);
145 for(i=0; i<s->nb_streams; i++) {
147 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
148 frame = avio_rl16(pb);
149 if ((res = av_get_packet(pb, pkt, len-2)) < 0)
153 pkt->stream_index = st->index;
157 } else if (tag == TAG_STREAMBLOCK) {
158 for (i = 0; i < s->nb_streams; i++) {
160 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
161 if (st->codec->codec_id == CODEC_ID_MP3) {
163 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
165 } else { // ADPCM, PCM
166 if ((res = av_get_packet(pb, pkt, len)) < 0)
170 pkt->stream_index = st->index;
174 } else if (tag == TAG_JPEG2) {
175 for (i=0; i<s->nb_streams; i++) {
177 if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
180 if (i == s->nb_streams) {
181 vst = avformat_new_stream(s, NULL);
184 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
185 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
186 vst->codec->codec_id = CODEC_ID_MJPEG;
187 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
190 avio_rl16(pb); /* BITMAP_ID */
191 if ((res = av_new_packet(pkt, len-2)) < 0)
193 avio_read(pb, pkt->data, 4);
194 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
195 AV_RB32(pkt->data) == 0xffd9ffd8) {
196 /* old SWF files containing SOI/EOI as data start */
197 /* files created by swink have reversed tag */
199 avio_read(pb, pkt->data, pkt->size);
201 avio_read(pb, pkt->data + 4, pkt->size - 4);
204 pkt->stream_index = st->index;
212 AVInputFormat ff_swf_demuxer = {
214 .long_name = NULL_IF_CONFIG_SMALL("Flash format"),
215 .priv_data_size = sizeof(SWFContext),
216 .read_probe = swf_probe,
217 .read_header = swf_read_header,
218 .read_packet = swf_read_packet,