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/channel_layout.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/intreadwrite.h"
29 static const AVCodecTag swf_audio_codec_tags[] = {
30 { AV_CODEC_ID_PCM_S16LE, 0x00 },
31 { AV_CODEC_ID_ADPCM_SWF, 0x01 },
32 { AV_CODEC_ID_MP3, 0x02 },
33 { AV_CODEC_ID_PCM_S16LE, 0x03 },
34 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
35 { AV_CODEC_ID_NONE, 0 },
38 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
56 static int swf_probe(AVProbeData *p)
61 /* check file header */
62 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
63 p->buf[2] == 'S' && p->buf[3] < 20)
64 return AVPROBE_SCORE_MAX;
70 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
72 AVFormatContext *s = opaque;
73 SWFContext *swf = s->priv_data;
74 z_stream *z = &swf->zstream;
79 int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
82 z->next_in = swf->zbuf_in;
87 z->avail_out = buf_size;
89 ret = inflate(z, Z_NO_FLUSH);
91 return AVERROR(EINVAL);
92 if (ret == Z_STREAM_END)
95 if (buf_size - z->avail_out == 0)
98 return buf_size - z->avail_out;
102 static int swf_read_header(AVFormatContext *s)
104 SWFContext *swf = s->priv_data;
105 AVIOContext *pb = s->pb;
108 tag = avio_rb32(pb) & 0xffffff00;
111 if (tag == MKBETAG('C', 'W', 'S', 0)) {
112 av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
114 swf->zbuf_in = av_malloc(ZBUF_SIZE);
115 swf->zbuf_out = av_malloc(ZBUF_SIZE);
116 swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
117 zlib_refill, NULL, NULL);
118 if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
119 return AVERROR(ENOMEM);
120 swf->zpb->seekable = 0;
121 if (inflateInit(&swf->zstream) != Z_OK) {
122 av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
123 return AVERROR(EINVAL);
127 av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
130 } else if (tag != MKBETAG('F', 'W', 'S', 0))
132 /* skip rectangle size */
133 nbits = avio_r8(pb) >> 3;
134 len = (4 * nbits - 3 + 7) / 8;
136 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
137 avio_rl16(pb); /* frame count */
139 swf->samples_per_frame = 0;
140 s->ctx_flags |= AVFMTCTX_NOHEADER;
144 static AVStream *create_new_audio_stream(AVFormatContext *s, int id, int info)
146 int sample_rate_code, sample_size_code;
147 AVStream *ast = avformat_new_stream(s, NULL);
152 ast->codec->channels = 2;
153 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
155 ast->codec->channels = 1;
156 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
158 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
159 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, info>>4 & 15);
160 ast->need_parsing = AVSTREAM_PARSE_FULL;
161 sample_rate_code = info>>2 & 3;
162 sample_size_code = info>>1 & 1;
163 if (!sample_size_code && ast->codec->codec_id == AV_CODEC_ID_PCM_S16LE)
164 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
165 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
166 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
170 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
172 SWFContext *swf = s->priv_data;
173 AVIOContext *pb = s->pb;
174 AVStream *vst = NULL, *ast = NULL, *st = 0;
175 int tag, len, i, frame, v, res;
183 uint64_t pos = avio_tell(pb);
184 tag = get_swf_tag(pb, &len);
188 av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
189 return AVERROR_INVALIDDATA;
191 if (tag == TAG_VIDEOSTREAM) {
192 int ch_id = avio_rl16(pb);
195 for (i=0; i<s->nb_streams; i++) {
197 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
206 vst = avformat_new_stream(s, NULL);
208 return AVERROR(ENOMEM);
210 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
211 vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
212 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
214 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
215 /* streaming found */
217 for (i=0; i<s->nb_streams; i++) {
219 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
225 swf->samples_per_frame = avio_rl16(pb);
226 ast = create_new_audio_stream(s, -1, v); /* -1 to avoid clash with video stream ch_id */
228 return AVERROR(ENOMEM);
230 } else if (tag == TAG_DEFINESOUND) {
232 int ch_id = avio_rl16(pb);
234 for (i=0; i<s->nb_streams; i++) {
236 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == ch_id)
240 // FIXME: The entire audio stream is stored in a single chunk/tag. Normally,
241 // these are smaller audio streams in DEFINESOUND tags, but it's technically
242 // possible they could be huge. Break it up into multiple packets if it's big.
244 ast = create_new_audio_stream(s, ch_id, v);
246 return AVERROR(ENOMEM);
247 ast->duration = avio_rl32(pb); // number of samples
248 if (((v>>4) & 15) == 2) { // MP3 sound data record
249 ast->skip_samples = avio_rl16(pb);
253 if ((res = av_get_packet(pb, pkt, len)) < 0)
256 pkt->stream_index = ast->index;
258 } else if (tag == TAG_VIDEOFRAME) {
259 int ch_id = avio_rl16(pb);
261 for(i=0; i<s->nb_streams; i++) {
263 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
264 frame = avio_rl16(pb);
268 if ((res = av_get_packet(pb, pkt, len)) < 0)
272 pkt->stream_index = st->index;
276 } else if (tag == TAG_DEFINEBITSLOSSLESS || tag == TAG_DEFINEBITSLOSSLESS2) {
279 uint8_t *buf = NULL, *zbuf = NULL, *pal;
280 uint32_t colormap[AVPALETTE_COUNT] = {0};
281 const int alpha_bmp = tag == TAG_DEFINEBITSLOSSLESS2;
282 const int colormapbpp = 3 + alpha_bmp;
283 int linesize, colormapsize = 0;
285 const int ch_id = avio_rl16(pb);
286 const int bmp_fmt = avio_r8(pb);
287 const int width = avio_rl16(pb);
288 const int height = avio_rl16(pb);
295 colormapsize = avio_r8(pb) + 1;
299 linesize = width * 2;
301 case 5: // RGB24 (0RGB)
302 linesize = width * 4;
305 av_log(s, AV_LOG_ERROR, "invalid bitmap format %d, skipped\n", bmp_fmt);
306 goto bitmap_end_skip;
309 linesize = FFALIGN(linesize, 4);
311 if (av_image_check_size(width, height, 0, s) < 0 ||
312 linesize >= INT_MAX / height ||
313 linesize * height >= INT_MAX - colormapsize * colormapbpp) {
314 av_log(s, AV_LOG_ERROR, "invalid frame size %dx%d\n", width, height);
315 goto bitmap_end_skip;
318 out_len = colormapsize * colormapbpp + linesize * height;
320 av_dlog(s, "bitmap: ch=%d fmt=%d %dx%d (linesize=%d) len=%d->%ld pal=%d\n",
321 ch_id, bmp_fmt, width, height, linesize, len, out_len, colormapsize);
323 zbuf = av_malloc(len);
324 buf = av_malloc(out_len);
326 res = AVERROR(ENOMEM);
330 len = avio_read(pb, zbuf, len);
331 if (len < 0 || (res = uncompress(buf, &out_len, zbuf, len)) != Z_OK) {
332 av_log(s, AV_LOG_WARNING, "Failed to uncompress one bitmap\n");
333 goto bitmap_end_skip;
336 for (i = 0; i < s->nb_streams; i++) {
338 if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && st->id == -3)
341 if (i == s->nb_streams) {
342 vst = avformat_new_stream(s, NULL);
344 res = AVERROR(ENOMEM);
347 vst->id = -3; /* -3 to avoid clash with video stream and audio stream */
348 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
349 vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
350 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
353 st->codec->width = width;
354 st->codec->height = height;
356 if ((res = av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0)
359 pkt->stream_index = st->index;
363 st->codec->pix_fmt = AV_PIX_FMT_PAL8;
364 for (i = 0; i < colormapsize; i++)
365 if (alpha_bmp) colormap[i] = buf[3]<<24 | AV_RB24(buf + 4*i);
366 else colormap[i] = 0xffU <<24 | AV_RB24(buf + 3*i);
367 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
369 res = AVERROR(ENOMEM);
372 memcpy(pal, colormap, AVPALETTE_SIZE);
375 st->codec->pix_fmt = AV_PIX_FMT_RGB555;
378 st->codec->pix_fmt = alpha_bmp ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
384 if (linesize * height > pkt->size) {
385 res = AVERROR_INVALIDDATA;
388 memcpy(pkt->data, buf + colormapsize*colormapbpp, linesize * height);
400 av_log(s, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
402 } else if (tag == TAG_STREAMBLOCK) {
403 for (i = 0; i < s->nb_streams; i++) {
405 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
406 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
411 if ((res = av_get_packet(pb, pkt, len)) < 0)
413 } else { // ADPCM, PCM
416 if ((res = av_get_packet(pb, pkt, len)) < 0)
420 pkt->stream_index = st->index;
424 } else if (tag == TAG_JPEG2) {
425 for (i=0; i<s->nb_streams; i++) {
427 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
430 if (i == s->nb_streams) {
431 vst = avformat_new_stream(s, NULL);
433 return AVERROR(ENOMEM);
434 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
435 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
436 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
437 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
440 avio_rl16(pb); /* BITMAP_ID */
444 if ((res = av_new_packet(pkt, len)) < 0)
446 avio_read(pb, pkt->data, 4);
447 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
448 AV_RB32(pkt->data) == 0xffd9ffd8) {
449 /* old SWF files containing SOI/EOI as data start */
450 /* files created by swink have reversed tag */
452 avio_read(pb, pkt->data, pkt->size);
454 avio_read(pb, pkt->data + 4, pkt->size - 4);
457 pkt->stream_index = st->index;
460 av_log(s, AV_LOG_DEBUG, "Unknown tag: %d\n", tag);
464 av_log(s, AV_LOG_WARNING, "Cliping len %d\n", len);
471 static av_cold int swf_read_close(AVFormatContext *avctx)
473 SWFContext *s = avctx->priv_data;
474 inflateEnd(&s->zstream);
475 av_freep(&s->zbuf_in);
476 av_freep(&s->zbuf_out);
482 AVInputFormat ff_swf_demuxer = {
484 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
485 .priv_data_size = sizeof(SWFContext),
486 .read_probe = swf_probe,
487 .read_header = swf_read_header,
488 .read_packet = swf_read_packet,
490 .read_close = swf_read_close,