Statistics
| Branch: | Revision:

ffmpeg / libavformat / swfdec.c @ e356fc57

History | View | Annotate | Download (7.16 KB)

1 3b35f4ab Baptiste Coudurier
/*
2 376aefdd Baptiste Coudurier
 * Flash Compatible Streaming Format demuxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2000 Fabrice Bellard
4
 * Copyright (c) 2003 Tinic Uro
5 3b35f4ab Baptiste Coudurier
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
24 3b35f4ab Baptiste Coudurier
#include "swf.h"
25
26 ae628ec1 Anton Khirnov
static int get_swf_tag(AVIOContext *pb, int *len_ptr)
27 3b35f4ab Baptiste Coudurier
{
28
    int tag, len;
29
30
    if (url_feof(pb))
31
        return -1;
32
33 b7effd4e Anton Khirnov
    tag = avio_rl16(pb);
34 3b35f4ab Baptiste Coudurier
    len = tag & 0x3f;
35
    tag = tag >> 6;
36
    if (len == 0x3f) {
37 b7effd4e Anton Khirnov
        len = avio_rl32(pb);
38 3b35f4ab Baptiste Coudurier
    }
39
//    av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
40
    *len_ptr = len;
41
    return tag;
42
}
43
44
45
static 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
55
static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
56
{
57
    SWFContext *swf = s->priv_data;
58 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
59 3b35f4ab Baptiste Coudurier
    int nbits, len, tag;
60
61 b7effd4e Anton Khirnov
    tag = avio_rb32(pb) & 0xffffff00;
62 3b35f4ab Baptiste Coudurier
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 b7effd4e Anton Khirnov
    avio_rl32(pb);
70 3b35f4ab Baptiste Coudurier
    /* skip rectangle size */
71 b7effd4e Anton Khirnov
    nbits = avio_r8(pb) >> 3;
72 3b35f4ab Baptiste Coudurier
    len = (4 * nbits - 3 + 7) / 8;
73 e356fc57 Anton Khirnov
    avio_seek(pb, len, SEEK_CUR);
74 b7effd4e Anton Khirnov
    swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
75
    avio_rl16(pb); /* frame count */
76 3b35f4ab Baptiste Coudurier
77
    swf->samples_per_frame = 0;
78
    s->ctx_flags |= AVFMTCTX_NOHEADER;
79
    return 0;
80
}
81
82
static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
83
{
84
    SWFContext *swf = s->priv_data;
85 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
86 3b35f4ab Baptiste Coudurier
    AVStream *vst = NULL, *ast = NULL, *st = 0;
87
    int tag, len, i, frame, v;
88
89
    for(;;) {
90 7e04cfba Reimar Döffinger
        uint64_t pos = url_ftell(pb);
91 3b35f4ab Baptiste Coudurier
        tag = get_swf_tag(pb, &len);
92
        if (tag < 0)
93
            return AVERROR(EIO);
94 4e35ffa9 Baptiste Coudurier
        if (tag == TAG_VIDEOSTREAM) {
95 b7effd4e Anton Khirnov
            int ch_id = avio_rl16(pb);
96 4e35ffa9 Baptiste Coudurier
            len -= 2;
97
98
            for (i=0; i<s->nb_streams; i++) {
99
                st = s->streams[i];
100 72415b2a Stefano Sabatini
                if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
101 4e35ffa9 Baptiste Coudurier
                    goto skip;
102
            }
103
104 b7effd4e Anton Khirnov
            avio_rl16(pb);
105
            avio_rl16(pb);
106
            avio_rl16(pb);
107
            avio_r8(pb);
108 3b35f4ab Baptiste Coudurier
            /* Check for FLV1 */
109
            vst = av_new_stream(s, ch_id);
110
            if (!vst)
111
                return -1;
112 72415b2a Stefano Sabatini
            vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
113 b7effd4e Anton Khirnov
            vst->codec->codec_id = ff_codec_get_id(swf_codec_tags, avio_r8(pb));
114 36a12218 Baptiste Coudurier
            av_set_pts_info(vst, 16, 256, swf->frame_rate);
115 3b35f4ab Baptiste Coudurier
            vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
116 4e35ffa9 Baptiste Coudurier
            len -= 8;
117
        } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
118 3b35f4ab Baptiste Coudurier
            /* streaming found */
119
            int sample_rate_code;
120 4e35ffa9 Baptiste Coudurier
121
            for (i=0; i<s->nb_streams; i++) {
122
                st = s->streams[i];
123 72415b2a Stefano Sabatini
                if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
124 4e35ffa9 Baptiste Coudurier
                    goto skip;
125
            }
126
127 b7effd4e Anton Khirnov
            avio_r8(pb);
128
            v = avio_r8(pb);
129
            swf->samples_per_frame = avio_rl16(pb);
130 3b35f4ab Baptiste Coudurier
            ast = av_new_stream(s, -1); /* -1 to avoid clash with video stream ch_id */
131
            if (!ast)
132
                return -1;
133
            ast->codec->channels = 1 + (v&1);
134 72415b2a Stefano Sabatini
            ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
135 1a40491e Daniel Verkamp
            ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
136 3b35f4ab Baptiste Coudurier
            ast->need_parsing = AVSTREAM_PARSE_FULL;
137
            sample_rate_code= (v>>2) & 3;
138
            if (!sample_rate_code)
139
                return AVERROR(EIO);
140
            ast->codec->sample_rate = 11025 << (sample_rate_code-1);
141
            av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
142
            len -= 4;
143
        } else if (tag == TAG_VIDEOFRAME) {
144 b7effd4e Anton Khirnov
            int ch_id = avio_rl16(pb);
145 3b35f4ab Baptiste Coudurier
            len -= 2;
146
            for(i=0; i<s->nb_streams; i++) {
147
                st = s->streams[i];
148 72415b2a Stefano Sabatini
                if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
149 b7effd4e Anton Khirnov
                    frame = avio_rl16(pb);
150 3b35f4ab Baptiste Coudurier
                    av_get_packet(pb, pkt, len-2);
151 7e04cfba Reimar Döffinger
                    pkt->pos = pos;
152 3b35f4ab Baptiste Coudurier
                    pkt->pts = frame;
153
                    pkt->stream_index = st->index;
154
                    return pkt->size;
155
                }
156
            }
157
        } else if (tag == TAG_STREAMBLOCK) {
158 4e35ffa9 Baptiste Coudurier
            for (i = 0; i < s->nb_streams; i++) {
159
                st = s->streams[i];
160 72415b2a Stefano Sabatini
                if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
161 3b35f4ab Baptiste Coudurier
            if (st->codec->codec_id == CODEC_ID_MP3) {
162 e356fc57 Anton Khirnov
                avio_seek(pb, 4, SEEK_CUR);
163 3b35f4ab Baptiste Coudurier
                av_get_packet(pb, pkt, len-4);
164
            } else { // ADPCM, PCM
165
                av_get_packet(pb, pkt, len);
166
            }
167 7e04cfba Reimar Döffinger
            pkt->pos = pos;
168 3b35f4ab Baptiste Coudurier
            pkt->stream_index = st->index;
169
            return pkt->size;
170 4e35ffa9 Baptiste Coudurier
                }
171
            }
