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