split swf de/muxer
[libav.git] / libavformat / swfenc.c
1 /*
2 * Flash Compatible Streaming Format muxer.
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 "libavcodec/bitstream.h"
24 #include "avformat.h"
25 #include "swf.h"
26
27 static void put_swf_tag(AVFormatContext *s, int tag)
28 {
29 SWFContext *swf = s->priv_data;
30 ByteIOContext *pb = s->pb;
31
32 swf->tag_pos = url_ftell(pb);
33 swf->tag = tag;
34 /* reserve some room for the tag */
35 if (tag & TAG_LONG) {
36 put_le16(pb, 0);
37 put_le32(pb, 0);
38 } else {
39 put_le16(pb, 0);
40 }
41 }
42
43 static void put_swf_end_tag(AVFormatContext *s)
44 {
45 SWFContext *swf = s->priv_data;
46 ByteIOContext *pb = s->pb;
47 offset_t pos;
48 int tag_len, tag;
49
50 pos = url_ftell(pb);
51 tag_len = pos - swf->tag_pos - 2;
52 tag = swf->tag;
53 url_fseek(pb, swf->tag_pos, SEEK_SET);
54 if (tag & TAG_LONG) {
55 tag &= ~TAG_LONG;
56 put_le16(pb, (tag << 6) | 0x3f);
57 put_le32(pb, tag_len - 4);
58 } else {
59 assert(tag_len < 0x3f);
60 put_le16(pb, (tag << 6) | tag_len);
61 }
62 url_fseek(pb, pos, SEEK_SET);
63 }
64
65 static inline void max_nbits(int *nbits_ptr, int val)
66 {
67 int n;
68
69 if (val == 0)
70 return;
71 val = abs(val);
72 n = 1;
73 while (val != 0) {
74 n++;
75 val >>= 1;
76 }
77 if (n > *nbits_ptr)
78 *nbits_ptr = n;
79 }
80
81 static void put_swf_rect(ByteIOContext *pb,
82 int xmin, int xmax, int ymin, int ymax)
83 {
84 PutBitContext p;
85 uint8_t buf[256];
86 int nbits, mask;
87
88 init_put_bits(&p, buf, sizeof(buf));
89
90 nbits = 0;
91 max_nbits(&nbits, xmin);
92 max_nbits(&nbits, xmax);
93 max_nbits(&nbits, ymin);
94 max_nbits(&nbits, ymax);
95 mask = (1 << nbits) - 1;
96
97 /* rectangle info */
98 put_bits(&p, 5, nbits);
99 put_bits(&p, nbits, xmin & mask);
100 put_bits(&p, nbits, xmax & mask);
101 put_bits(&p, nbits, ymin & mask);
102 put_bits(&p, nbits, ymax & mask);
103
104 flush_put_bits(&p);
105 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
106 }
107
108 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
109 {
110 int nbits, mask;
111
112 put_bits(pb, 1, 1); /* edge */
113 put_bits(pb, 1, 1); /* line select */
114 nbits = 2;
115 max_nbits(&nbits, dx);
116 max_nbits(&nbits, dy);
117
118 mask = (1 << nbits) - 1;
119 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
120 if (dx == 0) {
121 put_bits(pb, 1, 0);
122 put_bits(pb, 1, 1);
123 put_bits(pb, nbits, dy & mask);
124 } else if (dy == 0) {
125 put_bits(pb, 1, 0);
126 put_bits(pb, 1, 0);
127 put_bits(pb, nbits, dx & mask);
128 } else {
129 put_bits(pb, 1, 1);
130 put_bits(pb, nbits, dx & mask);
131 put_bits(pb, nbits, dy & mask);
132 }
133 }
134
135 #define FRAC_BITS 16
136
137 /* put matrix */
138 static void put_swf_matrix(ByteIOContext *pb,
139 int a, int b, int c, int d, int tx, int ty)
140 {
141 PutBitContext p;
142 uint8_t buf[256];
143 int nbits;
144
145 init_put_bits(&p, buf, sizeof(buf));
146
147 put_bits(&p, 1, 1); /* a, d present */
148 nbits = 1;
149 max_nbits(&nbits, a);
150 max_nbits(&nbits, d);
151 put_bits(&p, 5, nbits); /* nb bits */
152 put_bits(&p, nbits, a);
153 put_bits(&p, nbits, d);
154
155 put_bits(&p, 1, 1); /* b, c present */
156 nbits = 1;
157 max_nbits(&nbits, c);
158 max_nbits(&nbits, b);
159 put_bits(&p, 5, nbits); /* nb bits */
160 put_bits(&p, nbits, c);
161 put_bits(&p, nbits, b);
162
163 nbits = 1;
164 max_nbits(&nbits, tx);
165 max_nbits(&nbits, ty);
166 put_bits(&p, 5, nbits); /* nb bits */
167 put_bits(&p, nbits, tx);
168 put_bits(&p, nbits, ty);
169
170 flush_put_bits(&p);
171 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
172 }
173
174 /* */
175 static int swf_write_header(AVFormatContext *s)
176 {
177 SWFContext *swf = s->priv_data;
178 ByteIOContext *pb = s->pb;
179 AVCodecContext *enc, *audio_enc, *video_enc;
180 PutBitContext p;
181 uint8_t buf1[256];
182 int i, width, height, rate, rate_base;
183 int is_avm2;
184
185 swf->audio_in_pos = 0;
186 swf->sound_samples = 0;
187 swf->swf_frame_number = 0;
188 swf->video_frame_number = 0;
189
190 video_enc = NULL;
191 audio_enc = NULL;
192 for(i=0;i<s->nb_streams;i++) {
193 enc = s->streams[i]->codec;
194 if (enc->codec_type == CODEC_TYPE_AUDIO) {
195 if (enc->codec_id == CODEC_ID_MP3) {
196 if (!enc->frame_size) {
197 av_log(s, AV_LOG_ERROR, "audio frame size not set\n");
198 return -1;
199 }
200 audio_enc = enc;
201 } else {
202 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
203 return -1;
204 }
205 } else {
206 if (enc->codec_id == CODEC_ID_VP6F ||
207 enc->codec_id == CODEC_ID_FLV1 ||
208 enc->codec_id == CODEC_ID_MJPEG) {
209 video_enc = enc;
210 } else {
211 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
212 return -1;
213 }
214 }
215 }
216
217 if (!video_enc) {
218 /* currently, cannot work correctly if audio only */
219 swf->video_type = 0;
220 width = 320;
221 height = 200;
222 rate = 10;
223 rate_base= 1;
224 } else {
225 swf->video_type = video_enc->codec_id;
226 width = video_enc->width;
227 height = video_enc->height;
228 rate = video_enc->time_base.den;
229 rate_base = video_enc->time_base.num;
230 }
231
232 if (!audio_enc) {
233 swf->audio_type = 0;
234 swf->samples_per_frame = (44100. * rate_base) / rate;
235 } else {
236 swf->audio_type = audio_enc->codec_id;
237 swf->samples_per_frame = (audio_enc->sample_rate * rate_base) / rate;
238 }
239
240 is_avm2 = !strcmp("avm2", s->oformat->name);
241
242 put_tag(pb, "FWS");
243 if (is_avm2) {
244 put_byte(pb, 9);
245 } else if (video_enc && video_enc->codec_id == CODEC_ID_VP6F) {
246 put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */
247 } else if (video_enc && video_enc->codec_id == CODEC_ID_FLV1) {
248 put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */
249 } else {
250 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
251 }
252 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
253 (will be patched if not streamed) */
254
255 put_swf_rect(pb, 0, width * 20, 0, height * 20);
256 put_le16(pb, (rate * 256) / rate_base); /* frame rate */
257 swf->duration_pos = url_ftell(pb);
258 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
259
260 /* avm2/swf v9 (also v8?) files require a file attribute tag */
261 if (is_avm2) {
262 put_swf_tag(s, TAG_FILEATTRIBUTES);
263 put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
264 put_swf_end_tag(s);
265 }
266
267 /* define a shape with the jpeg inside */
268 if (video_enc && (video_enc->codec_id == CODEC_ID_VP6F ||
269 video_enc->codec_id == CODEC_ID_FLV1)) {
270 } else if (video_enc && video_enc->codec_id == CODEC_ID_MJPEG) {
271 put_swf_tag(s, TAG_DEFINESHAPE);
272
273 put_le16(pb, SHAPE_ID); /* ID of shape */
274 /* bounding rectangle */
275 put_swf_rect(pb, 0, width, 0, height);
276 /* style info */
277 put_byte(pb, 1); /* one fill style */
278 put_byte(pb, 0x41); /* clipped bitmap fill */
279 put_le16(pb, BITMAP_ID); /* bitmap ID */
280 /* position of the bitmap */
281 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
282 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
283 put_byte(pb, 0); /* no line style */
284
285 /* shape drawing */
286 init_put_bits(&p, buf1, sizeof(buf1));
287 put_bits(&p, 4, 1); /* one fill bit */
288 put_bits(&p, 4, 0); /* zero line bit */
289
290 put_bits(&p, 1, 0); /* not an edge */
291 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
292 put_bits(&p, 5, 1); /* nbits */
293 put_bits(&p, 1, 0); /* X */
294 put_bits(&p, 1, 0); /* Y */
295 put_bits(&p, 1, 1); /* set fill style 1 */
296
297 /* draw the rectangle ! */
298 put_swf_line_edge(&p, width, 0);
299 put_swf_line_edge(&p, 0, height);
300 put_swf_line_edge(&p, -width, 0);
301 put_swf_line_edge(&p, 0, -height);
302
303 /* end of shape */
304 put_bits(&p, 1, 0); /* not an edge */
305 put_bits(&p, 5, 0);
306
307 flush_put_bits(&p);
308 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
309
310 put_swf_end_tag(s);
311 }
312
313 if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3) {
314 int v;
315
316 /* start sound */
317 put_swf_tag(s, TAG_STREAMHEAD2);
318
319 v = 0;
320 switch(audio_enc->sample_rate) {
321 case 11025:
322 v |= 1 << 2;
323 break;
324 case 22050:
325 v |= 2 << 2;
326 break;
327 case 44100:
328 v |= 3 << 2;
329 break;
330 default:
331 /* not supported */
332 av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
333 return -1;
334 }
335 v |= 0x02; /* 16 bit playback */
336 if (audio_enc->channels == 2)
337 v |= 0x01; /* stereo playback */
338 put_byte(s->pb, v);
339 v |= 0x20; /* mp3 compressed */
340 put_byte(s->pb, v);
341 put_le16(s->pb, swf->samples_per_frame); /* avg samples per frame */
342 put_le16(s->pb, 0);
343
344 put_swf_end_tag(s);
345 }
346
347 put_flush_packet(s->pb);
348 return 0;
349 }
350
351 static int swf_write_video(AVFormatContext *s,
352 AVCodecContext *enc, const uint8_t *buf, int size)
353 {
354 SWFContext *swf = s->priv_data;
355 ByteIOContext *pb = s->pb;
356
357 /* Flash Player limit */
358 if (swf->swf_frame_number == 16000) {
359 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
360 }
361
362 if (swf->video_type == CODEC_ID_VP6F ||
363 swf->video_type == CODEC_ID_FLV1) {
364 if (swf->video_frame_number == 0) {
365 /* create a new video object */
366 put_swf_tag(s, TAG_VIDEOSTREAM);
367 put_le16(pb, VIDEO_ID);
368 put_le16(pb, 15000); /* hard flash player limit */
369 put_le16(pb, enc->width);
370 put_le16(pb, enc->height);
371 put_byte(pb, 0);
372 put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type));
373 put_swf_end_tag(s);
374
375 /* place the video object for the first time */
376 put_swf_tag(s, TAG_PLACEOBJECT2);
377 put_byte(pb, 0x36);
378 put_le16(pb, 1);
379 put_le16(pb, VIDEO_ID);
380 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
381 put_le16(pb, swf->video_frame_number);
382 put_byte(pb, 'v');
383 put_byte(pb, 'i');
384 put_byte(pb, 'd');
385 put_byte(pb, 'e');
386 put_byte(pb, 'o');
387 put_byte(pb, 0x00);
388 put_swf_end_tag(s);
389 } else {
390 /* mark the character for update */
391 put_swf_tag(s, TAG_PLACEOBJECT2);
392 put_byte(pb, 0x11);
393 put_le16(pb, 1);
394 put_le16(pb, swf->video_frame_number);
395 put_swf_end_tag(s);
396 }
397
398 /* set video frame data */
399 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
400 put_le16(pb, VIDEO_ID);
401 put_le16(pb, swf->video_frame_number++);
402 put_buffer(pb, buf, size);
403 put_swf_end_tag(s);
404 } else if (swf->video_type == CODEC_ID_MJPEG) {
405 if (swf->swf_frame_number > 0) {
406 /* remove the shape */
407 put_swf_tag(s, TAG_REMOVEOBJECT);
408 put_le16(pb, SHAPE_ID); /* shape ID */
409 put_le16(pb, 1); /* depth */
410 put_swf_end_tag(s);
411
412 /* free the bitmap */
413 put_swf_tag(s, TAG_FREECHARACTER);
414 put_le16(pb, BITMAP_ID);
415 put_swf_end_tag(s);
416 }
417
418 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
419
420 put_le16(pb, BITMAP_ID); /* ID of the image */
421
422 /* a dummy jpeg header seems to be required */
423 put_byte(pb, 0xff);
424 put_byte(pb, 0xd8);
425 put_byte(pb, 0xff);
426 put_byte(pb, 0xd9);
427 /* write the jpeg image */
428 put_buffer(pb, buf, size);
429
430 put_swf_end_tag(s);
431
432 /* draw the shape */
433
434 put_swf_tag(s, TAG_PLACEOBJECT);
435 put_le16(pb, SHAPE_ID); /* shape ID */
436 put_le16(pb, 1); /* depth */
437 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
438 put_swf_end_tag(s);
439 } else {
440 /* invalid codec */
441 }
442
443 swf->swf_frame_number ++;
444
445 /* streaming sound always should be placed just before showframe tags */
446 if (swf->audio_type && swf->audio_in_pos) {
447 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
448 put_le16(pb, swf->sound_samples);
449 put_le16(pb, 0); // seek samples
450 put_buffer(pb, swf->audio_fifo, swf->audio_in_pos);
451 put_swf_end_tag(s);
452
453 /* update FIFO */
454 swf->sound_samples = 0;
455 swf->audio_in_pos = 0;
456 }
457
458 /* output the frame */
459 put_swf_tag(s, TAG_SHOWFRAME);
460 put_swf_end_tag(s);
461
462 put_flush_packet(s->pb);
463
464 return 0;
465 }
466
467 static int swf_write_audio(AVFormatContext *s,
468 AVCodecContext *enc, const uint8_t *buf, int size)
469 {
470 SWFContext *swf = s->priv_data;
471
472 /* Flash Player limit */
473 if (swf->swf_frame_number == 16000) {
474 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
475 }
476
477 if (swf->audio_in_pos + size >= AUDIO_FIFO_SIZE) {
478 av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
479 return -1;
480 }
481
482 memcpy(swf->audio_fifo + swf->audio_in_pos, buf, size);
483 swf->audio_in_pos += size;
484 swf->sound_samples += enc->frame_size;
485
486 /* if audio only stream make sure we add swf frames */
487 if (swf->video_type == 0) {
488 swf_write_video(s, enc, 0, 0);
489 }
490
491 return 0;
492 }
493
494 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
495 {
496 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
497 if (codec->codec_type == CODEC_TYPE_AUDIO)
498 return swf_write_audio(s, codec, pkt->data, pkt->size);
499 else
500 return swf_write_video(s, codec, pkt->data, pkt->size);
501 }
502
503 static int swf_write_trailer(AVFormatContext *s)
504 {
505 SWFContext *swf = s->priv_data;
506 ByteIOContext *pb = s->pb;
507 AVCodecContext *enc, *video_enc;
508 int file_size, i;
509
510 video_enc = NULL;
511 for(i=0;i<s->nb_streams;i++) {
512 enc = s->streams[i]->codec;
513 if (enc->codec_type == CODEC_TYPE_VIDEO)
514 video_enc = enc;
515 }
516
517 put_swf_tag(s, TAG_END);
518 put_swf_end_tag(s);
519
520 put_flush_packet(s->pb);
521
522 /* patch file size and number of frames if not streamed */
523 if (!url_is_streamed(s->pb) && video_enc) {
524 file_size = url_ftell(pb);
525 url_fseek(pb, 4, SEEK_SET);
526 put_le32(pb, file_size);
527 url_fseek(pb, swf->duration_pos, SEEK_SET);
528 put_le16(pb, video_enc->frame_number);
529 url_fseek(pb, file_size, SEEK_SET);
530 }
531 return 0;
532 }
533
534 #ifdef CONFIG_SWF_MUXER
535 AVOutputFormat swf_muxer = {
536 "swf",
537 "Flash format",
538 "application/x-shockwave-flash",
539 "swf",
540 sizeof(SWFContext),
541 CODEC_ID_MP3,
542 CODEC_ID_FLV1,
543 swf_write_header,
544 swf_write_packet,
545 swf_write_trailer,
546 };
547 #endif
548 #ifdef CONFIG_AVM2_MUXER
549 AVOutputFormat avm2_muxer = {
550 "avm2",
551 "Flash 9 (AVM2) format",
552 "application/x-shockwave-flash",
553 NULL,
554 sizeof(SWFContext),
555 CODEC_ID_MP3,
556 CODEC_ID_FLV1,
557 swf_write_header,
558 swf_write_packet,
559 swf_write_trailer,
560 };
561 #endif