]> git.sesse.net Git - ffmpeg/blob - libavformat/swfdec.c
Merge commit '292d1e78743855404c7d07e3e7cb3f9c9ae6275b'
[ffmpeg] / libavformat / swfdec.c
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 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 #include "libavutil/avassert.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/intreadwrite.h"
26 #include "swf.h"
27
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 },
35 };
36
37 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
38 {
39     int tag, len;
40
41     if (url_feof(pb))
42         return AVERROR_EOF;
43
44     tag = avio_rl16(pb);
45     len = tag & 0x3f;
46     tag = tag >> 6;
47     if (len == 0x3f) {
48         len = avio_rl32(pb);
49     }
50     *len_ptr = len;
51     return tag;
52 }
53
54
55 static int swf_probe(AVProbeData *p)
56 {
57     /* check file header */
58     if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
59         p->buf[2] == 'S')
60         return AVPROBE_SCORE_MAX;
61     else
62         return 0;
63 }
64
65 #if CONFIG_ZLIB
66 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
67 {
68     AVFormatContext *s = opaque;
69     SWFContext *swf = s->priv_data;
70     z_stream *z = &swf->zstream;
71     int ret;
72
73 retry:
74     if (!z->avail_in) {
75         int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
76         if (n < 0)
77             return n;
78         z->next_in  = swf->zbuf_in;
79         z->avail_in = n;
80     }
81
82     z->next_out  = buf;
83     z->avail_out = buf_size;
84
85     ret = inflate(z, Z_NO_FLUSH);
86     if (ret < 0)
87         return AVERROR(EINVAL);
88     if (ret == Z_STREAM_END)
89         return AVERROR_EOF;
90
91     if (buf_size - z->avail_out == 0)
92         goto retry;
93
94     return buf_size - z->avail_out;
95 }
96 #endif
97
98 static int swf_read_header(AVFormatContext *s)
99 {
100     SWFContext *swf = s->priv_data;
101     AVIOContext *pb = s->pb;
102     int nbits, len, tag;
103
104     tag = avio_rb32(pb) & 0xffffff00;
105     avio_rl32(pb);
106
107     if (tag == MKBETAG('C', 'W', 'S', 0)) {
108         av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
109 #if CONFIG_ZLIB
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);
120         }
121         pb = swf->zpb;
122 #else
123         av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
124         return AVERROR(EIO);
125 #endif
126     } else if (tag != MKBETAG('F', 'W', 'S', 0))
127         return AVERROR(EIO);
128     /* skip rectangle size */
129     nbits = avio_r8(pb) >> 3;
130     len = (4 * nbits - 3 + 7) / 8;
131     avio_skip(pb, len);
132     swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
133     avio_rl16(pb); /* frame count */
134
135     swf->samples_per_frame = 0;
136     s->ctx_flags |= AVFMTCTX_NOHEADER;
137     return 0;
138 }
139
140 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
141 {
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;
146
147 #if CONFIG_ZLIB
148     if (swf->zpb)
149         pb = swf->zpb;
150 #endif
151
152     for(;;) {
153         uint64_t pos = avio_tell(pb);
154         tag = get_swf_tag(pb, &len);
155         if (tag < 0)
156             return tag;
157         if (tag == TAG_VIDEOSTREAM) {
158             int ch_id = avio_rl16(pb);
159             len -= 2;
160
161             for (i=0; i<s->nb_streams; i++) {
162                 st = s->streams[i];
163                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
164                     goto skip;
165             }
166
167             avio_rl16(pb);
168             avio_rl16(pb);
169             avio_rl16(pb);
170             avio_r8(pb);
171             /* Check for FLV1 */
172             vst = avformat_new_stream(s, NULL);
173             if (!vst)
174                 return AVERROR(ENOMEM);
175             vst->id = ch_id;
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);
179             len -= 8;
180         } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
181             /* streaming found */
182             int sample_rate_code;
183
184             for (i=0; i<s->nb_streams; i++) {
185                 st = s->streams[i];
186                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
187                     goto skip;
188             }
189
190             avio_r8(pb);
191             v = avio_r8(pb);
192             swf->samples_per_frame = avio_rl16(pb);
193             ast = avformat_new_stream(s, NULL);
194             if (!ast)
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);
204             len -= 4;
205         } else if (tag == TAG_DEFINESOUND) {
206             /* audio stream */
207             int sample_rate_code;
208             int ch_id = avio_rl16(pb);
209
210             for (i=0; i<s->nb_streams; i++) {
211                 st = s->streams[i];
212                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == ch_id)
213                     goto skip;
214             }
215
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.
220             v = avio_r8(pb);
221             ast = avformat_new_stream(s, NULL);
222             if (!ast)
223                 return AVERROR(ENOMEM);
224             ast->id = ch_id;
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);
235                 len -= 2;
236             }
237             len -= 7;
238             if ((res = av_get_packet(pb, pkt, len)) < 0)
239                 return res;
240             pkt->pos = pos;
241             pkt->stream_index = ast->index;
242             return pkt->size;
243         } else if (tag == TAG_VIDEOFRAME) {
244             int ch_id = avio_rl16(pb);
245             len -= 2;
246             for(i=0; i<s->nb_streams; i++) {
247                 st = s->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)
251                         return res;
252                     pkt->pos = pos;
253                     pkt->pts = frame;
254                     pkt->stream_index = st->index;
255                     return pkt->size;
256                 }
257             }
258         } else if (tag == TAG_DEFINEBITSLOSSLESS || tag == TAG_DEFINEBITSLOSSLESS2) {
259 #if CONFIG_ZLIB
260             long out_len;
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;
266
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);
271
272             len -= 2+1+2+2;
273
274             switch (bmp_fmt) {
275             case 3: // PAL-8
276                 linesize = width;
277                 colormapsize = avio_r8(pb) + 1;
278                 len--;
279                 break;
280             case 4: // RGB15
281                 linesize = width * 2;
282                 break;
283             case 5: // RGB24 (0RGB)
284                 linesize = width * 4;
285                 break;
286             default:
287                 av_log(s, AV_LOG_ERROR, "invalid bitmap format %d, skipped\n", bmp_fmt);
288                 goto bitmap_end_skip;
289             }
290
291             linesize = FFALIGN(linesize, 4);
292
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;
298             }
299
300             out_len = colormapsize * colormapbpp + linesize * height;
301
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);
304
305             zbuf = av_malloc(len);
306             buf  = av_malloc(out_len);
307             if (!zbuf || !buf) {
308                 res = AVERROR(ENOMEM);
309                 goto bitmap_end;
310             }
311
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;
316             }
317
318             for (i = 0; i < s->nb_streams; i++) {
319                 st = s->streams[i];
320                 if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && st->id == -3)
321                     break;
322             }
323             if (i == s->nb_streams) {
324                 vst = avformat_new_stream(s, NULL);
325                 if (!vst) {
326                     res = AVERROR(ENOMEM);
327                     goto bitmap_end;
328                 }
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);
333                 st = vst;
334             }
335             st->codec->width  = width;
336             st->codec->height = height;
337
338             if ((res = av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0)
339                 goto bitmap_end;
340             pkt->pos = pos;
341             pkt->stream_index = st->index;
342
343             switch (bmp_fmt) {
344             case 3:
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);
350                 if (!pal) {
351                     res = AVERROR(ENOMEM);
352                     goto bitmap_end;
353                 }
354                 memcpy(pal, colormap, AVPALETTE_SIZE);
355                 break;
356             case 4:
357                 st->codec->pix_fmt = AV_PIX_FMT_RGB555;
358                 break;
359             case 5:
360                 st->codec->pix_fmt = alpha_bmp ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
361                 break;
362             default:
363                 av_assert0(0);
364             }
365             memcpy(pkt->data, buf + colormapsize*colormapbpp, linesize * height);
366
367             res = pkt->size;
368
369 bitmap_end:
370             av_freep(&zbuf);
371             av_freep(&buf);
372             return res;
373 bitmap_end_skip:
374             av_freep(&zbuf);
375             av_freep(&buf);
376 #else
377             av_log(s, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
378 #endif
379         } else if (tag == TAG_STREAMBLOCK) {
380             for (i = 0; i < s->nb_streams; i++) {
381                 st = s->streams[i];
382                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
383             if (st->codec->codec_id == AV_CODEC_ID_MP3) {
384                 avio_skip(pb, 4);
385                 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
386                     return res;
387             } else { // ADPCM, PCM
388                 if ((res = av_get_packet(pb, pkt, len)) < 0)
389                     return res;
390             }
391             pkt->pos = pos;
392             pkt->stream_index = st->index;
393             return pkt->size;
394                 }
395             }
396         } else if (tag == TAG_JPEG2) {
397             for (i=0; i<s->nb_streams; i++) {
398                 st = s->streams[i];
399                 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
400                     break;
401             }
402             if (i == s->nb_streams) {
403                 vst = avformat_new_stream(s, NULL);
404                 if (!vst)
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);
410                 st = vst;
411             }
412             avio_rl16(pb); /* BITMAP_ID */
413             if ((res = av_new_packet(pkt, len-2)) < 0)
414                 return res;
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 */
420                 pkt->size -= 4;
421                 avio_read(pb, pkt->data, pkt->size);
422             } else {
423                 avio_read(pb, pkt->data + 4, pkt->size - 4);
424             }
425             pkt->pos = pos;
426             pkt->stream_index = st->index;
427             return pkt->size;
428         }
429     skip:
430         avio_skip(pb, len);
431     }
432 }
433
434 #if CONFIG_ZLIB
435 static av_cold int swf_read_close(AVFormatContext *avctx)
436 {
437     SWFContext *s = avctx->priv_data;
438     inflateEnd(&s->zstream);
439     av_freep(&s->zbuf_in);
440     av_freep(&s->zbuf_out);
441     av_freep(&s->zpb);
442     return 0;
443 }
444 #endif
445
446 AVInputFormat ff_swf_demuxer = {
447     .name           = "swf",
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,
453 #if CONFIG_ZLIB
454     .read_close     = swf_read_close,
455 #endif
456 };