2 * Flash Compatible Streaming Format muxer
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 "libavcodec/put_bits.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/fifo.h"
29 #define AUDIO_FIFO_SIZE 65536
31 typedef struct SWFEncContext {
35 int samples_per_frame;
38 int video_frame_number;
40 AVFifoBuffer *audio_fifo;
41 AVCodecParameters *audio_par, *video_par;
45 static void put_swf_tag(AVFormatContext *s, int tag)
47 SWFEncContext *swf = s->priv_data;
48 AVIOContext *pb = s->pb;
50 swf->tag_pos = avio_tell(pb);
52 /* reserve some room for the tag */
61 static void put_swf_end_tag(AVFormatContext *s)
63 SWFEncContext *swf = s->priv_data;
64 AVIOContext *pb = s->pb;
69 tag_len = pos - swf->tag_pos - 2;
71 avio_seek(pb, swf->tag_pos, SEEK_SET);
74 avio_wl16(pb, (tag << 6) | 0x3f);
75 avio_wl32(pb, tag_len - 4);
77 av_assert0(tag_len < 0x3f);
78 avio_wl16(pb, (tag << 6) | tag_len);
80 avio_seek(pb, pos, SEEK_SET);
83 static inline void max_nbits(int *nbits_ptr, int val)
99 static void put_swf_rect(AVIOContext *pb,
100 int xmin, int xmax, int ymin, int ymax)
106 init_put_bits(&p, buf, sizeof(buf));
109 max_nbits(&nbits, xmin);
110 max_nbits(&nbits, xmax);
111 max_nbits(&nbits, ymin);
112 max_nbits(&nbits, ymax);
113 mask = (1 << nbits) - 1;
116 put_bits(&p, 5, nbits);
117 put_bits(&p, nbits, xmin & mask);
118 put_bits(&p, nbits, xmax & mask);
119 put_bits(&p, nbits, ymin & mask);
120 put_bits(&p, nbits, ymax & mask);
123 avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
126 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
130 put_bits(pb, 1, 1); /* edge */
131 put_bits(pb, 1, 1); /* line select */
133 max_nbits(&nbits, dx);
134 max_nbits(&nbits, dy);
136 mask = (1 << nbits) - 1;
137 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
141 put_bits(pb, nbits, dy & mask);
142 } else if (dy == 0) {
145 put_bits(pb, nbits, dx & mask);
148 put_bits(pb, nbits, dx & mask);
149 put_bits(pb, nbits, dy & mask);
155 static void put_swf_matrix(AVIOContext *pb,
156 int a, int b, int c, int d, int tx, int ty)
162 init_put_bits(&p, buf, sizeof(buf));
164 put_bits(&p, 1, 1); /* a, d present */
166 max_nbits(&nbits, a);
167 max_nbits(&nbits, d);
168 put_bits(&p, 5, nbits); /* nb bits */
169 put_bits(&p, nbits, a);
170 put_bits(&p, nbits, d);
172 put_bits(&p, 1, 1); /* b, c present */
174 max_nbits(&nbits, c);
175 max_nbits(&nbits, b);
176 put_bits(&p, 5, nbits); /* nb bits */
177 put_bits(&p, nbits, c);
178 put_bits(&p, nbits, b);
181 max_nbits(&nbits, tx);
182 max_nbits(&nbits, ty);
183 put_bits(&p, 5, nbits); /* nb bits */
184 put_bits(&p, nbits, tx);
185 put_bits(&p, nbits, ty);
188 avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
191 static int swf_write_header(AVFormatContext *s)
193 SWFEncContext *swf = s->priv_data;
194 AVIOContext *pb = s->pb;
197 int i, width, height, rate, rate_base;
200 swf->sound_samples = 0;
201 swf->swf_frame_number = 0;
202 swf->video_frame_number = 0;
204 for(i=0;i<s->nb_streams;i++) {
205 AVCodecParameters *par = s->streams[i]->codecpar;
206 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
207 if (swf->audio_par) {
208 av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 audio stream\n");
209 return AVERROR_INVALIDDATA;
211 if (par->codec_id == AV_CODEC_ID_MP3) {
212 swf->audio_par = par;
213 swf->audio_fifo= av_fifo_alloc(AUDIO_FIFO_SIZE);
214 if (!swf->audio_fifo)
215 return AVERROR(ENOMEM);
217 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
221 if (swf->video_par) {
222 av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 video stream\n");
223 return AVERROR_INVALIDDATA;
225 if (par->codec_id == AV_CODEC_ID_VP6F ||
226 par->codec_id == AV_CODEC_ID_FLV1 ||
227 par->codec_id == AV_CODEC_ID_MJPEG) {
228 swf->video_st = s->streams[i];
229 swf->video_par = par;
231 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
237 if (!swf->video_par) {
238 /* currently, cannot work correctly if audio only */
244 width = swf->video_par->width;
245 height = swf->video_par->height;
246 // TODO: should be avg_frame_rate
247 rate = swf->video_st->time_base.den;
248 rate_base = swf->video_st->time_base.num;
252 swf->samples_per_frame = (44100LL * rate_base) / rate;
254 swf->samples_per_frame = (swf->audio_par->sample_rate * rate_base) / rate;
256 avio_write(pb, "FWS", 3);
258 if (!strcmp("avm2", s->oformat->name))
260 else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_VP6F)
261 version = 8; /* version 8 and above support VP6 codec */
262 else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1)
263 version = 6; /* version 6 and above support FLV1 codec */
265 version = 4; /* version 4 for mpeg audio support */
266 avio_w8(pb, version);
268 avio_wl32(pb, DUMMY_FILE_SIZE); /* dummy size
269 (will be patched if not streamed) */
271 put_swf_rect(pb, 0, width * 20, 0, height * 20);
272 if ((rate * 256LL) / rate_base >= (1<<16)) {
273 av_log(s, AV_LOG_ERROR, "Invalid (too large) frame rate %d/%d\n", rate, rate_base);
274 return AVERROR(EINVAL);
276 avio_wl16(pb, (rate * 256LL) / rate_base); /* frame rate */
277 swf->duration_pos = avio_tell(pb);
278 avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
280 /* swf v8 and later files require a file attribute tag */
282 put_swf_tag(s, TAG_FILEATTRIBUTES);
283 avio_wl32(pb, (version >= 9) << 3); /* set ActionScript v3/AVM2 flag */
287 /* define a shape with the jpeg inside */
288 if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_MJPEG) {
289 put_swf_tag(s, TAG_DEFINESHAPE);
291 avio_wl16(pb, SHAPE_ID); /* ID of shape */
292 /* bounding rectangle */
293 put_swf_rect(pb, 0, width, 0, height);
295 avio_w8(pb, 1); /* one fill style */
296 avio_w8(pb, 0x41); /* clipped bitmap fill */
297 avio_wl16(pb, BITMAP_ID); /* bitmap ID */
298 /* position of the bitmap */
299 put_swf_matrix(pb, 1 << FRAC_BITS, 0,
300 0, 1 << FRAC_BITS, 0, 0);
301 avio_w8(pb, 0); /* no line style */
304 init_put_bits(&p, buf1, sizeof(buf1));
305 put_bits(&p, 4, 1); /* one fill bit */
306 put_bits(&p, 4, 0); /* zero line bit */
308 put_bits(&p, 1, 0); /* not an edge */
309 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
310 put_bits(&p, 5, 1); /* nbits */
311 put_bits(&p, 1, 0); /* X */
312 put_bits(&p, 1, 0); /* Y */
313 put_bits(&p, 1, 1); /* set fill style 1 */
315 /* draw the rectangle ! */
316 put_swf_line_edge(&p, width, 0);
317 put_swf_line_edge(&p, 0, height);
318 put_swf_line_edge(&p, -width, 0);
319 put_swf_line_edge(&p, 0, -height);
322 put_bits(&p, 1, 0); /* not an edge */
326 avio_write(pb, buf1, put_bits_ptr(&p) - p.buf);
331 if (swf->audio_par && swf->audio_par->codec_id == AV_CODEC_ID_MP3) {
335 put_swf_tag(s, TAG_STREAMHEAD2);
336 switch(swf->audio_par->sample_rate) {
337 case 11025: v |= 1 << 2; break;
338 case 22050: v |= 2 << 2; break;
339 case 44100: v |= 3 << 2; break;
342 av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
345 v |= 0x02; /* 16 bit playback */
346 if (swf->audio_par->channels == 2)
347 v |= 0x01; /* stereo playback */
349 v |= 0x20; /* mp3 compressed */
351 avio_wl16(s->pb, swf->samples_per_frame); /* avg samples per frame */
360 static int swf_write_video(AVFormatContext *s,
361 AVCodecParameters *par, const uint8_t *buf, int size)
363 SWFEncContext *swf = s->priv_data;
364 AVIOContext *pb = s->pb;
366 /* Flash Player limit */
367 if (swf->swf_frame_number == 16000)
368 av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
370 if (par->codec_id == AV_CODEC_ID_VP6F ||
371 par->codec_id == AV_CODEC_ID_FLV1) {
372 if (swf->video_frame_number == 0) {
373 /* create a new video object */
374 put_swf_tag(s, TAG_VIDEOSTREAM);
375 avio_wl16(pb, VIDEO_ID);
376 swf->vframes_pos = avio_tell(pb);
377 avio_wl16(pb, 15000); /* hard flash player limit */
378 avio_wl16(pb, par->width);
379 avio_wl16(pb, par->height);
381 avio_w8(pb,ff_codec_get_tag(ff_swf_codec_tags, par->codec_id));
384 /* place the video object for the first time */
385 put_swf_tag(s, TAG_PLACEOBJECT2);
388 avio_wl16(pb, VIDEO_ID);
389 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
390 avio_wl16(pb, swf->video_frame_number);
391 avio_write(pb, "video", 5);
395 /* mark the character for update */
396 put_swf_tag(s, TAG_PLACEOBJECT2);
399 avio_wl16(pb, swf->video_frame_number);
403 /* set video frame data */
404 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
405 avio_wl16(pb, VIDEO_ID);
406 avio_wl16(pb, swf->video_frame_number++);
407 avio_write(pb, buf, size);
409 } else if (par->codec_id == AV_CODEC_ID_MJPEG) {
410 if (swf->swf_frame_number > 0) {
411 /* remove the shape */
412 put_swf_tag(s, TAG_REMOVEOBJECT);
413 avio_wl16(pb, SHAPE_ID); /* shape ID */
414 avio_wl16(pb, 1); /* depth */
417 /* free the bitmap */
418 put_swf_tag(s, TAG_FREECHARACTER);
419 avio_wl16(pb, BITMAP_ID);
423 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
425 avio_wl16(pb, BITMAP_ID); /* ID of the image */
427 /* a dummy jpeg header seems to be required */
428 avio_wb32(pb, 0xffd8ffd9);
429 /* write the jpeg image */
430 avio_write(pb, buf, size);
436 put_swf_tag(s, TAG_PLACEOBJECT);
437 avio_wl16(pb, SHAPE_ID); /* shape ID */
438 avio_wl16(pb, 1); /* depth */
439 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
443 swf->swf_frame_number++;
445 /* streaming sound always should be placed just before showframe tags */
446 if (swf->audio_par && av_fifo_size(swf->audio_fifo)) {
447 int frame_size = av_fifo_size(swf->audio_fifo);
448 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
449 avio_wl16(pb, swf->sound_samples);
450 avio_wl16(pb, 0); // seek samples
451 av_fifo_generic_read(swf->audio_fifo, pb, frame_size, (void*)avio_write);
455 swf->sound_samples = 0;
458 /* output the frame */
459 put_swf_tag(s, TAG_SHOWFRAME);
465 static int swf_write_audio(AVFormatContext *s,
466 AVCodecParameters *par, uint8_t *buf, int size)
468 SWFEncContext *swf = s->priv_data;
470 /* Flash Player limit */
471 if (swf->swf_frame_number == 16000)
472 av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
474 if (av_fifo_size(swf->audio_fifo) + size > AUDIO_FIFO_SIZE) {
475 av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
479 av_fifo_generic_write(swf->audio_fifo, buf, size, NULL);
480 swf->sound_samples += av_get_audio_frame_duration2(par, size);
482 /* if audio only stream make sure we add swf frames */
484 swf_write_video(s, par, 0, 0);
489 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
491 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
492 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
493 return swf_write_audio(s, par, pkt->data, pkt->size);
495 return swf_write_video(s, par, pkt->data, pkt->size);
498 static int swf_write_trailer(AVFormatContext *s)
500 SWFEncContext *swf = s->priv_data;
501 AVIOContext *pb = s->pb;
504 put_swf_tag(s, TAG_END);
507 /* patch file size and number of frames if not streamed */
508 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && swf->video_par) {
509 file_size = avio_tell(pb);
510 avio_seek(pb, 4, SEEK_SET);
511 avio_wl32(pb, file_size);
512 avio_seek(pb, swf->duration_pos, SEEK_SET);
513 avio_wl16(pb, swf->video_frame_number);
514 if (swf->vframes_pos) {
515 avio_seek(pb, swf->vframes_pos, SEEK_SET);
516 avio_wl16(pb, swf->video_frame_number);
518 avio_seek(pb, file_size, SEEK_SET);
523 static void swf_deinit(AVFormatContext *s)
525 SWFEncContext *swf = s->priv_data;
527 av_fifo_freep(&swf->audio_fifo);
531 AVOutputFormat ff_swf_muxer = {
533 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
534 .mime_type = "application/x-shockwave-flash",
536 .priv_data_size = sizeof(SWFEncContext),
537 .audio_codec = AV_CODEC_ID_MP3,
538 .video_codec = AV_CODEC_ID_FLV1,
539 .write_header = swf_write_header,
540 .write_packet = swf_write_packet,
541 .write_trailer = swf_write_trailer,
542 .deinit = swf_deinit,
543 .flags = AVFMT_TS_NONSTRICT,
546 #if CONFIG_AVM2_MUXER
547 AVOutputFormat ff_avm2_muxer = {
549 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash) (AVM2)"),
550 .mime_type = "application/x-shockwave-flash",
551 .priv_data_size = sizeof(SWFEncContext),
552 .audio_codec = AV_CODEC_ID_MP3,
553 .video_codec = AV_CODEC_ID_FLV1,
554 .write_header = swf_write_header,
555 .write_packet = swf_write_packet,
556 .write_trailer = swf_write_trailer,
557 .deinit = swf_deinit,
558 .flags = AVFMT_TS_NONSTRICT,