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"
30 #define AUDIO_FIFO_SIZE 65536
32 typedef struct SWFEncContext {
36 int samples_per_frame;
39 int video_frame_number;
41 AVFifoBuffer *audio_fifo;
42 AVCodecParameters *audio_par, *video_par;
46 static void put_swf_tag(AVFormatContext *s, int tag)
48 SWFEncContext *swf = s->priv_data;
49 AVIOContext *pb = s->pb;
51 swf->tag_pos = avio_tell(pb);
53 /* reserve some room for the tag */
62 static void put_swf_end_tag(AVFormatContext *s)
64 SWFEncContext *swf = s->priv_data;
65 AVIOContext *pb = s->pb;
70 tag_len = pos - swf->tag_pos - 2;
72 avio_seek(pb, swf->tag_pos, SEEK_SET);
75 avio_wl16(pb, (tag << 6) | 0x3f);
76 avio_wl32(pb, tag_len - 4);
78 av_assert0(tag_len < 0x3f);
79 avio_wl16(pb, (tag << 6) | tag_len);
81 avio_seek(pb, pos, SEEK_SET);
84 static inline void max_nbits(int *nbits_ptr, int val)
100 static void put_swf_rect(AVIOContext *pb,
101 int xmin, int xmax, int ymin, int ymax)
107 init_put_bits(&p, buf, sizeof(buf));
110 max_nbits(&nbits, xmin);
111 max_nbits(&nbits, xmax);
112 max_nbits(&nbits, ymin);
113 max_nbits(&nbits, ymax);
114 mask = (1 << nbits) - 1;
117 put_bits(&p, 5, nbits);
118 put_bits(&p, nbits, xmin & mask);
119 put_bits(&p, nbits, xmax & mask);
120 put_bits(&p, nbits, ymin & mask);
121 put_bits(&p, nbits, ymax & mask);
124 avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
127 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
131 put_bits(pb, 1, 1); /* edge */
132 put_bits(pb, 1, 1); /* line select */
134 max_nbits(&nbits, dx);
135 max_nbits(&nbits, dy);
137 mask = (1 << nbits) - 1;
138 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
142 put_bits(pb, nbits, dy & mask);
143 } else if (dy == 0) {
146 put_bits(pb, nbits, dx & mask);
149 put_bits(pb, nbits, dx & mask);
150 put_bits(pb, nbits, dy & mask);
156 static void put_swf_matrix(AVIOContext *pb,
157 int a, int b, int c, int d, int tx, int ty)
163 init_put_bits(&p, buf, sizeof(buf));
165 put_bits(&p, 1, 1); /* a, d present */
167 max_nbits(&nbits, a);
168 max_nbits(&nbits, d);
169 put_bits(&p, 5, nbits); /* nb bits */
170 put_bits(&p, nbits, a);
171 put_bits(&p, nbits, d);
173 put_bits(&p, 1, 1); /* b, c present */
175 max_nbits(&nbits, c);
176 max_nbits(&nbits, b);
177 put_bits(&p, 5, nbits); /* nb bits */
178 put_bits(&p, nbits, c);
179 put_bits(&p, nbits, b);
182 max_nbits(&nbits, tx);
183 max_nbits(&nbits, ty);
184 put_bits(&p, 5, nbits); /* nb bits */
185 put_bits(&p, nbits, tx);
186 put_bits(&p, nbits, ty);
189 avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
192 static int swf_write_header(AVFormatContext *s)
194 SWFEncContext *swf = s->priv_data;
195 AVIOContext *pb = s->pb;
198 int i, width, height, rate, rate_base;
201 swf->sound_samples = 0;
202 swf->swf_frame_number = 0;
203 swf->video_frame_number = 0;
205 for(i=0;i<s->nb_streams;i++) {
206 AVCodecParameters *par = s->streams[i]->codecpar;
207 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
208 if (swf->audio_par) {
209 av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 audio stream\n");
210 return AVERROR_INVALIDDATA;
212 if (par->codec_id == AV_CODEC_ID_MP3) {
213 swf->audio_par = par;
214 swf->audio_fifo= av_fifo_alloc(AUDIO_FIFO_SIZE);
215 if (!swf->audio_fifo)
216 return AVERROR(ENOMEM);
218 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
222 if (swf->video_par) {
223 av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 video stream\n");
224 return AVERROR_INVALIDDATA;
226 if (ff_codec_get_tag(ff_swf_codec_tags, par->codec_id) ||
227 par->codec_id == AV_CODEC_ID_PNG ||
228 par->codec_id == AV_CODEC_ID_MJPEG) {
229 swf->video_st = s->streams[i];
230 swf->video_par = par;
232 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV, Flash Screen Video, PNG and MJPEG\n");
238 if (!swf->video_par) {
239 /* currently, cannot work correctly if audio only */
245 width = swf->video_par->width;
246 height = swf->video_par->height;
247 // TODO: should be avg_frame_rate
248 rate = swf->video_st->time_base.den;
249 rate_base = swf->video_st->time_base.num;
253 swf->samples_per_frame = (44100LL * rate_base) / rate;
255 swf->samples_per_frame = (swf->audio_par->sample_rate * rate_base) / rate;
257 avio_write(pb, "FWS", 3);
259 if (!strcmp("avm2", s->oformat->name))
261 else if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_VP6A ||
262 swf->video_par->codec_id == AV_CODEC_ID_VP6F ||
263 swf->video_par->codec_id == AV_CODEC_ID_PNG))
264 version = 8; /* version 8 and above support VP6 and PNG codec */
265 else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLASHSV)
266 version = 7; /* version 7 and above support Flash Screen Video codec */
267 else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1)
268 version = 6; /* version 6 and above support FLV1 codec */
270 version = 4; /* version 4 for mpeg audio support */
271 avio_w8(pb, version);
273 avio_wl32(pb, DUMMY_FILE_SIZE); /* dummy size
274 (will be patched if not streamed) */
276 put_swf_rect(pb, 0, width * 20, 0, height * 20);
277 if ((rate * 256LL) / rate_base >= (1<<16)) {
278 av_log(s, AV_LOG_ERROR, "Invalid (too large) frame rate %d/%d\n", rate, rate_base);
279 return AVERROR(EINVAL);
281 avio_wl16(pb, (rate * 256LL) / rate_base); /* frame rate */
282 swf->duration_pos = avio_tell(pb);
283 avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
285 /* swf v8 and later files require a file attribute tag */
287 put_swf_tag(s, TAG_FILEATTRIBUTES);
288 avio_wl32(pb, (version >= 9) << 3); /* set ActionScript v3/AVM2 flag */
292 /* define a shape with the jpeg inside */
293 if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_MJPEG || swf->video_par->codec_id == AV_CODEC_ID_PNG)) {
294 put_swf_tag(s, TAG_DEFINESHAPE);
296 avio_wl16(pb, SHAPE_ID); /* ID of shape */
297 /* bounding rectangle */
298 put_swf_rect(pb, 0, width, 0, height);
300 avio_w8(pb, 1); /* one fill style */
301 avio_w8(pb, 0x41); /* clipped bitmap fill */
302 avio_wl16(pb, BITMAP_ID); /* bitmap ID */
303 /* position of the bitmap */
304 put_swf_matrix(pb, 1 << FRAC_BITS, 0,
305 0, 1 << FRAC_BITS, 0, 0);
306 avio_w8(pb, 0); /* no line style */
309 init_put_bits(&p, buf1, sizeof(buf1));
310 put_bits(&p, 4, 1); /* one fill bit */
311 put_bits(&p, 4, 0); /* zero line bit */
313 put_bits(&p, 1, 0); /* not an edge */
314 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
315 put_bits(&p, 5, 1); /* nbits */
316 put_bits(&p, 1, 0); /* X */
317 put_bits(&p, 1, 0); /* Y */
318 put_bits(&p, 1, 1); /* set fill style 1 */
320 /* draw the rectangle ! */
321 put_swf_line_edge(&p, width, 0);
322 put_swf_line_edge(&p, 0, height);
323 put_swf_line_edge(&p, -width, 0);
324 put_swf_line_edge(&p, 0, -height);
327 put_bits(&p, 1, 0); /* not an edge */
331 avio_write(pb, buf1, put_bits_ptr(&p) - p.buf);
336 if (swf->audio_par && swf->audio_par->codec_id == AV_CODEC_ID_MP3) {
340 put_swf_tag(s, TAG_STREAMHEAD2);
341 switch(swf->audio_par->sample_rate) {
342 case 11025: v |= 1 << 2; break;
343 case 22050: v |= 2 << 2; break;
344 case 44100: v |= 3 << 2; break;
347 av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
350 v |= 0x02; /* 16 bit playback */
351 if (swf->audio_par->channels == 2)
352 v |= 0x01; /* stereo playback */
354 v |= 0x20; /* mp3 compressed */
356 avio_wl16(s->pb, swf->samples_per_frame); /* avg samples per frame */
365 static int swf_write_video(AVFormatContext *s,
366 AVCodecParameters *par, const uint8_t *buf, int size, unsigned pkt_flags)
368 SWFEncContext *swf = s->priv_data;
369 AVIOContext *pb = s->pb;
370 unsigned codec_tag = ff_codec_get_tag(ff_swf_codec_tags, par->codec_id);
372 /* Flash Player limit */
373 if (swf->swf_frame_number == 16000)
374 av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
377 if (swf->video_frame_number == 0) {
378 /* create a new video object */
379 put_swf_tag(s, TAG_VIDEOSTREAM);
380 avio_wl16(pb, VIDEO_ID);
381 swf->vframes_pos = avio_tell(pb);
382 avio_wl16(pb, 15000); /* hard flash player limit */
383 avio_wl16(pb, par->width);
384 avio_wl16(pb, par->height);
386 avio_w8(pb, codec_tag);
389 /* place the video object for the first time */
390 put_swf_tag(s, TAG_PLACEOBJECT2);
393 avio_wl16(pb, VIDEO_ID);
394 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
395 avio_wl16(pb, swf->video_frame_number);
396 avio_write(pb, "video", 5);
400 /* mark the character for update */
401 put_swf_tag(s, TAG_PLACEOBJECT2);
404 avio_wl16(pb, swf->video_frame_number);
408 /* set video frame data */
409 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
410 avio_wl16(pb, VIDEO_ID);
411 avio_wl16(pb, swf->video_frame_number++);
412 if (par->codec_id == AV_CODEC_ID_FLASHSV) {
413 /* FrameType and CodecId is needed here even if it is not documented correctly in the SWF specs */
414 int flags = codec_tag | (pkt_flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER);
417 avio_write(pb, buf, size);
419 } else if (par->codec_id == AV_CODEC_ID_MJPEG || par->codec_id == AV_CODEC_ID_PNG) {
420 if (swf->swf_frame_number > 0) {
421 /* remove the shape */
422 put_swf_tag(s, TAG_REMOVEOBJECT);
423 avio_wl16(pb, SHAPE_ID); /* shape ID */
424 avio_wl16(pb, 1); /* depth */
427 /* free the bitmap */
428 put_swf_tag(s, TAG_FREECHARACTER);
429 avio_wl16(pb, BITMAP_ID);
433 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
435 avio_wl16(pb, BITMAP_ID); /* ID of the image */
437 /* a dummy jpeg header seems to be required */
438 if (par->codec_id == AV_CODEC_ID_MJPEG)
439 avio_wb32(pb, 0xffd8ffd9);
440 /* write the jpeg/png image */
441 avio_write(pb, buf, size);
447 put_swf_tag(s, TAG_PLACEOBJECT);
448 avio_wl16(pb, SHAPE_ID); /* shape ID */
449 avio_wl16(pb, 1); /* depth */
450 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
454 swf->swf_frame_number++;
456 /* streaming sound always should be placed just before showframe tags */
457 if (swf->audio_par && av_fifo_size(swf->audio_fifo)) {
458 int frame_size = av_fifo_size(swf->audio_fifo);
459 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
460 avio_wl16(pb, swf->sound_samples);
461 avio_wl16(pb, 0); // seek samples
462 av_fifo_generic_read(swf->audio_fifo, pb, frame_size, (void*)avio_write);
466 swf->sound_samples = 0;
469 /* output the frame */
470 put_swf_tag(s, TAG_SHOWFRAME);
476 static int swf_write_audio(AVFormatContext *s,
477 AVCodecParameters *par, uint8_t *buf, int size)
479 SWFEncContext *swf = s->priv_data;
481 /* Flash Player limit */
482 if (swf->swf_frame_number == 16000)
483 av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
485 if (av_fifo_size(swf->audio_fifo) + size > AUDIO_FIFO_SIZE) {
486 av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
490 av_fifo_generic_write(swf->audio_fifo, buf, size, NULL);
491 swf->sound_samples += av_get_audio_frame_duration2(par, size);
493 /* if audio only stream make sure we add swf frames */
495 swf_write_video(s, par, 0, 0, 0);
500 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
502 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
503 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
504 return swf_write_audio(s, par, pkt->data, pkt->size);
506 return swf_write_video(s, par, pkt->data, pkt->size, pkt->flags);
509 static int swf_write_trailer(AVFormatContext *s)
511 SWFEncContext *swf = s->priv_data;
512 AVIOContext *pb = s->pb;
515 put_swf_tag(s, TAG_END);
518 /* patch file size and number of frames if not streamed */
519 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && swf->video_par) {
520 file_size = avio_tell(pb);
521 avio_seek(pb, 4, SEEK_SET);
522 avio_wl32(pb, file_size);
523 avio_seek(pb, swf->duration_pos, SEEK_SET);
524 avio_wl16(pb, swf->video_frame_number);
525 if (swf->vframes_pos) {
526 avio_seek(pb, swf->vframes_pos, SEEK_SET);
527 avio_wl16(pb, swf->video_frame_number);
529 avio_seek(pb, file_size, SEEK_SET);
534 static void swf_deinit(AVFormatContext *s)
536 SWFEncContext *swf = s->priv_data;
538 av_fifo_freep(&swf->audio_fifo);
542 AVOutputFormat ff_swf_muxer = {
544 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
545 .mime_type = "application/x-shockwave-flash",
547 .priv_data_size = sizeof(SWFEncContext),
548 .audio_codec = AV_CODEC_ID_MP3,
549 .video_codec = AV_CODEC_ID_FLV1,
550 .write_header = swf_write_header,
551 .write_packet = swf_write_packet,
552 .write_trailer = swf_write_trailer,
553 .deinit = swf_deinit,
554 .flags = AVFMT_TS_NONSTRICT,
557 #if CONFIG_AVM2_MUXER
558 AVOutputFormat ff_avm2_muxer = {
560 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash) (AVM2)"),
561 .mime_type = "application/x-shockwave-flash",
562 .priv_data_size = sizeof(SWFEncContext),
563 .audio_codec = AV_CODEC_ID_MP3,
564 .video_codec = AV_CODEC_ID_FLV1,
565 .write_header = swf_write_header,
566 .write_packet = swf_write_packet,
567 .write_trailer = swf_write_trailer,
568 .deinit = swf_deinit,
569 .flags = AVFMT_TS_NONSTRICT,