172 3b35f4ab Baptiste Coudurier
        } else if (tag == TAG_JPEG2) {
173
            for (i=0; i<s->nb_streams; i++) {
174
                st = s->streams[i];
175 4e35ffa9 Baptiste Coudurier
                if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
176 3b35f4ab Baptiste Coudurier
                    break;
177
            }
178
            if (i == s->nb_streams) {
179
                vst = av_new_stream(s, -2); /* -2 to avoid clash with video stream and audio stream */
180
                if (!vst)
181
                    return -1;
182 72415b2a Stefano Sabatini
                vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
183 3b35f4ab Baptiste Coudurier
                vst->codec->codec_id = CODEC_ID_MJPEG;
184
                av_set_pts_info(vst, 64, 256, swf->frame_rate);
185
                vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
186
                st = vst;
187
            }
188 b7effd4e Anton Khirnov
            avio_rl16(pb); /* BITMAP_ID */
189 3b35f4ab Baptiste Coudurier
            av_new_packet(pkt, len-2);
190 b7effd4e Anton Khirnov
            avio_read(pb, pkt->data, 4);
191 3b35f4ab Baptiste Coudurier
            if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
192
                AV_RB32(pkt->data) == 0xffd9ffd8) {
193
                /* old SWF files containing SOI/EOI as data start */
194
                /* files created by swink have reversed tag */
195
                pkt->size -= 4;
196 b7effd4e Anton Khirnov
                avio_read(pb, pkt->data, pkt->size);
197 3b35f4ab Baptiste Coudurier
            } else {
198 b7effd4e Anton Khirnov
                avio_read(pb, pkt->data + 4, pkt->size - 4);
199 3b35f4ab Baptiste Coudurier
            }
200 7e04cfba Reimar Döffinger
            pkt->pos = pos;
201 3b35f4ab Baptiste Coudurier
            pkt->stream_index = st->index;
202
            return pkt->size;
203
        }
204 4e35ffa9 Baptiste Coudurier
    skip:
205 e356fc57 Anton Khirnov
        avio_seek(pb, len, SEEK_CUR);
206 3b35f4ab Baptiste Coudurier
    }
207
    return 0;
208
}
209
210 c6610a21 Diego Elio Pettenò
AVInputFormat ff_swf_demuxer = {
211 3b35f4ab Baptiste Coudurier
    "swf",
212 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Flash format"),
213 3b35f4ab Baptiste Coudurier
    sizeof(SWFContext),
214
    swf_probe,
215
    swf_read_header,
216
    swf_read_packet,
217
};