1/*
2 * Flash Compatible Streaming Format demuxer
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2003 Tinic Uro
5 *
6 * This file is part of Libav.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include "libavutil/intreadwrite.h"
24#include "swf.h"
25
26static int get_swf_tag(AVIOContext *pb, int *len_ptr)
27{
28    int tag, len;
29
30    if (pb->eof_reached)
31        return -1;
32
33    tag = avio_rl16(pb);
34    len = tag & 0x3f;
35    tag = tag >> 6;
36    if (len == 0x3f) {
37        len = avio_rl32(pb);
38    }
39//    av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
40    *len_ptr = len;
41    return tag;
42}
43
44
45static int swf_probe(AVProbeData *p)
46{
47    /* check file header */
48    if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
49        p->buf[2] == 'S')
50        return AVPROBE_SCORE_MAX;
51    else
52        return 0;
53}
54
55static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
56{
57    SWFContext *swf = s->priv_data;
58    AVIOContext *pb = s->pb;
59    int nbits, len, tag;
60
61    tag = avio_rb32(pb) & 0xffffff00;
62
63    if (tag == MKBETAG('C', 'W', 'S', 0)) {
64        av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
65        return AVERROR(EIO);
66    }
67    if (tag != MKBETAG('F', 'W', 'S', 0))
68        return AVERROR(EIO);
69    avio_rl32(pb);
70    /* skip rectangle size */
71    nbits = avio_r8(pb) >> 3;
72    len = (4 * nbits - 3 + 7) / 8;
73    avio_skip(pb, len);
74    swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
75    avio_rl16(pb); /* frame count */
76
77    swf->samples_per_frame = 0;
78    s->ctx_flags |= AVFMTCTX_NOHEADER;
79    return 0;
80}
81
82static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
83{
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;
88
89    for(;;) {
90        uint64_t pos = avio_tell(pb);
91        tag = get_swf_tag(pb, &len);
92        if (tag < 0)
93            return AVERROR(EIO);
94        if (tag == TAG_VIDEOSTREAM) {
95            int ch_id = avio_rl16(pb);
96            len -= 2;
97
98            for (i=0; i<s->nb_streams; i++) {
99                st = s->streams[i];
100                if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
101                    goto skip;
102            }
103
104            avio_rl16(pb);
105            avio_rl16(pb);
106            avio_rl16(pb);
107            avio_r8(pb);
108            /* Check for FLV1 */
109            vst = avformat_new_stream(s, NULL);
110            if (!vst)
111                return -1;
112            vst->id = ch_id;
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);
116            vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
117            len -= 8;
118        } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
119            /* streaming found */
120            int sample_rate_code;
121
122            for (i=0; i<s->nb_streams; i++) {
123                st = s->streams[i];
124                if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
125                    goto skip;
126            }
127
128            avio_r8(pb);
129            v = avio_r8(pb);
130            swf->samples_per_frame = avio_rl16(pb);
131            ast = avformat_new_stream(s, NULL);
132            if (!ast)
133                return -1;
134            ast->id = -1; /* -1 to avoid clash with video stream ch_id */
135            ast->codec->channels = 1 + (v&1);
136            ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
137            ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
138            ast->need_parsing = AVSTREAM_PARSE_FULL;
139            sample_rate_code= (v>>2) & 3;
140            if (!sample_rate_code)
141                ast->codec->sample_rate = 5512;
142            else
143                ast->codec->sample_rate = 11025 << (sample_rate_code-1);
144            avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
145            len -= 4;
146        } else if (tag == TAG_VIDEOFRAME) {
147            int ch_id = avio_rl16(pb);
148            len -= 2;
149            for(i=0; i<s->nb_streams; i++) {
150                st = s->streams[i];
151                if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
152                    frame = avio_rl16(pb);
153                    if ((res = av_get_packet(pb, pkt, len-2)) < 0)
154                        return res;
155                    pkt->pos = pos;
156                    pkt->pts = frame;
157                    pkt->stream_index = st->index;
158                    return pkt->size;
159                }
160            }
161        } else if (tag == TAG_STREAMBLOCK) {
162            for (i = 0; i < s->nb_streams; i++) {
163                st = s->streams[i];
164                if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
165            if (st->codec->codec_id == CODEC_ID_MP3) {
166                avio_skip(pb, 4);
167                if ((res = av_get_packet(pb, pkt, len-4)) < 0)
168                    return res;
169            } else { // ADPCM, PCM
170                if ((res = av_get_packet(pb, pkt, len)) < 0)
171                    return res;
172            }
173            pkt->pos = pos;
174            pkt->stream_index = st->index;
175            return pkt->size;
176                }
177            }
178        } else if (tag == TAG_JPEG2) {
179            for (i=0; i<s->nb_streams; i++) {
180                st = s->streams[i];
181                if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
182                    break;
183            }
184            if (i == s->nb_streams) {
185                vst = avformat_new_stream(s, NULL);
186                if (!vst)
187                    return -1;
188                vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
189                vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
190                vst->codec->codec_id = CODEC_ID_MJPEG;
191                avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
192                vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
193                st = vst;
194            }
195            avio_rl16(pb); /* BITMAP_ID */
196            if ((res = av_new_packet(pkt, len-2)) < 0)
197                return res;
198            avio_read(pb, pkt->data, 4);
199            if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
200                AV_RB32(pkt->data) == 0xffd9ffd8) {
201                /* old SWF files containing SOI/EOI as data start */
202                /* files created by swink have reversed tag */
203                pkt->size -= 4;
204                avio_read(pb, pkt->data, pkt->size);
205            } else {
206                avio_read(pb, pkt->data + 4, pkt->size - 4);
207            }
208            pkt->pos = pos;
209            pkt->stream_index = st->index;
210            return pkt->size;
211        }
212    skip:
213        avio_skip(pb, len);
214    }
215}
216
217AVInputFormat ff_swf_demuxer = {
218    .name           = "swf",
219    .long_name      = NULL_IF_CONFIG_SMALL("Flash format"),
220    .priv_data_size = sizeof(SWFContext),
221    .read_probe     = swf_probe,
222    .read_header    = swf_read_header,
223    .read_packet    = swf_read_packet,
224};
225