2 * Flash Compatible Streaming Format
3 * Copyright (c) 2000 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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, pbBufPtr(&p) - 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, pbBufPtr(&p) - 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, rate_base;
199 swf = av_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 */
221 width = video_enc->width;
222 height = video_enc->height;
223 rate = video_enc->frame_rate;
224 rate_base = video_enc->frame_rate_base;
228 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
229 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
230 (will be patched if not streamed) */
232 put_swf_rect(pb, 0, width, 0, height);
233 put_le16(pb, (rate * 256) / rate_base); /* frame rate */
234 swf->duration_pos = url_ftell(pb);
235 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
237 /* define a shape with the jpeg inside */
239 put_swf_tag(s, TAG_DEFINESHAPE);
241 put_le16(pb, SHAPE_ID); /* ID of shape */
242 /* bounding rectangle */
243 put_swf_rect(pb, 0, width, 0, height);
245 put_byte(pb, 1); /* one fill style */
246 put_byte(pb, 0x41); /* clipped bitmap fill */
247 put_le16(pb, BITMAP_ID); /* bitmap ID */
248 /* position of the bitmap */
249 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
250 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
251 put_byte(pb, 0); /* no line style */
254 init_put_bits(&p, buf1, sizeof(buf1), NULL, NULL);
255 put_bits(&p, 4, 1); /* one fill bit */
256 put_bits(&p, 4, 0); /* zero line bit */
258 put_bits(&p, 1, 0); /* not an edge */
259 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
260 put_bits(&p, 5, 1); /* nbits */
261 put_bits(&p, 1, 0); /* X */
262 put_bits(&p, 1, 0); /* Y */
263 put_bits(&p, 1, 1); /* set fill style 1 */
265 /* draw the rectangle ! */
266 put_swf_line_edge(&p, width, 0);
267 put_swf_line_edge(&p, 0, height);
268 put_swf_line_edge(&p, -width, 0);
269 put_swf_line_edge(&p, 0, -height);
272 put_bits(&p, 1, 0); /* not an edge */
276 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
287 switch(audio_enc->sample_rate) {
302 if (audio_enc->channels == 2)
304 v |= 0x20; /* mp3 compressed */
305 v |= 0x02; /* 16 bits */
307 put_swf_tag(s, TAG_STREAMHEAD);
310 put_le16(&s->pb, (audio_enc->sample_rate * rate_base) / rate); /* avg samples per frame */
316 put_flush_packet(&s->pb);
320 static int swf_write_video(AVFormatContext *s,
321 AVCodecContext *enc, uint8_t *buf, int size)
323 ByteIOContext *pb = &s->pb;
324 static int tag_id = 0;
326 if (enc->frame_number > 1) {
327 /* remove the shape */
328 put_swf_tag(s, TAG_REMOVEOBJECT);
329 put_le16(pb, SHAPE_ID); /* shape ID */
330 put_le16(pb, 1); /* depth */
333 /* free the bitmap */
334 put_swf_tag(s, TAG_FREECHARACTER);
335 put_le16(pb, BITMAP_ID);
339 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
341 put_le16(pb, tag_id); /* ID of the image */
343 /* a dummy jpeg header seems to be required */
348 /* write the jpeg image */
349 put_buffer(pb, buf, size);
355 put_swf_tag(s, TAG_PLACEOBJECT);
356 put_le16(pb, SHAPE_ID); /* shape ID */
357 put_le16(pb, 1); /* depth */
358 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
361 /* output the frame */
362 put_swf_tag(s, TAG_SHOWFRAME);
365 put_flush_packet(&s->pb);
369 static int swf_write_audio(AVFormatContext *s, uint8_t *buf, int size)
371 ByteIOContext *pb = &s->pb;
373 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
375 put_buffer(pb, buf, size);
378 put_flush_packet(&s->pb);
382 static int swf_write_packet(AVFormatContext *s, int stream_index,
383 uint8_t *buf, int size, int force_pts)
385 AVCodecContext *codec = &s->streams[stream_index]->codec;
386 if (codec->codec_type == CODEC_TYPE_AUDIO)
387 return swf_write_audio(s, buf, size);
389 return swf_write_video(s, codec, buf, size);
392 static int swf_write_trailer(AVFormatContext *s)
394 SWFContext *swf = s->priv_data;
395 ByteIOContext *pb = &s->pb;
396 AVCodecContext *enc, *video_enc;
400 for(i=0;i<s->nb_streams;i++) {
401 enc = &s->streams[i]->codec;
402 if (enc->codec_type == CODEC_TYPE_VIDEO)
406 put_swf_tag(s, TAG_END);
409 put_flush_packet(&s->pb);
411 /* patch file size and number of frames if not streamed */
412 if (!url_is_streamed(&s->pb) && video_enc) {
413 file_size = url_ftell(pb);
414 url_fseek(pb, 4, SEEK_SET);
415 put_le32(pb, file_size);
416 url_fseek(pb, swf->duration_pos, SEEK_SET);
417 put_le16(pb, video_enc->frame_number);
422 /***********************************/
423 /* just to extract MP3 from swf */
425 static int get_swf_tag(ByteIOContext *pb, int *len_ptr)
443 static int swf_probe(AVProbeData *p)
445 /* check file header */
446 if (p->buf_size <= 16)
448 if (p->buf[0] == 'F' && p->buf[1] == 'W' &&
450 return AVPROBE_SCORE_MAX;
455 static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
457 ByteIOContext *pb = &s->pb;
458 int nbits, len, frame_rate, tag, v;
461 if ((get_be32(pb) & 0xffffff00) != MKBETAG('F', 'W', 'S', 0))
464 /* skip rectangle size */
465 nbits = get_byte(pb) >> 3;
466 len = (4 * nbits - 3 + 7) / 8;
468 frame_rate = get_le16(pb);
469 get_le16(pb); /* frame count */
472 tag = get_swf_tag(pb, &len);
474 fprintf(stderr, "No streaming found in SWF\n");
477 if (tag == TAG_STREAMHEAD) {
478 /* streaming found */
482 /* if mp3 streaming found, OK */
483 if ((v & 0x20) != 0) {
484 st = av_mallocz(sizeof(AVStream));
487 avcodec_get_context_defaults(&st->codec);
490 st->codec.channels = 2;
492 st->codec.channels = 1;
496 switch((v>> 2) & 0x03) {
498 st->codec.sample_rate = 11025;
501 st->codec.sample_rate = 22050;
504 st->codec.sample_rate = 44100;
510 st->codec.codec_type = CODEC_TYPE_AUDIO;
511 st->codec.codec_id = CODEC_ID_MP2;
522 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
524 ByteIOContext *pb = &s->pb;
528 tag = get_swf_tag(pb, &len);
531 if (tag == TAG_STREAMBLOCK) {
532 av_new_packet(pkt, len);
533 get_buffer(pb, pkt->data, pkt->size);
542 static int swf_read_close(AVFormatContext *s)
547 static AVInputFormat swf_iformat = {
557 static AVOutputFormat swf_oformat = {
560 "application/x-shockwave-flash",
572 av_register_input_format(&swf_iformat);
573 av_register_output_format(&swf_oformat);