2 * Flash Compatible Streaming Format
3 * Copyright (c) 2000 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* should have a generic way to indicate probable size */
22 #define DUMMY_FILE_SIZE (100 * 1024 * 1024)
23 #define DUMMY_DURATION 600 /* in seconds */
26 #define TAG_SHOWFRAME 1
27 #define TAG_DEFINESHAPE 2
28 #define TAG_FREECHARACTER 3
29 #define TAG_PLACEOBJECT 4
30 #define TAG_REMOVEOBJECT 5
31 #define TAG_STREAMHEAD 18
32 #define TAG_STREAMBLOCK 19
35 #define TAG_LONG 0x100
37 /* flags for shape definition */
38 #define FLAG_MOVETO 0x01
39 #define FLAG_SETFILL0 0x02
40 #define FLAG_SETFILL1 0x04
42 /* character id used */
47 offset_t duration_pos;
52 static void put_swf_tag(AVFormatContext *s, int tag)
54 SWFContext *swf = s->priv_data;
55 ByteIOContext *pb = &s->pb;
57 swf->tag_pos = url_ftell(pb);
59 /* reserve some room for the tag */
68 static void put_swf_end_tag(AVFormatContext *s)
70 SWFContext *swf = s->priv_data;
71 ByteIOContext *pb = &s->pb;
76 tag_len = pos - swf->tag_pos - 2;
78 url_fseek(pb, swf->tag_pos, SEEK_SET);
81 put_le16(pb, (tag << 6) | 0x3f);
82 put_le32(pb, tag_len - 4);
84 assert(tag_len < 0x3f);
85 put_le16(pb, (tag << 6) | tag_len);
87 url_fseek(pb, pos, SEEK_SET);
90 static inline void max_nbits(int *nbits_ptr, int val)
106 static void put_swf_rect(ByteIOContext *pb,
107 int xmin, int xmax, int ymin, int ymax)
113 init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
116 max_nbits(&nbits, xmin);
117 max_nbits(&nbits, xmax);
118 max_nbits(&nbits, ymin);
119 max_nbits(&nbits, ymax);
120 mask = (1 << nbits) - 1;
123 put_bits(&p, 5, nbits);
124 put_bits(&p, nbits, xmin & mask);
125 put_bits(&p, nbits, xmax & mask);
126 put_bits(&p, nbits, ymin & mask);
127 put_bits(&p, nbits, ymax & mask);
130 put_buffer(pb, buf, p.buf_ptr - p.buf);
133 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
137 put_bits(pb, 1, 1); /* edge */
138 put_bits(pb, 1, 1); /* line select */
140 max_nbits(&nbits, dx);
141 max_nbits(&nbits, dy);
143 mask = (1 << nbits) - 1;
144 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
148 put_bits(pb, nbits, dy & mask);
149 } else if (dy == 0) {
152 put_bits(pb, nbits, dx & mask);
155 put_bits(pb, nbits, dx & mask);
156 put_bits(pb, nbits, dy & mask);
162 /* put matrix (not size optimized */
163 static void put_swf_matrix(ByteIOContext *pb,
164 int a, int b, int c, int d, int tx, int ty)
169 init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
171 put_bits(&p, 1, 1); /* a, d present */
172 put_bits(&p, 5, 20); /* nb bits */
176 put_bits(&p, 1, 1); /* b, c present */
177 put_bits(&p, 5, 20); /* nb bits */
181 put_bits(&p, 5, 20); /* nb bits */
182 put_bits(&p, 20, tx);
183 put_bits(&p, 20, ty);
186 put_buffer(pb, buf, p.buf_ptr - p.buf);
189 /* XXX: handle audio only */
190 static int swf_write_header(AVFormatContext *s)
193 ByteIOContext *pb = &s->pb;
194 AVCodecContext *enc, *audio_enc, *video_enc;
197 int i, width, height, rate;
199 swf = malloc(sizeof(SWFContext));
206 for(i=0;i<s->nb_streams;i++) {
207 enc = &s->streams[i]->codec;
208 if (enc->codec_type == CODEC_TYPE_AUDIO)
215 /* currenty, cannot work correctly if audio only */
218 rate = 10 * FRAME_RATE_BASE;
220 width = video_enc->width;
221 height = video_enc->height;
222 rate = video_enc->frame_rate;
226 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
227 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
228 (will be patched if not streamed) */
230 put_swf_rect(pb, 0, width, 0, height);
231 put_le16(pb, (rate * 256) / FRAME_RATE_BASE); /* frame rate */
232 swf->duration_pos = url_ftell(pb);
233 put_le16(pb, (UINT16)(DUMMY_DURATION * (INT64)rate / FRAME_RATE_BASE)); /* frame count */
235 /* define a shape with the jpeg inside */
237 put_swf_tag(s, TAG_DEFINESHAPE);
239 put_le16(pb, SHAPE_ID); /* ID of shape */
240 /* bounding rectangle */
241 put_swf_rect(pb, 0, width, 0, height);
243 put_byte(pb, 1); /* one fill style */
244 put_byte(pb, 0x41); /* clipped bitmap fill */
245 put_le16(pb, BITMAP_ID); /* bitmap ID */
246 /* position of the bitmap */
247 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
248 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
249 put_byte(pb, 0); /* no line style */
252 init_put_bits(&p, buf1, sizeof(buf1), NULL, NULL);
253 put_bits(&p, 4, 1); /* one fill bit */
254 put_bits(&p, 4, 0); /* zero line bit */
256 put_bits(&p, 1, 0); /* not an edge */
257 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
258 put_bits(&p, 5, 1); /* nbits */
259 put_bits(&p, 1, 0); /* X */
260 put_bits(&p, 1, 0); /* Y */
261 put_bits(&p, 1, 1); /* set fill style 1 */
263 /* draw the rectangle ! */
264 put_swf_line_edge(&p, width, 0);
265 put_swf_line_edge(&p, 0, height);
266 put_swf_line_edge(&p, -width, 0);
267 put_swf_line_edge(&p, 0, -height);
270 put_bits(&p, 1, 0); /* not an edge */
274 put_buffer(pb, buf1, p.buf_ptr - p.buf);
285 switch(audio_enc->sample_rate) {
300 if (audio_enc->channels == 2)
302 v |= 0x20; /* mp3 compressed */
303 v |= 0x02; /* 16 bits */
305 put_swf_tag(s, TAG_STREAMHEAD);
308 put_le16(&s->pb, (audio_enc->sample_rate * FRAME_RATE_BASE) / rate); /* avg samples per frame */
314 put_flush_packet(&s->pb);
318 static int swf_write_video(AVFormatContext *s,
319 AVCodecContext *enc, UINT8 *buf, int size)
321 ByteIOContext *pb = &s->pb;
322 static int tag_id = 0;
324 if (enc->frame_number > 1) {
325 /* remove the shape */
326 put_swf_tag(s, TAG_REMOVEOBJECT);
327 put_le16(pb, SHAPE_ID); /* shape ID */
328 put_le16(pb, 1); /* depth */
331 /* free the bitmap */
332 put_swf_tag(s, TAG_FREECHARACTER);
333 put_le16(pb, BITMAP_ID);
337 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
339 put_le16(pb, tag_id); /* ID of the image */
341 /* a dummy jpeg header seems to be required */
346 /* write the jpeg image */
347 put_buffer(pb, buf, size);
353 put_swf_tag(s, TAG_PLACEOBJECT);
354 put_le16(pb, SHAPE_ID); /* shape ID */
355 put_le16(pb, 1); /* depth */
356 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
359 /* output the frame */
360 put_swf_tag(s, TAG_SHOWFRAME);
363 put_flush_packet(&s->pb);
367 static int swf_write_audio(AVFormatContext *s, UINT8 *buf, int size)
369 ByteIOContext *pb = &s->pb;
371 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
373 put_buffer(pb, buf, size);
376 put_flush_packet(&s->pb);
380 static int swf_write_packet(AVFormatContext *s, int stream_index,
381 UINT8 *buf, int size)
383 AVCodecContext *codec = &s->streams[stream_index]->codec;
384 if (codec->codec_type == CODEC_TYPE_AUDIO)
385 return swf_write_audio(s, buf, size);
387 return swf_write_video(s, codec, buf, size);
390 static int swf_write_trailer(AVFormatContext *s)
392 SWFContext *swf = s->priv_data;
393 ByteIOContext *pb = &s->pb;
394 AVCodecContext *enc, *video_enc;
398 for(i=0;i<s->nb_streams;i++) {
399 enc = &s->streams[i]->codec;
400 if (enc->codec_type == CODEC_TYPE_VIDEO)
404 put_swf_tag(s, TAG_END);
407 put_flush_packet(&s->pb);
409 /* patch file size and number of frames if not streamed */
410 if (!url_is_streamed(&s->pb) && video_enc) {
411 file_size = url_ftell(pb);
412 url_fseek(pb, 4, SEEK_SET);
413 put_le32(pb, file_size);
414 url_fseek(pb, swf->duration_pos, SEEK_SET);
415 put_le16(pb, video_enc->frame_number);
421 /***********************************/
422 /* just to extract MP3 from swf */
424 static int get_swf_tag(ByteIOContext *pb, int *len_ptr)
441 static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
443 ByteIOContext *pb = &s->pb;
444 int nbits, len, frame_rate, tag, v;
447 if ((get_be32(pb) & 0xffffff00) != MKBETAG('F', 'W', 'S', 0))
450 /* skip rectangle size */
451 nbits = get_byte(pb) >> 3;
452 len = (4 * nbits - 3 + 7) / 8;
454 frame_rate = get_le16(pb);
455 get_le16(pb); /* frame count */
458 tag = get_swf_tag(pb, &len);
460 fprintf(stderr, "No streaming found in SWF\n");
463 if (tag == TAG_STREAMHEAD) {
464 /* streaming found */
468 /* if mp3 streaming found, OK */
469 if ((v & 0x20) != 0) {
470 st = av_mallocz(sizeof(AVStream));
474 st->codec.channels = 2;
476 st->codec.channels = 1;
480 switch((v>> 2) & 0x03) {
482 st->codec.sample_rate = 11025;
485 st->codec.sample_rate = 22050;
488 st->codec.sample_rate = 44100;
494 st->codec.codec_type = CODEC_TYPE_AUDIO;
495 st->codec.codec_id = CODEC_ID_MP2;
506 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
508 ByteIOContext *pb = &s->pb;
512 tag = get_swf_tag(pb, &len);
515 if (tag == TAG_STREAMBLOCK) {
516 av_new_packet(pkt, len);
517 get_buffer(pb, pkt->data, pkt->size);
526 static int swf_read_close(AVFormatContext *s)
531 AVFormat swf_format = {
534 "application/x-shockwave-flash",