2 * Flash Compatible Streaming Format demuxer
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2003 Tinic Uro
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/avassert.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/intreadwrite.h"
28 static const AVCodecTag swf_audio_codec_tags[] = {
29 { AV_CODEC_ID_PCM_S16LE, 0x00 },
30 { AV_CODEC_ID_ADPCM_SWF, 0x01 },
31 { AV_CODEC_ID_MP3, 0x02 },
32 { AV_CODEC_ID_PCM_S16LE, 0x03 },
33 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
34 { AV_CODEC_ID_NONE, 0 },
37 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
55 static int swf_probe(AVProbeData *p)
57 /* check file header */
58 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
60 return AVPROBE_SCORE_MAX;
66 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
68 AVFormatContext *s = opaque;
69 SWFContext *swf = s->priv_data;
70 z_stream *z = &swf->zstream;
75 int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
78 z->next_in = swf->zbuf_in;
83 z->avail_out = buf_size;
85 ret = inflate(z, Z_NO_FLUSH);
87 return AVERROR(EINVAL);
88 if (ret == Z_STREAM_END)
91 if (buf_size - z->avail_out == 0)
94 return buf_size - z->avail_out;
98 static int swf_read_header(AVFormatContext *s)
100 SWFContext *swf = s->priv_data;
101 AVIOContext *pb = s->pb;
104 tag = avio_rb32(pb) & 0xffffff00;
107 if (tag == MKBETAG('C', 'W', 'S', 0)) {
108 av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
110 swf->zbuf_in = av_malloc(ZBUF_SIZE);
111 swf->zbuf_out = av_malloc(ZBUF_SIZE);
112 swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
113 zlib_refill, NULL, NULL);
114 if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
115 return AVERROR(ENOMEM);
116 swf->zpb->seekable = 0;
117 if (inflateInit(&swf->zstream) != Z_OK) {
118 av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
119 return AVERROR(EINVAL);
123 av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
126 } else if (tag != MKBETAG('F', 'W', 'S', 0))
128 /* skip rectangle size */
129 nbits = avio_r8(pb) >> 3;
130 len = (4 * nbits - 3 + 7) / 8;
132 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
133 avio_rl16(pb); /* frame count */
135 swf->samples_per_frame = 0;
136 s->ctx_flags |= AVFMTCTX_NOHEADER;
140 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
142 SWFContext *swf = s->priv_data;
143 AVIOContext *pb = s->pb;
144 AVStream *vst = NULL, *ast = NULL, *st = 0;
145 int tag, len, i, frame, v, res;
153 uint64_t pos = avio_tell(pb);
154 tag = get_swf_tag(pb, &len);
157 if (tag == TAG_VIDEOSTREAM) {
158 int ch_id = avio_rl16(pb);
161 for (i=0; i<s->nb_streams; i++) {
163 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
172 vst = avformat_new_stream(s, NULL);
174 return AVERROR(ENOMEM);
176 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
177 vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
178 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
180 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
181 /* streaming found */
182 int sample_rate_code;
184 for (i=0; i<s->nb_streams; i++) {
186 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
192 swf->samples_per_frame = avio_rl16(pb);
193 ast = avformat_new_stream(s, NULL);
195 return AVERROR(ENOMEM);
196 ast->id = -1; /* -1 to avoid clash with video stream ch_id */
197 ast->codec->channels = 1 + (v&1);
198 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
199 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
200 ast->need_parsing = AVSTREAM_PARSE_FULL;
201 sample_rate_code= (v>>2) & 3;
202 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
203 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
205 } else if (tag == TAG_DEFINESOUND) {
207 int sample_rate_code;
208 int ch_id = avio_rl16(pb);
210 for (i=0; i<s->nb_streams; i++) {
212 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == ch_id)
216 // FIXME: 8-bit uncompressed PCM audio will be interpreted as 16-bit
217 // FIXME: The entire audio stream is stored in a single chunk/tag. Normally,
218 // these are smaller audio streams in DEFINESOUND tags, but it's technically
219 // possible they could be huge. Break it up into multiple packets if it's big.
221 ast = avformat_new_stream(s, NULL);
223 return AVERROR(ENOMEM);
225 ast->codec->channels = 1 + (v&1);
226 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
227 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
228 ast->need_parsing = AVSTREAM_PARSE_FULL;
229 sample_rate_code= (v>>2) & 3;
230 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
231 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
232 ast->duration = avio_rl32(pb); // number of samples
233 if (((v>>4) & 15) == 2) { // MP3 sound data record
234 ast->skip_samples = avio_rl16(pb);
238 if ((res = av_get_packet(pb, pkt, len)) < 0)
241 pkt->stream_index = ast->index;
243 } else if (tag == TAG_VIDEOFRAME) {
244 int ch_id = avio_rl16(pb);
246 for(i=0; i<s->nb_streams; i++) {
248 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
249 frame = avio_rl16(pb);
250 if ((res = av_get_packet(pb, pkt, len-2)) < 0)
254 pkt->stream_index = st->index;
258 } else if (tag == TAG_DEFINEBITSLOSSLESS || tag == TAG_DEFINEBITSLOSSLESS2) {
261 uint8_t *buf = NULL, *zbuf = NULL, *pal;
262 uint32_t colormap[AVPALETTE_COUNT] = {0};
263 const int alpha_bmp = tag == TAG_DEFINEBITSLOSSLESS2;
264 const int colormapbpp = 3 + alpha_bmp;
265 int linesize, colormapsize = 0;
267 const int ch_id = avio_rl16(pb);
268 const int bmp_fmt = avio_r8(pb);
269 const int width = avio_rl16(pb);
270 const int height = avio_rl16(pb);
277 colormapsize = avio_r8(pb) + 1;
281 linesize = width * 2;
283 case 5: // RGB24 (0RGB)
284 linesize = width * 4;
287 av_log(s, AV_LOG_ERROR, "invalid bitmap format %d, skipped\n", bmp_fmt);
288 goto bitmap_end_skip;
291 linesize = FFALIGN(linesize, 4);
293 if (av_image_check_size(width, height, 0, s) < 0 ||
294 linesize >= INT_MAX / height ||
295 linesize * height >= INT_MAX - colormapsize * colormapbpp) {
296 av_log(s, AV_LOG_ERROR, "invalid frame size %dx%d\n", width, height);
297 goto bitmap_end_skip;
300 out_len = colormapsize * colormapbpp + linesize * height;
302 av_dlog(s, "bitmap: ch=%d fmt=%d %dx%d (linesize=%d) len=%d->%ld pal=%d\n",
303 ch_id, bmp_fmt, width, height, linesize, len, out_len, colormapsize);
305 zbuf = av_malloc(len);
306 buf = av_malloc(out_len);
308 res = AVERROR(ENOMEM);
312 len = avio_read(pb, zbuf, len);
313 if (len < 0 || (res = uncompress(buf, &out_len, zbuf, len)) != Z_OK) {
314 av_log(s, AV_LOG_WARNING, "Failed to uncompress one bitmap\n");
315 goto bitmap_end_skip;
318 for (i = 0; i < s->nb_streams; i++) {
320 if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && st->id == -3)
323 if (i == s->nb_streams) {
324 vst = avformat_new_stream(s, NULL);
326 res = AVERROR(ENOMEM);
329 vst->id = -3; /* -3 to avoid clash with video stream and audio stream */
330 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
331 vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
332 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
335 st->codec->width = width;
336 st->codec->height = height;
338 if ((res = av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0)
341 pkt->stream_index = st->index;
345 st->codec->pix_fmt = AV_PIX_FMT_PAL8;
346 for (i = 0; i < colormapsize; i++)
347 if (alpha_bmp) colormap[i] = buf[3]<<24 | AV_RB24(buf + 4*i);
348 else colormap[i] = 0xffU <<24 | AV_RB24(buf + 3*i);
349 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
351 res = AVERROR(ENOMEM);
354 memcpy(pal, colormap, AVPALETTE_SIZE);
357 st->codec->pix_fmt = AV_PIX_FMT_RGB555;
360 st->codec->pix_fmt = alpha_bmp ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
365 memcpy(pkt->data, buf + colormapsize*colormapbpp, linesize * height);
377 av_log(s, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
379 } else if (tag == TAG_STREAMBLOCK) {
380 for (i = 0; i < s->nb_streams; i++) {
382 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
383 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
385 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
387 } else { // ADPCM, PCM
388 if ((res = av_get_packet(pb, pkt, len)) < 0)
392 pkt->stream_index = st->index;
396 } else if (tag == TAG_JPEG2) {
397 for (i=0; i<s->nb_streams; i++) {
399 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
402 if (i == s->nb_streams) {
403 vst = avformat_new_stream(s, NULL);
405 return AVERROR(ENOMEM);
406 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
407 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
408 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
409 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
412 avio_rl16(pb); /* BITMAP_ID */
413 if ((res = av_new_packet(pkt, len-2)) < 0)
415 avio_read(pb, pkt->data, 4);
416 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
417 AV_RB32(pkt->data) == 0xffd9ffd8) {
418 /* old SWF files containing SOI/EOI as data start */
419 /* files created by swink have reversed tag */
421 avio_read(pb, pkt->data, pkt->size);
423 avio_read(pb, pkt->data + 4, pkt->size - 4);
426 pkt->stream_index = st->index;
435 static av_cold int swf_read_close(AVFormatContext *avctx)
437 SWFContext *s = avctx->priv_data;
438 inflateEnd(&s->zstream);
439 av_freep(&s->zbuf_in);
440 av_freep(&s->zbuf_out);
446 AVInputFormat ff_swf_demuxer = {
448 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
449 .priv_data_size = sizeof(SWFContext),
450 .read_probe = swf_probe,
451 .read_header = swf_read_header,
452 .read_packet = swf_read_packet,
454 .read_close = swf_read_close,